3 * lwIP network interface abstraction
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
42 #include "lwip/ip_addr.h"
43 #include "lwip/netif.h"
45 #include "lwip/snmp.h"
46 #include "lwip/igmp.h"
47 #include "netif/etharp.h"
48 #if ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING
50 #endif /* ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING */
52 #if LWIP_NETIF_STATUS_CALLBACK
53 #define NETIF_STATUS_CALLBACK(n) { if (n->status_callback) (n->status_callback)(n); }
55 #define NETIF_STATUS_CALLBACK(n) { /* NOP */ }
56 #endif /* LWIP_NETIF_STATUS_CALLBACK */
58 #if LWIP_NETIF_LINK_CALLBACK
59 #define NETIF_LINK_CALLBACK(n) { if (n->link_callback) (n->link_callback)(n); }
61 #define NETIF_LINK_CALLBACK(n) { /* NOP */ }
62 #endif /* LWIP_NETIF_LINK_CALLBACK */
64 struct netif *netif_list;
65 struct netif *netif_default;
68 * Add a network interface to the list of lwIP netifs.
70 * @param netif a pre-allocated netif structure
71 * @param ipaddr IP address for the new netif
72 * @param netmask network mask for the new netif
73 * @param gw default gateway IP address for the new netif
74 * @param state opaque data passed to the new netif
75 * @param init callback function that initializes the interface
76 * @param input callback function that is called to pass
77 * ingress packets up in the protocol layer stack.
79 * @return netif, or NULL if failed.
82 netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
85 err_t (* init)(struct netif *netif),
86 err_t (* input)(struct pbuf *p, struct netif *netif))
88 static u8_t netifnum = 0;
90 /* reset new interface configuration state */
91 netif->ip_addr.addr = 0;
92 netif->netmask.addr = 0;
96 /* netif not under DHCP control by default */
98 #endif /* LWIP_DHCP */
100 /* netif not under AutoIP control by default */
101 netif->autoip = NULL;
102 #endif /* LWIP_AUTOIP */
103 #if LWIP_NETIF_STATUS_CALLBACK
104 netif->status_callback = NULL;
105 #endif /* LWIP_NETIF_STATUS_CALLBACK */
106 #if LWIP_NETIF_LINK_CALLBACK
107 netif->link_callback = NULL;
108 #endif /* LWIP_NETIF_LINK_CALLBACK */
110 netif->igmp_mac_filter = NULL;
111 #endif /* LWIP_IGMP */
112 #if ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING
113 netif->loop_first = NULL;
114 netif->loop_last = NULL;
115 #endif /* ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING */
117 /* remember netif specific state information data */
118 netif->state = state;
119 netif->num = netifnum++;
120 netif->input = input;
121 #if LWIP_NETIF_HWADDRHINT
122 netif->addr_hint = NULL;
123 #endif /* LWIP_NETIF_HWADDRHINT*/
125 netif_set_addr(netif, ipaddr, netmask, gw);
127 /* call user specified initialization function for netif */
128 if (init(netif) != ERR_OK) {
132 /* add this netif to the list */
133 netif->next = netif_list;
138 /* start IGMP processing */
139 if (netif->flags & NETIF_FLAG_IGMP) {
142 #endif /* LWIP_IGMP */
144 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
145 netif->name[0], netif->name[1]));
146 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
147 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
148 ip_addr_debug_print(NETIF_DEBUG, netmask);
149 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
150 ip_addr_debug_print(NETIF_DEBUG, gw);
151 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
156 * Change IP address configuration for a network interface (including netmask
157 * and default gateway).
159 * @param netif the network interface to change
160 * @param ipaddr the new IP address
161 * @param netmask the new netmask
162 * @param gw the new default gateway
165 netif_set_addr(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
168 netif_set_ipaddr(netif, ipaddr);
169 netif_set_netmask(netif, netmask);
170 netif_set_gw(netif, gw);
174 * Remove a network interface from the list of lwIP netifs.
176 * @param netif the network interface to remove
178 void netif_remove(struct netif * netif)
180 if ( netif == NULL ) return;
183 /* stop IGMP processing */
184 if (netif->flags & NETIF_FLAG_IGMP) {
187 #endif /* LWIP_IGMP */
189 snmp_delete_ipaddridx_tree(netif);
191 /* is it the first netif? */
192 if (netif_list == netif) {
193 netif_list = netif->next;
197 /* look for netif further down the list */
198 struct netif * tmpNetif;
199 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
200 if (tmpNetif->next == netif) {
201 tmpNetif->next = netif->next;
206 if (tmpNetif == NULL)
207 return; /* we didn't find any netif today */
209 /* this netif is default? */
210 if (netif_default == netif)
211 /* reset default netif */
212 netif_set_default(NULL);
213 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
217 * Find a network interface by searching for its name
219 * @param name the name of the netif (like netif->name) plus concatenated number
220 * in ascii representation (e.g. 'en0')
223 netif_find(char *name)
234 for(netif = netif_list; netif != NULL; netif = netif->next) {
235 if (num == netif->num &&
236 name[0] == netif->name[0] &&
237 name[1] == netif->name[1]) {
238 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
242 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
247 * Change the IP address of a network interface
249 * @param netif the network interface to change
250 * @param ipaddr the new IP address
252 * @note call netif_set_addr() if you also want to change netmask and
256 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
258 /* TODO: Handling of obsolete pcbs */
259 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
262 struct tcp_pcb_listen *lpcb;
264 /* address is actually being changed? */
265 if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
267 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
268 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n"));
269 pcb = tcp_active_pcbs;
270 while (pcb != NULL) {
271 /* PCB bound to current local interface address? */
272 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
273 /* this connection must be aborted */
274 struct tcp_pcb *next = pcb->next;
275 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
282 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
283 /* PCB bound to current local interface address? */
284 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
285 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
286 /* The PCB is listening to the old ipaddr and
287 * is set to listen to the new one instead */
288 ip_addr_set(&(lpcb->local_ip), ipaddr);
293 snmp_delete_ipaddridx_tree(netif);
294 snmp_delete_iprteidx_tree(0,netif);
295 /* set new IP address to netif */
296 ip_addr_set(&(netif->ip_addr), ipaddr);
297 snmp_insert_ipaddridx_tree(netif);
298 snmp_insert_iprteidx_tree(0,netif);
300 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | 3, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
301 netif->name[0], netif->name[1],
302 ip4_addr1(&netif->ip_addr),
303 ip4_addr2(&netif->ip_addr),
304 ip4_addr3(&netif->ip_addr),
305 ip4_addr4(&netif->ip_addr)));
309 * Change the default gateway for a network interface
311 * @param netif the network interface to change
312 * @param gw the new default gateway
314 * @note call netif_set_addr() if you also want to change ip address and netmask
317 netif_set_gw(struct netif *netif, struct ip_addr *gw)
319 ip_addr_set(&(netif->gw), gw);
320 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | 3, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
321 netif->name[0], netif->name[1],
322 ip4_addr1(&netif->gw),
323 ip4_addr2(&netif->gw),
324 ip4_addr3(&netif->gw),
325 ip4_addr4(&netif->gw)));
329 * Change the netmask of a network interface
331 * @param netif the network interface to change
332 * @param netmask the new netmask
334 * @note call netif_set_addr() if you also want to change ip address and
338 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
340 snmp_delete_iprteidx_tree(0, netif);
341 /* set new netmask to netif */
342 ip_addr_set(&(netif->netmask), netmask);
343 snmp_insert_iprteidx_tree(0, netif);
344 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | 3, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
345 netif->name[0], netif->name[1],
346 ip4_addr1(&netif->netmask),
347 ip4_addr2(&netif->netmask),
348 ip4_addr3(&netif->netmask),
349 ip4_addr4(&netif->netmask)));
353 * Set a network interface as the default network interface
354 * (used to output all packets for which no specific route is found)
356 * @param netif the default network interface
359 netif_set_default(struct netif *netif)
363 /* remove default route */
364 snmp_delete_iprteidx_tree(1, netif);
368 /* install default route */
369 snmp_insert_iprteidx_tree(1, netif);
371 netif_default = netif;
372 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
373 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
377 * Bring an interface up, available for processing
380 * @note: Enabling DHCP on a down interface will make it come
381 * up once configured.
385 void netif_set_up(struct netif *netif)
387 if ( !(netif->flags & NETIF_FLAG_UP )) {
388 netif->flags |= NETIF_FLAG_UP;
391 snmp_get_sysuptime(&netif->ts);
392 #endif /* LWIP_SNMP */
394 NETIF_LINK_CALLBACK(netif);
395 NETIF_STATUS_CALLBACK(netif);
398 /** For Ethernet network interfaces, we would like to send a
399 * "gratuitous ARP"; this is an ARP packet sent by a node in order
400 * to spontaneously cause other nodes to update an entry in their
401 * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
403 if (netif->flags & NETIF_FLAG_ETHARP) {
404 etharp_query(netif, &(netif->ip_addr), NULL);
406 #endif /* LWIP_ARP */
412 * Bring an interface down, disabling any traffic processing.
414 * @note: Enabling DHCP on a down interface will make it come
415 * up once configured.
419 void netif_set_down(struct netif *netif)
421 if ( netif->flags & NETIF_FLAG_UP )
423 netif->flags &= ~NETIF_FLAG_UP;
425 snmp_get_sysuptime(&netif->ts);
428 NETIF_LINK_CALLBACK(netif);
429 NETIF_STATUS_CALLBACK(netif);
434 * Ask if an interface is up
436 u8_t netif_is_up(struct netif *netif)
438 return (netif->flags & NETIF_FLAG_UP)?1:0;
441 #if LWIP_NETIF_STATUS_CALLBACK
443 * Set callback to be called when interface is brought up/down
445 void netif_set_status_callback(struct netif *netif, void (* status_callback)(struct netif *netif ))
448 netif->status_callback = status_callback;
450 #endif /* LWIP_NETIF_STATUS_CALLBACK */
452 #if LWIP_NETIF_LINK_CALLBACK
454 * Called by a driver when its link goes up
456 void netif_set_link_up(struct netif *netif )
458 netif->flags |= NETIF_FLAG_LINK_UP;
461 /** For Ethernet network interfaces, we would like to send a
462 * "gratuitous ARP"; this is an ARP packet sent by a node in order
463 * to spontaneously cause other nodes to update an entry in their
464 * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
466 if (netif->flags & NETIF_FLAG_ETHARP) {
467 etharp_query(netif, &(netif->ip_addr), NULL);
469 #endif /* LWIP_ARP */
472 /* resend IGMP memberships */
473 if (netif->flags & NETIF_FLAG_IGMP) {
474 igmp_report_groups( netif);
476 #endif /* LWIP_IGMP */
478 NETIF_LINK_CALLBACK(netif);
482 * Called by a driver when its link goes down
484 void netif_set_link_down(struct netif *netif )
486 netif->flags &= ~NETIF_FLAG_LINK_UP;
487 NETIF_LINK_CALLBACK(netif);
491 * Ask if a link is up
493 u8_t netif_is_link_up(struct netif *netif)
495 return (netif->flags & NETIF_FLAG_LINK_UP) ? 1 : 0;
499 * Set callback to be called when link is brought up/down
501 void netif_set_link_callback(struct netif *netif, void (* link_callback)(struct netif *netif ))
504 netif->link_callback = link_callback;
506 #endif /* LWIP_NETIF_LINK_CALLBACK */
510 * Send an IP packet to be received on the same netif (loopif-like).
511 * The pbuf is simply copied and handed back to netif->input.
512 * In multithreaded mode, this is done directly since netif->input must put
513 * the packet on a queue.
514 * In callback mode, the packet is put on an internal queue and is fed to
515 * netif->input by netif_poll().
517 * @param netif the lwip network interface structure
518 * @param p the (IP) packet to 'send'
519 * @param ipaddr the ip address to send the packet to (not used)
520 * @return ERR_OK if the packet has been sent
521 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
524 netif_loop_output(struct netif *netif, struct pbuf *p,
525 struct ip_addr *ipaddr)
529 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
531 SYS_ARCH_DECL_PROTECT(lev);
532 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
533 LWIP_UNUSED_ARG(ipaddr);
535 /* Allocate a new pbuf */
536 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
541 /* Copy the whole pbuf queue p into the single pbuf r */
542 if ((err = pbuf_copy(r, p)) != ERR_OK) {
548 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
549 /* Multithreading environment, netif->input() is supposed to put the packet
550 into a mailbox, so we can safely call it here without risking to re-enter
551 functions that are not reentrant (TCP!!!) */
552 LWIP_ASSERT(netif->input != ip_input, "Don't use ip_input as netif->input with LWIP_NETIF_LOOPBACK_MULTITHREADING = 1!");
553 if(netif->input(r, netif) != ERR_OK) {
557 #else /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
558 /* Raw API without threads: put the packet on a linked list which gets emptied
559 through calling netif_poll(). */
561 /* let last point to the last pbuf in chain r */
562 for (last = r; last->next != NULL; last = last->next);
563 SYS_ARCH_PROTECT(lev);
564 if(netif->loop_first != NULL) {
565 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
566 netif->loop_last->next = r;
567 netif->loop_last = last;
569 netif->loop_first = r;
570 netif->loop_last = last;
572 SYS_ARCH_UNPROTECT(lev);
573 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
578 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
580 * Calls netif_poll() for every netif on the netif_list.
585 struct netif *netif = netif_list;
586 /* loop through netifs */
587 while (netif != NULL) {
589 /* proceed to next network interface */
595 * Call netif_poll() in the main loop of your application. This is to prevent
596 * reentering non-reentrant functions like tcp_input(). Packets passed to
597 * netif_loop_output() are put on a list that is passed to netif->input() by
601 netif_poll(struct netif *netif)
604 SYS_ARCH_DECL_PROTECT(lev);
607 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
608 SYS_ARCH_PROTECT(lev);
609 in = netif->loop_first;
611 struct pbuf *in_end = in;
612 while(in_end->len != in_end->tot_len) {
613 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
614 in_end = in_end->next;
616 /* 'in_end' now points to the last pbuf from 'in' */
617 if(in_end == netif->loop_last) {
618 /* this was the last pbuf in the list */
619 netif->loop_first = netif->loop_last = NULL;
621 /* pop the pbuf off the list */
622 netif->loop_first = in_end->next;
623 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
625 /* De-queue the pbuf from its successors on the 'loop_' list. */
628 SYS_ARCH_UNPROTECT(lev);
631 if(netif->input(in, netif) != ERR_OK) {
634 /* Don't reference the packet any more! */
637 /* go on while there is a packet on the list */
638 } while(netif->loop_first != NULL);
640 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
641 #endif /* ENABLE_LOOPBACK */