ip.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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_uint8_t ttl)
  205. {
  206. struct grub_net_network_level_interface *inf = NULL;
  207. grub_err_t err;
  208. int multicast = 0;
  209. /* DHCP needs special treatment since we don't know IP yet. */
  210. {
  211. struct udphdr *udph;
  212. udph = (struct udphdr *) nb->data;
  213. if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
  214. {
  215. const struct grub_net_bootp_packet *bootp;
  216. if (udph->chksum)
  217. {
  218. grub_uint16_t chk, expected;
  219. chk = udph->chksum;
  220. udph->chksum = 0;
  221. expected = grub_net_ip_transport_checksum (nb,
  222. GRUB_NET_IP_UDP,
  223. source,
  224. dest);
  225. if (expected != chk)
  226. {
  227. grub_dprintf ("net", "Invalid UDP checksum. "
  228. "Expected %x, got %x\n",
  229. grub_be_to_cpu16 (expected),
  230. grub_be_to_cpu16 (chk));
  231. grub_netbuff_free (nb);
  232. return GRUB_ERR_NONE;
  233. }
  234. udph->chksum = chk;
  235. }
  236. err = grub_netbuff_pull (nb, sizeof (*udph));
  237. if (err)
  238. {
  239. grub_netbuff_free (nb);
  240. return err;
  241. }
  242. bootp = (const struct grub_net_bootp_packet *) nb->data;
  243. FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
  244. if (inf->card == card
  245. && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
  246. && inf->hwaddress.type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
  247. && grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
  248. sizeof (inf->hwaddress.mac)) == 0)
  249. {
  250. grub_net_process_dhcp (nb, inf->card);
  251. grub_netbuff_free (nb);
  252. return GRUB_ERR_NONE;
  253. }
  254. grub_netbuff_free (nb);
  255. return GRUB_ERR_NONE;
  256. }
  257. }
  258. FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
  259. {
  260. if (inf->card == card
  261. && grub_net_addr_cmp (&inf->address, dest) == 0
  262. && grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
  263. break;
  264. /* Solicited node multicast. */
  265. if (inf->card == card
  266. && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
  267. && dest->type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
  268. && dest->ipv6[0] == grub_be_to_cpu64_compile_time (0xff02ULL << 48)
  269. && dest->ipv6[1] == (grub_be_to_cpu64_compile_time (0x01ff000000ULL)
  270. | (inf->address.ipv6[1]
  271. & grub_be_to_cpu64_compile_time (0xffffff)))
  272. && hwaddress->type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
  273. && hwaddress->mac[0] == 0x33 && hwaddress->mac[1] == 0x33
  274. && hwaddress->mac[2] == 0xff
  275. && hwaddress->mac[3] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
  276. >> 16) & 0xff)
  277. && hwaddress->mac[4] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
  278. >> 8) & 0xff)
  279. && hwaddress->mac[5] == ((grub_be_to_cpu64 (inf->address.ipv6[1])
  280. >> 0) & 0xff))
  281. {
  282. multicast = 1;
  283. break;
  284. }
  285. }
  286. if (!inf && !(dest->type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
  287. && dest->ipv6[0] == grub_be_to_cpu64_compile_time (0xff02ULL
  288. << 48)
  289. && dest->ipv6[1] == grub_be_to_cpu64_compile_time (1)))
  290. {
  291. grub_netbuff_free (nb);
  292. return GRUB_ERR_NONE;
  293. }
  294. if (multicast)
  295. inf = NULL;
  296. switch (proto)
  297. {
  298. case GRUB_NET_IP_UDP:
  299. return grub_net_recv_udp_packet (nb, inf, source);
  300. case GRUB_NET_IP_TCP:
  301. return grub_net_recv_tcp_packet (nb, inf, source);
  302. case GRUB_NET_IP_ICMP:
  303. return grub_net_recv_icmp_packet (nb, inf, source_hwaddress, source);
  304. case GRUB_NET_IP_ICMPV6:
  305. return grub_net_recv_icmp6_packet (nb, card, inf, source_hwaddress,
  306. source, dest, ttl);
  307. default:
  308. grub_netbuff_free (nb);
  309. break;
  310. }
  311. return GRUB_ERR_NONE;
  312. }
  313. static void
  314. free_rsm (struct reassemble *rsm)
  315. {
  316. struct grub_net_buff **nb;
  317. while ((nb = grub_priority_queue_top (rsm->pq)))
  318. {
  319. grub_netbuff_free (*nb);
  320. grub_priority_queue_pop (rsm->pq);
  321. }
  322. grub_netbuff_free (rsm->asm_netbuff);
  323. grub_priority_queue_destroy (rsm->pq);
  324. grub_free (rsm);
  325. }
  326. static void
  327. free_old_fragments (void)
  328. {
  329. struct reassemble *rsm, **prev;
  330. grub_uint64_t limit_time = grub_get_time_ms () - 90000;
  331. for (prev = &reassembles, rsm = *prev; rsm; rsm = *prev)
  332. if (rsm->last_time < limit_time)
  333. {
  334. *prev = rsm->next;
  335. free_rsm (rsm);
  336. }
  337. else
  338. {
  339. prev = &rsm->next;
  340. }
  341. }
  342. static grub_err_t
  343. grub_net_recv_ip4_packets (struct grub_net_buff *nb,
  344. struct grub_net_card *card,
  345. const grub_net_link_level_address_t *hwaddress,
  346. const grub_net_link_level_address_t *src_hwaddress)
  347. {
  348. struct iphdr *iph = (struct iphdr *) nb->data;
  349. grub_err_t err;
  350. struct reassemble *rsm, **prev;
  351. if ((iph->verhdrlen >> 4) != 4)
  352. {
  353. grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
  354. grub_netbuff_free (nb);
  355. return GRUB_ERR_NONE;
  356. }
  357. if ((iph->verhdrlen & 0xf) < 5)
  358. {
  359. grub_dprintf ("net", "IP header too short: %d\n",
  360. (iph->verhdrlen & 0xf));
  361. grub_netbuff_free (nb);
  362. return GRUB_ERR_NONE;
  363. }
  364. if (nb->tail - nb->data < (grub_ssize_t) ((iph->verhdrlen & 0xf)
  365. * sizeof (grub_uint32_t)))
  366. {
  367. grub_dprintf ("net", "IP packet too short: %" PRIdGRUB_SSIZE "\n",
  368. (grub_ssize_t) (nb->tail - nb->data));
  369. grub_netbuff_free (nb);
  370. return GRUB_ERR_NONE;
  371. }
  372. /* Check size. */
  373. {
  374. grub_size_t expected_size = grub_be_to_cpu16 (iph->len);
  375. grub_size_t actual_size = (nb->tail - nb->data);
  376. if (actual_size > expected_size)
  377. {
  378. err = grub_netbuff_unput (nb, actual_size - expected_size);
  379. if (err)
  380. {
  381. grub_netbuff_free (nb);
  382. return err;
  383. }
  384. }
  385. if (actual_size < expected_size)
  386. {
  387. grub_dprintf ("net", "Cut IP packet actual: %" PRIuGRUB_SIZE
  388. ", expected %" PRIuGRUB_SIZE "\n", actual_size,
  389. expected_size);
  390. grub_netbuff_free (nb);
  391. return GRUB_ERR_NONE;
  392. }
  393. }
  394. /* Unfragmented packet. Good. */
  395. if (((grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS) == 0)
  396. && (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK) == 0)
  397. {
  398. grub_net_network_level_address_t source;
  399. grub_net_network_level_address_t dest;
  400. err = grub_netbuff_pull (nb, ((iph->verhdrlen & 0xf)
  401. * sizeof (grub_uint32_t)));
  402. if (err)
  403. {
  404. grub_netbuff_free (nb);
  405. return err;
  406. }
  407. source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  408. source.ipv4 = iph->src;
  409. dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  410. dest.ipv4 = iph->dest;
  411. return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
  412. &source, &dest, iph->ttl);
  413. }
  414. for (prev = &reassembles, rsm = *prev; rsm; prev = &rsm->next, rsm = *prev)
  415. if (rsm->source == iph->src && rsm->dest == iph->dest
  416. && rsm->id == iph->ident && rsm->proto == iph->protocol)
  417. break;
  418. if (!rsm)
  419. {
  420. rsm = grub_malloc (sizeof (*rsm));
  421. if (!rsm)
  422. return grub_errno;
  423. rsm->source = iph->src;
  424. rsm->dest = iph->dest;
  425. rsm->id = iph->ident;
  426. rsm->proto = iph->protocol;
  427. rsm->next = reassembles;
  428. reassembles = rsm;
  429. prev = &reassembles;
  430. rsm->pq = grub_priority_queue_new (sizeof (struct grub_net_buff **), cmp);
  431. if (!rsm->pq)
  432. {
  433. grub_free (rsm);
  434. return grub_errno;
  435. }
  436. rsm->asm_netbuff = 0;
  437. rsm->total_len = 0;
  438. rsm->cur_ptr = 0;
  439. rsm->ttl = 0xff;
  440. }
  441. if (rsm->ttl > iph->ttl)
  442. rsm->ttl = iph->ttl;
  443. rsm->last_time = grub_get_time_ms ();
  444. free_old_fragments ();
  445. err = grub_priority_queue_push (rsm->pq, &nb);
  446. if (err)
  447. return err;
  448. if (!(grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS))
  449. {
  450. rsm->total_len = (8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  451. + (nb->tail - nb->data));
  452. rsm->total_len -= ((iph->verhdrlen & 0xf) * sizeof (grub_uint32_t));
  453. rsm->asm_netbuff = grub_netbuff_alloc (rsm->total_len);
  454. if (!rsm->asm_netbuff)
  455. {
  456. *prev = rsm->next;
  457. free_rsm (rsm);
  458. return grub_errno;
  459. }
  460. }
  461. if (!rsm->asm_netbuff)
  462. return GRUB_ERR_NONE;
  463. while (1)
  464. {
  465. struct grub_net_buff **nb_top_p, *nb_top;
  466. grub_size_t copy;
  467. grub_size_t res_len;
  468. struct grub_net_buff *ret;
  469. grub_net_ip_protocol_t proto;
  470. grub_uint32_t src;
  471. grub_uint32_t dst;
  472. grub_net_network_level_address_t source;
  473. grub_net_network_level_address_t dest;
  474. grub_uint8_t ttl;
  475. nb_top_p = grub_priority_queue_top (rsm->pq);
  476. if (!nb_top_p)
  477. return GRUB_ERR_NONE;
  478. nb_top = *nb_top_p;
  479. grub_priority_queue_pop (rsm->pq);
  480. iph = (struct iphdr *) nb_top->data;
  481. err = grub_netbuff_pull (nb_top, ((iph->verhdrlen & 0xf)
  482. * sizeof (grub_uint32_t)));
  483. if (err)
  484. {
  485. grub_netbuff_free (nb_top);
  486. return err;
  487. }
  488. if (rsm->cur_ptr < (grub_size_t) 8 * (grub_be_to_cpu16 (iph->frags)
  489. & OFFSET_MASK))
  490. {
  491. grub_netbuff_free (nb_top);
  492. return GRUB_ERR_NONE;
  493. }
  494. rsm->cur_ptr = (8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  495. + (nb_top->tail - nb_top->head));
  496. if ((grub_size_t) 8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  497. >= rsm->total_len)
  498. {
  499. grub_netbuff_free (nb_top);
  500. continue;
  501. }
  502. copy = nb_top->tail - nb_top->data;
  503. if (rsm->total_len - 8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
  504. < copy)
  505. copy = rsm->total_len - 8 * (grub_be_to_cpu16 (iph->frags)
  506. & OFFSET_MASK);
  507. grub_memcpy (&rsm->asm_netbuff->data[8 * (grub_be_to_cpu16 (iph->frags)
  508. & OFFSET_MASK)],
  509. nb_top->data, copy);
  510. if ((grub_be_to_cpu16 (iph->frags) & MORE_FRAGMENTS))
  511. {
  512. grub_netbuff_free (nb_top);
  513. continue;
  514. }
  515. grub_netbuff_free (nb_top);
  516. ret = rsm->asm_netbuff;
  517. proto = rsm->proto;
  518. src = rsm->source;
  519. dst = rsm->dest;
  520. ttl = rsm->ttl;
  521. rsm->asm_netbuff = 0;
  522. res_len = rsm->total_len;
  523. *prev = rsm->next;
  524. free_rsm (rsm);
  525. if (grub_netbuff_put (ret, res_len))
  526. {
  527. grub_netbuff_free (ret);
  528. return GRUB_ERR_NONE;
  529. }
  530. source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  531. source.ipv4 = src;
  532. dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
  533. dest.ipv4 = dst;
  534. return handle_dgram (ret, card, src_hwaddress,
  535. hwaddress, proto, &source, &dest,
  536. ttl);
  537. }
  538. }
  539. static grub_err_t
  540. grub_net_send_ip6_packet (struct grub_net_network_level_interface *inf,
  541. const grub_net_network_level_address_t *target,
  542. const grub_net_link_level_address_t *ll_target_addr,
  543. struct grub_net_buff *nb,
  544. grub_net_ip_protocol_t proto)
  545. {
  546. struct ip6hdr *iph;
  547. grub_err_t err;
  548. COMPILE_TIME_ASSERT (GRUB_NET_OUR_IPV6_HEADER_SIZE == sizeof (*iph));
  549. if (nb->tail - nb->data + sizeof (struct iphdr) > inf->card->mtu)
  550. return grub_error (GRUB_ERR_NET_PACKET_TOO_BIG, "packet too big");
  551. err = grub_netbuff_push (nb, sizeof (*iph));
  552. if (err)
  553. return err;
  554. iph = (struct ip6hdr *) nb->data;
  555. iph->version_class_flow = grub_cpu_to_be32_compile_time ((6 << 28));
  556. iph->len = grub_cpu_to_be16 (nb->tail - nb->data - sizeof (*iph));
  557. iph->protocol = proto;
  558. iph->ttl = 0xff;
  559. grub_memcpy (&iph->src, inf->address.ipv6, sizeof (iph->src));
  560. grub_memcpy (&iph->dest, target->ipv6, sizeof (iph->dest));
  561. return send_ethernet_packet (inf, nb, *ll_target_addr,
  562. GRUB_NET_ETHERTYPE_IP6);
  563. }
  564. grub_err_t
  565. grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
  566. const grub_net_network_level_address_t *target,
  567. const grub_net_link_level_address_t *ll_target_addr,
  568. struct grub_net_buff *nb,
  569. grub_net_ip_protocol_t proto)
  570. {
  571. switch (target->type)
  572. {
  573. case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
  574. return grub_net_send_ip4_packet (inf, target, ll_target_addr, nb, proto);
  575. case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
  576. return grub_net_send_ip6_packet (inf, target, ll_target_addr, nb, proto);
  577. default:
  578. return grub_error (GRUB_ERR_BUG, "not an IP");
  579. }
  580. }
  581. static grub_err_t
  582. grub_net_recv_ip6_packets (struct grub_net_buff *nb,
  583. struct grub_net_card *card,
  584. const grub_net_link_level_address_t *hwaddress,
  585. const grub_net_link_level_address_t *src_hwaddress)
  586. {
  587. struct ip6hdr *iph = (struct ip6hdr *) nb->data;
  588. grub_err_t err;
  589. grub_net_network_level_address_t source;
  590. grub_net_network_level_address_t dest;
  591. if (nb->tail - nb->data < (grub_ssize_t) sizeof (*iph))
  592. {
  593. grub_dprintf ("net", "IP packet too short: %" PRIdGRUB_SSIZE "\n",
  594. (grub_ssize_t) (nb->tail - nb->data));
  595. grub_netbuff_free (nb);
  596. return GRUB_ERR_NONE;
  597. }
  598. err = grub_netbuff_pull (nb, sizeof (*iph));
  599. if (err)
  600. {
  601. grub_netbuff_free (nb);
  602. return err;
  603. }
  604. /* Check size. */
  605. {
  606. grub_size_t expected_size = grub_be_to_cpu16 (iph->len);
  607. grub_size_t actual_size = (nb->tail - nb->data);
  608. if (actual_size > expected_size)
  609. {
  610. err = grub_netbuff_unput (nb, actual_size - expected_size);
  611. if (err)
  612. {
  613. grub_netbuff_free (nb);
  614. return err;
  615. }
  616. }
  617. if (actual_size < expected_size)
  618. {
  619. grub_dprintf ("net", "Cut IP packet actual: %" PRIuGRUB_SIZE
  620. ", expected %" PRIuGRUB_SIZE "\n", actual_size,
  621. expected_size);
  622. grub_netbuff_free (nb);
  623. return GRUB_ERR_NONE;
  624. }
  625. }
  626. source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
  627. dest.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
  628. grub_memcpy (source.ipv6, &iph->src, sizeof (source.ipv6));
  629. grub_memcpy (dest.ipv6, &iph->dest, sizeof (dest.ipv6));
  630. return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
  631. &source, &dest, iph->ttl);
  632. }
  633. grub_err_t
  634. grub_net_recv_ip_packets (struct grub_net_buff *nb,
  635. struct grub_net_card *card,
  636. const grub_net_link_level_address_t *hwaddress,
  637. const grub_net_link_level_address_t *src_hwaddress)
  638. {
  639. struct iphdr *iph = (struct iphdr *) nb->data;
  640. if ((iph->verhdrlen >> 4) == 4)
  641. return grub_net_recv_ip4_packets (nb, card, hwaddress, src_hwaddress);
  642. if ((iph->verhdrlen >> 4) == 6)
  643. return grub_net_recv_ip6_packets (nb, card, hwaddress, src_hwaddress);
  644. grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
  645. grub_netbuff_free (nb);
  646. return GRUB_ERR_NONE;
  647. }