• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "lwip/fixme.h"
33 
34 #include <lwip/sys.h>
35 #include <lwip/snmp.h>
36 #include <lwip/etharp.h>
37 #include <lwip/netifapi.h>
38 #include <lwip/priv/api_msg.h>
39 
40 #define NETIFAPI_VAR_REF(name)      API_VAR_REF(name)
41 #define NETIFAPI_VAR_DECLARE(name)  API_VAR_DECLARE(struct netifapi_msg, name)
42 #define NETIFAPI_VAR_ALLOC(name)    API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM)
43 #define NETIFAPI_VAR_FREE(name)     API_VAR_FREE(MEMP_NETIFAPI_MSG, name)
44 
45 #if LWIP_DHCP
46 
47 #include <lwip/dhcp.h>
48 
49 /*
50  * Close DHCP and set static network.
51  * @param netif a pre-allocated netif structure
52  * @return ERR_OK, or ERR_VAL if failed.
53  */
netif_dhcp_off(struct netif * netif)54 err_t netif_dhcp_off(struct netif *netif)
55 {
56     ip_addr_t old_ipaddr;
57     ip_addr_t old_netmask;
58     ip_addr_t old_gateway;
59 
60     if (netif == NULL) {
61         return ERR_VAL;
62     }
63     old_ipaddr = netif->ip_addr;
64     old_netmask = netif->netmask;
65     old_gateway = netif->gw;
66 
67     if (netif_dhcp_data(netif)) {
68         (void)dhcp_release(netif);
69         (void)dhcp_stop(netif);
70         (void)dhcp_cleanup(netif);
71         LWIP_DEBUGF(NETIF_DEBUG, ("DHCP is close;set static IP\n"));
72     }
73 
74     ip_addr_set_val(&netif->ip_addr, &old_ipaddr);
75     ip_addr_set_val(&netif->netmask, &old_netmask);
76     ip_addr_set_val(&netif->gw, &old_gateway);
77     (void)netif_set_up(netif);
78 
79     return ERR_OK;
80 }
81 
dhcp_is_bound(struct netif * netif)82 err_t dhcp_is_bound(struct netif *netif)
83 {
84     struct dhcp *dhcp = NULL;
85 
86     LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG);
87 
88     dhcp = netif_dhcp_data(netif);
89     LWIP_ERROR("netif->dhcp != NULL", (dhcp != NULL), return ERR_ARG);
90 
91     if (dhcp->state == DHCP_STATE_BOUND) {
92         return ERR_OK;
93     } else {
94         return ERR_INPROGRESS;
95     }
96 }
97 
98 #endif /* LWIP_DHCP */
99 
100 #if LWIP_DHCPS
101 
102 #include "lwip/dhcps.h"
103 
netifapi_do_dhcps_start(struct tcpip_api_call_data * m)104 static err_t netifapi_do_dhcps_start(struct tcpip_api_call_data *m)
105 {
106     /* cast through void* to silence alignment warnings.
107      * We know it works because the structs have been instantiated as struct netifapi_msg */
108     err_t ret;
109     struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
110     ret = dhcps_start(msg->netif, msg->msg.dhcp_start_params.start_ip, msg->msg.dhcp_start_params.ip_num);
111     return ret;
112 }
113 
netifapi_dhcps_start(struct netif * netif,char * start_ip,u16_t ip_num)114 err_t netifapi_dhcps_start(struct netif *netif, char *start_ip, u16_t ip_num)
115 {
116     err_t err;
117     NETIFAPI_VAR_DECLARE(msg);
118 
119     LWIP_ERROR("netifapi_dhcps_start : invalid arguments", (netif != NULL), return ERR_VAL);
120     NETIFAPI_VAR_ALLOC(msg);
121 
122     NETIFAPI_VAR_REF(msg).netif = netif;
123     NETIFAPI_VAR_REF(msg).msg.dhcp_start_params.start_ip = start_ip;
124     NETIFAPI_VAR_REF(msg).msg.dhcp_start_params.ip_num = ip_num;
125 
126     err = tcpip_api_call(netifapi_do_dhcps_start, &API_VAR_REF(msg).call);
127 
128     NETIFAPI_VAR_FREE(msg);
129     return err;
130 }
131 
netifapi_dhcps_stop(struct netif * netif)132 err_t netifapi_dhcps_stop(struct netif *netif)
133 {
134     LWIP_ERROR("netifapi_dhcps_stop : invalid arguments", (netif != NULL), return ERR_VAL);
135 
136     return netifapi_netif_common(netif, dhcps_stop, NULL);
137 }
138 
139 #endif /* LWIP_DHCPS */
140 
141 /*
142  * This function is for making sure that accept() should not block indefinetely
143  * when removing IPv6 address used for accept() by using API[netifapi_netif_rmv_ip6_address].
144  */
tcp_unlock_accept(ip6_addr_t * ipaddr)145 static void tcp_unlock_accept(ip6_addr_t *ipaddr)
146 {
147     (void)ipaddr;
148 }
149 
netif_ip6_addr_setinvalid(struct netif * netif,const ip6_addr_t * addr6)150 static void netif_ip6_addr_setinvalid(struct netif *netif, const ip6_addr_t *addr6)
151 {
152     s8_t idx;
153     LWIP_ERROR("netif_ip6_addr_set : invalid arguments", (netif != NULL), return);
154     LWIP_ERROR("netif_ip6_addr_set : invalid arguments", (addr6 != NULL), return);
155 
156     idx = netif_get_ip6_addr_match(netif, addr6);
157     if (idx < 0) {
158         return;
159     }
160 
161     netif_ip6_addr_set_state(netif, idx, IP6_ADDR_INVALID);
162     return;
163 }
164 
netif_do_rmv_ipv6_addr(struct netif * netif,void * arguments)165 err_t netif_do_rmv_ipv6_addr(struct netif *netif, void *arguments)
166 {
167     ip_addr_t *ipaddr = (ip_addr_t *)arguments;
168 
169     if (IP_IS_V6(ipaddr)) {
170 #if LWIP_TCP
171         tcp_unlock_accept(ip_2_ip6(ipaddr));
172 #endif
173         netif_ip6_addr_setinvalid(netif, ip_2_ip6(ipaddr));
174     }
175     return ERR_OK;
176 }
177 
netif_do_rmv_ip6_address(struct tcpip_api_call_data * m)178 static err_t netif_do_rmv_ip6_address(struct tcpip_api_call_data *m)
179 {
180     /* cast through void* to silence alignment warnings.
181      * We know it works because the structs have been instantiated as struct netifapi_msg */
182     struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
183 
184     return netif_do_rmv_ipv6_addr(msg->netif, (void *)msg->msg.add.ipaddr);
185 }
186 
netifapi_netif_rmv_ip6_address(struct netif * netif,ip_addr_t * ipaddr)187 void netifapi_netif_rmv_ip6_address(struct netif *netif, ip_addr_t *ipaddr)
188 {
189     err_t err;
190     if (netif == NULL) {
191         return;
192     }
193     NETIFAPI_VAR_DECLARE(msg);
194     NETIFAPI_VAR_ALLOC(msg);
195 
196     NETIFAPI_VAR_REF(msg).netif = netif;
197     NETIFAPI_VAR_REF(msg).msg.add.ipaddr = (void *)ipaddr;
198 
199     err = tcpip_api_call(netif_do_rmv_ip6_address, &API_VAR_REF(msg).call);
200 
201     NETIFAPI_VAR_FREE(msg);
202     (void)err;
203 }
204 
netif_find_by_name(const char * name)205 static struct netif *netif_find_by_name(const char *name)
206 {
207     struct netif *netif = NULL;
208 
209     LWIP_ASSERT_CORE_LOCKED();
210 
211     if (name == NULL) {
212         return NULL;
213     }
214 
215     NETIF_FOREACH(netif) {
216         if (strcmp("lo", name) == 0 && (netif->name[0] == 'l' && netif->name[1] == 'o')) {
217             LWIP_DEBUGF(NETIF_DEBUG, ("netif_find_by_name: found lo\n"));
218             return netif;
219         }
220 
221         if (strcmp(netif->full_name, name) == 0) {
222             LWIP_DEBUGF(NETIF_DEBUG, ("netif_find_by_name: found %s\n", name));
223             return netif;
224         }
225     }
226 
227     LWIP_DEBUGF(NETIF_DEBUG, ("netif_find_by_name: didn't find %s\n", name));
228     return NULL;
229 }
230 
netifapi_do_find_by_name(struct tcpip_api_call_data * m)231 static err_t netifapi_do_find_by_name(struct tcpip_api_call_data *m)
232 {
233     /* cast through void* to silence alignment warnings.
234      * We know it works because the structs have been instantiated as struct netifapi_msg */
235     struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
236 
237     msg->netif = netif_find_by_name(msg->msg.ifs.name);
238     return ERR_OK;
239 }
240 
netifapi_netif_find_by_name(const char * name)241 struct netif *netifapi_netif_find_by_name(const char *name)
242 {
243     struct netif *netif = NULL;
244     NETIFAPI_VAR_DECLARE(msg);
245     NETIFAPI_VAR_ALLOC(msg);
246 
247     NETIFAPI_VAR_REF(msg).netif = NULL;
248 #if LWIP_MPU_COMPATIBLE
249     if (strncpy_s(NETIFAPI_VAR_REF(msg).msg.ifs.name, NETIF_NAMESIZE, name, NETIF_NAMESIZE - 1)) {
250         NETIFAPI_VAR_FREE(msg);
251         return netif;
252     }
253     NETIFAPI_VAR_REF(msg).msg.ifs.name[NETIF_NAMESIZE - 1] = '\0';
254 #else
255     NETIFAPI_VAR_REF(msg).msg.ifs.name = (char *)name;
256 #endif /* LWIP_MPU_COMPATIBLE */
257 
258     (void)tcpip_api_call(netifapi_do_find_by_name, &API_VAR_REF(msg).call);
259 
260     netif = msg.netif;
261     NETIFAPI_VAR_FREE(msg);
262     return netif;
263 }
264 
265 #define NETIF_MTU_MIN       1280
266 #ifndef IP_FRAG_MIN_MTU
267 #define IP_FRAG_MIN_MTU     68
268 #endif
269 
netif_set_mtu(struct netif * netif,u16_t netif_mtu)270 err_t netif_set_mtu(struct netif *netif, u16_t netif_mtu)
271 {
272     /*
273      * As per RFC 791, "Every internet module must be able to forward a datagram of 68
274      * octets without further fragmentation.  This is because an internet header
275      * may be up to 60 octets, and the minimum fragment is 8 octets."
276      */
277     LWIP_ERROR("netif_set_mtu: invalid arguments", (netif != NULL), return ERR_VAL);
278 
279 #if LWIP_IPV6
280     LWIP_ERROR("netif_set_mtu: invalid arguments", (netif_mtu >= NETIF_MTU_MIN) && (netif_mtu <= IP_FRAG_MAX_MTU),
281                return ERR_ARG);
282 #else
283     LWIP_ERROR("netif_set_mtu: invalid arguments", (netif_mtu >= IP_FRAG_MIN_MTU) && (netif_mtu <= IP_FRAG_MAX_MTU),
284              return ERR_ARG);
285 #endif
286 
287     netif->mtu = netif_mtu;
288 #if LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES
289     netif->mtu6 = netif_mtu;
290 #endif /* LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES */
291 
292     LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif: MTU of interface %s is changed to %d\n",
293                 netif_get_name(netif), netif->mtu));
294     return ERR_OK;
295 }
296 
netif_set_hwaddr(struct netif * netif,const unsigned char * hw_addr,int hw_len)297 err_t netif_set_hwaddr(struct netif *netif, const unsigned char *hw_addr, int hw_len)
298 {
299     LWIP_ERROR("netif_set_hwaddr : invalid arguments", (netif != NULL), return ERR_VAL);
300 
301     LWIP_ERROR("netif_set_hwaddr : invalid arguments", (hw_addr != NULL), return ERR_VAL);
302 
303     LWIP_ERROR("netif_set_hwaddr: invalid arguments",
304                ((unsigned int)hw_len == NETIF_MAX_HWADDR_LEN), return ERR_VAL);
305 
306     if (netif->drv_set_hwaddr == NULL) {
307         return ERR_IF; // ERR_OPNOTSUPP;
308     }
309 
310     if (netif->drv_set_hwaddr(netif, (u8_t *)hw_addr, hw_len) != ERR_OK) {
311         return ERR_VAL;
312     }
313 
314     if (memcpy_s(netif->hwaddr, NETIF_MAX_HWADDR_LEN, hw_addr, hw_len) != EOK) {
315         LWIP_DEBUGF(NETIF_DEBUG, ("netif_set_hwaddr: memcpy_s error\n"));
316         return ERR_VAL;
317     }
318 
319     LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
320                 ("netif: HW address of interface %s set to %02X:%02X:%02X:%02X:%02X:%02X\n",
321                  netif_get_name(netif),
322                  netif->hwaddr[0], netif->hwaddr[1], netif->hwaddr[2],
323                  netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]));
324 
325     return ERR_OK;
326 }
327 
etharp_update_arp_entry(struct netif * netif,const ip4_addr_t * ipaddr,struct eth_addr * ethaddr,u8_t flags)328 err_t etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
329 {
330     (void)netif;
331     (void)ipaddr;
332     (void)ethaddr;
333     (void)flags;
334     return 0;
335 }
336 
etharp_delete_arp_entry(struct netif * netif,ip4_addr_t * ipaddr)337 err_t etharp_delete_arp_entry(struct netif *netif, ip4_addr_t *ipaddr)
338 {
339     (void)netif;
340     (void)ipaddr;
341     return 0;
342 }
343 
344 
lwip_dns_setserver(u8_t numdns,ip_addr_t * dnsserver)345 err_t lwip_dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
346 {
347     (void)numdns;
348     (void)dnsserver;
349     return 0;
350 }
351 
lwip_dns_getserver(u8_t numdns,ip_addr_t * dnsserver)352 err_t lwip_dns_getserver(u8_t numdns, ip_addr_t *dnsserver)
353 {
354     (void)numdns;
355     (void)dnsserver;
356     return 0;
357 }
358 
359 #if PF_PKT_SUPPORT
360 struct raw_pcb *pkt_raw_pcbs;
361 #endif
362 
363 #if LWIP_RAW
364 struct raw_pcb *raw_pcbs; /* already defined in raw.c, but is static */
365 #endif
366 
367 #if LWIP_ENABLE_IP_CONFLICT_SIGNAL
368 u32_t is_ip_conflict_signal = 0;
369 sys_sem_t ip_conflict_detect;
370 #endif
371 
372 u32_t is_dup_detect_initialized = 0;
373 sys_sem_t dup_addr_detect;
374 
375 #if LWIP_SNTP
376 
377 #include <time.h>
378 
lwip_sntp_start(int server_num,char ** sntp_server,struct timeval * time)379 int lwip_sntp_start(int server_num, char **sntp_server, struct timeval *time)
380 {
381     (void)server_num;
382     (void)sntp_server;
383     (void)time;
384     return 0;
385 }
386 
387 #endif
388 
389 const char *const tcp_state_str[] = {
390     "CLOSED",
391     "LISTEN",
392     "SYN_SENT",
393     "SYN_RCVD",
394     "ESTABLISHED",
395     "FIN_WAIT_1",
396     "FIN_WAIT_2",
397     "CLOSE_WAIT",
398     "CLOSING",
399     "LAST_ACK",
400     "TIME_WAIT"
401 };
402 
403 volatile int tcpip_init_finish = 1; // needed by api_shell.c
404 
ip6addr_aton(const char * cp,ip6_addr_t * addr)405 int ip6addr_aton(const char *cp, ip6_addr_t *addr)
406 {
407     const int ipv6_blocks = 8;
408     u16_t current_block_index = 0;
409     u16_t current_block_value = 0;
410     u16_t addr16[ipv6_blocks];
411     u16_t *a16 = (u16_t *)addr->addr;
412     int squash_pos = ipv6_blocks;
413     int i;
414     const char *s = cp;
415     const char *ss = cp - 1;
416 
417     for (; ; s++) {
418         if (current_block_index >= ipv6_blocks) {
419             return 0; // address too long
420         }
421         if (*s == 0) {
422             if (s - ss == 1) {
423                 if (squash_pos != current_block_index) {
424                     return 0; // empty address or address ends with a single ':'
425                 } // else address ends with one valid "::"
426             } else {
427                 addr16[current_block_index++] = current_block_value;
428             }
429             break;
430         } else if (*s == ':') {
431             if (s - ss == 1) {
432                 if (s != cp || s[1] != ':') {
433                     return 0; // address begins with a single ':' or contains ":::"
434                 } // else address begins with one valid "::"
435             } else {
436                 addr16[current_block_index++] = current_block_value;
437             }
438             if (s[1] == ':') {
439                 if (squash_pos != ipv6_blocks) {
440                     return 0; // more than one "::"
441                 }
442                 squash_pos = current_block_index;
443                 s++;
444             }
445             ss = s; // ss points to the recent ':' position
446             current_block_value = 0;
447         } else if (lwip_isxdigit(*s) && (s - ss) < 5) { // 4 hex-digits at most
448             current_block_value = (current_block_value << 4) +
449                 ((u8_t)(*s) | ('a' - 'A')) - '0' - ('a' - '9' - 1) * (*s >= 'A');
450 #if LWIP_IPV4
451         } else if (*s == '.' && current_block_index < ipv6_blocks - 1) {
452             ip4_addr_t ip4;
453             int ret = ip4addr_aton(ss+1, &ip4);
454             if (!ret) {
455                 return 0;
456             }
457             ip4.addr = lwip_ntohl(ip4.addr);
458             addr16[current_block_index++] = (u16_t)(ip4.addr >> 16);
459             addr16[current_block_index++] = (u16_t)(ip4.addr);
460             break;
461 #endif /* LWIP_IPV4 */
462         } else {
463             return 0; // unexpected char or too many digits
464         }
465     }
466 
467     if (squash_pos == ipv6_blocks && current_block_index != ipv6_blocks) {
468         return 0; // address too short
469     }
470     if (squash_pos != ipv6_blocks && current_block_index == ipv6_blocks) {
471         return 0; // unexpected "::" in address
472     }
473 
474     for (i = 0; i < squash_pos; ++i) {
475         a16[i] = lwip_htons(addr16[i]);
476     }
477     for (; i < ipv6_blocks - current_block_index + squash_pos; ++i) {
478         a16[i] = 0;
479     }
480     for (; i < ipv6_blocks; ++i) {
481         a16[i] = lwip_htons(addr16[i - ipv6_blocks + current_block_index]);
482     }
483 
484     return 1;
485 }
486