3 * lwIP network interface abstraction
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
42 #include "lwip/ip_addr.h"
43 #include "lwip/ip6_addr.h"
44 #include "lwip/netif.h"
45 #include "lwip/tcp_impl.h"
46 #include "lwip/snmp.h"
47 #include "lwip/igmp.h"
48 #include "netif/etharp.h"
49 #include "lwip/stats.h"
52 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
53 #include "lwip/tcpip.h"
54 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
55 #endif /* ENABLE_LOOPBACK */
58 #include "lwip/autoip.h"
59 #endif /* LWIP_AUTOIP */
61 #include "lwip/dhcp.h"
62 #endif /* LWIP_DHCP */
64 #include "lwip/dhcp6.h"
65 #endif /* LWIP_IPV6_DHCP6 */
67 #include "lwip/mld6.h"
68 #endif /* LWIP_IPV6_MLD */
70 #if LWIP_NETIF_STATUS_CALLBACK
71 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
73 #define NETIF_STATUS_CALLBACK(n)
74 #endif /* LWIP_NETIF_STATUS_CALLBACK */
76 #if LWIP_NETIF_LINK_CALLBACK
77 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
79 #define NETIF_LINK_CALLBACK(n)
80 #endif /* LWIP_NETIF_LINK_CALLBACK */
82 struct netif *netif_list;
83 struct netif *netif_default;
85 static u8_t netif_num;
88 static struct netif loop_netif;
91 * Initialize a lwip network interface structure for a loopback interface
93 * @param netif the lwip network interface structure for this loopif
94 * @return ERR_OK if the loopif is initialized
95 * ERR_MEM if private data couldn't be allocated
98 netif_loopif_init(struct netif *netif)
100 /* initialize the snmp variables and counters inside the struct netif
101 * ifSpeed: no assumption can be made!
103 NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
105 netif->name[0] = 'l';
106 netif->name[1] = 'o';
107 netif->output = netif_loop_output;
110 #endif /* LWIP_HAVE_LOOPIF */
116 ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
117 IP4_ADDR(&loop_gw, 127,0,0,1);
118 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
119 IP4_ADDR(&loop_netmask, 255,0,0,0);
122 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
124 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
126 netif_set_up(&loop_netif);
128 #endif /* LWIP_HAVE_LOOPIF */
132 * Add a network interface to the list of lwIP netifs.
134 * @param netif a pre-allocated netif structure
135 * @param ipaddr IP address for the new netif
136 * @param netmask network mask for the new netif
137 * @param gw default gateway IP address for the new netif
138 * @param state opaque data passed to the new netif
139 * @param init callback function that initializes the interface
140 * @param input callback function that is called to pass
141 * ingress packets up in the protocol layer stack.
143 * @return netif, or NULL if failed.
146 netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
147 ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
153 LWIP_ASSERT("No init function given", init != NULL);
155 /* reset new interface configuration state */
156 ip_addr_set_zero(&netif->ip_addr);
157 ip_addr_set_zero(&netif->netmask);
158 ip_addr_set_zero(&netif->gw);
160 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
161 ip6_addr_set_zero(&netif->ip6_addr[i]);
162 netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
164 #endif /* LWIP_IPV6 */
167 /* netif not under DHCP control by default */
169 #endif /* LWIP_DHCP */
171 /* netif not under AutoIP control by default */
172 netif->autoip = NULL;
173 #endif /* LWIP_AUTOIP */
174 #if LWIP_IPV6_AUTOCONFIG
175 /* IPv6 address autoconfiguration not enabled by default */
176 netif->ip6_autoconfig_enabled = 0;
177 #endif /* LWIP_IPV6_AUTOCONFIG */
178 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
179 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
180 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
182 /* netif not under DHCPv6 control by default */
184 #endif /* LWIP_IPV6_DHCP6 */
185 #if LWIP_NETIF_STATUS_CALLBACK
186 netif->status_callback = NULL;
187 #endif /* LWIP_NETIF_STATUS_CALLBACK */
188 #if LWIP_NETIF_LINK_CALLBACK
189 netif->link_callback = NULL;
190 #endif /* LWIP_NETIF_LINK_CALLBACK */
192 netif->igmp_mac_filter = NULL;
193 #endif /* LWIP_IGMP */
194 #if LWIP_IPV6 && LWIP_IPV6_MLD
195 netif->mld_mac_filter = NULL;
196 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
198 netif->loop_first = NULL;
199 netif->loop_last = NULL;
200 #endif /* ENABLE_LOOPBACK */
202 /* remember netif specific state information data */
203 netif->state = state;
204 netif->num = netif_num++;
205 netif->input = input;
206 NETIF_SET_HWADDRHINT(netif, NULL);
207 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
208 netif->loop_cnt_current = 0;
209 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
211 netif_set_addr(netif, ipaddr, netmask, gw);
213 /* call user specified initialization function for netif */
214 if (init(netif) != ERR_OK) {
218 /* add this netif to the list */
219 netif->next = netif_list;
224 /* start IGMP processing */
225 if (netif->flags & NETIF_FLAG_IGMP) {
228 #endif /* LWIP_IGMP */
230 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
231 netif->name[0], netif->name[1]));
232 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
233 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
234 ip_addr_debug_print(NETIF_DEBUG, netmask);
235 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
236 ip_addr_debug_print(NETIF_DEBUG, gw);
237 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
242 * Change IP address configuration for a network interface (including netmask
243 * and default gateway).
245 * @param netif the network interface to change
246 * @param ipaddr the new IP address
247 * @param netmask the new netmask
248 * @param gw the new default gateway
251 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
254 netif_set_ipaddr(netif, ipaddr);
255 netif_set_netmask(netif, netmask);
256 netif_set_gw(netif, gw);
260 * Remove a network interface from the list of lwIP netifs.
262 * @param netif the network interface to remove
265 netif_remove(struct netif *netif)
272 /* stop IGMP processing */
273 if (netif->flags & NETIF_FLAG_IGMP) {
276 #endif /* LWIP_IGMP */
277 #if LWIP_IPV6 && LWIP_IPV6_MLD
278 /* stop MLD processing */
280 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
281 if (netif_is_up(netif)) {
282 /* set netif down before removing (call callback function) */
283 netif_set_down(netif);
286 snmp_delete_ipaddridx_tree(netif);
288 /* is it the first netif? */
289 if (netif_list == netif) {
290 netif_list = netif->next;
292 /* look for netif further down the list */
293 struct netif * tmpNetif;
294 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
295 if (tmpNetif->next == netif) {
296 tmpNetif->next = netif->next;
300 if (tmpNetif == NULL)
301 return; /* we didn't find any netif today */
304 /* this netif is default? */
305 if (netif_default == netif) {
306 /* reset default netif */
307 netif_set_default(NULL);
309 #if LWIP_NETIF_REMOVE_CALLBACK
310 if (netif->remove_callback) {
311 netif->remove_callback(netif);
313 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
314 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
318 * Find a network interface by searching for its name
320 * @param name the name of the netif (like netif->name) plus concatenated number
321 * in ascii representation (e.g. 'en0')
324 netif_find(char *name)
335 for(netif = netif_list; netif != NULL; netif = netif->next) {
336 if (num == netif->num &&
337 name[0] == netif->name[0] &&
338 name[1] == netif->name[1]) {
339 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
343 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
348 * Change the IP address of a network interface
350 * @param netif the network interface to change
351 * @param ipaddr the new IP address
353 * @note call netif_set_addr() if you also want to change netmask and
357 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
359 /* TODO: Handling of obsolete pcbs */
360 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
363 struct tcp_pcb_listen *lpcb;
365 /* address is actually being changed? */
366 if (ipaddr && (ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
367 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
368 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
369 pcb = tcp_active_pcbs;
370 while (pcb != NULL) {
371 /* PCB bound to current local interface address? */
372 if (ip_addr_cmp(ipX_2_ip(&pcb->local_ip), &(netif->ip_addr))
374 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
375 && !ip_addr_islinklocal(ipX_2_ip(&pcb->local_ip))
376 #endif /* LWIP_AUTOIP */
378 /* this connection must be aborted */
379 struct tcp_pcb *next = pcb->next;
380 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
387 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
388 /* PCB bound to current local interface address? */
389 if ((!(ip_addr_isany(ipX_2_ip(&lpcb->local_ip)))) &&
390 (ip_addr_cmp(ipX_2_ip(&lpcb->local_ip), &(netif->ip_addr)))) {
391 /* The PCB is listening to the old ipaddr and
392 * is set to listen to the new one instead */
393 ip_addr_set(ipX_2_ip(&lpcb->local_ip), ipaddr);
398 snmp_delete_ipaddridx_tree(netif);
399 snmp_delete_iprteidx_tree(0,netif);
400 /* set new IP address to netif */
401 ip_addr_set(&(netif->ip_addr), ipaddr);
402 snmp_insert_ipaddridx_tree(netif);
403 snmp_insert_iprteidx_tree(0,netif);
405 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",
406 netif->name[0], netif->name[1],
407 ip4_addr1_16(&netif->ip_addr),
408 ip4_addr2_16(&netif->ip_addr),
409 ip4_addr3_16(&netif->ip_addr),
410 ip4_addr4_16(&netif->ip_addr)));
414 * Change the default gateway for a network interface
416 * @param netif the network interface to change
417 * @param gw the new default gateway
419 * @note call netif_set_addr() if you also want to change ip address and netmask
422 netif_set_gw(struct netif *netif, ip_addr_t *gw)
424 ip_addr_set(&(netif->gw), gw);
425 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",
426 netif->name[0], netif->name[1],
427 ip4_addr1_16(&netif->gw),
428 ip4_addr2_16(&netif->gw),
429 ip4_addr3_16(&netif->gw),
430 ip4_addr4_16(&netif->gw)));
434 * Change the netmask of a network interface
436 * @param netif the network interface to change
437 * @param netmask the new netmask
439 * @note call netif_set_addr() if you also want to change ip address and
443 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
445 snmp_delete_iprteidx_tree(0, netif);
446 /* set new netmask to netif */
447 ip_addr_set(&(netif->netmask), netmask);
448 snmp_insert_iprteidx_tree(0, netif);
449 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",
450 netif->name[0], netif->name[1],
451 ip4_addr1_16(&netif->netmask),
452 ip4_addr2_16(&netif->netmask),
453 ip4_addr3_16(&netif->netmask),
454 ip4_addr4_16(&netif->netmask)));
458 * Set a network interface as the default network interface
459 * (used to output all packets for which no specific route is found)
461 * @param netif the default network interface
464 netif_set_default(struct netif *netif)
467 /* remove default route */
468 snmp_delete_iprteidx_tree(1, netif);
470 /* install default route */
471 snmp_insert_iprteidx_tree(1, netif);
473 netif_default = netif;
474 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
475 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
479 * Bring an interface up, available for processing
482 * @note: Enabling DHCP on a down interface will make it come
483 * up once configured.
487 void netif_set_up(struct netif *netif)
489 if (!(netif->flags & NETIF_FLAG_UP)) {
490 netif->flags |= NETIF_FLAG_UP;
493 snmp_get_sysuptime(&netif->ts);
494 #endif /* LWIP_SNMP */
496 NETIF_STATUS_CALLBACK(netif);
498 if (netif->flags & NETIF_FLAG_LINK_UP) {
500 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
501 if (netif->flags & (NETIF_FLAG_ETHARP)) {
502 etharp_gratuitous(netif);
504 #endif /* LWIP_ARP */
507 /* resend IGMP memberships */
508 if (netif->flags & NETIF_FLAG_IGMP) {
509 igmp_report_groups( netif);
511 #endif /* LWIP_IGMP */
512 #if LWIP_IPV6 && LWIP_IPV6_MLD
513 /* send mld memberships */
514 mld6_report_groups( netif);
515 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
517 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
518 /* Send Router Solicitation messages. */
519 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
520 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
527 * Bring an interface down, disabling any traffic processing.
529 * @note: Enabling DHCP on a down interface will make it come
530 * up once configured.
534 void netif_set_down(struct netif *netif)
536 if (netif->flags & NETIF_FLAG_UP) {
537 netif->flags &= ~NETIF_FLAG_UP;
539 snmp_get_sysuptime(&netif->ts);
543 if (netif->flags & NETIF_FLAG_ETHARP) {
544 etharp_cleanup_netif(netif);
546 #endif /* LWIP_ARP */
547 NETIF_STATUS_CALLBACK(netif);
551 #if LWIP_NETIF_STATUS_CALLBACK
553 * Set callback to be called when interface is brought up/down
555 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
558 netif->status_callback = status_callback;
561 #endif /* LWIP_NETIF_STATUS_CALLBACK */
563 #if LWIP_NETIF_REMOVE_CALLBACK
565 * Set callback to be called when the interface has been removed
568 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
571 netif->remove_callback = remove_callback;
574 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
577 * Called by a driver when its link goes up
579 void netif_set_link_up(struct netif *netif )
581 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
582 netif->flags |= NETIF_FLAG_LINK_UP;
586 dhcp_network_changed(netif);
588 #endif /* LWIP_DHCP */
592 autoip_network_changed(netif);
594 #endif /* LWIP_AUTOIP */
596 if (netif->flags & NETIF_FLAG_UP) {
598 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
599 if (netif->flags & NETIF_FLAG_ETHARP) {
600 etharp_gratuitous(netif);
602 #endif /* LWIP_ARP */
605 /* resend IGMP memberships */
606 if (netif->flags & NETIF_FLAG_IGMP) {
607 igmp_report_groups( netif);
609 #endif /* LWIP_IGMP */
610 #if LWIP_IPV6 && LWIP_IPV6_MLD
611 /* send mld memberships */
612 mld6_report_groups( netif);
613 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
615 NETIF_LINK_CALLBACK(netif);
620 * Called by a driver when its link goes down
622 void netif_set_link_down(struct netif *netif )
624 if (netif->flags & NETIF_FLAG_LINK_UP) {
625 netif->flags &= ~NETIF_FLAG_LINK_UP;
626 NETIF_LINK_CALLBACK(netif);
630 #if LWIP_NETIF_LINK_CALLBACK
632 * Set callback to be called when link is brought up/down
634 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
637 netif->link_callback = link_callback;
640 #endif /* LWIP_NETIF_LINK_CALLBACK */
644 * Send an IP packet to be received on the same netif (loopif-like).
645 * The pbuf is simply copied and handed back to netif->input.
646 * In multithreaded mode, this is done directly since netif->input must put
647 * the packet on a queue.
648 * In callback mode, the packet is put on an internal queue and is fed to
649 * netif->input by netif_poll().
651 * @param netif the lwip network interface structure
652 * @param p the (IP) packet to 'send'
653 * @param ipaddr the ip address to send the packet to (not used)
654 * @return ERR_OK if the packet has been sent
655 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
658 netif_loop_output(struct netif *netif, struct pbuf *p,
664 #if LWIP_LOOPBACK_MAX_PBUFS
666 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
667 /* If we have a loopif, SNMP counters are adjusted for it,
668 * if not they are adjusted for 'netif'. */
671 struct netif *stats_if = &loop_netif;
672 #else /* LWIP_HAVE_LOOPIF */
673 struct netif *stats_if = netif;
674 #endif /* LWIP_HAVE_LOOPIF */
675 #endif /* LWIP_SNMP */
676 SYS_ARCH_DECL_PROTECT(lev);
677 LWIP_UNUSED_ARG(ipaddr);
679 /* Allocate a new pbuf */
680 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
682 LINK_STATS_INC(link.memerr);
683 LINK_STATS_INC(link.drop);
684 snmp_inc_ifoutdiscards(stats_if);
687 #if LWIP_LOOPBACK_MAX_PBUFS
689 /* check for overflow or too many pbuf on queue */
690 if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
691 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
693 LINK_STATS_INC(link.memerr);
694 LINK_STATS_INC(link.drop);
695 snmp_inc_ifoutdiscards(stats_if);
698 netif->loop_cnt_current += clen;
699 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
701 /* Copy the whole pbuf queue p into the single pbuf r */
702 if ((err = pbuf_copy(r, p)) != ERR_OK) {
704 LINK_STATS_INC(link.memerr);
705 LINK_STATS_INC(link.drop);
706 snmp_inc_ifoutdiscards(stats_if);
710 /* Put the packet on a linked list which gets emptied through calling
713 /* let last point to the last pbuf in chain r */
714 for (last = r; last->next != NULL; last = last->next);
716 SYS_ARCH_PROTECT(lev);
717 if(netif->loop_first != NULL) {
718 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
719 netif->loop_last->next = r;
720 netif->loop_last = last;
722 netif->loop_first = r;
723 netif->loop_last = last;
725 SYS_ARCH_UNPROTECT(lev);
727 LINK_STATS_INC(link.xmit);
728 snmp_add_ifoutoctets(stats_if, p->tot_len);
729 snmp_inc_ifoutucastpkts(stats_if);
731 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
732 /* For multithreading environment, schedule a call to netif_poll */
733 tcpip_callback((tcpip_callback_fn)netif_poll, netif);
734 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
740 * Call netif_poll() in the main loop of your application. This is to prevent
741 * reentering non-reentrant functions like tcp_input(). Packets passed to
742 * netif_loop_output() are put on a list that is passed to netif->input() by
746 netif_poll(struct netif *netif)
749 /* If we have a loopif, SNMP counters are adjusted for it,
750 * if not they are adjusted for 'netif'. */
753 struct netif *stats_if = &loop_netif;
754 #else /* LWIP_HAVE_LOOPIF */
755 struct netif *stats_if = netif;
756 #endif /* LWIP_HAVE_LOOPIF */
757 #endif /* LWIP_SNMP */
758 SYS_ARCH_DECL_PROTECT(lev);
761 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
762 SYS_ARCH_PROTECT(lev);
763 in = netif->loop_first;
765 struct pbuf *in_end = in;
766 #if LWIP_LOOPBACK_MAX_PBUFS
767 u8_t clen = pbuf_clen(in);
768 /* adjust the number of pbufs on queue */
769 LWIP_ASSERT("netif->loop_cnt_current underflow",
770 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
771 netif->loop_cnt_current -= clen;
772 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
773 while (in_end->len != in_end->tot_len) {
774 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
775 in_end = in_end->next;
777 /* 'in_end' now points to the last pbuf from 'in' */
778 if (in_end == netif->loop_last) {
779 /* this was the last pbuf in the list */
780 netif->loop_first = netif->loop_last = NULL;
782 /* pop the pbuf off the list */
783 netif->loop_first = in_end->next;
784 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
786 /* De-queue the pbuf from its successors on the 'loop_' list. */
789 SYS_ARCH_UNPROTECT(lev);
792 LINK_STATS_INC(link.recv);
793 snmp_add_ifinoctets(stats_if, in->tot_len);
794 snmp_inc_ifinucastpkts(stats_if);
795 /* loopback packets are always IP packets! */
796 if (ip_input(in, netif) != ERR_OK) {
799 /* Don't reference the packet any more! */
802 /* go on while there is a packet on the list */
803 } while (netif->loop_first != NULL);
806 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
808 * Calls netif_poll() for every netif on the netif_list.
813 struct netif *netif = netif_list;
814 /* loop through netifs */
815 while (netif != NULL) {
817 /* proceed to next network interface */
821 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
822 #endif /* ENABLE_LOOPBACK */
826 netif_matches_ip6_addr(struct netif * netif, ip6_addr_t * ip6addr)
829 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
830 if (ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
838 netif_create_ip6_linklocal_address(struct netif * netif, u8_t from_mac_48bit)
842 /* Link-local prefix. */
843 netif->ip6_addr[0].addr[0] = PP_HTONL(0xfe800000ul);
844 netif->ip6_addr[0].addr[1] = 0;
846 /* Generate interface ID. */
847 if (from_mac_48bit) {
848 /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
849 netif->ip6_addr[0].addr[2] = htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
850 ((u32_t)(netif->hwaddr[1]) << 16) |
851 ((u32_t)(netif->hwaddr[2]) << 8) |
853 netif->ip6_addr[0].addr[3] = htonl((0xfeul << 24) |
854 ((u32_t)(netif->hwaddr[3]) << 16) |
855 ((u32_t)(netif->hwaddr[4]) << 8) |
859 /* Use hwaddr directly as interface ID. */
860 netif->ip6_addr[0].addr[2] = 0;
861 netif->ip6_addr[0].addr[3] = 0;
864 for (i = 0; i < 8; i++) {
868 netif->ip6_addr[0].addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
872 /* Set address state. */
873 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
874 /* Will perform duplicate address detection (DAD). */
875 netif->ip6_addr_state[0] = IP6_ADDR_TENTATIVE;
877 /* Consider address valid. */
878 netif->ip6_addr_state[0] = IP6_ADDR_PREFERRED;
879 #endif /* LWIP_IPV6_AUTOCONFIG */
881 #endif /* LWIP_IPV6 */