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 <uapi/linux/netfilter/nf_nat.h>
37 static struct workqueue_struct *act_ct_wq;
38 static struct rhashtable zones_ht;
39 static DEFINE_MUTEX(zones_mutex);
41 struct tcf_ct_flow_table {
42 struct rhash_head node; /* In zones tables */
44 struct rcu_work rwork;
45 struct nf_flowtable nf_ft;
52 static const struct rhashtable_params zones_params = {
53 .head_offset = offsetof(struct tcf_ct_flow_table, node),
54 .key_offset = offsetof(struct tcf_ct_flow_table, zone),
55 .key_len = sizeof_field(struct tcf_ct_flow_table, zone),
56 .automatic_shrinking = true,
59 static struct flow_action_entry *
60 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
62 int i = flow_action->num_entries++;
64 return &flow_action->entries[i];
67 static void tcf_ct_add_mangle_action(struct flow_action *action,
68 enum flow_action_mangle_base htype,
73 struct flow_action_entry *entry;
75 entry = tcf_ct_flow_table_flow_action_get_next(action);
76 entry->id = FLOW_ACTION_MANGLE;
77 entry->mangle.htype = htype;
78 entry->mangle.mask = ~mask;
79 entry->mangle.offset = offset;
80 entry->mangle.val = val;
83 /* The following nat helper functions check if the inverted reverse tuple
84 * (target) is different then the current dir tuple - meaning nat for ports
85 * and/or ip is needed, and add the relevant mangle actions.
88 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
89 struct nf_conntrack_tuple target,
90 struct flow_action *action)
92 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
93 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
94 offsetof(struct iphdr, saddr),
96 be32_to_cpu(target.src.u3.ip));
97 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
98 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
99 offsetof(struct iphdr, daddr),
101 be32_to_cpu(target.dst.u3.ip));
105 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
106 union nf_inet_addr *addr,
111 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++)
112 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
113 i * sizeof(u32) + offset,
114 0xFFFFFFFF, be32_to_cpu(addr->ip6[i]));
118 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
119 struct nf_conntrack_tuple target,
120 struct flow_action *action)
122 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
123 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3,
124 offsetof(struct ipv6hdr,
126 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
127 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3,
128 offsetof(struct ipv6hdr,
133 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
134 struct nf_conntrack_tuple target,
135 struct flow_action *action)
137 __be16 target_src = target.src.u.tcp.port;
138 __be16 target_dst = target.dst.u.tcp.port;
140 if (target_src != tuple->src.u.tcp.port)
141 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
142 offsetof(struct tcphdr, source),
143 0xFFFF, be16_to_cpu(target_src));
144 if (target_dst != tuple->dst.u.tcp.port)
145 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
146 offsetof(struct tcphdr, dest),
147 0xFFFF, be16_to_cpu(target_dst));
151 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
152 struct nf_conntrack_tuple target,
153 struct flow_action *action)
155 __be16 target_src = target.src.u.udp.port;
156 __be16 target_dst = target.dst.u.udp.port;
158 if (target_src != tuple->src.u.udp.port)
159 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
160 offsetof(struct udphdr, source),
161 0xFFFF, be16_to_cpu(target_src));
162 if (target_dst != tuple->dst.u.udp.port)
163 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
164 offsetof(struct udphdr, dest),
165 0xFFFF, be16_to_cpu(target_dst));
168 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
169 enum ip_conntrack_dir dir,
170 struct flow_action *action)
172 struct nf_conn_labels *ct_labels;
173 struct flow_action_entry *entry;
174 enum ip_conntrack_info ctinfo;
177 entry = tcf_ct_flow_table_flow_action_get_next(action);
178 entry->id = FLOW_ACTION_CT_METADATA;
179 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
180 entry->ct_metadata.mark = READ_ONCE(ct->mark);
182 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
183 IP_CT_ESTABLISHED_REPLY;
184 /* aligns with the CT reference on the SKB nf_ct_set */
185 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo;
187 act_ct_labels = entry->ct_metadata.labels;
188 ct_labels = nf_ct_labels_find(ct);
190 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE);
192 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE);
195 static int tcf_ct_flow_table_add_action_nat(struct net *net,
197 enum ip_conntrack_dir dir,
198 struct flow_action *action)
200 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
201 struct nf_conntrack_tuple target;
203 if (!(ct->status & IPS_NAT_MASK))
206 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
208 switch (tuple->src.l3num) {
210 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target,
214 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target,
221 switch (nf_ct_protonum(ct)) {
223 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action);
226 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action);
235 static int tcf_ct_flow_table_fill_actions(struct net *net,
236 const struct flow_offload *flow,
237 enum flow_offload_tuple_dir tdir,
238 struct nf_flow_rule *flow_rule)
240 struct flow_action *action = &flow_rule->rule->action;
241 int num_entries = action->num_entries;
242 struct nf_conn *ct = flow->ct;
243 enum ip_conntrack_dir dir;
247 case FLOW_OFFLOAD_DIR_ORIGINAL:
248 dir = IP_CT_DIR_ORIGINAL;
250 case FLOW_OFFLOAD_DIR_REPLY:
251 dir = IP_CT_DIR_REPLY;
257 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
261 tcf_ct_flow_table_add_action_meta(ct, dir, action);
265 /* Clear filled actions */
266 for (i = num_entries; i < action->num_entries; i++)
267 memset(&action->entries[i], 0, sizeof(action->entries[i]));
268 action->num_entries = num_entries;
273 static struct nf_flowtable_type flowtable_ct = {
274 .action = tcf_ct_flow_table_fill_actions,
275 .owner = THIS_MODULE,
278 static int tcf_ct_flow_table_get(struct tcf_ct_params *params)
280 struct tcf_ct_flow_table *ct_ft;
283 mutex_lock(&zones_mutex);
284 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params);
285 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref))
288 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL);
291 refcount_set(&ct_ft->ref, 1);
293 ct_ft->zone = params->zone;
294 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
298 ct_ft->nf_ft.type = &flowtable_ct;
299 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD;
300 err = nf_flow_table_init(&ct_ft->nf_ft);
304 __module_get(THIS_MODULE);
306 params->ct_ft = ct_ft;
307 params->nf_ft = &ct_ft->nf_ft;
308 mutex_unlock(&zones_mutex);
313 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
317 mutex_unlock(&zones_mutex);
321 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
323 struct flow_block_cb *block_cb, *tmp_cb;
324 struct tcf_ct_flow_table *ct_ft;
325 struct flow_block *block;
327 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
329 nf_flow_table_free(&ct_ft->nf_ft);
331 /* Remove any remaining callbacks before cleanup */
332 block = &ct_ft->nf_ft.flow_block;
333 down_write(&ct_ft->nf_ft.flow_block_lock);
334 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) {
335 list_del(&block_cb->list);
336 flow_block_cb_free(block_cb);
338 up_write(&ct_ft->nf_ft.flow_block_lock);
341 module_put(THIS_MODULE);
344 static void tcf_ct_flow_table_put(struct tcf_ct_params *params)
346 struct tcf_ct_flow_table *ct_ft = params->ct_ft;
348 if (refcount_dec_and_test(¶ms->ct_ft->ref)) {
349 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
350 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
351 queue_rcu_work(act_ct_wq, &ct_ft->rwork);
355 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
359 struct flow_offload *entry;
362 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
365 entry = flow_offload_alloc(ct);
372 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
373 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
376 err = flow_offload_add(&ct_ft->nf_ft, entry);
383 flow_offload_free(entry);
385 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
388 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
390 enum ip_conntrack_info ctinfo)
394 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
397 switch (nf_ct_protonum(ct)) {
400 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
409 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
410 ct->status & IPS_SEQ_ADJUST)
413 tcf_ct_flow_table_add(ct_ft, ct, tcp);
417 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
418 struct flow_offload_tuple *tuple,
419 struct tcphdr **tcph)
421 struct flow_ports *ports;
425 if (!pskb_network_may_pull(skb, sizeof(*iph)))
429 thoff = iph->ihl * 4;
431 if (ip_is_fragment(iph) ||
432 unlikely(thoff != sizeof(struct iphdr)))
435 if (iph->protocol != IPPROTO_TCP &&
436 iph->protocol != IPPROTO_UDP)
442 if (!pskb_network_may_pull(skb, iph->protocol == IPPROTO_TCP ?
443 thoff + sizeof(struct tcphdr) :
444 thoff + sizeof(*ports)))
448 if (iph->protocol == IPPROTO_TCP)
449 *tcph = (void *)(skb_network_header(skb) + thoff);
451 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
452 tuple->src_v4.s_addr = iph->saddr;
453 tuple->dst_v4.s_addr = iph->daddr;
454 tuple->src_port = ports->source;
455 tuple->dst_port = ports->dest;
456 tuple->l3proto = AF_INET;
457 tuple->l4proto = iph->protocol;
463 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
464 struct flow_offload_tuple *tuple,
465 struct tcphdr **tcph)
467 struct flow_ports *ports;
468 struct ipv6hdr *ip6h;
471 if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
474 ip6h = ipv6_hdr(skb);
476 if (ip6h->nexthdr != IPPROTO_TCP &&
477 ip6h->nexthdr != IPPROTO_UDP)
480 if (ip6h->hop_limit <= 1)
483 thoff = sizeof(*ip6h);
484 if (!pskb_network_may_pull(skb, ip6h->nexthdr == IPPROTO_TCP ?
485 thoff + sizeof(struct tcphdr) :
486 thoff + sizeof(*ports)))
489 ip6h = ipv6_hdr(skb);
490 if (ip6h->nexthdr == IPPROTO_TCP)
491 *tcph = (void *)(skb_network_header(skb) + thoff);
493 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
494 tuple->src_v6 = ip6h->saddr;
495 tuple->dst_v6 = ip6h->daddr;
496 tuple->src_port = ports->source;
497 tuple->dst_port = ports->dest;
498 tuple->l3proto = AF_INET6;
499 tuple->l4proto = ip6h->nexthdr;
504 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
508 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
509 struct flow_offload_tuple_rhash *tuplehash;
510 struct flow_offload_tuple tuple = {};
511 enum ip_conntrack_info ctinfo;
512 struct tcphdr *tcph = NULL;
513 struct flow_offload *flow;
519 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
523 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
530 tuplehash = flow_offload_lookup(nf_ft, &tuple);
534 dir = tuplehash->tuple.dir;
535 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
538 if (tcph && (unlikely(tcph->fin || tcph->rst))) {
539 flow_offload_teardown(flow);
543 ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
544 IP_CT_ESTABLISHED_REPLY;
546 flow_offload_refresh(nf_ft, flow);
547 nf_conntrack_get(&ct->ct_general);
548 nf_ct_set(skb, ct, ctinfo);
549 nf_ct_acct_update(ct, dir, skb->len);
554 static int tcf_ct_flow_tables_init(void)
556 return rhashtable_init(&zones_ht, &zones_params);
559 static void tcf_ct_flow_tables_uninit(void)
561 rhashtable_destroy(&zones_ht);
564 static struct tc_action_ops act_ct_ops;
565 static unsigned int ct_net_id;
567 struct tc_ct_action_net {
568 struct tc_action_net tn; /* Must be first */
572 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
573 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
574 u16 zone_id, bool force)
576 enum ip_conntrack_info ctinfo;
579 ct = nf_ct_get(skb, &ctinfo);
582 if (!net_eq(net, read_pnet(&ct->ct_net)))
584 if (nf_ct_zone(ct)->id != zone_id)
587 /* Force conntrack entry direction. */
588 if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
589 if (nf_ct_is_confirmed(ct))
592 nf_conntrack_put(&ct->ct_general);
593 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
601 /* Trim the skb to the length specified by the IP/IPv6 header,
602 * removing any trailing lower-layer padding. This prepares the skb
603 * for higher-layer processing that assumes skb->len excludes padding
604 * (such as nf_ip_checksum). The caller needs to pull the skb to the
605 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
607 static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family)
614 len = ntohs(ip_hdr(skb)->tot_len);
617 len = sizeof(struct ipv6hdr)
618 + ntohs(ipv6_hdr(skb)->payload_len);
624 err = pskb_trim_rcsum(skb, len);
629 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
631 u8 family = NFPROTO_UNSPEC;
633 switch (skb_protocol(skb, true)) {
634 case htons(ETH_P_IP):
635 family = NFPROTO_IPV4;
637 case htons(ETH_P_IPV6):
638 family = NFPROTO_IPV6;
647 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
651 len = skb_network_offset(skb) + sizeof(struct iphdr);
652 if (unlikely(skb->len < len))
654 if (unlikely(!pskb_may_pull(skb, len)))
657 *frag = ip_is_fragment(ip_hdr(skb));
661 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
663 unsigned int flags = 0, len, payload_ofs = 0;
664 unsigned short frag_off;
667 len = skb_network_offset(skb) + sizeof(struct ipv6hdr);
668 if (unlikely(skb->len < len))
670 if (unlikely(!pskb_may_pull(skb, len)))
673 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
674 if (unlikely(nexthdr < 0))
677 *frag = flags & IP6_FH_F_FRAG;
681 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
682 u8 family, u16 zone, bool *defrag)
684 enum ip_conntrack_info ctinfo;
685 struct qdisc_skb_cb cb;
690 /* Previously seen (loopback)? Ignore. */
691 ct = nf_ct_get(skb, &ctinfo);
692 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
695 if (family == NFPROTO_IPV4)
696 err = tcf_ct_ipv4_is_fragment(skb, &frag);
698 err = tcf_ct_ipv6_is_fragment(skb, &frag);
703 cb = *qdisc_skb_cb(skb);
705 if (family == NFPROTO_IPV4) {
706 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
708 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
710 err = ip_defrag(net, skb, user);
712 if (err && err != -EINPROGRESS)
717 cb.mru = IPCB(skb)->frag_max_size;
719 } else { /* NFPROTO_IPV6 */
720 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
721 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
723 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
724 err = nf_ct_frag6_gather(net, skb, user);
725 if (err && err != -EINPROGRESS)
730 cb.mru = IP6CB(skb)->frag_max_size;
738 *qdisc_skb_cb(skb) = cb;
748 static void tcf_ct_params_free(struct rcu_head *head)
750 struct tcf_ct_params *params = container_of(head,
751 struct tcf_ct_params, rcu);
753 tcf_ct_flow_table_put(params);
756 nf_conntrack_put(¶ms->tmpl->ct_general);
760 #if IS_ENABLED(CONFIG_NF_NAT)
761 /* Modelled after nf_nat_ipv[46]_fn().
762 * range is only used for new, uninitialized NAT state.
763 * Returns either NF_ACCEPT or NF_DROP.
765 static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
766 enum ip_conntrack_info ctinfo,
767 const struct nf_nat_range2 *range,
768 enum nf_nat_manip_type maniptype)
770 __be16 proto = skb_protocol(skb, true);
771 int hooknum, err = NF_ACCEPT;
773 /* See HOOK2MANIP(). */
774 if (maniptype == NF_NAT_MANIP_SRC)
775 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
777 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
781 case IP_CT_RELATED_REPLY:
782 if (proto == htons(ETH_P_IP) &&
783 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
784 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
788 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) {
790 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
791 int hdrlen = ipv6_skip_exthdr(skb,
792 sizeof(struct ipv6hdr),
793 &nexthdr, &frag_off);
795 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
796 if (!nf_nat_icmpv6_reply_translation(skb, ct,
804 /* Non-ICMP, fall thru to initialize if needed. */
807 /* Seen it before? This can happen for loopback, retrans,
810 if (!nf_nat_initialized(ct, maniptype)) {
811 /* Initialize according to the NAT action. */
812 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
813 /* Action is set up to establish a new
816 ? nf_nat_setup_info(ct, range, maniptype)
817 : nf_nat_alloc_null_binding(ct, hooknum);
818 if (err != NF_ACCEPT)
823 case IP_CT_ESTABLISHED:
824 case IP_CT_ESTABLISHED_REPLY:
832 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
836 #endif /* CONFIG_NF_NAT */
838 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
840 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
846 new_mark = mark | (READ_ONCE(ct->mark) & ~(mask));
847 if (READ_ONCE(ct->mark) != new_mark) {
848 WRITE_ONCE(ct->mark, new_mark);
849 if (nf_ct_is_confirmed(ct))
850 nf_conntrack_event_cache(IPCT_MARK, ct);
855 static void tcf_ct_act_set_labels(struct nf_conn *ct,
859 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
860 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
862 if (!memchr_inv(labels_m, 0, labels_sz))
865 nf_connlabels_replace(ct, labels, labels_m, 4);
869 static int tcf_ct_act_nat(struct sk_buff *skb,
871 enum ip_conntrack_info ctinfo,
873 struct nf_nat_range2 *range,
876 #if IS_ENABLED(CONFIG_NF_NAT)
878 enum nf_nat_manip_type maniptype;
880 if (!(ct_action & TCA_CT_ACT_NAT))
883 /* Add NAT extension if not confirmed yet. */
884 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
885 return NF_DROP; /* Can't NAT. */
887 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) &&
888 (ctinfo != IP_CT_RELATED || commit)) {
889 /* NAT an established or related connection like before. */
890 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
891 /* This is the REPLY direction for a connection
892 * for which NAT was applied in the forward
893 * direction. Do the reverse NAT.
895 maniptype = ct->status & IPS_SRC_NAT
896 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
898 maniptype = ct->status & IPS_SRC_NAT
899 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
900 } else if (ct_action & TCA_CT_ACT_NAT_SRC) {
901 maniptype = NF_NAT_MANIP_SRC;
902 } else if (ct_action & TCA_CT_ACT_NAT_DST) {
903 maniptype = NF_NAT_MANIP_DST;
908 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
909 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
910 if (ct->status & IPS_SRC_NAT) {
911 if (maniptype == NF_NAT_MANIP_SRC)
912 maniptype = NF_NAT_MANIP_DST;
914 maniptype = NF_NAT_MANIP_SRC;
916 err = ct_nat_execute(skb, ct, ctinfo, range,
918 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
919 err = ct_nat_execute(skb, ct, ctinfo, NULL,
929 static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
930 struct tcf_result *res)
932 struct net *net = dev_net(skb->dev);
933 bool cached, commit, clear, force;
934 enum ip_conntrack_info ctinfo;
935 struct tcf_ct *c = to_ct(a);
936 struct nf_conn *tmpl = NULL;
937 struct nf_hook_state state;
938 int nh_ofs, err, retval;
939 struct tcf_ct_params *p;
940 bool skip_add = false;
945 p = rcu_dereference_bh(c->params);
947 retval = READ_ONCE(c->tcf_action);
948 commit = p->ct_action & TCA_CT_ACT_COMMIT;
949 clear = p->ct_action & TCA_CT_ACT_CLEAR;
950 force = p->ct_action & TCA_CT_ACT_FORCE;
953 tcf_lastuse_update(&c->tcf_tm);
956 ct = nf_ct_get(skb, &ctinfo);
958 nf_conntrack_put(&ct->ct_general);
959 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
965 family = tcf_ct_skb_nf_family(skb);
966 if (family == NFPROTO_UNSPEC)
969 /* The conntrack module expects to be working at L3.
970 * We also try to pull the IPv4/6 header to linear area
972 nh_ofs = skb_network_offset(skb);
973 skb_pull_rcsum(skb, nh_ofs);
974 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag);
975 if (err == -EINPROGRESS) {
976 retval = TC_ACT_STOLEN;
982 err = tcf_ct_skb_network_trim(skb, family);
986 /* If we are recirculating packets to match on ct fields and
987 * committing with a separate ct action, then we don't need to
988 * actually run the packet through conntrack twice unless it's for a
991 cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force);
993 if (tcf_ct_flow_table_lookup(p, skb, family)) {
998 /* Associate skb with specified zone. */
1000 ct = nf_ct_get(skb, &ctinfo);
1002 nf_conntrack_put(skb_nfct(skb));
1003 nf_conntrack_get(&tmpl->ct_general);
1004 nf_ct_set(skb, tmpl, IP_CT_NEW);
1007 state.hook = NF_INET_PRE_ROUTING;
1010 err = nf_conntrack_in(skb, &state);
1011 if (err != NF_ACCEPT)
1016 ct = nf_ct_get(skb, &ctinfo);
1019 nf_ct_deliver_cached_events(ct);
1021 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1022 if (err != NF_ACCEPT)
1026 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1027 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1029 /* This will take care of sending queued events
1030 * even if the connection is already confirmed.
1032 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1037 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
1040 skb_push_rcsum(skb, nh_ofs);
1043 tcf_action_update_bstats(&c->common, skb);
1045 qdisc_skb_cb(skb)->pkt_len = skb->len;
1049 tcf_action_inc_drop_qstats(&c->common);
1053 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
1054 [TCA_CT_ACTION] = { .type = NLA_U16 },
1055 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
1056 [TCA_CT_ZONE] = { .type = NLA_U16 },
1057 [TCA_CT_MARK] = { .type = NLA_U32 },
1058 [TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1059 [TCA_CT_LABELS] = { .type = NLA_BINARY,
1060 .len = 128 / BITS_PER_BYTE },
1061 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1062 .len = 128 / BITS_PER_BYTE },
1063 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1064 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
1065 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1066 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1067 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1068 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1071 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1074 struct netlink_ext_ack *extack)
1076 struct nf_nat_range2 *range;
1078 if (!(p->ct_action & TCA_CT_ACT_NAT))
1081 if (!IS_ENABLED(CONFIG_NF_NAT)) {
1082 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1086 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1089 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1090 (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1091 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1096 if (tb[TCA_CT_NAT_IPV4_MIN]) {
1097 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1099 p->ipv4_range = true;
1100 range->flags |= NF_NAT_RANGE_MAP_IPS;
1101 range->min_addr.ip =
1102 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1104 range->max_addr.ip = max_attr ?
1105 nla_get_in_addr(max_attr) :
1107 } else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1108 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1110 p->ipv4_range = false;
1111 range->flags |= NF_NAT_RANGE_MAP_IPS;
1112 range->min_addr.in6 =
1113 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1115 range->max_addr.in6 = max_attr ?
1116 nla_get_in6_addr(max_attr) :
1117 range->min_addr.in6;
1120 if (tb[TCA_CT_NAT_PORT_MIN]) {
1121 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1122 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1124 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1125 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1126 range->min_proto.all;
1132 static void tcf_ct_set_key_val(struct nlattr **tb,
1133 void *val, int val_type,
1134 void *mask, int mask_type,
1139 nla_memcpy(val, tb[val_type], len);
1144 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1145 memset(mask, 0xff, len);
1147 nla_memcpy(mask, tb[mask_type], len);
1150 static int tcf_ct_fill_params(struct net *net,
1151 struct tcf_ct_params *p,
1154 struct netlink_ext_ack *extack)
1156 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1157 struct nf_conntrack_zone zone;
1158 struct nf_conn *tmpl;
1161 p->zone = NF_CT_DEFAULT_ZONE_ID;
1163 tcf_ct_set_key_val(tb,
1164 &p->ct_action, TCA_CT_ACTION,
1165 NULL, TCA_CT_UNSPEC,
1166 sizeof(p->ct_action));
1168 if (p->ct_action & TCA_CT_ACT_CLEAR)
1171 err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1175 if (tb[TCA_CT_MARK]) {
1176 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1177 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1180 tcf_ct_set_key_val(tb,
1181 &p->mark, TCA_CT_MARK,
1182 &p->mark_mask, TCA_CT_MARK_MASK,
1186 if (tb[TCA_CT_LABELS]) {
1187 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1188 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1193 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1196 tcf_ct_set_key_val(tb,
1197 p->labels, TCA_CT_LABELS,
1198 p->labels_mask, TCA_CT_LABELS_MASK,
1202 if (tb[TCA_CT_ZONE]) {
1203 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1204 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1208 tcf_ct_set_key_val(tb,
1209 &p->zone, TCA_CT_ZONE,
1210 NULL, TCA_CT_UNSPEC,
1214 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1215 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1217 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1220 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1221 nf_conntrack_get(&tmpl->ct_general);
1227 static int tcf_ct_init(struct net *net, struct nlattr *nla,
1228 struct nlattr *est, struct tc_action **a,
1229 int replace, int bind, bool rtnl_held,
1230 struct tcf_proto *tp, u32 flags,
1231 struct netlink_ext_ack *extack)
1233 struct tc_action_net *tn = net_generic(net, ct_net_id);
1234 struct tcf_ct_params *params = NULL;
1235 struct nlattr *tb[TCA_CT_MAX + 1];
1236 struct tcf_chain *goto_ch = NULL;
1243 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1247 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1251 if (!tb[TCA_CT_PARMS]) {
1252 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1255 parm = nla_data(tb[TCA_CT_PARMS]);
1256 index = parm->index;
1257 err = tcf_idr_check_alloc(tn, &index, a, bind);
1262 err = tcf_idr_create_from_flags(tn, index, est, a,
1263 &act_ct_ops, bind, flags);
1265 tcf_idr_cleanup(tn, index);
1268 res = ACT_P_CREATED;
1274 tcf_idr_release(*a, bind);
1278 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1284 params = kzalloc(sizeof(*params), GFP_KERNEL);
1285 if (unlikely(!params)) {
1290 err = tcf_ct_fill_params(net, params, parm, tb, extack);
1294 err = tcf_ct_flow_table_get(params);
1296 goto cleanup_params;
1298 spin_lock_bh(&c->tcf_lock);
1299 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1300 params = rcu_replace_pointer(c->params, params,
1301 lockdep_is_held(&c->tcf_lock));
1302 spin_unlock_bh(&c->tcf_lock);
1305 tcf_chain_put_by_act(goto_ch);
1307 call_rcu(¶ms->rcu, tcf_ct_params_free);
1313 nf_ct_put(params->tmpl);
1316 tcf_chain_put_by_act(goto_ch);
1318 tcf_idr_release(*a, bind);
1322 static void tcf_ct_cleanup(struct tc_action *a)
1324 struct tcf_ct_params *params;
1325 struct tcf_ct *c = to_ct(a);
1327 params = rcu_dereference_protected(c->params, 1);
1329 call_rcu(¶ms->rcu, tcf_ct_params_free);
1332 static int tcf_ct_dump_key_val(struct sk_buff *skb,
1333 void *val, int val_type,
1334 void *mask, int mask_type,
1339 if (mask && !memchr_inv(mask, 0, len))
1342 err = nla_put(skb, val_type, len, val);
1346 if (mask_type != TCA_CT_UNSPEC) {
1347 err = nla_put(skb, mask_type, len, mask);
1355 static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p)
1357 struct nf_nat_range2 *range = &p->range;
1359 if (!(p->ct_action & TCA_CT_ACT_NAT))
1362 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1365 if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1366 if (p->ipv4_range) {
1367 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1368 range->min_addr.ip))
1370 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1371 range->max_addr.ip))
1374 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1375 &range->min_addr.in6))
1377 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1378 &range->max_addr.in6))
1383 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1384 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1385 range->min_proto.all))
1387 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1388 range->max_proto.all))
1395 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1398 unsigned char *b = skb_tail_pointer(skb);
1399 struct tcf_ct *c = to_ct(a);
1400 struct tcf_ct_params *p;
1402 struct tc_ct opt = {
1403 .index = c->tcf_index,
1404 .refcnt = refcount_read(&c->tcf_refcnt) - ref,
1405 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1409 spin_lock_bh(&c->tcf_lock);
1410 p = rcu_dereference_protected(c->params,
1411 lockdep_is_held(&c->tcf_lock));
1412 opt.action = c->tcf_action;
1414 if (tcf_ct_dump_key_val(skb,
1415 &p->ct_action, TCA_CT_ACTION,
1416 NULL, TCA_CT_UNSPEC,
1417 sizeof(p->ct_action)))
1418 goto nla_put_failure;
1420 if (p->ct_action & TCA_CT_ACT_CLEAR)
1423 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1424 tcf_ct_dump_key_val(skb,
1425 &p->mark, TCA_CT_MARK,
1426 &p->mark_mask, TCA_CT_MARK_MASK,
1428 goto nla_put_failure;
1430 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1431 tcf_ct_dump_key_val(skb,
1432 p->labels, TCA_CT_LABELS,
1433 p->labels_mask, TCA_CT_LABELS_MASK,
1435 goto nla_put_failure;
1437 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1438 tcf_ct_dump_key_val(skb,
1439 &p->zone, TCA_CT_ZONE,
1440 NULL, TCA_CT_UNSPEC,
1442 goto nla_put_failure;
1444 if (tcf_ct_dump_nat(skb, p))
1445 goto nla_put_failure;
1448 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1449 goto nla_put_failure;
1451 tcf_tm_dump(&t, &c->tcf_tm);
1452 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1453 goto nla_put_failure;
1454 spin_unlock_bh(&c->tcf_lock);
1458 spin_unlock_bh(&c->tcf_lock);
1463 static int tcf_ct_walker(struct net *net, struct sk_buff *skb,
1464 struct netlink_callback *cb, int type,
1465 const struct tc_action_ops *ops,
1466 struct netlink_ext_ack *extack)
1468 struct tc_action_net *tn = net_generic(net, ct_net_id);
1470 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
1473 static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index)
1475 struct tc_action_net *tn = net_generic(net, ct_net_id);
1477 return tcf_idr_search(tn, a, index);
1480 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1481 u64 drops, u64 lastuse, bool hw)
1483 struct tcf_ct *c = to_ct(a);
1485 tcf_action_update_stats(a, bytes, packets, drops, hw);
1486 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1489 static struct tc_action_ops act_ct_ops = {
1492 .owner = THIS_MODULE,
1494 .dump = tcf_ct_dump,
1495 .init = tcf_ct_init,
1496 .cleanup = tcf_ct_cleanup,
1497 .walk = tcf_ct_walker,
1498 .lookup = tcf_ct_search,
1499 .stats_update = tcf_stats_update,
1500 .size = sizeof(struct tcf_ct),
1503 static __net_init int ct_init_net(struct net *net)
1505 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
1506 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1508 if (nf_connlabels_get(net, n_bits - 1)) {
1510 pr_err("act_ct: Failed to set connlabels length");
1515 return tc_action_net_init(net, &tn->tn, &act_ct_ops);
1518 static void __net_exit ct_exit_net(struct list_head *net_list)
1523 list_for_each_entry(net, net_list, exit_list) {
1524 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1527 nf_connlabels_put(net);
1531 tc_action_net_exit(net_list, ct_net_id);
1534 static struct pernet_operations ct_net_ops = {
1535 .init = ct_init_net,
1536 .exit_batch = ct_exit_net,
1538 .size = sizeof(struct tc_ct_action_net),
1541 static int __init ct_init_module(void)
1545 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1549 err = tcf_ct_flow_tables_init();
1553 err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1560 tcf_ct_flow_tables_uninit();
1562 destroy_workqueue(act_ct_wq);
1566 static void __exit ct_cleanup_module(void)
1568 tcf_unregister_action(&act_ct_ops, &ct_net_ops);
1569 tcf_ct_flow_tables_uninit();
1570 destroy_workqueue(act_ct_wq);
1573 module_init(ct_init_module);
1574 module_exit(ct_cleanup_module);
1575 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1576 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1577 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1578 MODULE_DESCRIPTION("Connection tracking action");
1579 MODULE_LICENSE("GPL v2");