1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 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/sys.h>
33 #include <lwip/etharp.h>
34 #include <lwip/netifapi.h>
35 #include <lwip/priv/api_msg.h>
36
37 #define NETIFAPI_VAR_REF(name) API_VAR_REF(name)
38 #define NETIFAPI_VAR_DECLARE(name) API_VAR_DECLARE(struct netifapi_msg, name)
39 #define NETIFAPI_VAR_ALLOC(name) API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM)
40 #define NETIFAPI_VAR_FREE(name) API_VAR_FREE(MEMP_NETIFAPI_MSG, name)
41
42 #if LWIP_DHCP
43 #include <lwip/dhcp.h>
44 #include <lwip/prot/dhcp.h>
45
dhcp_is_bound(struct netif * netif)46 err_t dhcp_is_bound(struct netif *netif)
47 {
48 struct dhcp *dhcp = NULL;
49
50 LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG);
51
52 dhcp = netif_dhcp_data(netif);
53 LWIP_ERROR("netif->dhcp != NULL", (dhcp != NULL), return ERR_ARG);
54
55 if (dhcp->state == DHCP_STATE_BOUND) {
56 return ERR_OK;
57 } else {
58 return ERR_INPROGRESS;
59 }
60 }
61 #endif /* LWIP_DHCP */
62
netif_find_by_name(const char * name)63 static struct netif *netif_find_by_name(const char *name)
64 {
65 struct netif *netif = NULL;
66 LWIP_ASSERT_CORE_LOCKED();
67 if (name == NULL) {
68 return NULL;
69 }
70 NETIF_FOREACH(netif) {
71 if (strcmp("lo", name) == 0 && (netif->name[0] == 'l' && netif->name[1] == 'o')) {
72 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find_by_name: found lo\n"));
73 return netif;
74 }
75
76 if (strcmp(netif->full_name, name) == 0) {
77 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find_by_name: found %s\n", name));
78 return netif;
79 }
80 }
81
82 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find_by_name: didn't find %s\n", name));
83 return NULL;
84 }
85
netifapi_do_find_by_name(struct tcpip_api_call_data * m)86 static err_t netifapi_do_find_by_name(struct tcpip_api_call_data *m)
87 {
88 /* cast through void* to silence alignment warnings.
89 * We know it works because the structs have been instantiated as struct netifapi_msg */
90 struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
91 msg->netif = netif_find_by_name(msg->msg.ifs.name);
92 return ERR_OK;
93 }
94
netifapi_netif_find_by_name(const char * name)95 struct netif *netifapi_netif_find_by_name(const char *name)
96 {
97 struct netif *netif = NULL;
98 NETIFAPI_VAR_DECLARE(msg);
99 NETIFAPI_VAR_ALLOC(msg);
100 NETIFAPI_VAR_REF(msg).netif = NULL;
101 #if LWIP_MPU_COMPATIBLE
102 if (strncpy_s(NETIFAPI_VAR_REF(msg).msg.ifs.name, NETIF_NAMESIZE, name, NETIF_NAMESIZE - 1)) {
103 NETIFAPI_VAR_FREE(msg);
104 return netif;
105 }
106 NETIFAPI_VAR_REF(msg).msg.ifs.name[NETIF_NAMESIZE - 1] = '\0';
107 #else
108 NETIFAPI_VAR_REF(msg).msg.ifs.name = (char *)name;
109 #endif /* LWIP_MPU_COMPATIBLE */
110
111 (void)tcpip_api_call(netifapi_do_find_by_name, &API_VAR_REF(msg).call);
112 netif = msg.netif;
113 NETIFAPI_VAR_FREE(msg);
114 return netif;
115 }
116
117 #if LWIP_IPV6
ip6addr_aton(const char * cp,ip6_addr_t * addr)118 int ip6addr_aton(const char *cp, ip6_addr_t *addr)
119 {
120 const int ipv6_blocks = 8;
121 u16_t current_block_index = 0;
122 u16_t current_block_value = 0;
123 u16_t addr16[ipv6_blocks];
124 u16_t *a16 = (u16_t *)addr->addr;
125 int squash_pos = ipv6_blocks;
126 int i;
127 const unsigned char *sc = (const unsigned char *)cp;
128 const unsigned char *ss = (const unsigned char *)(cp - 1);
129
130 for (; ; sc++) {
131 if (current_block_index >= ipv6_blocks) {
132 return 0; // address too long
133 }
134 if (*sc == 0) {
135 if (sc - ss == 1) {
136 if (squash_pos != current_block_index) {
137 return 0; // empty address or address ends with a single ':'
138 } // else address ends with one valid "::"
139 } else {
140 addr16[current_block_index++] = current_block_value;
141 }
142 break;
143 } else if (*sc == ':') {
144 if (sc - ss == 1) {
145 if (sc != (const unsigned char *)cp || sc[1] != ':') {
146 return 0; // address begins with a single ':' or contains ":::"
147 } // else address begins with one valid "::"
148 } else {
149 addr16[current_block_index++] = current_block_value;
150 }
151 if (sc[1] == ':') {
152 if (squash_pos != ipv6_blocks) {
153 return 0; // more than one "::"
154 }
155 squash_pos = current_block_index;
156 sc++;
157 }
158 ss = sc; // ss points to the recent ':' position
159 current_block_value = 0;
160 } else if (lwip_isxdigit(*sc) && (sc - ss) < 5) { // 4 hex-digits at most
161 current_block_value = (current_block_value << 4) +
162 (*sc | ('a' - 'A')) - '0' - ('a' - '9' - 1) * (*sc >= 'A');
163 #if LWIP_IPV4
164 } else if (*sc == '.' && current_block_index < ipv6_blocks - 1) {
165 ip4_addr_t ip4;
166 int ret = ip4addr_aton((const char *)(ss + 1), &ip4);
167 if (!ret) {
168 return 0;
169 }
170 ip4.addr = lwip_ntohl(ip4.addr);
171 addr16[current_block_index++] = (u16_t)(ip4.addr >> 16);
172 addr16[current_block_index++] = (u16_t)(ip4.addr);
173 break;
174 #endif /* LWIP_IPV4 */
175 } else {
176 return 0; // unexpected char or too many digits
177 }
178 }
179
180 if (squash_pos == ipv6_blocks && current_block_index != ipv6_blocks) {
181 return 0; // address too short
182 }
183 if (squash_pos != ipv6_blocks && current_block_index == ipv6_blocks) {
184 return 0; // unexpected "::" in address
185 }
186
187 for (i = 0; i < squash_pos; ++i) {
188 a16[i] = lwip_htons(addr16[i]);
189 }
190 for (; i < ipv6_blocks - current_block_index + squash_pos; ++i) {
191 a16[i] = 0;
192 }
193 for (; i < ipv6_blocks; ++i) {
194 a16[i] = lwip_htons(addr16[i - ipv6_blocks + current_block_index]);
195 }
196
197 return 1;
198 }
199 #endif /* LWIP_IPV6 */
200