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