blob: 7f137fcd99cf644ea3a2a33341d807b817d516a7 [file] [log] [blame]
Ben Menchaca84f36632014-02-28 20:57:38 +00001/*
2 **************************************************************************
Murat Sezgined9c1c62017-07-14 16:57:23 -07003 * Copyright (c) 2014-2018 The Linux Foundation. All rights reserved.
Ben Menchaca84f36632014-02-28 20:57:38 +00004 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 **************************************************************************
15 */
16
Murat Sezginb3731e82014-11-26 12:20:59 -080017#include <linux/version.h>
Ben Menchaca84f36632014-02-28 20:57:38 +000018#include <linux/types.h>
19#include <linux/ip.h>
20#include <linux/tcp.h>
21#include <linux/module.h>
22#include <linux/skbuff.h>
23#include <linux/icmp.h>
Ben Menchaca84f36632014-02-28 20:57:38 +000024#include <linux/kthread.h>
Ben Menchaca84f36632014-02-28 20:57:38 +000025#include <linux/pkt_sched.h>
26#include <linux/string.h>
27#include <net/ip6_route.h>
28#include <net/ip6_fib.h>
29#include <net/ipv6.h>
30#include <net/route.h>
Gareth Williams46f4b5f2014-06-01 23:35:23 +010031#include <net/ip_fib.h>
Ben Menchaca84f36632014-02-28 20:57:38 +000032#include <net/ip.h>
33#include <net/tcp.h>
34#include <asm/unaligned.h>
35#include <asm/uaccess.h> /* for put_user */
36#include <linux/inet.h>
37#include <linux/in6.h>
38#include <linux/in.h>
39#include <linux/udp.h>
40#include <linux/tcp.h>
Murat Sezgina205b042016-07-19 14:18:14 -070041#include <linux/kernel.h>
42#include <linux/netlink.h>
43#include <linux/rtnetlink.h>
44#include <linux/socket.h>
45#include <linux/wireless.h>
46
Murat Sezgin8c345822015-05-27 15:35:38 -070047#if defined(ECM_DB_XREF_ENABLE) && defined(ECM_BAND_STEERING_ENABLE)
ratheesh kannoth37e35b02015-03-26 11:25:02 +053048#include <linux/if_bridge.h>
Murat Sezgin8c345822015-05-27 15:35:38 -070049#endif
Ben Menchaca84f36632014-02-28 20:57:38 +000050#include <linux/inetdevice.h>
Murat Sezginc1402562015-03-12 12:32:20 -070051#if defined(ECM_INTERFACE_TUNIPIP6_ENABLE) || defined(ECM_INTERFACE_SIT_ENABLE)
Tushar Mathur39f63f62015-06-19 14:31:14 +053052#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 9, 0))
Ben Menchaca84f36632014-02-28 20:57:38 +000053#include <net/ipip.h>
Tushar Mathur39f63f62015-06-19 14:31:14 +053054#else
55#include <net/ip_tunnels.h>
56#endif
Murat Sezginb3731e82014-11-26 12:20:59 -080057#endif
Ben Menchaca84f36632014-02-28 20:57:38 +000058#include <net/ip6_tunnel.h>
Gareth Williams43fc0852014-05-26 19:10:00 +010059#include <net/addrconf.h>
Ben Menchaca84f36632014-02-28 20:57:38 +000060#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
62#include <linux/netfilter_bridge.h>
63#include <linux/if_bridge.h>
64#include <net/arp.h>
65#include <net/netfilter/nf_conntrack.h>
66#include <net/netfilter/nf_conntrack_acct.h>
67#include <net/netfilter/nf_conntrack_helper.h>
68#include <net/netfilter/nf_conntrack_l4proto.h>
69#include <net/netfilter/nf_conntrack_l3proto.h>
70#include <net/netfilter/nf_conntrack_zones.h>
71#include <net/netfilter/nf_conntrack_core.h>
ratheesh kannotha6d25952016-04-11 12:23:26 +053072#include <linux/netfilter_ipv6/ip6_tables.h>
Ben Menchaca84f36632014-02-28 20:57:38 +000073#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
74#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
Gareth Williams141d2382014-11-25 11:35:19 -080075#ifdef ECM_INTERFACE_VLAN_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +000076#include <linux/../../net/8021q/vlan.h>
77#include <linux/if_vlan.h>
Gareth Williams141d2382014-11-25 11:35:19 -080078#endif
Murat Sezgina683edd2015-01-20 10:48:30 -080079#ifdef ECM_INTERFACE_PPP_ENABLE
80#include <linux/if_pppox.h>
ratheesh kannotha32fdd12015-09-09 08:02:58 +053081#ifdef ECM_INTERFACE_L2TPV2_ENABLE
ratheesh kannoth08b8e442015-10-02 00:24:55 +053082#include <linux/if_pppol2tp.h>
ratheesh kannotha32fdd12015-09-09 08:02:58 +053083#endif
Shyam Sunder23f2e542015-09-28 14:56:49 +053084#ifdef ECM_INTERFACE_PPTP_ENABLE
85#include <linux/netfilter/nf_conntrack_proto_gre.h>
86#endif
Murat Sezgina683edd2015-01-20 10:48:30 -080087#endif
ratheesh kannothcfdcb332015-12-24 07:19:18 +053088#ifdef ECM_INTERFACE_MAP_T_ENABLE
89#include <nat46-core.h>
90#endif
Ben Menchaca84f36632014-02-28 20:57:38 +000091
92/*
93 * Debug output levels
94 * 0 = OFF
95 * 1 = ASSERTS / ERRORS
96 * 2 = 1 + WARN
97 * 3 = 2 + INFO
98 * 4 = 3 + TRACE
99 */
100#define DEBUG_LEVEL ECM_INTERFACE_DEBUG_LEVEL
101
Shyam Sunder6358b862015-05-04 15:06:24 +0530102#ifdef ECM_MULTICAST_ENABLE
103#include <mc_ecm.h>
104#endif
105
Ben Menchaca84f36632014-02-28 20:57:38 +0000106#include "ecm_types.h"
107#include "ecm_db_types.h"
Gareth Williamsd5618a82015-05-20 11:13:32 +0100108#include "ecm_state.h"
Ben Menchaca84f36632014-02-28 20:57:38 +0000109#include "ecm_tracker.h"
110#include "ecm_classifier.h"
111#include "ecm_front_end_types.h"
112#include "ecm_tracker_datagram.h"
113#include "ecm_tracker_udp.h"
114#include "ecm_tracker_tcp.h"
115#include "ecm_db.h"
116#include "ecm_interface.h"
117
Murat Sezgina205b042016-07-19 14:18:14 -0700118/*
119 * Wifi event handler structure.
120 */
121struct ecm_interface_wifi_event {
122 struct task_struct *thread;
123 struct socket *sock;
124};
125
126static struct ecm_interface_wifi_event __ewn;
127
Gareth Williams8ac34292015-03-17 14:06:58 +0000128#ifdef ECM_IPV6_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +0000129/*
Gareth Williamsadf425f2014-05-26 19:29:02 +0100130 * TODO: Remove once the Linux image and headers get propogated.
131 */
132struct net_device *ipv6_dev_find(struct net *net, struct in6_addr *addr, int strict);
Murat Sezgin49465a42014-11-24 15:37:48 -0800133#endif
Gareth Williamsadf425f2014-05-26 19:29:02 +0100134
135/*
Ben Menchaca84f36632014-02-28 20:57:38 +0000136 * Locking - concurrency control
137 */
Murat Sezgin908ecb32015-05-10 20:54:36 -0700138static DEFINE_SPINLOCK(ecm_interface_lock); /* Protect against SMP access between netfilter, events and private threaded function. */
Ben Menchaca84f36632014-02-28 20:57:38 +0000139
140/*
141 * Management thread control
142 */
143static bool ecm_interface_terminate_pending = false; /* True when the user has signalled we should quit */
Ben Menchaca84f36632014-02-28 20:57:38 +0000144
145/*
Murat Sezgineebedb92017-11-17 17:14:32 -0800146 * Source interface check flag.
147 * If it is enabled, the acceleration engine will check the flow's interface to see
148 * whether it matches with the rule's source interface or not.
149 */
150int ecm_interface_src_check;
151
152static struct ctl_table_header *ecm_interface_ctl_table_header; /* Sysctl table header */
153
154/*
Murat Sezginb3731e82014-11-26 12:20:59 -0800155 * ecm_interface_get_and_hold_dev_master()
156 * Returns the master device of a net device if any.
157 */
158struct net_device *ecm_interface_get_and_hold_dev_master(struct net_device *dev)
159{
160 struct net_device *master;
161#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,6,0))
162 rcu_read_lock();
163 master = netdev_master_upper_dev_get_rcu(dev);
164 if (!master) {
165 rcu_read_unlock();
166 return NULL;
167 }
168 dev_hold(master);
169 rcu_read_unlock();
170#else
171 master = dev->master;
172 if (!master) {
173 return NULL;
174 }
175 dev_hold(master);
176#endif
177 return master;
178}
179EXPORT_SYMBOL(ecm_interface_get_and_hold_dev_master);
180
181/*
Shyam Sunder9db20852016-03-09 19:04:49 +0530182 * ecm_interface_vlan_real_dev()
183 * Return immediate VLAN net device or Physical device pointer
184 */
185static inline struct net_device *ecm_interface_vlan_real_dev(struct net_device *vlan_dev)
186{
187#if (LINUX_VERSION_CODE > KERNEL_VERSION(3, 6, 0))
188 return vlan_dev_next_dev(vlan_dev);
189#else
190 return vlan_dev_real_dev(vlan_dev);
191#endif
192}
193
194/*
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100195 * ecm_interface_dev_find_by_local_addr_ipv4()
Shyam Sunder9db20852016-03-09 19:04:49 +0530196 * Return a hold to the device for the given local IP address. Returns NULL on failure.
Gareth Williamsadf425f2014-05-26 19:29:02 +0100197 */
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100198static struct net_device *ecm_interface_dev_find_by_local_addr_ipv4(ip_addr_t addr)
Gareth Williamsadf425f2014-05-26 19:29:02 +0100199{
200 __be32 be_addr;
201 struct net_device *dev;
202
203 ECM_IP_ADDR_TO_NIN4_ADDR(be_addr, addr);
204 dev = ip_dev_find(&init_net, be_addr);
205 return dev;
206}
207
Gareth Williams8ac34292015-03-17 14:06:58 +0000208#ifdef ECM_IPV6_ENABLE
Gareth Williamsadf425f2014-05-26 19:29:02 +0100209/*
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100210 * ecm_interface_dev_find_by_local_addr_ipv6()
211 * Return a hold to the device for the given local IP address. Returns NULL on failure.
Gareth Williamsadf425f2014-05-26 19:29:02 +0100212 */
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100213static struct net_device *ecm_interface_dev_find_by_local_addr_ipv6(ip_addr_t addr)
Gareth Williamsadf425f2014-05-26 19:29:02 +0100214{
215 struct in6_addr addr6;
216 struct net_device *dev;
217
218 ECM_IP_ADDR_TO_NIN6_ADDR(addr6, addr);
219 dev = (struct net_device *)ipv6_dev_find(&init_net, &addr6, 1);
220 return dev;
221}
Murat Sezgin49465a42014-11-24 15:37:48 -0800222#endif
Gareth Williamsadf425f2014-05-26 19:29:02 +0100223
224/*
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100225 * ecm_interface_dev_find_by_local_addr()
226 * Return the device on which the local address resides.
Gareth Williamsadf425f2014-05-26 19:29:02 +0100227 *
228 * Returns a hold to the device or NULL on failure.
229 */
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100230struct net_device *ecm_interface_dev_find_by_local_addr(ip_addr_t addr)
Gareth Williamsadf425f2014-05-26 19:29:02 +0100231{
232 char __attribute__((unused)) addr_str[40];
233
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100234 DEBUG_ECM_IP_ADDR_TO_STRING(addr_str, addr);
Gareth Williamsadf425f2014-05-26 19:29:02 +0100235 DEBUG_TRACE("Locate dev for: %s\n", addr_str);
236
237 if (ECM_IP_ADDR_IS_V4(addr)) {
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100238 return ecm_interface_dev_find_by_local_addr_ipv4(addr);
Gareth Williamsadf425f2014-05-26 19:29:02 +0100239 }
240
Gareth Williams8ac34292015-03-17 14:06:58 +0000241#ifdef ECM_IPV6_ENABLE
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100242 return ecm_interface_dev_find_by_local_addr_ipv6(addr);
Murat Sezgin49465a42014-11-24 15:37:48 -0800243#else
244 return NULL;
245#endif
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100246}
247EXPORT_SYMBOL(ecm_interface_dev_find_by_local_addr);
248
249/*
250 * ecm_interface_dev_find_by_addr()
251 * Return the net device on which the given IP address resides. Returns NULL on faiure.
252 *
253 * NOTE: The device may be the device upon which has a default gateway to reach the address.
254 * from_local_addr is true when the device was found by a local address search.
255 */
256struct net_device *ecm_interface_dev_find_by_addr(ip_addr_t addr, bool *from_local_addr)
257{
258 char __attribute__((unused)) addr_str[40];
259 struct ecm_interface_route ecm_rt;
260 struct net_device *dev;
261 struct dst_entry *dst;
262
263 DEBUG_ECM_IP_ADDR_TO_STRING(addr_str, addr);
264
265 /*
266 * Is the address a local IP?
267 */
268 DEBUG_TRACE("find net device for address: %s\n", addr_str);
269 dev = ecm_interface_dev_find_by_local_addr(addr);
270 if (dev) {
271 *from_local_addr = true;
272 DEBUG_TRACE("addr: %s is local: %p (%s)\n", addr_str, dev, dev->name);
273 return dev;
274 }
275
276 DEBUG_TRACE("addr: %s is not local\n", addr_str);
277
278 /*
279 * Try a route to the address instead
280 * NOTE: This will locate a route entry in the route destination *cache*.
281 */
282 if (!ecm_interface_find_route_by_addr(addr, &ecm_rt)) {
283 DEBUG_WARN("addr: %s - no dev locatable\n", addr_str);
284 return NULL;
285 }
286
287 *from_local_addr = false;
288 dst = ecm_rt.dst;
289 dev = dst->dev;
290 dev_hold(dev);
291 ecm_interface_route_release(&ecm_rt);
292 DEBUG_TRACE("dest_addr: %s uses dev: %p(%s)\n", addr_str, dev, dev->name);
293 return dev;
Gareth Williamsadf425f2014-05-26 19:29:02 +0100294}
295EXPORT_SYMBOL(ecm_interface_dev_find_by_addr);
296
Gareth Williams8ac34292015-03-17 14:06:58 +0000297#ifdef ECM_IPV6_ENABLE
Gareth Williamsadf425f2014-05-26 19:29:02 +0100298/*
Ben Menchaca84f36632014-02-28 20:57:38 +0000299 * ecm_interface_mac_addr_get_ipv6()
300 * Return mac for an IPv6 address
301 *
302 * GGG TODO Need to make sure this also works for local IP addresses too.
303 */
304static bool ecm_interface_mac_addr_get_ipv6(ip_addr_t addr, uint8_t *mac_addr, bool *on_link, ip_addr_t gw_addr)
305{
306 struct in6_addr daddr;
307 struct ecm_interface_route ecm_rt;
308 struct neighbour *neigh;
309 struct rt6_info *rt;
310 struct dst_entry *dst;
311
312 /*
313 * Get the MAC address that corresponds to IP address given.
314 * We look up the rt6_info entries and, from its neighbour structure, obtain the hardware address.
315 * This means we will also work if the neighbours are routers too.
316 */
317 ECM_IP_ADDR_TO_NIN6_ADDR(daddr, addr);
318 if (!ecm_interface_find_route_by_addr(addr, &ecm_rt)) {
Kiran Kumar C. S. K7d178222014-06-27 18:47:29 +0530319 *on_link = false;
Ben Menchaca84f36632014-02-28 20:57:38 +0000320 return false;
321 }
322 DEBUG_ASSERT(!ecm_rt.v4_route, "Did not locate a v6 route!\n");
323
324 /*
325 * Is this destination on link or off-link via a gateway?
326 */
327 rt = ecm_rt.rt.rtv6;
328 if (!ECM_IP_ADDR_MATCH(rt->rt6i_dst.addr.in6_u.u6_addr32, rt->rt6i_gateway.in6_u.u6_addr32) || (rt->rt6i_flags & RTF_GATEWAY)) {
329 *on_link = false;
330 ECM_NIN6_ADDR_TO_IP_ADDR(gw_addr, rt->rt6i_gateway)
331 } else {
332 *on_link = true;
333 }
334
335 rcu_read_lock();
336 dst = ecm_rt.dst;
Murat Sezgine9b84582015-01-27 17:34:14 -0800337#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3,6,0))
Ben Menchaca84f36632014-02-28 20:57:38 +0000338 neigh = dst_get_neighbour_noref(dst);
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700339 if (neigh) {
340 neigh_hold(neigh);
Murat Sezgine9b84582015-01-27 17:34:14 -0800341 }
342#else
343 neigh = dst_neigh_lookup(dst, &daddr);
344#endif
345 if (!neigh) {
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700346 neigh = neigh_lookup(&nd_tbl, &daddr, dst->dev);
347 }
Murat Sezgine9b84582015-01-27 17:34:14 -0800348
Ben Menchaca84f36632014-02-28 20:57:38 +0000349 if (!neigh) {
350 rcu_read_unlock();
351 ecm_interface_route_release(&ecm_rt);
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700352 DEBUG_WARN("No neigh reference\n");
Ben Menchaca84f36632014-02-28 20:57:38 +0000353 return false;
354 }
355 if (!(neigh->nud_state & NUD_VALID)) {
356 rcu_read_unlock();
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700357 neigh_release(neigh);
Ben Menchaca84f36632014-02-28 20:57:38 +0000358 ecm_interface_route_release(&ecm_rt);
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700359 DEBUG_WARN("NUD invalid\n");
Ben Menchaca84f36632014-02-28 20:57:38 +0000360 return false;
361 }
362 if (!neigh->dev) {
363 rcu_read_unlock();
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700364 neigh_release(neigh);
Ben Menchaca84f36632014-02-28 20:57:38 +0000365 ecm_interface_route_release(&ecm_rt);
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700366 DEBUG_WARN("Neigh dev invalid\n");
Ben Menchaca84f36632014-02-28 20:57:38 +0000367 return false;
368 }
369
370 /*
371 * If neigh->dev is a loopback then addr is a local address in which case we take the MAC from given device
372 */
373 if (neigh->dev->flags & IFF_LOOPBACK) {
374 // GGG TODO Create an equivalent logic to that for ipv4, maybe need to create an ip6_dev_find()?
375 DEBUG_TRACE("local address " ECM_IP_ADDR_OCTAL_FMT " (found loopback)\n", ECM_IP_ADDR_TO_OCTAL(addr));
376 memset(mac_addr, 0, 6);
377 } else {
378 memcpy(mac_addr, neigh->ha, 6);
379 }
380 rcu_read_unlock();
Murat Sezgindcbcdb52014-05-15 16:06:13 -0700381 neigh_release(neigh);
Ben Menchaca84f36632014-02-28 20:57:38 +0000382 ecm_interface_route_release(&ecm_rt);
383
384 DEBUG_TRACE(ECM_IP_ADDR_OCTAL_FMT " maps to %pM\n", ECM_IP_ADDR_TO_OCTAL(addr), mac_addr);
385 return true;
386}
Murat Sezgin5dae8832015-12-03 14:23:19 -0800387
388/*
389 * ecm_interface_find_gateway_ipv6()
390 * Finds the ipv6 gateway ip address of a given ipv6 address.
391 */
392static bool ecm_interface_find_gateway_ipv6(ip_addr_t addr, ip_addr_t gw_addr)
393{
394 struct ecm_interface_route ecm_rt;
395 struct rt6_info *rt;
396
397 /*
398 * Find the ipv6 route of the given ip address to look up
399 * whether we have a gateway to reach to that ip address or not.
400 */
401 if (!ecm_interface_find_route_by_addr(addr, &ecm_rt)) {
402 return false;
403 }
404 DEBUG_ASSERT(!ecm_rt.v4_route, "Did not locate a v6 route!\n");
405 DEBUG_TRACE("Found route\n");
406
407 /*
408 * Is this destination reachable via a gateway?
409 */
410 rt = ecm_rt.rt.rtv6;
411 if (ECM_IP_ADDR_MATCH(rt->rt6i_dst.addr.in6_u.u6_addr32, rt->rt6i_gateway.in6_u.u6_addr32) && !(rt->rt6i_flags & RTF_GATEWAY)) {
Murat Sezgin313f2222016-08-11 12:22:10 -0700412 ecm_interface_route_release(&ecm_rt);
Murat Sezgin5dae8832015-12-03 14:23:19 -0800413 return false;
414 }
415
416 ECM_NIN6_ADDR_TO_IP_ADDR(gw_addr, rt->rt6i_gateway)
Murat Sezgin313f2222016-08-11 12:22:10 -0700417 ecm_interface_route_release(&ecm_rt);
Murat Sezgin5dae8832015-12-03 14:23:19 -0800418 return true;
419}
Murat Sezgin49465a42014-11-24 15:37:48 -0800420#endif
Ben Menchaca84f36632014-02-28 20:57:38 +0000421
422/*
Murat Sezgin5dae8832015-12-03 14:23:19 -0800423 * ecm_interface_find_gateway_ipv4()
424 * Finds the ipv4 gateway address of a given ipv4 address.
425 */
426static bool ecm_interface_find_gateway_ipv4(ip_addr_t addr, ip_addr_t gw_addr)
427{
428 struct ecm_interface_route ecm_rt;
429 struct rtable *rt;
430
431 /*
432 * Find the ipv4 route of the given ip address to look up
433 * whether we have a gateway to reach to that ip address or not.
434 */
435 if (!ecm_interface_find_route_by_addr(addr, &ecm_rt)) {
436 return false;
437 }
438 DEBUG_ASSERT(ecm_rt.v4_route, "Did not locate a v4 route!\n");
439 DEBUG_TRACE("Found route\n");
440
441 /*
442 * Is this destination reachable via a gateway?
443 */
444 rt = ecm_rt.rt.rtv4;
445#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 6, 0))
446 if (!(rt->rt_dst != rt->rt_gateway) && !(rt->rt_flags & RTF_GATEWAY)) {
447#else
448 if (!rt->rt_uses_gateway && !(rt->rt_flags & RTF_GATEWAY)) {
449#endif
Murat Sezgin313f2222016-08-11 12:22:10 -0700450 ecm_interface_route_release(&ecm_rt);
Murat Sezgin5dae8832015-12-03 14:23:19 -0800451 return false;
452 }
453
454 ECM_NIN4_ADDR_TO_IP_ADDR(gw_addr, rt->rt_gateway)
Murat Sezgin313f2222016-08-11 12:22:10 -0700455 ecm_interface_route_release(&ecm_rt);
Murat Sezgin5dae8832015-12-03 14:23:19 -0800456 return true;
457}
458
459/*
460 * ecm_interface_find_gateway()
461 * Finds the gateway ip address of a given ECM ip address type.
462 */
463bool ecm_interface_find_gateway(ip_addr_t addr, ip_addr_t gw_addr)
464{
465 if (ECM_IP_ADDR_IS_V4(addr)) {
466 return ecm_interface_find_gateway_ipv4(addr, gw_addr);
467 }
468
469#ifdef ECM_IPV6_ENABLE
470 return ecm_interface_find_gateway_ipv6(addr, gw_addr);
471#else
472 return false;
473#endif
474}
475EXPORT_SYMBOL(ecm_interface_find_gateway);
476
477/*
Ben Menchaca84f36632014-02-28 20:57:38 +0000478 * ecm_interface_mac_addr_get_ipv4()
479 * Return mac for an IPv4 address
480 */
481static bool ecm_interface_mac_addr_get_ipv4(ip_addr_t addr, uint8_t *mac_addr, bool *on_link, ip_addr_t gw_addr)
482{
483 struct neighbour *neigh;
484 struct ecm_interface_route ecm_rt;
485 struct rtable *rt;
486 struct dst_entry *dst;
487 __be32 ipv4_addr;
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +0530488
Ben Menchaca84f36632014-02-28 20:57:38 +0000489 /*
490 * Get the MAC address that corresponds to IP address given.
491 * We look up the rtable entries and, from its neighbour structure, obtain the hardware address.
492 * This means we will also work if the neighbours are routers too.
493 * We also locate the MAC if the address is a local host address.
494 */
495 ECM_IP_ADDR_TO_NIN4_ADDR(ipv4_addr, addr);
496 if (!ecm_interface_find_route_by_addr(addr, &ecm_rt)) {
Kiran Kumar C. S. K7d178222014-06-27 18:47:29 +0530497 *on_link = false;
Ben Menchaca84f36632014-02-28 20:57:38 +0000498 return false;
499 }
500 DEBUG_ASSERT(ecm_rt.v4_route, "Did not locate a v4 route!\n");
Murat Sezginb3731e82014-11-26 12:20:59 -0800501 DEBUG_TRACE("Found route\n");
Ben Menchaca84f36632014-02-28 20:57:38 +0000502
503 /*
504 * Is this destination on link or off-link via a gateway?
505 */
506 rt = ecm_rt.rt.rtv4;
Murat Sezginb3731e82014-11-26 12:20:59 -0800507#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3,6,0))
Ben Menchaca84f36632014-02-28 20:57:38 +0000508 if ((rt->rt_dst != rt->rt_gateway) || (rt->rt_flags & RTF_GATEWAY)) {
Murat Sezginb3731e82014-11-26 12:20:59 -0800509#else
510 if (rt->rt_uses_gateway || (rt->rt_flags & RTF_GATEWAY)) {
511#endif
Ben Menchaca84f36632014-02-28 20:57:38 +0000512 *on_link = false;
513 ECM_NIN4_ADDR_TO_IP_ADDR(gw_addr, rt->rt_gateway)
514 } else {
515 *on_link = true;
516 }
517
518 /*
519 * Get the neighbour entry for the address
520 */
521 rcu_read_lock();
522 dst = ecm_rt.dst;
Murat Sezginb3731e82014-11-26 12:20:59 -0800523#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3,6,0))
Ben Menchaca84f36632014-02-28 20:57:38 +0000524 neigh = dst_get_neighbour_noref(dst);
525 if (neigh) {
526 neigh_hold(neigh);
Murat Sezginb3731e82014-11-26 12:20:59 -0800527 }
528#else
529 neigh = dst_neigh_lookup(dst, &ipv4_addr);
530#endif
531 if (!neigh) {
Ben Menchaca84f36632014-02-28 20:57:38 +0000532 neigh = neigh_lookup(&arp_tbl, &ipv4_addr, dst->dev);
533 }
534 if (!neigh) {
535 rcu_read_unlock();
536 ecm_interface_route_release(&ecm_rt);
Murat Sezginb3731e82014-11-26 12:20:59 -0800537 DEBUG_WARN("no neigh\n");
Ben Menchaca84f36632014-02-28 20:57:38 +0000538 return false;
539 }
540 if (!(neigh->nud_state & NUD_VALID)) {
541 rcu_read_unlock();
542 neigh_release(neigh);
543 ecm_interface_route_release(&ecm_rt);
Murat Sezginb3731e82014-11-26 12:20:59 -0800544 DEBUG_WARN("neigh nud state is not valid\n");
Ben Menchaca84f36632014-02-28 20:57:38 +0000545 return false;
546 }
547 if (!neigh->dev) {
548 rcu_read_unlock();
549 neigh_release(neigh);
550 ecm_interface_route_release(&ecm_rt);
Murat Sezginb3731e82014-11-26 12:20:59 -0800551 DEBUG_WARN("neigh has no device\n");
Ben Menchaca84f36632014-02-28 20:57:38 +0000552 return false;
553 }
554
555 /*
556 * If the device is loopback this will be because the address is a local address
557 * In this case locate the device that has this local address and get its mac.
558 */
559 if (neigh->dev->type == ARPHRD_LOOPBACK) {
560 struct net_device *dev;
561
562 DEBUG_TRACE("%pI4 finds loopback device, dev: %p (%s)\n", &ipv4_addr, neigh->dev, neigh->dev->name);
563 rcu_read_unlock();
564 neigh_release(neigh);
565 ecm_interface_route_release(&ecm_rt);
566
567 /*
568 * Lookup the device that has this IP address assigned
569 */
570 dev = ip_dev_find(&init_net, ipv4_addr);
571 if (!dev) {
572 DEBUG_WARN("Unable to locate dev for: %pI4\n", &ipv4_addr);
573 return false;
574 }
575 memcpy(mac_addr, dev->dev_addr, (size_t)dev->addr_len);
576 DEBUG_TRACE("is local addr: %pI4, mac: %pM, dev ifindex: %d, dev: %p (%s), dev_type: %d\n",
577 &ipv4_addr, mac_addr, dev->ifindex, dev, dev->name, dev->type);
578 dev_put(dev);
579 return true;
580 }
581
582 if (!(neigh->dev->flags & IFF_NOARP)) {
583 memcpy(mac_addr, neigh->ha, (size_t)neigh->dev->addr_len);
584 } else {
585 DEBUG_TRACE("non-arp device: %p (%s, type: %d) to reach %pI4\n", neigh->dev, neigh->dev->name, neigh->dev->type, &ipv4_addr);
586 memset(mac_addr, 0, 6);
587 }
588 DEBUG_TRACE("addr: %pI4, mac: %pM, iif: %d, neigh dev ifindex: %d, dev: %p (%s), dev_type: %d\n",
589 &ipv4_addr, mac_addr, rt->rt_iif, neigh->dev->ifindex, neigh->dev, neigh->dev->name, neigh->dev->type);
590
591 rcu_read_unlock();
592 neigh_release(neigh);
593 ecm_interface_route_release(&ecm_rt);
594 return true;
595}
596
597/*
598 * ecm_interface_mac_addr_get()
599 * Return the mac address for the given IP address. Returns false on failure.
Ben Menchaca84f36632014-02-28 20:57:38 +0000600 */
601bool ecm_interface_mac_addr_get(ip_addr_t addr, uint8_t *mac_addr, bool *on_link, ip_addr_t gw_addr)
602{
603 if (ECM_IP_ADDR_IS_V4(addr)) {
604 return ecm_interface_mac_addr_get_ipv4(addr, mac_addr, on_link, gw_addr);
605 }
606
Gareth Williams8ac34292015-03-17 14:06:58 +0000607#ifdef ECM_IPV6_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +0000608 return ecm_interface_mac_addr_get_ipv6(addr, mac_addr, on_link, gw_addr);
Murat Sezgin49465a42014-11-24 15:37:48 -0800609#else
610 return false;
611#endif
Ben Menchaca84f36632014-02-28 20:57:38 +0000612}
613EXPORT_SYMBOL(ecm_interface_mac_addr_get);
614
Murat Sezgin5dae8832015-12-03 14:23:19 -0800615#ifdef ECM_IPV6_ENABLE
616/*
617 * ecm_interface_mac_addr_get_ipv6_no_route()
618 * Finds the mac address of a node from its ip address reachable via
619 * the given device. It looks up the mac address in the neighbour entries.
620 * It doesn't do any route lookup to find the dst entry.
621 */
622static bool ecm_interface_mac_addr_get_ipv6_no_route(struct net_device *dev, ip_addr_t addr, uint8_t *mac_addr)
623{
624 struct in6_addr daddr;
625 struct neighbour *neigh;
626 struct net_device *local_dev;
627
628 memset(mac_addr, 0, ETH_ALEN);
629
630 /*
631 * Get the MAC address that corresponds to IP address given.
632 */
633 ECM_IP_ADDR_TO_NIN6_ADDR(daddr, addr);
634 local_dev = ipv6_dev_find(&init_net, &daddr, 1);
635 if (local_dev) {
636 DEBUG_TRACE("%pi6 is a local address\n", &daddr);
637 memcpy(mac_addr, dev->dev_addr, ETH_ALEN);
638 dev_put(local_dev);
639 return true;
640 }
641
642 rcu_read_lock();
643 neigh = neigh_lookup(&nd_tbl, &daddr, dev);
644 if (!neigh) {
645 rcu_read_unlock();
646 DEBUG_WARN("No neigh reference\n");
647 return false;
648 }
649 if (!(neigh->nud_state & NUD_VALID)) {
650 neigh_release(neigh);
651 rcu_read_unlock();
652 DEBUG_WARN("NUD invalid\n");
653 return false;
654 }
655 if (!neigh->dev) {
656 neigh_release(neigh);
657 rcu_read_unlock();
658 DEBUG_WARN("Neigh dev invalid\n");
659 return false;
660 }
661
662 if (neigh->dev->flags & IFF_NOARP) {
663 neigh_release(neigh);
664 rcu_read_unlock();
665 DEBUG_TRACE("dest MAC is zero: %pM\n", mac_addr);
666 return true;
667 }
668
669 memcpy(mac_addr, neigh->ha, (size_t)neigh->dev->addr_len);
670 neigh_release(neigh);
671 rcu_read_unlock();
672 DEBUG_TRACE(ECM_IP_ADDR_OCTAL_FMT " maps to %pM\n", ECM_IP_ADDR_TO_OCTAL(addr), mac_addr);
673 return true;
674}
675#endif
676
677/*
678 * ecm_interface_mac_addr_get_ipv4_no_route()
679 * Finds the mac address of a node from its ip address reachable via
680 * the given device. It looks up the mac address in the neighbour entries.
681 * It doesn't do any route lookup to find the dst entry.
682 */
683static bool ecm_interface_mac_addr_get_ipv4_no_route(struct net_device *dev, ip_addr_t ip_addr, uint8_t *mac_addr)
684{
685 struct neighbour *neigh;
686 __be32 be_addr;
687 struct net_device *local_dev;
688
689 memset(mac_addr, 0, ETH_ALEN);
690
691 ECM_IP_ADDR_TO_NIN4_ADDR(be_addr, ip_addr);
692 local_dev = ip_dev_find(&init_net, be_addr);
693 if (local_dev) {
694 DEBUG_TRACE("%pI4n is a local address\n", &be_addr);
695 memcpy(mac_addr, dev->dev_addr, ETH_ALEN);
696 dev_put(local_dev);
697 return true;
698 }
699
700 rcu_read_lock();
701 neigh = neigh_lookup(&arp_tbl, &be_addr, dev);
702 if (!neigh) {
703 rcu_read_unlock();
704 DEBUG_WARN("no neigh\n");
705 return false;
706 }
707 if (!(neigh->nud_state & NUD_VALID)) {
708 neigh_release(neigh);
709 rcu_read_unlock();
710 DEBUG_WARN("neigh nud state is not valid\n");
711 return false;
712 }
713 if (!neigh->dev) {
714 neigh_release(neigh);
715 rcu_read_unlock();
716 DEBUG_WARN("neigh has no device\n");
717 return false;
718 }
719
720 if (neigh->dev->flags & IFF_NOARP) {
721 neigh_release(neigh);
722 rcu_read_unlock();
723 DEBUG_TRACE("dest MAC is zero: %pM\n", mac_addr);
724 return true;
725 }
726
727 memcpy(mac_addr, neigh->ha, (size_t)neigh->dev->addr_len);
728 neigh_release(neigh);
729 rcu_read_unlock();
730 DEBUG_TRACE("dest MAC: %pM\n", mac_addr);
731 return true;
732
733}
734
735/*
736 * ecm_interface_mac_addr_get_no_route()
737 * Return the mac address for the given IP address reacahble via the given device.
738 * Return false on failure, true on success.
739 */
740bool ecm_interface_mac_addr_get_no_route(struct net_device *dev, ip_addr_t addr, uint8_t *mac_addr)
741{
742 if (ECM_IP_ADDR_IS_V4(addr)) {
743 return ecm_interface_mac_addr_get_ipv4_no_route(dev, addr, mac_addr);
744 }
745
746#ifdef ECM_IPV6_ENABLE
747 return ecm_interface_mac_addr_get_ipv6_no_route(dev, addr, mac_addr);
748#else
749 return false;
750#endif
751}
752EXPORT_SYMBOL(ecm_interface_mac_addr_get_no_route);
753
Shyam Sunder6358b862015-05-04 15:06:24 +0530754#ifdef ECM_MULTICAST_ENABLE
755/*
756 * ecm_interface_multicast_check_for_br_dev()
757 * Find a bridge dev is present or not in an
758 * array of Ifindexs
759 */
760bool ecm_interface_multicast_check_for_br_dev(uint32_t dest_if[], uint8_t max_if)
761{
762 struct net_device *br_dev;
763 int i;
764
765 for (i = 0; i < max_if; i++) {
766 br_dev = dev_get_by_index(&init_net, dest_if[i]);
767 if (!br_dev) {
ratheesh kannothc5df16c2018-03-16 21:07:23 +0530768 /*
769 * Interface got deleted; but is yet to be updated in MFC table
770 */
771 DEBUG_WARN("Could not find a valid netdev here\n");
Shyam Sunder6358b862015-05-04 15:06:24 +0530772 continue;
773 }
774
775 if (ecm_front_end_is_bridge_device(br_dev)) {
776 dev_put(br_dev);
777 return true;
778 }
779 dev_put(br_dev);
780 }
781 return false;
782}
783EXPORT_SYMBOL(ecm_interface_multicast_check_for_br_dev);
Shyam Sundera2e08ee2015-06-18 21:32:13 +0530784
785/*
ratheesh kannothc5df16c2018-03-16 21:07:23 +0530786 * ecm_interface_multicast_is_iface_type()
787 * Checks if interface of type exist in mc_if_index
788 */
789bool ecm_interface_multicast_is_iface_type(int32_t mc_if_index[], int32_t max_if_index, unsigned short type)
790{
791 int32_t i;
792 struct net_device *dev;
793
794 for (i = 0; i < max_if_index; i++) {
795
796 if (!mc_if_index[i]) {
797 break;
798 }
799
800 dev = dev_get_by_index(&init_net, mc_if_index[i]);
801 if (!dev) {
802 DEBUG_WARN("Could not find a valid interface with index = %d\n", mc_if_index[i]);
803 continue;
804 }
805
806 if (dev->type == type) {
807 DEBUG_TRACE("Interface dev = %s of type %u\n", dev->name, type);
808 dev_put(dev);
809 return true;
810 }
811
812 dev_put(dev);
813 }
814
815 return false;
816}
817
818/*
Shyam Sundera2e08ee2015-06-18 21:32:13 +0530819 * ecm_interface_multicast_check_for_src_if_index()
820 * Find if a source netdev ifindex is matching with list of
821 * multicast destination netdev ifindex. If find a match then
822 * returns a new list of destination netdev ifindex excluding
823 * the ifindex of source netdev.
824 */
825int32_t ecm_interface_multicast_check_for_src_ifindex(int32_t mc_if_index[], int32_t max_if_index, int32_t if_num)
826{
827 int32_t i;
828 int32_t valid_index;
829
830 for (i = 0, valid_index = 0; i < max_if_index; i++) {
831 if (mc_if_index[i] == 0) {
832 break;
833 }
834
835 if (mc_if_index[i] != if_num) {
836 mc_if_index[valid_index] = mc_if_index[i];
837 valid_index++;
838 continue;
839 }
840 }
841
842 return valid_index;
843}
844EXPORT_SYMBOL(ecm_interface_multicast_check_for_src_ifindex);
Shyam Sunder6358b862015-05-04 15:06:24 +0530845#endif
846
Ben Menchaca84f36632014-02-28 20:57:38 +0000847/*
848 * ecm_interface_addr_find_route_by_addr_ipv4()
849 * Return the route for the given IP address. Returns NULL on failure.
850 */
851static bool ecm_interface_find_route_by_addr_ipv4(ip_addr_t addr, struct ecm_interface_route *ecm_rt)
852{
853 __be32 be_addr;
854
855 /*
856 * Get a route to the given IP address, this will allow us to also find the interface
857 * it is using to communicate with that IP address.
858 */
859 ECM_IP_ADDR_TO_NIN4_ADDR(be_addr, addr);
860 ecm_rt->rt.rtv4 = ip_route_output(&init_net, be_addr, 0, 0, 0);
861 if (IS_ERR(ecm_rt->rt.rtv4)) {
862 DEBUG_TRACE("No output route to: %pI4n\n", &be_addr);
863 return false;
864 }
865 DEBUG_TRACE("Output route to: %pI4n is: %p\n", &be_addr, ecm_rt->rt.rtv4);
866 ecm_rt->dst = (struct dst_entry *)ecm_rt->rt.rtv4;
867 ecm_rt->v4_route = true;
868 return true;
869}
870
Gareth Williams8ac34292015-03-17 14:06:58 +0000871#ifdef ECM_IPV6_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +0000872/*
873 * ecm_interface_addr_find_route_by_addr_ipv6()
874 * Return the route for the given IP address. Returns NULL on failure.
875 */
876static bool ecm_interface_find_route_by_addr_ipv6(ip_addr_t addr, struct ecm_interface_route *ecm_rt)
877{
878 struct in6_addr naddr;
879
880 ECM_IP_ADDR_TO_NIN6_ADDR(naddr, addr);
881
882 /*
883 * Get a route to the given IP address, this will allow us to also find the interface
884 * it is using to communicate with that IP address.
885 */
886 ecm_rt->rt.rtv6 = rt6_lookup(&init_net, &naddr, NULL, 0, 0);
887 if (!ecm_rt->rt.rtv6) {
888 DEBUG_TRACE("No output route to: " ECM_IP_ADDR_OCTAL_FMT "\n", ECM_IP_ADDR_TO_OCTAL(addr));
889 return NULL;
890 }
891 DEBUG_TRACE("Output route to: " ECM_IP_ADDR_OCTAL_FMT " is: %p\n", ECM_IP_ADDR_TO_OCTAL(addr), ecm_rt->rt.rtv6);
892 ecm_rt->dst = (struct dst_entry *)ecm_rt->rt.rtv6;
893 ecm_rt->v4_route = false;
894 return true;
895}
Murat Sezgin49465a42014-11-24 15:37:48 -0800896#endif
Ben Menchaca84f36632014-02-28 20:57:38 +0000897
898/*
899 * ecm_interface_addr_find_route_by_addr()
900 * Return the route (in the given parameter) for the given IP address. Returns false on failure.
901 *
902 * Route is the device on which the addr is reachable, which may be loopback for local addresses.
903 *
904 * Returns true if the route was able to be located. The route must be released using ecm_interface_route_release().
905 */
906bool ecm_interface_find_route_by_addr(ip_addr_t addr, struct ecm_interface_route *ecm_rt)
907{
908 char __attribute__((unused)) addr_str[40];
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +0530909
Gareth Williams46f4b5f2014-06-01 23:35:23 +0100910 DEBUG_ECM_IP_ADDR_TO_STRING(addr_str, addr);
Ben Menchaca84f36632014-02-28 20:57:38 +0000911 DEBUG_TRACE("Locate route to: %s\n", addr_str);
912
913 if (ECM_IP_ADDR_IS_V4(addr)) {
914 return ecm_interface_find_route_by_addr_ipv4(addr, ecm_rt);
915 }
916
Gareth Williams8ac34292015-03-17 14:06:58 +0000917#ifdef ECM_IPV6_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +0000918 return ecm_interface_find_route_by_addr_ipv6(addr, ecm_rt);
Murat Sezgin49465a42014-11-24 15:37:48 -0800919#else
920 return false;
921#endif
Ben Menchaca84f36632014-02-28 20:57:38 +0000922}
923EXPORT_SYMBOL(ecm_interface_find_route_by_addr);
924
925/*
926 * ecm_interface_route_release()
927 * Release an ecm route
928 */
929void ecm_interface_route_release(struct ecm_interface_route *rt)
930{
931 dst_release(rt->dst);
932}
933EXPORT_SYMBOL(ecm_interface_route_release);
934
Murat Sezgin188b4a32015-06-03 10:58:59 -0700935#ifdef ECM_IPV6_ENABLE
936/*
937 * ecm_interface_send_neighbour_solicitation()
938 * Issue an IPv6 Neighbour soliciation request.
939 */
940void ecm_interface_send_neighbour_solicitation(struct net_device *dev, ip_addr_t addr)
941{
942 struct in6_addr dst_addr, src_addr;
943 struct in6_addr mc_dst_addr;
944 struct rt6_info *rt6i;
945 struct neighbour *neigh;
Murat Sezgin188b4a32015-06-03 10:58:59 -0700946 struct net *netf = dev_net(dev);
947 int ret;
948
Murat Sezgin188b4a32015-06-03 10:58:59 -0700949 /*
950 * Find source and destination addresses in Linux format. We need
951 * mcast destination address as well.
952 */
953 ECM_IP_ADDR_TO_NIN6_ADDR(dst_addr, addr);
954 addrconf_addr_solict_mult(&dst_addr, &mc_dst_addr);
Murat Sezgin96ed0342017-05-19 09:48:57 -0700955 ret = ipv6_dev_get_saddr(netf, dev, &mc_dst_addr, 0, &src_addr);
Murat Sezgin188b4a32015-06-03 10:58:59 -0700956
957 /*
Murat Sezgin188b4a32015-06-03 10:58:59 -0700958 * Find the route entry
959 */
960 rt6i = rt6_lookup(netf, &dst_addr, NULL, 0, 0);
961 if (!rt6i) {
Murat Sezginf21210e2016-04-04 13:58:20 -0700962 DEBUG_TRACE("IPv6 Route lookup failure for destination IPv6 address " ECM_IP_ADDR_OCTAL_FMT "\n", ECM_IP_ADDR_TO_OCTAL(addr));
Murat Sezgin188b4a32015-06-03 10:58:59 -0700963 return;
964 }
965
966 /*
967 * Find the neighbor entry
968 */
969#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3,6,0))
970 neigh = rt6i->dst.ops->neigh_lookup(&rt6i->dst, &dst_addr);
971#else
972 neigh = rt6i->dst.ops->neigh_lookup(&rt6i->dst, NULL, &dst_addr);
973#endif
974 if (neigh == NULL) {
Murat Sezginf21210e2016-04-04 13:58:20 -0700975 DEBUG_TRACE("Neighbour lookup failure for destination IPv6 address " ECM_IP_ADDR_OCTAL_FMT "\n", ECM_IP_ADDR_TO_OCTAL(addr));
Murat Sezgin188b4a32015-06-03 10:58:59 -0700976 dst_release(&rt6i->dst);
977 return;
978 }
979
980 /*
981 * Issue a Neighbour soliciation request
982 */
983 DEBUG_TRACE("Issue Neighbour solicitation request\n");
Murat Sezgine1c51d82016-02-10 16:42:58 -0800984#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0))
Murat Sezgin188b4a32015-06-03 10:58:59 -0700985 ndisc_send_ns(dev, neigh, &dst_addr, &mc_dst_addr, &src_addr);
Murat Sezgine1c51d82016-02-10 16:42:58 -0800986#else
987 ndisc_send_ns(dev, &dst_addr, &mc_dst_addr, &src_addr);
988#endif
Murat Sezgin188b4a32015-06-03 10:58:59 -0700989 neigh_release(neigh);
990 dst_release(&rt6i->dst);
991}
992EXPORT_SYMBOL(ecm_interface_send_neighbour_solicitation);
993#endif
994
995/*
996 * ecm_interface_send_arp_request()
997 * Issue and ARP request.
998 */
999void ecm_interface_send_arp_request(struct net_device *dest_dev, ip_addr_t dest_addr, bool on_link, ip_addr_t gw_addr)
1000{
1001 /*
1002 * Possible ARP does not know the address yet
1003 */
Murat Sezgin4c093212016-03-24 14:59:02 -07001004 struct neighbour *neigh;
Murat Sezgin188b4a32015-06-03 10:58:59 -07001005 __be32 ipv4_addr;
Murat Sezgin188b4a32015-06-03 10:58:59 -07001006
1007 /*
Murat Sezgin987493e2016-04-15 12:33:47 -07001008 * Convert the ECM IP address type to network order IPv4 address.
Murat Sezgin188b4a32015-06-03 10:58:59 -07001009 */
1010 ECM_IP_ADDR_TO_NIN4_ADDR(ipv4_addr, dest_addr);
Murat Sezgin188b4a32015-06-03 10:58:59 -07001011
1012 /*
1013 * If we have a GW for this address, then we have to send ARP request to the GW
1014 */
1015 if (!on_link && !ECM_IP_ADDR_IS_NULL(gw_addr)) {
1016 ECM_IP_ADDR_TO_NIN4_ADDR(ipv4_addr, gw_addr);
1017 }
1018
Murat Sezgin4c093212016-03-24 14:59:02 -07001019 /*
1020 * If we don't have this neighbor, create it before sending the arp request,
1021 * so that when we receive the arp reply we update the neigh entry.
1022 */
1023 neigh = neigh_lookup(&arp_tbl, &ipv4_addr, dest_dev);
1024 if (!neigh) {
Murat Sezgin987493e2016-04-15 12:33:47 -07001025 neigh = neigh_create(&arp_tbl, &ipv4_addr, dest_dev);
1026 if (IS_ERR(neigh)) {
1027 DEBUG_WARN("Unable to create ARP request neigh for %pI4\n", &ipv4_addr);
1028 return;
1029 }
Murat Sezgin4c093212016-03-24 14:59:02 -07001030 }
Murat Sezgin188b4a32015-06-03 10:58:59 -07001031
Murat Sezgin987493e2016-04-15 12:33:47 -07001032 DEBUG_TRACE("Send ARP for %pI4\n", &ipv4_addr);
1033 neigh_event_send(neigh, NULL);
1034 neigh_release(neigh);
Murat Sezgin188b4a32015-06-03 10:58:59 -07001035}
1036EXPORT_SYMBOL(ecm_interface_send_arp_request);
1037
Xiaoping Fanc7735462015-08-09 18:57:26 -07001038/*
1039 * ecm_interface_ipv4_neigh_get()
1040 * Returns neighbour reference for a given IP address which must be released when you are done with it.
1041 *
1042 * Returns NULL on fail.
1043 */
1044struct neighbour *ecm_interface_ipv4_neigh_get(ip_addr_t addr)
1045{
1046 struct neighbour *neigh;
1047 struct rtable *rt;
1048 struct dst_entry *dst;
1049 __be32 ipv4_addr;
1050
1051 ECM_IP_ADDR_TO_NIN4_ADDR(ipv4_addr, addr);
1052 rt = ip_route_output(&init_net, ipv4_addr, 0, 0, 0);
1053 if (IS_ERR(rt)) {
1054 return NULL;
1055 }
1056 dst = (struct dst_entry *)rt;
1057 neigh = dst_neigh_lookup(dst, &ipv4_addr);
1058 ip_rt_put(rt);
1059 return neigh;
1060}
1061
1062#ifdef ECM_IPV6_ENABLE
1063/*
1064 * ecm_interface_ipv6_neigh_get()
1065 * Returns neighbour reference for a given IP address which must be released when you are done with it.
1066 *
1067 * Returns NULL on fail.
1068 */
1069struct neighbour *ecm_interface_ipv6_neigh_get(ip_addr_t addr)
1070{
1071 struct neighbour *neigh;
1072 struct rt6_info *rt;
1073 struct dst_entry *dst;
1074 struct in6_addr ipv6_addr;
1075
1076 ECM_IP_ADDR_TO_NIN6_ADDR(ipv6_addr, addr);
1077 rt = rt6_lookup(&init_net, &ipv6_addr, NULL, 0, 0);
1078 if (!rt) {
1079 return NULL;
1080 }
1081 dst = (struct dst_entry *)rt;
1082 neigh = dst_neigh_lookup(dst, &ipv6_addr);
1083 dst_release(dst);
1084 return neigh;
1085}
1086#endif
1087
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301088/*
Shyam Sunder2ca02492016-08-05 20:55:12 +05301089 * ecm_interface_is_pptp()
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301090 * skip pptp tunnel encapsulated traffic
1091 *
Shyam Sunder23f2e542015-09-28 14:56:49 +05301092 * ECM does not handle PPTP,
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301093 * this function detects packets of that type so they can be skipped over to improve their throughput.
1094 */
Shyam Sunder2ca02492016-08-05 20:55:12 +05301095bool ecm_interface_is_pptp(struct sk_buff *skb, const struct net_device *out)
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301096{
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301097 struct net_device *in;
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301098
ratheesh kannothf4801a02016-12-09 22:46:39 +05301099 /*
1100 * skip first pass of l2tp/pptp tunnel encapsulated traffic
1101 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301102 if (out->type == ARPHRD_PPP) {
ratheesh kannothf4801a02016-12-09 22:46:39 +05301103 if (out->priv_flags & IFF_PPP_PPTP) {
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301104 return true;
1105 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301106 }
1107
1108 in = dev_get_by_index(&init_net, skb->skb_iif);
ratheesh kannoth08b8e442015-10-02 00:24:55 +05301109 if (!in) {
1110 return true;
1111 }
1112
1113 if (in->type == ARPHRD_PPP) {
ratheesh kannothf4801a02016-12-09 22:46:39 +05301114 if (in->priv_flags & IFF_PPP_PPTP) {
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301115 dev_put(in);
1116 return true;
1117 }
Shyam Sunder23f2e542015-09-28 14:56:49 +05301118 }
1119
1120 dev_put(in);
1121 return false;
1122}
1123
Shyam Sunder2ca02492016-08-05 20:55:12 +05301124#ifdef ECM_INTERFACE_PPP_ENABLE
Shyam Sunder23f2e542015-09-28 14:56:49 +05301125/*
Shyam Sunder2ca02492016-08-05 20:55:12 +05301126 * ecm_interface_is_l2tp_packet_by_version()
Shyam Sunder23f2e542015-09-28 14:56:49 +05301127 * Check version of l2tp tunnel encapsulated traffic
1128 *
1129 * ECM does not handle l2tp,
1130 * this function detects packets of that type so they can be skipped over to improve their throughput.
1131 */
Shyam Sunder2ca02492016-08-05 20:55:12 +05301132bool ecm_interface_is_l2tp_packet_by_version(struct sk_buff *skb, const struct net_device *out, int ver)
Shyam Sunder23f2e542015-09-28 14:56:49 +05301133{
ratheesh kannothf4801a02016-12-09 22:46:39 +05301134 uint32_t flag = 0;
Shyam Sunder23f2e542015-09-28 14:56:49 +05301135 struct net_device *in;
1136
ratheesh kannothf4801a02016-12-09 22:46:39 +05301137 switch (ver) {
1138 case 2:
1139 flag = IFF_PPP_L2TPV2;
1140 break;
1141 case 3:
1142 flag = IFF_PPP_L2TPV3;
1143 break;
1144 default:
1145 break;
1146 }
1147
1148 /*
1149 * skip first pass of l2tp/pptp tunnel encapsulated traffic
1150 */
Shyam Sunder23f2e542015-09-28 14:56:49 +05301151 if (out->type == ARPHRD_PPP) {
ratheesh kannothf4801a02016-12-09 22:46:39 +05301152 if (out->priv_flags & flag) {
Shyam Sunder23f2e542015-09-28 14:56:49 +05301153 return true;
1154 }
Shyam Sunder23f2e542015-09-28 14:56:49 +05301155 }
1156
1157 in = dev_get_by_index(&init_net, skb->skb_iif);
1158 if (!in) {
1159 return true;
1160 }
1161
1162 if (in->type == ARPHRD_PPP) {
ratheesh kannothf4801a02016-12-09 22:46:39 +05301163 if (in->priv_flags & flag) {
Shyam Sunder23f2e542015-09-28 14:56:49 +05301164 dev_put(in);
1165 return true;
1166 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301167 }
1168
ratheesh kannoth08b8e442015-10-02 00:24:55 +05301169 dev_put(in);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301170 return false;
1171}
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301172
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001173/*
Shyam Sunder2ca02492016-08-05 20:55:12 +05301174 * ecm_interface_is_l2tp_pptp()
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001175 * skip l2tp/pptp tunnel encapsulated traffic
1176 *
1177 * ECM does not handle L2TP or PPTP encapsulated packets,
1178 * this function detects packets of that type so they can be skipped over to improve their throughput.
1179 */
Shyam Sunder2ca02492016-08-05 20:55:12 +05301180bool ecm_interface_is_l2tp_pptp(struct sk_buff *skb, const struct net_device *out)
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001181{
ratheesh kannothf4801a02016-12-09 22:46:39 +05301182 struct net_device *in;
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001183
1184 /*
1185 * skip first pass of l2tp/pptp tunnel encapsulated traffic
1186 */
1187 if (out->type == ARPHRD_PPP) {
ratheesh kannothf4801a02016-12-09 22:46:39 +05301188 if (out->priv_flags & (IFF_PPP_L2TPV2 | IFF_PPP_L2TPV3 |
1189 IFF_PPP_PPTP)) {
1190 return true;
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001191 }
1192 }
1193
ratheesh kannothf4801a02016-12-09 22:46:39 +05301194 in = dev_get_by_index(&init_net, skb->skb_iif);
1195 if (!in) {
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001196 return true;
1197 }
1198
ratheesh kannothf4801a02016-12-09 22:46:39 +05301199 if (in->type == ARPHRD_PPP) {
1200 if (in->priv_flags & (IFF_PPP_L2TPV2 | IFF_PPP_L2TPV3 |
1201 IFF_PPP_PPTP)) {
1202 dev_put(in);
1203 return true;
1204 }
1205 }
1206
1207 dev_put(in);
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001208 return false;
ratheesh kannothf4801a02016-12-09 22:46:39 +05301209
Xiaoping Fan80e406b2015-01-29 16:15:31 -08001210}
1211#endif
1212
Gareth Williams141d2382014-11-25 11:35:19 -08001213#ifdef ECM_INTERFACE_VLAN_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001214/*
Ben Menchaca84f36632014-02-28 20:57:38 +00001215 * ecm_interface_vlan_interface_establish()
1216 * Returns a reference to a iface of the VLAN type, possibly creating one if necessary.
1217 * Returns NULL on failure or a reference to interface.
1218 */
1219static struct ecm_db_iface_instance *ecm_interface_vlan_interface_establish(struct ecm_db_interface_info_vlan *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001220 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001221{
1222 struct ecm_db_iface_instance *nii;
1223 struct ecm_db_iface_instance *ii;
1224
Murat Sezgin91c5d712015-06-12 15:16:22 -07001225 DEBUG_INFO("Establish VLAN iface: %s with address: %pM, vlan tag: %u, vlan_tpid: %x MTU: %d, if num: %d, accel engine if id: %d\n",
1226 dev_name, type_info->address, type_info->vlan_tag, type_info->vlan_tpid, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001227
1228 /*
1229 * Locate the iface
1230 */
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05301231 ii = ecm_db_iface_find_and_ref_vlan(type_info->address, type_info->vlan_tag, type_info->vlan_tpid);
Ben Menchaca84f36632014-02-28 20:57:38 +00001232 if (ii) {
1233 DEBUG_TRACE("%p: iface established\n", ii);
1234 return ii;
1235 }
1236
1237 /*
1238 * No iface - create one
1239 */
1240 nii = ecm_db_iface_alloc();
1241 if (!nii) {
1242 DEBUG_WARN("Failed to establish iface\n");
1243 return NULL;
1244 }
1245
1246 /*
1247 * Add iface into the database, atomically to avoid races creating the same thing
1248 */
1249 spin_lock_bh(&ecm_interface_lock);
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05301250 ii = ecm_db_iface_find_and_ref_vlan(type_info->address, type_info->vlan_tag, type_info->vlan_tpid);
Ben Menchaca84f36632014-02-28 20:57:38 +00001251 if (ii) {
1252 spin_unlock_bh(&ecm_interface_lock);
1253 ecm_db_iface_deref(nii);
1254 return ii;
1255 }
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05301256 ecm_db_iface_add_vlan(nii, type_info->address, type_info->vlan_tag, type_info->vlan_tpid, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001257 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001258 spin_unlock_bh(&ecm_interface_lock);
1259
1260 DEBUG_TRACE("%p: vlan iface established\n", nii);
1261 return nii;
1262}
Gareth Williams141d2382014-11-25 11:35:19 -08001263#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001264
1265/*
1266 * ecm_interface_bridge_interface_establish()
1267 * Returns a reference to a iface of the BRIDGE type, possibly creating one if necessary.
1268 * Returns NULL on failure or a reference to interface.
1269 */
1270static struct ecm_db_iface_instance *ecm_interface_bridge_interface_establish(struct ecm_db_interface_info_bridge *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001271 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001272{
1273 struct ecm_db_iface_instance *nii;
1274 struct ecm_db_iface_instance *ii;
1275
Murat Sezgin91c5d712015-06-12 15:16:22 -07001276 DEBUG_INFO("Establish BRIDGE iface: %s with address: %pM, MTU: %d, if num: %d, accel engine if id: %d\n",
1277 dev_name, type_info->address, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001278
1279 /*
1280 * Locate the iface
1281 */
1282 ii = ecm_db_iface_find_and_ref_bridge(type_info->address);
1283 if (ii) {
1284 DEBUG_TRACE("%p: iface established\n", ii);
1285 return ii;
1286 }
1287
1288 /*
1289 * No iface - create one
1290 */
1291 nii = ecm_db_iface_alloc();
1292 if (!nii) {
1293 DEBUG_WARN("Failed to establish iface\n");
1294 return NULL;
1295 }
1296
1297 /*
1298 * Add iface into the database, atomically to avoid races creating the same thing
1299 */
1300 spin_lock_bh(&ecm_interface_lock);
1301 ii = ecm_db_iface_find_and_ref_bridge(type_info->address);
1302 if (ii) {
1303 spin_unlock_bh(&ecm_interface_lock);
1304 ecm_db_iface_deref(nii);
1305 return ii;
1306 }
1307 ecm_db_iface_add_bridge(nii, type_info->address, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001308 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001309 spin_unlock_bh(&ecm_interface_lock);
1310
1311 DEBUG_TRACE("%p: bridge iface established\n", nii);
1312 return nii;
1313}
1314
Murat Sezgin910c9662015-03-11 16:15:06 -07001315#ifdef ECM_INTERFACE_BOND_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001316/*
1317 * ecm_interface_lag_interface_establish()
1318 * Returns a reference to a iface of the LAG type, possibly creating one if necessary.
1319 * Returns NULL on failure or a reference to interface.
1320 */
1321static struct ecm_db_iface_instance *ecm_interface_lag_interface_establish(struct ecm_db_interface_info_lag *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001322 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001323{
1324 struct ecm_db_iface_instance *nii;
1325 struct ecm_db_iface_instance *ii;
1326
Murat Sezgin91c5d712015-06-12 15:16:22 -07001327 DEBUG_INFO("Establish LAG iface: %s with address: %pM, MTU: %d, if num: %d, accel engine if id: %d\n",
1328 dev_name, type_info->address, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001329
1330 /*
1331 * Locate the iface
1332 */
1333 ii = ecm_db_iface_find_and_ref_lag(type_info->address);
1334 if (ii) {
1335 DEBUG_TRACE("%p: iface established\n", ii);
1336 return ii;
1337 }
1338
1339 /*
1340 * No iface - create one
1341 */
1342 nii = ecm_db_iface_alloc();
1343 if (!nii) {
1344 DEBUG_WARN("Failed to establish iface\n");
1345 return NULL;
1346 }
1347
1348 /*
1349 * Add iface into the database, atomically to avoid races creating the same thing
1350 */
1351 spin_lock_bh(&ecm_interface_lock);
1352 ii = ecm_db_iface_find_and_ref_lag(type_info->address);
1353 if (ii) {
1354 spin_unlock_bh(&ecm_interface_lock);
1355 ecm_db_iface_deref(nii);
1356 return ii;
1357 }
1358 ecm_db_iface_add_lag(nii, type_info->address, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001359 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001360 spin_unlock_bh(&ecm_interface_lock);
1361
1362 DEBUG_TRACE("%p: lag iface established\n", nii);
1363 return nii;
1364}
Murat Sezgin910c9662015-03-11 16:15:06 -07001365#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001366
1367/*
1368 * ecm_interface_ethernet_interface_establish()
1369 * Returns a reference to a iface of the ETHERNET type, possibly creating one if necessary.
1370 * Returns NULL on failure or a reference to interface.
1371 */
1372static struct ecm_db_iface_instance *ecm_interface_ethernet_interface_establish(struct ecm_db_interface_info_ethernet *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001373 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001374{
1375 struct ecm_db_iface_instance *nii;
1376 struct ecm_db_iface_instance *ii;
1377
Murat Sezgin91c5d712015-06-12 15:16:22 -07001378 DEBUG_INFO("Establish ETHERNET iface: %s with address: %pM, MTU: %d, if num: %d, accel engine if id: %d\n",
1379 dev_name, type_info->address, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001380
1381 /*
1382 * Locate the iface
1383 */
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05301384 ii = ecm_db_iface_ifidx_find_and_ref_ethernet(type_info->address, dev_interface_num);
1385
Ben Menchaca84f36632014-02-28 20:57:38 +00001386 if (ii) {
1387 DEBUG_TRACE("%p: iface established\n", ii);
Murat Sezgin5f2947a2016-06-28 12:09:33 -07001388 /*
1389 * Update the accel engine interface identifier, just in case it was changed.
1390 */
1391 ecm_db_iface_ae_interface_identifier_set(ii, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001392 return ii;
1393 }
1394
1395 /*
1396 * No iface - create one
1397 */
1398 nii = ecm_db_iface_alloc();
1399 if (!nii) {
1400 DEBUG_WARN("Failed to establish iface\n");
1401 return NULL;
1402 }
1403
1404 /*
1405 * Add iface into the database, atomically to avoid races creating the same thing
1406 */
1407 spin_lock_bh(&ecm_interface_lock);
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05301408 ii = ecm_db_iface_ifidx_find_and_ref_ethernet(type_info->address, dev_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001409 if (ii) {
1410 spin_unlock_bh(&ecm_interface_lock);
1411 ecm_db_iface_deref(nii);
1412 return ii;
1413 }
1414 ecm_db_iface_add_ethernet(nii, type_info->address, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001415 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001416 spin_unlock_bh(&ecm_interface_lock);
1417
1418 DEBUG_TRACE("%p: ethernet iface established\n", nii);
1419 return nii;
1420}
1421
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301422#ifdef ECM_INTERFACE_PPPOE_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001423/*
1424 * ecm_interface_pppoe_interface_establish()
1425 * Returns a reference to a iface of the PPPoE type, possibly creating one if necessary.
1426 * Returns NULL on failure or a reference to interface.
1427 */
1428static struct ecm_db_iface_instance *ecm_interface_pppoe_interface_establish(struct ecm_db_interface_info_pppoe *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001429 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001430{
1431 struct ecm_db_iface_instance *nii;
1432 struct ecm_db_iface_instance *ii;
1433
Murat Sezgin91c5d712015-06-12 15:16:22 -07001434 DEBUG_INFO("Establish PPPoE iface: %s with session id: %u, remote mac: %pM, MTU: %d, if num: %d, accel engine if id: %d\n",
1435 dev_name, type_info->pppoe_session_id, type_info->remote_mac, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001436
1437 /*
1438 * Locate the iface
1439 */
1440 ii = ecm_db_iface_find_and_ref_pppoe(type_info->pppoe_session_id, type_info->remote_mac);
1441 if (ii) {
1442 DEBUG_TRACE("%p: iface established\n", ii);
1443 return ii;
1444 }
1445
1446 /*
1447 * No iface - create one
1448 */
1449 nii = ecm_db_iface_alloc();
1450 if (!nii) {
1451 DEBUG_WARN("Failed to establish iface\n");
1452 return NULL;
1453 }
1454
1455 /*
1456 * Add iface into the database, atomically to avoid races creating the same thing
1457 */
1458 spin_lock_bh(&ecm_interface_lock);
1459 ii = ecm_db_iface_find_and_ref_pppoe(type_info->pppoe_session_id, type_info->remote_mac);
1460 if (ii) {
1461 spin_unlock_bh(&ecm_interface_lock);
1462 ecm_db_iface_deref(nii);
1463 return ii;
1464 }
1465 ecm_db_iface_add_pppoe(nii, type_info->pppoe_session_id, type_info->remote_mac, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001466 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001467 spin_unlock_bh(&ecm_interface_lock);
1468
1469 DEBUG_TRACE("%p: pppoe iface established\n", nii);
1470 return nii;
1471}
Gareth Williamsc5b9d712014-05-09 20:40:07 +01001472#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001473
ratheesh kannothcfdcb332015-12-24 07:19:18 +05301474#ifdef ECM_INTERFACE_MAP_T_ENABLE
1475/*
1476 * ecm_interface_map_t_interface_establish()
1477 * Returns a reference to a iface of the PPPoE type, possibly creating one if necessary.
1478 * Returns NULL on failure or a reference to interface.
1479 */
1480static struct ecm_db_iface_instance *ecm_interface_map_t_interface_establish(struct ecm_db_interface_info_map_t *type_info,
1481 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
1482{
1483 struct ecm_db_iface_instance *nii;
1484 struct ecm_db_iface_instance *ii;
1485
1486 DEBUG_TRACE("Establish MAP-T iface: %s MTU: %d, if num: %d, accel engine if id: %d\n",
1487 dev_name, mtu, dev_interface_num, ae_interface_num);
1488
1489 /*
1490 * Locate the iface
1491 */
Manish Vermaf70e1612018-08-20 01:15:40 +05301492 ii = ecm_db_iface_find_and_ref_map_t(type_info->if_index, ae_interface_num);
ratheesh kannothcfdcb332015-12-24 07:19:18 +05301493 if (ii) {
1494 DEBUG_TRACE("%p: iface established\n", ii);
ratheesh kannothcfdcb332015-12-24 07:19:18 +05301495 return ii;
1496 }
1497
1498 /*
1499 * No iface - create one
1500 */
1501 nii = ecm_db_iface_alloc();
1502 if (!nii) {
1503 DEBUG_WARN("Failed to establish iface\n");
1504 return NULL;
1505 }
1506
1507 /*
1508 * Add iface into the database, atomically to avoid races creating the same thing
1509 */
1510 spin_lock_bh(&ecm_interface_lock);
Manish Vermaf70e1612018-08-20 01:15:40 +05301511 ii = ecm_db_iface_find_and_ref_map_t(type_info->if_index, ae_interface_num);
ratheesh kannothcfdcb332015-12-24 07:19:18 +05301512 if (ii) {
1513 spin_unlock_bh(&ecm_interface_lock);
1514 ecm_db_iface_deref(nii);
ratheesh kannothcfdcb332015-12-24 07:19:18 +05301515 return ii;
1516 }
1517 ecm_db_iface_add_map_t(nii, type_info, dev_name,
1518 mtu, dev_interface_num, ae_interface_num, NULL, nii);
1519 spin_unlock_bh(&ecm_interface_lock);
1520
1521 DEBUG_TRACE("%p: map_t iface established\n", nii);
1522 return nii;
1523}
1524#endif
1525
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301526#ifdef ECM_INTERFACE_L2TPV2_ENABLE
1527/*
1528 * ecm_interface_pppol2tpv2_interface_establish()
1529 * Returns a reference to a iface of the PPPoL2TPV2 type, possibly creating one if necessary.
1530 * Returns NULL on failure or a reference to interface.
1531 */
1532static struct ecm_db_iface_instance *ecm_interface_pppol2tpv2_interface_establish(struct ecm_db_interface_info_pppol2tpv2 *type_info,
1533 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
1534{
1535 struct ecm_db_iface_instance *nii;
1536 struct ecm_db_iface_instance *ii;
1537
1538 DEBUG_INFO("Establish PPPol2tp iface: %s with tunnel id=%u session id %u\n", dev_name, type_info->l2tp.tunnel.tunnel_id,
1539 type_info->l2tp.session.session_id);
1540 /*
1541 * Locate the iface
1542 */
1543 ii = ecm_db_iface_find_and_ref_pppol2tpv2(type_info->l2tp.tunnel.tunnel_id, type_info->l2tp.session.session_id);
1544 if (ii) {
1545 DEBUG_TRACE("%p: iface established\n", ii);
ratheesh kannothed721852015-09-28 12:39:52 +05301546 ecm_db_iface_update_ae_interface_identifier(ii, ae_interface_num);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301547 return ii;
1548 }
1549
1550 /*
1551 * No iface - create one
1552 */
1553 nii = ecm_db_iface_alloc();
1554 if (!nii) {
1555 DEBUG_WARN("Failed to establish iface\n");
1556 return NULL;
1557 }
1558
1559 /*
1560 * Add iface into the database, atomically to avoid races creating the same thing
1561 */
1562 spin_lock_bh(&ecm_interface_lock);
1563 ii = ecm_db_iface_find_and_ref_pppol2tpv2(type_info->l2tp.tunnel.tunnel_id, type_info->l2tp.session.session_id);
1564 if (ii) {
1565 spin_unlock_bh(&ecm_interface_lock);
1566 ecm_db_iface_deref(nii);
ratheesh kannothed721852015-09-28 12:39:52 +05301567 ecm_db_iface_update_ae_interface_identifier(ii, ae_interface_num);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301568 return ii;
1569 }
1570
1571 ecm_db_iface_add_pppol2tpv2(nii, type_info, dev_name, mtu, dev_interface_num, ae_interface_num, NULL, nii);
1572 spin_unlock_bh(&ecm_interface_lock);
1573
1574 DEBUG_TRACE("%p: pppol2tpv2 iface established\n", nii);
1575 return nii;
1576}
1577
1578#endif
1579
Shyam Sunder23f2e542015-09-28 14:56:49 +05301580#ifdef ECM_INTERFACE_PPTP_ENABLE
1581/*
1582 * ecm_interface_pptp_interface_establish()
1583 * Returns a reference to a iface of the PPTP type, possibly creating one if necessary.
1584 * Returns NULL on failure or a reference to interface.
1585 */
1586static struct ecm_db_iface_instance *ecm_interface_pptp_interface_establish(struct ecm_db_interface_info_pptp *type_info,
1587 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
1588{
1589 struct ecm_db_iface_instance *nii;
1590 struct ecm_db_iface_instance *ii;
1591
1592 DEBUG_INFO("Establish PPTP iface: %s with local call id %u peer call id %u\n", dev_name, type_info->src_call_id,
1593 type_info->dst_call_id);
1594 /*
1595 * Locate the iface
1596 */
Swaraj Sha05620582018-08-15 23:21:16 +05301597 ii = ecm_db_iface_find_and_ref_pptp(type_info->src_call_id, type_info->dst_call_id, ae_interface_num);
Shyam Sunder23f2e542015-09-28 14:56:49 +05301598 if (ii) {
1599 DEBUG_TRACE("%p: iface established\n", ii);
1600 ecm_db_iface_update_ae_interface_identifier(ii, ae_interface_num);
1601 return ii;
1602 }
1603
1604 /*
1605 * No iface - create one
1606 */
1607 nii = ecm_db_iface_alloc();
1608 if (!nii) {
1609 DEBUG_WARN("Failed to establish iface\n");
1610 return NULL;
1611 }
1612
1613 /*
1614 * Add iface into the database, atomically to avoid races creating the same thing
1615 */
1616 spin_lock_bh(&ecm_interface_lock);
Swaraj Sha05620582018-08-15 23:21:16 +05301617 ii = ecm_db_iface_find_and_ref_pptp(type_info->src_call_id, type_info->dst_call_id, ae_interface_num);
Shyam Sunder23f2e542015-09-28 14:56:49 +05301618 if (ii) {
1619 spin_unlock_bh(&ecm_interface_lock);
1620 ecm_db_iface_deref(nii);
1621 ecm_db_iface_update_ae_interface_identifier(ii, ae_interface_num);
1622 return ii;
1623 }
1624
1625 ecm_db_iface_add_pptp(nii, type_info, dev_name, mtu, dev_interface_num, ae_interface_num, NULL, nii);
1626 spin_unlock_bh(&ecm_interface_lock);
1627
1628 DEBUG_TRACE("%p: pptp iface established\n", nii);
1629 return nii;
1630}
1631#endif
1632
Suman Ghosh9e4245f2018-04-11 01:11:41 +05301633#ifdef ECM_INTERFACE_GRE_TUN_ENABLE
1634/*
1635 * ecm_interface_gre_tun_interface_establish()
1636 * Returns a reference to a iface of the gre type, possibly creating one if necessary.
1637 * Returns NULL on failure or a reference to interface.
1638 */
1639static struct ecm_db_iface_instance *ecm_interface_gre_tun_interface_establish(struct ecm_db_interface_info_gre_tun *type_info,
1640 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
1641{
1642 struct ecm_db_iface_instance *nii;
1643 struct ecm_db_iface_instance *ii;
1644
1645 DEBUG_TRACE("Establish GRE TUN iface: %s MTU: %d, if num: %d, accel engine if id: %d\n",
1646 dev_name, mtu, dev_interface_num, ae_interface_num);
1647
1648 /*
1649 * Locate the iface
1650 */
Suman Ghosh8f199b22018-07-13 18:51:27 +05301651 ii = ecm_db_iface_find_and_ref_gre_tun(type_info->if_index, ae_interface_num);
Suman Ghosh9e4245f2018-04-11 01:11:41 +05301652 if (ii) {
1653 DEBUG_TRACE("%p: iface established\n", ii);
Suman Ghosh9e4245f2018-04-11 01:11:41 +05301654 return ii;
1655 }
1656
1657 /*
1658 * No iface - create one
1659 */
1660 nii = ecm_db_iface_alloc();
1661 if (!nii) {
1662 DEBUG_WARN("Failed to establish iface\n");
1663 return NULL;
1664 }
1665
1666 /*
1667 * Add iface into the database, atomically to avoid races creating the same thing
1668 */
1669 spin_lock_bh(&ecm_interface_lock);
Suman Ghosh8f199b22018-07-13 18:51:27 +05301670 ii = ecm_db_iface_find_and_ref_gre_tun(type_info->if_index, ae_interface_num);
Suman Ghosh9e4245f2018-04-11 01:11:41 +05301671 if (ii) {
1672 spin_unlock_bh(&ecm_interface_lock);
1673 ecm_db_iface_deref(nii);
Suman Ghosh9e4245f2018-04-11 01:11:41 +05301674 return ii;
1675 }
1676 ecm_db_iface_add_gre_tun(nii, type_info, dev_name,
1677 mtu, dev_interface_num, ae_interface_num, NULL, nii);
1678 spin_unlock_bh(&ecm_interface_lock);
1679
1680 DEBUG_TRACE("%p: gre iface established\n", nii);
1681 return nii;
1682}
1683#endif
1684
Ben Menchaca84f36632014-02-28 20:57:38 +00001685/*
1686 * ecm_interface_unknown_interface_establish()
1687 * Returns a reference to a iface of the UNKNOWN type, possibly creating one if necessary.
1688 * Returns NULL on failure or a reference to interface.
1689 */
1690static struct ecm_db_iface_instance *ecm_interface_unknown_interface_establish(struct ecm_db_interface_info_unknown *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001691 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001692{
1693 struct ecm_db_iface_instance *nii;
1694 struct ecm_db_iface_instance *ii;
1695
Murat Sezgin91c5d712015-06-12 15:16:22 -07001696 DEBUG_INFO("Establish UNKNOWN iface: %s with os_specific_ident: %u, MTU: %d, if num: %d, accel engine if id: %d\n",
1697 dev_name, type_info->os_specific_ident, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001698
1699 /*
1700 * Locate the iface
1701 */
1702 ii = ecm_db_iface_find_and_ref_unknown(type_info->os_specific_ident);
1703 if (ii) {
1704 DEBUG_TRACE("%p: iface established\n", ii);
1705 return ii;
1706 }
1707
1708 /*
1709 * No iface - create one
1710 */
1711 nii = ecm_db_iface_alloc();
1712 if (!nii) {
1713 DEBUG_WARN("Failed to establish iface\n");
1714 return NULL;
1715 }
1716
1717 /*
1718 * Add iface into the database, atomically to avoid races creating the same thing
1719 */
1720 spin_lock_bh(&ecm_interface_lock);
1721 ii = ecm_db_iface_find_and_ref_unknown(type_info->os_specific_ident);
1722 if (ii) {
1723 spin_unlock_bh(&ecm_interface_lock);
1724 ecm_db_iface_deref(nii);
1725 return ii;
1726 }
1727 ecm_db_iface_add_unknown(nii, type_info->os_specific_ident, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001728 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001729 spin_unlock_bh(&ecm_interface_lock);
1730
1731 DEBUG_TRACE("%p: unknown iface established\n", nii);
1732 return nii;
1733}
1734
1735/*
1736 * ecm_interface_loopback_interface_establish()
1737 * Returns a reference to a iface of the LOOPBACK type, possibly creating one if necessary.
1738 * Returns NULL on failure or a reference to interface.
1739 */
1740static struct ecm_db_iface_instance *ecm_interface_loopback_interface_establish(struct ecm_db_interface_info_loopback *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001741 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001742{
1743 struct ecm_db_iface_instance *nii;
1744 struct ecm_db_iface_instance *ii;
1745
Murat Sezgin91c5d712015-06-12 15:16:22 -07001746 DEBUG_INFO("Establish LOOPBACK iface: %s with os_specific_ident: %u, MTU: %d, if num: %d, accel engine if id: %d\n",
1747 dev_name, type_info->os_specific_ident, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001748
1749 /*
1750 * Locate the iface
1751 */
1752 ii = ecm_db_iface_find_and_ref_loopback(type_info->os_specific_ident);
1753 if (ii) {
1754 DEBUG_TRACE("%p: iface established\n", ii);
1755 return ii;
1756 }
1757
1758 /*
1759 * No iface - create one
1760 */
1761 nii = ecm_db_iface_alloc();
1762 if (!nii) {
1763 DEBUG_WARN("Failed to establish iface\n");
1764 return NULL;
1765 }
1766
1767 /*
1768 * Add iface into the database, atomically to avoid races creating the same thing
1769 */
1770 spin_lock_bh(&ecm_interface_lock);
1771 ii = ecm_db_iface_find_and_ref_loopback(type_info->os_specific_ident);
1772 if (ii) {
1773 spin_unlock_bh(&ecm_interface_lock);
1774 ecm_db_iface_deref(nii);
1775 return ii;
1776 }
1777 ecm_db_iface_add_loopback(nii, type_info->os_specific_ident, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001778 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001779 spin_unlock_bh(&ecm_interface_lock);
1780
1781 DEBUG_TRACE("%p: loopback iface established\n", nii);
1782 return nii;
1783}
1784
Murat Sezgin69a27532015-03-12 14:09:40 -07001785#ifdef ECM_INTERFACE_IPSEC_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001786/*
1787 * ecm_interface_ipsec_tunnel_interface_establish()
1788 * Returns a reference to a iface of the IPSEC_TUNNEL type, possibly creating one if necessary.
1789 * Returns NULL on failure or a reference to interface.
1790 *
1791 * NOTE: GGG TODO THIS NEEDS TO TAKE A PROPER APPROACH TO IPSEC TUNNELS USING ENDPOINT ADDRESSING AS THE TYPE INFO KEYS
1792 */
1793static struct ecm_db_iface_instance *ecm_interface_ipsec_tunnel_interface_establish(struct ecm_db_interface_info_ipsec_tunnel *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001794 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001795{
1796 struct ecm_db_iface_instance *nii;
1797 struct ecm_db_iface_instance *ii;
1798
Murat Sezgin91c5d712015-06-12 15:16:22 -07001799 DEBUG_INFO("Establish IPSEC_TUNNEL iface: %s with os_specific_ident: %u, MTU: %d, if num: %d, accel engine if id: %d\n",
1800 dev_name, type_info->os_specific_ident, mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001801
1802 /*
1803 * Locate the iface
1804 */
1805 ii = ecm_db_iface_find_and_ref_ipsec_tunnel(type_info->os_specific_ident);
1806 if (ii) {
1807 DEBUG_TRACE("%p: iface established\n", ii);
1808 return ii;
1809 }
1810
1811 /*
1812 * No iface - create one
1813 */
1814 nii = ecm_db_iface_alloc();
1815 if (!nii) {
1816 DEBUG_WARN("Failed to establish iface\n");
1817 return NULL;
1818 }
1819
1820 /*
1821 * Add iface into the database, atomically to avoid races creating the same thing
1822 */
1823 spin_lock_bh(&ecm_interface_lock);
1824 ii = ecm_db_iface_find_and_ref_ipsec_tunnel(type_info->os_specific_ident);
1825 if (ii) {
1826 spin_unlock_bh(&ecm_interface_lock);
1827 ecm_db_iface_deref(nii);
1828 return ii;
1829 }
1830 ecm_db_iface_add_ipsec_tunnel(nii, type_info->os_specific_ident, dev_name,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001831 mtu, dev_interface_num, ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001832 spin_unlock_bh(&ecm_interface_lock);
1833
1834 DEBUG_TRACE("%p: ipsec_tunnel iface established\n", nii);
1835 return nii;
1836}
Murat Sezgin69a27532015-03-12 14:09:40 -07001837#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001838
Murat Sezginb3731e82014-11-26 12:20:59 -08001839#ifdef ECM_INTERFACE_SIT_ENABLE
Murat Sezginbde55f92015-03-11 16:44:11 -07001840#ifdef CONFIG_IPV6_SIT_6RD
Ben Menchaca84f36632014-02-28 20:57:38 +00001841/*
1842 * ecm_interface_sit_interface_establish()
1843 * Returns a reference to a iface of the SIT type, possibly creating one if necessary.
1844 * Returns NULL on failure or a reference to interface.
1845 */
1846static struct ecm_db_iface_instance *ecm_interface_sit_interface_establish(struct ecm_db_interface_info_sit *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001847 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001848{
1849 struct ecm_db_iface_instance *nii;
1850 struct ecm_db_iface_instance *ii;
1851
Murat Sezgin91c5d712015-06-12 15:16:22 -07001852 DEBUG_INFO("Establish SIT iface: %s with saddr: " ECM_IP_ADDR_OCTAL_FMT ", daddr: " ECM_IP_ADDR_OCTAL_FMT ", MTU: %d, if num: %d, accel engine if id: %d\n",
1853 dev_name, ECM_IP_ADDR_TO_OCTAL(type_info->saddr), ECM_IP_ADDR_TO_OCTAL(type_info->daddr), mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001854
1855 /*
1856 * Locate the iface
1857 */
Murat Sezgined9c1c62017-07-14 16:57:23 -07001858 ii = ecm_db_iface_find_and_ref_sit(type_info->saddr, type_info->daddr, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001859 if (ii) {
1860 DEBUG_TRACE("%p: iface established\n", ii);
1861 return ii;
1862 }
1863
1864 /*
1865 * No iface - create one
1866 */
1867 nii = ecm_db_iface_alloc();
1868 if (!nii) {
1869 DEBUG_WARN("Failed to establish iface\n");
1870 return NULL;
1871 }
1872
1873 /*
1874 * Add iface into the database, atomically to avoid races creating the same thing
1875 */
1876 spin_lock_bh(&ecm_interface_lock);
Murat Sezgined9c1c62017-07-14 16:57:23 -07001877 ii = ecm_db_iface_find_and_ref_sit(type_info->saddr, type_info->daddr, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001878 if (ii) {
1879 spin_unlock_bh(&ecm_interface_lock);
1880 ecm_db_iface_deref(nii);
1881 return ii;
1882 }
1883 ecm_db_iface_add_sit(nii, type_info, dev_name, mtu, dev_interface_num,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001884 ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001885 spin_unlock_bh(&ecm_interface_lock);
1886
1887 DEBUG_TRACE("%p: sit iface established\n", nii);
1888 return nii;
1889}
Murat Sezgincc6eedf2014-05-09 23:59:19 -07001890#endif
Murat Sezginb3731e82014-11-26 12:20:59 -08001891#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001892
Murat Sezginc1402562015-03-12 12:32:20 -07001893#ifdef ECM_INTERFACE_TUNIPIP6_ENABLE
Gareth Williams8ac34292015-03-17 14:06:58 +00001894#ifdef ECM_IPV6_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001895/*
1896 * ecm_interface_tunipip6_interface_establish()
1897 * Returns a reference to a iface of the TUNIPIP6 type, possibly creating one if necessary.
1898 * Returns NULL on failure or a reference to interface.
1899 */
1900static struct ecm_db_iface_instance *ecm_interface_tunipip6_interface_establish(struct ecm_db_interface_info_tunipip6 *type_info,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001901 char *dev_name, int32_t dev_interface_num, int32_t ae_interface_num, int32_t mtu)
Ben Menchaca84f36632014-02-28 20:57:38 +00001902{
1903 struct ecm_db_iface_instance *nii;
1904 struct ecm_db_iface_instance *ii;
1905
Murat Sezgin91c5d712015-06-12 15:16:22 -07001906 DEBUG_INFO("Establish TUNIPIP6 iface: %s with saddr: " ECM_IP_ADDR_OCTAL_FMT ", daddr: " ECM_IP_ADDR_OCTAL_FMT ", MTU: %d, if num: %d, accel engine if id: %d\n",
1907 dev_name, ECM_IP_ADDR_TO_OCTAL(type_info->saddr), ECM_IP_ADDR_TO_OCTAL(type_info->daddr), mtu, dev_interface_num, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001908
1909 /*
1910 * Locate the iface
1911 */
Apoorv Gupta39b33282018-08-02 09:25:59 +05301912 ii = ecm_db_iface_find_and_ref_tunipip6(type_info->saddr, type_info->daddr, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001913 if (ii) {
1914 DEBUG_TRACE("%p: iface established\n", ii);
1915 return ii;
1916 }
1917
1918 /*
1919 * No iface - create one
1920 */
1921 nii = ecm_db_iface_alloc();
1922 if (!nii) {
1923 DEBUG_WARN("Failed to establish iface\n");
1924 return NULL;
1925 }
1926
1927 /*
1928 * Add iface into the database, atomically to avoid races creating the same thing
1929 */
1930 spin_lock_bh(&ecm_interface_lock);
Apoorv Gupta39b33282018-08-02 09:25:59 +05301931 ii = ecm_db_iface_find_and_ref_tunipip6(type_info->saddr, type_info->daddr, ae_interface_num);
Ben Menchaca84f36632014-02-28 20:57:38 +00001932 if (ii) {
1933 spin_unlock_bh(&ecm_interface_lock);
1934 ecm_db_iface_deref(nii);
1935 return ii;
1936 }
1937 ecm_db_iface_add_tunipip6(nii, type_info, dev_name, mtu, dev_interface_num,
Murat Sezgin91c5d712015-06-12 15:16:22 -07001938 ae_interface_num, NULL, nii);
Ben Menchaca84f36632014-02-28 20:57:38 +00001939 spin_unlock_bh(&ecm_interface_lock);
1940
1941 DEBUG_TRACE("%p: tunipip6 iface established\n", nii);
1942 return nii;
1943}
Murat Sezginc1402562015-03-12 12:32:20 -07001944#endif
Gareth Williams8ac34292015-03-17 14:06:58 +00001945#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001946
1947/*
1948 * ecm_interface_establish_and_ref()
1949 * Establish an interface instance for the given interface detail.
1950 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07001951struct ecm_db_iface_instance *ecm_interface_establish_and_ref(struct ecm_front_end_connection_instance *feci,
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301952 struct net_device *dev, struct sk_buff *skb)
Ben Menchaca84f36632014-02-28 20:57:38 +00001953{
1954 int32_t dev_interface_num;
1955 char *dev_name;
1956 int32_t dev_type;
1957 int32_t dev_mtu;
Murat Sezgin91c5d712015-06-12 15:16:22 -07001958 int32_t ae_interface_num;
Ben Menchaca84f36632014-02-28 20:57:38 +00001959 struct ecm_db_iface_instance *ii;
Suman Ghosh8f199b22018-07-13 18:51:27 +05301960 int32_t interface_type;
Ben Menchaca84f36632014-02-28 20:57:38 +00001961 union {
1962 struct ecm_db_interface_info_ethernet ethernet; /* type == ECM_DB_IFACE_TYPE_ETHERNET */
Gareth Williams141d2382014-11-25 11:35:19 -08001963#ifdef ECM_INTERFACE_VLAN_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001964 struct ecm_db_interface_info_vlan vlan; /* type == ECM_DB_IFACE_TYPE_VLAN */
Gareth Williams141d2382014-11-25 11:35:19 -08001965#endif
Murat Sezgin910c9662015-03-11 16:15:06 -07001966#ifdef ECM_INTERFACE_BOND_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001967 struct ecm_db_interface_info_lag lag; /* type == ECM_DB_IFACE_TYPE_LAG */
Murat Sezgin910c9662015-03-11 16:15:06 -07001968#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001969 struct ecm_db_interface_info_bridge bridge; /* type == ECM_DB_IFACE_TYPE_BRIDGE */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301970#ifdef ECM_INTERFACE_PPPOE_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001971 struct ecm_db_interface_info_pppoe pppoe; /* type == ECM_DB_IFACE_TYPE_PPPOE */
Murat Sezginaad635c2015-03-06 16:11:41 -08001972#endif
ratheesh kannotha32fdd12015-09-09 08:02:58 +05301973#ifdef ECM_INTERFACE_L2TPV2_ENABLE
1974 struct ecm_db_interface_info_pppol2tpv2 pppol2tpv2; /* type == ECM_DB_IFACE_TYPE_PPPOL2TPV2 */
1975#endif
Shyam Sunder23f2e542015-09-28 14:56:49 +05301976#ifdef ECM_INTERFACE_PPTP_ENABLE
1977 struct ecm_db_interface_info_pptp pptp; /* type == ECM_DB_IFACE_TYPE_PPTP */
1978#endif
ratheesh kannothcfdcb332015-12-24 07:19:18 +05301979#ifdef ECM_INTERFACE_MAP_T_ENABLE
1980 struct ecm_db_interface_info_map_t map_t; /* type == ECM_DB_IFACE_TYPE_MAP_T */
1981#endif
Suman Ghosh9e4245f2018-04-11 01:11:41 +05301982#ifdef ECM_INTERFACE_GRE_TUN_ENABLE
1983 struct ecm_db_interface_info_gre_tun gre_tun; /* type == ECM_DB_IFACE_TYPE_GRE */
1984#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001985 struct ecm_db_interface_info_unknown unknown; /* type == ECM_DB_IFACE_TYPE_UNKNOWN */
1986 struct ecm_db_interface_info_loopback loopback; /* type == ECM_DB_IFACE_TYPE_LOOPBACK */
Murat Sezgin69a27532015-03-12 14:09:40 -07001987#ifdef ECM_INTERFACE_IPSEC_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001988 struct ecm_db_interface_info_ipsec_tunnel ipsec_tunnel; /* type == ECM_DB_IFACE_TYPE_IPSEC_TUNNEL */
Murat Sezgin69a27532015-03-12 14:09:40 -07001989#endif
Murat Sezginbde55f92015-03-11 16:44:11 -07001990#ifdef ECM_INTERFACE_SIT_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001991 struct ecm_db_interface_info_sit sit; /* type == ECM_DB_IFACE_TYPE_SIT */
Murat Sezginbde55f92015-03-11 16:44:11 -07001992#endif
Murat Sezginc1402562015-03-12 12:32:20 -07001993#ifdef ECM_INTERFACE_TUNIPIP6_ENABLE
Gareth Williams8ac34292015-03-17 14:06:58 +00001994#ifdef ECM_IPV6_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00001995 struct ecm_db_interface_info_tunipip6 tunipip6; /* type == ECM_DB_IFACE_TYPE_TUNIPIP6 */
Murat Sezginc1402562015-03-12 12:32:20 -07001996#endif
Gareth Williams8ac34292015-03-17 14:06:58 +00001997#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00001998 } type_info;
Gareth Williamsc5b9d712014-05-09 20:40:07 +01001999
Suman Ghosh9e4245f2018-04-11 01:11:41 +05302000#ifdef ECM_INTERFACE_GRE_TUN_ENABLE
2001 struct ip_tunnel *gre4_tunnel;
2002 struct ip6_tnl *gre6_tunnel;
2003#endif
Gareth Williamsdbb2bfd2014-11-20 16:42:09 -08002004#ifdef ECM_INTERFACE_PPP_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00002005 int channel_count;
2006 struct ppp_channel *ppp_chan[1];
Ben Menchaca84f36632014-02-28 20:57:38 +00002007 int channel_protocol;
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302008#ifdef ECM_INTERFACE_PPPOE_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00002009 struct pppoe_opt addressing;
Gareth Williamsc5b9d712014-05-09 20:40:07 +01002010#endif
Shyam Sunder23f2e542015-09-28 14:56:49 +05302011#ifdef ECM_INTERFACE_PPTP_ENABLE
2012 int protocol = IPPROTO_IP;
2013 struct pptp_opt opt;
2014 struct iphdr *v4_hdr = NULL;
2015 if (skb) {
2016 v4_hdr = ip_hdr(skb);
2017 protocol = v4_hdr->protocol;
2018 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302019#endif
Shyam Sunder23f2e542015-09-28 14:56:49 +05302020#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002021 /*
2022 * Get basic information about the given device
2023 */
2024 dev_interface_num = dev->ifindex;
2025 dev_name = dev->name;
2026 dev_type = dev->type;
2027 dev_mtu = dev->mtu;
2028
2029 /*
Murat Sezgin91c5d712015-06-12 15:16:22 -07002030 * Does the accel engine recognise this interface?
Ben Menchaca84f36632014-02-28 20:57:38 +00002031 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002032 ae_interface_num = feci->ae_interface_number_by_dev_get(dev);
Ben Menchaca84f36632014-02-28 20:57:38 +00002033
Murat Sezgin91c5d712015-06-12 15:16:22 -07002034 DEBUG_TRACE("Establish interface instance for device: %p is type: %d, name: %s, ifindex: %d, ae_if: %d, mtu: %d\n",
2035 dev, dev_type, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002036
2037 /*
2038 * Extract from the device more type-specific information
2039 */
2040 if (dev_type == ARPHRD_ETHER) {
Shyam Sunder39e25672015-09-03 14:28:09 +05302041
Ben Menchaca84f36632014-02-28 20:57:38 +00002042 /*
Murat Sezginf983c4d2018-09-04 16:59:23 -07002043 * If MAC address is zeros, do nothing.
2044 */
2045 if (is_zero_ether_addr(dev->dev_addr)) {
2046 DEBUG_WARN("Net device %p MAC address is all zeros\n", dev);
2047 return NULL;
2048 }
2049
2050 /*
Ben Menchaca84f36632014-02-28 20:57:38 +00002051 * Ethernet - but what sub type?
2052 */
2053
Gareth Williams141d2382014-11-25 11:35:19 -08002054#ifdef ECM_INTERFACE_VLAN_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00002055 /*
2056 * VLAN?
2057 */
2058 if (is_vlan_dev(dev)) {
2059 /*
2060 * VLAN master
2061 * GGG No locking needed here, ASSUMPTION is that real_dev is held for as long as we have dev.
2062 */
2063 memcpy(type_info.vlan.address, dev->dev_addr, 6);
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05302064 type_info.vlan.vlan_tag = vlan_dev_vlan_id(dev);
Amit Gupta8975f9d2017-07-12 20:34:36 +05302065#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0))
Murat Sezginb2676062015-06-12 17:05:31 -07002066 type_info.vlan.vlan_tpid = ETH_P_8021Q;
Amit Gupta8975f9d2017-07-12 20:34:36 +05302067#else
2068 type_info.vlan.vlan_tpid = ntohs(vlan_dev_vlan_proto(dev));
2069#endif
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05302070 DEBUG_TRACE("Net device: %p is VLAN, mac: %pM, vlan_id: %x vlan_tpid: %x\n",
2071 dev, type_info.vlan.address, type_info.vlan.vlan_tag, type_info.vlan.vlan_tpid);
Ben Menchaca84f36632014-02-28 20:57:38 +00002072
2073 /*
2074 * Establish this type of interface
2075 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002076 ii = ecm_interface_vlan_interface_establish(&type_info.vlan, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Shyam Sunder39e25672015-09-03 14:28:09 +05302077 goto identifier_update;
Ben Menchaca84f36632014-02-28 20:57:38 +00002078 }
Gareth Williams141d2382014-11-25 11:35:19 -08002079#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002080
2081 /*
2082 * BRIDGE?
2083 */
2084 if (ecm_front_end_is_bridge_device(dev)) {
2085 /*
2086 * Bridge
2087 */
2088 memcpy(type_info.bridge.address, dev->dev_addr, 6);
2089
2090 DEBUG_TRACE("Net device: %p is BRIDGE, mac: %pM\n",
2091 dev, type_info.bridge.address);
2092
2093 /*
2094 * Establish this type of interface
2095 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002096 ii = ecm_interface_bridge_interface_establish(&type_info.bridge, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Shyam Sunder39e25672015-09-03 14:28:09 +05302097 goto identifier_update;
Ben Menchaca84f36632014-02-28 20:57:38 +00002098 }
2099
Murat Sezgin910c9662015-03-11 16:15:06 -07002100#ifdef ECM_INTERFACE_BOND_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00002101 /*
2102 * LAG?
2103 */
2104 if (ecm_front_end_is_lag_master(dev)) {
2105 /*
2106 * Link aggregation
2107 */
2108 memcpy(type_info.lag.address, dev->dev_addr, 6);
2109
2110 DEBUG_TRACE("Net device: %p is LAG, mac: %pM\n",
2111 dev, type_info.lag.address);
2112
2113 /*
2114 * Establish this type of interface
2115 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002116 ii = ecm_interface_lag_interface_establish(&type_info.lag, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Shyam Sunder39e25672015-09-03 14:28:09 +05302117 goto identifier_update;
Ben Menchaca84f36632014-02-28 20:57:38 +00002118 }
Murat Sezgin910c9662015-03-11 16:15:06 -07002119#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002120
Suman Ghosh9e4245f2018-04-11 01:11:41 +05302121#ifdef ECM_INTERFACE_GRE_TAP_ENABLE
Murat Sezgin31effef2017-11-13 15:09:34 -08002122 /*
2123 * GRE TAP?
2124 */
2125 if (dev->priv_flags & (IFF_GRE_V4_TAP | IFF_GRE_V6_TAP)) {
Suman Ghosh8f199b22018-07-13 18:51:27 +05302126 interface_type = feci->ae_interface_type_get(feci, dev);
2127 ae_interface_num = feci->ae_interface_number_by_dev_type_get(dev, interface_type);
2128
Murat Sezgin31effef2017-11-13 15:09:34 -08002129 /*
2130 * GRE TAP interface is handled as ethernet interface, however it is possible
2131 * that the acceleration engine may not be ready yet to handle the connection.
2132 * In this case the acceleration engine interface is not found for this type and
2133 * we should wait until it is ready.
2134 */
2135 if (ae_interface_num < 0) {
Suman Ghosh8f199b22018-07-13 18:51:27 +05302136 DEBUG_TRACE("GRE TAP interface is not ready yet. Interface type: %d\n", interface_type);
Murat Sezgin31effef2017-11-13 15:09:34 -08002137 return NULL;
2138 }
2139 }
2140#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002141 /*
2142 * ETHERNET!
2143 * Just plain ethernet it seems
2144 */
2145 memcpy(type_info.ethernet.address, dev->dev_addr, 6);
2146 DEBUG_TRACE("Net device: %p is ETHERNET, mac: %pM\n",
2147 dev, type_info.ethernet.address);
2148
2149 /*
2150 * Establish this type of interface
2151 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002152 ii = ecm_interface_ethernet_interface_establish(&type_info.ethernet, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Shyam Sunder39e25672015-09-03 14:28:09 +05302153
2154identifier_update:
2155 if (ii) {
2156 /*
2157 * An interface identifier/ifindex can be change after network restart. Below
2158 * functtion will check interface_identifier present in 'ii' with new dev_interface_num.
2159 * If differ then update new ifindex and update the interface identifier hash table.
2160 */
2161 ecm_db_iface_identifier_hash_table_entry_check_and_update(ii, dev_interface_num);
2162 }
2163
Ben Menchaca84f36632014-02-28 20:57:38 +00002164 return ii;
2165 }
2166
2167 /*
2168 * LOOPBACK?
2169 */
2170 if (dev_type == ARPHRD_LOOPBACK) {
2171 DEBUG_TRACE("Net device: %p is LOOPBACK type: %d\n", dev, dev_type);
2172 type_info.loopback.os_specific_ident = dev_interface_num;
Murat Sezgin91c5d712015-06-12 15:16:22 -07002173 ii = ecm_interface_loopback_interface_establish(&type_info.loopback, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002174 return ii;
2175 }
2176
Murat Sezgin69a27532015-03-12 14:09:40 -07002177#ifdef ECM_INTERFACE_IPSEC_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00002178 /*
2179 * IPSEC?
2180 */
2181 if (dev_type == ECM_ARPHRD_IPSEC_TUNNEL_TYPE) {
2182 DEBUG_TRACE("Net device: %p is IPSec tunnel type: %d\n", dev, dev_type);
2183 type_info.ipsec_tunnel.os_specific_ident = dev_interface_num;
Ankit Dhanuka6d5014a2014-06-22 17:21:44 +05302184
Ben Menchaca84f36632014-02-28 20:57:38 +00002185 // GGG TODO Flesh this out with tunnel endpoint addressing detail
Murat Sezgin91c5d712015-06-12 15:16:22 -07002186 ii = ecm_interface_ipsec_tunnel_interface_establish(&type_info.ipsec_tunnel, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002187 return ii;
2188 }
Murat Sezgin69a27532015-03-12 14:09:40 -07002189#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002190
ratheesh kannothcfdcb332015-12-24 07:19:18 +05302191#ifdef ECM_INTERFACE_MAP_T_ENABLE
2192 if (dev_type == ARPHRD_NONE) {
2193 if (is_map_t_dev(dev)) {
2194 type_info.map_t.if_index = dev_interface_num;
Manish Vermaf70e1612018-08-20 01:15:40 +05302195 interface_type = feci->ae_interface_type_get(feci, dev);
2196 ae_interface_num = feci->ae_interface_number_by_dev_type_get(dev, interface_type);
2197
2198 if (ae_interface_num < 0) {
2199 DEBUG_TRACE("MAP-T interface is not ready yet\n");
2200 return NULL;
2201 }
2202
ratheesh kannothcfdcb332015-12-24 07:19:18 +05302203 ii = ecm_interface_map_t_interface_establish(&type_info.map_t, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
2204 return ii;
2205 }
2206
2207 }
2208#endif
2209
Murat Sezginb3731e82014-11-26 12:20:59 -08002210#ifdef ECM_INTERFACE_SIT_ENABLE
Murat Sezginbde55f92015-03-11 16:44:11 -07002211#ifdef CONFIG_IPV6_SIT_6RD
Ben Menchaca84f36632014-02-28 20:57:38 +00002212 /*
2213 * SIT (6-in-4)?
2214 */
2215 if (dev_type == ARPHRD_SIT) {
2216 struct ip_tunnel *tunnel;
2217 struct ip_tunnel_6rd_parm *ip6rd;
2218 const struct iphdr *tiph;
2219
2220 DEBUG_TRACE("Net device: %p is SIT (6-in-4) type: %d\n", dev, dev_type);
2221
2222 tunnel = (struct ip_tunnel*)netdev_priv(dev);
2223 ip6rd = &tunnel->ip6rd;
2224
2225 /*
2226 * Get the Tunnel device IP header info
2227 */
2228 tiph = &tunnel->parms.iph ;
2229
2230 type_info.sit.prefixlen = ip6rd->prefixlen;
2231 type_info.sit.relay_prefix = ip6rd->relay_prefix;
2232 type_info.sit.relay_prefixlen = ip6rd->relay_prefixlen;
2233 ECM_NIN4_ADDR_TO_IP_ADDR(type_info.sit.saddr, tiph->saddr);
2234 ECM_NIN4_ADDR_TO_IP_ADDR(type_info.sit.daddr, tiph->daddr);
2235 type_info.sit.prefix[0] = ntohl(ip6rd->prefix.s6_addr32[0]);
2236 type_info.sit.prefix[1] = ntohl(ip6rd->prefix.s6_addr32[1]);
2237 type_info.sit.prefix[2] = ntohl(ip6rd->prefix.s6_addr32[2]);
2238 type_info.sit.prefix[3] = ntohl(ip6rd->prefix.s6_addr32[3]);
2239 type_info.sit.ttl = tiph->ttl;
2240 type_info.sit.tos = tiph->tos;
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05302241
Suman Ghosh8f199b22018-07-13 18:51:27 +05302242 interface_type = feci->ae_interface_type_get(feci, dev);
Murat Sezgined9c1c62017-07-14 16:57:23 -07002243 ae_interface_num = feci->ae_interface_number_by_dev_type_get(dev, interface_type);
2244
Murat Sezgin91c5d712015-06-12 15:16:22 -07002245 ii = ecm_interface_sit_interface_establish(&type_info.sit, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002246 return ii;
2247 }
Murat Sezgincc6eedf2014-05-09 23:59:19 -07002248#endif
Murat Sezginb3731e82014-11-26 12:20:59 -08002249#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002250
Murat Sezginc1402562015-03-12 12:32:20 -07002251#ifdef ECM_INTERFACE_TUNIPIP6_ENABLE
Gareth Williams8ac34292015-03-17 14:06:58 +00002252#ifdef ECM_IPV6_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00002253 /*
2254 * IPIP6 Tunnel?
2255 */
2256 if (dev_type == ARPHRD_TUNNEL6) {
2257 struct ip6_tnl *tunnel;
2258 struct flowi6 *fl6;
2259
2260 DEBUG_TRACE("Net device: %p is TUNIPIP6 type: %d\n", dev, dev_type);
2261
2262 /*
2263 * Get the tunnel device flow information (discover the output path of the tunnel)
2264 */
2265 tunnel = (struct ip6_tnl *)netdev_priv(dev);
2266 fl6 = &tunnel->fl.u.ip6;
2267
2268 ECM_NIN6_ADDR_TO_IP_ADDR(type_info.tunipip6.saddr, fl6->saddr);
2269 ECM_NIN6_ADDR_TO_IP_ADDR(type_info.tunipip6.daddr, fl6->daddr);
2270 type_info.tunipip6.hop_limit = tunnel->parms.hop_limit;
2271 type_info.tunipip6.flags = ntohl(tunnel->parms.flags);
2272 type_info.tunipip6.flowlabel = fl6->flowlabel; /* flow Label In kernel is stored in big endian format */
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05302273
Suman Ghosh8f199b22018-07-13 18:51:27 +05302274 interface_type = feci->ae_interface_type_get(feci, dev);
Apoorv Gupta39b33282018-08-02 09:25:59 +05302275 ae_interface_num = feci->ae_interface_number_by_dev_type_get(dev, interface_type);
2276
2277 if (ae_interface_num < 0) {
2278 DEBUG_TRACE("TUNIPIP6 interface is not ready yet\n");
2279 return NULL;
2280 }
2281
2282 ii = ecm_interface_tunipip6_interface_establish(&type_info.tunipip6, dev_name,
2283 dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002284 return ii;
2285 }
Murat Sezginc1402562015-03-12 12:32:20 -07002286#endif
Gareth Williams8ac34292015-03-17 14:06:58 +00002287#endif
2288
Suman Ghosh9e4245f2018-04-11 01:11:41 +05302289#ifdef ECM_INTERFACE_GRE_TUN_ENABLE
2290 if ((dev_type == ARPHRD_IPGRE) || (dev_type == ARPHRD_IP6GRE)) {
Suman Ghosh9e4245f2018-04-11 01:11:41 +05302291 type_info.gre_tun.if_index = dev_interface_num;
2292 if (dev_type == ARPHRD_IPGRE) {
2293 gre4_tunnel = netdev_priv(dev);
2294 if (!gre4_tunnel) {
2295 DEBUG_WARN("failed to obtain node address for host. GREv4 tunnel not ready\n");
2296 return NULL;
2297 }
2298 ECM_NIN4_ADDR_TO_IP_ADDR(type_info.gre_tun.local_ip, gre4_tunnel->parms.iph.saddr);
2299 ECM_NIN4_ADDR_TO_IP_ADDR(type_info.gre_tun.remote_ip, gre4_tunnel->parms.iph.daddr);
2300 } else {
2301 gre6_tunnel = netdev_priv(dev);
2302 if (!gre6_tunnel) {
2303 DEBUG_WARN("failed to obtain node address for host. GREv6 tunnel not ready\n");
2304 return NULL;
2305 }
2306 ECM_NIN6_ADDR_TO_IP_ADDR(type_info.gre_tun.local_ip, gre6_tunnel->parms.laddr);
2307 ECM_NIN6_ADDR_TO_IP_ADDR(type_info.gre_tun.remote_ip, gre6_tunnel->parms.raddr);
2308 }
2309
Suman Ghosh8f199b22018-07-13 18:51:27 +05302310 interface_type = feci->ae_interface_type_get(feci, dev);
2311 ae_interface_num = feci->ae_interface_number_by_dev_type_get(dev, interface_type);
2312 if (ae_interface_num < 0) {
2313 DEBUG_TRACE("GRE TUN interface is not ready yet. Interface type: %d\n", interface_type);
2314 return NULL;
2315 }
2316
Suman Ghosh9e4245f2018-04-11 01:11:41 +05302317 ii = ecm_interface_gre_tun_interface_establish(&type_info.gre_tun, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
2318 if (ii) {
2319 /*
2320 * The ifindex of a virtual netdevice like a GRE tunnel session can change if it is destroyed
2321 * and comes up again. Detect if the ifindex has changed and update it if required
2322 */
2323 ecm_db_iface_identifier_hash_table_entry_check_and_update(ii, dev_interface_num);
2324 }
2325 return ii;
2326 }
2327#endif
2328
Ben Menchaca84f36632014-02-28 20:57:38 +00002329 /*
2330 * If this is NOT PPP then it is unknown to the ecm
2331 */
2332 if (dev_type != ARPHRD_PPP) {
2333 DEBUG_TRACE("Net device: %p is UNKNOWN type: %d\n", dev, dev_type);
2334 type_info.unknown.os_specific_ident = dev_interface_num;
2335
2336 /*
2337 * Establish this type of interface
2338 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002339 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002340 return ii;
2341 }
2342
Gareth Williamsdbb2bfd2014-11-20 16:42:09 -08002343#ifndef ECM_INTERFACE_PPP_ENABLE
Gareth Williamsc5b9d712014-05-09 20:40:07 +01002344 /*
ratheesh kannothf4801a02016-12-09 22:46:39 +05302345 * PPP Support is NOT provided for.
Gareth Williamsc5b9d712014-05-09 20:40:07 +01002346 * Interface is therefore unknown
2347 */
2348 DEBUG_TRACE("Net device: %p is UNKNOWN (PPP Unsupported) type: %d\n", dev, dev_type);
2349 type_info.unknown.os_specific_ident = dev_interface_num;
2350
2351 /*
2352 * Establish this type of interface
2353 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002354 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Gareth Williamsc5b9d712014-05-09 20:40:07 +01002355 return ii;
2356#else
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302357
2358#ifdef ECM_INTERFACE_L2TPV2_ENABLE
2359 /*
2360 * ppp_xmit lock is held by linux kernel for l2tp packet in transmit
2361 * direction. we need to check for l2tp packet and avoid calls to
2362 * ppp_is_multilink() and ppp_hold_channels() which acquire same lock
2363 */
ratheesh kannothf4801a02016-12-09 22:46:39 +05302364
2365 if ((dev->priv_flags & IFF_PPP_L2TPV2) && ppp_is_xmit_locked(dev)) {
Murat Sezgindb0c5d82017-02-22 11:43:03 -08002366 if (skb && (skb->skb_iif == dev->ifindex)) {
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302367 struct pppol2tp_common_addr info;
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302368
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302369 if (__ppp_is_multilink(dev) > 0) {
2370 DEBUG_TRACE("Net device: %p is MULTILINK PPP - Unknown to the ECM\n", dev);
2371 type_info.unknown.os_specific_ident = dev_interface_num;
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302372
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302373 /*
2374 * Establish this type of interface
2375 */
2376 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
2377 return ii;
2378 }
2379 channel_count = __ppp_hold_channels(dev, ppp_chan, 1);
2380 if (channel_count != 1) {
2381 DEBUG_TRACE("Net device: %p PPP has %d channels - ECM cannot handle this (interface becomes Unknown type)\n",
2382 dev, channel_count);
2383 type_info.unknown.os_specific_ident = dev_interface_num;
2384
2385 /*
2386 * Establish this type of interface
2387 */
2388 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
2389 return ii;
2390 }
2391
2392 if (pppol2tp_channel_addressing_get(ppp_chan[0], &info)) {
2393 ppp_release_channels(ppp_chan, 1);
ratheesh kannothed721852015-09-28 12:39:52 +05302394 return NULL;
2395 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302396
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302397 type_info.pppol2tpv2.l2tp.tunnel.tunnel_id = info.local_tunnel_id;
2398 type_info.pppol2tpv2.l2tp.tunnel.peer_tunnel_id = info.remote_tunnel_id;
2399 type_info.pppol2tpv2.l2tp.session.session_id = info.local_session_id;
2400 type_info.pppol2tpv2.l2tp.session.peer_session_id = info.remote_session_id;
2401 type_info.pppol2tpv2.udp.sport = ntohs(info.local_addr.sin_port);
2402 type_info.pppol2tpv2.udp.dport = ntohs(info.remote_addr.sin_port);
2403 type_info.pppol2tpv2.ip.saddr = ntohl(info.local_addr.sin_addr.s_addr);
2404 type_info.pppol2tpv2.ip.daddr = ntohl(info.remote_addr.sin_addr.s_addr);
ratheesh kannothed721852015-09-28 12:39:52 +05302405
2406 /*
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302407 * Release the channel. Note that next_dev is still (correctly) held.
ratheesh kannothed721852015-09-28 12:39:52 +05302408 */
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302409 ppp_release_channels(ppp_chan, 1);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302410
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302411 DEBUG_TRACE("Net device: %p PPPo2L2TP session: %d,n", dev, type_info.pppol2tpv2.l2tp.session.peer_session_id);
ratheesh kannothed721852015-09-28 12:39:52 +05302412
2413 /*
2414 * Establish this type of interface
2415 */
2416 ii = ecm_interface_pppol2tpv2_interface_establish(&type_info.pppol2tpv2, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
2417 return ii;
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302418 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302419 }
2420#endif
2421
Shyam Sunder23f2e542015-09-28 14:56:49 +05302422#ifdef ECM_INTERFACE_PPTP_ENABLE
Suman Ghosh4273a8e2018-05-03 16:21:14 +05302423 if ((protocol == IPPROTO_GRE) && skb && v4_hdr && (dev->priv_flags & IFF_PPP_PPTP)) {
Shyam Sunder23f2e542015-09-28 14:56:49 +05302424 struct gre_hdr_pptp *gre_hdr;
2425 uint16_t proto;
2426 int ret;
2427
2428 skb_pull(skb, sizeof(struct iphdr));
2429 gre_hdr = (struct gre_hdr_pptp *)(skb->data);
2430 proto = ntohs(gre_hdr->protocol);
2431 if ((gre_hdr->version == GRE_VERSION_PPTP) && (proto == GRE_PROTOCOL_PPTP)) {
2432 ret = pptp_session_find(&opt, gre_hdr->call_id, v4_hdr->daddr);
2433 if (ret < 0) {
2434 skb_push(skb, sizeof(struct iphdr));
2435 DEBUG_WARN("PPTP session info not found\n");
2436 return NULL;
2437 }
2438
2439 /*
2440 * Get PPTP session info
2441 */
2442 type_info.pptp.src_call_id = ntohs(opt.src_addr.call_id);
2443 type_info.pptp.dst_call_id = ntohs(opt.dst_addr.call_id);
2444 type_info.pptp.src_ip = ntohl(opt.src_addr.sin_addr.s_addr);
2445 type_info.pptp.dst_ip = ntohl(opt.dst_addr.sin_addr.s_addr);
2446
2447 skb_push(skb, sizeof(struct iphdr));
2448
Swaraj Sha05620582018-08-15 23:21:16 +05302449 interface_type = feci->ae_interface_type_get(feci, dev);
2450 ae_interface_num = feci->ae_interface_number_by_dev_type_get(dev, interface_type);
2451
Shyam Sunder23f2e542015-09-28 14:56:49 +05302452 /*
2453 * Establish this type of interface
2454 */
2455 ii = ecm_interface_pptp_interface_establish(&type_info.pptp, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Shyam Sunder65f013e2015-12-14 18:04:54 +05302456 if (ii) {
2457 /*
2458 * The ifindex of a virtual netdevice like a PPTP session can change if it is destroyed
2459 * and comes up again. Detect if the ifindex has changed and update it if required
2460 */
2461 ecm_db_iface_identifier_hash_table_entry_check_and_update(ii, dev_interface_num);
2462 }
Shyam Sunder23f2e542015-09-28 14:56:49 +05302463 return ii;
2464 }
2465
2466 skb_push(skb, sizeof(struct iphdr));
2467
2468 DEBUG_TRACE("Unknown GRE protocol \n");
2469 type_info.unknown.os_specific_ident = dev_interface_num;
2470
2471 /*
2472 * Establish this type of interface
2473 */
2474 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
2475 return ii;
2476 }
2477#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002478 /*
2479 * PPP - but what is the channel type?
2480 * First: If this is multi-link then we do not support it
2481 */
2482 if (ppp_is_multilink(dev) > 0) {
2483 DEBUG_TRACE("Net device: %p is MULTILINK PPP - Unknown to the ECM\n", dev);
2484 type_info.unknown.os_specific_ident = dev_interface_num;
2485
2486 /*
2487 * Establish this type of interface
2488 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002489 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002490 return ii;
2491 }
2492
2493 DEBUG_TRACE("Net device: %p is PPP\n", dev);
2494
2495 /*
2496 * Get the PPP channel and then enquire what kind of channel it is
2497 * NOTE: Not multilink so only one channel to get.
2498 */
2499 channel_count = ppp_hold_channels(dev, ppp_chan, 1);
2500 if (channel_count != 1) {
Gareth Williams6f96a4b2014-05-29 19:41:21 +01002501 DEBUG_TRACE("Net device: %p PPP has %d channels - ECM cannot handle this (interface becomes Unknown type)\n",
2502 dev, channel_count);
Ben Menchaca84f36632014-02-28 20:57:38 +00002503 type_info.unknown.os_specific_ident = dev_interface_num;
2504
2505 /*
2506 * Establish this type of interface
2507 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07002508 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002509 return ii;
2510 }
2511
2512 /*
2513 * Get channel protocol type
Gareth Williams6f96a4b2014-05-29 19:41:21 +01002514 * NOTE: Not all PPP channels support channel specific methods.
Ben Menchaca84f36632014-02-28 20:57:38 +00002515 */
Gareth Williams6f96a4b2014-05-29 19:41:21 +01002516 channel_protocol = ppp_channel_get_protocol(ppp_chan[0]);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302517
2518#ifdef ECM_INTERFACE_L2TPV2_ENABLE
2519 if (channel_protocol == PX_PROTO_OL2TP) {
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302520 struct pppol2tp_common_addr info;
Ben Menchaca84f36632014-02-28 20:57:38 +00002521
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302522 if (pppol2tp_channel_addressing_get(ppp_chan[0], &info)) {
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302523 ppp_release_channels(ppp_chan, 1);
2524 return NULL;
2525 }
2526
ratheesh kannoth08b8e442015-10-02 00:24:55 +05302527 type_info.pppol2tpv2.l2tp.tunnel.tunnel_id = info.local_tunnel_id;
2528 type_info.pppol2tpv2.l2tp.tunnel.peer_tunnel_id = info.remote_tunnel_id;
2529 type_info.pppol2tpv2.l2tp.session.session_id = info.local_session_id;
2530 type_info.pppol2tpv2.l2tp.session.peer_session_id = info.remote_session_id;
2531 type_info.pppol2tpv2.udp.sport = ntohs(info.local_addr.sin_port);
2532 type_info.pppol2tpv2.udp.dport = ntohs(info.remote_addr.sin_port);
2533 type_info.pppol2tpv2.ip.saddr = ntohl(info.local_addr.sin_addr.s_addr);
2534 type_info.pppol2tpv2.ip.daddr = ntohl(info.remote_addr.sin_addr.s_addr);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302535
2536 /*
2537 * Release the channel. Note that next_dev is still (correctly) held.
Ben Menchaca84f36632014-02-28 20:57:38 +00002538 */
2539 ppp_release_channels(ppp_chan, 1);
2540
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302541 DEBUG_TRACE("Net device: %p PPPo2L2TP session: %d,n", dev, type_info.pppol2tpv2.l2tp.session.peer_session_id);
2542
Ben Menchaca84f36632014-02-28 20:57:38 +00002543 /*
2544 * Establish this type of interface
2545 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302546 ii = ecm_interface_pppol2tpv2_interface_establish(&type_info.pppol2tpv2, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002547 return ii;
2548 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302549#endif
2550#ifdef ECM_INTERFACE_PPPOE_ENABLE
2551 if (channel_protocol == PX_PROTO_OE) {
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302552 /*
2553 * PPPoE channel
2554 */
2555 DEBUG_TRACE("Net device: %p PPP channel is PPPoE\n", dev);
2556
2557 /*
2558 * Get PPPoE session information and the underlying device it is using.
2559 */
2560 pppoe_channel_addressing_get(ppp_chan[0], &addressing);
2561 type_info.pppoe.pppoe_session_id = (uint16_t)ntohs((uint16_t)addressing.pa.sid);
2562 memcpy(type_info.pppoe.remote_mac, addressing.pa.remote, ETH_ALEN);
2563 dev_put(addressing.dev);
2564
2565 /*
2566 * Release the channel. Note that next_dev is still (correctly) held.
2567 */
2568 ppp_release_channels(ppp_chan, 1);
2569
2570 DEBUG_TRACE("Net device: %p PPPoE session: %x, remote mac: %pM\n",
2571 dev, type_info.pppoe.pppoe_session_id, type_info.pppoe.remote_mac);
2572
2573 /*
2574 * Establish this type of interface
2575 */
2576 ii = ecm_interface_pppoe_interface_establish(&type_info.pppoe, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
2577 return ii;
2578 }
2579#endif
Shyam Sunder23f2e542015-09-28 14:56:49 +05302580
2581#ifdef ECM_INTERFACE_PPTP_ENABLE
2582 if (channel_protocol == PX_PROTO_PPTP) {
Swaraj Sha05620582018-08-15 23:21:16 +05302583
Shyam Sunder23f2e542015-09-28 14:56:49 +05302584 pptp_channel_addressing_get(&opt, ppp_chan[0]);
2585
2586 /*
2587 * Get PPTP session info
2588 */
2589 type_info.pptp.src_call_id = ntohs(opt.src_addr.call_id);
2590 type_info.pptp.dst_call_id = ntohs(opt.dst_addr.call_id);
2591 type_info.pptp.src_ip = ntohl(opt.src_addr.sin_addr.s_addr);
2592 type_info.pptp.dst_ip = ntohl(opt.dst_addr.sin_addr.s_addr);
2593
2594 DEBUG_TRACE("Net device: %p PPTP source call id: %d,n", dev, type_info.pptp.src_call_id);
2595 ppp_release_channels(ppp_chan, 1);
2596
Swaraj Sha05620582018-08-15 23:21:16 +05302597 interface_type = feci->ae_interface_type_get(feci, dev);
2598 ae_interface_num = feci->ae_interface_number_by_dev_type_get(dev, interface_type);
2599
Shyam Sunder23f2e542015-09-28 14:56:49 +05302600 /*
2601 * Establish this type of interface
2602 */
2603 ii = ecm_interface_pptp_interface_establish(&type_info.pptp, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Shyam Sunder65f013e2015-12-14 18:04:54 +05302604 if (ii) {
2605 /*
2606 * The ifindex of a virtual netdevice like a PPTP session can change if it is destroyed
2607 * and comes up again. Detect if the ifindex has changed and update it if required
2608 */
2609 ecm_db_iface_identifier_hash_table_entry_check_and_update(ii, dev_interface_num);
2610 }
Shyam Sunder23f2e542015-09-28 14:56:49 +05302611 return ii;
2612 }
2613#endif
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302614 DEBUG_TRACE("Net device: %p PPP channel protocol: %d - Unknown to the ECM\n", dev, channel_protocol);
2615 type_info.unknown.os_specific_ident = dev_interface_num;
Ben Menchaca84f36632014-02-28 20:57:38 +00002616
2617 /*
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302618 * Release the channel
Ben Menchaca84f36632014-02-28 20:57:38 +00002619 */
2620 ppp_release_channels(ppp_chan, 1);
2621
Ben Menchaca84f36632014-02-28 20:57:38 +00002622 /*
2623 * Establish this type of interface
2624 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302625 ii = ecm_interface_unknown_interface_establish(&type_info.unknown, dev_name, dev_interface_num, ae_interface_num, dev_mtu);
Ben Menchaca84f36632014-02-28 20:57:38 +00002626 return ii;
Gareth Williamsc5b9d712014-05-09 20:40:07 +01002627#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00002628}
2629EXPORT_SYMBOL(ecm_interface_establish_and_ref);
2630
Shyam Sunder6358b862015-05-04 15:06:24 +05302631#ifdef ECM_MULTICAST_ENABLE
2632/*
2633 * ecm_interface_multicast_heirarchy_construct_single()
2634 * Create and return an interface heirarchy for a single interface for a multicast connection
2635 *
2636 * src_addr IP source address
2637 * dest_addr IP Destination address/Group Address
2638 * interface Pointer to a single multicast interface heirarchy
2639 * given_dest_dev Netdev pointer for destination interface
2640 * br_slave_dev Netdev pointer to a bridge slave device. It could be NULL in case of pure
2641 * routed flow without any bridge interface in destination dev list.
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302642 * skb sk_buff
Shyam Sunder6358b862015-05-04 15:06:24 +05302643 */
Shyam Sunder81836832015-07-09 19:18:25 +05302644static uint32_t ecm_interface_multicast_heirarchy_construct_single(struct ecm_front_end_connection_instance *feci, ip_addr_t src_addr,
2645 ip_addr_t dest_addr, struct ecm_db_iface_instance *interface,
2646 struct net_device *given_dest_dev, struct net_device *br_slave_dev,
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302647 uint8_t *src_node_addr, bool is_routed, __be16 *layer4hdr, struct sk_buff *skb)
Shyam Sunder6358b862015-05-04 15:06:24 +05302648{
2649 struct ecm_db_iface_instance *to_list_single[ECM_DB_IFACE_HEIRARCHY_MAX];
2650 struct ecm_db_iface_instance **ifaces;
2651 struct ecm_db_iface_instance *ii_temp;
2652 struct net_device *dest_dev;
2653 int32_t current_interface_index;
2654 int32_t interfaces_cnt = 0;
2655 int32_t dest_dev_type;
2656
2657 dest_dev = given_dest_dev;
2658 dev_hold(dest_dev);
2659 dest_dev_type = dest_dev->type;
2660 current_interface_index = ECM_DB_IFACE_HEIRARCHY_MAX;
2661
2662 while (current_interface_index > 0) {
2663 struct ecm_db_iface_instance *ii;
2664 struct net_device *next_dev;
2665
2666 /*
2667 * Get the ecm db interface instance for the device at hand
2668 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05302669 ii = ecm_interface_establish_and_ref(feci, dest_dev, skb);
Shyam Sunder6358b862015-05-04 15:06:24 +05302670 interfaces_cnt++;
2671
2672 /*
2673 * If the interface could not be established then we abort
2674 */
2675 if (!ii) {
2676 DEBUG_WARN("Failed to establish interface: %p, name: %s\n", dest_dev, dest_dev->name);
2677 dev_put(dest_dev);
2678
2679 /*
2680 * Release the interfaces heirarchy we constructed to this point.
2681 */
2682 ecm_db_multicast_copy_if_heirarchy(to_list_single, interface);
2683 ecm_db_connection_interfaces_deref(to_list_single, current_interface_index);
2684 return ECM_DB_IFACE_HEIRARCHY_MAX;
2685 }
2686
2687 /*
2688 * Record the interface instance into the *ifaces
2689 */
2690 current_interface_index--;
2691 ii_temp = ecm_db_multicast_if_instance_get_at_index(interface, current_interface_index);
2692 ifaces = (struct ecm_db_iface_instance **)ii_temp;
2693 *ifaces = ii;
2694
2695 /*
2696 * Now we have to figure out what the next device will be (in the transmission path)
2697 */
2698 do {
Kiran Kumar C.S.Kd43bc3f2015-08-04 16:51:03 +05302699#ifdef ECM_INTERFACE_PPP_ENABLE
Shyam Sunder6358b862015-05-04 15:06:24 +05302700 int channel_count;
2701 struct ppp_channel *ppp_chan[1];
2702 int channel_protocol;
2703 struct pppoe_opt addressing;
2704#endif
2705 DEBUG_TRACE("Net device: %p is type: %d, name: %s\n", dest_dev, dest_dev_type, dest_dev->name);
2706 next_dev = NULL;
2707
2708 if (dest_dev_type == ARPHRD_ETHER) {
2709 /*
2710 * Ethernet - but what sub type?
2711 */
2712
2713 /*
2714 * VLAN?
2715 */
2716 if (is_vlan_dev(dest_dev)) {
2717 /*
2718 * VLAN master
2719 * No locking needed here, ASSUMPTION is that real_dev is held for as long as we have dev.
2720 */
Shyam Sunder9db20852016-03-09 19:04:49 +05302721 next_dev = ecm_interface_vlan_real_dev(dest_dev);
Shyam Sunder6358b862015-05-04 15:06:24 +05302722 dev_hold(next_dev);
2723 DEBUG_TRACE("Net device: %p is VLAN, slave dev: %p (%s)\n",
2724 dest_dev, next_dev, next_dev->name);
2725 break;
2726 }
2727
2728 /*
2729 * BRIDGE?
2730 */
2731 if (ecm_front_end_is_bridge_device(dest_dev)) {
2732 if (!ecm_front_end_is_bridge_port(br_slave_dev)) {
2733 DEBUG_ASSERT(NULL, "%p: expected only bridge slave here\n", interface);
2734
2735 /*
2736 * Release the interfaces heirarchy we constructed to this point.
2737 */
2738 ecm_db_multicast_copy_if_heirarchy(to_list_single, interface);
2739 ecm_db_connection_interfaces_deref(to_list_single, current_interface_index);
2740 dev_put(dest_dev);
2741 return ECM_DB_IFACE_HEIRARCHY_MAX;
2742 }
2743
2744 next_dev = br_slave_dev;
2745 if (!next_dev) {
2746 DEBUG_WARN("Unable to obtain output port \n");
2747
2748 /*
2749 * Release the interfaces heirarchy we constructed to this point.
2750 */
2751 ecm_db_multicast_copy_if_heirarchy(to_list_single, interface);
2752 ecm_db_connection_interfaces_deref(to_list_single, current_interface_index);
2753 dev_put(dest_dev);
2754 return ECM_DB_IFACE_HEIRARCHY_MAX;
2755 }
2756 DEBUG_TRACE("Net device: %p is BRIDGE, next_dev: %p (%s)\n", dest_dev, next_dev, next_dev->name);
2757 dev_hold(next_dev);
2758 break;
2759 }
2760
Shyam Sunder81836832015-07-09 19:18:25 +05302761#ifdef ECM_INTERFACE_BOND_ENABLE
Shyam Sunder6358b862015-05-04 15:06:24 +05302762 /*
2763 * LAG?
2764 */
2765 if (ecm_front_end_is_lag_master(dest_dev)) {
2766 /*
2767 * Link aggregation
2768 * Figure out which slave device of the link aggregation will be used to reach the destination.
2769 */
2770 uint32_t src_addr_32 = 0;
2771 uint32_t dest_addr_32 = 0;
Suman Ghoshf14d2172015-07-31 14:38:10 +05302772 struct in6_addr src_addr6;
2773 struct in6_addr dest_addr6;
Shyam Sunder6358b862015-05-04 15:06:24 +05302774 uint8_t src_mac_addr[ETH_ALEN];
2775 uint8_t dest_mac_addr[ETH_ALEN];
2776
2777 memset(src_mac_addr, 0, ETH_ALEN);
2778 memset(dest_mac_addr, 0, ETH_ALEN);
2779
Shyam Sunder81836832015-07-09 19:18:25 +05302780 if (ECM_IP_ADDR_IS_V4(src_addr)) {
2781 ECM_IP_ADDR_TO_NIN4_ADDR(src_addr_32, src_addr);
2782 ECM_IP_ADDR_TO_NIN4_ADDR(dest_addr_32, dest_addr);
2783 }
Shyam Sunder6358b862015-05-04 15:06:24 +05302784
Shyam Sunder81836832015-07-09 19:18:25 +05302785 if (!is_routed) {
2786 memcpy(src_mac_addr, src_node_addr, ETH_ALEN);
Shyam Sunder6358b862015-05-04 15:06:24 +05302787 } else {
Shyam Sunder81836832015-07-09 19:18:25 +05302788 struct net_device *dest_dev_master;
2789
2790 /*
2791 * Use appropriate source MAC address for routed packets
2792 */
2793 dest_dev_master = ecm_interface_get_and_hold_dev_master(dest_dev);
2794 if (dest_dev_master) {
2795 memcpy(src_mac_addr, dest_dev_master->dev_addr, ETH_ALEN);
2796 dev_put(dest_dev_master);
2797 } else {
2798 memcpy(src_mac_addr, dest_dev->dev_addr, ETH_ALEN);
2799 }
Shyam Sunder6358b862015-05-04 15:06:24 +05302800 }
2801
2802 /*
2803 * Create Destination MAC address using IP multicast destination address
2804 */
2805 ecm_translate_multicast_mac(dest_addr, dest_mac_addr);
2806
Shyam Sunder81836832015-07-09 19:18:25 +05302807 if (ECM_IP_ADDR_IS_V4(src_addr)) {
2808 next_dev = bond_get_tx_dev(NULL, src_mac_addr, dest_mac_addr,
2809 &src_addr_32, &dest_addr_32,
Suman Ghoshfb8e7702015-08-20 15:27:57 +05302810 htons((uint16_t)ETH_P_IP), dest_dev, layer4hdr);
Shyam Sunder81836832015-07-09 19:18:25 +05302811 } else {
Suman Ghoshf14d2172015-07-31 14:38:10 +05302812 ECM_IP_ADDR_TO_NIN6_ADDR(src_addr6, src_addr);
2813 ECM_IP_ADDR_TO_NIN6_ADDR(dest_addr6, dest_addr);
Shyam Sunder81836832015-07-09 19:18:25 +05302814 next_dev = bond_get_tx_dev(NULL, src_mac_addr, dest_mac_addr,
Suman Ghoshf14d2172015-07-31 14:38:10 +05302815 src_addr6.s6_addr, dest_addr6.s6_addr,
Shyam Sundere60540c2016-04-29 15:06:35 +05302816 htons((uint16_t)ETH_P_IPV6), dest_dev, layer4hdr);
Shyam Sunder81836832015-07-09 19:18:25 +05302817 }
2818
Shyam Sunder6358b862015-05-04 15:06:24 +05302819 if (!(next_dev && netif_carrier_ok(next_dev))) {
2820 DEBUG_WARN("Unable to obtain LAG output slave device\n");
2821 dev_put(dest_dev);
2822
2823 /*
2824 * Release the interfaces heirarchy we constructed to this point.
2825 */
2826 ecm_db_multicast_copy_if_heirarchy(to_list_single, interface);
2827 ecm_db_connection_interfaces_deref(to_list_single, current_interface_index);
2828 return ECM_DB_IFACE_HEIRARCHY_MAX;
2829 }
2830
2831 dev_hold(next_dev);
2832 DEBUG_TRACE("Net device: %p is LAG, slave dev: %p (%s)\n", dest_dev, next_dev, next_dev->name);
2833 break;
2834 }
2835#endif
2836
2837 /*
2838 * ETHERNET!
2839 * Just plain ethernet it seems.
2840 */
2841 DEBUG_TRACE("Net device: %p is ETHERNET\n", dest_dev);
2842 break;
2843 }
2844
2845 /*
2846 * LOOPBACK?
2847 */
2848 if (dest_dev_type == ARPHRD_LOOPBACK) {
2849 DEBUG_TRACE("Net device: %p is LOOPBACK type: %d\n", dest_dev, dest_dev_type);
2850 break;
2851 }
2852
2853 /*
2854 * IPSEC?
2855 */
2856 if (dest_dev_type == ECM_ARPHRD_IPSEC_TUNNEL_TYPE) {
2857 DEBUG_TRACE("Net device: %p is IPSec tunnel type: %d\n", dest_dev, dest_dev_type);
2858 /*
2859 * TODO Figure out the next device the tunnel is using...
2860 */
2861 break;
2862 }
2863
2864 /*
2865 * SIT (6-in-4)?
2866 */
2867 if (dest_dev_type == ARPHRD_SIT) {
2868 DEBUG_TRACE("Net device: %p is SIT (6-in-4) type: %d\n", dest_dev, dest_dev_type);
2869 /*
2870 * TODO Figure out the next device the tunnel is using...
2871 */
2872 break;
2873 }
2874
2875 /*
2876 * IPIP6 Tunnel?
2877 */
2878 if (dest_dev_type == ARPHRD_TUNNEL6) {
2879 DEBUG_TRACE("Net device: %p is TUNIPIP6 type: %d\n", dest_dev, dest_dev_type);
2880 /*
2881 * TODO Figure out the next device the tunnel is using...
2882 */
2883 break;
2884 }
2885
2886 /*
2887 * If this is NOT PPP then it is unknown to the ecm and we cannot figure out it's next device.
2888 */
2889 if (dest_dev_type != ARPHRD_PPP) {
2890 DEBUG_TRACE("Net device: %p is UNKNOWN type: %d\n", dest_dev, dest_dev_type);
2891 break;
2892 }
2893
Kiran Kumar C.S.Kd43bc3f2015-08-04 16:51:03 +05302894#ifndef ECM_INTERFACE_PPP_ENABLE
Shyam Sunder6358b862015-05-04 15:06:24 +05302895 DEBUG_TRACE("Net device: %p is UNKNOWN (PPP Unsupported) type: %d\n", dest_dev, dest_dev_type);
2896#else
2897 /*
2898 * PPP - but what is the channel type?
2899 * First: If this is multi-link then we do not support it
2900 */
2901 if (ppp_is_multilink(dest_dev) > 0) {
2902 DEBUG_TRACE("Net device: %p is MULTILINK PPP - Unknown to the ECM\n", dest_dev);
2903 break;
2904 }
2905
2906 DEBUG_TRACE("Net device: %p is PPP\n", dest_dev);
2907
2908 /*
2909 * Get the PPP channel and then enquire what kind of channel it is
2910 * NOTE: Not multilink so only one channel to get.
2911 */
2912 channel_count = ppp_hold_channels(dest_dev, ppp_chan, 1);
2913 if (channel_count != 1) {
2914 DEBUG_TRACE("Net device: %p PPP has %d channels - Unknown to the ECM\n",
2915 dest_dev, channel_count);
2916 break;
2917 }
2918
2919 /*
2920 * Get channel protocol type
2921 * NOTE: Not all PPP channels support channel specific methods.
2922 */
2923 channel_protocol = ppp_channel_get_protocol(ppp_chan[0]);
2924 if (channel_protocol != PX_PROTO_OE) {
2925 DEBUG_TRACE("Net device: %p PPP channel protocol: %d - Unknown to the ECM\n",
2926 dest_dev, channel_protocol);
2927
2928 /*
2929 * Release the channel
2930 */
2931 ppp_release_channels(ppp_chan, 1);
2932
2933 break;
2934 }
2935
2936 /*
2937 * PPPoE channel
2938 */
2939 DEBUG_TRACE("Net device: %p PPP channel is PPPoE\n", dest_dev);
2940
2941 /*
2942 * Get PPPoE session information and the underlying device it is using.
2943 */
2944 pppoe_channel_addressing_get(ppp_chan[0], &addressing);
2945
2946 /*
2947 * Copy the dev hold into this, we will release the hold later
2948 */
2949 next_dev = addressing.dev;
2950
Shyam Sunder6358b862015-05-04 15:06:24 +05302951 /*
2952 * Release the channel. Note that next_dev is still (correctly) held.
2953 */
2954 ppp_release_channels(ppp_chan, 1);
2955#endif
2956 } while (false);
2957
2958 /*
2959 * No longer need dest_dev as it may become next_dev
2960 */
2961 dev_put(dest_dev);
2962
2963 /*
2964 * Check out the next_dev, if any
2965 */
2966 if (!next_dev) {
2967 int32_t i __attribute__((unused));
2968 DEBUG_INFO("Completed interface heirarchy construct with first interface @: %d\n", current_interface_index);
2969#if DEBUG_LEVEL > 1
2970 ecm_db_multicast_copy_if_heirarchy(to_list_single, interface);
2971 for (i = current_interface_index; i < ECM_DB_IFACE_HEIRARCHY_MAX; ++i) {
2972 DEBUG_TRACE("\tInterface @ %d: %p, type: %d, name: %s\n", \
Murat Sezgin3d9a7642018-05-15 16:24:09 -07002973 i, to_list_single[i], ecm_db_iface_type_get(to_list_single[i]), \
2974 ecm_db_interface_type_to_string(ecm_db_iface_type_get(to_list_single[i])));
Shyam Sunder6358b862015-05-04 15:06:24 +05302975 }
2976#endif
2977 return current_interface_index;
2978 }
2979
2980 /*
2981 * dest_dev becomes next_dev
2982 */
2983 dest_dev = next_dev;
2984 dest_dev_type = dest_dev->type;
2985 }
2986
2987 dev_put(dest_dev);
2988
2989 ecm_db_multicast_copy_if_heirarchy(to_list_single, interface);
2990 ecm_db_connection_interfaces_deref(to_list_single, current_interface_index);
2991 return ECM_DB_IFACE_HEIRARCHY_MAX;
2992}
2993
2994/*
2995 * ecm_interface_multicast_heirarchy_construct_routed()
2996 * Create destination interface heirarchy for a routed multicast connectiona
2997 *
2998 * interfaces Pointer to the 2-D array of multicast interface heirarchies
2999 * in_dev Pointer to the source netdev
3000 * packet_src_addr Source IP of the multicast flow
3001 * packet_dest_addr Group(dest) IP of the multicast flow
3002 * max_dst Maximum number of netdev joined the multicast group
3003 * dst_if_index_base An array of if index joined the multicast group
3004 * interface_first_base An array of the index of the first interface in the list
3005 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07003006int32_t ecm_interface_multicast_heirarchy_construct_routed(struct ecm_front_end_connection_instance *feci,
3007 struct ecm_db_iface_instance *interfaces,
3008 struct net_device *in_dev,
3009 ip_addr_t packet_src_addr,
3010 ip_addr_t packet_dest_addr, uint8_t max_if,
3011 uint32_t *dst_if_index_base,
Shyam Sundera2e08ee2015-06-18 21:32:13 +05303012 uint32_t *interface_first_base, bool mfc_update,
ratheesh kannotha32fdd12015-09-09 08:02:58 +05303013 __be16 *layer4hdr, struct sk_buff *skb)
Shyam Sunder6358b862015-05-04 15:06:24 +05303014{
3015 struct ecm_db_iface_instance *to_list_single[ECM_DB_IFACE_HEIRARCHY_MAX];
3016 struct ecm_db_iface_instance *ifaces;
3017 struct net_device *dest_dev = NULL;
3018 struct net_device *br_dev_src = NULL;
3019 uint32_t *dst_if_index;
3020 uint32_t *interface_first;
3021 uint32_t br_if;
3022 uint32_t valid_if;
Shyam Sunder41637872015-06-11 21:19:05 +05303023 int32_t if_num;
Shyam Sunder6358b862015-05-04 15:06:24 +05303024 int32_t dest_dev_type;
3025 int if_index;
3026 int ii_cnt;
3027 int total_ii_count = 0;
3028 bool src_dev_is_bridge = false;
3029
3030 DEBUG_TRACE("Construct interface heirarchy for dest_addr: " ECM_IP_ADDR_DOT_FMT " src_addr: " ECM_IP_ADDR_DOT_FMT "total destination ifs %d\n",
3031 ECM_IP_ADDR_TO_DOT(packet_dest_addr), ECM_IP_ADDR_TO_DOT(packet_src_addr), max_if);
3032
3033 /*
3034 * Check if the source net_dev is a bridge slave.
3035 */
Shyam Sundera2e08ee2015-06-18 21:32:13 +05303036 if (in_dev && !mfc_update) {
Shyam Sunder6358b862015-05-04 15:06:24 +05303037 if (ecm_front_end_is_bridge_port(in_dev)) {
3038 src_dev_is_bridge = true;
Karthik Hariharan3e964a32015-09-22 21:40:52 +05303039 br_dev_src = ecm_interface_get_and_hold_dev_master(in_dev);
3040 DEBUG_ASSERT(br_dev_src, "Expected a master\n");
Shyam Sunder6358b862015-05-04 15:06:24 +05303041
3042 /*
3043 * The source net_dev found as bridge slave. In case of routed interface
3044 * heirarchy MFC is not aware of any other bridge slave has joined the same
3045 * multicast group as a destination interface. Therfore we assume there
3046 * are bridge slaves present in multicast destination interface list
3047 * and increase the max_if by one.
3048 */
3049 max_if++;
3050 }
3051 }
3052
3053 ii_cnt = 0;
3054 br_if = if_num = 0;
3055
3056 /*
3057 * This loop is for creating the destination interface hierarchy list.
3058 * We take the destination interface array we got from MFC (in form of ifindex array)
3059 * as input for this.
3060 */
3061 for (if_index = 0, valid_if = 0; if_index < max_if; if_index++) {
3062 dst_if_index = ecm_db_multicast_if_first_get_at_index(dst_if_index_base, if_index);
3063
3064 if (*dst_if_index == ECM_INTERFACE_LOOPBACK_DEV_INDEX) {
3065 continue;
3066 }
3067
3068 dest_dev = dev_get_by_index(&init_net, *dst_if_index);
3069 if (!dest_dev) {
3070 if (!src_dev_is_bridge) {
3071 int i;
3072
3073 /*
3074 * If already constructed any interface heirarchies before hitting
3075 * this error condition then Deref all interface heirarchies.
3076 */
3077 if (valid_if > 0) {
3078 for (i = 0; i < valid_if; i++) {
3079 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3080 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3081 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3082 }
3083 }
3084
3085 /*
3086 * If valid netdev not found, Return 0
3087 */
3088 return 0;
3089 }
Karthik Hariharan3e964a32015-09-22 21:40:52 +05303090
Shyam Sunder6358b862015-05-04 15:06:24 +05303091 dest_dev = br_dev_src;
Karthik Hariharan3e964a32015-09-22 21:40:52 +05303092
Shyam Sunder6358b862015-05-04 15:06:24 +05303093 }
3094
3095 dest_dev_type = dest_dev->type;
3096
3097 if (ecm_front_end_is_bridge_device(dest_dev)) {
3098 struct net_device *mc_br_slave_dev = NULL;
3099 uint32_t mc_max_dst = ECM_DB_MULTICAST_IF_MAX;
3100 uint32_t mc_dst_if_index[ECM_DB_MULTICAST_IF_MAX];
3101
3102 if (ECM_IP_ADDR_IS_V4(packet_src_addr)) {
3103 if_num = mc_bridge_ipv4_get_if(dest_dev, htonl((packet_src_addr[0])), htonl(packet_dest_addr[0]), mc_max_dst, mc_dst_if_index);
3104 } else {
3105 struct in6_addr origin6;
3106 struct in6_addr group6;
3107 ECM_IP_ADDR_TO_NIN6_ADDR(origin6, packet_src_addr);
3108 ECM_IP_ADDR_TO_NIN6_ADDR(group6, packet_dest_addr);
3109 if_num = mc_bridge_ipv6_get_if(dest_dev, &origin6, &group6, mc_max_dst, mc_dst_if_index);
3110 }
3111
Shyam Sunder81836832015-07-09 19:18:25 +05303112 if ((if_num < 0) || (if_num > ECM_DB_MULTICAST_IF_MAX)) {
3113 int i;
3114 DEBUG_WARN("MCS is not ready\n");
3115
3116 /*
3117 * If already constructed any interface heirarchies before hitting
3118 * this error condition then Deref all interface heirarchies.
3119 */
3120 if (valid_if > 0) {
3121 for (i = 0; i < valid_if; i++) {
3122 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3123 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3124 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3125 }
3126 }
3127
Shyam Sunder41637872015-06-11 21:19:05 +05303128 dev_put(dest_dev);
3129 return 0;
3130 }
3131
Shyam Sundera450b582016-07-22 14:44:14 +05303132 if (in_dev && !mfc_update) {
Shyam Sunder06588292016-04-18 19:57:39 +05303133 if_num = ecm_interface_multicast_check_for_src_ifindex(mc_dst_if_index, if_num, in_dev->ifindex);
3134 }
Shyam Sundera2e08ee2015-06-18 21:32:13 +05303135
Shyam Sunder6358b862015-05-04 15:06:24 +05303136 for (br_if = 0; br_if < if_num; br_if++) {
3137 mc_br_slave_dev = dev_get_by_index(&init_net, mc_dst_if_index[br_if]);
3138 if (!mc_br_slave_dev) {
3139 continue;
3140 }
3141
3142 if ((valid_if + br_if) > ECM_DB_MULTICAST_IF_MAX) {
3143 int i;
3144
3145 /*
3146 * If already constructed any interface heirarchies before hitting
3147 * this error condition then Deref all interface heirarchies.
3148 */
3149 for (i = 0; i < (valid_if + br_if); i++) {
3150 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3151 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3152 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3153 }
3154
3155 dev_put(dest_dev);
3156 dev_put(mc_br_slave_dev);
3157 return 0;
3158 }
3159
3160 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, valid_if + br_if);
3161 /*
3162 * Construct a single interface heirarchy of a multicast dev.
3163 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05303164 ii_cnt = ecm_interface_multicast_heirarchy_construct_single(feci, packet_src_addr, packet_dest_addr, ifaces, dest_dev, mc_br_slave_dev, NULL, true, layer4hdr, skb);
Shyam Sunder6358b862015-05-04 15:06:24 +05303165 if (ii_cnt == ECM_DB_IFACE_HEIRARCHY_MAX) {
3166
3167 /*
3168 * If already constructed any interface heirarchies before hitting
3169 * this error condition then Deref all interface heirarchies.
3170 */
3171 if ((valid_if + br_if) > 0) {
3172 int i;
3173 for (i = 0; i < (valid_if + br_if); i++) {
3174 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3175 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3176 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3177 }
3178 }
3179
3180 dev_put(dest_dev);
3181 dev_put(mc_br_slave_dev);
3182 return 0;
3183 }
3184
3185 interface_first = ecm_db_multicast_if_first_get_at_index(interface_first_base, (valid_if + br_if));
3186 *interface_first = ii_cnt;
3187 total_ii_count += ii_cnt;
3188 dev_put(mc_br_slave_dev);
3189 }
3190
3191 valid_if += br_if;
3192
3193 } else {
3194
3195 DEBUG_ASSERT(valid_if < ECM_DB_MULTICAST_IF_MAX, "Bad array index size %d\n", valid_if);
3196 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, valid_if);
3197 /*
3198 * Construct a single interface heirarchy of a multicast dev.
3199 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05303200 ii_cnt = ecm_interface_multicast_heirarchy_construct_single(feci, packet_src_addr, packet_dest_addr, ifaces, dest_dev, NULL, NULL, true, layer4hdr, skb);
Shyam Sunder6358b862015-05-04 15:06:24 +05303201 if (ii_cnt == ECM_DB_IFACE_HEIRARCHY_MAX) {
3202
3203 /*
3204 * If already constructed any interface heirarchies before hitting
3205 * this error condition then Deref all interface heirarchies.
3206 */
3207 if (valid_if > 0) {
3208 int i;
3209 for (i = 0; i < valid_if; i++) {
3210 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3211 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3212 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3213 }
3214 }
3215
3216 dev_put(dest_dev);
3217 return 0;
3218 }
3219
3220 interface_first = ecm_db_multicast_if_first_get_at_index(interface_first_base, valid_if);
3221 *interface_first = ii_cnt;
3222 total_ii_count += ii_cnt;
3223 valid_if++;
3224 }
3225
3226 dev_put(dest_dev);
3227 }
3228 return total_ii_count;
3229}
3230EXPORT_SYMBOL(ecm_interface_multicast_heirarchy_construct_routed);
3231
3232/*
3233 * ecm_interface_multicast_heirarchy_construct_bridged()
3234 * This function called when the Hyfi bridge snooper has IGMP/IMLD updates, this function
3235 * creates destination interface heirarchy for a bridged multicast connection.
3236 *
3237 * interfaces Pointer to the 2-D array of multicast interface heirarchies
3238 * dest_dev Pointer to the destination dev, here dest_dev is always a bridge type
3239 * packet_src_addr Source IP of the multicast flow
3240 * packet_dest_addr Group(dest) IP of the multicast flow
3241 * mc_max_dst Maximum number of bridge slaves joined the multicast group
3242 * mc_dst_if_index_base An array of if index joined the multicast group
3243 * interface_first_base An array of the index of the first interface in the list
3244 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07003245int32_t ecm_interface_multicast_heirarchy_construct_bridged(struct ecm_front_end_connection_instance *feci,
Shyam Sunder81836832015-07-09 19:18:25 +05303246 struct ecm_db_iface_instance *interfaces, struct net_device *dest_dev,
3247 ip_addr_t packet_src_addr, ip_addr_t packet_dest_addr, uint8_t mc_max_dst,
Suman Ghoshfb8e7702015-08-20 15:27:57 +05303248 int *mc_dst_if_index_base, uint32_t *interface_first_base, uint8_t *src_node_addr,
ratheesh kannotha32fdd12015-09-09 08:02:58 +05303249 __be16 *layer4hdr, struct sk_buff *skb)
Shyam Sunder6358b862015-05-04 15:06:24 +05303250{
3251 struct ecm_db_iface_instance *to_list_single[ECM_DB_IFACE_HEIRARCHY_MAX];
3252 struct ecm_db_iface_instance *ifaces;
3253 struct net_device *mc_br_slave_dev = NULL;
3254 uint32_t *interface_first;
3255 int *mc_dst_if_index;
3256 int valid_if;
3257 int ii_cnt = 0;
3258 int br_if;
3259 int total_ii_cnt = 0;
3260
3261 /*
3262 * Go through the newly joined interface index one by one and
3263 * create an interface heirarchy for each valid interface.
3264 */
3265 for (br_if = 0, valid_if = 0; br_if < mc_max_dst; br_if++) {
3266 mc_dst_if_index = (int *)ecm_db_multicast_if_num_get_at_index(mc_dst_if_index_base, br_if);
3267 mc_br_slave_dev = dev_get_by_index(&init_net, *mc_dst_if_index);
3268 if (!mc_br_slave_dev) {
3269
3270 /*
3271 * If already constructed any interface heirarchies before hitting
3272 * this error condition then Deref all interface heirarchies.
3273 */
3274 if (valid_if > 0) {
3275 int i;
3276 for (i = 0; i < valid_if; i++) {
3277 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3278 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3279 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3280 }
3281 }
3282
3283 /*
3284 * If valid netdev not found, Return 0
3285 */
3286 return 0;
3287 }
3288
3289 if (valid_if > ECM_DB_MULTICAST_IF_MAX) {
3290 int i;
3291
3292 /*
3293 * If already constructed any interface heirarchies before hitting
3294 * this error condition then Deref all interface heirarchies.
3295 */
3296 for (i = 0; i < valid_if; i++) {
3297 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3298 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3299 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3300 }
3301
3302 dev_put(mc_br_slave_dev);
3303 return 0;
3304 }
3305
3306 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, valid_if);
3307
3308 /*
3309 * Construct a single interface heirarchy of a multicast dev.
3310 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05303311 ii_cnt = ecm_interface_multicast_heirarchy_construct_single(feci, packet_src_addr, packet_dest_addr, ifaces, dest_dev, mc_br_slave_dev, src_node_addr, false, layer4hdr, skb);
Shyam Sunder6358b862015-05-04 15:06:24 +05303312 if (ii_cnt == ECM_DB_IFACE_HEIRARCHY_MAX) {
3313
3314 /*
3315 * If already constructed any interface heirarchies before hitting
3316 * this error condition then Deref all interface heirarchies.
3317 */
3318 if (valid_if > 0) {
3319 int i;
3320 for (i = 0; i < valid_if; i++) {
3321 ifaces = ecm_db_multicast_if_heirarchy_get(interfaces, i);
3322 ecm_db_multicast_copy_if_heirarchy(to_list_single, ifaces);
3323 ecm_db_connection_interfaces_deref(to_list_single, interface_first_base[i]);
3324 }
3325 }
3326
3327 dev_put(mc_br_slave_dev);
3328 return 0;
3329 }
3330
3331 interface_first = ecm_db_multicast_if_first_get_at_index(interface_first_base, valid_if);
3332 *interface_first = ii_cnt;
3333 total_ii_cnt += ii_cnt;
3334 valid_if++;
3335 dev_put(mc_br_slave_dev);
3336 }
3337
Shyam Sunder280b34a2016-06-22 19:30:22 +05303338 return valid_if;
Shyam Sunder6358b862015-05-04 15:06:24 +05303339}
3340EXPORT_SYMBOL(ecm_interface_multicast_heirarchy_construct_bridged);
Shyam Sunder6358b862015-05-04 15:06:24 +05303341
Ben Menchaca84f36632014-02-28 20:57:38 +00003342/*
Murat Sezgin5dae8832015-12-03 14:23:19 -08003343 * ecm_interface_multicast_get_next_node_mac_address()
3344 * Get the MAC address of the next node for multicast flows
3345 *
3346 * TODO: This function will be removed when the multicast flow code
3347 * is fixed to use the new interface hierarchy construction model.
3348 *
Murat Sezgin3aea6c92015-11-13 13:14:12 -08003349 */
Murat Sezgin5dae8832015-12-03 14:23:19 -08003350static bool ecm_interface_multicast_get_next_node_mac_address(
Murat Sezgin3aea6c92015-11-13 13:14:12 -08003351 ip_addr_t dest_addr, struct net_device *dest_dev, int ip_version,
3352 uint8_t *mac_addr)
3353{
3354 bool on_link;
3355 ip_addr_t gw_addr = ECM_IP_ADDR_NULL;
3356
3357 if (!ecm_interface_mac_addr_get(dest_addr, mac_addr, &on_link, gw_addr)) {
3358 if (ip_version == 4) {
3359 DEBUG_WARN("Unable to obtain MAC address for " ECM_IP_ADDR_DOT_FMT "\n",
3360 ECM_IP_ADDR_TO_DOT(dest_addr));
3361 ecm_interface_send_arp_request(dest_dev, dest_addr, on_link, gw_addr);
3362 }
Murat Sezgin5dae8832015-12-03 14:23:19 -08003363#ifdef ECM_IPV6_ENABLE
Murat Sezgin3aea6c92015-11-13 13:14:12 -08003364 if (ip_version == 6) {
3365 DEBUG_WARN("Unable to obtain MAC address for " ECM_IP_ADDR_OCTAL_FMT "\n",
3366 ECM_IP_ADDR_TO_OCTAL(dest_addr));
3367 ecm_interface_send_neighbour_solicitation(dest_dev, dest_addr);
3368 }
Murat Sezgin5dae8832015-12-03 14:23:19 -08003369#endif
3370 return false;
3371 }
Murat Sezgin3aea6c92015-11-13 13:14:12 -08003372
Murat Sezgin5dae8832015-12-03 14:23:19 -08003373 return true;
3374}
Murat Sezgin8f70b312016-02-02 23:33:10 -08003375#endif
Murat Sezgin5dae8832015-12-03 14:23:19 -08003376
3377/*
3378 * ecm_interface_get_next_node_mac_address()
3379 * Get the MAC address of the next node
3380 */
Suman Ghosh37af2642018-03-22 13:05:33 +05303381static bool ecm_interface_get_next_node_mac_address(ip_addr_t dest_addr,
3382 struct net_device *dest_dev,
3383 int ip_version, uint8_t *mac_addr)
Murat Sezgin5dae8832015-12-03 14:23:19 -08003384{
Suman Ghosh37af2642018-03-22 13:05:33 +05303385 ip_addr_t gw_addr = ECM_IP_ADDR_NULL;
3386 bool on_link = true;
Murat Sezgin00627c12016-02-05 16:53:21 -08003387
Suman Ghosh37af2642018-03-22 13:05:33 +05303388 if (ecm_interface_mac_addr_get_no_route(dest_dev, dest_addr, mac_addr)) {
3389 return true;
Murat Sezgin3aea6c92015-11-13 13:14:12 -08003390 }
3391
Suman Ghosh37af2642018-03-22 13:05:33 +05303392 /*
3393 * MAC address look up failed. The host IP address may not be in the
3394 * neighbour table. So, let's send an ARP or neighbour solicitation
3395 * request to this host IP address, so in the subsequent lookups it can be
3396 * found.
3397 *
3398 * If we have a gateway address, try one more time with that address.
3399 * If it fails, send the request with the current dest_addr or
3400 * found gateway address.
3401 */
3402 if (ecm_interface_find_gateway(dest_addr, gw_addr)) {
3403 on_link = false;
3404 if (ecm_interface_mac_addr_get_no_route(dest_dev, gw_addr, mac_addr)) {
3405 DEBUG_TRACE("Found the mac address for the gateway\n");
3406 return true;
3407 }
3408 }
3409
3410 if (ip_version == 4) {
3411 DEBUG_WARN("Unable to obtain MAC address for " ECM_IP_ADDR_DOT_FMT " send ARP request\n",
3412 ECM_IP_ADDR_TO_DOT(dest_addr));
3413 ecm_interface_send_arp_request(dest_dev, dest_addr, on_link, gw_addr);
3414 }
3415
3416#ifdef ECM_IPV6_ENABLE
3417 if (ip_version == 6) {
3418 DEBUG_WARN("Unable to obtain MAC address for " ECM_IP_ADDR_OCTAL_FMT " send solicitation request\n",
3419 ECM_IP_ADDR_TO_OCTAL(dest_addr));
3420 ecm_interface_send_neighbour_solicitation(dest_dev, dest_addr);
3421 }
3422#endif
3423
3424 return false;
Murat Sezgin3aea6c92015-11-13 13:14:12 -08003425}
3426
3427/*
3428 * ecm_interface_should_update_egress_device_bridged()
3429 * Determine if the egress port should be re-evaluated in the bridged case
3430 *
3431 * This will be done if:
3432 * - The egress port is the one provided from the front-end
3433 * - The egress port is not a bridge, but is a slave of the bridge
3434 * - Not routed
3435 *
3436 * If these conditions hold, this function will hold a reference to the bridge
3437 * port and return it to the caller. Otherwise no reference will be held and
3438 * it will return NULL.
3439 */
3440static struct net_device *ecm_interface_should_update_egress_device_bridged(
3441 struct net_device *given_dest_dev, struct net_device *dest_dev,
3442 bool is_routed)
3443{
3444 struct net_device *bridge;
3445
3446 /*
3447 * Determine if we should attempt to fetch the bridge device
3448 */
3449 if (!given_dest_dev || is_routed || (dest_dev != given_dest_dev) ||
3450 ecm_front_end_is_bridge_device(given_dest_dev))
3451 return NULL;
3452
3453 bridge = ecm_interface_get_and_hold_dev_master(given_dest_dev);
3454
3455 if (!bridge)
3456 return NULL;
3457
Murat Sezgin3aea6c92015-11-13 13:14:12 -08003458 if (!ecm_front_end_is_bridge_device(bridge)) {
3459 /*
3460 * Master is not a bridge - free the reference and return
3461 */
3462 dev_put(bridge);
3463 return NULL;
3464 }
3465
3466 /*
3467 * Reference is held to bridge and must be freed by caller
3468 */
3469 return bridge;
3470}
3471
3472/*
Ben Menchaca84f36632014-02-28 20:57:38 +00003473 * ecm_interface_heirarchy_construct()
3474 * Construct an interface heirarchy.
3475 *
3476 * Using the given addressing, locate the interface heirarchy used to emit packets to that destination.
3477 * This is the heirarchy of interfaces a packet would transit to emit from the device.
Gareth Williams46f4b5f2014-06-01 23:35:23 +01003478 *
3479 * We will use the given src/dest devices when is_routed is false.
Murat Sezgin5dae8832015-12-03 14:23:19 -08003480 * When is_routed is true we will use the construct and the other devices (which is the src device of the
3481 * construct device) whcih were obtained from the skb's route field and passed to this function..
Gareth Williams46f4b5f2014-06-01 23:35:23 +01003482 *
Ben Menchaca84f36632014-02-28 20:57:38 +00003483 * For example, with this network arrangement:
3484 *
3485 * PPPoE--VLAN--BRIDGE--BRIDGE_PORT(LAG_MASTER)--LAG_SLAVE_0--10.22.33.11
3486 *
Gareth Williams43fc0852014-05-26 19:10:00 +01003487 * Given the packet_dest_addr IP address 10.22.33.11 this will create an interface heirarchy (in interracfes[]) of:
Ben Menchaca84f36632014-02-28 20:57:38 +00003488 * LAG_SLAVE_0 @ [ECM_DB_IFACE_HEIRARCHY_MAX - 5]
3489 * LAG_MASTER @ [ECM_DB_IFACE_HEIRARCHY_MAX - 4]
3490 * BRIDGE @ [ECM_DB_IFACE_HEIRARCHY_MAX - 3]
3491 * VLAN @ [ECM_DB_IFACE_HEIRARCHY_MAX - 2]
3492 * PPPOE @ [ECM_DB_IFACE_HEIRARCHY_MAX - 1]
3493 * The value returned is (ECM_DB_IFACE_HEIRARCHY_MAX - 5)
3494 *
3495 * IMPORTANT: This function will return any known interfaces in the database, when interfaces do not exist in the database
3496 * they will be created and added automatically to the database.
Ben Menchaca84f36632014-02-28 20:57:38 +00003497 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07003498int32_t ecm_interface_heirarchy_construct(struct ecm_front_end_connection_instance *feci,
3499 struct ecm_db_iface_instance *interfaces[],
Murat Sezgin5dae8832015-12-03 14:23:19 -08003500 struct net_device *const_if, struct net_device *other_if,
Murat Sezginf92db492016-07-15 11:33:15 -07003501 ip_addr_t lookup_src_addr,
3502 ip_addr_t lookup_dest_addr,
3503 ip_addr_t real_dest_addr,
Murat Sezgin5dae8832015-12-03 14:23:19 -08003504 int ip_version, int packet_protocol,
3505 struct net_device *given_dest_dev,
3506 bool is_routed, struct net_device *given_src_dev,
3507 uint8_t *dest_node_addr, uint8_t *src_node_addr,
3508 __be16 *layer4hdr, struct sk_buff *skb)
3509{
3510 int protocol;
3511 ip_addr_t src_addr;
3512 ip_addr_t dest_addr;
3513 struct net_device *dest_dev;
3514 char *dest_dev_name;
3515 int32_t dest_dev_type;
3516 struct net_device *src_dev;
3517 char *src_dev_name;
3518 int32_t src_dev_type;
3519 int32_t current_interface_index;
3520 bool from_local_addr;
3521 bool next_dest_addr_valid;
3522 bool next_dest_node_addr_valid = false;
3523 ip_addr_t next_dest_addr;
3524 uint8_t next_dest_node_addr[ETH_ALEN] = {0};
Murat Sezgin70c44cd2016-03-02 11:32:10 -08003525 struct net_device *bridge;
Suman Ghosh9bb0abb2018-03-19 01:08:54 +05303526 struct net_device *top_dev = NULL;
Murat Sezgin545cf7d2016-03-04 09:48:21 -08003527 uint32_t serial = ecm_db_connection_serial_get(feci->ci);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003528
3529 /*
3530 * Get a big endian of the IPv4 address we have been given as our starting point.
3531 */
3532 protocol = packet_protocol;
Murat Sezginf92db492016-07-15 11:33:15 -07003533 ECM_IP_ADDR_COPY(src_addr, lookup_src_addr);
3534 ECM_IP_ADDR_COPY(dest_addr, lookup_dest_addr);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003535
3536 if (ip_version == 4) {
Murat Sezgin545cf7d2016-03-04 09:48:21 -08003537 DEBUG_TRACE("Construct interface heirarchy for from src_addr: " ECM_IP_ADDR_DOT_FMT " to dest_addr: " ECM_IP_ADDR_DOT_FMT ", protocol: %d (serial %u)\n",
3538 ECM_IP_ADDR_TO_DOT(src_addr), ECM_IP_ADDR_TO_DOT(dest_addr), protocol,
3539 serial);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003540#ifdef ECM_IPV6_ENABLE
3541 } else if (ip_version == 6) {
Murat Sezgin545cf7d2016-03-04 09:48:21 -08003542 DEBUG_TRACE("Construct interface heirarchy for from src_addr: " ECM_IP_ADDR_OCTAL_FMT " to dest_addr: " ECM_IP_ADDR_OCTAL_FMT ", protocol: %d (serial %u)\n",
3543 ECM_IP_ADDR_TO_OCTAL(src_addr), ECM_IP_ADDR_TO_OCTAL(dest_addr), protocol,
3544 serial);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003545#endif
3546 } else {
3547 DEBUG_WARN("Wrong IP protocol: %d\n", ip_version);
3548 return ECM_DB_IFACE_HEIRARCHY_MAX;
3549 }
3550
3551 /*
3552 * Get device to reach the given destination address.
3553 * If the heirarchy is for a routed connection we must use the devices obtained from the skb's route information..
3554 * If the heirarchy is NOT for a routed connection we try the given_dest_dev.
3555 */
3556 from_local_addr = false;
3557 if (!is_routed) {
3558 dest_dev = given_dest_dev;
3559 dev_hold(dest_dev);
3560 } else {
3561 dest_dev = ecm_interface_dev_find_by_local_addr(dest_addr);
3562 if (dest_dev) {
3563 from_local_addr = true;
3564 } else {
3565 dest_dev = const_if;
3566 dev_hold(dest_dev);
3567 }
3568 }
3569
3570 /*
3571 * If the address is a local address and protocol is an IP tunnel
3572 * then this connection is a tunnel endpoint made to this device.
3573 * In which case we circumvent all proper procedure and just hack the devices to make stuff work.
3574 *
3575 * TODO THIS MUST BE FIXED - WE MUST USE THE INTERFACE HIERARCHY FOR ITS INTENDED PURPOSE TO
3576 * PARSE THE DEVICES AND WORK OUT THE PROPER INTERFACES INVOLVED.
3577 * E.G. IF WE TRIED TO RUN A TUNNEL OVER A VLAN OR QINQ THIS WILL BREAK AS WE DON'T DISCOVER THAT HIERARCHY
3578 */
3579 if (dest_dev && from_local_addr) {
3580 if (((ip_version == 4) && (protocol == IPPROTO_IPV6)) ||
Murat Sezgin31effef2017-11-13 15:09:34 -08003581 ((ip_version == 6) && (protocol == IPPROTO_IPIP))
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303582#if defined(ECM_INTERFACE_GRE_TAP_ENABLE) || defined(ECM_INTERFACE_GRE_TUN_ENABLE)
3583 || ((protocol == IPPROTO_GRE))) {
Murat Sezgin31effef2017-11-13 15:09:34 -08003584#else
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303585 ) {
Murat Sezgin31effef2017-11-13 15:09:34 -08003586#endif
Murat Sezgin5dae8832015-12-03 14:23:19 -08003587 dev_put(dest_dev);
3588 dest_dev = given_dest_dev;
3589 if (dest_dev) {
3590 dev_hold(dest_dev);
3591 if (ip_version == 4) {
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303592 DEBUG_TRACE("HACK: %s tunnel packet with dest_addr: " ECM_IP_ADDR_DOT_FMT " uses dev: %p(%s)\n", "IPV4", ECM_IP_ADDR_TO_DOT(dest_addr), dest_dev, dest_dev->name);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003593 } else {
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303594 DEBUG_TRACE("HACK: %s tunnel packet with dest_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", "IPV6", ECM_IP_ADDR_TO_OCTAL(dest_addr), dest_dev, dest_dev->name);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003595 }
3596 }
3597 }
3598 }
3599
3600#ifdef ECM_INTERFACE_L2TPV2_ENABLE
ratheesh kannothf4801a02016-12-09 22:46:39 +05303601
Murat Sezgin5dae8832015-12-03 14:23:19 -08003602 /*
3603 * if the address is a local address and indev=l2tp.
3604 */
ratheesh kannothf4801a02016-12-09 22:46:39 +05303605 if ((given_src_dev->type == ARPHRD_PPP) && (given_src_dev->priv_flags & IFF_PPP_L2TPV2) && ppp_is_xmit_locked(given_src_dev)) {
Murat Sezgin5dae8832015-12-03 14:23:19 -08003606 dev_put(dest_dev);
3607 dest_dev = given_dest_dev;
3608 if (dest_dev) {
3609 dev_hold(dest_dev);
3610 DEBUG_TRACE("l2tp packet tunnel packet with dest_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", ECM_IP_ADDR_TO_OCTAL(dest_addr), dest_dev, dest_dev->name);
3611 }
3612 }
3613#endif
3614
3615#ifdef ECM_INTERFACE_PPTP_ENABLE
3616 /*
3617 * if the address is a local address and indev=PPTP.
3618 */
Suman Ghosh4273a8e2018-05-03 16:21:14 +05303619 if (protocol == IPPROTO_GRE && given_dest_dev && (given_dest_dev->priv_flags & IFF_PPP_PPTP)){
Murat Sezgin5dae8832015-12-03 14:23:19 -08003620 dev_put(dest_dev);
3621 dest_dev = given_dest_dev;
3622 if (dest_dev) {
3623 dev_hold(dest_dev);
3624 DEBUG_TRACE("PPTP packet tunnel packet with dest_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", ECM_IP_ADDR_TO_OCTAL(dest_addr), dest_dev, dest_dev->name);
3625 }
3626 }
3627#endif
3628
3629 if (!dest_dev) {
3630 DEBUG_WARN("dest_addr: " ECM_IP_ADDR_OCTAL_FMT " - cannot locate device\n", ECM_IP_ADDR_TO_OCTAL(dest_addr));
3631 return ECM_DB_IFACE_HEIRARCHY_MAX;
3632 }
3633 dest_dev_name = dest_dev->name;
3634 dest_dev_type = dest_dev->type;
3635
3636 /*
3637 * Get device to reach the given source address.
3638 * If the heirarchy is for a routed connection we must use the devices obtained from the skb's route information..
3639 * If the heirarchy is NOT for a routed connection we try the given_src_dev.
3640 */
3641 from_local_addr = false;
3642 if (!is_routed) {
3643 src_dev = given_src_dev;
3644 dev_hold(src_dev);
3645 } else {
3646 src_dev = ecm_interface_dev_find_by_local_addr(src_addr);
3647 if (src_dev) {
3648 from_local_addr = true;
3649 } else {
3650 src_dev = other_if;
3651 dev_hold(src_dev);
3652 }
3653 }
3654
3655 /*
3656 * If the address is a local address and protocol is an IP tunnel
3657 * then this connection is a tunnel endpoint made to this device.
3658 * In which case we circumvent all proper procedure and just hack the devices to make stuff work.
3659 *
3660 * TODO THIS MUST BE FIXED - WE MUST USE THE INTERFACE HIERARCHY FOR ITS INTENDED PURPOSE TO
3661 * PARSE THE DEVICES AND WORK OUT THE PROPER INTERFACES INVOLVED.
3662 * E.G. IF WE TRIED TO RUN A TUNNEL OVER A VLAN OR QINQ THIS WILL BREAK AS WE DON'T DISCOVER THAT HIERARCHY
3663 */
3664 if (src_dev && from_local_addr) {
3665 if (((ip_version == 4) && (protocol == IPPROTO_IPV6)) ||
Murat Sezgin31effef2017-11-13 15:09:34 -08003666 ((ip_version == 6) && (protocol == IPPROTO_IPIP))
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303667#if defined(ECM_INTERFACE_GRE_TAP_ENABLE) || defined(ECM_INTERFACE_GRE_TUN_ENABLE)
3668 || ((protocol == IPPROTO_GRE))) {
Murat Sezgin31effef2017-11-13 15:09:34 -08003669#else
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303670 ) {
Murat Sezgin31effef2017-11-13 15:09:34 -08003671#endif
Murat Sezgin5dae8832015-12-03 14:23:19 -08003672 dev_put(src_dev);
3673 src_dev = given_src_dev;
3674 if (src_dev) {
3675 dev_hold(src_dev);
3676 if (ip_version == 4) {
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303677 DEBUG_TRACE("HACK: %s tunnel packet with src_addr: " ECM_IP_ADDR_DOT_FMT " uses dev: %p(%s)\n", "IPV4", ECM_IP_ADDR_TO_DOT(src_addr), src_dev, src_dev->name);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003678 } else {
Suman Ghosh9e4245f2018-04-11 01:11:41 +05303679 DEBUG_TRACE("HACK: %s tunnel packet with src_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", "IPV6", ECM_IP_ADDR_TO_OCTAL(src_addr), src_dev, src_dev->name);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003680 }
3681 }
3682 }
3683 }
3684
3685 if (!src_dev) {
3686 DEBUG_WARN("src_addr: " ECM_IP_ADDR_OCTAL_FMT " - cannot locate device\n", ECM_IP_ADDR_TO_OCTAL(src_addr));
3687 dev_put(dest_dev);
3688 return ECM_DB_IFACE_HEIRARCHY_MAX;
3689 }
3690 src_dev_name = src_dev->name;
3691 src_dev_type = src_dev->type;
3692
3693 /*
3694 * Check if source and dest dev are same.
Murat Sezgin5dae8832015-12-03 14:23:19 -08003695 */
3696 if (src_dev == dest_dev) {
ratheesh kannotha6d25952016-04-11 12:23:26 +05303697 bool skip = false;
3698
Murat Sezgin5dae8832015-12-03 14:23:19 -08003699 DEBUG_TRACE("Protocol is :%d source dev and dest dev are same\n", protocol);
ratheesh kannotha6d25952016-04-11 12:23:26 +05303700
3701 switch (ip_version) {
3702 case 4:
3703 if ((protocol == IPPROTO_IPV6) || (protocol == IPPROTO_ESP)) {
3704 skip = true;
3705 break;
3706 }
3707
3708 if ((protocol == IPPROTO_UDP) && (udp_hdr(skb)->dest == htons(4500))) {
3709 skip = true;
3710 break;
3711 }
3712
3713 break;
3714
3715 case 6:
ratheesh kannotha6d25952016-04-11 12:23:26 +05303716 if ((protocol == IPPROTO_IPIP) || (protocol == IPPROTO_ESP)) {
3717 skip = true;
3718 break;
3719 }
3720
3721 break;
3722
3723 default:
3724 DEBUG_WARN("IP version = %d, Protocol = %d: Corrupted packet entered ecm\n", ip_version, protocol);
3725 skip = true;
3726 break;
3727 }
3728
3729 if (skip) {
Murat Sezgin5dae8832015-12-03 14:23:19 -08003730 /*
3731 * This happens from the input hook
3732 * We do not want to create a connection entry for this
3733 * TODO YES WE DO.
3734 * TODO THIS CONCERNS ME AS THIS SHOULD BE CAUGHT MUCH
3735 * EARLIER IN THE FRONT END IF POSSIBLE TO AVOID PERFORMANCE PENALTIES.
3736 * WE HAVE DONE A TREMENDOUS AMOUT OF WORK TO GET TO THIS POINT.
3737 * WE WILL ABORT HERE AND THIS WILL BE REPEATED FOR EVERY PACKET.
3738 * IN KEEPING WITH THE ECM DESIGN IT IS BETTER TO CREATE A CONNECTION AND RECORD IN THE HIERARCHY
3739 * ENOUGH INFORMATION TO ENSURE THAT ACCELERATION IS NOT BROKEN / DOES NOT OCCUR AT ALL.
3740 * THAT WAY WE DO A HEAVYWEIGHT ESTABLISHING OF A CONNECTION ONCE AND NEVER AGAIN...
3741 */
3742 dev_put(src_dev);
3743 dev_put(dest_dev);
3744 return ECM_DB_IFACE_HEIRARCHY_MAX;
3745 }
Murat Sezgin70c44cd2016-03-02 11:32:10 -08003746 }
Murat Sezgin5dae8832015-12-03 14:23:19 -08003747
Murat Sezgin70c44cd2016-03-02 11:32:10 -08003748 bridge = ecm_interface_should_update_egress_device_bridged(
3749 given_dest_dev, dest_dev, is_routed);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003750
Murat Sezgin70c44cd2016-03-02 11:32:10 -08003751 if (bridge) {
Murat Sezgin70c44cd2016-03-02 11:32:10 -08003752 struct net_device *new_dest_dev;
Murat Sezginb9bd5e02016-08-05 14:55:07 -07003753 new_dest_dev = br_port_dev_get(bridge, dest_node_addr, skb, serial);
3754 if (new_dest_dev) {
3755 dev_put(dest_dev);
3756 if (new_dest_dev != given_dest_dev) {
3757 DEBUG_INFO("Adjusted port for %pM is %s (given was %s)\n",
3758 dest_node_addr, new_dest_dev->name,
3759 given_dest_dev->name);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003760
Murat Sezginb9bd5e02016-08-05 14:55:07 -07003761 dest_dev = new_dest_dev;
3762 dest_dev_name = dest_dev->name;
3763 dest_dev_type = dest_dev->type;
Murat Sezgin5dae8832015-12-03 14:23:19 -08003764 }
Murat Sezgin5dae8832015-12-03 14:23:19 -08003765 }
Murat Sezgin70c44cd2016-03-02 11:32:10 -08003766 dev_put(bridge);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003767 }
3768
3769 next_dest_addr_valid = true;
3770 ECM_IP_ADDR_COPY(next_dest_addr, dest_addr);
3771
3772 /*
3773 * Iterate until we are done or get to the max number of interfaces we can record.
3774 * NOTE: current_interface_index tracks the position of the first interface position in interfaces[]
3775 * because we add from the end first_interface grows downwards.
3776 */
3777 current_interface_index = ECM_DB_IFACE_HEIRARCHY_MAX;
3778 while (current_interface_index > 0) {
3779 struct ecm_db_iface_instance *ii;
3780 struct net_device *next_dev;
3781 /*
3782 * Get the ecm db interface instance for the device at hand
3783 */
3784 ii = ecm_interface_establish_and_ref(feci, dest_dev, skb);
3785
3786 /*
3787 * If the interface could not be established then we abort
3788 */
3789 if (!ii) {
3790 DEBUG_WARN("Failed to establish interface: %p, name: %s\n", dest_dev, dest_dev_name);
3791 dev_put(src_dev);
3792 dev_put(dest_dev);
3793
3794 /*
3795 * Release the interfaces heirarchy we constructed to this point.
3796 */
3797 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
3798 return ECM_DB_IFACE_HEIRARCHY_MAX;
3799 }
3800
3801 /*
3802 * Record the interface instance into the interfaces[]
3803 */
3804 current_interface_index--;
3805 interfaces[current_interface_index] = ii;
3806
3807 /*
3808 * Now we have to figure out what the next device will be (in the transmission path) the skb
3809 * will use to emit to the destination address.
3810 */
3811 do {
3812#ifdef ECM_INTERFACE_PPP_ENABLE
3813 int channel_count;
3814 struct ppp_channel *ppp_chan[1];
3815 int channel_protocol;
3816#ifdef ECM_INTERFACE_PPPOE_ENABLE
3817 struct pppoe_opt addressing;
3818#endif
3819#endif
3820
3821 DEBUG_TRACE("Net device: %p is type: %d, name: %s\n", dest_dev, dest_dev_type, dest_dev_name);
3822 next_dev = NULL;
3823
3824 if (dest_dev_type == ARPHRD_ETHER) {
3825 /*
3826 * Ethernet - but what sub type?
3827 */
3828
3829#ifdef ECM_INTERFACE_VLAN_ENABLE
3830 /*
3831 * VLAN?
3832 */
3833 if (is_vlan_dev(dest_dev)) {
3834 /*
3835 * VLAN master
3836 * No locking needed here, ASSUMPTION is that real_dev is held for as long as we have dev.
3837 */
Shyam Sunder9db20852016-03-09 19:04:49 +05303838 next_dev = ecm_interface_vlan_real_dev(dest_dev);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003839 dev_hold(next_dev);
3840 DEBUG_TRACE("Net device: %p is VLAN, slave dev: %p (%s)\n",
3841 dest_dev, next_dev, next_dev->name);
Shyam Sunder2e9528b2016-07-05 14:45:55 +05303842 if (current_interface_index == (ECM_DB_IFACE_HEIRARCHY_MAX - 1)) {
Suman Ghosh9bb0abb2018-03-19 01:08:54 +05303843 top_dev = dest_dev;
Shyam Sunder2e9528b2016-07-05 14:45:55 +05303844 }
Murat Sezgin5dae8832015-12-03 14:23:19 -08003845 break;
3846 }
3847#endif
3848
3849 /*
3850 * BRIDGE?
3851 */
3852 if (ecm_front_end_is_bridge_device(dest_dev)) {
3853 /*
3854 * Bridge
3855 * Figure out which port device the skb will go to using the dest_addr.
3856 */
3857 uint8_t mac_addr[ETH_ALEN];
3858
3859 if (next_dest_node_addr_valid) {
3860 memcpy(mac_addr, next_dest_node_addr, ETH_ALEN);
3861 } else if (!next_dest_addr_valid) {
3862 dev_put(src_dev);
3863 dev_put(dest_dev);
3864
3865 /*
3866 * Release the interfaces heirarchy we constructed to this point.
3867 */
3868 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
3869 return ECM_DB_IFACE_HEIRARCHY_MAX;
3870 } else {
3871 if (!ecm_interface_get_next_node_mac_address(dest_addr, dest_dev, ip_version, mac_addr)) {
3872 dev_put(src_dev);
3873 dev_put(dest_dev);
3874
3875 /*
3876 * Release the interfaces heirarchy we constructed to this point.
3877 */
3878 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
3879 return ECM_DB_IFACE_HEIRARCHY_MAX;
3880 }
3881 }
3882
Murat Sezgin545cf7d2016-03-04 09:48:21 -08003883 next_dev = br_port_dev_get(dest_dev,
3884 mac_addr, skb, serial);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003885
3886 if (!next_dev) {
3887 DEBUG_WARN("Unable to obtain output port for: %pM\n", mac_addr);
3888 dev_put(src_dev);
3889 dev_put(dest_dev);
3890
3891 /*
3892 * Release the interfaces heirarchy we constructed to this point.
3893 */
3894 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
3895 return ECM_DB_IFACE_HEIRARCHY_MAX;
3896 }
3897 DEBUG_TRACE("Net device: %p is BRIDGE, next_dev: %p (%s)\n", dest_dev, next_dev, next_dev->name);
Suman Ghosh9bb0abb2018-03-19 01:08:54 +05303898 if (current_interface_index == (ECM_DB_IFACE_HEIRARCHY_MAX - 1)) {
3899 top_dev = dest_dev;
3900 }
Murat Sezgin5dae8832015-12-03 14:23:19 -08003901 break;
3902 }
3903
3904#ifdef ECM_INTERFACE_BOND_ENABLE
3905 /*
3906 * LAG?
3907 */
3908 if (ecm_front_end_is_lag_master(dest_dev)) {
3909 /*
3910 * Link aggregation
3911 * Figure out whiich slave device of the link aggregation will be used to reach the destination.
3912 */
3913 uint32_t src_addr_32 = 0;
3914 uint32_t dest_addr_32 = 0;
3915 struct in6_addr src_addr6;
3916 struct in6_addr dest_addr6;
3917 uint8_t src_mac_addr[ETH_ALEN];
3918 uint8_t dest_mac_addr[ETH_ALEN];
3919
3920 memset(src_mac_addr, 0, ETH_ALEN);
3921 memset(dest_mac_addr, 0, ETH_ALEN);
3922
3923 if (ip_version == 4) {
3924 ECM_IP_ADDR_TO_NIN4_ADDR(src_addr_32, src_addr);
Murat Sezginf92db492016-07-15 11:33:15 -07003925 ECM_IP_ADDR_TO_NIN4_ADDR(dest_addr_32, real_dest_addr);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003926 }
3927
3928 if (!is_routed) {
3929 memcpy(src_mac_addr, src_node_addr, ETH_ALEN);
3930 memcpy(dest_mac_addr, dest_node_addr, ETH_ALEN);
3931 } else {
Murat Sezgin7be87d22016-01-29 17:41:37 -08003932 struct net_device *master_dev;
Murat Sezgin5dae8832015-12-03 14:23:19 -08003933
3934 /*
Murat Sezgin7be87d22016-01-29 17:41:37 -08003935 * Use appropriate source MAC address for routed packets and
3936 * find proper interface to find the destination mac address and
3937 * from which to issue ARP or neighbour solicitation packet.
Murat Sezgin5dae8832015-12-03 14:23:19 -08003938 */
Murat Sezgin7be87d22016-01-29 17:41:37 -08003939 master_dev = ecm_interface_get_and_hold_dev_master(dest_dev);
3940 if (master_dev) {
3941 memcpy(src_mac_addr, master_dev->dev_addr, ETH_ALEN);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003942 } else {
Murat Sezgin7be87d22016-01-29 17:41:37 -08003943 master_dev = dest_dev;
Suman Ghosh9bb0abb2018-03-19 01:08:54 +05303944 if (top_dev) {
3945 master_dev = top_dev;
Shyam Sunder2e9528b2016-07-05 14:45:55 +05303946 }
Suman Ghosh9bb0abb2018-03-19 01:08:54 +05303947 memcpy(src_mac_addr, master_dev->dev_addr, ETH_ALEN);
Murat Sezgin7be87d22016-01-29 17:41:37 -08003948 dev_hold(master_dev);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003949 }
3950
3951 /*
3952 * Determine destination MAC address for this routed packet
3953 */
3954 if (next_dest_node_addr_valid) {
3955 memcpy(dest_mac_addr, next_dest_node_addr, ETH_ALEN);
3956 } else if (!next_dest_addr_valid) {
3957 dev_put(src_dev);
3958 dev_put(dest_dev);
Murat Sezgin7be87d22016-01-29 17:41:37 -08003959 dev_put(master_dev);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003960 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
3961 return ECM_DB_IFACE_HEIRARCHY_MAX;
3962 } else {
Murat Sezgin7be87d22016-01-29 17:41:37 -08003963 if (!ecm_interface_mac_addr_get_no_route(master_dev, dest_addr, dest_mac_addr)) {
Murat Sezgin5dae8832015-12-03 14:23:19 -08003964 ip_addr_t gw_addr = ECM_IP_ADDR_NULL;
3965 /*
3966 * Try one more time with gateway ip address if it exists.
3967 */
3968 if (!ecm_interface_find_gateway(dest_addr, gw_addr)) {
3969 goto lag_fail;
3970 }
3971
3972 if (ip_version == 4) {
3973 DEBUG_TRACE("Have a gw address " ECM_IP_ADDR_DOT_FMT "\n", ECM_IP_ADDR_TO_DOT(gw_addr));
3974 }
3975#ifdef ECM_IPV6_ENABLE
3976
3977 if (ip_version == 6) {
3978 DEBUG_TRACE("Have a gw address " ECM_IP_ADDR_OCTAL_FMT "\n", ECM_IP_ADDR_TO_OCTAL(gw_addr));
3979 }
3980#endif
Murat Sezgin7be87d22016-01-29 17:41:37 -08003981 if (ecm_interface_mac_addr_get_no_route(master_dev, gw_addr, dest_mac_addr)) {
Murat Sezgin5dae8832015-12-03 14:23:19 -08003982 DEBUG_TRACE("Found the mac address for gateway\n");
Murat Sezgin7be87d22016-01-29 17:41:37 -08003983 dev_put(master_dev);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003984 goto lag_success;
3985 }
3986
3987 if (ip_version == 4) {
Murat Sezgin7be87d22016-01-29 17:41:37 -08003988 ecm_interface_send_arp_request(master_dev, dest_addr, false, gw_addr);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003989
3990 DEBUG_WARN("Unable to obtain any MAC address for " ECM_IP_ADDR_DOT_FMT "\n", ECM_IP_ADDR_TO_DOT(dest_addr));
3991 }
3992#ifdef ECM_IPV6_ENABLE
3993 if (ip_version == 6) {
Murat Sezgin7be87d22016-01-29 17:41:37 -08003994 ecm_interface_send_neighbour_solicitation(master_dev, dest_addr);
Murat Sezgin5dae8832015-12-03 14:23:19 -08003995
3996 DEBUG_WARN("Unable to obtain any MAC address for " ECM_IP_ADDR_OCTAL_FMT "\n", ECM_IP_ADDR_TO_OCTAL(dest_addr));
3997 }
3998#endif
3999lag_fail:
4000 dev_put(src_dev);
4001 dev_put(dest_dev);
Murat Sezgin7be87d22016-01-29 17:41:37 -08004002 dev_put(master_dev);
Murat Sezgin5dae8832015-12-03 14:23:19 -08004003
4004 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4005 return ECM_DB_IFACE_HEIRARCHY_MAX;
4006 }
4007 }
Suman Ghosh11c6f9e2018-03-01 20:40:39 +05304008 dev_put(master_dev);
Murat Sezgin5dae8832015-12-03 14:23:19 -08004009 }
4010lag_success:
4011 if (ip_version == 4) {
4012 next_dev = bond_get_tx_dev(NULL, src_mac_addr, dest_mac_addr,
4013 &src_addr_32, &dest_addr_32,
4014 htons((uint16_t)ETH_P_IP), dest_dev, layer4hdr);
4015 } else if (ip_version == 6) {
4016 ECM_IP_ADDR_TO_NIN6_ADDR(src_addr6, src_addr);
Murat Sezginf92db492016-07-15 11:33:15 -07004017 ECM_IP_ADDR_TO_NIN6_ADDR(dest_addr6, real_dest_addr);
Murat Sezgin5dae8832015-12-03 14:23:19 -08004018 next_dev = bond_get_tx_dev(NULL, src_mac_addr, dest_mac_addr,
4019 src_addr6.s6_addr, dest_addr6.s6_addr,
Shyam Sundere60540c2016-04-29 15:06:35 +05304020 htons((uint16_t)ETH_P_IPV6), dest_dev, layer4hdr);
Murat Sezgin5dae8832015-12-03 14:23:19 -08004021 }
4022
4023 if (next_dev && netif_carrier_ok(next_dev)) {
4024 dev_hold(next_dev);
4025 } else {
4026 DEBUG_WARN("Unable to obtain LAG output slave device\n");
4027 dev_put(src_dev);
4028 dev_put(dest_dev);
4029
4030 /*
4031 * Release the interfaces heirarchy we constructed to this point.
4032 */
4033 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4034 return ECM_DB_IFACE_HEIRARCHY_MAX;
4035 }
4036
4037 DEBUG_TRACE("Net device: %p is LAG, slave dev: %p (%s)\n", dest_dev, next_dev, next_dev->name);
4038 break;
4039 }
4040#endif
4041
4042 /*
4043 * ETHERNET!
4044 * Just plain ethernet it seems.
4045 */
4046 DEBUG_TRACE("Net device: %p is ETHERNET\n", dest_dev);
4047 break;
4048 }
4049
Suman Ghosh9e4245f2018-04-11 01:11:41 +05304050#ifdef ECM_INTERFACE_GRE_TUN_ENABLE
4051 /*
4052 * GRE Tunnel?
4053 */
4054 if ((dest_dev_type == ARPHRD_IPGRE) || (dest_dev_type == ARPHRD_IP6GRE)) {
4055 DEBUG_TRACE("Net device: %p is GRE Tunnel type: %d\n", dest_dev, dest_dev_type);
4056 break;
4057 }
4058#endif
4059
Murat Sezgin5dae8832015-12-03 14:23:19 -08004060 /*
4061 * LOOPBACK?
4062 */
4063 if (dest_dev_type == ARPHRD_LOOPBACK) {
4064 DEBUG_TRACE("Net device: %p is LOOPBACK type: %d\n", dest_dev, dest_dev_type);
4065 break;
4066 }
4067
4068 /*
4069 * IPSEC?
4070 */
4071 if (dest_dev_type == ECM_ARPHRD_IPSEC_TUNNEL_TYPE) {
4072 DEBUG_TRACE("Net device: %p is IPSec tunnel type: %d\n", dest_dev, dest_dev_type);
4073 /* TODO Figure out the next device the tunnel is using... */
4074 break;
4075 }
4076
4077 /*
4078 * SIT (6-in-4)?
4079 */
4080 if (dest_dev_type == ARPHRD_SIT) {
4081 DEBUG_TRACE("Net device: %p is SIT (6-in-4) type: %d\n", dest_dev, dest_dev_type);
4082 /* TODO Figure out the next device the tunnel is using... */
4083 break;
4084 }
4085
4086 /*
4087 * IPIP6 Tunnel?
4088 */
4089 if (dest_dev_type == ARPHRD_TUNNEL6) {
4090 DEBUG_TRACE("Net device: %p is TUNIPIP6 type: %d\n", dest_dev, dest_dev_type);
4091 /* TODO Figure out the next device the tunnel is using... */
4092 break;
4093 }
4094
ratheesh kannothcfdcb332015-12-24 07:19:18 +05304095#ifdef ECM_INTERFACE_MAP_T_ENABLE
4096 /*
4097 * MAP-T xlate ?
4098 */
4099 if (dest_dev_type == ARPHRD_NONE) {
4100 if (is_map_t_dev(dest_dev)) {
4101 DEBUG_TRACE("Net device: %p is MAP-T type: %d\n", dest_dev, dest_dev_type);
4102 break;
4103 }
4104 }
4105#endif
4106
Murat Sezgin5dae8832015-12-03 14:23:19 -08004107 /*
4108 * If this is NOT PPP then it is unknown to the ecm and we cannot figure out it's next device.
4109 */
4110 if (dest_dev_type != ARPHRD_PPP) {
4111 DEBUG_TRACE("Net device: %p is UNKNOWN type: %d\n", dest_dev, dest_dev_type);
4112 break;
4113 }
4114
4115#ifndef ECM_INTERFACE_PPP_ENABLE
4116 DEBUG_TRACE("Net device: %p is UNKNOWN (PPP Unsupported) type: %d\n", dest_dev, dest_dev_type);
4117#else
4118 DEBUG_TRACE("Net device: %p is PPP\n", dest_dev);
4119
4120#ifdef ECM_INTERFACE_L2TPV2_ENABLE
ratheesh kannothf4801a02016-12-09 22:46:39 +05304121 if ((given_src_dev->priv_flags & IFF_PPP_L2TPV2) && ppp_is_xmit_locked(given_src_dev)) {
Murat Sezgin5dae8832015-12-03 14:23:19 -08004122 if (skb->skb_iif == dest_dev->ifindex) {
4123 DEBUG_TRACE("Net device: %p PPP channel is PPPoL2TPV2\n", dest_dev);
4124 break;
4125 }
4126 }
4127#endif
4128
4129#ifdef ECM_INTERFACE_PPTP_ENABLE
Suman Ghosh4273a8e2018-05-03 16:21:14 +05304130 if (protocol == IPPROTO_GRE && dest_dev && (dest_dev->priv_flags & IFF_PPP_PPTP)) {
Murat Sezgin5dae8832015-12-03 14:23:19 -08004131 DEBUG_TRACE("Net device: %p PPP channel is PPTP\n", dest_dev);
4132 break;
4133 }
4134#endif
4135 /*
4136 * PPP - but what is the channel type?
4137 * First: If this is multi-link then we do not support it
4138 */
4139 if (ppp_is_multilink(dest_dev) > 0) {
4140 DEBUG_TRACE("Net device: %p is MULTILINK PPP - Unknown to the ECM\n", dest_dev);
4141 break;
4142 }
4143
4144 /*
4145 * Get the PPP channel and then enquire what kind of channel it is
4146 * NOTE: Not multilink so only one channel to get.
4147 */
4148 channel_count = ppp_hold_channels(dest_dev, ppp_chan, 1);
4149 if (channel_count != 1) {
4150 DEBUG_TRACE("Net device: %p PPP has %d channels - Unknown to the ECM\n",
4151 dest_dev, channel_count);
4152 break;
4153 }
4154
4155 /*
4156 * Get channel protocol type
4157 * NOTE: Not all PPP channels support channel specific methods.
4158 */
4159 channel_protocol = ppp_channel_get_protocol(ppp_chan[0]);
4160
4161#ifdef ECM_INTERFACE_L2TPV2_ENABLE
4162 if (channel_protocol == PX_PROTO_OL2TP) {
4163
4164 /*
4165 * PPPoL2TPV2 channel
4166 */
4167 ppp_release_channels(ppp_chan, 1);
4168 DEBUG_TRACE("Net device: %p PPP channel is PPPoL2TPV2\n", dest_dev);
4169
4170 /*
4171 * Release the channel. Note that next_dev not held.
4172 */
4173 break;
4174 }
4175#endif
4176#ifdef ECM_INTERFACE_PPPOE_ENABLE
4177 if (channel_protocol == PX_PROTO_OE) {
4178 /*
4179 * PPPoE channel
4180 */
4181 DEBUG_TRACE("Net device: %p PPP channel is PPPoE\n", dest_dev);
4182
4183 /*
4184 * Get PPPoE session information and the underlying device it is using.
4185 */
4186 pppoe_channel_addressing_get(ppp_chan[0], &addressing);
4187
4188 /*
4189 * Copy the dev hold into this, we will release the hold later
4190 */
4191 next_dev = addressing.dev;
4192 next_dest_addr_valid = false;
4193 next_dest_node_addr_valid = true;
4194 memcpy(next_dest_node_addr, addressing.pa.remote, ETH_ALEN);
4195
4196 /*
4197 * Release the channel. Note that next_dev is still (correctly) held.
4198 */
4199 ppp_release_channels(ppp_chan, 1);
4200 break;
4201 }
4202#endif
4203
4204 DEBUG_TRACE("Net device: %p PPP channel protocol: %d - Unknown to the ECM\n",
4205 dest_dev, channel_protocol);
4206
4207 /*
4208 * Release the channel
4209 */
4210 ppp_release_channels(ppp_chan, 1);
4211
4212#endif
4213 } while (false);
4214
4215 /*
4216 * No longer need dest_dev as it may become next_dev
4217 */
4218 dev_put(dest_dev);
4219
4220 /*
4221 * Check out the next_dev, if any
4222 */
4223 if (!next_dev) {
4224 int32_t i __attribute__((unused));
4225 DEBUG_INFO("Completed interface heirarchy construct with first interface @: %d\n", current_interface_index);
4226#if DEBUG_LEVEL > 1
4227 for (i = current_interface_index; i < ECM_DB_IFACE_HEIRARCHY_MAX; ++i) {
4228 DEBUG_TRACE("\tInterface @ %d: %p, type: %d, name: %s\n",
Murat Sezgin3d9a7642018-05-15 16:24:09 -07004229 i, interfaces[i], ecm_db_iface_type_get(interfaces[i]), ecm_db_interface_type_to_string(ecm_db_iface_type_get(interfaces[i])));
Murat Sezgin5dae8832015-12-03 14:23:19 -08004230
4231 }
4232#endif
4233
4234 /*
4235 * Release src_dev now
4236 */
4237 dev_put(src_dev);
4238 return current_interface_index;
4239 }
4240
4241 /*
4242 * dest_dev becomes next_dev
4243 */
4244 dest_dev = next_dev;
4245 dest_dev_name = dest_dev->name;
4246 dest_dev_type = dest_dev->type;
4247 }
4248
4249 DEBUG_WARN("Too many interfaces: %d\n", current_interface_index);
4250 DEBUG_ASSERT(current_interface_index == 0, "Bad logic handling current_interface_index: %d\n", current_interface_index);
4251 dev_put(src_dev);
4252 dev_put(dest_dev);
4253
4254 /*
4255 * Release the interfaces heirarchy we constructed to this point.
4256 */
4257 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4258 return ECM_DB_IFACE_HEIRARCHY_MAX;
4259}
4260EXPORT_SYMBOL(ecm_interface_heirarchy_construct);
4261
4262#ifdef ECM_MULTICAST_ENABLE
4263/*
4264 * ecm_interface_multicast_from_heirarchy_construct()
4265 * Construct an interface heirarchy.
4266 *
4267 * TODO: This function will be removed later and ecm_interface_heirarchy_construct() function
4268 * will be used when the multicast code is fixed to use the new interface hierarchy
4269 * construction model which uses the skb's route information instead of doing
4270 * the route lookup based on the IP addresses.
4271 *
4272 * Using the given addressing, locate the interface heirarchy used to emit packets to that destination.
4273 * This is the heirarchy of interfaces a packet would transit to emit from the device.
4274 *
4275 * We will use the given src/dest devices when is_routed is false.
4276 * When is_routed is true we will try routing tables first, failing back to any given.
4277 *
4278 * For example, with this network arrangement:
4279 *
4280 * PPPoE--VLAN--BRIDGE--BRIDGE_PORT(LAG_MASTER)--LAG_SLAVE_0--10.22.33.11
4281 *
4282 * Given the packet_dest_addr IP address 10.22.33.11 this will create an interface heirarchy (in interracfes[]) of:
4283 * LAG_SLAVE_0 @ [ECM_DB_IFACE_HEIRARCHY_MAX - 5]
4284 * LAG_MASTER @ [ECM_DB_IFACE_HEIRARCHY_MAX - 4]
4285 * BRIDGE @ [ECM_DB_IFACE_HEIRARCHY_MAX - 3]
4286 * VLAN @ [ECM_DB_IFACE_HEIRARCHY_MAX - 2]
4287 * PPPOE @ [ECM_DB_IFACE_HEIRARCHY_MAX - 1]
4288 * The value returned is (ECM_DB_IFACE_HEIRARCHY_MAX - 5)
4289 *
4290 * IMPORTANT: This function will return any known interfaces in the database, when interfaces do not exist in the database
4291 * they will be created and added automatically to the database.
4292 */
4293int32_t ecm_interface_multicast_from_heirarchy_construct(struct ecm_front_end_connection_instance *feci,
4294 struct ecm_db_iface_instance *interfaces[],
Murat Sezgin188b4a32015-06-03 10:58:59 -07004295 ip_addr_t packet_src_addr,
4296 ip_addr_t packet_dest_addr,
4297 int ip_version, int packet_protocol,
4298 struct net_device *given_dest_dev,
4299 bool is_routed, struct net_device *given_src_dev,
Suman Ghoshfb8e7702015-08-20 15:27:57 +05304300 uint8_t *dest_node_addr, uint8_t *src_node_addr,
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304301 __be16 *layer4hdr, struct sk_buff *skb)
Ben Menchaca84f36632014-02-28 20:57:38 +00004302{
Ben Menchaca84f36632014-02-28 20:57:38 +00004303 int protocol;
4304 ip_addr_t src_addr;
4305 ip_addr_t dest_addr;
Ben Menchaca84f36632014-02-28 20:57:38 +00004306 struct net_device *dest_dev;
Ben Menchaca84f36632014-02-28 20:57:38 +00004307 char *dest_dev_name;
Ben Menchaca84f36632014-02-28 20:57:38 +00004308 int32_t dest_dev_type;
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004309 struct net_device *src_dev;
4310 char *src_dev_name;
4311 int32_t src_dev_type;
Ben Menchaca84f36632014-02-28 20:57:38 +00004312 int32_t current_interface_index;
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004313 bool from_local_addr;
Murat Sezgin7ef29962015-08-14 10:53:24 -07004314 bool next_dest_addr_valid;
Murat Sezgin3aea6c92015-11-13 13:14:12 -08004315 bool next_dest_node_addr_valid = false;
Murat Sezgin7ef29962015-08-14 10:53:24 -07004316 ip_addr_t next_dest_addr;
4317 uint8_t next_dest_node_addr[ETH_ALEN] = {0};
Murat Sezgin70c44cd2016-03-02 11:32:10 -08004318 struct net_device *bridge;
Murat Sezgin545cf7d2016-03-04 09:48:21 -08004319 uint32_t serial = ecm_db_connection_serial_get(feci->ci);
Ben Menchaca84f36632014-02-28 20:57:38 +00004320
4321 /*
4322 * Get a big endian of the IPv4 address we have been given as our starting point.
4323 */
4324 protocol = packet_protocol;
4325 ECM_IP_ADDR_COPY(src_addr, packet_src_addr);
4326 ECM_IP_ADDR_COPY(dest_addr, packet_dest_addr);
Murat Sezgin188b4a32015-06-03 10:58:59 -07004327
4328 if (ip_version == 4) {
Murat Sezgin545cf7d2016-03-04 09:48:21 -08004329 DEBUG_TRACE("Construct interface heirarchy for from src_addr: " ECM_IP_ADDR_DOT_FMT " to dest_addr: " ECM_IP_ADDR_DOT_FMT ", protocol: %d (serial %u)\n",
4330 ECM_IP_ADDR_TO_DOT(src_addr), ECM_IP_ADDR_TO_DOT(dest_addr), protocol,
4331 serial);
Murat Sezgin188b4a32015-06-03 10:58:59 -07004332#ifdef ECM_IPV6_ENABLE
4333 } else if (ip_version == 6) {
Murat Sezgin545cf7d2016-03-04 09:48:21 -08004334 DEBUG_TRACE("Construct interface heirarchy for from src_addr: " ECM_IP_ADDR_OCTAL_FMT " to dest_addr: " ECM_IP_ADDR_OCTAL_FMT ", protocol: %d (serial %u)\n",
4335 ECM_IP_ADDR_TO_OCTAL(src_addr), ECM_IP_ADDR_TO_OCTAL(dest_addr), protocol,
4336 serial);
Murat Sezgin188b4a32015-06-03 10:58:59 -07004337#endif
4338 } else {
4339 DEBUG_WARN("Wrong IP protocol: %d\n", ip_version);
4340 return ECM_DB_IFACE_HEIRARCHY_MAX;
4341 }
Ben Menchaca84f36632014-02-28 20:57:38 +00004342
4343 /*
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004344 * Get device to reach the given destination address.
4345 * If the heirarchy is for a routed connection we must try route lookup first, falling back to any given_dest_dev.
4346 * If the heirarchy is NOT for a routed connection we try the given_dest_dev first, followed by routed lookup.
Ben Menchaca84f36632014-02-28 20:57:38 +00004347 */
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004348 from_local_addr = false;
4349 if (is_routed) {
4350 dest_dev = ecm_interface_dev_find_by_addr(dest_addr, &from_local_addr);
4351 if (!dest_dev && given_dest_dev) {
4352 /*
4353 * Fall back to any given
4354 */
4355 dest_dev = given_dest_dev;
4356 dev_hold(dest_dev);
Gareth Williams43fc0852014-05-26 19:10:00 +01004357 }
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004358 } else if (given_dest_dev) {
4359 dest_dev = given_dest_dev;
4360 dev_hold(dest_dev);
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304361 } else {
4362 /*
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004363 * Fall back to routed look up
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304364 */
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004365 dest_dev = ecm_interface_dev_find_by_addr(dest_addr, &from_local_addr);
Gareth Williams43fc0852014-05-26 19:10:00 +01004366 }
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304367
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004368 /*
4369 * GGG ALERT: If the address is a local address and protocol is an IP tunnel
4370 * then this connection is a tunnel endpoint made to this device.
4371 * In which case we circumvent all proper procedure and just hack the devices to make stuff work.
4372 * GGG TODO THIS MUST BE FIXED - WE MUST USE THE INTERFACE HIERARCHY FOR ITS INTENDED PURPOSE TO
4373 * PARSE THE DEVICES AND WORK OUT THE PROPER INTERFACES INVOLVED.
4374 * E.G. IF WE TRIED TO RUN A TUNNEL OVER A VLAN OR QINQ THIS WILL BREAK AS WE DON'T DISCOVER THAT HIERARCHY
4375 */
Murat Sezgin188b4a32015-06-03 10:58:59 -07004376 if (dest_dev && from_local_addr) {
4377 if (((ip_version == 4) && (protocol == IPPROTO_IPV6)) ||
4378 ((ip_version == 6) && (protocol == IPPROTO_IPIP))) {
4379 dev_put(dest_dev);
4380 dest_dev = given_dest_dev;
4381 if (dest_dev) {
4382 dev_hold(dest_dev);
Tushar Mathurbac03242015-09-15 18:41:59 +05304383 if (ip_version == 4) {
4384 DEBUG_TRACE("HACK: %s tunnel packet with dest_addr: " ECM_IP_ADDR_DOT_FMT " uses dev: %p(%s)\n", "IPV6", ECM_IP_ADDR_TO_DOT(dest_addr), dest_dev, dest_dev->name);
4385 } else {
4386 DEBUG_TRACE("HACK: %s tunnel packet with dest_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", "IPIP", ECM_IP_ADDR_TO_OCTAL(dest_addr), dest_dev, dest_dev->name);
4387 }
Murat Sezgin188b4a32015-06-03 10:58:59 -07004388 }
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304389 }
Gareth Williams43fc0852014-05-26 19:10:00 +01004390 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304391
4392#ifdef ECM_INTERFACE_L2TPV2_ENABLE
4393 /*
4394 * if the address is a local address and indev=l2tp.
4395 */
ratheesh kannothf4801a02016-12-09 22:46:39 +05304396 if ((given_src_dev->type == ARPHRD_PPP) && (given_src_dev->priv_flags & IFF_PPP_L2TPV2) && ppp_is_xmit_locked(given_src_dev)) {
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304397 dev_put(dest_dev);
4398 dest_dev = given_dest_dev;
4399 if (dest_dev) {
4400 dev_hold(dest_dev);
4401 DEBUG_TRACE("l2tp packet tunnel packet with dest_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", ECM_IP_ADDR_TO_OCTAL(dest_addr), dest_dev, dest_dev->name);
4402 }
4403 }
4404#endif
4405
Shyam Sunder23f2e542015-09-28 14:56:49 +05304406#ifdef ECM_INTERFACE_PPTP_ENABLE
4407 /*
4408 * if the address is a local address and indev=PPTP.
4409 */
4410 if (protocol == IPPROTO_GRE && given_dest_dev && given_dest_dev->type == ARPHRD_PPP) {
4411 dev_put(dest_dev);
4412 dest_dev = given_dest_dev;
4413 if (dest_dev) {
4414 dev_hold(dest_dev);
4415 DEBUG_TRACE("PPTP packet tunnel packet with dest_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", ECM_IP_ADDR_TO_OCTAL(dest_addr), dest_dev, dest_dev->name);
4416 }
4417 }
4418#endif
4419
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004420 if (!dest_dev) {
4421 DEBUG_WARN("dest_addr: " ECM_IP_ADDR_OCTAL_FMT " - cannot locate device\n", ECM_IP_ADDR_TO_OCTAL(dest_addr));
4422 return ECM_DB_IFACE_HEIRARCHY_MAX;
4423 }
Ben Menchaca84f36632014-02-28 20:57:38 +00004424 dest_dev_name = dest_dev->name;
4425 dest_dev_type = dest_dev->type;
4426
4427 /*
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004428 * Get device to reach the given source address.
4429 * If the heirarchy is for a routed connection we must try route lookup first, falling back to any given_src_dev.
4430 * If the heirarchy is NOT for a routed connection we try the given_src_dev first, followed by routed lookup.
4431 */
4432 from_local_addr = false;
4433 if (is_routed) {
4434 src_dev = ecm_interface_dev_find_by_addr(src_addr, &from_local_addr);
4435 if (!src_dev && given_src_dev) {
4436 /*
4437 * Fall back to any given
4438 */
4439 src_dev = given_src_dev;
4440 dev_hold(src_dev);
4441 }
4442 } else if (given_src_dev) {
4443 src_dev = given_src_dev;
4444 dev_hold(src_dev);
4445 } else {
4446 /*
4447 * Fall back to routed look up
4448 */
4449 src_dev = ecm_interface_dev_find_by_addr(src_addr, &from_local_addr);
4450 }
4451
4452 /*
4453 * GGG ALERT: If the address is a local address and protocol is an IP tunnel
4454 * then this connection is a tunnel endpoint made to this device.
4455 * In which case we circumvent all proper procedure and just hack the devices to make stuff work.
4456 * GGG TODO THIS MUST BE FIXED - WE MUST USE THE INTERFACE HIERARCHY FOR ITS INTENDED PURPOSE TO
4457 * PARSE THE DEVICES AND WORK OUT THE PROPER INTERFACES INVOLVED.
4458 * E.G. IF WE TRIED TO RUN A TUNNEL OVER A VLAN OR QINQ THIS WILL BREAK AS WE DON'T DISCOVER THAT HIERARCHY
4459 */
Murat Sezgin188b4a32015-06-03 10:58:59 -07004460 if (src_dev && from_local_addr) {
4461 if (((ip_version == 4) && (protocol == IPPROTO_IPV6)) ||
4462 ((ip_version == 6) && (protocol == IPPROTO_IPIP))) {
4463 dev_put(src_dev);
4464 src_dev = given_src_dev;
4465 if (src_dev) {
4466 dev_hold(src_dev);
Tushar Mathurbac03242015-09-15 18:41:59 +05304467 if (ip_version == 4) {
4468 DEBUG_TRACE("HACK: %s tunnel packet with src_addr: " ECM_IP_ADDR_DOT_FMT " uses dev: %p(%s)\n", "IPV6", ECM_IP_ADDR_TO_DOT(src_addr), src_dev, src_dev->name);
4469 } else {
4470 DEBUG_TRACE("HACK: %s tunnel packet with src_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", "IPIP", ECM_IP_ADDR_TO_OCTAL(src_addr), src_dev, src_dev->name);
4471 }
Murat Sezgin188b4a32015-06-03 10:58:59 -07004472 }
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004473 }
4474 }
Murat Sezgin188b4a32015-06-03 10:58:59 -07004475
ratheesh kannothed721852015-09-28 12:39:52 +05304476#ifdef ECM_INTERFACE_L2TPV2_ENABLE
4477 /*
4478 * if the address is a local address and indev=l2tp.
4479 */
4480 if (skb && skb->sk && (skb->sk->sk_protocol == IPPROTO_UDP) && (udp_sk(skb->sk)->encap_type == UDP_ENCAP_L2TPINUDP)) {
4481 if (dest_dev != given_src_dev) {
4482 dev_put(src_dev);
4483 src_dev = given_src_dev;
4484 if (src_dev) {
4485 dev_hold(src_dev);
4486 DEBUG_TRACE("l2tp tunnel packet with src_addr: " ECM_IP_ADDR_OCTAL_FMT " uses dev: %p(%s)\n", ECM_IP_ADDR_TO_OCTAL(src_addr), src_dev, src_dev->name);
4487 }
4488 }
4489 }
4490#endif
4491
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004492 if (!src_dev) {
4493 DEBUG_WARN("src_addr: " ECM_IP_ADDR_OCTAL_FMT " - cannot locate device\n", ECM_IP_ADDR_TO_OCTAL(src_addr));
4494 dev_put(dest_dev);
4495 return ECM_DB_IFACE_HEIRARCHY_MAX;
4496 }
4497 src_dev_name = src_dev->name;
4498 src_dev_type = src_dev->type;
4499
4500 /*
4501 * Check if source and dest dev are same.
4502 * For the forwarded flows which involve tunnels this will happen when called from input hook.
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304503 */
4504 if (src_dev == dest_dev) {
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004505 DEBUG_TRACE("Protocol is :%d source dev and dest dev are same\n", protocol);
Murat Sezgin188b4a32015-06-03 10:58:59 -07004506 if (((ip_version == 4) && ((protocol == IPPROTO_IPV6) || (protocol == IPPROTO_ESP)))
4507 || ((ip_version == 6) && (protocol == IPPROTO_IPIP))) {
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304508 /*
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004509 * This happens from the input hook
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304510 * We do not want to create a connection entry for this
Gareth Williams46f4b5f2014-06-01 23:35:23 +01004511 * GGG TODO YES WE DO.
4512 * GGG TODO THIS CONCERNS ME AS THIS SHOULD BE CAUGHT MUCH
4513 * EARLIER IN THE FRONT END IF POSSIBLE TO AVOID PERFORMANCE PENALTIES.
4514 * WE HAVE DONE A TREMENDOUS AMOUT OF WORK TO GET TO THIS POINT.
4515 * WE WILL ABORT HERE AND THIS WILL BE REPEATED FOR EVERY PACKET.
4516 * IN KEEPING WITH THE ECM DESIGN IT IS BETTER TO CREATE A CONNECTION AND RECORD IN THE HIERARCHY
4517 * ENOUGH INFORMATION TO ENSURE THAT ACCELERATION IS NOT BROKEN / DOES NOT OCCUR AT ALL.
4518 * THAT WAY WE DO A HEAVYWEIGHT ESTABLISHING OF A CONNECTION ONCE AND NEVER AGAIN...
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304519 */
4520 dev_put(src_dev);
4521 dev_put(dest_dev);
4522 return ECM_DB_IFACE_HEIRARCHY_MAX;
4523 }
Murat Sezgin70c44cd2016-03-02 11:32:10 -08004524 }
Murat Sezgin3aea6c92015-11-13 13:14:12 -08004525
Murat Sezgin70c44cd2016-03-02 11:32:10 -08004526 bridge = ecm_interface_should_update_egress_device_bridged(
4527 given_dest_dev, dest_dev, is_routed);
Murat Sezgin3aea6c92015-11-13 13:14:12 -08004528
Murat Sezgin70c44cd2016-03-02 11:32:10 -08004529 if (bridge) {
Murat Sezgin70c44cd2016-03-02 11:32:10 -08004530 struct net_device *new_dest_dev;
Murat Sezginb9bd5e02016-08-05 14:55:07 -07004531 new_dest_dev = br_port_dev_get(bridge, dest_node_addr, skb, serial);
4532 if (new_dest_dev) {
4533 dev_put(dest_dev);
4534 if (new_dest_dev != given_dest_dev) {
4535 DEBUG_INFO("Adjusted port for %pM is %s (given was %s)\n",
4536 dest_node_addr, new_dest_dev->name,
4537 given_dest_dev->name);
Murat Sezgin3aea6c92015-11-13 13:14:12 -08004538
Murat Sezginb9bd5e02016-08-05 14:55:07 -07004539 dest_dev = new_dest_dev;
4540 dest_dev_name = dest_dev->name;
4541 dest_dev_type = dest_dev->type;
Murat Sezgin3aea6c92015-11-13 13:14:12 -08004542 }
Murat Sezgin3aea6c92015-11-13 13:14:12 -08004543 }
Murat Sezgin70c44cd2016-03-02 11:32:10 -08004544 dev_put(bridge);
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304545 }
4546
Murat Sezgin7ef29962015-08-14 10:53:24 -07004547 next_dest_addr_valid = true;
Murat Sezgin5dae8832015-12-03 14:23:19 -08004548 next_dest_node_addr_valid = false;
Murat Sezgin7ef29962015-08-14 10:53:24 -07004549 ECM_IP_ADDR_COPY(next_dest_addr, dest_addr);
4550
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05304551 /*
Ben Menchaca84f36632014-02-28 20:57:38 +00004552 * Iterate until we are done or get to the max number of interfaces we can record.
4553 * NOTE: current_interface_index tracks the position of the first interface position in interfaces[]
4554 * because we add from the end first_interface grows downwards.
4555 */
4556 current_interface_index = ECM_DB_IFACE_HEIRARCHY_MAX;
4557 while (current_interface_index > 0) {
4558 struct ecm_db_iface_instance *ii;
4559 struct net_device *next_dev;
Ben Menchaca84f36632014-02-28 20:57:38 +00004560 /*
4561 * Get the ecm db interface instance for the device at hand
4562 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304563 ii = ecm_interface_establish_and_ref(feci, dest_dev, skb);
Ben Menchaca84f36632014-02-28 20:57:38 +00004564
4565 /*
4566 * If the interface could not be established then we abort
4567 */
4568 if (!ii) {
4569 DEBUG_WARN("Failed to establish interface: %p, name: %s\n", dest_dev, dest_dev_name);
4570 dev_put(src_dev);
4571 dev_put(dest_dev);
4572
4573 /*
4574 * Release the interfaces heirarchy we constructed to this point.
4575 */
4576 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4577 return ECM_DB_IFACE_HEIRARCHY_MAX;
4578 }
4579
4580 /*
4581 * Record the interface instance into the interfaces[]
4582 */
4583 current_interface_index--;
4584 interfaces[current_interface_index] = ii;
4585
4586 /*
4587 * Now we have to figure out what the next device will be (in the transmission path) the skb
4588 * will use to emit to the destination address.
4589 */
4590 do {
Gareth Williamsdbb2bfd2014-11-20 16:42:09 -08004591#ifdef ECM_INTERFACE_PPP_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00004592 int channel_count;
4593 struct ppp_channel *ppp_chan[1];
Ben Menchaca84f36632014-02-28 20:57:38 +00004594 int channel_protocol;
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304595#ifdef ECM_INTERFACE_PPPOE_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00004596 struct pppoe_opt addressing;
Gareth Williamsc5b9d712014-05-09 20:40:07 +01004597#endif
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304598#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00004599
4600 DEBUG_TRACE("Net device: %p is type: %d, name: %s\n", dest_dev, dest_dev_type, dest_dev_name);
4601 next_dev = NULL;
4602
4603 if (dest_dev_type == ARPHRD_ETHER) {
4604 /*
4605 * Ethernet - but what sub type?
4606 */
4607
Gareth Williams141d2382014-11-25 11:35:19 -08004608#ifdef ECM_INTERFACE_VLAN_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00004609 /*
4610 * VLAN?
4611 */
4612 if (is_vlan_dev(dest_dev)) {
4613 /*
4614 * VLAN master
4615 * No locking needed here, ASSUMPTION is that real_dev is held for as long as we have dev.
4616 */
Shyam Sunder9db20852016-03-09 19:04:49 +05304617 next_dev = ecm_interface_vlan_real_dev(dest_dev);
Ben Menchaca84f36632014-02-28 20:57:38 +00004618 dev_hold(next_dev);
4619 DEBUG_TRACE("Net device: %p is VLAN, slave dev: %p (%s)\n",
4620 dest_dev, next_dev, next_dev->name);
4621 break;
4622 }
Gareth Williams141d2382014-11-25 11:35:19 -08004623#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00004624
4625 /*
4626 * BRIDGE?
4627 */
4628 if (ecm_front_end_is_bridge_device(dest_dev)) {
4629 /*
4630 * Bridge
4631 * Figure out which port device the skb will go to using the dest_addr.
4632 */
Ben Menchaca84f36632014-02-28 20:57:38 +00004633 uint8_t mac_addr[ETH_ALEN];
Murat Sezgin7ef29962015-08-14 10:53:24 -07004634
4635 if (next_dest_node_addr_valid) {
4636 memcpy(mac_addr, next_dest_node_addr, ETH_ALEN);
4637 } else if (!next_dest_addr_valid) {
Ben Menchaca84f36632014-02-28 20:57:38 +00004638 dev_put(src_dev);
4639 dev_put(dest_dev);
4640
4641 /*
4642 * Release the interfaces heirarchy we constructed to this point.
4643 */
4644 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4645 return ECM_DB_IFACE_HEIRARCHY_MAX;
Murat Sezgin7ef29962015-08-14 10:53:24 -07004646 } else {
Murat Sezgin5dae8832015-12-03 14:23:19 -08004647 if (!ecm_interface_multicast_get_next_node_mac_address(next_dest_addr, dest_dev, ip_version, mac_addr)) {
Murat Sezgin7ef29962015-08-14 10:53:24 -07004648 dev_put(src_dev);
4649 dev_put(dest_dev);
4650
4651 /*
4652 * Release the interfaces heirarchy we constructed to this point.
4653 */
4654 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4655 return ECM_DB_IFACE_HEIRARCHY_MAX;
4656 }
Ben Menchaca84f36632014-02-28 20:57:38 +00004657 }
Murat Sezgin545cf7d2016-03-04 09:48:21 -08004658 next_dev = br_port_dev_get(dest_dev,
4659 mac_addr, skb, serial);
Ben Menchaca84f36632014-02-28 20:57:38 +00004660 if (!next_dev) {
4661 DEBUG_WARN("Unable to obtain output port for: %pM\n", mac_addr);
4662 dev_put(src_dev);
4663 dev_put(dest_dev);
4664
4665 /*
4666 * Release the interfaces heirarchy we constructed to this point.
4667 */
4668 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4669 return ECM_DB_IFACE_HEIRARCHY_MAX;
4670 }
4671 DEBUG_TRACE("Net device: %p is BRIDGE, next_dev: %p (%s)\n", dest_dev, next_dev, next_dev->name);
4672 break;
4673 }
Gareth Williams8ac34292015-03-17 14:06:58 +00004674
Murat Sezginb3731e82014-11-26 12:20:59 -08004675#ifdef ECM_INTERFACE_BOND_ENABLE
Ben Menchaca84f36632014-02-28 20:57:38 +00004676 /*
4677 * LAG?
4678 */
4679 if (ecm_front_end_is_lag_master(dest_dev)) {
4680 /*
4681 * Link aggregation
Murat Sezginb3731e82014-11-26 12:20:59 -08004682 * Figure out whiich slave device of the link aggregation will be used to reach the destination.
Ben Menchaca84f36632014-02-28 20:57:38 +00004683 */
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304684 bool dest_on_link = false;
4685 ip_addr_t dest_gw_addr = ECM_IP_ADDR_NULL;
4686 uint32_t src_addr_32 = 0;
4687 uint32_t dest_addr_32 = 0;
Suman Ghoshf14d2172015-07-31 14:38:10 +05304688 struct in6_addr src_addr6;
4689 struct in6_addr dest_addr6;
Ben Menchaca84f36632014-02-28 20:57:38 +00004690 uint8_t src_mac_addr[ETH_ALEN];
4691 uint8_t dest_mac_addr[ETH_ALEN];
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304692 struct net_device *master_dev = NULL;
Ben Menchaca84f36632014-02-28 20:57:38 +00004693
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304694 memset(src_mac_addr, 0, ETH_ALEN);
4695 memset(dest_mac_addr, 0, ETH_ALEN);
Ben Menchaca84f36632014-02-28 20:57:38 +00004696
Gareth Williams341df382015-07-20 16:44:17 +01004697 if (ip_version == 4) {
4698 ECM_IP_ADDR_TO_NIN4_ADDR(src_addr_32, src_addr);
4699 ECM_IP_ADDR_TO_NIN4_ADDR(dest_addr_32, dest_addr);
4700 }
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304701
4702 if (!is_routed) {
4703 memcpy(src_mac_addr, src_node_addr, ETH_ALEN);
4704 memcpy(dest_mac_addr, dest_node_addr, ETH_ALEN);
4705 } else {
Murat Sezginb3731e82014-11-26 12:20:59 -08004706 struct net_device *dest_dev_master;
4707
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304708 /*
4709 * Use appropriate source MAC address for routed packets
4710 */
Murat Sezginb3731e82014-11-26 12:20:59 -08004711 dest_dev_master = ecm_interface_get_and_hold_dev_master(dest_dev);
4712 if (dest_dev_master) {
4713 memcpy(src_mac_addr, dest_dev_master->dev_addr, ETH_ALEN);
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304714 } else {
4715 memcpy(src_mac_addr, dest_dev->dev_addr, ETH_ALEN);
4716 }
4717
4718 /*
4719 * Determine destination MAC address for this routed packet
4720 */
Tushar Mathur78b5f432015-09-18 20:13:31 +05304721 if (next_dest_node_addr_valid) {
4722 memcpy(dest_mac_addr, next_dest_node_addr, ETH_ALEN);
4723 } else if (!next_dest_addr_valid) {
4724 dev_put(src_dev);
4725 dev_put(dest_dev);
Murat Sezginb3731e82014-11-26 12:20:59 -08004726 if (dest_dev_master) {
4727 dev_put(dest_dev_master);
4728 }
4729
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304730 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4731 return ECM_DB_IFACE_HEIRARCHY_MAX;
Tushar Mathur78b5f432015-09-18 20:13:31 +05304732 } else {
4733 if (!ecm_interface_mac_addr_get(dest_addr, dest_mac_addr,
4734 &dest_on_link, dest_gw_addr)) {
4735
4736 /*
4737 * Find proper interfce from which to issue ARP
4738 * or neighbour solicitation packet.
4739 */
4740 if (dest_dev_master) {
4741 master_dev = dest_dev_master;
4742 } else {
4743 master_dev = dest_dev;
4744 }
4745
4746 dev_hold(master_dev);
4747
4748 if (dest_dev_master) {
4749 dev_put(dest_dev_master);
4750 }
4751
4752 if (ip_version == 4) {
4753 DEBUG_WARN("Unable to obtain MAC address for " ECM_IP_ADDR_DOT_FMT "\n", ECM_IP_ADDR_TO_DOT(dest_addr));
4754 ecm_interface_send_arp_request(dest_dev, dest_addr, dest_on_link, dest_gw_addr);
4755 }
4756#ifdef ECM_IPV6_ENABLE
4757 if (ip_version == 6) {
Murat Sezgin5dae8832015-12-03 14:23:19 -08004758 DEBUG_WARN("Unable to obtain MAC address for " ECM_IP_ADDR_OCTAL_FMT "\n", ECM_IP_ADDR_TO_OCTAL(dest_addr));
Tushar Mathur78b5f432015-09-18 20:13:31 +05304759 ecm_interface_send_neighbour_solicitation(master_dev, dest_addr);
4760 }
4761#endif
4762 dev_put(src_dev);
4763 dev_put(dest_dev);
4764 dev_put(master_dev);
4765 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4766 return ECM_DB_IFACE_HEIRARCHY_MAX;
4767 }
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304768 }
Murat Sezginb3731e82014-11-26 12:20:59 -08004769
4770 if (dest_dev_master) {
4771 dev_put(dest_dev_master);
4772 }
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304773 }
4774
Murat Sezgin188b4a32015-06-03 10:58:59 -07004775 if (ip_version == 4) {
4776 next_dev = bond_get_tx_dev(NULL, src_mac_addr, dest_mac_addr,
4777 &src_addr_32, &dest_addr_32,
Suman Ghoshfb8e7702015-08-20 15:27:57 +05304778 htons((uint16_t)ETH_P_IP), dest_dev, layer4hdr);
Murat Sezgin188b4a32015-06-03 10:58:59 -07004779 } else if (ip_version == 6) {
Suman Ghoshf14d2172015-07-31 14:38:10 +05304780 ECM_IP_ADDR_TO_NIN6_ADDR(src_addr6, src_addr);
4781 ECM_IP_ADDR_TO_NIN6_ADDR(dest_addr6, dest_addr);
Murat Sezgin188b4a32015-06-03 10:58:59 -07004782 next_dev = bond_get_tx_dev(NULL, src_mac_addr, dest_mac_addr,
Suman Ghoshf14d2172015-07-31 14:38:10 +05304783 src_addr6.s6_addr, dest_addr6.s6_addr,
Shyam Sundere60540c2016-04-29 15:06:35 +05304784 htons((uint16_t)ETH_P_IPV6), dest_dev, layer4hdr);
Murat Sezgin188b4a32015-06-03 10:58:59 -07004785 }
4786
Tushar Mathur933907c2014-06-24 17:06:14 +05304787 if (next_dev && netif_carrier_ok(next_dev)) {
Ben Menchaca84f36632014-02-28 20:57:38 +00004788 dev_hold(next_dev);
4789 } else {
4790 DEBUG_WARN("Unable to obtain LAG output slave device\n");
4791 dev_put(src_dev);
4792 dev_put(dest_dev);
4793
4794 /*
4795 * Release the interfaces heirarchy we constructed to this point.
4796 */
4797 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
4798 return ECM_DB_IFACE_HEIRARCHY_MAX;
4799 }
4800
4801 DEBUG_TRACE("Net device: %p is LAG, slave dev: %p (%s)\n", dest_dev, next_dev, next_dev->name);
Ben Menchaca84f36632014-02-28 20:57:38 +00004802 break;
4803 }
Murat Sezginb3731e82014-11-26 12:20:59 -08004804#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00004805
4806 /*
4807 * ETHERNET!
4808 * Just plain ethernet it seems.
4809 */
4810 DEBUG_TRACE("Net device: %p is ETHERNET\n", dest_dev);
4811 break;
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05304812 }
Ben Menchaca84f36632014-02-28 20:57:38 +00004813
4814 /*
4815 * LOOPBACK?
4816 */
4817 if (dest_dev_type == ARPHRD_LOOPBACK) {
4818 DEBUG_TRACE("Net device: %p is LOOPBACK type: %d\n", dest_dev, dest_dev_type);
4819 break;
4820 }
4821
4822 /*
4823 * IPSEC?
4824 */
4825 if (dest_dev_type == ECM_ARPHRD_IPSEC_TUNNEL_TYPE) {
4826 DEBUG_TRACE("Net device: %p is IPSec tunnel type: %d\n", dest_dev, dest_dev_type);
Murat Sezgin5dae8832015-12-03 14:23:19 -08004827 /* TODO Figure out the next device the tunnel is using... */
Ben Menchaca84f36632014-02-28 20:57:38 +00004828 break;
4829 }
4830
4831 /*
4832 * SIT (6-in-4)?
4833 */
4834 if (dest_dev_type == ARPHRD_SIT) {
4835 DEBUG_TRACE("Net device: %p is SIT (6-in-4) type: %d\n", dest_dev, dest_dev_type);
Murat Sezgin5dae8832015-12-03 14:23:19 -08004836 /* TODO Figure out the next device the tunnel is using... */
Ben Menchaca84f36632014-02-28 20:57:38 +00004837 break;
4838 }
4839
4840 /*
4841 * IPIP6 Tunnel?
4842 */
4843 if (dest_dev_type == ARPHRD_TUNNEL6) {
4844 DEBUG_TRACE("Net device: %p is TUNIPIP6 type: %d\n", dest_dev, dest_dev_type);
Murat Sezgin5dae8832015-12-03 14:23:19 -08004845 /* TODO Figure out the next device the tunnel is using... */
Ben Menchaca84f36632014-02-28 20:57:38 +00004846 break;
4847 }
4848
4849 /*
4850 * If this is NOT PPP then it is unknown to the ecm and we cannot figure out it's next device.
4851 */
4852 if (dest_dev_type != ARPHRD_PPP) {
4853 DEBUG_TRACE("Net device: %p is UNKNOWN type: %d\n", dest_dev, dest_dev_type);
4854 break;
4855 }
4856
Gareth Williamsdbb2bfd2014-11-20 16:42:09 -08004857#ifndef ECM_INTERFACE_PPP_ENABLE
Gareth Williamsc5b9d712014-05-09 20:40:07 +01004858 DEBUG_TRACE("Net device: %p is UNKNOWN (PPP Unsupported) type: %d\n", dest_dev, dest_dev_type);
4859#else
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304860 DEBUG_TRACE("Net device: %p is PPP\n", dest_dev);
4861
4862#ifdef ECM_INTERFACE_L2TPV2_ENABLE
ratheesh kannothf4801a02016-12-09 22:46:39 +05304863 if ((given_src_dev->priv_flags & IFF_PPP_L2TPV2) && ppp_is_xmit_locked(given_src_dev)) {
ratheesh kannothed721852015-09-28 12:39:52 +05304864 if (skb->skb_iif == dest_dev->ifindex) {
4865 DEBUG_TRACE("Net device: %p PPP channel is PPPoL2TPV2\n", dest_dev);
4866 break;
4867 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304868 }
4869#endif
Shyam Sunder23f2e542015-09-28 14:56:49 +05304870
4871#ifdef ECM_INTERFACE_PPTP_ENABLE
4872 if (protocol == IPPROTO_GRE && dest_dev && dest_dev->type == ARPHRD_PPP) {
4873 DEBUG_TRACE("Net device: %p PPP channel is PPTP\n", dest_dev);
4874 break;
4875 }
4876#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00004877 /*
4878 * PPP - but what is the channel type?
4879 * First: If this is multi-link then we do not support it
4880 */
4881 if (ppp_is_multilink(dest_dev) > 0) {
4882 DEBUG_TRACE("Net device: %p is MULTILINK PPP - Unknown to the ECM\n", dest_dev);
4883 break;
4884 }
4885
Ben Menchaca84f36632014-02-28 20:57:38 +00004886 /*
4887 * Get the PPP channel and then enquire what kind of channel it is
4888 * NOTE: Not multilink so only one channel to get.
4889 */
4890 channel_count = ppp_hold_channels(dest_dev, ppp_chan, 1);
4891 if (channel_count != 1) {
4892 DEBUG_TRACE("Net device: %p PPP has %d channels - Unknown to the ECM\n",
4893 dest_dev, channel_count);
4894 break;
4895 }
4896
4897 /*
4898 * Get channel protocol type
Gareth Williams6f96a4b2014-05-29 19:41:21 +01004899 * NOTE: Not all PPP channels support channel specific methods.
Ben Menchaca84f36632014-02-28 20:57:38 +00004900 */
Gareth Williams6f96a4b2014-05-29 19:41:21 +01004901 channel_protocol = ppp_channel_get_protocol(ppp_chan[0]);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304902
4903#ifdef ECM_INTERFACE_L2TPV2_ENABLE
4904 if (channel_protocol == PX_PROTO_OL2TP) {
Radha krishna Simha Jiguruf7dc34c2014-05-12 18:59:07 +05304905
Ben Menchaca84f36632014-02-28 20:57:38 +00004906 /*
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304907 * PPPoL2TPV2 channel
Ben Menchaca84f36632014-02-28 20:57:38 +00004908 */
4909 ppp_release_channels(ppp_chan, 1);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304910 DEBUG_TRACE("Net device: %p PPP channel is PPPoL2TPV2\n", dest_dev);
Ben Menchaca84f36632014-02-28 20:57:38 +00004911
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304912 /*
4913 * Release the channel. Note that next_dev not held.
4914 */
Ben Menchaca84f36632014-02-28 20:57:38 +00004915 break;
4916 }
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304917#endif
4918#ifdef ECM_INTERFACE_PPPOE_ENABLE
4919 if (channel_protocol == PX_PROTO_OE) {
4920 /*
4921 * PPPoE channel
4922 */
4923 DEBUG_TRACE("Net device: %p PPP channel is PPPoE\n", dest_dev);
4924
4925 /*
4926 * Get PPPoE session information and the underlying device it is using.
4927 */
4928 pppoe_channel_addressing_get(ppp_chan[0], &addressing);
4929
4930 /*
4931 * Copy the dev hold into this, we will release the hold later
4932 */
4933 next_dev = addressing.dev;
4934 next_dest_addr_valid = false;
4935 next_dest_node_addr_valid = true;
4936 memcpy(next_dest_node_addr, addressing.pa.remote, ETH_ALEN);
4937
4938 /*
4939 * Release the channel. Note that next_dev is still (correctly) held.
4940 */
4941 ppp_release_channels(ppp_chan, 1);
4942 break;
4943 }
4944#endif
4945
4946 DEBUG_TRACE("Net device: %p PPP channel protocol: %d - Unknown to the ECM\n",
4947 dest_dev, channel_protocol);
Ben Menchaca84f36632014-02-28 20:57:38 +00004948
4949 /*
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304950 * Release the channel
Ben Menchaca84f36632014-02-28 20:57:38 +00004951 */
4952 ppp_release_channels(ppp_chan, 1);
ratheesh kannotha32fdd12015-09-09 08:02:58 +05304953
Gareth Williamsc5b9d712014-05-09 20:40:07 +01004954#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00004955 } while (false);
4956
4957 /*
Gareth Williamsa11d4352014-05-14 18:25:49 +01004958 * No longer need dest_dev as it may become next_dev
Ben Menchaca84f36632014-02-28 20:57:38 +00004959 */
Ben Menchaca84f36632014-02-28 20:57:38 +00004960 dev_put(dest_dev);
4961
4962 /*
4963 * Check out the next_dev, if any
4964 */
4965 if (!next_dev) {
4966 int32_t i __attribute__((unused));
4967 DEBUG_INFO("Completed interface heirarchy construct with first interface @: %d\n", current_interface_index);
4968#if DEBUG_LEVEL > 1
4969 for (i = current_interface_index; i < ECM_DB_IFACE_HEIRARCHY_MAX; ++i) {
4970 DEBUG_TRACE("\tInterface @ %d: %p, type: %d, name: %s\n",
Murat Sezgin3d9a7642018-05-15 16:24:09 -07004971 i, interfaces[i], ecm_db_iface_type_get(interfaces[i]), ecm_db_interface_type_to_string(ecm_db_iface_type_get(interfaces[i])));
Tushar Mathur4ab0bf92014-06-10 20:37:07 +05304972
Ben Menchaca84f36632014-02-28 20:57:38 +00004973 }
4974#endif
Gareth Williamsa11d4352014-05-14 18:25:49 +01004975
4976 /*
4977 * Release src_dev now
4978 */
4979 dev_put(src_dev);
Ben Menchaca84f36632014-02-28 20:57:38 +00004980 return current_interface_index;
4981 }
4982
Gareth Williamsa11d4352014-05-14 18:25:49 +01004983 /*
4984 * dest_dev becomes next_dev
4985 */
Ben Menchaca84f36632014-02-28 20:57:38 +00004986 dest_dev = next_dev;
4987 dest_dev_name = dest_dev->name;
4988 dest_dev_type = dest_dev->type;
4989 }
4990
4991 DEBUG_WARN("Too many interfaces: %d\n", current_interface_index);
4992 DEBUG_ASSERT(current_interface_index == 0, "Bad logic handling current_interface_index: %d\n", current_interface_index);
4993 dev_put(src_dev);
4994 dev_put(dest_dev);
4995
4996 /*
4997 * Release the interfaces heirarchy we constructed to this point.
4998 */
4999 ecm_db_connection_interfaces_deref(interfaces, current_interface_index);
5000 return ECM_DB_IFACE_HEIRARCHY_MAX;
5001}
Murat Sezgin5dae8832015-12-03 14:23:19 -08005002EXPORT_SYMBOL(ecm_interface_multicast_from_heirarchy_construct);
5003#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00005004
5005/*
Gareth Williamsadf425f2014-05-26 19:29:02 +01005006 * ecm_interface_list_stats_update()
5007 * Given an interface list, walk the interfaces and update the stats for certain types.
5008 */
5009static void ecm_interface_list_stats_update(int iface_list_first, struct ecm_db_iface_instance *iface_list[], uint8_t *mac_addr,
Suman Ghosh812ad302018-05-09 21:07:50 +05305010 bool is_mcast_flow, uint32_t tx_packets, uint32_t tx_bytes, uint32_t rx_packets, uint32_t rx_bytes,
5011 bool is_ported)
Gareth Williamsadf425f2014-05-26 19:29:02 +01005012{
5013 int list_index;
5014
5015 for (list_index = iface_list_first; (list_index < ECM_DB_IFACE_HEIRARCHY_MAX); list_index++) {
5016 struct ecm_db_iface_instance *ii;
5017 ecm_db_iface_type_t ii_type;
5018 char *ii_name;
5019 struct net_device *dev;
5020
5021 ii = iface_list[list_index];
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005022 ii_type = ecm_db_iface_type_get(ii);
Gareth Williamsadf425f2014-05-26 19:29:02 +01005023 ii_name = ecm_db_interface_type_to_string(ii_type);
5024 DEBUG_TRACE("list_index: %d, ii: %p, type: %d (%s)\n", list_index, ii, ii_type, ii_name);
5025
5026 /*
5027 * Locate real device in system
5028 */
5029 dev = dev_get_by_index(&init_net, ecm_db_iface_interface_identifier_get(ii));
5030 if (!dev) {
5031 DEBUG_WARN("Could not locate interface\n");
5032 continue;
5033 }
5034 DEBUG_TRACE("found dev: %p (%s)\n", dev, dev->name);
5035
Shyam Sunderbf40d0e2015-06-23 15:56:37 +05305036 if (likely(!is_mcast_flow)) {
5037 /*
5038 * Refresh the bridge forward table entry if the port is a bridge port
5039 * Note: A bridge port can be of different interface type, e.g VLAN, ethernet.
5040 * This check, therefore, should be performed for all interface types.
5041 */
Suman Ghosh812ad302018-05-09 21:07:50 +05305042 if (is_ported && is_valid_ether_addr(mac_addr) && ecm_front_end_is_bridge_port(dev) && rx_packets) {
Shyam Sunderbf40d0e2015-06-23 15:56:37 +05305043 DEBUG_TRACE("Update bridge fdb entry for mac: %pM\n", mac_addr);
5044 br_refresh_fdb_entry(dev, mac_addr);
5045 }
Selin Dag1af781a2014-06-10 10:37:54 -07005046 }
5047
Gareth Williamsadf425f2014-05-26 19:29:02 +01005048 switch (ii_type) {
5049 struct rtnl_link_stats64 stats;
5050
Gareth Williams141d2382014-11-25 11:35:19 -08005051#ifdef ECM_INTERFACE_VLAN_ENABLE
Gareth Williamsadf425f2014-05-26 19:29:02 +01005052 case ECM_DB_IFACE_TYPE_VLAN:
Radha krishna Simha Jigurue9d46fd2014-06-03 22:17:19 +05305053 DEBUG_INFO("VLAN\n");
Gareth Williamsadf425f2014-05-26 19:29:02 +01005054 stats.rx_packets = rx_packets;
5055 stats.rx_bytes = rx_bytes;
5056 stats.tx_packets = tx_packets;
5057 stats.tx_bytes = tx_bytes;
5058 __vlan_dev_update_accel_stats(dev, &stats);
5059 break;
Gareth Williams141d2382014-11-25 11:35:19 -08005060#endif
Gareth Williamsadf425f2014-05-26 19:29:02 +01005061 case ECM_DB_IFACE_TYPE_BRIDGE:
5062 DEBUG_INFO("BRIDGE\n");
5063 stats.rx_packets = rx_packets;
5064 stats.rx_bytes = rx_bytes;
5065 stats.tx_packets = tx_packets;
5066 stats.tx_bytes = tx_bytes;
5067 br_dev_update_stats(dev, &stats);
Gareth Williamsadf425f2014-05-26 19:29:02 +01005068 break;
ratheesh kannotha32fdd12015-09-09 08:02:58 +05305069#ifdef ECM_INTERFACE_PPPOE_ENABLE
Murat Sezgina683edd2015-01-20 10:48:30 -08005070 case ECM_DB_IFACE_TYPE_PPPOE:
5071 DEBUG_INFO("PPPOE\n");
ratheesh kannothb1b753a2015-09-28 11:17:35 +05305072 ppp_update_stats(dev, rx_packets, rx_bytes, tx_packets, tx_bytes, 0, 0, 0, 0);
Murat Sezgina683edd2015-01-20 10:48:30 -08005073 break;
5074#endif
Gareth Williamsadf425f2014-05-26 19:29:02 +01005075 default:
5076 /*
5077 * TODO: Extend it accordingly
5078 */
5079 break;
5080 }
5081
5082 dev_put(dev);
5083 }
5084}
5085
5086/*
5087 * ecm_interface_stats_update()
5088 * Using the interface lists for the given connection, update the interface statistics for each.
5089 *
5090 * 'from' here is wrt the connection 'from' side. Likewise with 'to'.
5091 * TX is wrt what the interface has transmitted. RX is what the interface has received.
5092 */
5093void ecm_interface_stats_update(struct ecm_db_connection_instance *ci,
5094 uint32_t from_tx_packets, uint32_t from_tx_bytes, uint32_t from_rx_packets, uint32_t from_rx_bytes,
5095 uint32_t to_tx_packets, uint32_t to_tx_bytes, uint32_t to_rx_packets, uint32_t to_rx_bytes)
5096{
5097 struct ecm_db_iface_instance *from_ifaces[ECM_DB_IFACE_HEIRARCHY_MAX];
5098 struct ecm_db_iface_instance *to_ifaces[ECM_DB_IFACE_HEIRARCHY_MAX];
5099 int from_ifaces_first;
5100 int to_ifaces_first;
5101 uint8_t mac_addr[ETH_ALEN];
Suman Ghosh812ad302018-05-09 21:07:50 +05305102 bool is_ported = false;
5103 uint8_t protocol = ecm_db_connection_protocol_get(ci);
5104
5105 /*
5106 * Set is_ported flag based on protocol
5107 */
5108 if ((protocol == IPPROTO_UDP) || (protocol == IPPROTO_TCP)) {
5109 is_ported = true;
5110 }
Gareth Williamsadf425f2014-05-26 19:29:02 +01005111
5112 /*
5113 * Iterate the 'from' side interfaces and update statistics and state for the real HLOS interfaces
5114 * from_tx_packets / bytes: the amount transmitted by the 'from' interface
5115 * from_rx_packets / bytes: the amount received by the 'from' interface
5116 */
5117 DEBUG_INFO("%p: Update from interface stats\n", ci);
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005118 from_ifaces_first = ecm_db_connection_interfaces_get_and_ref(ci, from_ifaces, ECM_DB_OBJ_DIR_FROM);
5119 ecm_db_connection_node_address_get(ci, ECM_DB_OBJ_DIR_FROM, mac_addr);
Suman Ghosh812ad302018-05-09 21:07:50 +05305120 ecm_interface_list_stats_update(from_ifaces_first, from_ifaces, mac_addr, false, from_tx_packets, from_tx_bytes, from_rx_packets, from_rx_bytes, is_ported);
Gareth Williamsadf425f2014-05-26 19:29:02 +01005121 ecm_db_connection_interfaces_deref(from_ifaces, from_ifaces_first);
5122
5123 /*
5124 * Iterate the 'to' side interfaces and update statistics and state for the real HLOS interfaces
5125 * to_tx_packets / bytes: the amount transmitted by the 'to' interface
5126 * to_rx_packets / bytes: the amount received by the 'to' interface
5127 */
5128 DEBUG_INFO("%p: Update to interface stats\n", ci);
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005129 to_ifaces_first = ecm_db_connection_interfaces_get_and_ref(ci, to_ifaces, ECM_DB_OBJ_DIR_TO);
5130 ecm_db_connection_node_address_get(ci, ECM_DB_OBJ_DIR_TO, mac_addr);
Suman Ghosh812ad302018-05-09 21:07:50 +05305131 ecm_interface_list_stats_update(to_ifaces_first, to_ifaces, mac_addr, false, to_tx_packets, to_tx_bytes, to_rx_packets, to_rx_bytes, is_ported);
Gareth Williamsadf425f2014-05-26 19:29:02 +01005132 ecm_db_connection_interfaces_deref(to_ifaces, to_ifaces_first);
5133}
5134EXPORT_SYMBOL(ecm_interface_stats_update);
5135
Shyam Sunder6358b862015-05-04 15:06:24 +05305136#ifdef ECM_MULTICAST_ENABLE
5137/*
Shyam Sunder6358b862015-05-04 15:06:24 +05305138 * ecm_interface_multicast_stats_update()
5139 * Using the interface lists for the given connection, update the interface statistics for each.
5140 *
5141 * 'from interface' here is the connection 'from' side. Likewise with 'to interface'.
5142 * TX is wrt what the interface has transmitted. RX is what the interface has received.
5143 */
5144void ecm_interface_multicast_stats_update(struct ecm_db_connection_instance *ci, uint32_t from_tx_packets, uint32_t from_tx_bytes,
5145 uint32_t from_rx_packets, uint32_t from_rx_bytes, uint32_t to_tx_packets, uint32_t to_tx_bytes,
5146 uint32_t to_rx_packets, uint32_t to_rx_bytes)
5147{
5148 struct ecm_db_iface_instance *from_ifaces[ECM_DB_IFACE_HEIRARCHY_MAX];
Shyam Sunderbf40d0e2015-06-23 15:56:37 +05305149 struct ecm_db_iface_instance *to_list_single[ECM_DB_IFACE_HEIRARCHY_MAX];
Shyam Sunder6358b862015-05-04 15:06:24 +05305150 struct ecm_db_iface_instance *to_ifaces;
5151 struct ecm_db_iface_instance *ii_temp;
5152 int from_ifaces_first;
5153 int *to_ifaces_first;
5154 int if_index;
5155 int ret;
5156 uint8_t mac_addr[ETH_ALEN];
Suman Ghosh812ad302018-05-09 21:07:50 +05305157 bool is_ported = false;
5158 uint8_t protocol = ecm_db_connection_protocol_get(ci);
5159
5160 /*
5161 * Set is_ported flag based on protocol
5162 */
5163 if (protocol == IPPROTO_UDP) {
5164 is_ported = true;
5165 }
Shyam Sunder6358b862015-05-04 15:06:24 +05305166
5167 /*
5168 * Iterate the 'from' side interfaces and update statistics and state for the real HLOS interfaces
5169 * from_tx_packets / bytes: the amount transmitted by the 'from' interface
5170 * from_rx_packets / bytes: the amount received by the 'from' interface
5171 */
5172 DEBUG_INFO("%p: Update from interface stats\n", ci);
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005173 from_ifaces_first = ecm_db_connection_interfaces_get_and_ref(ci, from_ifaces, ECM_DB_OBJ_DIR_FROM);
5174 ecm_db_connection_node_address_get(ci, ECM_DB_OBJ_DIR_FROM, mac_addr);
Suman Ghosh812ad302018-05-09 21:07:50 +05305175 ecm_interface_list_stats_update(from_ifaces_first, from_ifaces, mac_addr, false, from_tx_packets, from_tx_bytes, from_rx_packets, from_rx_bytes, is_ported);
Shyam Sunder6358b862015-05-04 15:06:24 +05305176 ecm_db_connection_interfaces_deref(from_ifaces, from_ifaces_first);
5177
5178 /*
5179 * Iterate the 'to' side interfaces and update statistics and state for the real HLOS interfaces
5180 * to_tx_packets / bytes: the amount transmitted by the 'to' interface
5181 * to_rx_packets / bytes: the amount received by the 'to' interface
5182 */
5183 DEBUG_INFO("%p: Update to interface stats\n", ci);
5184
5185 /*
5186 * This function allocates the memory for temporary destination interface heirarchies.
5187 * This memory needs to be free at the end.
5188 */
5189 ret = ecm_db_multicast_connection_to_interfaces_get_and_ref_all(ci, &to_ifaces, &to_ifaces_first);
5190 if (ret == 0) {
5191 DEBUG_WARN("%p: Get and ref to all multicast detination interface heirarchies failed\n", ci);
5192 return;
5193 }
5194
5195 for (if_index = 0; if_index < ECM_DB_MULTICAST_IF_MAX; if_index++) {
5196 if (to_ifaces_first[if_index] < ECM_DB_IFACE_HEIRARCHY_MAX) {
5197 ii_temp = ecm_db_multicast_if_heirarchy_get(to_ifaces, if_index);
Shyam Sunderbf40d0e2015-06-23 15:56:37 +05305198 ecm_db_multicast_copy_if_heirarchy(to_list_single, ii_temp);
Suman Ghosh812ad302018-05-09 21:07:50 +05305199 ecm_interface_list_stats_update(to_ifaces_first[if_index], to_list_single, mac_addr, true, to_tx_packets, to_tx_bytes, to_rx_packets, to_rx_bytes, is_ported);
Shyam Sunder6358b862015-05-04 15:06:24 +05305200 }
5201 }
5202
5203 ecm_db_multicast_connection_to_interfaces_deref_all(to_ifaces, to_ifaces_first);
5204}
5205EXPORT_SYMBOL(ecm_interface_multicast_stats_update);
5206#endif
5207
Gareth Williamsadf425f2014-05-26 19:29:02 +01005208/*
Ben Menchaca84f36632014-02-28 20:57:38 +00005209 * ecm_interface_regenerate_connections()
5210 * Cause regeneration of all connections that are using the specified interface.
5211 */
5212static void ecm_interface_regenerate_connections(struct ecm_db_iface_instance *ii)
5213{
Gareth Williamsb5903892015-03-20 15:13:07 +00005214#ifdef ECM_DB_XREF_ENABLE
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005215 int dir;
5216 struct ecm_db_connection_instance *ci[ECM_DB_OBJ_DIR_MAX];
Gareth Williamsa4390962015-08-26 12:51:31 +01005217 struct ecm_db_connection_instance *ci_mcast __attribute__ ((unused));
Gareth Williamsb5903892015-03-20 15:13:07 +00005218#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00005219
5220 DEBUG_TRACE("Regenerate connections using interface: %p\n", ii);
5221
Gareth Williamsb5903892015-03-20 15:13:07 +00005222#ifndef ECM_DB_XREF_ENABLE
5223 /*
5224 * An interface has changed, re-generate the connections to ensure all state is updated.
5225 */
Tushar Mathurd38cacd2015-07-28 12:19:10 +05305226 ecm_db_regeneration_needed();
Gareth Williamsb5903892015-03-20 15:13:07 +00005227#else
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005228 for (dir = 0; dir < ECM_DB_OBJ_DIR_MAX; dir++) {
5229 ci[dir] = ecm_db_iface_connections_get_and_ref_first(ii, dir);
5230 }
5231
Ben Menchaca84f36632014-02-28 20:57:38 +00005232 /*
Gareth Williamsa4390962015-08-26 12:51:31 +01005233 * If the interface has NO connections then we re-generate all.
5234 */
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005235 if (!ci[ECM_DB_OBJ_DIR_FROM] && !ci[ECM_DB_OBJ_DIR_TO] && !ci[ECM_DB_OBJ_DIR_FROM_NAT] && !ci[ECM_DB_OBJ_DIR_TO_NAT]) {
Gareth Williamsa4390962015-08-26 12:51:31 +01005236 ecm_db_regeneration_needed();
5237 DEBUG_TRACE("%p: Regenerate (ALL) COMPLETE\n", ii);
5238 return;
5239 }
5240
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005241 for (dir = 0; dir < ECM_DB_OBJ_DIR_MAX; dir++) {
5242 /*
5243 * Re-generate all connections associated with this interface
5244 */
5245 DEBUG_TRACE("%p: Regenerate %s direction connections\n", ii, ecm_db_obj_dir_strings[dir]);
5246 while (ci[dir]) {
5247 struct ecm_db_connection_instance *cin;
5248 cin = ecm_db_connection_iface_get_and_ref_next(ci[dir], dir);
Ben Menchaca84f36632014-02-28 20:57:38 +00005249
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005250 DEBUG_TRACE("%p: Regenerate: %p", ii, ci[dir]);
5251 ecm_db_connection_regenerate(ci[dir]);
5252 ecm_db_connection_deref(ci[dir]);
5253 ci[dir] = cin;
5254 }
Gareth Williamsa4390962015-08-26 12:51:31 +01005255 }
5256
5257#ifdef ECM_MULTICAST_ENABLE
5258 /*
5259 * Multicasts would not have recorded in the lists above.
5260 * Our only way to re-gen those is to iterate all multicasts.
5261 * GGG TODO This will be optimised in a future release.
5262 */
5263 ci_mcast = ecm_db_connections_get_and_ref_first();
5264 while (ci_mcast) {
5265 struct ecm_db_connection_instance *cin;
5266
5267 /*
5268 * Multicast and NOT flagged for re-gen?
5269 */
5270 if (ecm_db_multicast_connection_to_interfaces_set_check(ci_mcast)
5271 && ecm_db_connection_regeneration_required_peek(ci_mcast)) {
Xiaoping Faned6d37e2015-09-17 14:13:47 -07005272 ecm_db_connection_regenerate(ci_mcast);
Gareth Williamsa4390962015-08-26 12:51:31 +01005273 }
5274
5275 cin = ecm_db_connection_get_and_ref_next(ci_mcast);
5276 ecm_db_connection_deref(ci_mcast);
5277 ci_mcast = cin;
Ben Menchaca84f36632014-02-28 20:57:38 +00005278 }
Gareth Williamsb5903892015-03-20 15:13:07 +00005279#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00005280
Gareth Williamsa4390962015-08-26 12:51:31 +01005281#endif
Ben Menchaca84f36632014-02-28 20:57:38 +00005282 DEBUG_TRACE("%p: Regenerate COMPLETE\n", ii);
5283}
5284
5285/*
5286 * ecm_interface_dev_regenerate_connections()
5287 * Cause regeneration of all connections that are using the specified interface.
5288 */
Tushar Mathurcccbf282015-01-13 01:22:44 +05305289void ecm_interface_dev_regenerate_connections(struct net_device *dev)
Ben Menchaca84f36632014-02-28 20:57:38 +00005290{
5291 struct ecm_db_iface_instance *ii;
5292
5293 DEBUG_INFO("Regenerate connections for: %p (%s)\n", dev, dev->name);
5294
5295 /*
Murat Sezgin91c5d712015-06-12 15:16:22 -07005296 * If the interface is known to us then we will get it returned by this
5297 * function and process it accordingly.
Ben Menchaca84f36632014-02-28 20:57:38 +00005298 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07005299 ii = ecm_db_iface_find_and_ref_by_interface_identifier(dev->ifindex);
Ben Menchaca84f36632014-02-28 20:57:38 +00005300 if (!ii) {
5301 DEBUG_WARN("%p: No interface instance could be established for this dev\n", dev);
5302 return;
5303 }
5304 ecm_interface_regenerate_connections(ii);
5305 DEBUG_TRACE("%p: Regenerate for %p: COMPLETE\n", dev, ii);
5306 ecm_db_iface_deref(ii);
5307}
5308
5309/*
Suman Ghosh7a261712016-01-12 21:42:55 +05305310 * ecm_interface_defunct_connections()
5311 * Cause defunct of all connections that are using the specified interface.
5312 */
5313static void ecm_interface_defunct_connections(struct ecm_db_iface_instance *ii)
5314{
5315#ifndef ECM_DB_XREF_ENABLE
5316 ecm_db_connection_defunct_all();
5317#else
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005318 int dir;
5319 struct ecm_db_connection_instance *ci[ECM_DB_OBJ_DIR_MAX];
Suman Ghosh7a261712016-01-12 21:42:55 +05305320 struct ecm_db_connection_instance *ci_mcast __attribute__ ((unused));
5321
5322 DEBUG_TRACE("defunct connections using interface: %p\n", ii);
5323
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005324 for (dir = 0; dir < ECM_DB_OBJ_DIR_MAX; dir++) {
5325 ci[dir] = ecm_db_iface_connections_get_and_ref_first(ii, dir);
5326 }
Suman Ghosh7a261712016-01-12 21:42:55 +05305327
5328 /*
Bhaskar Valaboju35d2a8b2018-01-16 18:35:10 +05305329 * Defunct ALL if all the four connection instances are NULL
5330 */
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005331 if (!ci[ECM_DB_OBJ_DIR_FROM] && !ci[ECM_DB_OBJ_DIR_TO] && !ci[ECM_DB_OBJ_DIR_FROM_NAT] && !ci[ECM_DB_OBJ_DIR_TO_NAT]) {
5332 ecm_db_regeneration_needed();
5333 DEBUG_TRACE("%p: Regenerate (ALL) COMPLETE\n", ii);
Bhaskar Valaboju35d2a8b2018-01-16 18:35:10 +05305334 return;
5335 }
5336
5337 /*
Suman Ghosh7a261712016-01-12 21:42:55 +05305338 * Defunct all connections associated with this interface
5339 */
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005340 for (dir = 0; dir < ECM_DB_OBJ_DIR_MAX; dir++) {
5341 DEBUG_TRACE("%p: Defunct %s direction connections\n", ii, ecm_db_obj_dir_strings[dir]);
5342 while (ci[dir]) {
5343 struct ecm_db_connection_instance *cin;
5344 cin = ecm_db_connection_iface_get_and_ref_next(ci[dir], dir);
Suman Ghosh7a261712016-01-12 21:42:55 +05305345
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005346 DEBUG_TRACE("%p: Defunct: %p", ii, ci[dir]);
5347 ecm_db_connection_make_defunct(ci[dir]);
5348 ecm_db_connection_deref(ci[dir]);
5349 ci[dir] = cin;
5350 }
Suman Ghosh7a261712016-01-12 21:42:55 +05305351 }
5352#endif
5353 DEBUG_TRACE("%p: Defunct COMPLETE\n", ii);
5354}
5355
5356/*
5357 * ecm_interface_dev_defunct_connections()
5358 * Cause defunct of all connections that are using the specified interface.
5359 */
5360void ecm_interface_dev_defunct_connections(struct net_device *dev)
5361{
5362 struct ecm_db_iface_instance *ii;
5363
5364 DEBUG_INFO("defunct connections for: %p (%s)\n", dev, dev->name);
5365
5366 /*
5367 * If the interface is known to us then we will get it returned by this
5368 * function and process it accordingly.
5369 */
5370 ii = ecm_db_iface_find_and_ref_by_interface_identifier(dev->ifindex);
5371 if (!ii) {
5372 DEBUG_WARN("%p: No interface instance could be established for this dev\n", dev);
5373 return;
5374 }
5375 ecm_interface_defunct_connections(ii);
5376 DEBUG_TRACE("%p: defunct for %p: COMPLETE\n", dev, ii);
5377 ecm_db_iface_deref(ii);
5378}
5379
5380/*
Ben Menchaca84f36632014-02-28 20:57:38 +00005381 * ecm_interface_mtu_change()
5382 * MTU of interface has changed
5383 */
5384static void ecm_interface_mtu_change(struct net_device *dev)
5385{
5386 int mtu;
5387 struct ecm_db_iface_instance *ii;
5388
5389 mtu = dev->mtu;
5390 DEBUG_INFO("%p (%s): MTU Change to: %d\n", dev, dev->name, mtu);
5391
5392 /*
Murat Sezgin91c5d712015-06-12 15:16:22 -07005393 * Find the interface for the given device.
Ben Menchaca84f36632014-02-28 20:57:38 +00005394 */
Murat Sezgin91c5d712015-06-12 15:16:22 -07005395 ii = ecm_db_iface_find_and_ref_by_interface_identifier(dev->ifindex);
Ben Menchaca84f36632014-02-28 20:57:38 +00005396 if (!ii) {
5397 DEBUG_WARN("%p: No interface instance could be established for this dev\n", dev);
5398 return;
5399 }
5400
5401 /*
5402 * Change the mtu
5403 */
5404 ecm_db_iface_mtu_reset(ii, mtu);
5405 DEBUG_TRACE("%p (%s): MTU Changed to: %d\n", dev, dev->name, mtu);
Tushar Mathurad534d62015-06-10 18:37:52 +05305406 if (netif_is_bond_slave(dev)) {
5407 struct net_device *master = NULL;
5408 master = ecm_interface_get_and_hold_dev_master(dev);
5409 DEBUG_ASSERT(master, "Expected a master\n");
5410 ecm_interface_dev_regenerate_connections(master);
5411 dev_put(master);
5412 } else {
5413 ecm_interface_regenerate_connections(ii);
5414 }
5415
Ben Menchaca84f36632014-02-28 20:57:38 +00005416 DEBUG_TRACE("%p: Regenerate for %p: COMPLETE\n", dev, ii);
5417 ecm_db_iface_deref(ii);
5418}
5419
5420/*
5421 * ecm_interface_netdev_notifier_callback()
5422 * Netdevice notifier callback to inform us of change of state of a netdevice
5423 */
5424static int ecm_interface_netdev_notifier_callback(struct notifier_block *this, unsigned long event, void *ptr)
5425{
Stephen Wang69379c32015-02-04 18:37:13 -08005426#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 10, 0))
Ben Menchaca84f36632014-02-28 20:57:38 +00005427 struct net_device *dev __attribute__ ((unused)) = (struct net_device *)ptr;
Stephen Wang69379c32015-02-04 18:37:13 -08005428#else
5429 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5430#endif
Tushar Mathurad534d62015-06-10 18:37:52 +05305431 struct net_device *master = NULL;
Ben Menchaca84f36632014-02-28 20:57:38 +00005432
5433 DEBUG_INFO("Net device notifier for: %p, name: %s, event: %lx\n", dev, dev->name, event);
5434
5435 switch (event) {
5436 case NETDEV_DOWN:
5437 DEBUG_INFO("Net device: %p, DOWN\n", dev);
Tushar Mathurad534d62015-06-10 18:37:52 +05305438 if (netif_is_bond_slave(dev)) {
5439 master = ecm_interface_get_and_hold_dev_master(dev);
5440 DEBUG_ASSERT(master, "Expected a master\n");
Murat Sezgina52ce612017-11-14 17:08:08 -08005441 ecm_interface_dev_defunct_connections(master);
Tushar Mathurad534d62015-06-10 18:37:52 +05305442 dev_put(master);
5443 } else {
Murat Sezgina52ce612017-11-14 17:08:08 -08005444 ecm_interface_dev_defunct_connections(dev);
Tushar Mathurad534d62015-06-10 18:37:52 +05305445 }
Ben Menchaca84f36632014-02-28 20:57:38 +00005446 break;
5447
5448 case NETDEV_CHANGE:
5449 DEBUG_INFO("Net device: %p, CHANGE\n", dev);
5450 if (!netif_carrier_ok(dev)) {
5451 DEBUG_INFO("Net device: %p, CARRIER BAD\n", dev);
Tushar Mathur933907c2014-06-24 17:06:14 +05305452 if (netif_is_bond_slave(dev)) {
Murat Sezginb3731e82014-11-26 12:20:59 -08005453 master = ecm_interface_get_and_hold_dev_master(dev);
Bhaskar Valaboju5cbdf812018-02-21 09:59:24 +05305454 DEBUG_ASSERT(master, "Expected a master.\n");
5455 ecm_interface_dev_defunct_connections(master);
Murat Sezginb3731e82014-11-26 12:20:59 -08005456 dev_put(master);
Tushar Mathur933907c2014-06-24 17:06:14 +05305457 } else {
Bhaskar Valaboju5cbdf812018-02-21 09:59:24 +05305458 ecm_interface_dev_defunct_connections(dev);
Tushar Mathur933907c2014-06-24 17:06:14 +05305459 }
Ben Menchaca84f36632014-02-28 20:57:38 +00005460 }
5461 break;
5462
5463 case NETDEV_CHANGEMTU:
5464 DEBUG_INFO("Net device: %p, MTU CHANGE\n", dev);
5465 ecm_interface_mtu_change(dev);
5466 break;
5467
5468 default:
5469 DEBUG_TRACE("Net device: %p, UNHANDLED: %lx\n", dev, event);
5470 break;
5471 }
5472
5473 return NOTIFY_DONE;
5474}
5475
5476/*
Murat Sezgin9304d472017-04-14 10:16:52 -07005477 * ecm_interface_node_connections_defunct()
5478 * Defunct the connections on this node.
Murat Sezgina205b042016-07-19 14:18:14 -07005479 */
Murat Sezgin9304d472017-04-14 10:16:52 -07005480void ecm_interface_node_connections_defunct(uint8_t *mac)
Murat Sezgina205b042016-07-19 14:18:14 -07005481{
Murat Sezgina5f3de12016-08-02 17:29:30 -07005482 struct ecm_db_node_instance *ni = NULL;
Murat Sezgina205b042016-07-19 14:18:14 -07005483
5484 if (unlikely(!mac)) {
5485 DEBUG_WARN("mac address is null\n");
5486 return;
5487 }
5488
Murat Sezgina5f3de12016-08-02 17:29:30 -07005489 ni = ecm_db_node_chain_get_and_ref_first(mac);
5490 while (ni) {
5491 struct ecm_db_node_instance *nin;
Murat Sezgina205b042016-07-19 14:18:14 -07005492
Murat Sezgina5f3de12016-08-02 17:29:30 -07005493 if (ecm_db_node_is_mac_addr_equal(ni, mac)) {
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005494 int dir;
5495 for (dir = 0; dir < ECM_DB_OBJ_DIR_MAX; dir++) {
5496 ecm_db_traverse_node_connection_list_and_defunct(ni, dir);
5497 }
Murat Sezgina5f3de12016-08-02 17:29:30 -07005498 }
5499
5500 /*
5501 * Get next node in the chain
5502 */
5503 nin = ecm_db_node_chain_get_and_ref_next(ni);
5504 ecm_db_node_deref(ni);
5505 ni = nin;
Murat Sezgina205b042016-07-19 14:18:14 -07005506 }
Murat Sezgina205b042016-07-19 14:18:14 -07005507}
Murat Sezgin9304d472017-04-14 10:16:52 -07005508EXPORT_SYMBOL(ecm_interface_node_connections_defunct);
Murat Sezgina205b042016-07-19 14:18:14 -07005509
5510/*
Ben Menchaca84f36632014-02-28 20:57:38 +00005511 * struct notifier_block ecm_interface_netdev_notifier
5512 * Registration for net device changes of state.
5513 */
5514static struct notifier_block ecm_interface_netdev_notifier __read_mostly = {
5515 .notifier_call = ecm_interface_netdev_notifier_callback,
5516};
Murat Sezgina205b042016-07-19 14:18:14 -07005517
Murat Sezgin8c345822015-05-27 15:35:38 -07005518#if defined(ECM_DB_XREF_ENABLE) && defined(ECM_BAND_STEERING_ENABLE)
Ben Menchaca84f36632014-02-28 20:57:38 +00005519/*
ratheesh kannoth37e35b02015-03-26 11:25:02 +05305520 * ecm_interfae_node_br_fdb_notify_event()
5521 * This is a call back for "bridge fdb update event/ageing timer expire
5522 * event".
5523 */
5524static int ecm_interface_node_br_fdb_notify_event(struct notifier_block *nb,
5525 unsigned long val,
5526 void *data)
5527{
5528 uint8_t *mac = (uint8_t *)data;
ratheesh kannoth37e35b02015-03-26 11:25:02 +05305529
Murat Sezgin04470032017-07-07 14:53:32 -07005530 if (ECM_FRONT_END_TYPE_NSS == ecm_front_end_type_get()) {
5531 DEBUG_INFO("FDB updated for node %pM\n", mac);
5532 ecm_interface_node_connections_defunct(mac);
5533 }
ratheesh kannoth37e35b02015-03-26 11:25:02 +05305534
5535 return NOTIFY_DONE;
5536}
5537
5538static struct notifier_block ecm_interface_node_br_fdb_update_nb = {
5539 .notifier_call = ecm_interface_node_br_fdb_notify_event,
5540};
Xiaoping Fan8fe3edd2016-06-22 16:03:17 -07005541
5542static int ecm_interface_node_br_fdb_delete_event(struct notifier_block *nb,
5543 unsigned long event,
5544 void *ctx)
5545{
5546 struct br_fdb_event *fe = (struct br_fdb_event *)ctx;
5547
5548 if ((event != BR_FDB_EVENT_DEL) || fe->is_local) {
5549 DEBUG_WARN("local fdb or not deleting event, ignore\n");
5550 return NOTIFY_DONE;
5551 }
5552
5553 return ecm_interface_node_br_fdb_notify_event(nb, event, fe->addr);
5554}
5555
5556static struct notifier_block ecm_interface_node_br_fdb_delete_nb = {
5557 .notifier_call = ecm_interface_node_br_fdb_delete_event,
5558};
Murat Sezgin8c345822015-05-27 15:35:38 -07005559#endif
Shyam Sunder6358b862015-05-04 15:06:24 +05305560
5561#ifdef ECM_MULTICAST_ENABLE
5562/*
5563 * ecm_interface_multicast_find_outdated_iface_instances()
5564 *
5565 * Called in the case of Routing/Bridging Multicast update events.
5566 *
5567 * This function takes a list of ifindex for the connection which was received
5568 * from MFC or bridge snooper, compares it against the existing list of interfaces
5569 * in the DB connection, and extracts the list of those interfaces that have left
5570 * the multicast group.
5571 *
5572 * ci A DB connection instance.
5573 * mc_updates Part of return Information. The function will mark the index of those
5574 * interfaces in the DB connection 'to_mcast_interfaces' array that have
5575 * left the group, in the mc_updates->if_leave_idx array. The caller uses this
5576 * information to delete those outdated interface heirarchies from the
5577 * connection.
5578 * is_bridged True if the function called due to bridge multicast snooper update event.
5579 * dst_dev Holds the netdevice ifindex number of the new list of interfaces as reported
5580 * by the update from MFC or Bridge snooper.
5581 * max_to_dev Size of the array 'dst_dev'
5582 *
5583 * Return true if outdated interfaces found
5584 */
5585static bool ecm_interface_multicast_find_outdated_iface_instances(struct ecm_db_connection_instance *ci, struct ecm_multicast_if_update *mc_updates,
5586 uint32_t flags, bool is_br_snooper, uint32_t *mc_dst_if_index, uint32_t max_to_dev)
5587{
5588 struct ecm_db_iface_instance *mc_ifaces;
5589 struct ecm_db_iface_instance *ii_temp;
5590 struct ecm_db_iface_instance *ii_single;
5591 struct ecm_db_iface_instance **ifaces;
5592 struct ecm_db_iface_instance *to_iface;
5593 int32_t *to_iface_first;
5594 int32_t *mc_ifaces_first;
5595 uint32_t *dst_if_index;
5596 ecm_db_iface_type_t ii_type;
5597 int32_t heirarchy_index;
5598 int32_t if_index;
5599 int32_t if_cnt = 0;
5600 int found = 0;
5601 int ii;
5602 int ret;
5603 int32_t ifaces_identifier;
5604
5605 ret = ecm_db_multicast_connection_to_interfaces_get_and_ref_all(ci, &mc_ifaces, &mc_ifaces_first);
5606 if (ret == 0) {
5607 DEBUG_WARN("%p: multicast interfaces ref fail!\n", ci);
5608 return false;
5609 }
5610
5611 /*
5612 * Loop through the current interface list in the DB
5613 * connection 'to_mcast_interfaces' array
5614 */
5615 for (heirarchy_index = 0; heirarchy_index < ECM_DB_MULTICAST_IF_MAX; heirarchy_index++) {
5616 found = 0;
5617 to_iface_first = ecm_db_multicast_if_first_get_at_index(mc_ifaces_first, heirarchy_index);
5618
5619 /*
5620 * Invalid interface entry, skip
5621 */
5622 if (*to_iface_first == ECM_DB_IFACE_HEIRARCHY_MAX) {
5623 continue;
5624 }
5625
5626 ii_temp = ecm_db_multicast_if_heirarchy_get(mc_ifaces, heirarchy_index);
5627 ii_single = ecm_db_multicast_if_instance_get_at_index(ii_temp, ECM_DB_IFACE_HEIRARCHY_MAX - 1);
5628 ifaces = (struct ecm_db_iface_instance **)ii_single;
5629 to_iface = *ifaces;
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005630 ii_type = ecm_db_iface_type_get(to_iface);
Shyam Sunder6358b862015-05-04 15:06:24 +05305631
5632 /*
5633 * If the update was received from bridge snooper, do not consider entries in the
5634 * interface list that are not part of a bridge.
5635 */
5636 if (is_br_snooper && (ii_type != ECM_DB_IFACE_TYPE_BRIDGE)) {
5637 continue;
5638 }
5639
5640 /*
5641 * If the update was received from MFC, do not consider entries in the
5642 * interface list that are part of a bridge. The bridge entries will be
5643 * taken care by the Bridge Snooper Callback
5644 */
5645 if (ii_type == ECM_DB_IFACE_TYPE_BRIDGE) {
5646 if (!is_br_snooper && !(flags & ECM_DB_MULTICAST_CONNECTION_BRIDGE_DEV_SET_FLAG)) {
5647 continue;
5648 }
5649 }
5650
5651 /*
5652 * Try to find a match in the newly received interface list, for any of
5653 * the interface instance in the heirarchy. If found, it means that this
5654 * interface has not left the group. If not found, it means that this
5655 * interface has left the group.
5656 */
5657 for (ii = ECM_DB_IFACE_HEIRARCHY_MAX - 1; ii >= *to_iface_first; ii--) {
5658 ii_single = ecm_db_multicast_if_instance_get_at_index(ii_temp, ii);
5659 ifaces = (struct ecm_db_iface_instance **)ii_single;
5660 to_iface = *ifaces;
5661
Murat Sezgin3d9a7642018-05-15 16:24:09 -07005662 ii_type = ecm_db_iface_type_get(to_iface);
Shyam Sunder6358b862015-05-04 15:06:24 +05305663 ifaces_identifier = ecm_db_iface_interface_identifier_get(to_iface);
5664 for (if_index = 0; if_index < max_to_dev; if_index++) {
5665 dst_if_index = ecm_db_multicast_if_num_get_at_index(mc_dst_if_index, if_index);
5666 if (*dst_if_index == ifaces_identifier) {
5667 found = 1;
5668 break;
5669 }
5670 }
5671 if (found) {
5672 break;
5673 }
5674 }
5675
5676 /*
5677 * We did not find a match for the interface in the present list. So mark
5678 * if as one that has left the group.
5679 */
5680 if (!found) {
5681 if_cnt++;
5682 mc_updates->if_leave_idx[heirarchy_index] = 1;
5683 }
5684 }
5685
5686 ecm_db_multicast_connection_to_interfaces_deref_all(mc_ifaces, mc_ifaces_first);
5687 mc_updates->if_leave_cnt = if_cnt;
5688 return (if_cnt > 0);
5689}
5690
5691/*
5692 * ecm_interface_multicast_find_new_iface_instances()
5693 *
5694 * Called in the case of Routing/Bridging Multicast update events.
5695 *
5696 * This function takes a list of ifindex for the connection which was received
5697 * from MFC or bridge snooper, compares it against the existing list of interfaces
5698 * in the DB connection, and extracts the list of the new joinees for the multicast
5699 * group.
5700 *
5701 * ci A DB connection instance.
5702 * mc_updates Part of return Information. The function will mark the index of those
5703 * interfaces in the 'dst_dev' array that have joined the group, in the
5704 * mc_updates->if_join_idx array. The caller uses this information to add the new
5705 * interface heirarchies into the connection.
5706 * dst_dev Holds the netdevice ifindex number of the new list of interfaces as reported
5707 * by the update from MFC or Bridge snooper.
5708 * max_to_dev Size of the array 'dst_dev'
5709 *
5710 * Return true if new joinees found
5711 */
5712static bool ecm_interface_multicast_find_new_iface_instances(struct ecm_db_connection_instance *ci,
5713 struct ecm_multicast_if_update *mc_updates, uint32_t *mc_dst_if_index, uint32_t max_to_dev)
5714{
5715 struct ecm_db_iface_instance *mc_ifaces;
5716 struct ecm_db_iface_instance *ii_temp;
5717 struct ecm_db_iface_instance *ii_single;
5718 struct ecm_db_iface_instance **ifaces;
5719 int32_t *mc_ifaces_first;
5720 int32_t *to_list_first;
5721 int32_t heirarchy_index;
5722 int32_t if_index;
5723 int32_t if_cnt = 0;
5724 int found = 0;
5725 int ii;
5726 int ret;
5727 uint32_t *dst_if_index;
5728 int32_t ifaces_identifier;
5729 struct ecm_db_iface_instance *to_list;
5730
5731 ret = ecm_db_multicast_connection_to_interfaces_get_and_ref_all(ci, &mc_ifaces, &mc_ifaces_first);
5732 if (ret == 0) {
5733 DEBUG_WARN("%p: multicast interfaces ref fail!\n", ci);
5734 return false;
5735 }
5736
5737 /*
5738 * Loop through the new interface list 'dst_dev'
5739 */
5740 for (if_index = 0; if_index < max_to_dev; if_index++) {
5741 found = 0;
5742 dst_if_index = ecm_db_multicast_if_num_get_at_index(mc_dst_if_index, if_index);
5743 if (*dst_if_index == 0) {
5744 continue;
5745 }
5746
5747 for (heirarchy_index = 0; heirarchy_index < ECM_DB_MULTICAST_IF_MAX ; heirarchy_index++) {
5748 to_list_first = ecm_db_multicast_if_first_get_at_index(mc_ifaces_first, heirarchy_index);
5749
5750 /*
5751 * Invalid interface entry, skip
5752 */
5753 if (*to_list_first == ECM_DB_IFACE_HEIRARCHY_MAX) {
5754 continue;
5755 }
5756
5757 ii_temp = ecm_db_multicast_if_heirarchy_get(mc_ifaces, heirarchy_index);
5758
5759 /*
5760 * Try to find a match for this ifindex (dst_dev[if_index]), in any of the
5761 * interface instance in the heirarchy. If not found, it means that this
5762 * ifindex has joined the group. If found, it means that this ifindex was
5763 * already part of the list of destination interfaces.
5764 */
5765 for (ii = ECM_DB_IFACE_HEIRARCHY_MAX - 1; ii >= *to_list_first; ii--) {
5766 ii_single = ecm_db_multicast_if_instance_get_at_index(ii_temp, ii);
5767 ifaces = (struct ecm_db_iface_instance **)ii_single;
5768 to_list = *ifaces;
5769 ifaces_identifier = ecm_db_iface_interface_identifier_get(to_list);
5770 if (*dst_if_index == ifaces_identifier) {
5771 found = 1;
5772 break;
5773 }
5774 }
5775
5776 if (found) {
5777 break;
5778 }
5779 }
5780
5781 /*
5782 * We did not find a match for the interface in the present list. So mark
5783 * it as one that has joined the group.
5784 */
5785 if (!found) {
5786
5787 /*
5788 * Store the if index of the new joinee
5789 */
5790 mc_updates->join_dev[if_cnt] = *dst_if_index;
5791
5792 /*
5793 * Identify a new vacant slot in the 'to_mcast_interfaces' to place
5794 * the new interface
5795 */
5796 for (heirarchy_index = 0; heirarchy_index < ECM_DB_MULTICAST_IF_MAX ; heirarchy_index++) {
5797 to_list_first = ecm_db_multicast_if_first_get_at_index(mc_ifaces_first, heirarchy_index);
5798 if (*to_list_first == ECM_DB_IFACE_HEIRARCHY_MAX) {
5799 mc_updates->if_join_idx[heirarchy_index] = 1;
5800 break;
5801 }
5802 }
5803
5804 if_cnt++;
5805 }
5806 }
5807
5808 ecm_db_multicast_connection_to_interfaces_deref_all(mc_ifaces, mc_ifaces_first);
5809 mc_updates->if_join_cnt = if_cnt;
5810
5811 return (if_cnt > 0);
5812}
5813
5814/*
5815 * ecm_interface_multicast_find_updates_to_iface_list()
5816 * Process IGMP/MLD updates either from MFC or bridge snooper. Identity the interfaces
5817 * that have left the group and new interfaces that have joined the group.
5818 *
5819 * The function returns true if there was any update necessary to the current destination
5820 * interface list
5821 */
5822bool ecm_interface_multicast_find_updates_to_iface_list(struct ecm_db_connection_instance *ci, struct ecm_multicast_if_update *mc_updates,
5823 uint32_t flags, bool is_br_snooper, uint32_t *mc_dst_if_index, uint32_t max_to_dev)
5824{
5825 bool join;
5826 bool leave;
5827 /*
5828 * Find destination interfaces that have left the group
5829 */
5830 leave = ecm_interface_multicast_find_outdated_iface_instances(ci, mc_updates, flags, is_br_snooper, mc_dst_if_index, max_to_dev);
5831 /*
5832 * Find new destination interfaces that have joined the group
5833 */
5834 join = ecm_interface_multicast_find_new_iface_instances(ci, mc_updates, mc_dst_if_index, max_to_dev);
5835
5836 return (leave || join);
5837}
5838EXPORT_SYMBOL(ecm_interface_multicast_find_updates_to_iface_list);
5839#endif
5840
Murat Sezgin42a44c22016-05-05 14:06:39 -07005841#ifdef ECM_DB_XREF_ENABLE
5842/*
5843 * ecm_interface_neigh_mac_update_notify_event()
5844 * Neighbour mac address change handler.
5845 */
5846static int ecm_interface_neigh_mac_update_notify_event(struct notifier_block *nb,
5847 unsigned long val,
5848 void *data)
5849{
Murat Sezgin42a44c22016-05-05 14:06:39 -07005850 struct neigh_mac_update *nmu = (struct neigh_mac_update *)data;
5851
5852 /*
5853 * If the old and new mac addresses are equal, do nothing.
5854 * This case shouldn't happen.
5855 */
5856 if (!ecm_mac_addr_equal(nmu->old_mac, nmu->update_mac)) {
5857 DEBUG_TRACE("old and new mac addresses are equal: %pM\n", nmu->old_mac);
5858 return NOTIFY_DONE;
5859 }
5860
5861 /*
5862 * If the old mac is zero, do nothing. When a host joins the arp table first
5863 * time, its old mac comes as zero. We shouldn't handle this case, because
5864 * there is not any connection in ECM db with zero mac.
5865 */
5866 if (is_zero_ether_addr(nmu->old_mac)) {
5867 DEBUG_WARN("old mac is zero\n");
5868 return NOTIFY_DONE;
5869 }
5870
5871 DEBUG_TRACE("old mac: %pM new mac: %pM\n", nmu->old_mac, nmu->update_mac);
5872
Murat Sezgina205b042016-07-19 14:18:14 -07005873 DEBUG_INFO("neigh mac update notify for node %pM\n", nmu->old_mac);
Murat Sezgin9304d472017-04-14 10:16:52 -07005874 ecm_interface_node_connections_defunct((uint8_t *)nmu->old_mac);
Murat Sezgin42a44c22016-05-05 14:06:39 -07005875
5876 return NOTIFY_DONE;
5877}
5878
5879/*
5880 * struct notifier_block ecm_interface_neigh_mac_update_nb
5881 * Registration for neighbour mac address update.
5882 */
5883static struct notifier_block ecm_interface_neigh_mac_update_nb = {
5884 .notifier_call = ecm_interface_neigh_mac_update_notify_event,
5885};
5886#endif
5887
Murat Sezgin1f381852014-11-20 09:51:07 -08005888/*
Murat Sezgina205b042016-07-19 14:18:14 -07005889 * ecm_interface_wifi_event_iwevent
5890 * wireless event handler
5891 */
5892static int ecm_interface_wifi_event_iwevent(int ifindex, unsigned char *buf, size_t len)
5893{
5894 struct iw_event iwe_buf, *iwe = &iwe_buf;
5895 char *pos, *end;
5896
5897 pos = buf;
5898 end = buf + len;
5899 while (pos + IW_EV_LCP_LEN <= end) {
5900
5901 /*
5902 * Copy the base data structure to get iwe->len
5903 */
5904 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
5905
5906 /*
5907 * Check that len is valid and that we have that much in the buffer.
5908 *
5909 */
5910 if (iwe->len < IW_EV_LCP_LEN) {
5911 return -1;
5912 }
5913
5914 if ((iwe->len > sizeof (struct iw_event)) || (iwe->len + pos) > end) {
5915 return -1;
5916 }
5917
5918 /*
5919 * Do the copy again with the full length.
5920 */
5921 memcpy(&iwe_buf, pos, iwe->len);
5922
5923 if (iwe->cmd == IWEVREGISTERED) {
5924 DEBUG_INFO("STA %pM joining\n", (uint8_t *)iwe->u.addr.sa_data);
5925 } else if (iwe->cmd == IWEVEXPIRED) {
5926 DEBUG_INFO("STA %pM leaving\n", (uint8_t *)iwe->u.addr.sa_data);
Murat Sezgin9304d472017-04-14 10:16:52 -07005927 ecm_interface_node_connections_defunct((uint8_t *)iwe->u.addr.sa_data);
Murat Sezgina205b042016-07-19 14:18:14 -07005928 } else {
5929 DEBUG_INFO("iwe->cmd is %d for STA %pM\n", iwe->cmd, (unsigned char *) iwe->u.addr.sa_data);
5930 }
5931
5932 pos += iwe->len;
5933 }
5934
5935 return 0;
5936}
5937
5938/*
5939 * ecm_interface_wifi_event_newlink
5940 * Link event handler
5941 */
5942static int ecm_interface_wifi_event_newlink(struct ifinfomsg *ifi, unsigned char *buf, size_t len)
5943{
5944 struct rtattr *attr;
5945 int attrlen, rta_len;
5946
5947 DEBUG_TRACE("Event from interface %d\n", ifi->ifi_index);
5948
5949 attrlen = len;
5950 attr = (struct rtattr *) buf;
5951 rta_len = RTA_ALIGN(sizeof(struct rtattr));
5952
5953 while (RTA_OK(attr, attrlen)) {
5954 if (attr->rta_type == IFLA_WIRELESS) {
5955 ecm_interface_wifi_event_iwevent(ifi->ifi_index, ((char *) attr) + rta_len, attr->rta_len - rta_len);
5956 }
5957 attr = RTA_NEXT(attr, attrlen);
5958 }
5959
5960 return 0;
5961}
5962
5963/*
5964 * ecm_interface_wifi_event_handler
5965 * Netlink event handler
5966 */
5967static int ecm_interface_wifi_event_handler(unsigned char *buf, int len)
5968{
5969 struct nlmsghdr *nlh;
5970 struct ifinfomsg *ifi;
5971 int left;
5972
5973 nlh = (struct nlmsghdr *) buf;
5974 left = len;
5975
5976 while (NLMSG_OK(nlh, left)) {
5977 switch (nlh->nlmsg_type) {
5978 case RTM_NEWLINK:
5979 case RTM_DELLINK:
5980 if (NLMSG_PAYLOAD(nlh, 0) < sizeof(struct ifinfomsg)) {
5981 DEBUG_INFO("invalid netlink message\n");
5982 break;
5983 }
5984
5985 ifi = NLMSG_DATA(nlh);
5986 DEBUG_INFO("ifi->ifi_family: %d\n", ifi->ifi_family);
5987 if (ifi->ifi_family != AF_BRIDGE) {
5988 ecm_interface_wifi_event_newlink(ifi, (u8 *)ifi + NLMSG_ALIGN(sizeof(struct ifinfomsg)),
5989 NLMSG_PAYLOAD(nlh, sizeof(struct ifinfomsg)));
5990 }
5991 break;
5992 }
5993
5994 nlh = NLMSG_NEXT(nlh, left);
5995 }
5996
5997 return 0;
5998}
5999
6000/*
6001 * ecm_interface_wifi_event_rx
6002 * Receive netlink message from socket
6003 */
6004static int ecm_interface_wifi_event_rx(struct socket *sock, struct sockaddr_nl *addr, unsigned char *buf, int len)
6005{
6006 struct msghdr msg;
6007 struct iovec iov;
6008 mm_segment_t oldfs;
6009 int size;
6010
6011 iov.iov_base = buf;
6012 iov.iov_len = len;
6013
6014 msg.msg_flags = 0;
6015 msg.msg_name = addr;
6016 msg.msg_namelen = sizeof(struct sockaddr_nl);
6017 msg.msg_control = NULL;
6018 msg.msg_controllen = 0;
6019#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 19, 0))
6020 msg.msg_iov = &iov;
6021 msg.msg_iovlen = 1;
6022#else
6023 iov_iter_init(&msg.msg_iter, READ, &iov, 1, 1);
6024#endif
6025 oldfs = get_fs();
6026 set_fs(KERNEL_DS);
6027 size = sock_recvmsg(sock, &msg, len, msg.msg_flags);
6028 set_fs(oldfs);
6029
6030 return size;
6031}
6032
6033/*
6034 * ecm_interface_wifi_event_thread
6035 */
6036static void ecm_interface_wifi_event_thread(void)
6037{
6038 int err;
6039 int size;
6040 struct sockaddr_nl saddr;
6041 unsigned char buf[512];
6042 int len = sizeof(buf);
6043
6044 allow_signal(SIGKILL|SIGSTOP);
6045 err = sock_create(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, &__ewn.sock);
6046 if (err < 0) {
6047 DEBUG_ERROR("failed to create sock\n");
6048 goto exit1;
6049 }
6050
Murat Sezginac267642016-11-02 15:55:11 -07006051 memset(&saddr, 0, sizeof(saddr));
Murat Sezgina205b042016-07-19 14:18:14 -07006052 saddr.nl_family = AF_NETLINK;
6053 saddr.nl_groups = RTNLGRP_LINK;
6054 saddr.nl_pid = current->pid;
6055
6056 err = __ewn.sock->ops->bind(__ewn.sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr));
6057 if (err < 0) {
6058 DEBUG_ERROR("failed to bind sock\n");
6059 goto exit2;
6060 }
6061
6062 DEBUG_INFO("ecm_interface_wifi_event thread started\n");
6063 while (!kthread_should_stop()) {
6064 size = ecm_interface_wifi_event_rx(__ewn.sock, &saddr, buf, len);
6065 DEBUG_TRACE("got a netlink msg with len %d\n", size);
6066
6067 if (signal_pending(current))
6068 break;
6069
6070 if (size < 0) {
6071 DEBUG_WARN("netlink rx error\n");
6072 } else {
6073 ecm_interface_wifi_event_handler(buf, size);
6074 }
6075 }
6076
6077 DEBUG_INFO("ecm_interface_wifi_event thread stopped\n");
6078exit2:
6079 sock_release(__ewn.sock);
6080exit1:
6081 __ewn.sock = NULL;
6082
6083 return;
6084}
6085
6086/*
6087 * ecm_interface_wifi_event_start()
6088 */
6089int ecm_interface_wifi_event_start(void)
6090{
6091 if (__ewn.thread) {
6092 return 0;
6093 }
6094
6095 __ewn.thread = kthread_run((void *)ecm_interface_wifi_event_thread, NULL, "ECM_wifi_event");
6096 if (IS_ERR(__ewn.thread)) {
6097 DEBUG_ERROR("Unable to start kernel thread\n");
6098 return -ENOMEM;
6099 }
6100
6101 return 0;
6102}
6103
6104/*
6105 * ecm_interface_wifi_event_stop()
6106 */
6107int ecm_interface_wifi_event_stop(void)
6108{
6109 int err;
6110
6111 if (__ewn.thread == NULL) {
6112 return 0;
6113 }
6114
6115 DEBUG_INFO("kill ecm_interface_wifi_event thread\n");
6116 force_sig(SIGKILL, __ewn.thread);
6117 err = kthread_stop(__ewn.thread);
6118 __ewn.thread = NULL;
6119
6120 return err;
6121}
6122
6123/*
Murat Sezgineebedb92017-11-17 17:14:32 -08006124 * ecm_interface_src_check_handler()
6125 * Source interface check sysctl node handler.
6126 */
6127static int ecm_interface_src_check_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
6128{
6129 int ret;
6130 int current_value;
6131
6132 /*
6133 * Take the current value
6134 */
6135 current_value = ecm_interface_src_check;
6136
6137 /*
6138 * Write the variable with user input
6139 */
6140 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6141 if (ret || (!write)) {
6142 return ret;
6143 }
6144
6145 if (ECM_FRONT_END_TYPE_NSS != ecm_front_end_type_get()) {
6146 DEBUG_WARN("Source interface check is for NSS only.\n");
6147 return -EINVAL;
6148 }
6149
6150 if ((ecm_interface_src_check != 1) && (ecm_interface_src_check != 0)) {
6151 DEBUG_WARN("Invalid input. Valid values 0/1\n");
6152 ecm_interface_src_check = current_value;
6153 return -EINVAL;
6154 }
6155
6156 return ret;
6157}
6158
6159static struct ctl_table ecm_interface_table[] = {
6160 {
6161 .procname = "src_interface_check",
6162 .data = &ecm_interface_src_check,
6163 .maxlen = sizeof(int),
6164 .mode = 0644,
6165 .proc_handler = &ecm_interface_src_check_handler,
6166 },
6167 { }
6168};
6169
6170static struct ctl_table ecm_interface_root_dir[] = {
6171 {
6172 .procname = "ecm",
6173 .mode = 0555,
6174 .child = ecm_interface_table,
6175 },
6176 { }
6177};
6178
6179static struct ctl_table ecm_interface_root[] = {
6180 {
6181 .procname = "net",
6182 .mode = 0555,
6183 .child = ecm_interface_root_dir,
6184 },
6185 { }
6186};
6187
6188/*
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006189 * ecm_interface_init()
Ben Menchaca84f36632014-02-28 20:57:38 +00006190 */
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006191int ecm_interface_init(void)
Ben Menchaca84f36632014-02-28 20:57:38 +00006192{
6193 int result;
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006194 DEBUG_INFO("ECM Interface init\n");
Ben Menchaca84f36632014-02-28 20:57:38 +00006195
Murat Sezgineebedb92017-11-17 17:14:32 -08006196 /*
6197 * Register sysctl table.
6198 */
6199 ecm_interface_ctl_table_header = register_sysctl_table(ecm_interface_root);
6200
Ben Menchaca84f36632014-02-28 20:57:38 +00006201 result = register_netdevice_notifier(&ecm_interface_netdev_notifier);
6202 if (result != 0) {
6203 DEBUG_ERROR("Failed to register netdevice notifier %d\n", result);
Murat Sezgineebedb92017-11-17 17:14:32 -08006204 unregister_sysctl_table(ecm_interface_ctl_table_header);
Murat Sezgin908ecb32015-05-10 20:54:36 -07006205 return result;
Ben Menchaca84f36632014-02-28 20:57:38 +00006206 }
Murat Sezgin8c345822015-05-27 15:35:38 -07006207#if defined(ECM_DB_XREF_ENABLE) && defined(ECM_BAND_STEERING_ENABLE)
ratheesh kannoth37e35b02015-03-26 11:25:02 +05306208 /*
6209 * register for bridge fdb database modificationevents
6210 */
ratheesh kannotha32fdd12015-09-09 08:02:58 +05306211 br_fdb_update_register_notify(&ecm_interface_node_br_fdb_update_nb);
Xiaoping Fan8fe3edd2016-06-22 16:03:17 -07006212 br_fdb_register_notify(&ecm_interface_node_br_fdb_delete_nb);
Murat Sezgin8c345822015-05-27 15:35:38 -07006213#endif
Murat Sezgin42a44c22016-05-05 14:06:39 -07006214#ifdef ECM_DB_XREF_ENABLE
6215 neigh_mac_update_register_notify(&ecm_interface_neigh_mac_update_nb);
6216#endif
Murat Sezgina205b042016-07-19 14:18:14 -07006217 ecm_interface_wifi_event_start();
6218
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006219 return 0;
Ben Menchaca84f36632014-02-28 20:57:38 +00006220}
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006221EXPORT_SYMBOL(ecm_interface_init);
Ben Menchaca84f36632014-02-28 20:57:38 +00006222
6223/*
6224 * ecm_interface_exit()
6225 */
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006226void ecm_interface_exit(void)
Ben Menchaca84f36632014-02-28 20:57:38 +00006227{
6228 DEBUG_INFO("ECM Interface exit\n");
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006229
6230 spin_lock_bh(&ecm_interface_lock);
6231 ecm_interface_terminate_pending = true;
6232 spin_unlock_bh(&ecm_interface_lock);
6233
6234 unregister_netdevice_notifier(&ecm_interface_netdev_notifier);
Murat Sezgin42a44c22016-05-05 14:06:39 -07006235#ifdef ECM_DB_XREF_ENABLE
6236 neigh_mac_update_unregister_notify(&ecm_interface_neigh_mac_update_nb);
6237#endif
6238
Murat Sezgin8c345822015-05-27 15:35:38 -07006239#if defined(ECM_DB_XREF_ENABLE) && defined(ECM_BAND_STEERING_ENABLE)
ratheesh kannoth37e35b02015-03-26 11:25:02 +05306240 /*
6241 * unregister for bridge fdb update events
6242 */
6243 br_fdb_update_unregister_notify(&ecm_interface_node_br_fdb_update_nb);
Xiaoping Fan8fe3edd2016-06-22 16:03:17 -07006244 br_fdb_unregister_notify(&ecm_interface_node_br_fdb_delete_nb);
Murat Sezgin8c345822015-05-27 15:35:38 -07006245#endif
Murat Sezgina205b042016-07-19 14:18:14 -07006246 ecm_interface_wifi_event_stop();
Murat Sezgineebedb92017-11-17 17:14:32 -08006247
6248 /*
6249 * Unregister sysctl table.
6250 */
6251 if (ecm_interface_ctl_table_header) {
6252 unregister_sysctl_table(ecm_interface_ctl_table_header);
6253 }
Ben Menchaca84f36632014-02-28 20:57:38 +00006254}
Nicolas Costaf46c33b2014-05-15 10:02:00 -05006255EXPORT_SYMBOL(ecm_interface_exit);