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>
27 #include <net/tc_wrapper.h>
29 #include <net/netfilter/nf_flow_table.h>
30 #include <net/netfilter/nf_conntrack.h>
31 #include <net/netfilter/nf_conntrack_core.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 #include <net/netfilter/nf_conntrack_helper.h>
34 #include <net/netfilter/nf_conntrack_acct.h>
35 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
36 #include <net/netfilter/nf_conntrack_act_ct.h>
37 #include <net/netfilter/nf_conntrack_seqadj.h>
38 #include <uapi/linux/netfilter/nf_nat.h>
40 static struct workqueue_struct *act_ct_wq;
41 static struct rhashtable zones_ht;
42 static DEFINE_MUTEX(zones_mutex);
44 struct tcf_ct_flow_table {
45 struct rhash_head node; /* In zones tables */
47 struct rcu_work rwork;
48 struct nf_flowtable nf_ft;
55 static const struct rhashtable_params zones_params = {
56 .head_offset = offsetof(struct tcf_ct_flow_table, node),
57 .key_offset = offsetof(struct tcf_ct_flow_table, zone),
58 .key_len = sizeof_field(struct tcf_ct_flow_table, zone),
59 .automatic_shrinking = true,
62 static struct flow_action_entry *
63 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
65 int i = flow_action->num_entries++;
67 return &flow_action->entries[i];
70 static void tcf_ct_add_mangle_action(struct flow_action *action,
71 enum flow_action_mangle_base htype,
76 struct flow_action_entry *entry;
78 entry = tcf_ct_flow_table_flow_action_get_next(action);
79 entry->id = FLOW_ACTION_MANGLE;
80 entry->mangle.htype = htype;
81 entry->mangle.mask = ~mask;
82 entry->mangle.offset = offset;
83 entry->mangle.val = val;
86 /* The following nat helper functions check if the inverted reverse tuple
87 * (target) is different then the current dir tuple - meaning nat for ports
88 * and/or ip is needed, and add the relevant mangle actions.
91 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
92 struct nf_conntrack_tuple target,
93 struct flow_action *action)
95 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
96 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
97 offsetof(struct iphdr, saddr),
99 be32_to_cpu(target.src.u3.ip));
100 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
101 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
102 offsetof(struct iphdr, daddr),
104 be32_to_cpu(target.dst.u3.ip));
108 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
109 union nf_inet_addr *addr,
114 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++)
115 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
116 i * sizeof(u32) + offset,
117 0xFFFFFFFF, be32_to_cpu(addr->ip6[i]));
121 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
122 struct nf_conntrack_tuple target,
123 struct flow_action *action)
125 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
126 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3,
127 offsetof(struct ipv6hdr,
129 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
130 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3,
131 offsetof(struct ipv6hdr,
136 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
137 struct nf_conntrack_tuple target,
138 struct flow_action *action)
140 __be16 target_src = target.src.u.tcp.port;
141 __be16 target_dst = target.dst.u.tcp.port;
143 if (target_src != tuple->src.u.tcp.port)
144 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
145 offsetof(struct tcphdr, source),
146 0xFFFF, be16_to_cpu(target_src));
147 if (target_dst != tuple->dst.u.tcp.port)
148 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
149 offsetof(struct tcphdr, dest),
150 0xFFFF, be16_to_cpu(target_dst));
154 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
155 struct nf_conntrack_tuple target,
156 struct flow_action *action)
158 __be16 target_src = target.src.u.udp.port;
159 __be16 target_dst = target.dst.u.udp.port;
161 if (target_src != tuple->src.u.udp.port)
162 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
163 offsetof(struct udphdr, source),
164 0xFFFF, be16_to_cpu(target_src));
165 if (target_dst != tuple->dst.u.udp.port)
166 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
167 offsetof(struct udphdr, dest),
168 0xFFFF, be16_to_cpu(target_dst));
171 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
172 enum ip_conntrack_dir dir,
173 enum ip_conntrack_info ctinfo,
174 struct flow_action *action)
176 struct nf_conn_labels *ct_labels;
177 struct flow_action_entry *entry;
180 entry = tcf_ct_flow_table_flow_action_get_next(action);
181 entry->id = FLOW_ACTION_CT_METADATA;
182 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
183 entry->ct_metadata.mark = READ_ONCE(ct->mark);
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 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_info ctinfo;
246 enum ip_conntrack_dir dir;
250 case FLOW_OFFLOAD_DIR_ORIGINAL:
251 dir = IP_CT_DIR_ORIGINAL;
252 ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ?
253 IP_CT_ESTABLISHED : IP_CT_NEW;
254 if (ctinfo == IP_CT_ESTABLISHED)
255 set_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags);
257 case FLOW_OFFLOAD_DIR_REPLY:
258 dir = IP_CT_DIR_REPLY;
259 ctinfo = IP_CT_ESTABLISHED_REPLY;
265 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
269 tcf_ct_flow_table_add_action_meta(ct, dir, ctinfo, action);
273 /* Clear filled actions */
274 for (i = num_entries; i < action->num_entries; i++)
275 memset(&action->entries[i], 0, sizeof(action->entries[i]));
276 action->num_entries = num_entries;
281 static struct nf_flowtable_type flowtable_ct = {
282 .action = tcf_ct_flow_table_fill_actions,
283 .owner = THIS_MODULE,
286 static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params)
288 struct tcf_ct_flow_table *ct_ft;
291 mutex_lock(&zones_mutex);
292 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params);
293 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref))
296 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL);
299 refcount_set(&ct_ft->ref, 1);
301 ct_ft->zone = params->zone;
302 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
306 ct_ft->nf_ft.type = &flowtable_ct;
307 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD |
308 NF_FLOWTABLE_COUNTER;
309 err = nf_flow_table_init(&ct_ft->nf_ft);
312 write_pnet(&ct_ft->nf_ft.net, net);
314 __module_get(THIS_MODULE);
316 params->ct_ft = ct_ft;
317 params->nf_ft = &ct_ft->nf_ft;
318 mutex_unlock(&zones_mutex);
323 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
327 mutex_unlock(&zones_mutex);
331 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
333 struct flow_block_cb *block_cb, *tmp_cb;
334 struct tcf_ct_flow_table *ct_ft;
335 struct flow_block *block;
337 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
339 nf_flow_table_free(&ct_ft->nf_ft);
341 /* Remove any remaining callbacks before cleanup */
342 block = &ct_ft->nf_ft.flow_block;
343 down_write(&ct_ft->nf_ft.flow_block_lock);
344 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) {
345 list_del(&block_cb->list);
346 flow_block_cb_free(block_cb);
348 up_write(&ct_ft->nf_ft.flow_block_lock);
351 module_put(THIS_MODULE);
354 static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft)
356 if (refcount_dec_and_test(&ct_ft->ref)) {
357 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
358 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
359 queue_rcu_work(act_ct_wq, &ct_ft->rwork);
363 static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry,
364 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir)
366 entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC;
367 entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir];
370 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
372 bool tcp, bool bidirectional)
374 struct nf_conn_act_ct_ext *act_ct_ext;
375 struct flow_offload *entry;
378 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
381 entry = flow_offload_alloc(ct);
388 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
389 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
392 __set_bit(NF_FLOW_HW_BIDIRECTIONAL, &entry->flags);
394 act_ct_ext = nf_conn_act_ct_ext_find(ct);
396 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL);
397 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY);
400 err = flow_offload_add(&ct_ft->nf_ft, entry);
407 flow_offload_free(entry);
409 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
412 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
414 enum ip_conntrack_info ctinfo)
416 bool tcp = false, bidirectional = true;
418 switch (nf_ct_protonum(ct)) {
420 if ((ctinfo != IP_CT_ESTABLISHED &&
421 ctinfo != IP_CT_ESTABLISHED_REPLY) ||
422 !test_bit(IPS_ASSURED_BIT, &ct->status) ||
423 ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
429 if (!nf_ct_is_confirmed(ct))
431 if (!test_bit(IPS_ASSURED_BIT, &ct->status))
432 bidirectional = false;
434 #ifdef CONFIG_NF_CT_PROTO_GRE
436 struct nf_conntrack_tuple *tuple;
438 if ((ctinfo != IP_CT_ESTABLISHED &&
439 ctinfo != IP_CT_ESTABLISHED_REPLY) ||
440 !test_bit(IPS_ASSURED_BIT, &ct->status) ||
441 ct->status & IPS_NAT_MASK)
444 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
445 /* No support for GRE v1 */
446 if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
455 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
456 ct->status & IPS_SEQ_ADJUST)
459 tcf_ct_flow_table_add(ct_ft, ct, tcp, bidirectional);
463 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
464 struct flow_offload_tuple *tuple,
465 struct tcphdr **tcph)
467 struct flow_ports *ports;
473 if (!pskb_network_may_pull(skb, sizeof(*iph)))
477 thoff = iph->ihl * 4;
479 if (ip_is_fragment(iph) ||
480 unlikely(thoff != sizeof(struct iphdr)))
483 ipproto = iph->protocol;
486 hdrsize = sizeof(struct tcphdr);
489 hdrsize = sizeof(*ports);
491 #ifdef CONFIG_NF_CT_PROTO_GRE
493 hdrsize = sizeof(struct gre_base_hdr);
503 if (!pskb_network_may_pull(skb, thoff + hdrsize))
508 *tcph = (void *)(skb_network_header(skb) + thoff);
511 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
512 tuple->src_port = ports->source;
513 tuple->dst_port = ports->dest;
516 struct gre_base_hdr *greh;
518 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
519 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
527 tuple->src_v4.s_addr = iph->saddr;
528 tuple->dst_v4.s_addr = iph->daddr;
529 tuple->l3proto = AF_INET;
530 tuple->l4proto = ipproto;
536 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
537 struct flow_offload_tuple *tuple,
538 struct tcphdr **tcph)
540 struct flow_ports *ports;
541 struct ipv6hdr *ip6h;
546 if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
549 ip6h = ipv6_hdr(skb);
550 thoff = sizeof(*ip6h);
552 nexthdr = ip6h->nexthdr;
555 hdrsize = sizeof(struct tcphdr);
558 hdrsize = sizeof(*ports);
560 #ifdef CONFIG_NF_CT_PROTO_GRE
562 hdrsize = sizeof(struct gre_base_hdr);
569 if (ip6h->hop_limit <= 1)
572 if (!pskb_network_may_pull(skb, thoff + hdrsize))
577 *tcph = (void *)(skb_network_header(skb) + thoff);
580 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
581 tuple->src_port = ports->source;
582 tuple->dst_port = ports->dest;
585 struct gre_base_hdr *greh;
587 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
588 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
594 ip6h = ipv6_hdr(skb);
596 tuple->src_v6 = ip6h->saddr;
597 tuple->dst_v6 = ip6h->daddr;
598 tuple->l3proto = AF_INET6;
599 tuple->l4proto = nexthdr;
604 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
608 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
609 struct flow_offload_tuple_rhash *tuplehash;
610 struct flow_offload_tuple tuple = {};
611 enum ip_conntrack_info ctinfo;
612 struct tcphdr *tcph = NULL;
613 struct flow_offload *flow;
619 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
623 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
630 tuplehash = flow_offload_lookup(nf_ft, &tuple);
634 dir = tuplehash->tuple.dir;
635 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
638 if (dir == FLOW_OFFLOAD_DIR_REPLY &&
639 !test_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags)) {
640 /* Only offload reply direction after connection became
643 if (test_bit(IPS_ASSURED_BIT, &ct->status))
644 set_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags);
645 else if (test_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags))
646 /* If flow_table flow has already been updated to the
647 * established state, then don't refresh.
652 if (tcph && (unlikely(tcph->fin || tcph->rst))) {
653 flow_offload_teardown(flow);
657 if (dir == FLOW_OFFLOAD_DIR_ORIGINAL)
658 ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ?
659 IP_CT_ESTABLISHED : IP_CT_NEW;
661 ctinfo = IP_CT_ESTABLISHED_REPLY;
663 flow_offload_refresh(nf_ft, flow);
664 nf_conntrack_get(&ct->ct_general);
665 nf_ct_set(skb, ct, ctinfo);
666 if (nf_ft->flags & NF_FLOWTABLE_COUNTER)
667 nf_ct_acct_update(ct, dir, skb->len);
672 static int tcf_ct_flow_tables_init(void)
674 return rhashtable_init(&zones_ht, &zones_params);
677 static void tcf_ct_flow_tables_uninit(void)
679 rhashtable_destroy(&zones_ht);
682 static struct tc_action_ops act_ct_ops;
684 struct tc_ct_action_net {
685 struct tc_action_net tn; /* Must be first */
689 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
690 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
691 struct tcf_ct_params *p)
693 enum ip_conntrack_info ctinfo;
696 ct = nf_ct_get(skb, &ctinfo);
699 if (!net_eq(net, read_pnet(&ct->ct_net)))
701 if (nf_ct_zone(ct)->id != p->zone)
704 struct nf_conn_help *help;
706 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
707 if (help && rcu_access_pointer(help->helper) != p->helper)
711 /* Force conntrack entry direction. */
712 if ((p->ct_action & TCA_CT_ACT_FORCE) &&
713 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
714 if (nf_ct_is_confirmed(ct))
724 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
729 /* Trim the skb to the length specified by the IP/IPv6 header,
730 * removing any trailing lower-layer padding. This prepares the skb
731 * for higher-layer processing that assumes skb->len excludes padding
732 * (such as nf_ip_checksum). The caller needs to pull the skb to the
733 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
735 static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family)
741 len = skb_ip_totlen(skb);
744 len = sizeof(struct ipv6hdr)
745 + ntohs(ipv6_hdr(skb)->payload_len);
751 return pskb_trim_rcsum(skb, len);
754 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
756 u8 family = NFPROTO_UNSPEC;
758 switch (skb_protocol(skb, true)) {
759 case htons(ETH_P_IP):
760 family = NFPROTO_IPV4;
762 case htons(ETH_P_IPV6):
763 family = NFPROTO_IPV6;
772 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
776 len = skb_network_offset(skb) + sizeof(struct iphdr);
777 if (unlikely(skb->len < len))
779 if (unlikely(!pskb_may_pull(skb, len)))
782 *frag = ip_is_fragment(ip_hdr(skb));
786 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
788 unsigned int flags = 0, len, payload_ofs = 0;
789 unsigned short frag_off;
792 len = skb_network_offset(skb) + sizeof(struct ipv6hdr);
793 if (unlikely(skb->len < len))
795 if (unlikely(!pskb_may_pull(skb, len)))
798 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
799 if (unlikely(nexthdr < 0))
802 *frag = flags & IP6_FH_F_FRAG;
806 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
807 u8 family, u16 zone, bool *defrag)
809 enum ip_conntrack_info ctinfo;
815 /* Previously seen (loopback)? Ignore. */
816 ct = nf_ct_get(skb, &ctinfo);
817 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
820 if (family == NFPROTO_IPV4)
821 err = tcf_ct_ipv4_is_fragment(skb, &frag);
823 err = tcf_ct_ipv6_is_fragment(skb, &frag);
828 mru = tc_skb_cb(skb)->mru;
830 if (family == NFPROTO_IPV4) {
831 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
833 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
835 err = ip_defrag(net, skb, user);
837 if (err && err != -EINPROGRESS)
842 mru = IPCB(skb)->frag_max_size;
844 } else { /* NFPROTO_IPV6 */
845 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
846 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
848 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
849 err = nf_ct_frag6_gather(net, skb, user);
850 if (err && err != -EINPROGRESS)
855 mru = IP6CB(skb)->frag_max_size;
863 if (err != -EINPROGRESS)
864 tc_skb_cb(skb)->mru = mru;
874 static void tcf_ct_params_free(struct tcf_ct_params *params)
876 if (params->helper) {
877 #if IS_ENABLED(CONFIG_NF_NAT)
878 if (params->ct_action & TCA_CT_ACT_NAT)
879 nf_nat_helper_put(params->helper);
881 nf_conntrack_helper_put(params->helper);
884 tcf_ct_flow_table_put(params->ct_ft);
886 nf_ct_put(params->tmpl);
890 static void tcf_ct_params_free_rcu(struct rcu_head *head)
892 struct tcf_ct_params *params;
894 params = container_of(head, struct tcf_ct_params, rcu);
895 tcf_ct_params_free(params);
898 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
900 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
906 new_mark = mark | (READ_ONCE(ct->mark) & ~(mask));
907 if (READ_ONCE(ct->mark) != new_mark) {
908 WRITE_ONCE(ct->mark, new_mark);
909 if (nf_ct_is_confirmed(ct))
910 nf_conntrack_event_cache(IPCT_MARK, ct);
915 static void tcf_ct_act_set_labels(struct nf_conn *ct,
919 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
920 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
922 if (!memchr_inv(labels_m, 0, labels_sz))
925 nf_connlabels_replace(ct, labels, labels_m, 4);
929 static int tcf_ct_act_nat(struct sk_buff *skb,
931 enum ip_conntrack_info ctinfo,
933 struct nf_nat_range2 *range,
936 #if IS_ENABLED(CONFIG_NF_NAT)
939 if (!(ct_action & TCA_CT_ACT_NAT))
941 if (ct_action & TCA_CT_ACT_NAT_SRC)
942 action |= BIT(NF_NAT_MANIP_SRC);
943 if (ct_action & TCA_CT_ACT_NAT_DST)
944 action |= BIT(NF_NAT_MANIP_DST);
946 err = nf_ct_nat(skb, ct, ctinfo, &action, range, commit);
948 if (action & BIT(NF_NAT_MANIP_SRC))
949 tc_skb_cb(skb)->post_ct_snat = 1;
950 if (action & BIT(NF_NAT_MANIP_DST))
951 tc_skb_cb(skb)->post_ct_dnat = 1;
959 TC_INDIRECT_SCOPE int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
960 struct tcf_result *res)
962 struct net *net = dev_net(skb->dev);
963 enum ip_conntrack_info ctinfo;
964 struct tcf_ct *c = to_ct(a);
965 struct nf_conn *tmpl = NULL;
966 struct nf_hook_state state;
967 bool cached, commit, clear;
968 int nh_ofs, err, retval;
969 struct tcf_ct_params *p;
970 bool add_helper = false;
971 bool skip_add = false;
976 p = rcu_dereference_bh(c->params);
978 retval = READ_ONCE(c->tcf_action);
979 commit = p->ct_action & TCA_CT_ACT_COMMIT;
980 clear = p->ct_action & TCA_CT_ACT_CLEAR;
983 tcf_lastuse_update(&c->tcf_tm);
984 tcf_action_update_bstats(&c->common, skb);
987 tc_skb_cb(skb)->post_ct = false;
988 ct = nf_ct_get(skb, &ctinfo);
991 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
997 family = tcf_ct_skb_nf_family(skb);
998 if (family == NFPROTO_UNSPEC)
1001 /* The conntrack module expects to be working at L3.
1002 * We also try to pull the IPv4/6 header to linear area
1004 nh_ofs = skb_network_offset(skb);
1005 skb_pull_rcsum(skb, nh_ofs);
1006 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag);
1007 if (err == -EINPROGRESS) {
1008 retval = TC_ACT_STOLEN;
1014 err = tcf_ct_skb_network_trim(skb, family);
1018 /* If we are recirculating packets to match on ct fields and
1019 * committing with a separate ct action, then we don't need to
1020 * actually run the packet through conntrack twice unless it's for a
1023 cached = tcf_ct_skb_nfct_cached(net, skb, p);
1025 if (tcf_ct_flow_table_lookup(p, skb, family)) {
1030 /* Associate skb with specified zone. */
1032 nf_conntrack_put(skb_nfct(skb));
1033 nf_conntrack_get(&tmpl->ct_general);
1034 nf_ct_set(skb, tmpl, IP_CT_NEW);
1037 state.hook = NF_INET_PRE_ROUTING;
1040 err = nf_conntrack_in(skb, &state);
1041 if (err != NF_ACCEPT)
1046 ct = nf_ct_get(skb, &ctinfo);
1049 nf_ct_deliver_cached_events(ct);
1050 nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
1052 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1053 if (err != NF_ACCEPT)
1056 if (!nf_ct_is_confirmed(ct) && commit && p->helper && !nfct_help(ct)) {
1057 err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC);
1061 if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) {
1062 if (!nfct_seqadj_ext_add(ct))
1067 if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) {
1068 if (nf_ct_helper(skb, ct, ctinfo, family) != NF_ACCEPT)
1073 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1074 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1076 if (!nf_ct_is_confirmed(ct))
1077 nf_conn_act_ct_ext_add(ct);
1079 /* This will take care of sending queued events
1080 * even if the connection is already confirmed.
1082 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1087 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
1090 skb_push_rcsum(skb, nh_ofs);
1092 tc_skb_cb(skb)->post_ct = true;
1093 tc_skb_cb(skb)->zone = p->zone;
1096 qdisc_skb_cb(skb)->pkt_len = skb->len;
1100 tcf_action_inc_drop_qstats(&c->common);
1104 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
1105 [TCA_CT_ACTION] = { .type = NLA_U16 },
1106 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
1107 [TCA_CT_ZONE] = { .type = NLA_U16 },
1108 [TCA_CT_MARK] = { .type = NLA_U32 },
1109 [TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1110 [TCA_CT_LABELS] = { .type = NLA_BINARY,
1111 .len = 128 / BITS_PER_BYTE },
1112 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1113 .len = 128 / BITS_PER_BYTE },
1114 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1115 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
1116 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1117 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1118 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1119 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1120 [TCA_CT_HELPER_NAME] = { .type = NLA_STRING, .len = NF_CT_HELPER_NAME_LEN },
1121 [TCA_CT_HELPER_FAMILY] = { .type = NLA_U8 },
1122 [TCA_CT_HELPER_PROTO] = { .type = NLA_U8 },
1125 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1128 struct netlink_ext_ack *extack)
1130 struct nf_nat_range2 *range;
1132 if (!(p->ct_action & TCA_CT_ACT_NAT))
1135 if (!IS_ENABLED(CONFIG_NF_NAT)) {
1136 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1140 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1143 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1144 (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1145 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1150 if (tb[TCA_CT_NAT_IPV4_MIN]) {
1151 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1153 p->ipv4_range = true;
1154 range->flags |= NF_NAT_RANGE_MAP_IPS;
1155 range->min_addr.ip =
1156 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1158 range->max_addr.ip = max_attr ?
1159 nla_get_in_addr(max_attr) :
1161 } else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1162 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1164 p->ipv4_range = false;
1165 range->flags |= NF_NAT_RANGE_MAP_IPS;
1166 range->min_addr.in6 =
1167 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1169 range->max_addr.in6 = max_attr ?
1170 nla_get_in6_addr(max_attr) :
1171 range->min_addr.in6;
1174 if (tb[TCA_CT_NAT_PORT_MIN]) {
1175 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1176 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1178 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1179 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1180 range->min_proto.all;
1186 static void tcf_ct_set_key_val(struct nlattr **tb,
1187 void *val, int val_type,
1188 void *mask, int mask_type,
1193 nla_memcpy(val, tb[val_type], len);
1198 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1199 memset(mask, 0xff, len);
1201 nla_memcpy(mask, tb[mask_type], len);
1204 static int tcf_ct_fill_params(struct net *net,
1205 struct tcf_ct_params *p,
1208 struct netlink_ext_ack *extack)
1210 struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id);
1211 struct nf_conntrack_zone zone;
1212 int err, family, proto, len;
1213 struct nf_conn *tmpl;
1216 p->zone = NF_CT_DEFAULT_ZONE_ID;
1218 tcf_ct_set_key_val(tb,
1219 &p->ct_action, TCA_CT_ACTION,
1220 NULL, TCA_CT_UNSPEC,
1221 sizeof(p->ct_action));
1223 if (p->ct_action & TCA_CT_ACT_CLEAR)
1226 err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1230 if (tb[TCA_CT_MARK]) {
1231 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1232 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1235 tcf_ct_set_key_val(tb,
1236 &p->mark, TCA_CT_MARK,
1237 &p->mark_mask, TCA_CT_MARK_MASK,
1241 if (tb[TCA_CT_LABELS]) {
1242 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1243 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1248 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1251 tcf_ct_set_key_val(tb,
1252 p->labels, TCA_CT_LABELS,
1253 p->labels_mask, TCA_CT_LABELS_MASK,
1257 if (tb[TCA_CT_ZONE]) {
1258 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1259 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1263 tcf_ct_set_key_val(tb,
1264 &p->zone, TCA_CT_ZONE,
1265 NULL, TCA_CT_UNSPEC,
1269 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1270 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1272 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1276 if (tb[TCA_CT_HELPER_NAME]) {
1277 name = nla_data(tb[TCA_CT_HELPER_NAME]);
1278 len = nla_len(tb[TCA_CT_HELPER_NAME]);
1279 if (len > 16 || name[len - 1] != '\0') {
1280 NL_SET_ERR_MSG_MOD(extack, "Failed to parse helper name.");
1284 family = tb[TCA_CT_HELPER_FAMILY] ? nla_get_u8(tb[TCA_CT_HELPER_FAMILY]) : AF_INET;
1285 proto = tb[TCA_CT_HELPER_PROTO] ? nla_get_u8(tb[TCA_CT_HELPER_PROTO]) : IPPROTO_TCP;
1286 err = nf_ct_add_helper(tmpl, name, family, proto,
1287 p->ct_action & TCA_CT_ACT_NAT, &p->helper);
1289 NL_SET_ERR_MSG_MOD(extack, "Failed to add helper");
1294 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1302 static int tcf_ct_init(struct net *net, struct nlattr *nla,
1303 struct nlattr *est, struct tc_action **a,
1304 struct tcf_proto *tp, u32 flags,
1305 struct netlink_ext_ack *extack)
1307 struct tc_action_net *tn = net_generic(net, act_ct_ops.net_id);
1308 bool bind = flags & TCA_ACT_FLAGS_BIND;
1309 struct tcf_ct_params *params = NULL;
1310 struct nlattr *tb[TCA_CT_MAX + 1];
1311 struct tcf_chain *goto_ch = NULL;
1318 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1322 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1326 if (!tb[TCA_CT_PARMS]) {
1327 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1330 parm = nla_data(tb[TCA_CT_PARMS]);
1331 index = parm->index;
1332 err = tcf_idr_check_alloc(tn, &index, a, bind);
1337 err = tcf_idr_create_from_flags(tn, index, est, a,
1338 &act_ct_ops, bind, flags);
1340 tcf_idr_cleanup(tn, index);
1343 res = ACT_P_CREATED;
1348 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
1349 tcf_idr_release(*a, bind);
1353 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1359 params = kzalloc(sizeof(*params), GFP_KERNEL);
1360 if (unlikely(!params)) {
1365 err = tcf_ct_fill_params(net, params, parm, tb, extack);
1369 err = tcf_ct_flow_table_get(net, params);
1373 spin_lock_bh(&c->tcf_lock);
1374 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1375 params = rcu_replace_pointer(c->params, params,
1376 lockdep_is_held(&c->tcf_lock));
1377 spin_unlock_bh(&c->tcf_lock);
1380 tcf_chain_put_by_act(goto_ch);
1382 call_rcu(¶ms->rcu, tcf_ct_params_free_rcu);
1388 tcf_chain_put_by_act(goto_ch);
1390 tcf_ct_params_free(params);
1391 tcf_idr_release(*a, bind);
1395 static void tcf_ct_cleanup(struct tc_action *a)
1397 struct tcf_ct_params *params;
1398 struct tcf_ct *c = to_ct(a);
1400 params = rcu_dereference_protected(c->params, 1);
1402 call_rcu(¶ms->rcu, tcf_ct_params_free_rcu);
1405 static int tcf_ct_dump_key_val(struct sk_buff *skb,
1406 void *val, int val_type,
1407 void *mask, int mask_type,
1412 if (mask && !memchr_inv(mask, 0, len))
1415 err = nla_put(skb, val_type, len, val);
1419 if (mask_type != TCA_CT_UNSPEC) {
1420 err = nla_put(skb, mask_type, len, mask);
1428 static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p)
1430 struct nf_nat_range2 *range = &p->range;
1432 if (!(p->ct_action & TCA_CT_ACT_NAT))
1435 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1438 if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1439 if (p->ipv4_range) {
1440 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1441 range->min_addr.ip))
1443 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1444 range->max_addr.ip))
1447 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1448 &range->min_addr.in6))
1450 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1451 &range->max_addr.in6))
1456 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1457 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1458 range->min_proto.all))
1460 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1461 range->max_proto.all))
1468 static int tcf_ct_dump_helper(struct sk_buff *skb, struct nf_conntrack_helper *helper)
1473 if (nla_put_string(skb, TCA_CT_HELPER_NAME, helper->name) ||
1474 nla_put_u8(skb, TCA_CT_HELPER_FAMILY, helper->tuple.src.l3num) ||
1475 nla_put_u8(skb, TCA_CT_HELPER_PROTO, helper->tuple.dst.protonum))
1481 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1484 unsigned char *b = skb_tail_pointer(skb);
1485 struct tcf_ct *c = to_ct(a);
1486 struct tcf_ct_params *p;
1488 struct tc_ct opt = {
1489 .index = c->tcf_index,
1490 .refcnt = refcount_read(&c->tcf_refcnt) - ref,
1491 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1495 spin_lock_bh(&c->tcf_lock);
1496 p = rcu_dereference_protected(c->params,
1497 lockdep_is_held(&c->tcf_lock));
1498 opt.action = c->tcf_action;
1500 if (tcf_ct_dump_key_val(skb,
1501 &p->ct_action, TCA_CT_ACTION,
1502 NULL, TCA_CT_UNSPEC,
1503 sizeof(p->ct_action)))
1504 goto nla_put_failure;
1506 if (p->ct_action & TCA_CT_ACT_CLEAR)
1509 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1510 tcf_ct_dump_key_val(skb,
1511 &p->mark, TCA_CT_MARK,
1512 &p->mark_mask, TCA_CT_MARK_MASK,
1514 goto nla_put_failure;
1516 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1517 tcf_ct_dump_key_val(skb,
1518 p->labels, TCA_CT_LABELS,
1519 p->labels_mask, TCA_CT_LABELS_MASK,
1521 goto nla_put_failure;
1523 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1524 tcf_ct_dump_key_val(skb,
1525 &p->zone, TCA_CT_ZONE,
1526 NULL, TCA_CT_UNSPEC,
1528 goto nla_put_failure;
1530 if (tcf_ct_dump_nat(skb, p))
1531 goto nla_put_failure;
1533 if (tcf_ct_dump_helper(skb, p->helper))
1534 goto nla_put_failure;
1537 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1538 goto nla_put_failure;
1540 tcf_tm_dump(&t, &c->tcf_tm);
1541 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1542 goto nla_put_failure;
1543 spin_unlock_bh(&c->tcf_lock);
1547 spin_unlock_bh(&c->tcf_lock);
1552 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1553 u64 drops, u64 lastuse, bool hw)
1555 struct tcf_ct *c = to_ct(a);
1557 tcf_action_update_stats(a, bytes, packets, drops, hw);
1558 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1561 static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data,
1562 u32 *index_inc, bool bind,
1563 struct netlink_ext_ack *extack)
1566 struct flow_action_entry *entry = entry_data;
1568 entry->id = FLOW_ACTION_CT;
1569 entry->ct.action = tcf_ct_action(act);
1570 entry->ct.zone = tcf_ct_zone(act);
1571 entry->ct.flow_table = tcf_ct_ft(act);
1574 struct flow_offload_action *fl_action = entry_data;
1576 fl_action->id = FLOW_ACTION_CT;
1582 static struct tc_action_ops act_ct_ops = {
1585 .owner = THIS_MODULE,
1587 .dump = tcf_ct_dump,
1588 .init = tcf_ct_init,
1589 .cleanup = tcf_ct_cleanup,
1590 .stats_update = tcf_stats_update,
1591 .offload_act_setup = tcf_ct_offload_act_setup,
1592 .size = sizeof(struct tcf_ct),
1595 static __net_init int ct_init_net(struct net *net)
1597 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
1598 struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id);
1600 if (nf_connlabels_get(net, n_bits - 1)) {
1602 pr_err("act_ct: Failed to set connlabels length");
1607 return tc_action_net_init(net, &tn->tn, &act_ct_ops);
1610 static void __net_exit ct_exit_net(struct list_head *net_list)
1615 list_for_each_entry(net, net_list, exit_list) {
1616 struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id);
1619 nf_connlabels_put(net);
1623 tc_action_net_exit(net_list, act_ct_ops.net_id);
1626 static struct pernet_operations ct_net_ops = {
1627 .init = ct_init_net,
1628 .exit_batch = ct_exit_net,
1629 .id = &act_ct_ops.net_id,
1630 .size = sizeof(struct tc_ct_action_net),
1633 static int __init ct_init_module(void)
1637 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1641 err = tcf_ct_flow_tables_init();
1645 err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1649 static_branch_inc(&tcf_frag_xmit_count);
1654 tcf_ct_flow_tables_uninit();
1656 destroy_workqueue(act_ct_wq);
1660 static void __exit ct_cleanup_module(void)
1662 static_branch_dec(&tcf_frag_xmit_count);
1663 tcf_unregister_action(&act_ct_ops, &ct_net_ops);
1664 tcf_ct_flow_tables_uninit();
1665 destroy_workqueue(act_ct_wq);
1668 module_init(ct_init_module);
1669 module_exit(ct_cleanup_module);
1670 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1671 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1672 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1673 MODULE_DESCRIPTION("Connection tracking action");
1674 MODULE_LICENSE("GPL v2");