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"
48 #include "netif/etharp.h"
51 #if LWIP_NETIF_STATUS_CALLBACK
52 #define NETIF_STATUS_CALLBACK(n) { if (n->status_callback) (n->status_callback)(n); }
54 #define NETIF_STATUS_CALLBACK(n) { /* NOP */ }
55 #endif /* LWIP_NETIF_STATUS_CALLBACK */
57 #if LWIP_NETIF_LINK_CALLBACK
58 #define NETIF_LINK_CALLBACK(n) { if (n->link_callback) (n->link_callback)(n); }
60 #define NETIF_LINK_CALLBACK(n) { /* NOP */ }
61 #endif /* LWIP_NETIF_LINK_CALLBACK */
63 struct netif *netif_list = NULL;
64 struct netif *netif_default = NULL;
67 * Initialize this module
72 netif_list = netif_default = NULL;
76 * Add a network interface to the list of lwIP netifs.
78 * @param netif a pre-allocated netif structure
79 * @param ipaddr IP address for the new netif
80 * @param netmask network mask for the new netif
81 * @param gw default gateway IP address for the new netif
82 * @param state opaque data passed to the new netif
83 * @param init callback function that initializes the interface
84 * @param input callback function that is called to pass
85 * ingress packets up in the protocol layer stack.
87 * @return netif, or NULL if failed.
90 netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
93 err_t (* init)(struct netif *netif),
94 err_t (* input)(struct pbuf *p, struct netif *netif))
96 static u8_t netifnum = 0;
98 /* reset new interface configuration state */
99 netif->ip_addr.addr = 0;
100 netif->netmask.addr = 0;
104 /* netif not under DHCP control by default */
106 #endif /* LWIP_DHCP */
108 /* netif not under AutoIP control by default */
109 netif->autoip = NULL;
110 #endif /* LWIP_AUTOIP */
111 #if LWIP_NETIF_STATUS_CALLBACK
112 netif->status_callback = NULL;
113 #endif /* LWIP_NETIF_STATUS_CALLBACK */
114 #if LWIP_NETIF_LINK_CALLBACK
115 netif->link_callback = NULL;
116 #endif /* LWIP_NETIF_LINK_CALLBACK */
118 /* remember netif specific state information data */
119 netif->state = state;
120 netif->num = netifnum++;
121 netif->input = input;
122 #if LWIP_NETIF_HWADDRHINT
123 netif->addr_hint = NULL;
124 #endif /* LWIP_NETIF_HWADDRHINT*/
126 netif_set_addr(netif, ipaddr, netmask, gw);
128 /* call user specified initialization function for netif */
129 if (init(netif) != ERR_OK) {
133 /* add this netif to the list */
134 netif->next = netif_list;
138 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
139 netif->name[0], netif->name[1]));
140 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
141 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
142 ip_addr_debug_print(NETIF_DEBUG, netmask);
143 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
144 ip_addr_debug_print(NETIF_DEBUG, gw);
145 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
150 * Change IP address configuration for a network interface (including netmask
151 * and default gateway).
153 * @param netif the network interface to change
154 * @param ipaddr the new IP address
155 * @param netmask the new netmask
156 * @param gw the new default gateway
159 netif_set_addr(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
162 netif_set_ipaddr(netif, ipaddr);
163 netif_set_netmask(netif, netmask);
164 netif_set_gw(netif, gw);
168 * Remove a network interface from the list of lwIP netifs.
170 * @param netif the network interface to remove
172 void netif_remove(struct netif * netif)
174 if ( netif == NULL ) return;
176 snmp_delete_ipaddridx_tree(netif);
178 /* is it the first netif? */
179 if (netif_list == netif) {
180 netif_list = netif->next;
184 /* look for netif further down the list */
185 struct netif * tmpNetif;
186 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
187 if (tmpNetif->next == netif) {
188 tmpNetif->next = netif->next;
193 if (tmpNetif == NULL)
194 return; /* we didn't find any netif today */
196 /* this netif is default? */
197 if (netif_default == netif)
198 /* reset default netif */
199 netif_default = NULL;
200 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
204 * Find a network interface by searching for its name
206 * @param name the name of the netif (like netif->name) plus concatenated number
207 * in ascii representation (e.g. 'en0')
210 netif_find(char *name)
221 for(netif = netif_list; netif != NULL; netif = netif->next) {
222 if (num == netif->num &&
223 name[0] == netif->name[0] &&
224 name[1] == netif->name[1]) {
225 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
229 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
234 * Change the IP address of a network interface
236 * @param netif the network interface to change
237 * @param ipaddr the new IP address
239 * @note call netif_set_addr() if you also want to change netmask and
243 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
245 /* TODO: Handling of obsolete pcbs */
246 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
249 struct tcp_pcb_listen *lpcb;
251 /* address is actually being changed? */
252 if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
254 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
255 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n"));
256 pcb = tcp_active_pcbs;
257 while (pcb != NULL) {
258 /* PCB bound to current local interface address? */
259 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
260 /* this connection must be aborted */
261 struct tcp_pcb *next = pcb->next;
262 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
269 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
270 /* PCB bound to current local interface address? */
271 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
272 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
273 /* The PCB is listening to the old ipaddr and
274 * is set to listen to the new one instead */
275 ip_addr_set(&(lpcb->local_ip), ipaddr);
280 snmp_delete_ipaddridx_tree(netif);
281 snmp_delete_iprteidx_tree(0,netif);
282 /* set new IP address to netif */
283 ip_addr_set(&(netif->ip_addr), ipaddr);
284 snmp_insert_ipaddridx_tree(netif);
285 snmp_insert_iprteidx_tree(0,netif);
287 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",
288 netif->name[0], netif->name[1],
289 ip4_addr1(&netif->ip_addr),
290 ip4_addr2(&netif->ip_addr),
291 ip4_addr3(&netif->ip_addr),
292 ip4_addr4(&netif->ip_addr)));
296 * Change the default gateway for a network interface
298 * @param netif the network interface to change
299 * @param gw the new default gateway
301 * @note call netif_set_addr() if you also want to change ip address and netmask
304 netif_set_gw(struct netif *netif, struct ip_addr *gw)
306 ip_addr_set(&(netif->gw), gw);
307 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",
308 netif->name[0], netif->name[1],
309 ip4_addr1(&netif->gw),
310 ip4_addr2(&netif->gw),
311 ip4_addr3(&netif->gw),
312 ip4_addr4(&netif->gw)));
316 * Change the netmask of a network interface
318 * @param netif the network interface to change
319 * @param netmask the new netmask
321 * @note call netif_set_addr() if you also want to change ip address and
325 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
327 snmp_delete_iprteidx_tree(0, netif);
328 /* set new netmask to netif */
329 ip_addr_set(&(netif->netmask), netmask);
330 snmp_insert_iprteidx_tree(0, netif);
331 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",
332 netif->name[0], netif->name[1],
333 ip4_addr1(&netif->netmask),
334 ip4_addr2(&netif->netmask),
335 ip4_addr3(&netif->netmask),
336 ip4_addr4(&netif->netmask)));
340 * Set a network interface as the default network interface
341 * (used to output all packets for which no specific route is found)
343 * @param netif the default network interface
346 netif_set_default(struct netif *netif)
350 /* remove default route */
351 snmp_delete_iprteidx_tree(1, netif);
355 /* install default route */
356 snmp_insert_iprteidx_tree(1, netif);
358 netif_default = netif;
359 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
360 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
364 * Bring an interface up, available for processing
367 * @note: Enabling DHCP on a down interface will make it come
368 * up once configured.
372 void netif_set_up(struct netif *netif)
374 if ( !(netif->flags & NETIF_FLAG_UP )) {
375 netif->flags |= NETIF_FLAG_UP;
378 snmp_get_sysuptime(&netif->ts);
379 #endif /* LWIP_SNMP */
381 NETIF_LINK_CALLBACK(netif);
382 NETIF_STATUS_CALLBACK(netif);
385 /** For Ethernet network interfaces, we would like to send a
386 * "gratuitous ARP"; this is an ARP packet sent by a node in order
387 * to spontaneously cause other nodes to update an entry in their
388 * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
390 if (netif->flags & NETIF_FLAG_ETHARP) {
391 etharp_query(netif, &(netif->ip_addr), NULL);
393 #endif /* LWIP_ARP */
399 * Ask if an interface is up
401 u8_t netif_is_up(struct netif *netif)
403 return (netif->flags & NETIF_FLAG_UP)?1:0;
407 * Bring an interface down, disabling any traffic processing.
409 * @note: Enabling DHCP on a down interface will make it come
410 * up once configured.
414 void netif_set_down(struct netif *netif)
416 if ( netif->flags & NETIF_FLAG_UP )
418 netif->flags &= ~NETIF_FLAG_UP;
420 snmp_get_sysuptime(&netif->ts);
423 NETIF_LINK_CALLBACK(netif);
424 NETIF_STATUS_CALLBACK(netif);
428 #if LWIP_NETIF_STATUS_CALLBACK
430 * Set callback to be called when interface is brought up/down
432 void netif_set_status_callback( struct netif *netif, void (* status_callback)(struct netif *netif ))
435 netif->status_callback = status_callback;
437 #endif /* LWIP_NETIF_STATUS_CALLBACK */
439 #if LWIP_NETIF_LINK_CALLBACK
441 * Set callback to be called when link is brought up/down
443 void netif_set_link_callback( struct netif *netif, void (* link_callback)(struct netif *netif ))
446 netif->link_callback = link_callback;
450 * Called by a driver when its link goes down
452 void netif_set_link_down( struct netif *netif )
454 netif->flags &= ~NETIF_FLAG_LINK_UP;
455 NETIF_LINK_CALLBACK(netif);
459 * Called by a driver when its link goes up
461 void netif_set_link_up( struct netif *netif )
463 netif->flags |= NETIF_FLAG_LINK_UP;
466 /** For Ethernet network interfaces, we would like to send a
467 * "gratuitous ARP"; this is an ARP packet sent by a node in order
468 * to spontaneously cause other nodes to update an entry in their
469 * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
471 if (netif->flags & NETIF_FLAG_ETHARP) {
472 etharp_query(netif, &(netif->ip_addr), NULL);
474 #endif /* LWIP_ARP */
476 NETIF_LINK_CALLBACK(netif);
478 #endif /* LWIP_NETIF_LINK_CALLBACK */