4 * 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"
46 #include "lwip/snmp.h"
49 struct netif *netif_list = NULL;
50 struct netif *netif_default = NULL;
53 * Add a network interface to the list of lwIP netifs.
55 * @param netif a pre-allocated netif structure
56 * @param ipaddr IP address for the new netif
57 * @param netmask network mask for the new netif
58 * @param gw default gateway IP address for the new netif
59 * @param state opaque data passed to the new netif
60 * @param init callback function that initializes the interface
61 * @param input callback function that is called to pass
62 * ingress packets up in the protocol layer stack.
64 * @return netif, or NULL if failed.
67 netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
70 err_t (* init)(struct netif *netif),
71 err_t (* input)(struct pbuf *p, struct netif *netif))
73 static s16_t netifnum = 0;
76 /* netif not under DHCP control by default */
79 /* remember netif specific state information data */
81 netif->num = netifnum++;
84 netif_set_addr(netif, ipaddr, netmask, gw);
86 /* call user specified initialization function for netif */
87 if (init(netif) != ERR_OK) {
91 /* add this netif to the list */
92 netif->next = netif_list;
96 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
97 netif->name[0], netif->name[1]));
98 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
99 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
100 ip_addr_debug_print(NETIF_DEBUG, netmask);
101 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
102 ip_addr_debug_print(NETIF_DEBUG, gw);
103 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
108 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
111 netif_set_ipaddr(netif, ipaddr);
112 netif_set_netmask(netif, netmask);
113 netif_set_gw(netif, gw);
116 void netif_remove(struct netif * netif)
118 if ( netif == NULL ) return;
120 snmp_delete_ipaddridx_tree(netif);
122 /* is it the first netif? */
123 if (netif_list == netif) {
124 netif_list = netif->next;
128 /* look for netif further down the list */
129 struct netif * tmpNetif;
130 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
131 if (tmpNetif->next == netif) {
132 tmpNetif->next = netif->next;
137 if (tmpNetif == NULL)
138 return; /* we didn't find any netif today */
140 /* this netif is default? */
141 if (netif_default == netif)
142 /* reset default netif */
143 netif_default = NULL;
144 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
148 netif_find(char *name)
159 for(netif = netif_list; netif != NULL; netif = netif->next) {
160 if (num == netif->num &&
161 name[0] == netif->name[0] &&
162 name[1] == netif->name[1]) {
163 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
167 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
172 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
174 /* TODO: Handling of obsolete pcbs */
175 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
178 struct tcp_pcb_listen *lpcb;
180 /* address is actually being changed? */
181 if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
183 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
184 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n"));
185 pcb = tcp_active_pcbs;
186 while (pcb != NULL) {
187 /* PCB bound to current local interface address? */
188 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
189 /* this connection must be aborted */
190 struct tcp_pcb *next = pcb->next;
191 LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
198 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
199 /* PCB bound to current local interface address? */
200 if (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr))) {
201 /* The PCB is listening to the old ipaddr and
202 * is set to listen to the new one instead */
203 ip_addr_set(&(lpcb->local_ip), ipaddr);
208 snmp_delete_ipaddridx_tree(netif);
209 snmp_delete_iprteidx_tree(0,netif);
210 /* set new IP address to netif */
211 ip_addr_set(&(netif->ip_addr), ipaddr);
212 snmp_insert_ipaddridx_tree(netif);
213 snmp_insert_iprteidx_tree(0,netif);
215 #if 0 /* only allowed for Ethernet interfaces TODO: how can we check? */
216 /** For Ethernet network interfaces, we would like to send a
217 * "gratuitous ARP"; this is an ARP packet sent by a node in order
218 * to spontaneously cause other nodes to update an entry in their
219 * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
221 etharp_query(netif, ipaddr, NULL);
223 LWIP_DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
224 netif->name[0], netif->name[1],
225 ip4_addr1(&netif->ip_addr),
226 ip4_addr2(&netif->ip_addr),
227 ip4_addr3(&netif->ip_addr),
228 ip4_addr4(&netif->ip_addr)));
232 netif_set_gw(struct netif *netif, struct ip_addr *gw)
234 ip_addr_set(&(netif->gw), gw);
235 LWIP_DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
236 netif->name[0], netif->name[1],
237 ip4_addr1(&netif->gw),
238 ip4_addr2(&netif->gw),
239 ip4_addr3(&netif->gw),
240 ip4_addr4(&netif->gw)));
244 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
246 snmp_delete_iprteidx_tree(0, netif);
247 /* set new netmask to netif */
248 ip_addr_set(&(netif->netmask), netmask);
249 snmp_insert_iprteidx_tree(0, netif);
250 LWIP_DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
251 netif->name[0], netif->name[1],
252 ip4_addr1(&netif->netmask),
253 ip4_addr2(&netif->netmask),
254 ip4_addr3(&netif->netmask),
255 ip4_addr4(&netif->netmask)));
259 netif_set_default(struct netif *netif)
263 /* remove default route */
264 snmp_delete_iprteidx_tree(1, netif);
268 /* install default route */
269 snmp_insert_iprteidx_tree(1, netif);
271 netif_default = netif;
272 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
273 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
277 * Bring an interface up, available for processing
280 * @note: Enabling DHCP on a down interface will make it come
281 * up once configured.
285 void netif_set_up(struct netif *netif)
287 netif->flags |= NETIF_FLAG_UP;
289 snmp_get_sysuptime(&netif->ts);
294 * Ask if an interface is up
296 u8_t netif_is_up(struct netif *netif)
298 return (netif->flags & NETIF_FLAG_UP)?1:0;
302 * Bring an interface down, disabling any traffic processing.
304 * @note: Enabling DHCP on a down interface will make it come
305 * up once configured.
309 void netif_set_down(struct netif *netif)
311 netif->flags &= ~NETIF_FLAG_UP;
313 snmp_get_sysuptime(&netif->ts);
320 netif_list = netif_default = NULL;