3 * lwIP network interface abstraction
5 * @defgroup netif Network interface (NETIF)
6 * @ingroup callbackstyle_api
8 * @defgroup netif_ip4 IPv4 address handling
11 * @defgroup netif_ip6 IPv6 address handling
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
21 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
22 * All rights reserved.
24 * Redistribution and use in source and binary forms, with or without modification,
25 * are permitted provided that the following conditions are met:
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.
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
46 * This file is part of the lwIP TCP/IP stack.
48 * Author: Adam Dunkels <adam@sics.se>
56 #include "lwip/ip_addr.h"
57 #include "lwip/ip6_addr.h"
58 #include "lwip/netif.h"
59 #include "lwip/priv/tcp_priv.h"
62 #include "lwip/snmp.h"
63 #include "lwip/igmp.h"
64 #include "lwip/etharp.h"
65 #include "lwip/stats.h"
70 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
71 #include "lwip/tcpip.h"
72 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
73 #endif /* ENABLE_LOOPBACK */
75 #include "netif/ethernet.h"
78 #include "lwip/autoip.h"
79 #endif /* LWIP_AUTOIP */
81 #include "lwip/dhcp.h"
82 #endif /* LWIP_DHCP */
84 #include "lwip/dhcp6.h"
85 #endif /* LWIP_IPV6_DHCP6 */
87 #include "lwip/mld6.h"
88 #endif /* LWIP_IPV6_MLD */
93 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
94 #include "lwip/ip6_route_table.h"
95 #endif /* LWIP_IPV6_ROUTE_TABLE_SUPPORT */
97 #if LWIP_NETIF_STATUS_CALLBACK
98 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
100 #define NETIF_STATUS_CALLBACK(n)
101 #endif /* LWIP_NETIF_STATUS_CALLBACK */
103 #if LWIP_NETIF_LINK_CALLBACK
104 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
106 #define NETIF_LINK_CALLBACK(n)
107 #endif /* LWIP_NETIF_LINK_CALLBACK */
109 struct netif *netif_list;
110 struct netif *netif_default;
112 #define netif_index_to_num(index) ((index) - 1)
113 static u8_t netif_num;
115 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
116 static u8_t netif_client_id;
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);
124 static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
125 #endif /* LWIP_IPV6 */
129 static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr);
132 static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr);
136 static struct netif loop_netif;
139 * Initialize a lwip network interface structure for a loopback interface
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
146 netif_loopif_init(struct netif *netif)
148 /* initialize the snmp variables and counters inside the struct netif
149 * ifSpeed: no assumption can be made!
151 MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
153 netif->name[0] = 'l';
154 netif->name[1] = 'o';
156 netif->output = netif_loop_output_ipv4;
159 netif->output_ip6 = netif_loop_output_ipv6;
161 #if LWIP_LOOPIF_MULTICAST
162 netif->flags |= NETIF_FLAG_IGMP;
166 #endif /* LWIP_HAVE_LOOPIF */
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 */
183 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
185 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
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 */
193 netif_set_link_up(&loop_netif);
194 netif_set_up(&loop_netif);
196 #endif /* LWIP_HAVE_LOOPIF */
200 netif_apply_pcb(struct netif *netif, struct ip_pcb *pcb)
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;
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
214 * Only works if the netif driver correctly sets
215 * NETIF_FLAG_ETHARP and/or NETIF_FLAG_ETHERNET flag!
218 netif_input(struct pbuf *p, struct netif *inp)
220 LWIP_ASSERT_CORE_LOCKED();
223 if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
224 return ethernet_input(p, inp);
226 #endif /* LWIP_ETHERNET */
227 return ip_input(p, inp);
232 * Add a network interface to the list of lwIP netifs.
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).
255 * @return netif, or NULL if failed.
258 netif_add(struct netif *netif,
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)
268 LWIP_ASSERT_CORE_LOCKED();
270 LWIP_ASSERT("No init function given", init != NULL);
272 /* reset new interface configuration state */
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 */
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 */
287 netif->output_ip6 = netif_null_output_ip6;
288 #endif /* LWIP_IPV6 */
289 NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
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 */
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 */
314 netif->loop_first = NULL;
315 netif->loop_last = NULL;
316 #endif /* ENABLE_LOOPBACK */
318 /* remember netif specific state information data */
319 netif->state = state;
320 netif->num = netif_num++;
321 netif->input = input;
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 */
329 netif_set_addr(netif, ipaddr, netmask, gw);
330 #endif /* LWIP_IPV4 */
332 /* call user specified initialization function for netif */
333 if (init(netif) != ERR_OK) {
337 /* add this netif to the list */
338 netif->next = netif_list;
340 mib2_netif_added(netif);
343 /* start IGMP processing */
344 if (netif->flags & NETIF_FLAG_IGMP) {
347 #endif /* LWIP_IGMP */
349 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
350 netif->name[0], netif->name[1]));
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"));
366 * Change IP address configuration for a network interface (including netmask
367 * and default gateway).
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
375 netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
376 const ip4_addr_t *gw)
378 LWIP_ASSERT_CORE_LOCKED();
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);
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);
393 #endif /* LWIP_IPV4*/
397 * Remove a network interface from the list of lwIP netifs.
399 * @param netif the network interface to remove
402 netif_remove(struct netif *netif)
408 LWIP_ASSERT_CORE_LOCKED();
415 if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
417 tcp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
418 #endif /* LWIP_TCP */
420 udp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
421 #endif /* LWIP_UDP */
423 raw_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
424 #endif /* LWIP_RAW */
428 /* stop IGMP processing */
429 if (netif->flags & NETIF_FLAG_IGMP) {
432 #endif /* LWIP_IGMP */
433 #endif /* LWIP_IPV4*/
436 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
437 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
439 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
440 #endif /* LWIP_TCP */
442 udp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
443 #endif /* LWIP_UDP */
445 raw_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
446 #endif /* LWIP_RAW */
450 /* stop MLD processing */
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);
459 mib2_remove_ip4(netif);
461 /* this netif is default? */
462 if (netif_default == netif) {
463 /* reset default netif */
464 netif_set_default(NULL);
466 /* is it the first netif? */
467 if (netif_list == netif) {
468 netif_list = netif->next;
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;
478 if (tmp_netif == NULL) {
479 return; /* netif is not on the list */
482 mib2_netif_removed(netif);
483 #if LWIP_NETIF_REMOVE_CALLBACK
484 if (netif->remove_callback) {
485 netif->remove_callback(netif);
487 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
488 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
493 * Find a network interface by searching for its name
495 * @param name the name of the netif (like netif->name) plus concatenated number
496 * in ascii representation (e.g. 'en0')
499 netif_find(const char *name)
508 num = (u8_t)(name[2] - '0');
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]));
518 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
525 * Change the IP address of a network interface
527 * @param netif the network interface to change
528 * @param ipaddr the new IP address
530 * @note call netif_set_addr() if you also want to change netmask and
534 netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
538 LWIP_ASSERT_CORE_LOCKED();
540 *ip_2_ip4(&new_addr) = (ipaddr ? *ipaddr : *IP4_ADDR_ANY4);
541 IP_SET_TYPE_VAL(new_addr, IPADDR_TYPE_V4);
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"));
547 tcp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
548 #endif /* LWIP_TCP */
550 udp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
551 #endif /* LWIP_UDP */
553 raw_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
554 #endif /* LWIP_RAW */
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);
562 mib2_add_route_ip4(0, netif);
564 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
566 NETIF_STATUS_CALLBACK(netif);
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))));
579 * Change the default gateway for a network interface
581 * @param netif the network interface to change
582 * @param gw the new default gateway
584 * @note call netif_set_addr() if you also want to change ip address and netmask
587 netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
589 LWIP_ASSERT_CORE_LOCKED();
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))));
603 * Change the netmask of a network interface
605 * @param netif the network interface to change
606 * @param netmask the new netmask
608 * @note call netif_set_addr() if you also want to change ip address and
612 netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
614 LWIP_ASSERT_CORE_LOCKED();
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))));
628 #endif /* LWIP_IPV4 */
632 * Set a network interface as the default network interface
633 * (used to output all packets for which no specific route is found)
635 * @param netif the default network interface
638 netif_set_default(struct netif *netif)
640 LWIP_ASSERT_CORE_LOCKED();
643 /* remove default route */
644 mib2_remove_route_ip4(1, netif);
646 /* install default route */
647 mib2_add_route_ip4(1, netif);
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] : '\''));
656 * Bring an interface up, available for processing
660 netif_set_up(struct netif *netif)
662 LWIP_ASSERT_CORE_LOCKED();
664 if (!(netif->flags & NETIF_FLAG_UP)) {
665 netif->flags |= NETIF_FLAG_UP;
667 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
669 NETIF_STATUS_CALLBACK(netif);
671 if (netif->flags & NETIF_FLAG_LINK_UP) {
672 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
677 /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
680 netif_issue_reports(struct netif* netif, u8_t report_type)
683 if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
684 !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
686 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
687 if (netif->flags & (NETIF_FLAG_ETHARP)) {
688 etharp_gratuitous(netif);
690 #endif /* LWIP_ARP */
693 /* resend IGMP memberships */
694 if (netif->flags & NETIF_FLAG_IGMP) {
695 igmp_report_groups(netif);
697 #endif /* LWIP_IGMP */
699 #endif /* LWIP_IPV4 */
702 if (report_type & NETIF_REPORT_TYPE_IPV6) {
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 */
712 #endif /* LWIP_IPV6 */
717 * Bring an interface down, disabling any traffic processing.
720 netif_set_down(struct netif *netif)
722 LWIP_ASSERT_CORE_LOCKED();
724 if (netif->flags & NETIF_FLAG_UP) {
725 netif->flags &= ~NETIF_FLAG_UP;
726 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
728 #if LWIP_IPV4 && LWIP_ARP
729 if (netif->flags & NETIF_FLAG_ETHARP) {
730 etharp_cleanup_netif(netif);
732 #endif /* LWIP_IPV4 && LWIP_ARP */
734 #if LWIP_IPV6 && LWIP_IPV6_ND
735 nd6_cleanup_netif(netif);
736 #endif /* LWIP_IPV6 && LWIP_IPV6_ND */
738 NETIF_STATUS_CALLBACK(netif);
742 #if LWIP_NETIF_STATUS_CALLBACK
745 * Set callback to be called when interface is brought up/down or address is changed while up
748 netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
750 LWIP_ASSERT_CORE_LOCKED();
753 netif->status_callback = status_callback;
756 #endif /* LWIP_NETIF_STATUS_CALLBACK */
758 #if LWIP_NETIF_REMOVE_CALLBACK
761 * Set callback to be called when the interface has been removed
764 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
767 netif->remove_callback = remove_callback;
770 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
774 * Called by a driver when its link goes up
777 netif_set_link_up(struct netif *netif)
779 LWIP_ASSERT_CORE_LOCKED();
781 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
782 netif->flags |= NETIF_FLAG_LINK_UP;
785 dhcp_network_changed(netif);
786 #endif /* LWIP_DHCP */
789 autoip_network_changed(netif);
790 #endif /* LWIP_AUTOIP */
792 if (netif->flags & NETIF_FLAG_UP) {
793 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
795 NETIF_LINK_CALLBACK(netif);
801 * Called by a driver when its link goes down
804 netif_set_link_down(struct netif *netif )
806 LWIP_ASSERT_CORE_LOCKED();
808 if (netif->flags & NETIF_FLAG_LINK_UP) {
809 netif->flags &= ~NETIF_FLAG_LINK_UP;
810 NETIF_LINK_CALLBACK(netif);
814 #if LWIP_NETIF_LINK_CALLBACK
817 * Set callback to be called when link is brought up/down
820 netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
822 LWIP_ASSERT_CORE_LOCKED();
825 netif->link_callback = link_callback;
828 #endif /* LWIP_NETIF_LINK_CALLBACK */
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().
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
846 netif_loop_output(struct netif *netif, struct pbuf *p)
851 #if LWIP_LOOPBACK_MAX_PBUFS
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'. */
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);
865 /* Allocate a new pbuf */
866 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
868 LINK_STATS_INC(link.memerr);
869 LINK_STATS_INC(link.drop);
870 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
873 #if LWIP_LOOPBACK_MAX_PBUFS
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)) {
879 LINK_STATS_INC(link.memerr);
880 LINK_STATS_INC(link.drop);
881 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
884 netif->loop_cnt_current += clen;
885 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
887 /* Copy the whole pbuf queue p into the single pbuf r */
888 if ((err = pbuf_copy(r, p)) != ERR_OK) {
890 LINK_STATS_INC(link.memerr);
891 LINK_STATS_INC(link.drop);
892 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
896 /* Put the packet on a linked list which gets emptied through calling
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 */
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;
910 netif->loop_first = r;
911 netif->loop_last = last;
913 SYS_ARCH_UNPROTECT(lev);
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);
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 */
930 netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr)
932 LWIP_UNUSED_ARG(addr);
933 return netif_loop_output(netif, p);
935 #endif /* LWIP_IPV4 */
939 netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr)
941 LWIP_UNUSED_ARG(addr);
942 return netif_loop_output(netif, p);
944 #endif /* LWIP_IPV6 */
945 #endif /* LWIP_HAVE_LOOPIF */
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
955 netif_poll(struct netif *netif)
957 /* If we have a loopif, SNMP counters are adjusted for it,
958 * if not they are adjusted for 'netif'. */
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);
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
974 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
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
982 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
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 */
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;
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);
1000 /* De-queue the pbuf from its successors on the 'loop_' list. */
1001 in_end->next = NULL;
1002 SYS_ARCH_UNPROTECT(lev);
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) {
1011 SYS_ARCH_PROTECT(lev);
1013 SYS_ARCH_UNPROTECT(lev);
1016 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
1018 * Calls netif_poll() for every netif on the netif_list.
1021 netif_poll_all(void)
1023 struct netif *netif = netif_list;
1024 /* loop through netifs */
1025 while (netif != NULL) {
1027 /* proceed to next network interface */
1028 netif = netif->next;
1031 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
1032 #endif /* ENABLE_LOOPBACK */
1034 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
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
1042 netif_alloc_client_data_id(void)
1044 u8_t result = netif_client_id;
1047 LWIP_ASSERT_CORE_LOCKED();
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;
1056 * @ingroup netif_ip6
1057 * Change an IPv6 address of a network interface
1059 * @param netif the network interface to change
1060 * @param addr_idx index of the IPv6 address
1061 * @param addr6 the new IPv6 address
1063 * @note call netif_ip6_addr_set_state() to set the address valid/temptative
1066 netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6)
1068 LWIP_ASSERT("addr6 != NULL", addr6 != NULL);
1070 LWIP_ASSERT_CORE_LOCKED();
1072 netif_ip6_addr_set_parts(netif, addr_idx, addr6->addr[0], addr6->addr[1],
1073 addr6->addr[2], addr6->addr[3]);
1077 * Change an IPv6 address of a network interface (internal version taking 4 * u32_t)
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
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)
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);
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"));
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 */
1106 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1107 #endif /* LWIP_TCP */
1109 udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1110 #endif /* LWIP_UDP */
1112 raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1113 #endif /* LWIP_RAW */
1115 /* @todo: remove/readd mib2 ip6 entries? */
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);
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);
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)));
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)
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
1142 netif_ip6_addr_set_state(struct netif* netif, s8_t addr_idx, u8_t state)
1145 LWIP_ASSERT_CORE_LOCKED();
1146 LWIP_ASSERT("netif != NULL", netif != NULL);
1147 LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
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"));
1157 /* Reevaluate solicited-node multicast group membership. */
1158 if (netif->flags & NETIF_FLAG_MLD6) {
1159 nd6_adjust_mld_membership(netif, addr_idx, state);
1161 #endif /* LWIP_IPV6_MLD */
1163 if (old_valid && !new_valid) {
1164 /* address about to be removed by setting invalid */
1166 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1167 #endif /* LWIP_TCP */
1169 udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1170 #endif /* LWIP_UDP */
1172 raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1173 #endif /* LWIP_RAW */
1174 /* @todo: remove mib2 ip6 entries? */
1176 netif->ip6_addr_state[addr_idx] = state;
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);
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);
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)));
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).
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
1206 netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
1210 LWIP_ASSERT_CORE_LOCKED();
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)) {
1222 * @ingroup netif_ip6
1223 * Create a link-local IPv6 address on a netif (stored in slot 0)
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
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.
1234 netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
1238 LWIP_ASSERT_CORE_LOCKED();
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;
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) |
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]));
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;
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);
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 */
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.
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
1284 netif_add_ip6_address_with_route(struct netif *netif, ip6_addr_t *ip6addr,
1285 u8_t prefix_len, s8_t *chosen_idx)
1287 s8_t retval = ERR_OK;
1288 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
1289 struct ip6_prefix ip6_pref;
1291 if (!ip6_prefix_valid(prefix_len)) {
1296 if (prefix_len != 64 && prefix_len != 128) {
1302 if ((retval = netif_add_ip6_address(netif, ip6addr, chosen_idx)) !=
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)
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)) !=
1319 #endif /* LWIP_IPV6_ROUTE_TABLE_SUPPORT */
1326 * This function allows for the removal of an IPv6 address from an interface
1327 * as well as any routes associated with it.
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
1334 netif_remove_ip6_address_with_route(struct netif *netif, ip6_addr_t *ip6addr,
1337 s8_t retval = ERR_OK;
1338 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
1339 struct ip6_prefix ip6_pref;
1341 if (!ip6_prefix_valid(prefix_len)) {
1346 if (prefix_len != 64 && prefix_len != 128) {
1350 #endif /* LWIP_IPV6_ROUTE_TABLE_SUPPORT */
1352 if ((retval = netif_remove_ip6_address(netif, ip6addr)) != ERR_OK) {
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 */
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).
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
1378 netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
1382 LWIP_ASSERT_CORE_LOCKED();
1384 i = netif_get_ip6_addr_match(netif, ip6addr);
1386 /* Address already added */
1387 if (chosen_idx != NULL) {
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) {
1405 if (chosen_idx != NULL) {
1411 /** This function allows for the easy removal of an IPv6 address from an interface.
1413 * @param netif netif on which the address is assigned
1414 * @param ip6addr address to remove
1417 netif_remove_ip6_address(struct netif *netif, ip6_addr_t *ip6addr)
1421 i = netif_get_ip6_addr_match(netif, ip6addr);
1423 ip6_addr_set_zero(ip_2_ip6(&netif->ip6_addr[i]));
1424 netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
1432 netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
1434 LWIP_UNUSED_ARG(netif);
1436 LWIP_UNUSED_ARG(ipaddr);
1440 #endif /* LWIP_IPV6 */
1444 * Return the interface index for the netif with name
1445 * or 0 (invalid interface) if not found/on error
1447 * @param name the name of the netif
1450 netif_name_to_index(const char *name)
1452 struct netif *netif = netif_find(name);
1453 if (netif != NULL) {
1454 return netif_get_index(netif);
1456 /* No name found, return invalid index */
1462 * Return the interface name for the netif matching index
1463 * or NULL if not found/on error
1465 * @param index the interface index of the netif
1466 * @param name char buffer of at least IF_NAMESIZE bytes
1469 netif_index_to_name(u8_t index, char *name)
1471 struct netif *curif = netif_list;
1473 LWIP_ASSERT_CORE_LOCKED();
1477 LWIP_ASSERT_CORE_LOCKED();
1480 return NULL; /* indexes start at 1 */
1482 num = netif_index_to_num(index);
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);
1492 curif = curif->next;
1499 * Return the interface for the netif index
1501 * @param index index of netif to find
1504 netif_get_by_index(u8_t index)
1506 struct netif* netif;
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! */