]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/netif.c
fixed bug #34684: Clear the arp table cache when netif is brought down
[pes-rpp/rpp-lwip.git] / src / 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/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"
50 #if ENABLE_LOOPBACK
51 #include "lwip/sys.h"
52 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
53 #include "lwip/tcpip.h"
54 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
55 #endif /* ENABLE_LOOPBACK */
56
57 #if LWIP_AUTOIP
58 #include "lwip/autoip.h"
59 #endif /* LWIP_AUTOIP */
60 #if LWIP_DHCP
61 #include "lwip/dhcp.h"
62 #endif /* LWIP_DHCP */
63 #if LWIP_IPV6_DHCP6
64 #include "lwip/dhcp6.h"
65 #endif /* LWIP_IPV6_DHCP6 */
66 #if LWIP_IPV6_MLD
67 #include "lwip/mld6.h"
68 #endif /* LWIP_IPV6_MLD */
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
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
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 *
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 #if LWIP_IPV6
150   u32_t i;
151 #endif
152
153   LWIP_ASSERT("No init function given", init != NULL);
154
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);
159 #if LWIP_IPV6
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);
163   }
164 #endif /* LWIP_IPV6 */
165   netif->flags = 0;
166 #if LWIP_DHCP
167   /* netif not under DHCP control by default */
168   netif->dhcp = NULL;
169 #endif /* LWIP_DHCP */
170 #if LWIP_AUTOIP
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 */
181 #if LWIP_IPV6_DHCP6
182   /* netif not under DHCPv6 control by default */
183   netif->dhcp6 = NULL;
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 */
191 #if LWIP_IGMP
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 */
197 #if ENABLE_LOOPBACK
198   netif->loop_first = NULL;
199   netif->loop_last = NULL;
200 #endif /* ENABLE_LOOPBACK */
201
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 */
210
211   netif_set_addr(netif, ipaddr, netmask, gw);
212
213   /* call user specified initialization function for netif */
214   if (init(netif) != ERR_OK) {
215     return NULL;
216   }
217
218   /* add this netif to the list */
219   netif->next = netif_list;
220   netif_list = netif;
221   snmp_inc_iflist();
222
223 #if LWIP_IGMP
224   /* start IGMP processing */
225   if (netif->flags & NETIF_FLAG_IGMP) {
226     igmp_start(netif);
227   }
228 #endif /* LWIP_IGMP */
229
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"));
238   return netif;
239 }
240
241 /**
242  * Change IP address configuration for a network interface (including netmask
243  * and default gateway).
244  *
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
249  */
250 void
251 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
252     ip_addr_t *gw)
253 {
254   netif_set_ipaddr(netif, ipaddr);
255   netif_set_netmask(netif, netmask);
256   netif_set_gw(netif, gw);
257 }
258
259 /**
260  * Remove a network interface from the list of lwIP netifs.
261  *
262  * @param netif the network interface to remove
263  */
264 void
265 netif_remove(struct netif *netif)
266 {
267   if (netif == NULL) {
268     return;
269   }
270
271 #if LWIP_IGMP
272   /* stop IGMP processing */
273   if (netif->flags & NETIF_FLAG_IGMP) {
274     igmp_stop(netif);
275   }
276 #endif /* LWIP_IGMP */
277 #if LWIP_IPV6 && LWIP_IPV6_MLD
278   /* stop MLD processing */
279   mld6_stop(netif);
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);
284   }
285
286   snmp_delete_ipaddridx_tree(netif);
287
288   /*  is it the first netif? */
289   if (netif_list == netif) {
290     netif_list = netif->next;
291   } else {
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;
297         break;
298       }
299     }
300     if (tmpNetif == NULL)
301       return; /*  we didn't find any netif today */
302   }
303   snmp_dec_iflist();
304   /* this netif is default? */
305   if (netif_default == netif) {
306     /* reset default netif */
307     netif_set_default(NULL);
308   }
309 #if LWIP_NETIF_REMOVE_CALLBACK
310   if (netif->remove_callback) {
311     netif->remove_callback(netif);
312   }
313 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
314   LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
315 }
316
317 /**
318  * Find a network interface by searching for its name
319  *
320  * @param name the name of the netif (like netif->name) plus concatenated number
321  * in ascii representation (e.g. 'en0')
322  */
323 struct netif *
324 netif_find(char *name)
325 {
326   struct netif *netif;
327   u8_t num;
328
329   if (name == NULL) {
330     return NULL;
331   }
332
333   num = name[2] - '0';
334
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]));
340       return netif;
341     }
342   }
343   LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
344   return NULL;
345 }
346
347 /**
348  * Change the IP address of a network interface
349  *
350  * @param netif the network interface to change
351  * @param ipaddr the new IP address
352  *
353  * @note call netif_set_addr() if you also want to change netmask and
354  * default gateway
355  */
356 void
357 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
358 {
359   /* TODO: Handling of obsolete pcbs */
360   /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
361 #if LWIP_TCP
362   struct tcp_pcb *pcb;
363   struct tcp_pcb_listen *lpcb;
364
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))
373 #if LWIP_AUTOIP
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 */
377         ) {
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));
381         tcp_abort(pcb);
382         pcb = next;
383       } else {
384         pcb = pcb->next;
385       }
386     }
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);
394       }
395     }
396   }
397 #endif
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);
404
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)));
411 }
412
413 /**
414  * Change the default gateway for a network interface
415  *
416  * @param netif the network interface to change
417  * @param gw the new default gateway
418  *
419  * @note call netif_set_addr() if you also want to change ip address and netmask
420  */
421 void
422 netif_set_gw(struct netif *netif, ip_addr_t *gw)
423 {
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)));
431 }
432
433 /**
434  * Change the netmask of a network interface
435  *
436  * @param netif the network interface to change
437  * @param netmask the new netmask
438  *
439  * @note call netif_set_addr() if you also want to change ip address and
440  * default gateway
441  */
442 void
443 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
444 {
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)));
455 }
456
457 /**
458  * Set a network interface as the default network interface
459  * (used to output all packets for which no specific route is found)
460  *
461  * @param netif the default network interface
462  */
463 void
464 netif_set_default(struct netif *netif)
465 {
466   if (netif == NULL) {
467     /* remove default route */
468     snmp_delete_iprteidx_tree(1, netif);
469   } else {
470     /* install default route */
471     snmp_insert_iprteidx_tree(1, netif);
472   }
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] : '\''));
476 }
477
478 /**
479  * Bring an interface up, available for processing
480  * traffic.
481  * 
482  * @note: Enabling DHCP on a down interface will make it come
483  * up once configured.
484  * 
485  * @see dhcp_start()
486  */ 
487 void netif_set_up(struct netif *netif)
488 {
489   if (!(netif->flags & NETIF_FLAG_UP)) {
490     netif->flags |= NETIF_FLAG_UP;
491     
492 #if LWIP_SNMP
493     snmp_get_sysuptime(&netif->ts);
494 #endif /* LWIP_SNMP */
495
496     NETIF_STATUS_CALLBACK(netif);
497
498     if (netif->flags & NETIF_FLAG_LINK_UP) {
499 #if LWIP_ARP
500       /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ 
501       if (netif->flags & (NETIF_FLAG_ETHARP)) {
502         etharp_gratuitous(netif);
503       }
504 #endif /* LWIP_ARP */
505
506 #if LWIP_IGMP
507       /* resend IGMP memberships */
508       if (netif->flags & NETIF_FLAG_IGMP) {
509         igmp_report_groups( netif);
510       }
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 */
516
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 */
521
522     }
523   }
524 }
525
526 /**
527  * Bring an interface down, disabling any traffic processing.
528  *
529  * @note: Enabling DHCP on a down interface will make it come
530  * up once configured.
531  * 
532  * @see dhcp_start()
533  */ 
534 void netif_set_down(struct netif *netif)
535 {
536   if (netif->flags & NETIF_FLAG_UP) {
537     netif->flags &= ~NETIF_FLAG_UP;
538 #if LWIP_SNMP
539     snmp_get_sysuptime(&netif->ts);
540 #endif
541
542 #if LWIP_ARP
543     if (netif->flags & NETIF_FLAG_ETHARP) {
544       etharp_cleanup_netif(netif);
545     }
546 #endif /* LWIP_ARP */
547     NETIF_STATUS_CALLBACK(netif);
548   }
549 }
550
551 #if LWIP_NETIF_STATUS_CALLBACK
552 /**
553  * Set callback to be called when interface is brought up/down
554  */
555 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
556 {
557   if (netif) {
558     netif->status_callback = status_callback;
559   }
560 }
561 #endif /* LWIP_NETIF_STATUS_CALLBACK */
562
563 #if LWIP_NETIF_REMOVE_CALLBACK
564 /**
565  * Set callback to be called when the interface has been removed
566  */
567 void
568 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
569 {
570   if (netif) {
571     netif->remove_callback = remove_callback;
572   }
573 }
574 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
575
576 /**
577  * Called by a driver when its link goes up
578  */
579 void netif_set_link_up(struct netif *netif )
580 {
581   if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
582     netif->flags |= NETIF_FLAG_LINK_UP;
583
584 #if LWIP_DHCP
585     if (netif->dhcp) {
586       dhcp_network_changed(netif);
587     }
588 #endif /* LWIP_DHCP */
589
590 #if LWIP_AUTOIP
591     if (netif->autoip) {
592       autoip_network_changed(netif);
593     }
594 #endif /* LWIP_AUTOIP */
595
596     if (netif->flags & NETIF_FLAG_UP) {
597 #if LWIP_ARP
598       /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ 
599       if (netif->flags & NETIF_FLAG_ETHARP) {
600         etharp_gratuitous(netif);
601       }
602 #endif /* LWIP_ARP */
603
604 #if LWIP_IGMP
605       /* resend IGMP memberships */
606       if (netif->flags & NETIF_FLAG_IGMP) {
607         igmp_report_groups( netif);
608       }
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 */
614     }
615     NETIF_LINK_CALLBACK(netif);
616   }
617 }
618
619 /**
620  * Called by a driver when its link goes down
621  */
622 void netif_set_link_down(struct netif *netif )
623 {
624   if (netif->flags & NETIF_FLAG_LINK_UP) {
625     netif->flags &= ~NETIF_FLAG_LINK_UP;
626     NETIF_LINK_CALLBACK(netif);
627   }
628 }
629
630 #if LWIP_NETIF_LINK_CALLBACK
631 /**
632  * Set callback to be called when link is brought up/down
633  */
634 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
635 {
636   if (netif) {
637     netif->link_callback = link_callback;
638   }
639 }
640 #endif /* LWIP_NETIF_LINK_CALLBACK */
641
642 #if ENABLE_LOOPBACK
643 /**
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().
650  *
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
656  */
657 err_t
658 netif_loop_output(struct netif *netif, struct pbuf *p,
659        ip_addr_t *ipaddr)
660 {
661   struct pbuf *r;
662   err_t err;
663   struct pbuf *last;
664 #if LWIP_LOOPBACK_MAX_PBUFS
665   u8_t clen = 0;
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'. */
669 #if LWIP_SNMP
670 #if LWIP_HAVE_LOOPIF
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);
678
679   /* Allocate a new pbuf */
680   r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
681   if (r == NULL) {
682     LINK_STATS_INC(link.memerr);
683     LINK_STATS_INC(link.drop);
684     snmp_inc_ifoutdiscards(stats_if);
685     return ERR_MEM;
686   }
687 #if LWIP_LOOPBACK_MAX_PBUFS
688   clen = pbuf_clen(r);
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)) {
692     pbuf_free(r);
693     LINK_STATS_INC(link.memerr);
694     LINK_STATS_INC(link.drop);
695     snmp_inc_ifoutdiscards(stats_if);
696     return ERR_MEM;
697   }
698   netif->loop_cnt_current += clen;
699 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
700
701   /* Copy the whole pbuf queue p into the single pbuf r */
702   if ((err = pbuf_copy(r, p)) != ERR_OK) {
703     pbuf_free(r);
704     LINK_STATS_INC(link.memerr);
705     LINK_STATS_INC(link.drop);
706     snmp_inc_ifoutdiscards(stats_if);
707     return err;
708   }
709
710   /* Put the packet on a linked list which gets emptied through calling
711      netif_poll(). */
712
713   /* let last point to the last pbuf in chain r */
714   for (last = r; last->next != NULL; last = last->next);
715
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;
721   } else {
722     netif->loop_first = r;
723     netif->loop_last = last;
724   }
725   SYS_ARCH_UNPROTECT(lev);
726
727   LINK_STATS_INC(link.xmit);
728   snmp_add_ifoutoctets(stats_if, p->tot_len);
729   snmp_inc_ifoutucastpkts(stats_if);
730
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 */
735
736   return ERR_OK;
737 }
738
739 /**
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
743  * netif_poll().
744  */
745 void
746 netif_poll(struct netif *netif)
747 {
748   struct pbuf *in;
749   /* If we have a loopif, SNMP counters are adjusted for it,
750    * if not they are adjusted for 'netif'. */
751 #if LWIP_SNMP
752 #if LWIP_HAVE_LOOPIF
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);
759
760   do {
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;
764     if (in != NULL) {
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;
776       }
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;
781       } else {
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);
785       }
786       /* De-queue the pbuf from its successors on the 'loop_' list. */
787       in_end->next = NULL;
788     }
789     SYS_ARCH_UNPROTECT(lev);
790
791     if (in != NULL) {
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) {
797         pbuf_free(in);
798       }
799       /* Don't reference the packet any more! */
800       in = NULL;
801     }
802   /* go on while there is a packet on the list */
803   } while (netif->loop_first != NULL);
804 }
805
806 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
807 /**
808  * Calls netif_poll() for every netif on the netif_list.
809  */
810 void
811 netif_poll_all(void)
812 {
813   struct netif *netif = netif_list;
814   /* loop through netifs */
815   while (netif != NULL) {
816     netif_poll(netif);
817     /* proceed to next network interface */
818     netif = netif->next;
819   }
820 }
821 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
822 #endif /* ENABLE_LOOPBACK */
823
824 #if LWIP_IPV6
825 s8_t
826 netif_matches_ip6_addr(struct netif * netif, ip6_addr_t * ip6addr)
827 {
828   s8_t i;
829   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
830     if (ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
831       return i;
832     }
833   }
834   return -1;
835 }
836
837 void
838 netif_create_ip6_linklocal_address(struct netif * netif, u8_t from_mac_48bit)
839 {
840   u8_t i, addr_index;
841
842   /* Link-local prefix. */
843   netif->ip6_addr[0].addr[0] = PP_HTONL(0xfe800000ul);
844   netif->ip6_addr[0].addr[1] = 0;
845
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) |
852         (0xff));
853     netif->ip6_addr[0].addr[3] = htonl((0xfeul << 24) |
854         ((u32_t)(netif->hwaddr[3]) << 16) |
855         ((u32_t)(netif->hwaddr[4]) << 8) |
856         (netif->hwaddr[5]));
857   }
858   else {
859     /* Use hwaddr directly as interface ID. */
860     netif->ip6_addr[0].addr[2] = 0;
861     netif->ip6_addr[0].addr[3] = 0;
862
863     addr_index = 3;
864     for (i = 0; i < 8; i++) {
865       if (i == 4) {
866         addr_index--;
867       }
868       netif->ip6_addr[0].addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
869     }
870   }
871
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;
876 #else
877   /* Consider address valid. */
878   netif->ip6_addr_state[0] = IP6_ADDR_PREFERRED;
879 #endif /* LWIP_IPV6_AUTOCONFIG */
880 }
881 #endif /* LWIP_IPV6 */