blob: c57776ad5fb1669ea04cf01cdb4deb50a1c76674 [file] [log] [blame]
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001/*
2 * sfe-cm.c
3 * Shortcut forwarding engine connection manager.
4 *
Matthew McClintocka3221942014-01-16 11:44:26 -06005 * Copyright (c) 2013 Qualcomm Atheros, Inc.
6 *
7 * All Rights Reserved.
8 * Qualcomm Atheros Confidential and Proprietary.
Dave Hudsondcd08fb2013-11-22 09:25:16 -06009 */
Matthew McClintocka3221942014-01-16 11:44:26 -060010
Dave Hudsondcd08fb2013-11-22 09:25:16 -060011#include <linux/module.h>
12#include <linux/sysfs.h>
13#include <linux/skbuff.h>
14#include <net/route.h>
15#include <linux/inetdevice.h>
16#include <linux/netfilter_bridge.h>
17#include <net/netfilter/nf_conntrack_acct.h>
18#include <net/netfilter/nf_conntrack_helper.h>
19#include <net/netfilter/nf_conntrack_zones.h>
20#include <net/netfilter/nf_conntrack_core.h>
Matthew McClintockbf6b5bc2014-02-24 12:27:14 -060021#include <linux/if_bridge.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060022
23#include "sfe.h"
24#include "sfe_ipv4.h"
25
26/*
27 * Per-module structure.
28 */
29struct sfe_cm {
30 spinlock_t lock; /* Lock for SMP correctness */
31
32 /*
33 * Control state.
34 */
35 struct kobject *sys_sfe_cm; /* sysfs linkage */
36
37 /*
38 * Callback notifiers.
39 */
40 struct notifier_block dev_notifier;
41 /* Device notifier */
42 struct notifier_block inet_notifier;
43 /* IP notifier */
44};
45
46struct sfe_cm __sc;
47
48/*
49 * Expose the hook for the receive processing.
50 */
51extern int (*athrs_fast_nat_recv)(struct sk_buff *skb);
52
53/*
54 * Expose what should be a static flag in the TCP connection tracker.
55 */
56extern int nf_ct_tcp_no_window_check;
57
58/*
59 * sfe_cm_recv()
60 * Handle packet receives.
61 *
62 * Returns 1 if the packet is forwarded or 0 if it isn't.
63 */
64int sfe_cm_recv(struct sk_buff *skb)
65{
66 struct net_device *dev;
67#if (SFE_HOOK_ABOVE_BRIDGE)
68 struct in_device *in_dev;
69#endif
70
71 /*
72 * We know that for the vast majority of packets we need the transport
73 * layer header so we may as well start to fetch it now!
74 */
75 prefetch(skb->data + 32);
76 barrier();
77
78 dev = skb->dev;
79
80#if (SFE_HOOK_ABOVE_BRIDGE)
81 /*
82 * Does our input device support IP processing?
83 */
84 in_dev = (struct in_device *)dev->ip_ptr;
85 if (unlikely(!in_dev)) {
86 DEBUG_TRACE("no IP processing for device: %s\n", dev->name);
87 return 0;
88 }
89
90 /*
91 * Does it have an IP address? If it doesn't then we can't do anything
92 * interesting here!
93 */
94 if (unlikely(!in_dev->ifa_list)) {
95 DEBUG_TRACE("no IP address for device: %s\n", dev->name);
96 return 0;
97 }
98#endif
99
100 /*
101 * We're only interested in IP packets.
102 */
103 if (likely(htons(ETH_P_IP) == skb->protocol)) {
104 return sfe_ipv4_recv(dev, skb);
105 }
106
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600107 DEBUG_TRACE("not IP packet\n");
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600108 return 0;
109}
110
111/*
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600112 * sfe_cm_find_dev_and_mac_addr()
113 * Find the device and MAC address for a given IPv4 address.
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600114 *
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600115 * Returns true if we find the device and MAC address, otherwise false.
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600116 *
117 * We look up the rtable entry for the address and, from its neighbour
118 * structure, obtain the hardware address. This means this function also
119 * works if the neighbours are routers too.
120 */
Matthew McClintock0d37fe72014-08-06 16:42:08 -0500121static bool sfe_cm_find_dev_and_mac_addr(uint32_t addr, struct net_device **dev, uint8_t *mac_addr)
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600122{
123 struct neighbour *neigh;
124 struct rtable *rt;
125 struct dst_entry *dst;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600126 struct net_device *mac_dev;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600127
128 /*
129 * Look up the rtable entry for the IP address then get the hardware
130 * address from its neighbour structure. This means this work when the
131 * neighbours are routers too.
132 */
Matthew McClintock0d37fe72014-08-06 16:42:08 -0500133 rt = ip_route_output(&init_net, addr, 0, 0, 0);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600134 if (unlikely(IS_ERR(rt))) {
135 return false;
136 }
137
138 dst = (struct dst_entry *)rt;
139
140 rcu_read_lock();
141 neigh = dst_get_neighbour_noref(dst);
142 if (unlikely(!neigh)) {
143 rcu_read_unlock();
144 dst_release(dst);
145 return false;
146 }
147
148 if (unlikely(!(neigh->nud_state & NUD_VALID))) {
149 rcu_read_unlock();
150 dst_release(dst);
151 return false;
152 }
153
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600154 mac_dev = neigh->dev;
155 if (!mac_dev) {
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600156 rcu_read_unlock();
157 dst_release(dst);
158 return false;
159 }
160
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600161 memcpy(mac_addr, neigh->ha, (size_t)mac_dev->addr_len);
162
163 dev_hold(mac_dev);
164 *dev = mac_dev;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600165 rcu_read_unlock();
166
167 dst_release(dst);
168
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600169 return true;
170}
171
172/*
173 * sfe_cm_ipv4_post_routing_hook()
174 * Called for packets about to leave the box - either locally generated or forwarded from another interface
175 */
176static unsigned int sfe_cm_ipv4_post_routing_hook(unsigned int hooknum,
177 struct sk_buff *skb,
178 const struct net_device *in_unused,
179 const struct net_device *out,
180 int (*okfn)(struct sk_buff *))
181{
182 struct sfe_ipv4_create sic;
183 struct net_device *in;
184 struct nf_conn *ct;
185 enum ip_conntrack_info ctinfo;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600186 struct net_device *dev;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600187 struct net_device *src_dev;
188 struct net_device *dest_dev;
189 struct net_device *src_br_dev = NULL;
190 struct net_device *dest_br_dev = NULL;
191 struct nf_conntrack_tuple orig_tuple;
192 struct nf_conntrack_tuple reply_tuple;
193
194 /*
195 * Don't process broadcast or multicast packets.
196 */
197 if (unlikely(skb->pkt_type == PACKET_BROADCAST)) {
198 DEBUG_TRACE("broadcast, ignoring\n");
199 return NF_ACCEPT;
200 }
201 if (unlikely(skb->pkt_type == PACKET_MULTICAST)) {
202 DEBUG_TRACE("multicast, ignoring\n");
203 return NF_ACCEPT;
204 }
205
206 /*
207 * Don't process packets that are not being forwarded.
208 */
209 in = dev_get_by_index(&init_net, skb->skb_iif);
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600210 if (!in) {
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600211 DEBUG_TRACE("packet not forwarding\n");
212 return NF_ACCEPT;
213 }
214
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600215 dev_put(in);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600216
217 /*
218 * Don't process packets that aren't being tracked by conntrack.
219 */
220 ct = nf_ct_get(skb, &ctinfo);
221 if (unlikely(!ct)) {
222 DEBUG_TRACE("no conntrack connection, ignoring\n");
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600223 return NF_ACCEPT;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600224 }
225
226 /*
227 * Don't process untracked connections.
228 */
229 if (unlikely(ct == &nf_conntrack_untracked)) {
230 DEBUG_TRACE("untracked connection\n");
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600231 return NF_ACCEPT;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600232 }
233
234 /*
235 * Don't process connections that require support from a 'helper' (typically a NAT ALG).
236 */
237 if (unlikely(nfct_help(ct))) {
238 DEBUG_TRACE("connection has helper\n");
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600239 return NF_ACCEPT;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600240 }
241
242 /*
243 * Look up the details of our connection in conntrack.
244 *
245 * Note that the data we get from conntrack is for the "ORIGINAL" direction
246 * but our packet may actually be in the "REPLY" direction.
247 */
248 orig_tuple = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
249 reply_tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
250 sic.protocol = (int32_t)orig_tuple.dst.protonum;
251
252 /*
253 * Get addressing information, non-NAT first
254 */
255 sic.src_ip = (__be32)orig_tuple.src.u3.ip;
256 sic.dest_ip = (__be32)orig_tuple.dst.u3.ip;
257
Matthew McClintocka11c7cd2014-08-06 16:41:30 -0500258 if (ipv4_is_multicast(sic.src_ip) || ipv4_is_multicast(sic.dest_ip)) {
259 DEBUG_TRACE("multicast address\n");
260 return NF_ACCEPT;
261 }
262
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600263 /*
264 * NAT'ed addresses - note these are as seen from the 'reply' direction
265 * When NAT does not apply to this connection these will be identical to the above.
266 */
267 sic.src_ip_xlate = (__be32)reply_tuple.dst.u3.ip;
268 sic.dest_ip_xlate = (__be32)reply_tuple.src.u3.ip;
269
270 sic.flags = 0;
271
272 switch (sic.protocol) {
273 case IPPROTO_TCP:
274 sic.src_port = orig_tuple.src.u.tcp.port;
275 sic.dest_port = orig_tuple.dst.u.tcp.port;
276 sic.src_port_xlate = reply_tuple.dst.u.tcp.port;
277 sic.dest_port_xlate = reply_tuple.src.u.tcp.port;
278 sic.src_td_window_scale = ct->proto.tcp.seen[0].td_scale;
279 sic.src_td_max_window = ct->proto.tcp.seen[0].td_maxwin;
280 sic.src_td_end = ct->proto.tcp.seen[0].td_end;
281 sic.src_td_max_end = ct->proto.tcp.seen[0].td_maxend;
282 sic.dest_td_window_scale = ct->proto.tcp.seen[1].td_scale;
283 sic.dest_td_max_window = ct->proto.tcp.seen[1].td_maxwin;
284 sic.dest_td_end = ct->proto.tcp.seen[1].td_end;
285 sic.dest_td_max_end = ct->proto.tcp.seen[1].td_maxend;
286 if (nf_ct_tcp_no_window_check
287 || (ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_BE_LIBERAL)
288 || (ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_BE_LIBERAL)) {
289 sic.flags |= SFE_IPV4_CREATE_FLAG_NO_SEQ_CHECK;
290 }
291
292 /*
293 * Don't try to manage a non-established connection.
294 */
295 if (!test_bit(IPS_ASSURED_BIT, &ct->status)) {
296 DEBUG_TRACE("non-established connection\n");
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600297 return NF_ACCEPT;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600298 }
299
300 /*
301 * If the connection is shutting down do not manage it.
302 * state can not be SYN_SENT, SYN_RECV because connection is assured
303 * Not managed states: FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT, CLOSE.
304 */
305 spin_lock_bh(&ct->lock);
306 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) {
307 spin_unlock_bh(&ct->lock);
308 DEBUG_TRACE("connection in termination state: %#x, s: %pI4:%u, d: %pI4:%u\n",
309 ct->proto.tcp.state, &sic.src_ip, ntohs(sic.src_port),
310 &sic.dest_ip, ntohs(sic.dest_port));
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600311 return NF_ACCEPT;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600312 }
313 spin_unlock_bh(&ct->lock);
314 break;
315
316 case IPPROTO_UDP:
317 sic.src_port = orig_tuple.src.u.udp.port;
318 sic.dest_port = orig_tuple.dst.u.udp.port;
319 sic.src_port_xlate = reply_tuple.dst.u.udp.port;
320 sic.dest_port_xlate = reply_tuple.src.u.udp.port;
321 break;
322
323 default:
324 DEBUG_TRACE("unhandled protocol %d\n", sic.protocol);
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600325 return NF_ACCEPT;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600326 }
327
328 /*
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600329 * Get the net device and MAC addresses that correspond to the various source and
330 * destination host addresses.
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600331 */
Matthew McClintock0d37fe72014-08-06 16:42:08 -0500332 if (!sfe_cm_find_dev_and_mac_addr(sic.src_ip, &src_dev, sic.src_mac)) {
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600333 DEBUG_TRACE("failed to find MAC address for src IP: %pI4\n", &sic.src_ip);
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600334 return NF_ACCEPT;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600335 }
336
Matthew McClintock0d37fe72014-08-06 16:42:08 -0500337 if (!sfe_cm_find_dev_and_mac_addr(sic.src_ip_xlate, &dev, sic.src_mac_xlate)) {
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600338 DEBUG_TRACE("failed to find MAC address for xlate src IP: %pI4\n", &sic.src_ip_xlate);
339 goto done1;
340 }
341
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600342 dev_put(dev);
343
Matthew McClintock0d37fe72014-08-06 16:42:08 -0500344 if (!sfe_cm_find_dev_and_mac_addr(sic.dest_ip, &dev, sic.dest_mac)) {
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600345 DEBUG_TRACE("failed to find MAC address for dest IP: %pI4\n", &sic.dest_ip);
346 goto done1;
347 }
348
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600349 dev_put(dev);
350
Matthew McClintock0d37fe72014-08-06 16:42:08 -0500351 if (!sfe_cm_find_dev_and_mac_addr(sic.dest_ip_xlate, &dest_dev, sic.dest_mac_xlate)) {
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600352 DEBUG_TRACE("failed to find MAC address for xlate dest IP: %pI4\n", &sic.dest_ip_xlate);
353 goto done1;
354 }
355
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600356#if (!SFE_HOOK_ABOVE_BRIDGE)
357 /*
358 * Now our devices may actually be a bridge interface. If that's
359 * the case then we need to hunt down the underlying interface.
360 */
361 if (src_dev->priv_flags & IFF_EBRIDGE) {
362 src_br_dev = br_port_dev_get(src_dev, sic.src_mac);
363 if (!src_br_dev) {
364 DEBUG_TRACE("no port found on bridge\n");
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600365 goto done2;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600366 }
367
368 src_dev = src_br_dev;
369 }
370
371 if (dest_dev->priv_flags & IFF_EBRIDGE) {
372 dest_br_dev = br_port_dev_get(dest_dev, sic.dest_mac_xlate);
373 if (!dest_br_dev) {
374 DEBUG_TRACE("no port found on bridge\n");
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600375 goto done3;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600376 }
377
378 dest_dev = dest_br_dev;
379 }
380#else
381 /*
382 * Our devices may actually be part of a bridge interface. If that's
383 * the case then find the bridge interface instead.
384 */
385 if (src_dev->priv_flags & IFF_BRIDGE_PORT) {
386 src_br_dev = src_dev->master;
387 if (!src_br_dev) {
388 DEBUG_TRACE("no bridge found for: %s\n", src_dev->name);
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600389 goto done2;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600390 }
391
392 dev_hold(src_br_dev);
393 src_dev = src_br_dev;
394 }
395
396 if (dest_dev->priv_flags & IFF_BRIDGE_PORT) {
397 dest_br_dev = dest_dev->master;
398 if (!dest_br_dev) {
399 DEBUG_TRACE("no bridge found for: %s\n", dest_dev->name);
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600400 goto done3;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600401 }
402
403 dev_hold(dest_br_dev);
404 dest_dev = dest_br_dev;
405 }
406#endif
407
408 sic.src_dev = src_dev;
409 sic.dest_dev = dest_dev;
410
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600411 sic.src_mtu = src_dev->mtu;
412 sic.dest_mtu = dest_dev->mtu;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600413
414 sfe_ipv4_create_rule(&sic);
415
416 /*
417 * If we had bridge ports then release them too.
418 */
419 if (dest_br_dev) {
420 dev_put(dest_br_dev);
421 }
422
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600423done3:
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600424 if (src_br_dev) {
425 dev_put(src_br_dev);
426 }
427
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600428done2:
429 dev_put(dest_dev);
430
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600431done1:
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600432 dev_put(src_dev);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600433
434 return NF_ACCEPT;
435}
436
437#ifdef CONFIG_NF_CONNTRACK_EVENTS
438/*
439 * sfe_cm_conntrack_event()
440 * Callback event invoked when a conntrack connection's state changes.
441 */
Matthew McClintock0680e9f2013-11-26 15:43:10 -0600442#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
443static int sfe_cm_conntrack_event(struct notifier_block *this,
Matthew McClintock0f5c0592014-02-12 11:17:11 -0600444 unsigned long events, void *ptr)
Matthew McClintock0680e9f2013-11-26 15:43:10 -0600445#else
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600446static int sfe_cm_conntrack_event(unsigned int events, struct nf_ct_event *item)
Matthew McClintock0680e9f2013-11-26 15:43:10 -0600447#endif
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600448{
Matthew McClintock0f5c0592014-02-12 11:17:11 -0600449#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
450 struct nf_ct_event *item = ptr;
451#endif
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600452 struct sfe_ipv4_destroy sid;
453 struct nf_conn *ct = item->ct;
454 struct nf_conntrack_tuple orig_tuple;
455
456 /*
457 * If we don't have a conntrack entry then we're done.
458 */
459 if (unlikely(!ct)) {
460 DEBUG_WARN("no ct in conntrack event callback\n");
461 return NOTIFY_DONE;
462 }
463
464 /*
465 * If this is an untracked connection then we can't have any state either.
466 */
467 if (unlikely(ct == &nf_conntrack_untracked)) {
468 DEBUG_TRACE("ignoring untracked conn\n");
469 return NOTIFY_DONE;
470 }
471
472 /*
473 * Ignore anything other than IPv4 connections.
474 */
475 if (unlikely(nf_ct_l3num(ct) != AF_INET)) {
476 DEBUG_TRACE("ignoring non-IPv4 conn\n");
477 return NOTIFY_DONE;
478 }
479
480 /*
481 * We're only interested in destroy events.
482 */
483 if (unlikely(!(events & (1 << IPCT_DESTROY)))) {
484 DEBUG_TRACE("ignoring non-destroy event\n");
485 return NOTIFY_DONE;
486 }
487
488 orig_tuple = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
489 sid.protocol = (int32_t)orig_tuple.dst.protonum;
490
491 /*
492 * Extract information from the conntrack connection. We're only interested
493 * in nominal connection information (i.e. we're ignoring any NAT information).
494 */
495 sid.src_ip = (__be32)orig_tuple.src.u3.ip;
496 sid.dest_ip = (__be32)orig_tuple.dst.u3.ip;
497
498 switch (sid.protocol) {
499 case IPPROTO_TCP:
500 sid.src_port = orig_tuple.src.u.tcp.port;
501 sid.dest_port = orig_tuple.dst.u.tcp.port;
502 break;
503
504 case IPPROTO_UDP:
505 sid.src_port = orig_tuple.src.u.udp.port;
506 sid.dest_port = orig_tuple.dst.u.udp.port;
507 break;
508
509 default:
510 DEBUG_TRACE("unhandled protocol: %d\n", sid.protocol);
511 return NOTIFY_DONE;
512 }
513
514
515 sfe_ipv4_destroy_rule(&sid);
516 return NOTIFY_DONE;
517}
518
519/*
520 * Netfilter conntrack event system to monitor connection tracking changes
521 */
Matthew McClintock0680e9f2013-11-26 15:43:10 -0600522#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
523static struct notifier_block sfe_cm_conntrack_notifier = {
524 .notifier_call = sfe_cm_conntrack_event,
525};
526#else
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600527static struct nf_ct_event_notifier sfe_cm_conntrack_notifier = {
528 .fcn = sfe_cm_conntrack_event,
529};
530#endif
Matthew McClintock0680e9f2013-11-26 15:43:10 -0600531#endif
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600532
533/*
534 * Structure to establish a hook into the post routing netfilter point - this
535 * will pick up local outbound and packets going from one interface to another.
536 *
537 * Note: see include/linux/netfilter_ipv4.h for info related to priority levels.
538 * We want to examine packets after NAT translation and any ALG processing.
539 */
540static struct nf_hook_ops sfe_cm_ipv4_ops_post_routing[] __read_mostly = {
541 {
542 .hook = sfe_cm_ipv4_post_routing_hook,
543 .owner = THIS_MODULE,
544 .pf = PF_INET,
545 .hooknum = NF_INET_POST_ROUTING,
546 .priority = NF_IP_PRI_NAT_SRC + 1,
547 },
548};
549
550/*
551 * sfe_cm_sync_rule()
552 * Synchronize a connection's state.
553 */
554static void sfe_cm_sync_rule(struct sfe_ipv4_sync *sis)
555{
556 struct nf_conntrack_tuple_hash *h;
557 struct nf_conntrack_tuple tuple;
558 struct nf_conn *ct;
559 struct nf_conn_counter *acct;
560
561 /*
562 * Create a tuple so as to be able to look up a connection
563 */
564 memset(&tuple, 0, sizeof(tuple));
565 tuple.src.u3.ip = sis->src_ip;
566 tuple.src.u.all = (__be16)sis->src_port;
567 tuple.src.l3num = AF_INET;
568
569 tuple.dst.u3.ip = sis->dest_ip;
570 tuple.dst.dir = IP_CT_DIR_ORIGINAL;
571 tuple.dst.protonum = (uint8_t)sis->protocol;
572 tuple.dst.u.all = (__be16)sis->dest_port;
573
574 DEBUG_TRACE("update connection - p: %d, s: %pI4:%u, d: %pI4:%u\n",
575 (int)tuple.dst.protonum,
576 &tuple.src.u3.ip, (unsigned int)ntohs(tuple.src.u.all),
577 &tuple.dst.u3.ip, (unsigned int)ntohs(tuple.dst.u.all));
578
579 /*
580 * Look up conntrack connection
581 */
582 h = nf_conntrack_find_get(&init_net, NF_CT_DEFAULT_ZONE, &tuple);
583 if (unlikely(!h)) {
584 DEBUG_TRACE("no connection found\n");
585 return;
586 }
587
588 ct = nf_ct_tuplehash_to_ctrack(h);
589 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
590
591 /*
592 * Only update if this is not a fixed timeout
593 */
594 if (!test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
595 ct->timeout.expires += sis->delta_jiffies;
596 }
597
598 acct = nf_conn_acct_find(ct);
599 if (acct) {
600 spin_lock_bh(&ct->lock);
Matthew McClintock704b7a62013-12-19 16:13:01 -0600601 atomic64_set(&acct[IP_CT_DIR_ORIGINAL].packets, sis->src_packet_count);
602 atomic64_set(&acct[IP_CT_DIR_ORIGINAL].bytes, sis->src_byte_count);
603 atomic64_set(&acct[IP_CT_DIR_REPLY].packets, sis->dest_packet_count);
604 atomic64_set(&acct[IP_CT_DIR_REPLY].bytes, sis->dest_byte_count);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600605 spin_unlock_bh(&ct->lock);
606 }
607
608 switch (sis->protocol) {
609 case IPPROTO_TCP:
610 spin_lock_bh(&ct->lock);
611 if (ct->proto.tcp.seen[0].td_maxwin < sis->src_td_max_window) {
612 ct->proto.tcp.seen[0].td_maxwin = sis->src_td_max_window;
613 }
614 if ((int32_t)(ct->proto.tcp.seen[0].td_end - sis->src_td_end) < 0) {
615 ct->proto.tcp.seen[0].td_end = sis->src_td_end;
616 }
617 if ((int32_t)(ct->proto.tcp.seen[0].td_maxend - sis->src_td_max_end) < 0) {
618 ct->proto.tcp.seen[0].td_maxend = sis->src_td_max_end;
619 }
620 if (ct->proto.tcp.seen[1].td_maxwin < sis->dest_td_max_window) {
621 ct->proto.tcp.seen[1].td_maxwin = sis->dest_td_max_window;
622 }
623 if ((int32_t)(ct->proto.tcp.seen[1].td_end - sis->dest_td_end) < 0) {
624 ct->proto.tcp.seen[1].td_end = sis->dest_td_end;
625 }
626 if ((int32_t)(ct->proto.tcp.seen[1].td_maxend - sis->dest_td_max_end) < 0) {
627 ct->proto.tcp.seen[1].td_maxend = sis->dest_td_max_end;
628 }
629 spin_unlock_bh(&ct->lock);
630 break;
631 }
632
633 /*
634 * Release connection
635 */
636 nf_ct_put(ct);
637}
638
639/*
640 * sfe_cm_device_event()
641 */
642static int sfe_cm_device_event(struct notifier_block *this, unsigned long event, void *ptr)
643{
644 struct net_device *dev = (struct net_device *)ptr;
645
646 switch (event) {
647 case NETDEV_DOWN:
648 if (dev) {
649 sfe_ipv4_destroy_all_rules_for_dev(dev);
650 }
651 break;
652 }
653
654 return NOTIFY_DONE;
655}
656
657/*
658 * sfe_cm_inet_event()
659 */
660static int sfe_cm_inet_event(struct notifier_block *this, unsigned long event, void *ptr)
661{
662 struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
663 return sfe_cm_device_event(this, event, dev);
664}
665
666/*
667 * sfe_cm_init()
668 */
669static int __init sfe_cm_init(void)
670{
671 struct sfe_cm *sc = &__sc;
672 int result = -1;
673
674 DEBUG_INFO("SFE CM init\n");
675
676 /*
677 * Create sys/sfe_cm
678 */
679 sc->sys_sfe_cm = kobject_create_and_add("sfe_cm", NULL);
680 if (!sc->sys_sfe_cm) {
681 DEBUG_ERROR("failed to register sfe_cm\n");
682 goto exit1;
683 }
684
685 sc->dev_notifier.notifier_call = sfe_cm_device_event;
686 sc->dev_notifier.priority = 1;
687 register_netdevice_notifier(&sc->dev_notifier);
688
689 sc->inet_notifier.notifier_call = sfe_cm_inet_event;
690 sc->inet_notifier.priority = 1;
691 register_inetaddr_notifier(&sc->inet_notifier);
692
693 /*
694 * Register our netfilter hooks.
695 */
696 result = nf_register_hooks(sfe_cm_ipv4_ops_post_routing, ARRAY_SIZE(sfe_cm_ipv4_ops_post_routing));
697 if (result < 0) {
698 DEBUG_ERROR("can't register nf post routing hook: %d\n", result);
699 goto exit6;
700 }
701
702#ifdef CONFIG_NF_CONNTRACK_EVENTS
703 /*
704 * Register a notifier hook to get fast notifications of expired connections.
705 */
706 result = nf_conntrack_register_notifier(&init_net, &sfe_cm_conntrack_notifier);
707 if (result < 0) {
708 DEBUG_ERROR("can't register nf notifier hook: %d\n", result);
709 goto exit7;
710 }
711#endif
712
713 spin_lock_init(&sc->lock);
714
715 /*
716 * Hook the receive path in the network stack.
717 */
718 BUG_ON(athrs_fast_nat_recv != NULL);
719 RCU_INIT_POINTER(athrs_fast_nat_recv, sfe_cm_recv);
720
721 /*
722 * Hook the shortcut sync callback.
723 */
724 sfe_ipv4_register_sync_rule_callback(sfe_cm_sync_rule);
725 return 0;
726
727#ifdef CONFIG_NF_CONNTRACK_EVENTS
728exit7:
729#endif
730 nf_unregister_hooks(sfe_cm_ipv4_ops_post_routing, ARRAY_SIZE(sfe_cm_ipv4_ops_post_routing));
731
732exit6:
733 unregister_inetaddr_notifier(&sc->inet_notifier);
734 unregister_netdevice_notifier(&sc->dev_notifier);
735 kobject_put(sc->sys_sfe_cm);
736
737exit1:
738 return result;
739}
740
741/*
742 * sfe_cm_exit()
743 */
744static void __exit sfe_cm_exit(void)
745{
746 struct sfe_cm *sc = &__sc;
747
748 DEBUG_INFO("SFE CM exit\n");
749
750 /*
751 * Unregister our sync callback.
752 */
753 sfe_ipv4_register_sync_rule_callback(NULL);
754
755 /*
756 * Unregister our receive callback.
757 */
758 RCU_INIT_POINTER(athrs_fast_nat_recv, NULL);
759
760 /*
761 * Wait for all callbacks to complete.
762 */
763 rcu_barrier();
764
765 /*
766 * Destroy all connections.
767 */
768 sfe_ipv4_destroy_all_rules_for_dev(NULL);
769
770// XXX - this is where we need to unregister with any lower level offload services.
771
772#ifdef CONFIG_NF_CONNTRACK_EVENTS
773 nf_conntrack_unregister_notifier(&init_net, &sfe_cm_conntrack_notifier);
774
775#endif
776 nf_unregister_hooks(sfe_cm_ipv4_ops_post_routing, ARRAY_SIZE(sfe_cm_ipv4_ops_post_routing));
777
778 unregister_inetaddr_notifier(&sc->inet_notifier);
779 unregister_netdevice_notifier(&sc->dev_notifier);
780
781 kobject_put(sc->sys_sfe_cm);
782
783}
784
785module_init(sfe_cm_init)
786module_exit(sfe_cm_exit)
787
788MODULE_AUTHOR("Qualcomm Atheros Inc.");
789MODULE_DESCRIPTION("Shortcut Forwarding Engine - Connection Manager");
Matthew McClintocka3221942014-01-16 11:44:26 -0600790MODULE_LICENSE("Dual BSD/GPL");
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600791