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 err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, ip6_addr_t *ipaddr);
89 #endif /* LWIP_IPV6 */
92 static struct netif loop_netif;
95 * Initialize a lwip network interface structure for a loopback interface
97 * @param netif the lwip network interface structure for this loopif
98 * @return ERR_OK if the loopif is initialized
99 * ERR_MEM if private data couldn't be allocated
102 netif_loopif_init(struct netif *netif)
104 /* initialize the snmp variables and counters inside the struct netif
105 * ifSpeed: no assumption can be made!
107 NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
109 netif->name[0] = 'l';
110 netif->name[1] = 'o';
111 netif->output = netif_loop_output;
114 #endif /* LWIP_HAVE_LOOPIF */
120 ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
121 IP4_ADDR(&loop_gw, 127,0,0,1);
122 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
123 IP4_ADDR(&loop_netmask, 255,0,0,0);
126 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
128 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
130 netif_set_up(&loop_netif);
132 #endif /* LWIP_HAVE_LOOPIF */
136 * Add a network interface to the list of lwIP netifs.
138 * @param netif a pre-allocated netif structure
139 * @param ipaddr IP address for the new netif
140 * @param netmask network mask for the new netif
141 * @param gw default gateway IP address for the new netif
142 * @param state opaque data passed to the new netif
143 * @param init callback function that initializes the interface
144 * @param input callback function that is called to pass
145 * ingress packets up in the protocol layer stack.
147 * @return netif, or NULL if failed.
150 netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
151 ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
157 LWIP_ASSERT("No init function given", init != NULL);
159 /* reset new interface configuration state */
160 ip_addr_set_zero(&netif->ip_addr);
161 ip_addr_set_zero(&netif->netmask);
162 ip_addr_set_zero(&netif->gw);
164 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
165 ip6_addr_set_zero(&netif->ip6_addr[i]);
166 netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
168 netif->output_ip6 = netif_null_output_ip6;
169 #endif /* LWIP_IPV6 */
172 /* netif not under DHCP control by default */
174 #endif /* LWIP_DHCP */
176 /* netif not under AutoIP control by default */
177 netif->autoip = NULL;
178 #endif /* LWIP_AUTOIP */
179 #if LWIP_IPV6_AUTOCONFIG
180 /* IPv6 address autoconfiguration not enabled by default */
181 netif->ip6_autoconfig_enabled = 0;
182 #endif /* LWIP_IPV6_AUTOCONFIG */
183 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
184 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
185 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
187 /* netif not under DHCPv6 control by default */
189 #endif /* LWIP_IPV6_DHCP6 */
190 #if LWIP_NETIF_STATUS_CALLBACK
191 netif->status_callback = NULL;
192 #endif /* LWIP_NETIF_STATUS_CALLBACK */
193 #if LWIP_NETIF_LINK_CALLBACK
194 netif->link_callback = NULL;
195 #endif /* LWIP_NETIF_LINK_CALLBACK */
197 netif->igmp_mac_filter = NULL;
198 #endif /* LWIP_IGMP */
199 #if LWIP_IPV6 && LWIP_IPV6_MLD
200 netif->mld_mac_filter = NULL;
201 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
203 netif->loop_first = NULL;
204 netif->loop_last = NULL;
205 #endif /* ENABLE_LOOPBACK */
207 /* remember netif specific state information data */
208 netif->state = state;
209 netif->num = netif_num++;
210 netif->input = input;
211 NETIF_SET_HWADDRHINT(netif, NULL);
212 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
213 netif->loop_cnt_current = 0;
214 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
216 netif_set_addr(netif, ipaddr, netmask, gw);
218 /* call user specified initialization function for netif */
219 if (init(netif) != ERR_OK) {
223 /* add this netif to the list */
224 netif->next = netif_list;
229 /* start IGMP processing */
230 if (netif->flags & NETIF_FLAG_IGMP) {
233 #endif /* LWIP_IGMP */
235 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
236 netif->name[0], netif->name[1]));
237 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
238 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
239 ip_addr_debug_print(NETIF_DEBUG, netmask);
240 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
241 ip_addr_debug_print(NETIF_DEBUG, gw);
242 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
247 * Change IP address configuration for a network interface (including netmask
248 * and default gateway).
250 * @param netif the network interface to change
251 * @param ipaddr the new IP address
252 * @param netmask the new netmask
253 * @param gw the new default gateway
256 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
259 netif_set_ipaddr(netif, ipaddr);
260 netif_set_netmask(netif, netmask);
261 netif_set_gw(netif, gw);
265 * Remove a network interface from the list of lwIP netifs.
267 * @param netif the network interface to remove
270 netif_remove(struct netif *netif)
277 /* stop IGMP processing */
278 if (netif->flags & NETIF_FLAG_IGMP) {
281 #endif /* LWIP_IGMP */
282 #if LWIP_IPV6 && LWIP_IPV6_MLD
283 /* stop MLD processing */
285 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
286 if (netif_is_up(netif)) {
287 /* set netif down before removing (call callback function) */
288 netif_set_down(netif);
291 snmp_delete_ipaddridx_tree(netif);
293 /* is it the first netif? */
294 if (netif_list == netif) {
295 netif_list = netif->next;
297 /* look for netif further down the list */
298 struct netif * tmpNetif;
299 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
300 if (tmpNetif->next == netif) {
301 tmpNetif->next = netif->next;
305 if (tmpNetif == NULL)
306 return; /* we didn't find any netif today */
309 /* this netif is default? */
310 if (netif_default == netif) {
311 /* reset default netif */
312 netif_set_default(NULL);
314 #if LWIP_NETIF_REMOVE_CALLBACK
315 if (netif->remove_callback) {
316 netif->remove_callback(netif);
318 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
319 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
323 * Find a network interface by searching for its name
325 * @param name the name of the netif (like netif->name) plus concatenated number
326 * in ascii representation (e.g. 'en0')
329 netif_find(char *name)
340 for(netif = netif_list; netif != NULL; netif = netif->next) {
341 if (num == netif->num &&
342 name[0] == netif->name[0] &&
343 name[1] == netif->name[1]) {
344 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
348 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
353 * Change the IP address of a network interface
355 * @param netif the network interface to change
356 * @param ipaddr the new IP address
358 * @note call netif_set_addr() if you also want to change netmask and
362 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
364 /* TODO: Handling of obsolete pcbs */
365 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
368 struct tcp_pcb_listen *lpcb;
370 /* address is actually being changed? */
371 if (ipaddr && (ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
372 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
373 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
374 pcb = tcp_active_pcbs;
375 while (pcb != NULL) {
376 /* PCB bound to current local interface address? */
377 if (ip_addr_cmp(ipX_2_ip(&pcb->local_ip), &(netif->ip_addr))
379 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
380 && !ip_addr_islinklocal(ipX_2_ip(&pcb->local_ip))
381 #endif /* LWIP_AUTOIP */
383 /* this connection must be aborted */
384 struct tcp_pcb *next = pcb->next;
385 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
392 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
393 /* PCB bound to current local interface address? */
394 if ((!(ip_addr_isany(ipX_2_ip(&lpcb->local_ip)))) &&
395 (ip_addr_cmp(ipX_2_ip(&lpcb->local_ip), &(netif->ip_addr)))) {
396 /* The PCB is listening to the old ipaddr and
397 * is set to listen to the new one instead */
398 ip_addr_set(ipX_2_ip(&lpcb->local_ip), ipaddr);
403 snmp_delete_ipaddridx_tree(netif);
404 snmp_delete_iprteidx_tree(0,netif);
405 /* set new IP address to netif */
406 ip_addr_set(&(netif->ip_addr), ipaddr);
407 snmp_insert_ipaddridx_tree(netif);
408 snmp_insert_iprteidx_tree(0,netif);
410 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",
411 netif->name[0], netif->name[1],
412 ip4_addr1_16(&netif->ip_addr),
413 ip4_addr2_16(&netif->ip_addr),
414 ip4_addr3_16(&netif->ip_addr),
415 ip4_addr4_16(&netif->ip_addr)));
419 * Change the default gateway for a network interface
421 * @param netif the network interface to change
422 * @param gw the new default gateway
424 * @note call netif_set_addr() if you also want to change ip address and netmask
427 netif_set_gw(struct netif *netif, ip_addr_t *gw)
429 ip_addr_set(&(netif->gw), gw);
430 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",
431 netif->name[0], netif->name[1],
432 ip4_addr1_16(&netif->gw),
433 ip4_addr2_16(&netif->gw),
434 ip4_addr3_16(&netif->gw),
435 ip4_addr4_16(&netif->gw)));
439 * Change the netmask of a network interface
441 * @param netif the network interface to change
442 * @param netmask the new netmask
444 * @note call netif_set_addr() if you also want to change ip address and
448 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
450 snmp_delete_iprteidx_tree(0, netif);
451 /* set new netmask to netif */
452 ip_addr_set(&(netif->netmask), netmask);
453 snmp_insert_iprteidx_tree(0, netif);
454 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",
455 netif->name[0], netif->name[1],
456 ip4_addr1_16(&netif->netmask),
457 ip4_addr2_16(&netif->netmask),
458 ip4_addr3_16(&netif->netmask),
459 ip4_addr4_16(&netif->netmask)));
463 * Set a network interface as the default network interface
464 * (used to output all packets for which no specific route is found)
466 * @param netif the default network interface
469 netif_set_default(struct netif *netif)
472 /* remove default route */
473 snmp_delete_iprteidx_tree(1, netif);
475 /* install default route */
476 snmp_insert_iprteidx_tree(1, netif);
478 netif_default = netif;
479 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
480 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
484 * Bring an interface up, available for processing
487 * @note: Enabling DHCP on a down interface will make it come
488 * up once configured.
492 void netif_set_up(struct netif *netif)
494 if (!(netif->flags & NETIF_FLAG_UP)) {
495 netif->flags |= NETIF_FLAG_UP;
498 snmp_get_sysuptime(&netif->ts);
499 #endif /* LWIP_SNMP */
501 NETIF_STATUS_CALLBACK(netif);
503 if (netif->flags & NETIF_FLAG_LINK_UP) {
505 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
506 if (netif->flags & (NETIF_FLAG_ETHARP)) {
507 etharp_gratuitous(netif);
509 #endif /* LWIP_ARP */
512 /* resend IGMP memberships */
513 if (netif->flags & NETIF_FLAG_IGMP) {
514 igmp_report_groups( netif);
516 #endif /* LWIP_IGMP */
517 #if LWIP_IPV6 && LWIP_IPV6_MLD
518 /* send mld memberships */
519 mld6_report_groups( netif);
520 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
522 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
523 /* Send Router Solicitation messages. */
524 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
525 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
532 * Bring an interface down, disabling any traffic processing.
534 * @note: Enabling DHCP on a down interface will make it come
535 * up once configured.
539 void netif_set_down(struct netif *netif)
541 if (netif->flags & NETIF_FLAG_UP) {
542 netif->flags &= ~NETIF_FLAG_UP;
544 snmp_get_sysuptime(&netif->ts);
548 if (netif->flags & NETIF_FLAG_ETHARP) {
549 etharp_cleanup_netif(netif);
551 #endif /* LWIP_ARP */
552 NETIF_STATUS_CALLBACK(netif);
556 #if LWIP_NETIF_STATUS_CALLBACK
558 * Set callback to be called when interface is brought up/down
560 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
563 netif->status_callback = status_callback;
566 #endif /* LWIP_NETIF_STATUS_CALLBACK */
568 #if LWIP_NETIF_REMOVE_CALLBACK
570 * Set callback to be called when the interface has been removed
573 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
576 netif->remove_callback = remove_callback;
579 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
582 * Called by a driver when its link goes up
584 void netif_set_link_up(struct netif *netif )
586 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
587 netif->flags |= NETIF_FLAG_LINK_UP;
591 dhcp_network_changed(netif);
593 #endif /* LWIP_DHCP */
597 autoip_network_changed(netif);
599 #endif /* LWIP_AUTOIP */
601 if (netif->flags & NETIF_FLAG_UP) {
603 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
604 if (netif->flags & NETIF_FLAG_ETHARP) {
605 etharp_gratuitous(netif);
607 #endif /* LWIP_ARP */
610 /* resend IGMP memberships */
611 if (netif->flags & NETIF_FLAG_IGMP) {
612 igmp_report_groups( netif);
614 #endif /* LWIP_IGMP */
615 #if LWIP_IPV6 && LWIP_IPV6_MLD
616 /* send mld memberships */
617 mld6_report_groups( netif);
618 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
620 NETIF_LINK_CALLBACK(netif);
625 * Called by a driver when its link goes down
627 void netif_set_link_down(struct netif *netif )
629 if (netif->flags & NETIF_FLAG_LINK_UP) {
630 netif->flags &= ~NETIF_FLAG_LINK_UP;
631 NETIF_LINK_CALLBACK(netif);
635 #if LWIP_NETIF_LINK_CALLBACK
637 * Set callback to be called when link is brought up/down
639 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
642 netif->link_callback = link_callback;
645 #endif /* LWIP_NETIF_LINK_CALLBACK */
649 * Send an IP packet to be received on the same netif (loopif-like).
650 * The pbuf is simply copied and handed back to netif->input.
651 * In multithreaded mode, this is done directly since netif->input must put
652 * the packet on a queue.
653 * In callback mode, the packet is put on an internal queue and is fed to
654 * netif->input by netif_poll().
656 * @param netif the lwip network interface structure
657 * @param p the (IP) packet to 'send'
658 * @param ipaddr the ip address to send the packet to (not used)
659 * @return ERR_OK if the packet has been sent
660 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
663 netif_loop_output(struct netif *netif, struct pbuf *p,
669 #if LWIP_LOOPBACK_MAX_PBUFS
671 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
672 /* If we have a loopif, SNMP counters are adjusted for it,
673 * if not they are adjusted for 'netif'. */
676 struct netif *stats_if = &loop_netif;
677 #else /* LWIP_HAVE_LOOPIF */
678 struct netif *stats_if = netif;
679 #endif /* LWIP_HAVE_LOOPIF */
680 #endif /* LWIP_SNMP */
681 SYS_ARCH_DECL_PROTECT(lev);
682 LWIP_UNUSED_ARG(ipaddr);
684 /* Allocate a new pbuf */
685 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
687 LINK_STATS_INC(link.memerr);
688 LINK_STATS_INC(link.drop);
689 snmp_inc_ifoutdiscards(stats_if);
692 #if LWIP_LOOPBACK_MAX_PBUFS
694 /* check for overflow or too many pbuf on queue */
695 if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
696 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
698 LINK_STATS_INC(link.memerr);
699 LINK_STATS_INC(link.drop);
700 snmp_inc_ifoutdiscards(stats_if);
703 netif->loop_cnt_current += clen;
704 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
706 /* Copy the whole pbuf queue p into the single pbuf r */
707 if ((err = pbuf_copy(r, p)) != ERR_OK) {
709 LINK_STATS_INC(link.memerr);
710 LINK_STATS_INC(link.drop);
711 snmp_inc_ifoutdiscards(stats_if);
715 /* Put the packet on a linked list which gets emptied through calling
718 /* let last point to the last pbuf in chain r */
719 for (last = r; last->next != NULL; last = last->next);
721 SYS_ARCH_PROTECT(lev);
722 if(netif->loop_first != NULL) {
723 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
724 netif->loop_last->next = r;
725 netif->loop_last = last;
727 netif->loop_first = r;
728 netif->loop_last = last;
730 SYS_ARCH_UNPROTECT(lev);
732 LINK_STATS_INC(link.xmit);
733 snmp_add_ifoutoctets(stats_if, p->tot_len);
734 snmp_inc_ifoutucastpkts(stats_if);
736 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
737 /* For multithreading environment, schedule a call to netif_poll */
738 tcpip_callback((tcpip_callback_fn)netif_poll, netif);
739 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
745 * Call netif_poll() in the main loop of your application. This is to prevent
746 * reentering non-reentrant functions like tcp_input(). Packets passed to
747 * netif_loop_output() are put on a list that is passed to netif->input() by
751 netif_poll(struct netif *netif)
754 /* If we have a loopif, SNMP counters are adjusted for it,
755 * if not they are adjusted for 'netif'. */
758 struct netif *stats_if = &loop_netif;
759 #else /* LWIP_HAVE_LOOPIF */
760 struct netif *stats_if = netif;
761 #endif /* LWIP_HAVE_LOOPIF */
762 #endif /* LWIP_SNMP */
763 SYS_ARCH_DECL_PROTECT(lev);
766 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
767 SYS_ARCH_PROTECT(lev);
768 in = netif->loop_first;
770 struct pbuf *in_end = in;
771 #if LWIP_LOOPBACK_MAX_PBUFS
772 u8_t clen = pbuf_clen(in);
773 /* adjust the number of pbufs on queue */
774 LWIP_ASSERT("netif->loop_cnt_current underflow",
775 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
776 netif->loop_cnt_current -= clen;
777 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
778 while (in_end->len != in_end->tot_len) {
779 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
780 in_end = in_end->next;
782 /* 'in_end' now points to the last pbuf from 'in' */
783 if (in_end == netif->loop_last) {
784 /* this was the last pbuf in the list */
785 netif->loop_first = netif->loop_last = NULL;
787 /* pop the pbuf off the list */
788 netif->loop_first = in_end->next;
789 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
791 /* De-queue the pbuf from its successors on the 'loop_' list. */
794 SYS_ARCH_UNPROTECT(lev);
797 LINK_STATS_INC(link.recv);
798 snmp_add_ifinoctets(stats_if, in->tot_len);
799 snmp_inc_ifinucastpkts(stats_if);
800 /* loopback packets are always IP packets! */
801 if (ip_input(in, netif) != ERR_OK) {
804 /* Don't reference the packet any more! */
807 /* go on while there is a packet on the list */
808 } while (netif->loop_first != NULL);
811 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
813 * Calls netif_poll() for every netif on the netif_list.
818 struct netif *netif = netif_list;
819 /* loop through netifs */
820 while (netif != NULL) {
822 /* proceed to next network interface */
826 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
827 #endif /* ENABLE_LOOPBACK */
831 netif_matches_ip6_addr(struct netif * netif, ip6_addr_t * ip6addr)
834 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
835 if (ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
843 netif_create_ip6_linklocal_address(struct netif * netif, u8_t from_mac_48bit)
847 /* Link-local prefix. */
848 netif->ip6_addr[0].addr[0] = PP_HTONL(0xfe800000ul);
849 netif->ip6_addr[0].addr[1] = 0;
851 /* Generate interface ID. */
852 if (from_mac_48bit) {
853 /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
854 netif->ip6_addr[0].addr[2] = htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
855 ((u32_t)(netif->hwaddr[1]) << 16) |
856 ((u32_t)(netif->hwaddr[2]) << 8) |
858 netif->ip6_addr[0].addr[3] = htonl((0xfeul << 24) |
859 ((u32_t)(netif->hwaddr[3]) << 16) |
860 ((u32_t)(netif->hwaddr[4]) << 8) |
864 /* Use hwaddr directly as interface ID. */
865 netif->ip6_addr[0].addr[2] = 0;
866 netif->ip6_addr[0].addr[3] = 0;
869 for (i = 0; i < 8; i++) {
873 netif->ip6_addr[0].addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
877 /* Set address state. */
878 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
879 /* Will perform duplicate address detection (DAD). */
880 netif->ip6_addr_state[0] = IP6_ADDR_TENTATIVE;
882 /* Consider address valid. */
883 netif->ip6_addr_state[0] = IP6_ADDR_PREFERRED;
884 #endif /* LWIP_IPV6_AUTOCONFIG */
888 netif_null_output_ip6(struct netif *netif, struct pbuf *p, ip6_addr_t *ipaddr)
896 #endif /* LWIP_IPV6 */