1 /**
2 * @file
3 * lwIP network interface abstraction
4 *
5 * @defgroup netif Network interface (NETIF)
6 * @ingroup callbackstyle_api
7 *
8 * @defgroup netif_ip4 IPv4 address handling
9 * @ingroup netif
10 *
11 * @defgroup netif_ip6 IPv6 address handling
12 * @ingroup netif
13 *
14 * @defgroup netif_cd Client data handling
15 * Store data (void*) on a netif for application usage.
16 * @see @ref LWIP_NUM_NETIF_CLIENT_DATA
17 * @ingroup netif
18 */
19
20 /*
21 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without modification,
25 * are permitted provided that the following conditions are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright notice,
28 * this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright notice,
30 * this list of conditions and the following disclaimer in the documentation
31 * and/or other materials provided with the distribution.
32 * 3. The name of the author may not be used to endorse or promote products
33 * derived from this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
38 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
44 * OF SUCH DAMAGE.
45 *
46 * This file is part of the lwIP TCP/IP stack.
47 *
48 * Author: Adam Dunkels <adam@sics.se>
49 */
50
51 #include "lwip/opt.h"
52
53 #include <string.h>
54
55 #include "lwip/def.h"
56 #include "lwip/ip_addr.h"
57 #include "lwip/ip6_addr.h"
58 #include "lwip/netif.h"
59 #include "lwip/priv/tcp_priv.h"
60 #include "lwip/udp.h"
61 #include "lwip/raw.h"
62 #include "lwip/snmp.h"
63 #include "lwip/igmp.h"
64 #include "lwip/etharp.h"
65 #include "lwip/stats.h"
66 #include "lwip/sys.h"
67 #include "lwip/ip.h"
68 #if ENABLE_LOOPBACK
69 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
70 #include "lwip/tcpip.h"
71 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
72 #endif /* ENABLE_LOOPBACK */
73
74 #include "netif/ethernet.h"
75
76 #if LWIP_AUTOIP
77 #include "lwip/autoip.h"
78 #endif /* LWIP_AUTOIP */
79 #if LWIP_DHCP
80 #include "lwip/dhcp.h"
81 #endif /* LWIP_DHCP */
82 #if LWIP_IPV6_DHCP6
83 #include "lwip/dhcp6.h"
84 #endif /* LWIP_IPV6_DHCP6 */
85 #if LWIP_IPV6_MLD
86 #include "lwip/mld6.h"
87 #endif /* LWIP_IPV6_MLD */
88 #if LWIP_IPV6
89 #include "lwip/nd6.h"
90 #endif
91
92 #include <rtthread.h>
93
94 #ifdef RT_USING_NETDEV
95 #include "lwip/netdb.h"
96 #include <netdev.h>
97 #endif /* RT_USING_NETDEV */
98
99 #if LWIP_NETIF_STATUS_CALLBACK
100 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
101 #else
102 #define NETIF_STATUS_CALLBACK(n)
103 #endif /* LWIP_NETIF_STATUS_CALLBACK */
104
105 #if LWIP_NETIF_LINK_CALLBACK
106 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
107 #else
108 #define NETIF_LINK_CALLBACK(n)
109 #endif /* LWIP_NETIF_LINK_CALLBACK */
110
111 struct netif *netif_list;
112 struct netif *netif_default;
113
114 static u8_t netif_num;
115
116 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
117 static u8_t netif_client_id;
118 #endif
119
120 #define NETIF_REPORT_TYPE_IPV4 0x01
121 #define NETIF_REPORT_TYPE_IPV6 0x02
122 static void netif_issue_reports(struct netif* netif, u8_t report_type);
123
124 #if LWIP_IPV6
125 static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
126 #endif /* LWIP_IPV6 */
127
128 #if LWIP_HAVE_LOOPIF
129 #if LWIP_IPV4
130 static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr);
131 #endif
132 #if LWIP_IPV6
133 static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr);
134 #endif
135
136
137 static struct netif loop_netif;
138
139 /**
140 * Initialize a lwip network interface structure for a loopback interface
141 *
142 * @param netif the lwip network interface structure for this loopif
143 * @return ERR_OK if the loopif is initialized
144 * ERR_MEM if private data couldn't be allocated
145 */
146 static err_t
netif_loopif_init(struct netif * netif)147 netif_loopif_init(struct netif *netif)
148 {
149 /* initialize the snmp variables and counters inside the struct netif
150 * ifSpeed: no assumption can be made!
151 */
152 MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
153
154 netif->name[0] = 'l';
155 netif->name[1] = 'o';
156 #if LWIP_IPV4
157 netif->output = netif_loop_output_ipv4;
158 #endif
159 #if LWIP_IPV6
160 netif->output_ip6 = netif_loop_output_ipv6;
161 #endif
162 #if LWIP_LOOPIF_MULTICAST
163 netif->flags |= NETIF_FLAG_IGMP;
164 #endif
165 return ERR_OK;
166 }
167 #endif /* LWIP_HAVE_LOOPIF */
168
169 void
netif_init(void)170 netif_init(void)
171 {
172 #if LWIP_HAVE_LOOPIF
173 #if LWIP_IPV4
174 #define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw,
175 ip4_addr_t loop_ipaddr, loop_netmask, loop_gw;
176 IP4_ADDR(&loop_gw, 127,0,0,1);
177 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
178 IP4_ADDR(&loop_netmask, 255,0,0,0);
179 #else /* LWIP_IPV4 */
180 #define LOOPIF_ADDRINIT
181 #endif /* LWIP_IPV4 */
182
183 #if NO_SYS
184 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
185 #else /* NO_SYS */
186 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
187 #endif /* NO_SYS */
188
189 #if LWIP_IPV6
190 IP_ADDR6_HOST(loop_netif.ip6_addr, 0, 0, 0, 0x00000001UL);
191 loop_netif.ip6_addr_state[0] = IP6_ADDR_VALID;
192 #endif /* LWIP_IPV6 */
193
194 netif_set_link_up(&loop_netif);
195 netif_set_up(&loop_netif);
196
197 #endif /* LWIP_HAVE_LOOPIF */
198 }
199
200 /**
201 * @ingroup lwip_nosys
202 * Forwards a received packet for input processing with
203 * ethernet_input() or ip_input() depending on netif flags.
204 * Don't call directly, pass to netif_add() and call
205 * netif->input().
206 * Only works if the netif driver correctly sets
207 * NETIF_FLAG_ETHARP and/or NETIF_FLAG_ETHERNET flag!
208 */
209 err_t
netif_input(struct pbuf * p,struct netif * inp)210 netif_input(struct pbuf *p, struct netif *inp)
211 {
212 #if LWIP_ETHERNET
213 if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
214 return ethernet_input(p, inp);
215 } else
216 #endif /* LWIP_ETHERNET */
217 return ip_input(p, inp);
218 }
219
220 /**
221 * @ingroup netif
222 * Add a network interface to the list of lwIP netifs.
223 *
224 * @param netif a pre-allocated netif structure
225 * @param ipaddr IP address for the new netif
226 * @param netmask network mask for the new netif
227 * @param gw default gateway IP address for the new netif
228 * @param state opaque data passed to the new netif
229 * @param init callback function that initializes the interface
230 * @param input callback function that is called to pass
231 * ingress packets up in the protocol layer stack.\n
232 * It is recommended to use a function that passes the input directly
233 * to the stack (netif_input(), NO_SYS=1 mode) or via sending a
234 * message to TCPIP thread (tcpip_input(), NO_SYS=0 mode).\n
235 * These functions use netif flags NETIF_FLAG_ETHARP and NETIF_FLAG_ETHERNET
236 * to decide whether to forward to ethernet_input() or ip_input().
237 * In other words, the functions only work when the netif
238 * driver is implemented correctly!\n
239 * Most members of struct netif should be be initialized by the
240 * netif init function = netif driver (init parameter of this function).\n
241 * IPv6: Don't forget to call netif_create_ip6_linklocal_address() after
242 * setting the MAC address in struct netif.hwaddr
243 * (IPv6 requires a link-local address).
244 *
245 * @return netif, or NULL if failed.
246 */
247 struct netif *
netif_add(struct netif * netif,const ip4_addr_t * ipaddr,const ip4_addr_t * netmask,const ip4_addr_t * gw,void * state,netif_init_fn init,netif_input_fn input)248 netif_add(struct netif *netif,
249 #if LWIP_IPV4
250 const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
251 #endif /* LWIP_IPV4 */
252 void *state, netif_init_fn init, netif_input_fn input)
253 {
254 #if LWIP_IPV6
255 s8_t i;
256 #endif
257
258 LWIP_ASSERT("No init function given", init != NULL);
259
260 /* reset new interface configuration state */
261 #if LWIP_IPV4
262 ip_addr_set_zero_ip4(&netif->ip_addr);
263 ip_addr_set_zero_ip4(&netif->netmask);
264 ip_addr_set_zero_ip4(&netif->gw);
265 #endif /* LWIP_IPV4 */
266 #if LWIP_IPV6
267 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
268 ip_addr_set_zero_ip6(&netif->ip6_addr[i]);
269 netif->ip6_addr_state[i] = IP6_ADDR_INVALID;
270 }
271 netif->output_ip6 = netif_null_output_ip6;
272 #endif /* LWIP_IPV6 */
273 NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
274 netif->flags = 0;
275 #ifdef netif_get_client_data
276 memset(netif->client_data, 0, sizeof(netif->client_data));
277 #endif /* LWIP_NUM_NETIF_CLIENT_DATA */
278 #if LWIP_IPV6_AUTOCONFIG
279 /* IPv6 address autoconfiguration not enabled by default */
280 netif->ip6_autoconfig_enabled = 0;
281 #endif /* LWIP_IPV6_AUTOCONFIG */
282 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
283 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
284 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
285 #if LWIP_NETIF_STATUS_CALLBACK
286 netif->status_callback = NULL;
287 #endif /* LWIP_NETIF_STATUS_CALLBACK */
288 #if LWIP_NETIF_LINK_CALLBACK
289 netif->link_callback = NULL;
290 #endif /* LWIP_NETIF_LINK_CALLBACK */
291 #if LWIP_IGMP
292 netif->igmp_mac_filter = NULL;
293 #endif /* LWIP_IGMP */
294 #if LWIP_IPV6 && LWIP_IPV6_MLD
295 netif->mld_mac_filter = NULL;
296 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
297 #if ENABLE_LOOPBACK
298 netif->loop_first = NULL;
299 netif->loop_last = NULL;
300 #endif /* ENABLE_LOOPBACK */
301
302 /* remember netif specific state information data */
303 netif->state = state;
304 netif->num = netif_num++;
305 netif->input = input;
306
307 NETIF_SET_HWADDRHINT(netif, NULL);
308 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
309 netif->loop_cnt_current = 0;
310 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
311
312 #if LWIP_IPV4
313 netif_set_addr(netif, ipaddr, netmask, gw);
314 #endif /* LWIP_IPV4 */
315
316 /* call user specified initialization function for netif */
317 if (init(netif) != ERR_OK) {
318 return NULL;
319 }
320
321 /* add this netif to the list */
322 netif->next = netif_list;
323 netif_list = netif;
324 mib2_netif_added(netif);
325
326 #if LWIP_IGMP
327 /* start IGMP processing */
328 if (netif->flags & NETIF_FLAG_IGMP) {
329 igmp_start(netif);
330 }
331 #endif /* LWIP_IGMP */
332
333 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
334 netif->name[0], netif->name[1]));
335 #if LWIP_IPV4
336 LWIP_DEBUGF(NETIF_DEBUG, (" addr "));
337 ip4_addr_debug_print(NETIF_DEBUG, ipaddr);
338 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
339 ip4_addr_debug_print(NETIF_DEBUG, netmask);
340 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
341 ip4_addr_debug_print(NETIF_DEBUG, gw);
342 #endif /* LWIP_IPV4 */
343 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
344 return netif;
345 }
346
347 #if LWIP_IPV4
348 /**
349 * @ingroup netif_ip4
350 * Change IP address configuration for a network interface (including netmask
351 * and default gateway).
352 *
353 * @param netif the network interface to change
354 * @param ipaddr the new IP address
355 * @param netmask the new netmask
356 * @param gw the new default gateway
357 */
358 void
netif_set_addr(struct netif * netif,const ip4_addr_t * ipaddr,const ip4_addr_t * netmask,const ip4_addr_t * gw)359 netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
360 const ip4_addr_t *gw)
361 {
362 if (ip4_addr_isany(ipaddr)) {
363 /* when removing an address, we have to remove it *before* changing netmask/gw
364 to ensure that tcp RST segment can be sent correctly */
365 netif_set_ipaddr(netif, ipaddr);
366 netif_set_netmask(netif, netmask);
367 netif_set_gw(netif, gw);
368 } else {
369 netif_set_netmask(netif, netmask);
370 netif_set_gw(netif, gw);
371 /* set ipaddr last to ensure netmask/gw have been set when status callback is called */
372 netif_set_ipaddr(netif, ipaddr);
373 }
374 }
375 #endif /* LWIP_IPV4*/
376
377 /**
378 * @ingroup netif
379 * Remove a network interface from the list of lwIP netifs.
380 *
381 * @param netif the network interface to remove
382 */
383 void
netif_remove(struct netif * netif)384 netif_remove(struct netif *netif)
385 {
386 #if LWIP_IPV6
387 int i;
388 #endif
389
390 if (netif == NULL) {
391 return;
392 }
393
394 #if LWIP_IPV4
395 if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
396 #if LWIP_TCP
397 tcp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
398 #endif /* LWIP_TCP */
399 #if LWIP_UDP
400 udp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
401 #endif /* LWIP_UDP */
402 #if LWIP_RAW
403 raw_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
404 #endif /* LWIP_RAW */
405 }
406
407 #if LWIP_IGMP
408 /* stop IGMP processing */
409 if (netif->flags & NETIF_FLAG_IGMP) {
410 igmp_stop(netif);
411 }
412 #endif /* LWIP_IGMP */
413 #endif /* LWIP_IPV4*/
414
415 #if LWIP_IPV6
416 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
417 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
418 #if LWIP_TCP
419 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
420 #endif /* LWIP_TCP */
421 #if LWIP_UDP
422 udp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
423 #endif /* LWIP_UDP */
424 #if LWIP_RAW
425 raw_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
426 #endif /* LWIP_RAW */
427 }
428 }
429 #if LWIP_IPV6_MLD
430 /* stop MLD processing */
431 mld6_stop(netif);
432 #endif /* LWIP_IPV6_MLD */
433 #endif /* LWIP_IPV6 */
434 if (netif_is_up(netif)) {
435 /* set netif down before removing (call callback function) */
436 netif_set_down(netif);
437 }
438
439 mib2_remove_ip4(netif);
440
441 /* this netif is default? */
442 if (netif_default == netif) {
443 /* reset default netif */
444 netif_set_default(NULL);
445 }
446 /* is it the first netif? */
447 if (netif_list == netif) {
448 netif_list = netif->next;
449 } else {
450 /* look for netif further down the list */
451 struct netif * tmp_netif;
452 for (tmp_netif = netif_list; tmp_netif != NULL; tmp_netif = tmp_netif->next) {
453 if (tmp_netif->next == netif) {
454 tmp_netif->next = netif->next;
455 break;
456 }
457 }
458 if (tmp_netif == NULL) {
459 return; /* netif is not on the list */
460 }
461 }
462 mib2_netif_removed(netif);
463 #if LWIP_NETIF_REMOVE_CALLBACK
464 if (netif->remove_callback) {
465 netif->remove_callback(netif);
466 }
467 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
468 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
469 }
470
471 /**
472 * @ingroup netif
473 * Find a network interface by searching for its name
474 *
475 * @param name the name of the netif (like netif->name) plus concatenated number
476 * in ascii representation (e.g. 'en0')
477 */
478 struct netif *
netif_find(const char * name)479 netif_find(const char *name)
480 {
481 struct netif *netif;
482 u8_t num;
483
484 if (name == NULL) {
485 return NULL;
486 }
487
488 num = (u8_t)(name[2] - '0');
489
490 for (netif = netif_list; netif != NULL; netif = netif->next) {
491 if (num == netif->num &&
492 name[0] == netif->name[0] &&
493 name[1] == netif->name[1]) {
494 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
495 return netif;
496 }
497 }
498 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
499 return NULL;
500 }
501
502 #if LWIP_IPV4
503 /**
504 * @ingroup netif_ip4
505 * Change the IP address of a network interface
506 *
507 * @param netif the network interface to change
508 * @param ipaddr the new IP address
509 *
510 * @note call netif_set_addr() if you also want to change netmask and
511 * default gateway
512 */
513 void
netif_set_ipaddr(struct netif * netif,const ip4_addr_t * ipaddr)514 netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
515 {
516 ip_addr_t new_addr;
517 *ip_2_ip4(&new_addr) = (ipaddr ? *ipaddr : *IP4_ADDR_ANY4);
518 IP_SET_TYPE_VAL(new_addr, IPADDR_TYPE_V4);
519
520 /* address is actually being changed? */
521 if (ip4_addr_cmp(ip_2_ip4(&new_addr), netif_ip4_addr(netif)) == 0) {
522 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
523 #if LWIP_TCP
524 tcp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
525 #endif /* LWIP_TCP */
526 #if LWIP_UDP
527 udp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
528 #endif /* LWIP_UDP */
529 #if LWIP_RAW
530 raw_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
531 #endif /* LWIP_RAW */
532
533 mib2_remove_ip4(netif);
534 mib2_remove_route_ip4(0, netif);
535 /* set new IP address to netif */
536 ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr);
537 IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4);
538 mib2_add_ip4(netif);
539 mib2_add_route_ip4(0, netif);
540
541 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
542
543 NETIF_STATUS_CALLBACK(netif);
544
545 #ifdef RT_USING_NETDEV
546 /* rt-thread sal network interface device set IP address operations */
547 netdev_low_level_set_ipaddr(netdev_get_by_name(netif->name), &netif->ip_addr);
548 #endif /* RT_USING_NETDEV */
549 }
550
551 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
552 netif->name[0], netif->name[1],
553 ip4_addr1_16(netif_ip4_addr(netif)),
554 ip4_addr2_16(netif_ip4_addr(netif)),
555 ip4_addr3_16(netif_ip4_addr(netif)),
556 ip4_addr4_16(netif_ip4_addr(netif))));
557 }
558
559 /**
560 * @ingroup netif_ip4
561 * Change the default gateway for a network interface
562 *
563 * @param netif the network interface to change
564 * @param gw the new default gateway
565 *
566 * @note call netif_set_addr() if you also want to change ip address and netmask
567 */
568 void
netif_set_gw(struct netif * netif,const ip4_addr_t * gw)569 netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
570 {
571 ip4_addr_set(ip_2_ip4(&netif->gw), gw);
572 IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4);
573 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
574 netif->name[0], netif->name[1],
575 ip4_addr1_16(netif_ip4_gw(netif)),
576 ip4_addr2_16(netif_ip4_gw(netif)),
577 ip4_addr3_16(netif_ip4_gw(netif)),
578 ip4_addr4_16(netif_ip4_gw(netif))));
579
580 #ifdef RT_USING_NETDEV
581 /* rt_thread network interface device set gateway address */
582 netdev_low_level_set_gw(netdev_get_by_name(netif->name), &netif->gw);
583 #endif /* RT_USING_NETDEV */
584 }
585
586 /**
587 * @ingroup netif_ip4
588 * Change the netmask of a network interface
589 *
590 * @param netif the network interface to change
591 * @param netmask the new netmask
592 *
593 * @note call netif_set_addr() if you also want to change ip address and
594 * default gateway
595 */
596 void
netif_set_netmask(struct netif * netif,const ip4_addr_t * netmask)597 netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
598 {
599 mib2_remove_route_ip4(0, netif);
600 /* set new netmask to netif */
601 ip4_addr_set(ip_2_ip4(&netif->netmask), netmask);
602 IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4);
603 mib2_add_route_ip4(0, netif);
604 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
605 netif->name[0], netif->name[1],
606 ip4_addr1_16(netif_ip4_netmask(netif)),
607 ip4_addr2_16(netif_ip4_netmask(netif)),
608 ip4_addr3_16(netif_ip4_netmask(netif)),
609 ip4_addr4_16(netif_ip4_netmask(netif))));
610
611 #ifdef RT_USING_NETDEV
612 /* rt-thread network interface device set netmask address */
613 netdev_low_level_set_netmask(netdev_get_by_name(netif->name), &netif->netmask);
614 #endif /* RT_USING_NETDEV */
615 }
616 #endif /* LWIP_IPV4 */
617
618 /**
619 * @ingroup netif
620 * Set a network interface as the default network interface
621 * (used to output all packets for which no specific route is found)
622 *
623 * @param netif the default network interface
624 */
625 void
netif_set_default(struct netif * netif)626 netif_set_default(struct netif *netif)
627 {
628 if (netif == NULL) {
629 /* remove default route */
630 mib2_remove_route_ip4(1, netif);
631 } else {
632 /* install default route */
633 mib2_add_route_ip4(1, netif);
634 }
635 netif_default = netif;
636 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
637 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
638 }
639
640 /**
641 * @ingroup netif
642 * Bring an interface up, available for processing
643 * traffic.
644 */
645 void
netif_set_up(struct netif * netif)646 netif_set_up(struct netif *netif)
647 {
648 if (!(netif->flags & NETIF_FLAG_UP)) {
649 netif->flags |= NETIF_FLAG_UP;
650
651 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
652
653 NETIF_STATUS_CALLBACK(netif);
654
655 if (netif->flags & NETIF_FLAG_LINK_UP) {
656 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
657 }
658
659 #ifdef RT_USING_NETDEV
660 /* rt-thread network interface device set up status */
661 netdev_low_level_set_status(netdev_get_by_name(netif->name), RT_TRUE);
662 #endif /* RT_USING_NETDEV */
663 }
664 }
665
666 /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
667 */
668 static void
netif_issue_reports(struct netif * netif,u8_t report_type)669 netif_issue_reports(struct netif* netif, u8_t report_type)
670 {
671 #if LWIP_IPV4
672 if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
673 !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
674 #if LWIP_ARP
675 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
676 if (netif->flags & (NETIF_FLAG_ETHARP)) {
677 etharp_gratuitous(netif);
678 }
679 #endif /* LWIP_ARP */
680
681 #if LWIP_IGMP
682 /* resend IGMP memberships */
683 if (netif->flags & NETIF_FLAG_IGMP) {
684 igmp_report_groups(netif);
685 }
686 #endif /* LWIP_IGMP */
687 }
688 #endif /* LWIP_IPV4 */
689
690 #if LWIP_IPV6
691 if (report_type & NETIF_REPORT_TYPE_IPV6) {
692 #if LWIP_IPV6_MLD
693 /* send mld memberships */
694 mld6_report_groups(netif);
695 #endif /* LWIP_IPV6_MLD */
696 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
697 /* Send Router Solicitation messages. */
698 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
699 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
700 }
701 #endif /* LWIP_IPV6 */
702 }
703
704 /**
705 * @ingroup netif
706 * Bring an interface down, disabling any traffic processing.
707 */
708 void
netif_set_down(struct netif * netif)709 netif_set_down(struct netif *netif)
710 {
711 if (netif->flags & NETIF_FLAG_UP) {
712 netif->flags &= ~NETIF_FLAG_UP;
713 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
714
715 #if LWIP_IPV4 && LWIP_ARP
716 if (netif->flags & NETIF_FLAG_ETHARP) {
717 etharp_cleanup_netif(netif);
718 }
719 #endif /* LWIP_IPV4 && LWIP_ARP */
720
721 #if LWIP_IPV6
722 nd6_cleanup_netif(netif);
723 #endif /* LWIP_IPV6 */
724
725 NETIF_STATUS_CALLBACK(netif);
726
727 #ifdef RT_USING_NETDEV
728 /* rt-thread network interface device set down status */
729 netdev_low_level_set_status(netdev_get_by_name(netif->name), RT_FALSE);
730 #endif /* RT_USING_NETDEV */
731 }
732 }
733
734 #if LWIP_NETIF_STATUS_CALLBACK
735 /**
736 * @ingroup netif
737 * Set callback to be called when interface is brought up/down or address is changed while up
738 */
739 void
netif_set_status_callback(struct netif * netif,netif_status_callback_fn status_callback)740 netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
741 {
742 if (netif) {
743 netif->status_callback = status_callback;
744 }
745 }
746 #endif /* LWIP_NETIF_STATUS_CALLBACK */
747
748 #if LWIP_NETIF_REMOVE_CALLBACK
749 /**
750 * @ingroup netif
751 * Set callback to be called when the interface has been removed
752 */
753 void
netif_set_remove_callback(struct netif * netif,netif_status_callback_fn remove_callback)754 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
755 {
756 if (netif) {
757 netif->remove_callback = remove_callback;
758 }
759 }
760 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
761
762 /**
763 * @ingroup netif
764 * Called by a driver when its link goes up
765 */
766 void
netif_set_link_up(struct netif * netif)767 netif_set_link_up(struct netif *netif)
768 {
769 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
770 netif->flags |= NETIF_FLAG_LINK_UP;
771
772 #if LWIP_DHCP
773 dhcp_network_changed(netif);
774 #endif /* LWIP_DHCP */
775
776 #if LWIP_AUTOIP
777 autoip_network_changed(netif);
778 #endif /* LWIP_AUTOIP */
779
780 if (netif->flags & NETIF_FLAG_UP) {
781 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
782 }
783 NETIF_LINK_CALLBACK(netif);
784
785 #ifdef RT_USING_NETDEV
786 /* rt-thread network interface device set link up status */
787 netdev_low_level_set_link_status(netdev_get_by_name(netif->name), RT_TRUE);
788 #endif /* RT_USING_NETDEV */
789 }
790 }
791
792 /**
793 * @ingroup netif
794 * Called by a driver when its link goes down
795 */
796 void
netif_set_link_down(struct netif * netif)797 netif_set_link_down(struct netif *netif )
798 {
799 if (netif->flags & NETIF_FLAG_LINK_UP) {
800 netif->flags &= ~NETIF_FLAG_LINK_UP;
801 NETIF_LINK_CALLBACK(netif);
802
803 #ifdef RT_USING_NETDEV
804 /* rt-thread network interface device set link down status */
805 netdev_low_level_set_link_status(netdev_get_by_name(netif->name), RT_FALSE);
806 #endif /* RT_USING_NETDEV */
807 }
808 }
809
810 #if LWIP_NETIF_LINK_CALLBACK
811 /**
812 * @ingroup netif
813 * Set callback to be called when link is brought up/down
814 */
815 void
netif_set_link_callback(struct netif * netif,netif_status_callback_fn link_callback)816 netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
817 {
818 if (netif) {
819 netif->link_callback = link_callback;
820 }
821 }
822 #endif /* LWIP_NETIF_LINK_CALLBACK */
823
824 #if ENABLE_LOOPBACK
825 /**
826 * @ingroup netif
827 * Send an IP packet to be received on the same netif (loopif-like).
828 * The pbuf is simply copied and handed back to netif->input.
829 * In multithreaded mode, this is done directly since netif->input must put
830 * the packet on a queue.
831 * In callback mode, the packet is put on an internal queue and is fed to
832 * netif->input by netif_poll().
833 *
834 * @param netif the lwip network interface structure
835 * @param p the (IP) packet to 'send'
836 * @return ERR_OK if the packet has been sent
837 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
838 */
839 err_t
netif_loop_output(struct netif * netif,struct pbuf * p)840 netif_loop_output(struct netif *netif, struct pbuf *p)
841 {
842 struct pbuf *r;
843 err_t err;
844 struct pbuf *last;
845 #if LWIP_LOOPBACK_MAX_PBUFS
846 u16_t clen = 0;
847 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
848 /* If we have a loopif, SNMP counters are adjusted for it,
849 * if not they are adjusted for 'netif'. */
850 #if MIB2_STATS
851 #if LWIP_HAVE_LOOPIF
852 struct netif *stats_if = &loop_netif;
853 #else /* LWIP_HAVE_LOOPIF */
854 struct netif *stats_if = netif;
855 #endif /* LWIP_HAVE_LOOPIF */
856 #endif /* MIB2_STATS */
857 SYS_ARCH_DECL_PROTECT(lev);
858
859 /* Allocate a new pbuf */
860 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
861 if (r == NULL) {
862 LINK_STATS_INC(link.memerr);
863 LINK_STATS_INC(link.drop);
864 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
865 return ERR_MEM;
866 }
867 #if LWIP_LOOPBACK_MAX_PBUFS
868 clen = pbuf_clen(r);
869 /* check for overflow or too many pbuf on queue */
870 if (((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
871 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
872 pbuf_free(r);
873 LINK_STATS_INC(link.memerr);
874 LINK_STATS_INC(link.drop);
875 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
876 return ERR_MEM;
877 }
878 netif->loop_cnt_current += clen;
879 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
880
881 /* Copy the whole pbuf queue p into the single pbuf r */
882 if ((err = pbuf_copy(r, p)) != ERR_OK) {
883 pbuf_free(r);
884 LINK_STATS_INC(link.memerr);
885 LINK_STATS_INC(link.drop);
886 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
887 return err;
888 }
889
890 /* Put the packet on a linked list which gets emptied through calling
891 netif_poll(). */
892
893 /* let last point to the last pbuf in chain r */
894 for (last = r; last->next != NULL; last = last->next) {
895 /* nothing to do here, just get to the last pbuf */
896 }
897
898 SYS_ARCH_PROTECT(lev);
899 if (netif->loop_first != NULL) {
900 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
901 netif->loop_last->next = r;
902 netif->loop_last = last;
903 } else {
904 netif->loop_first = r;
905 netif->loop_last = last;
906 }
907 SYS_ARCH_UNPROTECT(lev);
908
909 LINK_STATS_INC(link.xmit);
910 MIB2_STATS_NETIF_ADD(stats_if, ifoutoctets, p->tot_len);
911 MIB2_STATS_NETIF_INC(stats_if, ifoutucastpkts);
912
913 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
914 /* For multithreading environment, schedule a call to netif_poll */
915 tcpip_callback_with_block((tcpip_callback_fn)netif_poll, netif, 0);
916 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
917
918 return ERR_OK;
919 }
920
921 #if LWIP_HAVE_LOOPIF
922 #if LWIP_IPV4
923 static err_t
netif_loop_output_ipv4(struct netif * netif,struct pbuf * p,const ip4_addr_t * addr)924 netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr)
925 {
926 LWIP_UNUSED_ARG(addr);
927 return netif_loop_output(netif, p);
928 }
929 #endif /* LWIP_IPV4 */
930
931 #if LWIP_IPV6
932 static err_t
netif_loop_output_ipv6(struct netif * netif,struct pbuf * p,const ip6_addr_t * addr)933 netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr)
934 {
935 LWIP_UNUSED_ARG(addr);
936 return netif_loop_output(netif, p);
937 }
938 #endif /* LWIP_IPV6 */
939 #endif /* LWIP_HAVE_LOOPIF */
940
941
942 /**
943 * Call netif_poll() in the main loop of your application. This is to prevent
944 * reentering non-reentrant functions like tcp_input(). Packets passed to
945 * netif_loop_output() are put on a list that is passed to netif->input() by
946 * netif_poll().
947 */
948 void
netif_poll(struct netif * netif)949 netif_poll(struct netif *netif)
950 {
951 /* If we have a loopif, SNMP counters are adjusted for it,
952 * if not they are adjusted for 'netif'. */
953 #if MIB2_STATS
954 #if LWIP_HAVE_LOOPIF
955 struct netif *stats_if = &loop_netif;
956 #else /* LWIP_HAVE_LOOPIF */
957 struct netif *stats_if = netif;
958 #endif /* LWIP_HAVE_LOOPIF */
959 #endif /* MIB2_STATS */
960 SYS_ARCH_DECL_PROTECT(lev);
961
962 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
963 SYS_ARCH_PROTECT(lev);
964 while (netif->loop_first != NULL) {
965 struct pbuf *in, *in_end;
966 #if LWIP_LOOPBACK_MAX_PBUFS
967 u8_t clen = 1;
968 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
969
970 in = in_end = netif->loop_first;
971 while (in_end->len != in_end->tot_len) {
972 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
973 in_end = in_end->next;
974 #if LWIP_LOOPBACK_MAX_PBUFS
975 clen++;
976 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
977 }
978 #if LWIP_LOOPBACK_MAX_PBUFS
979 /* adjust the number of pbufs on queue */
980 LWIP_ASSERT("netif->loop_cnt_current underflow",
981 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
982 netif->loop_cnt_current -= clen;
983 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
984
985 /* 'in_end' now points to the last pbuf from 'in' */
986 if (in_end == netif->loop_last) {
987 /* this was the last pbuf in the list */
988 netif->loop_first = netif->loop_last = NULL;
989 } else {
990 /* pop the pbuf off the list */
991 netif->loop_first = in_end->next;
992 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
993 }
994 /* De-queue the pbuf from its successors on the 'loop_' list. */
995 in_end->next = NULL;
996 SYS_ARCH_UNPROTECT(lev);
997
998 LINK_STATS_INC(link.recv);
999 MIB2_STATS_NETIF_ADD(stats_if, ifinoctets, in->tot_len);
1000 MIB2_STATS_NETIF_INC(stats_if, ifinucastpkts);
1001 /* loopback packets are always IP packets! */
1002 if (ip_input(in, netif) != ERR_OK) {
1003 pbuf_free(in);
1004 }
1005 SYS_ARCH_PROTECT(lev);
1006 }
1007 SYS_ARCH_UNPROTECT(lev);
1008 }
1009
1010 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
1011 /**
1012 * Calls netif_poll() for every netif on the netif_list.
1013 */
1014 void
netif_poll_all(void)1015 netif_poll_all(void)
1016 {
1017 struct netif *netif = netif_list;
1018 /* loop through netifs */
1019 while (netif != NULL) {
1020 netif_poll(netif);
1021 /* proceed to next network interface */
1022 netif = netif->next;
1023 }
1024 }
1025 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
1026 #endif /* ENABLE_LOOPBACK */
1027
1028 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
1029 /**
1030 * @ingroup netif_cd
1031 * Allocate an index to store data in client_data member of struct netif.
1032 * Returned value is an index in mentioned array.
1033 * @see LWIP_NUM_NETIF_CLIENT_DATA
1034 */
1035 u8_t
netif_alloc_client_data_id(void)1036 netif_alloc_client_data_id(void)
1037 {
1038 u8_t result = netif_client_id;
1039 netif_client_id++;
1040
1041 LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", result < LWIP_NUM_NETIF_CLIENT_DATA);
1042 return result + LWIP_NETIF_CLIENT_DATA_INDEX_MAX;
1043 }
1044 #endif
1045
1046 #if LWIP_IPV6
1047 /**
1048 * @ingroup netif_ip6
1049 * Change an IPv6 address of a network interface
1050 *
1051 * @param netif the network interface to change
1052 * @param addr_idx index of the IPv6 address
1053 * @param addr6 the new IPv6 address
1054 *
1055 * @note call netif_ip6_addr_set_state() to set the address valid/temptative
1056 */
1057 void
netif_ip6_addr_set(struct netif * netif,s8_t addr_idx,const ip6_addr_t * addr6)1058 netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6)
1059 {
1060 LWIP_ASSERT("addr6 != NULL", addr6 != NULL);
1061 netif_ip6_addr_set_parts(netif, addr_idx, addr6->addr[0], addr6->addr[1],
1062 addr6->addr[2], addr6->addr[3]);
1063 }
1064
1065 /*
1066 * Change an IPv6 address of a network interface (internal version taking 4 * u32_t)
1067 *
1068 * @param netif the network interface to change
1069 * @param addr_idx index of the IPv6 address
1070 * @param i0 word0 of the new IPv6 address
1071 * @param i1 word1 of the new IPv6 address
1072 * @param i2 word2 of the new IPv6 address
1073 * @param i3 word3 of the new IPv6 address
1074 */
1075 void
netif_ip6_addr_set_parts(struct netif * netif,s8_t addr_idx,u32_t i0,u32_t i1,u32_t i2,u32_t i3)1076 netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3)
1077 {
1078 const ip6_addr_t *old_addr;
1079 LWIP_ASSERT("netif != NULL", netif != NULL);
1080 LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1081
1082 old_addr = netif_ip6_addr(netif, addr_idx);
1083 /* address is actually being changed? */
1084 if ((old_addr->addr[0] != i0) || (old_addr->addr[1] != i1) ||
1085 (old_addr->addr[2] != i2) || (old_addr->addr[3] != i3)) {
1086 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set: netif address being changed\n"));
1087
1088 if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
1089 #if LWIP_TCP || LWIP_UDP
1090 ip_addr_t new_ipaddr;
1091 IP_ADDR6(&new_ipaddr, i0, i1, i2, i3);
1092 #endif /* LWIP_TCP || LWIP_UDP */
1093 #if LWIP_TCP
1094 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1095 #endif /* LWIP_TCP */
1096 #if LWIP_UDP
1097 udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1098 #endif /* LWIP_UDP */
1099 #if LWIP_RAW
1100 raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1101 #endif /* LWIP_RAW */
1102 }
1103 /* @todo: remove/readd mib2 ip6 entries? */
1104
1105 IP6_ADDR(ip_2_ip6(&(netif->ip6_addr[addr_idx])), i0, i1, i2, i3);
1106 IP_SET_TYPE_VAL(netif->ip6_addr[addr_idx], IPADDR_TYPE_V6);
1107
1108 if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
1109 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1110 NETIF_STATUS_CALLBACK(netif);
1111 }
1112 }
1113
1114 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IPv6 address %d of interface %c%c set to %s/0x%"X8_F"\n",
1115 addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1116 netif_ip6_addr_state(netif, addr_idx)));
1117 }
1118
1119 /**
1120 * @ingroup netif_ip6
1121 * Change the state of an IPv6 address of a network interface
1122 * (INVALID, TEMPTATIVE, PREFERRED, DEPRECATED, where TEMPTATIVE
1123 * includes the number of checks done, see ip6_addr.h)
1124 *
1125 * @param netif the network interface to change
1126 * @param addr_idx index of the IPv6 address
1127 * @param state the new IPv6 address state
1128 */
1129 void
netif_ip6_addr_set_state(struct netif * netif,s8_t addr_idx,u8_t state)1130 netif_ip6_addr_set_state(struct netif* netif, s8_t addr_idx, u8_t state)
1131 {
1132 u8_t old_state;
1133 LWIP_ASSERT("netif != NULL", netif != NULL);
1134 LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1135
1136 old_state = netif_ip6_addr_state(netif, addr_idx);
1137 /* state is actually being changed? */
1138 if (old_state != state) {
1139 u8_t old_valid = old_state & IP6_ADDR_VALID;
1140 u8_t new_valid = state & IP6_ADDR_VALID;
1141 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set_state: netif address state being changed\n"));
1142
1143 #if LWIP_IPV6_MLD
1144 /* Reevaluate solicited-node multicast group membership. */
1145 if (netif->flags & NETIF_FLAG_MLD6) {
1146 nd6_adjust_mld_membership(netif, addr_idx, state);
1147 }
1148 #endif /* LWIP_IPV6_MLD */
1149
1150 if (old_valid && !new_valid) {
1151 /* address about to be removed by setting invalid */
1152 #if LWIP_TCP
1153 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1154 #endif /* LWIP_TCP */
1155 #if LWIP_UDP
1156 udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1157 #endif /* LWIP_UDP */
1158 #if LWIP_RAW
1159 raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1160 #endif /* LWIP_RAW */
1161 /* @todo: remove mib2 ip6 entries? */
1162 }
1163 netif->ip6_addr_state[addr_idx] = state;
1164
1165 if (!old_valid && new_valid) {
1166 /* address added by setting valid */
1167 /* @todo: add mib2 ip6 entries? */
1168 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1169 }
1170 if ((old_state & IP6_ADDR_PREFERRED) != (state & IP6_ADDR_PREFERRED)) {
1171 /* address state has changed (valid flag changed or switched between
1172 preferred and deprecated) -> call the callback function */
1173 NETIF_STATUS_CALLBACK(netif);
1174 }
1175 }
1176
1177 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IPv6 address %d of interface %c%c set to %s/0x%"X8_F"\n",
1178 addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1179 netif_ip6_addr_state(netif, addr_idx)));
1180 }
1181
1182 /**
1183 * Checks if a specific address is assigned to the netif and returns its
1184 * index.
1185 *
1186 * @param netif the netif to check
1187 * @param ip6addr the IPv6 address to find
1188 * @return >= 0: address found, this is its index
1189 * -1: address not found on this netif
1190 */
1191 s8_t
netif_get_ip6_addr_match(struct netif * netif,const ip6_addr_t * ip6addr)1192 netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
1193 {
1194 s8_t i;
1195 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1196 if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
1197 ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
1198 return i;
1199 }
1200 }
1201 return -1;
1202 }
1203
1204 /**
1205 * @ingroup netif_ip6
1206 * Create a link-local IPv6 address on a netif (stored in slot 0)
1207 *
1208 * @param netif the netif to create the address on
1209 * @param from_mac_48bit if != 0, assume hwadr is a 48-bit MAC address (std conversion)
1210 * if == 0, use hwaddr directly as interface ID
1211 */
1212 void
netif_create_ip6_linklocal_address(struct netif * netif,u8_t from_mac_48bit)1213 netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
1214 {
1215 u8_t i, addr_index;
1216
1217 /* Link-local prefix. */
1218 ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
1219 ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
1220
1221 /* Generate interface ID. */
1222 if (from_mac_48bit) {
1223 /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
1224 ip_2_ip6(&netif->ip6_addr[0])->addr[2] = lwip_htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
1225 ((u32_t)(netif->hwaddr[1]) << 16) |
1226 ((u32_t)(netif->hwaddr[2]) << 8) |
1227 (0xff));
1228 ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((0xfeul << 24) |
1229 ((u32_t)(netif->hwaddr[3]) << 16) |
1230 ((u32_t)(netif->hwaddr[4]) << 8) |
1231 (netif->hwaddr[5]));
1232 } else {
1233 /* Use hwaddr directly as interface ID. */
1234 ip_2_ip6(&netif->ip6_addr[0])->addr[2] = 0;
1235 ip_2_ip6(&netif->ip6_addr[0])->addr[3] = 0;
1236
1237 addr_index = 3;
1238 for (i = 0; (i < 8) && (i < netif->hwaddr_len); i++) {
1239 if (i == 4) {
1240 addr_index--;
1241 }
1242 ip_2_ip6(&netif->ip6_addr[0])->addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
1243 }
1244 }
1245
1246 #ifdef RT_USING_NETDEV
1247 /* rt-thread network interface device set ipv6 address */
1248 ip_addr_copy(netdev_get_by_name(netif->name)->ip6_addr[0], netif->ip6_addr[0]);
1249 #endif /* RT_USING_NETDEV */
1250
1251 /* Set address state. */
1252 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
1253 /* Will perform duplicate address detection (DAD). */
1254 netif_ip6_addr_set_state(netif, 0, IP6_ADDR_TENTATIVE);
1255 #else
1256 /* Consider address valid. */
1257 netif_ip6_addr_set_state(netif, 0, IP6_ADDR_PREFERRED);
1258 #endif /* LWIP_IPV6_AUTOCONFIG */
1259 }
1260
1261 /**
1262 * @ingroup netif_ip6
1263 * This function allows for the easy addition of a new IPv6 address to an interface.
1264 * It takes care of finding an empty slot and then sets the address tentative
1265 * (to make sure that all the subsequent processing happens).
1266 *
1267 * @param netif netif to add the address on
1268 * @param ip6addr address to add
1269 * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
1270 */
1271 err_t
netif_add_ip6_address(struct netif * netif,const ip6_addr_t * ip6addr,s8_t * chosen_idx)1272 netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
1273 {
1274 s8_t i;
1275
1276 i = netif_get_ip6_addr_match(netif, ip6addr);
1277 if (i >= 0) {
1278 /* Address already added */
1279 if (chosen_idx != NULL) {
1280 *chosen_idx = i;
1281 }
1282 return ERR_OK;
1283 }
1284
1285 /* Find a free slot -- musn't be the first one (reserved for link local) */
1286 for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1287 if (ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) {
1288 ip_addr_copy_from_ip6(netif->ip6_addr[i], *ip6addr);
1289 #ifdef RT_USING_NETDEV
1290 /* rt-thread network interface device set ipv6 address */
1291 ip_addr_copy(netdev_get_by_name(netif->name)->ip6_addr[i], netif->ip6_addr[i]);
1292 #endif /* RT_USING_NETDEV */
1293 netif_ip6_addr_set_state(netif, i, IP6_ADDR_TENTATIVE);
1294 if (chosen_idx != NULL) {
1295 *chosen_idx = i;
1296 }
1297 return ERR_OK;
1298 }
1299 }
1300
1301 if (chosen_idx != NULL) {
1302 *chosen_idx = -1;
1303 }
1304 return ERR_VAL;
1305 }
1306
1307 /** Dummy IPv6 output function for netifs not supporting IPv6
1308 */
1309 static err_t
netif_null_output_ip6(struct netif * netif,struct pbuf * p,const ip6_addr_t * ipaddr)1310 netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
1311 {
1312 LWIP_UNUSED_ARG(netif);
1313 LWIP_UNUSED_ARG(p);
1314 LWIP_UNUSED_ARG(ipaddr);
1315
1316 return ERR_IF;
1317 }
1318 #endif /* LWIP_IPV6 */
1319