1 /*
2 Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7 * Redistributions of source code must retain the above copyright
8 	notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 	copyright notice, this list of conditions and the following
11 	disclaimer in the documentation and/or other materials provided
12 	with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 	contributors may be used to endorse or promote products derived
15 	from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*!
30 @file
31 IPACM_Wlan.cpp
32 
33 @brief
34 This file implements the WLAN iface functionality.
35 
36 @Author
37 Skylar Chang
38 
39 */
40 
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/ioctl.h>
44 #include <IPACM_Wlan.h>
45 #include <IPACM_Netlink.h>
46 #include <fcntl.h>
47 #include <sys/inotify.h>
48 #include <IPACM_Wan.h>
49 #include <IPACM_Lan.h>
50 #include <IPACM_IfaceManager.h>
51 #include <IPACM_ConntrackListener.h>
52 #ifdef FEATURE_IPACM_HAL
53 #include "IPACM_OffloadManager.h"
54 #endif
55 
56 /* static member to store the number of total wifi clients within all APs*/
57 int IPACM_Wlan::total_num_wifi_clients = 0;
58 
59 int IPACM_Wlan::num_wlan_ap_iface = 0;
60 
IPACM_Wlan(int iface_index)61 IPACM_Wlan::IPACM_Wlan(int iface_index) : IPACM_Lan(iface_index)
62 {
63 #define WLAN_AMPDU_DEFAULT_FILTER_RULES 3
64 
65 	wlan_ap_index = IPACM_Wlan::num_wlan_ap_iface;
66 	if(wlan_ap_index < 0 || wlan_ap_index > 1)
67 	{
68 		IPACMERR("Wlan_ap_index is not correct: %d, not creating instance.\n", wlan_ap_index);
69 		if (tx_prop != NULL)
70 		{
71 			free(tx_prop);
72 		}
73 		if (rx_prop != NULL)
74 		{
75 			free(rx_prop);
76 		}
77 		if (iface_query != NULL)
78 		{
79 			free(iface_query);
80 		}
81 		delete this;
82 		return;
83 	}
84 
85 	num_wifi_client = 0;
86 	header_name_count = 0;
87 	wlan_client = NULL;
88 	wlan_client_len = 0;
89 
90 	if(iface_query != NULL)
91 	{
92 		wlan_client_len = (sizeof(ipa_wlan_client)) + (iface_query->num_tx_props * sizeof(wlan_client_rt_hdl));
93 		wlan_client = (ipa_wlan_client *)calloc(IPA_MAX_NUM_WIFI_CLIENTS, wlan_client_len);
94 		if (wlan_client == NULL)
95 		{
96 			IPACMERR("unable to allocate memory\n");
97 			return;
98 		}
99 		IPACMDBG_H("index:%d constructor: Tx properties:%d\n", iface_index, iface_query->num_tx_props);
100 	}
101 	Nat_App = NatApp::GetInstance();
102 	if (Nat_App == NULL)
103 	{
104 		IPACMERR("unable to get Nat App instance \n");
105 		return;
106 	}
107 
108 	IPACM_Wlan::num_wlan_ap_iface++;
109 	IPACMDBG_H("Now the number of wlan AP iface is %d\n", IPACM_Wlan::num_wlan_ap_iface);
110 
111 	m_is_guest_ap = false;
112 	if (IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].wlan_mode == INTERNET)
113 	{
114 		m_is_guest_ap = true;
115 	}
116 	IPACMDBG_H("%s: guest ap enable: %d \n",
117 		IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].iface_name, m_is_guest_ap);
118 
119 #ifdef FEATURE_IPA_ANDROID
120 	/* set the IPA-client pipe enum */
121 	if(ipa_if_cate == WLAN_IF)
122 	{
123 #ifdef FEATURE_IPACM_HAL
124 		handle_tethering_client(false, IPACM_CLIENT_MAX);
125 #else
126 		handle_tethering_client(false, IPACM_CLIENT_WLAN);
127 #endif
128 	}
129 #endif
130 	return;
131 }
132 
133 
~IPACM_Wlan()134 IPACM_Wlan::~IPACM_Wlan()
135 {
136 	IPACM_EvtDispatcher::deregistr(this);
137 	IPACM_IfaceManager::deregistr(this);
138 	IPACM_Wlan::num_wlan_ap_iface--;
139 	return;
140 }
141 
event_callback(ipa_cm_event_id event,void * param)142 void IPACM_Wlan::event_callback(ipa_cm_event_id event, void *param)
143 {
144 	if(is_active == false && event != IPA_LAN_DELETE_SELF)
145 	{
146 		IPACMDBG_H("The interface is no longer active, return.\n");
147 		return;
148 	}
149 
150 	int ipa_interface_index;
151 	int wlan_index;
152 	ipacm_ext_prop* ext_prop;
153 	ipacm_event_iface_up_tehter* data_wan_tether;
154 #ifdef FEATURE_IPACM_HAL
155 	IPACM_OffloadManager* OffloadMng;
156 #endif
157 
158 	switch (event)
159 	{
160 
161 	case IPA_WLAN_LINK_DOWN_EVENT:
162 		{
163 			ipacm_event_data_fid *data = (ipacm_event_data_fid *)param;
164 			ipa_interface_index = iface_ipa_index_query(data->if_index);
165 			if (ipa_interface_index == ipa_if_num)
166 			{
167 				IPACMDBG_H("Received IPA_WLAN_LINK_DOWN_EVENT\n");
168 				handle_down_evt();
169 				/* reset the AP-iface category to unknown */
170 				IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].if_cat = UNKNOWN_IF;
171 				IPACM_Iface::ipacmcfg->DelNatIfaces(dev_name); // delete NAT-iface
172 				IPACM_Wlan::total_num_wifi_clients = (IPACM_Wlan::total_num_wifi_clients) - \
173                                                                      (num_wifi_client);
174 				return;
175 			}
176 		}
177 		break;
178 
179 	case IPA_PRIVATE_SUBNET_CHANGE_EVENT:
180 		{
181 			ipacm_event_data_fid *data = (ipacm_event_data_fid *)param;
182 			/* internel event: data->if_index is ipa_if_index */
183 			if (data->if_index == ipa_if_num)
184 			{
185 				IPACMDBG_H("Received IPA_PRIVATE_SUBNET_CHANGE_EVENT from itself posting, ignore\n");
186 				return;
187 			}
188 			else
189 			{
190 				IPACMDBG_H("Received IPA_PRIVATE_SUBNET_CHANGE_EVENT from other LAN iface \n");
191 #ifdef FEATURE_IPA_ANDROID
192 				handle_private_subnet_android(IPA_IP_v4);
193 #endif
194 				IPACMDBG_H(" delete old private subnet rules, use new sets \n");
195 				return;
196 			}
197 		}
198 		break;
199 
200 	case IPA_LAN_DELETE_SELF:
201 	{
202 		ipacm_event_data_fid *data = (ipacm_event_data_fid *)param;
203 		if(data->if_index == ipa_if_num)
204 		{
205 			IPACMDBG_H("Now the number of wlan AP iface is %d\n", IPACM_Wlan::num_wlan_ap_iface);
206 
207 			IPACMDBG_H("Received IPA_LAN_DELETE_SELF event.\n");
208 			IPACMDBG_H("ipa_WLAN (%s):ipa_index (%d) instance close \n", IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].iface_name, ipa_if_num);
209 #ifdef FEATURE_ETH_BRIDGE_LE
210 			if(rx_prop != NULL)
211 			{
212 				free(rx_prop);
213 			}
214 			if(tx_prop != NULL)
215 			{
216 				free(tx_prop);
217 			}
218 			if(iface_query != NULL)
219 			{
220 				free(iface_query);
221 			}
222 #endif
223 			delete this;
224 		}
225 		break;
226 	}
227 
228 	case IPA_ADDR_ADD_EVENT:
229 		{
230 			ipacm_event_data_addr *data = (ipacm_event_data_addr *)param;
231 			ipa_interface_index = iface_ipa_index_query(data->if_index);
232 
233 			if ( (data->iptype == IPA_IP_v4 && data->ipv4_addr == 0) ||
234 					 (data->iptype == IPA_IP_v6 &&
235 						data->ipv6_addr[0] == 0 && data->ipv6_addr[1] == 0 &&
236 					  data->ipv6_addr[2] == 0 && data->ipv6_addr[3] == 0) )
237 			{
238 				IPACMDBG_H("Invalid address, ignore IPA_ADDR_ADD_EVENT event\n");
239 				return;
240 			}
241 
242 			if (ipa_interface_index == ipa_if_num)
243 			{
244 				/* check v4 not setup before, v6 can have 2 iface ip */
245 				if( ((data->iptype != ip_type) && (ip_type != IPA_IP_MAX))
246 				    || ((data->iptype==IPA_IP_v6) && (num_dft_rt_v6!=MAX_DEFAULT_v6_ROUTE_RULES)))
247 				{
248 					IPACMDBG_H("Got IPA_ADDR_ADD_EVENT ip-family:%d, v6 num %d: \n",data->iptype,num_dft_rt_v6);
249 					/* Post event to NAT */
250 					if (data->iptype == IPA_IP_v4)
251 					{
252 						ipacm_cmd_q_data evt_data;
253 						ipacm_event_iface_up *info;
254 
255 						info = (ipacm_event_iface_up *)
256 							 malloc(sizeof(ipacm_event_iface_up));
257 						if (info == NULL)
258 						{
259 							IPACMERR("Unable to allocate memory\n");
260 							return;
261 						}
262 
263 						memcpy(info->ifname, dev_name, IF_NAME_LEN);
264 						info->ipv4_addr = data->ipv4_addr;
265 						info->addr_mask = IPACM_Iface::ipacmcfg->private_subnet_table[0].subnet_mask;
266 
267 						evt_data.event = IPA_HANDLE_WLAN_UP;
268 						evt_data.evt_data = (void *)info;
269 
270 						/* Insert IPA_HANDLE_WLAN_UP to command queue */
271 						IPACMDBG_H("posting IPA_HANDLE_WLAN_UP for IPv4 with below information\n");
272 						IPACMDBG_H("IPv4 address:0x%x, IPv4 address mask:0x%x\n",
273 										 info->ipv4_addr, info->addr_mask);
274 						IPACM_EvtDispatcher::PostEvt(&evt_data);
275 
276 						/* Query wlan-clients */
277 						ipa_query_wlan_client();
278 					}
279 
280 					if(handle_addr_evt(data) == IPACM_FAILURE)
281 					{
282 						return;
283 					}
284 
285 #ifdef FEATURE_IPA_ANDROID
286 					add_dummy_private_subnet_flt_rule(data->iptype);
287 					handle_private_subnet_android(data->iptype);
288 #else
289 					handle_private_subnet(data->iptype);
290 #endif
291 
292 #ifndef FEATURE_IPACM_HAL
293 					if (IPACM_Wan::isWanUP(ipa_if_num))
294 					{
295 						if(data->iptype == IPA_IP_v4 || data->iptype == IPA_IP_MAX)
296 						{
297 							if(IPACM_Wan::backhaul_is_sta_mode == false)
298 							{
299 								ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v4);
300 								IPACM_Lan::handle_wan_up_ex(ext_prop, IPA_IP_v4,
301 												IPACM_Wan::getXlat_Mux_Id());
302 							}
303 							else
304 							{
305 								IPACM_Lan::handle_wan_up(IPA_IP_v4);
306 							}
307 						}
308 						IPACMDBG_H("Finished checking wan_up\n");
309 					} else {
310 						IPACMDBG_H("Wan_V4 haven't up yet \n");
311 					}
312 
313 					if(IPACM_Wan::isWanUP_V6(ipa_if_num))
314 					{
315 						if((data->iptype == IPA_IP_v6 || data->iptype == IPA_IP_MAX) && num_dft_rt_v6 == 1)
316 						{
317 							memcpy(ipv6_prefix, IPACM_Wan::backhaul_ipv6_prefix, sizeof(ipv6_prefix));
318 							install_ipv6_prefix_flt_rule(IPACM_Wan::backhaul_ipv6_prefix);
319 
320 							if(IPACM_Wan::backhaul_is_sta_mode == false)
321 							{
322 								ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v6);
323 								IPACM_Lan::handle_wan_up_ex(ext_prop, IPA_IP_v6, 0);
324 							}
325 							else
326 							{
327 								IPACM_Lan::handle_wan_up(IPA_IP_v6);
328 							}
329 						}
330 						IPACMDBG_H("Finished checking wan_up_v6\n");
331 					} else {
332 						IPACMDBG_H("Wan_V6 haven't up yet \n");
333 					}
334 #else
335 					/* check if Upstream was set before */
336 					if (IPACM_Wan::isWanUP(ipa_if_num))
337 					{
338 						IPACMDBG_H("Upstream was set previously for ipv4, change is_upstream_set flag\n");
339 						is_upstream_set[IPA_IP_v4] = true;
340 					}
341 					if (IPACM_Wan::isWanUP_V6(ipa_if_num))
342 					{
343 						IPACMDBG_H("Upstream was set previously for ipv6, change is_upstream_set flag\n");
344 						is_upstream_set[IPA_IP_v6] = true;
345 					}
346 #endif
347 					/* checking if SW-RT_enable */
348 					if (IPACM_Iface::ipacmcfg->ipa_sw_rt_enable == true)
349 					{
350 						/* handle software routing enable event*/
351 						IPACMDBG_H("IPA_SW_ROUTING_ENABLE for iface: %s \n",IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].iface_name);
352 						handle_software_routing_enable();
353 					}
354 				}
355 			}
356 		}
357 		break;
358 #ifdef FEATURE_IPA_ANDROID
359 	case IPA_HANDLE_WAN_UP_TETHER:
360 		IPACMDBG_H("Received IPA_HANDLE_WAN_UP_TETHER event\n");
361 
362 		data_wan_tether = (ipacm_event_iface_up_tehter*)param;
363 		if(data_wan_tether == NULL)
364 		{
365 			IPACMERR("No event data is found.\n");
366 			return;
367 		}
368 		IPACMDBG_H("Backhaul is sta mode?%d, if_index_tether:%d tether_if_name:%s\n", data_wan_tether->is_sta,
369 					data_wan_tether->if_index_tether,
370 					IPACM_Iface::ipacmcfg->iface_table[data_wan_tether->if_index_tether].iface_name);
371 #ifndef FEATURE_IPACM_HAL
372 		if (data_wan_tether->if_index_tether != ipa_if_num)
373 		{
374 			IPACMERR("IPA_HANDLE_WAN_UP_TETHER tether_if(%d), not valid (%d) ignore\n", data_wan_tether->if_index_tether, ipa_if_num);
375 			return;
376 		}
377 #endif
378 		if(ip_type == IPA_IP_v4 || ip_type == IPA_IP_MAX)
379 		{
380 #ifdef FEATURE_IPACM_HAL
381 			if(is_upstream_set[IPA_IP_v4] == false)
382 			{
383 				IPACMDBG_H("Add upstream for IPv4.\n");
384 				is_upstream_set[IPA_IP_v4] = true;
385 				if(is_downstream_set[IPA_IP_v4] == true)
386 				{
387 					IPACMDBG_H("Downstream was set before, adding UL rules.\n");
388 					if(data_wan_tether->is_sta == false)
389 					{
390 						ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v4);
391 						handle_wan_up_ex(ext_prop, IPA_IP_v4,
392 							IPACM_Wan::getXlat_Mux_Id());
393 					} else {
394 						handle_wan_up(IPA_IP_v4);
395 					}
396 				}
397 			}
398 #else
399 			if(data_wan_tether->is_sta == false)
400 			{
401 				ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v4);
402 				handle_wan_up_ex(ext_prop, IPA_IP_v4, 0);
403 			} else {
404 				handle_wan_up(IPA_IP_v4);
405 			}
406 #endif
407 		}
408 		break;
409 
410 	case IPA_HANDLE_WAN_UP_V6_TETHER:
411 		IPACMDBG_H("Received IPA_HANDLE_WAN_UP_V6_TETHER event\n");
412 
413 		data_wan_tether = (ipacm_event_iface_up_tehter*)param;
414 		if(data_wan_tether == NULL)
415 		{
416 			IPACMERR("No event data is found.\n");
417 			return;
418 		}
419 		IPACMDBG_H("Backhaul is sta mode?%d, if_index_tether:%d tether_if_name:%s\n", data_wan_tether->is_sta,
420 					data_wan_tether->if_index_tether,
421 					IPACM_Iface::ipacmcfg->iface_table[data_wan_tether->if_index_tether].iface_name);
422 #ifndef FEATURE_IPACM_HAL
423 		if (data_wan_tether->if_index_tether != ipa_if_num)
424 		{
425 			IPACMERR("IPA_HANDLE_WAN_UP_V6_TETHER tether_if(%d), not valid (%d) ignore\n", data_wan_tether->if_index_tether, ipa_if_num);
426 			return;
427 		}
428 #endif
429 		if(ip_type == IPA_IP_v6 || ip_type == IPA_IP_MAX)
430 		{
431 #ifdef FEATURE_IPACM_HAL
432 			if(is_upstream_set[IPA_IP_v6] == false)
433 			{
434 				IPACMDBG_H("Add upstream for IPv6.\n");
435 				is_upstream_set[IPA_IP_v6] = true;
436 
437 				if(is_downstream_set[IPA_IP_v6] == true)
438 				{
439 					IPACMDBG_H("Downstream was set before, adding UL rules.\n");
440 					memcpy(ipv6_prefix, data_wan_tether->ipv6_prefix, sizeof(ipv6_prefix));
441 					install_ipv6_prefix_flt_rule(data_wan_tether->ipv6_prefix);
442 					if(data_wan_tether->is_sta == false)
443 					{
444 						ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v6);
445 						handle_wan_up_ex(ext_prop, IPA_IP_v6, 0);
446 					}
447 					else
448 					{
449 						handle_wan_up(IPA_IP_v6);
450 					}
451 				}
452 			}
453 #else
454 			if(data_wan_tether->is_sta == false)
455 			{
456 				ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v6);
457 				handle_wan_up_ex(ext_prop, IPA_IP_v6, 0);
458 			}
459 			else
460 			{
461 				handle_wan_up(IPA_IP_v6);
462 			}
463 #endif
464 		}
465 		break;
466 
467 	case IPA_HANDLE_WAN_DOWN_TETHER:
468 		IPACMDBG_H("Received IPA_HANDLE_WAN_DOWN_TETHER event\n");
469 		data_wan_tether = (ipacm_event_iface_up_tehter*)param;
470 		if(data_wan_tether == NULL)
471 		{
472 			IPACMERR("No event data is found.\n");
473 			return;
474 		}
475 		if(rx_prop == NULL)
476 		{
477 			IPACMERR("No rx prop.\n");
478 			return;
479 		}
480 		IPACMDBG_H("Backhaul is sta mode?%d, if_index_tether:%d tether_if_name:%s\n", data_wan_tether->is_sta,
481 					data_wan_tether->if_index_tether,
482 					IPACM_Iface::ipacmcfg->iface_table[data_wan_tether->if_index_tether].iface_name);
483 #ifndef FEATURE_IPACM_HAL
484 		if (data_wan_tether->if_index_tether != ipa_if_num)
485 		{
486 			IPACMERR("IPA_HANDLE_WAN_DOWN_TETHER tether_if(%d), not valid (%d) ignore\n", data_wan_tether->if_index_tether, ipa_if_num);
487 			return;
488 		}
489 #endif
490 		if(ip_type == IPA_IP_v4 || ip_type == IPA_IP_MAX)
491 		{
492 #ifdef FEATURE_IPACM_HAL
493 			if(is_upstream_set[IPA_IP_v4] == true)
494 			{
495 				IPACMDBG_H("Del upstream for IPv4.\n");
496 				is_upstream_set[IPA_IP_v4] = false;
497 				if(is_downstream_set[IPA_IP_v4] == true)
498 				{
499 					IPACMDBG_H("Downstream was set before, deleting UL rules.\n");
500 					handle_wan_down(data_wan_tether->is_sta);
501 				}
502 			}
503 #else
504 			handle_wan_down(data_wan_tether->is_sta);
505 #endif
506 		}
507 		break;
508 
509 	case IPA_HANDLE_WAN_DOWN_V6_TETHER:
510 		IPACMDBG_H("Received IPA_HANDLE_WAN_DOWN_V6_TETHER event\n");
511 		data_wan_tether = (ipacm_event_iface_up_tehter*)param;
512 		if(data_wan_tether == NULL)
513 		{
514 			IPACMERR("No event data is found.\n");
515 			return;
516 		}
517 		if(rx_prop == NULL)
518 		{
519 			IPACMERR("No rx prop.\n");
520 			return;
521 		}
522 		IPACMDBG_H("Backhaul is sta mode?%d, if_index_tether:%d tether_if_name:%s\n", data_wan_tether->is_sta,
523 					data_wan_tether->if_index_tether,
524 					IPACM_Iface::ipacmcfg->iface_table[data_wan_tether->if_index_tether].iface_name);
525 #ifndef FEATURE_IPACM_HAL
526 		if (data_wan_tether->if_index_tether != ipa_if_num)
527 		{
528 			IPACMERR("IPA_HANDLE_WAN_DOWN_V6_TETHER tether_if(%d), not valid (%d) ignore\n", data_wan_tether->if_index_tether, ipa_if_num);
529 			return;
530 		}
531 #endif
532 		if(ip_type == IPA_IP_v6 || ip_type == IPA_IP_MAX)
533 		{
534 #ifdef FEATURE_IPACM_HAL
535 			if(is_upstream_set[IPA_IP_v6] == true)
536 			{
537 				IPACMDBG_H("Del upstream for IPv6.\n");
538 				is_upstream_set[IPA_IP_v6] = false;
539 				if(is_downstream_set[IPA_IP_v6] == true)
540 				{
541 					IPACMDBG_H("Downstream was set before, deleting UL rules.\n");
542 					/* reset usb-client ipv6 rt-rules */
543 					handle_wlan_client_reset_rt(IPA_IP_v6);
544 					handle_wan_down_v6(data_wan_tether->is_sta);
545 				}
546 			}
547 #else
548 			/* reset usb-client ipv6 rt-rules */
549 			handle_wlan_client_reset_rt(IPA_IP_v6);
550 			handle_wan_down_v6(data_wan_tether->is_sta);
551 #endif
552 		}
553 		break;
554 
555 	case IPA_DOWNSTREAM_ADD:
556 	{
557 		ipacm_event_ipahal_stream *data = (ipacm_event_ipahal_stream *)param;
558 		ipa_interface_index = iface_ipa_index_query(data->if_index);
559 		if(ipa_interface_index == ipa_if_num)
560 		{
561 			IPACMDBG_H("Received IPA_DOWNSTREAM_ADD event.\n");
562 			if(is_downstream_set[data->prefix.iptype] == false)
563 			{
564 				IPACMDBG_H("Add downstream for IP iptype %d.\n", data->prefix.iptype);
565 				is_downstream_set[data->prefix.iptype] = true;
566 				memcpy(&prefix[data->prefix.iptype], &data->prefix,
567 					sizeof(prefix[data->prefix.iptype]));
568 
569 				if (is_upstream_set[data->prefix.iptype] == true)
570 				{
571 					IPACMDBG_H("Upstream was set before, adding modem UL rules.\n");
572 					if(ip_type == IPA_IP_MAX || ip_type == data->prefix.iptype)
573 					{
574 						if (data->prefix.iptype == IPA_IP_v6) /* ipv6 only */
575 						{
576 							/* Only offload clients has same prefix as Android gave */
577 							ipv6_prefix[0] = data->prefix.v6Addr[0];
578 							ipv6_prefix[1] = data->prefix.v6Addr[1];
579 							IPACMDBG_H("ipv6_prefix0x%x:%x\n", ipv6_prefix[0], ipv6_prefix[1]);
580 							install_ipv6_prefix_flt_rule(ipv6_prefix);
581 						}
582 
583 						if (IPACM_Wan::backhaul_is_sta_mode == false) /* LTE */
584 						{
585 							ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(data->prefix.iptype);
586 							if (data->prefix.iptype == IPA_IP_v4)
587 							{
588 								handle_wan_up_ex(ext_prop, data->prefix.iptype,
589 									IPACM_Wan::getXlat_Mux_Id());
590 							}
591 							else {
592 								handle_wan_up_ex(ext_prop, data->prefix.iptype, 0);
593 							}
594 						} else {
595 							handle_wan_up(data->prefix.iptype); /* STA */
596 						}
597 					}
598 				}
599 			}
600 		}
601 		break;
602 	}
603 
604 	case IPA_DOWNSTREAM_DEL:
605 	{
606 		ipacm_event_ipahal_stream *data = (ipacm_event_ipahal_stream *)param;
607 		ipa_interface_index = iface_ipa_index_query(data->if_index);
608 		if(ipa_interface_index == ipa_if_num)
609 		{
610 			IPACMDBG_H("Received IPA_DOWNSTREAM_DEL event.\n");
611 			if(is_downstream_set[data->prefix.iptype] == true)
612 			{
613 				IPACMDBG_H("Del downstream for IP iptype %d.\n", data->prefix.iptype);
614 				is_downstream_set[data->prefix.iptype] = false;
615 
616 				if(is_upstream_set[data->prefix.iptype] == true)
617 				{
618 					IPACMDBG_H("Upstream was set before, deleting UL rules.\n");
619 					if (data->prefix.iptype == IPA_IP_v4)
620 					{
621 						handle_wan_down(IPACM_Wan::backhaul_is_sta_mode); /* LTE STA */
622 					} else {
623 						handle_wlan_client_reset_rt(IPA_IP_v6);
624 						handle_wan_down_v6(IPACM_Wan::backhaul_is_sta_mode); /* LTE STA */
625 					}
626 				}
627 			}
628 		}
629 		break;
630 	}
631 #else
632 	case IPA_HANDLE_WAN_UP:
633 		IPACMDBG_H("Received IPA_HANDLE_WAN_UP event\n");
634 
635 		ipacm_event_iface_up* data_wan = (ipacm_event_iface_up*)param;
636 		if(data_wan == NULL)
637 		{
638 			IPACMERR("No event data is found.\n");
639 			return;
640 		}
641 		IPACMDBG_H("Backhaul is sta mode?%d\n", data_wan->is_sta);
642 		if(ip_type == IPA_IP_v4 || ip_type == IPA_IP_MAX)
643 		{
644 			if(data_wan->is_sta == false)
645 			{
646 				ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v4);
647 				IPACM_Lan::handle_wan_up_ex(ext_prop, IPA_IP_v4, data_wan->xlat_mux_id);
648 			}
649 			else
650 			{
651 				IPACM_Lan::handle_wan_up(IPA_IP_v4);
652 			}
653 		}
654 		break;
655 
656 	case IPA_HANDLE_WAN_UP_V6:
657 		IPACMDBG_H("Received IPA_HANDLE_WAN_UP_V6 event\n");
658 
659 		data_wan = (ipacm_event_iface_up*)param;
660 		if(data_wan == NULL)
661 		{
662 			IPACMERR("No event data is found.\n");
663 			return;
664 		}
665 		IPACMDBG_H("Backhaul is sta mode?%d\n", data_wan->is_sta);
666 		if(ip_type == IPA_IP_v6 || ip_type == IPA_IP_MAX)
667 		{
668 			memcpy(ipv6_prefix, data_wan->ipv6_prefix, sizeof(ipv6_prefix));
669 			install_ipv6_prefix_flt_rule(data_wan->ipv6_prefix);
670 
671 			if(data_wan->is_sta == false)
672 			{
673 				ext_prop = IPACM_Iface::ipacmcfg->GetExtProp(IPA_IP_v6);
674 				IPACM_Lan::handle_wan_up_ex(ext_prop, IPA_IP_v6, 0);
675 			}
676 			else
677 			{
678 				IPACM_Lan::handle_wan_up(IPA_IP_v6);
679 			}
680 		}
681 		break;
682 
683 	case IPA_HANDLE_WAN_DOWN:
684 		IPACMDBG_H("Received IPA_HANDLE_WAN_DOWN event\n");
685 		data_wan = (ipacm_event_iface_up*)param;
686 		if(data_wan == NULL)
687 		{
688 			IPACMERR("No event data is found.\n");
689 			return;
690 		}
691 		IPACMDBG_H("Backhaul is sta mode?%d\n", data_wan->is_sta);
692 		if (rx_prop != NULL)
693 		{
694 			if(ip_type == IPA_IP_v4 || ip_type == IPA_IP_MAX)
695 			{
696 				handle_wan_down(data_wan->is_sta);
697 			}
698 		}
699 		break;
700 
701 	case IPA_HANDLE_WAN_DOWN_V6:
702 		IPACMDBG_H("Received IPA_HANDLE_WAN_DOWN_V6 event\n");
703 		data_wan = (ipacm_event_iface_up*)param;
704 		if(data_wan == NULL)
705 		{
706 			IPACMERR("No event data is found.\n");
707 			return;
708 		}
709 		/* clean up v6 RT rules*/
710 		IPACMDBG_H("Received IPA_WAN_V6_DOWN in WLAN-instance and need clean up client IPv6 address \n");
711 		/* reset wifi-client ipv6 rt-rules */
712 		handle_wlan_client_reset_rt(IPA_IP_v6);
713 		IPACMDBG_H("Backhaul is sta mode ? %d\n", data_wan->is_sta);
714 		if (rx_prop != NULL)
715 		{
716 			if(ip_type == IPA_IP_v6 || ip_type == IPA_IP_MAX)
717 			{
718 				handle_wan_down_v6(data_wan->is_sta);
719 			}
720 		}
721 		break;
722 #endif
723 
724 	case IPA_WLAN_CLIENT_ADD_EVENT_EX:
725 		{
726 			ipacm_event_data_wlan_ex *data = (ipacm_event_data_wlan_ex *)param;
727 			ipa_interface_index = iface_ipa_index_query(data->if_index);
728 			if (ipa_interface_index == ipa_if_num)
729 			{
730 				int i;
731 				for(i=0; i<data->num_of_attribs; i++)
732 				{
733 					if(data->attribs[i].attrib_type == WLAN_HDR_ATTRIB_MAC_ADDR)
734 					{
735 						eth_bridge_post_event(IPA_ETH_BRIDGE_CLIENT_ADD, IPA_IP_MAX, data->attribs[i].u.mac_addr, NULL, NULL);
736 						break;
737 					}
738 				}
739 				IPACMDBG_H("Received IPA_WLAN_CLIENT_ADD_EVENT\n");
740 				handle_wlan_client_init_ex(data);
741 			}
742 		}
743 		break;
744 
745 	case IPA_WLAN_CLIENT_DEL_EVENT:
746 		{
747 			ipacm_event_data_mac *data = (ipacm_event_data_mac *)param;
748 			ipa_interface_index = iface_ipa_index_query(data->if_index);
749 			if (ipa_interface_index == ipa_if_num)
750 			{
751 				IPACMDBG_H("Received IPA_WLAN_CLIENT_DEL_EVENT\n");
752 				eth_bridge_post_event(IPA_ETH_BRIDGE_CLIENT_DEL, IPA_IP_MAX, data->mac_addr, NULL, NULL);
753 				handle_wlan_client_down_evt(data->mac_addr);
754 			}
755 		}
756 		break;
757 
758 	case IPA_WLAN_CLIENT_POWER_SAVE_EVENT:
759 		{
760 			ipacm_event_data_mac *data = (ipacm_event_data_mac *)param;
761 			ipa_interface_index = iface_ipa_index_query(data->if_index);
762 			if (ipa_interface_index == ipa_if_num)
763 			{
764 				IPACMDBG_H("Received IPA_WLAN_CLIENT_POWER_SAVE_EVENT\n");
765 				handle_wlan_client_pwrsave(data->mac_addr);
766 			}
767 		}
768 		break;
769 
770 	case IPA_WLAN_CLIENT_RECOVER_EVENT:
771 		{
772 			ipacm_event_data_mac *data = (ipacm_event_data_mac *)param;
773 			ipa_interface_index = iface_ipa_index_query(data->if_index);
774 			if (ipa_interface_index == ipa_if_num)
775 			{
776 				IPACMDBG_H("Received IPA_WLAN_CLIENT_RECOVER_EVENT\n");
777 
778 				wlan_index = get_wlan_client_index(data->mac_addr);
779 				if ((wlan_index != IPACM_INVALID_INDEX) &&
780 						(get_client_memptr(wlan_client, wlan_index)->power_save_set == true))
781 				{
782 
783 					IPACMDBG_H("change wlan client out of  power safe mode \n");
784 					get_client_memptr(wlan_client, wlan_index)->power_save_set = false;
785 
786 					/* First add route rules and then nat rules */
787 					if(get_client_memptr(wlan_client, wlan_index)->ipv4_set == true) /* for ipv4 */
788 					{
789 						     IPACMDBG_H("recover client index(%d):ipv4 address: 0x%x\n",
790 										 wlan_index,
791 										 get_client_memptr(wlan_client, wlan_index)->v4_addr);
792 
793 						IPACMDBG_H("Adding Route Rules\n");
794 						handle_wlan_client_route_rule(data->mac_addr, IPA_IP_v4);
795 						IPACMDBG_H("Adding Nat Rules\n");
796 						Nat_App->ResetPwrSaveIf(get_client_memptr(wlan_client, wlan_index)->v4_addr);
797 					}
798 
799 					if(get_client_memptr(wlan_client, wlan_index)->ipv6_set != 0) /* for ipv6 */
800 					{
801 						handle_wlan_client_route_rule(data->mac_addr, IPA_IP_v6);
802 					}
803 				}
804 			}
805 		}
806 		break;
807 
808 	case IPA_NEIGH_CLIENT_IP_ADDR_ADD_EVENT:
809 		{
810 			ipacm_event_data_all *data = (ipacm_event_data_all *)param;
811 			ipa_interface_index = iface_ipa_index_query(data->if_index);
812 			if (ipa_interface_index == ipa_if_num)
813 			{
814 				IPACMDBG_H("Received IPA_NEIGH_CLIENT_IP_ADDR_ADD_EVENT\n");
815 				if (handle_wlan_client_ipaddr(data) == IPACM_FAILURE)
816 				{
817 					return;
818 				}
819 
820 				handle_wlan_client_route_rule(data->mac_addr, data->iptype);
821 				if (data->iptype == IPA_IP_v4)
822 				{
823 					/* Add NAT rules after ipv4 RT rules are set */
824 					CtList->HandleNeighIpAddrAddEvt(data);
825 					//Nat_App->ResetPwrSaveIf(data->ipv4_addr);
826 				}
827 			}
828 		}
829 		break;
830 
831 		/* handle software routing enable event, iface will update softwarerouting_act to true*/
832 	case IPA_SW_ROUTING_ENABLE:
833 		IPACMDBG_H("Received IPA_SW_ROUTING_ENABLE\n");
834 		IPACM_Iface::handle_software_routing_enable();
835 		break;
836 
837 		/* handle software routing disable event, iface will update softwarerouting_act to false*/
838 	case IPA_SW_ROUTING_DISABLE:
839 		IPACMDBG_H("Received IPA_SW_ROUTING_DISABLE\n");
840 		IPACM_Iface::handle_software_routing_disable();
841 		break;
842 
843 	case IPA_WLAN_SWITCH_TO_SCC:
844 		IPACMDBG_H("Received IPA_WLAN_SWITCH_TO_SCC\n");
845 		if(ip_type == IPA_IP_MAX)
846 		{
847 			handle_SCC_MCC_switch(IPA_IP_v4);
848 			handle_SCC_MCC_switch(IPA_IP_v6);
849 		}
850 		else
851 		{
852 			handle_SCC_MCC_switch(ip_type);
853 		}
854 		eth_bridge_post_event(IPA_ETH_BRIDGE_WLAN_SCC_MCC_SWITCH, IPA_IP_MAX, NULL, NULL, NULL);
855 		break;
856 
857 	case IPA_WLAN_SWITCH_TO_MCC:
858 		IPACMDBG_H("Received IPA_WLAN_SWITCH_TO_MCC\n");
859 		if(ip_type == IPA_IP_MAX)
860 		{
861 			handle_SCC_MCC_switch(IPA_IP_v4);
862 			handle_SCC_MCC_switch(IPA_IP_v6);
863 		}
864 		else
865 		{
866 			handle_SCC_MCC_switch(ip_type);
867 		}
868 		eth_bridge_post_event(IPA_ETH_BRIDGE_WLAN_SCC_MCC_SWITCH, IPA_IP_MAX, NULL, NULL, NULL);
869 		break;
870 
871 	case IPA_CRADLE_WAN_MODE_SWITCH:
872 	{
873 		IPACMDBG_H("Received IPA_CRADLE_WAN_MODE_SWITCH event.\n");
874 		ipacm_event_cradle_wan_mode* wan_mode = (ipacm_event_cradle_wan_mode*)param;
875 		if(wan_mode == NULL)
876 		{
877 			IPACMERR("Event data is empty.\n");
878 			return;
879 		}
880 
881 		if(wan_mode->cradle_wan_mode == BRIDGE)
882 		{
883 			handle_cradle_wan_mode_switch(true);
884 		}
885 		else
886 		{
887 			handle_cradle_wan_mode_switch(false);
888 		}
889 	}
890 	break;
891 	case IPA_CFG_CHANGE_EVENT:
892 	{
893 		IPACMDBG_H("Received IPA_CFG_CHANGE_EVENT event for %s with new wlan-mode: %s old wlan-mode: %s\n",
894 				IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].iface_name,
895 				(IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].wlan_mode == 0) ? "full" : "internet",
896 				(m_is_guest_ap == true) ? "internet" : "full");
897 		/* Add Natting iface to IPACM_Config if there is  Rx/Tx property */
898 		if (rx_prop != NULL || tx_prop != NULL)
899 		{
900 			IPACMDBG_H(" Has rx/tx properties registered for iface %s, add for NATTING \n", dev_name);
901 			IPACM_Iface::ipacmcfg->AddNatIfaces(dev_name, IPA_IP_MAX);
902 		}
903 
904 		if (m_is_guest_ap == true && (IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].wlan_mode == FULL))
905 		{
906 			m_is_guest_ap = false;
907 			IPACMDBG_H("wlan mode is switched to full access mode. \n");
908 			eth_bridge_handle_wlan_mode_switch();
909 		}
910 		else if (m_is_guest_ap == false && (IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].wlan_mode == INTERNET))
911 		{
912 			m_is_guest_ap = true;
913 			IPACMDBG_H("wlan mode is switched to internet only access mode. \n");
914 			eth_bridge_handle_wlan_mode_switch();
915 		}
916 		else
917 		{
918 			IPACMDBG_H("No change in %s access mode. \n",
919 					IPACM_Iface::ipacmcfg->iface_table[ipa_if_num].iface_name);
920 		}
921 	}
922 	break;
923 	case IPA_TETHERING_STATS_UPDATE_EVENT:
924 	{
925 		IPACMDBG_H("Received IPA_TETHERING_STATS_UPDATE_EVENT event.\n");
926 		if (IPACM_Wan::isWanUP(ipa_if_num) || IPACM_Wan::isWanUP_V6(ipa_if_num))
927 		{
928 			if(IPACM_Wan::backhaul_is_sta_mode == false) /* LTE */
929 			{
930 				ipa_get_data_stats_resp_msg_v01 *data = (ipa_get_data_stats_resp_msg_v01 *)param;
931 				if (data->ipa_stats_type != QMI_IPA_STATS_TYPE_PIPE_V01)
932 				{
933 					IPACMERR("not valid pipe stats\n");
934 					return;
935 				}
936 				handle_tethering_stats_event(data);
937 			};
938 		}
939 	}
940 	break;
941 #ifdef FEATURE_IPACM_HAL
942 	/* WA for WLAN to clean up NAT instance during SSR */
943 	case IPA_SSR_NOTICE:
944 	{
945 		IPACMDBG_H("Received IPA_SSR_NOTICE event.\n");
946 		IPACM_Iface::ipacmcfg->DelNatIfaces(dev_name); // delete NAT-iface
947 	}
948 	break;
949 	case IPA_WLAN_FWR_SSR_BEFORE_SHUTDOWN_NOTICE:
950         {
951                 IPACMDBG_H("Received IPA_WLAN_FWR_SSR_BEFORE_SHUTDOWN_NOTICE.\n");
952 
953                 /* internal push add_downstream event in cache */
954                 OffloadMng = IPACM_OffloadManager::GetInstance();
955                 if (OffloadMng == NULL) {
956                         IPACMERR("failed to get IPACM_OffloadManager instance !\n");
957                 } else {
958                         IPACMDBG_H("Update iface %s add_downstream cache events\n", dev_name);
959 			if(ip_type == IPA_IP_v4 || ip_type == IPA_IP_MAX)
960 			{
961 				OffloadMng->push_framework_event(dev_name, prefix[IPA_IP_v4]);
962 			}
963 			else if(ip_type == IPA_IP_v6 || ip_type == IPA_IP_MAX)
964 			{
965 				OffloadMng->push_framework_event(dev_name, prefix[IPA_IP_v6]);
966 			}
967                 }
968                 IPACM_Iface::ipacmcfg->DelNatIfaces(dev_name); // delete NAT-iface
969         }
970         break;
971 #endif
972 	default:
973 		break;
974 	}
975 	return;
976 }
977 
978 /* handle wifi client initial,copy all partial headers (tx property) */
handle_wlan_client_init_ex(ipacm_event_data_wlan_ex * data)979 int IPACM_Wlan::handle_wlan_client_init_ex(ipacm_event_data_wlan_ex *data)
980 {
981 
982 #define WLAN_IFACE_INDEX_LEN 2
983 
984 	int res = IPACM_SUCCESS, len = 0, i, evt_size;
985 	char index[WLAN_IFACE_INDEX_LEN];
986 	struct ipa_ioc_copy_hdr sCopyHeader;
987 	struct ipa_ioc_add_hdr *pHeaderDescriptor = NULL;
988         uint32_t cnt;
989 
990 	/* start of adding header */
991 	IPACMDBG_H("Wifi client number for this iface: %d & total number of wlan clients: %d\n",
992                  num_wifi_client,IPACM_Wlan::total_num_wifi_clients);
993 
994 	if ((num_wifi_client >= IPA_MAX_NUM_WIFI_CLIENTS) ||
995 			(IPACM_Wlan::total_num_wifi_clients >= IPA_MAX_NUM_WIFI_CLIENTS))
996 	{
997 		IPACMERR("Reached maximum number of wlan clients\n");
998 		return IPACM_FAILURE;
999 	}
1000 
1001 	IPACMDBG_H("Wifi client number: %d\n", num_wifi_client);
1002 
1003 	/* add header to IPA */
1004 	if(tx_prop != NULL)
1005 	{
1006 		len = sizeof(struct ipa_ioc_add_hdr) + (1 * sizeof(struct ipa_hdr_add));
1007 		pHeaderDescriptor = (struct ipa_ioc_add_hdr *)calloc(1, len);
1008 		if (pHeaderDescriptor == NULL)
1009 		{
1010 			IPACMERR("calloc failed to allocate pHeaderDescriptor\n");
1011 			return IPACM_FAILURE;
1012 		}
1013 
1014 		evt_size = sizeof(ipacm_event_data_wlan_ex) + data->num_of_attribs * sizeof(struct ipa_wlan_hdr_attrib_val);
1015 		get_client_memptr(wlan_client, num_wifi_client)->p_hdr_info = (ipacm_event_data_wlan_ex*)malloc(evt_size);
1016 		memcpy(get_client_memptr(wlan_client, num_wifi_client)->p_hdr_info, data, evt_size);
1017 
1018 		/* copy partial header for v4*/
1019 		for (cnt=0; cnt<tx_prop->num_tx_props; cnt++)
1020 		{
1021 			if(tx_prop->tx[cnt].ip==IPA_IP_v4)
1022 			{
1023 				IPACMDBG_H("Got partial v4-header name from %d tx props\n", cnt);
1024 				memset(&sCopyHeader, 0, sizeof(sCopyHeader));
1025 				memcpy(sCopyHeader.name,
1026 							 tx_prop->tx[cnt].hdr_name,
1027 							 sizeof(sCopyHeader.name));
1028 
1029 				IPACMDBG_H("header name: %s in tx:%d\n", sCopyHeader.name,cnt);
1030 				if (m_header.CopyHeader(&sCopyHeader) == false)
1031 				{
1032 					PERROR("ioctl copy header failed");
1033 					res = IPACM_FAILURE;
1034 					goto fail;
1035 				}
1036 
1037 				IPACMDBG_H("header length: %d, paritial: %d\n", sCopyHeader.hdr_len, sCopyHeader.is_partial);
1038 				if (sCopyHeader.hdr_len > IPA_HDR_MAX_SIZE)
1039 				{
1040 					IPACMERR("header oversize\n");
1041 					res = IPACM_FAILURE;
1042 					goto fail;
1043 				}
1044 				else
1045 				{
1046 					memcpy(pHeaderDescriptor->hdr[0].hdr,
1047 								 sCopyHeader.hdr,
1048 								 sCopyHeader.hdr_len);
1049 				}
1050 
1051 				for(i = 0; i < data->num_of_attribs; i++)
1052 				{
1053 					if(data->attribs[i].attrib_type == WLAN_HDR_ATTRIB_MAC_ADDR)
1054 					{
1055 						memcpy(get_client_memptr(wlan_client, num_wifi_client)->mac,
1056 								data->attribs[i].u.mac_addr,
1057 								sizeof(get_client_memptr(wlan_client, num_wifi_client)->mac));
1058 
1059 						/* copy client mac_addr to partial header */
1060 						memcpy(&pHeaderDescriptor->hdr[0].hdr[data->attribs[i].offset],
1061 									 get_client_memptr(wlan_client, num_wifi_client)->mac,
1062 									 IPA_MAC_ADDR_SIZE);
1063 						/* replace src mac to bridge mac_addr if any  */
1064 						if (IPACM_Iface::ipacmcfg->ipa_bridge_enable)
1065 						{
1066 							memcpy(&pHeaderDescriptor->hdr[0].hdr[data->attribs[i].offset+IPA_MAC_ADDR_SIZE],
1067 									 IPACM_Iface::ipacmcfg->bridge_mac,
1068 									 IPA_MAC_ADDR_SIZE);
1069 							IPACMDBG_H("device is in bridge mode \n");
1070 						}
1071 
1072 					}
1073 					else if(data->attribs[i].attrib_type == WLAN_HDR_ATTRIB_STA_ID)
1074 					{
1075 						/* copy client id to header */
1076 						memcpy(&pHeaderDescriptor->hdr[0].hdr[data->attribs[i].offset],
1077 									&data->attribs[i].u.sta_id, sizeof(data->attribs[i].u.sta_id));
1078 					}
1079 					else
1080 					{
1081 						IPACMDBG_H("The attribute type is not expected!\n");
1082 					}
1083 				}
1084 
1085 				pHeaderDescriptor->commit = true;
1086 				pHeaderDescriptor->num_hdrs = 1;
1087 
1088 				memset(pHeaderDescriptor->hdr[0].name, 0,
1089 							 sizeof(pHeaderDescriptor->hdr[0].name));
1090 
1091 				snprintf(index,sizeof(index), "%d", ipa_if_num);
1092 				strlcpy(pHeaderDescriptor->hdr[0].name, index, sizeof(pHeaderDescriptor->hdr[0].name));
1093 				pHeaderDescriptor->hdr[0].name[IPA_RESOURCE_NAME_MAX-1] = '\0';
1094 
1095 				if (strlcat(pHeaderDescriptor->hdr[0].name, IPA_WLAN_PARTIAL_HDR_NAME_v4, sizeof(pHeaderDescriptor->hdr[0].name)) > IPA_RESOURCE_NAME_MAX)
1096 				{
1097 					IPACMERR(" header name construction failed exceed length (%zu)\n", strlen(pHeaderDescriptor->hdr[0].name));
1098 					res = IPACM_FAILURE;
1099 					goto fail;
1100 				}
1101 				snprintf(index,sizeof(index), "%d", header_name_count);
1102 				if (strlcat(pHeaderDescriptor->hdr[0].name, index, sizeof(pHeaderDescriptor->hdr[0].name)) > IPA_RESOURCE_NAME_MAX)
1103 				{
1104 					IPACMERR(" header name construction failed exceed length (%zu)\n", strlen(pHeaderDescriptor->hdr[0].name));
1105 					res = IPACM_FAILURE;
1106 					goto fail;
1107 				}
1108 
1109 
1110 				pHeaderDescriptor->hdr[0].hdr_len = sCopyHeader.hdr_len;
1111 				pHeaderDescriptor->hdr[0].hdr_hdl = -1;
1112 				pHeaderDescriptor->hdr[0].is_partial = 0;
1113 				pHeaderDescriptor->hdr[0].status = -1;
1114 
1115 				if (m_header.AddHeader(pHeaderDescriptor) == false ||
1116 						pHeaderDescriptor->hdr[0].status != 0)
1117 				{
1118 					IPACMERR("ioctl IPA_IOC_ADD_HDR failed: %d\n", pHeaderDescriptor->hdr[0].status);
1119 					res = IPACM_FAILURE;
1120 					goto fail;
1121 				}
1122 
1123 				get_client_memptr(wlan_client, num_wifi_client)->hdr_hdl_v4 = pHeaderDescriptor->hdr[0].hdr_hdl;
1124 				IPACMDBG_H("client(%d) v4 full header name:%s header handle:(0x%x)\n",
1125 								 num_wifi_client,
1126 								 pHeaderDescriptor->hdr[0].name,
1127 								 get_client_memptr(wlan_client, num_wifi_client)->hdr_hdl_v4);
1128 				get_client_memptr(wlan_client, num_wifi_client)->ipv4_header_set=true;
1129 				break;
1130 			}
1131 		}
1132 
1133 		/* copy partial header for v6*/
1134 		for (cnt=0; cnt<tx_prop->num_tx_props; cnt++)
1135 		{
1136 			if(tx_prop->tx[cnt].ip==IPA_IP_v6)
1137 			{
1138 				IPACMDBG_H("Got partial v6-header name from %d tx props\n", cnt);
1139 				memset(&sCopyHeader, 0, sizeof(sCopyHeader));
1140 				memcpy(sCopyHeader.name,
1141 							 tx_prop->tx[cnt].hdr_name,
1142 							 sizeof(sCopyHeader.name));
1143 
1144 				IPACMDBG_H("header name: %s in tx:%d\n", sCopyHeader.name,cnt);
1145 				if (m_header.CopyHeader(&sCopyHeader) == false)
1146 				{
1147 					PERROR("ioctl copy header failed");
1148 					res = IPACM_FAILURE;
1149 					goto fail;
1150 				}
1151 
1152 				IPACMDBG_H("header length: %d, paritial: %d\n", sCopyHeader.hdr_len, sCopyHeader.is_partial);
1153 				if (sCopyHeader.hdr_len > IPA_HDR_MAX_SIZE)
1154 				{
1155 					IPACMERR("header oversize\n");
1156 					res = IPACM_FAILURE;
1157 					goto fail;
1158 				}
1159 				else
1160 				{
1161 					memcpy(pHeaderDescriptor->hdr[0].hdr,
1162 								 sCopyHeader.hdr,
1163 								 sCopyHeader.hdr_len);
1164 				}
1165 
1166 				for(i = 0; i < data->num_of_attribs; i++)
1167 				{
1168 					if(data->attribs[i].attrib_type == WLAN_HDR_ATTRIB_MAC_ADDR)
1169 					{
1170 						memcpy(get_client_memptr(wlan_client, num_wifi_client)->mac,
1171 								data->attribs[i].u.mac_addr,
1172 								sizeof(get_client_memptr(wlan_client, num_wifi_client)->mac));
1173 
1174 						/* copy client mac_addr to partial header */
1175 						memcpy(&pHeaderDescriptor->hdr[0].hdr[data->attribs[i].offset],
1176 								get_client_memptr(wlan_client, num_wifi_client)->mac,
1177 								IPA_MAC_ADDR_SIZE);
1178 
1179 						/* replace src mac to bridge mac_addr if any  */
1180 						if (IPACM_Iface::ipacmcfg->ipa_bridge_enable)
1181 						{
1182 							memcpy(&pHeaderDescriptor->hdr[0].hdr[data->attribs[i].offset+IPA_MAC_ADDR_SIZE],
1183 									 IPACM_Iface::ipacmcfg->bridge_mac,
1184 									 IPA_MAC_ADDR_SIZE);
1185 							IPACMDBG_H("device is in bridge mode \n");
1186 						}
1187 					}
1188 					else if (data->attribs[i].attrib_type == WLAN_HDR_ATTRIB_STA_ID)
1189 					{
1190 						/* copy client id to header */
1191 						memcpy(&pHeaderDescriptor->hdr[0].hdr[data->attribs[i].offset],
1192 								&data->attribs[i].u.sta_id, sizeof(data->attribs[i].u.sta_id));
1193 					}
1194 					else
1195 					{
1196 						IPACMDBG_H("The attribute type is not expected!\n");
1197 					}
1198 				}
1199 
1200 				pHeaderDescriptor->commit = true;
1201 				pHeaderDescriptor->num_hdrs = 1;
1202 
1203 				memset(pHeaderDescriptor->hdr[0].name, 0,
1204 							 sizeof(pHeaderDescriptor->hdr[0].name));
1205 
1206 				snprintf(index,sizeof(index), "%d", ipa_if_num);
1207 				strlcpy(pHeaderDescriptor->hdr[0].name, index, sizeof(pHeaderDescriptor->hdr[0].name));
1208 				pHeaderDescriptor->hdr[0].name[IPA_RESOURCE_NAME_MAX-1] = '\0';
1209 				if (strlcat(pHeaderDescriptor->hdr[0].name, IPA_WLAN_PARTIAL_HDR_NAME_v6, sizeof(pHeaderDescriptor->hdr[0].name)) > IPA_RESOURCE_NAME_MAX)
1210 				{
1211 					IPACMERR(" header name construction failed exceed length (%zu)\n", strlen(pHeaderDescriptor->hdr[0].name));
1212 					res = IPACM_FAILURE;
1213 					goto fail;
1214 				}
1215 
1216 				snprintf(index,sizeof(index), "%d", header_name_count);
1217 				if (strlcat(pHeaderDescriptor->hdr[0].name, index, sizeof(pHeaderDescriptor->hdr[0].name)) > IPA_RESOURCE_NAME_MAX)
1218 				{
1219 					IPACMERR(" header name construction failed exceed length (%zu)\n", strlen(pHeaderDescriptor->hdr[0].name));
1220 					res = IPACM_FAILURE;
1221 					goto fail;
1222 				}
1223 
1224 				pHeaderDescriptor->hdr[0].hdr_len = sCopyHeader.hdr_len;
1225 				pHeaderDescriptor->hdr[0].hdr_hdl = -1;
1226 				pHeaderDescriptor->hdr[0].is_partial = 0;
1227 				pHeaderDescriptor->hdr[0].status = -1;
1228 
1229 				if (m_header.AddHeader(pHeaderDescriptor) == false ||
1230 						pHeaderDescriptor->hdr[0].status != 0)
1231 				{
1232 					IPACMERR("ioctl IPA_IOC_ADD_HDR failed: %d\n", pHeaderDescriptor->hdr[0].status);
1233 					res = IPACM_FAILURE;
1234 					goto fail;
1235 				}
1236 
1237 				get_client_memptr(wlan_client, num_wifi_client)->hdr_hdl_v6 = pHeaderDescriptor->hdr[0].hdr_hdl;
1238 				IPACMDBG_H("client(%d) v6 full header name:%s header handle:(0x%x)\n",
1239 								 num_wifi_client,
1240 								 pHeaderDescriptor->hdr[0].name,
1241 											 get_client_memptr(wlan_client, num_wifi_client)->hdr_hdl_v6);
1242 
1243 				get_client_memptr(wlan_client, num_wifi_client)->ipv6_header_set=true;
1244 				break;
1245 			}
1246 		}
1247 
1248 		/* initialize wifi client*/
1249 		get_client_memptr(wlan_client, num_wifi_client)->route_rule_set_v4 = false;
1250 		get_client_memptr(wlan_client, num_wifi_client)->route_rule_set_v6 = 0;
1251 		get_client_memptr(wlan_client, num_wifi_client)->ipv4_set = false;
1252 		get_client_memptr(wlan_client, num_wifi_client)->ipv6_set = 0;
1253 		get_client_memptr(wlan_client, num_wifi_client)->power_save_set=false;
1254 		num_wifi_client++;
1255 		header_name_count++; //keep increasing header_name_count
1256 		IPACM_Wlan::total_num_wifi_clients++;
1257 		res = IPACM_SUCCESS;
1258 		IPACMDBG_H("Wifi client number: %d\n", num_wifi_client);
1259 	}
1260 	else
1261 	{
1262 		return res;
1263 	}
1264 
1265 fail:
1266 	free(pHeaderDescriptor);
1267 	return res;
1268 }
1269 
1270 /*handle wifi client */
handle_wlan_client_ipaddr(ipacm_event_data_all * data)1271 int IPACM_Wlan::handle_wlan_client_ipaddr(ipacm_event_data_all *data)
1272 {
1273 	int clnt_indx;
1274 	int v6_num;
1275 	uint32_t ipv6_link_local_prefix = 0xFE800000;
1276 	uint32_t ipv6_link_local_prefix_mask = 0xFFC00000;
1277 
1278 	IPACMDBG_H("number of wifi clients: %d\n", num_wifi_client);
1279 	IPACMDBG_H(" event MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
1280 					 data->mac_addr[0],
1281 					 data->mac_addr[1],
1282 					 data->mac_addr[2],
1283 					 data->mac_addr[3],
1284 					 data->mac_addr[4],
1285 					 data->mac_addr[5]);
1286 
1287 	clnt_indx = get_wlan_client_index(data->mac_addr);
1288 
1289 		if (clnt_indx == IPACM_INVALID_INDEX)
1290 		{
1291 			IPACMERR("wlan client not found/attached \n");
1292 			return IPACM_FAILURE;
1293 		}
1294 
1295 	IPACMDBG_H("Ip-type received %d\n", data->iptype);
1296 	if (data->iptype == IPA_IP_v4)
1297 	{
1298 		IPACMDBG_H("ipv4 address: 0x%x\n", data->ipv4_addr);
1299 		if (data->ipv4_addr != 0) /* not 0.0.0.0 */
1300 		{
1301 			if (get_client_memptr(wlan_client, clnt_indx)->ipv4_set == false)
1302 			{
1303 				get_client_memptr(wlan_client, clnt_indx)->v4_addr = data->ipv4_addr;
1304 				get_client_memptr(wlan_client, clnt_indx)->ipv4_set = true;
1305 			}
1306 			else
1307 			{
1308 			   /* check if client got new IPv4 address*/
1309 			   if(data->ipv4_addr == get_client_memptr(wlan_client, clnt_indx)->v4_addr)
1310 			   {
1311 			     IPACMDBG_H("Already setup ipv4 addr for client:%d, ipv4 address didn't change\n", clnt_indx);
1312 				 return IPACM_FAILURE;
1313 			   }
1314 			   else
1315 			   {
1316 			     IPACMDBG_H("ipv4 addr for client:%d is changed \n", clnt_indx);
1317 				 /* delete NAT rules first */
1318 				 CtList->HandleNeighIpAddrDelEvt(get_client_memptr(wlan_client, clnt_indx)->v4_addr);
1319 			     delete_default_qos_rtrules(clnt_indx,IPA_IP_v4);
1320 		         get_client_memptr(wlan_client, clnt_indx)->route_rule_set_v4 = false;
1321 			     get_client_memptr(wlan_client, clnt_indx)->v4_addr = data->ipv4_addr;
1322 			}
1323 		}
1324 	}
1325 	else
1326 	{
1327 		    IPACMDBG_H("Invalid client IPv4 address \n");
1328 		    return IPACM_FAILURE;
1329 		}
1330 	}
1331 	else
1332 	{
1333 		if ((data->ipv6_addr[0] != 0) || (data->ipv6_addr[1] != 0) ||
1334 				(data->ipv6_addr[2] != 0) || (data->ipv6_addr[3] != 0)) /* check if all 0 not valid ipv6 address */
1335 		{
1336 			IPACMDBG_H("ipv6 address: 0x%x:%x:%x:%x\n", data->ipv6_addr[0], data->ipv6_addr[1], data->ipv6_addr[2], data->ipv6_addr[3]);
1337 			if( (data->ipv6_addr[0] & ipv6_link_local_prefix_mask) != (ipv6_link_local_prefix & ipv6_link_local_prefix_mask) &&
1338 				memcmp(ipv6_prefix, data->ipv6_addr, sizeof(ipv6_prefix)) != 0)
1339 			{
1340 				IPACMDBG_H("This IPv6 address is not global IPv6 address with correct prefix, ignore.\n");
1341 				IPACMDBG_H("ipv6 address: 0x%x:%x ipv6_prefix0x%x:%x\n", data->ipv6_addr[0], data->ipv6_addr[1], ipv6_prefix[0], ipv6_prefix[1]);
1342 				return IPACM_FAILURE;
1343 			}
1344 
1345 			if(get_client_memptr(wlan_client, clnt_indx)->ipv6_set < IPV6_NUM_ADDR)
1346 			{
1347 
1348 		       for(v6_num=0;v6_num < get_client_memptr(wlan_client, clnt_indx)->ipv6_set;v6_num++)
1349 				{
1350 					if( data->ipv6_addr[0] == get_client_memptr(wlan_client, clnt_indx)->v6_addr[v6_num][0] &&
1351 			           data->ipv6_addr[1] == get_client_memptr(wlan_client, clnt_indx)->v6_addr[v6_num][1] &&
1352 			  	        data->ipv6_addr[2]== get_client_memptr(wlan_client, clnt_indx)->v6_addr[v6_num][2] &&
1353 			  	         data->ipv6_addr[3] == get_client_memptr(wlan_client, clnt_indx)->v6_addr[v6_num][3])
1354 					{
1355 			  	    IPACMDBG_H("Already see this ipv6 addr for client:%d\n", clnt_indx);
1356 			  	    return IPACM_FAILURE; /* not setup the RT rules*/
1357 			  		break;
1358 					}
1359 				}
1360 
1361 		       /* not see this ipv6 before for wifi client*/
1362 			   get_client_memptr(wlan_client, clnt_indx)->v6_addr[get_client_memptr(wlan_client, clnt_indx)->ipv6_set][0] = data->ipv6_addr[0];
1363 			   get_client_memptr(wlan_client, clnt_indx)->v6_addr[get_client_memptr(wlan_client, clnt_indx)->ipv6_set][1] = data->ipv6_addr[1];
1364 			   get_client_memptr(wlan_client, clnt_indx)->v6_addr[get_client_memptr(wlan_client, clnt_indx)->ipv6_set][2] = data->ipv6_addr[2];
1365 			   get_client_memptr(wlan_client, clnt_indx)->v6_addr[get_client_memptr(wlan_client, clnt_indx)->ipv6_set][3] = data->ipv6_addr[3];
1366 			   get_client_memptr(wlan_client, clnt_indx)->ipv6_set++;
1367 		    }
1368 		    else
1369 		    {
1370 				IPACMDBG_H("Already got %d ipv6 addr for client:%d\n", IPV6_NUM_ADDR, clnt_indx);
1371 				return IPACM_FAILURE; /* not setup the RT rules*/
1372 		    }
1373 		}
1374 		else
1375 		{
1376 			IPACMDBG_H("IPV6 address is invalid\n");
1377 			return IPACM_FAILURE;
1378 		}
1379 	}
1380 
1381 	return IPACM_SUCCESS;
1382 }
1383 
1384 /*handle wifi client routing rule*/
handle_wlan_client_route_rule(uint8_t * mac_addr,ipa_ip_type iptype)1385 int IPACM_Wlan::handle_wlan_client_route_rule(uint8_t *mac_addr, ipa_ip_type iptype)
1386 {
1387 	struct ipa_ioc_add_rt_rule *rt_rule;
1388 	struct ipa_rt_rule_add *rt_rule_entry;
1389 	uint32_t tx_index;
1390 	int wlan_index,v6_num;
1391 	const int NUM = 1;
1392 
1393 	if(tx_prop == NULL)
1394 	{
1395 		IPACMDBG_H("No rx properties registered for iface %s\n", dev_name);
1396 		return IPACM_SUCCESS;
1397 	}
1398 
1399 	IPACMDBG_H("Received mac_addr MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
1400 			mac_addr[0], mac_addr[1], mac_addr[2],
1401 			mac_addr[3], mac_addr[4], mac_addr[5]);
1402 
1403 	wlan_index = get_wlan_client_index(mac_addr);
1404 	if (wlan_index == IPACM_INVALID_INDEX)
1405 	{
1406 		IPACMDBG_H("wlan client not found/attached \n");
1407 		return IPACM_SUCCESS;
1408 	}
1409 
1410 	/* during power_save mode, even receive IP_ADDR_ADD, not setting RT rules*/
1411 	if (get_client_memptr(wlan_client, wlan_index)->power_save_set == true)
1412 	{
1413 		IPACMDBG_H("wlan client is in power safe mode \n");
1414 		return IPACM_SUCCESS;
1415 	}
1416 
1417 	if (iptype==IPA_IP_v4)
1418 	{
1419 		IPACMDBG_H("wlan client index: %d, ip-type: %d, ipv4_set:%d, ipv4_rule_set:%d \n", wlan_index, iptype,
1420 				get_client_memptr(wlan_client, wlan_index)->ipv4_set,
1421 				get_client_memptr(wlan_client, wlan_index)->route_rule_set_v4);
1422 	}
1423 	else
1424 	{
1425 		IPACMDBG_H("wlan client index: %d, ip-type: %d, ipv6_set:%d, ipv6_rule_num:%d \n", wlan_index, iptype,
1426 				get_client_memptr(wlan_client, wlan_index)->ipv6_set,
1427 				get_client_memptr(wlan_client, wlan_index)->route_rule_set_v6);
1428 	}
1429 
1430 
1431 	/* Add default  Qos routing rules if not set yet */
1432 	if ((iptype == IPA_IP_v4
1433 				&& get_client_memptr(wlan_client, wlan_index)->route_rule_set_v4 == false
1434 				&& get_client_memptr(wlan_client, wlan_index)->ipv4_set == true)
1435 			|| (iptype == IPA_IP_v6
1436 				&& get_client_memptr(wlan_client, wlan_index)->route_rule_set_v6 < get_client_memptr(wlan_client, wlan_index)->ipv6_set
1437 			   ))
1438 	{
1439 		rt_rule = (struct ipa_ioc_add_rt_rule *)
1440 			calloc(1, sizeof(struct ipa_ioc_add_rt_rule) +
1441 					NUM * sizeof(struct ipa_rt_rule_add));
1442 
1443 		if (rt_rule == NULL)
1444 		{
1445 			PERROR("Error Locate ipa_ioc_add_rt_rule memory...\n");
1446 			return IPACM_FAILURE;
1447 		}
1448 
1449 		rt_rule->commit = 1;
1450 		rt_rule->num_rules = (uint8_t)NUM;
1451 		rt_rule->ip = iptype;
1452 
1453 
1454 		for (tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
1455 		{
1456 
1457 			if(iptype != tx_prop->tx[tx_index].ip)
1458 			{
1459 				IPACMDBG_H("Tx:%d, ip-type: %d conflict ip-type: %d no RT-rule added\n",
1460 						tx_index, tx_prop->tx[tx_index].ip,iptype);
1461 				continue;
1462 			}
1463 
1464 			rt_rule_entry = &rt_rule->rules[0];
1465 			rt_rule_entry->at_rear = 0;
1466 
1467 			if (iptype == IPA_IP_v4)
1468 			{
1469 				IPACMDBG_H("client index(%d):ipv4 address: 0x%x\n", wlan_index,
1470 						get_client_memptr(wlan_client, wlan_index)->v4_addr);
1471 
1472 				IPACMDBG_H("client(%d): v4 header handle:(0x%x)\n",
1473 						wlan_index,
1474 						get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v4);
1475 				strlcpy(rt_rule->rt_tbl_name,
1476 						IPACM_Iface::ipacmcfg->rt_tbl_lan_v4.name,
1477 						sizeof(rt_rule->rt_tbl_name));
1478 				rt_rule->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
1479 
1480 				if(IPACM_Iface::ipacmcfg->isMCC_Mode)
1481 				{
1482 					IPACMDBG_H("In MCC mode, use alt dst pipe: %d\n",
1483 							tx_prop->tx[tx_index].alt_dst_pipe);
1484 					rt_rule_entry->rule.dst = tx_prop->tx[tx_index].alt_dst_pipe;
1485 				}
1486 				else
1487 				{
1488 					rt_rule_entry->rule.dst = tx_prop->tx[tx_index].dst_pipe;
1489 				}
1490 
1491 				memcpy(&rt_rule_entry->rule.attrib,
1492 						&tx_prop->tx[tx_index].attrib,
1493 						sizeof(rt_rule_entry->rule.attrib));
1494 				rt_rule_entry->rule.attrib.attrib_mask |= IPA_FLT_DST_ADDR;
1495 				rt_rule_entry->rule.hdr_hdl = get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v4;
1496 				rt_rule_entry->rule.attrib.u.v4.dst_addr = get_client_memptr(wlan_client, wlan_index)->v4_addr;
1497 				rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF;
1498 
1499 				if(IPACM_Iface::ipacmcfg->GetIPAVer() >= IPA_HW_v4_0)
1500 				{
1501 					rt_rule_entry->rule.hashable = true;
1502 				}
1503 
1504 				if (false == m_routing.AddRoutingRule(rt_rule))
1505 				{
1506 					IPACMERR("Routing rule addition failed!\n");
1507 					free(rt_rule);
1508 					return IPACM_FAILURE;
1509 				}
1510 
1511 				/* copy ipv4 RT hdl */
1512 				get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v4 =
1513 					rt_rule->rules[0].rt_rule_hdl;
1514 				IPACMDBG_H("tx:%d, rt rule hdl=%x ip-type: %d\n", tx_index,
1515 						get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v4, iptype);
1516 			}
1517 			else
1518 			{
1519 				for(v6_num = get_client_memptr(wlan_client, wlan_index)->route_rule_set_v6;v6_num < get_client_memptr(wlan_client, wlan_index)->ipv6_set;v6_num++)
1520 				{
1521 					IPACMDBG_H("client(%d): v6 header handle:(0x%x)\n",
1522 							wlan_index,
1523 							get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v6);
1524 
1525 					/* v6 LAN_RT_TBL */
1526 					strlcpy(rt_rule->rt_tbl_name,
1527 							IPACM_Iface::ipacmcfg->rt_tbl_v6.name,
1528 							sizeof(rt_rule->rt_tbl_name));
1529 					rt_rule->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
1530 					/* Support QCMAP LAN traffic feature, send to A5 */
1531 					rt_rule_entry->rule.dst = iface_query->excp_pipe;
1532 					memset(&rt_rule_entry->rule.attrib, 0, sizeof(rt_rule_entry->rule.attrib));
1533 					rt_rule_entry->rule.hdr_hdl = 0;
1534 					rt_rule_entry->rule.attrib.attrib_mask |= IPA_FLT_DST_ADDR;
1535 					rt_rule_entry->rule.attrib.u.v6.dst_addr[0] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][0];
1536 					rt_rule_entry->rule.attrib.u.v6.dst_addr[1] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][1];
1537 					rt_rule_entry->rule.attrib.u.v6.dst_addr[2] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][2];
1538 					rt_rule_entry->rule.attrib.u.v6.dst_addr[3] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][3];
1539 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[0] = 0xFFFFFFFF;
1540 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[1] = 0xFFFFFFFF;
1541 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[2] = 0xFFFFFFFF;
1542 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[3] = 0xFFFFFFFF;
1543 #ifdef FEATURE_IPA_V3
1544 					rt_rule_entry->rule.hashable = true;
1545 #endif
1546 					if (false == m_routing.AddRoutingRule(rt_rule))
1547 					{
1548 						IPACMERR("Routing rule addition failed!\n");
1549 						free(rt_rule);
1550 						return IPACM_FAILURE;
1551 					}
1552 
1553 					get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6[v6_num] = rt_rule->rules[0].rt_rule_hdl;
1554 					IPACMDBG_H("tx:%d, rt rule hdl=%x ip-type: %d\n", tx_index,
1555 							get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6[v6_num], iptype);
1556 
1557 					/*Copy same rule to v6 WAN RT TBL*/
1558 					strlcpy(rt_rule->rt_tbl_name,
1559 							IPACM_Iface::ipacmcfg->rt_tbl_wan_v6.name,
1560 							sizeof(rt_rule->rt_tbl_name));
1561 					rt_rule->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
1562 					/* Downlink traffic from Wan iface, directly through IPA */
1563 					if(IPACM_Iface::ipacmcfg->isMCC_Mode)
1564 					{
1565 						IPACMDBG_H("In MCC mode, use alt dst pipe: %d\n",
1566 								tx_prop->tx[tx_index].alt_dst_pipe);
1567 						rt_rule_entry->rule.dst = tx_prop->tx[tx_index].alt_dst_pipe;
1568 					}
1569 					else
1570 					{
1571 						rt_rule_entry->rule.dst = tx_prop->tx[tx_index].dst_pipe;
1572 					}
1573 					memcpy(&rt_rule_entry->rule.attrib,
1574 							&tx_prop->tx[tx_index].attrib,
1575 							sizeof(rt_rule_entry->rule.attrib));
1576 					rt_rule_entry->rule.hdr_hdl = get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v6;
1577 					rt_rule_entry->rule.attrib.attrib_mask |= IPA_FLT_DST_ADDR;
1578 					rt_rule_entry->rule.attrib.u.v6.dst_addr[0] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][0];
1579 					rt_rule_entry->rule.attrib.u.v6.dst_addr[1] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][1];
1580 					rt_rule_entry->rule.attrib.u.v6.dst_addr[2] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][2];
1581 					rt_rule_entry->rule.attrib.u.v6.dst_addr[3] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][3];
1582 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[0] = 0xFFFFFFFF;
1583 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[1] = 0xFFFFFFFF;
1584 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[2] = 0xFFFFFFFF;
1585 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[3] = 0xFFFFFFFF;
1586 #ifdef FEATURE_IPA_V3
1587 					rt_rule_entry->rule.hashable = true;
1588 #endif
1589 					if (false == m_routing.AddRoutingRule(rt_rule))
1590 					{
1591 						IPACMERR("Routing rule addition failed!\n");
1592 						free(rt_rule);
1593 						return IPACM_FAILURE;
1594 					}
1595 
1596 					get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6_wan[v6_num] = rt_rule->rules[0].rt_rule_hdl;
1597 
1598 					IPACMDBG_H("tx:%d, rt rule hdl=%x ip-type: %d\n", tx_index,
1599 							get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6_wan[v6_num], iptype);
1600 				}
1601 			}
1602 
1603 		} /* end of for loop */
1604 
1605 		free(rt_rule);
1606 
1607 		if (iptype == IPA_IP_v4)
1608 		{
1609 			get_client_memptr(wlan_client, wlan_index)->route_rule_set_v4 = true;
1610 		}
1611 		else
1612 		{
1613 			get_client_memptr(wlan_client, wlan_index)->route_rule_set_v6 = get_client_memptr(wlan_client, wlan_index)->ipv6_set;
1614 		}
1615 	}
1616 
1617 	return IPACM_SUCCESS;
1618 }
1619 
1620 /*handle wifi client power-save mode*/
handle_wlan_client_pwrsave(uint8_t * mac_addr)1621 int IPACM_Wlan::handle_wlan_client_pwrsave(uint8_t *mac_addr)
1622 {
1623 	int clt_indx;
1624 	IPACMDBG_H("wlan->handle_wlan_client_pwrsave();\n");
1625 
1626 	clt_indx = get_wlan_client_index(mac_addr);
1627 	if (clt_indx == IPACM_INVALID_INDEX)
1628 	{
1629 		IPACMDBG_H("wlan client not attached\n");
1630 		return IPACM_SUCCESS;
1631 	}
1632 
1633         if (get_client_memptr(wlan_client, clt_indx)->power_save_set == false)
1634 	{
1635 		/* First reset nat rules and then route rules */
1636 	    if(get_client_memptr(wlan_client, clt_indx)->ipv4_set == true)
1637 	    {
1638 			IPACMDBG_H("Deleting Nat Rules\n");
1639 			Nat_App->UpdatePwrSaveIf(get_client_memptr(wlan_client, clt_indx)->v4_addr);
1640  	     }
1641 
1642 		IPACMDBG_H("Deleting default qos Route Rules\n");
1643 		delete_default_qos_rtrules(clt_indx, IPA_IP_v4);
1644 		delete_default_qos_rtrules(clt_indx, IPA_IP_v6);
1645                 get_client_memptr(wlan_client, clt_indx)->power_save_set = true;
1646 	}
1647 	else
1648 	{
1649 		IPACMDBG_H("wlan client already in power-save mode\n");
1650 	}
1651     return IPACM_SUCCESS;
1652 }
1653 
1654 /*handle wifi client del mode*/
handle_wlan_client_down_evt(uint8_t * mac_addr)1655 int IPACM_Wlan::handle_wlan_client_down_evt(uint8_t *mac_addr)
1656 {
1657 	int clt_indx;
1658 	uint32_t tx_index;
1659 	int num_wifi_client_tmp = num_wifi_client;
1660 	int num_v6;
1661 
1662 	IPACMDBG_H("total client: %d\n", num_wifi_client_tmp);
1663 
1664 	clt_indx = get_wlan_client_index(mac_addr);
1665 	if (clt_indx == IPACM_INVALID_INDEX)
1666 	{
1667 		IPACMDBG_H("wlan client not attached\n");
1668 		return IPACM_SUCCESS;
1669 	}
1670 
1671 	/* First reset nat rules and then route rules */
1672 	if(get_client_memptr(wlan_client, clt_indx)->ipv4_set == true)
1673 	{
1674 	        IPACMDBG_H("Clean Nat Rules for ipv4:0x%x\n", get_client_memptr(wlan_client, clt_indx)->v4_addr);
1675 			CtList->HandleNeighIpAddrDelEvt(get_client_memptr(wlan_client, clt_indx)->v4_addr);
1676  	}
1677 
1678 	if (delete_default_qos_rtrules(clt_indx, IPA_IP_v4))
1679 	{
1680 		IPACMERR("unbale to delete v4 default qos route rules for index: %d\n", clt_indx);
1681 		return IPACM_FAILURE;
1682 	}
1683 
1684 	if (delete_default_qos_rtrules(clt_indx, IPA_IP_v6))
1685 	{
1686 		IPACMERR("unbale to delete v6 default qos route rules for indexn: %d\n", clt_indx);
1687 		return IPACM_FAILURE;
1688 	}
1689 
1690 	/* Delete wlan client header */
1691 	if(get_client_memptr(wlan_client, clt_indx)->ipv4_header_set == true)
1692 	{
1693 	if (m_header.DeleteHeaderHdl(get_client_memptr(wlan_client, clt_indx)->hdr_hdl_v4)
1694 			== false)
1695 	{
1696 		return IPACM_FAILURE;
1697 	}
1698 		get_client_memptr(wlan_client, clt_indx)->ipv4_header_set = false;
1699 	}
1700 
1701 	if(get_client_memptr(wlan_client, clt_indx)->ipv6_header_set == true)
1702 	{
1703 	if (m_header.DeleteHeaderHdl(get_client_memptr(wlan_client, clt_indx)->hdr_hdl_v6)
1704 			== false)
1705 	{
1706 		return IPACM_FAILURE;
1707 	}
1708 		get_client_memptr(wlan_client, clt_indx)->ipv6_header_set = false;
1709 	}
1710 
1711 	/* Reset ip_set to 0*/
1712 	get_client_memptr(wlan_client, clt_indx)->ipv4_set = false;
1713 	get_client_memptr(wlan_client, clt_indx)->ipv6_set = 0;
1714 	get_client_memptr(wlan_client, clt_indx)->ipv4_header_set = false;
1715 	get_client_memptr(wlan_client, clt_indx)->ipv6_header_set = false;
1716 	get_client_memptr(wlan_client, clt_indx)->route_rule_set_v4 = false;
1717 	get_client_memptr(wlan_client, clt_indx)->route_rule_set_v6 = 0;
1718 	free(get_client_memptr(wlan_client, clt_indx)->p_hdr_info);
1719 
1720 	for (; clt_indx < num_wifi_client_tmp - 1; clt_indx++)
1721 	{
1722 		get_client_memptr(wlan_client, clt_indx)->p_hdr_info = get_client_memptr(wlan_client, (clt_indx + 1))->p_hdr_info;
1723 
1724 		memcpy(get_client_memptr(wlan_client, clt_indx)->mac,
1725 					 get_client_memptr(wlan_client, (clt_indx + 1))->mac,
1726 					 sizeof(get_client_memptr(wlan_client, clt_indx)->mac));
1727 
1728 		get_client_memptr(wlan_client, clt_indx)->hdr_hdl_v4 = get_client_memptr(wlan_client, (clt_indx + 1))->hdr_hdl_v4;
1729 		get_client_memptr(wlan_client, clt_indx)->hdr_hdl_v6 = get_client_memptr(wlan_client, (clt_indx + 1))->hdr_hdl_v6;
1730 		get_client_memptr(wlan_client, clt_indx)->v4_addr = get_client_memptr(wlan_client, (clt_indx + 1))->v4_addr;
1731 
1732 		get_client_memptr(wlan_client, clt_indx)->ipv4_set = get_client_memptr(wlan_client, (clt_indx + 1))->ipv4_set;
1733 		get_client_memptr(wlan_client, clt_indx)->ipv6_set = get_client_memptr(wlan_client, (clt_indx + 1))->ipv6_set;
1734 		get_client_memptr(wlan_client, clt_indx)->ipv4_header_set = get_client_memptr(wlan_client, (clt_indx + 1))->ipv4_header_set;
1735 		get_client_memptr(wlan_client, clt_indx)->ipv6_header_set = get_client_memptr(wlan_client, (clt_indx + 1))->ipv6_header_set;
1736 
1737 		get_client_memptr(wlan_client, clt_indx)->route_rule_set_v4 = get_client_memptr(wlan_client, (clt_indx + 1))->route_rule_set_v4;
1738 		get_client_memptr(wlan_client, clt_indx)->route_rule_set_v6 = get_client_memptr(wlan_client, (clt_indx + 1))->route_rule_set_v6;
1739 
1740                 for(num_v6=0;num_v6< get_client_memptr(wlan_client, clt_indx)->ipv6_set;num_v6++)
1741 	        {
1742 		    get_client_memptr(wlan_client, clt_indx)->v6_addr[num_v6][0] = get_client_memptr(wlan_client, (clt_indx + 1))->v6_addr[num_v6][0];
1743 		    get_client_memptr(wlan_client, clt_indx)->v6_addr[num_v6][1] = get_client_memptr(wlan_client, (clt_indx + 1))->v6_addr[num_v6][1];
1744 		    get_client_memptr(wlan_client, clt_indx)->v6_addr[num_v6][2] = get_client_memptr(wlan_client, (clt_indx + 1))->v6_addr[num_v6][2];
1745 		    get_client_memptr(wlan_client, clt_indx)->v6_addr[num_v6][3] = get_client_memptr(wlan_client, (clt_indx + 1))->v6_addr[num_v6][3];
1746                 }
1747 
1748 		for (tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
1749 		{
1750 			get_client_memptr(wlan_client, clt_indx)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v4 =
1751 				 get_client_memptr(wlan_client, (clt_indx + 1))->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v4;
1752 
1753 			for(num_v6=0;num_v6< get_client_memptr(wlan_client, clt_indx)->route_rule_set_v6;num_v6++)
1754 			{
1755 			  get_client_memptr(wlan_client, clt_indx)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6[num_v6] =
1756 			   	 get_client_memptr(wlan_client, (clt_indx + 1))->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6[num_v6];
1757 			  get_client_memptr(wlan_client, clt_indx)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6_wan[num_v6] =
1758 			   	 get_client_memptr(wlan_client, (clt_indx + 1))->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6_wan[num_v6];
1759 		    }
1760 		}
1761 	}
1762 
1763 	IPACMDBG_H(" %d wifi client deleted successfully \n", num_wifi_client);
1764 	num_wifi_client = num_wifi_client - 1;
1765 	IPACM_Wlan::total_num_wifi_clients = IPACM_Wlan::total_num_wifi_clients - 1;
1766 	IPACMDBG_H(" Number of wifi client: %d\n", num_wifi_client);
1767 
1768 	return IPACM_SUCCESS;
1769 }
1770 
1771 /*handle wlan iface down event*/
handle_down_evt()1772 int IPACM_Wlan::handle_down_evt()
1773 {
1774 	int res = IPACM_SUCCESS, num_private_subnet_fl_rule;
1775 	uint32_t i;
1776 	num_private_subnet_fl_rule = 0;
1777 
1778 	IPACMDBG_H("WLAN ip-type: %d \n", ip_type);
1779 	/* no iface address up, directly close iface*/
1780 	if (ip_type == IPACM_IP_NULL)
1781 	{
1782 		IPACMERR("Invalid iptype: 0x%x\n", ip_type);
1783 		goto fail;
1784 	}
1785 
1786 	/* delete wan filter rule */
1787 	if (IPACM_Wan::isWanUP(ipa_if_num) && rx_prop != NULL)
1788 	{
1789 		IPACMDBG_H("LAN IF goes down, backhaul type %d\n", IPACM_Wan::backhaul_is_sta_mode);
1790 		IPACM_Lan::handle_wan_down(IPACM_Wan::backhaul_is_sta_mode);
1791 #ifdef FEATURE_IPA_ANDROID
1792 #ifndef FEATURE_IPACM_HAL
1793 		/* Clean-up tethered-iface list */
1794 		IPACM_Wan::delete_tether_iface(IPA_IP_v4, ipa_if_num);
1795 #endif
1796 #endif
1797 	}
1798 
1799 	if (IPACM_Wan::isWanUP_V6(ipa_if_num) && rx_prop != NULL)
1800 	{
1801 		IPACMDBG_H("LAN IF goes down, backhaul type %d\n", IPACM_Wan::backhaul_is_sta_mode);
1802 		handle_wan_down_v6(IPACM_Wan::backhaul_is_sta_mode);
1803 #ifdef FEATURE_IPA_ANDROID
1804 		/* Clean-up tethered-iface list */
1805 		IPACM_Wan::delete_tether_iface(IPA_IP_v6, ipa_if_num);
1806 #endif
1807 	}
1808 	IPACMDBG_H("finished deleting wan filtering rules\n ");
1809 
1810 	/* Delete v4 filtering rules */
1811 	if (ip_type != IPA_IP_v6 && rx_prop != NULL)
1812 	{
1813 		/* delete IPv4 icmp filter rules */
1814 		if(m_filtering.DeleteFilteringHdls(ipv4_icmp_flt_rule_hdl, IPA_IP_v4, NUM_IPV4_ICMP_FLT_RULE) == false)
1815 		{
1816 			IPACMERR("Error Deleting ICMPv4 Filtering Rule, aborting...\n");
1817 			res = IPACM_FAILURE;
1818 			goto fail;
1819 		}
1820 		IPACM_Iface::ipacmcfg->decreaseFltRuleCount(rx_prop->rx[0].src_pipe, IPA_IP_v4, NUM_IPV4_ICMP_FLT_RULE);
1821 		if (dft_v4fl_rule_hdl[0] != 0)
1822 		{
1823 			if (m_filtering.DeleteFilteringHdls(dft_v4fl_rule_hdl, IPA_IP_v4, IPV4_DEFAULT_FILTERTING_RULES) == false)
1824 			{
1825 				IPACMERR("Error Deleting Filtering Rule, aborting...\n");
1826 				res = IPACM_FAILURE;
1827 				goto fail;
1828 			}
1829 			IPACM_Iface::ipacmcfg->decreaseFltRuleCount(rx_prop->rx[0].src_pipe, IPA_IP_v4, IPV4_DEFAULT_FILTERTING_RULES);
1830 			IPACMDBG_H("Deleted default v4 filter rules successfully.\n");
1831 		}
1832 		/* delete private-ipv4 filter rules */
1833 #ifdef FEATURE_IPA_ANDROID
1834 		if(m_filtering.DeleteFilteringHdls(private_fl_rule_hdl, IPA_IP_v4, IPA_MAX_PRIVATE_SUBNET_ENTRIES) == false)
1835 		{
1836 			IPACMERR("Error deleting private subnet IPv4 flt rules.\n");
1837 			res = IPACM_FAILURE;
1838 			goto fail;
1839 		}
1840 		IPACM_Iface::ipacmcfg->decreaseFltRuleCount(rx_prop->rx[0].src_pipe, IPA_IP_v4, IPA_MAX_PRIVATE_SUBNET_ENTRIES);
1841 #else
1842 		num_private_subnet_fl_rule = IPACM_Iface::ipacmcfg->ipa_num_private_subnet > IPA_MAX_PRIVATE_SUBNET_ENTRIES?
1843 			IPA_MAX_PRIVATE_SUBNET_ENTRIES : IPACM_Iface::ipacmcfg->ipa_num_private_subnet;
1844 		if(m_filtering.DeleteFilteringHdls(private_fl_rule_hdl, IPA_IP_v4, num_private_subnet_fl_rule) == false)
1845 		{
1846 			IPACMERR("Error deleting private subnet flt rules, aborting...\n");
1847 			res = IPACM_FAILURE;
1848 			goto fail;
1849 		}
1850 		IPACM_Iface::ipacmcfg->decreaseFltRuleCount(rx_prop->rx[0].src_pipe, IPA_IP_v4, num_private_subnet_fl_rule);
1851 #endif
1852 		IPACMDBG_H("Deleted private subnet v4 filter rules successfully.\n");
1853 
1854 #ifdef FEATURE_L2TP
1855 		if(m_filtering.DeleteFilteringHdls(&tcp_syn_flt_rule_hdl[IPA_IP_v4], IPA_IP_v4, 1) == false)
1856 		{
1857 			IPACMERR("Error deleting tcp syn flt rule, aborting...\n");
1858 			res = IPACM_FAILURE;
1859 			goto fail;
1860 		}
1861 #endif
1862 	}
1863 
1864 	/* Delete v6 filtering rules */
1865 	if (ip_type != IPA_IP_v4 && rx_prop != NULL)
1866 	{
1867 		/* delete icmp filter rules */
1868 		if(m_filtering.DeleteFilteringHdls(ipv6_icmp_flt_rule_hdl, IPA_IP_v6, NUM_IPV6_ICMP_FLT_RULE) == false)
1869 		{
1870 			IPACMERR("Error Deleting ICMPv6 Filtering Rule, aborting...\n");
1871 			res = IPACM_FAILURE;
1872 			goto fail;
1873 		}
1874 		IPACM_Iface::ipacmcfg->decreaseFltRuleCount(rx_prop->rx[0].src_pipe, IPA_IP_v6, NUM_IPV6_ICMP_FLT_RULE);
1875 
1876 		if (dft_v6fl_rule_hdl[0] != 0)
1877 		{
1878 			if (m_filtering.DeleteFilteringHdls(dft_v6fl_rule_hdl, IPA_IP_v6, IPV6_DEFAULT_FILTERTING_RULES) == false)
1879 			{
1880 				IPACMERR("Error Adding RuleTable(1) to Filtering, aborting...\n");
1881 				res = IPACM_FAILURE;
1882 				goto fail;
1883 			}
1884 			IPACM_Iface::ipacmcfg->decreaseFltRuleCount(rx_prop->rx[0].src_pipe, IPA_IP_v6, IPV6_DEFAULT_FILTERTING_RULES);
1885 			IPACMDBG_H("Deleted default v6 filter rules successfully.\n");
1886 		}
1887 #ifdef FEATURE_L2TP
1888 		if(m_filtering.DeleteFilteringHdls(&tcp_syn_flt_rule_hdl[IPA_IP_v6], IPA_IP_v6, 1) == false)
1889 		{
1890 			IPACMERR("Error deleting tcp syn flt rule, aborting...\n");
1891 			res = IPACM_FAILURE;
1892 			goto fail;
1893 		}
1894 #endif
1895 	}
1896 	IPACMDBG_H("finished delete filtering rules\n ");
1897 
1898 	/* Delete default v4 RT rule */
1899 	if (ip_type != IPA_IP_v6)
1900 	{
1901 		IPACMDBG_H("Delete default v4 routing rules\n");
1902 		if (m_routing.DeleteRoutingHdl(dft_rt_rule_hdl[0], IPA_IP_v4)
1903 				== false)
1904 		{
1905 			IPACMERR("Routing rule deletion failed!\n");
1906 			res = IPACM_FAILURE;
1907 			goto fail;
1908 		}
1909 	}
1910 
1911 	/* Delete default v6 RT rule */
1912 	if (ip_type != IPA_IP_v4)
1913 	{
1914 		IPACMDBG_H("Delete default v6 routing rules\n");
1915 		/* May have multiple ipv6 iface-RT rules */
1916 		for (i = 0; i < 2*num_dft_rt_v6; i++)
1917 		{
1918 			if (m_routing.DeleteRoutingHdl(dft_rt_rule_hdl[MAX_DEFAULT_v4_ROUTE_RULES+i], IPA_IP_v6)
1919 					== false)
1920 			{
1921 				IPACMERR("Routing rule deletion failed!\n");
1922 				res = IPACM_FAILURE;
1923 				goto fail;
1924 			}
1925 		}
1926 	}
1927 	IPACMDBG_H("finished deleting default RT rules\n ");
1928 
1929 	eth_bridge_post_event(IPA_ETH_BRIDGE_IFACE_DOWN, IPA_IP_MAX, NULL, NULL, NULL);
1930 
1931 	/* free the wlan clients cache */
1932 	IPACMDBG_H("Free wlan clients cache\n");
1933 
1934 	/* Delete private subnet*/
1935 #ifdef FEATURE_IPA_ANDROID
1936 	if (ip_type != IPA_IP_v6)
1937 	{
1938 		IPACMDBG_H("current IPACM private subnet_addr number(%d)\n", IPACM_Iface::ipacmcfg->ipa_num_private_subnet);
1939 		IPACMDBG_H(" Delete IPACM private subnet_addr as: 0x%x \n", if_ipv4_subnet);
1940 		if(IPACM_Iface::ipacmcfg->DelPrivateSubnet(if_ipv4_subnet, ipa_if_num) == false)
1941 		{
1942 			IPACMERR(" can't Delete IPACM private subnet_addr as: 0x%x \n", if_ipv4_subnet);
1943 		}
1944 	}
1945 	/* reset the IPA-client pipe enum */
1946 #ifdef FEATURE_IPACM_HAL
1947 	handle_tethering_client(true, IPACM_CLIENT_MAX);
1948 #else
1949 	handle_tethering_client(true, IPACM_CLIENT_WLAN);
1950 #endif
1951 #endif /* defined(FEATURE_IPA_ANDROID)*/
1952 
1953 fail:
1954 	/* clean wifi-client header, routing rules */
1955 	/* clean wifi client rule*/
1956 	IPACMDBG_H("left %d wifi clients need to be deleted \n ", num_wifi_client);
1957 	for (i = 0; i < num_wifi_client; i++)
1958 	{
1959 		/* First reset nat rules and then route rules */
1960 		if(get_client_memptr(wlan_client, i)->ipv4_set == true)
1961 		{
1962 	        IPACMDBG_H("Clean Nat Rules for ipv4:0x%x\n", get_client_memptr(wlan_client, i)->v4_addr);
1963 			CtList->HandleNeighIpAddrDelEvt(get_client_memptr(wlan_client, i)->v4_addr);
1964 		}
1965 
1966 		if (delete_default_qos_rtrules(i, IPA_IP_v4))
1967 		{
1968 			IPACMERR("unbale to delete v4 default qos route rules for index: %d\n", i);
1969 			res = IPACM_FAILURE;
1970 		}
1971 
1972 		if (delete_default_qos_rtrules(i, IPA_IP_v6))
1973 		{
1974 			IPACMERR("unbale to delete v6 default qos route rules for index: %d\n", i);
1975 			res = IPACM_FAILURE;
1976 		}
1977 
1978 		IPACMDBG_H("Delete %d client header\n", num_wifi_client);
1979 
1980 		if(get_client_memptr(wlan_client, i)->ipv4_header_set == true)
1981 		{
1982 			if (m_header.DeleteHeaderHdl(get_client_memptr(wlan_client, i)->hdr_hdl_v4)
1983 				== false)
1984 			{
1985 				res = IPACM_FAILURE;
1986 			}
1987 		}
1988 
1989 		if(get_client_memptr(wlan_client, i)->ipv6_header_set == true)
1990 		{
1991 			if (m_header.DeleteHeaderHdl(get_client_memptr(wlan_client, i)->hdr_hdl_v6)
1992 					== false)
1993 			{
1994 				res = IPACM_FAILURE;
1995 			}
1996 		}
1997 	} /* end of for loop */
1998 
1999 	/* check software routing fl rule hdl */
2000 	if (softwarerouting_act == true && rx_prop != NULL )
2001 	{
2002 		IPACMDBG_H("Delete sw routing filtering rules\n");
2003 		IPACM_Iface::handle_software_routing_disable();
2004 	}
2005 	IPACMDBG_H("finished delete software-routing filtering rules\n ");
2006 
2007 	/* Delete corresponding ipa_rm_resource_name of RX-endpoint after delete all IPV4V6 FT-rule */
2008 	if (rx_prop != NULL)
2009 	{
2010 		IPACMDBG_H("dev %s add producer dependency\n", dev_name);
2011 		IPACMDBG_H("depend Got pipe %d rm index : %d \n", rx_prop->rx[0].src_pipe, IPACM_Iface::ipacmcfg->ipa_client_rm_map_tbl[rx_prop->rx[0].src_pipe]);
2012 		IPACM_Iface::ipacmcfg->DelRmDepend(IPACM_Iface::ipacmcfg->ipa_client_rm_map_tbl[rx_prop->rx[0].src_pipe]);
2013 #ifndef FEATURE_ETH_BRIDGE_LE
2014 		free(rx_prop);
2015 #endif
2016 	}
2017 
2018 	for (i = 0; i < num_wifi_client; i++)
2019 	{
2020 		if(get_client_memptr(wlan_client, i)->p_hdr_info != NULL)
2021 		{
2022 			free(get_client_memptr(wlan_client, i)->p_hdr_info);
2023 		}
2024 	}
2025 	if(wlan_client != NULL)
2026 	{
2027 		free(wlan_client);
2028 	}
2029 #ifndef FEATURE_ETH_BRIDGE_LE
2030 	if (tx_prop != NULL)
2031 	{
2032 		free(tx_prop);
2033 	}
2034 
2035 	if (iface_query != NULL)
2036 	{
2037 		free(iface_query);
2038 	}
2039 #endif
2040 
2041 	is_active = false;
2042 	post_del_self_evt();
2043 
2044 	return res;
2045 }
2046 
2047 /*handle reset wifi-client rt-rules */
handle_wlan_client_reset_rt(ipa_ip_type iptype)2048 int IPACM_Wlan::handle_wlan_client_reset_rt(ipa_ip_type iptype)
2049 {
2050 	uint32_t i;
2051 	int res = IPACM_SUCCESS;
2052 
2053 	/* clean wifi-client routing rules */
2054 	IPACMDBG_H("left %d wifi clients to reset ip-type(%d) rules \n ", num_wifi_client, iptype);
2055 
2056 	for (i = 0; i < num_wifi_client; i++)
2057 	{
2058 		/* Reset RT rules */
2059 		res = delete_default_qos_rtrules(i, iptype);
2060 		if (res != IPACM_SUCCESS)
2061 		{
2062 			IPACMERR("Failed to delete old iptype(%d) rules.\n", iptype);
2063 			return res;
2064 		}
2065 
2066 		/* Reset ip-address */
2067 		if(iptype == IPA_IP_v4)
2068 		{
2069 			get_client_memptr(wlan_client, i)->ipv4_set = false;
2070 		}
2071 		else
2072 		{
2073 			get_client_memptr(wlan_client, i)->ipv6_set = 0;
2074 		}
2075 	} /* end of for loop */
2076 	return res;
2077 }
2078 
handle_SCC_MCC_switch(ipa_ip_type iptype)2079 void IPACM_Wlan::handle_SCC_MCC_switch(ipa_ip_type iptype)
2080 {
2081 	struct ipa_ioc_mdfy_rt_rule *rt_rule = NULL;
2082 	struct ipa_rt_rule_mdfy *rt_rule_entry;
2083 	uint32_t tx_index;
2084 	int wlan_index, v6_num;
2085 	const int NUM = 1;
2086 	int num_wifi_client_tmp = IPACM_Wlan::num_wifi_client;
2087 	bool isAdded = false;
2088 
2089 	if (tx_prop == NULL)
2090 	{
2091 		IPACMDBG_H("No rx properties registered for iface %s\n", dev_name);
2092 		return;
2093 	}
2094 
2095 	if (rt_rule == NULL)
2096 	{
2097 		rt_rule = (struct ipa_ioc_mdfy_rt_rule *)
2098 			calloc(1, sizeof(struct ipa_ioc_mdfy_rt_rule) +
2099 					NUM * sizeof(struct ipa_rt_rule_mdfy));
2100 
2101 		if (rt_rule == NULL)
2102 		{
2103 			PERROR("Error Locate ipa_ioc_mdfy_rt_rule memory...\n");
2104 			return;
2105 		}
2106 
2107 		rt_rule->commit = 0;
2108 		rt_rule->num_rules = NUM;
2109 		rt_rule->ip = iptype;
2110 	}
2111 	rt_rule_entry = &rt_rule->rules[0];
2112 
2113 	/* modify ipv4 routing rule */
2114 	if (iptype == IPA_IP_v4)
2115 	{
2116 		for (wlan_index = 0; wlan_index < num_wifi_client_tmp; wlan_index++)
2117 		{
2118 			IPACMDBG_H("wlan client index: %d, ip-type: %d, ipv4_set:%d, ipv4_rule_set:%d \n",
2119 					wlan_index, iptype,
2120 					get_client_memptr(wlan_client, wlan_index)->ipv4_set,
2121 					get_client_memptr(wlan_client, wlan_index)->route_rule_set_v4);
2122 
2123 			if (get_client_memptr(wlan_client, wlan_index)->power_save_set == true ||
2124 					get_client_memptr(wlan_client, wlan_index)->route_rule_set_v4 == false)
2125 			{
2126 				IPACMDBG_H("client %d route rules not set\n", wlan_index);
2127 				continue;
2128 			}
2129 
2130 			IPACMDBG_H("Modify client %d route rule\n", wlan_index);
2131 			for (tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
2132 			{
2133 				if (iptype != tx_prop->tx[tx_index].ip)
2134 				{
2135 					IPACMDBG_H("Tx:%d, ip-type: %d ip-type not matching: %d ignore\n",
2136 							tx_index, tx_prop->tx[tx_index].ip, iptype);
2137 					continue;
2138 				}
2139 
2140 				IPACMDBG_H("client index(%d):ipv4 address: 0x%x\n", wlan_index,
2141 						get_client_memptr(wlan_client, wlan_index)->v4_addr);
2142 
2143 				IPACMDBG_H("client(%d): v4 header handle:(0x%x)\n",
2144 						wlan_index,
2145 						get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v4);
2146 
2147 				if (IPACM_Iface::ipacmcfg->isMCC_Mode)
2148 				{
2149 					IPACMDBG_H("In MCC mode, use alt dst pipe: %d\n",
2150 							tx_prop->tx[tx_index].alt_dst_pipe);
2151 					rt_rule_entry->rule.dst = tx_prop->tx[tx_index].alt_dst_pipe;
2152 				}
2153 				else
2154 				{
2155 					rt_rule_entry->rule.dst = tx_prop->tx[tx_index].dst_pipe;
2156 				}
2157 
2158 				memcpy(&rt_rule_entry->rule.attrib,
2159 						&tx_prop->tx[tx_index].attrib,
2160 						sizeof(rt_rule_entry->rule.attrib));
2161 
2162 				rt_rule_entry->rule.attrib.attrib_mask |= IPA_FLT_DST_ADDR;
2163 				rt_rule_entry->rule.hdr_hdl = get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v4;
2164 
2165 				rt_rule_entry->rule.attrib.u.v4.dst_addr = get_client_memptr(wlan_client, wlan_index)->v4_addr;
2166 				rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF;
2167 
2168 				IPACMDBG_H("tx:%d, rt rule hdl=%x ip-type: %d\n", tx_index,
2169 						get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v4, iptype);
2170 
2171 				rt_rule_entry->rt_rule_hdl =
2172 					get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v4;
2173 
2174 				if (false == m_routing.ModifyRoutingRule(rt_rule))
2175 				{
2176 					IPACMERR("Routing rule modify failed!\n");
2177 					free(rt_rule);
2178 					return;
2179 				}
2180 				isAdded = true;
2181 			}
2182 
2183 		}
2184 	}
2185 
2186 	/* modify ipv6 routing rule */
2187 	if (iptype == IPA_IP_v6)
2188 	{
2189 		for (wlan_index = 0; wlan_index < num_wifi_client_tmp; wlan_index++)
2190 		{
2191 
2192 			IPACMDBG_H("wlan client index: %d, ip-type: %d, ipv6_set:%d, ipv6_rule_num:%d \n", wlan_index, iptype,
2193 					get_client_memptr(wlan_client, wlan_index)->ipv6_set,
2194 					get_client_memptr(wlan_client, wlan_index)->route_rule_set_v6);
2195 
2196 			if (get_client_memptr(wlan_client, wlan_index)->power_save_set == true ||
2197 					(get_client_memptr(wlan_client, wlan_index)->route_rule_set_v6 <
2198 					 get_client_memptr(wlan_client, wlan_index)->ipv6_set) )
2199 			{
2200 				IPACMDBG_H("client %d route rules not set\n", wlan_index);
2201 				continue;
2202 			}
2203 
2204 			IPACMDBG_H("Modify client %d route rule\n", wlan_index);
2205 			for (tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
2206 			{
2207 				if (iptype != tx_prop->tx[tx_index].ip)
2208 				{
2209 					IPACMDBG_H("Tx:%d, ip-type: %d ip-type not matching: %d Ignore\n",
2210 							tx_index, tx_prop->tx[tx_index].ip, iptype);
2211 					continue;
2212 				}
2213 
2214 				for (v6_num = get_client_memptr(wlan_client, wlan_index)->route_rule_set_v6;
2215 						v6_num < get_client_memptr(wlan_client, wlan_index)->ipv6_set;
2216 						v6_num++)
2217 				{
2218 
2219 					IPACMDBG_H("client(%d): v6 header handle:(0x%x)\n",
2220 							wlan_index,
2221 							get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v6);
2222 
2223 					if (IPACM_Iface::ipacmcfg->isMCC_Mode)
2224 					{
2225 						IPACMDBG_H("In MCC mode, use alt dst pipe: %d\n",
2226 								tx_prop->tx[tx_index].alt_dst_pipe);
2227 						rt_rule_entry->rule.dst = tx_prop->tx[tx_index].alt_dst_pipe;
2228 					}
2229 					else
2230 					{
2231 						rt_rule_entry->rule.dst = tx_prop->tx[tx_index].dst_pipe;
2232 					}
2233 
2234 					memcpy(&rt_rule_entry->rule.attrib,
2235 							&tx_prop->tx[tx_index].attrib,
2236 							sizeof(rt_rule_entry->rule.attrib));
2237 
2238 					rt_rule_entry->rule.hdr_hdl = get_client_memptr(wlan_client, wlan_index)->hdr_hdl_v6;
2239 					rt_rule_entry->rule.attrib.attrib_mask |= IPA_FLT_DST_ADDR;
2240 
2241 					rt_rule_entry->rule.attrib.u.v6.dst_addr[0] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][0];
2242 					rt_rule_entry->rule.attrib.u.v6.dst_addr[1] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][1];
2243 					rt_rule_entry->rule.attrib.u.v6.dst_addr[2] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][2];
2244 					rt_rule_entry->rule.attrib.u.v6.dst_addr[3] = get_client_memptr(wlan_client, wlan_index)->v6_addr[v6_num][3];
2245 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[0] = 0xFFFFFFFF;
2246 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[1] = 0xFFFFFFFF;
2247 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[2] = 0xFFFFFFFF;
2248 					rt_rule_entry->rule.attrib.u.v6.dst_addr_mask[3] = 0xFFFFFFFF;
2249 
2250 					rt_rule_entry->rt_rule_hdl =
2251 						get_client_memptr(wlan_client, wlan_index)->wifi_rt_hdl[tx_index].wifi_rt_rule_hdl_v6_wan[v6_num];
2252 
2253 					if (false == m_routing.ModifyRoutingRule(rt_rule))
2254 					{
2255 						IPACMERR("Routing rule modify failed!\n");
2256 						free(rt_rule);
2257 						return;
2258 					}
2259 					isAdded = true;
2260 				}
2261 			}
2262 
2263 		}
2264 	}
2265 
2266 
2267 	if (isAdded)
2268 	{
2269 		if (false == m_routing.Commit(iptype))
2270 		{
2271 			IPACMERR("Routing rule modify commit failed!\n");
2272 			free(rt_rule);
2273 			return;
2274 		}
2275 
2276 		IPACMDBG("Routing rule modified successfully \n");
2277 	}
2278 
2279 	if(rt_rule)
2280 	{
2281 		free(rt_rule);
2282 	}
2283 	return;
2284 }
2285 
ipa_query_wlan_client()2286 int IPACM_Wlan::ipa_query_wlan_client()
2287 {
2288 	int fd = -1;
2289 
2290 	if ((fd = open(IPA_DEVICE_NAME, O_RDWR)) < 0) {
2291 		IPACMERR("Failed opening %s.\n", IPA_DEVICE_NAME);
2292 		return IPACM_FAILURE;
2293 	}
2294 
2295 	if (ioctl(fd, IPA_IOC_QUERY_WLAN_CLIENT) < 0) {
2296 		IPACMERR("IOCTL IPA_IOC_QUERY_WLAN_CLIENT call failed: %s \n", strerror(errno));
2297 		close(fd);
2298 		return IPACM_FAILURE;
2299 	}
2300 
2301 	IPACMDBG_H("send IPA_IOC_QUERY_WLAN_CLIENT \n");
2302 	close(fd);
2303 	return IPACM_SUCCESS;
2304 }
2305 
eth_bridge_handle_wlan_mode_switch()2306 void IPACM_Wlan::eth_bridge_handle_wlan_mode_switch()
2307 {
2308 	uint32_t i;
2309 
2310 	/* ====== post events to mimic WLAN interface goes down/up when AP mode is changing ====== */
2311 
2312 	/* first post IFACE_DOWN event */
2313 	eth_bridge_post_event(IPA_ETH_BRIDGE_IFACE_DOWN, IPA_IP_MAX, NULL, NULL, NULL);
2314 
2315 	/* then post IFACE_UP event */
2316 	if(ip_type == IPA_IP_v4 || ip_type == IPA_IP_MAX)
2317 	{
2318 		eth_bridge_post_event(IPA_ETH_BRIDGE_IFACE_UP, IPA_IP_v4, NULL, NULL, NULL);
2319 	}
2320 	if(ip_type == IPA_IP_v6 || ip_type == IPA_IP_MAX)
2321 	{
2322 		eth_bridge_post_event(IPA_ETH_BRIDGE_IFACE_UP, IPA_IP_v6, NULL, NULL, NULL);
2323 	}
2324 
2325 	/* at last post CLIENT_ADD event */
2326 	for(i = 0; i < num_wifi_client; i++)
2327 	{
2328 		eth_bridge_post_event(IPA_ETH_BRIDGE_CLIENT_ADD, IPA_IP_MAX,
2329 			get_client_memptr(wlan_client, i)->mac, NULL, NULL);
2330 	}
2331 
2332 	return;
2333 }
2334 
is_guest_ap()2335 bool IPACM_Wlan::is_guest_ap()
2336 {
2337 	return m_is_guest_ap;
2338 }
2339