]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/netif.c
netif.h, netif.c, opt.h: Rename LWIP_NETIF_CALLBACK in LWIP_NETIF_STATUS_CALLBACK...
[pes-rpp/rpp-lwip.git] / src / core / netif.c
1 /**
2  * @file
3  * lwIP network interface abstraction
4  *
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 #include "lwip/snmp.h"
46
47 #if LWIP_ARP
48 #include "netif/etharp.h"
49 #endif /* LWIP_ARP */
50
51 #if LWIP_NETIF_STATUS_CALLBACK
52 #define NETIF_STATUS_CALLBACK(n) { if (n->status_callback) (n->status_callback)(n); }
53 #else
54 #define NETIF_STATUS_CALLBACK(n) { /* NOP */ }
55 #endif /* LWIP_NETIF_STATUS_CALLBACK */ 
56
57 #if LWIP_NETIF_LINK_CALLBACK
58 #define NETIF_LINK_CALLBACK(n) { if (n->link_callback) (n->link_callback)(n); }
59 #else
60 #define NETIF_LINK_CALLBACK(n) { /* NOP */ }
61 #endif /* LWIP_NETIF_LINK_CALLBACK */ 
62
63 struct netif *netif_list = NULL;
64 struct netif *netif_default = NULL;
65
66 /**
67  * Initialize this module
68  */
69 void
70 netif_init(void)
71 {
72   netif_list = netif_default = NULL;
73 }
74
75 /**
76  * Add a network interface to the list of lwIP netifs.
77  *
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.
86  *
87  * @return netif, or NULL if failed.
88  */
89 struct netif *
90 netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
91   struct ip_addr *gw,
92   void *state,
93   err_t (* init)(struct netif *netif),
94   err_t (* input)(struct pbuf *p, struct netif *netif))
95 {
96   static u8_t netifnum = 0;
97
98   /* reset new interface configuration state */
99   netif->ip_addr.addr = 0;
100   netif->netmask.addr = 0;
101   netif->gw.addr = 0;
102   netif->flags = 0;
103 #if LWIP_DHCP
104   /* netif not under DHCP control by default */
105   netif->dhcp = NULL;
106 #endif /* LWIP_DHCP */
107 #if LWIP_AUTOIP
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 */
117
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*/
125
126   netif_set_addr(netif, ipaddr, netmask, gw);
127
128   /* call user specified initialization function for netif */
129   if (init(netif) != ERR_OK) {
130     return NULL;
131   }
132
133   /* add this netif to the list */
134   netif->next = netif_list;
135   netif_list = netif;
136   snmp_inc_iflist();
137
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"));
146   return netif;
147 }
148
149 /**
150  * Change IP address configuration for a network interface (including netmask
151  * and default gateway).
152  *
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
157  */
158 void
159 netif_set_addr(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
160     struct ip_addr *gw)
161 {
162   netif_set_ipaddr(netif, ipaddr);
163   netif_set_netmask(netif, netmask);
164   netif_set_gw(netif, gw);
165 }
166
167 /**
168  * Remove a network interface from the list of lwIP netifs.
169  *
170  * @param netif the network interface to remove
171  */
172 void netif_remove(struct netif * netif)
173 {
174   if ( netif == NULL ) return;
175
176   snmp_delete_ipaddridx_tree(netif);
177
178   /*  is it the first netif? */
179   if (netif_list == netif) {
180     netif_list = netif->next;
181     snmp_dec_iflist();
182   }
183   else {
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;
189         snmp_dec_iflist();
190         break;
191       }
192     }
193     if (tmpNetif == NULL)
194       return; /*  we didn't find any netif today */
195   }
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") );
201 }
202
203 /**
204  * Find a network interface by searching for its name
205  *
206  * @param name the name of the netif (like netif->name) plus concatenated number
207  * in ascii representation (e.g. 'en0')
208  */
209 struct netif *
210 netif_find(char *name)
211 {
212   struct netif *netif;
213   u8_t num;
214
215   if (name == NULL) {
216     return NULL;
217   }
218
219   num = name[2] - '0';
220
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]));
226       return netif;
227     }
228   }
229   LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
230   return NULL;
231 }
232
233 /**
234  * Change the IP address of a network interface
235  *
236  * @param netif the network interface to change
237  * @param ipaddr the new IP address
238  *
239  * @note call netif_set_addr() if you also want to change netmask and
240  * default gateway
241  */
242 void
243 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
244 {
245   /* TODO: Handling of obsolete pcbs */
246   /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
247 #if LWIP_TCP
248   struct tcp_pcb *pcb;
249   struct tcp_pcb_listen *lpcb;
250
251   /* address is actually being changed? */
252   if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
253   {
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));
263         tcp_abort(pcb);
264         pcb = next;
265       } else {
266         pcb = pcb->next;
267       }
268     }
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);
276       }
277     }
278   }
279 #endif
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);
286
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)));
293 }
294
295 /**
296  * Change the default gateway for a network interface
297  *
298  * @param netif the network interface to change
299  * @param gw the new default gateway
300  *
301  * @note call netif_set_addr() if you also want to change ip address and netmask
302  */
303 void
304 netif_set_gw(struct netif *netif, struct ip_addr *gw)
305 {
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)));
313 }
314
315 /**
316  * Change the netmask of a network interface
317  *
318  * @param netif the network interface to change
319  * @param netmask the new netmask
320  *
321  * @note call netif_set_addr() if you also want to change ip address and
322  * default gateway
323  */
324 void
325 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
326 {
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)));
337 }
338
339 /**
340  * Set a network interface as the default network interface
341  * (used to output all packets for which no specific route is found)
342  *
343  * @param netif the default network interface
344  */
345 void
346 netif_set_default(struct netif *netif)
347 {
348   if (netif == NULL)
349   {
350     /* remove default route */
351     snmp_delete_iprteidx_tree(1, netif);
352   }
353   else
354   {
355     /* install default route */
356     snmp_insert_iprteidx_tree(1, netif);
357   }
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] : '\''));
361 }
362
363 /**
364  * Bring an interface up, available for processing
365  * traffic.
366  * 
367  * @note: Enabling DHCP on a down interface will make it come
368  * up once configured.
369  * 
370  * @see dhcp_start()
371  */ 
372 void netif_set_up(struct netif *netif)
373 {
374   if ( !(netif->flags & NETIF_FLAG_UP )) {
375     netif->flags |= NETIF_FLAG_UP;
376     
377 #if LWIP_SNMP
378     snmp_get_sysuptime(&netif->ts);
379 #endif /* LWIP_SNMP */
380
381     NETIF_LINK_CALLBACK(netif);
382     NETIF_STATUS_CALLBACK(netif);
383
384 #if LWIP_ARP
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.
389      */ 
390     if (netif->flags & NETIF_FLAG_ETHARP) {
391       etharp_query(netif, &(netif->ip_addr), NULL);
392     }
393 #endif /* LWIP_ARP */
394     
395   }
396 }
397
398 /**
399  * Ask if an interface is up
400  */ 
401 u8_t netif_is_up(struct netif *netif)
402 {
403   return (netif->flags & NETIF_FLAG_UP)?1:0;
404 }
405
406 /**
407  * Bring an interface down, disabling any traffic processing.
408  *
409  * @note: Enabling DHCP on a down interface will make it come
410  * up once configured.
411  * 
412  * @see dhcp_start()
413  */ 
414 void netif_set_down(struct netif *netif)
415 {
416   if ( netif->flags & NETIF_FLAG_UP )
417     {
418       netif->flags &= ~NETIF_FLAG_UP;
419 #if LWIP_SNMP
420       snmp_get_sysuptime(&netif->ts);
421 #endif
422       
423       NETIF_LINK_CALLBACK(netif);
424       NETIF_STATUS_CALLBACK(netif);
425     }
426 }
427
428 #if LWIP_NETIF_STATUS_CALLBACK
429 /**
430  * Set callback to be called when interface is brought up/down
431  */
432 void netif_set_status_callback( struct netif *netif, void (* status_callback)(struct netif *netif ))
433 {
434     if ( netif )
435         netif->status_callback = status_callback;
436 }
437 #endif /* LWIP_NETIF_STATUS_CALLBACK */
438
439 #if LWIP_NETIF_LINK_CALLBACK
440 /**
441  * Set callback to be called when link is brought up/down
442  */
443 void netif_set_link_callback( struct netif *netif, void (* link_callback)(struct netif *netif ))
444 {
445     if ( netif )
446         netif->link_callback = link_callback;
447 }
448
449 /**
450  * Called by a driver when its link goes down
451  */
452 void netif_set_link_down( struct netif *netif )
453 {
454   netif->flags &= ~NETIF_FLAG_LINK_UP;
455   NETIF_LINK_CALLBACK(netif);
456 }
457
458 /**
459  * Called by a driver when its link goes up
460  */
461 void netif_set_link_up( struct netif *netif )
462 {
463   netif->flags |= NETIF_FLAG_LINK_UP;
464
465 #if LWIP_ARP
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.
470    */ 
471   if (netif->flags & NETIF_FLAG_ETHARP) {
472     etharp_query(netif, &(netif->ip_addr), NULL);
473   }
474 #endif /* LWIP_ARP */
475
476   NETIF_LINK_CALLBACK(netif);
477 }
478 #endif /* LWIP_NETIF_LINK_CALLBACK */