Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / lwip / repo / lwip / src / core / netif.c
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 #include "lwip/if.h"
69 #if ENABLE_LOOPBACK
70 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
71 #include "lwip/tcpip.h"
72 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
73 #endif /* ENABLE_LOOPBACK */
74
75 #include "netif/ethernet.h"
76
77 #if LWIP_AUTOIP
78 #include "lwip/autoip.h"
79 #endif /* LWIP_AUTOIP */
80 #if LWIP_DHCP
81 #include "lwip/dhcp.h"
82 #endif /* LWIP_DHCP */
83 #if LWIP_IPV6_DHCP6
84 #include "lwip/dhcp6.h"
85 #endif /* LWIP_IPV6_DHCP6 */
86 #if LWIP_IPV6_MLD
87 #include "lwip/mld6.h"
88 #endif /* LWIP_IPV6_MLD */
89 #if LWIP_IPV6
90 #include "lwip/nd6.h"
91 #endif
92
93 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
94 #include "lwip/ip6_route_table.h"
95 #endif /* LWIP_IPV6_ROUTE_TABLE_SUPPORT */
96
97 #if LWIP_NETIF_STATUS_CALLBACK
98 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
99 #else
100 #define NETIF_STATUS_CALLBACK(n)
101 #endif /* LWIP_NETIF_STATUS_CALLBACK */
102
103 #if LWIP_NETIF_LINK_CALLBACK
104 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
105 #else
106 #define NETIF_LINK_CALLBACK(n)
107 #endif /* LWIP_NETIF_LINK_CALLBACK */
108
109 struct netif *netif_list;
110 struct netif *netif_default;
111
112 #define netif_index_to_num(index)   ((index) - 1)
113 static u8_t netif_num;
114
115 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
116 static u8_t netif_client_id;
117 #endif
118
119 #define NETIF_REPORT_TYPE_IPV4  0x01
120 #define NETIF_REPORT_TYPE_IPV6  0x02
121 static void netif_issue_reports(struct netif* netif, u8_t report_type);
122
123 #if LWIP_IPV6
124 static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
125 #endif /* LWIP_IPV6 */
126
127 #if LWIP_HAVE_LOOPIF
128 #if LWIP_IPV4
129 static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr);
130 #endif
131 #if LWIP_IPV6
132 static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr);
133 #endif
134
135
136 static struct netif loop_netif;
137
138 /**
139  * Initialize a lwip network interface structure for a loopback interface
140  *
141  * @param netif the lwip network interface structure for this loopif
142  * @return ERR_OK if the loopif is initialized
143  *         ERR_MEM if private data couldn't be allocated
144  */
145 static err_t
146 netif_loopif_init(struct netif *netif)
147 {
148   /* initialize the snmp variables and counters inside the struct netif
149    * ifSpeed: no assumption can be made!
150    */
151   MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
152
153   netif->name[0] = 'l';
154   netif->name[1] = 'o';
155 #if LWIP_IPV4
156   netif->output = netif_loop_output_ipv4;
157 #endif
158 #if LWIP_IPV6
159   netif->output_ip6 = netif_loop_output_ipv6;
160 #endif
161 #if LWIP_LOOPIF_MULTICAST
162   netif->flags |= NETIF_FLAG_IGMP;
163 #endif
164   return ERR_OK;
165 }
166 #endif /* LWIP_HAVE_LOOPIF */
167
168 void
169 netif_init(void)
170 {
171 #if LWIP_HAVE_LOOPIF
172 #if LWIP_IPV4
173 #define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw,
174   ip4_addr_t loop_ipaddr, loop_netmask, loop_gw;
175   IP4_ADDR(&loop_gw, 127,0,0,1);
176   IP4_ADDR(&loop_ipaddr, 127,0,0,1);
177   IP4_ADDR(&loop_netmask, 255,0,0,0);
178 #else /* LWIP_IPV4 */
179 #define LOOPIF_ADDRINIT
180 #endif /* LWIP_IPV4 */
181
182 #if NO_SYS
183   netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
184 #else  /* NO_SYS */
185   netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
186 #endif /* NO_SYS */
187
188 #if LWIP_IPV6
189   IP_ADDR6_HOST(loop_netif.ip6_addr, 0, 0, 0, 0x00000001UL);
190   loop_netif.ip6_addr_state[0] = IP6_ADDR_VALID;
191 #endif /* LWIP_IPV6 */
192
193   netif_set_link_up(&loop_netif);
194   netif_set_up(&loop_netif);
195
196 #endif /* LWIP_HAVE_LOOPIF */
197 }
198
199 void
200 netif_apply_pcb(struct netif *netif, struct ip_pcb *pcb)
201 {
202   NETIF_SET_HWADDRHINT(netif, pcb?&(pcb->addr_hint)?NULL);
203 #if LWIP_MANAGEMENT_CHANNEL
204   netif->using_management_channel = pcb?!!ip_get_option(pcb,SOF_MANAGEMENT):0;
205 #endif
206 }
207
208 /**
209  * @ingroup lwip_nosys
210  * Forwards a received packet for input processing with
211  * ethernet_input() or ip_input() depending on netif flags.
212  * Don't call directly, pass to netif_add() and call
213  * netif->input().
214  * Only works if the netif driver correctly sets 
215  * NETIF_FLAG_ETHARP and/or NETIF_FLAG_ETHERNET flag!
216  */
217 err_t
218 netif_input(struct pbuf *p, struct netif *inp)
219 {
220   LWIP_ASSERT_CORE_LOCKED();
221
222 #if LWIP_ETHERNET
223   if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
224     return ethernet_input(p, inp);
225   } else
226 #endif /* LWIP_ETHERNET */
227   return ip_input(p, inp);
228 }
229
230 /**
231  * @ingroup netif
232  * Add a network interface to the list of lwIP netifs.
233  *
234  * @param netif a pre-allocated netif structure
235  * @param ipaddr IP address for the new netif
236  * @param netmask network mask for the new netif
237  * @param gw default gateway IP address for the new netif
238  * @param state opaque data passed to the new netif
239  * @param init callback function that initializes the interface
240  * @param input callback function that is called to pass
241  * ingress packets up in the protocol layer stack.\n
242  * It is recommended to use a function that passes the input directly
243  * to the stack (netif_input(), NO_SYS=1 mode) or via sending a
244  * message to TCPIP thread (tcpip_input(), NO_SYS=0 mode).\n
245  * These functions use netif flags NETIF_FLAG_ETHARP and NETIF_FLAG_ETHERNET
246  * to decide whether to forward to ethernet_input() or ip_input().
247  * In other words, the functions only work when the netif
248  * driver is implemented correctly!\n
249  * Most members of struct netif should be be initialized by the 
250  * netif init function = netif driver (init parameter of this function).\n
251  * IPv6: Don't forget to call netif_create_ip6_linklocal_address() after
252  * setting the MAC address in struct netif.hwaddr
253  * (IPv6 requires a link-local address).
254  * 
255  * @return netif, or NULL if failed.
256  */
257 struct netif *
258 netif_add(struct netif *netif,
259 #if LWIP_IPV4
260           const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
261 #endif /* LWIP_IPV4 */
262           void *state, netif_init_fn init, netif_input_fn input)
263 {
264 #if LWIP_IPV6
265   s8_t i;
266 #endif
267
268   LWIP_ASSERT_CORE_LOCKED();
269
270   LWIP_ASSERT("No init function given", init != NULL);
271
272   /* reset new interface configuration state */
273 #if LWIP_IPV4
274   ip_addr_set_zero_ip4(&netif->ip_addr);
275   ip_addr_set_zero_ip4(&netif->netmask);
276   ip_addr_set_zero_ip4(&netif->gw);
277 #endif /* LWIP_IPV4 */
278 #if LWIP_IPV6
279   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
280     ip_addr_set_zero_ip6(&netif->ip6_addr[i]);
281     netif->ip6_addr_state[i] = IP6_ADDR_INVALID;
282 #if LWIP_IPV6_ADDRESS_LIFETIMES
283     netif->ip6_addr_valid_life[i] = IP6_ADDR_LIFE_STATIC;
284     netif->ip6_addr_pref_life[i] = IP6_ADDR_LIFE_STATIC;
285 #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */
286   }
287   netif->output_ip6 = netif_null_output_ip6;
288 #endif /* LWIP_IPV6 */
289   NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
290   netif->flags = 0;
291 #ifdef netif_get_client_data
292   memset(netif->client_data, 0, sizeof(netif->client_data));
293 #endif /* LWIP_NUM_NETIF_CLIENT_DATA */
294 #if LWIP_IPV6_AUTOCONFIG
295   /* IPv6 address autoconfiguration not enabled by default */
296   netif->ip6_autoconfig_enabled = 0;
297 #endif /* LWIP_IPV6_AUTOCONFIG */
298 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
299   netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
300 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
301 #if LWIP_NETIF_STATUS_CALLBACK
302   netif->status_callback = NULL;
303 #endif /* LWIP_NETIF_STATUS_CALLBACK */
304 #if LWIP_NETIF_LINK_CALLBACK
305   netif->link_callback = NULL;
306 #endif /* LWIP_NETIF_LINK_CALLBACK */
307 #if LWIP_IGMP
308   netif->igmp_mac_filter = NULL;
309 #endif /* LWIP_IGMP */
310 #if LWIP_IPV6 && LWIP_IPV6_MLD
311   netif->mld_mac_filter = NULL;
312 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
313 #if ENABLE_LOOPBACK
314   netif->loop_first = NULL;
315   netif->loop_last = NULL;
316 #endif /* ENABLE_LOOPBACK */
317
318   /* remember netif specific state information data */
319   netif->state = state;
320   netif->num = netif_num++;
321   netif->input = input;
322
323   NETIF_SET_HWADDRHINT(netif, NULL);
324 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
325   netif->loop_cnt_current = 0;
326 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
327
328 #if LWIP_IPV4
329   netif_set_addr(netif, ipaddr, netmask, gw);
330 #endif /* LWIP_IPV4 */
331
332   /* call user specified initialization function for netif */
333   if (init(netif) != ERR_OK) {
334     return NULL;
335   }
336
337   /* add this netif to the list */
338   netif->next = netif_list;
339   netif_list = netif;
340   mib2_netif_added(netif);
341
342 #if LWIP_IGMP
343   /* start IGMP processing */
344   if (netif->flags & NETIF_FLAG_IGMP) {
345     igmp_start(netif);
346   }
347 #endif /* LWIP_IGMP */
348
349   LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
350     netif->name[0], netif->name[1]));
351 #if LWIP_IPV4
352   LWIP_DEBUGF(NETIF_DEBUG, (" addr "));
353   ip4_addr_debug_print(NETIF_DEBUG, ipaddr);
354   LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
355   ip4_addr_debug_print(NETIF_DEBUG, netmask);
356   LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
357   ip4_addr_debug_print(NETIF_DEBUG, gw);
358 #endif /* LWIP_IPV4 */
359   LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
360   return netif;
361 }
362
363 #if LWIP_IPV4
364 /**
365  * @ingroup netif_ip4
366  * Change IP address configuration for a network interface (including netmask
367  * and default gateway).
368  *
369  * @param netif the network interface to change
370  * @param ipaddr the new IP address
371  * @param netmask the new netmask
372  * @param gw the new default gateway
373  */
374 void
375 netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
376     const ip4_addr_t *gw)
377 {
378   LWIP_ASSERT_CORE_LOCKED();
379
380   if (ip4_addr_isany(ipaddr)) {
381     /* when removing an address, we have to remove it *before* changing netmask/gw
382        to ensure that tcp RST segment can be sent correctly */
383     netif_set_ipaddr(netif, ipaddr);
384     netif_set_netmask(netif, netmask);
385     netif_set_gw(netif, gw);
386   } else {
387     netif_set_netmask(netif, netmask);
388     netif_set_gw(netif, gw);
389     /* set ipaddr last to ensure netmask/gw have been set when status callback is called */
390     netif_set_ipaddr(netif, ipaddr);
391   }
392 }
393 #endif /* LWIP_IPV4*/
394
395 /**
396  * @ingroup netif
397  * Remove a network interface from the list of lwIP netifs.
398  *
399  * @param netif the network interface to remove
400  */
401 void
402 netif_remove(struct netif *netif)
403 {
404 #if LWIP_IPV6
405   int i;
406 #endif
407
408   LWIP_ASSERT_CORE_LOCKED();
409
410   if (netif == NULL) {
411     return;
412   }
413
414 #if LWIP_IPV4
415   if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
416 #if LWIP_TCP
417     tcp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
418 #endif /* LWIP_TCP */
419 #if LWIP_UDP
420     udp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
421 #endif /* LWIP_UDP */
422 #if LWIP_RAW
423     raw_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
424 #endif /* LWIP_RAW */
425   }
426
427 #if LWIP_IGMP
428   /* stop IGMP processing */
429   if (netif->flags & NETIF_FLAG_IGMP) {
430     igmp_stop(netif);
431   }
432 #endif /* LWIP_IGMP */
433 #endif /* LWIP_IPV4*/
434
435 #if LWIP_IPV6
436   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
437     if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
438 #if LWIP_TCP
439       tcp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
440 #endif /* LWIP_TCP */
441 #if LWIP_UDP
442       udp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
443 #endif /* LWIP_UDP */
444 #if LWIP_RAW
445       raw_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
446 #endif /* LWIP_RAW */
447     }
448   }
449 #if LWIP_IPV6_MLD
450   /* stop MLD processing */
451   mld6_stop(netif);
452 #endif /* LWIP_IPV6_MLD */
453 #endif /* LWIP_IPV6 */
454   if (netif_is_up(netif)) {
455     /* set netif down before removing (call callback function) */
456     netif_set_down(netif);
457   }
458
459   mib2_remove_ip4(netif);
460
461   /* this netif is default? */
462   if (netif_default == netif) {
463     /* reset default netif */
464     netif_set_default(NULL);
465   }
466   /*  is it the first netif? */
467   if (netif_list == netif) {
468     netif_list = netif->next;
469   } else {
470     /*  look for netif further down the list */
471     struct netif * tmp_netif;
472     for (tmp_netif = netif_list; tmp_netif != NULL; tmp_netif = tmp_netif->next) {
473       if (tmp_netif->next == netif) {
474         tmp_netif->next = netif->next;
475         break;
476       }
477     }
478     if (tmp_netif == NULL) {
479       return; /* netif is not on the list */
480     }
481   }
482   mib2_netif_removed(netif);
483 #if LWIP_NETIF_REMOVE_CALLBACK
484   if (netif->remove_callback) {
485     netif->remove_callback(netif);
486   }
487 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
488   LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
489 }
490
491 /**
492  * @ingroup netif
493  * Find a network interface by searching for its name
494  *
495  * @param name the name of the netif (like netif->name) plus concatenated number
496  * in ascii representation (e.g. 'en0')
497  */
498 struct netif *
499 netif_find(const char *name)
500 {
501   struct netif *netif;
502   u8_t num;
503
504   if (name == NULL) {
505     return NULL;
506   }
507
508   num = (u8_t)(name[2] - '0');
509
510   for (netif = netif_list; netif != NULL; netif = netif->next) {
511     if (num == netif->num &&
512        name[0] == netif->name[0] &&
513        name[1] == netif->name[1]) {
514       LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
515       return netif;
516     }
517   }
518   LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
519   return NULL;
520 }
521
522 #if LWIP_IPV4
523 /**
524  * @ingroup netif_ip4
525  * Change the IP address of a network interface
526  *
527  * @param netif the network interface to change
528  * @param ipaddr the new IP address
529  *
530  * @note call netif_set_addr() if you also want to change netmask and
531  * default gateway
532  */
533 void
534 netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
535 {
536   ip_addr_t new_addr;
537
538   LWIP_ASSERT_CORE_LOCKED();
539
540   *ip_2_ip4(&new_addr) = (ipaddr ? *ipaddr : *IP4_ADDR_ANY4);
541   IP_SET_TYPE_VAL(new_addr, IPADDR_TYPE_V4);
542
543   /* address is actually being changed? */
544   if (ip4_addr_cmp(ip_2_ip4(&new_addr), netif_ip4_addr(netif)) == 0) {
545     LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
546 #if LWIP_TCP
547     tcp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
548 #endif /* LWIP_TCP */
549 #if LWIP_UDP
550     udp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
551 #endif /* LWIP_UDP */
552 #if LWIP_RAW
553     raw_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
554 #endif /* LWIP_RAW */
555
556     mib2_remove_ip4(netif);
557     mib2_remove_route_ip4(0, netif);
558     /* set new IP address to netif */
559     ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr);
560     IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4);
561     mib2_add_ip4(netif);
562     mib2_add_route_ip4(0, netif);
563
564     netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
565
566     NETIF_STATUS_CALLBACK(netif);
567   }
568
569   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",
570     netif->name[0], netif->name[1],
571     ip4_addr1_16(netif_ip4_addr(netif)),
572     ip4_addr2_16(netif_ip4_addr(netif)),
573     ip4_addr3_16(netif_ip4_addr(netif)),
574     ip4_addr4_16(netif_ip4_addr(netif))));
575 }
576
577 /**
578  * @ingroup netif_ip4
579  * Change the default gateway for a network interface
580  *
581  * @param netif the network interface to change
582  * @param gw the new default gateway
583  *
584  * @note call netif_set_addr() if you also want to change ip address and netmask
585  */
586 void
587 netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
588 {
589   LWIP_ASSERT_CORE_LOCKED();
590
591   ip4_addr_set(ip_2_ip4(&netif->gw), gw);
592   IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4);
593   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",
594     netif->name[0], netif->name[1],
595     ip4_addr1_16(netif_ip4_gw(netif)),
596     ip4_addr2_16(netif_ip4_gw(netif)),
597     ip4_addr3_16(netif_ip4_gw(netif)),
598     ip4_addr4_16(netif_ip4_gw(netif))));
599 }
600
601 /**
602  * @ingroup netif_ip4
603  * Change the netmask of a network interface
604  *
605  * @param netif the network interface to change
606  * @param netmask the new netmask
607  *
608  * @note call netif_set_addr() if you also want to change ip address and
609  * default gateway
610  */
611 void
612 netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
613 {
614   LWIP_ASSERT_CORE_LOCKED();
615
616   mib2_remove_route_ip4(0, netif);
617   /* set new netmask to netif */
618   ip4_addr_set(ip_2_ip4(&netif->netmask), netmask);
619   IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4);
620   mib2_add_route_ip4(0, netif);
621   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",
622     netif->name[0], netif->name[1],
623     ip4_addr1_16(netif_ip4_netmask(netif)),
624     ip4_addr2_16(netif_ip4_netmask(netif)),
625     ip4_addr3_16(netif_ip4_netmask(netif)),
626     ip4_addr4_16(netif_ip4_netmask(netif))));
627 }
628 #endif /* LWIP_IPV4 */
629
630 /**
631  * @ingroup netif
632  * Set a network interface as the default network interface
633  * (used to output all packets for which no specific route is found)
634  *
635  * @param netif the default network interface
636  */
637 void
638 netif_set_default(struct netif *netif)
639 {
640   LWIP_ASSERT_CORE_LOCKED();
641
642   if (netif == NULL) {
643     /* remove default route */
644     mib2_remove_route_ip4(1, netif);
645   } else {
646     /* install default route */
647     mib2_add_route_ip4(1, netif);
648   }
649   netif_default = netif;
650   LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
651            netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
652 }
653
654 /**
655  * @ingroup netif
656  * Bring an interface up, available for processing
657  * traffic.
658  */
659 void
660 netif_set_up(struct netif *netif)
661 {
662   LWIP_ASSERT_CORE_LOCKED();
663
664   if (!(netif->flags & NETIF_FLAG_UP)) {
665     netif->flags |= NETIF_FLAG_UP;
666
667     MIB2_COPY_SYSUPTIME_TO(&netif->ts);
668
669     NETIF_STATUS_CALLBACK(netif);
670
671     if (netif->flags & NETIF_FLAG_LINK_UP) {
672       netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
673     }
674   }
675 }
676
677 /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
678  */
679 static void
680 netif_issue_reports(struct netif* netif, u8_t report_type)
681 {
682 #if LWIP_IPV4
683   if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
684       !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
685 #if LWIP_ARP
686     /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
687     if (netif->flags & (NETIF_FLAG_ETHARP)) {
688       etharp_gratuitous(netif);
689     }
690 #endif /* LWIP_ARP */
691
692 #if LWIP_IGMP
693     /* resend IGMP memberships */
694     if (netif->flags & NETIF_FLAG_IGMP) {
695       igmp_report_groups(netif);
696     }
697 #endif /* LWIP_IGMP */
698   }
699 #endif /* LWIP_IPV4 */
700
701 #if LWIP_IPV6
702   if (report_type & NETIF_REPORT_TYPE_IPV6) {
703 #if LWIP_IPV6_MLD
704     /* send mld memberships */
705     mld6_report_groups(netif);
706 #endif /* LWIP_IPV6_MLD */
707 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
708     /* Send Router Solicitation messages. */
709     netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
710 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
711   }
712 #endif /* LWIP_IPV6 */
713 }
714
715 /**
716  * @ingroup netif
717  * Bring an interface down, disabling any traffic processing.
718  */
719 void
720 netif_set_down(struct netif *netif)
721 {
722   LWIP_ASSERT_CORE_LOCKED();
723
724   if (netif->flags & NETIF_FLAG_UP) {
725     netif->flags &= ~NETIF_FLAG_UP;
726     MIB2_COPY_SYSUPTIME_TO(&netif->ts);
727
728 #if LWIP_IPV4 && LWIP_ARP
729     if (netif->flags & NETIF_FLAG_ETHARP) {
730       etharp_cleanup_netif(netif);
731     }
732 #endif /* LWIP_IPV4 && LWIP_ARP */
733
734 #if LWIP_IPV6 && LWIP_IPV6_ND
735     nd6_cleanup_netif(netif);
736 #endif /* LWIP_IPV6 && LWIP_IPV6_ND */
737
738     NETIF_STATUS_CALLBACK(netif);
739   }
740 }
741
742 #if LWIP_NETIF_STATUS_CALLBACK
743 /**
744  * @ingroup netif
745  * Set callback to be called when interface is brought up/down or address is changed while up
746  */
747 void
748 netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
749 {
750   LWIP_ASSERT_CORE_LOCKED();
751
752   if (netif) {
753     netif->status_callback = status_callback;
754   }
755 }
756 #endif /* LWIP_NETIF_STATUS_CALLBACK */
757
758 #if LWIP_NETIF_REMOVE_CALLBACK
759 /**
760  * @ingroup netif
761  * Set callback to be called when the interface has been removed
762  */
763 void
764 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
765 {
766   if (netif) {
767     netif->remove_callback = remove_callback;
768   }
769 }
770 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
771
772 /**
773  * @ingroup netif
774  * Called by a driver when its link goes up
775  */
776 void
777 netif_set_link_up(struct netif *netif)
778 {
779   LWIP_ASSERT_CORE_LOCKED();
780
781   if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
782     netif->flags |= NETIF_FLAG_LINK_UP;
783
784 #if LWIP_DHCP
785     dhcp_network_changed(netif);
786 #endif /* LWIP_DHCP */
787
788 #if LWIP_AUTOIP
789     autoip_network_changed(netif);
790 #endif /* LWIP_AUTOIP */
791
792     if (netif->flags & NETIF_FLAG_UP) {
793       netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
794     }
795     NETIF_LINK_CALLBACK(netif);
796   }
797 }
798
799 /**
800  * @ingroup netif
801  * Called by a driver when its link goes down
802  */
803 void
804 netif_set_link_down(struct netif *netif )
805 {
806   LWIP_ASSERT_CORE_LOCKED();
807
808   if (netif->flags & NETIF_FLAG_LINK_UP) {
809     netif->flags &= ~NETIF_FLAG_LINK_UP;
810     NETIF_LINK_CALLBACK(netif);
811   }
812 }
813
814 #if LWIP_NETIF_LINK_CALLBACK
815 /**
816  * @ingroup netif
817  * Set callback to be called when link is brought up/down
818  */
819 void
820 netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
821 {
822   LWIP_ASSERT_CORE_LOCKED();
823
824   if (netif) {
825     netif->link_callback = link_callback;
826   }
827 }
828 #endif /* LWIP_NETIF_LINK_CALLBACK */
829
830 #if ENABLE_LOOPBACK
831 /**
832  * @ingroup netif
833  * Send an IP packet to be received on the same netif (loopif-like).
834  * The pbuf is simply copied and handed back to netif->input.
835  * In multithreaded mode, this is done directly since netif->input must put
836  * the packet on a queue.
837  * In callback mode, the packet is put on an internal queue and is fed to
838  * netif->input by netif_poll().
839  *
840  * @param netif the lwip network interface structure
841  * @param p the (IP) packet to 'send'
842  * @return ERR_OK if the packet has been sent
843  *         ERR_MEM if the pbuf used to copy the packet couldn't be allocated
844  */
845 err_t
846 netif_loop_output(struct netif *netif, struct pbuf *p)
847 {
848   struct pbuf *r;
849   err_t err;
850   struct pbuf *last;
851 #if LWIP_LOOPBACK_MAX_PBUFS
852   u16_t clen = 0;
853 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
854   /* If we have a loopif, SNMP counters are adjusted for it,
855    * if not they are adjusted for 'netif'. */
856 #if MIB2_STATS
857 #if LWIP_HAVE_LOOPIF
858   struct netif *stats_if = &loop_netif;
859 #else /* LWIP_HAVE_LOOPIF */
860   struct netif *stats_if = netif;
861 #endif /* LWIP_HAVE_LOOPIF */
862 #endif /* MIB2_STATS */
863   SYS_ARCH_DECL_PROTECT(lev);
864
865   /* Allocate a new pbuf */
866   r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
867   if (r == NULL) {
868     LINK_STATS_INC(link.memerr);
869     LINK_STATS_INC(link.drop);
870     MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
871     return ERR_MEM;
872   }
873 #if LWIP_LOOPBACK_MAX_PBUFS
874   clen = pbuf_clen(r);
875   /* check for overflow or too many pbuf on queue */
876   if (((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
877      ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
878     pbuf_free(r);
879     LINK_STATS_INC(link.memerr);
880     LINK_STATS_INC(link.drop);
881     MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
882     return ERR_MEM;
883   }
884   netif->loop_cnt_current += clen;
885 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
886
887   /* Copy the whole pbuf queue p into the single pbuf r */
888   if ((err = pbuf_copy(r, p)) != ERR_OK) {
889     pbuf_free(r);
890     LINK_STATS_INC(link.memerr);
891     LINK_STATS_INC(link.drop);
892     MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
893     return err;
894   }
895
896   /* Put the packet on a linked list which gets emptied through calling
897      netif_poll(). */
898
899   /* let last point to the last pbuf in chain r */
900   for (last = r; last->next != NULL; last = last->next) {
901     /* nothing to do here, just get to the last pbuf */
902   }
903
904   SYS_ARCH_PROTECT(lev);
905   if (netif->loop_first != NULL) {
906     LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
907     netif->loop_last->next = r;
908     netif->loop_last = last;
909   } else {
910     netif->loop_first = r;
911     netif->loop_last = last;
912   }
913   SYS_ARCH_UNPROTECT(lev);
914
915   LINK_STATS_INC(link.xmit);
916   MIB2_STATS_NETIF_ADD(stats_if, ifoutoctets, p->tot_len);
917   MIB2_STATS_NETIF_INC(stats_if, ifoutucastpkts);
918
919 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
920   /* For multithreading environment, schedule a call to netif_poll */
921   tcpip_callback_with_block((tcpip_callback_fn)netif_poll, netif, 0);
922 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
923
924   return ERR_OK;
925 }
926
927 #if LWIP_HAVE_LOOPIF
928 #if LWIP_IPV4
929 static err_t
930 netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr)
931 {
932   LWIP_UNUSED_ARG(addr);
933   return netif_loop_output(netif, p);
934 }
935 #endif /* LWIP_IPV4 */
936
937 #if LWIP_IPV6
938 static err_t
939 netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr)
940 {
941   LWIP_UNUSED_ARG(addr);
942   return netif_loop_output(netif, p);
943 }
944 #endif /* LWIP_IPV6 */
945 #endif /* LWIP_HAVE_LOOPIF */
946
947
948 /**
949  * Call netif_poll() in the main loop of your application. This is to prevent
950  * reentering non-reentrant functions like tcp_input(). Packets passed to
951  * netif_loop_output() are put on a list that is passed to netif->input() by
952  * netif_poll().
953  */
954 void
955 netif_poll(struct netif *netif)
956 {
957   /* If we have a loopif, SNMP counters are adjusted for it,
958    * if not they are adjusted for 'netif'. */
959 #if MIB2_STATS
960 #if LWIP_HAVE_LOOPIF
961   struct netif *stats_if = &loop_netif;
962 #else /* LWIP_HAVE_LOOPIF */
963   struct netif *stats_if = netif;
964 #endif /* LWIP_HAVE_LOOPIF */
965 #endif /* MIB2_STATS */
966   SYS_ARCH_DECL_PROTECT(lev);
967
968   /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
969   SYS_ARCH_PROTECT(lev);
970   while (netif->loop_first != NULL) {
971     struct pbuf *in, *in_end;
972 #if LWIP_LOOPBACK_MAX_PBUFS
973     u8_t clen = 1;
974 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
975
976     in = in_end = netif->loop_first;
977     while (in_end->len != in_end->tot_len) {
978       LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
979       in_end = in_end->next;
980 #if LWIP_LOOPBACK_MAX_PBUFS
981       clen++;
982 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
983     }
984 #if LWIP_LOOPBACK_MAX_PBUFS
985     /* adjust the number of pbufs on queue */
986     LWIP_ASSERT("netif->loop_cnt_current underflow",
987       ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
988     netif->loop_cnt_current -= clen;
989 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
990
991     /* 'in_end' now points to the last pbuf from 'in' */
992     if (in_end == netif->loop_last) {
993       /* this was the last pbuf in the list */
994       netif->loop_first = netif->loop_last = NULL;
995     } else {
996       /* pop the pbuf off the list */
997       netif->loop_first = in_end->next;
998       LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
999     }
1000     /* De-queue the pbuf from its successors on the 'loop_' list. */
1001     in_end->next = NULL;
1002     SYS_ARCH_UNPROTECT(lev);
1003
1004     LINK_STATS_INC(link.recv);
1005     MIB2_STATS_NETIF_ADD(stats_if, ifinoctets, in->tot_len);
1006     MIB2_STATS_NETIF_INC(stats_if, ifinucastpkts);
1007     /* loopback packets are always IP packets! */
1008     if (ip_input(in, netif) != ERR_OK) {
1009       pbuf_free(in);
1010     }
1011     SYS_ARCH_PROTECT(lev);
1012   }
1013   SYS_ARCH_UNPROTECT(lev);
1014 }
1015
1016 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
1017 /**
1018  * Calls netif_poll() for every netif on the netif_list.
1019  */
1020 void
1021 netif_poll_all(void)
1022 {
1023   struct netif *netif = netif_list;
1024   /* loop through netifs */
1025   while (netif != NULL) {
1026     netif_poll(netif);
1027     /* proceed to next network interface */
1028     netif = netif->next;
1029   }
1030 }
1031 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
1032 #endif /* ENABLE_LOOPBACK */
1033
1034 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
1035 /**
1036  * @ingroup netif_cd
1037  * Allocate an index to store data in client_data member of struct netif.
1038  * Returned value is an index in mentioned array.
1039  * @see LWIP_NUM_NETIF_CLIENT_DATA
1040  */
1041 u8_t
1042 netif_alloc_client_data_id(void)
1043 {
1044   u8_t result = netif_client_id;
1045   netif_client_id++;
1046
1047   LWIP_ASSERT_CORE_LOCKED();
1048
1049   LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", result < LWIP_NUM_NETIF_CLIENT_DATA);
1050   return result + LWIP_NETIF_CLIENT_DATA_INDEX_MAX;
1051 }
1052 #endif
1053
1054 #if LWIP_IPV6
1055 /**
1056  * @ingroup netif_ip6
1057  * Change an IPv6 address of a network interface
1058  *
1059  * @param netif the network interface to change
1060  * @param addr_idx index of the IPv6 address
1061  * @param addr6 the new IPv6 address
1062  *
1063  * @note call netif_ip6_addr_set_state() to set the address valid/temptative
1064  */
1065 void
1066 netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6)
1067 {
1068   LWIP_ASSERT("addr6 != NULL", addr6 != NULL);
1069
1070   LWIP_ASSERT_CORE_LOCKED();
1071
1072   netif_ip6_addr_set_parts(netif, addr_idx, addr6->addr[0], addr6->addr[1],
1073     addr6->addr[2], addr6->addr[3]);
1074 }
1075
1076 /*
1077  * Change an IPv6 address of a network interface (internal version taking 4 * u32_t)
1078  *
1079  * @param netif the network interface to change
1080  * @param addr_idx index of the IPv6 address
1081  * @param i0 word0 of the new IPv6 address
1082  * @param i1 word1 of the new IPv6 address
1083  * @param i2 word2 of the new IPv6 address
1084  * @param i3 word3 of the new IPv6 address
1085  */
1086 void
1087 netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3)
1088 {
1089   const ip6_addr_t *old_addr;
1090   LWIP_ASSERT_CORE_LOCKED();
1091   LWIP_ASSERT("netif != NULL", netif != NULL);
1092   LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1093
1094   old_addr = netif_ip6_addr(netif, addr_idx);
1095   /* address is actually being changed? */
1096   if ((old_addr->addr[0] != i0) || (old_addr->addr[1] != i1) ||
1097       (old_addr->addr[2] != i2) || (old_addr->addr[3] != i3)) {
1098     LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set: netif address being changed\n"));
1099
1100     if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
1101 #if LWIP_TCP || LWIP_UDP
1102       ip_addr_t new_ipaddr;
1103       IP_ADDR6(&new_ipaddr, i0, i1, i2, i3);
1104 #endif /* LWIP_TCP || LWIP_UDP */
1105 #if LWIP_TCP
1106       tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1107 #endif /* LWIP_TCP */
1108 #if LWIP_UDP
1109       udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1110 #endif /* LWIP_UDP */
1111 #if LWIP_RAW
1112       raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1113 #endif /* LWIP_RAW */
1114     }
1115     /* @todo: remove/readd mib2 ip6 entries? */
1116
1117     IP6_ADDR(ip_2_ip6(&(netif->ip6_addr[addr_idx])), i0, i1, i2, i3);
1118     IP_SET_TYPE_VAL(netif->ip6_addr[addr_idx], IPADDR_TYPE_V6);
1119
1120     if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
1121       netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1122       NETIF_STATUS_CALLBACK(netif);
1123     }
1124   }
1125
1126   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",
1127     addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1128     netif_ip6_addr_state(netif, addr_idx)));
1129 }
1130
1131 /**
1132  * @ingroup netif_ip6
1133  * Change the state of an IPv6 address of a network interface
1134  * (INVALID, TEMPTATIVE, PREFERRED, DEPRECATED, where TEMPTATIVE
1135  * includes the number of checks done, see ip6_addr.h)
1136  *
1137  * @param netif the network interface to change
1138  * @param addr_idx index of the IPv6 address
1139  * @param state the new IPv6 address state
1140  */
1141 void
1142 netif_ip6_addr_set_state(struct netif* netif, s8_t addr_idx, u8_t state)
1143 {
1144   u8_t old_state;
1145   LWIP_ASSERT_CORE_LOCKED();
1146   LWIP_ASSERT("netif != NULL", netif != NULL);
1147   LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1148
1149   old_state = netif_ip6_addr_state(netif, addr_idx);
1150   /* state is actually being changed? */
1151   if (old_state != state) {
1152     u8_t old_valid = old_state & IP6_ADDR_VALID;
1153     u8_t new_valid = state & IP6_ADDR_VALID;
1154     LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set_state: netif address state being changed\n"));
1155
1156 #if LWIP_IPV6_MLD
1157     /* Reevaluate solicited-node multicast group membership. */
1158     if (netif->flags & NETIF_FLAG_MLD6) {
1159       nd6_adjust_mld_membership(netif, addr_idx, state);
1160     }
1161 #endif /* LWIP_IPV6_MLD */
1162
1163     if (old_valid && !new_valid) {
1164       /* address about to be removed by setting invalid */
1165 #if LWIP_TCP
1166       tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1167 #endif /* LWIP_TCP */
1168 #if LWIP_UDP
1169       udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1170 #endif /* LWIP_UDP */
1171 #if LWIP_RAW
1172       raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1173 #endif /* LWIP_RAW */
1174       /* @todo: remove mib2 ip6 entries? */
1175     }
1176     netif->ip6_addr_state[addr_idx] = state;
1177
1178     if (!old_valid && new_valid) {
1179       /* address added by setting valid */
1180       /* @todo: add mib2 ip6 entries? */
1181       netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1182     }
1183     if ((old_state & IP6_ADDR_PREFERRED) != (state & IP6_ADDR_PREFERRED)) {
1184       /* address state has changed (valid flag changed or switched between
1185          preferred and deprecated) -> call the callback function */
1186       NETIF_STATUS_CALLBACK(netif);
1187     }
1188   }
1189
1190   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",
1191     addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1192     netif_ip6_addr_state(netif, addr_idx)));
1193 }
1194
1195 /**
1196  * Checks if a specific local address is present on the netif and returns its
1197  * index. Depending on its state, it may or may not be assigned to the
1198  * interface (as per RFC terminology).
1199  *
1200  * @param netif the netif to check
1201  * @param ip6addr the IPv6 address to find
1202  * @return >= 0: address found, this is its index
1203  *         -1: address not found on this netif
1204  */
1205 s8_t
1206 netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
1207 {
1208   s8_t i;
1209
1210   LWIP_ASSERT_CORE_LOCKED();
1211
1212   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1213     if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
1214         ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
1215       return i;
1216     }
1217   }
1218   return -1;
1219 }
1220
1221 /**
1222  * @ingroup netif_ip6
1223  * Create a link-local IPv6 address on a netif (stored in slot 0)
1224  *
1225  * @param netif the netif to create the address on
1226  * @param from_mac_48bit if != 0, assume hwadr is a 48-bit MAC address (std conversion)
1227  *                       if == 0, use hwaddr directly as interface ID
1228  *
1229  * @note
1230  *  Nest has changed this interface so that if from_mac_48bit == 0, then it means the
1231  *  hwaddr is an EUI64 that is to be subject to the standard conversion.
1232  */
1233 void
1234 netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
1235 {
1236   u8_t i, addr_index;
1237
1238   LWIP_ASSERT_CORE_LOCKED();
1239
1240   /* Link-local prefix. */
1241   ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
1242   ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
1243
1244   /* Generate interface ID. */
1245   if (from_mac_48bit) {
1246     /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
1247     ip_2_ip6(&netif->ip6_addr[0])->addr[2] = lwip_htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
1248         ((u32_t)(netif->hwaddr[1]) << 16) |
1249         ((u32_t)(netif->hwaddr[2]) << 8) |
1250         (0xff));
1251     ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((0xfeul << 24) |
1252         ((u32_t)(netif->hwaddr[3]) << 16) |
1253         ((u32_t)(netif->hwaddr[4]) << 8) |
1254         (netif->hwaddr[5]));
1255   } else {
1256     /* Use hwaddr directly as interface ID. */
1257     ip_2_ip6(&netif->ip6_addr[0])->addr[2] = 0;
1258     ip_2_ip6(&netif->ip6_addr[0])->addr[3] = 0;
1259
1260     memcpy((void*)&ip_2_ip6(&netif->ip6_addr[0])->addr[2], netif->hwaddr, netif->hwaddr_len);
1261     ip_2_ip6(&netif->ip6_addr[0])->addr[2] ^= PP_HTONL(0x02000000ul);
1262   }
1263
1264   /* Set address state. */
1265 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
1266   /* Will perform duplicate address detection (DAD). */
1267   netif_ip6_addr_set_state(netif, 0, IP6_ADDR_TENTATIVE);
1268 #else /* !LWIP_IPV6_DUP_DETECT_ATTEMPTS */
1269   /* Consider address valid. */
1270   netif_ip6_addr_set_state(netif, 0, IP6_ADDR_PREFERRED);
1271 #endif /* !LWIP_IPV6_DUP_DETECT_ATTEMPTS */
1272 }
1273
1274 /**
1275  * This function allows for the addition of a new IPv6 address to an interface
1276  * along with a prefix len to add a route to the routing table.
1277  *
1278  * @param netif netif to add the address on
1279  * @param ip6addr address to add
1280  * @prefix_len the prefix length for the route corresponding to the address
1281  * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
1282  */
1283 err_t
1284 netif_add_ip6_address_with_route(struct netif *netif, ip6_addr_t *ip6addr, 
1285                                  u8_t prefix_len, s8_t *chosen_idx)
1286 {
1287   s8_t retval = ERR_OK;
1288 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
1289   struct ip6_prefix ip6_pref;
1290   
1291   if (!ip6_prefix_valid(prefix_len)) {
1292     retval = ERR_ARG;
1293     goto fail;
1294   }
1295 #else
1296   if (prefix_len != 64 && prefix_len != 128) {
1297     retval = ERR_ARG;
1298     goto fail;
1299   }
1300 #endif
1301
1302   if ((retval = netif_add_ip6_address(netif, ip6addr, chosen_idx)) !=
1303                 ERR_OK) {
1304     goto fail;
1305   }
1306
1307 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
1308   /* Add a route in routing table for the prefix len. Ignore adding route if
1309    * prefix_len is zero(default route) or prefix_len is 128(host route) 
1310    */
1311   if (prefix_len > 0 && prefix_len < IP6_MAX_PREFIX_LEN) {
1312     ip6_addr_copy(ip6_pref.addr, *ip6addr);
1313     ip6_pref.prefix_len = prefix_len;
1314     if ((retval = ip6_add_route_entry(&ip6_pref, netif, NULL, NULL)) != 
1315                   ERR_OK) {
1316       goto fail;
1317     }
1318   }
1319 #endif /* LWIP_IPV6_ROUTE_TABLE_SUPPORT */
1320
1321 fail:
1322   return retval;
1323 }
1324
1325 /**
1326  * This function allows for the removal of an IPv6 address from an interface
1327  * as well as any routes associated with it.
1328  *
1329  * @param netif netif on which the address is assigned
1330  * @param ip6addr address to remove
1331  * @prefix_len the prefix length for the route corresponding to the address
1332  */
1333 err_t
1334 netif_remove_ip6_address_with_route(struct netif *netif, ip6_addr_t *ip6addr, 
1335                                     u8_t prefix_len)
1336 {
1337   s8_t retval = ERR_OK;
1338 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
1339   struct ip6_prefix ip6_pref;
1340   
1341   if (!ip6_prefix_valid(prefix_len)) {
1342     retval = ERR_ARG;
1343     goto fail;
1344   }
1345 #else
1346   if (prefix_len != 64 && prefix_len != 128) {
1347     retval = ERR_ARG;
1348     goto fail;
1349   }
1350 #endif /* LWIP_IPV6_ROUTE_TABLE_SUPPORT */
1351
1352   if ((retval = netif_remove_ip6_address(netif, ip6addr)) != ERR_OK) {
1353       goto fail;
1354   }
1355
1356 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
1357   /* Remove the route in routing table for the prefix len */
1358   ip6_addr_copy(ip6_pref.addr, *ip6addr);
1359   ip6_pref.prefix_len = prefix_len;
1360   ip6_remove_route_entry(&ip6_pref);
1361 #endif /* LWIP_IPV6_ROUTE_TABLE_SUPPORT */
1362
1363 fail:
1364   return retval;
1365 }
1366
1367 /**
1368  * @ingroup netif_ip6
1369  * This function allows for the easy addition of a new IPv6 address to an interface.
1370  * It takes care of finding an empty slot and then sets the address tentative
1371  * (to make sure that all the subsequent processing happens).
1372  *
1373  * @param netif netif to add the address on
1374  * @param ip6addr address to add
1375  * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
1376  */
1377 err_t
1378 netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
1379 {
1380   s8_t i;
1381
1382   LWIP_ASSERT_CORE_LOCKED();
1383
1384   i = netif_get_ip6_addr_match(netif, ip6addr);
1385   if (i >= 0) {
1386     /* Address already added */
1387     if (chosen_idx != NULL) {
1388       *chosen_idx = i;
1389     }
1390     return ERR_OK;
1391   }
1392
1393   /* Find a free slot -- musn't be the first one (reserved for link local) */
1394   for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1395     if (ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) {
1396       ip_addr_copy_from_ip6(netif->ip6_addr[i], *ip6addr);
1397       netif_ip6_addr_set_state(netif, i, IP6_ADDR_TENTATIVE);
1398       if (chosen_idx != NULL) {
1399         *chosen_idx = i;
1400       }
1401       return ERR_OK;
1402     }
1403   }
1404
1405   if (chosen_idx != NULL) {
1406     *chosen_idx = -1;
1407   }
1408   return ERR_VAL;
1409 }
1410
1411 /** This function allows for the easy removal of an IPv6 address from an interface.
1412  *
1413  * @param netif netif on which the address is assigned
1414  * @param ip6addr address to remove
1415  */
1416 err_t
1417 netif_remove_ip6_address(struct netif *netif, ip6_addr_t *ip6addr)
1418 {
1419   s8_t i;
1420
1421   i = netif_get_ip6_addr_match(netif, ip6addr);
1422   if (i >= 0) {
1423     ip6_addr_set_zero(ip_2_ip6(&netif->ip6_addr[i]));
1424     netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
1425     return ERR_OK;
1426   }
1427
1428   return ERR_VAL;
1429 }
1430
1431 static err_t
1432 netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
1433 {
1434   LWIP_UNUSED_ARG(netif);
1435   LWIP_UNUSED_ARG(p);
1436   LWIP_UNUSED_ARG(ipaddr);
1437
1438   return ERR_IF;
1439 }
1440 #endif /* LWIP_IPV6 */
1441
1442 /**
1443 * @ingroup netif_if
1444 * Return the interface index for the netif with name
1445 * or 0 (invalid interface) if not found/on error
1446 *
1447 * @param name the name of the netif
1448 */
1449 u8_t
1450 netif_name_to_index(const char *name)
1451 {
1452   struct netif *netif = netif_find(name);
1453   if (netif != NULL) {
1454     return netif_get_index(netif);
1455   }
1456   /* No name found, return invalid index */
1457   return 0;
1458 }
1459
1460 /**
1461 * @ingroup netif_if
1462 * Return the interface name for the netif matching index
1463 * or NULL if not found/on error
1464 *
1465 * @param index the interface index of the netif
1466 * @param name char buffer of at least IF_NAMESIZE bytes
1467 */
1468 char *
1469 netif_index_to_name(u8_t index, char *name)
1470 {
1471   struct netif *curif = netif_list;
1472
1473   LWIP_ASSERT_CORE_LOCKED();
1474
1475   u8_t num;
1476
1477   LWIP_ASSERT_CORE_LOCKED();
1478
1479   if (index == 0) {
1480     return NULL; /* indexes start at 1 */
1481   }
1482   num = netif_index_to_num(index);
1483
1484   /* find netif from num */
1485   while (curif != NULL) {
1486     if (curif->num == num) {
1487       name[0] = curif->name[0];
1488       name[1] = curif->name[1];
1489       lwip_itoa(&name[2], IF_NAMESIZE - 2, num);
1490       return name;
1491     }
1492     curif = curif->next;
1493   }
1494   return NULL;
1495 }
1496
1497 /**
1498 * @ingroup netif
1499 * Return the interface for the netif index
1500 *
1501 * @param index index of netif to find
1502 */
1503 struct netif*
1504 netif_get_by_index(u8_t index)
1505 {
1506   struct netif* netif;
1507
1508   if (index != NETIF_NO_INDEX) {
1509     for (netif = netif_list; netif != NULL; netif = netif->next) {
1510       if (index == netif_get_index(netif)) {
1511         return netif; /* found! */
1512       }
1513     }
1514   }
1515
1516   return NULL;   
1517 }