1 /*
2 Copyright (c) 2013-2016, 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 
173 
174 protected:
175 
176 	int each_client_rt_rule_count[IPA_IP_MAX];
177 
178 	uint32_t eth_bridge_flt_rule_offset[IPA_IP_MAX];
179 
180 	/* mac address has to be provided for client related events */
181 	void eth_bridge_post_event(ipa_cm_event_id evt, ipa_ip_type iptype, uint8_t *mac);
182 
183 
184 
185 	virtual int add_dummy_private_subnet_flt_rule(ipa_ip_type iptype);
186 
187 	int handle_private_subnet_android(ipa_ip_type iptype);
188 
189 	int reset_to_dummy_flt_rule(ipa_ip_type iptype, uint32_t rule_hdl);
190 
191 	virtual int install_ipv6_prefix_flt_rule(uint32_t* prefix);
192 
193 	virtual void delete_ipv6_prefix_flt_rule();
194 
195 	int install_ipv6_icmp_flt_rule();
196 
197 	void post_del_self_evt();
198 
199 	/* handle tethering stats */
200 	int handle_tethering_stats_event(ipa_get_data_stats_resp_msg_v01 *data);
201 
202 	/* handle tethering client */
203 	int handle_tethering_client(bool reset, ipacm_client_enum ipa_client);
204 
205 	/* store ipv4 UL filter rule handlers from Q6*/
206 	uint32_t wan_ul_fl_rule_hdl_v4[MAX_WAN_UL_FILTER_RULES];
207 
208 	/* store ipv6 UL filter rule handlers from Q6*/
209 	uint32_t wan_ul_fl_rule_hdl_v6[MAX_WAN_UL_FILTER_RULES];
210 
211 	uint32_t ipv4_icmp_flt_rule_hdl[NUM_IPV4_ICMP_FLT_RULE];
212 
213 	uint32_t ipv6_prefix_flt_rule_hdl[NUM_IPV6_PREFIX_FLT_RULE];
214 	uint32_t ipv6_icmp_flt_rule_hdl[NUM_IPV6_ICMP_FLT_RULE];
215 
216 	int num_wan_ul_fl_rule_v4;
217 	int num_wan_ul_fl_rule_v6;
218 
219 	bool is_active;
220 	bool modem_ul_v4_set;
221 	bool modem_ul_v6_set;
222 
223 	uint32_t if_ipv4_subnet;
224 
225 	uint32_t ipv6_prefix[2];
226 
227 	bool is_upstream_set[IPA_IP_MAX];
228 	bool is_downstream_set[IPA_IP_MAX];
229 	_ipacm_offload_prefix prefix[IPA_IP_MAX];
230 
231 private:
232 
233 	/* get hdr proc ctx type given source and destination l2 hdr type */
234 	ipa_hdr_proc_type eth_bridge_get_hdr_proc_type(ipa_hdr_l2_type t1, ipa_hdr_l2_type t2);
235 
236 	/* get partial header (header template of hdr proc ctx) */
237 	int eth_bridge_get_hdr_template_hdl(uint32_t* hdr_hdl);
238 
239 
240 	/* dynamically allocate lan iface's unicast routing rule structure */
241 
242 	bool is_mode_switch; /* indicate mode switch, need post internal up event */
243 
244 	int eth_client_len;
245 
246 	ipa_eth_client *eth_client;
247 
248 	int header_name_count;
249 
250 	uint32_t num_eth_client;
251 
252 	NatApp *Nat_App;
253 
254 	int ipv6_set;
255 
256 	uint32_t ODU_hdr_hdl_v4, ODU_hdr_hdl_v6;
257 
258 	uint32_t *odu_route_rule_v4_hdl;
259 
260 	uint32_t *odu_route_rule_v6_hdl;
261 
262 	bool ipv4_header_set;
263 
264 	bool ipv6_header_set;
265 
get_client_memptr(ipa_eth_client * param,int cnt)266 	inline ipa_eth_client* get_client_memptr(ipa_eth_client *param, int cnt)
267 	{
268 	    char *ret = ((char *)param) + (eth_client_len * cnt);
269 		return (ipa_eth_client *)ret;
270 	}
271 
get_eth_client_index(uint8_t * mac_addr)272 	inline int get_eth_client_index(uint8_t *mac_addr)
273 	{
274 		int cnt;
275 		int num_eth_client_tmp = num_eth_client;
276 
277 		IPACMDBG_H("Passed MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
278 						 mac_addr[0], mac_addr[1], mac_addr[2],
279 						 mac_addr[3], mac_addr[4], mac_addr[5]);
280 
281 		for(cnt = 0; cnt < num_eth_client_tmp; cnt++)
282 		{
283 			IPACMDBG_H("stored MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
284 							 get_client_memptr(eth_client, cnt)->mac[0],
285 							 get_client_memptr(eth_client, cnt)->mac[1],
286 							 get_client_memptr(eth_client, cnt)->mac[2],
287 							 get_client_memptr(eth_client, cnt)->mac[3],
288 							 get_client_memptr(eth_client, cnt)->mac[4],
289 							 get_client_memptr(eth_client, cnt)->mac[5]);
290 
291 			if(memcmp(get_client_memptr(eth_client, cnt)->mac,
292 								mac_addr,
293 								sizeof(get_client_memptr(eth_client, cnt)->mac)) == 0)
294 			{
295 				IPACMDBG_H("Matched client index: %d\n", cnt);
296 				return cnt;
297 			}
298 		}
299 
300 		return IPACM_INVALID_INDEX;
301 	}
302 
delete_eth_rtrules(int clt_indx,ipa_ip_type iptype)303 	inline int delete_eth_rtrules(int clt_indx, ipa_ip_type iptype)
304 	{
305 		uint32_t tx_index;
306 		uint32_t rt_hdl;
307 		int num_v6;
308 
309 		if(iptype == IPA_IP_v4)
310 		{
311 		    for(tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
312 		    {
313 		        if((tx_prop->tx[tx_index].ip == IPA_IP_v4) && (get_client_memptr(eth_client, clt_indx)->route_rule_set_v4==true)) /* for ipv4 */
314 				{
315 					IPACMDBG_H("Delete client index %d ipv4 RT-rules for tx:%d\n",clt_indx,tx_index);
316 					rt_hdl = get_client_memptr(eth_client, clt_indx)->eth_rt_hdl[tx_index].eth_rt_rule_hdl_v4;
317 
318 					if(m_routing.DeleteRoutingHdl(rt_hdl, IPA_IP_v4) == false)
319 					{
320 						return IPACM_FAILURE;
321 					}
322 				}
323 		    } /* end of for loop */
324 
325 		     /* clean the ipv4 RT rules for eth-client:clt_indx */
326 		     if(get_client_memptr(eth_client, clt_indx)->route_rule_set_v4==true) /* for ipv4 */
327 		     {
328 				get_client_memptr(eth_client, clt_indx)->route_rule_set_v4 = false;
329 		     }
330 		}
331 
332 		if(iptype == IPA_IP_v6)
333 		{
334 			for(tx_index = 0; tx_index < iface_query->num_tx_props; tx_index++)
335 			{
336 				if((tx_prop->tx[tx_index].ip == IPA_IP_v6) && (get_client_memptr(eth_client, clt_indx)->route_rule_set_v6 != 0)) /* for ipv6 */
337 				{
338 					for(num_v6 =0;num_v6 < get_client_memptr(eth_client, clt_indx)->route_rule_set_v6;num_v6++)
339 					{
340 						IPACMDBG_H("Delete client index %d ipv6 RT-rules for %d-st ipv6 for tx:%d\n", clt_indx,num_v6,tx_index);
341 						rt_hdl = get_client_memptr(eth_client, clt_indx)->eth_rt_hdl[tx_index].eth_rt_rule_hdl_v6[num_v6];
342 						if(m_routing.DeleteRoutingHdl(rt_hdl, IPA_IP_v6) == false)
343 							{
344 								return IPACM_FAILURE;
345 							}
346 
347 							rt_hdl = get_client_memptr(eth_client, clt_indx)->eth_rt_hdl[tx_index].eth_rt_rule_hdl_v6_wan[num_v6];
348 							if(m_routing.DeleteRoutingHdl(rt_hdl, IPA_IP_v6) == false)
349 							{
350 								return IPACM_FAILURE;
351 							}
352 						}
353                     }
354 		    } /* end of for loop */
355 
356 		    /* clean the ipv6 RT rules for eth-client:clt_indx */
357 		    if(get_client_memptr(eth_client, clt_indx)->route_rule_set_v6 != 0) /* for ipv6 */
358 		    {
359 		        get_client_memptr(eth_client, clt_indx)->route_rule_set_v6 = 0;
360             }
361 		}
362 
363 		return IPACM_SUCCESS;
364 	}
365 
366 	/* handle eth client initial, construct full headers (tx property) */
367 	int handle_eth_hdr_init(uint8_t *mac_addr);
368 
369 	/* handle eth client ip-address */
370 	int handle_eth_client_ipaddr(ipacm_event_data_all *data);
371 
372 	/* handle eth client routing rule*/
373 	int handle_eth_client_route_rule(uint8_t *mac_addr, ipa_ip_type iptype);
374 
375 	/*handle eth client del mode*/
376 	int handle_eth_client_down_evt(uint8_t *mac_addr);
377 
378 	/* handle odu client initial, construct full headers (tx property) */
379 	int handle_odu_hdr_init(uint8_t *mac_addr);
380 
381 	/* handle odu default route rule configuration */
382 	int handle_odu_route_add();
383 
384 	/* handle odu default route rule deletion */
385 	int handle_odu_route_del();
386 
387 	/*handle lan iface down event*/
388 	int handle_down_evt();
389 
390 	/*handle reset usb-client rt-rules */
391 	int handle_lan_client_reset_rt(ipa_ip_type iptype);
392 };
393 
394 #endif /* IPACM_LAN_H */
395