ipv6_input.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /****************************************************************************
  2. * net/devif/ipv6_input.c
  3. * Device driver IPv6 packet receipt interface
  4. *
  5. * Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Adapted for NuttX from logic in uIP which also has a BSD-like license:
  9. *
  10. * uIP is an implementation of the TCP/IP protocol stack intended for
  11. * small 8-bit and 16-bit microcontrollers.
  12. *
  13. * uIP provides the necessary protocols for Internet communication,
  14. * with a very small code footprint and RAM requirements - the uIP
  15. * code size is on the order of a few kilobytes and RAM usage is on
  16. * the order of a few hundred bytes.
  17. *
  18. * Original author Adam Dunkels <adam@dunkels.com>
  19. * Copyright () 2001-2003, Adam Dunkels.
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * 1. Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. The name of the author may not be used to endorse or promote
  32. * products derived from this software without specific prior
  33. * written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  36. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  37. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  39. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  41. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  42. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  43. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  45. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ****************************************************************************/
  48. /****************************************************************************
  49. * uIP is a small implementation of the IP, UDP and TCP protocols (as
  50. * well as some basic ICMP stuff). The implementation couples the IP,
  51. * UDP, TCP and the application layers very tightly. To keep the size
  52. * of the compiled code down, this code frequently uses the goto
  53. * statement. While it would be possible to break the ipv6_input()
  54. * function into many smaller functions, this would increase the code
  55. * size because of the overhead of parameter passing and the fact that
  56. * the optimizer would not be as efficient.
  57. *
  58. * The principle is that we have a small buffer, called the d_buf,
  59. * in which the device driver puts an incoming packet. The TCP/IP
  60. * stack parses the headers in the packet, and calls the
  61. * application. If the remote host has sent data to the application,
  62. * this data is present in the d_buf and the application read the
  63. * data from there. It is up to the application to put this data into
  64. * a byte stream if needed. The application will not be fed with data
  65. * that is out of sequence.
  66. *
  67. * If the application wishes to send data to the peer, it should put
  68. * its data into the d_buf. The d_appdata pointer points to the
  69. * first available byte. The TCP/IP stack will calculate the
  70. * checksums, and fill in the necessary header fields and finally send
  71. * the packet back to the peer.
  72. *
  73. ****************************************************************************/
  74. /****************************************************************************
  75. * Included Files
  76. ****************************************************************************/
  77. #include <nuttx/config.h>
  78. #ifdef CONFIG_NET_IPv6
  79. #include <sys/ioctl.h>
  80. #include <stdint.h>
  81. #include <stdbool.h>
  82. #include <debug.h>
  83. #include <string.h>
  84. #include <net/if.h>
  85. #include <nuttx/net/netconfig.h>
  86. #include <nuttx/net/netdev.h>
  87. #include <nuttx/net/netstats.h>
  88. #include <nuttx/net/ip.h>
  89. #include <nuttx/net/ipv6ext.h>
  90. #include "neighbor/neighbor.h"
  91. #include "tcp/tcp.h"
  92. #include "udp/udp.h"
  93. #include "sixlowpan/sixlowpan.h"
  94. #include "pkt/pkt.h"
  95. #include "icmpv6/icmpv6.h"
  96. #include "netdev/netdev.h"
  97. #include "ipforward/ipforward.h"
  98. #include "inet/inet.h"
  99. #include "devif/devif.h"
  100. /****************************************************************************
  101. * Pre-processor Definitions
  102. ****************************************************************************/
  103. /* Macros */
  104. #define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  105. #define PAYLOAD ((FAR uint8_t *)&dev->d_buf[NET_LL_HDRLEN(dev)] + IPv6_HDRLEN)
  106. /****************************************************************************
  107. * Private Functions
  108. ****************************************************************************/
  109. /****************************************************************************
  110. * Name: ipv6_exthdr
  111. *
  112. * Description:
  113. * Return true if the next header value is an IPv6 extension header.
  114. *
  115. ****************************************************************************/
  116. static bool ipv6_exthdr(uint8_t nxthdr)
  117. {
  118. switch (nxthdr)
  119. {
  120. case NEXT_HOPBYBOT_EH: /* Hop-by-Hop Options Header */
  121. case NEXT_ENCAP_EH: /* Encapsulated IPv6 Header */
  122. case NEXT_ROUTING_EH: /* Routing Header */
  123. case NEXT_FRAGMENT_EH: /* Fragment Header */
  124. case NEXT_RRSVP_EH: /* Resource ReSerVation Protocol */
  125. case NEXT_ENCAPSEC_EH: /* Encapsulating Security Payload */
  126. case NEXT_AUTH_EH: /* Authentication Header */
  127. case NEXT_DESTOPT_EH: /* Destination Options Header */
  128. case NEXT_MOBILITY_EH: /* Mobility */
  129. case NEXT_HOSTID_EH: /* Host Identity Protocol */
  130. case NEXT_SHIM6_EH: /* Shim6 Protocol */
  131. return true;
  132. case NEXT_NOHEADER: /* No next header */
  133. default:
  134. return false;
  135. }
  136. }
  137. /****************************************************************************
  138. * Name: check_dev_destipaddr
  139. *
  140. * Description:
  141. * Check if the destination address in the IPv6 is destined for the
  142. * provided network device.
  143. *
  144. * Returned Value:
  145. * 1 - This packet is destined for this network device
  146. * 0 - This packet is NOT destined for this network device
  147. *
  148. ****************************************************************************/
  149. static int check_dev_destipaddr(FAR struct net_driver_s *dev, FAR void *arg)
  150. {
  151. FAR struct ipv6_hdr_s *ipv6 = (FAR struct ipv6_hdr_s *)arg;
  152. /* Check if the IPv6 destination address matches the IPv6 address assigned
  153. * to this device.
  154. */
  155. if (net_ipv6addr_cmp(ipv6->destipaddr, dev->d_ipv6addr))
  156. {
  157. return 1;
  158. }
  159. /* No match, return 0 to keep searching */
  160. return 0;
  161. }
  162. /****************************************************************************
  163. * Name: check_destipaddr
  164. *
  165. * Description:
  166. * Check if the destination address in the IPv6 is destined for us. This
  167. * is typically just a comparison the of the IPv6 destination address in
  168. * the IPv6 packet with the IPv6 address assigned to the receiving device.
  169. *
  170. * Input Parameters:
  171. * dev - The device on which the packet was received and which contains
  172. * the IPv6 packet.
  173. * ipv6 - A convenience pointer to the IPv6 header in within the IPv6
  174. * packet
  175. *
  176. * Returned Value:
  177. * true - This packet is destined for us
  178. * false - This packet is NOT destined for us and may need to be forwarded.
  179. *
  180. ****************************************************************************/
  181. static bool check_destipaddr(FAR struct net_driver_s *dev,
  182. FAR struct ipv6_hdr_s *ipv6)
  183. {
  184. int ret;
  185. /* For IPv6, packet reception is a little trickier as we need to make sure
  186. * that we listen to certain multicast addresses (all hosts multicast
  187. * address, and the solicited-node multicast address) as well. However,
  188. * we will cheat here and accept all multicast packets that are sent to
  189. * the ff02::/16 addresses.
  190. */
  191. if (ipv6->destipaddr[0] == HTONS(0xff02))
  192. {
  193. #ifdef CONFIG_NET_IPFORWARD_BROADCAST
  194. /* Forward multicast packets */
  195. ipv6_forward_broadcast(dev, ipv6);
  196. #endif
  197. return true;
  198. }
  199. /* We will also allow for a perverse case where we receive a packet
  200. * addressed to us, but on a different device. Can that really happen?
  201. */
  202. ret = netdev_foreach(check_dev_destipaddr, ipv6);
  203. if (ret == 1)
  204. {
  205. /* The traversal of the network devices will return 0 if there is
  206. * no network device with that address or 1 if there is a network
  207. * device with such an address.
  208. */
  209. return true;
  210. }
  211. return false;
  212. }
  213. /****************************************************************************
  214. * Public Functions
  215. ****************************************************************************/
  216. /****************************************************************************
  217. * Name: ipv6_input
  218. *
  219. * Description:
  220. * Receive an IPv6 packet from the network device. Verify and forward to
  221. * L3 packet handling logic if the packet is destined for us.
  222. *
  223. * Input Parameters:
  224. * dev - The device on which the packet was received and which contains
  225. * the IPv6 packet.
  226. * Returned Value:
  227. * OK - The packet was processed (or dropped) and can be discarded.
  228. * ERROR - Hold the packet and try again later. There is a listening
  229. * socket but no receive in place to catch the packet yet. The
  230. * device's d_len will be set to zero in this case as there is
  231. * no outgoing data.
  232. *
  233. * If this function returns to the network driver with dev->d_len > 0,
  234. * that is an indication to the driver that there is an outgoing response
  235. * to this input.
  236. *
  237. * Assumptions:
  238. * The network is locked.
  239. *
  240. ****************************************************************************/
  241. int ipv6_input(FAR struct net_driver_s *dev)
  242. {
  243. FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
  244. FAR uint8_t *payload;
  245. uint16_t llhdrlen;
  246. uint16_t iphdrlen;
  247. uint16_t paylen;
  248. uint8_t nxthdr;
  249. #ifdef CONFIG_NET_IPFORWARD
  250. int ret;
  251. #endif
  252. /* This is where the input processing starts. */
  253. #ifdef CONFIG_NET_STATISTICS
  254. g_netstats.ipv6.recv++;
  255. #endif
  256. /* Start of IP input header processing code.
  257. *
  258. * Check validity of the IP header.
  259. */
  260. if ((ipv6->vtc & 0xf0) != 0x60)
  261. {
  262. /* IP version and header length. */
  263. nwarn("WARNING: Invalid IPv6 version: %d\n", ipv6->vtc >> 4);
  264. #ifdef CONFIG_NET_STATISTICS
  265. g_netstats.ipv6.vhlerr++;
  266. #endif
  267. goto drop;
  268. }
  269. /* Get the size of the packet minus the size of link layer header */
  270. llhdrlen = NET_LL_HDRLEN(dev);
  271. if ((llhdrlen + IPv6_HDRLEN) > dev->d_len)
  272. {
  273. nwarn("WARNING: Packet shorter than IPv6 header\n");
  274. goto drop;
  275. }
  276. dev->d_len -= llhdrlen;
  277. /* Make sure that all packet processing logic knows that there is an IPv6
  278. * packet in the device buffer.
  279. */
  280. IFF_SET_IPv6(dev->d_flags);
  281. /* Check the size of the packet. If the size reported to us in d_len is
  282. * smaller the size reported in the IP header, we assume that the packet
  283. * has been corrupted in transit. If the size of d_len is larger than the
  284. * size reported in the IP packet header, the packet has been padded and
  285. * we set d_len to the correct value.
  286. *
  287. * The length reported in the IPv6 header is the length of the payload
  288. * that follows the header. The device interface uses the d_len variable
  289. * for holding the size of the entire packet, including the IP header but
  290. * without the link layer header (subtracted out above).
  291. *
  292. * NOTE: The payload length in the includes the size of the Ipv6 extension
  293. * options, but not the size of the IPv6 header.
  294. *
  295. * REVISIT: Length will be set to zero if the extension header carries
  296. * a Jumbo payload option.
  297. */
  298. paylen = ((uint16_t)ipv6->len[0] << 8) + (uint16_t)ipv6->len[1] +
  299. IPv6_HDRLEN;
  300. if (paylen <= dev->d_len)
  301. {
  302. dev->d_len = paylen;
  303. }
  304. else
  305. {
  306. nwarn("WARNING: IP packet shorter than length in IP header\n");
  307. goto drop;
  308. }
  309. /* Parse IPv6 extension headers (parsed but ignored) */
  310. payload = PAYLOAD; /* Assume payload starts right after IPv6 header */
  311. iphdrlen = IPv6_HDRLEN; /* Total length of the IPv6 header */
  312. nxthdr = ipv6->proto; /* Next header determined by IPv6 header prototype */
  313. while (ipv6_exthdr(nxthdr))
  314. {
  315. FAR struct ipv6_extension_s *exthdr;
  316. uint16_t extlen;
  317. /* Just skip over the extension header */
  318. exthdr = (FAR struct ipv6_extension_s *)payload;
  319. extlen = EXTHDR_LEN((unsigned int)exthdr->len);
  320. payload += extlen;
  321. iphdrlen += extlen;
  322. nxthdr = exthdr->nxthdr;
  323. }
  324. #ifdef CONFIG_NET_BROADCAST
  325. /* Check for a multicast packet, which may be destined to us (even if
  326. * there is no IP address yet assigned to the device). We only expect
  327. * multicast packets destined for sockets that have joined a multicast
  328. * group or for ICMPv6 Autoconfiguration and Neighbor discovery or ICMPv6
  329. * MLD packets.
  330. *
  331. * We should actually pick off certain multicast address (all hosts
  332. * multicast address, and the solicited-node multicast address). We
  333. * will cheat here and accept all multicast packets that are sent to the
  334. * ff00::/8 addresses (see net_is_addr_mcast).
  335. */
  336. if (net_is_addr_mcast(ipv6->destipaddr))
  337. {
  338. #ifdef CONFIG_NET_IPFORWARD_BROADCAST
  339. /* Packets sent to ffx0 are reserved, ffx1 are interface-local, and ffx2
  340. * are interface-local, and therefore, should not be forwarded
  341. */
  342. if ((ipv6->destipaddr[0] & HTONS(0xff0f) != HTONS(0xff00)) &&
  343. (ipv6->destipaddr[0] & HTONS(0xff0f) != HTONS(0xff01)) &&
  344. (ipv6->destipaddr[0] & HTONS(0xff0f) != HTONS(0xff02)))
  345. {
  346. /* Forward broadcast packets */
  347. ipv6_forward_broadcast(dev, ipv6);
  348. }
  349. #endif
  350. /* Fall through with no further address checks and handle the multicast
  351. * address by its IPv6 nexthdr field.
  352. */
  353. }
  354. /* In other cases, the device must be assigned a non-zero IP address
  355. * (the all zero address is the "unspecified" address.
  356. */
  357. else
  358. #endif
  359. #ifdef CONFIG_NET_ICMPv6
  360. if (net_ipv6addr_cmp(dev->d_ipv6addr, g_ipv6_unspecaddr))
  361. {
  362. nwarn("WARNING: No IP address assigned\n");
  363. goto drop;
  364. }
  365. /* Check if the packet is destined for out IP address */
  366. else
  367. #endif
  368. {
  369. /* Check if the packet is destined for us. */
  370. if (!check_destipaddr(dev, ipv6))
  371. {
  372. #ifdef CONFIG_NET_IPFORWARD
  373. /* Not destined for us, try to forward the packet */
  374. ret = ipv6_forward(dev, ipv6);
  375. if (ret >= 0)
  376. {
  377. /* The packet was forwarded. Return success; d_len will
  378. * be set appropriately by the forwarding logic: Cleared
  379. * if the packet is forward via another device or non-
  380. * zero if it will be forwarded by the same device that
  381. * it was received on.
  382. */
  383. return OK;
  384. }
  385. else
  386. #endif
  387. {
  388. /* Not destined for us and not forwardable... drop the packet. */
  389. nwarn("WARNING: Not destined for us; not forwardable... Dropping!\n");
  390. goto drop;
  391. }
  392. }
  393. }
  394. /* Now process the incoming packet according to the protocol specified in
  395. * the next header IPv6 field.
  396. */
  397. switch (nxthdr)
  398. {
  399. #ifdef NET_TCP_HAVE_STACK
  400. case IP_PROTO_TCP: /* TCP input */
  401. /* Forward the IPv6 TCP packet */
  402. tcp_ipv6_input(dev, iphdrlen);
  403. #ifdef CONFIG_NET_6LOWPAN
  404. /* TCP output comes through three different mechanisms. Either from:
  405. *
  406. * 1. TCP socket output. For the case of TCP output to an
  407. * IEEE802.15.4, the TCP output is caught in the socket
  408. * send()/sendto() logic and and redirected to 6LoWPAN logic.
  409. * 2. TCP output from the TCP state machine. That will occur
  410. * during TCP packet processing by the TCP state machine.
  411. * 3. TCP output resulting from TX or timer polling
  412. *
  413. * Case 3 is handled here. Logic here detects if (1) an attempt
  414. * to return with d_len > 0 and (2) that the device is an
  415. * IEEE802.15.4 MAC network driver. Under those conditions, 6LoWPAN
  416. * logic will be called to create the IEEE80215.4 frames.
  417. */
  418. if (dev->d_len > 0 && dev->d_lltype == CONFIG_NET_6LOWPAN)
  419. {
  420. /* Let 6LoWPAN handle the TCP output */
  421. sixlowpan_tcp_send(dev, dev, ipv6);
  422. /* Drop the packet in the d_buf */
  423. goto drop;
  424. }
  425. #endif /* CONFIG_NET_6LOWPAN */
  426. break;
  427. #endif /* NET_TCP_HAVE_STACK */
  428. #ifdef NET_UDP_HAVE_STACK
  429. case IP_PROTO_UDP: /* UDP input */
  430. /* Forward the IPv6 UDP packet */
  431. udp_ipv6_input(dev, iphdrlen);
  432. break;
  433. #endif
  434. /* Check for ICMP input */
  435. #ifdef NET_ICMPv6_HAVE_STACK
  436. case IP_PROTO_ICMP6: /* ICMP6 input */
  437. /* Forward the ICMPv6 packet */
  438. icmpv6_input(dev, iphdrlen);
  439. #ifdef CONFIG_NET_6LOWPAN
  440. /* All outgoing ICMPv6 messages come through one of two mechanisms:
  441. *
  442. * 1. The output from internal ICMPv6 message passing. These
  443. * outgoing messages will use device polling and will be
  444. * handled elsewhere.
  445. * 2. ICMPv6 output resulting from TX or timer polling.
  446. *
  447. * Case 2 is handled here. Logic here detects if (1) an attempt
  448. * to return with d_len > 0 and (2) that the device is an
  449. * IEEE802.15.4 MAC network driver. Under those conditions, 6LoWPAN
  450. * logic will be called to create the IEEE80215.4 frames.
  451. */
  452. if (dev->d_len > 0 && dev->d_lltype == CONFIG_NET_6LOWPAN)
  453. {
  454. /* Let 6LoWPAN handle the ICMPv6 output */
  455. sixlowpan_icmpv6_send(dev, dev, ipv6);
  456. /* Drop the packet in the d_buf */
  457. goto drop;
  458. }
  459. #endif /* CONFIG_NET_6LOWPAN */
  460. break;
  461. #endif /* NET_ICMPv6_HAVE_STACK */
  462. default: /* Unrecognized/unsupported protocol */
  463. nwarn("WARNING: Unrecognized IP protocol: %04x\n", ipv6->proto);
  464. #ifdef CONFIG_NET_STATISTICS
  465. g_netstats.ipv6.protoerr++;
  466. #endif
  467. goto drop;
  468. }
  469. /* Return and let the caller do any pending transmission. */
  470. return OK;
  471. /* Drop the packet. NOTE that OK is returned meaning that the
  472. * packet has been processed (although processed unsuccessfully).
  473. */
  474. drop:
  475. #ifdef CONFIG_NET_STATISTICS
  476. g_netstats.ipv6.drop++;
  477. #endif
  478. dev->d_len = 0;
  479. return OK;
  480. }
  481. #endif /* CONFIG_NET_IPv6 */