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"
50 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
51 #include "lwip/tcpip.h"
52 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
53 #endif /* ENABLE_LOOPBACK */
55 #if LWIP_NETIF_STATUS_CALLBACK
56 #define NETIF_STATUS_CALLBACK(n) { if (n->status_callback) (n->status_callback)(n); }
58 #define NETIF_STATUS_CALLBACK(n) { /* NOP */ }
59 #endif /* LWIP_NETIF_STATUS_CALLBACK */
61 #if LWIP_NETIF_LINK_CALLBACK
62 #define NETIF_LINK_CALLBACK(n) { if (n->link_callback) (n->link_callback)(n); }
64 #define NETIF_LINK_CALLBACK(n) { /* NOP */ }
65 #endif /* LWIP_NETIF_LINK_CALLBACK */
67 struct netif *netif_list;
68 struct netif *netif_default;
71 * Add a network interface to the list of lwIP netifs.
73 * @param netif a pre-allocated netif structure
74 * @param ipaddr IP address for the new netif
75 * @param netmask network mask for the new netif
76 * @param gw default gateway IP address for the new netif
77 * @param state opaque data passed to the new netif
78 * @param init callback function that initializes the interface
79 * @param input callback function that is called to pass
80 * ingress packets up in the protocol layer stack.
82 * @return netif, or NULL if failed.
85 netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
88 err_t (* init)(struct netif *netif),
89 err_t (* input)(struct pbuf *p, struct netif *netif))
91 static u8_t netifnum = 0;
93 /* reset new interface configuration state */
94 netif->ip_addr.addr = 0;
95 netif->netmask.addr = 0;
99 /* netif not under DHCP control by default */
101 #endif /* LWIP_DHCP */
103 /* netif not under AutoIP control by default */
104 netif->autoip = NULL;
105 #endif /* LWIP_AUTOIP */
106 #if LWIP_NETIF_STATUS_CALLBACK
107 netif->status_callback = NULL;
108 #endif /* LWIP_NETIF_STATUS_CALLBACK */
109 #if LWIP_NETIF_LINK_CALLBACK
110 netif->link_callback = NULL;
111 #endif /* LWIP_NETIF_LINK_CALLBACK */
113 netif->igmp_mac_filter = NULL;
114 #endif /* LWIP_IGMP */
116 netif->loop_first = NULL;
117 netif->loop_last = NULL;
118 #endif /* ENABLE_LOOPBACK */
120 /* remember netif specific state information data */
121 netif->state = state;
122 netif->num = netifnum++;
123 netif->input = input;
124 #if LWIP_NETIF_HWADDRHINT
125 netif->addr_hint = NULL;
126 #endif /* LWIP_NETIF_HWADDRHINT*/
127 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
128 netif->loop_cnt_current = 0;
129 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
131 netif_set_addr(netif, ipaddr, netmask, gw);
133 /* call user specified initialization function for netif */
134 if (init(netif) != ERR_OK) {
138 /* add this netif to the list */
139 netif->next = netif_list;
144 /* start IGMP processing */
145 if (netif->flags & NETIF_FLAG_IGMP) {
148 #endif /* LWIP_IGMP */
150 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
151 netif->name[0], netif->name[1]));
152 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
153 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
154 ip_addr_debug_print(NETIF_DEBUG, netmask);
155 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
156 ip_addr_debug_print(NETIF_DEBUG, gw);
157 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
162 * Change IP address configuration for a network interface (including netmask
163 * and default gateway).
165 * @param netif the network interface to change
166 * @param ipaddr the new IP address
167 * @param netmask the new netmask
168 * @param gw the new default gateway
171 netif_set_addr(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
174 netif_set_ipaddr(netif, ipaddr);
175 netif_set_netmask(netif, netmask);
176 netif_set_gw(netif, gw);
180 * Remove a network interface from the list of lwIP netifs.
182 * @param netif the network interface to remove
184 void netif_remove(struct netif * netif)
186 if ( netif == NULL ) return;
189 /* stop IGMP processing */
190 if (netif->flags & NETIF_FLAG_IGMP) {
193 #endif /* LWIP_IGMP */
195 snmp_delete_ipaddridx_tree(netif);
197 /* is it the first netif? */
198 if (netif_list == netif) {
199 netif_list = netif->next;
203 /* look for netif further down the list */
204 struct netif * tmpNetif;
205 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
206 if (tmpNetif->next == netif) {
207 tmpNetif->next = netif->next;
212 if (tmpNetif == NULL)
213 return; /* we didn't find any netif today */
215 /* this netif is default? */
216 if (netif_default == netif)
217 /* reset default netif */
218 netif_set_default(NULL);
219 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
223 * Find a network interface by searching for its name
225 * @param name the name of the netif (like netif->name) plus concatenated number
226 * in ascii representation (e.g. 'en0')
229 netif_find(char *name)
240 for(netif = netif_list; netif != NULL; netif = netif->next) {
241 if (num == netif->num &&
242 name[0] == netif->name[0] &&
243 name[1] == netif->name[1]) {
244 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
248 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
253 * Change the IP address of a network interface
255 * @param netif the network interface to change
256 * @param ipaddr the new IP address
258 * @note call netif_set_addr() if you also want to change netmask and
262 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
264 /* TODO: Handling of obsolete pcbs */
265 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
268 struct tcp_pcb_listen *lpcb;
270 /* address is actually being changed? */
271 if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
273 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
274 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n"));
275 pcb = tcp_active_pcbs;
276 while (pcb != NULL) {
277 /* PCB bound to current local interface address? */
278 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
279 /* this connection must be aborted */
280 struct tcp_pcb *next = pcb->next;
281 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
288 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
289 /* PCB bound to current local interface address? */
290 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
291 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
292 /* The PCB is listening to the old ipaddr and
293 * is set to listen to the new one instead */
294 ip_addr_set(&(lpcb->local_ip), ipaddr);
299 snmp_delete_ipaddridx_tree(netif);
300 snmp_delete_iprteidx_tree(0,netif);
301 /* set new IP address to netif */
302 ip_addr_set(&(netif->ip_addr), ipaddr);
303 snmp_insert_ipaddridx_tree(netif);
304 snmp_insert_iprteidx_tree(0,netif);
306 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",
307 netif->name[0], netif->name[1],
308 ip4_addr1(&netif->ip_addr),
309 ip4_addr2(&netif->ip_addr),
310 ip4_addr3(&netif->ip_addr),
311 ip4_addr4(&netif->ip_addr)));
315 * Change the default gateway for a network interface
317 * @param netif the network interface to change
318 * @param gw the new default gateway
320 * @note call netif_set_addr() if you also want to change ip address and netmask
323 netif_set_gw(struct netif *netif, struct ip_addr *gw)
325 ip_addr_set(&(netif->gw), gw);
326 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",
327 netif->name[0], netif->name[1],
328 ip4_addr1(&netif->gw),
329 ip4_addr2(&netif->gw),
330 ip4_addr3(&netif->gw),
331 ip4_addr4(&netif->gw)));
335 * Change the netmask of a network interface
337 * @param netif the network interface to change
338 * @param netmask the new netmask
340 * @note call netif_set_addr() if you also want to change ip address and
344 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
346 snmp_delete_iprteidx_tree(0, netif);
347 /* set new netmask to netif */
348 ip_addr_set(&(netif->netmask), netmask);
349 snmp_insert_iprteidx_tree(0, netif);
350 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",
351 netif->name[0], netif->name[1],
352 ip4_addr1(&netif->netmask),
353 ip4_addr2(&netif->netmask),
354 ip4_addr3(&netif->netmask),
355 ip4_addr4(&netif->netmask)));
359 * Set a network interface as the default network interface
360 * (used to output all packets for which no specific route is found)
362 * @param netif the default network interface
365 netif_set_default(struct netif *netif)
369 /* remove default route */
370 snmp_delete_iprteidx_tree(1, netif);
374 /* install default route */
375 snmp_insert_iprteidx_tree(1, netif);
377 netif_default = netif;
378 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
379 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
383 * Bring an interface up, available for processing
386 * @note: Enabling DHCP on a down interface will make it come
387 * up once configured.
391 void netif_set_up(struct netif *netif)
393 if ( !(netif->flags & NETIF_FLAG_UP )) {
394 netif->flags |= NETIF_FLAG_UP;
397 snmp_get_sysuptime(&netif->ts);
398 #endif /* LWIP_SNMP */
400 NETIF_LINK_CALLBACK(netif);
401 NETIF_STATUS_CALLBACK(netif);
404 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
405 if (netif->flags & NETIF_FLAG_ETHARP) {
406 etharp_gratuitous(netif);
408 #endif /* LWIP_ARP */
414 * Bring an interface down, disabling any traffic processing.
416 * @note: Enabling DHCP on a down interface will make it come
417 * up once configured.
421 void netif_set_down(struct netif *netif)
423 if ( netif->flags & NETIF_FLAG_UP )
425 netif->flags &= ~NETIF_FLAG_UP;
427 snmp_get_sysuptime(&netif->ts);
430 NETIF_LINK_CALLBACK(netif);
431 NETIF_STATUS_CALLBACK(netif);
436 * Ask if an interface is up
438 u8_t netif_is_up(struct netif *netif)
440 return (netif->flags & NETIF_FLAG_UP)?1:0;
443 #if LWIP_NETIF_STATUS_CALLBACK
445 * Set callback to be called when interface is brought up/down
447 void netif_set_status_callback(struct netif *netif, void (* status_callback)(struct netif *netif ))
450 netif->status_callback = status_callback;
452 #endif /* LWIP_NETIF_STATUS_CALLBACK */
454 #if LWIP_NETIF_LINK_CALLBACK
456 * Called by a driver when its link goes up
458 void netif_set_link_up(struct netif *netif )
460 netif->flags |= NETIF_FLAG_LINK_UP;
463 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
464 if (netif->flags & NETIF_FLAG_ETHARP) {
465 etharp_gratuitous(netif);
467 #endif /* LWIP_ARP */
470 /* resend IGMP memberships */
471 if (netif->flags & NETIF_FLAG_IGMP) {
472 igmp_report_groups( netif);
474 #endif /* LWIP_IGMP */
476 NETIF_LINK_CALLBACK(netif);
480 * Called by a driver when its link goes down
482 void netif_set_link_down(struct netif *netif )
484 netif->flags &= ~NETIF_FLAG_LINK_UP;
485 NETIF_LINK_CALLBACK(netif);
489 * Ask if a link is up
491 u8_t netif_is_link_up(struct netif *netif)
493 return (netif->flags & NETIF_FLAG_LINK_UP) ? 1 : 0;
497 * Set callback to be called when link is brought up/down
499 void netif_set_link_callback(struct netif *netif, void (* link_callback)(struct netif *netif ))
502 netif->link_callback = link_callback;
505 #endif /* LWIP_NETIF_LINK_CALLBACK */
509 * Send an IP packet to be received on the same netif (loopif-like).
510 * The pbuf is simply copied and handed back to netif->input.
511 * In multithreaded mode, this is done directly since netif->input must put
512 * the packet on a queue.
513 * In callback mode, the packet is put on an internal queue and is fed to
514 * netif->input by netif_poll().
516 * @param netif the lwip network interface structure
517 * @param p the (IP) packet to 'send'
518 * @param ipaddr the ip address to send the packet to (not used)
519 * @return ERR_OK if the packet has been sent
520 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
523 netif_loop_output(struct netif *netif, struct pbuf *p,
524 struct ip_addr *ipaddr)
529 #if LWIP_LOOPBACK_MAX_PBUFS
531 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
532 SYS_ARCH_DECL_PROTECT(lev);
533 LWIP_UNUSED_ARG(ipaddr);
535 /* Allocate a new pbuf */
536 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
540 #if LWIP_LOOPBACK_MAX_PBUFS
542 /* check for overflow or too many pbuf on queue */
543 if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
544 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
549 netif->loop_cnt_current += clen;
550 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
552 /* Copy the whole pbuf queue p into the single pbuf r */
553 if ((err = pbuf_copy(r, p)) != ERR_OK) {
559 /* Put the packet on a linked list which gets emptied through calling
562 /* let last point to the last pbuf in chain r */
563 for (last = r; last->next != NULL; last = last->next);
565 SYS_ARCH_PROTECT(lev);
566 if(netif->loop_first != NULL) {
567 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
568 netif->loop_last->next = r;
569 netif->loop_last = last;
571 netif->loop_first = r;
572 netif->loop_last = last;
574 SYS_ARCH_UNPROTECT(lev);
576 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
577 /* For multithreading environment, schedule a call to netif_poll */
578 tcpip_callback((void (*)(void *))(netif_poll), netif);
579 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
585 * Call netif_poll() in the main loop of your application. This is to prevent
586 * reentering non-reentrant functions like tcp_input(). Packets passed to
587 * netif_loop_output() are put on a list that is passed to netif->input() by
591 netif_poll(struct netif *netif)
594 SYS_ARCH_DECL_PROTECT(lev);
597 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
598 SYS_ARCH_PROTECT(lev);
599 in = netif->loop_first;
601 struct pbuf *in_end = in;
602 #if LWIP_LOOPBACK_MAX_PBUFS
603 u8_t clen = pbuf_clen(in);
604 /* adjust the number of pbufs on queue */
605 LWIP_ASSERT("netif->loop_cnt_current underflow",
606 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
607 netif->loop_cnt_current -= clen;
608 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
609 while(in_end->len != in_end->tot_len) {
610 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
611 in_end = in_end->next;
613 /* 'in_end' now points to the last pbuf from 'in' */
614 if(in_end == netif->loop_last) {
615 /* this was the last pbuf in the list */
616 netif->loop_first = netif->loop_last = NULL;
618 /* pop the pbuf off the list */
619 netif->loop_first = in_end->next;
620 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
622 /* De-queue the pbuf from its successors on the 'loop_' list. */
625 SYS_ARCH_UNPROTECT(lev);
628 /* loopback packets are always IP packets! */
629 if(ip_input(in, netif) != ERR_OK) {
632 /* Don't reference the packet any more! */
635 /* go on while there is a packet on the list */
636 } while(netif->loop_first != NULL);
639 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
641 * Calls netif_poll() for every netif on the netif_list.
646 struct netif *netif = netif_list;
647 /* loop through netifs */
648 while (netif != NULL) {
650 /* proceed to next network interface */
654 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
655 #endif /* ENABLE_LOOPBACK */