1
2
3
4
5
6
7
8
9
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>
16#include <linux/ip.h>
17#include <linux/ipv6.h>
18#include <linux/rhashtable.h>
19#include <linux/rh_flags.h>
20#include <net/netlink.h>
21#include <net/pkt_sched.h>
22#include <net/pkt_cls.h>
23#include <net/act_api.h>
24#include <net/ip.h>
25#include <net/ipv6_frag.h>
26#include <uapi/linux/tc_act/tc_ct.h>
27#include <net/tc_act/tc_ct.h>
28
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 <uapi/linux/netfilter/nf_nat.h>
37
38static struct workqueue_struct *act_ct_wq;
39static struct rhashtable zones_ht;
40static DEFINE_MUTEX(zones_mutex);
41
42struct tcf_ct_flow_table {
43 struct rhash_head node;
44
45 struct rcu_work rwork;
46 struct nf_flowtable nf_ft;
47 refcount_t ref;
48 u16 zone;
49
50 bool dying;
51};
52
53static 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,
58};
59
60static struct flow_action_entry *
61tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
62{
63 int i = flow_action->num_entries++;
64
65 return &flow_action->entries[i];
66}
67
68static void tcf_ct_add_mangle_action(struct flow_action *action,
69 enum flow_action_mangle_base htype,
70 u32 offset,
71 u32 mask,
72 u32 val)
73{
74 struct flow_action_entry *entry;
75
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;
82}
83
84
85
86
87
88static void
89tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
90 struct nf_conntrack_tuple target,
91 struct flow_action *action)
92{
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),
96 0xFFFFFFFF,
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),
101 0xFFFFFFFF,
102 be32_to_cpu(target.dst.u3.ip));
103}
104
105static void
106tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
107 union nf_inet_addr *addr,
108 u32 offset)
109{
110 int i;
111
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]));
116}
117
118static void
119tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
120 struct nf_conntrack_tuple target,
121 struct flow_action *action)
122{
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,
126 saddr));
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,
130 daddr));
131}
132
133static void
134tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
135 struct nf_conntrack_tuple target,
136 struct flow_action *action)
137{
138 __be16 target_src = target.src.u.tcp.port;
139 __be16 target_dst = target.dst.u.tcp.port;
140
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));
149}
150
151static void
152tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
153 struct nf_conntrack_tuple target,
154 struct flow_action *action)
155{
156 __be16 target_src = target.src.u.udp.port;
157 __be16 target_dst = target.dst.u.udp.port;
158
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));
167}
168
169static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
170 enum ip_conntrack_dir dir,
171 struct flow_action *action)
172{
173 struct nf_conn_labels *ct_labels;
174 struct flow_action_entry *entry;
175 enum ip_conntrack_info ctinfo;
176 u32 *act_ct_labels;
177
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;
182#endif
183 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
184 IP_CT_ESTABLISHED_REPLY;
185
186 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo;
187 entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL;
188
189 act_ct_labels = entry->ct_metadata.labels;
190 ct_labels = nf_ct_labels_find(ct);
191 if (ct_labels)
192 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE);
193 else
194 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE);
195}
196
197static int tcf_ct_flow_table_add_action_nat(struct net *net,
198 struct nf_conn *ct,
199 enum ip_conntrack_dir dir,
200 struct flow_action *action)
201{
202 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
203 struct nf_conntrack_tuple target;
204
205 if (!(ct->status & IPS_NAT_MASK))
206 return 0;
207
208 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
209
210 switch (tuple->src.l3num) {
211 case NFPROTO_IPV4:
212 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target,
213 action);
214 break;
215 case NFPROTO_IPV6:
216 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target,
217 action);
218 break;
219 default:
220 return -EOPNOTSUPP;
221 }
222
223 switch (nf_ct_protonum(ct)) {
224 case IPPROTO_TCP:
225 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action);
226 break;
227 case IPPROTO_UDP:
228 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action);
229 break;
230 default:
231 return -EOPNOTSUPP;
232 }
233
234 return 0;
235}
236
237static 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)
241{
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;
246 int i, err;
247
248 switch (tdir) {
249 case FLOW_OFFLOAD_DIR_ORIGINAL:
250 dir = IP_CT_DIR_ORIGINAL;
251 break;
252 case FLOW_OFFLOAD_DIR_REPLY:
253 dir = IP_CT_DIR_REPLY;
254 break;
255 default:
256 return -EOPNOTSUPP;
257 }
258
259 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
260 if (err)
261 goto err_nat;
262
263 tcf_ct_flow_table_add_action_meta(ct, dir, action);
264 return 0;
265
266err_nat:
267
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;
271
272 return err;
273}
274
275static struct nf_flowtable_type flowtable_ct = {
276 .action = tcf_ct_flow_table_fill_actions,
277 .owner = THIS_MODULE,
278};
279
280static int tcf_ct_flow_table_get(struct tcf_ct_params *params)
281{
282 struct tcf_ct_flow_table *ct_ft;
283 int err = -ENOMEM;
284
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))
288 goto out_unlock;
289
290 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL);
291 if (!ct_ft)
292 goto err_alloc;
293 refcount_set(&ct_ft->ref, 1);
294
295 ct_ft->zone = params->zone;
296 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
297 if (err)
298 goto err_insert;
299
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);
304 if (err)
305 goto err_init;
306
307 __module_get(THIS_MODULE);
308out_unlock:
309 params->ct_ft = ct_ft;
310 params->nf_ft = &ct_ft->nf_ft;
311 mutex_unlock(&zones_mutex);
312
313 return 0;
314
315err_init:
316 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
317err_insert:
318 kfree(ct_ft);
319err_alloc:
320 mutex_unlock(&zones_mutex);
321 return err;
322}
323
324static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
325{
326 struct flow_block_cb *block_cb, *tmp_cb;
327 struct tcf_ct_flow_table *ct_ft;
328 struct flow_block *block;
329
330 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
331 rwork);
332 nf_flow_table_free(&ct_ft->nf_ft);
333
334
335 block = &ct_ft->nf_ft.flow_block;
336 down_write(&ct_ft->nf_ft.flow_block_lock);
337 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) {
338 list_del(&block_cb->list);
339 flow_block_cb_free(block_cb);
340 }
341 up_write(&ct_ft->nf_ft.flow_block_lock);
342 kfree(ct_ft);
343
344 module_put(THIS_MODULE);
345}
346
347static void tcf_ct_flow_table_put(struct tcf_ct_params *params)
348{
349 struct tcf_ct_flow_table *ct_ft = params->ct_ft;
350
351 if (refcount_dec_and_test(¶ms->ct_ft->ref)) {
352 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
353 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
354 queue_rcu_work(act_ct_wq, &ct_ft->rwork);
355 }
356}
357
358static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
359 struct nf_conn *ct,
360 bool tcp)
361{
362 struct flow_offload *entry;
363 int err;
364
365 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
366 return;
367
368 entry = flow_offload_alloc(ct);
369 if (!entry) {
370 WARN_ON_ONCE(1);
371 goto err_alloc;
372 }
373
374 if (tcp) {
375 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
376 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
377 }
378
379 err = flow_offload_add(&ct_ft->nf_ft, entry);
380 if (err)
381 goto err_add;
382
383 return;
384
385err_add:
386 flow_offload_free(entry);
387err_alloc:
388 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
389}
390
391static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
392 struct nf_conn *ct,
393 enum ip_conntrack_info ctinfo)
394{
395 bool tcp = false;
396
397 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
398 return;
399
400 switch (nf_ct_protonum(ct)) {
401 case IPPROTO_TCP:
402 tcp = true;
403 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
404 return;
405 break;
406 case IPPROTO_UDP:
407 break;
408 default:
409 return;
410 }
411
412 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
413 ct->status & IPS_SEQ_ADJUST)
414 return;
415
416 tcf_ct_flow_table_add(ct_ft, ct, tcp);
417}
418
419static bool
420tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
421 struct flow_offload_tuple *tuple,
422 struct tcphdr **tcph)
423{
424 struct flow_ports *ports;
425 unsigned int thoff;
426 struct iphdr *iph;
427
428 if (!pskb_network_may_pull(skb, sizeof(*iph)))
429 return false;
430
431 iph = ip_hdr(skb);
432 thoff = iph->ihl * 4;
433
434 if (ip_is_fragment(iph) ||
435 unlikely(thoff != sizeof(struct iphdr)))
436 return false;
437
438 if (iph->protocol != IPPROTO_TCP &&
439 iph->protocol != IPPROTO_UDP)
440 return false;
441
442 if (iph->ttl <= 1)
443 return false;
444
445 if (!pskb_network_may_pull(skb, iph->protocol == IPPROTO_TCP ?
446 thoff + sizeof(struct tcphdr) :
447 thoff + sizeof(*ports)))
448 return false;
449
450 iph = ip_hdr(skb);
451 if (iph->protocol == IPPROTO_TCP)
452 *tcph = (void *)(skb_network_header(skb) + thoff);
453
454 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
455 tuple->src_v4.s_addr = iph->saddr;
456 tuple->dst_v4.s_addr = iph->daddr;
457 tuple->src_port = ports->source;
458 tuple->dst_port = ports->dest;
459 tuple->l3proto = AF_INET;
460 tuple->l4proto = iph->protocol;
461
462 return true;
463}
464
465static bool
466tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
467 struct flow_offload_tuple *tuple,
468 struct tcphdr **tcph)
469{
470 struct flow_ports *ports;
471 struct ipv6hdr *ip6h;
472 unsigned int thoff;
473
474 if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
475 return false;
476
477 ip6h = ipv6_hdr(skb);
478
479 if (ip6h->nexthdr != IPPROTO_TCP &&
480 ip6h->nexthdr != IPPROTO_UDP)
481 return false;
482
483 if (ip6h->hop_limit <= 1)
484 return false;
485
486 thoff = sizeof(*ip6h);
487 if (!pskb_network_may_pull(skb, ip6h->nexthdr == IPPROTO_TCP ?
488 thoff + sizeof(struct tcphdr) :
489 thoff + sizeof(*ports)))
490 return false;
491
492 ip6h = ipv6_hdr(skb);
493 if (ip6h->nexthdr == IPPROTO_TCP)
494 *tcph = (void *)(skb_network_header(skb) + thoff);
495
496 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
497 tuple->src_v6 = ip6h->saddr;
498 tuple->dst_v6 = ip6h->daddr;
499 tuple->src_port = ports->source;
500 tuple->dst_port = ports->dest;
501 tuple->l3proto = AF_INET6;
502 tuple->l4proto = ip6h->nexthdr;
503
504 return true;
505}
506
507static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
508 struct sk_buff *skb,
509 u8 family)
510{
511 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
512 struct flow_offload_tuple_rhash *tuplehash;
513 struct flow_offload_tuple tuple = {};
514 enum ip_conntrack_info ctinfo;
515 struct tcphdr *tcph = NULL;
516 struct flow_offload *flow;
517 struct nf_conn *ct;
518 u8 dir;
519
520
521 ct = nf_ct_get(skb, &ctinfo);
522 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
523 return false;
524
525 switch (family) {
526 case NFPROTO_IPV4:
527 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
528 return false;
529 break;
530 case NFPROTO_IPV6:
531 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
532 return false;
533 break;
534 default:
535 return false;
536 }
537
538 tuplehash = flow_offload_lookup(nf_ft, &tuple);
539 if (!tuplehash)
540 return false;
541
542 dir = tuplehash->tuple.dir;
543 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
544 ct = flow->ct;
545
546 if (tcph && (unlikely(tcph->fin || tcph->rst))) {
547 flow_offload_teardown(flow);
548 return false;
549 }
550
551 ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
552 IP_CT_ESTABLISHED_REPLY;
553
554 flow_offload_refresh(nf_ft, flow);
555 nf_conntrack_get(&ct->ct_general);
556 nf_ct_set(skb, ct, ctinfo);
557 if (nf_ft->flags & NF_FLOWTABLE_COUNTER)
558 nf_ct_acct_update(ct, dir, skb->len);
559
560 return true;
561}
562
563static int tcf_ct_flow_tables_init(void)
564{
565 return rhashtable_init(&zones_ht, &zones_params);
566}
567
568static void tcf_ct_flow_tables_uninit(void)
569{
570 rhashtable_destroy(&zones_ht);
571}
572
573static struct tc_action_ops act_ct_ops;
574static unsigned int ct_net_id;
575
576struct tc_ct_action_net {
577 struct tc_action_net tn;
578 bool labels;
579};
580
581
582static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
583 u16 zone_id, bool force)
584{
585 enum ip_conntrack_info ctinfo;
586 struct nf_conn *ct;
587
588 ct = nf_ct_get(skb, &ctinfo);
589 if (!ct)
590 return false;
591 if (!net_eq(net, read_pnet(&ct->ct_net)))
592 return false;
593 if (nf_ct_zone(ct)->id != zone_id)
594 return false;
595
596
597 if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
598 if (nf_ct_is_confirmed(ct))
599 nf_ct_kill(ct);
600
601 nf_conntrack_put(&ct->ct_general);
602 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
603
604 return false;
605 }
606
607 return true;
608}
609
610
611
612
613
614
615
616static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family)
617{
618 unsigned int len;
619 int err;
620
621 switch (family) {
622 case NFPROTO_IPV4:
623 len = ntohs(ip_hdr(skb)->tot_len);
624 break;
625 case NFPROTO_IPV6:
626 len = sizeof(struct ipv6hdr)
627 + ntohs(ipv6_hdr(skb)->payload_len);
628 break;
629 default:
630 len = skb->len;
631 }
632
633 err = pskb_trim_rcsum(skb, len);
634
635 return err;
636}
637
638static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
639{
640 u8 family = NFPROTO_UNSPEC;
641
642 switch (skb_protocol(skb, true)) {
643 case htons(ETH_P_IP):
644 family = NFPROTO_IPV4;
645 break;
646 case htons(ETH_P_IPV6):
647 family = NFPROTO_IPV6;
648 break;
649 default:
650 break;
651 }
652
653 return family;
654}
655
656static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
657{
658 unsigned int len;
659
660 len = skb_network_offset(skb) + sizeof(struct iphdr);
661 if (unlikely(skb->len < len))
662 return -EINVAL;
663 if (unlikely(!pskb_may_pull(skb, len)))
664 return -ENOMEM;
665
666 *frag = ip_is_fragment(ip_hdr(skb));
667 return 0;
668}
669
670static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
671{
672 unsigned int flags = 0, len, payload_ofs = 0;
673 unsigned short frag_off;
674 int nexthdr;
675
676 len = skb_network_offset(skb) + sizeof(struct ipv6hdr);
677 if (unlikely(skb->len < len))
678 return -EINVAL;
679 if (unlikely(!pskb_may_pull(skb, len)))
680 return -ENOMEM;
681
682 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
683 if (unlikely(nexthdr < 0))
684 return -EPROTO;
685
686 *frag = flags & IP6_FH_F_FRAG;
687 return 0;
688}
689
690static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
691 u8 family, u16 zone, bool *defrag)
692{
693 enum ip_conntrack_info ctinfo;
694 struct nf_conn *ct;
695 int err = 0;
696 bool frag;
697 u16 mru;
698
699
700 ct = nf_ct_get(skb, &ctinfo);
701 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
702 return 0;
703
704 if (family == NFPROTO_IPV4)
705 err = tcf_ct_ipv4_is_fragment(skb, &frag);
706 else
707 err = tcf_ct_ipv6_is_fragment(skb, &frag);
708 if (err || !frag)
709 return err;
710
711 skb_get(skb);
712 mru = tc_skb_cb(skb)->mru;
713
714 if (family == NFPROTO_IPV4) {
715 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
716
717 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
718 local_bh_disable();
719 err = ip_defrag(net, skb, user);
720 local_bh_enable();
721 if (err && err != -EINPROGRESS)
722 return err;
723
724 if (!err) {
725 *defrag = true;
726 mru = IPCB(skb)->frag_max_size;
727 }
728 } else {
729#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
730 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
731
732 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
733 err = nf_ct_frag6_gather(net, skb, user);
734 if (err && err != -EINPROGRESS)
735 goto out_free;
736
737 if (!err) {
738 *defrag = true;
739 mru = IP6CB(skb)->frag_max_size;
740 }
741#else
742 err = -EOPNOTSUPP;
743 goto out_free;
744#endif
745 }
746
747 if (err != -EINPROGRESS)
748 tc_skb_cb(skb)->mru = mru;
749 skb_clear_hash(skb);
750 skb->ignore_df = 1;
751 return err;
752
753out_free:
754 kfree_skb(skb);
755 return err;
756}
757
758static void tcf_ct_params_free(struct rcu_head *head)
759{
760 struct tcf_ct_params *params = container_of(head,
761 struct tcf_ct_params, rcu);
762
763 tcf_ct_flow_table_put(params);
764
765 if (params->tmpl)
766 nf_conntrack_put(¶ms->tmpl->ct_general);
767 kfree(params);
768}
769
770#if IS_ENABLED(CONFIG_NF_NAT)
771
772
773
774
775static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
776 enum ip_conntrack_info ctinfo,
777 const struct nf_nat_range2 *range,
778 enum nf_nat_manip_type maniptype)
779{
780 __be16 proto = skb_protocol(skb, true);
781 int hooknum, err = NF_ACCEPT;
782
783
784 if (maniptype == NF_NAT_MANIP_SRC)
785 hooknum = NF_INET_LOCAL_IN;
786 else
787 hooknum = NF_INET_LOCAL_OUT;
788
789 switch (ctinfo) {
790 case IP_CT_RELATED:
791 case IP_CT_RELATED_REPLY:
792 if (proto == htons(ETH_P_IP) &&
793 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
794 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
795 hooknum))
796 err = NF_DROP;
797 goto out;
798 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) {
799 __be16 frag_off;
800 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
801 int hdrlen = ipv6_skip_exthdr(skb,
802 sizeof(struct ipv6hdr),
803 &nexthdr, &frag_off);
804
805 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
806 if (!nf_nat_icmpv6_reply_translation(skb, ct,
807 ctinfo,
808 hooknum,
809 hdrlen))
810 err = NF_DROP;
811 goto out;
812 }
813 }
814
815 fallthrough;
816 case IP_CT_NEW:
817
818
819
820 if (!nf_nat_initialized(ct, maniptype)) {
821
822 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
823
824
825
826 ? nf_nat_setup_info(ct, range, maniptype)
827 : nf_nat_alloc_null_binding(ct, hooknum);
828 if (err != NF_ACCEPT)
829 goto out;
830 }
831 break;
832
833 case IP_CT_ESTABLISHED:
834 case IP_CT_ESTABLISHED_REPLY:
835 break;
836
837 default:
838 err = NF_DROP;
839 goto out;
840 }
841
842 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
843 if (err == NF_ACCEPT) {
844 if (maniptype == NF_NAT_MANIP_SRC)
845 tc_skb_cb(skb)->post_ct_snat = 1;
846 if (maniptype == NF_NAT_MANIP_DST)
847 tc_skb_cb(skb)->post_ct_dnat = 1;
848 }
849out:
850 return err;
851}
852#endif
853
854static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
855{
856#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
857 u32 new_mark;
858
859 if (!mask)
860 return;
861
862 new_mark = mark | (ct->mark & ~(mask));
863 if (ct->mark != new_mark) {
864 ct->mark = new_mark;
865 if (nf_ct_is_confirmed(ct))
866 nf_conntrack_event_cache(IPCT_MARK, ct);
867 }
868#endif
869}
870
871static void tcf_ct_act_set_labels(struct nf_conn *ct,
872 u32 *labels,
873 u32 *labels_m)
874{
875#if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
876 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
877
878 if (!memchr_inv(labels_m, 0, labels_sz))
879 return;
880
881 nf_connlabels_replace(ct, labels, labels_m, 4);
882#endif
883}
884
885static int tcf_ct_act_nat(struct sk_buff *skb,
886 struct nf_conn *ct,
887 enum ip_conntrack_info ctinfo,
888 int ct_action,
889 struct nf_nat_range2 *range,
890 bool commit)
891{
892#if IS_ENABLED(CONFIG_NF_NAT)
893 int err;
894 enum nf_nat_manip_type maniptype;
895
896 if (!(ct_action & TCA_CT_ACT_NAT))
897 return NF_ACCEPT;
898
899
900 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
901 return NF_DROP;
902
903 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) &&
904 (ctinfo != IP_CT_RELATED || commit)) {
905
906 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
907
908
909
910
911 maniptype = ct->status & IPS_SRC_NAT
912 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
913 else
914 maniptype = ct->status & IPS_SRC_NAT
915 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
916 } else if (ct_action & TCA_CT_ACT_NAT_SRC) {
917 maniptype = NF_NAT_MANIP_SRC;
918 } else if (ct_action & TCA_CT_ACT_NAT_DST) {
919 maniptype = NF_NAT_MANIP_DST;
920 } else {
921 return NF_ACCEPT;
922 }
923
924 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
925 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
926 if (ct->status & IPS_SRC_NAT) {
927 if (maniptype == NF_NAT_MANIP_SRC)
928 maniptype = NF_NAT_MANIP_DST;
929 else
930 maniptype = NF_NAT_MANIP_SRC;
931
932 err = ct_nat_execute(skb, ct, ctinfo, range,
933 maniptype);
934 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
935 err = ct_nat_execute(skb, ct, ctinfo, NULL,
936 NF_NAT_MANIP_SRC);
937 }
938 }
939 return err;
940#else
941 return NF_ACCEPT;
942#endif
943}
944
945static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
946 struct tcf_result *res)
947{
948 struct net *net = dev_net(skb->dev);
949 bool cached, commit, clear, force;
950 enum ip_conntrack_info ctinfo;
951 struct tcf_ct *c = to_ct(a);
952 struct nf_conn *tmpl = NULL;
953 struct nf_hook_state state;
954 int nh_ofs, err, retval;
955 struct tcf_ct_params *p;
956 bool skip_add = false;
957 bool defrag = false;
958 struct nf_conn *ct;
959 u8 family;
960
961 p = rcu_dereference_bh(c->params);
962
963 retval = READ_ONCE(c->tcf_action);
964 commit = p->ct_action & TCA_CT_ACT_COMMIT;
965 clear = p->ct_action & TCA_CT_ACT_CLEAR;
966 force = p->ct_action & TCA_CT_ACT_FORCE;
967 tmpl = p->tmpl;
968
969 tcf_lastuse_update(&c->tcf_tm);
970 tcf_action_update_bstats(&c->common, skb);
971
972 if (clear) {
973 tc_skb_cb(skb)->post_ct = false;
974 ct = nf_ct_get(skb, &ctinfo);
975 if (ct) {
976 nf_conntrack_put(&ct->ct_general);
977 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
978 }
979
980 goto out_clear;
981 }
982
983 family = tcf_ct_skb_nf_family(skb);
984 if (family == NFPROTO_UNSPEC)
985 goto drop;
986
987
988
989
990 nh_ofs = skb_network_offset(skb);
991 skb_pull_rcsum(skb, nh_ofs);
992 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag);
993 if (err == -EINPROGRESS) {
994 retval = TC_ACT_STOLEN;
995 goto out_clear;
996 }
997 if (err)
998 goto drop;
999
1000 err = tcf_ct_skb_network_trim(skb, family);
1001 if (err)
1002 goto drop;
1003
1004
1005
1006
1007
1008
1009 cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force);
1010 if (!cached) {
1011 if (tcf_ct_flow_table_lookup(p, skb, family)) {
1012 skip_add = true;
1013 goto do_nat;
1014 }
1015
1016
1017 if (tmpl) {
1018 nf_conntrack_put(skb_nfct(skb));
1019 nf_conntrack_get(&tmpl->ct_general);
1020 nf_ct_set(skb, tmpl, IP_CT_NEW);
1021 }
1022
1023 state.hook = NF_INET_PRE_ROUTING;
1024 state.net = net;
1025 state.pf = family;
1026 err = nf_conntrack_in(skb, &state);
1027 if (err != NF_ACCEPT)
1028 goto out_push;
1029 }
1030
1031do_nat:
1032 ct = nf_ct_get(skb, &ctinfo);
1033 if (!ct)
1034 goto out_push;
1035 nf_ct_deliver_cached_events(ct);
1036
1037 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1038 if (err != NF_ACCEPT)
1039 goto drop;
1040
1041 if (commit) {
1042 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1043 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1044
1045
1046
1047
1048 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1049 goto drop;
1050 }
1051
1052 if (!skip_add)
1053 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
1054
1055out_push:
1056 skb_push_rcsum(skb, nh_ofs);
1057
1058 tc_skb_cb(skb)->post_ct = true;
1059 tc_skb_cb(skb)->zone = p->zone;
1060out_clear:
1061 if (defrag)
1062 qdisc_skb_cb(skb)->pkt_len = skb->len;
1063 return retval;
1064
1065drop:
1066 tcf_action_inc_drop_qstats(&c->common);
1067 return TC_ACT_SHOT;
1068}
1069
1070static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
1071 [TCA_CT_ACTION] = { .type = NLA_U16 },
1072 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
1073 [TCA_CT_ZONE] = { .type = NLA_U16 },
1074 [TCA_CT_MARK] = { .type = NLA_U32 },
1075 [TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1076 [TCA_CT_LABELS] = { .type = NLA_BINARY,
1077 .len = 128 / BITS_PER_BYTE },
1078 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1079 .len = 128 / BITS_PER_BYTE },
1080 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1081 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
1082 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1083 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1084 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1085 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1086};
1087
1088static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1089 struct tc_ct *parm,
1090 struct nlattr **tb,
1091 struct netlink_ext_ack *extack)
1092{
1093 struct nf_nat_range2 *range;
1094
1095 if (!(p->ct_action & TCA_CT_ACT_NAT))
1096 return 0;
1097
1098 if (!IS_ENABLED(CONFIG_NF_NAT)) {
1099 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1100 return -EOPNOTSUPP;
1101 }
1102
1103 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1104 return 0;
1105
1106 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1107 (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1108 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1109 return -EOPNOTSUPP;
1110 }
1111
1112 range = &p->range;
1113 if (tb[TCA_CT_NAT_IPV4_MIN]) {
1114 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1115
1116 p->ipv4_range = true;
1117 range->flags |= NF_NAT_RANGE_MAP_IPS;
1118 range->min_addr.ip =
1119 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1120
1121 range->max_addr.ip = max_attr ?
1122 nla_get_in_addr(max_attr) :
1123 range->min_addr.ip;
1124 } else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1125 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1126
1127 p->ipv4_range = false;
1128 range->flags |= NF_NAT_RANGE_MAP_IPS;
1129 range->min_addr.in6 =
1130 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1131
1132 range->max_addr.in6 = max_attr ?
1133 nla_get_in6_addr(max_attr) :
1134 range->min_addr.in6;
1135 }
1136
1137 if (tb[TCA_CT_NAT_PORT_MIN]) {
1138 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1139 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1140
1141 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1142 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1143 range->min_proto.all;
1144 }
1145
1146 return 0;
1147}
1148
1149static void tcf_ct_set_key_val(struct nlattr **tb,
1150 void *val, int val_type,
1151 void *mask, int mask_type,
1152 int len)
1153{
1154 if (!tb[val_type])
1155 return;
1156 nla_memcpy(val, tb[val_type], len);
1157
1158 if (!mask)
1159 return;
1160
1161 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1162 memset(mask, 0xff, len);
1163 else
1164 nla_memcpy(mask, tb[mask_type], len);
1165}
1166
1167static int tcf_ct_fill_params(struct net *net,
1168 struct tcf_ct_params *p,
1169 struct tc_ct *parm,
1170 struct nlattr **tb,
1171 struct netlink_ext_ack *extack)
1172{
1173 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1174 struct nf_conntrack_zone zone;
1175 struct nf_conn *tmpl;
1176 int err;
1177
1178 p->zone = NF_CT_DEFAULT_ZONE_ID;
1179
1180 tcf_ct_set_key_val(tb,
1181 &p->ct_action, TCA_CT_ACTION,
1182 NULL, TCA_CT_UNSPEC,
1183 sizeof(p->ct_action));
1184
1185 if (p->ct_action & TCA_CT_ACT_CLEAR)
1186 return 0;
1187
1188 err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1189 if (err)
1190 return err;
1191
1192 if (tb[TCA_CT_MARK]) {
1193 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1194 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1195 return -EOPNOTSUPP;
1196 }
1197 tcf_ct_set_key_val(tb,
1198 &p->mark, TCA_CT_MARK,
1199 &p->mark_mask, TCA_CT_MARK_MASK,
1200 sizeof(p->mark));
1201 }
1202
1203 if (tb[TCA_CT_LABELS]) {
1204 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1205 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1206 return -EOPNOTSUPP;
1207 }
1208
1209 if (!tn->labels) {
1210 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1211 return -EOPNOTSUPP;
1212 }
1213 tcf_ct_set_key_val(tb,
1214 p->labels, TCA_CT_LABELS,
1215 p->labels_mask, TCA_CT_LABELS_MASK,
1216 sizeof(p->labels));
1217 }
1218
1219 if (tb[TCA_CT_ZONE]) {
1220 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1221 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1222 return -EOPNOTSUPP;
1223 }
1224
1225 tcf_ct_set_key_val(tb,
1226 &p->zone, TCA_CT_ZONE,
1227 NULL, TCA_CT_UNSPEC,
1228 sizeof(p->zone));
1229 }
1230
1231 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1232 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1233 if (!tmpl) {
1234 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1235 return -ENOMEM;
1236 }
1237 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1238 nf_conntrack_get(&tmpl->ct_general);
1239 p->tmpl = tmpl;
1240
1241 return 0;
1242}
1243
1244static int tcf_ct_init(struct net *net, struct nlattr *nla,
1245 struct nlattr *est, struct tc_action **a,
1246 int replace, int bind, bool rtnl_held,
1247 struct tcf_proto *tp, u32 flags,
1248 struct netlink_ext_ack *extack)
1249{
1250 struct tc_action_net *tn = net_generic(net, ct_net_id);
1251 struct tcf_ct_params *params = NULL;
1252 struct nlattr *tb[TCA_CT_MAX + 1];
1253 struct tcf_chain *goto_ch = NULL;
1254 struct tc_ct *parm;
1255 struct tcf_ct *c;
1256 int err, res = 0;
1257 u32 index;
1258
1259 if (!nla) {
1260 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1261 return -EINVAL;
1262 }
1263
1264 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1265 if (err < 0)
1266 return err;
1267
1268 if (!tb[TCA_CT_PARMS]) {
1269 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1270 return -EINVAL;
1271 }
1272 parm = nla_data(tb[TCA_CT_PARMS]);
1273 index = parm->index;
1274 err = tcf_idr_check_alloc(tn, &index, a, bind);
1275 if (err < 0)
1276 return err;
1277
1278 if (!err) {
1279 err = tcf_idr_create_from_flags(tn, index, est, a,
1280 &act_ct_ops, bind, flags);
1281 if (err) {
1282 tcf_idr_cleanup(tn, index);
1283 return err;
1284 }
1285 res = ACT_P_CREATED;
1286 } else {
1287 if (bind)
1288 return 0;
1289
1290 if (!replace) {
1291 tcf_idr_release(*a, bind);
1292 return -EEXIST;
1293 }
1294 }
1295 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1296 if (err < 0)
1297 goto cleanup;
1298
1299 c = to_ct(*a);
1300
1301 params = kzalloc(sizeof(*params), GFP_KERNEL);
1302 if (unlikely(!params)) {
1303 err = -ENOMEM;
1304 goto cleanup;
1305 }
1306
1307 err = tcf_ct_fill_params(net, params, parm, tb, extack);
1308 if (err)
1309 goto cleanup;
1310
1311 err = tcf_ct_flow_table_get(params);
1312 if (err)
1313 goto cleanup;
1314
1315 spin_lock_bh(&c->tcf_lock);
1316 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1317 params = rcu_replace_pointer(c->params, params,
1318 lockdep_is_held(&c->tcf_lock));
1319 spin_unlock_bh(&c->tcf_lock);
1320
1321 if (goto_ch)
1322 tcf_chain_put_by_act(goto_ch);
1323 if (params)
1324 call_rcu(¶ms->rcu, tcf_ct_params_free);
1325
1326 return res;
1327
1328cleanup:
1329 if (goto_ch)
1330 tcf_chain_put_by_act(goto_ch);
1331 kfree(params);
1332 tcf_idr_release(*a, bind);
1333 return err;
1334}
1335
1336static void tcf_ct_cleanup(struct tc_action *a)
1337{
1338 struct tcf_ct_params *params;
1339 struct tcf_ct *c = to_ct(a);
1340
1341 params = rcu_dereference_protected(c->params, 1);
1342 if (params)
1343 call_rcu(¶ms->rcu, tcf_ct_params_free);
1344}
1345
1346static int tcf_ct_dump_key_val(struct sk_buff *skb,
1347 void *val, int val_type,
1348 void *mask, int mask_type,
1349 int len)
1350{
1351 int err;
1352
1353 if (mask && !memchr_inv(mask, 0, len))
1354 return 0;
1355
1356 err = nla_put(skb, val_type, len, val);
1357 if (err)
1358 return err;
1359
1360 if (mask_type != TCA_CT_UNSPEC) {
1361 err = nla_put(skb, mask_type, len, mask);
1362 if (err)
1363 return err;
1364 }
1365
1366 return 0;
1367}
1368
1369static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p)
1370{
1371 struct nf_nat_range2 *range = &p->range;
1372
1373 if (!(p->ct_action & TCA_CT_ACT_NAT))
1374 return 0;
1375
1376 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1377 return 0;
1378
1379 if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1380 if (p->ipv4_range) {
1381 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1382 range->min_addr.ip))
1383 return -1;
1384 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1385 range->max_addr.ip))
1386 return -1;
1387 } else {
1388 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1389 &range->min_addr.in6))
1390 return -1;
1391 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1392 &range->max_addr.in6))
1393 return -1;
1394 }
1395 }
1396
1397 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1398 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1399 range->min_proto.all))
1400 return -1;
1401 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1402 range->max_proto.all))
1403 return -1;
1404 }
1405
1406 return 0;
1407}
1408
1409static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1410 int bind, int ref)
1411{
1412 unsigned char *b = skb_tail_pointer(skb);
1413 struct tcf_ct *c = to_ct(a);
1414 struct tcf_ct_params *p;
1415
1416 struct tc_ct opt = {
1417 .index = c->tcf_index,
1418 .refcnt = refcount_read(&c->tcf_refcnt) - ref,
1419 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1420 };
1421 struct tcf_t t;
1422
1423 spin_lock_bh(&c->tcf_lock);
1424 p = rcu_dereference_protected(c->params,
1425 lockdep_is_held(&c->tcf_lock));
1426 opt.action = c->tcf_action;
1427
1428 if (tcf_ct_dump_key_val(skb,
1429 &p->ct_action, TCA_CT_ACTION,
1430 NULL, TCA_CT_UNSPEC,
1431 sizeof(p->ct_action)))
1432 goto nla_put_failure;
1433
1434 if (p->ct_action & TCA_CT_ACT_CLEAR)
1435 goto skip_dump;
1436
1437 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1438 tcf_ct_dump_key_val(skb,
1439 &p->mark, TCA_CT_MARK,
1440 &p->mark_mask, TCA_CT_MARK_MASK,
1441 sizeof(p->mark)))
1442 goto nla_put_failure;
1443
1444 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1445 tcf_ct_dump_key_val(skb,
1446 p->labels, TCA_CT_LABELS,
1447 p->labels_mask, TCA_CT_LABELS_MASK,
1448 sizeof(p->labels)))
1449 goto nla_put_failure;
1450
1451 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1452 tcf_ct_dump_key_val(skb,
1453 &p->zone, TCA_CT_ZONE,
1454 NULL, TCA_CT_UNSPEC,
1455 sizeof(p->zone)))
1456 goto nla_put_failure;
1457
1458 if (tcf_ct_dump_nat(skb, p))
1459 goto nla_put_failure;
1460
1461skip_dump:
1462 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1463 goto nla_put_failure;
1464
1465 tcf_tm_dump(&t, &c->tcf_tm);
1466 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1467 goto nla_put_failure;
1468 spin_unlock_bh(&c->tcf_lock);
1469
1470 return skb->len;
1471nla_put_failure:
1472 spin_unlock_bh(&c->tcf_lock);
1473 nlmsg_trim(skb, b);
1474 return -1;
1475}
1476
1477static int tcf_ct_walker(struct net *net, struct sk_buff *skb,
1478 struct netlink_callback *cb, int type,
1479 const struct tc_action_ops *ops,
1480 struct netlink_ext_ack *extack)
1481{
1482 struct tc_action_net *tn = net_generic(net, ct_net_id);
1483
1484 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
1485}
1486
1487static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index)
1488{
1489 struct tc_action_net *tn = net_generic(net, ct_net_id);
1490
1491 return tcf_idr_search(tn, a, index);
1492}
1493
1494static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1495 u64 drops, u64 lastuse, bool hw)
1496{
1497 struct tcf_ct *c = to_ct(a);
1498
1499 tcf_action_update_stats(a, bytes, packets, drops, hw);
1500 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1501}
1502
1503static struct tc_action_ops act_ct_ops = {
1504 .kind = "ct",
1505 .id = TCA_ID_CT,
1506 .owner = THIS_MODULE,
1507 .act = tcf_ct_act,
1508 .dump = tcf_ct_dump,
1509 .init = tcf_ct_init,
1510 .cleanup = tcf_ct_cleanup,
1511 .walk = tcf_ct_walker,
1512 .lookup = tcf_ct_search,
1513 .stats_update = tcf_stats_update,
1514 .size = sizeof(struct tcf_ct),
1515};
1516
1517static __net_init int ct_init_net(struct net *net)
1518{
1519 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
1520 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1521
1522 if (nf_connlabels_get(net, n_bits - 1)) {
1523 tn->labels = false;
1524 pr_err("act_ct: Failed to set connlabels length");
1525 } else {
1526 tn->labels = true;
1527 }
1528
1529 return tc_action_net_init(net, &tn->tn, &act_ct_ops);
1530}
1531
1532static void __net_exit ct_exit_net(struct list_head *net_list)
1533{
1534 struct net *net;
1535
1536 rtnl_lock();
1537 list_for_each_entry(net, net_list, exit_list) {
1538 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1539
1540 if (tn->labels)
1541 nf_connlabels_put(net);
1542 }
1543 rtnl_unlock();
1544
1545 tc_action_net_exit(net_list, ct_net_id);
1546}
1547
1548static struct pernet_operations ct_net_ops = {
1549 .init = ct_init_net,
1550 .exit_batch = ct_exit_net,
1551 .id = &ct_net_id,
1552 .size = sizeof(struct tc_ct_action_net),
1553};
1554
1555static int __init ct_init_module(void)
1556{
1557 int err;
1558
1559 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1560 if (!act_ct_wq)
1561 return -ENOMEM;
1562
1563 err = tcf_ct_flow_tables_init();
1564 if (err)
1565 goto err_tbl_init;
1566
1567 err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1568 if (err)
1569 goto err_register;
1570
1571 static_branch_inc(&tcf_frag_xmit_count);
1572
1573 return 0;
1574
1575err_register:
1576 tcf_ct_flow_tables_uninit();
1577err_tbl_init:
1578 destroy_workqueue(act_ct_wq);
1579 return err;
1580}
1581
1582static void __exit ct_cleanup_module(void)
1583{
1584 static_branch_dec(&tcf_frag_xmit_count);
1585 tcf_unregister_action(&act_ct_ops, &ct_net_ops);
1586 tcf_ct_flow_tables_uninit();
1587 destroy_workqueue(act_ct_wq);
1588}
1589
1590module_init(ct_init_module);
1591module_exit(ct_cleanup_module);
1592MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1593MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1594MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1595MODULE_DESCRIPTION("Connection tracking action");
1596MODULE_LICENSE("GPL v2");
1597