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