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"
49 #include "netif/etharp.h"
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 = NULL;
65 struct netif *netif_default = NULL;
68 * Initialize this module
73 netif_list = netif_default = NULL;
77 * Add a network interface to the list of lwIP netifs.
79 * @param netif a pre-allocated netif structure
80 * @param ipaddr IP address for the new netif
81 * @param netmask network mask for the new netif
82 * @param gw default gateway IP address for the new netif
83 * @param state opaque data passed to the new netif
84 * @param init callback function that initializes the interface
85 * @param input callback function that is called to pass
86 * ingress packets up in the protocol layer stack.
88 * @return netif, or NULL if failed.
91 netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
94 err_t (* init)(struct netif *netif),
95 err_t (* input)(struct pbuf *p, struct netif *netif))
97 static u8_t netifnum = 0;
99 /* reset new interface configuration state */
100 netif->ip_addr.addr = 0;
101 netif->netmask.addr = 0;
105 /* netif not under DHCP control by default */
107 #endif /* LWIP_DHCP */
109 /* netif not under AutoIP control by default */
110 netif->autoip = NULL;
111 #endif /* LWIP_AUTOIP */
112 #if LWIP_NETIF_STATUS_CALLBACK
113 netif->status_callback = NULL;
114 #endif /* LWIP_NETIF_STATUS_CALLBACK */
115 #if LWIP_NETIF_LINK_CALLBACK
116 netif->link_callback = NULL;
117 #endif /* LWIP_NETIF_LINK_CALLBACK */
119 /* remember netif specific state information data */
120 netif->state = state;
121 netif->num = netifnum++;
122 netif->input = input;
123 #if LWIP_NETIF_HWADDRHINT
124 netif->addr_hint = NULL;
125 #endif /* LWIP_NETIF_HWADDRHINT*/
127 netif_set_addr(netif, ipaddr, netmask, gw);
129 /* call user specified initialization function for netif */
130 if (init(netif) != ERR_OK) {
134 /* add this netif to the list */
135 netif->next = netif_list;
140 /* start IGMP processing */
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;
182 snmp_delete_ipaddridx_tree(netif);
184 /* is it the first netif? */
185 if (netif_list == netif) {
186 netif_list = netif->next;
190 /* look for netif further down the list */
191 struct netif * tmpNetif;
192 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
193 if (tmpNetif->next == netif) {
194 tmpNetif->next = netif->next;
199 if (tmpNetif == NULL)
200 return; /* we didn't find any netif today */
202 /* this netif is default? */
203 if (netif_default == netif)
204 /* reset default netif */
205 netif_default = NULL;
206 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
210 * Find a network interface by searching for its name
212 * @param name the name of the netif (like netif->name) plus concatenated number
213 * in ascii representation (e.g. 'en0')
216 netif_find(char *name)
227 for(netif = netif_list; netif != NULL; netif = netif->next) {
228 if (num == netif->num &&
229 name[0] == netif->name[0] &&
230 name[1] == netif->name[1]) {
231 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
235 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
240 * Change the IP address of a network interface
242 * @param netif the network interface to change
243 * @param ipaddr the new IP address
245 * @note call netif_set_addr() if you also want to change netmask and
249 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
251 /* TODO: Handling of obsolete pcbs */
252 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
255 struct tcp_pcb_listen *lpcb;
257 /* address is actually being changed? */
258 if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
260 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
261 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n"));
262 pcb = tcp_active_pcbs;
263 while (pcb != NULL) {
264 /* PCB bound to current local interface address? */
265 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
266 /* this connection must be aborted */
267 struct tcp_pcb *next = pcb->next;
268 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
275 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
276 /* PCB bound to current local interface address? */
277 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
278 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
279 /* The PCB is listening to the old ipaddr and
280 * is set to listen to the new one instead */
281 ip_addr_set(&(lpcb->local_ip), ipaddr);
286 snmp_delete_ipaddridx_tree(netif);
287 snmp_delete_iprteidx_tree(0,netif);
288 /* set new IP address to netif */
289 ip_addr_set(&(netif->ip_addr), ipaddr);
290 snmp_insert_ipaddridx_tree(netif);
291 snmp_insert_iprteidx_tree(0,netif);
293 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",
294 netif->name[0], netif->name[1],
295 ip4_addr1(&netif->ip_addr),
296 ip4_addr2(&netif->ip_addr),
297 ip4_addr3(&netif->ip_addr),
298 ip4_addr4(&netif->ip_addr)));
302 * Change the default gateway for a network interface
304 * @param netif the network interface to change
305 * @param gw the new default gateway
307 * @note call netif_set_addr() if you also want to change ip address and netmask
310 netif_set_gw(struct netif *netif, struct ip_addr *gw)
312 ip_addr_set(&(netif->gw), gw);
313 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",
314 netif->name[0], netif->name[1],
315 ip4_addr1(&netif->gw),
316 ip4_addr2(&netif->gw),
317 ip4_addr3(&netif->gw),
318 ip4_addr4(&netif->gw)));
322 * Change the netmask of a network interface
324 * @param netif the network interface to change
325 * @param netmask the new netmask
327 * @note call netif_set_addr() if you also want to change ip address and
331 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
333 snmp_delete_iprteidx_tree(0, netif);
334 /* set new netmask to netif */
335 ip_addr_set(&(netif->netmask), netmask);
336 snmp_insert_iprteidx_tree(0, netif);
337 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",
338 netif->name[0], netif->name[1],
339 ip4_addr1(&netif->netmask),
340 ip4_addr2(&netif->netmask),
341 ip4_addr3(&netif->netmask),
342 ip4_addr4(&netif->netmask)));
346 * Set a network interface as the default network interface
347 * (used to output all packets for which no specific route is found)
349 * @param netif the default network interface
352 netif_set_default(struct netif *netif)
356 /* remove default route */
357 snmp_delete_iprteidx_tree(1, netif);
361 /* install default route */
362 snmp_insert_iprteidx_tree(1, netif);
364 netif_default = netif;
365 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
366 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
370 * Bring an interface up, available for processing
373 * @note: Enabling DHCP on a down interface will make it come
374 * up once configured.
378 void netif_set_up(struct netif *netif)
380 if ( !(netif->flags & NETIF_FLAG_UP )) {
381 netif->flags |= NETIF_FLAG_UP;
384 snmp_get_sysuptime(&netif->ts);
385 #endif /* LWIP_SNMP */
387 NETIF_LINK_CALLBACK(netif);
388 NETIF_STATUS_CALLBACK(netif);
391 /** For Ethernet network interfaces, we would like to send a
392 * "gratuitous ARP"; this is an ARP packet sent by a node in order
393 * to spontaneously cause other nodes to update an entry in their
394 * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
396 if (netif->flags & NETIF_FLAG_ETHARP) {
397 etharp_query(netif, &(netif->ip_addr), NULL);
399 #endif /* LWIP_ARP */
405 * Bring an interface down, disabling any traffic processing.
407 * @note: Enabling DHCP on a down interface will make it come
408 * up once configured.
412 void netif_set_down(struct netif *netif)
414 if ( netif->flags & NETIF_FLAG_UP )
416 netif->flags &= ~NETIF_FLAG_UP;
418 snmp_get_sysuptime(&netif->ts);
421 NETIF_LINK_CALLBACK(netif);
422 NETIF_STATUS_CALLBACK(netif);
427 * Ask if an interface is up
429 u8_t netif_is_up(struct netif *netif)
431 return (netif->flags & NETIF_FLAG_UP)?1:0;
434 #if LWIP_NETIF_STATUS_CALLBACK
436 * Set callback to be called when interface is brought up/down
438 void netif_set_status_callback(struct netif *netif, void (* status_callback)(struct netif *netif ))
441 netif->status_callback = status_callback;
443 #endif /* LWIP_NETIF_STATUS_CALLBACK */
445 #if LWIP_NETIF_LINK_CALLBACK
447 * Called by a driver when its link goes up
449 void netif_set_link_up(struct netif *netif )
451 netif->flags |= NETIF_FLAG_LINK_UP;
454 /** For Ethernet network interfaces, we would like to send a
455 * "gratuitous ARP"; this is an ARP packet sent by a node in order
456 * to spontaneously cause other nodes to update an entry in their
457 * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
459 if (netif->flags & NETIF_FLAG_ETHARP) {
460 etharp_query(netif, &(netif->ip_addr), NULL);
462 #endif /* LWIP_ARP */
464 NETIF_LINK_CALLBACK(netif);
468 * Called by a driver when its link goes down
470 void netif_set_link_down(struct netif *netif )
472 netif->flags &= ~NETIF_FLAG_LINK_UP;
473 NETIF_LINK_CALLBACK(netif);
477 * Ask if a link is up
479 u8_t netif_is_link_up(struct netif *netif)
481 return (netif->flags & NETIF_FLAG_LINK_UP) ? 1 : 0;
485 * Set callback to be called when link is brought up/down
487 void netif_set_link_callback(struct netif *netif, void (* link_callback)(struct netif *netif ))
490 netif->link_callback = link_callback;
492 #endif /* LWIP_NETIF_LINK_CALLBACK */