]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/netif.c
Changed index structs to mib_list_node structs to place the table index trees directl...
[pes-rpp/rpp-lwip.git] / src / core / netif.c
1 /**
2  * @file
3  *
4  * lwIP network interface abstraction
5  */
6
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
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.
21  *
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
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38
39 #include "lwip/opt.h"
40
41 #include "lwip/def.h"
42 #include "lwip/ip_addr.h"
43 #include "lwip/netif.h"
44 #include "lwip/tcp.h"
45 #if LWIP_SNMP
46 #include "lwip/snmp.h"
47 #endif
48
49 struct netif *netif_list = NULL;
50 struct netif *netif_default = NULL;
51
52 /**
53  * Add a network interface to the list of lwIP netifs.
54  *
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.
63  *
64  * @return netif, or NULL if failed.
65  */
66 struct netif *
67 netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
68   struct ip_addr *gw,
69   void *state,
70   err_t (* init)(struct netif *netif),
71   err_t (* input)(struct pbuf *p, struct netif *netif))
72 {
73   static s16_t netifnum = 0;
74   
75 #if LWIP_DHCP
76   /* netif not under DHCP control by default */
77   netif->dhcp = NULL;
78 #endif
79   /* remember netif specific state information data */
80   netif->state = state;
81   netif->num = netifnum++;
82   netif->input = input;
83
84   netif_set_addr(netif, ipaddr, netmask, gw);
85
86   /* call user specified initialization function for netif */
87   if (init(netif) != ERR_OK) {
88     return NULL;
89   }
90
91   /* add this netif to the list */
92   netif->next = netif_list;
93   netif_list = netif;
94   snmp_inc_iflist();
95
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"));
104   return netif;
105 }
106
107 void
108 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
109     struct ip_addr *gw)
110 {
111   netif_set_ipaddr(netif, ipaddr);
112   netif_set_netmask(netif, netmask);
113   netif_set_gw(netif, gw);
114 }
115
116 void netif_remove(struct netif * netif)
117 {
118   if ( netif == NULL ) return;
119
120   snmp_delete_ipaddridx_tree(netif);
121
122   /*  is it the first netif? */
123   if (netif_list == netif) {
124     netif_list = netif->next;
125     snmp_dec_iflist();
126   }
127   else {
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;
133         snmp_dec_iflist();
134         break;
135       }
136     }
137     if (tmpNetif == NULL)
138       return; /*  we didn't find any netif today */
139   }
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") );
145 }
146
147 struct netif *
148 netif_find(char *name)
149 {
150   struct netif *netif;
151   u8_t num;
152
153   if (name == NULL) {
154     return NULL;
155   }
156
157   num = name[2] - '0';
158
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]));
164       return netif;
165     }
166   }
167   LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
168   return NULL;
169 }
170
171 void
172 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
173 {
174   /* TODO: Handling of obsolete pcbs */
175   /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
176 #if LWIP_TCP
177   struct tcp_pcb *pcb;
178   struct tcp_pcb_listen *lpcb;
179
180   /* address is actually being changed? */
181   if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
182   {
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));
192         tcp_abort(pcb);
193         pcb = next;
194       } else {
195         pcb = pcb->next;
196       }
197     }
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);
204       }
205     }
206   }
207 #endif
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);
214
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.
220    */ 
221   etharp_query(netif, ipaddr, NULL);
222 #endif
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)));
229 }
230
231 void
232 netif_set_gw(struct netif *netif, struct ip_addr *gw)
233 {
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)));
241 }
242
243 void
244 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
245 {
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)));
256 }
257
258 void
259 netif_set_default(struct netif *netif)
260 {
261   if (netif == NULL)
262   {
263     /* remove default route */
264     snmp_delete_iprteidx_tree(1, netif);
265   }
266   else
267   {
268     /* install default route */
269     snmp_insert_iprteidx_tree(1, netif);
270   }
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] : '\''));
274 }
275
276 /**
277  * Bring an interface up, available for processing
278  * traffic.
279  * 
280  * @note: Enabling DHCP on a down interface will make it come
281  * up once configured.
282  * 
283  * @see dhcp_start()
284  */ 
285 void netif_set_up(struct netif *netif)
286 {
287   netif->flags |= NETIF_FLAG_UP;
288 #if LWIP_SNMP
289   snmp_get_sysuptime(&netif->ts);
290 #endif
291 }
292
293 /**
294  * Ask if an interface is up
295  */ 
296 u8_t netif_is_up(struct netif *netif)
297 {
298   return (netif->flags & NETIF_FLAG_UP)?1:0;
299 }
300
301 /**
302  * Bring an interface down, disabling any traffic processing.
303  *
304  * @note: Enabling DHCP on a down interface will make it come
305  * up once configured.
306  * 
307  * @see dhcp_start()
308  */ 
309 void netif_set_down(struct netif *netif)
310 {
311   netif->flags &= ~NETIF_FLAG_UP;
312 #if LWIP_SNMP
313   snmp_get_sysuptime(&netif->ts);
314 #endif
315 }
316
317 void
318 netif_init(void)
319 {
320   netif_list = netif_default = NULL;
321 }
322