ip.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2011 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/net/ip.h>
  19. #include <grub/misc.h>
  20. #include <grub/net/arp.h>
  21. #include <grub/net/udp.h>
  22. #include <grub/net/ethernet.h>
  23. #include <grub/net.h>
  24. #include <grub/net/netbuff.h>
  25. #include <grub/mm.h>
  26. #include <grub/priority_queue.h>
  27. #include <grub/safemath.h>
  28. #include <grub/time.h>
  29. struct iphdr {
  30. grub_uint8_t verhdrlen;
  31. grub_uint8_t service;
  32. grub_uint16_t len;
  33. grub_uint16_t ident;
  34. grub_uint16_t frags;
  35. grub_uint8_t ttl;
  36. grub_uint8_t protocol;
  37. grub_uint16_t chksum;
  38. grub_uint32_t src;
  39. grub_uint32_t dest;
  40. } GRUB_PACKED ;
  41. enum
  42. {
  43. DONT_FRAGMENT = 0x4000,
  44. MORE_FRAGMENTS = 0x2000,
  45. OFFSET_MASK = 0x1fff
  46. };
  47. typedef grub_uint64_t ip6addr[2];
  48. struct ip6hdr {
  49. grub_uint32_t version_class_flow;
  50. grub_uint16_t len;
  51. grub_uint8_t protocol;
  52. grub_uint8_t ttl;
  53. ip6addr src;
  54. ip6addr dest;
  55. } GRUB_PACKED ;
  56. static int
  57. cmp (const void *a__, const void *b__)
  58. {
  59. struct grub_net_buff *a_ = *(struct grub_net_buff **) a__;
  60. struct grub_net_buff *b_ = *(struct grub_net_buff **) b__;
  61. struct iphdr *a = (struct iphdr *) a_->data;
  62. struct iphdr *b = (struct iphdr *) b_->data;
  63. /* We want the first elements to be on top. */
  64. if ((grub_be_to_cpu16 (a->frags) & OFFSET_MASK)
  65. < (grub_be_to_cpu16 (b->frags) & OFFSET_MASK))
  66. return +1;
  67. if ((grub_be_to_cpu16 (a->frags) & OFFSET_MASK)
  68. > (grub_be_to_cpu16 (b->frags) & OFFSET_MASK))
  69. return -1;
  70. return 0;
  71. }
  72. struct reassemble
  73. {
  74. struct reassemble *next;
  75. grub_uint32_t source;
  76. grub_uint32_t dest;
  77. grub_uint16_t id;
  78. grub_uint8_t proto;
  79. grub_uint64_t last_time;
  80. grub_priority_queue_t pq;
  81. struct grub_net_buff *asm_netbuff;
  82. grub_size_t total_len;
  83. grub_size_t cur_ptr;
  84. grub_uint8_t ttl;
  85. };
  86. static struct reassemble *reassembles;
  87. grub_uint16_t
  88. grub_net_ip_chksum (void *ipv, grub_size_t len)
  89. {
  90. grub_uint16_t *ip = (grub_uint16_t *) ipv;
  91. grub_uint32_t sum = 0;
  92. for (; len >= 2; len -= 2)
  93. {
  94. sum += grub_be_to_cpu16 (grub_get_unaligned16 (ip++));
  95. if (sum > 0xFFFF)
  96. sum -= 0xFFFF;
  97. }
  98. if (len)
  99. {
  100. sum += *((grub_uint8_t *) ip) << 8;
  101. if (sum > 0xFFFF)
  102. sum -= 0xFFFF;
  103. }
  104. if (sum >= 0xFFFF)
  105. sum -= 0xFFFF;
  106. return grub_cpu_to_be16 ((~sum) & 0x0000FFFF);
  107. }
  108. static int id = 0x2400;
  109. static grub_err_t
  110. send_fragmented (struct grub_net_network_level_interface * inf,
  111. const grub_net_network_level_address_t * target,
  112. struct grub_net_buff * nb,
  113. grub_net_ip_protocol_t proto,
  114. grub_net_link_level_address_t ll_target_addr)
  115. {
  116. grub_size_t off = 0;
  117. grub_size_t fraglen;
  118. grub_err_t err;
  119. fraglen = (inf->card->mtu - sizeof (struct iphdr)) & ~7;
  120. id++;
  121. while (nb->tail - nb->data)
  122. {
  123. grub_size_t len = fraglen;
  124. struct grub_net_buff *nb2;
  125. struct iphdr *iph;
  126. if ((grub_ssize_t) len > nb->tail - nb->data)
  127. len = nb->tail - nb->data;
  128. nb2 = grub_netbuff_alloc (fraglen + sizeof (struct iphdr)
  129. + GRUB_NET_MAX_LINK_HEADER_SIZE);
  130. if (!nb2)
  131. return grub_errno;
  132. err = grub_netbuff_reserve (nb2, GRUB_NET_MAX_LINK_HEADER_SIZE);
  133. if (err)
  134. return err;
  135. err = grub_netbuff_put (nb2, sizeof (struct iphdr));
  136. if (err)
  137. return err;
  138. iph = (struct iphdr *) nb2->data;
  139. iph->verhdrlen = ((4 << 4) | 5);
  140. iph->service = 0;
  141. iph->len = grub_cpu_to_be16 (len + sizeof (struct iphdr));
  142. iph->ident = grub_cpu_to_be16 (id);
  143. iph->frags = grub_cpu_to_be16 (off | (((grub_ssize_t) len
  144. == nb->tail - nb->data)
  145. ? 0 : MORE_FRAGMENTS));
  146. iph->ttl = 0xff;
  147. iph->protocol = proto;
  148. iph->src = inf->address.ipv4;
  149. iph->dest = target->ipv4;
  150. off += len / 8;
  151. iph->chksum = 0;
  152. iph->chksum = grub_net_ip_chksum ((void *) nb2->data, sizeof (*iph));
  153. err = grub_netbuff_put (nb2, len);
  154. if (err)
  155. return err;
  156. grub_memcpy (iph + 1, nb->data, len);
  157. err = grub_netbuff_pull (nb, len);
  158. if (err)
  159. return err;
  160. err = send_ethernet_packet (inf, nb2, ll_target_addr,
  161. GRUB_NET_ETHERTYPE_IP);
  162. if (err)
  163. return err;
  164. }
  165. return GRUB_ERR_NONE;
  166. }
  167. static grub_err_t
  168. grub_net_send_ip4_packet (struct grub_net_network_level_interface *inf,
  169. const grub_net_network_level_address_t *target,
  170. const grub_net_link_level_address_t *ll_target_addr,
  171. struct grub_net_buff *nb,
  172. grub_net_ip_protocol_t proto)
  173. {
  174. struct iphdr *iph;
  175. grub_err_t err;
  176. COMPILE_TIME_ASSERT (GRUB_NET_OUR_IPV4_HEADER_SIZE == sizeof (*iph));
  177. if (nb->tail - nb->data + sizeof (struct iphdr) > inf->card->mtu)
  178. return send_fragmented (inf, target, nb, proto, *ll_target_addr);
  179. err = grub_netbuff_push (nb, sizeof (*iph));
  180. if (err)
  181. return err;
  182. iph = (struct iphdr *) nb->data;
  183. iph->verhdrlen = ((4 << 4) | 5);
  184. iph->service = 0;
  185. iph->len = grub_cpu_to_be16 (nb->tail - nb->data);
  186. iph->ident = grub_cpu_to_be16 (++id);
  187. iph->frags = 0;
  188. iph->ttl = 0xff;
  189. iph->protocol = proto;
  190. iph->src = inf->address.ipv4;
  191. iph->dest = target->ipv4;
  192. iph->chksum = 0;
  193. iph->chksum = grub_net_ip_chksum ((void *) nb->data, sizeof (*iph));
  194. return send_ethernet_packet (inf, nb, *ll_target_addr,
  195. GRUB_NET_ETHERTYPE_IP);
  196. }
  197. static grub_err_t
  198. handle_dgram (struct grub_net_buff *nb,
  199. struct grub_net_card *card,
  200. const grub_net_link_level_address_t *source_hwaddress,
  201. const grub_net_link_level_address_t *hwaddress,
  202. grub_net_ip_protocol_t proto,
  203. const grub_net_network_level_address_t *source,
  204. const grub_net_network_level_address_t *dest,
  205. grub_uint16_t *vlantag,
  206. grub_uint8_t ttl)
  207. {
  208. struct grub_net_network_level_interface *inf = NULL;
  209. grub_err_t err;
  210. int multicast = 0;
  211. /* DHCP needs special treatment since we don't know IP yet. */
  212. {
  213. struct udphdr *udph;
  214. udph = (struct udphdr *) nb->data;
  215. if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
  216. {
  217. const struct grub_net_bootp_packet *bootp;
  218. if (udph->chksum)
  219. {
  220. grub_uint16_t chk, expected;
  221. chk = udph->chksum;
  222. udph->chksum = 0;
  223. expected = grub_net_ip_transport_checksum (nb,
  224. GRUB_NET_IP_UDP,
  225. source,
  226. dest);
  227. if (expected != chk)
  228. {
  229. grub_dprintf ("net", "Invalid UDP checksum. "
  230. "Expected %x, got %x\n",
  231. grub_be_to_cpu16 (expected),
  232. grub_be_to_cpu16 (chk));
  233. grub_netbuff_free (nb);
  234. return GRUB_ERR_NONE;
  235. }
  236. udph->chksum = chk;
  237. }
  238. err = grub_netbuff_pull (nb, sizeof (*udph));
  239. if (err)
  240. {
  241. grub_netbuff_free (nb);
  242. return err;
  243. }
  244. bootp = (const struct grub_net_bootp_packet *) nb->data;
  245. FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
  246. if (inf->card == card
  247. && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
  248. && inf->hwaddress.type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
  249. && grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
  250. sizeof (inf->hwaddress.mac)) == 0)
  251. {
  252. grub_net_process_dhcp (nb, inf);
  253. grub_netbuff_free (nb);
  254. return GRUB_ERR_NONE;
  255. }
  256. grub_netbuff_free (nb);
  257. return GRUB_ERR_NONE;
  258. }
  259. }
  260. FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
  261. {
  262. if (inf->card == card
  263. && grub_net_addr_cmp (&inf->address, dest) == 0
  264. && grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
  265. break;
  266. /* Verify vlantag id */
  267. if (inf->card == card && inf->vlantag != *vlantag)
  268. {
  269. grub_dprintf ("net", "invalid vlantag! %x != %x\n",
  270. inf->vlantag, *vlantag);
  271. break;
  272. }
  273. /* Solicited node multicast. */
  274. if (inf->card == card
  275. && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
  276. && dest->type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
  277. && dest->ipv6[0] == grub_be_to_cpu64_compile_time (0xff02ULL << 48)
  278. && dest->ipv6[1] == (grub_be_to_cpu64_compile_time (0x01ff000000ULL)
  279. | (inf->address.ipv6[1]
  280. & grub_be_to_cpu64_compile_time (0xffffff)))
  281. && hwaddress->type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
  282. && hwaddress->mac[0] == 0x33 && hwaddress->mac[1] == 0x33
  283. && hwaddress->mac[2] == 0xff
  284. && hwaddress->mac[3] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
  285. >> 16) & 0xff)
  286. && hwaddress->mac[4] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
  287. >> 8) & 0xff)
  288. && hwaddress->mac[5] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
  289. >> 0) & 0xff))
  290. {
  291. multicast = 1;
  292. break;
  293. }
  294. }
  295. if (!inf && !(dest->type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
  296. && dest->ipv6[0] == grub_be_to_cpu64_compile_time (0xff02ULL
  297. << 48)
  298. && dest->ipv6[1] == grub_be_to_cpu64_compile_time (1)))
  299. {
  300. grub_netbuff_free (nb);
  301. return GRUB_ERR_NONE;
  302. }
  303. if (multicast)
  304. inf = NULL;
  305. switch (proto)
  306. {
  307. case GRUB_NET_IP_UDP:
  308. return grub_net_recv_udp_packet (nb, inf, source);
  309. case GRUB_NET_IP_TCP:
  310. return grub_net_recv_tcp_packet (nb, inf, source);
  311. case GRUB_NET_IP_ICMP:
  312. return grub_net_recv_icmp_packet (nb, inf, source_hwaddress, source);
  313. case GRUB_NET_IP_ICMPV6:
  314. return grub_net_recv_icmp6_packet (nb, card, inf, source_hwaddress,
  315. source, dest, ttl);
  316. default:
  317. grub_netbuff_free (nb);
  318. break;
  319. }
  320. return GRUB_ERR_NONE;
  321. }
  322. static void
  323. free_rsm (struct reassemble *rsm)
  324. {
  325. struct grub_net_buff **nb;
  326. while ((nb = grub_priority_queue_top (rsm->pq)))
  327. {
  328. grub_netbuff_free (*nb);
  329. grub_priority_queue_pop (rsm->pq);
  330. }
  331. grub_netbuff_free (rsm->asm_netbuff);
  332. grub_priority_queue_destroy (rsm->pq);
  333. grub_free (rsm);
  334. }
  335. static void
  336. free_old_fragments (void)
  337. {
  338. struct reassemble *rsm, **prev;
  339. grub_uint64_t limit_time = grub_get_time_ms ();
  340. limit_time = (limit_time > 90000) ? limit_time - 90000 : 0;
  341. for (prev = &reassembles, rsm = *prev; rsm; rsm = *prev)
  342. if (rsm->last_time < limit_time)
  343. {
  344. *prev = rsm->next;
  345. free_rsm (rsm);
  346. }
  347. else
  348. {
  349. prev = &rsm->next;
  350. }
  351. }
  352. static grub_err_t
  353. grub_net_recv_ip4_packets (struct grub_net_buff *nb,
  354. struct grub_net_card *card,
  355. const grub_net_link_level_address_t *hwaddress,
  356. const grub_net_link_level_address_t *src_hwaddress,
  357. grub_uint16_t *vlantag)
  358. {
  359. struct iphdr *iph = (struct iphdr *) nb->data;
  360. grub_err_t err;
  361. struct reassemble *rsm, **prev;
  362. if ((iph->verhdrlen >> 4) != 4)
  363. {
  364. grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
  365. grub_netbuff_free (nb);
  366. return GRUB_ERR_NONE;
  367. }
  368. if ((iph->verhdrlen & 0xf) < 5)
  369. {
  370. grub_dprintf ("net", "IP header too short: %d\n",
  371. (iph->verhdrlen & 0xf));
  372. grub_netbuff_free (nb);
  373. return GRUB_ERR_NONE;
  374. }
  375. if (nb->tail - nb->data < (grub_ssize_t) ((iph->verhdrlen & 0xf)
  376. * sizeof (grub_uint32_t)))
  377. {
  378. grub_dprintf ("net", "IP packet too short: %" PRIdGRUB_SSIZE "\n",
  379. (grub_ssize_t) (nb->tail - nb->data));
  380. grub_netbuff_free (nb);
  381. return GRUB_ERR_NONE;
  382. }
  383. /* Check size. */
  384. {
  385. grub_size_t expected_size = grub_be_to_cpu16 (iph->len);
  386. grub_size_t actual_size = (nb->tail - nb->data);
  387. if (actual_size > expected_size)
  388. {
  389. err = grub_netbuff_unput (nb, actual_size - expected_size);
  390. if (err)
  391. {
  392. grub_netbuff_free (nb);
  393. return err;
  394. }
  395. }
  396. if (actual_size < expected_size)
  397. {
  398. grub_dprintf ("net", "Cut IP packet actual: %" PRIuGRUB_SIZE
  399. ", expected %" PRIuGRUB_SIZE "\n", actual_size,
  400. expected_size);
  401. grub_netbuff_free (nb);
  402. return GRUB_ERR_NONE;
  403. }
  404. }
  405. /* Unfragmented packet. Good. */
  406. if (((grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS) == 0)
  407. && (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK) == 0)
  408. {
  409. grub_net_network_level_address_t source;
  410. grub_net_network_level_address_t dest;
  411. err = grub_netbuff_pull (nb, ((iph->verhdrlen & 0xf)
  412. * sizeof (grub_uint32_t)));
  413. if (err)
  414. {
  415. grub_netbuff_free (nb);
  416. return err;
  417. }
  418. source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  419. source.ipv4 = iph->src;
  420. dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  421. dest.ipv4 = iph->dest;
  422. return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
  423. &source, &dest, vlantag, iph->ttl);
  424. }
  425. for (prev = &reassembles, rsm = *prev; rsm; prev = &rsm->next, rsm = *prev)
  426. if (rsm->source == iph->src && rsm->dest == iph->dest
  427. && rsm->id == iph->ident && rsm->proto == iph->protocol)
  428. break;
  429. if (!rsm)
  430. {
  431. rsm = grub_malloc (sizeof (*rsm));
  432. if (!rsm)
  433. return grub_errno;
  434. rsm->source = iph->src;
  435. rsm->dest = iph->dest;
  436. rsm->id = iph->ident;
  437. rsm->proto = iph->protocol;
  438. rsm->next = reassembles;
  439. reassembles = rsm;
  440. prev = &reassembles;
  441. rsm->pq = grub_priority_queue_new (sizeof (struct grub_net_buff **), cmp);
  442. if (!rsm->pq)
  443. {
  444. grub_free (rsm);
  445. return grub_errno;
  446. }
  447. rsm->asm_netbuff = 0;
  448. rsm->total_len = 0;
  449. rsm->cur_ptr = 0;
  450. rsm->ttl = 0xff;
  451. }
  452. if (rsm->ttl > iph->ttl)
  453. rsm->ttl = iph->ttl;
  454. rsm->last_time = grub_get_time_ms ();
  455. free_old_fragments ();
  456. err = grub_priority_queue_push (rsm->pq, &nb);
  457. if (err)
  458. return err;
  459. if (!(grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS))
  460. {
  461. rsm->total_len = (8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  462. + (nb->tail - nb->data));
  463. if (grub_sub (rsm->total_len, (iph->verhdrlen & 0xf) * sizeof (grub_uint32_t),
  464. &rsm->total_len))
  465. {
  466. grub_dprintf ("net", "IP reassembly size underflow\n");
  467. return GRUB_ERR_NONE;
  468. }
  469. rsm->asm_netbuff = grub_netbuff_alloc (rsm->total_len);
  470. if (!rsm->asm_netbuff)
  471. {
  472. *prev = rsm->next;
  473. free_rsm (rsm);
  474. return grub_errno;
  475. }
  476. }
  477. if (!rsm->asm_netbuff)
  478. return GRUB_ERR_NONE;
  479. while (1)
  480. {
  481. struct grub_net_buff **nb_top_p, *nb_top;
  482. grub_size_t copy;
  483. grub_size_t res_len;
  484. struct grub_net_buff *ret;
  485. grub_net_ip_protocol_t proto;
  486. grub_uint32_t src;
  487. grub_uint32_t dst;
  488. grub_net_network_level_address_t source;
  489. grub_net_network_level_address_t dest;
  490. grub_uint8_t ttl;
  491. nb_top_p = grub_priority_queue_top (rsm->pq);
  492. if (!nb_top_p)
  493. return GRUB_ERR_NONE;
  494. nb_top = *nb_top_p;
  495. grub_priority_queue_pop (rsm->pq);
  496. iph = (struct iphdr *) nb_top->data;
  497. err = grub_netbuff_pull (nb_top, ((iph->verhdrlen & 0xf)
  498. * sizeof (grub_uint32_t)));
  499. if (err)
  500. {
  501. grub_netbuff_free (nb_top);
  502. return err;
  503. }
  504. if (rsm->cur_ptr < (grub_size_t) 8 * (grub_be_to_cpu16 (iph->frags)
  505. & OFFSET_MASK))
  506. {
  507. grub_netbuff_free (nb_top);
  508. return GRUB_ERR_NONE;
  509. }
  510. rsm->cur_ptr = (8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  511. + (nb_top->tail - nb_top->head));
  512. if ((grub_size_t) 8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  513. >= rsm->total_len)
  514. {
  515. grub_netbuff_free (nb_top);
  516. continue;
  517. }
  518. copy = nb_top->tail - nb_top->data;
  519. if (rsm->total_len - 8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  520. < copy)
  521. copy = rsm->total_len - 8 * (grub_be_to_cpu16 (iph->frags)
  522. & OFFSET_MASK);
  523. grub_memcpy (&rsm->asm_netbuff->data[8 * (grub_be_to_cpu16 (iph->frags)
  524. & OFFSET_MASK)],
  525. nb_top->data, copy);
  526. if ((grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS))
  527. {
  528. grub_netbuff_free (nb_top);
  529. continue;
  530. }
  531. grub_netbuff_free (nb_top);
  532. ret = rsm->asm_netbuff;
  533. proto = rsm->proto;
  534. src = rsm->source;
  535. dst = rsm->dest;
  536. ttl = rsm->ttl;
  537. rsm->asm_netbuff = 0;
  538. res_len = rsm->total_len;
  539. *prev = rsm->next;
  540. free_rsm (rsm);
  541. if (grub_netbuff_put (ret, res_len))
  542. {
  543. grub_netbuff_free (ret);
  544. return GRUB_ERR_NONE;
  545. }
  546. source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  547. source.ipv4 = src;
  548. dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  549. dest.ipv4 = dst;
  550. return handle_dgram (ret, card, src_hwaddress,
  551. hwaddress, proto, &source, &dest, vlantag,
  552. ttl);
  553. }
  554. }
  555. static grub_err_t
  556. grub_net_send_ip6_packet (struct grub_net_network_level_interface *inf,
  557. const grub_net_network_level_address_t *target,
  558. const grub_net_link_level_address_t *ll_target_addr,
  559. struct grub_net_buff *nb,
  560. grub_net_ip_protocol_t proto)
  561. {
  562. struct ip6hdr *iph;
  563. grub_err_t err;
  564. COMPILE_TIME_ASSERT (GRUB_NET_OUR_IPV6_HEADER_SIZE == sizeof (*iph));
  565. if (nb->tail - nb->data + sizeof (struct iphdr) > inf->card->mtu)
  566. return grub_error (GRUB_ERR_NET_PACKET_TOO_BIG, "packet too big");
  567. err = grub_netbuff_push (nb, sizeof (*iph));
  568. if (err)
  569. return err;
  570. iph = (struct ip6hdr *) nb->data;
  571. iph->version_class_flow = grub_cpu_to_be32_compile_time ((6 << 28));
  572. iph->len = grub_cpu_to_be16 (nb->tail - nb->data - sizeof (*iph));
  573. iph->protocol = proto;
  574. iph->ttl = 0xff;
  575. grub_memcpy (&iph->src, inf->address.ipv6, sizeof (iph->src));
  576. grub_memcpy (&iph->dest, target->ipv6, sizeof (iph->dest));
  577. return send_ethernet_packet (inf, nb, *ll_target_addr,
  578. GRUB_NET_ETHERTYPE_IP6);
  579. }
  580. grub_err_t
  581. grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
  582. const grub_net_network_level_address_t *target,
  583. const grub_net_link_level_address_t *ll_target_addr,
  584. struct grub_net_buff *nb,
  585. grub_net_ip_protocol_t proto)
  586. {
  587. switch (target->type)
  588. {
  589. case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
  590. return grub_net_send_ip4_packet (inf, target, ll_target_addr, nb, proto);
  591. case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
  592. return grub_net_send_ip6_packet (inf, target, ll_target_addr, nb, proto);
  593. default:
  594. return grub_error (GRUB_ERR_BUG, "not an IP");
  595. }
  596. }
  597. static grub_err_t
  598. grub_net_recv_ip6_packets (struct grub_net_buff *nb,
  599. struct grub_net_card *card,
  600. const grub_net_link_level_address_t *hwaddress,
  601. const grub_net_link_level_address_t *src_hwaddress,
  602. grub_uint16_t *vlantag)
  603. {
  604. struct ip6hdr *iph = (struct ip6hdr *) nb->data;
  605. grub_err_t err;
  606. grub_net_network_level_address_t source;
  607. grub_net_network_level_address_t dest;
  608. if (nb->tail - nb->data < (grub_ssize_t) sizeof (*iph))
  609. {
  610. grub_dprintf ("net", "IP packet too short: %" PRIdGRUB_SSIZE "\n",
  611. (grub_ssize_t) (nb->tail - nb->data));
  612. grub_netbuff_free (nb);
  613. return GRUB_ERR_NONE;
  614. }
  615. err = grub_netbuff_pull (nb, sizeof (*iph));
  616. if (err)
  617. {
  618. grub_netbuff_free (nb);
  619. return err;
  620. }
  621. /* Check size. */
  622. {
  623. grub_size_t expected_size = grub_be_to_cpu16 (iph->len);
  624. grub_size_t actual_size = (nb->tail - nb->data);
  625. if (actual_size > expected_size)
  626. {
  627. err = grub_netbuff_unput (nb, actual_size - expected_size);
  628. if (err)
  629. {
  630. grub_netbuff_free (nb);
  631. return err;
  632. }
  633. }
  634. if (actual_size < expected_size)
  635. {
  636. grub_dprintf ("net", "Cut IP packet actual: %" PRIuGRUB_SIZE
  637. ", expected %" PRIuGRUB_SIZE "\n", actual_size,
  638. expected_size);
  639. grub_netbuff_free (nb);
  640. return GRUB_ERR_NONE;
  641. }
  642. }
  643. source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
  644. dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
  645. grub_memcpy (source.ipv6, &iph->src, sizeof (source.ipv6));
  646. grub_memcpy (dest.ipv6, &iph->dest, sizeof (dest.ipv6));
  647. return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
  648. &source, &dest, vlantag, iph->ttl);
  649. }
  650. grub_err_t
  651. grub_net_recv_ip_packets (struct grub_net_buff *nb,
  652. struct grub_net_card *card,
  653. const grub_net_link_level_address_t *hwaddress,
  654. const grub_net_link_level_address_t *src_hwaddress,
  655. grub_uint16_t *vlantag)
  656. {
  657. struct iphdr *iph = (struct iphdr *) nb->data;
  658. if ((iph->verhdrlen >> 4) == 4)
  659. return grub_net_recv_ip4_packets (nb, card, hwaddress, src_hwaddress,
  660. vlantag);
  661. if ((iph->verhdrlen >> 4) == 6)
  662. return grub_net_recv_ip6_packets (nb, card, hwaddress, src_hwaddress,
  663. vlantag);
  664. grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
  665. grub_netbuff_free (nb);
  666. return GRUB_ERR_NONE;
  667. }