• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Beken Corporation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <common/bk_include.h>
17 #include <stdio.h>
18 #include <string.h>
19 
20 #include <lwip/inet.h>
21 #include "netif/etharp.h"
22 #include "lwip/netif.h"
23 #include <lwip/netifapi.h>
24 #include <lwip/tcpip.h>
25 #include <lwip/dns.h>
26 #include <lwip/dhcp.h>
27 #include "lwip/prot/dhcp.h"
28 #include "lwip/apps/mdns.h"
29 
30 #include <lwip/sockets.h>
31 #include "ethernetif.h"
32 
33 #include <components/system.h>
34 #include "bk_drv_model.h"
35 #include <os/mem.h>
36 #include "bk_wifi_netif.h" //TODO use standard EVENT instead!!!
37 #include "lwip_netif_address.h"
38 #include <os/os.h>
39 #include "net.h"
40 
41 #include "wlan_defs_pub.h"
42 #include "bk_wifi.h"
43 #include <os/str.h>
44 #include <modules/wifi.h>
45 #include "wlan_ui_pub.h"
46 
47 /* forward declaration */
48 FUNC_1PARAM_PTR bk_wlan_get_status_cb(void);
49 
50 struct ipv4_config sta_ip_settings = {
51 	.addr_type = ADDR_TYPE_DHCP,
52 	.address = 0,
53 	.gw = 0,
54 	.netmask = 0,
55 	.dns1 = 0,
56 	.dns2 = 0,
57 };
58 
59 struct ipv4_config uap_ip_settings = {
60 	.addr_type = ADDR_TYPE_STATIC,
61 	.address = 0xc0a80001, //192.168.0.1
62 	.gw = 0xc0a80001,      //192.168.0.1
63 	.netmask = 0xffffff00, //255.255.255.0
64 	.dns1 = 0xc0a80001,    //192.168.0.1
65 	.dns2 = 0,
66 };
67 static char up_iface;
68 uint8 sta_ip_start_flag = 0;
69 uint8 uap_ip_start_flag = 0;
70 
71 #ifdef CONFIG_IPV6
72 #define IPV6_ADDR_STATE_TENTATIVE       "Tentative"
73 #define IPV6_ADDR_STATE_PREFERRED       "Preferred"
74 #define IPV6_ADDR_STATE_INVALID         "Invalid"
75 #define IPV6_ADDR_STATE_VALID           "Valid"
76 #define IPV6_ADDR_STATE_DEPRECATED      "Deprecated"
77 #define IPV6_ADDR_TYPE_LINKLOCAL	    "Link-Local"
78 #define IPV6_ADDR_TYPE_GLOBAL		    "Global"
79 #define IPV6_ADDR_TYPE_UNIQUELOCAL	    "Unique-Local"
80 #define IPV6_ADDR_TYPE_SITELOCAL	    "Site-Local"
81 #define IPV6_ADDR_UNKNOWN		        "Unknown"
82 #endif
83 
84 typedef void (*net_sta_ipup_cb_fn)(void* data);
85 
86 struct interface {
87 	struct netif netif;
88 	ip_addr_t ipaddr;
89 	ip_addr_t nmask;
90 	ip_addr_t gw;
91 };
92 FUNCPTR sta_connected_func;
93 
94 static struct interface g_mlan = {{0}};
95 static struct interface g_uap = {{0}};
96 net_sta_ipup_cb_fn sta_ipup_cb = NULL;
97 
98 extern void *net_get_sta_handle(void);
99 extern void *net_get_uap_handle(void);
100 extern err_t lwip_netif_init(struct netif *netif);
101 extern err_t lwip_netif_uap_init(struct netif *netif);
102 extern int net_get_if_ip_addr(uint32_t *ip, void *intrfc_handle);
103 extern int net_get_if_ip_mask(uint32_t *nm, void *intrfc_handle);
104 extern int net_configure_address(struct ipv4_config *addr, void *intrfc_handle);
105 extern int dhcp_server_start(void *intrfc_handle);
106 extern void dhcp_server_stop(void);
107 extern void net_configure_dns(struct wlan_ip_config *ip);
108 extern bk_err_t bk_wifi_get_ip_status(IPStatusTypedef *outNetpara, WiFi_Interface inInterface);
109 
110 
111 #ifdef CONFIG_IPV6
ipv6_addr_state_to_desc(unsigned char addr_state)112 char *ipv6_addr_state_to_desc(unsigned char addr_state)
113 {
114 	if (ip6_addr_istentative(addr_state))
115 		return IPV6_ADDR_STATE_TENTATIVE;
116 	else if (ip6_addr_ispreferred(addr_state))
117 		return IPV6_ADDR_STATE_PREFERRED;
118 	else if (ip6_addr_isinvalid(addr_state))
119 		return IPV6_ADDR_STATE_INVALID;
120 	else if (ip6_addr_isvalid(addr_state))
121 		return IPV6_ADDR_STATE_VALID;
122 	else if (ip6_addr_isdeprecated(addr_state))
123 		return IPV6_ADDR_STATE_DEPRECATED;
124 	else
125 		return IPV6_ADDR_UNKNOWN;
126 }
127 
ipv6_addr_type_to_desc(struct ipv6_config * ipv6_conf)128 char *ipv6_addr_type_to_desc(struct ipv6_config *ipv6_conf)
129 {
130 	if (ip6_addr_islinklocal((ip6_addr_t *)&ipv6_conf->address))
131 		return IPV6_ADDR_TYPE_LINKLOCAL;
132 	else if (ip6_addr_isglobal((ip6_addr_t *)&ipv6_conf->address))
133 		return IPV6_ADDR_TYPE_GLOBAL;
134 	else if (ip6_addr_isuniquelocal((ip6_addr_t *)&ipv6_conf->address))
135 		return IPV6_ADDR_TYPE_UNIQUELOCAL;
136 	else if (ip6_addr_issitelocal((ip6_addr_t *)&ipv6_conf->address))
137 		return IPV6_ADDR_TYPE_SITELOCAL;
138 	else
139 		return IPV6_ADDR_UNKNOWN;
140 }
141 #endif /* CONFIG_IPV6 */
142 
net_dhcp_hostname_set(char * hostname)143 int net_dhcp_hostname_set(char *hostname)
144 {
145 	netif_set_hostname(&g_mlan.netif, hostname);
146 	return 0;
147 }
148 
net_ipv4stack_init(void)149 void net_ipv4stack_init(void)
150 {
151 	static bool tcpip_init_done = 0;
152 	if (tcpip_init_done)
153 		return;
154 
155 	LWIP_LOGI("init TCP/IP\r\n");
156 	tcpip_init(NULL, NULL);
157 	tcpip_init_done = true;
158 }
159 
160 #ifdef CONFIG_IPV6
net_ipv6stack_init(struct netif * netif)161 void net_ipv6stack_init(struct netif *netif)
162 {
163 	bk_get_mac(netif->hwaddr, MAC_TYPE_STA);
164 	netif->hwaddr_len = 6;
165 }
166 #endif /* CONFIG_IPV6 */
167 
net_wlan_init(void)168 void net_wlan_init(void)
169 {
170 	static int wlan_init_done = 0;
171 	int ret;
172 
173 	if (!wlan_init_done) {
174 		net_ipv4stack_init();
175         ip_addr_set_ip4_u32(&g_mlan.ipaddr,INADDR_ANY);
176 		ret = netifapi_netif_add(&g_mlan.netif,ip_2_ip4(&g_mlan.ipaddr),
177 					 ip_2_ip4(&g_mlan.ipaddr), ip_2_ip4(&g_mlan.ipaddr), NULL,
178 					 lwip_netif_init, tcpip_input);
179 		if (ret) {
180 			/*FIXME: Handle the error case cleanly */
181 			LWIP_LOGE("sta netif add failed");
182 		}
183 #ifdef CONFIG_IPV6
184 		net_ipv6stack_init(&g_mlan.netif);
185 #endif /* CONFIG_IPV6 */
186 
187 		ret = netifapi_netif_add(&g_uap.netif, ip_2_ip4(&g_uap.ipaddr),
188 					 ip_2_ip4(&g_uap.ipaddr), ip_2_ip4(&g_uap.ipaddr), NULL,
189 					 lwip_netif_uap_init, tcpip_input);
190 		if (ret) {
191 			/*FIXME: Handle the error case cleanly */
192 			LWIP_LOGE("ap netif add failed");
193 		}
194 		wlan_init_done = 1;
195 	}
196 
197 	return;
198 }
199 
net_set_sta_ipup_callback(void * fn)200 void net_set_sta_ipup_callback(void *fn)
201 {
202     sta_ipup_cb = (net_sta_ipup_cb_fn)fn;
203 }
204 
user_connected_callback(FUNCPTR fn)205 void user_connected_callback(FUNCPTR fn)
206 {
207 	sta_connected_func = fn;
208 }
209 
wm_netif_status_static_callback(struct netif * n)210 static void wm_netif_status_static_callback(struct netif *n)
211 {
212 	if (n->flags & NETIF_FLAG_UP) {
213 		// static IP success;
214 		LWIP_LOGI("using static ip...\n");
215 		wifi_netif_notify_sta_got_ip();
216 #if CONFIG_LWIP_FAST_DHCP
217 		/* read stored IP from flash as the static IP */
218 		struct wlan_fast_connect_info fci = {0};
219 		wlan_read_fast_connect_info(&fci);
220 		os_memcpy((char *)&n->ip_addr, (char *)&fci.ip_addr, sizeof(fci.ip_addr));
221 		os_memcpy((char *)&n->netmask, (char *)&fci.netmask, sizeof(fci.netmask));
222 		os_memcpy((char *)&n->gw, (char *)&fci.gw, sizeof(fci.gw));
223 		os_memcpy((char *)&n->dns1, (char *)&fci.dns1, sizeof(fci.dns1));
224 		LWIP_LOGI("ip_addr: "BK_IP4_FORMAT" \r\n", BK_IP4_STR(ip_addr_get_ip4_u32(&n->ip_addr)));
225 #endif
226 
227 #if !CONFIG_DISABLE_DEPRECIATED_WIFI_API
228 		if (sta_ipup_cb != NULL)
229 			sta_ipup_cb(NULL);
230 
231 		if (sta_connected_func != NULL)
232 			(*sta_connected_func)();
233 #endif
234 	} else {
235 		// static IP fail;
236 	}
237 }
238 
239 extern wifi_connect_tick_t sta_tick;
wm_netif_status_callback(struct netif * n)240 static void wm_netif_status_callback(struct netif *n)
241 {
242 	struct dhcp *dhcp;
243 
244 	if (n->flags & NETIF_FLAG_UP) {
245 		dhcp = netif_dhcp_data(n);
246 #ifdef CONFIG_IPV6
247         int i;
248         u8 *ipv6_addr;
249 
250         {
251             for (i = 0; i < MAX_IPV6_ADDRESSES; i++) {
252                 if (ip6_addr_isvalid(netif_ip6_addr_state(n, i))) {
253                     ipv6_addr = (u8*)(ip_2_ip6(&n->ip6_addr[i]))->addr;
254                     bk_printf("ipv6_addr[%d] : %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\r\n", i,
255                         ipv6_addr[0], ipv6_addr[1], ipv6_addr[2], ipv6_addr[3],
256                         ipv6_addr[4], ipv6_addr[5], ipv6_addr[6], ipv6_addr[7],
257                         ipv6_addr[8], ipv6_addr[9], ipv6_addr[10], ipv6_addr[11],
258                         ipv6_addr[12], ipv6_addr[13], ipv6_addr[14], ipv6_addr[15]);
259                     bk_printf("ipv6_type[%d] :0x%x\r\n", i, n->ip6_addr[i].type);
260                     bk_printf("ipv6_state[%d] :0x%x\r\n", i, n->ip6_addr_state[i]);
261                 }
262             }
263         }
264 #endif
265 
266 #ifdef CONFIG_MDNS
267 		if(!ip_addr_isany(&n->ip_addr)){
268 			mdns_resp_restart(n);
269 		}
270 #endif
271 
272 		if(dhcp != NULL) {
273 			/* dhcp success*/
274 			if (dhcp->state == DHCP_STATE_BOUND) {
275 				LWIP_LOGI("ip_addr: "BK_IP4_FORMAT" \r\n", BK_IP4_STR(ip_addr_get_ip4_u32(&n->ip_addr)));
276 				sta_tick.sta_ip_tick = rtos_get_time();
277 				LWIP_LOGI("STA assoc delta:%d, eapol delta:%d, dhcp delta:%d, total:%d\n",
278 					sta_tick.sta_assoc_tick - sta_tick.sta_start_tick,
279 					sta_tick.sta_eapol_tick - sta_tick.sta_assoc_tick,
280 					sta_tick.sta_ip_tick - sta_tick.sta_eapol_tick,
281 					sta_tick.sta_ip_tick - sta_tick.sta_start_tick);
282 #if !CONFIG_DISABLE_DEPRECIATED_WIFI_API
283 				wifi_netif_call_status_cb_when_sta_got_ip();
284 #endif
285 				wifi_netif_notify_sta_got_ip();
286 
287 #if CONFIG_LWIP_FAST_DHCP
288 				/* store current IP to flash */
289 				const ip_addr_t *dns_server;
290 				dns_server = dns_getserver(0);
291 				n->dns1 = ip_addr_get_ip4_u32(dns_server);
292 				struct wlan_fast_connect_info fci = {0};
293 				wlan_read_fast_connect_info(&fci);
294 				os_memset(&fci.ip_addr, 0, sizeof(ip_addr_t)*4);
295 				os_memcpy((char *)&fci.ip_addr, (char *)&n->ip_addr, sizeof(n->ip_addr));
296 				os_memcpy((char *)&fci.netmask, (char *)&n->netmask, sizeof(n->netmask));
297 				os_memcpy((char *)&fci.gw, (char *)&n->gw, sizeof(n->gw));
298 				os_memcpy((char *)&fci.dns1, (char *)&n->dns1, sizeof(n->dns1));
299 				wlan_write_fast_connect_info(&fci);
300 #endif
301 
302 #if !CONFIG_DISABLE_DEPRECIATED_WIFI_API
303 				if(sta_ipup_cb != NULL)
304 					sta_ipup_cb(NULL);
305 
306 				if(sta_connected_func != NULL)
307 					(*sta_connected_func)();
308 #endif
309 			} else {
310 				// dhcp fail
311 #if !CONFIG_DISABLE_DEPRECIATED_WIFI_API
312 				wifi_netif_call_status_cb_when_sta_dhcp_timeout();
313 #endif
314 			}
315 		} else {
316 			// static IP success;
317 		}
318 	} else {
319 		// dhcp fail;
320 	}
321 }
322 
check_iface_mask(void * handle,uint32_t ipaddr)323 static int check_iface_mask(void *handle, uint32_t ipaddr)
324 {
325 	uint32_t interface_ip, interface_mask;
326 	net_get_if_ip_addr(&interface_ip, handle);
327 	net_get_if_ip_mask(&interface_mask, handle);
328 	if (interface_ip > 0)
329 		if ((interface_ip & interface_mask) ==
330 		    (ipaddr & interface_mask))
331 			return 0;
332 	return -1;
333 }
334 
net_ip_to_interface(uint32_t ipaddr)335 void *net_ip_to_interface(uint32_t ipaddr)
336 {
337 	int ret;
338 	void *handle;
339 	/* Check mlan handle */
340 	handle = net_get_sta_handle();
341 	ret = check_iface_mask(handle, ipaddr);
342 	if (ret == 0)
343 		return handle;
344 
345 	/* Check uap handle */
346 	handle = net_get_uap_handle();
347 	ret = check_iface_mask(handle, ipaddr);
348 	if (ret == 0)
349 		return handle;
350 
351 	/* If more interfaces are added then above check needs to done for
352 	 * those newly added interfaces
353 	 */
354 	return NULL;
355 }
356 
net_sock_to_interface(int sock)357 void *net_sock_to_interface(int sock)
358 {
359 	struct sockaddr_in peer;
360 	unsigned long peerlen = sizeof(struct sockaddr_in);
361 	void *req_iface = NULL;
362 
363 	getpeername(sock, (struct sockaddr *)&peer, &peerlen);
364 	req_iface = net_ip_to_interface(peer.sin_addr.s_addr);
365 	return req_iface;
366 }
367 
net_get_sta_handle(void)368 void *net_get_sta_handle(void)
369 {
370 	return &g_mlan.netif;
371 }
372 
net_get_uap_handle(void)373 void *net_get_uap_handle(void)
374 {
375 	return &g_uap.netif;
376 }
377 
net_get_netif_handle(uint8_t iface)378 void *net_get_netif_handle(uint8_t iface)
379 {
380 
381     return NULL;
382 }
383 
net_interface_up(void * intrfc_handle)384 void net_interface_up(void *intrfc_handle)
385 {
386 	struct interface *if_handle = (struct interface *)intrfc_handle;
387 	netifapi_netif_set_up(&if_handle->netif);
388 }
389 
net_interface_down(void * intrfc_handle)390 void net_interface_down(void *intrfc_handle)
391 {
392 	struct interface *if_handle = (struct interface *)intrfc_handle;
393 	netifapi_netif_set_down(&if_handle->netif);
394 }
395 
396 
net_interface_dhcp_stop(void * intrfc_handle)397 void net_interface_dhcp_stop(void *intrfc_handle)
398 {
399 	struct interface *if_handle = (struct interface *)intrfc_handle;
400 	netifapi_dhcp_stop(&if_handle->netif);
401 	netif_set_status_callback(&if_handle->netif, NULL);
402 }
403 
sta_ip_down(void)404 void sta_ip_down(void)
405 {
406 	if(sta_ip_start_flag)
407 	{
408 		LWIP_LOGI("sta ip down\r\n");
409 
410 		sta_ip_start_flag = 0;
411 
412 		netifapi_netif_set_down(&g_mlan.netif);
413 		netif_set_status_callback(&g_mlan.netif, NULL);
414 		netifapi_dhcp_stop(&g_mlan.netif);
415 #if LWIP_IPV6
416 		for(u8_t addr_idx = 1; addr_idx < LWIP_IPV6_NUM_ADDRESSES; addr_idx++) {
417 			netif_ip6_addr_set(&g_mlan.netif, addr_idx, (const ip6_addr_t *)IP6_ADDR_ANY);
418 			g_mlan.netif.ip6_addr_state[addr_idx] = IP6_ADDR_INVALID;
419 		}
420 #endif
421 	}
422 }
423 
sta_ip_start(void)424 void sta_ip_start(void)
425 {
426 	struct wlan_ip_config address = {0};
427 
428 	if(!sta_ip_start_flag) {
429 		LWIP_LOGI("sta ip start\r\n");
430 		sta_ip_start_flag = 1;
431 		net_configure_address(&sta_ip_settings, net_get_sta_handle());
432 		return;
433 	}
434 
435 	LWIP_LOGI("sta ip start: "BK_IP4_FORMAT" \r\n", BK_IP4_STR(address.ipv4.address));
436 	net_get_if_addr(&address, net_get_sta_handle());
437 	if(wifi_netif_sta_is_connected()
438 		&& (0 != address.ipv4.address)) {
439 		wifi_netif_notify_sta_got_ip();
440 	}
441 }
442 
sta_set_default_netif(void)443 void sta_set_default_netif(void)
444 {
445     netifapi_netif_set_default(net_get_sta_handle());
446 }
447 
ap_set_default_netif(void)448 void ap_set_default_netif(void)
449 {
450     // as the default netif is sta's netif, so ap need to send
451     // boardcast or not sub net packets, need set ap netif before
452     // send those packets, after finish sending, reset default netif
453     // to sat's netif.
454     netifapi_netif_set_default(net_get_uap_handle());
455 }
456 
reset_default_netif(void)457 void reset_default_netif(void)
458 {
459     netifapi_netif_set_default(NULL);
460 }
461 
sta_ip_is_start(void)462 uint32_t sta_ip_is_start(void)
463 {
464 	return sta_ip_start_flag;
465 }
466 
uap_ip_down(void)467 void uap_ip_down(void)
468 {
469 	if (uap_ip_start_flag )
470 	{
471 		LWIP_LOGI("uap ip down\r\n");
472 		uap_ip_start_flag = 0;
473 
474 		netifapi_netif_set_down(&g_uap.netif);
475 		netif_set_status_callback(&g_uap.netif, NULL);
476 		dhcp_server_stop();
477 	}
478 }
479 
uap_ip_start(void)480 void uap_ip_start(void)
481 {
482 	if ( !uap_ip_start_flag )
483 	{
484 		LWIP_LOGI("uap ip start\r\n");
485 		uap_ip_start_flag = 1;
486     	net_configure_address(&uap_ip_settings, net_get_uap_handle());
487     }
488 }
489 
uap_ip_is_start(void)490 uint32_t uap_ip_is_start(void)
491 {
492 	return uap_ip_start_flag;
493 }
494 
495 #define DEF_UAP_IP	      0xc0a80a01UL /* 192.168.10.1 */
496 
ip_address_set(int iface,int dhcp,char * ip,char * mask,char * gw,char * dns)497 void ip_address_set(int iface, int dhcp, char *ip, char *mask, char*gw, char*dns)
498 {
499 	uint32_t tmp;
500 	struct ipv4_config addr;
501 
502 	memset(&addr, 0, sizeof(struct ipv4_config));
503 	if (dhcp == 1) {
504 		addr.addr_type = ADDR_TYPE_DHCP;
505 	} else {
506 		addr.addr_type = ADDR_TYPE_STATIC;
507 	    tmp = inet_addr((char*)ip);
508 	    addr.address = (tmp);
509 	    tmp = inet_addr((char*)mask);
510 	    if (tmp == 0xFFFFFFFF)
511 	        tmp = 0x00FFFFFF;// if not set valid netmask, set as 255.255.255.0
512 	    addr.netmask= (tmp);
513 	    tmp = inet_addr((char*)gw);
514 	    addr.gw = (tmp);
515 
516 	    tmp = inet_addr((char*)dns);
517 	    addr.dns1 = (tmp);
518 	}
519 
520 	if (iface == 1) // Station
521 		memcpy(&sta_ip_settings, &addr, sizeof(addr));
522 	else
523 		memcpy(&uap_ip_settings, &addr, sizeof(addr));
524 }
525 
sta_ip_mode_set(int dhcp)526 void sta_ip_mode_set(int dhcp)
527 {
528 	if (dhcp == 1) {
529 		ip_address_set(1, DHCP_CLIENT, NULL, NULL, NULL, NULL);
530 	} else {
531 	        IPStatusTypedef ipStatus;
532 	        bk_err_t ret = kNoErr;
533 		os_memset(&ipStatus, 0x0, sizeof(IPStatusTypedef));
534 		ret = bk_wifi_get_ip_status(&ipStatus, BK_STATION);
535 		if (ret == kNoErr)
536 		    ip_address_set(1, DHCP_DISABLE, ipStatus.ip, ipStatus.mask, ipStatus.gate, ipStatus.dns);
537 	}
538 }
539 
540 #if CONFIG_LWIP_FAST_DHCP
net_restart_dhcp(void)541 void net_restart_dhcp(void)
542 {
543 	sta_ip_down();
544 	ip_address_set(BK_STATION, DHCP_CLIENT, NULL, NULL, NULL, NULL);
545 	sta_ip_start();
546 }
547 #endif
548 
net_configure_address(struct ipv4_config * addr,void * intrfc_handle)549 int net_configure_address(struct ipv4_config *addr, void *intrfc_handle)
550 {
551 	if (!intrfc_handle)
552 		return -1;
553 
554 	struct interface *if_handle = (struct interface *)intrfc_handle;
555 
556 	LWIP_LOGI("configuring interface %s (with %s)\n",
557 		(if_handle == &g_mlan) ? "sta" :"ap",
558 		(addr->addr_type == ADDR_TYPE_DHCP)
559 		? "DHCP client" : "Static IP");
560 	netifapi_netif_set_down(&if_handle->netif);
561 
562 	/* De-register previously registered DHCP Callback for correct
563 	 * address configuration.
564 	 */
565 	netif_set_status_callback(&if_handle->netif, NULL);
566 
567 	switch (addr->addr_type) {
568 	case ADDR_TYPE_STATIC:
569 		ip_addr_set_ip4_u32(&if_handle->ipaddr , addr->address);
570 		ip_addr_set_ip4_u32(&if_handle->nmask,addr->netmask);
571 		ip_addr_set_ip4_u32(&if_handle->gw ,addr->gw);
572 
573 		netifapi_netif_set_addr(&if_handle->netif, ip_2_ip4(&if_handle->ipaddr),
574 					ip_2_ip4(&if_handle->nmask), ip_2_ip4(&if_handle->gw));
575 
576 		//AP never configure DNS server address!!!
577 
578 		if(if_handle == &g_mlan)
579 		{
580 			netif_set_status_callback(&if_handle->netif,
581 										wm_netif_status_static_callback);
582 		}
583 		netifapi_netif_set_up(&if_handle->netif);
584 		net_configure_dns((struct wlan_ip_config *)addr);
585 		break;
586 
587 	case ADDR_TYPE_DHCP:
588 		/* Reset the address since we might be transitioning from static to DHCP */
589 		memset(&if_handle->ipaddr, 0, sizeof(ip_addr_t));
590 		memset(&if_handle->nmask, 0, sizeof(ip_addr_t));
591 		memset(&if_handle->gw, 0, sizeof(ip_addr_t));
592 		netifapi_netif_set_addr(&if_handle->netif, ip_2_ip4(&if_handle->ipaddr),
593 				ip_2_ip4(&if_handle->nmask), ip_2_ip4(&if_handle->gw));
594 
595 		netif_set_status_callback(&if_handle->netif,
596 					wm_netif_status_callback);
597 		netifapi_netif_set_up(&if_handle->netif);
598 		netifapi_dhcp_start(&if_handle->netif);
599 		break;
600 
601 	default:
602 		break;
603 	}
604 	/* Finally this should send the following event. */
605 	if (if_handle == &g_mlan) {
606 		// static IP up;
607 
608 		/* XXX For DHCP, the above event will only indicate that the
609 		 * DHCP address obtaining process has started. Once the DHCP
610 		 * address has been obtained, another event,
611 		 * WD_EVENT_NET_DHCP_CONFIG, should be sent to the wlcmgr.
612 		 */
613 		 up_iface = 1;
614 
615          // we always set sta netif as the default.
616          sta_set_default_netif();
617 	} else {
618 		// softap IP up, start dhcp server;
619 		dhcp_server_start(net_get_uap_handle());
620 		up_iface = 0;
621 	}
622 
623 	return 0;
624 }
625 
net_get_if_addr(struct wlan_ip_config * addr,void * intrfc_handle)626 int net_get_if_addr(struct wlan_ip_config *addr, void *intrfc_handle)
627 {
628 	const ip_addr_t *tmp;
629 	struct interface *if_handle = (struct interface *)intrfc_handle;
630 
631     if(netif_is_up(&if_handle->netif)) {
632     	addr->ipv4.address = ip_addr_get_ip4_u32(&if_handle->netif.ip_addr);
633     	addr->ipv4.netmask = ip_addr_get_ip4_u32(&if_handle->netif.netmask);
634     	addr->ipv4.gw = ip_addr_get_ip4_u32(&if_handle->netif.gw);
635 
636     	tmp = dns_getserver(0);
637     	addr->ipv4.dns1 = ip_addr_get_ip4_u32(tmp);
638     	tmp = dns_getserver(1);
639     	addr->ipv4.dns2 = ip_addr_get_ip4_u32(tmp);
640     }
641 
642 	return 0;
643 }
644 
net_get_if_macaddr(void * macaddr,void * intrfc_handle)645 int net_get_if_macaddr(void *macaddr, void *intrfc_handle)
646 {
647 	struct interface *if_handle = (struct interface *)intrfc_handle;
648 
649     os_memcpy(macaddr, &if_handle->netif.hwaddr[0], if_handle->netif.hwaddr_len);
650 
651 	return 0;
652 }
653 
654 #ifdef CONFIG_IPV6
net_get_if_ipv6_addr(struct wlan_ip_config * addr,void * intrfc_handle)655 int net_get_if_ipv6_addr(struct wlan_ip_config *addr, void *intrfc_handle)
656 {
657 	struct interface *if_handle = (struct interface *)intrfc_handle;
658 	int i;
659 
660 	for (i = 0; i < MAX_IPV6_ADDRESSES; i++) {
661 		memcpy(&addr->ipv6[i].address,
662 			ip_2_ip6(&(if_handle->netif.ip6_addr[i])), 16);
663 		addr->ipv6[i].addr_state = if_handle->netif.ip6_addr_state[i];
664 	}
665 	/* TODO carry out more processing based on IPv6 fields in netif */
666 	return 0;
667 }
668 
net_get_if_ipv6_pref_addr(struct wlan_ip_config * addr,void * intrfc_handle)669 int net_get_if_ipv6_pref_addr(struct wlan_ip_config *addr, void *intrfc_handle)
670 {
671 	int i, ret = 0;
672 	struct interface *if_handle = (struct interface *)intrfc_handle;
673 
674 	for (i = 0; i < MAX_IPV6_ADDRESSES; i++) {
675 		if (if_handle->netif.ip6_addr_state[i] == IP6_ADDR_PREFERRED) {
676 			memcpy(&addr->ipv6[ret++].address,
677 				&(if_handle->netif.ip6_addr[i]), 16);
678 		}
679 	}
680 	return ret;
681 }
682 #endif /* CONFIG_IPV6 */
683 
net_get_if_ip_addr(uint32_t * ip,void * intrfc_handle)684 int net_get_if_ip_addr(uint32_t *ip, void *intrfc_handle)
685 {
686 	struct interface *if_handle = (struct interface *)intrfc_handle;
687 
688 	*ip = ip_addr_get_ip4_u32(&if_handle->netif.ip_addr);
689 	return 0;
690 }
691 
net_get_if_gw_addr(uint32_t * ip,void * intrfc_handle)692 int net_get_if_gw_addr(uint32_t *ip, void *intrfc_handle)
693 {
694 	struct interface *if_handle = (struct interface *)intrfc_handle;
695 
696 	*ip = ip_addr_get_ip4_u32(&if_handle->netif.gw);
697 
698 	return 0;
699 }
700 
net_get_if_ip_mask(uint32_t * nm,void * intrfc_handle)701 int net_get_if_ip_mask(uint32_t *nm, void *intrfc_handle)
702 {
703 	struct interface *if_handle = (struct interface *)intrfc_handle;
704 
705 	*nm = ip_addr_get_ip4_u32(&if_handle->netif.netmask);
706 	return 0;
707 }
708 
net_configure_dns(struct wlan_ip_config * ip)709 void net_configure_dns(struct wlan_ip_config *ip)
710 {
711 	ip_addr_t tmp;
712 
713 	if (ip->ipv4.addr_type == ADDR_TYPE_STATIC) {
714 
715 		if (ip->ipv4.dns1 == 0){
716 #ifdef CONFIG_LWIP_FAST_DHCP
717 			struct wlan_fast_connect_info fci = {0};
718 			wlan_read_fast_connect_info(&fci);
719 			os_memcpy((char *)&ip->ipv4.dns1, (char *)&fci.dns1, sizeof(fci.dns1));
720 #else
721 			ip->ipv4.dns1 = ip->ipv4.gw;
722 #endif
723 		}
724 		if (ip->ipv4.dns2 == 0)
725 			ip->ipv4.dns2 = ip->ipv4.dns1;
726 
727 		ip_addr_set_ip4_u32(&tmp,ip->ipv4.dns1);
728 		dns_setserver(0, &tmp);
729 		ip_addr_set_ip4_u32(&tmp, ip->ipv4.dns2);
730 		dns_setserver(1, &tmp);
731 	}
732 
733 	/* DNS MAX Retries should be configured in lwip/dns.c to 3/4 */
734 	/* DNS Cache size of about 4 is sufficient */
735 }
736 
net_wlan_initial(void)737 void net_wlan_initial(void)
738 {
739     net_ipv4stack_init();
740 
741 #ifdef CONFIG_IPV6
742     net_ipv6stack_init(&g_mlan.netif);
743 #endif /* CONFIG_IPV6 */
744 }
745 
net_wlan_add_netif(uint8_t * mac)746 int net_wlan_add_netif(uint8_t *mac)
747 {
748 	struct interface *wlan_if = NULL;
749 	netif_if_t netif_if;
750 	void *vif = NULL;
751 	int vifid = 0;
752 	err_t err;
753 
754 	vifid = wifi_netif_mac_to_vifid(mac);
755 	vif = wifi_netif_mac_to_vif(mac);
756 	netif_if = wifi_netif_vif_to_netif_type(vif);
757 	if (netif_if == NETIF_IF_AP) {
758 		wlan_if = &g_uap;
759 	} else if(netif_if == NETIF_IF_STA) {
760 		wlan_if = &g_mlan;
761 	} else {
762 		LWIP_LOGE("unknown netif(%d)\n", netif_if);
763 		return ERR_ARG;
764 	}
765 
766 	ip_addr_set_ip4_u32(&wlan_if->ipaddr, INADDR_ANY);
767 	err = netifapi_netif_add(&wlan_if->netif,
768 		ip_2_ip4(&wlan_if->ipaddr),
769 		ip_2_ip4(&wlan_if->ipaddr),
770 		ip_2_ip4(&wlan_if->ipaddr),
771 		vif,
772 		ethernetif_init,
773 		tcpip_input);
774 	if (err) {
775 		LWIP_LOGE("net_wlan_add_netif failed(%d)\n", err);
776 		return err;
777 	} else {
778 		wifi_netif_set_vif_private_data(vif, &wlan_if->netif);
779 		#if CONFIG_IPV6
780 		if(netif_if == NETIF_IF_STA) {
781 			netif_create_ip6_linklocal_address(&wlan_if->netif, 1);
782 			netif_set_ip6_autoconfig_enabled(&wlan_if->netif, 1);
783 		}
784 		#endif
785 	}
786 
787 	LWIP_LOGI("add vif%d\n", vifid);
788 	return ERR_OK;
789 }
790 
net_wlan_remove_netif(uint8_t * mac)791 int net_wlan_remove_netif(uint8_t *mac)
792 {
793 	struct netif *netif = NULL;
794 	void *vif;
795 	err_t err;
796 	int vifid;
797 
798 	vifid = wifi_netif_mac_to_vifid(mac);
799 	vif = wifi_netif_mac_to_vif(mac);
800 	if(!vif) {
801 		LWIP_LOGE("remove netif, vif%d not found\n", vifid);
802 		return ERR_ARG;
803 	}
804 
805 	netif = (struct netif *)wifi_netif_get_vif_private_data(vif);
806 	if(!netif) {
807 		LWIP_LOGE("remove netif, netif is null\n");
808 		return ERR_ARG;
809 	}
810 
811 	err = netifapi_netif_remove(netif);
812 	if(err != ERR_OK) {
813 		LWIP_LOGE("remove netif, failed(%d)\n", err);
814 		return err;
815 	} else {
816 		netif->state = NULL;
817 	}
818 
819 	LWIP_LOGI("remove vif%d\n", vifid);
820 	return ERR_OK;
821 }
822 #if CONFIG_WIFI6_CODE_STACK
823 bool etharp_tmr_flag = false;
net_begin_send_arp_reply(bool is_send_arp,bool is_allow_send_req)824 void net_begin_send_arp_reply(bool is_send_arp, bool is_allow_send_req)
825 {
826 #if !CONFIG_HARMONY_LWIP
827 	//send reply
828 	if(is_send_arp && !is_allow_send_req) {
829 		etharp_tmr_flag = true;
830 		LWIP_LOGI("send reply %s\n", __func__);
831 	}
832 	//stop send reply
833 	if(!is_send_arp && is_allow_send_req) {
834 		etharp_tmr_flag = false;
835 		LWIP_LOGI("stop send reply %s\n", __func__);
836 		return;
837 	}
838 	etharp_reply();
839 #endif
840 }
841 #endif
842