ip6.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /**
  2. * @file
  3. *
  4. * IPv6 layer.
  5. */
  6. /*
  7. * Copyright (c) 2010 Inico Technologies Ltd.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Ivan Delamer <delamer@inicotech.com>
  35. *
  36. *
  37. * Please coordinate changes and requests with Ivan Delamer
  38. * <delamer@inicotech.com>
  39. */
  40. #include "lwip/opt.h"
  41. #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
  42. #include "lwip/def.h"
  43. #include "lwip/mem.h"
  44. #include "lwip/netif.h"
  45. #include "lwip/ip.h"
  46. #include "lwip/ip6.h"
  47. #include "lwip/ip6_addr.h"
  48. #include "lwip/ip6_frag.h"
  49. #include "lwip/icmp6.h"
  50. #include "lwip/priv/raw_priv.h"
  51. #include "lwip/udp.h"
  52. #include "lwip/priv/tcp_priv.h"
  53. #include "lwip/dhcp6.h"
  54. #include "lwip/nd6.h"
  55. #include "lwip/mld6.h"
  56. #include "lwip/debug.h"
  57. #include "lwip/stats.h"
  58. #ifdef LWIP_HOOK_FILENAME
  59. #include LWIP_HOOK_FILENAME
  60. #endif
  61. /**
  62. * Finds the appropriate network interface for a given IPv6 address. It tries to select
  63. * a netif following a sequence of heuristics:
  64. * 1) if there is only 1 netif, return it
  65. * 2) if the destination is a zoned address, match its zone to a netif
  66. * 3) if the either the source or destination address is a scoped address,
  67. * match the source address's zone (if set) or address (if not) to a netif
  68. * 4) tries to match the destination subnet to a configured address
  69. * 5) tries to find a router-announced route
  70. * 6) tries to match the (unscoped) source address to the netif
  71. * 7) returns the default netif, if configured
  72. *
  73. * Note that each of the two given addresses may or may not be properly zoned.
  74. *
  75. * @param src the source IPv6 address, if known
  76. * @param dest the destination IPv6 address for which to find the route
  77. * @return the netif on which to send to reach dest
  78. */
  79. struct netif *
  80. ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest)
  81. {
  82. #if LWIP_SINGLE_NETIF
  83. LWIP_UNUSED_ARG(src);
  84. LWIP_UNUSED_ARG(dest);
  85. #else /* LWIP_SINGLE_NETIF */
  86. struct netif *netif;
  87. s8_t i;
  88. LWIP_ASSERT_CORE_LOCKED();
  89. /* If single netif configuration, fast return. */
  90. if ((netif_list != NULL) && (netif_list->next == NULL)) {
  91. if (!netif_is_up(netif_list) || !netif_is_link_up(netif_list) ||
  92. (ip6_addr_has_zone(dest) && !ip6_addr_test_zone(dest, netif_list))) {
  93. return NULL;
  94. }
  95. return netif_list;
  96. }
  97. #if LWIP_IPV6_SCOPES
  98. /* Special processing for zoned destination addresses. This includes link-
  99. * local unicast addresses and interface/link-local multicast addresses. Use
  100. * the zone to find a matching netif. If the address is not zoned, then there
  101. * is technically no "wrong" netif to choose, and we leave routing to other
  102. * rules; in most cases this should be the scoped-source rule below. */
  103. if (ip6_addr_has_zone(dest)) {
  104. IP6_ADDR_ZONECHECK(dest);
  105. /* Find a netif based on the zone. For custom mappings, one zone may map
  106. * to multiple netifs, so find one that can actually send a packet. */
  107. NETIF_FOREACH(netif) {
  108. if (ip6_addr_test_zone(dest, netif) &&
  109. netif_is_up(netif) && netif_is_link_up(netif)) {
  110. return netif;
  111. }
  112. }
  113. /* No matching netif found. Do no try to route to a different netif,
  114. * as that would be a zone violation, resulting in any packets sent to
  115. * that netif being dropped on output. */
  116. return NULL;
  117. }
  118. #endif /* LWIP_IPV6_SCOPES */
  119. /* Special processing for scoped source and destination addresses. If we get
  120. * here, the destination address does not have a zone, so either way we need
  121. * to look at the source address, which may or may not have a zone. If it
  122. * does, the zone is restrictive: there is (typically) only one matching
  123. * netif for it, and we should avoid routing to any other netif as that would
  124. * result in guaranteed zone violations. For scoped source addresses that do
  125. * not have a zone, use (only) a netif that has that source address locally
  126. * assigned. This case also applies to the loopback source address, which has
  127. * an implied link-local scope. If only the destination address is scoped
  128. * (but, again, not zoned), we still want to use only the source address to
  129. * determine its zone because that's most likely what the user/application
  130. * wants, regardless of whether the source address is scoped. Finally, some
  131. * of this story also applies if scoping is disabled altogether. */
  132. #if LWIP_IPV6_SCOPES
  133. if (ip6_addr_has_scope(dest, IP6_UNKNOWN) ||
  134. ip6_addr_has_scope(src, IP6_UNICAST) ||
  135. #else /* LWIP_IPV6_SCOPES */
  136. if (ip6_addr_islinklocal(dest) || ip6_addr_ismulticast_iflocal(dest) ||
  137. ip6_addr_ismulticast_linklocal(dest) || ip6_addr_islinklocal(src) ||
  138. #endif /* LWIP_IPV6_SCOPES */
  139. ip6_addr_isloopback(src)) {
  140. #if LWIP_IPV6_SCOPES
  141. if (ip6_addr_has_zone(src)) {
  142. /* Find a netif matching the source zone (relatively cheap). */
  143. NETIF_FOREACH(netif) {
  144. if (netif_is_up(netif) && netif_is_link_up(netif) &&
  145. ip6_addr_test_zone(src, netif)) {
  146. return netif;
  147. }
  148. }
  149. } else
  150. #endif /* LWIP_IPV6_SCOPES */
  151. {
  152. /* Find a netif matching the source address (relatively expensive). */
  153. NETIF_FOREACH(netif) {
  154. if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
  155. continue;
  156. }
  157. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  158. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
  159. ip6_addr_zoneless_eq(src, netif_ip6_addr(netif, i))) {
  160. return netif;
  161. }
  162. }
  163. }
  164. }
  165. /* Again, do not use any other netif in this case, as that could result in
  166. * zone boundary violations. */
  167. return NULL;
  168. }
  169. /* We come here only if neither source nor destination is scoped. */
  170. IP6_ADDR_ZONECHECK(src);
  171. #ifdef LWIP_HOOK_IP6_ROUTE
  172. netif = LWIP_HOOK_IP6_ROUTE(src, dest);
  173. if (netif != NULL) {
  174. return netif;
  175. }
  176. #endif
  177. /* See if the destination subnet matches a configured address. In accordance
  178. * with RFC 5942, dynamically configured addresses do not have an implied
  179. * local subnet, and thus should be considered /128 assignments. However, as
  180. * such, the destination address may still match a local address, and so we
  181. * still need to check for exact matches here. By (lwIP) policy, statically
  182. * configured addresses do always have an implied local /64 subnet. */
  183. NETIF_FOREACH(netif) {
  184. if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
  185. continue;
  186. }
  187. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  188. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
  189. ip6_addr_net_eq(dest, netif_ip6_addr(netif, i)) &&
  190. (netif_ip6_addr_isstatic(netif, i) ||
  191. ip6_addr_nethost_eq(dest, netif_ip6_addr(netif, i)))) {
  192. return netif;
  193. }
  194. }
  195. }
  196. /* Get the netif for a suitable router-announced route. */
  197. netif = nd6_find_route(dest);
  198. if (netif != NULL) {
  199. return netif;
  200. }
  201. /* Try with the netif that matches the source address. Given the earlier rule
  202. * for scoped source addresses, this applies to unscoped addresses only. */
  203. if (!ip6_addr_isany(src)) {
  204. NETIF_FOREACH(netif) {
  205. if (!netif_is_up(netif) || !netif_is_link_up(netif)) {
  206. continue;
  207. }
  208. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  209. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
  210. ip6_addr_eq(src, netif_ip6_addr(netif, i))) {
  211. return netif;
  212. }
  213. }
  214. }
  215. }
  216. #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
  217. /* loopif is disabled, loopback traffic is passed through any netif */
  218. if (ip6_addr_isloopback(dest)) {
  219. /* don't check for link on loopback traffic */
  220. if (netif_default != NULL && netif_is_up(netif_default)) {
  221. return netif_default;
  222. }
  223. /* default netif is not up, just use any netif for loopback traffic */
  224. NETIF_FOREACH(netif) {
  225. if (netif_is_up(netif)) {
  226. return netif;
  227. }
  228. }
  229. return NULL;
  230. }
  231. #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
  232. #endif /* !LWIP_SINGLE_NETIF */
  233. /* no matching netif found, use default netif, if up */
  234. if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default)) {
  235. return NULL;
  236. }
  237. return netif_default;
  238. }
  239. /**
  240. * @ingroup ip6
  241. * Select the best IPv6 source address for a given destination IPv6 address.
  242. *
  243. * This implementation follows RFC 6724 Sec. 5 to the following extent:
  244. * - Rules 1, 2, 3: fully implemented
  245. * - Rules 4, 5, 5.5: not applicable
  246. * - Rule 6: not implemented
  247. * - Rule 7: not applicable
  248. * - Rule 8: limited to "prefer /64 subnet match over non-match"
  249. *
  250. * For Rule 2, we deliberately deviate from RFC 6724 Sec. 3.1 by considering
  251. * ULAs to be of smaller scope than global addresses, to avoid that a preferred
  252. * ULA is picked over a deprecated global address when given a global address
  253. * as destination, as that would likely result in broken two-way communication.
  254. *
  255. * As long as temporary addresses are not supported (as used in Rule 7), a
  256. * proper implementation of Rule 8 would obviate the need to implement Rule 6.
  257. *
  258. * @param netif the netif on which to send a packet
  259. * @param dest the destination we are trying to reach (possibly not properly
  260. * zoned)
  261. * @return the most suitable source address to use, or NULL if no suitable
  262. * source address is found
  263. */
  264. const ip_addr_t *
  265. ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
  266. {
  267. const ip_addr_t *best_addr;
  268. const ip6_addr_t *cand_addr;
  269. s8_t dest_scope, cand_scope;
  270. s8_t best_scope = IP6_MULTICAST_SCOPE_RESERVED;
  271. u8_t i, cand_pref, cand_bits;
  272. u8_t best_pref = 0;
  273. u8_t best_bits = 0;
  274. /* Start by determining the scope of the given destination address. These
  275. * tests are hopefully (roughly) in order of likeliness to match. */
  276. if (ip6_addr_isglobal(dest)) {
  277. dest_scope = IP6_MULTICAST_SCOPE_GLOBAL;
  278. } else if (ip6_addr_islinklocal(dest) || ip6_addr_isloopback(dest)) {
  279. dest_scope = IP6_MULTICAST_SCOPE_LINK_LOCAL;
  280. } else if (ip6_addr_isuniquelocal(dest)) {
  281. dest_scope = IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL;
  282. } else if (ip6_addr_ismulticast(dest)) {
  283. dest_scope = ip6_addr_multicast_scope(dest);
  284. } else if (ip6_addr_issitelocal(dest)) {
  285. dest_scope = IP6_MULTICAST_SCOPE_SITE_LOCAL;
  286. } else {
  287. /* no match, consider scope global */
  288. dest_scope = IP6_MULTICAST_SCOPE_GLOBAL;
  289. }
  290. best_addr = NULL;
  291. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  292. /* Consider only valid (= preferred and deprecated) addresses. */
  293. if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
  294. continue;
  295. }
  296. /* Determine the scope of this candidate address. Same ordering idea. */
  297. cand_addr = netif_ip6_addr(netif, i);
  298. if (ip6_addr_isglobal(cand_addr)) {
  299. cand_scope = IP6_MULTICAST_SCOPE_GLOBAL;
  300. } else if (ip6_addr_islinklocal(cand_addr)) {
  301. cand_scope = IP6_MULTICAST_SCOPE_LINK_LOCAL;
  302. } else if (ip6_addr_isuniquelocal(cand_addr)) {
  303. cand_scope = IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL;
  304. } else if (ip6_addr_issitelocal(cand_addr)) {
  305. cand_scope = IP6_MULTICAST_SCOPE_SITE_LOCAL;
  306. } else {
  307. /* no match, treat as low-priority global scope */
  308. cand_scope = IP6_MULTICAST_SCOPE_RESERVEDF;
  309. }
  310. cand_pref = ip6_addr_ispreferred(netif_ip6_addr_state(netif, i));
  311. /* @todo compute the actual common bits, for longest matching prefix. */
  312. /* We cannot count on the destination address having a proper zone
  313. * assignment, so do not compare zones in this case. */
  314. cand_bits = ip6_addr_net_zoneless_eq(cand_addr, dest); /* just 1 or 0 for now */
  315. if (cand_bits && ip6_addr_nethost_eq(cand_addr, dest)) {
  316. return netif_ip_addr6(netif, i); /* Rule 1 */
  317. }
  318. if ((best_addr == NULL) || /* no alternative yet */
  319. ((cand_scope < best_scope) && (cand_scope >= dest_scope)) ||
  320. ((cand_scope > best_scope) && (best_scope < dest_scope)) || /* Rule 2 */
  321. ((cand_scope == best_scope) && ((cand_pref > best_pref) || /* Rule 3 */
  322. ((cand_pref == best_pref) && (cand_bits > best_bits))))) { /* Rule 8 */
  323. /* We found a new "winning" candidate. */
  324. best_addr = netif_ip_addr6(netif, i);
  325. best_scope = cand_scope;
  326. best_pref = cand_pref;
  327. best_bits = cand_bits;
  328. }
  329. }
  330. return best_addr; /* may be NULL */
  331. }
  332. #if LWIP_IPV6_FORWARD
  333. /**
  334. * Forwards an IPv6 packet. It finds an appropriate route for the
  335. * packet, decrements the HL value of the packet, and outputs
  336. * the packet on the appropriate interface.
  337. *
  338. * @param p the packet to forward (p->payload points to IP header)
  339. * @param iphdr the IPv6 header of the input packet
  340. * @param inp the netif on which this packet was received
  341. */
  342. static void
  343. ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp)
  344. {
  345. struct netif *netif;
  346. /* do not forward link-local or loopback addresses */
  347. if (ip6_addr_islinklocal(ip6_current_dest_addr()) ||
  348. ip6_addr_isloopback(ip6_current_dest_addr())) {
  349. LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding link-local address.\n"));
  350. IP6_STATS_INC(ip6.rterr);
  351. IP6_STATS_INC(ip6.drop);
  352. return;
  353. }
  354. /* Find network interface where to forward this IP packet to. */
  355. netif = ip6_route(IP6_ADDR_ANY6, ip6_current_dest_addr());
  356. if (netif == NULL) {
  357. LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
  358. IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
  359. IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
  360. IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
  361. IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
  362. IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
  363. IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
  364. IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
  365. IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
  366. #if LWIP_ICMP6
  367. /* Don't send ICMP messages in response to ICMP messages */
  368. if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
  369. icmp6_dest_unreach(p, ICMP6_DUR_NO_ROUTE);
  370. }
  371. #endif /* LWIP_ICMP6 */
  372. IP6_STATS_INC(ip6.rterr);
  373. IP6_STATS_INC(ip6.drop);
  374. return;
  375. }
  376. #if LWIP_IPV6_SCOPES
  377. /* Do not forward packets with a zoned (e.g., link-local) source address
  378. * outside of their zone. We determined the zone a bit earlier, so we know
  379. * that the address is properly zoned here, so we can safely use has_zone.
  380. * Also skip packets with a loopback source address (link-local implied). */
  381. if ((ip6_addr_has_zone(ip6_current_src_addr()) &&
  382. !ip6_addr_test_zone(ip6_current_src_addr(), netif)) ||
  383. ip6_addr_isloopback(ip6_current_src_addr())) {
  384. LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding packet beyond its source address zone.\n"));
  385. IP6_STATS_INC(ip6.rterr);
  386. IP6_STATS_INC(ip6.drop);
  387. return;
  388. }
  389. #endif /* LWIP_IPV6_SCOPES */
  390. /* Do not forward packets onto the same network interface on which
  391. * they arrived. */
  392. if (netif == inp) {
  393. LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not bouncing packets back on incoming interface.\n"));
  394. IP6_STATS_INC(ip6.rterr);
  395. IP6_STATS_INC(ip6.drop);
  396. return;
  397. }
  398. /* decrement HL */
  399. IP6H_HOPLIM_SET(iphdr, IP6H_HOPLIM(iphdr) - 1);
  400. /* send ICMP6 if HL == 0 */
  401. if (IP6H_HOPLIM(iphdr) == 0) {
  402. #if LWIP_ICMP6
  403. /* Don't send ICMP messages in response to ICMP messages */
  404. if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
  405. icmp6_time_exceeded(p, ICMP6_TE_HL);
  406. }
  407. #endif /* LWIP_ICMP6 */
  408. IP6_STATS_INC(ip6.drop);
  409. return;
  410. }
  411. if (netif->mtu && (p->tot_len > netif->mtu)) {
  412. #if LWIP_ICMP6
  413. /* Don't send ICMP messages in response to ICMP messages */
  414. if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) {
  415. icmp6_packet_too_big(p, netif->mtu);
  416. }
  417. #endif /* LWIP_ICMP6 */
  418. IP6_STATS_INC(ip6.drop);
  419. return;
  420. }
  421. LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: forwarding packet to %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
  422. IP6_ADDR_BLOCK1(ip6_current_dest_addr()),
  423. IP6_ADDR_BLOCK2(ip6_current_dest_addr()),
  424. IP6_ADDR_BLOCK3(ip6_current_dest_addr()),
  425. IP6_ADDR_BLOCK4(ip6_current_dest_addr()),
  426. IP6_ADDR_BLOCK5(ip6_current_dest_addr()),
  427. IP6_ADDR_BLOCK6(ip6_current_dest_addr()),
  428. IP6_ADDR_BLOCK7(ip6_current_dest_addr()),
  429. IP6_ADDR_BLOCK8(ip6_current_dest_addr())));
  430. /* transmit pbuf on chosen interface */
  431. netif->output_ip6(netif, p, ip6_current_dest_addr());
  432. IP6_STATS_INC(ip6.fw);
  433. IP6_STATS_INC(ip6.xmit);
  434. return;
  435. }
  436. #endif /* LWIP_IPV6_FORWARD */
  437. /** Return true if the current input packet should be accepted on this netif */
  438. static int
  439. ip6_input_accept(struct netif *netif)
  440. {
  441. /* interface is up? */
  442. if (netif_is_up(netif)) {
  443. u8_t i;
  444. /* unicast to this interface address? address configured? */
  445. /* If custom scopes are used, the destination zone will be tested as
  446. * part of the local-address comparison, but we need to test the source
  447. * scope as well (e.g., is this interface on the same link?). */
  448. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  449. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
  450. ip6_addr_eq(ip6_current_dest_addr(), netif_ip6_addr(netif, i))
  451. #if IPV6_CUSTOM_SCOPES
  452. && (!ip6_addr_has_zone(ip6_current_src_addr()) ||
  453. ip6_addr_test_zone(ip6_current_src_addr(), netif))
  454. #endif /* IPV6_CUSTOM_SCOPES */
  455. ) {
  456. /* accept on this netif */
  457. return 1;
  458. }
  459. }
  460. }
  461. return 0;
  462. }
  463. /**
  464. * This function is called by the network interface device driver when
  465. * an IPv6 packet is received. The function does the basic checks of the
  466. * IP header such as packet size being at least larger than the header
  467. * size etc. If the packet was not destined for us, the packet is
  468. * forwarded (using ip6_forward).
  469. *
  470. * Finally, the packet is sent to the upper layer protocol input function.
  471. *
  472. * @param p the received IPv6 packet (p->payload points to IPv6 header)
  473. * @param inp the netif on which this packet was received
  474. * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
  475. * processed, but currently always returns ERR_OK)
  476. */
  477. err_t
  478. ip6_input(struct pbuf *p, struct netif *inp)
  479. {
  480. struct ip6_hdr *ip6hdr;
  481. struct netif *netif;
  482. const u8_t *nexth;
  483. u16_t hlen, hlen_tot; /* the current header length */
  484. #if 0 /*IP_ACCEPT_LINK_LAYER_ADDRESSING*/
  485. @todo
  486. int check_ip_src=1;
  487. #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
  488. #if LWIP_RAW
  489. raw_input_state_t raw_status;
  490. #endif /* LWIP_RAW */
  491. LWIP_ASSERT_CORE_LOCKED();
  492. IP6_STATS_INC(ip6.recv);
  493. /* identify the IP header */
  494. ip6hdr = (struct ip6_hdr *)p->payload;
  495. if (IP6H_V(ip6hdr) != 6) {
  496. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IPv6 packet dropped due to bad version number %"U32_F"\n",
  497. IP6H_V(ip6hdr)));
  498. pbuf_free(p);
  499. IP6_STATS_INC(ip6.err);
  500. IP6_STATS_INC(ip6.drop);
  501. return ERR_OK;
  502. }
  503. #ifdef LWIP_HOOK_IP6_INPUT
  504. if (LWIP_HOOK_IP6_INPUT(p, inp)) {
  505. /* the packet has been eaten */
  506. return ERR_OK;
  507. }
  508. #endif
  509. /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
  510. if ((IP6_HLEN > p->len) || (IP6H_PLEN(ip6hdr) > (p->tot_len - IP6_HLEN))) {
  511. if (IP6_HLEN > p->len) {
  512. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  513. ("IPv6 header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
  514. (u16_t)IP6_HLEN, p->len));
  515. }
  516. if ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len) {
  517. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  518. ("IPv6 (plen %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
  519. (u16_t)(IP6H_PLEN(ip6hdr) + IP6_HLEN), p->tot_len));
  520. }
  521. /* free (drop) packet pbufs */
  522. pbuf_free(p);
  523. IP6_STATS_INC(ip6.lenerr);
  524. IP6_STATS_INC(ip6.drop);
  525. return ERR_OK;
  526. }
  527. /* Trim pbuf. This should have been done at the netif layer,
  528. * but we'll do it anyway just to be sure that its done. */
  529. pbuf_realloc(p, (u16_t)(IP6_HLEN + IP6H_PLEN(ip6hdr)));
  530. /* copy IP addresses to aligned ip6_addr_t */
  531. ip_addr_copy_from_ip6_packed(ip_data.current_iphdr_dest, ip6hdr->dest);
  532. ip_addr_copy_from_ip6_packed(ip_data.current_iphdr_src, ip6hdr->src);
  533. /* Don't accept virtual IPv4 mapped IPv6 addresses.
  534. * Don't accept multicast source addresses. */
  535. if (ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_dest)) ||
  536. ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_src)) ||
  537. ip6_addr_ismulticast(ip_2_ip6(&ip_data.current_iphdr_src))) {
  538. /* free (drop) packet pbufs */
  539. pbuf_free(p);
  540. IP6_STATS_INC(ip6.err);
  541. IP6_STATS_INC(ip6.drop);
  542. return ERR_OK;
  543. }
  544. /* Set the appropriate zone identifier on the addresses. */
  545. ip6_addr_assign_zone(ip_2_ip6(&ip_data.current_iphdr_dest), IP6_UNKNOWN, inp);
  546. ip6_addr_assign_zone(ip_2_ip6(&ip_data.current_iphdr_src), IP6_UNICAST, inp);
  547. /* current header pointer. */
  548. ip_data.current_ip6_header = ip6hdr;
  549. /* In netif, used in case we need to send ICMPv6 packets back. */
  550. ip_data.current_netif = inp;
  551. ip_data.current_input_netif = inp;
  552. /* match packet against an interface, i.e. is this packet for us? */
  553. if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
  554. /* Always joined to multicast if-local and link-local all-nodes group. */
  555. if (ip6_addr_isallnodes_iflocal(ip6_current_dest_addr()) ||
  556. ip6_addr_isallnodes_linklocal(ip6_current_dest_addr())) {
  557. netif = inp;
  558. }
  559. #if LWIP_IPV6_MLD
  560. else if (mld6_lookfor_group(inp, ip6_current_dest_addr())) {
  561. netif = inp;
  562. }
  563. #else /* LWIP_IPV6_MLD */
  564. else if (ip6_addr_issolicitednode(ip6_current_dest_addr())) {
  565. u8_t i;
  566. /* Filter solicited node packets when MLD is not enabled
  567. * (for Neighbor discovery). */
  568. netif = NULL;
  569. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  570. if (ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) &&
  571. ip6_addr_solicitednode_eq(ip6_current_dest_addr(), netif_ip6_addr(inp, i))) {
  572. netif = inp;
  573. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: solicited node packet accepted on interface %c%c\n",
  574. netif->name[0], netif->name[1]));
  575. break;
  576. }
  577. }
  578. }
  579. #endif /* LWIP_IPV6_MLD */
  580. else {
  581. netif = NULL;
  582. }
  583. } else {
  584. /* start trying with inp. if that's not acceptable, start walking the
  585. list of configured netifs. */
  586. if (ip6_input_accept(inp)) {
  587. netif = inp;
  588. } else {
  589. netif = NULL;
  590. #if !IPV6_CUSTOM_SCOPES
  591. /* Shortcut: stop looking for other interfaces if either the source or
  592. * the destination has a scope constrained to this interface. Custom
  593. * scopes may break the 1:1 link/interface mapping, however. */
  594. if (ip6_addr_islinklocal(ip6_current_dest_addr()) ||
  595. ip6_addr_islinklocal(ip6_current_src_addr())) {
  596. goto netif_found;
  597. }
  598. #endif /* !IPV6_CUSTOM_SCOPES */
  599. #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
  600. /* The loopback address is to be considered link-local. Packets to it
  601. * should be dropped on other interfaces, as per RFC 4291 Sec. 2.5.3.
  602. * Its implied scope means packets *from* the loopback address should
  603. * not be accepted on other interfaces, either. These requirements
  604. * cannot be implemented in the case that loopback traffic is sent
  605. * across a non-loopback interface, however. */
  606. if (ip6_addr_isloopback(ip6_current_dest_addr()) ||
  607. ip6_addr_isloopback(ip6_current_src_addr())) {
  608. goto netif_found;
  609. }
  610. #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
  611. #if !LWIP_SINGLE_NETIF
  612. NETIF_FOREACH(netif) {
  613. if (netif == inp) {
  614. /* we checked that before already */
  615. continue;
  616. }
  617. if (ip6_input_accept(netif)) {
  618. break;
  619. }
  620. }
  621. #endif /* !LWIP_SINGLE_NETIF */
  622. }
  623. netif_found:
  624. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet accepted on interface %c%c\n",
  625. netif ? netif->name[0] : 'X', netif? netif->name[1] : 'X'));
  626. }
  627. /* "::" packet source address? (used in duplicate address detection) */
  628. if (ip6_addr_isany(ip6_current_src_addr()) &&
  629. (!ip6_addr_issolicitednode(ip6_current_dest_addr()))) {
  630. /* packet source is not valid */
  631. /* free (drop) packet pbufs */
  632. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with src ANY_ADDRESS dropped\n"));
  633. pbuf_free(p);
  634. IP6_STATS_INC(ip6.drop);
  635. goto ip6_input_cleanup;
  636. }
  637. /* packet not for us? */
  638. if (netif == NULL) {
  639. /* packet not for us, route or discard */
  640. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_TRACE, ("ip6_input: packet not for us.\n"));
  641. #if LWIP_IPV6_FORWARD
  642. /* non-multicast packet? */
  643. if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
  644. /* try to forward IP packet on (other) interfaces */
  645. ip6_forward(p, ip6hdr, inp);
  646. }
  647. #endif /* LWIP_IPV6_FORWARD */
  648. pbuf_free(p);
  649. goto ip6_input_cleanup;
  650. }
  651. /* current netif pointer. */
  652. ip_data.current_netif = netif;
  653. /* Save next header type. */
  654. nexth = &IP6H_NEXTH(ip6hdr);
  655. /* Init header length. */
  656. hlen = hlen_tot = IP6_HLEN;
  657. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n"));
  658. ip6_debug_print(p);
  659. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
  660. /* Move to payload. */
  661. pbuf_remove_header(p, IP6_HLEN);
  662. /* Process known option extension headers, if present. */
  663. while (*nexth != IP6_NEXTH_NONE)
  664. {
  665. switch (*nexth) {
  666. case IP6_NEXTH_HOPBYHOP:
  667. {
  668. s32_t opt_offset;
  669. struct ip6_hbh_hdr *hbh_hdr;
  670. struct ip6_opt_hdr *opt_hdr;
  671. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header\n"));
  672. /* Get and check the header length, while staying in packet bounds. */
  673. hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
  674. /* Get next header type. */
  675. nexth = &IP6_HBH_NEXTH(hbh_hdr);
  676. /* Get the header length. */
  677. hlen = (u16_t)(8 * (1 + hbh_hdr->_hlen));
  678. if ((p->len < 8) || (hlen > p->len)) {
  679. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  680. ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
  681. hlen, p->len));
  682. /* free (drop) packet pbufs */
  683. pbuf_free(p);
  684. IP6_STATS_INC(ip6.lenerr);
  685. IP6_STATS_INC(ip6.drop);
  686. goto ip6_input_cleanup;
  687. }
  688. hlen_tot = (u16_t)(hlen_tot + hlen);
  689. /* The extended option header starts right after Hop-by-Hop header. */
  690. opt_offset = IP6_HBH_HLEN;
  691. while (opt_offset < hlen)
  692. {
  693. s32_t opt_dlen = 0;
  694. opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + opt_offset);
  695. switch (IP6_OPT_TYPE(opt_hdr)) {
  696. /* @todo: process IPV6 Hop-by-Hop option data */
  697. case IP6_PAD1_OPTION:
  698. /* PAD1 option doesn't have length and value field */
  699. opt_dlen = -1;
  700. break;
  701. case IP6_PADN_OPTION:
  702. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  703. break;
  704. case IP6_ROUTER_ALERT_OPTION:
  705. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  706. break;
  707. case IP6_JUMBO_OPTION:
  708. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  709. break;
  710. default:
  711. /* Check 2 MSB of Hop-by-Hop header type. */
  712. switch (IP6_OPT_TYPE_ACTION(opt_hdr)) {
  713. case 1:
  714. /* Discard the packet. */
  715. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n"));
  716. pbuf_free(p);
  717. IP6_STATS_INC(ip6.drop);
  718. goto ip6_input_cleanup;
  719. case 2:
  720. /* Send ICMP Parameter Problem */
  721. icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
  722. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n"));
  723. pbuf_free(p);
  724. IP6_STATS_INC(ip6.drop);
  725. goto ip6_input_cleanup;
  726. case 3:
  727. /* Send ICMP Parameter Problem if destination address is not a multicast address */
  728. if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
  729. icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
  730. }
  731. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n"));
  732. pbuf_free(p);
  733. IP6_STATS_INC(ip6.drop);
  734. goto ip6_input_cleanup;
  735. default:
  736. /* Skip over this option. */
  737. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  738. break;
  739. }
  740. break;
  741. }
  742. /* Adjust the offset to move to the next extended option header */
  743. opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen;
  744. }
  745. pbuf_remove_header(p, hlen);
  746. break;
  747. }
  748. case IP6_NEXTH_DESTOPTS:
  749. {
  750. s32_t opt_offset;
  751. struct ip6_dest_hdr *dest_hdr;
  752. struct ip6_opt_hdr *opt_hdr;
  753. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Destination options header\n"));
  754. dest_hdr = (struct ip6_dest_hdr *)p->payload;
  755. /* Get next header type. */
  756. nexth = &IP6_DEST_NEXTH(dest_hdr);
  757. /* Get the header length. */
  758. hlen = 8 * (1 + dest_hdr->_hlen);
  759. if ((p->len < 8) || (hlen > p->len)) {
  760. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  761. ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
  762. hlen, p->len));
  763. /* free (drop) packet pbufs */
  764. pbuf_free(p);
  765. IP6_STATS_INC(ip6.lenerr);
  766. IP6_STATS_INC(ip6.drop);
  767. goto ip6_input_cleanup;
  768. }
  769. hlen_tot = (u16_t)(hlen_tot + hlen);
  770. /* The extended option header starts right after Destination header. */
  771. opt_offset = IP6_DEST_HLEN;
  772. while (opt_offset < hlen)
  773. {
  774. s32_t opt_dlen = 0;
  775. opt_hdr = (struct ip6_opt_hdr *)((u8_t *)dest_hdr + opt_offset);
  776. switch (IP6_OPT_TYPE(opt_hdr))
  777. {
  778. /* @todo: process IPV6 Destination option data */
  779. case IP6_PAD1_OPTION:
  780. /* PAD1 option deosn't have length and value field */
  781. opt_dlen = -1;
  782. break;
  783. case IP6_PADN_OPTION:
  784. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  785. break;
  786. case IP6_ROUTER_ALERT_OPTION:
  787. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  788. break;
  789. case IP6_JUMBO_OPTION:
  790. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  791. break;
  792. case IP6_HOME_ADDRESS_OPTION:
  793. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  794. break;
  795. default:
  796. /* Check 2 MSB of Destination header type. */
  797. switch (IP6_OPT_TYPE_ACTION(opt_hdr))
  798. {
  799. case 1:
  800. /* Discard the packet. */
  801. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n"));
  802. pbuf_free(p);
  803. IP6_STATS_INC(ip6.drop);
  804. goto ip6_input_cleanup;
  805. case 2:
  806. /* Send ICMP Parameter Problem */
  807. icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
  808. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n"));
  809. pbuf_free(p);
  810. IP6_STATS_INC(ip6.drop);
  811. goto ip6_input_cleanup;
  812. case 3:
  813. /* Send ICMP Parameter Problem if destination address is not a multicast address */
  814. if (!ip6_addr_ismulticast(ip6_current_dest_addr())) {
  815. icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr);
  816. }
  817. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n"));
  818. pbuf_free(p);
  819. IP6_STATS_INC(ip6.drop);
  820. goto ip6_input_cleanup;
  821. default:
  822. /* Skip over this option. */
  823. opt_dlen = IP6_OPT_DLEN(opt_hdr);
  824. break;
  825. }
  826. break;
  827. }
  828. /* Adjust the offset to move to the next extended option header */
  829. opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen;
  830. }
  831. pbuf_remove_header(p, hlen);
  832. break;
  833. }
  834. case IP6_NEXTH_ROUTING:
  835. {
  836. struct ip6_rout_hdr *rout_hdr;
  837. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Routing header\n"));
  838. rout_hdr = (struct ip6_rout_hdr *)p->payload;
  839. /* Get next header type. */
  840. nexth = &IP6_ROUT_NEXTH(rout_hdr);
  841. /* Get the header length. */
  842. hlen = 8 * (1 + rout_hdr->_hlen);
  843. if ((p->len < 8) || (hlen > p->len)) {
  844. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  845. ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
  846. hlen, p->len));
  847. /* free (drop) packet pbufs */
  848. pbuf_free(p);
  849. IP6_STATS_INC(ip6.lenerr);
  850. IP6_STATS_INC(ip6.drop);
  851. goto ip6_input_cleanup;
  852. }
  853. /* Skip over this header. */
  854. hlen_tot = (u16_t)(hlen_tot + hlen);
  855. /* if segment left value is 0 in routing header, ignore the option */
  856. if (IP6_ROUT_SEG_LEFT(rout_hdr)) {
  857. /* The length field of routing option header must be even */
  858. if (rout_hdr->_hlen & 0x1) {
  859. /* Discard and send parameter field error */
  860. icmp6_param_problem(p, ICMP6_PP_FIELD, &rout_hdr->_hlen);
  861. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n"));
  862. pbuf_free(p);
  863. IP6_STATS_INC(ip6.drop);
  864. goto ip6_input_cleanup;
  865. }
  866. switch (IP6_ROUT_TYPE(rout_hdr))
  867. {
  868. /* TODO: process routing by the type */
  869. case IP6_ROUT_TYPE2:
  870. break;
  871. case IP6_ROUT_RPL:
  872. break;
  873. default:
  874. /* Discard unrecognized routing type and send parameter field error */
  875. icmp6_param_problem(p, ICMP6_PP_FIELD, &IP6_ROUT_TYPE(rout_hdr));
  876. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n"));
  877. pbuf_free(p);
  878. IP6_STATS_INC(ip6.drop);
  879. goto ip6_input_cleanup;
  880. }
  881. }
  882. pbuf_remove_header(p, hlen);
  883. break;
  884. }
  885. case IP6_NEXTH_FRAGMENT:
  886. {
  887. struct ip6_frag_hdr *frag_hdr;
  888. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n"));
  889. frag_hdr = (struct ip6_frag_hdr *)p->payload;
  890. /* Get next header type. */
  891. nexth = &IP6_FRAG_NEXTH(frag_hdr);
  892. /* Fragment Header length. */
  893. hlen = 8;
  894. /* Make sure this header fits in current pbuf. */
  895. if (hlen > p->len) {
  896. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  897. ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n",
  898. hlen, p->len));
  899. /* free (drop) packet pbufs */
  900. pbuf_free(p);
  901. IP6_FRAG_STATS_INC(ip6_frag.lenerr);
  902. IP6_FRAG_STATS_INC(ip6_frag.drop);
  903. goto ip6_input_cleanup;
  904. }
  905. hlen_tot = (u16_t)(hlen_tot + hlen);
  906. /* check payload length is multiple of 8 octets when mbit is set */
  907. if (IP6_FRAG_MBIT(frag_hdr) && (IP6H_PLEN(ip6hdr) & 0x7)) {
  908. /* ipv6 payload length is not multiple of 8 octets */
  909. icmp6_param_problem(p, ICMP6_PP_FIELD, LWIP_PACKED_CAST(const void *, &ip6hdr->_plen));
  910. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid payload length dropped\n"));
  911. pbuf_free(p);
  912. IP6_STATS_INC(ip6.drop);
  913. goto ip6_input_cleanup;
  914. }
  915. /* Offset == 0 and more_fragments == 0? */
  916. if ((frag_hdr->_fragment_offset &
  917. PP_HTONS(IP6_FRAG_OFFSET_MASK | IP6_FRAG_MORE_FLAG)) == 0) {
  918. /* This is a 1-fragment packet. Skip this header and continue. */
  919. pbuf_remove_header(p, hlen);
  920. } else {
  921. #if LWIP_IPV6_REASS
  922. /* reassemble the packet */
  923. ip_data.current_ip_header_tot_len = hlen_tot;
  924. p = ip6_reass(p);
  925. /* packet not fully reassembled yet? */
  926. if (p == NULL) {
  927. goto ip6_input_cleanup;
  928. }
  929. /* Returned p points to IPv6 header.
  930. * Update all our variables and pointers and continue. */
  931. ip6hdr = (struct ip6_hdr *)p->payload;
  932. ip_data.current_ip6_header = ip6hdr;
  933. nexth = &IP6H_NEXTH(ip6hdr);
  934. hlen = hlen_tot = IP6_HLEN;
  935. pbuf_remove_header(p, IP6_HLEN);
  936. #else /* LWIP_IPV6_REASS */
  937. /* free (drop) packet pbufs */
  938. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header dropped (with LWIP_IPV6_REASS==0)\n"));
  939. pbuf_free(p);
  940. IP6_STATS_INC(ip6.opterr);
  941. IP6_STATS_INC(ip6.drop);
  942. goto ip6_input_cleanup;
  943. #endif /* LWIP_IPV6_REASS */
  944. }
  945. break;
  946. }
  947. default:
  948. goto options_done;
  949. }
  950. if (*nexth == IP6_NEXTH_HOPBYHOP) {
  951. /* Hop-by-Hop header comes only as a first option */
  952. icmp6_param_problem(p, ICMP6_PP_HEADER, nexth);
  953. LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header dropped (only valid as a first option)\n"));
  954. pbuf_free(p);
  955. IP6_STATS_INC(ip6.drop);
  956. goto ip6_input_cleanup;
  957. }
  958. }
  959. options_done:
  960. /* send to upper layers */
  961. ip_data.current_ip_header_tot_len = hlen_tot;
  962. #if LWIP_RAW
  963. /* p points to IPv6 header again for raw_input. */
  964. pbuf_add_header_force(p, hlen_tot);
  965. /* raw input did not eat the packet? */
  966. raw_status = raw_input(p, inp);
  967. if (raw_status != RAW_INPUT_EATEN)
  968. {
  969. /* Point to payload. */
  970. pbuf_remove_header(p, hlen_tot);
  971. #else /* LWIP_RAW */
  972. {
  973. #endif /* LWIP_RAW */
  974. switch (*nexth) {
  975. case IP6_NEXTH_NONE:
  976. pbuf_free(p);
  977. break;
  978. #if LWIP_UDP
  979. case IP6_NEXTH_UDP:
  980. #if LWIP_UDPLITE
  981. case IP6_NEXTH_UDPLITE:
  982. #endif /* LWIP_UDPLITE */
  983. udp_input(p, inp);
  984. break;
  985. #endif /* LWIP_UDP */
  986. #if LWIP_TCP
  987. case IP6_NEXTH_TCP:
  988. tcp_input(p, inp);
  989. break;
  990. #endif /* LWIP_TCP */
  991. #if LWIP_ICMP6
  992. case IP6_NEXTH_ICMP6:
  993. icmp6_input(p, inp);
  994. break;
  995. #endif /* LWIP_ICMP */
  996. default:
  997. #if LWIP_RAW
  998. if (raw_status == RAW_INPUT_DELIVERED) {
  999. /* @todo: ipv6 mib in-delivers? */
  1000. } else
  1001. #endif /* LWIP_RAW */
  1002. {
  1003. #if LWIP_ICMP6
  1004. /* p points to IPv6 header again for raw_input. */
  1005. pbuf_add_header_force(p, hlen_tot);
  1006. /* send ICMP parameter problem unless it was a multicast or ICMPv6 */
  1007. if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) &&
  1008. (IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) {
  1009. icmp6_param_problem(p, ICMP6_PP_HEADER, nexth);
  1010. }
  1011. #endif /* LWIP_ICMP */
  1012. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", (u16_t)IP6H_NEXTH(ip6hdr)));
  1013. IP6_STATS_INC(ip6.proterr);
  1014. IP6_STATS_INC(ip6.drop);
  1015. }
  1016. pbuf_free(p);
  1017. break;
  1018. }
  1019. }
  1020. ip6_input_cleanup:
  1021. ip_data.current_netif = NULL;
  1022. ip_data.current_input_netif = NULL;
  1023. ip_data.current_ip6_header = NULL;
  1024. ip_data.current_ip_header_tot_len = 0;
  1025. ip6_addr_set_zero(ip6_current_src_addr());
  1026. ip6_addr_set_zero(ip6_current_dest_addr());
  1027. return ERR_OK;
  1028. }
  1029. /**
  1030. * Sends an IPv6 packet on a network interface. This function constructs
  1031. * the IPv6 header. If the source IPv6 address is NULL, the IPv6 "ANY" address is
  1032. * used as source (usually during network startup). If the source IPv6 address it
  1033. * IP6_ADDR_ANY, the most appropriate IPv6 address of the outgoing network
  1034. * interface is filled in as source address. If the destination IPv6 address is
  1035. * LWIP_IP_HDRINCL, p is assumed to already include an IPv6 header and
  1036. * p->payload points to it instead of the data.
  1037. *
  1038. * @param p the packet to send (p->payload points to the data, e.g. next
  1039. protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
  1040. IPv6 header and p->payload points to that IPv6 header)
  1041. * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
  1042. * IP address of the netif is selected and used as source address.
  1043. * if src == NULL, IP6_ADDR_ANY is used as source) (src is possibly not
  1044. * properly zoned)
  1045. * @param dest the destination IPv6 address to send the packet to (possibly not
  1046. * properly zoned)
  1047. * @param hl the Hop Limit value to be set in the IPv6 header
  1048. * @param tc the Traffic Class value to be set in the IPv6 header
  1049. * @param nexth the Next Header to be set in the IPv6 header
  1050. * @param netif the netif on which to send this packet
  1051. * @return ERR_OK if the packet was sent OK
  1052. * ERR_BUF if p doesn't have enough space for IPv6/LINK headers
  1053. * returns errors returned by netif->output_ip6
  1054. */
  1055. err_t
  1056. ip6_output_if(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
  1057. u8_t hl, u8_t tc,
  1058. u8_t nexth, struct netif *netif)
  1059. {
  1060. const ip6_addr_t *src_used = src;
  1061. if (dest != LWIP_IP_HDRINCL) {
  1062. if (src != NULL && ip6_addr_isany(src)) {
  1063. src_used = ip_2_ip6(ip6_select_source_address(netif, dest));
  1064. if ((src_used == NULL) || ip6_addr_isany(src_used)) {
  1065. /* No appropriate source address was found for this packet. */
  1066. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: No suitable source address for packet.\n"));
  1067. IP6_STATS_INC(ip6.rterr);
  1068. return ERR_RTE;
  1069. }
  1070. }
  1071. }
  1072. return ip6_output_if_src(p, src_used, dest, hl, tc, nexth, netif);
  1073. }
  1074. /**
  1075. * Same as ip6_output_if() but 'src' address is not replaced by netif address
  1076. * when it is 'any'.
  1077. */
  1078. err_t
  1079. ip6_output_if_src(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
  1080. u8_t hl, u8_t tc,
  1081. u8_t nexth, struct netif *netif)
  1082. {
  1083. struct ip6_hdr *ip6hdr;
  1084. ip6_addr_t dest_addr;
  1085. LWIP_ASSERT_CORE_LOCKED();
  1086. LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
  1087. /* Should the IPv6 header be generated or is it already included in p? */
  1088. if (dest != LWIP_IP_HDRINCL) {
  1089. #if LWIP_IPV6_SCOPES
  1090. /* If the destination address is scoped but lacks a zone, add a zone now,
  1091. * based on the outgoing interface. The lower layers (e.g., nd6) absolutely
  1092. * require addresses to be properly zoned for correctness. In some cases,
  1093. * earlier attempts will have been made to add a zone to the destination,
  1094. * but this function is the only one that is called in all (other) cases,
  1095. * so we must do this here. */
  1096. if (ip6_addr_lacks_zone(dest, IP6_UNKNOWN)) {
  1097. ip6_addr_copy(dest_addr, *dest);
  1098. ip6_addr_assign_zone(&dest_addr, IP6_UNKNOWN, netif);
  1099. dest = &dest_addr;
  1100. }
  1101. #endif /* LWIP_IPV6_SCOPES */
  1102. /* generate IPv6 header */
  1103. if (pbuf_add_header(p, IP6_HLEN)) {
  1104. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: not enough room for IPv6 header in pbuf\n"));
  1105. IP6_STATS_INC(ip6.err);
  1106. return ERR_BUF;
  1107. }
  1108. ip6hdr = (struct ip6_hdr *)p->payload;
  1109. LWIP_ASSERT("check that first pbuf can hold struct ip6_hdr",
  1110. (p->len >= sizeof(struct ip6_hdr)));
  1111. IP6H_HOPLIM_SET(ip6hdr, hl);
  1112. IP6H_NEXTH_SET(ip6hdr, nexth);
  1113. /* dest cannot be NULL here */
  1114. ip6_addr_copy_to_packed(ip6hdr->dest, *dest);
  1115. IP6H_VTCFL_SET(ip6hdr, 6, tc, 0);
  1116. IP6H_PLEN_SET(ip6hdr, (u16_t)(p->tot_len - IP6_HLEN));
  1117. if (src == NULL) {
  1118. src = IP6_ADDR_ANY6;
  1119. }
  1120. /* src cannot be NULL here */
  1121. ip6_addr_copy_to_packed(ip6hdr->src, *src);
  1122. } else {
  1123. /* IP header already included in p */
  1124. ip6hdr = (struct ip6_hdr *)p->payload;
  1125. ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
  1126. ip6_addr_assign_zone(&dest_addr, IP6_UNKNOWN, netif);
  1127. dest = &dest_addr;
  1128. }
  1129. IP6_STATS_INC(ip6.xmit);
  1130. LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
  1131. ip6_debug_print(p);
  1132. #if ENABLE_LOOPBACK
  1133. {
  1134. int i;
  1135. #if !LWIP_HAVE_LOOPIF
  1136. if (ip6_addr_isloopback(dest)) {
  1137. return netif_loop_output(netif, p);
  1138. }
  1139. #endif /* !LWIP_HAVE_LOOPIF */
  1140. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  1141. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
  1142. ip6_addr_eq(dest, netif_ip6_addr(netif, i))) {
  1143. /* Packet to self, enqueue it for loopback */
  1144. LWIP_DEBUGF(IP6_DEBUG, ("netif_loop_output()\n"));
  1145. return netif_loop_output(netif, p);
  1146. }
  1147. }
  1148. }
  1149. #if LWIP_MULTICAST_TX_OPTIONS
  1150. if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
  1151. netif_loop_output(netif, p);
  1152. }
  1153. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  1154. #endif /* ENABLE_LOOPBACK */
  1155. #if LWIP_IPV6_FRAG
  1156. /* don't fragment if interface has mtu set to 0 [loopif] */
  1157. if (netif_mtu6(netif) && (p->tot_len > nd6_get_destination_mtu(dest, netif))) {
  1158. return ip6_frag(p, netif, dest);
  1159. }
  1160. #endif /* LWIP_IPV6_FRAG */
  1161. LWIP_DEBUGF(IP6_DEBUG, ("netif->output_ip6()\n"));
  1162. return netif->output_ip6(netif, p, dest);
  1163. }
  1164. /**
  1165. * Simple interface to ip6_output_if. It finds the outgoing network
  1166. * interface and calls upon ip6_output_if to do the actual work.
  1167. *
  1168. * @param p the packet to send (p->payload points to the data, e.g. next
  1169. protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
  1170. IPv6 header and p->payload points to that IPv6 header)
  1171. * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
  1172. * IP address of the netif is selected and used as source address.
  1173. * if src == NULL, IP6_ADDR_ANY is used as source)
  1174. * @param dest the destination IPv6 address to send the packet to
  1175. * @param hl the Hop Limit value to be set in the IPv6 header
  1176. * @param tc the Traffic Class value to be set in the IPv6 header
  1177. * @param nexth the Next Header to be set in the IPv6 header
  1178. *
  1179. * @return ERR_RTE if no route is found
  1180. * see ip_output_if() for more return values
  1181. */
  1182. err_t
  1183. ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
  1184. u8_t hl, u8_t tc, u8_t nexth)
  1185. {
  1186. struct netif *netif;
  1187. struct ip6_hdr *ip6hdr;
  1188. ip6_addr_t src_addr, dest_addr;
  1189. LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
  1190. if (dest != LWIP_IP_HDRINCL) {
  1191. netif = ip6_route(src, dest);
  1192. } else {
  1193. /* IP header included in p, read addresses. */
  1194. ip6hdr = (struct ip6_hdr *)p->payload;
  1195. ip6_addr_copy_from_packed(src_addr, ip6hdr->src);
  1196. ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
  1197. netif = ip6_route(&src_addr, &dest_addr);
  1198. dest = &dest_addr;
  1199. }
  1200. if (netif == NULL) {
  1201. LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
  1202. IP6_ADDR_BLOCK1(dest),
  1203. IP6_ADDR_BLOCK2(dest),
  1204. IP6_ADDR_BLOCK3(dest),
  1205. IP6_ADDR_BLOCK4(dest),
  1206. IP6_ADDR_BLOCK5(dest),
  1207. IP6_ADDR_BLOCK6(dest),
  1208. IP6_ADDR_BLOCK7(dest),
  1209. IP6_ADDR_BLOCK8(dest)));
  1210. IP6_STATS_INC(ip6.rterr);
  1211. return ERR_RTE;
  1212. }
  1213. return ip6_output_if(p, src, dest, hl, tc, nexth, netif);
  1214. }
  1215. #if LWIP_NETIF_USE_HINTS
  1216. /** Like ip6_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
  1217. * before calling ip6_output_if.
  1218. *
  1219. * @param p the packet to send (p->payload points to the data, e.g. next
  1220. protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
  1221. IPv6 header and p->payload points to that IPv6 header)
  1222. * @param src the source IPv6 address to send from (if src == IP6_ADDR_ANY, an
  1223. * IP address of the netif is selected and used as source address.
  1224. * if src == NULL, IP6_ADDR_ANY is used as source)
  1225. * @param dest the destination IPv6 address to send the packet to
  1226. * @param hl the Hop Limit value to be set in the IPv6 header
  1227. * @param tc the Traffic Class value to be set in the IPv6 header
  1228. * @param nexth the Next Header to be set in the IPv6 header
  1229. * @param netif_hint netif output hint pointer set to netif->hint before
  1230. * calling ip_output_if()
  1231. *
  1232. * @return ERR_RTE if no route is found
  1233. * see ip_output_if() for more return values
  1234. */
  1235. err_t
  1236. ip6_output_hinted(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest,
  1237. u8_t hl, u8_t tc, u8_t nexth, struct netif_hint *netif_hint)
  1238. {
  1239. struct netif *netif;
  1240. struct ip6_hdr *ip6hdr;
  1241. ip6_addr_t src_addr, dest_addr;
  1242. err_t err;
  1243. LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
  1244. if (dest != LWIP_IP_HDRINCL) {
  1245. netif = ip6_route(src, dest);
  1246. } else {
  1247. /* IP header included in p, read addresses. */
  1248. ip6hdr = (struct ip6_hdr *)p->payload;
  1249. ip6_addr_copy_from_packed(src_addr, ip6hdr->src);
  1250. ip6_addr_copy_from_packed(dest_addr, ip6hdr->dest);
  1251. netif = ip6_route(&src_addr, &dest_addr);
  1252. dest = &dest_addr;
  1253. }
  1254. if (netif == NULL) {
  1255. LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n",
  1256. IP6_ADDR_BLOCK1(dest),
  1257. IP6_ADDR_BLOCK2(dest),
  1258. IP6_ADDR_BLOCK3(dest),
  1259. IP6_ADDR_BLOCK4(dest),
  1260. IP6_ADDR_BLOCK5(dest),
  1261. IP6_ADDR_BLOCK6(dest),
  1262. IP6_ADDR_BLOCK7(dest),
  1263. IP6_ADDR_BLOCK8(dest)));
  1264. IP6_STATS_INC(ip6.rterr);
  1265. return ERR_RTE;
  1266. }
  1267. NETIF_SET_HINTS(netif, netif_hint);
  1268. err = ip6_output_if(p, src, dest, hl, tc, nexth, netif);
  1269. NETIF_RESET_HINTS(netif);
  1270. return err;
  1271. }
  1272. #endif /* LWIP_NETIF_USE_HINTS*/
  1273. #if LWIP_IPV6_MLD
  1274. /**
  1275. * Add a hop-by-hop options header with a router alert option and padding.
  1276. *
  1277. * Used by MLD when sending a Multicast listener report/done message.
  1278. *
  1279. * @param p the packet to which we will prepend the options header
  1280. * @param nexth the next header protocol number (e.g. IP6_NEXTH_ICMP6)
  1281. * @param value the value of the router alert option data (e.g. IP6_ROUTER_ALERT_VALUE_MLD)
  1282. * @return ERR_OK if hop-by-hop header was added, ERR_* otherwise
  1283. */
  1284. err_t
  1285. ip6_options_add_hbh_ra(struct pbuf *p, u8_t nexth, u8_t value)
  1286. {
  1287. u8_t *opt_data;
  1288. u32_t offset = 0;
  1289. struct ip6_hbh_hdr *hbh_hdr;
  1290. struct ip6_opt_hdr *opt_hdr;
  1291. /* fixed 4 bytes for router alert option and 2 bytes padding */
  1292. const u8_t hlen = (sizeof(struct ip6_opt_hdr) * 2) + IP6_ROUTER_ALERT_DLEN;
  1293. /* Move pointer to make room for hop-by-hop options header. */
  1294. if (pbuf_add_header(p, sizeof(struct ip6_hbh_hdr) + hlen)) {
  1295. LWIP_DEBUGF(IP6_DEBUG, ("ip6_options: no space for options header\n"));
  1296. IP6_STATS_INC(ip6.err);
  1297. return ERR_BUF;
  1298. }
  1299. /* Set fields of Hop-by-Hop header */
  1300. hbh_hdr = (struct ip6_hbh_hdr *)p->payload;
  1301. IP6_HBH_NEXTH(hbh_hdr) = nexth;
  1302. hbh_hdr->_hlen = 0;
  1303. offset = IP6_HBH_HLEN;
  1304. /* Set router alert options to Hop-by-Hop extended option header */
  1305. opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + offset);
  1306. IP6_OPT_TYPE(opt_hdr) = IP6_ROUTER_ALERT_OPTION;
  1307. IP6_OPT_DLEN(opt_hdr) = IP6_ROUTER_ALERT_DLEN;
  1308. offset += IP6_OPT_HLEN;
  1309. /* Set router alert option data */
  1310. opt_data = (u8_t *)hbh_hdr + offset;
  1311. opt_data[0] = value;
  1312. opt_data[1] = 0;
  1313. offset += IP6_OPT_DLEN(opt_hdr);
  1314. /* add 2 bytes padding to make 8 bytes Hop-by-Hop header length */
  1315. opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + offset);
  1316. IP6_OPT_TYPE(opt_hdr) = IP6_PADN_OPTION;
  1317. IP6_OPT_DLEN(opt_hdr) = 0;
  1318. return ERR_OK;
  1319. }
  1320. #endif /* LWIP_IPV6_MLD */
  1321. #if IP6_DEBUG
  1322. /* Print an IPv6 header by using LWIP_DEBUGF
  1323. * @param p an IPv6 packet, p->payload pointing to the IPv6 header
  1324. */
  1325. void
  1326. ip6_debug_print(struct pbuf *p)
  1327. {
  1328. struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
  1329. LWIP_DEBUGF(IP6_DEBUG, ("IPv6 header:\n"));
  1330. LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
  1331. LWIP_DEBUGF(IP6_DEBUG, ("| %2"U16_F" | %3"U16_F" | %7"U32_F" | (ver, class, flow)\n",
  1332. IP6H_V(ip6hdr),
  1333. IP6H_TC(ip6hdr),
  1334. IP6H_FL(ip6hdr)));
  1335. LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
  1336. LWIP_DEBUGF(IP6_DEBUG, ("| %5"U16_F" | %3"U16_F" | %3"U16_F" | (plen, nexth, hopl)\n",
  1337. IP6H_PLEN(ip6hdr),
  1338. IP6H_NEXTH(ip6hdr),
  1339. IP6H_HOPLIM(ip6hdr)));
  1340. LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
  1341. LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (src)\n",
  1342. IP6_ADDR_BLOCK1(&(ip6hdr->src)),
  1343. IP6_ADDR_BLOCK2(&(ip6hdr->src)),
  1344. IP6_ADDR_BLOCK3(&(ip6hdr->src)),
  1345. IP6_ADDR_BLOCK4(&(ip6hdr->src))));
  1346. LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n",
  1347. IP6_ADDR_BLOCK5(&(ip6hdr->src)),
  1348. IP6_ADDR_BLOCK6(&(ip6hdr->src)),
  1349. IP6_ADDR_BLOCK7(&(ip6hdr->src)),
  1350. IP6_ADDR_BLOCK8(&(ip6hdr->src))));
  1351. LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
  1352. LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (dest)\n",
  1353. IP6_ADDR_BLOCK1(&(ip6hdr->dest)),
  1354. IP6_ADDR_BLOCK2(&(ip6hdr->dest)),
  1355. IP6_ADDR_BLOCK3(&(ip6hdr->dest)),
  1356. IP6_ADDR_BLOCK4(&(ip6hdr->dest))));
  1357. LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n",
  1358. IP6_ADDR_BLOCK5(&(ip6hdr->dest)),
  1359. IP6_ADDR_BLOCK6(&(ip6hdr->dest)),
  1360. IP6_ADDR_BLOCK7(&(ip6hdr->dest)),
  1361. IP6_ADDR_BLOCK8(&(ip6hdr->dest))));
  1362. LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n"));
  1363. }
  1364. #endif /* IP6_DEBUG */
  1365. #endif /* LWIP_IPV6 */