1 /*
2 Copyright (c) 2013-2019, 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_Lan.h
32 
33 	@brief
34 	This file implements the LAN iface definitions
35 
36 	@Author
37 	Skylar Chang
38 
39 */
40 #ifndef IPACM_LAN_H
41 #define IPACM_LAN_H
42 
43 #include <stdio.h>
44 #include <linux/msm_ipa.h>
45 
46 #include "IPACM_CmdQueue.h"
47 #include "IPACM_Iface.h"
48 #include "IPACM_Routing.h"
49 #include "IPACM_Filtering.h"
50 #include "IPACM_Config.h"
51 #include "IPACM_Conntrack_NATApp.h"
52 #include "IPACM_Wan.h"
53 
54 #define IPA_WAN_DEFAULT_FILTER_RULE_HANDLES  1
55 #define IPA_PRIV_SUBNET_FILTER_RULE_HANDLES  3
56 #define IPA_NUM_ODU_ROUTE_RULES 2
57 #define MAX_WAN_UL_FILTER_RULES MAX_NUM_EXT_PROPS
58 #define NUM_IPV4_ICMP_FLT_RULE 1
59 #define NUM_IPV6_ICMP_FLT_RULE 1
60 
61 /* ndc bandwidth ipatetherstats <ifaceIn> <ifaceOut> */
62 /* <in->out_bytes> <in->out_pkts> <out->in_bytes> <out->in_pkts */
63 
64 #define PIPE_STATS "%s %s %llu %llu %llu %llu"
65 #define IPA_PIPE_STATS_FILE_NAME "/data/misc/ipa/tether_stats"
66 
67 /* store each lan-iface unicast routing rule and its handler*/
68 struct ipa_lan_rt_rule
69 {
70 	ipa_ip_type ip;
71 	uint32_t v4_addr;
72 	uint32_t v4_addr_mask;
73 	uint32_t v6_addr[4];
74 	uint32_t rt_rule_hdl[0];
75 };
76 
77 /* Support multiple eth client */
78 typedef struct _eth_client_rt_hdl
79 {
80 	uint32_t eth_rt_rule_hdl_v4;
81 	uint32_t eth_rt_rule_hdl_v6[IPV6_NUM_ADDR];
82 	uint32_t eth_rt_rule_hdl_v6_wan[IPV6_NUM_ADDR];
83 }eth_client_rt_hdl;
84 
85 typedef struct _ipa_eth_client
86 {
87 	uint8_t mac[IPA_MAC_ADDR_SIZE];
88 	uint32_t v4_addr;
89 	uint32_t v6_addr[IPV6_NUM_ADDR][4];
90 	uint32_t hdr_hdl_v4;
91 	uint32_t hdr_hdl_v6;
92 	bool route_rule_set_v4;
93 	int route_rule_set_v6;
94 	bool ipv4_set;
95 	int ipv6_set;
96 	bool ipv4_header_set;
97 	bool ipv6_header_set;
98 	/* used for pcie-modem */
99 	uint32_t v6_rt_rule_id[IPV6_NUM_ADDR];
100 	eth_client_rt_hdl eth_rt_hdl[0]; /* depends on number of tx properties */
101 }ipa_eth_client;
102 
103 
104 /* lan iface */
105 class IPACM_Lan : public IPACM_Iface
106 {
107 public:
108 
109 	IPACM_Lan(int iface_index);
110 	~IPACM_Lan();
111 
112 	/* store lan's wan-up filter rule handlers */
113 	uint32_t lan_wan_fl_rule_hdl[IPA_WAN_DEFAULT_FILTER_RULE_HANDLES];
114 
115 	/* store private-subnet filter rule handlers */
116 	uint32_t private_fl_rule_hdl[IPA_MAX_PRIVATE_SUBNET_ENTRIES + IPA_MAX_MTU_ENTRIES];
117 
118 	/* LAN-iface's callback function */
119 	void event_callback(ipa_cm_event_id event, void *data);
120 
121 	virtual int handle_wan_up(ipa_ip_type ip_type);
122 
123 	/* configure filter rule for wan_up event*/
124 	virtual int handle_wan_up_ex(ipacm_ext_prop* ext_prop, ipa_ip_type iptype, uint8_t xlat_mux_id);
125 
126 	/* delete filter rule for wan_down event*/
127 	virtual int handle_wan_down(ipacm_wan_iface_type backhaul_mode);
128 
129 	/* delete filter rule for wan_down event*/
130 	virtual int handle_wan_down_v6(ipacm_wan_iface_type backhaul_mode);
131 
132 	/* configure private subnet filter rules*/
133 	virtual int handle_private_subnet(ipa_ip_type iptype);
134 
135 	/* handle new_address event*/
136 	int handle_addr_evt(ipacm_event_data_addr *data);
137 
138 	int handle_addr_evt_odu_bridge(ipacm_event_data_addr* data);
139 
140 	int handle_del_ipv6_addr(ipacm_event_data_all *data);
141 
142 	static bool odu_up;
143 
144 	/* install UL filter rule from Q6 */
145 	virtual int handle_uplink_filter_rule(ipacm_ext_prop* prop, ipa_ip_type iptype, uint8_t xlat_mux_id);
146 
147 	int handle_cradle_wan_mode_switch(bool is_wan_bridge_mode);
148 
149 	int install_ipv4_icmp_flt_rule();
150 
151 
152 	/* add header processing context and return handle to lan2lan controller */
153 	int eth_bridge_add_hdr_proc_ctx(ipa_hdr_l2_type peer_l2_hdr_type, uint32_t *hdl);
154 
155 	/* add routing rule and return handle to lan2lan controller */
156 	int eth_bridge_add_rt_rule(uint8_t *mac, char *rt_tbl_name, uint32_t hdr_proc_ctx_hdl,
157 		ipa_hdr_l2_type peer_l2_hdr_type, ipa_ip_type iptype, uint32_t *rt_rule_hdl, int *rt_rule_count, int ep);
158 
159 	/* modify routing rule*/
160 	int eth_bridge_modify_rt_rule(uint8_t *mac, uint32_t hdr_proc_ctx_hdl,
161 		ipa_hdr_l2_type peer_l2_hdr_type, ipa_ip_type iptype, uint32_t *rt_rule_hdl, int rt_rule_count);
162 
163 	/* add filtering rule and return handle to lan2lan controller */
164 	int eth_bridge_add_flt_rule(uint8_t *mac, uint32_t rt_tbl_hdl, ipa_ip_type iptype, uint32_t *flt_rule_hdl);
165 
166 	/* delete filtering rule */
167 	int eth_bridge_del_flt_rule(uint32_t flt_rule_hdl, ipa_ip_type iptype);
168 
169 	/* delete routing rule */
170 	int eth_bridge_del_rt_rule(uint32_t rt_rule_hdl, ipa_ip_type iptype);
171 
172 	/* delete header processing context */
173 	int eth_bridge_del_hdr_proc_ctx(uint32_t hdr_proc_ctx_hdl);
174 
175 #ifdef FEATURE_L2TP
176 	/* add l2tp rt rule for l2tp client */
177 	int add_l2tp_rt_rule(ipa_ip_type iptype, uint8_t *dst_mac, ipa_hdr_l2_type peer_l2_hdr_type,
178 		uint32_t l2tp_session_id, uint32_t vlan_id, uint8_t *vlan_client_mac, uint32_t *vlan_iface_ipv6_addr,
179 		uint32_t *vlan_client_ipv6_addr, uint32_t *first_pass_hdr_hdl, uint32_t *first_pass_hdr_proc_ctx_hdl,
180 		uint32_t *second_pass_hdr_hdl, int *num_rt_hdl, uint32_t *first_pass_rt_rule_hdl, uint32_t *second_pass_rt_rule_hdl);
181 
182 	/* delete l2tp rt rule for l2tp client */
183 	int del_l2tp_rt_rule(ipa_ip_type iptype, uint32_t first_pass_hdr_hdl, uint32_t first_pass_hdr_proc_ctx_hdl,
184 		uint32_t second_pass_hdr_hdl, int num_rt_hdl, uint32_t *first_pass_rt_rule_hdl, uint32_t *second_pass_rt_rule_hdl);
185 
186 	/* add l2tp rt rule for non l2tp client */
187 	int add_l2tp_rt_rule(ipa_ip_type iptype, uint8_t *dst_mac, uint32_t *hdr_proc_ctx_hdl,
188 		int *num_rt_hdl, uint32_t *rt_rule_hdl);
189 
190 	/* delete l2tp rt rule for non l2tp client */
191 	int del_l2tp_rt_rule(ipa_ip_type iptype, int num_rt_hdl, uint32_t *rt_rule_hdl);
192 
193 	/* add l2tp flt rule on l2tp interface */
194 	int add_l2tp_flt_rule(uint8_t *dst_mac, uint32_t *flt_rule_hdl);
195 
196 	/* delete l2tp flt rule on l2tp interface */
197 	int del_l2tp_flt_rule(uint32_t flt_rule_hdl);
198 
199 	/* add l2tp flt rule on non l2tp interface */
200 	int add_l2tp_flt_rule(ipa_ip_type iptype, uint8_t *dst_mac, uint32_t *vlan_client_ipv6_addr,
201 		uint32_t *first_pass_flt_rule_hdl, uint32_t *second_pass_flt_rule_hdl);
202 
203 	/* delete l2tp flt rule on non l2tp interface */
204 	int del_l2tp_flt_rule(ipa_ip_type iptype, uint32_t first_pass_flt_rule_hdl, uint32_t second_pass_flt_rule_hdl);
205 #endif
206 
207 protected:
208 
209 	int each_client_rt_rule_count[IPA_IP_MAX];
210 
211 	uint32_t eth_bridge_flt_rule_offset[IPA_IP_MAX];
212 
213 	/* mac address has to be provided for client related events */
214 	void eth_bridge_post_event(ipa_cm_event_id evt, ipa_ip_type iptype, uint8_t *mac,
215 		uint32_t *ipv6_addr, char *iface_name, int ep);
216 
217 #ifdef FEATURE_L2TP
218 	/* check if the event is associated with vlan interface */
219 	bool is_vlan_event(char *event_iface_name);
220 	/* check if the event is associated with l2tp interface */
221 	bool is_l2tp_event(char *event_iface_name);
222 
223 	/* check if the IPv6 address is unique local address */
224 	bool is_unique_local_ipv6_addr(uint32_t *ipv6_addr);
225 
226 #endif
227 	virtual int add_dummy_private_subnet_flt_rule(ipa_ip_type iptype);
228 
229 	int handle_private_subnet_android(ipa_ip_type iptype);
230 
231 	int reset_to_dummy_flt_rule(ipa_ip_type iptype, uint32_t rule_hdl);
232 
233 	virtual int modify_ipv6_prefix_flt_rule(uint32_t* prefix);
234 
235 	virtual int install_ipv6_prefix_flt_rule(uint32_t* prefix);
236 
237 	virtual void delete_ipv6_prefix_flt_rule();
238 
239 	int install_ipv6_icmp_flt_rule();
240 
241 	void post_del_self_evt();
242 
243 	/* handle tethering stats */
244 	int handle_tethering_stats_event(ipa_get_data_stats_resp_msg_v01 *data);
245 
246 	/* handle tethering client */
247 	int handle_tethering_client(bool reset, ipacm_client_enum ipa_client);
248 
249 	/* add tcp syn flt rule */
250 	int add_tcp_syn_flt_rule(ipa_ip_type iptype);
251 
252 	/* add tcp syn flt rule for l2tp interface*/
253 	int add_tcp_syn_flt_rule_l2tp(ipa_ip_type inner_ip_type);
254 
255 	/* store ipv4 UL filter rule handlers from Q6*/
256 	uint32_t wan_ul_fl_rule_hdl_v4[MAX_WAN_UL_FILTER_RULES];
257 
258 	/* store ipv6 UL filter rule handlers from Q6*/
259 	uint32_t wan_ul_fl_rule_hdl_v6[MAX_WAN_UL_FILTER_RULES];
260 
261 	uint32_t ipv4_icmp_flt_rule_hdl[NUM_IPV4_ICMP_FLT_RULE];
262 
263 	uint32_t ipv6_prefix_flt_rule_hdl[NUM_IPV6_PREFIX_FLT_RULE + NUM_IPV6_PREFIX_MTU_RULE];
264 	uint32_t ipv6_icmp_flt_rule_hdl[NUM_IPV6_ICMP_FLT_RULE];
265 
266 	int num_wan_ul_fl_rule_v4;
267 	int num_wan_ul_fl_rule_v6;
268 
269 	bool is_active;
270 	bool modem_ul_v4_set;
271 	uint8_t v4_mux_id;
272 	bool modem_ul_v6_set;
273 	uint8_t v6_mux_id;
274 
275 	bool sta_ul_v4_set;
276 	bool sta_ul_v6_set;
277 
278 	uint32_t if_ipv4_subnet;
279 
280 	uint32_t ipv6_prefix[2];
281 
282 	bool is_upstream_set[IPA_IP_MAX];
283 	bool is_downstream_set[IPA_IP_MAX];
284 	_ipacm_offload_prefix prefix[IPA_IP_MAX];
285 
286 	uint32_t tcp_syn_flt_rule_hdl[IPA_IP_MAX];
287 
288 private:
289 
290 	int set_client_pipe(enum ipa_client_type client, uint32_t *pipe);
291 	int set_tether_client(wan_ioctl_set_tether_client_pipe *tether_client);
292 	int set_tether_client_wigig(wan_ioctl_set_tether_client_pipe *tether_client);
293 
294 	/* get hdr proc ctx type given source and destination l2 hdr type */
295 	ipa_hdr_proc_type eth_bridge_get_hdr_proc_type(ipa_hdr_l2_type t1, ipa_hdr_l2_type t2);
296 
297 	/* get partial header (header template of hdr proc ctx) */
298 	int eth_bridge_get_hdr_template_hdl(uint32_t* hdr_hdl);
299 
300 
301 	/* dynamically allocate lan iface's unicast routing rule structure */
302 
303 	bool is_mode_switch; /* indicate mode switch, need post internal up event */
304 
305 	int eth_client_len;
306 
307 	ipa_eth_client *eth_client;
308 
309 	int header_name_count;
310 
311 	uint32_t num_eth_client;
312 
313 	NatApp *Nat_App;
314 
315 	int ipv6_set;
316 
317 	uint32_t ODU_hdr_hdl_v4, ODU_hdr_hdl_v6;
318 
319 	uint32_t *odu_route_rule_v4_hdl;
320 
321 	uint32_t *odu_route_rule_v6_hdl;
322 
323 	bool ipv4_header_set;
324 
325 	bool ipv6_header_set;
326 
get_client_memptr(ipa_eth_client * param,int cnt)327 	inline ipa_eth_client* get_client_memptr(ipa_eth_client *param, int cnt)
328 	{
329 	    char *ret = ((char *)param) + (eth_client_len * cnt);
330 		return (ipa_eth_client *)ret;
331 	}
332 
get_eth_client_index(uint8_t * mac_addr)333 	inline int get_eth_client_index(uint8_t *mac_addr)
334 	{
335 		int cnt;
336 		int num_eth_client_tmp = num_eth_client;
337 
338 		IPACMDBG_H("Passed MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
339 						 mac_addr[0], mac_addr[1], mac_addr[2],
340 						 mac_addr[3], mac_addr[4], mac_addr[5]);
341 
342 		for(cnt = 0; cnt < num_eth_client_tmp; cnt++)
343 		{
344 			IPACMDBG_H("stored MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
345 							 get_client_memptr(eth_client, cnt)->mac[0],
346 							 get_client_memptr(eth_client, cnt)->mac[1],
347 							 get_client_memptr(eth_client, cnt)->mac[2],
348 							 get_client_memptr(eth_client, cnt)->mac[3],
349 							 get_client_memptr(eth_client, cnt)->mac[4],
350 							 get_client_memptr(eth_client, cnt)->mac[5]);
351 
352 			if(memcmp(get_client_memptr(eth_client, cnt)->mac,
353 								mac_addr,
354 								sizeof(get_client_memptr(eth_client, cnt)->mac)) == 0)
355 			{
356 				IPACMDBG_H("Matched client index: %d\n", cnt);
357 				return cnt;
358 			}
359 		}
360 
361 		return IPACM_INVALID_INDEX;
362 	}
363 
delete_eth_rtrules(int clt_indx,ipa_ip_type iptype)364 	inline int delete_eth_rtrules(int clt_indx, ipa_ip_type iptype)
365 	{
366 		uint32_t tx_index;
367 		uint32_t rt_hdl;
368 		int num_v6;
369 
370 		if(iptype == IPA_IP_v4)
371 		{
372 		    for(tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
373 		    {
374 		        if((tx_prop->tx[tx_index].ip == IPA_IP_v4) && (get_client_memptr(eth_client, clt_indx)->route_rule_set_v4==true)) /* for ipv4 */
375 				{
376 					IPACMDBG_H("Delete client index %d ipv4 RT-rules for tx:%d\n",clt_indx,tx_index);
377 					rt_hdl = get_client_memptr(eth_client, clt_indx)->eth_rt_hdl[tx_index].eth_rt_rule_hdl_v4;
378 
379 					if(m_routing.DeleteRoutingHdl(rt_hdl, IPA_IP_v4) == false)
380 					{
381 						return IPACM_FAILURE;
382 					}
383 				}
384 		    } /* end of for loop */
385 
386 		     /* clean the ipv4 RT rules for eth-client:clt_indx */
387 		     if(get_client_memptr(eth_client, clt_indx)->route_rule_set_v4==true) /* for ipv4 */
388 		     {
389 				get_client_memptr(eth_client, clt_indx)->route_rule_set_v4 = false;
390 		     }
391 		}
392 
393 		if(iptype == IPA_IP_v6)
394 		{
395 			for(tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
396 			{
397 				if((tx_prop->tx[tx_index].ip == IPA_IP_v6) && (get_client_memptr(eth_client, clt_indx)->route_rule_set_v6 != 0)) /* for ipv6 */
398 				{
399 					for(num_v6 =0;num_v6 < get_client_memptr(eth_client, clt_indx)->route_rule_set_v6;num_v6++)
400 					{
401 						/* send client-v6 delete to pcie modem only with global ipv6 with tx_index = 1 one time*/
402 						if(is_global_ipv6_addr(get_client_memptr(eth_client, clt_indx)->v6_addr[num_v6]) && (IPACM_Wan::backhaul_mode == Q6_MHI_WAN)
403 							&& (get_client_memptr(eth_client, clt_indx)->v6_rt_rule_id[num_v6] > 0))
404 						{
405 							IPACMDBG_H("Delete client index %d ipv6 RT-rules for %d-st ipv6 for rule-id:%d\n", clt_indx,num_v6,
406 								get_client_memptr(eth_client, clt_indx)->v6_rt_rule_id[num_v6]);
407 							if (del_connection(clt_indx, num_v6))
408 							{
409 								IPACMERR("PCIE filter rule deletion failed! (%d-client) %d v6-entry\n",clt_indx, num_v6);
410 							}
411 						}
412 
413 						IPACMDBG_H("Delete client index %d ipv6 RT-rules for %d-st ipv6 for tx:%d\n", clt_indx,num_v6,tx_index);
414 						rt_hdl = get_client_memptr(eth_client, clt_indx)->eth_rt_hdl[tx_index].eth_rt_rule_hdl_v6[num_v6];
415 						if(m_routing.DeleteRoutingHdl(rt_hdl, IPA_IP_v6) == false)
416 							{
417 								return IPACM_FAILURE;
418 							}
419 
420 							rt_hdl = get_client_memptr(eth_client, clt_indx)->eth_rt_hdl[tx_index].eth_rt_rule_hdl_v6_wan[num_v6];
421 							if(m_routing.DeleteRoutingHdl(rt_hdl, IPA_IP_v6) == false)
422 							{
423 								return IPACM_FAILURE;
424 							}
425 						}
426                     }
427 		    } /* end of for loop */
428 
429 		    /* clean the ipv6 RT rules for eth-client:clt_indx */
430 		    if(get_client_memptr(eth_client, clt_indx)->route_rule_set_v6 != 0) /* for ipv6 */
431 		    {
432 		        get_client_memptr(eth_client, clt_indx)->route_rule_set_v6 = 0;
433             }
434 		}
435 
436 		return IPACM_SUCCESS;
437 	}
438 
439 	/* handle eth client initial, construct full headers (tx property) */
440 	int handle_eth_hdr_init(uint8_t *mac_addr);
441 
442 	/* handle eth client ip-address */
443 	int handle_eth_client_ipaddr(ipacm_event_data_all *data);
444 
445 	/* handle eth client routing rule*/
446 	int handle_eth_client_route_rule(uint8_t *mac_addr, ipa_ip_type iptype);
447 
448 	/*handle eth client del mode*/
449 	int handle_eth_client_down_evt(uint8_t *mac_addr);
450 
451 	/* handle odu client initial, construct full headers (tx property) */
452 	int handle_odu_hdr_init(uint8_t *mac_addr);
453 
454 	/* handle odu default route rule configuration */
455 	int handle_odu_route_add();
456 
457 	/* handle odu default route rule deletion */
458 	int handle_odu_route_del();
459 
460 	/*handle lan iface down event*/
461 	int handle_down_evt();
462 
463 	/*handle reset usb-client rt-rules */
464 	int handle_lan_client_reset_rt(ipa_ip_type iptype);
465 
466 	/* for pcie modem */
467 	virtual int add_connection(int client_index, int v6_num);
468 	virtual int del_connection(int client_index, int v6_num);
469 
470 	int construct_mtu_rule(struct ipa_flt_rule *rule, enum ipa_ip_type iptype, uint16_t mtu);
471 };
472 
473 #endif /* IPACM_LAN_H */
474