ip.c 20 KB

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