]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/net/lwip/lwip/core/netif.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / net / lwip / lwip / core / netif.c
1 /**
2  * @file
3  * lwIP network interface abstraction
4  *
5  */
6
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
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.
21  *
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
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38
39 #include "lwip/opt.h"
40
41 #include "lwip/def.h"
42 #include "lwip/ip_addr.h"
43 #include "lwip/netif.h"
44 #include "lwip/tcp_impl.h"
45 #include "lwip/snmp.h"
46 #include "lwip/igmp.h"
47 #include "netif/etharp.h"
48 #include "lwip/stats.h"
49 #if ENABLE_LOOPBACK
50 #include "lwip/sys.h"
51 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
52 #include "lwip/tcpip.h"
53 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
54 #endif /* ENABLE_LOOPBACK */
55
56 #if LWIP_AUTOIP
57 #include "lwip/autoip.h"
58 #endif /* LWIP_AUTOIP */
59 #if LWIP_DHCP
60 #include "lwip/dhcp.h"
61 #endif /* LWIP_DHCP */
62
63 #if LWIP_NETIF_STATUS_CALLBACK
64 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
65 #else
66 #define NETIF_STATUS_CALLBACK(n)
67 #endif /* LWIP_NETIF_STATUS_CALLBACK */ 
68
69 #if LWIP_NETIF_LINK_CALLBACK
70 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
71 #else
72 #define NETIF_LINK_CALLBACK(n)
73 #endif /* LWIP_NETIF_LINK_CALLBACK */ 
74
75 struct netif *netif_list;
76 struct netif *netif_default;
77
78 #if LWIP_HAVE_LOOPIF
79 static struct netif loop_netif;
80
81 /**
82  * Initialize a lwip network interface structure for a loopback interface
83  *
84  * @param netif the lwip network interface structure for this loopif
85  * @return ERR_OK if the loopif is initialized
86  *         ERR_MEM if private data couldn't be allocated
87  */
88 static err_t
89 netif_loopif_init(struct netif *netif)
90 {
91   /* initialize the snmp variables and counters inside the struct netif
92    * ifSpeed: no assumption can be made!
93    */
94   NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
95
96   netif->name[0] = 'l';
97   netif->name[1] = 'o';
98   netif->output = netif_loop_output;
99   return ERR_OK;
100 }
101 #endif /* LWIP_HAVE_LOOPIF */
102
103 void
104 netif_init(void)
105 {
106 #if LWIP_HAVE_LOOPIF
107   ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
108   IP4_ADDR(&loop_gw, 127,0,0,1);
109   IP4_ADDR(&loop_ipaddr, 127,0,0,1);
110   IP4_ADDR(&loop_netmask, 255,0,0,0);
111
112 #if NO_SYS
113   netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
114 #else  /* NO_SYS */
115   netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
116 #endif /* NO_SYS */
117   netif_set_up(&loop_netif);
118
119 #endif /* LWIP_HAVE_LOOPIF */
120 }
121
122 /**
123  * Add a network interface to the list of lwIP netifs.
124  *
125  * @param netif a pre-allocated netif structure
126  * @param ipaddr IP address for the new netif
127  * @param netmask network mask for the new netif
128  * @param gw default gateway IP address for the new netif
129  * @param state opaque data passed to the new netif
130  * @param init callback function that initializes the interface
131  * @param input callback function that is called to pass
132  * ingress packets up in the protocol layer stack.
133  *
134  * @return netif, or NULL if failed.
135  */
136 struct netif *
137 netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
138   ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
139 {
140   static u8_t netifnum = 0;
141
142   LWIP_ASSERT("No init function given", init != NULL);
143
144   /* reset new interface configuration state */
145   ip_addr_set_zero(&netif->ip_addr);
146   ip_addr_set_zero(&netif->netmask);
147   ip_addr_set_zero(&netif->gw);
148   netif->flags = 0;
149 #if LWIP_DHCP
150   /* netif not under DHCP control by default */
151   netif->dhcp = NULL;
152 #endif /* LWIP_DHCP */
153 #if LWIP_AUTOIP
154   /* netif not under AutoIP control by default */
155   netif->autoip = NULL;
156 #endif /* LWIP_AUTOIP */
157 #if LWIP_NETIF_STATUS_CALLBACK
158   netif->status_callback = NULL;
159 #endif /* LWIP_NETIF_STATUS_CALLBACK */
160 #if LWIP_NETIF_LINK_CALLBACK
161   netif->link_callback = NULL;
162 #endif /* LWIP_NETIF_LINK_CALLBACK */
163 #if LWIP_IGMP
164   netif->igmp_mac_filter = NULL;
165 #endif /* LWIP_IGMP */
166 #if ENABLE_LOOPBACK
167   netif->loop_first = NULL;
168   netif->loop_last = NULL;
169 #endif /* ENABLE_LOOPBACK */
170
171   /* remember netif specific state information data */
172   netif->state = state;
173   netif->num = netifnum++;
174   netif->input = input;
175 #if LWIP_NETIF_HWADDRHINT
176   netif->addr_hint = NULL;
177 #endif /* LWIP_NETIF_HWADDRHINT*/
178 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
179   netif->loop_cnt_current = 0;
180 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
181
182   netif_set_addr(netif, ipaddr, netmask, gw);
183
184   /* call user specified initialization function for netif */
185   if (init(netif) != ERR_OK) {
186     return NULL;
187   }
188
189   /* add this netif to the list */
190   netif->next = netif_list;
191   netif_list = netif;
192   snmp_inc_iflist();
193
194 #if LWIP_IGMP
195   /* start IGMP processing */
196   if (netif->flags & NETIF_FLAG_IGMP) {
197     igmp_start(netif);
198   }
199 #endif /* LWIP_IGMP */
200
201   LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
202     netif->name[0], netif->name[1]));
203   ip_addr_debug_print(NETIF_DEBUG, ipaddr);
204   LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
205   ip_addr_debug_print(NETIF_DEBUG, netmask);
206   LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
207   ip_addr_debug_print(NETIF_DEBUG, gw);
208   LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
209   return netif;
210 }
211
212 /**
213  * Change IP address configuration for a network interface (including netmask
214  * and default gateway).
215  *
216  * @param netif the network interface to change
217  * @param ipaddr the new IP address
218  * @param netmask the new netmask
219  * @param gw the new default gateway
220  */
221 void
222 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
223     ip_addr_t *gw)
224 {
225   netif_set_ipaddr(netif, ipaddr);
226   netif_set_netmask(netif, netmask);
227   netif_set_gw(netif, gw);
228 }
229
230 /**
231  * Remove a network interface from the list of lwIP netifs.
232  *
233  * @param netif the network interface to remove
234  */
235 void
236 netif_remove(struct netif *netif)
237 {
238   if (netif == NULL) {
239     return;
240   }
241
242 #if LWIP_IGMP
243   /* stop IGMP processing */
244   if (netif->flags & NETIF_FLAG_IGMP) {
245     igmp_stop(netif);
246   }
247 #endif /* LWIP_IGMP */
248   if (netif_is_up(netif)) {
249     /* set netif down before removing (call callback function) */
250     netif_set_down(netif);
251   }
252
253   snmp_delete_ipaddridx_tree(netif);
254
255   /*  is it the first netif? */
256   if (netif_list == netif) {
257     netif_list = netif->next;
258   } else {
259     /*  look for netif further down the list */
260     struct netif * tmpNetif;
261     for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
262       if (tmpNetif->next == netif) {
263         tmpNetif->next = netif->next;
264         break;
265       }
266     }
267     if (tmpNetif == NULL)
268       return; /*  we didn't find any netif today */
269   }
270   snmp_dec_iflist();
271   /* this netif is default? */
272   if (netif_default == netif) {
273     /* reset default netif */
274     netif_set_default(NULL);
275   }
276   LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
277 }
278
279 /**
280  * Find a network interface by searching for its name
281  *
282  * @param name the name of the netif (like netif->name) plus concatenated number
283  * in ascii representation (e.g. 'en0')
284  */
285 struct netif *
286 netif_find(char *name)
287 {
288   struct netif *netif;
289   u8_t num;
290
291   if (name == NULL) {
292     return NULL;
293   }
294
295   num = name[2] - '0';
296
297   for(netif = netif_list; netif != NULL; netif = netif->next) {
298     if (num == netif->num &&
299        name[0] == netif->name[0] &&
300        name[1] == netif->name[1]) {
301       LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
302       return netif;
303     }
304   }
305   LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
306   return NULL;
307 }
308
309 /**
310  * Change the IP address of a network interface
311  *
312  * @param netif the network interface to change
313  * @param ipaddr the new IP address
314  *
315  * @note call netif_set_addr() if you also want to change netmask and
316  * default gateway
317  */
318 void
319 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
320 {
321   /* Protect against dereferencing NULL pointers by
322      treating like ANY, as does ip_addr_set() */
323   if (!ipaddr) {
324     ipaddr = IP_ADDR_ANY;
325   }
326   
327   /* TODO: Handling of obsolete pcbs */
328   /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
329 #if LWIP_TCP
330   struct tcp_pcb *pcb;
331   struct tcp_pcb_listen *lpcb;
332
333   /* address is actually being changed? */
334   if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
335     /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
336     LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
337     pcb = tcp_active_pcbs;
338     while (pcb != NULL) {
339       /* PCB bound to current local interface address? */
340       if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))
341 #if LWIP_AUTOIP
342         /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
343         && !ip_addr_islinklocal(&(pcb->local_ip))
344 #endif /* LWIP_AUTOIP */
345         ) {
346         /* this connection must be aborted */
347         struct tcp_pcb *next = pcb->next;
348         LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
349         tcp_abort(pcb);
350         pcb = next;
351       } else {
352         pcb = pcb->next;
353       }
354     }
355     for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
356       /* PCB bound to current local interface address? */
357       if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
358           (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
359         /* The PCB is listening to the old ipaddr and
360          * is set to listen to the new one instead */
361         ip_addr_set(&(lpcb->local_ip), ipaddr);
362       }
363     }
364   }
365 #endif
366   snmp_delete_ipaddridx_tree(netif);
367   snmp_delete_iprteidx_tree(0,netif);
368   /* set new IP address to netif */
369   ip_addr_set(&(netif->ip_addr), ipaddr);
370   snmp_insert_ipaddridx_tree(netif);
371   snmp_insert_iprteidx_tree(0,netif);
372
373   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",
374     netif->name[0], netif->name[1],
375     ip4_addr1_16(&netif->ip_addr),
376     ip4_addr2_16(&netif->ip_addr),
377     ip4_addr3_16(&netif->ip_addr),
378     ip4_addr4_16(&netif->ip_addr)));
379 }
380
381 /**
382  * Change the default gateway for a network interface
383  *
384  * @param netif the network interface to change
385  * @param gw the new default gateway
386  *
387  * @note call netif_set_addr() if you also want to change ip address and netmask
388  */
389 void
390 netif_set_gw(struct netif *netif, ip_addr_t *gw)
391 {
392   ip_addr_set(&(netif->gw), gw);
393   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",
394     netif->name[0], netif->name[1],
395     ip4_addr1_16(&netif->gw),
396     ip4_addr2_16(&netif->gw),
397     ip4_addr3_16(&netif->gw),
398     ip4_addr4_16(&netif->gw)));
399 }
400
401 /**
402  * Change the netmask of a network interface
403  *
404  * @param netif the network interface to change
405  * @param netmask the new netmask
406  *
407  * @note call netif_set_addr() if you also want to change ip address and
408  * default gateway
409  */
410 void
411 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
412 {
413   snmp_delete_iprteidx_tree(0, netif);
414   /* set new netmask to netif */
415   ip_addr_set(&(netif->netmask), netmask);
416   snmp_insert_iprteidx_tree(0, netif);
417   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",
418     netif->name[0], netif->name[1],
419     ip4_addr1_16(&netif->netmask),
420     ip4_addr2_16(&netif->netmask),
421     ip4_addr3_16(&netif->netmask),
422     ip4_addr4_16(&netif->netmask)));
423 }
424
425 /**
426  * Set a network interface as the default network interface
427  * (used to output all packets for which no specific route is found)
428  *
429  * @param netif the default network interface
430  */
431 void
432 netif_set_default(struct netif *netif)
433 {
434   if (netif == NULL) {
435     /* remove default route */
436     snmp_delete_iprteidx_tree(1, netif);
437   } else {
438     /* install default route */
439     snmp_insert_iprteidx_tree(1, netif);
440   }
441   netif_default = netif;
442   LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
443            netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
444 }
445
446 /**
447  * Bring an interface up, available for processing
448  * traffic.
449  * 
450  * @note: Enabling DHCP on a down interface will make it come
451  * up once configured.
452  * 
453  * @see dhcp_start()
454  */ 
455 void netif_set_up(struct netif *netif)
456 {
457   if (!(netif->flags & NETIF_FLAG_UP)) {
458     netif->flags |= NETIF_FLAG_UP;
459     
460 #if LWIP_SNMP
461     snmp_get_sysuptime(&netif->ts);
462 #endif /* LWIP_SNMP */
463
464     NETIF_STATUS_CALLBACK(netif);
465
466     if (netif->flags & NETIF_FLAG_LINK_UP) {
467 #if LWIP_ARP
468       /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ 
469       if (netif->flags & (NETIF_FLAG_ETHARP)) {
470         etharp_gratuitous(netif);
471       }
472 #endif /* LWIP_ARP */
473
474 #if LWIP_IGMP
475       /* resend IGMP memberships */
476       if (netif->flags & NETIF_FLAG_IGMP) {
477         igmp_report_groups( netif);
478       }
479 #endif /* LWIP_IGMP */
480     }
481   }
482 }
483
484 /**
485  * Bring an interface down, disabling any traffic processing.
486  *
487  * @note: Enabling DHCP on a down interface will make it come
488  * up once configured.
489  * 
490  * @see dhcp_start()
491  */ 
492 void netif_set_down(struct netif *netif)
493 {
494   if (netif->flags & NETIF_FLAG_UP) {
495     netif->flags &= ~NETIF_FLAG_UP;
496 #if LWIP_SNMP
497     snmp_get_sysuptime(&netif->ts);
498 #endif
499
500     NETIF_STATUS_CALLBACK(netif);
501   }
502 }
503
504 #if LWIP_NETIF_STATUS_CALLBACK
505 /**
506  * Set callback to be called when interface is brought up/down
507  */
508 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
509 {
510   if (netif) {
511     netif->status_callback = status_callback;
512   }
513 }
514 #endif /* LWIP_NETIF_STATUS_CALLBACK */
515
516 /**
517  * Called by a driver when its link goes up
518  */
519 void netif_set_link_up(struct netif *netif )
520 {
521   if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
522     netif->flags |= NETIF_FLAG_LINK_UP;
523
524 #if LWIP_DHCP
525     if (netif->dhcp) {
526       dhcp_network_changed(netif);
527     }
528 #endif /* LWIP_DHCP */
529
530 #if LWIP_AUTOIP
531     if (netif->autoip) {
532       autoip_network_changed(netif);
533     }
534 #endif /* LWIP_AUTOIP */
535
536     if (netif->flags & NETIF_FLAG_UP) {
537 #if LWIP_ARP
538       /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ 
539       if (netif->flags & NETIF_FLAG_ETHARP) {
540         etharp_gratuitous(netif);
541       }
542 #endif /* LWIP_ARP */
543
544 #if LWIP_IGMP
545       /* resend IGMP memberships */
546       if (netif->flags & NETIF_FLAG_IGMP) {
547         igmp_report_groups( netif);
548       }
549 #endif /* LWIP_IGMP */
550     }
551     NETIF_LINK_CALLBACK(netif);
552   }
553 }
554
555 /**
556  * Called by a driver when its link goes down
557  */
558 void netif_set_link_down(struct netif *netif )
559 {
560   if (netif->flags & NETIF_FLAG_LINK_UP) {
561     netif->flags &= ~NETIF_FLAG_LINK_UP;
562     NETIF_LINK_CALLBACK(netif);
563   }
564 }
565
566 #if LWIP_NETIF_LINK_CALLBACK
567 /**
568  * Set callback to be called when link is brought up/down
569  */
570 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
571 {
572   if (netif) {
573     netif->link_callback = link_callback;
574   }
575 }
576 #endif /* LWIP_NETIF_LINK_CALLBACK */
577
578 #if ENABLE_LOOPBACK
579 /**
580  * Send an IP packet to be received on the same netif (loopif-like).
581  * The pbuf is simply copied and handed back to netif->input.
582  * In multithreaded mode, this is done directly since netif->input must put
583  * the packet on a queue.
584  * In callback mode, the packet is put on an internal queue and is fed to
585  * netif->input by netif_poll().
586  *
587  * @param netif the lwip network interface structure
588  * @param p the (IP) packet to 'send'
589  * @param ipaddr the ip address to send the packet to (not used)
590  * @return ERR_OK if the packet has been sent
591  *         ERR_MEM if the pbuf used to copy the packet couldn't be allocated
592  */
593 err_t
594 netif_loop_output(struct netif *netif, struct pbuf *p,
595        ip_addr_t *ipaddr)
596 {
597   struct pbuf *r;
598   err_t err;
599   struct pbuf *last;
600 #if LWIP_LOOPBACK_MAX_PBUFS
601   u8_t clen = 0;
602 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
603   /* If we have a loopif, SNMP counters are adjusted for it,
604    * if not they are adjusted for 'netif'. */
605 #if LWIP_SNMP
606 #if LWIP_HAVE_LOOPIF
607   struct netif *stats_if = &loop_netif;
608 #else /* LWIP_HAVE_LOOPIF */
609   struct netif *stats_if = netif;
610 #endif /* LWIP_HAVE_LOOPIF */
611 #endif /* LWIP_SNMP */
612   SYS_ARCH_DECL_PROTECT(lev);
613   LWIP_UNUSED_ARG(ipaddr);
614
615   /* Allocate a new pbuf */
616   r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
617   if (r == NULL) {
618     LINK_STATS_INC(link.memerr);
619     LINK_STATS_INC(link.drop);
620     snmp_inc_ifoutdiscards(stats_if);
621     return ERR_MEM;
622   }
623 #if LWIP_LOOPBACK_MAX_PBUFS
624   clen = pbuf_clen(r);
625   /* check for overflow or too many pbuf on queue */
626   if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
627      ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
628     pbuf_free(r);
629     LINK_STATS_INC(link.memerr);
630     LINK_STATS_INC(link.drop);
631     snmp_inc_ifoutdiscards(stats_if);
632     return ERR_MEM;
633   }
634   netif->loop_cnt_current += clen;
635 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
636
637   /* Copy the whole pbuf queue p into the single pbuf r */
638   if ((err = pbuf_copy(r, p)) != ERR_OK) {
639     pbuf_free(r);
640     LINK_STATS_INC(link.memerr);
641     LINK_STATS_INC(link.drop);
642     snmp_inc_ifoutdiscards(stats_if);
643     return err;
644   }
645
646   /* Put the packet on a linked list which gets emptied through calling
647      netif_poll(). */
648
649   /* let last point to the last pbuf in chain r */
650   for (last = r; last->next != NULL; last = last->next);
651
652   SYS_ARCH_PROTECT(lev);
653   if(netif->loop_first != NULL) {
654     LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
655     netif->loop_last->next = r;
656     netif->loop_last = last;
657   } else {
658     netif->loop_first = r;
659     netif->loop_last = last;
660   }
661   SYS_ARCH_UNPROTECT(lev);
662
663   LINK_STATS_INC(link.xmit);
664   snmp_add_ifoutoctets(stats_if, p->tot_len);
665   snmp_inc_ifoutucastpkts(stats_if);
666
667 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
668   /* For multithreading environment, schedule a call to netif_poll */
669   tcpip_callback((tcpip_callback_fn)netif_poll, netif);
670 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
671
672   return ERR_OK;
673 }
674
675 /**
676  * Call netif_poll() in the main loop of your application. This is to prevent
677  * reentering non-reentrant functions like tcp_input(). Packets passed to
678  * netif_loop_output() are put on a list that is passed to netif->input() by
679  * netif_poll().
680  */
681 void
682 netif_poll(struct netif *netif)
683 {
684   struct pbuf *in;
685   /* If we have a loopif, SNMP counters are adjusted for it,
686    * if not they are adjusted for 'netif'. */
687 #if LWIP_SNMP
688 #if LWIP_HAVE_LOOPIF
689   struct netif *stats_if = &loop_netif;
690 #else /* LWIP_HAVE_LOOPIF */
691   struct netif *stats_if = netif;
692 #endif /* LWIP_HAVE_LOOPIF */
693 #endif /* LWIP_SNMP */
694   SYS_ARCH_DECL_PROTECT(lev);
695
696   do {
697     /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
698     SYS_ARCH_PROTECT(lev);
699     in = netif->loop_first;
700     if (in != NULL) {
701       struct pbuf *in_end = in;
702 #if LWIP_LOOPBACK_MAX_PBUFS
703       u8_t clen = pbuf_clen(in);
704       /* adjust the number of pbufs on queue */
705       LWIP_ASSERT("netif->loop_cnt_current underflow",
706         ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
707       netif->loop_cnt_current -= clen;
708 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
709       while (in_end->len != in_end->tot_len) {
710         LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
711         in_end = in_end->next;
712       }
713       /* 'in_end' now points to the last pbuf from 'in' */
714       if (in_end == netif->loop_last) {
715         /* this was the last pbuf in the list */
716         netif->loop_first = netif->loop_last = NULL;
717       } else {
718         /* pop the pbuf off the list */
719         netif->loop_first = in_end->next;
720         LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
721       }
722       /* De-queue the pbuf from its successors on the 'loop_' list. */
723       in_end->next = NULL;
724     }
725     SYS_ARCH_UNPROTECT(lev);
726
727     if (in != NULL) {
728       LINK_STATS_INC(link.recv);
729       snmp_add_ifinoctets(stats_if, in->tot_len);
730       snmp_inc_ifinucastpkts(stats_if);
731       /* loopback packets are always IP packets! */
732       if (ip_input(in, netif) != ERR_OK) {
733         pbuf_free(in);
734       }
735       /* Don't reference the packet any more! */
736       in = NULL;
737     }
738   /* go on while there is a packet on the list */
739   } while (netif->loop_first != NULL);
740 }
741
742 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
743 /**
744  * Calls netif_poll() for every netif on the netif_list.
745  */
746 void
747 netif_poll_all(void)
748 {
749   struct netif *netif = netif_list;
750   /* loop through netifs */
751   while (netif != NULL) {
752     netif_poll(netif);
753     /* proceed to next network interface */
754     netif = netif->next;
755   }
756 }
757 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
758 #endif /* ENABLE_LOOPBACK */