1 /**
2 * @file
3 * lwIP network interface abstraction
4 *
5 * @defgroup netif Network interface (NETIF)
6 * @ingroup callbackstyle_api
7 *
8 * @defgroup netif_ip4 IPv4 address handling
9 * @ingroup netif
10 *
11 * @defgroup netif_ip6 IPv6 address handling
12 * @ingroup netif
13 *
14 * @defgroup netif_cd Client data handling
15 * Store data (void*) on a netif for application usage.
16 * @see @ref LWIP_NUM_NETIF_CLIENT_DATA
17 * @ingroup netif
18 */
19
20 /*
21 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without modification,
25 * are permitted provided that the following conditions are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright notice,
28 * this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright notice,
30 * this list of conditions and the following disclaimer in the documentation
31 * and/or other materials provided with the distribution.
32 * 3. The name of the author may not be used to endorse or promote products
33 * derived from this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
38 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
44 * OF SUCH DAMAGE.
45 *
46 * This file is part of the lwIP TCP/IP stack.
47 *
48 * Author: Adam Dunkels <adam@sics.se>
49 */
50
51 #include "lwip/opt.h"
52
53 #include <string.h> /* memset */
54 #include <stdlib.h> /* atoi */
55
56 #include "lwip/def.h"
57 #include "lwip/ip_addr.h"
58 #include "lwip/ip6_addr.h"
59 #include "lwip/netif.h"
60 #include "lwip/priv/tcp_priv.h"
61 #include "lwip/udp.h"
62 #include "lwip/priv/raw_priv.h"
63 #include "lwip/snmp.h"
64 #include "lwip/igmp.h"
65 #include "lwip/etharp.h"
66 #include "lwip/stats.h"
67 #include "lwip/sys.h"
68 #include "lwip/ip.h"
69 #if ENABLE_LOOPBACK
70 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
71 #include "lwip/tcpip.h"
72 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
73 #endif /* ENABLE_LOOPBACK */
74
75 #include "netif/ethernet.h"
76
77 #if LWIP_AUTOIP
78 #include "lwip/autoip.h"
79 #endif /* LWIP_AUTOIP */
80 #if LWIP_DHCP
81 #include "lwip/dhcp.h"
82 #endif /* LWIP_DHCP */
83 #if LWIP_IPV6_DHCP6
84 #include "lwip/dhcp6.h"
85 #endif /* LWIP_IPV6_DHCP6 */
86 #if LWIP_IPV6_MLD
87 #include "lwip/mld6.h"
88 #endif /* LWIP_IPV6_MLD */
89 #if LWIP_IPV6
90 #include "lwip/nd6.h"
91 #endif
92
93 #if LWIP_NETIF_STATUS_CALLBACK
94 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
95 #else
96 #define NETIF_STATUS_CALLBACK(n)
97 #endif /* LWIP_NETIF_STATUS_CALLBACK */
98
99 #if LWIP_NETIF_LINK_CALLBACK
100 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
101 #else
102 #define NETIF_LINK_CALLBACK(n)
103 #endif /* LWIP_NETIF_LINK_CALLBACK */
104
105 #if LWIP_NETIF_EXT_STATUS_CALLBACK
106 static netif_ext_callback_t *ext_callback;
107 #endif
108
109 #if !LWIP_SINGLE_NETIF
110 struct netif *netif_list;
111 #endif /* !LWIP_SINGLE_NETIF */
112 struct netif *netif_default;
113
114 #define netif_index_to_num(index) ((index) - 1)
115 static u8_t netif_num;
116
117 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
118 static u8_t netif_client_id;
119 #endif
120
121 #define NETIF_REPORT_TYPE_IPV4 0x01
122 #define NETIF_REPORT_TYPE_IPV6 0x02
123 static void netif_issue_reports(struct netif *netif, u8_t report_type);
124
125 #if LWIP_IPV6
126 static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
127 #endif /* LWIP_IPV6 */
128 #if LWIP_IPV4
129 static err_t netif_null_output_ip4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr);
130 #endif /* LWIP_IPV4 */
131
132 #if LWIP_HAVE_LOOPIF
133 #if LWIP_IPV4
134 static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr);
135 #endif
136 #if LWIP_IPV6
137 static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr);
138 #endif
139
140
141 static struct netif loop_netif;
142
143 /**
144 * Initialize a lwip network interface structure for a loopback interface
145 *
146 * @param netif the lwip network interface structure for this loopif
147 * @return ERR_OK if the loopif is initialized
148 * ERR_MEM if private data couldn't be allocated
149 */
150 static err_t
netif_loopif_init(struct netif * netif)151 netif_loopif_init(struct netif *netif)
152 {
153 LWIP_ASSERT("netif_loopif_init: invalid netif", netif != NULL);
154
155 /* initialize the snmp variables and counters inside the struct netif
156 * ifSpeed: no assumption can be made!
157 */
158 MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
159
160 netif->name[0] = 'l';
161 netif->name[1] = 'o';
162 #if LWIP_IPV4
163 netif->output = netif_loop_output_ipv4;
164 #endif
165 #if LWIP_IPV6
166 netif->output_ip6 = netif_loop_output_ipv6;
167 #endif
168 #if LWIP_LOOPIF_MULTICAST
169 netif_set_flags(netif, NETIF_FLAG_IGMP);
170 #endif
171 NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_DISABLE_ALL);
172 return ERR_OK;
173 }
174 #endif /* LWIP_HAVE_LOOPIF */
175
176 void
netif_init(void)177 netif_init(void)
178 {
179 #if LWIP_HAVE_LOOPIF
180 #if LWIP_IPV4
181 #define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw,
182 ip4_addr_t loop_ipaddr, loop_netmask, loop_gw;
183 IP4_ADDR(&loop_gw, 127, 0, 0, 1);
184 IP4_ADDR(&loop_ipaddr, 127, 0, 0, 1);
185 IP4_ADDR(&loop_netmask, 255, 0, 0, 0);
186 #else /* LWIP_IPV4 */
187 #define LOOPIF_ADDRINIT
188 #endif /* LWIP_IPV4 */
189
190 #if NO_SYS
191 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
192 #else /* NO_SYS */
193 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
194 #endif /* NO_SYS */
195
196 #if LWIP_IPV6
197 IP_ADDR6_HOST(loop_netif.ip6_addr, 0, 0, 0, 0x00000001UL);
198 loop_netif.ip6_addr_state[0] = IP6_ADDR_VALID;
199 #endif /* LWIP_IPV6 */
200
201 netif_set_link_up(&loop_netif);
202 netif_set_up(&loop_netif);
203
204 #endif /* LWIP_HAVE_LOOPIF */
205 }
206
207 /**
208 * @ingroup lwip_nosys
209 * Forwards a received packet for input processing with
210 * ethernet_input() or ip_input() depending on netif flags.
211 * Don't call directly, pass to netif_add() and call
212 * netif->input().
213 * Only works if the netif driver correctly sets
214 * NETIF_FLAG_ETHARP and/or NETIF_FLAG_ETHERNET flag!
215 */
216 err_t
netif_input(struct pbuf * p,struct netif * inp)217 netif_input(struct pbuf *p, struct netif *inp)
218 {
219 LWIP_ASSERT_CORE_LOCKED();
220
221 LWIP_ASSERT("netif_input: invalid pbuf", p != NULL);
222 LWIP_ASSERT("netif_input: invalid netif", inp != NULL);
223
224 #if LWIP_ETHERNET
225 if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
226 return ethernet_input(p, inp);
227 } else
228 #endif /* LWIP_ETHERNET */
229 return ip_input(p, inp);
230 }
231
232 /**
233 * @ingroup netif
234 * Add a network interface to the list of lwIP netifs.
235 *
236 * Same as @ref netif_add but without IPv4 addresses
237 */
238 struct netif *
netif_add_noaddr(struct netif * netif,void * state,netif_init_fn init,netif_input_fn input)239 netif_add_noaddr(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
240 {
241 return netif_add(netif,
242 #if LWIP_IPV4
243 NULL, NULL, NULL,
244 #endif /* LWIP_IPV4*/
245 state, init, input);
246 }
247
248 /**
249 * @ingroup netif
250 * Add a network interface to the list of lwIP netifs.
251 *
252 * @param netif a pre-allocated netif structure
253 * @param ipaddr IP address for the new netif
254 * @param netmask network mask for the new netif
255 * @param gw default gateway IP address for the new netif
256 * @param state opaque data passed to the new netif
257 * @param init callback function that initializes the interface
258 * @param input callback function that is called to pass
259 * ingress packets up in the protocol layer stack.\n
260 * It is recommended to use a function that passes the input directly
261 * to the stack (netif_input(), NO_SYS=1 mode) or via sending a
262 * message to TCPIP thread (tcpip_input(), NO_SYS=0 mode).\n
263 * These functions use netif flags NETIF_FLAG_ETHARP and NETIF_FLAG_ETHERNET
264 * to decide whether to forward to ethernet_input() or ip_input().
265 * In other words, the functions only work when the netif
266 * driver is implemented correctly!\n
267 * Most members of struct netif should be be initialized by the
268 * netif init function = netif driver (init parameter of this function).\n
269 * IPv6: Don't forget to call netif_create_ip6_linklocal_address() after
270 * setting the MAC address in struct netif.hwaddr
271 * (IPv6 requires a link-local address).
272 *
273 * @return netif, or NULL if failed.
274 */
275 struct netif *
netif_add(struct netif * netif,const ip4_addr_t * ipaddr,const ip4_addr_t * netmask,const ip4_addr_t * gw,void * state,netif_init_fn init,netif_input_fn input)276 netif_add(struct netif *netif,
277 #if LWIP_IPV4
278 const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
279 #endif /* LWIP_IPV4 */
280 void *state, netif_init_fn init, netif_input_fn input)
281 {
282 #if LWIP_IPV6
283 s8_t i;
284 #endif
285
286 LWIP_ASSERT_CORE_LOCKED();
287
288 #if LWIP_SINGLE_NETIF
289 if (netif_default != NULL) {
290 LWIP_ASSERT("single netif already set", 0);
291 return NULL;
292 }
293 #endif
294
295 LWIP_ERROR("netif_add: invalid netif", netif != NULL, return NULL);
296 LWIP_ERROR("netif_add: No init function given", init != NULL, return NULL);
297
298 #if LWIP_IPV4
299 if (ipaddr == NULL) {
300 ipaddr = ip_2_ip4(IP4_ADDR_ANY);
301 }
302 if (netmask == NULL) {
303 netmask = ip_2_ip4(IP4_ADDR_ANY);
304 }
305 if (gw == NULL) {
306 gw = ip_2_ip4(IP4_ADDR_ANY);
307 }
308
309 /* reset new interface configuration state */
310 ip_addr_set_zero_ip4(&netif->ip_addr);
311 ip_addr_set_zero_ip4(&netif->netmask);
312 ip_addr_set_zero_ip4(&netif->gw);
313 netif->output = netif_null_output_ip4;
314 #endif /* LWIP_IPV4 */
315 #if LWIP_IPV6
316 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
317 ip_addr_set_zero_ip6(&netif->ip6_addr[i]);
318 netif->ip6_addr_state[i] = IP6_ADDR_INVALID;
319 #if LWIP_IPV6_ADDRESS_LIFETIMES
320 netif->ip6_addr_valid_life[i] = IP6_ADDR_LIFE_STATIC;
321 netif->ip6_addr_pref_life[i] = IP6_ADDR_LIFE_STATIC;
322 #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */
323 }
324 netif->output_ip6 = netif_null_output_ip6;
325 #endif /* LWIP_IPV6 */
326 NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
327 netif->mtu = 0;
328 netif->flags = 0;
329 #ifdef netif_get_client_data
330 memset(netif->client_data, 0, sizeof(netif->client_data));
331 #endif /* LWIP_NUM_NETIF_CLIENT_DATA */
332 #if LWIP_IPV6
333 #if LWIP_IPV6_AUTOCONFIG
334 /* IPv6 address autoconfiguration not enabled by default */
335 netif->ip6_autoconfig_enabled = 0;
336 #endif /* LWIP_IPV6_AUTOCONFIG */
337 nd6_restart_netif(netif);
338 #endif /* LWIP_IPV6 */
339 #if LWIP_NETIF_STATUS_CALLBACK
340 netif->status_callback = NULL;
341 #endif /* LWIP_NETIF_STATUS_CALLBACK */
342 #if LWIP_NETIF_LINK_CALLBACK
343 netif->link_callback = NULL;
344 #endif /* LWIP_NETIF_LINK_CALLBACK */
345 #if LWIP_IGMP
346 netif->igmp_mac_filter = NULL;
347 #endif /* LWIP_IGMP */
348 #if LWIP_IPV6 && LWIP_IPV6_MLD
349 netif->mld_mac_filter = NULL;
350 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
351
352 /* remember netif specific state information data */
353 netif->state = state;
354 netif->num = netif_num;
355 netif->input = input;
356
357 NETIF_RESET_HINTS(netif);
358 #if ENABLE_LOOPBACK
359 netif->loop_first = NULL;
360 netif->loop_last = NULL;
361 #if LWIP_LOOPBACK_MAX_PBUFS
362 netif->loop_cnt_current = 0;
363 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
364 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
365 netif->reschedule_poll = 0;
366 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
367 #endif /* ENABLE_LOOPBACK */
368
369 #if LWIP_IPV4
370 netif_set_addr(netif, ipaddr, netmask, gw);
371 #endif /* LWIP_IPV4 */
372
373 /* call user specified initialization function for netif */
374 if (init(netif) != ERR_OK) {
375 return NULL;
376 }
377 #if LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES
378 /* Initialize the MTU for IPv6 to the one set by the netif driver.
379 This can be updated later by RA. */
380 netif->mtu6 = netif->mtu;
381 #endif /* LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES */
382
383 #if !LWIP_SINGLE_NETIF
384 /* Assign a unique netif number in the range [0..254], so that (num+1) can
385 serve as an interface index that fits in a u8_t.
386 We assume that the new netif has not yet been added to the list here.
387 This algorithm is O(n^2), but that should be OK for lwIP.
388 */
389 {
390 struct netif *netif2;
391 int num_netifs;
392 do {
393 if (netif->num == 255) {
394 netif->num = 0;
395 }
396 num_netifs = 0;
397 for (netif2 = netif_list; netif2 != NULL; netif2 = netif2->next) {
398 LWIP_ASSERT("netif already added", netif2 != netif);
399 num_netifs++;
400 LWIP_ASSERT("too many netifs, max. supported number is 255", num_netifs <= 255);
401 if (netif2->num == netif->num) {
402 netif->num++;
403 break;
404 }
405 }
406 } while (netif2 != NULL);
407 }
408 if (netif->num == 254) {
409 netif_num = 0;
410 } else {
411 netif_num = (u8_t)(netif->num + 1);
412 }
413
414 /* add this netif to the list */
415 netif->next = netif_list;
416 netif_list = netif;
417 #endif /* "LWIP_SINGLE_NETIF */
418 mib2_netif_added(netif);
419
420 #if LWIP_IGMP
421 /* start IGMP processing */
422 if (netif->flags & NETIF_FLAG_IGMP) {
423 igmp_start(netif);
424 }
425 #endif /* LWIP_IGMP */
426
427 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
428 netif->name[0], netif->name[1]));
429 #if LWIP_IPV4
430 LWIP_DEBUGF(NETIF_DEBUG, (" addr "));
431 ip4_addr_debug_print(NETIF_DEBUG, ipaddr);
432 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
433 ip4_addr_debug_print(NETIF_DEBUG, netmask);
434 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
435 ip4_addr_debug_print(NETIF_DEBUG, gw);
436 #endif /* LWIP_IPV4 */
437 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
438
439 netif_invoke_ext_callback(netif, LWIP_NSC_NETIF_ADDED, NULL);
440
441 return netif;
442 }
443
444 static void
netif_do_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)445 netif_do_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
446 {
447 #if LWIP_TCP
448 tcp_netif_ip_addr_changed(old_addr, new_addr);
449 #endif /* LWIP_TCP */
450 #if LWIP_UDP
451 udp_netif_ip_addr_changed(old_addr, new_addr);
452 #endif /* LWIP_UDP */
453 #if LWIP_RAW
454 raw_netif_ip_addr_changed(old_addr, new_addr);
455 #endif /* LWIP_RAW */
456 }
457
458 #if LWIP_IPV4
459 static int
netif_do_set_ipaddr(struct netif * netif,const ip4_addr_t * ipaddr,ip_addr_t * old_addr)460 netif_do_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr, ip_addr_t *old_addr)
461 {
462 LWIP_ASSERT("invalid pointer", ipaddr != NULL);
463 LWIP_ASSERT("invalid pointer", old_addr != NULL);
464
465 /* address is actually being changed? */
466 if (ip4_addr_cmp(ipaddr, netif_ip4_addr(netif)) == 0) {
467 ip_addr_t new_addr;
468 *ip_2_ip4(&new_addr) = *ipaddr;
469 IP_SET_TYPE_VAL(new_addr, IPADDR_TYPE_V4);
470
471 ip_addr_copy(*old_addr, *netif_ip_addr4(netif));
472
473 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
474 netif_do_ip_addr_changed(old_addr, &new_addr);
475
476 mib2_remove_ip4(netif);
477 mib2_remove_route_ip4(0, netif);
478 /* set new IP address to netif */
479 ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr);
480 IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4);
481 mib2_add_ip4(netif);
482 mib2_add_route_ip4(0, netif);
483
484 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
485
486 NETIF_STATUS_CALLBACK(netif);
487 return 1; /* address changed */
488 }
489 return 0; /* address unchanged */
490 }
491
492 /**
493 * @ingroup netif_ip4
494 * Change the IP address of a network interface
495 *
496 * @param netif the network interface to change
497 * @param ipaddr the new IP address
498 *
499 * @note call netif_set_addr() if you also want to change netmask and
500 * default gateway
501 */
502 void
netif_set_ipaddr(struct netif * netif,const ip4_addr_t * ipaddr)503 netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
504 {
505 ip_addr_t old_addr;
506
507 LWIP_ERROR("netif_set_ipaddr: invalid netif", netif != NULL, return);
508
509 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
510 if (ipaddr == NULL) {
511 ipaddr = IP4_ADDR_ANY4;
512 }
513
514 LWIP_ASSERT_CORE_LOCKED();
515
516 if (netif_do_set_ipaddr(netif, ipaddr, &old_addr)) {
517 #if LWIP_NETIF_EXT_STATUS_CALLBACK
518 netif_ext_callback_args_t args;
519 args.ipv4_changed.old_address = &old_addr;
520 netif_invoke_ext_callback(netif, LWIP_NSC_IPV4_ADDRESS_CHANGED, &args);
521 #endif
522 }
523 }
524
525 static int
netif_do_set_netmask(struct netif * netif,const ip4_addr_t * netmask,ip_addr_t * old_nm)526 netif_do_set_netmask(struct netif *netif, const ip4_addr_t *netmask, ip_addr_t *old_nm)
527 {
528 /* address is actually being changed? */
529 if (ip4_addr_cmp(netmask, netif_ip4_netmask(netif)) == 0) {
530 #if LWIP_NETIF_EXT_STATUS_CALLBACK
531 LWIP_ASSERT("invalid pointer", old_nm != NULL);
532 ip_addr_copy(*old_nm, *netif_ip_netmask4(netif));
533 #else
534 LWIP_UNUSED_ARG(old_nm);
535 #endif
536 mib2_remove_route_ip4(0, netif);
537 /* set new netmask to netif */
538 ip4_addr_set(ip_2_ip4(&netif->netmask), netmask);
539 IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4);
540 mib2_add_route_ip4(0, netif);
541 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
542 netif->name[0], netif->name[1],
543 ip4_addr1_16(netif_ip4_netmask(netif)),
544 ip4_addr2_16(netif_ip4_netmask(netif)),
545 ip4_addr3_16(netif_ip4_netmask(netif)),
546 ip4_addr4_16(netif_ip4_netmask(netif))));
547 return 1; /* netmask changed */
548 }
549 return 0; /* netmask unchanged */
550 }
551
552 /**
553 * @ingroup netif_ip4
554 * Change the netmask of a network interface
555 *
556 * @param netif the network interface to change
557 * @param netmask the new netmask
558 *
559 * @note call netif_set_addr() if you also want to change ip address and
560 * default gateway
561 */
562 void
netif_set_netmask(struct netif * netif,const ip4_addr_t * netmask)563 netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
564 {
565 #if LWIP_NETIF_EXT_STATUS_CALLBACK
566 ip_addr_t old_nm_val;
567 ip_addr_t *old_nm = &old_nm_val;
568 #else
569 ip_addr_t *old_nm = NULL;
570 #endif
571 LWIP_ASSERT_CORE_LOCKED();
572
573 LWIP_ERROR("netif_set_netmask: invalid netif", netif != NULL, return);
574
575 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
576 if (netmask == NULL) {
577 netmask = IP4_ADDR_ANY4;
578 }
579
580 if (netif_do_set_netmask(netif, netmask, old_nm)) {
581 #if LWIP_NETIF_EXT_STATUS_CALLBACK
582 netif_ext_callback_args_t args;
583 args.ipv4_changed.old_netmask = old_nm;
584 netif_invoke_ext_callback(netif, LWIP_NSC_IPV4_NETMASK_CHANGED, &args);
585 #endif
586 }
587 }
588
589 static int
netif_do_set_gw(struct netif * netif,const ip4_addr_t * gw,ip_addr_t * old_gw)590 netif_do_set_gw(struct netif *netif, const ip4_addr_t *gw, ip_addr_t *old_gw)
591 {
592 /* address is actually being changed? */
593 if (ip4_addr_cmp(gw, netif_ip4_gw(netif)) == 0) {
594 #if LWIP_NETIF_EXT_STATUS_CALLBACK
595 LWIP_ASSERT("invalid pointer", old_gw != NULL);
596 ip_addr_copy(*old_gw, *netif_ip_gw4(netif));
597 #else
598 LWIP_UNUSED_ARG(old_gw);
599 #endif
600
601 ip4_addr_set(ip_2_ip4(&netif->gw), gw);
602 IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4);
603 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
604 netif->name[0], netif->name[1],
605 ip4_addr1_16(netif_ip4_gw(netif)),
606 ip4_addr2_16(netif_ip4_gw(netif)),
607 ip4_addr3_16(netif_ip4_gw(netif)),
608 ip4_addr4_16(netif_ip4_gw(netif))));
609 return 1; /* gateway changed */
610 }
611 return 0; /* gateway unchanged */
612 }
613
614 /**
615 * @ingroup netif_ip4
616 * Change the default gateway for a network interface
617 *
618 * @param netif the network interface to change
619 * @param gw the new default gateway
620 *
621 * @note call netif_set_addr() if you also want to change ip address and netmask
622 */
623 void
netif_set_gw(struct netif * netif,const ip4_addr_t * gw)624 netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
625 {
626 #if LWIP_NETIF_EXT_STATUS_CALLBACK
627 ip_addr_t old_gw_val;
628 ip_addr_t *old_gw = &old_gw_val;
629 #else
630 ip_addr_t *old_gw = NULL;
631 #endif
632 LWIP_ASSERT_CORE_LOCKED();
633
634 LWIP_ERROR("netif_set_gw: invalid netif", netif != NULL, return);
635
636 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
637 if (gw == NULL) {
638 gw = IP4_ADDR_ANY4;
639 }
640
641 if (netif_do_set_gw(netif, gw, old_gw)) {
642 #if LWIP_NETIF_EXT_STATUS_CALLBACK
643 netif_ext_callback_args_t args;
644 args.ipv4_changed.old_gw = old_gw;
645 netif_invoke_ext_callback(netif, LWIP_NSC_IPV4_GATEWAY_CHANGED, &args);
646 #endif
647 }
648 }
649
650 /**
651 * @ingroup netif_ip4
652 * Change IP address configuration for a network interface (including netmask
653 * and default gateway).
654 *
655 * @param netif the network interface to change
656 * @param ipaddr the new IP address
657 * @param netmask the new netmask
658 * @param gw the new default gateway
659 */
660 void
netif_set_addr(struct netif * netif,const ip4_addr_t * ipaddr,const ip4_addr_t * netmask,const ip4_addr_t * gw)661 netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
662 const ip4_addr_t *gw)
663 {
664 #if LWIP_NETIF_EXT_STATUS_CALLBACK
665 netif_nsc_reason_t change_reason = LWIP_NSC_NONE;
666 netif_ext_callback_args_t cb_args;
667 ip_addr_t old_nm_val;
668 ip_addr_t old_gw_val;
669 ip_addr_t *old_nm = &old_nm_val;
670 ip_addr_t *old_gw = &old_gw_val;
671 #else
672 ip_addr_t *old_nm = NULL;
673 ip_addr_t *old_gw = NULL;
674 #endif
675 ip_addr_t old_addr;
676 int remove;
677
678 LWIP_ASSERT_CORE_LOCKED();
679
680 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
681 if (ipaddr == NULL) {
682 ipaddr = IP4_ADDR_ANY4;
683 }
684 if (netmask == NULL) {
685 netmask = IP4_ADDR_ANY4;
686 }
687 if (gw == NULL) {
688 gw = IP4_ADDR_ANY4;
689 }
690
691 remove = ip4_addr_isany(ipaddr);
692 if (remove) {
693 /* when removing an address, we have to remove it *before* changing netmask/gw
694 to ensure that tcp RST segment can be sent correctly */
695 if (netif_do_set_ipaddr(netif, ipaddr, &old_addr)) {
696 #if LWIP_NETIF_EXT_STATUS_CALLBACK
697 change_reason |= LWIP_NSC_IPV4_ADDRESS_CHANGED;
698 cb_args.ipv4_changed.old_address = &old_addr;
699 #endif
700 }
701 }
702 if (netif_do_set_netmask(netif, netmask, old_nm)) {
703 #if LWIP_NETIF_EXT_STATUS_CALLBACK
704 change_reason |= LWIP_NSC_IPV4_NETMASK_CHANGED;
705 cb_args.ipv4_changed.old_netmask = old_nm;
706 #endif
707 }
708 if (netif_do_set_gw(netif, gw, old_gw)) {
709 #if LWIP_NETIF_EXT_STATUS_CALLBACK
710 change_reason |= LWIP_NSC_IPV4_GATEWAY_CHANGED;
711 cb_args.ipv4_changed.old_gw = old_gw;
712 #endif
713 }
714 if (!remove) {
715 /* set ipaddr last to ensure netmask/gw have been set when status callback is called */
716 if (netif_do_set_ipaddr(netif, ipaddr, &old_addr)) {
717 #if LWIP_NETIF_EXT_STATUS_CALLBACK
718 change_reason |= LWIP_NSC_IPV4_ADDRESS_CHANGED;
719 cb_args.ipv4_changed.old_address = &old_addr;
720 #endif
721 }
722 }
723
724 #if LWIP_NETIF_EXT_STATUS_CALLBACK
725 if (change_reason != LWIP_NSC_NONE) {
726 change_reason |= LWIP_NSC_IPV4_SETTINGS_CHANGED;
727 netif_invoke_ext_callback(netif, change_reason, &cb_args);
728 }
729 #endif
730 }
731 #endif /* LWIP_IPV4*/
732
733 /**
734 * @ingroup netif
735 * Remove a network interface from the list of lwIP netifs.
736 *
737 * @param netif the network interface to remove
738 */
739 void
netif_remove(struct netif * netif)740 netif_remove(struct netif *netif)
741 {
742 #if LWIP_IPV6
743 int i;
744 #endif
745
746 LWIP_ASSERT_CORE_LOCKED();
747
748 if (netif == NULL) {
749 return;
750 }
751
752 netif_invoke_ext_callback(netif, LWIP_NSC_NETIF_REMOVED, NULL);
753
754 #if LWIP_IPV4
755 if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
756 netif_do_ip_addr_changed(netif_ip_addr4(netif), NULL);
757 }
758
759 #if LWIP_IGMP
760 /* stop IGMP processing */
761 if (netif->flags & NETIF_FLAG_IGMP) {
762 igmp_stop(netif);
763 }
764 #endif /* LWIP_IGMP */
765 #endif /* LWIP_IPV4*/
766
767 #if LWIP_IPV6
768 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
769 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
770 netif_do_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
771 }
772 }
773 #if LWIP_IPV6_MLD
774 /* stop MLD processing */
775 mld6_stop(netif);
776 #endif /* LWIP_IPV6_MLD */
777 #endif /* LWIP_IPV6 */
778 if (netif_is_up(netif)) {
779 /* set netif down before removing (call callback function) */
780 netif_set_down(netif);
781 }
782
783 mib2_remove_ip4(netif);
784
785 /* this netif is default? */
786 if (netif_default == netif) {
787 /* reset default netif */
788 netif_set_default(NULL);
789 }
790 #if !LWIP_SINGLE_NETIF
791 /* is it the first netif? */
792 if (netif_list == netif) {
793 netif_list = netif->next;
794 } else {
795 /* look for netif further down the list */
796 struct netif *tmp_netif;
797 NETIF_FOREACH(tmp_netif) {
798 if (tmp_netif->next == netif) {
799 tmp_netif->next = netif->next;
800 break;
801 }
802 }
803 if (tmp_netif == NULL) {
804 return; /* netif is not on the list */
805 }
806 }
807 #endif /* !LWIP_SINGLE_NETIF */
808 mib2_netif_removed(netif);
809 #if LWIP_NETIF_REMOVE_CALLBACK
810 if (netif->remove_callback) {
811 netif->remove_callback(netif);
812 }
813 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
814 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
815 }
816
817 /**
818 * @ingroup netif
819 * Set a network interface as the default network interface
820 * (used to output all packets for which no specific route is found)
821 *
822 * @param netif the default network interface
823 */
824 void
netif_set_default(struct netif * netif)825 netif_set_default(struct netif *netif)
826 {
827 LWIP_ASSERT_CORE_LOCKED();
828
829 if (netif == NULL) {
830 /* remove default route */
831 mib2_remove_route_ip4(1, netif);
832 } else {
833 /* install default route */
834 mib2_add_route_ip4(1, netif);
835 }
836 netif_default = netif;
837 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
838 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
839 }
840
841 /**
842 * @ingroup netif
843 * Bring an interface up, available for processing
844 * traffic.
845 */
846 void
netif_set_up(struct netif * netif)847 netif_set_up(struct netif *netif)
848 {
849 LWIP_ASSERT_CORE_LOCKED();
850
851 LWIP_ERROR("netif_set_up: invalid netif", netif != NULL, return);
852
853 if (!(netif->flags & NETIF_FLAG_UP)) {
854 netif_set_flags(netif, NETIF_FLAG_UP);
855
856 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
857
858 NETIF_STATUS_CALLBACK(netif);
859
860 #if LWIP_NETIF_EXT_STATUS_CALLBACK
861 {
862 netif_ext_callback_args_t args;
863 args.status_changed.state = 1;
864 netif_invoke_ext_callback(netif, LWIP_NSC_STATUS_CHANGED, &args);
865 }
866 #endif
867
868 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4 | NETIF_REPORT_TYPE_IPV6);
869 #if LWIP_IPV6
870 nd6_restart_netif(netif);
871 #endif /* LWIP_IPV6 */
872 }
873 }
874
875 /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
876 */
877 static void
netif_issue_reports(struct netif * netif,u8_t report_type)878 netif_issue_reports(struct netif *netif, u8_t report_type)
879 {
880 LWIP_ASSERT("netif_issue_reports: invalid netif", netif != NULL);
881
882 /* Only send reports when both link and admin states are up */
883 if (!(netif->flags & NETIF_FLAG_LINK_UP) ||
884 !(netif->flags & NETIF_FLAG_UP)) {
885 return;
886 }
887
888 #if LWIP_IPV4
889 if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
890 !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
891 #if LWIP_ARP
892 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
893 if (netif->flags & (NETIF_FLAG_ETHARP)) {
894 etharp_gratuitous(netif);
895 }
896 #endif /* LWIP_ARP */
897
898 #if LWIP_IGMP
899 /* resend IGMP memberships */
900 if (netif->flags & NETIF_FLAG_IGMP) {
901 igmp_report_groups(netif);
902 }
903 #endif /* LWIP_IGMP */
904 }
905 #endif /* LWIP_IPV4 */
906
907 #if LWIP_IPV6
908 if (report_type & NETIF_REPORT_TYPE_IPV6) {
909 #if LWIP_IPV6_MLD
910 /* send mld memberships */
911 mld6_report_groups(netif);
912 #endif /* LWIP_IPV6_MLD */
913 }
914 #endif /* LWIP_IPV6 */
915 }
916
917 /**
918 * @ingroup netif
919 * Bring an interface down, disabling any traffic processing.
920 */
921 void
netif_set_down(struct netif * netif)922 netif_set_down(struct netif *netif)
923 {
924 LWIP_ASSERT_CORE_LOCKED();
925
926 LWIP_ERROR("netif_set_down: invalid netif", netif != NULL, return);
927
928 if (netif->flags & NETIF_FLAG_UP) {
929 #if LWIP_NETIF_EXT_STATUS_CALLBACK
930 {
931 netif_ext_callback_args_t args;
932 args.status_changed.state = 0;
933 netif_invoke_ext_callback(netif, LWIP_NSC_STATUS_CHANGED, &args);
934 }
935 #endif
936
937 netif_clear_flags(netif, NETIF_FLAG_UP);
938 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
939
940 #if LWIP_IPV4 && LWIP_ARP
941 if (netif->flags & NETIF_FLAG_ETHARP) {
942 etharp_cleanup_netif(netif);
943 }
944 #endif /* LWIP_IPV4 && LWIP_ARP */
945
946 #if LWIP_IPV6
947 nd6_cleanup_netif(netif);
948 #endif /* LWIP_IPV6 */
949
950 NETIF_STATUS_CALLBACK(netif);
951 }
952 }
953
954 #if LWIP_NETIF_STATUS_CALLBACK
955 /**
956 * @ingroup netif
957 * Set callback to be called when interface is brought up/down or address is changed while up
958 */
959 void
netif_set_status_callback(struct netif * netif,netif_status_callback_fn status_callback)960 netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
961 {
962 LWIP_ASSERT_CORE_LOCKED();
963
964 if (netif) {
965 netif->status_callback = status_callback;
966 }
967 }
968 #endif /* LWIP_NETIF_STATUS_CALLBACK */
969
970 #if LWIP_NETIF_REMOVE_CALLBACK
971 /**
972 * @ingroup netif
973 * Set callback to be called when the interface has been removed
974 */
975 void
netif_set_remove_callback(struct netif * netif,netif_status_callback_fn remove_callback)976 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
977 {
978 LWIP_ASSERT_CORE_LOCKED();
979
980 if (netif) {
981 netif->remove_callback = remove_callback;
982 }
983 }
984 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
985
986 /**
987 * @ingroup netif
988 * Called by a driver when its link goes up
989 */
990 void
netif_set_link_up(struct netif * netif)991 netif_set_link_up(struct netif *netif)
992 {
993 LWIP_ASSERT_CORE_LOCKED();
994
995 LWIP_ERROR("netif_set_link_up: invalid netif", netif != NULL, return);
996
997 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
998 netif_set_flags(netif, NETIF_FLAG_LINK_UP);
999
1000 #if LWIP_DHCP
1001 dhcp_network_changed(netif);
1002 #endif /* LWIP_DHCP */
1003
1004 #if LWIP_AUTOIP
1005 autoip_network_changed(netif);
1006 #endif /* LWIP_AUTOIP */
1007
1008 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4 | NETIF_REPORT_TYPE_IPV6);
1009 #if LWIP_IPV6
1010 nd6_restart_netif(netif);
1011 #endif /* LWIP_IPV6 */
1012
1013 NETIF_LINK_CALLBACK(netif);
1014 #if LWIP_NETIF_EXT_STATUS_CALLBACK
1015 {
1016 netif_ext_callback_args_t args;
1017 args.link_changed.state = 1;
1018 netif_invoke_ext_callback(netif, LWIP_NSC_LINK_CHANGED, &args);
1019 }
1020 #endif
1021 }
1022 }
1023
1024 /**
1025 * @ingroup netif
1026 * Called by a driver when its link goes down
1027 */
1028 void
netif_set_link_down(struct netif * netif)1029 netif_set_link_down(struct netif *netif)
1030 {
1031 LWIP_ASSERT_CORE_LOCKED();
1032
1033 LWIP_ERROR("netif_set_link_down: invalid netif", netif != NULL, return);
1034
1035 if (netif->flags & NETIF_FLAG_LINK_UP) {
1036 netif_clear_flags(netif, NETIF_FLAG_LINK_UP);
1037 #if LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES
1038 netif->mtu6 = netif->mtu;
1039 #endif
1040
1041 NETIF_LINK_CALLBACK(netif);
1042 #if LWIP_NETIF_EXT_STATUS_CALLBACK
1043 {
1044 netif_ext_callback_args_t args;
1045 args.link_changed.state = 0;
1046 netif_invoke_ext_callback(netif, LWIP_NSC_LINK_CHANGED, &args);
1047 }
1048 #endif
1049 }
1050 }
1051
1052 #if LWIP_NETIF_LINK_CALLBACK
1053 /**
1054 * @ingroup netif
1055 * Set callback to be called when link is brought up/down
1056 */
1057 void
netif_set_link_callback(struct netif * netif,netif_status_callback_fn link_callback)1058 netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
1059 {
1060 LWIP_ASSERT_CORE_LOCKED();
1061
1062 if (netif) {
1063 netif->link_callback = link_callback;
1064 }
1065 }
1066 #endif /* LWIP_NETIF_LINK_CALLBACK */
1067
1068 #if ENABLE_LOOPBACK
1069 /**
1070 * @ingroup netif
1071 * Send an IP packet to be received on the same netif (loopif-like).
1072 * The pbuf is copied and added to an internal queue which is fed to
1073 * netif->input by netif_poll().
1074 * In multithreaded mode, the call to netif_poll() is queued to be done on the
1075 * TCP/IP thread.
1076 * In callback mode, the user has the responsibility to call netif_poll() in
1077 * the main loop of their application.
1078 *
1079 * @param netif the lwip network interface structure
1080 * @param p the (IP) packet to 'send'
1081 * @return ERR_OK if the packet has been sent
1082 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
1083 */
1084 err_t
netif_loop_output(struct netif * netif,struct pbuf * p)1085 netif_loop_output(struct netif *netif, struct pbuf *p)
1086 {
1087 struct pbuf *r;
1088 err_t err;
1089 struct pbuf *last;
1090 #if LWIP_LOOPBACK_MAX_PBUFS
1091 u16_t clen = 0;
1092 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
1093 /* If we have a loopif, SNMP counters are adjusted for it,
1094 * if not they are adjusted for 'netif'. */
1095 #if MIB2_STATS
1096 #if LWIP_HAVE_LOOPIF
1097 struct netif *stats_if = &loop_netif;
1098 #else /* LWIP_HAVE_LOOPIF */
1099 struct netif *stats_if = netif;
1100 #endif /* LWIP_HAVE_LOOPIF */
1101 #endif /* MIB2_STATS */
1102 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
1103 u8_t schedule_poll = 0;
1104 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
1105 SYS_ARCH_DECL_PROTECT(lev);
1106
1107 LWIP_ASSERT("netif_loop_output: invalid netif", netif != NULL);
1108 LWIP_ASSERT("netif_loop_output: invalid pbuf", p != NULL);
1109
1110 /* Allocate a new pbuf */
1111 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
1112 if (r == NULL) {
1113 LINK_STATS_INC(link.memerr);
1114 LINK_STATS_INC(link.drop);
1115 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
1116 return ERR_MEM;
1117 }
1118 #if LWIP_LOOPBACK_MAX_PBUFS
1119 clen = pbuf_clen(r);
1120 /* check for overflow or too many pbuf on queue */
1121 if (((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
1122 ((netif->loop_cnt_current + clen) > LWIP_MIN(LWIP_LOOPBACK_MAX_PBUFS, 0xFFFF))) {
1123 pbuf_free(r);
1124 LINK_STATS_INC(link.memerr);
1125 LINK_STATS_INC(link.drop);
1126 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
1127 return ERR_MEM;
1128 }
1129 netif->loop_cnt_current = (u16_t)(netif->loop_cnt_current + clen);
1130 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
1131
1132 /* Copy the whole pbuf queue p into the single pbuf r */
1133 if ((err = pbuf_copy(r, p)) != ERR_OK) {
1134 pbuf_free(r);
1135 LINK_STATS_INC(link.memerr);
1136 LINK_STATS_INC(link.drop);
1137 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
1138 return err;
1139 }
1140
1141 /* Put the packet on a linked list which gets emptied through calling
1142 netif_poll(). */
1143
1144 /* let last point to the last pbuf in chain r */
1145 for (last = r; last->next != NULL; last = last->next) {
1146 /* nothing to do here, just get to the last pbuf */
1147 }
1148
1149 SYS_ARCH_PROTECT(lev);
1150 if (netif->loop_first != NULL) {
1151 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
1152 netif->loop_last->next = r;
1153 netif->loop_last = last;
1154 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
1155 if (netif->reschedule_poll) {
1156 schedule_poll = 1;
1157 netif->reschedule_poll = 0;
1158 }
1159 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
1160 } else {
1161 netif->loop_first = r;
1162 netif->loop_last = last;
1163 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
1164 /* No existing packets queued, schedule poll */
1165 schedule_poll = 1;
1166 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
1167 }
1168 SYS_ARCH_UNPROTECT(lev);
1169
1170 LINK_STATS_INC(link.xmit);
1171 MIB2_STATS_NETIF_ADD(stats_if, ifoutoctets, p->tot_len);
1172 MIB2_STATS_NETIF_INC(stats_if, ifoutucastpkts);
1173
1174 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
1175 /* For multithreading environment, schedule a call to netif_poll */
1176 if (schedule_poll) {
1177 if (tcpip_try_callback((tcpip_callback_fn)netif_poll, netif) != ERR_OK) {
1178 SYS_ARCH_PROTECT(lev);
1179 netif->reschedule_poll = 1;
1180 SYS_ARCH_UNPROTECT(lev);
1181 }
1182 }
1183 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
1184
1185 return ERR_OK;
1186 }
1187
1188 #if LWIP_HAVE_LOOPIF
1189 #if LWIP_IPV4
1190 static err_t
netif_loop_output_ipv4(struct netif * netif,struct pbuf * p,const ip4_addr_t * addr)1191 netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr)
1192 {
1193 LWIP_UNUSED_ARG(addr);
1194 return netif_loop_output(netif, p);
1195 }
1196 #endif /* LWIP_IPV4 */
1197
1198 #if LWIP_IPV6
1199 static err_t
netif_loop_output_ipv6(struct netif * netif,struct pbuf * p,const ip6_addr_t * addr)1200 netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr)
1201 {
1202 LWIP_UNUSED_ARG(addr);
1203 return netif_loop_output(netif, p);
1204 }
1205 #endif /* LWIP_IPV6 */
1206 #endif /* LWIP_HAVE_LOOPIF */
1207
1208
1209 /**
1210 * Call netif_poll() in the main loop of your application. This is to prevent
1211 * reentering non-reentrant functions like tcp_input(). Packets passed to
1212 * netif_loop_output() are put on a list that is passed to netif->input() by
1213 * netif_poll().
1214 */
1215 void
netif_poll(struct netif * netif)1216 netif_poll(struct netif *netif)
1217 {
1218 /* If we have a loopif, SNMP counters are adjusted for it,
1219 * if not they are adjusted for 'netif'. */
1220 #if MIB2_STATS
1221 #if LWIP_HAVE_LOOPIF
1222 struct netif *stats_if = &loop_netif;
1223 #else /* LWIP_HAVE_LOOPIF */
1224 struct netif *stats_if = netif;
1225 #endif /* LWIP_HAVE_LOOPIF */
1226 #endif /* MIB2_STATS */
1227 SYS_ARCH_DECL_PROTECT(lev);
1228
1229 LWIP_ASSERT("netif_poll: invalid netif", netif != NULL);
1230
1231 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
1232 SYS_ARCH_PROTECT(lev);
1233 while (netif->loop_first != NULL) {
1234 struct pbuf *in, *in_end;
1235 #if LWIP_LOOPBACK_MAX_PBUFS
1236 u8_t clen = 1;
1237 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
1238
1239 in = in_end = netif->loop_first;
1240 while (in_end->len != in_end->tot_len) {
1241 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
1242 in_end = in_end->next;
1243 #if LWIP_LOOPBACK_MAX_PBUFS
1244 clen++;
1245 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
1246 }
1247 #if LWIP_LOOPBACK_MAX_PBUFS
1248 /* adjust the number of pbufs on queue */
1249 LWIP_ASSERT("netif->loop_cnt_current underflow",
1250 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
1251 netif->loop_cnt_current = (u16_t)(netif->loop_cnt_current - clen);
1252 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
1253
1254 /* 'in_end' now points to the last pbuf from 'in' */
1255 if (in_end == netif->loop_last) {
1256 /* this was the last pbuf in the list */
1257 netif->loop_first = netif->loop_last = NULL;
1258 } else {
1259 /* pop the pbuf off the list */
1260 netif->loop_first = in_end->next;
1261 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
1262 }
1263 /* De-queue the pbuf from its successors on the 'loop_' list. */
1264 in_end->next = NULL;
1265 SYS_ARCH_UNPROTECT(lev);
1266
1267 in->if_idx = netif_get_index(netif);
1268
1269 LINK_STATS_INC(link.recv);
1270 MIB2_STATS_NETIF_ADD(stats_if, ifinoctets, in->tot_len);
1271 MIB2_STATS_NETIF_INC(stats_if, ifinucastpkts);
1272 /* loopback packets are always IP packets! */
1273 if (ip_input(in, netif) != ERR_OK) {
1274 pbuf_free(in);
1275 }
1276 SYS_ARCH_PROTECT(lev);
1277 }
1278 SYS_ARCH_UNPROTECT(lev);
1279 }
1280
1281 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
1282 /**
1283 * Calls netif_poll() for every netif on the netif_list.
1284 */
1285 void
netif_poll_all(void)1286 netif_poll_all(void)
1287 {
1288 struct netif *netif;
1289 /* loop through netifs */
1290 NETIF_FOREACH(netif) {
1291 netif_poll(netif);
1292 }
1293 }
1294 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
1295 #endif /* ENABLE_LOOPBACK */
1296
1297 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
1298 /**
1299 * @ingroup netif_cd
1300 * Allocate an index to store data in client_data member of struct netif.
1301 * Returned value is an index in mentioned array.
1302 * @see LWIP_NUM_NETIF_CLIENT_DATA
1303 */
1304 u8_t
netif_alloc_client_data_id(void)1305 netif_alloc_client_data_id(void)
1306 {
1307 u8_t result = netif_client_id;
1308 netif_client_id++;
1309
1310 LWIP_ASSERT_CORE_LOCKED();
1311
1312 #if LWIP_NUM_NETIF_CLIENT_DATA > 256
1313 #error LWIP_NUM_NETIF_CLIENT_DATA must be <= 256
1314 #endif
1315 LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", result < LWIP_NUM_NETIF_CLIENT_DATA);
1316 return (u8_t)(result + LWIP_NETIF_CLIENT_DATA_INDEX_MAX);
1317 }
1318 #endif
1319
1320 #if LWIP_IPV6
1321 /**
1322 * @ingroup netif_ip6
1323 * Change an IPv6 address of a network interface
1324 *
1325 * @param netif the network interface to change
1326 * @param addr_idx index of the IPv6 address
1327 * @param addr6 the new IPv6 address
1328 *
1329 * @note call netif_ip6_addr_set_state() to set the address valid/temptative
1330 */
1331 void
netif_ip6_addr_set(struct netif * netif,s8_t addr_idx,const ip6_addr_t * addr6)1332 netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6)
1333 {
1334 LWIP_ASSERT_CORE_LOCKED();
1335
1336 LWIP_ASSERT("netif_ip6_addr_set: invalid netif", netif != NULL);
1337 LWIP_ASSERT("netif_ip6_addr_set: invalid addr6", addr6 != NULL);
1338
1339 netif_ip6_addr_set_parts(netif, addr_idx, addr6->addr[0], addr6->addr[1],
1340 addr6->addr[2], addr6->addr[3]);
1341 }
1342
1343 /*
1344 * Change an IPv6 address of a network interface (internal version taking 4 * u32_t)
1345 *
1346 * @param netif the network interface to change
1347 * @param addr_idx index of the IPv6 address
1348 * @param i0 word0 of the new IPv6 address
1349 * @param i1 word1 of the new IPv6 address
1350 * @param i2 word2 of the new IPv6 address
1351 * @param i3 word3 of the new IPv6 address
1352 */
1353 void
netif_ip6_addr_set_parts(struct netif * netif,s8_t addr_idx,u32_t i0,u32_t i1,u32_t i2,u32_t i3)1354 netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3)
1355 {
1356 ip_addr_t old_addr;
1357 ip_addr_t new_ipaddr;
1358 LWIP_ASSERT_CORE_LOCKED();
1359 LWIP_ASSERT("netif != NULL", netif != NULL);
1360 LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1361
1362 ip6_addr_copy(*ip_2_ip6(&old_addr), *netif_ip6_addr(netif, addr_idx));
1363 IP_SET_TYPE_VAL(old_addr, IPADDR_TYPE_V6);
1364
1365 /* address is actually being changed? */
1366 if ((ip_2_ip6(&old_addr)->addr[0] != i0) || (ip_2_ip6(&old_addr)->addr[1] != i1) ||
1367 (ip_2_ip6(&old_addr)->addr[2] != i2) || (ip_2_ip6(&old_addr)->addr[3] != i3)) {
1368 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set: netif address being changed\n"));
1369
1370 IP_ADDR6(&new_ipaddr, i0, i1, i2, i3);
1371 ip6_addr_assign_zone(ip_2_ip6(&new_ipaddr), IP6_UNICAST, netif);
1372
1373 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addr_idx))) {
1374 netif_do_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1375 }
1376 /* @todo: remove/readd mib2 ip6 entries? */
1377
1378 ip_addr_copy(netif->ip6_addr[addr_idx], new_ipaddr);
1379
1380 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addr_idx))) {
1381 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1382 NETIF_STATUS_CALLBACK(netif);
1383 }
1384
1385 #if LWIP_NETIF_EXT_STATUS_CALLBACK
1386 {
1387 netif_ext_callback_args_t args;
1388 args.ipv6_set.addr_index = addr_idx;
1389 args.ipv6_set.old_address = &old_addr;
1390 netif_invoke_ext_callback(netif, LWIP_NSC_IPV6_SET, &args);
1391 }
1392 #endif
1393 }
1394
1395 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IPv6 address %d of interface %c%c set to %s/0x%"X8_F"\n",
1396 addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1397 netif_ip6_addr_state(netif, addr_idx)));
1398 }
1399
1400 /**
1401 * @ingroup netif_ip6
1402 * Change the state of an IPv6 address of a network interface
1403 * (INVALID, TEMPTATIVE, PREFERRED, DEPRECATED, where TEMPTATIVE
1404 * includes the number of checks done, see ip6_addr.h)
1405 *
1406 * @param netif the network interface to change
1407 * @param addr_idx index of the IPv6 address
1408 * @param state the new IPv6 address state
1409 */
1410 void
netif_ip6_addr_set_state(struct netif * netif,s8_t addr_idx,u8_t state)1411 netif_ip6_addr_set_state(struct netif *netif, s8_t addr_idx, u8_t state)
1412 {
1413 u8_t old_state;
1414 LWIP_ASSERT_CORE_LOCKED();
1415 LWIP_ASSERT("netif != NULL", netif != NULL);
1416 LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1417
1418 old_state = netif_ip6_addr_state(netif, addr_idx);
1419 /* state is actually being changed? */
1420 if (old_state != state) {
1421 u8_t old_valid = old_state & IP6_ADDR_VALID;
1422 u8_t new_valid = state & IP6_ADDR_VALID;
1423 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set_state: netif address state being changed\n"));
1424
1425 #if LWIP_IPV6_MLD
1426 /* Reevaluate solicited-node multicast group membership. */
1427 if (netif->flags & NETIF_FLAG_MLD6) {
1428 nd6_adjust_mld_membership(netif, addr_idx, state);
1429 }
1430 #endif /* LWIP_IPV6_MLD */
1431
1432 if (old_valid && !new_valid) {
1433 /* address about to be removed by setting invalid */
1434 netif_do_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1435 /* @todo: remove mib2 ip6 entries? */
1436 }
1437 netif->ip6_addr_state[addr_idx] = state;
1438
1439 if (!old_valid && new_valid) {
1440 /* address added by setting valid */
1441 /* This is a good moment to check that the address is properly zoned. */
1442 IP6_ADDR_ZONECHECK_NETIF(netif_ip6_addr(netif, addr_idx), netif);
1443 /* @todo: add mib2 ip6 entries? */
1444 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1445 }
1446 if ((old_state & ~IP6_ADDR_TENTATIVE_COUNT_MASK) !=
1447 (state & ~IP6_ADDR_TENTATIVE_COUNT_MASK)) {
1448 /* address state has changed -> call the callback function */
1449 NETIF_STATUS_CALLBACK(netif);
1450 }
1451
1452 #if LWIP_NETIF_EXT_STATUS_CALLBACK
1453 {
1454 netif_ext_callback_args_t args;
1455 args.ipv6_addr_state_changed.addr_index = addr_idx;
1456 args.ipv6_addr_state_changed.old_state = old_state;
1457 args.ipv6_addr_state_changed.address = netif_ip_addr6(netif, addr_idx);
1458 netif_invoke_ext_callback(netif, LWIP_NSC_IPV6_ADDR_STATE_CHANGED, &args);
1459 }
1460 #endif
1461 }
1462 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IPv6 address %d of interface %c%c set to %s/0x%"X8_F"\n",
1463 addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1464 netif_ip6_addr_state(netif, addr_idx)));
1465 }
1466
1467 /**
1468 * Checks if a specific local address is present on the netif and returns its
1469 * index. Depending on its state, it may or may not be assigned to the
1470 * interface (as per RFC terminology).
1471 *
1472 * The given address may or may not be zoned (i.e., have a zone index other
1473 * than IP6_NO_ZONE). If the address is zoned, it must have the correct zone
1474 * for the given netif, or no match will be found.
1475 *
1476 * @param netif the netif to check
1477 * @param ip6addr the IPv6 address to find
1478 * @return >= 0: address found, this is its index
1479 * -1: address not found on this netif
1480 */
1481 s8_t
netif_get_ip6_addr_match(struct netif * netif,const ip6_addr_t * ip6addr)1482 netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
1483 {
1484 s8_t i;
1485
1486 LWIP_ASSERT_CORE_LOCKED();
1487
1488 LWIP_ASSERT("netif_get_ip6_addr_match: invalid netif", netif != NULL);
1489 LWIP_ASSERT("netif_get_ip6_addr_match: invalid ip6addr", ip6addr != NULL);
1490
1491 #if LWIP_IPV6_SCOPES
1492 if (ip6_addr_has_zone(ip6addr) && !ip6_addr_test_zone(ip6addr, netif)) {
1493 return -1; /* wrong zone, no match */
1494 }
1495 #endif /* LWIP_IPV6_SCOPES */
1496
1497 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1498 if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
1499 ip6_addr_cmp_zoneless(netif_ip6_addr(netif, i), ip6addr)) {
1500 return i;
1501 }
1502 }
1503 return -1;
1504 }
1505
1506 /**
1507 * @ingroup netif_ip6
1508 * Create a link-local IPv6 address on a netif (stored in slot 0)
1509 *
1510 * @param netif the netif to create the address on
1511 * @param from_mac_48bit if != 0, assume hwadr is a 48-bit MAC address (std conversion)
1512 * if == 0, use hwaddr directly as interface ID
1513 */
1514 void
netif_create_ip6_linklocal_address(struct netif * netif,u8_t from_mac_48bit)1515 netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
1516 {
1517 u8_t i, addr_index;
1518
1519 LWIP_ASSERT_CORE_LOCKED();
1520
1521 LWIP_ASSERT("netif_create_ip6_linklocal_address: invalid netif", netif != NULL);
1522
1523 /* Link-local prefix. */
1524 ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
1525 ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
1526
1527 /* Generate interface ID. */
1528 if (from_mac_48bit) {
1529 /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
1530 ip_2_ip6(&netif->ip6_addr[0])->addr[2] = lwip_htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
1531 ((u32_t)(netif->hwaddr[1]) << 16) |
1532 ((u32_t)(netif->hwaddr[2]) << 8) |
1533 (0xff));
1534 ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((u32_t)(0xfeul << 24) |
1535 ((u32_t)(netif->hwaddr[3]) << 16) |
1536 ((u32_t)(netif->hwaddr[4]) << 8) |
1537 (netif->hwaddr[5]));
1538 } else {
1539 /* Use hwaddr directly as interface ID. */
1540 ip_2_ip6(&netif->ip6_addr[0])->addr[2] = 0;
1541 ip_2_ip6(&netif->ip6_addr[0])->addr[3] = 0;
1542
1543 addr_index = 3;
1544 for (i = 0; (i < 8) && (i < netif->hwaddr_len); i++) {
1545 if (i == 4) {
1546 addr_index--;
1547 }
1548 ip_2_ip6(&netif->ip6_addr[0])->addr[addr_index] |= lwip_htonl(((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03)));
1549 }
1550 }
1551
1552 /* Set a link-local zone. Even though the zone is implied by the owning
1553 * netif, setting the zone anyway has two important conceptual advantages:
1554 * 1) it avoids the need for a ton of exceptions in internal code, allowing
1555 * e.g. ip6_addr_cmp() to be used on local addresses;
1556 * 2) the properly zoned address is visible externally, e.g. when any outside
1557 * code enumerates available addresses or uses one to bind a socket.
1558 * Any external code unaware of address scoping is likely to just ignore the
1559 * zone field, so this should not create any compatibility problems. */
1560 ip6_addr_assign_zone(ip_2_ip6(&netif->ip6_addr[0]), IP6_UNICAST, netif);
1561
1562 /* Set address state. */
1563 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
1564 /* Will perform duplicate address detection (DAD). */
1565 netif_ip6_addr_set_state(netif, 0, IP6_ADDR_TENTATIVE);
1566 #else
1567 /* Consider address valid. */
1568 netif_ip6_addr_set_state(netif, 0, IP6_ADDR_PREFERRED);
1569 #endif /* LWIP_IPV6_AUTOCONFIG */
1570 }
1571
1572 /**
1573 * @ingroup netif_ip6
1574 * This function allows for the easy addition of a new IPv6 address to an interface.
1575 * It takes care of finding an empty slot and then sets the address tentative
1576 * (to make sure that all the subsequent processing happens).
1577 *
1578 * @param netif netif to add the address on
1579 * @param ip6addr address to add
1580 * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
1581 */
1582 err_t
netif_add_ip6_address(struct netif * netif,const ip6_addr_t * ip6addr,s8_t * chosen_idx)1583 netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
1584 {
1585 s8_t i;
1586
1587 LWIP_ASSERT_CORE_LOCKED();
1588
1589 LWIP_ASSERT("netif_add_ip6_address: invalid netif", netif != NULL);
1590 LWIP_ASSERT("netif_add_ip6_address: invalid ip6addr", ip6addr != NULL);
1591
1592 i = netif_get_ip6_addr_match(netif, ip6addr);
1593 if (i >= 0) {
1594 /* Address already added */
1595 if (chosen_idx != NULL) {
1596 *chosen_idx = i;
1597 }
1598 return ERR_OK;
1599 }
1600
1601 /* Find a free slot. The first one is reserved for link-local addresses. */
1602 for (i = ip6_addr_islinklocal(ip6addr) ? 0 : 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1603 if (ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) {
1604 ip_addr_copy_from_ip6(netif->ip6_addr[i], *ip6addr);
1605 ip6_addr_assign_zone(ip_2_ip6(&netif->ip6_addr[i]), IP6_UNICAST, netif);
1606 netif_ip6_addr_set_state(netif, i, IP6_ADDR_TENTATIVE);
1607 if (chosen_idx != NULL) {
1608 *chosen_idx = i;
1609 }
1610 return ERR_OK;
1611 }
1612 }
1613
1614 if (chosen_idx != NULL) {
1615 *chosen_idx = -1;
1616 }
1617 return ERR_VAL;
1618 }
1619
1620 /** Dummy IPv6 output function for netifs not supporting IPv6
1621 */
1622 static err_t
netif_null_output_ip6(struct netif * netif,struct pbuf * p,const ip6_addr_t * ipaddr)1623 netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
1624 {
1625 LWIP_UNUSED_ARG(netif);
1626 LWIP_UNUSED_ARG(p);
1627 LWIP_UNUSED_ARG(ipaddr);
1628
1629 return ERR_IF;
1630 }
1631 #endif /* LWIP_IPV6 */
1632
1633 #if LWIP_IPV4
1634 /** Dummy IPv4 output function for netifs not supporting IPv4
1635 */
1636 static err_t
netif_null_output_ip4(struct netif * netif,struct pbuf * p,const ip4_addr_t * ipaddr)1637 netif_null_output_ip4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
1638 {
1639 LWIP_UNUSED_ARG(netif);
1640 LWIP_UNUSED_ARG(p);
1641 LWIP_UNUSED_ARG(ipaddr);
1642
1643 return ERR_IF;
1644 }
1645 #endif /* LWIP_IPV4 */
1646
1647 /**
1648 * @ingroup netif
1649 * Return the interface index for the netif with name
1650 * or NETIF_NO_INDEX if not found/on error
1651 *
1652 * @param name the name of the netif
1653 */
1654 u8_t
netif_name_to_index(const char * name)1655 netif_name_to_index(const char *name)
1656 {
1657 struct netif *netif = netif_find(name);
1658 if (netif != NULL) {
1659 return netif_get_index(netif);
1660 }
1661 /* No name found, return invalid index */
1662 return NETIF_NO_INDEX;
1663 }
1664
1665 /**
1666 * @ingroup netif
1667 * Return the interface name for the netif matching index
1668 * or NULL if not found/on error
1669 *
1670 * @param idx the interface index of the netif
1671 * @param name char buffer of at least NETIF_NAMESIZE bytes
1672 */
1673 char *
netif_index_to_name(u8_t idx,char * name)1674 netif_index_to_name(u8_t idx, char *name)
1675 {
1676 struct netif *netif = netif_get_by_index(idx);
1677
1678 if (netif != NULL) {
1679 name[0] = netif->name[0];
1680 name[1] = netif->name[1];
1681 lwip_itoa(&name[2], NETIF_NAMESIZE - 2, netif_index_to_num(idx));
1682 return name;
1683 }
1684 return NULL;
1685 }
1686
1687 /**
1688 * @ingroup netif
1689 * Return the interface for the netif index
1690 *
1691 * @param idx index of netif to find
1692 */
1693 struct netif *
netif_get_by_index(u8_t idx)1694 netif_get_by_index(u8_t idx)
1695 {
1696 struct netif *netif;
1697
1698 LWIP_ASSERT_CORE_LOCKED();
1699
1700 if (idx != NETIF_NO_INDEX) {
1701 NETIF_FOREACH(netif) {
1702 if (idx == netif_get_index(netif)) {
1703 return netif; /* found! */
1704 }
1705 }
1706 }
1707
1708 return NULL;
1709 }
1710
1711 #ifndef netif_find
1712 /**
1713 * @ingroup netif
1714 * Find a network interface by searching for its name
1715 *
1716 * @param name the name of the netif (like netif->name) plus concatenated number
1717 * in ascii representation (e.g. 'en0')
1718 */
1719 struct netif *
netif_find(const char * name)1720 netif_find(const char *name)
1721 {
1722 struct netif *netif;
1723 u8_t num;
1724
1725 LWIP_ASSERT_CORE_LOCKED();
1726
1727 if (name == NULL) {
1728 return NULL;
1729 }
1730
1731 num = (u8_t)atoi(&name[2]);
1732 if (!num && (name[2] != '0')) {
1733 /* this means atoi has failed */
1734 return NULL;
1735 }
1736
1737 NETIF_FOREACH(netif) {
1738 if (num == netif->num &&
1739 name[0] == netif->name[0] &&
1740 name[1] == netif->name[1]) {
1741 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
1742 return netif;
1743 }
1744 }
1745 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
1746 return NULL;
1747 }
1748 #endif
1749
1750 #if LWIP_NETIF_EXT_STATUS_CALLBACK
1751 /**
1752 * @ingroup netif
1753 * Add extended netif events listener
1754 * @param callback pointer to listener structure
1755 * @param fn callback function
1756 */
1757 void
netif_add_ext_callback(netif_ext_callback_t * callback,netif_ext_callback_fn fn)1758 netif_add_ext_callback(netif_ext_callback_t *callback, netif_ext_callback_fn fn)
1759 {
1760 LWIP_ASSERT_CORE_LOCKED();
1761 LWIP_ASSERT("callback must be != NULL", callback != NULL);
1762 LWIP_ASSERT("fn must be != NULL", fn != NULL);
1763
1764 callback->callback_fn = fn;
1765 callback->next = ext_callback;
1766 ext_callback = callback;
1767 }
1768
1769 /**
1770 * @ingroup netif
1771 * Remove extended netif events listener
1772 * @param callback pointer to listener structure
1773 */
1774 void
netif_remove_ext_callback(netif_ext_callback_t * callback)1775 netif_remove_ext_callback(netif_ext_callback_t* callback)
1776 {
1777 netif_ext_callback_t *last, *iter;
1778
1779 LWIP_ASSERT_CORE_LOCKED();
1780 LWIP_ASSERT("callback must be != NULL", callback != NULL);
1781
1782 if (ext_callback == NULL) {
1783 return;
1784 }
1785
1786 if (callback == ext_callback) {
1787 ext_callback = ext_callback->next;
1788 } else {
1789 last = ext_callback;
1790 for (iter = ext_callback->next; iter != NULL; last = iter, iter = iter->next) {
1791 if (iter == callback) {
1792 LWIP_ASSERT("last != NULL", last != NULL);
1793 last->next = callback->next;
1794 callback->next = NULL;
1795 return;
1796 }
1797 }
1798 }
1799 }
1800
1801 /**
1802 * Invoke extended netif status event
1803 * @param netif netif that is affected by change
1804 * @param reason change reason
1805 * @param args depends on reason, see reason description
1806 */
1807 void
netif_invoke_ext_callback(struct netif * netif,netif_nsc_reason_t reason,const netif_ext_callback_args_t * args)1808 netif_invoke_ext_callback(struct netif *netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t *args)
1809 {
1810 netif_ext_callback_t *callback = ext_callback;
1811
1812 LWIP_ASSERT("netif must be != NULL", netif != NULL);
1813
1814 while (callback != NULL) {
1815 callback->callback_fn(netif, reason, args);
1816 callback = callback->next;
1817 }
1818 }
1819 #endif /* LWIP_NETIF_EXT_STATUS_CALLBACK */
1820