Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
lwip_nd6.c
00001 /** 00002 * @file 00003 * 00004 * Neighbor discovery and stateless address autoconfiguration for IPv6. 00005 * Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862 00006 * (Address autoconfiguration). 00007 */ 00008 00009 /* 00010 * Copyright (c) 2010 Inico Technologies Ltd. 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. The name of the author may not be used to endorse or promote products 00022 * derived from this software without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00025 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00026 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00027 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00028 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00029 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00030 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00031 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00032 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00033 * OF SUCH DAMAGE. 00034 * 00035 * This file is part of the lwIP TCP/IP stack. 00036 * 00037 * Author: Ivan Delamer <delamer@inicotech.com> 00038 * 00039 * 00040 * Please coordinate changes and requests with Ivan Delamer 00041 * <delamer@inicotech.com> 00042 */ 00043 00044 #include "lwip/opt.h" 00045 00046 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ 00047 00048 #include "lwip/nd6.h" 00049 #include "lwip/priv/nd6_priv.h" 00050 #include "lwip/prot/nd6.h" 00051 #include "lwip/prot/icmp6.h" 00052 #include "lwip/pbuf.h" 00053 #include "lwip/mem.h" 00054 #include "lwip/memp.h" 00055 #include "lwip/ip6.h" 00056 #include "lwip/ip6_addr.h" 00057 #include "lwip/inet_chksum.h" 00058 #include "lwip/netif.h" 00059 #include "lwip/icmp6.h" 00060 #include "lwip/mld6.h" 00061 #include "lwip/dhcp6.h" 00062 #include "lwip/ip.h" 00063 #include "lwip/stats.h" 00064 #include "lwip/dns.h" 00065 00066 #include <string.h> 00067 00068 #ifdef LWIP_HOOK_FILENAME 00069 #include LWIP_HOOK_FILENAME 00070 #endif 00071 00072 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK 00073 #error LWIP_IPV6_DUP_DETECT_ATTEMPTS > IP6_ADDR_TENTATIVE_COUNT_MASK 00074 #endif 00075 00076 /* Router tables. */ 00077 struct nd6_neighbor_cache_entry neighbor_cache[LWIP_ND6_NUM_NEIGHBORS]; 00078 struct nd6_destination_cache_entry destination_cache[LWIP_ND6_NUM_DESTINATIONS]; 00079 struct nd6_prefix_list_entry prefix_list[LWIP_ND6_NUM_PREFIXES]; 00080 struct nd6_router_list_entry default_router_list[LWIP_ND6_NUM_ROUTERS]; 00081 00082 /* Default values, can be updated by a RA message. */ 00083 u32_t reachable_time = LWIP_ND6_REACHABLE_TIME; 00084 u32_t retrans_timer = LWIP_ND6_RETRANS_TIMER; /* @todo implement this value in timer */ 00085 00086 /* Index for cache entries. */ 00087 static u8_t nd6_cached_neighbor_index; 00088 static netif_addr_idx_t nd6_cached_destination_index; 00089 00090 /* Multicast address holder. */ 00091 static ip6_addr_t multicast_address; 00092 00093 static u8_t nd6_tmr_rs_reduction; 00094 00095 /* Static buffer to parse RA packet options */ 00096 union ra_options { 00097 struct lladdr_option lladdr; 00098 struct mtu_option mtu; 00099 struct prefix_option prefix; 00100 #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS 00101 struct rdnss_option rdnss; 00102 #endif 00103 }; 00104 static union ra_options nd6_ra_buffer; 00105 00106 /* Forward declarations. */ 00107 static s8_t nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr); 00108 static s8_t nd6_new_neighbor_cache_entry(void); 00109 static void nd6_free_neighbor_cache_entry(s8_t i); 00110 static s16_t nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr); 00111 static s16_t nd6_new_destination_cache_entry(void); 00112 static int nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif); 00113 static s8_t nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif); 00114 static s8_t nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif); 00115 static s8_t nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif); 00116 static s8_t nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif); 00117 static s8_t nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif); 00118 static s8_t nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif); 00119 static err_t nd6_queue_packet(s8_t neighbor_index, struct pbuf *q); 00120 00121 #define ND6_SEND_FLAG_MULTICAST_DEST 0x01 00122 #define ND6_SEND_FLAG_ALLNODES_DEST 0x02 00123 #define ND6_SEND_FLAG_ANY_SRC 0x04 00124 static void nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags); 00125 static void nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags); 00126 static void nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags); 00127 #if LWIP_IPV6_SEND_ROUTER_SOLICIT 00128 static err_t nd6_send_rs(struct netif *netif); 00129 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ 00130 00131 #if LWIP_ND6_QUEUEING 00132 static void nd6_free_q(struct nd6_q_entry *q); 00133 #else /* LWIP_ND6_QUEUEING */ 00134 #define nd6_free_q(q) pbuf_free(q) 00135 #endif /* LWIP_ND6_QUEUEING */ 00136 static void nd6_send_q(s8_t i); 00137 00138 00139 /** 00140 * A local address has been determined to be a duplicate. Take the appropriate 00141 * action(s) on the address and the interface as a whole. 00142 * 00143 * @param netif the netif that owns the address 00144 * @param addr_idx the index of the address detected to be a duplicate 00145 */ 00146 static void 00147 nd6_duplicate_addr_detected(struct netif *netif, s8_t addr_idx) 00148 { 00149 00150 /* Mark the address as duplicate, but leave its lifetimes alone. If this was 00151 * a manually assigned address, it will remain in existence as duplicate, and 00152 * as such be unusable for any practical purposes until manual intervention. 00153 * If this was an autogenerated address, the address will follow normal 00154 * expiration rules, and thus disappear once its valid lifetime expires. */ 00155 netif_ip6_addr_set_state(netif, addr_idx, IP6_ADDR_DUPLICATED); 00156 00157 #if LWIP_IPV6_AUTOCONFIG 00158 /* If the affected address was the link-local address that we use to generate 00159 * all other addresses, then we should not continue to use those derived 00160 * addresses either, so mark them as duplicate as well. For autoconfig-only 00161 * setups, this will make the interface effectively unusable, approaching the 00162 * intention of RFC 4862 Sec. 5.4.5. @todo implement the full requirements */ 00163 if (addr_idx == 0) { 00164 s8_t i; 00165 for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) { 00166 if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) && 00167 !netif_ip6_addr_isstatic(netif, i)) { 00168 netif_ip6_addr_set_state(netif, i, IP6_ADDR_DUPLICATED); 00169 } 00170 } 00171 } 00172 #endif /* LWIP_IPV6_AUTOCONFIG */ 00173 } 00174 00175 #if LWIP_IPV6_AUTOCONFIG 00176 /** 00177 * We received a router advertisement that contains a prefix with the 00178 * autoconfiguration flag set. Add or update an associated autogenerated 00179 * address. 00180 * 00181 * @param netif the netif on which the router advertisement arrived 00182 * @param prefix_opt a pointer to the prefix option data 00183 * @param prefix_addr an aligned copy of the prefix address 00184 */ 00185 static void 00186 nd6_process_autoconfig_prefix(struct netif *netif, 00187 struct prefix_option *prefix_opt, const ip6_addr_t *prefix_addr) 00188 { 00189 ip6_addr_t ip6addr; 00190 u32_t valid_life, pref_life; 00191 u8_t addr_state; 00192 s8_t i, free_idx; 00193 00194 /* The caller already checks RFC 4862 Sec. 5.5.3 points (a) and (b). We do 00195 * the rest, starting with checks for (c) and (d) here. */ 00196 valid_life = lwip_htonl(prefix_opt->valid_lifetime); 00197 pref_life = lwip_htonl(prefix_opt->preferred_lifetime); 00198 if (pref_life > valid_life || prefix_opt->prefix_length != 64) { 00199 return; /* silently ignore this prefix for autoconfiguration purposes */ 00200 } 00201 00202 /* If an autogenerated address already exists for this prefix, update its 00203 * lifetimes. An address is considered autogenerated if 1) it is not static 00204 * (i.e., manually assigned), and 2) there is an advertised autoconfiguration 00205 * prefix for it (the one we are processing here). This does not necessarily 00206 * exclude the possibility that the address was actually assigned by, say, 00207 * DHCPv6. If that distinction becomes important in the future, more state 00208 * must be kept. As explained elsewhere we also update lifetimes of tentative 00209 * and duplicate addresses. Skip address slot 0 (the link-local address). */ 00210 for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) { 00211 addr_state = netif_ip6_addr_state(netif, i); 00212 if (!ip6_addr_isinvalid(addr_state) && !netif_ip6_addr_isstatic(netif, i) && 00213 ip6_addr_netcmp(prefix_addr, netif_ip6_addr(netif, i))) { 00214 /* Update the valid lifetime, as per RFC 4862 Sec. 5.5.3 point (e). 00215 * The valid lifetime will never drop to zero as a result of this. */ 00216 u32_t remaining_life = netif_ip6_addr_valid_life(netif, i); 00217 if (valid_life > ND6_2HRS || valid_life > remaining_life) { 00218 netif_ip6_addr_set_valid_life(netif, i, valid_life); 00219 } else if (remaining_life > ND6_2HRS) { 00220 netif_ip6_addr_set_valid_life(netif, i, ND6_2HRS); 00221 } 00222 LWIP_ASSERT("bad valid lifetime", !netif_ip6_addr_isstatic(netif, i)); 00223 /* Update the preferred lifetime. No bounds checks are needed here. In 00224 * rare cases the advertisement may un-deprecate the address, though. 00225 * Deprecation is left to the timer code where it is handled anyway. */ 00226 if (pref_life > 0 && addr_state == IP6_ADDR_DEPRECATED) { 00227 netif_ip6_addr_set_state(netif, i, IP6_ADDR_PREFERRED); 00228 } 00229 netif_ip6_addr_set_pref_life(netif, i, pref_life); 00230 return; /* there should be at most one matching address */ 00231 } 00232 } 00233 00234 /* No autogenerated address exists for this prefix yet. See if we can add a 00235 * new one. However, if IPv6 autoconfiguration is administratively disabled, 00236 * do not generate new addresses, but do keep updating lifetimes for existing 00237 * addresses. Also, when adding new addresses, we must protect explicitly 00238 * against a valid lifetime of zero, because again, we use that as a special 00239 * value. The generated address would otherwise expire immediately anyway. 00240 * Finally, the original link-local address must be usable at all. We start 00241 * creating addresses even if the link-local address is still in tentative 00242 * state though, and deal with the fallout of that upon DAD collision. */ 00243 addr_state = netif_ip6_addr_state(netif, 0); 00244 if (!netif->ip6_autoconfig_enabled || valid_life == IP6_ADDR_LIFE_STATIC || 00245 ip6_addr_isinvalid(addr_state) || ip6_addr_isduplicated(addr_state)) { 00246 return; 00247 } 00248 00249 /* Construct the new address that we intend to use, and then see if that 00250 * address really does not exist. It might have been added manually, after 00251 * all. As a side effect, find a free slot. Note that we cannot use 00252 * netif_add_ip6_address() here, as it would return ERR_OK if the address 00253 * already did exist, resulting in that address being given lifetimes. */ 00254 IP6_ADDR(&ip6addr, prefix_addr->addr[0], prefix_addr->addr[1], 00255 netif_ip6_addr(netif, 0)->addr[2], netif_ip6_addr(netif, 0)->addr[3]); 00256 ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif); 00257 00258 free_idx = 0; 00259 for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) { 00260 if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) { 00261 if (ip6_addr_cmp(&ip6addr, netif_ip6_addr(netif, i))) { 00262 return; /* formed address already exists */ 00263 } 00264 } else if (free_idx == 0) { 00265 free_idx = i; 00266 } 00267 } 00268 if (free_idx == 0) { 00269 return; /* no address slots available, try again on next advertisement */ 00270 } 00271 00272 /* Assign the new address to the interface. */ 00273 ip_addr_copy_from_ip6(netif->ip6_addr[free_idx], ip6addr); 00274 netif_ip6_addr_set_valid_life(netif, free_idx, valid_life); 00275 netif_ip6_addr_set_pref_life(netif, free_idx, pref_life); 00276 netif_ip6_addr_set_state(netif, free_idx, IP6_ADDR_TENTATIVE); 00277 } 00278 #endif /* LWIP_IPV6_AUTOCONFIG */ 00279 00280 /** 00281 * Process an incoming neighbor discovery message 00282 * 00283 * @param p the nd packet, p->payload pointing to the icmpv6 header 00284 * @param inp the netif on which this packet was received 00285 */ 00286 void 00287 nd6_input(struct pbuf *p, struct netif *inp) 00288 { 00289 u8_t msg_type; 00290 s8_t i; 00291 s16_t dest_idx; 00292 00293 ND6_STATS_INC(nd6.recv); 00294 00295 msg_type = *((u8_t *)p->payload); 00296 switch (msg_type) { 00297 case ICMP6_TYPE_NA: /* Neighbor Advertisement. */ 00298 { 00299 struct na_header *na_hdr; 00300 struct lladdr_option *lladdr_opt; 00301 ip6_addr_t target_address; 00302 00303 /* Check that na header fits in packet. */ 00304 if (p->len < (sizeof(struct na_header))) { 00305 /* @todo debug message */ 00306 pbuf_free(p); 00307 ND6_STATS_INC(nd6.lenerr); 00308 ND6_STATS_INC(nd6.drop); 00309 return; 00310 } 00311 00312 na_hdr = (struct na_header *)p->payload; 00313 00314 /* Create an aligned, zoned copy of the target address. */ 00315 ip6_addr_copy_from_packed(target_address, na_hdr->target_address); 00316 ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); 00317 00318 /* Check a subset of the other RFC 4861 Sec. 7.1.2 requirements. */ 00319 if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || na_hdr->code != 0 || 00320 ip6_addr_ismulticast(&target_address)) { 00321 pbuf_free(p); 00322 ND6_STATS_INC(nd6.proterr); 00323 ND6_STATS_INC(nd6.drop); 00324 return; 00325 } 00326 00327 /* @todo RFC MUST: if IP destination is multicast, Solicited flag is zero */ 00328 /* @todo RFC MUST: all included options have a length greater than zero */ 00329 00330 /* Unsolicited NA?*/ 00331 if (ip6_addr_ismulticast(ip6_current_dest_addr())) { 00332 /* This is an unsolicited NA. 00333 * link-layer changed? 00334 * part of DAD mechanism? */ 00335 00336 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS 00337 /* If the target address matches this netif, it is a DAD response. */ 00338 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { 00339 if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) && 00340 !ip6_addr_isduplicated(netif_ip6_addr_state(inp, i)) && 00341 ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { 00342 /* We are using a duplicate address. */ 00343 nd6_duplicate_addr_detected(inp, i); 00344 00345 pbuf_free(p); 00346 return; 00347 } 00348 } 00349 #endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */ 00350 00351 /* Check that link-layer address option also fits in packet. */ 00352 if (p->len < (sizeof(struct na_header) + 2)) { 00353 /* @todo debug message */ 00354 pbuf_free(p); 00355 ND6_STATS_INC(nd6.lenerr); 00356 ND6_STATS_INC(nd6.drop); 00357 return; 00358 } 00359 00360 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header)); 00361 00362 if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) { 00363 /* @todo debug message */ 00364 pbuf_free(p); 00365 ND6_STATS_INC(nd6.lenerr); 00366 ND6_STATS_INC(nd6.drop); 00367 return; 00368 } 00369 00370 /* This is an unsolicited NA, most likely there was a LLADDR change. */ 00371 i = nd6_find_neighbor_cache_entry(&target_address); 00372 if (i >= 0) { 00373 if (na_hdr->flags & ND6_FLAG_OVERRIDE) { 00374 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); 00375 } 00376 } 00377 } else { 00378 /* This is a solicited NA. 00379 * neighbor address resolution response? 00380 * neighbor unreachability detection response? */ 00381 00382 /* Find the cache entry corresponding to this na. */ 00383 i = nd6_find_neighbor_cache_entry(&target_address); 00384 if (i < 0) { 00385 /* We no longer care about this target address. drop it. */ 00386 pbuf_free(p); 00387 return; 00388 } 00389 00390 /* Update cache entry. */ 00391 if ((na_hdr->flags & ND6_FLAG_OVERRIDE) || 00392 (neighbor_cache[i].state == ND6_INCOMPLETE)) { 00393 /* Check that link-layer address option also fits in packet. */ 00394 if (p->len < (sizeof(struct na_header) + 2)) { 00395 /* @todo debug message */ 00396 pbuf_free(p); 00397 ND6_STATS_INC(nd6.lenerr); 00398 ND6_STATS_INC(nd6.drop); 00399 return; 00400 } 00401 00402 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header)); 00403 00404 if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) { 00405 /* @todo debug message */ 00406 pbuf_free(p); 00407 ND6_STATS_INC(nd6.lenerr); 00408 ND6_STATS_INC(nd6.drop); 00409 return; 00410 } 00411 00412 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); 00413 } 00414 00415 neighbor_cache[i].netif = inp; 00416 neighbor_cache[i].state = ND6_REACHABLE; 00417 neighbor_cache[i].counter.reachable_time = reachable_time; 00418 00419 /* Send queued packets, if any. */ 00420 if (neighbor_cache[i].q != NULL) { 00421 nd6_send_q(i); 00422 } 00423 } 00424 00425 break; /* ICMP6_TYPE_NA */ 00426 } 00427 case ICMP6_TYPE_NS: /* Neighbor solicitation. */ 00428 { 00429 struct ns_header *ns_hdr; 00430 struct lladdr_option *lladdr_opt; 00431 ip6_addr_t target_address; 00432 u8_t accepted; 00433 00434 /* Check that ns header fits in packet. */ 00435 if (p->len < sizeof(struct ns_header)) { 00436 /* @todo debug message */ 00437 pbuf_free(p); 00438 ND6_STATS_INC(nd6.lenerr); 00439 ND6_STATS_INC(nd6.drop); 00440 return; 00441 } 00442 00443 ns_hdr = (struct ns_header *)p->payload; 00444 00445 /* Create an aligned, zoned copy of the target address. */ 00446 ip6_addr_copy_from_packed(target_address, ns_hdr->target_address); 00447 ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); 00448 00449 /* Check a subset of the other RFC 4861 Sec. 7.1.1 requirements. */ 00450 if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ns_hdr->code != 0 || 00451 ip6_addr_ismulticast(&target_address)) { 00452 pbuf_free(p); 00453 ND6_STATS_INC(nd6.proterr); 00454 ND6_STATS_INC(nd6.drop); 00455 return; 00456 } 00457 00458 /* @todo RFC MUST: all included options have a length greater than zero */ 00459 /* @todo RFC MUST: if IP source is 'any', destination is solicited-node multicast address */ 00460 /* @todo RFC MUST: if IP source is 'any', there is no source LL address option */ 00461 00462 /* Check if there is a link-layer address provided. Only point to it if in this buffer. */ 00463 if (p->len >= (sizeof(struct ns_header) + 2)) { 00464 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header)); 00465 if (p->len < (sizeof(struct ns_header) + (lladdr_opt->length << 3))) { 00466 lladdr_opt = NULL; 00467 } 00468 } else { 00469 lladdr_opt = NULL; 00470 } 00471 00472 /* Check if the target address is configured on the receiving netif. */ 00473 accepted = 0; 00474 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { 00475 if ((ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) || 00476 (ip6_addr_istentative(netif_ip6_addr_state(inp, i)) && 00477 ip6_addr_isany(ip6_current_src_addr()))) && 00478 ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { 00479 accepted = 1; 00480 break; 00481 } 00482 } 00483 00484 /* NS not for us? */ 00485 if (!accepted) { 00486 pbuf_free(p); 00487 return; 00488 } 00489 00490 /* Check for ANY address in src (DAD algorithm). */ 00491 if (ip6_addr_isany(ip6_current_src_addr())) { 00492 /* Sender is validating this address. */ 00493 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { 00494 if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) && 00495 ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { 00496 /* Send a NA back so that the sender does not use this address. */ 00497 nd6_send_na(inp, netif_ip6_addr(inp, i), ND6_FLAG_OVERRIDE | ND6_SEND_FLAG_ALLNODES_DEST); 00498 if (ip6_addr_istentative(netif_ip6_addr_state(inp, i))) { 00499 /* We shouldn't use this address either. */ 00500 nd6_duplicate_addr_detected(inp, i); 00501 } 00502 } 00503 } 00504 } else { 00505 /* Sender is trying to resolve our address. */ 00506 /* Verify that they included their own link-layer address. */ 00507 if (lladdr_opt == NULL) { 00508 /* Not a valid message. */ 00509 pbuf_free(p); 00510 ND6_STATS_INC(nd6.proterr); 00511 ND6_STATS_INC(nd6.drop); 00512 return; 00513 } 00514 00515 i = nd6_find_neighbor_cache_entry(ip6_current_src_addr()); 00516 if (i>= 0) { 00517 /* We already have a record for the solicitor. */ 00518 if (neighbor_cache[i].state == ND6_INCOMPLETE) { 00519 neighbor_cache[i].netif = inp; 00520 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); 00521 00522 /* Delay probe in case we get confirmation of reachability from upper layer (TCP). */ 00523 neighbor_cache[i].state = ND6_DELAY; 00524 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; 00525 } 00526 } else { 00527 /* Add their IPv6 address and link-layer address to neighbor cache. 00528 * We will need it at least to send a unicast NA message, but most 00529 * likely we will also be communicating with this node soon. */ 00530 i = nd6_new_neighbor_cache_entry(); 00531 if (i < 0) { 00532 /* We couldn't assign a cache entry for this neighbor. 00533 * we won't be able to reply. drop it. */ 00534 pbuf_free(p); 00535 ND6_STATS_INC(nd6.memerr); 00536 return; 00537 } 00538 neighbor_cache[i].netif = inp; 00539 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); 00540 ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr()); 00541 00542 /* Receiving a message does not prove reachability: only in one direction. 00543 * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ 00544 neighbor_cache[i].state = ND6_DELAY; 00545 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; 00546 } 00547 00548 /* Send back a NA for us. Allocate the reply pbuf. */ 00549 nd6_send_na(inp, &target_address, ND6_FLAG_SOLICITED | ND6_FLAG_OVERRIDE); 00550 } 00551 00552 break; /* ICMP6_TYPE_NS */ 00553 } 00554 case ICMP6_TYPE_RA: /* Router Advertisement. */ 00555 { 00556 struct ra_header *ra_hdr; 00557 u8_t *buffer; /* Used to copy options. */ 00558 u16_t offset; 00559 #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS 00560 /* There can be multiple RDNSS options per RA */ 00561 u8_t rdnss_server_idx = 0; 00562 #endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */ 00563 00564 /* Check that RA header fits in packet. */ 00565 if (p->len < sizeof(struct ra_header)) { 00566 /* @todo debug message */ 00567 pbuf_free(p); 00568 ND6_STATS_INC(nd6.lenerr); 00569 ND6_STATS_INC(nd6.drop); 00570 return; 00571 } 00572 00573 ra_hdr = (struct ra_header *)p->payload; 00574 00575 /* Check a subset of the other RFC 4861 Sec. 6.1.2 requirements. */ 00576 if (!ip6_addr_islinklocal(ip6_current_src_addr()) || 00577 IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ra_hdr->code != 0) { 00578 pbuf_free(p); 00579 ND6_STATS_INC(nd6.proterr); 00580 ND6_STATS_INC(nd6.drop); 00581 return; 00582 } 00583 00584 /* @todo RFC MUST: all included options have a length greater than zero */ 00585 00586 /* If we are sending RS messages, stop. */ 00587 #if LWIP_IPV6_SEND_ROUTER_SOLICIT 00588 /* ensure at least one solicitation is sent (see RFC 4861, ch. 6.3.7) */ 00589 if ((inp->rs_count < LWIP_ND6_MAX_MULTICAST_SOLICIT) || 00590 (nd6_send_rs(inp) == ERR_OK)) { 00591 inp->rs_count = 0; 00592 } else { 00593 inp->rs_count = 1; 00594 } 00595 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ 00596 00597 /* Get the matching default router entry. */ 00598 i = nd6_get_router(ip6_current_src_addr(), inp); 00599 if (i < 0) { 00600 /* Create a new router entry. */ 00601 i = nd6_new_router(ip6_current_src_addr(), inp); 00602 } 00603 00604 if (i < 0) { 00605 /* Could not create a new router entry. */ 00606 pbuf_free(p); 00607 ND6_STATS_INC(nd6.memerr); 00608 return; 00609 } 00610 00611 /* Re-set invalidation timer. */ 00612 default_router_list[i].invalidation_timer = lwip_htons(ra_hdr->router_lifetime); 00613 00614 /* Re-set default timer values. */ 00615 #if LWIP_ND6_ALLOW_RA_UPDATES 00616 if (ra_hdr->retrans_timer > 0) { 00617 retrans_timer = lwip_htonl(ra_hdr->retrans_timer); 00618 } 00619 if (ra_hdr->reachable_time > 0) { 00620 reachable_time = lwip_htonl(ra_hdr->reachable_time); 00621 } 00622 #endif /* LWIP_ND6_ALLOW_RA_UPDATES */ 00623 00624 /* @todo set default hop limit... */ 00625 /* ra_hdr->current_hop_limit;*/ 00626 00627 /* Update flags in local entry (incl. preference). */ 00628 default_router_list[i].flags = ra_hdr->flags; 00629 00630 #if LWIP_IPV6_DHCP6 00631 /* Trigger DHCPv6 if enabled */ 00632 dhcp6_nd6_ra_trigger(inp, ra_hdr->flags & ND6_RA_FLAG_MANAGED_ADDR_CONFIG, 00633 ra_hdr->flags & ND6_RA_FLAG_OTHER_CONFIG); 00634 #endif 00635 00636 /* Offset to options. */ 00637 offset = sizeof(struct ra_header); 00638 00639 /* Process each option. */ 00640 while ((p->tot_len - offset) >= 2) { 00641 u8_t option_type; 00642 u16_t option_len; 00643 int option_len8 = pbuf_try_get_at(p, offset + 1); 00644 if (option_len8 <= 0) { 00645 /* read beyond end or zero length */ 00646 goto lenerr_drop_free_return; 00647 } 00648 option_len = ((u8_t)option_len8) << 3; 00649 if (option_len > p->tot_len - offset) { 00650 /* short packet (option does not fit in) */ 00651 goto lenerr_drop_free_return; 00652 } 00653 if (p->len == p->tot_len) { 00654 /* no need to copy from contiguous pbuf */ 00655 buffer = &((u8_t*)p->payload)[offset]; 00656 } else { 00657 /* check if this option fits into our buffer */ 00658 if (option_len > sizeof(nd6_ra_buffer)) { 00659 option_type = pbuf_get_at(p, offset); 00660 /* invalid option length */ 00661 if (option_type != ND6_OPTION_TYPE_RDNSS) { 00662 goto lenerr_drop_free_return; 00663 } 00664 /* we allow RDNSS option to be longer - we'll just drop some servers */ 00665 option_len = sizeof(nd6_ra_buffer); 00666 } 00667 buffer = (u8_t*)&nd6_ra_buffer; 00668 option_len = pbuf_copy_partial(p, &nd6_ra_buffer, option_len, offset); 00669 } 00670 option_type = buffer[0]; 00671 switch (option_type) { 00672 case ND6_OPTION_TYPE_SOURCE_LLADDR: 00673 { 00674 struct lladdr_option *lladdr_opt; 00675 if (option_len < sizeof(struct lladdr_option)) { 00676 goto lenerr_drop_free_return; 00677 } 00678 lladdr_opt = (struct lladdr_option *)buffer; 00679 if ((default_router_list[i].neighbor_entry != NULL) && 00680 (default_router_list[i].neighbor_entry->state == ND6_INCOMPLETE)) { 00681 SMEMCPY(default_router_list[i].neighbor_entry->lladdr, lladdr_opt->addr, inp->hwaddr_len); 00682 default_router_list[i].neighbor_entry->state = ND6_REACHABLE; 00683 default_router_list[i].neighbor_entry->counter.reachable_time = reachable_time; 00684 } 00685 break; 00686 } 00687 case ND6_OPTION_TYPE_MTU: 00688 { 00689 struct mtu_option *mtu_opt; 00690 u32_t mtu32; 00691 if (option_len < sizeof(struct mtu_option)) { 00692 goto lenerr_drop_free_return; 00693 } 00694 mtu_opt = (struct mtu_option *)buffer; 00695 mtu32 = lwip_htonl(mtu_opt->mtu); 00696 if ((mtu32 >= 1280) && (mtu32 <= 0xffff)) { 00697 #if LWIP_ND6_ALLOW_RA_UPDATES 00698 if (inp->mtu) { 00699 /* don't set the mtu for IPv6 higher than the netif driver supports */ 00700 inp->mtu6 = LWIP_MIN(inp->mtu, (u16_t)mtu32); 00701 } else { 00702 inp->mtu6 = (u16_t)mtu32; 00703 } 00704 #endif /* LWIP_ND6_ALLOW_RA_UPDATES */ 00705 } 00706 break; 00707 } 00708 case ND6_OPTION_TYPE_PREFIX_INFO: 00709 { 00710 struct prefix_option *prefix_opt; 00711 ip6_addr_t prefix_addr; 00712 if (option_len < sizeof(struct prefix_option)) { 00713 goto lenerr_drop_free_return; 00714 } 00715 00716 prefix_opt = (struct prefix_option *)buffer; 00717 00718 /* Get a memory-aligned copy of the prefix. */ 00719 ip6_addr_copy_from_packed(prefix_addr, prefix_opt->prefix); 00720 ip6_addr_assign_zone(&prefix_addr, IP6_UNICAST, inp); 00721 00722 if (!ip6_addr_islinklocal(&prefix_addr)) { 00723 if ((prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) && 00724 (prefix_opt->prefix_length == 64)) { 00725 /* Add to on-link prefix list. */ 00726 u32_t valid_life; 00727 s8_t prefix; 00728 00729 valid_life = lwip_htonl(prefix_opt->valid_lifetime); 00730 00731 /* find cache entry for this prefix. */ 00732 prefix = nd6_get_onlink_prefix(&prefix_addr, inp); 00733 if (prefix < 0 && valid_life > 0) { 00734 /* Create a new cache entry. */ 00735 prefix = nd6_new_onlink_prefix(&prefix_addr, inp); 00736 } 00737 if (prefix >= 0) { 00738 prefix_list[prefix].invalidation_timer = valid_life; 00739 } 00740 } 00741 #if LWIP_IPV6_AUTOCONFIG 00742 if (prefix_opt->flags & ND6_PREFIX_FLAG_AUTONOMOUS) { 00743 /* Perform processing for autoconfiguration. */ 00744 nd6_process_autoconfig_prefix(inp, prefix_opt, &prefix_addr); 00745 } 00746 #endif /* LWIP_IPV6_AUTOCONFIG */ 00747 } 00748 00749 break; 00750 } 00751 case ND6_OPTION_TYPE_ROUTE_INFO: 00752 /* @todo implement preferred routes. 00753 struct route_option * route_opt; 00754 route_opt = (struct route_option *)buffer;*/ 00755 00756 break; 00757 #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS 00758 case ND6_OPTION_TYPE_RDNSS: 00759 { 00760 u8_t num, n; 00761 u16_t copy_offset = offset + SIZEOF_RDNSS_OPTION_BASE; 00762 struct rdnss_option * rdnss_opt; 00763 if (option_len < SIZEOF_RDNSS_OPTION_BASE) { 00764 goto lenerr_drop_free_return; 00765 } 00766 00767 rdnss_opt = (struct rdnss_option *)buffer; 00768 num = (rdnss_opt->length - 1) / 2; 00769 for (n = 0; (rdnss_server_idx < DNS_MAX_SERVERS) && (n < num); n++) { 00770 ip_addr_t rdnss_address; 00771 00772 /* Copy directly from pbuf to get an aligned, zoned copy of the prefix. */ 00773 if (pbuf_copy_partial(p, &rdnss_address, sizeof(ip6_addr_p_t), copy_offset) == sizeof(ip6_addr_p_t)) { 00774 IP_SET_TYPE_VAL(rdnss_address, IPADDR_TYPE_V6); 00775 ip6_addr_assign_zone(ip_2_ip6(&rdnss_address), IP6_UNKNOWN, inp); 00776 00777 if (htonl(rdnss_opt->lifetime) > 0) { 00778 /* TODO implement Lifetime > 0 */ 00779 dns_setserver(rdnss_server_idx++, &rdnss_address, inp); 00780 } else { 00781 /* TODO implement DNS removal in dns.c */ 00782 u8_t s; 00783 for (s = 0; s < DNS_MAX_SERVERS; s++) { 00784 const ip_addr_t *addr = dns_getserver(s,netif_get_name(inp)); 00785 if(ip_addr_cmp(addr, &rdnss_address)) { 00786 dns_setserver(s, NULL, inp); 00787 } 00788 } 00789 } 00790 } 00791 } 00792 break; 00793 } 00794 #endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */ 00795 default: 00796 /* Unrecognized option, abort. */ 00797 ND6_STATS_INC(nd6.proterr); 00798 break; 00799 } 00800 /* option length is checked earlier to be non-zero to make sure loop ends */ 00801 offset += 8 * (u8_t)option_len8; 00802 } 00803 00804 break; /* ICMP6_TYPE_RA */ 00805 } 00806 case ICMP6_TYPE_RD: /* Redirect */ 00807 { 00808 struct redirect_header *redir_hdr; 00809 struct lladdr_option *lladdr_opt; 00810 ip6_addr_t destination_address, target_address; 00811 00812 /* Check that Redir header fits in packet. */ 00813 if (p->len < sizeof(struct redirect_header)) { 00814 /* @todo debug message */ 00815 pbuf_free(p); 00816 ND6_STATS_INC(nd6.lenerr); 00817 ND6_STATS_INC(nd6.drop); 00818 return; 00819 } 00820 00821 redir_hdr = (struct redirect_header *)p->payload; 00822 00823 /* Create an aligned, zoned copy of the destination address. */ 00824 ip6_addr_copy_from_packed(destination_address, redir_hdr->destination_address); 00825 ip6_addr_assign_zone(&destination_address, IP6_UNICAST, inp); 00826 00827 /* Check a subset of the other RFC 4861 Sec. 8.1 requirements. */ 00828 if (!ip6_addr_islinklocal(ip6_current_src_addr()) || 00829 IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || 00830 redir_hdr->code != 0 || ip6_addr_ismulticast(&destination_address)) { 00831 pbuf_free(p); 00832 ND6_STATS_INC(nd6.proterr); 00833 ND6_STATS_INC(nd6.drop); 00834 return; 00835 } 00836 00837 /* @todo RFC MUST: IP source address equals first-hop router for destination_address */ 00838 /* @todo RFC MUST: ICMP target address is either link-local address or same as destination_address */ 00839 /* @todo RFC MUST: all included options have a length greater than zero */ 00840 00841 if (p->len >= (sizeof(struct redirect_header) + 2)) { 00842 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct redirect_header)); 00843 if (p->len < (sizeof(struct redirect_header) + (lladdr_opt->length << 3))) { 00844 lladdr_opt = NULL; 00845 } 00846 } else { 00847 lladdr_opt = NULL; 00848 } 00849 00850 /* Find dest address in cache */ 00851 dest_idx = nd6_find_destination_cache_entry(&destination_address); 00852 if (dest_idx < 0) { 00853 /* Destination not in cache, drop packet. */ 00854 pbuf_free(p); 00855 return; 00856 } 00857 00858 /* Create an aligned, zoned copy of the target address. */ 00859 ip6_addr_copy_from_packed(target_address, redir_hdr->target_address); 00860 ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); 00861 00862 /* Set the new target address. */ 00863 ip6_addr_copy(destination_cache[dest_idx].next_hop_addr, target_address); 00864 00865 /* If Link-layer address of other router is given, try to add to neighbor cache. */ 00866 if (lladdr_opt != NULL) { 00867 if (lladdr_opt->type == ND6_OPTION_TYPE_TARGET_LLADDR) { 00868 i = nd6_find_neighbor_cache_entry(&target_address); 00869 if (i < 0) { 00870 i = nd6_new_neighbor_cache_entry(); 00871 if (i >= 0) { 00872 neighbor_cache[i].netif = inp; 00873 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); 00874 ip6_addr_copy(neighbor_cache[i].next_hop_address, target_address); 00875 00876 /* Receiving a message does not prove reachability: only in one direction. 00877 * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ 00878 neighbor_cache[i].state = ND6_DELAY; 00879 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; 00880 } 00881 } 00882 if (i >= 0) { 00883 if (neighbor_cache[i].state == ND6_INCOMPLETE) { 00884 MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); 00885 /* Receiving a message does not prove reachability: only in one direction. 00886 * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ 00887 neighbor_cache[i].state = ND6_DELAY; 00888 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; 00889 } 00890 } 00891 } 00892 } 00893 break; /* ICMP6_TYPE_RD */ 00894 } 00895 case ICMP6_TYPE_PTB: /* Packet too big */ 00896 { 00897 struct icmp6_hdr *icmp6hdr; /* Packet too big message */ 00898 struct ip6_hdr *ip6hdr; /* IPv6 header of the packet which caused the error */ 00899 u32_t pmtu; 00900 ip6_addr_t destination_address; 00901 00902 /* Check that ICMPv6 header + IPv6 header fit in payload */ 00903 if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) { 00904 /* drop short packets */ 00905 pbuf_free(p); 00906 ND6_STATS_INC(nd6.lenerr); 00907 ND6_STATS_INC(nd6.drop); 00908 return; 00909 } 00910 00911 icmp6hdr = (struct icmp6_hdr *)p->payload; 00912 ip6hdr = (struct ip6_hdr *)((u8_t*)p->payload + sizeof(struct icmp6_hdr)); 00913 00914 /* Create an aligned, zoned copy of the destination address. */ 00915 ip6_addr_copy_from_packed(destination_address, ip6hdr->dest); 00916 ip6_addr_assign_zone(&destination_address, IP6_UNKNOWN, inp); 00917 00918 /* Look for entry in destination cache. */ 00919 dest_idx = nd6_find_destination_cache_entry(&destination_address); 00920 if (dest_idx < 0) { 00921 /* Destination not in cache, drop packet. */ 00922 pbuf_free(p); 00923 return; 00924 } 00925 00926 /* Change the Path MTU. */ 00927 pmtu = lwip_htonl(icmp6hdr->data); 00928 destination_cache[dest_idx].pmtu = (u16_t)LWIP_MIN(pmtu, 0xFFFF); 00929 00930 break; /* ICMP6_TYPE_PTB */ 00931 } 00932 00933 default: 00934 ND6_STATS_INC(nd6.proterr); 00935 ND6_STATS_INC(nd6.drop); 00936 break; /* default */ 00937 } 00938 00939 pbuf_free(p); 00940 return; 00941 lenerr_drop_free_return: 00942 ND6_STATS_INC(nd6.lenerr); 00943 ND6_STATS_INC(nd6.drop); 00944 pbuf_free(p); 00945 } 00946 00947 00948 /** 00949 * Periodic timer for Neighbor discovery functions: 00950 * 00951 * - Update neighbor reachability states 00952 * - Update destination cache entries age 00953 * - Update invalidation timers of default routers and on-link prefixes 00954 * - Update lifetimes of our addresses 00955 * - Perform duplicate address detection (DAD) for our addresses 00956 * - Send router solicitations 00957 */ 00958 void 00959 nd6_tmr(void) 00960 { 00961 s8_t i; 00962 struct netif *netif; 00963 00964 /* Process neighbor entries. */ 00965 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 00966 switch (neighbor_cache[i].state) { 00967 case ND6_INCOMPLETE: 00968 if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) && 00969 (!neighbor_cache[i].isrouter)) { 00970 /* Retries exceeded. */ 00971 nd6_free_neighbor_cache_entry(i); 00972 } else { 00973 /* Send a NS for this entry. */ 00974 neighbor_cache[i].counter.probes_sent++; 00975 nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST); 00976 } 00977 break; 00978 case ND6_REACHABLE: 00979 /* Send queued packets, if any are left. Should have been sent already. */ 00980 if (neighbor_cache[i].q != NULL) { 00981 nd6_send_q(i); 00982 } 00983 if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) { 00984 /* Change to stale state. */ 00985 neighbor_cache[i].state = ND6_STALE; 00986 neighbor_cache[i].counter.stale_time = 0; 00987 } else { 00988 neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL; 00989 } 00990 break; 00991 case ND6_STALE: 00992 neighbor_cache[i].counter.stale_time++; 00993 break; 00994 case ND6_DELAY: 00995 if (neighbor_cache[i].counter.delay_time <= 1) { 00996 /* Change to PROBE state. */ 00997 neighbor_cache[i].state = ND6_PROBE; 00998 neighbor_cache[i].counter.probes_sent = 0; 00999 } else { 01000 neighbor_cache[i].counter.delay_time--; 01001 } 01002 break; 01003 case ND6_PROBE: 01004 if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) && 01005 (!neighbor_cache[i].isrouter)) { 01006 /* Retries exceeded. */ 01007 nd6_free_neighbor_cache_entry(i); 01008 } else { 01009 /* Send a NS for this entry. */ 01010 neighbor_cache[i].counter.probes_sent++; 01011 nd6_send_neighbor_cache_probe(&neighbor_cache[i], 0); 01012 } 01013 break; 01014 case ND6_NO_ENTRY: 01015 default: 01016 /* Do nothing. */ 01017 break; 01018 } 01019 } 01020 01021 /* Process destination entries. */ 01022 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) { 01023 destination_cache[i].age++; 01024 } 01025 01026 /* Process router entries. */ 01027 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) { 01028 if (default_router_list[i].neighbor_entry != NULL) { 01029 /* Active entry. */ 01030 if (default_router_list[i].invalidation_timer <= ND6_TMR_INTERVAL / 1000) { 01031 /* No more than 1 second remaining. Clear this entry. Also clear any of 01032 * its destination cache entries, as per RFC 4861 Sec. 5.3 and 6.3.5. */ 01033 s8_t j; 01034 for (j = 0; j < LWIP_ND6_NUM_DESTINATIONS; j++) { 01035 if (ip6_addr_cmp(&destination_cache[j].next_hop_addr, 01036 &default_router_list[i].neighbor_entry->next_hop_address)) { 01037 ip6_addr_set_any(&destination_cache[j].destination_addr); 01038 } 01039 } 01040 default_router_list[i].neighbor_entry->isrouter = 0; 01041 default_router_list[i].neighbor_entry = NULL; 01042 default_router_list[i].invalidation_timer = 0; 01043 default_router_list[i].flags = 0; 01044 } else { 01045 default_router_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000; 01046 } 01047 } 01048 } 01049 01050 /* Process prefix entries. */ 01051 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) { 01052 if (prefix_list[i].netif != NULL) { 01053 if (prefix_list[i].invalidation_timer <= ND6_TMR_INTERVAL / 1000) { 01054 /* Entry timed out, remove it */ 01055 prefix_list[i].invalidation_timer = 0; 01056 prefix_list[i].netif = NULL; 01057 } else { 01058 prefix_list[i].invalidation_timer -= ND6_TMR_INTERVAL / 1000; 01059 } 01060 } 01061 } 01062 01063 /* Process our own addresses, updating address lifetimes and/or DAD state. */ 01064 NETIF_FOREACH(netif) { 01065 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { 01066 u8_t addr_state; 01067 #if LWIP_IPV6_ADDRESS_LIFETIMES 01068 /* Step 1: update address lifetimes (valid and preferred). */ 01069 addr_state = netif_ip6_addr_state(netif, i); 01070 /* RFC 4862 is not entirely clear as to whether address lifetimes affect 01071 * tentative addresses, and is even less clear as to what should happen 01072 * with duplicate addresses. We choose to track and update lifetimes for 01073 * both those types, although for different reasons: 01074 * - for tentative addresses, the line of thought of Sec. 5.7 combined 01075 * with the potentially long period that an address may be in tentative 01076 * state (due to the interface being down) suggests that lifetimes 01077 * should be independent of external factors which would include DAD; 01078 * - for duplicate addresses, retiring them early could result in a new 01079 * but unwanted attempt at marking them as valid, while retiring them 01080 * late/never could clog up address slots on the netif. 01081 * As a result, we may end up expiring addresses of either type here. 01082 */ 01083 if (!ip6_addr_isinvalid(addr_state) && 01084 !netif_ip6_addr_isstatic(netif, i)) { 01085 u32_t life = netif_ip6_addr_valid_life(netif, i); 01086 if (life <= ND6_TMR_INTERVAL / 1000) { 01087 /* The address has expired. */ 01088 netif_ip6_addr_set_valid_life(netif, i, 0); 01089 netif_ip6_addr_set_pref_life(netif, i, 0); 01090 netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID); 01091 } else { 01092 if (!ip6_addr_life_isinfinite(life)) { 01093 life -= ND6_TMR_INTERVAL / 1000; 01094 LWIP_ASSERT("bad valid lifetime", life != IP6_ADDR_LIFE_STATIC); 01095 netif_ip6_addr_set_valid_life(netif, i, life); 01096 } 01097 /* The address is still here. Update the preferred lifetime too. */ 01098 life = netif_ip6_addr_pref_life(netif, i); 01099 if (life <= ND6_TMR_INTERVAL / 1000) { 01100 /* This case must also trigger if 'life' was already zero, so as to 01101 * deal correctly with advertised preferred-lifetime reductions. */ 01102 netif_ip6_addr_set_pref_life(netif, i, 0); 01103 if (addr_state == IP6_ADDR_PREFERRED) 01104 netif_ip6_addr_set_state(netif, i, IP6_ADDR_DEPRECATED); 01105 } else if (!ip6_addr_life_isinfinite(life)) { 01106 life -= ND6_TMR_INTERVAL / 1000; 01107 netif_ip6_addr_set_pref_life(netif, i, life); 01108 } 01109 } 01110 } 01111 /* The address state may now have changed, so reobtain it next. */ 01112 #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */ 01113 /* Step 2: update DAD state. */ 01114 addr_state = netif_ip6_addr_state(netif, i); 01115 if (ip6_addr_istentative(addr_state)) { 01116 if ((addr_state & IP6_ADDR_TENTATIVE_COUNT_MASK) >= LWIP_IPV6_DUP_DETECT_ATTEMPTS) { 01117 /* No NA received in response. Mark address as valid. For dynamic 01118 * addresses with an expired preferred lifetime, the state is set to 01119 * deprecated right away. That should almost never happen, though. */ 01120 addr_state = IP6_ADDR_PREFERRED; 01121 #if LWIP_IPV6_ADDRESS_LIFETIMES 01122 if (!netif_ip6_addr_isstatic(netif, i) && 01123 netif_ip6_addr_pref_life(netif, i) == 0) { 01124 addr_state = IP6_ADDR_DEPRECATED; 01125 } 01126 #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */ 01127 netif_ip6_addr_set_state(netif, i, addr_state); 01128 } else if (netif_is_up(netif) && netif_is_link_up(netif)) { 01129 /* tentative: set next state by increasing by one */ 01130 netif_ip6_addr_set_state(netif, i, addr_state + 1); 01131 /* Send a NS for this address. Use the unspecified address as source 01132 * address in all cases (RFC 4862 Sec. 5.4.2), not in the least 01133 * because as it is, we only consider multicast replies for DAD. */ 01134 nd6_send_ns(netif, netif_ip6_addr(netif, i), 01135 ND6_SEND_FLAG_MULTICAST_DEST | ND6_SEND_FLAG_ANY_SRC); 01136 } 01137 } 01138 } 01139 } 01140 01141 #if LWIP_IPV6_SEND_ROUTER_SOLICIT 01142 /* Send router solicitation messages, if necessary. */ 01143 if (!nd6_tmr_rs_reduction) { 01144 nd6_tmr_rs_reduction = (ND6_RTR_SOLICITATION_INTERVAL / ND6_TMR_INTERVAL) - 1; 01145 NETIF_FOREACH(netif) { 01146 if ((netif->rs_count > 0) && netif_is_up(netif) && 01147 netif_is_link_up(netif) && 01148 !ip6_addr_isinvalid(netif_ip6_addr_state(netif, 0)) && 01149 !ip6_addr_isduplicated(netif_ip6_addr_state(netif, 0))) { 01150 if (nd6_send_rs(netif) == ERR_OK) { 01151 netif->rs_count--; 01152 } 01153 } 01154 } 01155 } else { 01156 nd6_tmr_rs_reduction--; 01157 } 01158 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ 01159 01160 } 01161 01162 /** Send a neighbor solicitation message for a specific neighbor cache entry 01163 * 01164 * @param entry the neightbor cache entry for wich to send the message 01165 * @param flags one of ND6_SEND_FLAG_* 01166 */ 01167 static void 01168 nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags) 01169 { 01170 nd6_send_ns(entry->netif, &entry->next_hop_address, flags); 01171 } 01172 01173 /** 01174 * Send a neighbor solicitation message 01175 * 01176 * @param netif the netif on which to send the message 01177 * @param target_addr the IPv6 target address for the ND message 01178 * @param flags one of ND6_SEND_FLAG_* 01179 */ 01180 static void 01181 nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags) 01182 { 01183 struct ns_header *ns_hdr; 01184 struct pbuf *p; 01185 const ip6_addr_t *src_addr; 01186 u16_t lladdr_opt_len; 01187 01188 LWIP_ASSERT("target address is required", target_addr != NULL); 01189 01190 if (!(flags & ND6_SEND_FLAG_ANY_SRC) && 01191 ip6_addr_isvalid(netif_ip6_addr_state(netif,0))) { 01192 /* Use link-local address as source address. */ 01193 src_addr = netif_ip6_addr(netif, 0); 01194 /* calculate option length (in 8-byte-blocks) */ 01195 lladdr_opt_len = ((netif->hwaddr_len + 2) + 7) >> 3; 01196 } else { 01197 src_addr = IP6_ADDR_ANY6; 01198 /* Option "MUST NOT be included when the source IP address is the unspecified address." */ 01199 lladdr_opt_len = 0; 01200 } 01201 01202 /* Allocate a packet. */ 01203 p = pbuf_alloc(PBUF_IP, sizeof(struct ns_header) + (lladdr_opt_len << 3), PBUF_RAM); 01204 if (p == NULL) { 01205 ND6_STATS_INC(nd6.memerr); 01206 return; 01207 } 01208 01209 /* Set fields. */ 01210 ns_hdr = (struct ns_header *)p->payload; 01211 01212 ns_hdr->type = ICMP6_TYPE_NS; 01213 ns_hdr->code = 0; 01214 ns_hdr->chksum = 0; 01215 ns_hdr->reserved = 0; 01216 ip6_addr_copy_to_packed(ns_hdr->target_address, *target_addr); 01217 01218 if (lladdr_opt_len != 0) { 01219 struct lladdr_option *lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header)); 01220 lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR; 01221 lladdr_opt->length = (u8_t)lladdr_opt_len; 01222 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len); 01223 } 01224 01225 /* Generate the solicited node address for the target address. */ 01226 if (flags & ND6_SEND_FLAG_MULTICAST_DEST) { 01227 ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]); 01228 ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif); 01229 target_addr = &multicast_address; 01230 } 01231 01232 #if CHECKSUM_GEN_ICMP6 01233 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { 01234 ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, 01235 target_addr); 01236 } 01237 #endif /* CHECKSUM_GEN_ICMP6 */ 01238 01239 /* Send the packet out. */ 01240 ND6_STATS_INC(nd6.xmit); 01241 ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, target_addr, 01242 ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); 01243 pbuf_free(p); 01244 } 01245 01246 /** 01247 * Send a neighbor advertisement message 01248 * 01249 * @param netif the netif on which to send the message 01250 * @param target_addr the IPv6 target address for the ND message 01251 * @param flags one of ND6_SEND_FLAG_* 01252 */ 01253 static void 01254 nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags) 01255 { 01256 struct na_header *na_hdr; 01257 struct lladdr_option *lladdr_opt; 01258 struct pbuf *p; 01259 const ip6_addr_t *src_addr; 01260 const ip6_addr_t *dest_addr; 01261 u16_t lladdr_opt_len; 01262 01263 LWIP_ASSERT("target address is required", target_addr != NULL); 01264 01265 /* Use link-local address as source address. */ 01266 /* src_addr = netif_ip6_addr(netif, 0); */ 01267 /* Use target address as source address. */ 01268 src_addr = target_addr; 01269 01270 /* Allocate a packet. */ 01271 lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0); 01272 p = pbuf_alloc(PBUF_IP, sizeof(struct na_header) + (lladdr_opt_len << 3), PBUF_RAM); 01273 if (p == NULL) { 01274 ND6_STATS_INC(nd6.memerr); 01275 return; 01276 } 01277 01278 /* Set fields. */ 01279 na_hdr = (struct na_header *)p->payload; 01280 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header)); 01281 01282 na_hdr->type = ICMP6_TYPE_NA; 01283 na_hdr->code = 0; 01284 na_hdr->chksum = 0; 01285 na_hdr->flags = flags & 0xf0; 01286 na_hdr->reserved[0] = 0; 01287 na_hdr->reserved[1] = 0; 01288 na_hdr->reserved[2] = 0; 01289 ip6_addr_copy_to_packed(na_hdr->target_address, *target_addr); 01290 01291 lladdr_opt->type = ND6_OPTION_TYPE_TARGET_LLADDR; 01292 lladdr_opt->length = (u8_t)lladdr_opt_len; 01293 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len); 01294 01295 /* Generate the solicited node address for the target address. */ 01296 if (flags & ND6_SEND_FLAG_MULTICAST_DEST) { 01297 ip6_addr_set_solicitednode(&multicast_address, target_addr->addr[3]); 01298 ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif); 01299 dest_addr = &multicast_address; 01300 } else if (flags & ND6_SEND_FLAG_ALLNODES_DEST) { 01301 ip6_addr_set_allnodes_linklocal(&multicast_address); 01302 ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif); 01303 dest_addr = &multicast_address; 01304 } else { 01305 dest_addr = ip6_current_src_addr(); 01306 } 01307 01308 #if CHECKSUM_GEN_ICMP6 01309 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { 01310 na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, 01311 dest_addr); 01312 } 01313 #endif /* CHECKSUM_GEN_ICMP6 */ 01314 01315 /* Send the packet out. */ 01316 ND6_STATS_INC(nd6.xmit); 01317 ip6_output_if(p, src_addr, dest_addr, 01318 ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); 01319 pbuf_free(p); 01320 } 01321 01322 #if LWIP_IPV6_SEND_ROUTER_SOLICIT 01323 /** 01324 * Send a router solicitation message 01325 * 01326 * @param netif the netif on which to send the message 01327 */ 01328 static err_t 01329 nd6_send_rs(struct netif *netif) 01330 { 01331 struct rs_header *rs_hdr; 01332 struct lladdr_option *lladdr_opt; 01333 struct pbuf *p; 01334 const ip6_addr_t *src_addr; 01335 err_t err; 01336 u16_t lladdr_opt_len = 0; 01337 01338 /* Link-local source address, or unspecified address? */ 01339 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) { 01340 src_addr = netif_ip6_addr(netif, 0); 01341 } else { 01342 src_addr = IP6_ADDR_ANY6; 01343 } 01344 01345 /* Generate the all routers target address. */ 01346 ip6_addr_set_allrouters_linklocal(&multicast_address); 01347 ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif); 01348 01349 /* Allocate a packet. */ 01350 if (src_addr != IP6_ADDR_ANY6 && netif->hwaddr_len) { 01351 lladdr_opt_len = ((netif->hwaddr_len + 2) >> 3) + (((netif->hwaddr_len + 2) & 0x07) ? 1 : 0); 01352 } 01353 p = pbuf_alloc(PBUF_IP, sizeof(struct rs_header) + (lladdr_opt_len << 3), PBUF_RAM); 01354 if (p == NULL) { 01355 ND6_STATS_INC(nd6.memerr); 01356 return ERR_BUF; 01357 } 01358 01359 /* Set fields. */ 01360 rs_hdr = (struct rs_header *)p->payload; 01361 01362 rs_hdr->type = ICMP6_TYPE_RS; 01363 rs_hdr->code = 0; 01364 rs_hdr->chksum = 0; 01365 rs_hdr->reserved = 0; 01366 01367 if (src_addr != IP6_ADDR_ANY6 && lladdr_opt_len) { 01368 /* Include our hw address. */ 01369 lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header)); 01370 lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR; 01371 lladdr_opt->length = (u8_t)lladdr_opt_len; 01372 SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len); 01373 } 01374 01375 #if CHECKSUM_GEN_ICMP6 01376 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { 01377 rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, 01378 &multicast_address); 01379 } 01380 #endif /* CHECKSUM_GEN_ICMP6 */ 01381 01382 /* Send the packet out. */ 01383 ND6_STATS_INC(nd6.xmit); 01384 01385 err = ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, &multicast_address, 01386 ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); 01387 pbuf_free(p); 01388 01389 return err; 01390 } 01391 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ 01392 01393 /** 01394 * Search for a neighbor cache entry 01395 * 01396 * @param ip6addr the IPv6 address of the neighbor 01397 * @return The neighbor cache entry index that matched, -1 if no 01398 * entry is found 01399 */ 01400 static s8_t 01401 nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr) 01402 { 01403 s8_t i; 01404 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01405 if (ip6_addr_cmp(ip6addr, &(neighbor_cache[i].next_hop_address))) { 01406 return i; 01407 } 01408 } 01409 return -1; 01410 } 01411 01412 /** 01413 * Create a new neighbor cache entry. 01414 * 01415 * If no unused entry is found, will try to recycle an old entry 01416 * according to ad-hoc "age" heuristic. 01417 * 01418 * @return The neighbor cache entry index that was created, -1 if no 01419 * entry could be created 01420 */ 01421 static s8_t 01422 nd6_new_neighbor_cache_entry(void) 01423 { 01424 s8_t i; 01425 s8_t j; 01426 u32_t time; 01427 01428 01429 /* First, try to find an empty entry. */ 01430 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01431 if (neighbor_cache[i].state == ND6_NO_ENTRY) { 01432 return i; 01433 } 01434 } 01435 01436 /* We need to recycle an entry. in general, do not recycle if it is a router. */ 01437 01438 /* Next, try to find a Stale entry. */ 01439 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01440 if ((neighbor_cache[i].state == ND6_STALE) && 01441 (!neighbor_cache[i].isrouter)) { 01442 nd6_free_neighbor_cache_entry(i); 01443 return i; 01444 } 01445 } 01446 01447 /* Next, try to find a Probe entry. */ 01448 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01449 if ((neighbor_cache[i].state == ND6_PROBE) && 01450 (!neighbor_cache[i].isrouter)) { 01451 nd6_free_neighbor_cache_entry(i); 01452 return i; 01453 } 01454 } 01455 01456 /* Next, try to find a Delayed entry. */ 01457 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01458 if ((neighbor_cache[i].state == ND6_DELAY) && 01459 (!neighbor_cache[i].isrouter)) { 01460 nd6_free_neighbor_cache_entry(i); 01461 return i; 01462 } 01463 } 01464 01465 /* Next, try to find the oldest reachable entry. */ 01466 time = 0xfffffffful; 01467 j = -1; 01468 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01469 if ((neighbor_cache[i].state == ND6_REACHABLE) && 01470 (!neighbor_cache[i].isrouter)) { 01471 if (neighbor_cache[i].counter.reachable_time < time) { 01472 j = i; 01473 time = neighbor_cache[i].counter.reachable_time; 01474 } 01475 } 01476 } 01477 if (j >= 0) { 01478 nd6_free_neighbor_cache_entry(j); 01479 return j; 01480 } 01481 01482 /* Next, find oldest incomplete entry without queued packets. */ 01483 time = 0; 01484 j = -1; 01485 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01486 if ( 01487 (neighbor_cache[i].q == NULL) && 01488 (neighbor_cache[i].state == ND6_INCOMPLETE) && 01489 (!neighbor_cache[i].isrouter)) { 01490 if (neighbor_cache[i].counter.probes_sent >= time) { 01491 j = i; 01492 time = neighbor_cache[i].counter.probes_sent; 01493 } 01494 } 01495 } 01496 if (j >= 0) { 01497 nd6_free_neighbor_cache_entry(j); 01498 return j; 01499 } 01500 01501 /* Next, find oldest incomplete entry with queued packets. */ 01502 time = 0; 01503 j = -1; 01504 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 01505 if ((neighbor_cache[i].state == ND6_INCOMPLETE) && 01506 (!neighbor_cache[i].isrouter)) { 01507 if (neighbor_cache[i].counter.probes_sent >= time) { 01508 j = i; 01509 time = neighbor_cache[i].counter.probes_sent; 01510 } 01511 } 01512 } 01513 if (j >= 0) { 01514 nd6_free_neighbor_cache_entry(j); 01515 return j; 01516 } 01517 01518 /* No more entries to try. */ 01519 return -1; 01520 } 01521 01522 /** 01523 * Will free any resources associated with a neighbor cache 01524 * entry, and will mark it as unused. 01525 * 01526 * @param i the neighbor cache entry index to free 01527 */ 01528 static void 01529 nd6_free_neighbor_cache_entry(s8_t i) 01530 { 01531 if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) { 01532 return; 01533 } 01534 if (neighbor_cache[i].isrouter) { 01535 /* isrouter needs to be cleared before deleting a neighbor cache entry */ 01536 return; 01537 } 01538 01539 /* Free any queued packets. */ 01540 if (neighbor_cache[i].q != NULL) { 01541 nd6_free_q(neighbor_cache[i].q); 01542 neighbor_cache[i].q = NULL; 01543 } 01544 01545 neighbor_cache[i].state = ND6_NO_ENTRY; 01546 neighbor_cache[i].isrouter = 0; 01547 neighbor_cache[i].netif = NULL; 01548 neighbor_cache[i].counter.reachable_time = 0; 01549 ip6_addr_set_zero(&(neighbor_cache[i].next_hop_address)); 01550 } 01551 01552 /** 01553 * Search for a destination cache entry 01554 * 01555 * @param ip6addr the IPv6 address of the destination 01556 * @return The destination cache entry index that matched, -1 if no 01557 * entry is found 01558 */ 01559 static s16_t 01560 nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr) 01561 { 01562 s16_t i; 01563 01564 IP6_ADDR_ZONECHECK(ip6addr); 01565 01566 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) { 01567 if (ip6_addr_cmp(ip6addr, &(destination_cache[i].destination_addr))) { 01568 return i; 01569 } 01570 } 01571 return -1; 01572 } 01573 01574 /** 01575 * Create a new destination cache entry. If no unused entry is found, 01576 * will recycle oldest entry. 01577 * 01578 * @return The destination cache entry index that was created, -1 if no 01579 * entry was created 01580 */ 01581 static s16_t 01582 nd6_new_destination_cache_entry(void) 01583 { 01584 s16_t i, j; 01585 u32_t age; 01586 01587 /* Find an empty entry. */ 01588 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) { 01589 if (ip6_addr_isany(&(destination_cache[i].destination_addr))) { 01590 return i; 01591 } 01592 } 01593 01594 /* Find oldest entry. */ 01595 age = 0; 01596 j = LWIP_ND6_NUM_DESTINATIONS - 1; 01597 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) { 01598 if (destination_cache[i].age > age) { 01599 j = i; 01600 } 01601 } 01602 01603 return j; 01604 } 01605 01606 /** 01607 * Clear the destination cache. 01608 * 01609 * This operation may be necessary for consistency in the light of changing 01610 * local addresses and/or use of the gateway hook. 01611 */ 01612 void 01613 nd6_clear_destination_cache(void) 01614 { 01615 int i; 01616 01617 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) { 01618 ip6_addr_set_any(&destination_cache[i].destination_addr); 01619 } 01620 } 01621 01622 /** 01623 * Determine whether an address matches an on-link prefix or the subnet of a 01624 * statically assigned address. 01625 * 01626 * @param ip6addr the IPv6 address to match 01627 * @return 1 if the address is on-link, 0 otherwise 01628 */ 01629 static int 01630 nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif) 01631 { 01632 s8_t i; 01633 01634 /* Check to see if the address matches an on-link prefix. */ 01635 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) { 01636 if ((prefix_list[i].netif == netif) && 01637 (prefix_list[i].invalidation_timer > 0) && 01638 ip6_addr_netcmp(ip6addr, &(prefix_list[i].prefix))) { 01639 return 1; 01640 } 01641 } 01642 /* Check to see if address prefix matches a manually configured (= static) 01643 * address. Static addresses have an implied /64 subnet assignment. Dynamic 01644 * addresses (from autoconfiguration) have no implied subnet assignment, and 01645 * are thus effectively /128 assignments. See RFC 5942 for more on this. */ 01646 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { 01647 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && 01648 netif_ip6_addr_isstatic(netif, i) && 01649 ip6_addr_netcmp(ip6addr, netif_ip6_addr(netif, i))) { 01650 return 1; 01651 } 01652 } 01653 return 0; 01654 } 01655 01656 /** 01657 * Select a default router for a destination. 01658 * 01659 * This function is used both for routing and for finding a next-hop target for 01660 * a packet. In the former case, the given netif is NULL, and the returned 01661 * router entry must be for a netif suitable for sending packets (up, link up). 01662 * In the latter case, the given netif is not NULL and restricts router choice. 01663 * 01664 * @param ip6addr the destination address 01665 * @param netif the netif for the outgoing packet, if known 01666 * @return the default router entry index, or -1 if no suitable 01667 * router is found 01668 */ 01669 static s8_t 01670 nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif) 01671 { 01672 struct netif *router_netif; 01673 s8_t i, j, valid_router; 01674 static s8_t last_router; 01675 01676 LWIP_UNUSED_ARG(ip6addr); /* @todo match preferred routes!! (must implement ND6_OPTION_TYPE_ROUTE_INFO) */ 01677 01678 /* @todo: implement default router preference */ 01679 01680 /* Look for valid routers. A reachable router is preferred. */ 01681 valid_router = -1; 01682 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) { 01683 /* Is the router netif both set and apppropriate? */ 01684 if (default_router_list[i].neighbor_entry != NULL) { 01685 router_netif = default_router_list[i].neighbor_entry->netif; 01686 if ((router_netif != NULL) && (netif != NULL ? netif == router_netif : 01687 (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) { 01688 /* Is the router valid, i.e., reachable or probably reachable as per 01689 * RFC 4861 Sec. 6.3.6? Note that we will never return a router that 01690 * has no neighbor cache entry, due to the netif association tests. */ 01691 if (default_router_list[i].neighbor_entry->state != ND6_INCOMPLETE) { 01692 /* Is the router known to be reachable? */ 01693 if (default_router_list[i].neighbor_entry->state == ND6_REACHABLE) { 01694 return i; /* valid and reachable - done! */ 01695 } else if (valid_router < 0) { 01696 valid_router = i; /* valid but not known to be reachable */ 01697 } 01698 } 01699 } 01700 } 01701 } 01702 if (valid_router >= 0) { 01703 return valid_router; 01704 } 01705 01706 /* Look for any router for which we have any information at all. */ 01707 /* last_router is used for round-robin selection of incomplete routers, as 01708 * recommended in RFC 4861 Sec. 6.3.6 point (2). Advance only when picking a 01709 * route, to select the same router as next-hop target in the common case. */ 01710 if ((netif == NULL) && (++last_router >= LWIP_ND6_NUM_ROUTERS)) { 01711 last_router = 0; 01712 } 01713 i = last_router; 01714 for (j = 0; j < LWIP_ND6_NUM_ROUTERS; j++) { 01715 if (default_router_list[i].neighbor_entry != NULL) { 01716 router_netif = default_router_list[i].neighbor_entry->netif; 01717 if ((router_netif != NULL) && (netif != NULL ? netif == router_netif : 01718 (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) { 01719 return i; 01720 } 01721 } 01722 if (++i >= LWIP_ND6_NUM_ROUTERS) { 01723 i = 0; 01724 } 01725 } 01726 01727 /* no suitable router found. */ 01728 return -1; 01729 } 01730 01731 /** 01732 * Find a router-announced route to the given destination. This route may be 01733 * based on an on-link prefix or a default router. 01734 * 01735 * If a suitable route is found, the returned netif is guaranteed to be in a 01736 * suitable state (up, link up) to be used for packet transmission. 01737 * 01738 * @param ip6addr the destination IPv6 address 01739 * @return the netif to use for the destination, or NULL if none found 01740 */ 01741 struct netif * 01742 nd6_find_route(const ip6_addr_t *ip6addr) 01743 { 01744 struct netif *netif; 01745 s8_t i; 01746 01747 /* @todo decide if it makes sense to check the destination cache first */ 01748 01749 /* Check if there is a matching on-link prefix. There may be multiple 01750 * matches. Pick the first one that is associated with a suitable netif. */ 01751 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) { 01752 netif = prefix_list[i].netif; 01753 if ((netif != NULL) && ip6_addr_netcmp(&prefix_list[i].prefix, ip6addr) && 01754 netif_is_up(netif) && netif_is_link_up(netif)) { 01755 return netif; 01756 } 01757 } 01758 01759 /* No on-link prefix match. Find a router that can forward the packet. */ 01760 i = nd6_select_router(ip6addr, NULL); 01761 if (i >= 0) { 01762 LWIP_ASSERT("selected router must have a neighbor entry", 01763 default_router_list[i].neighbor_entry != NULL); 01764 return default_router_list[i].neighbor_entry->netif; 01765 } 01766 01767 return NULL; 01768 } 01769 01770 /** 01771 * Find an entry for a default router. 01772 * 01773 * @param router_addr the IPv6 address of the router 01774 * @param netif the netif on which the router is found, if known 01775 * @return the index of the router entry, or -1 if not found 01776 */ 01777 static s8_t 01778 nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif) 01779 { 01780 s8_t i; 01781 01782 IP6_ADDR_ZONECHECK_NETIF(router_addr, netif); 01783 01784 /* Look for router. */ 01785 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) { 01786 if ((default_router_list[i].neighbor_entry != NULL) && 01787 ((netif != NULL) ? netif == default_router_list[i].neighbor_entry->netif : 1) && 01788 ip6_addr_cmp(router_addr, &(default_router_list[i].neighbor_entry->next_hop_address))) { 01789 return i; 01790 } 01791 } 01792 01793 /* router not found. */ 01794 return -1; 01795 } 01796 01797 /** 01798 * Create a new entry for a default router. 01799 * 01800 * @param router_addr the IPv6 address of the router 01801 * @param netif the netif on which the router is connected, if known 01802 * @return the index on the router table, or -1 if could not be created 01803 */ 01804 static s8_t 01805 nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif) 01806 { 01807 s8_t router_index; 01808 s8_t free_router_index; 01809 s8_t neighbor_index; 01810 01811 IP6_ADDR_ZONECHECK_NETIF(router_addr, netif); 01812 01813 /* Do we have a neighbor entry for this router? */ 01814 neighbor_index = nd6_find_neighbor_cache_entry(router_addr); 01815 if (neighbor_index < 0) { 01816 /* Create a neighbor entry for this router. */ 01817 neighbor_index = nd6_new_neighbor_cache_entry(); 01818 if (neighbor_index < 0) { 01819 /* Could not create neighbor entry for this router. */ 01820 return -1; 01821 } 01822 ip6_addr_set(&(neighbor_cache[neighbor_index].next_hop_address), router_addr); 01823 neighbor_cache[neighbor_index].netif = netif; 01824 neighbor_cache[neighbor_index].q = NULL; 01825 if (netif->hwaddr_len) { 01826 neighbor_cache[neighbor_index].state = ND6_INCOMPLETE; 01827 neighbor_cache[neighbor_index].counter.probes_sent = 1; 01828 nd6_send_neighbor_cache_probe(&neighbor_cache[neighbor_index], ND6_SEND_FLAG_MULTICAST_DEST); 01829 } else { 01830 neighbor_cache[neighbor_index].state = ND6_STALE; 01831 } 01832 } 01833 01834 /* Mark neighbor as router. */ 01835 neighbor_cache[neighbor_index].isrouter = 1; 01836 01837 /* Look for empty entry. */ 01838 free_router_index = LWIP_ND6_NUM_ROUTERS; 01839 for (router_index = LWIP_ND6_NUM_ROUTERS - 1; router_index >= 0; router_index--) { 01840 /* check if router already exists (this is a special case for 2 netifs on the same subnet 01841 - e.g. wifi and cable) */ 01842 if(default_router_list[router_index].neighbor_entry == &(neighbor_cache[neighbor_index])){ 01843 return router_index; 01844 } 01845 if (default_router_list[router_index].neighbor_entry == NULL) { 01846 /* remember lowest free index to create a new entry */ 01847 free_router_index = router_index; 01848 } 01849 } 01850 if (free_router_index < LWIP_ND6_NUM_ROUTERS) { 01851 default_router_list[free_router_index].neighbor_entry = &(neighbor_cache[neighbor_index]); 01852 return free_router_index; 01853 } 01854 01855 /* Could not create a router entry. */ 01856 01857 /* Mark neighbor entry as not-router. Entry might be useful as neighbor still. */ 01858 neighbor_cache[neighbor_index].isrouter = 0; 01859 01860 /* router not found. */ 01861 return -1; 01862 } 01863 01864 /** 01865 * Find the cached entry for an on-link prefix. 01866 * 01867 * @param prefix the IPv6 prefix that is on-link 01868 * @param netif the netif on which the prefix is on-link 01869 * @return the index on the prefix table, or -1 if not found 01870 */ 01871 static s8_t 01872 nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif) 01873 { 01874 s8_t i; 01875 01876 /* Look for prefix in list. */ 01877 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) { 01878 if ((ip6_addr_netcmp(&(prefix_list[i].prefix), prefix)) && 01879 (prefix_list[i].netif == netif)) { 01880 return i; 01881 } 01882 } 01883 01884 /* Entry not available. */ 01885 return -1; 01886 } 01887 01888 /** 01889 * Creates a new entry for an on-link prefix. 01890 * 01891 * @param prefix the IPv6 prefix that is on-link 01892 * @param netif the netif on which the prefix is on-link 01893 * @return the index on the prefix table, or -1 if not created 01894 */ 01895 static s8_t 01896 nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif) 01897 { 01898 s8_t i; 01899 01900 /* Create new entry. */ 01901 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) { 01902 if ((prefix_list[i].netif == NULL) || 01903 (prefix_list[i].invalidation_timer == 0)) { 01904 /* Found empty prefix entry. */ 01905 prefix_list[i].netif = netif; 01906 ip6_addr_set(&(prefix_list[i].prefix), prefix); 01907 return i; 01908 } 01909 } 01910 01911 /* Entry not available. */ 01912 return -1; 01913 } 01914 01915 /** 01916 * Determine the next hop for a destination. Will determine if the 01917 * destination is on-link, else a suitable on-link router is selected. 01918 * 01919 * The last entry index is cached for fast entry search. 01920 * 01921 * @param ip6addr the destination address 01922 * @param netif the netif on which the packet will be sent 01923 * @return the neighbor cache entry for the next hop, ERR_RTE if no 01924 * suitable next hop was found, ERR_MEM if no cache entry 01925 * could be created 01926 */ 01927 static s8_t 01928 nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif) 01929 { 01930 #ifdef LWIP_HOOK_ND6_GET_GW 01931 const ip6_addr_t *next_hop_addr; 01932 #endif /* LWIP_HOOK_ND6_GET_GW */ 01933 s8_t i; 01934 s16_t dst_idx; 01935 01936 IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif); 01937 01938 #if LWIP_NETIF_HWADDRHINT 01939 if (netif->hints != NULL) { 01940 /* per-pcb cached entry was given */ 01941 netif_addr_idx_t addr_hint = netif->hints->addr_hint; 01942 if (addr_hint < LWIP_ND6_NUM_DESTINATIONS) { 01943 nd6_cached_destination_index = addr_hint; 01944 } 01945 } 01946 #endif /* LWIP_NETIF_HWADDRHINT */ 01947 01948 /* Look for ip6addr in destination cache. */ 01949 if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) { 01950 /* the cached entry index is the right one! */ 01951 /* do nothing. */ 01952 ND6_STATS_INC(nd6.cachehit); 01953 } else { 01954 /* Search destination cache. */ 01955 dst_idx = nd6_find_destination_cache_entry(ip6addr); 01956 if (dst_idx >= 0) { 01957 /* found destination entry. make it our new cached index. */ 01958 LWIP_ASSERT("type overflow", (size_t)dst_idx < NETIF_ADDR_IDX_MAX); 01959 nd6_cached_destination_index = (netif_addr_idx_t)dst_idx; 01960 } else { 01961 /* Not found. Create a new destination entry. */ 01962 dst_idx = nd6_new_destination_cache_entry(); 01963 if (dst_idx >= 0) { 01964 /* got new destination entry. make it our new cached index. */ 01965 LWIP_ASSERT("type overflow", (size_t)dst_idx < NETIF_ADDR_IDX_MAX); 01966 nd6_cached_destination_index = (netif_addr_idx_t)dst_idx; 01967 } else { 01968 /* Could not create a destination cache entry. */ 01969 return ERR_MEM; 01970 } 01971 01972 /* Copy dest address to destination cache. */ 01973 ip6_addr_set(&(destination_cache[nd6_cached_destination_index].destination_addr), ip6addr); 01974 01975 /* Now find the next hop. is it a neighbor? */ 01976 if (ip6_addr_islinklocal(ip6addr) || 01977 nd6_is_prefix_in_netif(ip6addr, netif)) { 01978 /* Destination in local link. */ 01979 destination_cache[nd6_cached_destination_index].pmtu = netif_mtu6(netif); 01980 ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, destination_cache[nd6_cached_destination_index].destination_addr); 01981 #ifdef LWIP_HOOK_ND6_GET_GW 01982 } else if ((next_hop_addr = LWIP_HOOK_ND6_GET_GW(netif, ip6addr)) != NULL) { 01983 /* Next hop for destination provided by hook function. */ 01984 destination_cache[nd6_cached_destination_index].pmtu = netif->mtu; 01985 ip6_addr_set(&destination_cache[nd6_cached_destination_index].next_hop_addr, next_hop_addr); 01986 #endif /* LWIP_HOOK_ND6_GET_GW */ 01987 } else { 01988 /* We need to select a router. */ 01989 i = nd6_select_router(ip6addr, netif); 01990 if (i < 0) { 01991 /* No router found. */ 01992 ip6_addr_set_any(&(destination_cache[nd6_cached_destination_index].destination_addr)); 01993 return ERR_RTE; 01994 } 01995 destination_cache[nd6_cached_destination_index].pmtu = netif_mtu6(netif); /* Start with netif mtu, correct through ICMPv6 if necessary */ 01996 ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, default_router_list[i].neighbor_entry->next_hop_address); 01997 } 01998 } 01999 } 02000 02001 #if LWIP_NETIF_HWADDRHINT 02002 if (netif->hints != NULL) { 02003 /* per-pcb cached entry was given */ 02004 netif->hints->addr_hint = nd6_cached_destination_index; 02005 } 02006 #endif /* LWIP_NETIF_HWADDRHINT */ 02007 02008 /* Look in neighbor cache for the next-hop address. */ 02009 if (ip6_addr_cmp(&(destination_cache[nd6_cached_destination_index].next_hop_addr), 02010 &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) { 02011 /* Cache hit. */ 02012 /* Do nothing. */ 02013 ND6_STATS_INC(nd6.cachehit); 02014 } else { 02015 i = nd6_find_neighbor_cache_entry(&(destination_cache[nd6_cached_destination_index].next_hop_addr)); 02016 if (i >= 0) { 02017 /* Found a matching record, make it new cached entry. */ 02018 nd6_cached_neighbor_index = i; 02019 } else { 02020 /* Neighbor not in cache. Make a new entry. */ 02021 i = nd6_new_neighbor_cache_entry(); 02022 if (i >= 0) { 02023 /* got new neighbor entry. make it our new cached index. */ 02024 nd6_cached_neighbor_index = i; 02025 } else { 02026 /* Could not create a neighbor cache entry. */ 02027 return ERR_MEM; 02028 } 02029 02030 /* Initialize fields. */ 02031 ip6_addr_copy(neighbor_cache[i].next_hop_address, 02032 destination_cache[nd6_cached_destination_index].next_hop_addr); 02033 neighbor_cache[i].isrouter = 0; 02034 neighbor_cache[i].netif = netif; 02035 neighbor_cache[i].state = ND6_INCOMPLETE; 02036 neighbor_cache[i].counter.probes_sent = 1; 02037 nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST); 02038 } 02039 } 02040 02041 /* Reset this destination's age. */ 02042 destination_cache[nd6_cached_destination_index].age = 0; 02043 02044 return nd6_cached_neighbor_index; 02045 } 02046 02047 /** 02048 * Queue a packet for a neighbor. 02049 * 02050 * @param neighbor_index the index in the neighbor cache table 02051 * @param q packet to be queued 02052 * @return ERR_OK if succeeded, ERR_MEM if out of memory 02053 */ 02054 static err_t 02055 nd6_queue_packet(s8_t neighbor_index, struct pbuf *q) 02056 { 02057 err_t result = ERR_MEM; 02058 struct pbuf *p; 02059 int copy_needed = 0; 02060 #if LWIP_ND6_QUEUEING 02061 struct nd6_q_entry *new_entry, *r; 02062 #endif /* LWIP_ND6_QUEUEING */ 02063 02064 if ((neighbor_index < 0) || (neighbor_index >= LWIP_ND6_NUM_NEIGHBORS)) { 02065 return ERR_ARG; 02066 } 02067 02068 /* IF q includes a pbuf that must be copied, we have to copy the whole chain 02069 * into a new PBUF_RAM. See the definition of PBUF_NEEDS_COPY for details. */ 02070 p = q; 02071 while (p) { 02072 if (PBUF_NEEDS_COPY(p)) { 02073 copy_needed = 1; 02074 break; 02075 } 02076 p = p->next; 02077 } 02078 if (copy_needed) { 02079 /* copy the whole packet into new pbufs */ 02080 p = pbuf_clone(PBUF_LINK, PBUF_RAM, q); 02081 while ((p == NULL) && (neighbor_cache[neighbor_index].q != NULL)) { 02082 /* Free oldest packet (as per RFC recommendation) */ 02083 #if LWIP_ND6_QUEUEING 02084 r = neighbor_cache[neighbor_index].q; 02085 neighbor_cache[neighbor_index].q = r->next; 02086 r->next = NULL; 02087 nd6_free_q(r); 02088 #else /* LWIP_ND6_QUEUEING */ 02089 pbuf_free(neighbor_cache[neighbor_index].q); 02090 neighbor_cache[neighbor_index].q = NULL; 02091 #endif /* LWIP_ND6_QUEUEING */ 02092 p = pbuf_clone(PBUF_LINK, PBUF_RAM, q); 02093 } 02094 } else { 02095 /* referencing the old pbuf is enough */ 02096 p = q; 02097 pbuf_ref(p); 02098 } 02099 /* packet was copied/ref'd? */ 02100 if (p != NULL) { 02101 /* queue packet ... */ 02102 #if LWIP_ND6_QUEUEING 02103 /* allocate a new nd6 queue entry */ 02104 new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE); 02105 if ((new_entry == NULL) && (neighbor_cache[neighbor_index].q != NULL)) { 02106 /* Free oldest packet (as per RFC recommendation) */ 02107 r = neighbor_cache[neighbor_index].q; 02108 neighbor_cache[neighbor_index].q = r->next; 02109 r->next = NULL; 02110 nd6_free_q(r); 02111 new_entry = (struct nd6_q_entry *)memp_malloc(MEMP_ND6_QUEUE); 02112 } 02113 if (new_entry != NULL) { 02114 new_entry->next = NULL; 02115 new_entry->p = p; 02116 if (neighbor_cache[neighbor_index].q != NULL) { 02117 /* queue was already existent, append the new entry to the end */ 02118 r = neighbor_cache[neighbor_index].q; 02119 while (r->next != NULL) { 02120 r = r->next; 02121 } 02122 r->next = new_entry; 02123 } else { 02124 /* queue did not exist, first item in queue */ 02125 neighbor_cache[neighbor_index].q = new_entry; 02126 } 02127 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index)); 02128 result = ERR_OK; 02129 } else { 02130 /* the pool MEMP_ND6_QUEUE is empty */ 02131 pbuf_free(p); 02132 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)p)); 02133 /* { result == ERR_MEM } through initialization */ 02134 } 02135 #else /* LWIP_ND6_QUEUEING */ 02136 /* Queue a single packet. If an older packet is already queued, free it as per RFC. */ 02137 if (neighbor_cache[neighbor_index].q != NULL) { 02138 pbuf_free(neighbor_cache[neighbor_index].q); 02139 } 02140 neighbor_cache[neighbor_index].q = p; 02141 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index)); 02142 result = ERR_OK; 02143 #endif /* LWIP_ND6_QUEUEING */ 02144 } else { 02145 LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)q)); 02146 /* { result == ERR_MEM } through initialization */ 02147 } 02148 02149 return result; 02150 } 02151 02152 #if LWIP_ND6_QUEUEING 02153 /** 02154 * Free a complete queue of nd6 q entries 02155 * 02156 * @param q a queue of nd6_q_entry to free 02157 */ 02158 static void 02159 nd6_free_q(struct nd6_q_entry *q) 02160 { 02161 struct nd6_q_entry *r; 02162 LWIP_ASSERT("q != NULL", q != NULL); 02163 LWIP_ASSERT("q->p != NULL", q->p != NULL); 02164 while (q) { 02165 r = q; 02166 q = q->next; 02167 LWIP_ASSERT("r->p != NULL", (r->p != NULL)); 02168 pbuf_free(r->p); 02169 memp_free(MEMP_ND6_QUEUE, r); 02170 } 02171 } 02172 #endif /* LWIP_ND6_QUEUEING */ 02173 02174 /** 02175 * Send queued packets for a neighbor 02176 * 02177 * @param i the neighbor to send packets to 02178 */ 02179 static void 02180 nd6_send_q(s8_t i) 02181 { 02182 struct ip6_hdr *ip6hdr; 02183 ip6_addr_t dest; 02184 #if LWIP_ND6_QUEUEING 02185 struct nd6_q_entry *q; 02186 #endif /* LWIP_ND6_QUEUEING */ 02187 02188 if ((i < 0) || (i >= LWIP_ND6_NUM_NEIGHBORS)) { 02189 return; 02190 } 02191 02192 #if LWIP_ND6_QUEUEING 02193 while (neighbor_cache[i].q != NULL) { 02194 /* remember first in queue */ 02195 q = neighbor_cache[i].q; 02196 /* pop first item off the queue */ 02197 neighbor_cache[i].q = q->next; 02198 /* Get ipv6 header. */ 02199 ip6hdr = (struct ip6_hdr *)(q->p->payload); 02200 /* Create an aligned copy. */ 02201 ip6_addr_copy_from_packed(dest, ip6hdr->dest); 02202 /* Restore the zone, if applicable. */ 02203 ip6_addr_assign_zone(&dest, IP6_UNKNOWN, neighbor_cache[i].netif); 02204 /* send the queued IPv6 packet */ 02205 (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, q->p, &dest); 02206 /* free the queued IP packet */ 02207 pbuf_free(q->p); 02208 /* now queue entry can be freed */ 02209 memp_free(MEMP_ND6_QUEUE, q); 02210 } 02211 #else /* LWIP_ND6_QUEUEING */ 02212 if (neighbor_cache[i].q != NULL) { 02213 /* Get ipv6 header. */ 02214 ip6hdr = (struct ip6_hdr *)(neighbor_cache[i].q->payload); 02215 /* Create an aligned copy. */ 02216 ip6_addr_copy_from_packed(dest, ip6hdr->dest); 02217 /* Restore the zone, if applicable. */ 02218 ip6_addr_assign_zone(&dest, IP6_UNKNOWN, neighbor_cache[i].netif); 02219 /* send the queued IPv6 packet */ 02220 (neighbor_cache[i].netif)->output_ip6(neighbor_cache[i].netif, neighbor_cache[i].q, &dest); 02221 /* free the queued IP packet */ 02222 pbuf_free(neighbor_cache[i].q); 02223 neighbor_cache[i].q = NULL; 02224 } 02225 #endif /* LWIP_ND6_QUEUEING */ 02226 } 02227 02228 /** 02229 * A packet is to be transmitted to a specific IPv6 destination on a specific 02230 * interface. Check if we can find the hardware address of the next hop to use 02231 * for the packet. If so, give the hardware address to the caller, which should 02232 * use it to send the packet right away. Otherwise, enqueue the packet for 02233 * later transmission while looking up the hardware address, if possible. 02234 * 02235 * As such, this function returns one of three different possible results: 02236 * 02237 * - ERR_OK with a non-NULL 'hwaddrp': the caller should send the packet now. 02238 * - ERR_OK with a NULL 'hwaddrp': the packet has been enqueued for later. 02239 * - not ERR_OK: something went wrong; forward the error upward in the stack. 02240 * 02241 * @param netif The lwIP network interface on which the IP packet will be sent. 02242 * @param q The pbuf(s) containing the IP packet to be sent. 02243 * @param ip6addr The destination IPv6 address of the packet. 02244 * @param hwaddrp On success, filled with a pointer to a HW address or NULL (meaning 02245 * the packet has been queued). 02246 * @return 02247 * - ERR_OK on success, ERR_RTE if no route was found for the packet, 02248 * or ERR_MEM if low memory conditions prohibit sending the packet at all. 02249 */ 02250 err_t 02251 nd6_get_next_hop_addr_or_queue(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr, const u8_t **hwaddrp) 02252 { 02253 s8_t i; 02254 02255 /* Get next hop record. */ 02256 i = nd6_get_next_hop_entry(ip6addr, netif); 02257 if (i < 0) { 02258 /* failed to get a next hop neighbor record. */ 02259 return i; 02260 } 02261 02262 /* Now that we have a destination record, send or queue the packet. */ 02263 if (neighbor_cache[i].state == ND6_STALE) { 02264 /* Switch to delay state. */ 02265 neighbor_cache[i].state = ND6_DELAY; 02266 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; 02267 } 02268 /* @todo should we send or queue if PROBE? send for now, to let unicast NS pass. */ 02269 if ((neighbor_cache[i].state == ND6_REACHABLE) || 02270 (neighbor_cache[i].state == ND6_DELAY) || 02271 (neighbor_cache[i].state == ND6_PROBE)) { 02272 02273 /* Tell the caller to send out the packet now. */ 02274 *hwaddrp = neighbor_cache[i].lladdr; 02275 return ERR_OK; 02276 } 02277 02278 /* We should queue packet on this interface. */ 02279 *hwaddrp = NULL; 02280 return nd6_queue_packet(i, q); 02281 } 02282 02283 02284 /** 02285 * Get the Path MTU for a destination. 02286 * 02287 * @param ip6addr the destination address 02288 * @param netif the netif on which the packet will be sent 02289 * @return the Path MTU, if known, or the netif default MTU 02290 */ 02291 u16_t 02292 nd6_get_destination_mtu(const ip6_addr_t *ip6addr, struct netif *netif) 02293 { 02294 s16_t i; 02295 02296 i = nd6_find_destination_cache_entry(ip6addr); 02297 if (i >= 0) { 02298 if (destination_cache[i].pmtu > 0) { 02299 return destination_cache[i].pmtu; 02300 } 02301 } 02302 02303 if (netif != NULL) { 02304 return netif_mtu6(netif); 02305 } 02306 02307 return 1280; /* Minimum MTU */ 02308 } 02309 02310 02311 #if LWIP_ND6_TCP_REACHABILITY_HINTS 02312 /** 02313 * Provide the Neighbor discovery process with a hint that a 02314 * destination is reachable. Called by tcp_receive when ACKs are 02315 * received or sent (as per RFC). This is useful to avoid sending 02316 * NS messages every 30 seconds. 02317 * 02318 * @param ip6addr the destination address which is know to be reachable 02319 * by an upper layer protocol (TCP) 02320 */ 02321 void 02322 nd6_reachability_hint(const ip6_addr_t *ip6addr) 02323 { 02324 s8_t i; 02325 s16_t dst_idx; 02326 02327 /* Find destination in cache. */ 02328 if (ip6_addr_cmp(ip6addr, &(destination_cache[nd6_cached_destination_index].destination_addr))) { 02329 dst_idx = nd6_cached_destination_index; 02330 ND6_STATS_INC(nd6.cachehit); 02331 } else { 02332 dst_idx = nd6_find_destination_cache_entry(ip6addr); 02333 } 02334 if (dst_idx < 0) { 02335 return; 02336 } 02337 02338 /* Find next hop neighbor in cache. */ 02339 if (ip6_addr_cmp(&(destination_cache[dst_idx].next_hop_addr), &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) { 02340 i = nd6_cached_neighbor_index; 02341 ND6_STATS_INC(nd6.cachehit); 02342 } else { 02343 i = nd6_find_neighbor_cache_entry(&(destination_cache[dst_idx].next_hop_addr)); 02344 } 02345 if (i < 0) { 02346 return; 02347 } 02348 02349 /* For safety: don't set as reachable if we don't have a LL address yet. Misuse protection. */ 02350 if (neighbor_cache[i].state == ND6_INCOMPLETE || neighbor_cache[i].state == ND6_NO_ENTRY) { 02351 return; 02352 } 02353 02354 /* Set reachability state. */ 02355 neighbor_cache[i].state = ND6_REACHABLE; 02356 neighbor_cache[i].counter.reachable_time = reachable_time; 02357 } 02358 #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */ 02359 02360 /** 02361 * Remove all prefix, neighbor_cache and router entries of the specified netif. 02362 * 02363 * @param netif points to a network interface 02364 */ 02365 void 02366 nd6_cleanup_netif(struct netif *netif) 02367 { 02368 u8_t i; 02369 s8_t router_index; 02370 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) { 02371 if (prefix_list[i].netif == netif) { 02372 prefix_list[i].netif = NULL; 02373 } 02374 } 02375 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { 02376 if (neighbor_cache[i].netif == netif) { 02377 for (router_index = 0; router_index < LWIP_ND6_NUM_ROUTERS; router_index++) { 02378 if (default_router_list[router_index].neighbor_entry == &neighbor_cache[i]) { 02379 default_router_list[router_index].neighbor_entry = NULL; 02380 default_router_list[router_index].flags = 0; 02381 } 02382 } 02383 neighbor_cache[i].isrouter = 0; 02384 nd6_free_neighbor_cache_entry(i); 02385 } 02386 } 02387 /* Clear the destination cache, since many entries may now have become 02388 * invalid for one of several reasons. As destination cache entries have no 02389 * netif association, use a sledgehammer approach (this can be improved). */ 02390 nd6_clear_destination_cache(); 02391 } 02392 02393 #if LWIP_IPV6_MLD 02394 /** 02395 * The state of a local IPv6 address entry is about to change. If needed, join 02396 * or leave the solicited-node multicast group for the address. 02397 * 02398 * @param netif The netif that owns the address. 02399 * @param addr_idx The index of the address. 02400 * @param new_state The new (IP6_ADDR_) state for the address. 02401 */ 02402 void 02403 nd6_adjust_mld_membership(struct netif *netif, s8_t addr_idx, u8_t new_state) 02404 { 02405 u8_t old_state, old_member, new_member; 02406 02407 old_state = netif_ip6_addr_state(netif, addr_idx); 02408 02409 /* Determine whether we were, and should be, a member of the solicited-node 02410 * multicast group for this address. For tentative addresses, the group is 02411 * not joined until the address enters the TENTATIVE_1 (or VALID) state. */ 02412 old_member = (old_state != IP6_ADDR_INVALID && old_state != IP6_ADDR_DUPLICATED && old_state != IP6_ADDR_TENTATIVE); 02413 new_member = (new_state != IP6_ADDR_INVALID && new_state != IP6_ADDR_DUPLICATED && new_state != IP6_ADDR_TENTATIVE); 02414 02415 if (old_member != new_member) { 02416 ip6_addr_set_solicitednode(&multicast_address, netif_ip6_addr(netif, addr_idx)->addr[3]); 02417 ip6_addr_assign_zone(&multicast_address, IP6_MULTICAST, netif); 02418 02419 if (new_member) { 02420 mld6_joingroup_netif(netif, &multicast_address); 02421 } else { 02422 mld6_leavegroup_netif(netif, &multicast_address); 02423 } 02424 } 02425 } 02426 #endif /* LWIP_IPV6_MLD */ 02427 02428 /** Netif was added, set up, or reconnected (link up) */ 02429 void 02430 nd6_restart_netif(struct netif *netif) 02431 { 02432 #if LWIP_IPV6_SEND_ROUTER_SOLICIT 02433 /* Send Router Solicitation messages (see RFC 4861, ch. 6.3.7). */ 02434 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT; 02435 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ 02436 } 02437 02438 #endif /* LWIP_IPV6 */
Generated on Tue Jul 12 2022 13:54:29 by
