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>
54
55 #include "lwip/def.h"
56 #include "lwip/ip_addr.h"
57 #include "lwip/ip6_addr.h"
58 #include "lwip/netif.h"
59 #include "lwip/priv/tcp_priv.h"
60 #include "lwip/udp.h"
61 #include "lwip/raw.h"
62 #include "lwip/snmp.h"
63 #include "lwip/igmp.h"
64 #include "lwip/etharp.h"
65 #include "lwip/stats.h"
66 #include "lwip/sys.h"
67 #include "lwip/ip.h"
68 #if ENABLE_LOOPBACK
69 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
70 #include "lwip/tcpip.h"
71 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
72 #endif /* ENABLE_LOOPBACK */
73
74 #include "netif/ethernet.h"
75
76 #if LWIP_AUTOIP
77 #include "lwip/autoip.h"
78 #endif /* LWIP_AUTOIP */
79 #if LWIP_DHCP
80 #include "lwip/dhcp.h"
81 #endif /* LWIP_DHCP */
82 #if LWIP_IPV6_DHCP6
83 #include "lwip/dhcp6.h"
84 #endif /* LWIP_IPV6_DHCP6 */
85 #if LWIP_IPV6_MLD
86 #include "lwip/mld6.h"
87 #endif /* LWIP_IPV6_MLD */
88 #if LWIP_IPV6
89 #include "lwip/nd6.h"
90 #endif
91 #ifdef CELLULAR_SUPPORT
92 #include "lwip/ip6_zone.h"
93 #endif
94
95 #if LWIP_NETIF_STATUS_CALLBACK
96 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
97 #else
98 #define NETIF_STATUS_CALLBACK(n)
99 #endif /* LWIP_NETIF_STATUS_CALLBACK */
100
101 #if LWIP_NETIF_LINK_CALLBACK
102 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
103 #else
104 #define NETIF_LINK_CALLBACK(n)
105 #endif /* LWIP_NETIF_LINK_CALLBACK */
106
107 struct netif *netif_list;
108 struct netif *netif_default;
109
110 static u8_t netif_num;
111
112 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
113 static u8_t netif_client_id;
114 #endif
115
116 #define NETIF_REPORT_TYPE_IPV4 0x01
117 #define NETIF_REPORT_TYPE_IPV6 0x02
118 static void netif_issue_reports(struct netif* netif, u8_t report_type);
119
120 #if LWIP_IPV6
121 static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
122 #endif /* LWIP_IPV6 */
123
124 #if LWIP_HAVE_LOOPIF
125 #if LWIP_IPV4
126 static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr);
127 #endif
128 #if LWIP_IPV6
129 static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr);
130 #endif
131
132
133 static struct netif loop_netif;
134
135 /**
136 * Initialize a lwip network interface structure for a loopback interface
137 *
138 * @param netif the lwip network interface structure for this loopif
139 * @return ERR_OK if the loopif is initialized
140 * ERR_MEM if private data couldn't be allocated
141 */
142 static err_t
netif_loopif_init(struct netif * netif)143 netif_loopif_init(struct netif *netif)
144 {
145 /* initialize the snmp variables and counters inside the struct netif
146 * ifSpeed: no assumption can be made!
147 */
148 MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
149
150 netif->name[0] = 'l';
151 netif->name[1] = 'o';
152 #if LWIP_IPV4
153 netif->output = netif_loop_output_ipv4;
154 #endif
155 #if LWIP_IPV6
156 netif->output_ip6 = netif_loop_output_ipv6;
157 #endif
158 #if LWIP_LOOPIF_MULTICAST
159 netif->flags |= NETIF_FLAG_IGMP;
160 #endif
161 return ERR_OK;
162 }
163 #endif /* LWIP_HAVE_LOOPIF */
164
165 void
netif_init(void)166 netif_init(void)
167 {
168 #if LWIP_HAVE_LOOPIF
169 #if LWIP_IPV4
170 #define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw,
171 ip4_addr_t loop_ipaddr, loop_netmask, loop_gw;
172 IP4_ADDR(&loop_gw, 127,0,0,1);
173 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
174 IP4_ADDR(&loop_netmask, 255,0,0,0);
175 #else /* LWIP_IPV4 */
176 #define LOOPIF_ADDRINIT
177 #endif /* LWIP_IPV4 */
178
179 #if NO_SYS
180 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
181 #else /* NO_SYS */
182 netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
183 #endif /* NO_SYS */
184
185 #if LWIP_IPV6
186 IP_ADDR6(loop_netif.ip6_addr, 0, 0, 0, PP_HTONL(0x00000001UL));
187 loop_netif.ip6_addr_state[0] = IP6_ADDR_VALID;
188 #endif /* LWIP_IPV6 */
189
190 netif_set_link_up(&loop_netif);
191 netif_set_up(&loop_netif);
192
193 #endif /* LWIP_HAVE_LOOPIF */
194 }
195
196 /**
197 * @ingroup lwip_nosys
198 * Forwards a received packet for input processing with
199 * ethernet_input() or ip_input() depending on netif flags.
200 * Don't call directly, pass to netif_add() and call
201 * netif->input().
202 * Only works if the netif driver correctly sets
203 * NETIF_FLAG_ETHARP and/or NETIF_FLAG_ETHERNET flag!
204 */
205 err_t
netif_input(struct pbuf * p,struct netif * inp)206 netif_input(struct pbuf *p, struct netif *inp)
207 {
208 #if LWIP_ETHERNET
209 if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
210 return ethernet_input(p, inp);
211 } else
212 #endif /* LWIP_ETHERNET */
213 return ip_input(p, inp);
214 }
215
216 /**
217 * @ingroup netif
218 * Add a network interface to the list of lwIP netifs.
219 *
220 * @param netif a pre-allocated netif structure
221 * @param ipaddr IP address for the new netif
222 * @param netmask network mask for the new netif
223 * @param gw default gateway IP address for the new netif
224 * @param state opaque data passed to the new netif
225 * @param init callback function that initializes the interface
226 * @param input callback function that is called to pass
227 * ingress packets up in the protocol layer stack.\n
228 * It is recommended to use a function that passes the input directly
229 * to the stack (netif_input(), NO_SYS=1 mode) or via sending a
230 * message to TCPIP thread (tcpip_input(), NO_SYS=0 mode).\n
231 * These functions use netif flags NETIF_FLAG_ETHARP and NETIF_FLAG_ETHERNET
232 * to decide whether to forward to ethernet_input() or ip_input().
233 * In other words, the functions only work when the netif
234 * driver is implemented correctly!\n
235 * Most members of struct netif should be be initialized by the
236 * netif init function = netif driver (init parameter of this function).\n
237 * IPv6: Don't forget to call netif_create_ip6_linklocal_address() after
238 * setting the MAC address in struct netif.hwaddr
239 * (IPv6 requires a link-local address).
240 *
241 * @return netif, or NULL if failed.
242 */
243 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)244 netif_add(struct netif *netif,
245 #if LWIP_IPV4
246 const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
247 #endif /* LWIP_IPV4 */
248 void *state, netif_init_fn init, netif_input_fn input)
249 {
250 #if LWIP_IPV6
251 s8_t i;
252 #endif
253
254 LWIP_ASSERT("No init function given", init != NULL);
255
256 /* reset new interface configuration state */
257 #if LWIP_IPV4
258 ip_addr_set_zero_ip4(&netif->ip_addr);
259 ip_addr_set_zero_ip4(&netif->netmask);
260 ip_addr_set_zero_ip4(&netif->gw);
261 #endif /* LWIP_IPV4 */
262 #if LWIP_IPV6
263 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
264 ip_addr_set_zero_ip6(&netif->ip6_addr[i]);
265 netif->ip6_addr_state[0] = IP6_ADDR_INVALID;
266 }
267 netif->output_ip6 = netif_null_output_ip6;
268 #endif /* LWIP_IPV6 */
269 NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
270 netif->flags = 0;
271 #ifdef netif_get_client_data
272 memset(netif->client_data, 0, sizeof(netif->client_data));
273 #endif /* LWIP_NUM_NETIF_CLIENT_DATA */
274 #if LWIP_IPV6_AUTOCONFIG
275 /* IPv6 address autoconfiguration not enabled by default */
276 #ifdef CELLULAR_SUPPORT
277 netif->ip6_autoconfig_enabled = 1;
278 #else
279 netif->ip6_autoconfig_enabled = 0;
280 #endif /* CELLULAR_SUPPORT */
281 #endif /* LWIP_IPV6_AUTOCONFIG */
282 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
283 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
284 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
285 #if LWIP_NETIF_STATUS_CALLBACK
286 netif->status_callback = NULL;
287 #endif /* LWIP_NETIF_STATUS_CALLBACK */
288 #if LWIP_NETIF_LINK_CALLBACK
289 netif->link_callback = NULL;
290 #endif /* LWIP_NETIF_LINK_CALLBACK */
291 #if LWIP_IGMP
292 netif->igmp_mac_filter = NULL;
293 #endif /* LWIP_IGMP */
294 #if LWIP_IPV6 && LWIP_IPV6_MLD
295 netif->mld_mac_filter = NULL;
296 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
297 #if ENABLE_LOOPBACK
298 netif->loop_first = NULL;
299 netif->loop_last = NULL;
300 #endif /* ENABLE_LOOPBACK */
301
302 /* remember netif specific state information data */
303 netif->state = state;
304 netif->num = netif_num++;
305 netif->input = input;
306
307 NETIF_SET_HWADDRHINT(netif, NULL);
308 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
309 netif->loop_cnt_current = 0;
310 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
311
312 #if LWIP_IPV4
313 netif_set_addr(netif, ipaddr, netmask, gw);
314 #endif /* LWIP_IPV4 */
315 #ifdef CELLULAR_SUPPORT
316 for(int i=0; i<DNS_MAX_SERVERS; i++)
317 {
318 ip_addr_set_zero(&netif->dns_srv[i]);
319 }
320 #endif
321
322 /* call user specified initialization function for netif */
323 if (init(netif) != ERR_OK) {
324 return NULL;
325 }
326
327 /* add this netif to the list */
328 netif->next = netif_list;
329 netif_list = netif;
330 mib2_netif_added(netif);
331
332 #if LWIP_IGMP
333 /* start IGMP processing */
334 if (netif->flags & NETIF_FLAG_IGMP) {
335 igmp_start(netif);
336 }
337 #endif /* LWIP_IGMP */
338
339 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
340 netif->name[0], netif->name[1]));
341 #if LWIP_IPV4
342 LWIP_DEBUGF(NETIF_DEBUG, (" addr "));
343 ip4_addr_debug_print(NETIF_DEBUG, ipaddr);
344 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
345 ip4_addr_debug_print(NETIF_DEBUG, netmask);
346 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
347 ip4_addr_debug_print(NETIF_DEBUG, gw);
348 #endif /* LWIP_IPV4 */
349 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
350 return netif;
351 }
352
353 #if LWIP_IPV4
354 /**
355 * @ingroup netif_ip4
356 * Change IP address configuration for a network interface (including netmask
357 * and default gateway).
358 *
359 * @param netif the network interface to change
360 * @param ipaddr the new IP address
361 * @param netmask the new netmask
362 * @param gw the new default gateway
363 */
364 void
netif_set_addr(struct netif * netif,const ip4_addr_t * ipaddr,const ip4_addr_t * netmask,const ip4_addr_t * gw)365 netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
366 const ip4_addr_t *gw)
367 {
368 if (ip4_addr_isany(ipaddr)) {
369 /* when removing an address, we have to remove it *before* changing netmask/gw
370 to ensure that tcp RST segment can be sent correctly */
371 netif_set_ipaddr(netif, ipaddr);
372 netif_set_netmask(netif, netmask);
373 netif_set_gw(netif, gw);
374 } else {
375 netif_set_netmask(netif, netmask);
376 netif_set_gw(netif, gw);
377 /* set ipaddr last to ensure netmask/gw have been set when status callback is called */
378 netif_set_ipaddr(netif, ipaddr);
379 }
380 }
381 #endif /* LWIP_IPV4*/
382
383 /**
384 * @ingroup netif
385 * Remove a network interface from the list of lwIP netifs.
386 *
387 * @param netif the network interface to remove
388 */
389 void
netif_remove(struct netif * netif)390 netif_remove(struct netif *netif)
391 {
392 #if LWIP_IPV6
393 int i;
394 #endif
395
396 if (netif == NULL) {
397 return;
398 }
399
400 #if LWIP_IPV4
401 if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
402 #if LWIP_TCP
403 tcp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
404 #endif /* LWIP_TCP */
405 #if LWIP_UDP
406 udp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
407 #endif /* LWIP_UDP */
408 #if LWIP_RAW
409 raw_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
410 #endif /* LWIP_RAW */
411 }
412
413 #if LWIP_IGMP
414 /* stop IGMP processing */
415 if (netif->flags & NETIF_FLAG_IGMP) {
416 igmp_stop(netif);
417 }
418 #endif /* LWIP_IGMP */
419 #endif /* LWIP_IPV4*/
420
421 #if LWIP_IPV6
422 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
423 if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
424 #if LWIP_TCP
425 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
426 #endif /* LWIP_TCP */
427 #if LWIP_UDP
428 udp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
429 #endif /* LWIP_UDP */
430 #if LWIP_RAW
431 raw_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
432 #endif /* LWIP_RAW */
433 }
434 }
435 #if LWIP_IPV6_MLD
436 /* stop MLD processing */
437 mld6_stop(netif);
438 #endif /* LWIP_IPV6_MLD */
439 #endif /* LWIP_IPV6 */
440 if (netif_is_up(netif)) {
441 /* set netif down before removing (call callback function) */
442 netif_set_down(netif);
443 }
444
445 mib2_remove_ip4(netif);
446
447 /* this netif is default? */
448 if (netif_default == netif) {
449 /* reset default netif */
450 netif_set_default(NULL);
451 }
452 /* is it the first netif? */
453 if (netif_list == netif) {
454 netif_list = netif->next;
455 } else {
456 /* look for netif further down the list */
457 struct netif * tmp_netif;
458 for (tmp_netif = netif_list; tmp_netif != NULL; tmp_netif = tmp_netif->next) {
459 if (tmp_netif->next == netif) {
460 tmp_netif->next = netif->next;
461 break;
462 }
463 }
464 if (tmp_netif == NULL) {
465 return; /* netif is not on the list */
466 }
467 }
468 mib2_netif_removed(netif);
469 #if LWIP_NETIF_REMOVE_CALLBACK
470 if (netif->remove_callback) {
471 netif->remove_callback(netif);
472 }
473 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
474 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
475 }
476
477 /**
478 * @ingroup netif
479 * Find a network interface by searching for its name
480 *
481 * @param name the name of the netif (like netif->name) plus concatenated number
482 * in ascii representation (e.g. 'en0')
483 */
484 struct netif *
netif_find(const char * name)485 netif_find(const char *name)
486 {
487 struct netif *netif;
488 u8_t num;
489
490 if (name == NULL) {
491 return NULL;
492 }
493
494 num = name[2] - '0';
495
496 for (netif = netif_list; netif != NULL; netif = netif->next) {
497 if (num == netif->num &&
498 name[0] == netif->name[0] &&
499 name[1] == netif->name[1]) {
500 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
501 return netif;
502 }
503 }
504 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
505 return NULL;
506 }
507 #if LWIP_PACKET
508 struct netif *
netif_find_by_index(int index)509 netif_find_by_index(int index)
510 {
511 struct netif *netif;
512
513 if (index != NETIF_NO_INDEX) {
514 NETIF_FOREACH(netif) {
515 if (index == netif_get_index(netif)) {
516 return netif; /* found! */
517 }
518 }
519 }
520
521 return NULL;
522 }
523 #endif
524
525 #if LWIP_IPV4
526 /**
527 * @ingroup netif_ip4
528 * Change the IP address of a network interface
529 *
530 * @param netif the network interface to change
531 * @param ipaddr the new IP address
532 *
533 * @note call netif_set_addr() if you also want to change netmask and
534 * default gateway
535 */
536 void
netif_set_ipaddr(struct netif * netif,const ip4_addr_t * ipaddr)537 netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
538 {
539 ip_addr_t new_addr;
540 *ip_2_ip4(&new_addr) = (ipaddr ? *ipaddr : *IP4_ADDR_ANY4);
541 IP_SET_TYPE_VAL(new_addr, IPADDR_TYPE_V4);
542
543 /* address is actually being changed? */
544 if (ip4_addr_cmp(ip_2_ip4(&new_addr), netif_ip4_addr(netif)) == 0) {
545 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
546 #if LWIP_TCP
547 tcp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
548 #endif /* LWIP_TCP */
549 #if LWIP_UDP
550 udp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
551 #endif /* LWIP_UDP */
552 #if LWIP_RAW
553 raw_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
554 #endif /* LWIP_RAW */
555
556 mib2_remove_ip4(netif);
557 mib2_remove_route_ip4(0, netif);
558 /* set new IP address to netif */
559 ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr);
560 IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4);
561 mib2_add_ip4(netif);
562 mib2_add_route_ip4(0, netif);
563
564 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
565
566 NETIF_STATUS_CALLBACK(netif);
567 }
568
569 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
570 netif->name[0], netif->name[1],
571 ip4_addr1_16(netif_ip4_addr(netif)),
572 ip4_addr2_16(netif_ip4_addr(netif)),
573 ip4_addr3_16(netif_ip4_addr(netif)),
574 ip4_addr4_16(netif_ip4_addr(netif))));
575 }
576
577 /**
578 * @ingroup netif_ip4
579 * Change the default gateway for a network interface
580 *
581 * @param netif the network interface to change
582 * @param gw the new default gateway
583 *
584 * @note call netif_set_addr() if you also want to change ip address and netmask
585 */
586 void
netif_set_gw(struct netif * netif,const ip4_addr_t * gw)587 netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
588 {
589 ip4_addr_set(ip_2_ip4(&netif->gw), gw);
590 IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4);
591 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",
592 netif->name[0], netif->name[1],
593 ip4_addr1_16(netif_ip4_gw(netif)),
594 ip4_addr2_16(netif_ip4_gw(netif)),
595 ip4_addr3_16(netif_ip4_gw(netif)),
596 ip4_addr4_16(netif_ip4_gw(netif))));
597 }
598
599 /**
600 * @ingroup netif_ip4
601 * Change the netmask of a network interface
602 *
603 * @param netif the network interface to change
604 * @param netmask the new netmask
605 *
606 * @note call netif_set_addr() if you also want to change ip address and
607 * default gateway
608 */
609 void
netif_set_netmask(struct netif * netif,const ip4_addr_t * netmask)610 netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
611 {
612 mib2_remove_route_ip4(0, netif);
613 /* set new netmask to netif */
614 ip4_addr_set(ip_2_ip4(&netif->netmask), netmask);
615 IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4);
616 mib2_add_route_ip4(0, netif);
617 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",
618 netif->name[0], netif->name[1],
619 ip4_addr1_16(netif_ip4_netmask(netif)),
620 ip4_addr2_16(netif_ip4_netmask(netif)),
621 ip4_addr3_16(netif_ip4_netmask(netif)),
622 ip4_addr4_16(netif_ip4_netmask(netif))));
623 }
624 #endif /* LWIP_IPV4 */
625
626 /**
627 * @ingroup netif
628 * Set a network interface as the default network interface
629 * (used to output all packets for which no specific route is found)
630 *
631 * @param netif the default network interface
632 */
633 void
netif_set_default(struct netif * netif)634 netif_set_default(struct netif *netif)
635 {
636 if (netif == NULL) {
637 /* remove default route */
638 mib2_remove_route_ip4(1, netif);
639 } else {
640 /* install default route */
641 mib2_add_route_ip4(1, netif);
642 }
643 netif_default = netif;
644 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
645 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
646 }
647
648 /**
649 * @ingroup netif
650 * Bring an interface up, available for processing
651 * traffic.
652 */
653 void
netif_set_up(struct netif * netif)654 netif_set_up(struct netif *netif)
655 {
656 if (!(netif->flags & NETIF_FLAG_UP)) {
657 netif->flags |= NETIF_FLAG_UP;
658
659 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
660
661 NETIF_STATUS_CALLBACK(netif);
662
663 if (netif->flags & NETIF_FLAG_LINK_UP) {
664 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
665 }
666 }
667 }
668
669 /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
670 */
671 static void
netif_issue_reports(struct netif * netif,u8_t report_type)672 netif_issue_reports(struct netif* netif, u8_t report_type)
673 {
674 #if LWIP_IPV4
675 if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
676 !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
677 #if LWIP_ARP
678 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
679 if (netif->flags & (NETIF_FLAG_ETHARP)) {
680 etharp_gratuitous(netif);
681 }
682 #endif /* LWIP_ARP */
683
684 #if LWIP_IGMP
685 /* resend IGMP memberships */
686 if (netif->flags & NETIF_FLAG_IGMP) {
687 igmp_report_groups(netif);
688 }
689 #endif /* LWIP_IGMP */
690 }
691 #endif /* LWIP_IPV4 */
692
693 #if LWIP_IPV6
694 if (report_type & NETIF_REPORT_TYPE_IPV6) {
695 #if LWIP_IPV6_MLD
696 /* send mld memberships */
697 mld6_report_groups(netif);
698 #endif /* LWIP_IPV6_MLD */
699 #if LWIP_IPV6_SEND_ROUTER_SOLICIT
700 /* Send Router Solicitation messages. */
701 netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
702 #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
703 }
704 #endif /* LWIP_IPV6 */
705 }
706
707 /**
708 * @ingroup netif
709 * Bring an interface down, disabling any traffic processing.
710 */
711 void
netif_set_down(struct netif * netif)712 netif_set_down(struct netif *netif)
713 {
714 if (netif->flags & NETIF_FLAG_UP) {
715 netif->flags &= ~NETIF_FLAG_UP;
716 MIB2_COPY_SYSUPTIME_TO(&netif->ts);
717
718 #if LWIP_IPV4 && LWIP_ARP
719 if (netif->flags & NETIF_FLAG_ETHARP) {
720 etharp_cleanup_netif(netif);
721 }
722 #endif /* LWIP_IPV4 && LWIP_ARP */
723
724 #if LWIP_IPV6
725 nd6_cleanup_netif(netif);
726 #endif /* LWIP_IPV6 */
727
728 NETIF_STATUS_CALLBACK(netif);
729 }
730 }
731
732 #if LWIP_NETIF_STATUS_CALLBACK
733 /**
734 * @ingroup netif
735 * Set callback to be called when interface is brought up/down or address is changed while up
736 */
737 void
netif_set_status_callback(struct netif * netif,netif_status_callback_fn status_callback)738 netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
739 {
740 if (netif) {
741 netif->status_callback = status_callback;
742 }
743 }
744 #endif /* LWIP_NETIF_STATUS_CALLBACK */
745
746 #if LWIP_NETIF_REMOVE_CALLBACK
747 /**
748 * @ingroup netif
749 * Set callback to be called when the interface has been removed
750 */
751 void
netif_set_remove_callback(struct netif * netif,netif_status_callback_fn remove_callback)752 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
753 {
754 if (netif) {
755 netif->remove_callback = remove_callback;
756 }
757 }
758 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
759
760 /**
761 * @ingroup netif
762 * Called by a driver when its link goes up
763 */
764 void
netif_set_link_up(struct netif * netif)765 netif_set_link_up(struct netif *netif)
766 {
767 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
768 netif->flags |= NETIF_FLAG_LINK_UP;
769
770 #if LWIP_DHCP
771 dhcp_network_changed(netif);
772 #endif /* LWIP_DHCP */
773
774 #if LWIP_AUTOIP
775 autoip_network_changed(netif);
776 #endif /* LWIP_AUTOIP */
777
778 if (netif->flags & NETIF_FLAG_UP) {
779 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
780 }
781 NETIF_LINK_CALLBACK(netif);
782 }
783 }
784
785 /**
786 * @ingroup netif
787 * Called by a driver when its link goes down
788 */
789 void
netif_set_link_down(struct netif * netif)790 netif_set_link_down(struct netif *netif )
791 {
792 if (netif->flags & NETIF_FLAG_LINK_UP) {
793 netif->flags &= ~NETIF_FLAG_LINK_UP;
794 NETIF_LINK_CALLBACK(netif);
795 }
796 }
797
798 #if LWIP_NETIF_LINK_CALLBACK
799 /**
800 * @ingroup netif
801 * Set callback to be called when link is brought up/down
802 */
803 void
netif_set_link_callback(struct netif * netif,netif_status_callback_fn link_callback)804 netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
805 {
806 if (netif) {
807 netif->link_callback = link_callback;
808 }
809 }
810 #endif /* LWIP_NETIF_LINK_CALLBACK */
811
812 #if ENABLE_LOOPBACK
813 /**
814 * @ingroup netif
815 * Send an IP packet to be received on the same netif (loopif-like).
816 * The pbuf is simply copied and handed back to netif->input.
817 * In multithreaded mode, this is done directly since netif->input must put
818 * the packet on a queue.
819 * In callback mode, the packet is put on an internal queue and is fed to
820 * netif->input by netif_poll().
821 *
822 * @param netif the lwip network interface structure
823 * @param p the (IP) packet to 'send'
824 * @return ERR_OK if the packet has been sent
825 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
826 */
827 err_t
netif_loop_output(struct netif * netif,struct pbuf * p)828 netif_loop_output(struct netif *netif, struct pbuf *p)
829 {
830 struct pbuf *r;
831 err_t err;
832 struct pbuf *last;
833 #if LWIP_LOOPBACK_MAX_PBUFS
834 u16_t clen = 0;
835 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
836 /* If we have a loopif, SNMP counters are adjusted for it,
837 * if not they are adjusted for 'netif'. */
838 #if MIB2_STATS
839 #if LWIP_HAVE_LOOPIF
840 struct netif *stats_if = &loop_netif;
841 #else /* LWIP_HAVE_LOOPIF */
842 struct netif *stats_if = netif;
843 #endif /* LWIP_HAVE_LOOPIF */
844 #endif /* MIB2_STATS */
845 SYS_ARCH_DECL_PROTECT(lev);
846
847 /* Allocate a new pbuf */
848 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
849 if (r == NULL) {
850 LINK_STATS_INC(link.memerr);
851 LINK_STATS_INC(link.drop);
852 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
853 return ERR_MEM;
854 }
855 #if LWIP_LOOPBACK_MAX_PBUFS
856 clen = pbuf_clen(r);
857 /* check for overflow or too many pbuf on queue */
858 if (((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
859 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
860 pbuf_free(r);
861 LINK_STATS_INC(link.memerr);
862 LINK_STATS_INC(link.drop);
863 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
864 return ERR_MEM;
865 }
866 netif->loop_cnt_current += clen;
867 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
868
869 /* Copy the whole pbuf queue p into the single pbuf r */
870 if ((err = pbuf_copy(r, p)) != ERR_OK) {
871 pbuf_free(r);
872 LINK_STATS_INC(link.memerr);
873 LINK_STATS_INC(link.drop);
874 MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
875 return err;
876 }
877
878 /* Put the packet on a linked list which gets emptied through calling
879 netif_poll(). */
880
881 /* let last point to the last pbuf in chain r */
882 for (last = r; last->next != NULL; last = last->next);
883
884 SYS_ARCH_PROTECT(lev);
885 if (netif->loop_first != NULL) {
886 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
887 netif->loop_last->next = r;
888 netif->loop_last = last;
889 } else {
890 netif->loop_first = r;
891 netif->loop_last = last;
892 }
893 SYS_ARCH_UNPROTECT(lev);
894
895 LINK_STATS_INC(link.xmit);
896 MIB2_STATS_NETIF_ADD(stats_if, ifoutoctets, p->tot_len);
897 MIB2_STATS_NETIF_INC(stats_if, ifoutucastpkts);
898
899 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
900 /* For multithreading environment, schedule a call to netif_poll */
901 tcpip_callback_with_block((tcpip_callback_fn)netif_poll, netif, 0);
902 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
903
904 return ERR_OK;
905 }
906
907 #if LWIP_HAVE_LOOPIF
908 #if LWIP_IPV4
909 static err_t
netif_loop_output_ipv4(struct netif * netif,struct pbuf * p,const ip4_addr_t * addr)910 netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr)
911 {
912 LWIP_UNUSED_ARG(addr);
913 return netif_loop_output(netif, p);
914 }
915 #endif /* LWIP_IPV4 */
916
917 #if LWIP_IPV6
918 static err_t
netif_loop_output_ipv6(struct netif * netif,struct pbuf * p,const ip6_addr_t * addr)919 netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr)
920 {
921 LWIP_UNUSED_ARG(addr);
922 return netif_loop_output(netif, p);
923 }
924 #endif /* LWIP_IPV6 */
925 #endif /* LWIP_HAVE_LOOPIF */
926
927
928 /**
929 * Call netif_poll() in the main loop of your application. This is to prevent
930 * reentering non-reentrant functions like tcp_input(). Packets passed to
931 * netif_loop_output() are put on a list that is passed to netif->input() by
932 * netif_poll().
933 */
934 void
netif_poll(struct netif * netif)935 netif_poll(struct netif *netif)
936 {
937 struct pbuf *in;
938 /* If we have a loopif, SNMP counters are adjusted for it,
939 * if not they are adjusted for 'netif'. */
940 #if MIB2_STATS
941 #if LWIP_HAVE_LOOPIF
942 struct netif *stats_if = &loop_netif;
943 #else /* LWIP_HAVE_LOOPIF */
944 struct netif *stats_if = netif;
945 #endif /* LWIP_HAVE_LOOPIF */
946 #endif /* MIB2_STATS */
947 SYS_ARCH_DECL_PROTECT(lev);
948
949 do {
950 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
951 SYS_ARCH_PROTECT(lev);
952 in = netif->loop_first;
953 if (in != NULL) {
954 struct pbuf *in_end = in;
955 #if LWIP_LOOPBACK_MAX_PBUFS
956 u8_t clen = 1;
957 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
958 while (in_end->len != in_end->tot_len) {
959 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
960 in_end = in_end->next;
961 #if LWIP_LOOPBACK_MAX_PBUFS
962 clen++;
963 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
964 }
965 #if LWIP_LOOPBACK_MAX_PBUFS
966 /* adjust the number of pbufs on queue */
967 LWIP_ASSERT("netif->loop_cnt_current underflow",
968 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
969 netif->loop_cnt_current -= clen;
970 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
971
972 /* 'in_end' now points to the last pbuf from 'in' */
973 if (in_end == netif->loop_last) {
974 /* this was the last pbuf in the list */
975 netif->loop_first = netif->loop_last = NULL;
976 } else {
977 /* pop the pbuf off the list */
978 netif->loop_first = in_end->next;
979 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
980 }
981 /* De-queue the pbuf from its successors on the 'loop_' list. */
982 in_end->next = NULL;
983 }
984 SYS_ARCH_UNPROTECT(lev);
985
986 if (in != NULL) {
987 LINK_STATS_INC(link.recv);
988 MIB2_STATS_NETIF_ADD(stats_if, ifinoctets, in->tot_len);
989 MIB2_STATS_NETIF_INC(stats_if, ifinucastpkts);
990 /* loopback packets are always IP packets! */
991 if (ip_input(in, netif) != ERR_OK) {
992 pbuf_free(in);
993 }
994 /* Don't reference the packet any more! */
995 in = NULL;
996 }
997 /* go on while there is a packet on the list */
998 } while (netif->loop_first != NULL);
999 }
1000
1001 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
1002 /**
1003 * Calls netif_poll() for every netif on the netif_list.
1004 */
1005 void
netif_poll_all(void)1006 netif_poll_all(void)
1007 {
1008 struct netif *netif = netif_list;
1009 /* loop through netifs */
1010 while (netif != NULL) {
1011 netif_poll(netif);
1012 /* proceed to next network interface */
1013 netif = netif->next;
1014 }
1015 }
1016 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
1017 #endif /* ENABLE_LOOPBACK */
1018
1019 #if LWIP_NUM_NETIF_CLIENT_DATA > 0
1020 /**
1021 * @ingroup netif_cd
1022 * Allocate an index to store data in client_data member of struct netif.
1023 * Returned value is an index in mentioned array.
1024 * @see LWIP_NUM_NETIF_CLIENT_DATA
1025 */
1026 u8_t
netif_alloc_client_data_id(void)1027 netif_alloc_client_data_id(void)
1028 {
1029 u8_t result = netif_client_id;
1030 netif_client_id++;
1031
1032 LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", result < LWIP_NUM_NETIF_CLIENT_DATA);
1033 return result + LWIP_NETIF_CLIENT_DATA_INDEX_MAX;
1034 }
1035 #endif
1036
1037 #if LWIP_IPV6
1038 /**
1039 * @ingroup netif_ip6
1040 * Change an IPv6 address of a network interface
1041 *
1042 * @param netif the network interface to change
1043 * @param addr_idx index of the IPv6 address
1044 * @param addr6 the new IPv6 address
1045 *
1046 * @note call netif_ip6_addr_set_state() to set the address valid/temptative
1047 */
1048 void
netif_ip6_addr_set(struct netif * netif,s8_t addr_idx,const ip6_addr_t * addr6)1049 netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6)
1050 {
1051 LWIP_ASSERT("addr6 != NULL", addr6 != NULL);
1052 netif_ip6_addr_set_parts(netif, addr_idx, addr6->addr[0], addr6->addr[1],
1053 addr6->addr[2], addr6->addr[3]);
1054 }
1055
1056 /*
1057 * Change an IPv6 address of a network interface (internal version taking 4 * u32_t)
1058 *
1059 * @param netif the network interface to change
1060 * @param addr_idx index of the IPv6 address
1061 * @param i0 word0 of the new IPv6 address
1062 * @param i1 word1 of the new IPv6 address
1063 * @param i2 word2 of the new IPv6 address
1064 * @param i3 word3 of the new IPv6 address
1065 */
1066 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)1067 netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3)
1068 {
1069 const ip6_addr_t *old_addr;
1070 LWIP_ASSERT("netif != NULL", netif != NULL);
1071 LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1072
1073 old_addr = netif_ip6_addr(netif, addr_idx);
1074 /* address is actually being changed? */
1075 if ((old_addr->addr[0] != i0) || (old_addr->addr[1] != i1) ||
1076 (old_addr->addr[2] != i2) || (old_addr->addr[3] != i3)) {
1077 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set: netif address being changed\n"));
1078
1079 if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
1080 #if LWIP_TCP || LWIP_UDP
1081 ip_addr_t new_ipaddr;
1082 IP_ADDR6(&new_ipaddr, i0, i1, i2, i3);
1083 #endif /* LWIP_TCP || LWIP_UDP */
1084 #if LWIP_TCP
1085 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1086 #endif /* LWIP_TCP */
1087 #if LWIP_UDP
1088 udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1089 #endif /* LWIP_UDP */
1090 #if LWIP_RAW
1091 raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
1092 #endif /* LWIP_RAW */
1093 }
1094 /* @todo: remove/readd mib2 ip6 entries? */
1095
1096 IP6_ADDR(ip_2_ip6(&(netif->ip6_addr[addr_idx])), i0, i1, i2, i3);
1097 IP_SET_TYPE_VAL(netif->ip6_addr[addr_idx], IPADDR_TYPE_V6);
1098
1099 if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
1100 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1101 NETIF_STATUS_CALLBACK(netif);
1102 }
1103 }
1104
1105 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",
1106 addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1107 netif_ip6_addr_state(netif, addr_idx)));
1108 }
1109
1110 /**
1111 * @ingroup netif_ip6
1112 * Change the state of an IPv6 address of a network interface
1113 * (INVALID, TEMPTATIVE, PREFERRED, DEPRECATED, where TEMPTATIVE
1114 * includes the number of checks done, see ip6_addr.h)
1115 *
1116 * @param netif the network interface to change
1117 * @param addr_idx index of the IPv6 address
1118 * @param state the new IPv6 address state
1119 */
1120 void
netif_ip6_addr_set_state(struct netif * netif,s8_t addr_idx,u8_t state)1121 netif_ip6_addr_set_state(struct netif* netif, s8_t addr_idx, u8_t state)
1122 {
1123 u8_t old_state;
1124 LWIP_ASSERT("netif != NULL", netif != NULL);
1125 LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
1126
1127 old_state = netif_ip6_addr_state(netif, addr_idx);
1128 /* state is actually being changed? */
1129 if (old_state != state) {
1130 u8_t old_valid = old_state & IP6_ADDR_VALID;
1131 u8_t new_valid = state & IP6_ADDR_VALID;
1132 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set_state: netif address state being changed\n"));
1133
1134 if (old_valid && !new_valid) {
1135 /* address about to be removed by setting invalid */
1136 #if LWIP_TCP
1137 tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1138 #endif /* LWIP_TCP */
1139 #if LWIP_UDP
1140 udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1141 #endif /* LWIP_UDP */
1142 #if LWIP_RAW
1143 raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
1144 #endif /* LWIP_RAW */
1145 /* @todo: remove mib2 ip6 entries? */
1146 }
1147 netif->ip6_addr_state[addr_idx] = state;
1148
1149 if (!old_valid && new_valid) {
1150 /* address added by setting valid */
1151 /* @todo: add mib2 ip6 entries? */
1152 netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
1153 }
1154 if ((old_state & IP6_ADDR_PREFERRED) != (state & IP6_ADDR_PREFERRED)) {
1155 /* address state has changed (valid flag changed or switched between
1156 preferred and deprecated) -> call the callback function */
1157 NETIF_STATUS_CALLBACK(netif);
1158 }
1159 }
1160
1161 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",
1162 addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
1163 netif_ip6_addr_state(netif, addr_idx)));
1164 }
1165
1166 /**
1167 * Checks if a specific address is assigned to the netif and returns its
1168 * index.
1169 *
1170 * @param netif the netif to check
1171 * @param ip6addr the IPv6 address to find
1172 * @return >= 0: address found, this is its index
1173 * -1: address not found on this netif
1174 */
1175 s8_t
netif_get_ip6_addr_match(struct netif * netif,const ip6_addr_t * ip6addr)1176 netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
1177 {
1178 s8_t i;
1179 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1180 if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
1181 ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
1182 return i;
1183 }
1184 }
1185 return -1;
1186 }
1187
1188 #ifdef CELLULAR_SUPPORT
1189 void
netif_create_ip6_linklocal_address_from_if_id(struct netif * netif,u8_t * if_id)1190 netif_create_ip6_linklocal_address_from_if_id(struct netif *netif, u8_t *if_id)
1191 {
1192 u8_t i, addr_index;
1193
1194 /* Link-local prefix. */
1195 ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
1196 ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
1197 ip_2_ip6(&netif->ip6_addr[0])->addr[2] = PP_HTONL(if_id[0]<<24|if_id[1]<<16|if_id[2]<<8|if_id[3]);
1198 ip_2_ip6(&netif->ip6_addr[0])->addr[3] = PP_HTONL(if_id[4]<<24|if_id[5]<<16|if_id[6]<<8|if_id[7]);
1199 /* Set a link-local zone. Even though the zone is implied by the owning
1200 * netif, setting the zone anyway has two important conceptual advantages:
1201 * 1) it avoids the need for a ton of exceptions in internal code, allowing
1202 * e.g. ip6_addr_cmp() to be used on local addresses;
1203 * 2) the properly zoned address is visible externally, e.g. when any outside
1204 * code enumerates available addresses or uses one to bind a socket.
1205 * Any external code unaware of address scoping is likely to just ignore the
1206 * zone field, so this should not create any compatibility problems. */
1207 ip6_addr_assign_zone(ip_2_ip6(&netif->ip6_addr[0]), IP6_UNICAST, netif);
1208 /* Set address state. */
1209 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
1210 /* Will perform duplicate address detection (DAD). */
1211 netif_ip6_addr_set_state(netif, 0, IP6_ADDR_TENTATIVE);
1212 #else
1213 /* Consider address valid. */
1214 netif_ip6_addr_set_state(netif, 0, IP6_ADDR_PREFERRED);
1215 #endif /* LWIP_IPV6_AUTOCONFIG */
1216 }
1217 #endif /* CELLULAR_SUPPORT */
1218 /**
1219 * @ingroup netif_ip6
1220 * Create a link-local IPv6 address on a netif (stored in slot 0)
1221 *
1222 * @param netif the netif to create the address on
1223 * @param from_mac_48bit if != 0, assume hwadr is a 48-bit MAC address (std conversion)
1224 * if == 0, use hwaddr directly as interface ID
1225 */
1226 void
netif_create_ip6_linklocal_address(struct netif * netif,u8_t from_mac_48bit)1227 netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
1228 {
1229 u8_t i, addr_index;
1230
1231 /* Link-local prefix. */
1232 ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
1233 ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
1234
1235 /* Generate interface ID. */
1236 if (from_mac_48bit) {
1237 /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
1238 ip_2_ip6(&netif->ip6_addr[0])->addr[2] = lwip_htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
1239 ((u32_t)(netif->hwaddr[1]) << 16) |
1240 ((u32_t)(netif->hwaddr[2]) << 8) |
1241 (0xff));
1242 ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((0xfeul << 24) |
1243 ((u32_t)(netif->hwaddr[3]) << 16) |
1244 ((u32_t)(netif->hwaddr[4]) << 8) |
1245 (netif->hwaddr[5]));
1246 } else {
1247 /* Use hwaddr directly as interface ID. */
1248 ip_2_ip6(&netif->ip6_addr[0])->addr[2] = 0;
1249 ip_2_ip6(&netif->ip6_addr[0])->addr[3] = 0;
1250
1251 addr_index = 3;
1252 for (i = 0; (i < 8) && (i < netif->hwaddr_len); i++) {
1253 if (i == 4) {
1254 addr_index--;
1255 }
1256 ip_2_ip6(&netif->ip6_addr[0])->addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
1257 }
1258 }
1259
1260 /* Set address state. */
1261 #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
1262 /* Will perform duplicate address detection (DAD). */
1263 netif->ip6_addr_state[0] = IP6_ADDR_TENTATIVE;
1264 #else
1265 /* Consider address valid. */
1266 netif->ip6_addr_state[0] = IP6_ADDR_PREFERRED;
1267 #endif /* LWIP_IPV6_AUTOCONFIG */
1268 }
1269
1270 /**
1271 * @ingroup netif_ip6
1272 * This function allows for the easy addition of a new IPv6 address to an interface.
1273 * It takes care of finding an empty slot and then sets the address tentative
1274 * (to make sure that all the subsequent processing happens).
1275 *
1276 * @param netif netif to add the address on
1277 * @param ip6addr address to add
1278 * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
1279 */
1280 err_t
netif_add_ip6_address(struct netif * netif,const ip6_addr_t * ip6addr,s8_t * chosen_idx)1281 netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
1282 {
1283 s8_t i;
1284
1285 i = netif_get_ip6_addr_match(netif, ip6addr);
1286 if (i >= 0) {
1287 /* Address already added */
1288 if (chosen_idx != NULL) {
1289 *chosen_idx = i;
1290 }
1291 return ERR_OK;
1292 }
1293
1294 /* Find a free slot -- musn't be the first one (reserved for link local) */
1295 for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1296 if (!ip6_addr_isvalid(netif->ip6_addr_state[i])) {
1297 ip_addr_copy_from_ip6(netif->ip6_addr[i], *ip6addr);
1298 netif_ip6_addr_set_state(netif, i, IP6_ADDR_TENTATIVE);
1299 if (chosen_idx != NULL) {
1300 *chosen_idx = i;
1301 }
1302 return ERR_OK;
1303 }
1304 }
1305
1306 if (chosen_idx != NULL) {
1307 *chosen_idx = -1;
1308 }
1309 return ERR_VAL;
1310 }
1311
1312 /** Dummy IPv6 output function for netifs not supporting IPv6
1313 */
1314 static err_t
netif_null_output_ip6(struct netif * netif,struct pbuf * p,const ip6_addr_t * ipaddr)1315 netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
1316 {
1317 LWIP_UNUSED_ARG(netif);
1318 LWIP_UNUSED_ARG(p);
1319 LWIP_UNUSED_ARG(ipaddr);
1320
1321 return ERR_IF;
1322 }
1323 #endif /* LWIP_IPV6 */
1324