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