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