1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * net/sched/act_ct.c Connection Tracking action
5 * Authors: Paul Blakey <paulb@mellanox.com>
6 * Yossi Kuperman <yossiku@mellanox.com>
7 * Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/pkt_cls.h>
17 #include <linux/ipv6.h>
18 #include <linux/rhashtable.h>
19 #include <net/netlink.h>
20 #include <net/pkt_sched.h>
21 #include <net/pkt_cls.h>
22 #include <net/act_api.h>
24 #include <net/ipv6_frag.h>
25 #include <uapi/linux/tc_act/tc_ct.h>
26 #include <net/tc_act/tc_ct.h>
28 #include <net/netfilter/nf_flow_table.h>
29 #include <net/netfilter/nf_conntrack.h>
30 #include <net/netfilter/nf_conntrack_core.h>
31 #include <net/netfilter/nf_conntrack_zones.h>
32 #include <net/netfilter/nf_conntrack_helper.h>
33 #include <net/netfilter/nf_conntrack_acct.h>
34 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
35 #include <net/netfilter/nf_conntrack_act_ct.h>
36 #include <uapi/linux/netfilter/nf_nat.h>
38 static struct workqueue_struct *act_ct_wq;
39 static struct rhashtable zones_ht;
40 static DEFINE_MUTEX(zones_mutex);
42 struct tcf_ct_flow_table {
43 struct rhash_head node; /* In zones tables */
45 struct rcu_work rwork;
46 struct nf_flowtable nf_ft;
53 static const struct rhashtable_params zones_params = {
54 .head_offset = offsetof(struct tcf_ct_flow_table, node),
55 .key_offset = offsetof(struct tcf_ct_flow_table, zone),
56 .key_len = sizeof_field(struct tcf_ct_flow_table, zone),
57 .automatic_shrinking = true,
60 static struct flow_action_entry *
61 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
63 int i = flow_action->num_entries++;
65 return &flow_action->entries[i];
68 static void tcf_ct_add_mangle_action(struct flow_action *action,
69 enum flow_action_mangle_base htype,
74 struct flow_action_entry *entry;
76 entry = tcf_ct_flow_table_flow_action_get_next(action);
77 entry->id = FLOW_ACTION_MANGLE;
78 entry->mangle.htype = htype;
79 entry->mangle.mask = ~mask;
80 entry->mangle.offset = offset;
81 entry->mangle.val = val;
84 /* The following nat helper functions check if the inverted reverse tuple
85 * (target) is different then the current dir tuple - meaning nat for ports
86 * and/or ip is needed, and add the relevant mangle actions.
89 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
90 struct nf_conntrack_tuple target,
91 struct flow_action *action)
93 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
94 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
95 offsetof(struct iphdr, saddr),
97 be32_to_cpu(target.src.u3.ip));
98 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
99 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
100 offsetof(struct iphdr, daddr),
102 be32_to_cpu(target.dst.u3.ip));
106 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
107 union nf_inet_addr *addr,
112 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++)
113 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
114 i * sizeof(u32) + offset,
115 0xFFFFFFFF, be32_to_cpu(addr->ip6[i]));
119 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
120 struct nf_conntrack_tuple target,
121 struct flow_action *action)
123 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
124 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3,
125 offsetof(struct ipv6hdr,
127 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
128 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3,
129 offsetof(struct ipv6hdr,
134 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
135 struct nf_conntrack_tuple target,
136 struct flow_action *action)
138 __be16 target_src = target.src.u.tcp.port;
139 __be16 target_dst = target.dst.u.tcp.port;
141 if (target_src != tuple->src.u.tcp.port)
142 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
143 offsetof(struct tcphdr, source),
144 0xFFFF, be16_to_cpu(target_src));
145 if (target_dst != tuple->dst.u.tcp.port)
146 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
147 offsetof(struct tcphdr, dest),
148 0xFFFF, be16_to_cpu(target_dst));
152 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
153 struct nf_conntrack_tuple target,
154 struct flow_action *action)
156 __be16 target_src = target.src.u.udp.port;
157 __be16 target_dst = target.dst.u.udp.port;
159 if (target_src != tuple->src.u.udp.port)
160 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
161 offsetof(struct udphdr, source),
162 0xFFFF, be16_to_cpu(target_src));
163 if (target_dst != tuple->dst.u.udp.port)
164 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
165 offsetof(struct udphdr, dest),
166 0xFFFF, be16_to_cpu(target_dst));
169 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
170 enum ip_conntrack_dir dir,
171 struct flow_action *action)
173 struct nf_conn_labels *ct_labels;
174 struct flow_action_entry *entry;
175 enum ip_conntrack_info ctinfo;
178 entry = tcf_ct_flow_table_flow_action_get_next(action);
179 entry->id = FLOW_ACTION_CT_METADATA;
180 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
181 entry->ct_metadata.mark = ct->mark;
183 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
184 IP_CT_ESTABLISHED_REPLY;
185 /* aligns with the CT reference on the SKB nf_ct_set */
186 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo;
187 entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL;
189 act_ct_labels = entry->ct_metadata.labels;
190 ct_labels = nf_ct_labels_find(ct);
192 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE);
194 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE);
197 static int tcf_ct_flow_table_add_action_nat(struct net *net,
199 enum ip_conntrack_dir dir,
200 struct flow_action *action)
202 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
203 struct nf_conntrack_tuple target;
205 if (!(ct->status & IPS_NAT_MASK))
208 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
210 switch (tuple->src.l3num) {
212 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target,
216 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target,
223 switch (nf_ct_protonum(ct)) {
225 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action);
228 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action);
237 static int tcf_ct_flow_table_fill_actions(struct net *net,
238 const struct flow_offload *flow,
239 enum flow_offload_tuple_dir tdir,
240 struct nf_flow_rule *flow_rule)
242 struct flow_action *action = &flow_rule->rule->action;
243 int num_entries = action->num_entries;
244 struct nf_conn *ct = flow->ct;
245 enum ip_conntrack_dir dir;
249 case FLOW_OFFLOAD_DIR_ORIGINAL:
250 dir = IP_CT_DIR_ORIGINAL;
252 case FLOW_OFFLOAD_DIR_REPLY:
253 dir = IP_CT_DIR_REPLY;
259 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
263 tcf_ct_flow_table_add_action_meta(ct, dir, action);
267 /* Clear filled actions */
268 for (i = num_entries; i < action->num_entries; i++)
269 memset(&action->entries[i], 0, sizeof(action->entries[i]));
270 action->num_entries = num_entries;
275 static struct nf_flowtable_type flowtable_ct = {
276 .action = tcf_ct_flow_table_fill_actions,
277 .owner = THIS_MODULE,
280 static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params)
282 struct tcf_ct_flow_table *ct_ft;
285 mutex_lock(&zones_mutex);
286 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params);
287 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref))
290 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL);
293 refcount_set(&ct_ft->ref, 1);
295 ct_ft->zone = params->zone;
296 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
300 ct_ft->nf_ft.type = &flowtable_ct;
301 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD |
302 NF_FLOWTABLE_COUNTER;
303 err = nf_flow_table_init(&ct_ft->nf_ft);
306 write_pnet(&ct_ft->nf_ft.net, net);
308 __module_get(THIS_MODULE);
310 params->ct_ft = ct_ft;
311 params->nf_ft = &ct_ft->nf_ft;
312 mutex_unlock(&zones_mutex);
317 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
321 mutex_unlock(&zones_mutex);
325 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
327 struct flow_block_cb *block_cb, *tmp_cb;
328 struct tcf_ct_flow_table *ct_ft;
329 struct flow_block *block;
331 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
333 nf_flow_table_free(&ct_ft->nf_ft);
335 /* Remove any remaining callbacks before cleanup */
336 block = &ct_ft->nf_ft.flow_block;
337 down_write(&ct_ft->nf_ft.flow_block_lock);
338 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) {
339 list_del(&block_cb->list);
340 flow_block_cb_free(block_cb);
342 up_write(&ct_ft->nf_ft.flow_block_lock);
345 module_put(THIS_MODULE);
348 static void tcf_ct_flow_table_put(struct tcf_ct_params *params)
350 struct tcf_ct_flow_table *ct_ft = params->ct_ft;
352 if (refcount_dec_and_test(¶ms->ct_ft->ref)) {
353 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
354 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
355 queue_rcu_work(act_ct_wq, &ct_ft->rwork);
359 static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry,
360 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir)
362 entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC;
363 entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir];
366 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
370 struct nf_conn_act_ct_ext *act_ct_ext;
371 struct flow_offload *entry;
374 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
377 entry = flow_offload_alloc(ct);
384 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
385 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
388 act_ct_ext = nf_conn_act_ct_ext_find(ct);
390 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL);
391 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY);
394 err = flow_offload_add(&ct_ft->nf_ft, entry);
401 flow_offload_free(entry);
403 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
406 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
408 enum ip_conntrack_info ctinfo)
412 if ((ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) ||
413 !test_bit(IPS_ASSURED_BIT, &ct->status))
416 switch (nf_ct_protonum(ct)) {
419 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
424 #ifdef CONFIG_NF_CT_PROTO_GRE
426 struct nf_conntrack_tuple *tuple;
428 if (ct->status & IPS_NAT_MASK)
430 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
431 /* No support for GRE v1 */
432 if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
441 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
442 ct->status & IPS_SEQ_ADJUST)
445 tcf_ct_flow_table_add(ct_ft, ct, tcp);
449 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
450 struct flow_offload_tuple *tuple,
451 struct tcphdr **tcph)
453 struct flow_ports *ports;
459 if (!pskb_network_may_pull(skb, sizeof(*iph)))
463 thoff = iph->ihl * 4;
465 if (ip_is_fragment(iph) ||
466 unlikely(thoff != sizeof(struct iphdr)))
469 ipproto = iph->protocol;
472 hdrsize = sizeof(struct tcphdr);
475 hdrsize = sizeof(*ports);
477 #ifdef CONFIG_NF_CT_PROTO_GRE
479 hdrsize = sizeof(struct gre_base_hdr);
489 if (!pskb_network_may_pull(skb, thoff + hdrsize))
494 *tcph = (void *)(skb_network_header(skb) + thoff);
497 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
498 tuple->src_port = ports->source;
499 tuple->dst_port = ports->dest;
502 struct gre_base_hdr *greh;
504 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
505 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
513 tuple->src_v4.s_addr = iph->saddr;
514 tuple->dst_v4.s_addr = iph->daddr;
515 tuple->l3proto = AF_INET;
516 tuple->l4proto = ipproto;
522 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
523 struct flow_offload_tuple *tuple,
524 struct tcphdr **tcph)
526 struct flow_ports *ports;
527 struct ipv6hdr *ip6h;
532 if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
535 ip6h = ipv6_hdr(skb);
536 thoff = sizeof(*ip6h);
538 nexthdr = ip6h->nexthdr;
541 hdrsize = sizeof(struct tcphdr);
544 hdrsize = sizeof(*ports);
546 #ifdef CONFIG_NF_CT_PROTO_GRE
548 hdrsize = sizeof(struct gre_base_hdr);
555 if (ip6h->hop_limit <= 1)
558 if (!pskb_network_may_pull(skb, thoff + hdrsize))
563 *tcph = (void *)(skb_network_header(skb) + thoff);
566 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
567 tuple->src_port = ports->source;
568 tuple->dst_port = ports->dest;
571 struct gre_base_hdr *greh;
573 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
574 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
580 ip6h = ipv6_hdr(skb);
582 tuple->src_v6 = ip6h->saddr;
583 tuple->dst_v6 = ip6h->daddr;
584 tuple->l3proto = AF_INET6;
585 tuple->l4proto = nexthdr;
590 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
594 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
595 struct flow_offload_tuple_rhash *tuplehash;
596 struct flow_offload_tuple tuple = {};
597 enum ip_conntrack_info ctinfo;
598 struct tcphdr *tcph = NULL;
599 struct flow_offload *flow;
605 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
609 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
616 tuplehash = flow_offload_lookup(nf_ft, &tuple);
620 dir = tuplehash->tuple.dir;
621 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
624 if (tcph && (unlikely(tcph->fin || tcph->rst))) {
625 flow_offload_teardown(flow);
629 ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
630 IP_CT_ESTABLISHED_REPLY;
632 flow_offload_refresh(nf_ft, flow);
633 nf_conntrack_get(&ct->ct_general);
634 nf_ct_set(skb, ct, ctinfo);
635 if (nf_ft->flags & NF_FLOWTABLE_COUNTER)
636 nf_ct_acct_update(ct, dir, skb->len);
641 static int tcf_ct_flow_tables_init(void)
643 return rhashtable_init(&zones_ht, &zones_params);
646 static void tcf_ct_flow_tables_uninit(void)
648 rhashtable_destroy(&zones_ht);
651 static struct tc_action_ops act_ct_ops;
652 static unsigned int ct_net_id;
654 struct tc_ct_action_net {
655 struct tc_action_net tn; /* Must be first */
659 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
660 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
661 u16 zone_id, bool force)
663 enum ip_conntrack_info ctinfo;
666 ct = nf_ct_get(skb, &ctinfo);
669 if (!net_eq(net, read_pnet(&ct->ct_net)))
671 if (nf_ct_zone(ct)->id != zone_id)
674 /* Force conntrack entry direction. */
675 if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
676 if (nf_ct_is_confirmed(ct))
686 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
691 /* Trim the skb to the length specified by the IP/IPv6 header,
692 * removing any trailing lower-layer padding. This prepares the skb
693 * for higher-layer processing that assumes skb->len excludes padding
694 * (such as nf_ip_checksum). The caller needs to pull the skb to the
695 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
697 static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family)
704 len = ntohs(ip_hdr(skb)->tot_len);
707 len = sizeof(struct ipv6hdr)
708 + ntohs(ipv6_hdr(skb)->payload_len);
714 err = pskb_trim_rcsum(skb, len);
719 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
721 u8 family = NFPROTO_UNSPEC;
723 switch (skb_protocol(skb, true)) {
724 case htons(ETH_P_IP):
725 family = NFPROTO_IPV4;
727 case htons(ETH_P_IPV6):
728 family = NFPROTO_IPV6;
737 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
741 len = skb_network_offset(skb) + sizeof(struct iphdr);
742 if (unlikely(skb->len < len))
744 if (unlikely(!pskb_may_pull(skb, len)))
747 *frag = ip_is_fragment(ip_hdr(skb));
751 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
753 unsigned int flags = 0, len, payload_ofs = 0;
754 unsigned short frag_off;
757 len = skb_network_offset(skb) + sizeof(struct ipv6hdr);
758 if (unlikely(skb->len < len))
760 if (unlikely(!pskb_may_pull(skb, len)))
763 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
764 if (unlikely(nexthdr < 0))
767 *frag = flags & IP6_FH_F_FRAG;
771 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
772 u8 family, u16 zone, bool *defrag)
774 enum ip_conntrack_info ctinfo;
780 /* Previously seen (loopback)? Ignore. */
781 ct = nf_ct_get(skb, &ctinfo);
782 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
785 if (family == NFPROTO_IPV4)
786 err = tcf_ct_ipv4_is_fragment(skb, &frag);
788 err = tcf_ct_ipv6_is_fragment(skb, &frag);
793 mru = tc_skb_cb(skb)->mru;
795 if (family == NFPROTO_IPV4) {
796 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
798 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
800 err = ip_defrag(net, skb, user);
802 if (err && err != -EINPROGRESS)
807 mru = IPCB(skb)->frag_max_size;
809 } else { /* NFPROTO_IPV6 */
810 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
811 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
813 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
814 err = nf_ct_frag6_gather(net, skb, user);
815 if (err && err != -EINPROGRESS)
820 mru = IP6CB(skb)->frag_max_size;
828 if (err != -EINPROGRESS)
829 tc_skb_cb(skb)->mru = mru;
839 static void tcf_ct_params_free(struct rcu_head *head)
841 struct tcf_ct_params *params = container_of(head,
842 struct tcf_ct_params, rcu);
844 tcf_ct_flow_table_put(params);
847 nf_ct_put(params->tmpl);
851 #if IS_ENABLED(CONFIG_NF_NAT)
852 /* Modelled after nf_nat_ipv[46]_fn().
853 * range is only used for new, uninitialized NAT state.
854 * Returns either NF_ACCEPT or NF_DROP.
856 static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
857 enum ip_conntrack_info ctinfo,
858 const struct nf_nat_range2 *range,
859 enum nf_nat_manip_type maniptype)
861 __be16 proto = skb_protocol(skb, true);
862 int hooknum, err = NF_ACCEPT;
864 /* See HOOK2MANIP(). */
865 if (maniptype == NF_NAT_MANIP_SRC)
866 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
868 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
872 case IP_CT_RELATED_REPLY:
873 if (proto == htons(ETH_P_IP) &&
874 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
875 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
879 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) {
881 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
882 int hdrlen = ipv6_skip_exthdr(skb,
883 sizeof(struct ipv6hdr),
884 &nexthdr, &frag_off);
886 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
887 if (!nf_nat_icmpv6_reply_translation(skb, ct,
895 /* Non-ICMP, fall thru to initialize if needed. */
898 /* Seen it before? This can happen for loopback, retrans,
901 if (!nf_nat_initialized(ct, maniptype)) {
902 /* Initialize according to the NAT action. */
903 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
904 /* Action is set up to establish a new
907 ? nf_nat_setup_info(ct, range, maniptype)
908 : nf_nat_alloc_null_binding(ct, hooknum);
909 if (err != NF_ACCEPT)
914 case IP_CT_ESTABLISHED:
915 case IP_CT_ESTABLISHED_REPLY:
923 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
924 if (err == NF_ACCEPT) {
925 if (maniptype == NF_NAT_MANIP_SRC)
926 tc_skb_cb(skb)->post_ct_snat = 1;
927 if (maniptype == NF_NAT_MANIP_DST)
928 tc_skb_cb(skb)->post_ct_dnat = 1;
933 #endif /* CONFIG_NF_NAT */
935 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
937 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
943 new_mark = mark | (ct->mark & ~(mask));
944 if (ct->mark != new_mark) {
946 if (nf_ct_is_confirmed(ct))
947 nf_conntrack_event_cache(IPCT_MARK, ct);
952 static void tcf_ct_act_set_labels(struct nf_conn *ct,
956 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
957 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
959 if (!memchr_inv(labels_m, 0, labels_sz))
962 nf_connlabels_replace(ct, labels, labels_m, 4);
966 static int tcf_ct_act_nat(struct sk_buff *skb,
968 enum ip_conntrack_info ctinfo,
970 struct nf_nat_range2 *range,
973 #if IS_ENABLED(CONFIG_NF_NAT)
975 enum nf_nat_manip_type maniptype;
977 if (!(ct_action & TCA_CT_ACT_NAT))
980 /* Add NAT extension if not confirmed yet. */
981 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
982 return NF_DROP; /* Can't NAT. */
984 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) &&
985 (ctinfo != IP_CT_RELATED || commit)) {
986 /* NAT an established or related connection like before. */
987 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
988 /* This is the REPLY direction for a connection
989 * for which NAT was applied in the forward
990 * direction. Do the reverse NAT.
992 maniptype = ct->status & IPS_SRC_NAT
993 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
995 maniptype = ct->status & IPS_SRC_NAT
996 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
997 } else if (ct_action & TCA_CT_ACT_NAT_SRC) {
998 maniptype = NF_NAT_MANIP_SRC;
999 } else if (ct_action & TCA_CT_ACT_NAT_DST) {
1000 maniptype = NF_NAT_MANIP_DST;
1005 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
1006 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
1007 if (ct->status & IPS_SRC_NAT) {
1008 if (maniptype == NF_NAT_MANIP_SRC)
1009 maniptype = NF_NAT_MANIP_DST;
1011 maniptype = NF_NAT_MANIP_SRC;
1013 err = ct_nat_execute(skb, ct, ctinfo, range,
1015 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
1016 err = ct_nat_execute(skb, ct, ctinfo, NULL,
1026 static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
1027 struct tcf_result *res)
1029 struct net *net = dev_net(skb->dev);
1030 bool cached, commit, clear, force;
1031 enum ip_conntrack_info ctinfo;
1032 struct tcf_ct *c = to_ct(a);
1033 struct nf_conn *tmpl = NULL;
1034 struct nf_hook_state state;
1035 int nh_ofs, err, retval;
1036 struct tcf_ct_params *p;
1037 bool skip_add = false;
1038 bool defrag = false;
1042 p = rcu_dereference_bh(c->params);
1044 retval = READ_ONCE(c->tcf_action);
1045 commit = p->ct_action & TCA_CT_ACT_COMMIT;
1046 clear = p->ct_action & TCA_CT_ACT_CLEAR;
1047 force = p->ct_action & TCA_CT_ACT_FORCE;
1050 tcf_lastuse_update(&c->tcf_tm);
1051 tcf_action_update_bstats(&c->common, skb);
1054 tc_skb_cb(skb)->post_ct = false;
1055 ct = nf_ct_get(skb, &ctinfo);
1058 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1064 family = tcf_ct_skb_nf_family(skb);
1065 if (family == NFPROTO_UNSPEC)
1068 /* The conntrack module expects to be working at L3.
1069 * We also try to pull the IPv4/6 header to linear area
1071 nh_ofs = skb_network_offset(skb);
1072 skb_pull_rcsum(skb, nh_ofs);
1073 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag);
1074 if (err == -EINPROGRESS) {
1075 retval = TC_ACT_STOLEN;
1081 err = tcf_ct_skb_network_trim(skb, family);
1085 /* If we are recirculating packets to match on ct fields and
1086 * committing with a separate ct action, then we don't need to
1087 * actually run the packet through conntrack twice unless it's for a
1090 cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force);
1092 if (tcf_ct_flow_table_lookup(p, skb, family)) {
1097 /* Associate skb with specified zone. */
1099 nf_conntrack_put(skb_nfct(skb));
1100 nf_conntrack_get(&tmpl->ct_general);
1101 nf_ct_set(skb, tmpl, IP_CT_NEW);
1104 state.hook = NF_INET_PRE_ROUTING;
1107 err = nf_conntrack_in(skb, &state);
1108 if (err != NF_ACCEPT)
1113 ct = nf_ct_get(skb, &ctinfo);
1116 nf_ct_deliver_cached_events(ct);
1117 nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
1119 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1120 if (err != NF_ACCEPT)
1124 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1125 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1127 if (!nf_ct_is_confirmed(ct))
1128 nf_conn_act_ct_ext_add(ct);
1130 /* This will take care of sending queued events
1131 * even if the connection is already confirmed.
1133 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1138 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
1141 skb_push_rcsum(skb, nh_ofs);
1143 tc_skb_cb(skb)->post_ct = true;
1144 tc_skb_cb(skb)->zone = p->zone;
1147 qdisc_skb_cb(skb)->pkt_len = skb->len;
1151 tcf_action_inc_drop_qstats(&c->common);
1155 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
1156 [TCA_CT_ACTION] = { .type = NLA_U16 },
1157 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
1158 [TCA_CT_ZONE] = { .type = NLA_U16 },
1159 [TCA_CT_MARK] = { .type = NLA_U32 },
1160 [TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1161 [TCA_CT_LABELS] = { .type = NLA_BINARY,
1162 .len = 128 / BITS_PER_BYTE },
1163 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1164 .len = 128 / BITS_PER_BYTE },
1165 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1166 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
1167 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1168 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1169 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1170 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1173 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1176 struct netlink_ext_ack *extack)
1178 struct nf_nat_range2 *range;
1180 if (!(p->ct_action & TCA_CT_ACT_NAT))
1183 if (!IS_ENABLED(CONFIG_NF_NAT)) {
1184 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1188 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1191 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1192 (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1193 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1198 if (tb[TCA_CT_NAT_IPV4_MIN]) {
1199 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1201 p->ipv4_range = true;
1202 range->flags |= NF_NAT_RANGE_MAP_IPS;
1203 range->min_addr.ip =
1204 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1206 range->max_addr.ip = max_attr ?
1207 nla_get_in_addr(max_attr) :
1209 } else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1210 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1212 p->ipv4_range = false;
1213 range->flags |= NF_NAT_RANGE_MAP_IPS;
1214 range->min_addr.in6 =
1215 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1217 range->max_addr.in6 = max_attr ?
1218 nla_get_in6_addr(max_attr) :
1219 range->min_addr.in6;
1222 if (tb[TCA_CT_NAT_PORT_MIN]) {
1223 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1224 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1226 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1227 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1228 range->min_proto.all;
1234 static void tcf_ct_set_key_val(struct nlattr **tb,
1235 void *val, int val_type,
1236 void *mask, int mask_type,
1241 nla_memcpy(val, tb[val_type], len);
1246 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1247 memset(mask, 0xff, len);
1249 nla_memcpy(mask, tb[mask_type], len);
1252 static int tcf_ct_fill_params(struct net *net,
1253 struct tcf_ct_params *p,
1256 struct netlink_ext_ack *extack)
1258 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1259 struct nf_conntrack_zone zone;
1260 struct nf_conn *tmpl;
1263 p->zone = NF_CT_DEFAULT_ZONE_ID;
1265 tcf_ct_set_key_val(tb,
1266 &p->ct_action, TCA_CT_ACTION,
1267 NULL, TCA_CT_UNSPEC,
1268 sizeof(p->ct_action));
1270 if (p->ct_action & TCA_CT_ACT_CLEAR)
1273 err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1277 if (tb[TCA_CT_MARK]) {
1278 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1279 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1282 tcf_ct_set_key_val(tb,
1283 &p->mark, TCA_CT_MARK,
1284 &p->mark_mask, TCA_CT_MARK_MASK,
1288 if (tb[TCA_CT_LABELS]) {
1289 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1290 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1295 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1298 tcf_ct_set_key_val(tb,
1299 p->labels, TCA_CT_LABELS,
1300 p->labels_mask, TCA_CT_LABELS_MASK,
1304 if (tb[TCA_CT_ZONE]) {
1305 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1306 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1310 tcf_ct_set_key_val(tb,
1311 &p->zone, TCA_CT_ZONE,
1312 NULL, TCA_CT_UNSPEC,
1316 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1317 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1319 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1322 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1328 static int tcf_ct_init(struct net *net, struct nlattr *nla,
1329 struct nlattr *est, struct tc_action **a,
1330 struct tcf_proto *tp, u32 flags,
1331 struct netlink_ext_ack *extack)
1333 struct tc_action_net *tn = net_generic(net, ct_net_id);
1334 bool bind = flags & TCA_ACT_FLAGS_BIND;
1335 struct tcf_ct_params *params = NULL;
1336 struct nlattr *tb[TCA_CT_MAX + 1];
1337 struct tcf_chain *goto_ch = NULL;
1344 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1348 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1352 if (!tb[TCA_CT_PARMS]) {
1353 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1356 parm = nla_data(tb[TCA_CT_PARMS]);
1357 index = parm->index;
1358 err = tcf_idr_check_alloc(tn, &index, a, bind);
1363 err = tcf_idr_create_from_flags(tn, index, est, a,
1364 &act_ct_ops, bind, flags);
1366 tcf_idr_cleanup(tn, index);
1369 res = ACT_P_CREATED;
1374 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
1375 tcf_idr_release(*a, bind);
1379 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1385 params = kzalloc(sizeof(*params), GFP_KERNEL);
1386 if (unlikely(!params)) {
1391 err = tcf_ct_fill_params(net, params, parm, tb, extack);
1395 err = tcf_ct_flow_table_get(net, params);
1399 spin_lock_bh(&c->tcf_lock);
1400 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1401 params = rcu_replace_pointer(c->params, params,
1402 lockdep_is_held(&c->tcf_lock));
1403 spin_unlock_bh(&c->tcf_lock);
1406 tcf_chain_put_by_act(goto_ch);
1408 call_rcu(¶ms->rcu, tcf_ct_params_free);
1414 tcf_chain_put_by_act(goto_ch);
1416 tcf_idr_release(*a, bind);
1420 static void tcf_ct_cleanup(struct tc_action *a)
1422 struct tcf_ct_params *params;
1423 struct tcf_ct *c = to_ct(a);
1425 params = rcu_dereference_protected(c->params, 1);
1427 call_rcu(¶ms->rcu, tcf_ct_params_free);
1430 static int tcf_ct_dump_key_val(struct sk_buff *skb,
1431 void *val, int val_type,
1432 void *mask, int mask_type,
1437 if (mask && !memchr_inv(mask, 0, len))
1440 err = nla_put(skb, val_type, len, val);
1444 if (mask_type != TCA_CT_UNSPEC) {
1445 err = nla_put(skb, mask_type, len, mask);
1453 static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p)
1455 struct nf_nat_range2 *range = &p->range;
1457 if (!(p->ct_action & TCA_CT_ACT_NAT))
1460 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1463 if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1464 if (p->ipv4_range) {
1465 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1466 range->min_addr.ip))
1468 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1469 range->max_addr.ip))
1472 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1473 &range->min_addr.in6))
1475 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1476 &range->max_addr.in6))
1481 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1482 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1483 range->min_proto.all))
1485 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1486 range->max_proto.all))
1493 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1496 unsigned char *b = skb_tail_pointer(skb);
1497 struct tcf_ct *c = to_ct(a);
1498 struct tcf_ct_params *p;
1500 struct tc_ct opt = {
1501 .index = c->tcf_index,
1502 .refcnt = refcount_read(&c->tcf_refcnt) - ref,
1503 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1507 spin_lock_bh(&c->tcf_lock);
1508 p = rcu_dereference_protected(c->params,
1509 lockdep_is_held(&c->tcf_lock));
1510 opt.action = c->tcf_action;
1512 if (tcf_ct_dump_key_val(skb,
1513 &p->ct_action, TCA_CT_ACTION,
1514 NULL, TCA_CT_UNSPEC,
1515 sizeof(p->ct_action)))
1516 goto nla_put_failure;
1518 if (p->ct_action & TCA_CT_ACT_CLEAR)
1521 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1522 tcf_ct_dump_key_val(skb,
1523 &p->mark, TCA_CT_MARK,
1524 &p->mark_mask, TCA_CT_MARK_MASK,
1526 goto nla_put_failure;
1528 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1529 tcf_ct_dump_key_val(skb,
1530 p->labels, TCA_CT_LABELS,
1531 p->labels_mask, TCA_CT_LABELS_MASK,
1533 goto nla_put_failure;
1535 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1536 tcf_ct_dump_key_val(skb,
1537 &p->zone, TCA_CT_ZONE,
1538 NULL, TCA_CT_UNSPEC,
1540 goto nla_put_failure;
1542 if (tcf_ct_dump_nat(skb, p))
1543 goto nla_put_failure;
1546 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1547 goto nla_put_failure;
1549 tcf_tm_dump(&t, &c->tcf_tm);
1550 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1551 goto nla_put_failure;
1552 spin_unlock_bh(&c->tcf_lock);
1556 spin_unlock_bh(&c->tcf_lock);
1561 static int tcf_ct_walker(struct net *net, struct sk_buff *skb,
1562 struct netlink_callback *cb, int type,
1563 const struct tc_action_ops *ops,
1564 struct netlink_ext_ack *extack)
1566 struct tc_action_net *tn = net_generic(net, ct_net_id);
1568 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
1571 static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index)
1573 struct tc_action_net *tn = net_generic(net, ct_net_id);
1575 return tcf_idr_search(tn, a, index);
1578 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1579 u64 drops, u64 lastuse, bool hw)
1581 struct tcf_ct *c = to_ct(a);
1583 tcf_action_update_stats(a, bytes, packets, drops, hw);
1584 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1587 static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data,
1588 u32 *index_inc, bool bind,
1589 struct netlink_ext_ack *extack)
1592 struct flow_action_entry *entry = entry_data;
1594 entry->id = FLOW_ACTION_CT;
1595 entry->ct.action = tcf_ct_action(act);
1596 entry->ct.zone = tcf_ct_zone(act);
1597 entry->ct.flow_table = tcf_ct_ft(act);
1600 struct flow_offload_action *fl_action = entry_data;
1602 fl_action->id = FLOW_ACTION_CT;
1608 static struct tc_action_ops act_ct_ops = {
1611 .owner = THIS_MODULE,
1613 .dump = tcf_ct_dump,
1614 .init = tcf_ct_init,
1615 .cleanup = tcf_ct_cleanup,
1616 .walk = tcf_ct_walker,
1617 .lookup = tcf_ct_search,
1618 .stats_update = tcf_stats_update,
1619 .offload_act_setup = tcf_ct_offload_act_setup,
1620 .size = sizeof(struct tcf_ct),
1623 static __net_init int ct_init_net(struct net *net)
1625 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
1626 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1628 if (nf_connlabels_get(net, n_bits - 1)) {
1630 pr_err("act_ct: Failed to set connlabels length");
1635 return tc_action_net_init(net, &tn->tn, &act_ct_ops);
1638 static void __net_exit ct_exit_net(struct list_head *net_list)
1643 list_for_each_entry(net, net_list, exit_list) {
1644 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1647 nf_connlabels_put(net);
1651 tc_action_net_exit(net_list, ct_net_id);
1654 static struct pernet_operations ct_net_ops = {
1655 .init = ct_init_net,
1656 .exit_batch = ct_exit_net,
1658 .size = sizeof(struct tc_ct_action_net),
1661 static int __init ct_init_module(void)
1665 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1669 err = tcf_ct_flow_tables_init();
1673 err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1677 static_branch_inc(&tcf_frag_xmit_count);
1682 tcf_ct_flow_tables_uninit();
1684 destroy_workqueue(act_ct_wq);
1688 static void __exit ct_cleanup_module(void)
1690 static_branch_dec(&tcf_frag_xmit_count);
1691 tcf_unregister_action(&act_ct_ops, &ct_net_ops);
1692 tcf_ct_flow_tables_uninit();
1693 destroy_workqueue(act_ct_wq);
1696 module_init(ct_init_module);
1697 module_exit(ct_cleanup_module);
1698 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1699 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1700 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1701 MODULE_DESCRIPTION("Connection tracking action");
1702 MODULE_LICENSE("GPL v2");