]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/netif.c
Fixed typo in previous commit.
[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_IPV6
88 static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, ip6_addr_t *ipaddr);
89 #endif /* LWIP_IPV6 */
90
91 #if LWIP_HAVE_LOOPIF
92 static struct netif loop_netif;
93
94 /**
95  * Initialize a lwip network interface structure for a loopback interface
96  *
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
100  */
101 static err_t
102 netif_loopif_init(struct netif *netif)
103 {
104   /* initialize the snmp variables and counters inside the struct netif
105    * ifSpeed: no assumption can be made!
106    */
107   NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
108
109   netif->name[0] = 'l';
110   netif->name[1] = 'o';
111   netif->output = netif_loop_output;
112   return ERR_OK;
113 }
114 #endif /* LWIP_HAVE_LOOPIF */
115
116 void
117 netif_init(void)
118 {
119 #if 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);
124
125 #if NO_SYS
126   netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
127 #else  /* NO_SYS */
128   netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
129 #endif /* NO_SYS */
130   netif_set_up(&loop_netif);
131
132 #endif /* LWIP_HAVE_LOOPIF */
133 }
134
135 /**
136  * Add a network interface to the list of lwIP netifs.
137  *
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.
146  *
147  * @return netif, or NULL if failed.
148  */
149 struct netif *
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)
152 {
153 #if LWIP_IPV6
154   u32_t i;
155 #endif
156
157   LWIP_ASSERT("No init function given", init != NULL);
158
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);
163 #if LWIP_IPV6
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);
167   }
168   netif->output_ip6 = netif_null_output_ip6;
169 #endif /* LWIP_IPV6 */
170   netif->flags = 0;
171 #if LWIP_DHCP
172   /* netif not under DHCP control by default */
173   netif->dhcp = NULL;
174 #endif /* LWIP_DHCP */
175 #if LWIP_AUTOIP
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 */
186 #if LWIP_IPV6_DHCP6
187   /* netif not under DHCPv6 control by default */
188   netif->dhcp6 = NULL;
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 */
196 #if LWIP_IGMP
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 */
202 #if ENABLE_LOOPBACK
203   netif->loop_first = NULL;
204   netif->loop_last = NULL;
205 #endif /* ENABLE_LOOPBACK */
206
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 */
215
216   netif_set_addr(netif, ipaddr, netmask, gw);
217
218   /* call user specified initialization function for netif */
219   if (init(netif) != ERR_OK) {
220     return NULL;
221   }
222
223   /* add this netif to the list */
224   netif->next = netif_list;
225   netif_list = netif;
226   snmp_inc_iflist();
227
228 #if LWIP_IGMP
229   /* start IGMP processing */
230   if (netif->flags & NETIF_FLAG_IGMP) {
231     igmp_start(netif);
232   }
233 #endif /* LWIP_IGMP */
234
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"));
243   return netif;
244 }
245
246 /**
247  * Change IP address configuration for a network interface (including netmask
248  * and default gateway).
249  *
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
254  */
255 void
256 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
257     ip_addr_t *gw)
258 {
259   netif_set_ipaddr(netif, ipaddr);
260   netif_set_netmask(netif, netmask);
261   netif_set_gw(netif, gw);
262 }
263
264 /**
265  * Remove a network interface from the list of lwIP netifs.
266  *
267  * @param netif the network interface to remove
268  */
269 void
270 netif_remove(struct netif *netif)
271 {
272   if (netif == NULL) {
273     return;
274   }
275
276 #if LWIP_IGMP
277   /* stop IGMP processing */
278   if (netif->flags & NETIF_FLAG_IGMP) {
279     igmp_stop(netif);
280   }
281 #endif /* LWIP_IGMP */
282 #if LWIP_IPV6 && LWIP_IPV6_MLD
283   /* stop MLD processing */
284   mld6_stop(netif);
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);
289   }
290
291   snmp_delete_ipaddridx_tree(netif);
292
293   /*  is it the first netif? */
294   if (netif_list == netif) {
295     netif_list = netif->next;
296   } else {
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;
302         break;
303       }
304     }
305     if (tmpNetif == NULL)
306       return; /*  we didn't find any netif today */
307   }
308   snmp_dec_iflist();
309   /* this netif is default? */
310   if (netif_default == netif) {
311     /* reset default netif */
312     netif_set_default(NULL);
313   }
314 #if LWIP_NETIF_REMOVE_CALLBACK
315   if (netif->remove_callback) {
316     netif->remove_callback(netif);
317   }
318 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
319   LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
320 }
321
322 /**
323  * Find a network interface by searching for its name
324  *
325  * @param name the name of the netif (like netif->name) plus concatenated number
326  * in ascii representation (e.g. 'en0')
327  */
328 struct netif *
329 netif_find(char *name)
330 {
331   struct netif *netif;
332   u8_t num;
333
334   if (name == NULL) {
335     return NULL;
336   }
337
338   num = name[2] - '0';
339
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]));
345       return netif;
346     }
347   }
348   LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
349   return NULL;
350 }
351
352 /**
353  * Change the IP address of a network interface
354  *
355  * @param netif the network interface to change
356  * @param ipaddr the new IP address
357  *
358  * @note call netif_set_addr() if you also want to change netmask and
359  * default gateway
360  */
361 void
362 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
363 {
364   /* TODO: Handling of obsolete pcbs */
365   /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
366 #if LWIP_TCP
367   struct tcp_pcb *pcb;
368   struct tcp_pcb_listen *lpcb;
369
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))
378 #if LWIP_AUTOIP
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 */
382         ) {
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));
386         tcp_abort(pcb);
387         pcb = next;
388       } else {
389         pcb = pcb->next;
390       }
391     }
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);
399       }
400     }
401   }
402 #endif
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);
409
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)));
416 }
417
418 /**
419  * Change the default gateway for a network interface
420  *
421  * @param netif the network interface to change
422  * @param gw the new default gateway
423  *
424  * @note call netif_set_addr() if you also want to change ip address and netmask
425  */
426 void
427 netif_set_gw(struct netif *netif, ip_addr_t *gw)
428 {
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)));
436 }
437
438 /**
439  * Change the netmask of a network interface
440  *
441  * @param netif the network interface to change
442  * @param netmask the new netmask
443  *
444  * @note call netif_set_addr() if you also want to change ip address and
445  * default gateway
446  */
447 void
448 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
449 {
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)));
460 }
461
462 /**
463  * Set a network interface as the default network interface
464  * (used to output all packets for which no specific route is found)
465  *
466  * @param netif the default network interface
467  */
468 void
469 netif_set_default(struct netif *netif)
470 {
471   if (netif == NULL) {
472     /* remove default route */
473     snmp_delete_iprteidx_tree(1, netif);
474   } else {
475     /* install default route */
476     snmp_insert_iprteidx_tree(1, netif);
477   }
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] : '\''));
481 }
482
483 /**
484  * Bring an interface up, available for processing
485  * traffic.
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_up(struct netif *netif)
493 {
494   if (!(netif->flags & NETIF_FLAG_UP)) {
495     netif->flags |= NETIF_FLAG_UP;
496     
497 #if LWIP_SNMP
498     snmp_get_sysuptime(&netif->ts);
499 #endif /* LWIP_SNMP */
500
501     NETIF_STATUS_CALLBACK(netif);
502
503     if (netif->flags & NETIF_FLAG_LINK_UP) {
504 #if LWIP_ARP
505       /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ 
506       if (netif->flags & (NETIF_FLAG_ETHARP)) {
507         etharp_gratuitous(netif);
508       }
509 #endif /* LWIP_ARP */
510
511 #if LWIP_IGMP
512       /* resend IGMP memberships */
513       if (netif->flags & NETIF_FLAG_IGMP) {
514         igmp_report_groups( netif);
515       }
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 */
521
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 */
526
527     }
528   }
529 }
530
531 /**
532  * Bring an interface down, disabling any traffic processing.
533  *
534  * @note: Enabling DHCP on a down interface will make it come
535  * up once configured.
536  * 
537  * @see dhcp_start()
538  */ 
539 void netif_set_down(struct netif *netif)
540 {
541   if (netif->flags & NETIF_FLAG_UP) {
542     netif->flags &= ~NETIF_FLAG_UP;
543 #if LWIP_SNMP
544     snmp_get_sysuptime(&netif->ts);
545 #endif
546
547 #if LWIP_ARP
548     if (netif->flags & NETIF_FLAG_ETHARP) {
549       etharp_cleanup_netif(netif);
550     }
551 #endif /* LWIP_ARP */
552     NETIF_STATUS_CALLBACK(netif);
553   }
554 }
555
556 #if LWIP_NETIF_STATUS_CALLBACK
557 /**
558  * Set callback to be called when interface is brought up/down
559  */
560 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
561 {
562   if (netif) {
563     netif->status_callback = status_callback;
564   }
565 }
566 #endif /* LWIP_NETIF_STATUS_CALLBACK */
567
568 #if LWIP_NETIF_REMOVE_CALLBACK
569 /**
570  * Set callback to be called when the interface has been removed
571  */
572 void
573 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
574 {
575   if (netif) {
576     netif->remove_callback = remove_callback;
577   }
578 }
579 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
580
581 /**
582  * Called by a driver when its link goes up
583  */
584 void netif_set_link_up(struct netif *netif )
585 {
586   if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
587     netif->flags |= NETIF_FLAG_LINK_UP;
588
589 #if LWIP_DHCP
590     if (netif->dhcp) {
591       dhcp_network_changed(netif);
592     }
593 #endif /* LWIP_DHCP */
594
595 #if LWIP_AUTOIP
596     if (netif->autoip) {
597       autoip_network_changed(netif);
598     }
599 #endif /* LWIP_AUTOIP */
600
601     if (netif->flags & NETIF_FLAG_UP) {
602 #if LWIP_ARP
603       /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ 
604       if (netif->flags & NETIF_FLAG_ETHARP) {
605         etharp_gratuitous(netif);
606       }
607 #endif /* LWIP_ARP */
608
609 #if LWIP_IGMP
610       /* resend IGMP memberships */
611       if (netif->flags & NETIF_FLAG_IGMP) {
612         igmp_report_groups( netif);
613       }
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 */
619     }
620     NETIF_LINK_CALLBACK(netif);
621   }
622 }
623
624 /**
625  * Called by a driver when its link goes down
626  */
627 void netif_set_link_down(struct netif *netif )
628 {
629   if (netif->flags & NETIF_FLAG_LINK_UP) {
630     netif->flags &= ~NETIF_FLAG_LINK_UP;
631     NETIF_LINK_CALLBACK(netif);
632   }
633 }
634
635 #if LWIP_NETIF_LINK_CALLBACK
636 /**
637  * Set callback to be called when link is brought up/down
638  */
639 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
640 {
641   if (netif) {
642     netif->link_callback = link_callback;
643   }
644 }
645 #endif /* LWIP_NETIF_LINK_CALLBACK */
646
647 #if ENABLE_LOOPBACK
648 /**
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().
655  *
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
661  */
662 err_t
663 netif_loop_output(struct netif *netif, struct pbuf *p,
664        ip_addr_t *ipaddr)
665 {
666   struct pbuf *r;
667   err_t err;
668   struct pbuf *last;
669 #if LWIP_LOOPBACK_MAX_PBUFS
670   u8_t clen = 0;
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'. */
674 #if LWIP_SNMP
675 #if LWIP_HAVE_LOOPIF
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);
683
684   /* Allocate a new pbuf */
685   r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
686   if (r == NULL) {
687     LINK_STATS_INC(link.memerr);
688     LINK_STATS_INC(link.drop);
689     snmp_inc_ifoutdiscards(stats_if);
690     return ERR_MEM;
691   }
692 #if LWIP_LOOPBACK_MAX_PBUFS
693   clen = pbuf_clen(r);
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)) {
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_MEM;
702   }
703   netif->loop_cnt_current += clen;
704 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
705
706   /* Copy the whole pbuf queue p into the single pbuf r */
707   if ((err = pbuf_copy(r, p)) != ERR_OK) {
708     pbuf_free(r);
709     LINK_STATS_INC(link.memerr);
710     LINK_STATS_INC(link.drop);
711     snmp_inc_ifoutdiscards(stats_if);
712     return err;
713   }
714
715   /* Put the packet on a linked list which gets emptied through calling
716      netif_poll(). */
717
718   /* let last point to the last pbuf in chain r */
719   for (last = r; last->next != NULL; last = last->next);
720
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;
726   } else {
727     netif->loop_first = r;
728     netif->loop_last = last;
729   }
730   SYS_ARCH_UNPROTECT(lev);
731
732   LINK_STATS_INC(link.xmit);
733   snmp_add_ifoutoctets(stats_if, p->tot_len);
734   snmp_inc_ifoutucastpkts(stats_if);
735
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 */
740
741   return ERR_OK;
742 }
743
744 /**
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
748  * netif_poll().
749  */
750 void
751 netif_poll(struct netif *netif)
752 {
753   struct pbuf *in;
754   /* If we have a loopif, SNMP counters are adjusted for it,
755    * if not they are adjusted for 'netif'. */
756 #if LWIP_SNMP
757 #if LWIP_HAVE_LOOPIF
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);
764
765   do {
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;
769     if (in != NULL) {
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;
781       }
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;
786       } else {
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);
790       }
791       /* De-queue the pbuf from its successors on the 'loop_' list. */
792       in_end->next = NULL;
793     }
794     SYS_ARCH_UNPROTECT(lev);
795
796     if (in != NULL) {
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) {
802         pbuf_free(in);
803       }
804       /* Don't reference the packet any more! */
805       in = NULL;
806     }
807   /* go on while there is a packet on the list */
808   } while (netif->loop_first != NULL);
809 }
810
811 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
812 /**
813  * Calls netif_poll() for every netif on the netif_list.
814  */
815 void
816 netif_poll_all(void)
817 {
818   struct netif *netif = netif_list;
819   /* loop through netifs */
820   while (netif != NULL) {
821     netif_poll(netif);
822     /* proceed to next network interface */
823     netif = netif->next;
824   }
825 }
826 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
827 #endif /* ENABLE_LOOPBACK */
828
829 #if LWIP_IPV6
830 s8_t
831 netif_matches_ip6_addr(struct netif * netif, ip6_addr_t * ip6addr)
832 {
833   s8_t i;
834   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
835     if (ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
836       return i;
837     }
838   }
839   return -1;
840 }
841
842 void
843 netif_create_ip6_linklocal_address(struct netif * netif, u8_t from_mac_48bit)
844 {
845   u8_t i, addr_index;
846
847   /* Link-local prefix. */
848   netif->ip6_addr[0].addr[0] = PP_HTONL(0xfe800000ul);
849   netif->ip6_addr[0].addr[1] = 0;
850
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) |
857         (0xff));
858     netif->ip6_addr[0].addr[3] = htonl((0xfeul << 24) |
859         ((u32_t)(netif->hwaddr[3]) << 16) |
860         ((u32_t)(netif->hwaddr[4]) << 8) |
861         (netif->hwaddr[5]));
862   }
863   else {
864     /* Use hwaddr directly as interface ID. */
865     netif->ip6_addr[0].addr[2] = 0;
866     netif->ip6_addr[0].addr[3] = 0;
867
868     addr_index = 3;
869     for (i = 0; i < 8; i++) {
870       if (i == 4) {
871         addr_index--;
872       }
873       netif->ip6_addr[0].addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
874     }
875   }
876
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;
881 #else
882   /* Consider address valid. */
883   netif->ip6_addr_state[0] = IP6_ADDR_PREFERRED;
884 #endif /* LWIP_IPV6_AUTOCONFIG */
885 }
886
887 static err_t
888 netif_null_output_ip6(struct netif *netif, struct pbuf *p, ip6_addr_t *ipaddr)
889 {
890     (void)netif;
891     (void)p;
892     (void)ipaddr;
893
894     return ERR_IF;
895 }
896 #endif /* LWIP_IPV6 */