nd6.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  1. /**
  2. * @file
  3. *
  4. * Neighbor discovery and stateless address autoconfiguration for IPv6.
  5. * Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862
  6. * (Address autoconfiguration).
  7. */
  8. /*
  9. * Copyright (c) 2010 Inico Technologies Ltd.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the lwIP TCP/IP stack.
  35. *
  36. * Author: Ivan Delamer <delamer@inicotech.com>
  37. *
  38. *
  39. * Please coordinate changes and requests with Ivan Delamer
  40. * <delamer@inicotech.com>
  41. */
  42. #include "lwip/opt.h"
  43. #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
  44. #include "lwip/nd6.h"
  45. #include "lwip/priv/nd6_priv.h"
  46. #include "lwip/prot/nd6.h"
  47. #include "lwip/prot/icmp6.h"
  48. #include "lwip/pbuf.h"
  49. #include "lwip/mem.h"
  50. #include "lwip/memp.h"
  51. #include "lwip/ip6.h"
  52. #include "lwip/ip6_addr.h"
  53. #include "lwip/inet_chksum.h"
  54. #include "lwip/netif.h"
  55. #include "lwip/icmp6.h"
  56. #include "lwip/mld6.h"
  57. #include "lwip/dhcp6.h"
  58. #include "lwip/ip.h"
  59. #include "lwip/stats.h"
  60. #include "lwip/dns.h"
  61. #include <string.h>
  62. #ifdef LWIP_HOOK_FILENAME
  63. #include LWIP_HOOK_FILENAME
  64. #endif
  65. #if LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK
  66. #error LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK
  67. #endif
  68. #if LWIP_ND6_NUM_NEIGHBORS > 127
  69. #error LWIP_ND6_NUM_NEIGHBORS must fit into an s8_t (max value: 127)
  70. #endif
  71. #if LWIP_ND6_NUM_DESTINATIONS > 32767
  72. #error LWIP_ND6_NUM_DESTINATIONS must fit into an s16_t (max value: 32767)
  73. #endif
  74. #if LWIP_ND6_NUM_PREFIXES > 127
  75. #error LWIP_ND6_NUM_PREFIXES must fit into an s8_t (max value: 127)
  76. #endif
  77. #if LWIP_ND6_NUM_ROUTERS > 127
  78. #error LWIP_ND6_NUM_ROUTERS must fit into an s8_t (max value: 127)
  79. #endif
  80. /* Router tables. */
  81. struct nd6_neighbor_cache_entry neighbor_cache[LWIP_ND6_NUM_NEIGHBORS];
  82. struct nd6_destination_cache_entry destination_cache[LWIP_ND6_NUM_DESTINATIONS];
  83. struct nd6_prefix_list_entry prefix_list[LWIP_ND6_NUM_PREFIXES];
  84. struct nd6_router_list_entry default_router_list[LWIP_ND6_NUM_ROUTERS];
  85. /* Default values, can be updated by a RA message. */
  86. u32_t reachable_time = LWIP_ND6_REACHABLE_TIME;
  87. u32_t retrans_timer = LWIP_ND6_RETRANS_TIMER; /* @todo implement this value in timer */
  88. #if LWIP_ND6_QUEUEING
  89. static u8_t nd6_queue_size = 0;
  90. #endif
  91. /* Index for cache entries. */
  92. static netif_addr_idx_t nd6_cached_destination_index;
  93. /* Multicast address holder. */
  94. static ip6_addr_t multicast_address;
  95. static u8_t nd6_tmr_rs_reduction;
  96. /* Static buffer to parse RA packet options */
  97. union ra_options {
  98. struct lladdr_option lladdr;
  99. struct mtu_option mtu;
  100. struct prefix_option prefix;
  101. #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
  102. struct rdnss_option rdnss;
  103. #endif
  104. };
  105. static union ra_options nd6_ra_buffer;
  106. /* Forward declarations. */
  107. static s8_t nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr);
  108. static s8_t nd6_new_neighbor_cache_entry(void);
  109. static void nd6_free_neighbor_cache_entry(s8_t i);
  110. static s16_t nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr);
  111. static s16_t nd6_new_destination_cache_entry(void);
  112. static int nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif);
  113. static s8_t nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif);
  114. static s8_t nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif);
  115. static s8_t nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif);
  116. static s8_t nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif);
  117. static s8_t nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif);
  118. static s8_t nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif);
  119. static err_t nd6_queue_packet(s8_t neighbor_index, struct pbuf *q);
  120. #define ND6_SEND_FLAG_MULTICAST_DEST 0x01
  121. #define ND6_SEND_FLAG_ALLNODES_DEST 0x02
  122. #define ND6_SEND_FLAG_ANY_SRC 0x04
  123. static void nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags);
  124. static void nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags);
  125. static void nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags);
  126. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  127. static err_t nd6_send_rs(struct netif *netif);
  128. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  129. #if LWIP_ND6_QUEUEING
  130. static void nd6_free_q(struct nd6_q_entry *q);
  131. #else /* LWIP_ND6_QUEUEING */
  132. #define nd6_free_q(q) pbuf_free(q)
  133. #endif /* LWIP_ND6_QUEUEING */
  134. static void nd6_send_q(s8_t i);
  135. /**
  136. * A local address has been determined to be a duplicate. Take the appropriate
  137. * action(s) on the address and the interface as a whole.
  138. *
  139. * @param netif the netif that owns the address
  140. * @param addr_idx the index of the address detected to be a duplicate
  141. */
  142. static void
  143. nd6_duplicate_addr_detected(struct netif *netif, s8_t addr_idx)
  144. {
  145. /* Mark the address as duplicate, but leave its lifetimes alone. If this was
  146. * a manually assigned address, it will remain in existence as duplicate, and
  147. * as such be unusable for any practical purposes until manual intervention.
  148. * If this was an autogenerated address, the address will follow normal
  149. * expiration rules, and thus disappear once its valid lifetime expires. */
  150. netif_ip6_addr_set_state(netif, addr_idx, IP6_ADDR_DUPLICATED);
  151. #if LWIP_IPV6_AUTOCONFIG
  152. /* If the affected address was the link-local address that we use to generate
  153. * all other addresses, then we should not continue to use those derived
  154. * addresses either, so mark them as duplicate as well. For autoconfig-only
  155. * setups, this will make the interface effectively unusable, approaching the
  156. * intention of RFC 4862 Sec. 5.4.5. @todo implement the full requirements */
  157. if (addr_idx == 0) {
  158. s8_t i;
  159. for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  160. if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
  161. !netif_ip6_addr_isstatic(netif, i)) {
  162. netif_ip6_addr_set_state(netif, i, IP6_ADDR_DUPLICATED);
  163. }
  164. }
  165. }
  166. #endif /* LWIP_IPV6_AUTOCONFIG */
  167. }
  168. #if LWIP_IPV6_AUTOCONFIG
  169. /**
  170. * We received a router advertisement that contains a prefix with the
  171. * autoconfiguration flag set. Add or update an associated autogenerated
  172. * address.
  173. *
  174. * @param netif the netif on which the router advertisement arrived
  175. * @param prefix_opt a pointer to the prefix option data
  176. * @param prefix_addr an aligned copy of the prefix address
  177. */
  178. static void
  179. nd6_process_autoconfig_prefix(struct netif *netif,
  180. struct prefix_option *prefix_opt, const ip6_addr_t *prefix_addr)
  181. {
  182. ip6_addr_t ip6addr;
  183. u32_t valid_life, pref_life;
  184. u8_t addr_state;
  185. s8_t i, free_idx;
  186. /* The caller already checks RFC 4862 Sec. 5.5.3 points (a) and (b). We do
  187. * the rest, starting with checks for (c) and (d) here. */
  188. valid_life = lwip_htonl(prefix_opt->valid_lifetime);
  189. pref_life = lwip_htonl(prefix_opt->preferred_lifetime);
  190. if (pref_life > valid_life || prefix_opt->prefix_length != 64) {
  191. return; /* silently ignore this prefix for autoconfiguration purposes */
  192. }
  193. /* If an autogenerated address already exists for this prefix, update its
  194. * lifetimes. An address is considered autogenerated if 1) it is not static
  195. * (i.e., manually assigned), and 2) there is an advertised autoconfiguration
  196. * prefix for it (the one we are processing here). This does not necessarily
  197. * exclude the possibility that the address was actually assigned by, say,
  198. * DHCPv6. If that distinction becomes important in the future, more state
  199. * must be kept. As explained elsewhere we also update lifetimes of tentative
  200. * and duplicate addresses. Skip address slot 0 (the link-local address). */
  201. for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  202. addr_state = netif_ip6_addr_state(netif, i);
  203. if (!ip6_addr_isinvalid(addr_state) && !netif_ip6_addr_isstatic(netif, i) &&
  204. ip6_addr_net_eq(prefix_addr, netif_ip6_addr(netif, i))) {
  205. /* Update the valid lifetime, as per RFC 4862 Sec. 5.5.3 point (e).
  206. * The valid lifetime will never drop to zero as a result of this. */
  207. u32_t remaining_life = netif_ip6_addr_valid_life(netif, i);
  208. if (valid_life > ND6_2HRS || valid_life > remaining_life) {
  209. netif_ip6_addr_set_valid_life(netif, i, valid_life);
  210. } else if (remaining_life > ND6_2HRS) {
  211. netif_ip6_addr_set_valid_life(netif, i, ND6_2HRS);
  212. }
  213. LWIP_ASSERT("bad valid lifetime", !netif_ip6_addr_isstatic(netif, i));
  214. /* Update the preferred lifetime. No bounds checks are needed here. In
  215. * rare cases the advertisement may un-deprecate the address, though.
  216. * Deprecation is left to the timer code where it is handled anyway. */
  217. if (pref_life > 0 && addr_state == IP6_ADDR_DEPRECATED) {
  218. netif_ip6_addr_set_state(netif, i, IP6_ADDR_PREFERRED);
  219. }
  220. netif_ip6_addr_set_pref_life(netif, i, pref_life);
  221. return; /* there should be at most one matching address */
  222. }
  223. }
  224. /* No autogenerated address exists for this prefix yet. See if we can add a
  225. * new one. However, if IPv6 autoconfiguration is administratively disabled,
  226. * do not generate new addresses, but do keep updating lifetimes for existing
  227. * addresses. Also, when adding new addresses, we must protect explicitly
  228. * against a valid lifetime of zero, because again, we use that as a special
  229. * value. The generated address would otherwise expire immediately anyway.
  230. * Finally, the original link-local address must be usable at all. We start
  231. * creating addresses even if the link-local address is still in tentative
  232. * state though, and deal with the fallout of that upon DAD collision. */
  233. addr_state = netif_ip6_addr_state(netif, 0);
  234. if (!netif->ip6_autoconfig_enabled || valid_life == IP6_ADDR_LIFE_STATIC ||
  235. ip6_addr_isinvalid(addr_state) || ip6_addr_isduplicated(addr_state)) {
  236. return;
  237. }
  238. /* Construct the new address that we intend to use, and then see if that
  239. * address really does not exist. It might have been added manually, after
  240. * all. As a side effect, find a free slot. Note that we cannot use
  241. * netif_add_ip6_address() here, as it would return ERR_OK if the address
  242. * already did exist, resulting in that address being given lifetimes. */
  243. IP6_ADDR(&ip6addr, prefix_addr->addr[0], prefix_addr->addr[1],
  244. netif_ip6_addr(netif, 0)->addr[2], netif_ip6_addr(netif, 0)->addr[3]);
  245. ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif);
  246. free_idx = 0;
  247. for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  248. if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) {
  249. if (ip6_addr_eq(&ip6addr, netif_ip6_addr(netif, i))) {
  250. return; /* formed address already exists */
  251. }
  252. } else if (free_idx == 0) {
  253. free_idx = i;
  254. }
  255. }
  256. if (free_idx == 0) {
  257. return; /* no address slots available, try again on next advertisement */
  258. }
  259. /* Assign the new address to the interface. */
  260. ip_addr_copy_from_ip6(netif->ip6_addr[free_idx], ip6addr);
  261. netif_ip6_addr_set_valid_life(netif, free_idx, valid_life);
  262. netif_ip6_addr_set_pref_life(netif, free_idx, pref_life);
  263. netif_ip6_addr_set_state(netif, free_idx, IP6_ADDR_TENTATIVE);
  264. }
  265. #endif /* LWIP_IPV6_AUTOCONFIG */
  266. /**
  267. * Process an incoming neighbor discovery message
  268. *
  269. * @param p the nd packet, p->payload pointing to the icmpv6 header
  270. * @param inp the netif on which this packet was received
  271. */
  272. void
  273. nd6_input(struct pbuf *p, struct netif *inp)
  274. {
  275. u8_t msg_type;
  276. s8_t i;
  277. s16_t dest_idx;
  278. ND6_STATS_INC(nd6.recv);
  279. msg_type = *((u8_t *)p->payload);
  280. switch (msg_type) {
  281. case ICMP6_TYPE_NA: /* Neighbor Advertisement. */
  282. {
  283. struct na_header *na_hdr;
  284. struct lladdr_option *lladdr_opt;
  285. ip6_addr_t target_address;
  286. /* Check that na header fits in packet. */
  287. if (p->len < (sizeof(struct na_header))) {
  288. /* @todo debug message */
  289. pbuf_free(p);
  290. ND6_STATS_INC(nd6.lenerr);
  291. ND6_STATS_INC(nd6.drop);
  292. return;
  293. }
  294. na_hdr = (struct na_header *)p->payload;
  295. /* Create an aligned, zoned copy of the target address. */
  296. ip6_addr_copy_from_packed(target_address, na_hdr->target_address);
  297. ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp);
  298. /* Check a subset of the other RFC 4861 Sec. 7.1.2 requirements. */
  299. if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || na_hdr->code != 0 ||
  300. ip6_addr_ismulticast(&target_address)) {
  301. pbuf_free(p);
  302. ND6_STATS_INC(nd6.proterr);
  303. ND6_STATS_INC(nd6.drop);
  304. return;
  305. }
  306. /* @todo RFC MUST: if IP destination is multicast, Solicited flag is zero */
  307. /* @todo RFC MUST: all included options have a length greater than zero */
  308. /* Unsolicited NA?*/
  309. if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
  310. /* This is an unsolicited NA.
  311. * link-layer changed?
  312. * part of DAD mechanism? */
  313. #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
  314. /* If the target address matches this netif, it is a DAD response. */
  315. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  316. if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) &&
  317. !ip6_addr_isduplicated(netif_ip6_addr_state(inp, i)) &&
  318. ip6_addr_eq(&target_address, netif_ip6_addr(inp, i))) {
  319. /* We are using a duplicate address. */
  320. nd6_duplicate_addr_detected(inp, i);
  321. pbuf_free(p);
  322. return;
  323. }
  324. }
  325. #endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */
  326. /* Check that link-layer address option also fits in packet. */
  327. if (p->len < (sizeof(struct na_header) + 2)) {
  328. /* @todo debug message */
  329. pbuf_free(p);
  330. ND6_STATS_INC(nd6.lenerr);
  331. ND6_STATS_INC(nd6.drop);
  332. return;
  333. }
  334. lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
  335. if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) {
  336. /* @todo debug message */
  337. pbuf_free(p);
  338. ND6_STATS_INC(nd6.lenerr);
  339. ND6_STATS_INC(nd6.drop);
  340. return;
  341. }
  342. /* This is an unsolicited NA, most likely there was a LLADDR change. */
  343. i = nd6_find_neighbor_cache_entry(&target_address);
  344. if (i >= 0) {
  345. if (na_hdr->flags & ND6_FLAG_OVERRIDE) {
  346. MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
  347. }
  348. }
  349. } else {
  350. /* This is a solicited NA.
  351. * neighbor address resolution response?
  352. * neighbor unreachability detection response? */
  353. /* Find the cache entry corresponding to this na. */
  354. i = nd6_find_neighbor_cache_entry(&target_address);
  355. if (i < 0) {
  356. /* We no longer care about this target address. drop it. */
  357. pbuf_free(p);
  358. return;
  359. }
  360. /* Update cache entry. */
  361. if ((na_hdr->flags & ND6_FLAG_OVERRIDE) ||
  362. (neighbor_cache[i].state == ND6_INCOMPLETE)) {
  363. /* Check that link-layer address option also fits in packet. */
  364. if (p->len < (sizeof(struct na_header) + 2)) {
  365. /* @todo debug message */
  366. pbuf_free(p);
  367. ND6_STATS_INC(nd6.lenerr);
  368. ND6_STATS_INC(nd6.drop);
  369. return;
  370. }
  371. lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
  372. if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) {
  373. /* @todo debug message */
  374. pbuf_free(p);
  375. ND6_STATS_INC(nd6.lenerr);
  376. ND6_STATS_INC(nd6.drop);
  377. return;
  378. }
  379. MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
  380. }
  381. neighbor_cache[i].netif = inp;
  382. neighbor_cache[i].state = ND6_REACHABLE;
  383. neighbor_cache[i].counter.reachable_time = reachable_time;
  384. /* Send queued packets, if any. */
  385. if (neighbor_cache[i].q != NULL) {
  386. nd6_send_q(i);
  387. }
  388. }
  389. break; /* ICMP6_TYPE_NA */
  390. }
  391. case ICMP6_TYPE_NS: /* Neighbor solicitation. */
  392. {
  393. struct ns_header *ns_hdr;
  394. struct lladdr_option *lladdr_opt;
  395. ip6_addr_t target_address;
  396. u8_t accepted;
  397. /* Check that ns header fits in packet. */
  398. if (p->len < sizeof(struct ns_header)) {
  399. /* @todo debug message */
  400. pbuf_free(p);
  401. ND6_STATS_INC(nd6.lenerr);
  402. ND6_STATS_INC(nd6.drop);
  403. return;
  404. }
  405. ns_hdr = (struct ns_header *)p->payload;
  406. /* Create an aligned, zoned copy of the target address. */
  407. ip6_addr_copy_from_packed(target_address, ns_hdr->target_address);
  408. ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp);
  409. /* Check a subset of the other RFC 4861 Sec. 7.1.1 requirements. */
  410. if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ns_hdr->code != 0 ||
  411. ip6_addr_ismulticast(&target_address)) {
  412. pbuf_free(p);
  413. ND6_STATS_INC(nd6.proterr);
  414. ND6_STATS_INC(nd6.drop);
  415. return;
  416. }
  417. /* @todo RFC MUST: all included options have a length greater than zero */
  418. /* @todo RFC MUST: if IP source is 'any', destination is solicited-node multicast address */
  419. /* @todo RFC MUST: if IP source is 'any', there is no source LL address option */
  420. /* Check if there is a link-layer address provided. Only point to it if in this buffer. */
  421. if (p->len >= (sizeof(struct ns_header) + 2)) {
  422. lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
  423. if (p->len < (sizeof(struct ns_header) + (lladdr_opt->length << 3))) {
  424. lladdr_opt = NULL;
  425. }
  426. } else {
  427. lladdr_opt = NULL;
  428. }
  429. /* Check if the target address is configured on the receiving netif. */
  430. accepted = 0;
  431. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
  432. if ((ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) ||
  433. (ip6_addr_istentative(netif_ip6_addr_state(inp, i)) &&
  434. ip6_addr_isany(ip6_current_src_addr()))) &&
  435. ip6_addr_eq(&target_address, netif_ip6_addr(inp, i))) {
  436. accepted = 1;
  437. break;
  438. }
  439. }
  440. /* NS not for us? */
  441. if (!accepted) {
  442. pbuf_free(p);
  443. return;
  444. }
  445. /* Check for ANY address in src (DAD algorithm). */
  446. if (ip6_addr_isany(ip6_current_src_addr())) {
  447. /* Sender is validating this address. */
  448. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
  449. if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) &&
  450. ip6_addr_eq(&target_address, netif_ip6_addr(inp, i))) {
  451. /* Send a NA back so that the sender does not use this address. */
  452. nd6_send_na(inp, netif_ip6_addr(inp, i), ND6_FLAG_OVERRIDE | ND6_SEND_FLAG_ALLNODES_DEST);
  453. if (ip6_addr_istentative(netif_ip6_addr_state(inp, i))) {
  454. /* We shouldn't use this address either. */
  455. nd6_duplicate_addr_detected(inp, i);
  456. }
  457. }
  458. }
  459. } else {
  460. /* Sender is trying to resolve our address. */
  461. /* Verify that they included their own link-layer address. */
  462. if (lladdr_opt == NULL) {
  463. /* Not a valid message. */
  464. pbuf_free(p);
  465. ND6_STATS_INC(nd6.proterr);
  466. ND6_STATS_INC(nd6.drop);
  467. return;
  468. }
  469. i = nd6_find_neighbor_cache_entry(ip6_current_src_addr());
  470. if (i>= 0) {
  471. /* We already have a record for the solicitor. */
  472. if (neighbor_cache[i].state == ND6_INCOMPLETE) {
  473. neighbor_cache[i].netif = inp;
  474. MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
  475. /* Delay probe in case we get confirmation of reachability from upper layer (TCP). */
  476. neighbor_cache[i].state = ND6_DELAY;
  477. neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
  478. }
  479. } else {
  480. /* Add their IPv6 address and link-layer address to neighbor cache.
  481. * We will need it at least to send a unicast NA message, but most
  482. * likely we will also be communicating with this node soon. */
  483. i = nd6_new_neighbor_cache_entry();
  484. if (i < 0) {
  485. /* We couldn't assign a cache entry for this neighbor.
  486. * we won't be able to reply. drop it. */
  487. pbuf_free(p);
  488. ND6_STATS_INC(nd6.memerr);
  489. return;
  490. }
  491. neighbor_cache[i].netif = inp;
  492. MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
  493. ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr());
  494. /* Receiving a message does not prove reachability: only in one direction.
  495. * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
  496. neighbor_cache[i].state = ND6_DELAY;
  497. neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
  498. }
  499. /* Send back a NA for us. Allocate the reply pbuf. */
  500. nd6_send_na(inp, &target_address, ND6_FLAG_SOLICITED | ND6_FLAG_OVERRIDE);
  501. }
  502. break; /* ICMP6_TYPE_NS */
  503. }
  504. case ICMP6_TYPE_RA: /* Router Advertisement. */
  505. {
  506. struct ra_header *ra_hdr;
  507. u8_t *buffer; /* Used to copy options. */
  508. u16_t offset;
  509. #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
  510. /* There can be multiple RDNSS options per RA */
  511. u8_t rdnss_server_idx = 0;
  512. #endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */
  513. /* Check that RA header fits in packet. */
  514. if (p->len < sizeof(struct ra_header)) {
  515. /* @todo debug message */
  516. pbuf_free(p);
  517. ND6_STATS_INC(nd6.lenerr);
  518. ND6_STATS_INC(nd6.drop);
  519. return;
  520. }
  521. ra_hdr = (struct ra_header *)p->payload;
  522. /* Check a subset of the other RFC 4861 Sec. 6.1.2 requirements. */
  523. if (!ip6_addr_islinklocal(ip6_current_src_addr()) ||
  524. IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ra_hdr->code != 0) {
  525. pbuf_free(p);
  526. ND6_STATS_INC(nd6.proterr);
  527. ND6_STATS_INC(nd6.drop);
  528. return;
  529. }
  530. /* @todo RFC MUST: all included options have a length greater than zero */
  531. /* If we are sending RS messages, stop. */
  532. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  533. /* ensure at least one solicitation is sent (see RFC 4861, ch. 6.3.7) */
  534. if ((inp->rs_count < LWIP_ND6_MAX_MULTICAST_SOLICIT) ||
  535. (nd6_send_rs(inp) == ERR_OK)) {
  536. inp->rs_count = 0;
  537. } else {
  538. inp->rs_count = 1;
  539. }
  540. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  541. /* Get the matching default router entry. */
  542. i = nd6_get_router(ip6_current_src_addr(), inp);
  543. if (i < 0) {
  544. /* Create a new router entry. */
  545. i = nd6_new_router(ip6_current_src_addr(), inp);
  546. }
  547. if (i < 0) {
  548. /* Could not create a new router entry. */
  549. pbuf_free(p);
  550. ND6_STATS_INC(nd6.memerr);
  551. return;
  552. }
  553. /* Re-set invalidation timer. */
  554. default_router_list[i].invalidation_timer = lwip_htons(ra_hdr->router_lifetime);
  555. /* Re-set default timer values. */
  556. #if LWIP_ND6_ALLOW_RA_UPDATES
  557. if (ra_hdr->retrans_timer > 0) {
  558. retrans_timer = lwip_htonl(ra_hdr->retrans_timer);
  559. }
  560. if (ra_hdr->reachable_time > 0) {
  561. reachable_time = lwip_htonl(ra_hdr->reachable_time);
  562. }
  563. #endif /* LWIP_ND6_ALLOW_RA_UPDATES */
  564. /* @todo set default hop limit... */
  565. /* ra_hdr->current_hop_limit;*/
  566. /* Update flags in local entry (incl. preference). */
  567. default_router_list[i].flags = ra_hdr->flags;
  568. #if LWIP_IPV6_DHCP6
  569. /* Trigger DHCPv6 if enabled */
  570. dhcp6_nd6_ra_trigger(inp, ra_hdr->flags & ND6_RA_FLAG_MANAGED_ADDR_CONFIG,
  571. ra_hdr->flags & ND6_RA_FLAG_OTHER_CONFIG);
  572. #endif
  573. /* Offset to options. */
  574. offset = sizeof(struct ra_header);
  575. /* Process each option. */
  576. while ((p->tot_len - offset) >= 2) {
  577. u8_t option_type;
  578. u16_t option_len;
  579. int option_len8 = pbuf_try_get_at(p, offset + 1);
  580. if (option_len8 <= 0) {
  581. /* read beyond end or zero length */
  582. goto lenerr_drop_free_return;
  583. }
  584. option_len = ((u8_t)option_len8) << 3;
  585. if (option_len > p->tot_len - offset) {
  586. /* short packet (option does not fit in) */
  587. goto lenerr_drop_free_return;
  588. }
  589. if (p->len == p->tot_len) {
  590. /* no need to copy from contiguous pbuf */
  591. buffer = &((u8_t*)p->payload)[offset];
  592. } else {
  593. /* check if this option fits into our buffer */
  594. if (option_len > sizeof(nd6_ra_buffer)) {
  595. option_type = pbuf_get_at(p, offset);
  596. /* invalid option length */
  597. if (option_type != ND6_OPTION_TYPE_RDNSS) {
  598. goto lenerr_drop_free_return;
  599. }
  600. /* we allow RDNSS option to be longer - we'll just drop some servers */
  601. option_len = sizeof(nd6_ra_buffer);
  602. }
  603. buffer = (u8_t*)&nd6_ra_buffer;
  604. option_len = pbuf_copy_partial(p, &nd6_ra_buffer, option_len, offset);
  605. }
  606. option_type = buffer[0];
  607. switch (option_type) {
  608. case ND6_OPTION_TYPE_SOURCE_LLADDR:
  609. {
  610. struct lladdr_option *lladdr_opt;
  611. if (option_len < ND6_LLADDR_OPTION_MIN_LENGTH) {
  612. goto lenerr_drop_free_return;
  613. }
  614. lladdr_opt = (struct lladdr_option *)buffer;
  615. if ((lladdr_opt->length == inp->hwaddr_len) &&
  616. (default_router_list[i].neighbor_entry != NULL) &&
  617. (default_router_list[i].neighbor_entry->state == ND6_INCOMPLETE)) {
  618. SMEMCPY(default_router_list[i].neighbor_entry->lladdr, lladdr_opt->addr, inp->hwaddr_len);
  619. default_router_list[i].neighbor_entry->state = ND6_REACHABLE;
  620. default_router_list[i].neighbor_entry->counter.reachable_time = reachable_time;
  621. }
  622. break;
  623. }
  624. case ND6_OPTION_TYPE_MTU:
  625. {
  626. struct mtu_option *mtu_opt;
  627. u32_t mtu32;
  628. if (option_len < sizeof(struct mtu_option)) {
  629. goto lenerr_drop_free_return;
  630. }
  631. mtu_opt = (struct mtu_option *)buffer;
  632. mtu32 = lwip_htonl(mtu_opt->mtu);
  633. if ((mtu32 >= IP6_MIN_MTU_LENGTH) && (mtu32 <= 0xffff)) {
  634. #if LWIP_ND6_ALLOW_RA_UPDATES
  635. if (inp->mtu) {
  636. /* don't set the mtu for IPv6 higher than the netif driver supports */
  637. inp->mtu6 = LWIP_MIN(LWIP_MIN(inp->mtu, inp->mtu6), (u16_t)mtu32);
  638. } else {
  639. inp->mtu6 = (u16_t)mtu32;
  640. }
  641. #endif /* LWIP_ND6_ALLOW_RA_UPDATES */
  642. }
  643. break;
  644. }
  645. case ND6_OPTION_TYPE_PREFIX_INFO:
  646. {
  647. struct prefix_option *prefix_opt;
  648. ip6_addr_t prefix_addr;
  649. if (option_len < sizeof(struct prefix_option)) {
  650. goto lenerr_drop_free_return;
  651. }
  652. prefix_opt = (struct prefix_option *)buffer;
  653. /* Get a memory-aligned copy of the prefix. */
  654. ip6_addr_copy_from_packed(prefix_addr, prefix_opt->prefix);
  655. ip6_addr_assign_zone(&prefix_addr, IP6_UNICAST, inp);
  656. if (!ip6_addr_islinklocal(&prefix_addr)) {
  657. if ((prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) &&
  658. (prefix_opt->prefix_length == 64)) {
  659. /* Add to on-link prefix list. */
  660. u32_t valid_life;
  661. s8_t prefix;
  662. valid_life = lwip_htonl(prefix_opt->valid_lifetime);
  663. /* find cache entry for this prefix. */
  664. prefix = nd6_get_onlink_prefix(&prefix_addr, inp);
  665. if (prefix < 0 && valid_life > 0) {
  666. /* Create a new cache entry. */
  667. prefix = nd6_new_onlink_prefix(&prefix_addr, inp);
  668. }
  669. if (prefix >= 0) {
  670. prefix_list[prefix].invalidation_timer = valid_life;
  671. }
  672. }
  673. #if LWIP_IPV6_AUTOCONFIG
  674. if (prefix_opt->flags & ND6_PREFIX_FLAG_AUTONOMOUS) {
  675. /* Perform processing for autoconfiguration. */
  676. nd6_process_autoconfig_prefix(inp, prefix_opt, &prefix_addr);
  677. }
  678. #endif /* LWIP_IPV6_AUTOCONFIG */
  679. }
  680. break;
  681. }
  682. case ND6_OPTION_TYPE_ROUTE_INFO:
  683. /* @todo implement preferred routes.
  684. struct route_option * route_opt;
  685. route_opt = (struct route_option *)buffer;*/
  686. break;
  687. #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS
  688. case ND6_OPTION_TYPE_RDNSS:
  689. {
  690. u8_t num, n;
  691. u16_t copy_offset = offset + SIZEOF_RDNSS_OPTION_BASE;
  692. struct rdnss_option * rdnss_opt;
  693. if (option_len < SIZEOF_RDNSS_OPTION_BASE) {
  694. goto lenerr_drop_free_return;
  695. }
  696. rdnss_opt = (struct rdnss_option *)buffer;
  697. num = (rdnss_opt->length - 1) / 2;
  698. for (n = 0; (rdnss_server_idx < DNS_MAX_SERVERS) && (n < num); n++, copy_offset += sizeof(ip6_addr_p_t)) {
  699. ip_addr_t rdnss_address;
  700. /* Copy directly from pbuf to get an aligned, zoned copy of the prefix. */
  701. if (pbuf_copy_partial(p, &rdnss_address, sizeof(ip6_addr_p_t), copy_offset) == sizeof(ip6_addr_p_t)) {
  702. IP_SET_TYPE_VAL(rdnss_address, IPADDR_TYPE_V6);
  703. ip6_addr_assign_zone(ip_2_ip6(&rdnss_address), IP6_UNKNOWN, inp);
  704. if (htonl(rdnss_opt->lifetime) > 0) {
  705. /* TODO implement Lifetime > 0 */
  706. dns_setserver(rdnss_server_idx++, &rdnss_address);
  707. } else {
  708. /* TODO implement DNS removal in dns.c */
  709. u8_t s;
  710. for (s = 0; s < DNS_MAX_SERVERS; s++) {
  711. const ip_addr_t *addr = dns_getserver(s);
  712. if(ip_addr_eq(addr, &rdnss_address)) {
  713. dns_setserver(s, NULL);
  714. }
  715. }
  716. }
  717. }
  718. }
  719. break;
  720. }
  721. #endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */
  722. default:
  723. /* Unrecognized option, abort. */
  724. ND6_STATS_INC(nd6.proterr);
  725. break;
  726. }
  727. /* option length is checked earlier to be non-zero to make sure loop ends */
  728. offset += 8 * (u8_t)option_len8;
  729. }
  730. break; /* ICMP6_TYPE_RA */
  731. }
  732. case ICMP6_TYPE_RD: /* Redirect */
  733. {
  734. struct redirect_header *redir_hdr;
  735. struct lladdr_option *lladdr_opt;
  736. ip6_addr_t destination_address, target_address;
  737. /* Check that Redir header fits in packet. */
  738. if (p->len < sizeof(struct redirect_header)) {
  739. /* @todo debug message */
  740. pbuf_free(p);
  741. ND6_STATS_INC(nd6.lenerr);
  742. ND6_STATS_INC(nd6.drop);
  743. return;
  744. }
  745. redir_hdr = (struct redirect_header *)p->payload;
  746. /* Create an aligned, zoned copy of the destination address. */
  747. ip6_addr_copy_from_packed(destination_address, redir_hdr->destination_address);
  748. ip6_addr_assign_zone(&destination_address, IP6_UNICAST, inp);
  749. /* Check a subset of the other RFC 4861 Sec. 8.1 requirements. */
  750. if (!ip6_addr_islinklocal(ip6_current_src_addr()) ||
  751. IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM ||
  752. redir_hdr->code != 0 || ip6_addr_ismulticast(&destination_address)) {
  753. pbuf_free(p);
  754. ND6_STATS_INC(nd6.proterr);
  755. ND6_STATS_INC(nd6.drop);
  756. return;
  757. }
  758. /* @todo RFC MUST: IP source address equals first-hop router for destination_address */
  759. /* @todo RFC MUST: ICMP target address is either link-local address or same as destination_address */
  760. /* @todo RFC MUST: all included options have a length greater than zero */
  761. if (p->len >= (sizeof(struct redirect_header) + 2)) {
  762. lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct redirect_header));
  763. if (p->len < (sizeof(struct redirect_header) + (lladdr_opt->length << 3))) {
  764. lladdr_opt = NULL;
  765. }
  766. } else {
  767. lladdr_opt = NULL;
  768. }
  769. /* Find dest address in cache */
  770. dest_idx = nd6_find_destination_cache_entry(&destination_address);
  771. if (dest_idx < 0) {
  772. /* Destination not in cache, drop packet. */
  773. pbuf_free(p);
  774. return;
  775. }
  776. /* Create an aligned, zoned copy of the target address. */
  777. ip6_addr_copy_from_packed(target_address, redir_hdr->target_address);
  778. ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp);
  779. /* Set the new target address. */
  780. ip6_addr_copy(destination_cache[dest_idx].next_hop_addr, target_address);
  781. /* If Link-layer address of other router is given, try to add to neighbor cache. */
  782. if (lladdr_opt != NULL) {
  783. if (lladdr_opt->type == ND6_OPTION_TYPE_TARGET_LLADDR) {
  784. i = nd6_find_neighbor_cache_entry(&target_address);
  785. if (i < 0) {
  786. i = nd6_new_neighbor_cache_entry();
  787. if (i >= 0) {
  788. neighbor_cache[i].netif = inp;
  789. MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
  790. ip6_addr_copy(neighbor_cache[i].next_hop_address, target_address);
  791. /* Receiving a message does not prove reachability: only in one direction.
  792. * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
  793. neighbor_cache[i].state = ND6_DELAY;
  794. neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
  795. }
  796. }
  797. if (i >= 0) {
  798. if (neighbor_cache[i].state == ND6_INCOMPLETE) {
  799. MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len);
  800. /* Receiving a message does not prove reachability: only in one direction.
  801. * Delay probe in case we get confirmation of reachability from upper layer (TCP). */
  802. neighbor_cache[i].state = ND6_DELAY;
  803. neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
  804. }
  805. }
  806. }
  807. }
  808. break; /* ICMP6_TYPE_RD */
  809. }
  810. case ICMP6_TYPE_PTB: /* Packet too big */
  811. {
  812. struct icmp6_hdr *icmp6hdr; /* Packet too big message */
  813. struct ip6_hdr *ip6hdr; /* IPv6 header of the packet which caused the error */
  814. u32_t pmtu;
  815. ip6_addr_t destination_address;
  816. /* Check that ICMPv6 header + IPv6 header fit in payload */
  817. if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) {
  818. /* drop short packets */
  819. pbuf_free(p);
  820. ND6_STATS_INC(nd6.lenerr);
  821. ND6_STATS_INC(nd6.drop);
  822. return;
  823. }
  824. icmp6hdr = (struct icmp6_hdr *)p->payload;
  825. ip6hdr = (struct ip6_hdr *)((u8_t*)p->payload + sizeof(struct icmp6_hdr));
  826. /* Create an aligned, zoned copy of the destination address. */
  827. ip6_addr_copy_from_packed(destination_address, ip6hdr->dest);
  828. ip6_addr_assign_zone(&destination_address, IP6_UNKNOWN, inp);
  829. /* Look for entry in destination cache. */
  830. dest_idx = nd6_find_destination_cache_entry(&destination_address);
  831. if (dest_idx < 0) {
  832. /* Destination not in cache, drop packet. */
  833. pbuf_free(p);
  834. return;
  835. }
  836. /* Change the Path MTU. */
  837. pmtu = lwip_htonl(icmp6hdr->data);
  838. destination_cache[dest_idx].pmtu = (u16_t)LWIP_MIN(pmtu, 0xFFFF);
  839. break; /* ICMP6_TYPE_PTB */
  840. }
  841. default:
  842. ND6_STATS_INC(nd6.proterr);
  843. ND6_STATS_INC(nd6.drop);
  844. break; /* default */
  845. }
  846. pbuf_free(p);
  847. return;
  848. lenerr_drop_free_return:
  849. ND6_STATS_INC(nd6.lenerr);
  850. ND6_STATS_INC(nd6.drop);
  851. pbuf_free(p);
  852. }
  853. /**
  854. * Periodic timer for Neighbor discovery functions:
  855. *
  856. * - Update neighbor reachability states
  857. * - Update destination cache entries age
  858. * - Update invalidation timers of default routers and on-link prefixes
  859. * - Update lifetimes of our addresses
  860. * - Perform duplicate address detection (DAD) for our addresses
  861. * - Send router solicitations
  862. */
  863. void
  864. nd6_tmr(void)
  865. {
  866. s8_t i;
  867. struct netif *netif;
  868. /* Process neighbor entries. */
  869. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  870. switch (neighbor_cache[i].state) {
  871. case ND6_INCOMPLETE:
  872. if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) &&
  873. (!neighbor_cache[i].isrouter)) {
  874. /* Retries exceeded. */
  875. nd6_free_neighbor_cache_entry(i);
  876. } else {
  877. /* Send a NS for this entry. */
  878. neighbor_cache[i].counter.probes_sent++;
  879. nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST);
  880. }
  881. break;
  882. case ND6_REACHABLE:
  883. /* Send queued packets, if any are left. Should have been sent already. */
  884. if (neighbor_cache[i].q != NULL) {
  885. nd6_send_q(i);
  886. }
  887. if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) {
  888. /* Change to stale state. */
  889. neighbor_cache[i].state = ND6_STALE;
  890. neighbor_cache[i].counter.stale_time = 0;
  891. } else {
  892. neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL;
  893. }
  894. break;
  895. case ND6_STALE:
  896. neighbor_cache[i].counter.stale_time++;
  897. break;
  898. case ND6_DELAY:
  899. if (neighbor_cache[i].counter.delay_time <= 1) {
  900. /* Change to PROBE state. */
  901. neighbor_cache[i].state = ND6_PROBE;
  902. neighbor_cache[i].counter.probes_sent = 0;
  903. } else {
  904. neighbor_cache[i].counter.delay_time--;
  905. }
  906. break;
  907. case ND6_PROBE:
  908. if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) &&
  909. (!neighbor_cache[i].isrouter)) {
  910. /* Retries exceeded. */
  911. nd6_free_neighbor_cache_entry(i);
  912. } else {
  913. /* Send a NS for this entry. */
  914. neighbor_cache[i].counter.probes_sent++;
  915. nd6_send_neighbor_cache_probe(&neighbor_cache[i], 0);
  916. }
  917. break;
  918. case ND6_NO_ENTRY:
  919. default:
  920. /* Do nothing. */
  921. break;
  922. }
  923. }
  924. /* Process destination entries. */
  925. for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
  926. destination_cache[i].age++;
  927. }
  928. /* Process router entries. */
  929. for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
  930. if (default_router_list[i].neighbor_entry != NULL) {
  931. /* Active entry. */
  932. if (default_router_list[i].invalidation_timer <= ND6_TMR_INTERVAL / 1000) {
  933. /* No more than 1 second remaining. Clear this entry. Also clear any of
  934. * its destination cache entries, as per RFC 4861 Sec. 5.3 and 6.3.5. */
  935. s8_t j;
  936. for (j = 0; j < LWIP_ND6_NUM_DESTINATIONS; j++) {
  937. if (ip6_addr_eq(&destination_cache[j].next_hop_addr,
  938. &default_router_list[i].neighbor_entry->next_hop_address)) {
  939. ip6_addr_set_any(&destination_cache[j].destination_addr);
  940. }
  941. }
  942. default_router_list[i].neighbor_entry->isrouter = 0;
  943. default_router_list[i].neighbor_entry = NULL;
  944. default_router_list[i].invalidation_timer = 0;
  945. default_router_list[i].flags = 0;
  946. } else {
  947. default_router_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
  948. }
  949. }
  950. }
  951. /* Process prefix entries. */
  952. for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
  953. if (prefix_list[i].netif != NULL) {
  954. if (prefix_list[i].invalidation_timer <= ND6_TMR_INTERVAL / 1000) {
  955. /* Entry timed out, remove it */
  956. prefix_list[i].invalidation_timer = 0;
  957. prefix_list[i].netif = NULL;
  958. } else {
  959. prefix_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000;
  960. }
  961. }
  962. }
  963. /* Process our own addresses, updating address lifetimes and/or DAD state. */
  964. NETIF_FOREACH(netif) {
  965. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
  966. u8_t addr_state;
  967. #if LWIP_IPV6_ADDRESS_LIFETIMES
  968. /* Step 1: update address lifetimes (valid and preferred). */
  969. addr_state = netif_ip6_addr_state(netif, i);
  970. /* RFC 4862 is not entirely clear as to whether address lifetimes affect
  971. * tentative addresses, and is even less clear as to what should happen
  972. * with duplicate addresses. We choose to track and update lifetimes for
  973. * both those types, although for different reasons:
  974. * - for tentative addresses, the line of thought of Sec. 5.7 combined
  975. * with the potentially long period that an address may be in tentative
  976. * state (due to the interface being down) suggests that lifetimes
  977. * should be independent of external factors which would include DAD;
  978. * - for duplicate addresses, retiring them early could result in a new
  979. * but unwanted attempt at marking them as valid, while retiring them
  980. * late/never could clog up address slots on the netif.
  981. * As a result, we may end up expiring addresses of either type here.
  982. */
  983. if (!ip6_addr_isinvalid(addr_state) &&
  984. !netif_ip6_addr_isstatic(netif, i)) {
  985. u32_t life = netif_ip6_addr_valid_life(netif, i);
  986. if (life <= ND6_TMR_INTERVAL / 1000) {
  987. /* The address has expired. */
  988. netif_ip6_addr_set_valid_life(netif, i, 0);
  989. netif_ip6_addr_set_pref_life(netif, i, 0);
  990. netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
  991. } else {
  992. if (!ip6_addr_life_isinfinite(life)) {
  993. life -= ND6_TMR_INTERVAL / 1000;
  994. LWIP_ASSERT("bad valid lifetime", life != IP6_ADDR_LIFE_STATIC);
  995. netif_ip6_addr_set_valid_life(netif, i, life);
  996. }
  997. /* The address is still here. Update the preferred lifetime too. */
  998. life = netif_ip6_addr_pref_life(netif, i);
  999. if (life <= ND6_TMR_INTERVAL / 1000) {
  1000. /* This case must also trigger if 'life' was already zero, so as to
  1001. * deal correctly with advertised preferred-lifetime reductions. */
  1002. netif_ip6_addr_set_pref_life(netif, i, 0);
  1003. if (addr_state == IP6_ADDR_PREFERRED)
  1004. netif_ip6_addr_set_state(netif, i, IP6_ADDR_DEPRECATED);
  1005. } else if (!ip6_addr_life_isinfinite(life)) {
  1006. life -= ND6_TMR_INTERVAL / 1000;
  1007. netif_ip6_addr_set_pref_life(netif, i, life);
  1008. }
  1009. }
  1010. }
  1011. /* The address state may now have changed, so reobtain it next. */
  1012. #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */
  1013. /* Step 2: update DAD state. */
  1014. addr_state = netif_ip6_addr_state(netif, i);
  1015. if (ip6_addr_istentative(addr_state)) {
  1016. if ((addr_state & IP6_ADDR_TENTATIVE_COUNT_MASK) >= LWIP_IPV6_DUP_DETECT_ATTEMPTS) {
  1017. /* No NA received in response. Mark address as valid. For dynamic
  1018. * addresses with an expired preferred lifetime, the state is set to
  1019. * deprecated right away. That should almost never happen, though. */
  1020. addr_state = IP6_ADDR_PREFERRED;
  1021. #if LWIP_IPV6_ADDRESS_LIFETIMES
  1022. if (!netif_ip6_addr_isstatic(netif, i) &&
  1023. netif_ip6_addr_pref_life(netif, i) == 0) {
  1024. addr_state = IP6_ADDR_DEPRECATED;
  1025. }
  1026. #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */
  1027. netif_ip6_addr_set_state(netif, i, addr_state);
  1028. } else if (netif_is_up(netif) && netif_is_link_up(netif)) {
  1029. /* tentative: set next state by increasing by one */
  1030. netif_ip6_addr_set_state(netif, i, addr_state + 1);
  1031. /* Send a NS for this address. Use the unspecified address as source
  1032. * address in all cases (RFC 4862 Sec. 5.4.2), not in the least
  1033. * because as it is, we only consider multicast replies for DAD. */
  1034. nd6_send_ns(netif, netif_ip6_addr(netif, i),
  1035. ND6_SEND_FLAG_MULTICAST_DEST | ND6_SEND_FLAG_ANY_SRC);
  1036. }
  1037. }
  1038. }
  1039. }
  1040. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  1041. /* Send router solicitation messages, if necessary. */
  1042. if (!nd6_tmr_rs_reduction) {
  1043. nd6_tmr_rs_reduction = (ND6_RTR_SOLICITATION_INTERVAL / ND6_TMR_INTERVAL) - 1;
  1044. NETIF_FOREACH(netif) {
  1045. if ((netif->rs_count > 0) && netif_is_up(netif) &&
  1046. netif_is_link_up(netif) &&
  1047. !ip6_addr_isinvalid(netif_ip6_addr_state(netif, 0)) &&
  1048. !ip6_addr_isduplicated(netif_ip6_addr_state(netif, 0))) {
  1049. if (nd6_send_rs(netif) == ERR_OK) {
  1050. netif->rs_count--;
  1051. }
  1052. }
  1053. }
  1054. } else {
  1055. nd6_tmr_rs_reduction--;
  1056. }
  1057. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  1058. }
  1059. /** Send a neighbor solicitation message for a specific neighbor cache entry
  1060. *
  1061. * @param entry the neightbor cache entry for which to send the message
  1062. * @param flags one of ND6_SEND_FLAG_*
  1063. */
  1064. static void
  1065. nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags)
  1066. {
  1067. nd6_send_ns(entry->netif, &entry->next_hop_address, flags);
  1068. }
  1069. /**
  1070. * Send a neighbor solicitation message
  1071. *
  1072. * @param netif the netif on which to send the message
  1073. * @param target_addr the IPv6 target address for the ND message
  1074. * @param flags one of ND6_SEND_FLAG_*
  1075. */
  1076. static void
  1077. nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags)
  1078. {
  1079. struct ns_header *ns_hdr;
  1080. struct pbuf *p;
  1081. const ip6_addr_t *src_addr = NULL;
  1082. u16_t lladdr_opt_len;
  1083. LWIP_ASSERT("target address is required", target_addr != NULL);
  1084. if (!(flags & ND6_SEND_FLAG_ANY_SRC)) {
  1085. int i;
  1086. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  1087. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
  1088. ip6_addr_net_eq(target_addr, netif_ip6_addr(netif, i))) {
  1089. src_addr = netif_ip6_addr(netif, i);
  1090. break;
  1091. }
  1092. }
  1093. if (i == LWIP_IPV6_NUM_ADDRESSES) {
  1094. LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("ICMPv6 NS: no available src address\n"));
  1095. ND6_STATS_INC(nd6.err);
  1096. return;
  1097. }
  1098. /* calculate option length (in 8-byte-blocks) */
  1099. lladdr_opt_len = ((netif->hwaddr_len + 2) + 7) >> 3;
  1100. } else {
  1101. src_addr = IP6_ADDR_ANY6;
  1102. /* Option "MUST NOT be included when the source IP address is the unspecified address." */
  1103. lladdr_opt_len = 0;
  1104. }
  1105. /* Allocate a packet. */
  1106. p = pbuf_alloc(PBUF_IP, sizeof(struct ns_header) + (lladdr_opt_len << 3), PBUF_RAM);
  1107. if (p == NULL) {
  1108. ND6_STATS_INC(nd6.memerr);
  1109. return;
  1110. }
  1111. /* Set fields. */
  1112. ns_hdr = (struct ns_header *)p->payload;
  1113. ns_hdr->type = ICMP6_TYPE_NS;
  1114. ns_hdr->code = 0;
  1115. ns_hdr->chksum = 0;
  1116. ns_hdr->reserved = 0;
  1117. ip6_addr_copy_to_packed(ns_hdr->target_address, *target_addr);
  1118. if (lladdr_opt_len != 0) {
  1119. struct lladdr_option *lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header));
  1120. lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
  1121. lladdr_opt->length = (u8_t)lladdr_opt_len;
  1122. SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
  1123. }
  1124. /* Generate the solicited node address for the target address. */
  1125. if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
  1126. ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
  1127. ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
  1128. target_addr = &multicast_address;
  1129. }
  1130. #if CHECKSUM_GEN_ICMP6
  1131. IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
  1132. ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
  1133. target_addr);
  1134. }
  1135. #endif /* CHECKSUM_GEN_ICMP6 */
  1136. /* Send the packet out. */
  1137. ND6_STATS_INC(nd6.xmit);
  1138. ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, target_addr,
  1139. ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif);
  1140. pbuf_free(p);
  1141. }
  1142. /**
  1143. * Send a neighbor advertisement message
  1144. *
  1145. * @param netif the netif on which to send the message
  1146. * @param target_addr the IPv6 target address for the ND message
  1147. * @param flags one of ND6_SEND_FLAG_*
  1148. */
  1149. static void
  1150. nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags)
  1151. {
  1152. struct na_header *na_hdr;
  1153. struct lladdr_option *lladdr_opt;
  1154. struct pbuf *p;
  1155. const ip6_addr_t *src_addr;
  1156. const ip6_addr_t *dest_addr;
  1157. u16_t lladdr_opt_len;
  1158. LWIP_ASSERT("target address is required", target_addr != NULL);
  1159. /* Use link-local address as source address. */
  1160. /* src_addr = netif_ip6_addr(netif, 0); */
  1161. /* Use target address as source address. */
  1162. src_addr = target_addr;
  1163. /* Allocate a packet. */
  1164. lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
  1165. p = pbuf_alloc(PBUF_IP, sizeof(struct na_header) + (lladdr_opt_len << 3), PBUF_RAM);
  1166. if (p == NULL) {
  1167. ND6_STATS_INC(nd6.memerr);
  1168. return;
  1169. }
  1170. /* Set fields. */
  1171. na_hdr = (struct na_header *)p->payload;
  1172. lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header));
  1173. na_hdr->type = ICMP6_TYPE_NA;
  1174. na_hdr->code = 0;
  1175. na_hdr->chksum = 0;
  1176. na_hdr->flags = flags & 0xf0;
  1177. na_hdr->reserved[0] = 0;
  1178. na_hdr->reserved[1] = 0;
  1179. na_hdr->reserved[2] = 0;
  1180. ip6_addr_copy_to_packed(na_hdr->target_address, *target_addr);
  1181. lladdr_opt->type = ND6_OPTION_TYPE_TARGET_LLADDR;
  1182. lladdr_opt->length = (u8_t)lladdr_opt_len;
  1183. SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
  1184. /* Generate the solicited node address for the target address. */
  1185. if (flags & ND6_SEND_FLAG_MULTICAST_DEST) {
  1186. ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]);
  1187. ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
  1188. dest_addr = &multicast_address;
  1189. } else if (flags & ND6_SEND_FLAG_ALLNODES_DEST) {
  1190. ip6_addr_set_allnodes_linklocal(&multicast_address);
  1191. ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
  1192. dest_addr = &multicast_address;
  1193. } else {
  1194. dest_addr = ip6_current_src_addr();
  1195. }
  1196. #if CHECKSUM_GEN_ICMP6
  1197. IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
  1198. na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
  1199. dest_addr);
  1200. }
  1201. #endif /* CHECKSUM_GEN_ICMP6 */
  1202. /* Send the packet out. */
  1203. ND6_STATS_INC(nd6.xmit);
  1204. ip6_output_if(p, src_addr, dest_addr,
  1205. ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif);
  1206. pbuf_free(p);
  1207. }
  1208. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  1209. /**
  1210. * Send a router solicitation message
  1211. *
  1212. * @param netif the netif on which to send the message
  1213. */
  1214. static err_t
  1215. nd6_send_rs(struct netif *netif)
  1216. {
  1217. struct rs_header *rs_hdr;
  1218. struct lladdr_option *lladdr_opt;
  1219. struct pbuf *p;
  1220. const ip6_addr_t *src_addr;
  1221. err_t err;
  1222. u16_t lladdr_opt_len = 0;
  1223. /* Link-local source address, or unspecified address? */
  1224. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) {
  1225. src_addr = netif_ip6_addr(netif, 0);
  1226. } else {
  1227. src_addr = IP6_ADDR_ANY6;
  1228. }
  1229. /* Generate the all routers target address. */
  1230. ip6_addr_set_allrouters_linklocal(&multicast_address);
  1231. ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
  1232. /* Allocate a packet. */
  1233. if (src_addr != IP6_ADDR_ANY6) {
  1234. lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0);
  1235. }
  1236. p = pbuf_alloc(PBUF_IP, sizeof(struct rs_header) + (lladdr_opt_len << 3), PBUF_RAM);
  1237. if (p == NULL) {
  1238. ND6_STATS_INC(nd6.memerr);
  1239. return ERR_BUF;
  1240. }
  1241. /* Set fields. */
  1242. rs_hdr = (struct rs_header *)p->payload;
  1243. rs_hdr->type = ICMP6_TYPE_RS;
  1244. rs_hdr->code = 0;
  1245. rs_hdr->chksum = 0;
  1246. rs_hdr->reserved = 0;
  1247. if (src_addr != IP6_ADDR_ANY6) {
  1248. /* Include our hw address. */
  1249. lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header));
  1250. lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR;
  1251. lladdr_opt->length = (u8_t)lladdr_opt_len;
  1252. SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
  1253. }
  1254. #if CHECKSUM_GEN_ICMP6
  1255. IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
  1256. rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
  1257. &multicast_address);
  1258. }
  1259. #endif /* CHECKSUM_GEN_ICMP6 */
  1260. /* Send the packet out. */
  1261. ND6_STATS_INC(nd6.xmit);
  1262. err = ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, &multicast_address,
  1263. ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif);
  1264. pbuf_free(p);
  1265. return err;
  1266. }
  1267. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  1268. /**
  1269. * Search for a neighbor cache entry
  1270. *
  1271. * @param ip6addr the IPv6 address of the neighbor
  1272. * @return The neighbor cache entry index that matched, -1 if no
  1273. * entry is found
  1274. */
  1275. static s8_t
  1276. nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr)
  1277. {
  1278. s8_t i;
  1279. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1280. if (ip6_addr_eq(ip6addr, &(neighbor_cache[i].next_hop_address))) {
  1281. return i;
  1282. }
  1283. }
  1284. return -1;
  1285. }
  1286. /**
  1287. * Create a new neighbor cache entry.
  1288. *
  1289. * If no unused entry is found, will try to recycle an old entry
  1290. * according to ad-hoc "age" heuristic.
  1291. *
  1292. * @return The neighbor cache entry index that was created, -1 if no
  1293. * entry could be created
  1294. */
  1295. static s8_t
  1296. nd6_new_neighbor_cache_entry(void)
  1297. {
  1298. s8_t i;
  1299. s8_t j;
  1300. u32_t time;
  1301. /* First, try to find an empty entry. */
  1302. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1303. if (neighbor_cache[i].state == ND6_NO_ENTRY) {
  1304. return i;
  1305. }
  1306. }
  1307. /* We need to recycle an entry. in general, do not recycle if it is a router. */
  1308. /* Next, try to find a Stale entry. */
  1309. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1310. if ((neighbor_cache[i].state == ND6_STALE) &&
  1311. (!neighbor_cache[i].isrouter)) {
  1312. nd6_free_neighbor_cache_entry(i);
  1313. return i;
  1314. }
  1315. }
  1316. /* Next, try to find a Probe entry. */
  1317. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1318. if ((neighbor_cache[i].state == ND6_PROBE) &&
  1319. (!neighbor_cache[i].isrouter)) {
  1320. nd6_free_neighbor_cache_entry(i);
  1321. return i;
  1322. }
  1323. }
  1324. /* Next, try to find a Delayed entry. */
  1325. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1326. if ((neighbor_cache[i].state == ND6_DELAY) &&
  1327. (!neighbor_cache[i].isrouter)) {
  1328. nd6_free_neighbor_cache_entry(i);
  1329. return i;
  1330. }
  1331. }
  1332. /* Next, try to find the oldest reachable entry. */
  1333. time = 0xfffffffful;
  1334. j = -1;
  1335. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1336. if ((neighbor_cache[i].state == ND6_REACHABLE) &&
  1337. (!neighbor_cache[i].isrouter)) {
  1338. if (neighbor_cache[i].counter.reachable_time < time) {
  1339. j = i;
  1340. time = neighbor_cache[i].counter.reachable_time;
  1341. }
  1342. }
  1343. }
  1344. if (j >= 0) {
  1345. nd6_free_neighbor_cache_entry(j);
  1346. return j;
  1347. }
  1348. /* Next, find oldest incomplete entry without queued packets. */
  1349. time = 0;
  1350. j = -1;
  1351. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1352. if (
  1353. (neighbor_cache[i].q == NULL) &&
  1354. (neighbor_cache[i].state == ND6_INCOMPLETE) &&
  1355. (!neighbor_cache[i].isrouter)) {
  1356. if (neighbor_cache[i].counter.probes_sent >= time) {
  1357. j = i;
  1358. time = neighbor_cache[i].counter.probes_sent;
  1359. }
  1360. }
  1361. }
  1362. if (j >= 0) {
  1363. nd6_free_neighbor_cache_entry(j);
  1364. return j;
  1365. }
  1366. /* Next, find oldest incomplete entry with queued packets. */
  1367. time = 0;
  1368. j = -1;
  1369. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  1370. if ((neighbor_cache[i].state == ND6_INCOMPLETE) &&
  1371. (!neighbor_cache[i].isrouter)) {
  1372. if (neighbor_cache[i].counter.probes_sent >= time) {
  1373. j = i;
  1374. time = neighbor_cache[i].counter.probes_sent;
  1375. }
  1376. }
  1377. }
  1378. if (j >= 0) {
  1379. nd6_free_neighbor_cache_entry(j);
  1380. return j;
  1381. }
  1382. /* No more entries to try. */
  1383. return -1;
  1384. }
  1385. /**
  1386. * Will free any resources associated with a neighbor cache
  1387. * entry, and will mark it as unused.
  1388. *
  1389. * @param i the neighbor cache entry index to free
  1390. */
  1391. static void
  1392. nd6_free_neighbor_cache_entry(s8_t i)
  1393. {
  1394. if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
  1395. return;
  1396. }
  1397. if (neighbor_cache[i].isrouter) {
  1398. /* isrouter needs to be cleared before deleting a neighbor cache entry */
  1399. return;
  1400. }
  1401. /* Free any queued packets. */
  1402. if (neighbor_cache[i].q != NULL) {
  1403. nd6_free_q(neighbor_cache[i].q);
  1404. neighbor_cache[i].q = NULL;
  1405. }
  1406. neighbor_cache[i].state = ND6_NO_ENTRY;
  1407. neighbor_cache[i].isrouter = 0;
  1408. neighbor_cache[i].netif = NULL;
  1409. neighbor_cache[i].counter.reachable_time = 0;
  1410. ip6_addr_set_zero(&(neighbor_cache[i].next_hop_address));
  1411. }
  1412. /**
  1413. * Search for a destination cache entry
  1414. *
  1415. * @param ip6addr the IPv6 address of the destination
  1416. * @return The destination cache entry index that matched, -1 if no
  1417. * entry is found
  1418. */
  1419. static s16_t
  1420. nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr)
  1421. {
  1422. s16_t i;
  1423. IP6_ADDR_ZONECHECK(ip6addr);
  1424. for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
  1425. if (ip6_addr_eq(ip6addr, &(destination_cache[i].destination_addr))) {
  1426. return i;
  1427. }
  1428. }
  1429. return -1;
  1430. }
  1431. /**
  1432. * Create a new destination cache entry. If no unused entry is found,
  1433. * will recycle oldest entry.
  1434. *
  1435. * @return The destination cache entry index that was created, -1 if no
  1436. * entry was created
  1437. */
  1438. static s16_t
  1439. nd6_new_destination_cache_entry(void)
  1440. {
  1441. s16_t i, j;
  1442. u32_t age;
  1443. /* Find an empty entry. */
  1444. for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
  1445. if (ip6_addr_isany(&(destination_cache[i].destination_addr))) {
  1446. return i;
  1447. }
  1448. }
  1449. /* Find oldest entry. */
  1450. age = 0;
  1451. j = LWIP_ND6_NUM_DESTINATIONS - 1;
  1452. for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
  1453. if (destination_cache[i].age > age) {
  1454. j = i;
  1455. }
  1456. }
  1457. return j;
  1458. }
  1459. /**
  1460. * Clear the destination cache.
  1461. *
  1462. * This operation may be necessary for consistency in the light of changing
  1463. * local addresses and/or use of the gateway hook.
  1464. */
  1465. void
  1466. nd6_clear_destination_cache(void)
  1467. {
  1468. int i;
  1469. for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
  1470. ip6_addr_set_any(&destination_cache[i].destination_addr);
  1471. }
  1472. }
  1473. /**
  1474. * Determine whether an address matches an on-link prefix or the subnet of a
  1475. * statically assigned address.
  1476. *
  1477. * @param ip6addr the IPv6 address to match
  1478. * @return 1 if the address is on-link, 0 otherwise
  1479. */
  1480. static int
  1481. nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif)
  1482. {
  1483. s8_t i;
  1484. /* Check to see if the address matches an on-link prefix. */
  1485. for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
  1486. if ((prefix_list[i].netif == netif) &&
  1487. (prefix_list[i].invalidation_timer > 0) &&
  1488. ip6_addr_net_eq(ip6addr, &(prefix_list[i].prefix))) {
  1489. return 1;
  1490. }
  1491. }
  1492. /* Check to see if address prefix matches a manually configured (= static)
  1493. * address. Static addresses have an implied /64 subnet assignment. Dynamic
  1494. * addresses (from autoconfiguration) have no implied subnet assignment, and
  1495. * are thus effectively /128 assignments. See RFC 5942 for more on this. */
  1496. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  1497. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
  1498. netif_ip6_addr_isstatic(netif, i) &&
  1499. ip6_addr_net_eq(ip6addr, netif_ip6_addr(netif, i))) {
  1500. return 1;
  1501. }
  1502. }
  1503. return 0;
  1504. }
  1505. /**
  1506. * Select a default router for a destination.
  1507. *
  1508. * This function is used both for routing and for finding a next-hop target for
  1509. * a packet. In the former case, the given netif is NULL, and the returned
  1510. * router entry must be for a netif suitable for sending packets (up, link up).
  1511. * In the latter case, the given netif is not NULL and restricts router choice.
  1512. *
  1513. * @param ip6addr the destination address
  1514. * @param netif the netif for the outgoing packet, if known
  1515. * @return the default router entry index, or -1 if no suitable
  1516. * router is found
  1517. */
  1518. static s8_t
  1519. nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif)
  1520. {
  1521. struct netif *router_netif;
  1522. s8_t i, j, valid_router;
  1523. static s8_t last_router;
  1524. LWIP_UNUSED_ARG(ip6addr); /* @todo match preferred routes!! (must implement ND6_OPTION_TYPE_ROUTE_INFO) */
  1525. /* @todo: implement default router preference */
  1526. /* Look for valid routers. A reachable router is preferred. */
  1527. valid_router = -1;
  1528. for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
  1529. /* Is the router netif both set and apppropriate? */
  1530. if (default_router_list[i].neighbor_entry != NULL) {
  1531. router_netif = default_router_list[i].neighbor_entry->netif;
  1532. if ((router_netif != NULL) && (netif != NULL ? netif == router_netif :
  1533. (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) {
  1534. /* Is the router valid, i.e., reachable or probably reachable as per
  1535. * RFC 4861 Sec. 6.3.6? Note that we will never return a router that
  1536. * has no neighbor cache entry, due to the netif association tests. */
  1537. if (default_router_list[i].neighbor_entry->state != ND6_INCOMPLETE) {
  1538. /* Is the router known to be reachable? */
  1539. if (default_router_list[i].neighbor_entry->state == ND6_REACHABLE) {
  1540. return i; /* valid and reachable - done! */
  1541. } else if (valid_router < 0) {
  1542. valid_router = i; /* valid but not known to be reachable */
  1543. }
  1544. }
  1545. }
  1546. }
  1547. }
  1548. if (valid_router >= 0) {
  1549. return valid_router;
  1550. }
  1551. /* Look for any router for which we have any information at all. */
  1552. /* last_router is used for round-robin selection of incomplete routers, as
  1553. * recommended in RFC 4861 Sec. 6.3.6 point (2). Advance only when picking a
  1554. * route, to select the same router as next-hop target in the common case. */
  1555. if ((netif == NULL) && (++last_router >= LWIP_ND6_NUM_ROUTERS)) {
  1556. last_router = 0;
  1557. }
  1558. i = last_router;
  1559. for (j = 0; j < LWIP_ND6_NUM_ROUTERS; j++) {
  1560. if (default_router_list[i].neighbor_entry != NULL) {
  1561. router_netif = default_router_list[i].neighbor_entry->netif;
  1562. if ((router_netif != NULL) && (netif != NULL ? netif == router_netif :
  1563. (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) {
  1564. return i;
  1565. }
  1566. }
  1567. if (++i >= LWIP_ND6_NUM_ROUTERS) {
  1568. i = 0;
  1569. }
  1570. }
  1571. /* no suitable router found. */
  1572. return -1;
  1573. }
  1574. /**
  1575. * Find a router-announced route to the given destination. This route may be
  1576. * based on an on-link prefix or a default router.
  1577. *
  1578. * If a suitable route is found, the returned netif is guaranteed to be in a
  1579. * suitable state (up, link up) to be used for packet transmission.
  1580. *
  1581. * @param ip6addr the destination IPv6 address
  1582. * @return the netif to use for the destination, or NULL if none found
  1583. */
  1584. struct netif *
  1585. nd6_find_route(const ip6_addr_t *ip6addr)
  1586. {
  1587. struct netif *netif;
  1588. s8_t i;
  1589. /* @todo decide if it makes sense to check the destination cache first */
  1590. /* Check if there is a matching on-link prefix. There may be multiple
  1591. * matches. Pick the first one that is associated with a suitable netif. */
  1592. for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
  1593. netif = prefix_list[i].netif;
  1594. if ((netif != NULL) && ip6_addr_net_eq(&prefix_list[i].prefix, ip6addr) &&
  1595. netif_is_up(netif) && netif_is_link_up(netif)) {
  1596. return netif;
  1597. }
  1598. }
  1599. /* No on-link prefix match. Find a router that can forward the packet. */
  1600. i = nd6_select_router(ip6addr, NULL);
  1601. if (i >= 0) {
  1602. LWIP_ASSERT("selected router must have a neighbor entry",
  1603. default_router_list[i].neighbor_entry != NULL);
  1604. return default_router_list[i].neighbor_entry->netif;
  1605. }
  1606. return NULL;
  1607. }
  1608. /**
  1609. * Find an entry for a default router.
  1610. *
  1611. * @param router_addr the IPv6 address of the router
  1612. * @param netif the netif on which the router is found, if known
  1613. * @return the index of the router entry, or -1 if not found
  1614. */
  1615. static s8_t
  1616. nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif)
  1617. {
  1618. s8_t i;
  1619. IP6_ADDR_ZONECHECK_NETIF(router_addr, netif);
  1620. /* Look for router. */
  1621. for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
  1622. if ((default_router_list[i].neighbor_entry != NULL) &&
  1623. ((netif != NULL) ? netif == default_router_list[i].neighbor_entry->netif : 1) &&
  1624. ip6_addr_eq(router_addr, &(default_router_list[i].neighbor_entry->next_hop_address))) {
  1625. return i;
  1626. }
  1627. }
  1628. /* router not found. */
  1629. return -1;
  1630. }
  1631. /**
  1632. * Create a new entry for a default router.
  1633. *
  1634. * @param router_addr the IPv6 address of the router
  1635. * @param netif the netif on which the router is connected, if known
  1636. * @return the index on the router table, or -1 if could not be created
  1637. */
  1638. static s8_t
  1639. nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif)
  1640. {
  1641. s8_t router_index;
  1642. s8_t free_router_index;
  1643. s8_t neighbor_index;
  1644. IP6_ADDR_ZONECHECK_NETIF(router_addr, netif);
  1645. /* Do we have a neighbor entry for this router? */
  1646. neighbor_index = nd6_find_neighbor_cache_entry(router_addr);
  1647. if (neighbor_index < 0) {
  1648. /* Create a neighbor entry for this router. */
  1649. neighbor_index = nd6_new_neighbor_cache_entry();
  1650. if (neighbor_index < 0) {
  1651. /* Could not create neighbor entry for this router. */
  1652. return -1;
  1653. }
  1654. ip6_addr_set(&(neighbor_cache[neighbor_index].next_hop_address), router_addr);
  1655. neighbor_cache[neighbor_index].netif = netif;
  1656. neighbor_cache[neighbor_index].q = NULL;
  1657. neighbor_cache[neighbor_index].state = ND6_INCOMPLETE;
  1658. neighbor_cache[neighbor_index].counter.probes_sent = 1;
  1659. nd6_send_neighbor_cache_probe(&neighbor_cache[neighbor_index], ND6_SEND_FLAG_MULTICAST_DEST);
  1660. }
  1661. /* Mark neighbor as router. */
  1662. neighbor_cache[neighbor_index].isrouter = 1;
  1663. /* Look for empty entry. */
  1664. free_router_index = LWIP_ND6_NUM_ROUTERS;
  1665. for (router_index = LWIP_ND6_NUM_ROUTERS - 1; router_index >= 0; router_index--) {
  1666. /* check if router already exists (this is a special case for 2 netifs on the same subnet
  1667. - e.g. wifi and cable) */
  1668. if(default_router_list[router_index].neighbor_entry == &(neighbor_cache[neighbor_index])){
  1669. return router_index;
  1670. }
  1671. if (default_router_list[router_index].neighbor_entry == NULL) {
  1672. /* remember lowest free index to create a new entry */
  1673. free_router_index = router_index;
  1674. }
  1675. }
  1676. if (free_router_index < LWIP_ND6_NUM_ROUTERS) {
  1677. default_router_list[free_router_index].neighbor_entry = &(neighbor_cache[neighbor_index]);
  1678. return free_router_index;
  1679. }
  1680. /* Could not create a router entry. */
  1681. /* Mark neighbor entry as not-router. Entry might be useful as neighbor still. */
  1682. neighbor_cache[neighbor_index].isrouter = 0;
  1683. /* router not found. */
  1684. return -1;
  1685. }
  1686. /**
  1687. * Find the cached entry for an on-link prefix.
  1688. *
  1689. * @param prefix the IPv6 prefix that is on-link
  1690. * @param netif the netif on which the prefix is on-link
  1691. * @return the index on the prefix table, or -1 if not found
  1692. */
  1693. static s8_t
  1694. nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif)
  1695. {
  1696. s8_t i;
  1697. /* Look for prefix in list. */
  1698. for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
  1699. if ((ip6_addr_net_eq(&(prefix_list[i].prefix), prefix)) &&
  1700. (prefix_list[i].netif == netif)) {
  1701. return i;
  1702. }
  1703. }
  1704. /* Entry not available. */
  1705. return -1;
  1706. }
  1707. /**
  1708. * Creates a new entry for an on-link prefix.
  1709. *
  1710. * @param prefix the IPv6 prefix that is on-link
  1711. * @param netif the netif on which the prefix is on-link
  1712. * @return the index on the prefix table, or -1 if not created
  1713. */
  1714. static s8_t
  1715. nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif)
  1716. {
  1717. s8_t i;
  1718. /* Create new entry. */
  1719. for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) {
  1720. if ((prefix_list[i].netif == NULL) ||
  1721. (prefix_list[i].invalidation_timer == 0)) {
  1722. /* Found empty prefix entry. */
  1723. prefix_list[i].netif = netif;
  1724. ip6_addr_set(&(prefix_list[i].prefix), prefix);
  1725. return i;
  1726. }
  1727. }
  1728. /* Entry not available. */
  1729. return -1;
  1730. }
  1731. /**
  1732. * Determine the next hop for a destination. Will determine if the
  1733. * destination is on-link, else a suitable on-link router is selected.
  1734. *
  1735. * The last entry index is cached for fast entry search.
  1736. *
  1737. * @param ip6addr the destination address
  1738. * @param netif the netif on which the packet will be sent
  1739. * @return the neighbor cache entry for the next hop, ERR_RTE if no
  1740. * suitable next hop was found, ERR_MEM if no cache entry
  1741. * could be created
  1742. */
  1743. static s8_t
  1744. nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif)
  1745. {
  1746. #ifdef LWIP_HOOK_ND6_GET_GW
  1747. const ip6_addr_t *next_hop_addr;
  1748. #endif /* LWIP_HOOK_ND6_GET_GW */
  1749. s8_t i;
  1750. s16_t dst_idx;
  1751. struct nd6_destination_cache_entry *dest;
  1752. IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif);
  1753. #if LWIP_NETIF_HWADDRHINT
  1754. if (netif->hints != NULL) {
  1755. /* per-pcb cached entry was given */
  1756. netif_addr_idx_t addr_hint = netif->hints->addr_hint;
  1757. if (addr_hint < LWIP_ND6_NUM_DESTINATIONS) {
  1758. nd6_cached_destination_index = addr_hint;
  1759. }
  1760. }
  1761. #endif /* LWIP_NETIF_HWADDRHINT */
  1762. LWIP_ASSERT("sane cache index", nd6_cached_destination_index < LWIP_ND6_NUM_DESTINATIONS);
  1763. /* Look for ip6addr in destination cache. */
  1764. dest = &destination_cache[nd6_cached_destination_index];
  1765. if (ip6_addr_eq(ip6addr, &dest->destination_addr)) {
  1766. /* the cached entry index is the right one! */
  1767. /* do nothing. */
  1768. ND6_STATS_INC(nd6.cachehit);
  1769. } else {
  1770. /* Search destination cache. */
  1771. dst_idx = nd6_find_destination_cache_entry(ip6addr);
  1772. if (dst_idx >= 0) {
  1773. /* found destination entry. make it our new cached index. */
  1774. LWIP_ASSERT("type overflow", (size_t)dst_idx < NETIF_ADDR_IDX_MAX);
  1775. nd6_cached_destination_index = (netif_addr_idx_t)dst_idx;
  1776. dest = &destination_cache[dst_idx];
  1777. } else {
  1778. /* Not found. Create a new destination entry. */
  1779. dst_idx = nd6_new_destination_cache_entry();
  1780. if (dst_idx >= 0) {
  1781. /* got new destination entry. make it our new cached index. */
  1782. LWIP_ASSERT("type overflow", (size_t)dst_idx < NETIF_ADDR_IDX_MAX);
  1783. nd6_cached_destination_index = (netif_addr_idx_t)dst_idx;
  1784. dest = &destination_cache[dst_idx];
  1785. } else {
  1786. /* Could not create a destination cache entry. */
  1787. return ERR_MEM;
  1788. }
  1789. /* Copy dest address to destination cache. */
  1790. ip6_addr_set(&dest->destination_addr, ip6addr);
  1791. /* Now find the next hop. is it a neighbor? */
  1792. if (ip6_addr_islinklocal(ip6addr) ||
  1793. nd6_is_prefix_in_netif(ip6addr, netif)) {
  1794. /* Destination in local link. */
  1795. dest->pmtu = netif_mtu6(netif);
  1796. ip6_addr_copy(dest->next_hop_addr, dest->destination_addr);
  1797. #ifdef LWIP_HOOK_ND6_GET_GW
  1798. } else if ((next_hop_addr = LWIP_HOOK_ND6_GET_GW(netif, ip6addr)) != NULL) {
  1799. /* Next hop for destination provided by hook function. */
  1800. dest->pmtu = netif->mtu;
  1801. ip6_addr_set(&dest->next_hop_addr, next_hop_addr);
  1802. #endif /* LWIP_HOOK_ND6_GET_GW */
  1803. } else {
  1804. /* We need to select a router. */
  1805. i = nd6_select_router(ip6addr, netif);
  1806. if (i < 0) {
  1807. /* No router found. */
  1808. ip6_addr_set_any(&dest->destination_addr);
  1809. return ERR_RTE;
  1810. }
  1811. dest->pmtu = netif_mtu6(netif); /* Start with netif mtu, correct through ICMPv6 if necessary */
  1812. ip6_addr_copy(dest->next_hop_addr, default_router_list[i].neighbor_entry->next_hop_address);
  1813. }
  1814. }
  1815. #if LWIP_NETIF_HWADDRHINT
  1816. if (netif->hints != NULL) {
  1817. /* per-pcb cached entry was given */
  1818. netif->hints->addr_hint = nd6_cached_destination_index;
  1819. }
  1820. #endif /* LWIP_NETIF_HWADDRHINT */
  1821. }
  1822. /* Look in neighbor cache for the next-hop address. */
  1823. if (ip6_addr_eq(&dest->next_hop_addr,
  1824. &(neighbor_cache[dest->cached_neighbor_idx].next_hop_address))) {
  1825. /* Cache hit. */
  1826. /* Do nothing. */
  1827. ND6_STATS_INC(nd6.cachehit);
  1828. } else {
  1829. i = nd6_find_neighbor_cache_entry(&dest->next_hop_addr);
  1830. if (i >= 0) {
  1831. /* Found a matching record, make it new cached entry. */
  1832. dest->cached_neighbor_idx = i;
  1833. } else {
  1834. /* Neighbor not in cache. Make a new entry. */
  1835. i = nd6_new_neighbor_cache_entry();
  1836. if (i >= 0) {
  1837. /* got new neighbor entry. make it our new cached index. */
  1838. dest->cached_neighbor_idx = i;
  1839. } else {
  1840. /* Could not create a neighbor cache entry. */
  1841. return ERR_MEM;
  1842. }
  1843. /* Initialize fields. */
  1844. ip6_addr_copy(neighbor_cache[i].next_hop_address, dest->next_hop_addr);
  1845. neighbor_cache[i].isrouter = 0;
  1846. neighbor_cache[i].netif = netif;
  1847. neighbor_cache[i].state = ND6_INCOMPLETE;
  1848. neighbor_cache[i].counter.probes_sent = 1;
  1849. nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST);
  1850. }
  1851. }
  1852. /* Reset this destination's age. */
  1853. dest->age = 0;
  1854. return dest->cached_neighbor_idx;
  1855. }
  1856. /**
  1857. * Queue a packet for a neighbor.
  1858. *
  1859. * @param neighbor_index the index in the neighbor cache table
  1860. * @param q packet to be queued
  1861. * @return ERR_OK if succeeded, ERR_MEM if out of memory
  1862. */
  1863. static err_t
  1864. nd6_queue_packet(s8_t neighbor_index, struct pbuf *q)
  1865. {
  1866. err_t result = ERR_MEM;
  1867. struct pbuf *p;
  1868. int copy_needed = 0;
  1869. #if LWIP_ND6_QUEUEING
  1870. struct nd6_q_entry *new_entry, *r;
  1871. #endif /* LWIP_ND6_QUEUEING */
  1872. if ((neighbor_index < 0) || (neighbor_index >= LWIP_ND6_NUM_NEIGHBORS)) {
  1873. return ERR_ARG;
  1874. }
  1875. /* IF q includes a pbuf that must be copied, we have to copy the whole chain
  1876. * into a new PBUF_RAM. See the definition of PBUF_NEEDS_COPY for details. */
  1877. p = q;
  1878. while (p) {
  1879. if (PBUF_NEEDS_COPY(p)) {
  1880. copy_needed = 1;
  1881. break;
  1882. }
  1883. p = p->next;
  1884. }
  1885. if (copy_needed) {
  1886. /* copy the whole packet into new pbufs */
  1887. p = pbuf_clone(PBUF_LINK, PBUF_RAM, q);
  1888. while ((p == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
  1889. /* Free oldest packet (as per RFC recommendation) */
  1890. #if LWIP_ND6_QUEUEING
  1891. r = neighbor_cache[neighbor_index].q;
  1892. neighbor_cache[neighbor_index].q = r->next;
  1893. r->next = NULL;
  1894. nd6_free_q(r);
  1895. #else /* LWIP_ND6_QUEUEING */
  1896. pbuf_free(neighbor_cache[neighbor_index].q);
  1897. neighbor_cache[neighbor_index].q = NULL;
  1898. #endif /* LWIP_ND6_QUEUEING */
  1899. p = pbuf_clone(PBUF_LINK, PBUF_RAM, q);
  1900. }
  1901. } else {
  1902. /* referencing the old pbuf is enough */
  1903. p = q;
  1904. pbuf_ref(p);
  1905. }
  1906. /* packet was copied/ref'd? */
  1907. if (p != NULL) {
  1908. /* queue packet ... */
  1909. #if LWIP_ND6_QUEUEING
  1910. /* allocate a new nd6 queue entry */
  1911. new_entry = NULL;
  1912. if (nd6_queue_size < MEMP_NUM_ND6_QUEUE) {
  1913. new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
  1914. nd6_queue_size++;
  1915. }
  1916. if ((new_entry == NULL) && (neighbor_cache[neighbor_index].q != NULL)) {
  1917. /* Free oldest packet (as per RFC recommendation) */
  1918. r = neighbor_cache[neighbor_index].q;
  1919. neighbor_cache[neighbor_index].q = r->next;
  1920. r->next = NULL;
  1921. nd6_free_q(r);
  1922. new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE);
  1923. nd6_queue_size++;
  1924. }
  1925. if (new_entry != NULL) {
  1926. new_entry->next = NULL;
  1927. new_entry->p = p;
  1928. if (neighbor_cache[neighbor_index].q != NULL) {
  1929. /* queue was already existent, append the new entry to the end */
  1930. r = neighbor_cache[neighbor_index].q;
  1931. while (r->next != NULL) {
  1932. r = r->next;
  1933. }
  1934. r->next = new_entry;
  1935. } else {
  1936. /* queue did not exist, first item in queue */
  1937. neighbor_cache[neighbor_index].q = new_entry;
  1938. }
  1939. LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
  1940. result = ERR_OK;
  1941. } else {
  1942. /* the pool MEMP_ND6_QUEUE is empty */
  1943. pbuf_free(p);
  1944. LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)p));
  1945. /* { result == ERR_MEM } through initialization */
  1946. }
  1947. #else /* LWIP_ND6_QUEUEING */
  1948. /* Queue a single packet. If an older packet is already queued, free it as per RFC. */
  1949. if (neighbor_cache[neighbor_index].q != NULL) {
  1950. pbuf_free(neighbor_cache[neighbor_index].q);
  1951. }
  1952. neighbor_cache[neighbor_index].q = p;
  1953. LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index));
  1954. result = ERR_OK;
  1955. #endif /* LWIP_ND6_QUEUEING */
  1956. } else {
  1957. LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)q));
  1958. /* { result == ERR_MEM } through initialization */
  1959. }
  1960. return result;
  1961. }
  1962. #if LWIP_ND6_QUEUEING
  1963. /**
  1964. * Free a complete queue of nd6 q entries
  1965. *
  1966. * @param q a queue of nd6_q_entry to free
  1967. */
  1968. static void
  1969. nd6_free_q(struct nd6_q_entry *q)
  1970. {
  1971. struct nd6_q_entry *r;
  1972. LWIP_ASSERT("q != NULL", q != NULL);
  1973. LWIP_ASSERT("q->p != NULL", q->p != NULL);
  1974. while (q) {
  1975. r = q;
  1976. q = q->next;
  1977. LWIP_ASSERT("r->p != NULL", (r->p != NULL));
  1978. pbuf_free(r->p);
  1979. memp_free(MEMP_ND6_QUEUE, r);
  1980. nd6_queue_size--;
  1981. }
  1982. }
  1983. #endif /* LWIP_ND6_QUEUEING */
  1984. /**
  1985. * Send queued packets for a neighbor
  1986. *
  1987. * @param i the neighbor to send packets to
  1988. */
  1989. static void
  1990. nd6_send_q(s8_t i)
  1991. {
  1992. struct ip6_hdr *ip6hdr;
  1993. ip6_addr_t dest;
  1994. #if LWIP_ND6_QUEUEING
  1995. struct nd6_q_entry *q;
  1996. #endif /* LWIP_ND6_QUEUEING */
  1997. if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) {
  1998. return;
  1999. }
  2000. #if LWIP_ND6_QUEUEING
  2001. while (neighbor_cache[i].q != NULL) {
  2002. /* remember first in queue */
  2003. q = neighbor_cache[i].q;
  2004. /* pop first item off the queue */
  2005. neighbor_cache[i].q = q->next;
  2006. /* Get ipv6 header. */
  2007. ip6hdr = (struct ip6_hdr *)(q->p->payload);
  2008. /* Create an aligned copy. */
  2009. ip6_addr_copy_from_packed(dest, ip6hdr->dest);
  2010. /* Restore the zone, if applicable. */
  2011. ip6_addr_assign_zone(&dest, IP6_UNKNOWN, neighbor_cache[i].netif);
  2012. /* send the queued IPv6 packet */
  2013. (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, q->p, &dest);
  2014. /* free the queued IP packet */
  2015. pbuf_free(q->p);
  2016. /* now queue entry can be freed */
  2017. memp_free(MEMP_ND6_QUEUE, q);
  2018. nd6_queue_size--;
  2019. }
  2020. #else /* LWIP_ND6_QUEUEING */
  2021. if (neighbor_cache[i].q != NULL) {
  2022. /* Get ipv6 header. */
  2023. ip6hdr = (struct ip6_hdr *)(neighbor_cache[i].q->payload);
  2024. /* Create an aligned copy. */
  2025. ip6_addr_copy_from_packed(dest, ip6hdr->dest);
  2026. /* Restore the zone, if applicable. */
  2027. ip6_addr_assign_zone(&dest, IP6_UNKNOWN, neighbor_cache[i].netif);
  2028. /* send the queued IPv6 packet */
  2029. (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, neighbor_cache[i].q, &dest);
  2030. /* free the queued IP packet */
  2031. pbuf_free(neighbor_cache[i].q);
  2032. neighbor_cache[i].q = NULL;
  2033. }
  2034. #endif /* LWIP_ND6_QUEUEING */
  2035. }
  2036. /**
  2037. * A packet is to be transmitted to a specific IPv6 destination on a specific
  2038. * interface. Check if we can find the hardware address of the next hop to use
  2039. * for the packet. If so, give the hardware address to the caller, which should
  2040. * use it to send the packet right away. Otherwise, enqueue the packet for
  2041. * later transmission while looking up the hardware address, if possible.
  2042. *
  2043. * As such, this function returns one of three different possible results:
  2044. *
  2045. * - ERR_OK with a non-NULL 'hwaddrp': the caller should send the packet now.
  2046. * - ERR_OK with a NULL 'hwaddrp': the packet has been enqueued for later.
  2047. * - not ERR_OK: something went wrong; forward the error upward in the stack.
  2048. *
  2049. * @param netif The lwIP network interface on which the IP packet will be sent.
  2050. * @param q The pbuf(s) containing the IP packet to be sent.
  2051. * @param ip6addr The destination IPv6 address of the packet.
  2052. * @param hwaddrp On success, filled with a pointer to a HW address or NULL (meaning
  2053. * the packet has been queued).
  2054. * @return
  2055. * - ERR_OK on success, ERR_RTE if no route was found for the packet,
  2056. * or ERR_MEM if low memory conditions prohibit sending the packet at all.
  2057. */
  2058. err_t
  2059. nd6_get_next_hop_addr_or_queue(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr, const u8_t **hwaddrp)
  2060. {
  2061. s8_t i;
  2062. /* Get next hop record. */
  2063. i = nd6_get_next_hop_entry(ip6addr, netif);
  2064. if (i < 0) {
  2065. /* failed to get a next hop neighbor record. */
  2066. return i;
  2067. }
  2068. /* Now that we have a destination record, send or queue the packet. */
  2069. if (neighbor_cache[i].state == ND6_STALE) {
  2070. /* Switch to delay state. */
  2071. neighbor_cache[i].state = ND6_DELAY;
  2072. neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
  2073. }
  2074. /* @todo should we send or queue if PROBE? send for now, to let unicast NS pass. */
  2075. if ((neighbor_cache[i].state == ND6_REACHABLE) ||
  2076. (neighbor_cache[i].state == ND6_DELAY) ||
  2077. (neighbor_cache[i].state == ND6_PROBE)) {
  2078. /* Tell the caller to send out the packet now. */
  2079. *hwaddrp = neighbor_cache[i].lladdr;
  2080. return ERR_OK;
  2081. }
  2082. /* We should queue packet on this interface. */
  2083. *hwaddrp = NULL;
  2084. return nd6_queue_packet(i, q);
  2085. }
  2086. /**
  2087. * Get the Path MTU for a destination.
  2088. *
  2089. * @param ip6addr the destination address
  2090. * @param netif the netif on which the packet will be sent
  2091. * @return the Path MTU, if known, or the netif default MTU
  2092. */
  2093. u16_t
  2094. nd6_get_destination_mtu(const ip6_addr_t *ip6addr, struct netif *netif)
  2095. {
  2096. s16_t i;
  2097. i = nd6_find_destination_cache_entry(ip6addr);
  2098. if (i >= 0) {
  2099. if (destination_cache[i].pmtu > 0) {
  2100. return destination_cache[i].pmtu;
  2101. }
  2102. }
  2103. if (netif != NULL) {
  2104. return netif_mtu6(netif);
  2105. }
  2106. return IP6_MIN_MTU_LENGTH; /* Minimum MTU */
  2107. }
  2108. #if LWIP_ND6_TCP_REACHABILITY_HINTS
  2109. /**
  2110. * Provide the Neighbor discovery process with a hint that a
  2111. * destination is reachable. Called by tcp_receive when ACKs are
  2112. * received or sent (as per RFC). This is useful to avoid sending
  2113. * NS messages every 30 seconds.
  2114. *
  2115. * @param ip6addr the destination address which is know to be reachable
  2116. * by an upper layer protocol (TCP)
  2117. */
  2118. void
  2119. nd6_reachability_hint(const ip6_addr_t *ip6addr)
  2120. {
  2121. s8_t i;
  2122. s16_t dst_idx;
  2123. struct nd6_destination_cache_entry *dest;
  2124. /* Find destination in cache. */
  2125. if (ip6_addr_eq(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) {
  2126. dst_idx = nd6_cached_destination_index;
  2127. ND6_STATS_INC(nd6.cachehit);
  2128. } else {
  2129. dst_idx = nd6_find_destination_cache_entry(ip6addr);
  2130. }
  2131. if (dst_idx < 0) {
  2132. return;
  2133. }
  2134. /* Find next hop neighbor in cache. */
  2135. dest = &destination_cache[dst_idx];
  2136. if (ip6_addr_eq(&dest->next_hop_addr, &(neighbor_cache[dest->cached_neighbor_idx].next_hop_address))) {
  2137. i = dest->cached_neighbor_idx;
  2138. ND6_STATS_INC(nd6.cachehit);
  2139. } else {
  2140. i = nd6_find_neighbor_cache_entry(&dest->next_hop_addr);
  2141. }
  2142. if (i < 0) {
  2143. return;
  2144. }
  2145. /* For safety: don't set as reachable if we don't have a LL address yet. Misuse protection. */
  2146. if (neighbor_cache[i].state == ND6_INCOMPLETE || neighbor_cache[i].state == ND6_NO_ENTRY) {
  2147. return;
  2148. }
  2149. /* Set reachability state. */
  2150. neighbor_cache[i].state = ND6_REACHABLE;
  2151. neighbor_cache[i].counter.reachable_time = reachable_time;
  2152. }
  2153. #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */
  2154. /**
  2155. * Remove all prefix, neighbor_cache and router entries of the specified netif.
  2156. *
  2157. * @param netif points to a network interface
  2158. */
  2159. void
  2160. nd6_cleanup_netif(struct netif *netif)
  2161. {
  2162. u8_t i;
  2163. s8_t router_index;
  2164. for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
  2165. if (prefix_list[i].netif == netif) {
  2166. prefix_list[i].netif = NULL;
  2167. }
  2168. }
  2169. for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
  2170. if (neighbor_cache[i].netif == netif) {
  2171. for (router_index = 0; router_index < LWIP_ND6_NUM_ROUTERS; router_index++) {
  2172. if (default_router_list[router_index].neighbor_entry == &neighbor_cache[i]) {
  2173. default_router_list[router_index].neighbor_entry = NULL;
  2174. default_router_list[router_index].flags = 0;
  2175. }
  2176. }
  2177. neighbor_cache[i].isrouter = 0;
  2178. nd6_free_neighbor_cache_entry(i);
  2179. }
  2180. }
  2181. /* Clear the destination cache, since many entries may now have become
  2182. * invalid for one of several reasons. As destination cache entries have no
  2183. * netif association, use a sledgehammer approach (this can be improved). */
  2184. nd6_clear_destination_cache();
  2185. }
  2186. #if LWIP_IPV6_MLD
  2187. /**
  2188. * The state of a local IPv6 address entry is about to change. If needed, join
  2189. * or leave the solicited-node multicast group for the address.
  2190. *
  2191. * @param netif The netif that owns the address.
  2192. * @param addr_idx The index of the address.
  2193. * @param new_state The new (IP6_ADDR_) state for the address.
  2194. */
  2195. void
  2196. nd6_adjust_mld_membership(struct netif *netif, s8_t addr_idx, u8_t new_state)
  2197. {
  2198. u8_t old_state, old_member, new_member;
  2199. old_state = netif_ip6_addr_state(netif, addr_idx);
  2200. /* Determine whether we were, and should be, a member of the solicited-node
  2201. * multicast group for this address. For tentative addresses, the group is
  2202. * not joined until the address enters the TENTATIVE_1 (or VALID) state. */
  2203. old_member = (old_state != IP6_ADDR_INVALID && old_state != IP6_ADDR_DUPLICATED && old_state != IP6_ADDR_TENTATIVE);
  2204. new_member = (new_state != IP6_ADDR_INVALID && new_state != IP6_ADDR_DUPLICATED && new_state != IP6_ADDR_TENTATIVE);
  2205. if (old_member != new_member) {
  2206. ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(netif, addr_idx)->addr[3]);
  2207. ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif);
  2208. if (new_member) {
  2209. mld6_joingroup_netif(netif, &multicast_address);
  2210. } else {
  2211. mld6_leavegroup_netif(netif, &multicast_address);
  2212. }
  2213. }
  2214. }
  2215. #endif /* LWIP_IPV6_MLD */
  2216. /** Netif was added, set up, or reconnected (link up) */
  2217. void
  2218. nd6_restart_netif(struct netif *netif)
  2219. {
  2220. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  2221. /* Send Router Solicitation messages (see RFC 4861, ch. 6.3.7). */
  2222. netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
  2223. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  2224. }
  2225. #endif /* LWIP_IPV6 */