1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/openvswitch.h>
16#include <linux/tcp.h>
17#include <linux/udp.h>
18#include <linux/sctp.h>
19#include <linux/static_key.h>
20#include <net/ip.h>
21#include <net/genetlink.h>
22#include <net/netfilter/nf_conntrack_core.h>
23#include <net/netfilter/nf_conntrack_count.h>
24#include <net/netfilter/nf_conntrack_helper.h>
25#include <net/netfilter/nf_conntrack_labels.h>
26#include <net/netfilter/nf_conntrack_seqadj.h>
27#include <net/netfilter/nf_conntrack_timeout.h>
28#include <net/netfilter/nf_conntrack_zones.h>
29#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
30#include <net/ipv6_frag.h>
31
32#ifdef CONFIG_NF_NAT_NEEDED
33#include <net/netfilter/nf_nat.h>
34#endif
35
36#include "datapath.h"
37#include "conntrack.h"
38#include "flow.h"
39#include "flow_netlink.h"
40
41struct ovs_ct_len_tbl {
42 int maxlen;
43 int minlen;
44};
45
46
47struct md_mark {
48 u32 value;
49 u32 mask;
50};
51
52
53struct md_labels {
54 struct ovs_key_ct_labels value;
55 struct ovs_key_ct_labels mask;
56};
57
58enum ovs_ct_nat {
59 OVS_CT_NAT = 1 << 0,
60 OVS_CT_SRC_NAT = 1 << 1,
61 OVS_CT_DST_NAT = 1 << 2,
62};
63
64
65struct ovs_conntrack_info {
66 struct nf_conntrack_helper *helper;
67 struct nf_conntrack_zone zone;
68 struct nf_conn *ct;
69 u8 commit : 1;
70 u8 nat : 3;
71 u8 force : 1;
72 u8 have_eventmask : 1;
73 u16 family;
74 u32 eventmask;
75 struct md_mark mark;
76 struct md_labels labels;
77 char timeout[CTNL_TIMEOUT_NAME_MAX];
78 struct nf_ct_timeout *nf_ct_timeout;
79#ifdef CONFIG_NF_NAT_NEEDED
80 struct nf_nat_range2 range;
81#endif
82};
83
84#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
85#define OVS_CT_LIMIT_UNLIMITED 0
86#define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
87#define CT_LIMIT_HASH_BUCKETS 512
88static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled);
89
90struct ovs_ct_limit {
91
92 struct hlist_node hlist_node;
93 struct rcu_head rcu;
94 u16 zone;
95 u32 limit;
96};
97
98struct ovs_ct_limit_info {
99 u32 default_limit;
100 struct hlist_head *limits;
101 struct nf_conncount_data *data;
102};
103
104static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = {
105 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, },
106};
107#endif
108
109static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
110
111static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
112
113static u16 key_to_nfproto(const struct sw_flow_key *key)
114{
115 switch (ntohs(key->eth.type)) {
116 case ETH_P_IP:
117 return NFPROTO_IPV4;
118 case ETH_P_IPV6:
119 return NFPROTO_IPV6;
120 default:
121 return NFPROTO_UNSPEC;
122 }
123}
124
125
126static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
127{
128 u8 ct_state = OVS_CS_F_TRACKED;
129
130 switch (ctinfo) {
131 case IP_CT_ESTABLISHED_REPLY:
132 case IP_CT_RELATED_REPLY:
133 ct_state |= OVS_CS_F_REPLY_DIR;
134 break;
135 default:
136 break;
137 }
138
139 switch (ctinfo) {
140 case IP_CT_ESTABLISHED:
141 case IP_CT_ESTABLISHED_REPLY:
142 ct_state |= OVS_CS_F_ESTABLISHED;
143 break;
144 case IP_CT_RELATED:
145 case IP_CT_RELATED_REPLY:
146 ct_state |= OVS_CS_F_RELATED;
147 break;
148 case IP_CT_NEW:
149 ct_state |= OVS_CS_F_NEW;
150 break;
151 default:
152 break;
153 }
154
155 return ct_state;
156}
157
158static u32 ovs_ct_get_mark(const struct nf_conn *ct)
159{
160#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
161 return ct ? ct->mark : 0;
162#else
163 return 0;
164#endif
165}
166
167
168#if NF_CT_LABELS_MAX_SIZE < 16
169#error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
170#endif
171
172static void ovs_ct_get_labels(const struct nf_conn *ct,
173 struct ovs_key_ct_labels *labels)
174{
175 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
176
177 if (cl)
178 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
179 else
180 memset(labels, 0, OVS_CT_LABELS_LEN);
181}
182
183static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
184 const struct nf_conntrack_tuple *orig,
185 u8 icmp_proto)
186{
187 key->ct_orig_proto = orig->dst.protonum;
188 if (orig->dst.protonum == icmp_proto) {
189 key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
190 key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
191 } else {
192 key->ct.orig_tp.src = orig->src.u.all;
193 key->ct.orig_tp.dst = orig->dst.u.all;
194 }
195}
196
197static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
198 const struct nf_conntrack_zone *zone,
199 const struct nf_conn *ct)
200{
201 key->ct_state = state;
202 key->ct_zone = zone->id;
203 key->ct.mark = ovs_ct_get_mark(ct);
204 ovs_ct_get_labels(ct, &key->ct.labels);
205
206 if (ct) {
207 const struct nf_conntrack_tuple *orig;
208
209
210 if (ct->master)
211 ct = ct->master;
212 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
213
214
215 if (key->eth.type == htons(ETH_P_IP) &&
216 nf_ct_l3num(ct) == NFPROTO_IPV4) {
217 key->ipv4.ct_orig.src = orig->src.u3.ip;
218 key->ipv4.ct_orig.dst = orig->dst.u3.ip;
219 __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
220 return;
221 } else if (key->eth.type == htons(ETH_P_IPV6) &&
222 !sw_flow_key_is_nd(key) &&
223 nf_ct_l3num(ct) == NFPROTO_IPV6) {
224 key->ipv6.ct_orig.src = orig->src.u3.in6;
225 key->ipv6.ct_orig.dst = orig->dst.u3.in6;
226 __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
227 return;
228 }
229 }
230
231
232
233 key->ct_orig_proto = 0;
234}
235
236
237
238
239
240
241static void ovs_ct_update_key(const struct sk_buff *skb,
242 const struct ovs_conntrack_info *info,
243 struct sw_flow_key *key, bool post_ct,
244 bool keep_nat_flags)
245{
246 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
247 enum ip_conntrack_info ctinfo;
248 struct nf_conn *ct;
249 u8 state = 0;
250
251 ct = nf_ct_get(skb, &ctinfo);
252 if (ct) {
253 state = ovs_ct_get_state(ctinfo);
254
255 if (!nf_ct_is_confirmed(ct))
256 state |= OVS_CS_F_NEW;
257
258
259
260 if (ct->master)
261 state |= OVS_CS_F_RELATED;
262 if (keep_nat_flags) {
263 state |= key->ct_state & OVS_CS_F_NAT_MASK;
264 } else {
265 if (ct->status & IPS_SRC_NAT)
266 state |= OVS_CS_F_SRC_NAT;
267 if (ct->status & IPS_DST_NAT)
268 state |= OVS_CS_F_DST_NAT;
269 }
270 zone = nf_ct_zone(ct);
271 } else if (post_ct) {
272 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
273 if (info)
274 zone = &info->zone;
275 }
276 __ovs_ct_update_key(key, state, zone, ct);
277}
278
279
280
281
282void ovs_ct_fill_key(const struct sk_buff *skb,
283 struct sw_flow_key *key,
284 bool post_ct)
285{
286 ovs_ct_update_key(skb, NULL, key, post_ct, false);
287}
288
289int ovs_ct_put_key(const struct sw_flow_key *swkey,
290 const struct sw_flow_key *output, struct sk_buff *skb)
291{
292 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
293 return -EMSGSIZE;
294
295 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
296 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
297 return -EMSGSIZE;
298
299 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
300 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
301 return -EMSGSIZE;
302
303 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
304 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
305 &output->ct.labels))
306 return -EMSGSIZE;
307
308 if (swkey->ct_orig_proto) {
309 if (swkey->eth.type == htons(ETH_P_IP)) {
310 struct ovs_key_ct_tuple_ipv4 orig;
311
312 memset(&orig, 0, sizeof(orig));
313 orig.ipv4_src = output->ipv4.ct_orig.src;
314 orig.ipv4_dst = output->ipv4.ct_orig.dst;
315 orig.src_port = output->ct.orig_tp.src;
316 orig.dst_port = output->ct.orig_tp.dst;
317 orig.ipv4_proto = output->ct_orig_proto;
318
319 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
320 sizeof(orig), &orig))
321 return -EMSGSIZE;
322 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
323 struct ovs_key_ct_tuple_ipv6 orig;
324
325 memset(&orig, 0, sizeof(orig));
326 memcpy(orig.ipv6_src, output->ipv6.ct_orig.src.s6_addr32,
327 sizeof(orig.ipv6_src));
328 memcpy(orig.ipv6_dst, output->ipv6.ct_orig.dst.s6_addr32,
329 sizeof(orig.ipv6_dst));
330 orig.src_port = output->ct.orig_tp.src;
331 orig.dst_port = output->ct.orig_tp.dst;
332 orig.ipv6_proto = output->ct_orig_proto;
333
334 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
335 sizeof(orig), &orig))
336 return -EMSGSIZE;
337 }
338 }
339
340 return 0;
341}
342
343static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
344 u32 ct_mark, u32 mask)
345{
346#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
347 u32 new_mark;
348
349 new_mark = ct_mark | (ct->mark & ~(mask));
350 if (ct->mark != new_mark) {
351 ct->mark = new_mark;
352 if (nf_ct_is_confirmed(ct))
353 nf_conntrack_event_cache(IPCT_MARK, ct);
354 key->ct.mark = new_mark;
355 }
356
357 return 0;
358#else
359 return -ENOTSUPP;
360#endif
361}
362
363static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
364{
365 struct nf_conn_labels *cl;
366
367 cl = nf_ct_labels_find(ct);
368 if (!cl) {
369 nf_ct_labels_ext_add(ct);
370 cl = nf_ct_labels_find(ct);
371 }
372
373 return cl;
374}
375
376
377
378
379
380static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
381 const struct ovs_key_ct_labels *labels,
382 const struct ovs_key_ct_labels *mask)
383{
384 struct nf_conn_labels *cl, *master_cl;
385 bool have_mask = labels_nonzero(mask);
386
387
388 master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
389
390 if (!master_cl && !have_mask)
391 return 0;
392
393 cl = ovs_ct_get_conn_labels(ct);
394 if (!cl)
395 return -ENOSPC;
396
397
398 if (master_cl)
399 *cl = *master_cl;
400
401 if (have_mask) {
402 u32 *dst = (u32 *)cl->bits;
403 int i;
404
405 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
406 dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
407 (labels->ct_labels_32[i]
408 & mask->ct_labels_32[i]);
409 }
410
411
412
413
414 nf_conntrack_event_cache(IPCT_LABEL, ct);
415
416 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
417
418 return 0;
419}
420
421static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
422 const struct ovs_key_ct_labels *labels,
423 const struct ovs_key_ct_labels *mask)
424{
425 struct nf_conn_labels *cl;
426 int err;
427
428 cl = ovs_ct_get_conn_labels(ct);
429 if (!cl)
430 return -ENOSPC;
431
432 err = nf_connlabels_replace(ct, labels->ct_labels_32,
433 mask->ct_labels_32,
434 OVS_CT_LABELS_LEN_32);
435 if (err)
436 return err;
437
438 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
439
440 return 0;
441}
442
443
444static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
445{
446 const struct nf_conntrack_helper *helper;
447 const struct nf_conn_help *help;
448 enum ip_conntrack_info ctinfo;
449 unsigned int protoff;
450 struct nf_conn *ct;
451 int err;
452
453 ct = nf_ct_get(skb, &ctinfo);
454 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
455 return NF_ACCEPT;
456
457 help = nfct_help(ct);
458 if (!help)
459 return NF_ACCEPT;
460
461 helper = rcu_dereference(help->helper);
462 if (!helper)
463 return NF_ACCEPT;
464
465 switch (proto) {
466 case NFPROTO_IPV4:
467 protoff = ip_hdrlen(skb);
468 break;
469 case NFPROTO_IPV6: {
470 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
471 __be16 frag_off;
472 int ofs;
473
474 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
475 &frag_off);
476 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
477 pr_debug("proto header not found\n");
478 return NF_ACCEPT;
479 }
480 protoff = ofs;
481 break;
482 }
483 default:
484 WARN_ONCE(1, "helper invoked on non-IP family!");
485 return NF_DROP;
486 }
487
488 err = helper->help(skb, protoff, ct, ctinfo);
489 if (err != NF_ACCEPT)
490 return err;
491
492
493
494
495
496 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
497 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
498 return NF_DROP;
499 return NF_ACCEPT;
500}
501
502
503
504
505static int handle_fragments(struct net *net, struct sw_flow_key *key,
506 u16 zone, struct sk_buff *skb)
507{
508 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
509 int err;
510
511 if (key->eth.type == htons(ETH_P_IP)) {
512 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
513
514 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
515 err = ip_defrag(net, skb, user);
516 if (err)
517 return err;
518
519 ovs_cb.mru = IPCB(skb)->frag_max_size;
520#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
521 } else if (key->eth.type == htons(ETH_P_IPV6)) {
522 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
523
524 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
525 err = nf_ct_frag6_gather(net, skb, user);
526 if (err) {
527 if (err != -EINPROGRESS)
528 kfree_skb(skb);
529 return err;
530 }
531
532 key->ip.proto = ipv6_hdr(skb)->nexthdr;
533 ovs_cb.mru = IP6CB(skb)->frag_max_size;
534#endif
535 } else {
536 kfree_skb(skb);
537 return -EPFNOSUPPORT;
538 }
539
540
541
542
543 ovs_flow_key_update_l3l4(skb, key);
544
545 key->ip.frag = OVS_FRAG_TYPE_NONE;
546 skb_clear_hash(skb);
547 skb->ignore_df = 1;
548 *OVS_CB(skb) = ovs_cb;
549
550 return 0;
551}
552
553static struct nf_conntrack_expect *
554ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
555 u16 proto, const struct sk_buff *skb)
556{
557 struct nf_conntrack_tuple tuple;
558 struct nf_conntrack_expect *exp;
559
560 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
561 return NULL;
562
563 exp = __nf_ct_expect_find(net, zone, &tuple);
564 if (exp) {
565 struct nf_conntrack_tuple_hash *h;
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580 h = nf_conntrack_find_get(net, zone, &tuple);
581 if (h) {
582 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
583
584 nf_ct_delete(ct, 0, 0);
585 nf_conntrack_put(&ct->ct_general);
586 }
587 }
588
589 return exp;
590}
591
592
593static enum ip_conntrack_info
594ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
595{
596 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
597
598 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
599 return IP_CT_ESTABLISHED_REPLY;
600
601 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
602 return IP_CT_ESTABLISHED;
603 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
604 return IP_CT_RELATED;
605 return IP_CT_NEW;
606}
607
608
609
610
611
612
613
614
615
616
617static struct nf_conn *
618ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
619 u8 l3num, struct sk_buff *skb, bool natted)
620{
621 struct nf_conntrack_tuple tuple;
622 struct nf_conntrack_tuple_hash *h;
623 struct nf_conn *ct;
624
625 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num,
626 net, &tuple)) {
627 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
628 return NULL;
629 }
630
631
632 if (natted) {
633 struct nf_conntrack_tuple inverse;
634
635 if (!nf_ct_invert_tuple(&inverse, &tuple)) {
636 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
637 return NULL;
638 }
639 tuple = inverse;
640 }
641
642
643 h = nf_conntrack_find_get(net, zone, &tuple);
644 if (!h)
645 return NULL;
646
647 ct = nf_ct_tuplehash_to_ctrack(h);
648
649
650
651
652
653 if (natted)
654 h = &ct->tuplehash[!h->tuple.dst.dir];
655
656 nf_ct_set(skb, ct, ovs_ct_get_info(h));
657 return ct;
658}
659
660static
661struct nf_conn *ovs_ct_executed(struct net *net,
662 const struct sw_flow_key *key,
663 const struct ovs_conntrack_info *info,
664 struct sk_buff *skb,
665 bool *ct_executed)
666{
667 struct nf_conn *ct = NULL;
668
669
670
671
672
673
674
675 *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
676 !(key->ct_state & OVS_CS_F_INVALID) &&
677 (key->ct_zone == info->zone.id);
678
679 if (*ct_executed || (!key->ct_state && info->force)) {
680 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
681 !!(key->ct_state &
682 OVS_CS_F_NAT_MASK));
683 }
684
685 return ct;
686}
687
688
689static bool skb_nfct_cached(struct net *net,
690 const struct sw_flow_key *key,
691 const struct ovs_conntrack_info *info,
692 struct sk_buff *skb)
693{
694 enum ip_conntrack_info ctinfo;
695 struct nf_conn *ct;
696 bool ct_executed = true;
697
698 ct = nf_ct_get(skb, &ctinfo);
699 if (!ct)
700 ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
701
702 if (ct)
703 nf_ct_get(skb, &ctinfo);
704 else
705 return false;
706
707 if (!net_eq(net, read_pnet(&ct->ct_net)))
708 return false;
709 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
710 return false;
711 if (info->helper) {
712 struct nf_conn_help *help;
713
714 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
715 if (help && rcu_access_pointer(help->helper) != info->helper)
716 return false;
717 }
718 if (info->nf_ct_timeout) {
719 struct nf_conn_timeout *timeout_ext;
720
721 timeout_ext = nf_ct_timeout_find(ct);
722 if (!timeout_ext || info->nf_ct_timeout !=
723 rcu_dereference(timeout_ext->timeout))
724 return false;
725 }
726
727 if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
728
729
730
731 if (nf_ct_is_confirmed(ct))
732 nf_ct_delete(ct, 0, 0);
733
734 nf_conntrack_put(&ct->ct_general);
735 nf_ct_set(skb, NULL, 0);
736 return false;
737 }
738
739 return ct_executed;
740}
741
742#ifdef CONFIG_NF_NAT_NEEDED
743
744
745
746
747static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
748 enum ip_conntrack_info ctinfo,
749 const struct nf_nat_range2 *range,
750 enum nf_nat_manip_type maniptype)
751{
752 int hooknum, nh_off, err = NF_ACCEPT;
753
754 nh_off = skb_network_offset(skb);
755 skb_pull_rcsum(skb, nh_off);
756
757
758 if (maniptype == NF_NAT_MANIP_SRC)
759 hooknum = NF_INET_LOCAL_IN;
760 else
761 hooknum = NF_INET_LOCAL_OUT;
762
763 switch (ctinfo) {
764 case IP_CT_RELATED:
765 case IP_CT_RELATED_REPLY:
766 if (IS_ENABLED(CONFIG_NF_NAT) &&
767 skb->protocol == htons(ETH_P_IP) &&
768 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
769 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
770 hooknum))
771 err = NF_DROP;
772 goto push;
773 } else if (IS_ENABLED(CONFIG_IPV6) &&
774 skb->protocol == htons(ETH_P_IPV6)) {
775 __be16 frag_off;
776 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
777 int hdrlen = ipv6_skip_exthdr(skb,
778 sizeof(struct ipv6hdr),
779 &nexthdr, &frag_off);
780
781 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
782 if (!nf_nat_icmpv6_reply_translation(skb, ct,
783 ctinfo,
784 hooknum,
785 hdrlen))
786 err = NF_DROP;
787 goto push;
788 }
789 }
790
791
792 case IP_CT_NEW:
793
794
795
796 if (!nf_nat_initialized(ct, maniptype)) {
797
798 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
799
800
801
802 ? nf_nat_setup_info(ct, range, maniptype)
803 : nf_nat_alloc_null_binding(ct, hooknum);
804 if (err != NF_ACCEPT)
805 goto push;
806 }
807 break;
808
809 case IP_CT_ESTABLISHED:
810 case IP_CT_ESTABLISHED_REPLY:
811 break;
812
813 default:
814 err = NF_DROP;
815 goto push;
816 }
817
818 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
819push:
820 skb_push_rcsum(skb, nh_off);
821
822 return err;
823}
824
825static void ovs_nat_update_key(struct sw_flow_key *key,
826 const struct sk_buff *skb,
827 enum nf_nat_manip_type maniptype)
828{
829 if (maniptype == NF_NAT_MANIP_SRC) {
830 __be16 src;
831
832 key->ct_state |= OVS_CS_F_SRC_NAT;
833 if (key->eth.type == htons(ETH_P_IP))
834 key->ipv4.addr.src = ip_hdr(skb)->saddr;
835 else if (key->eth.type == htons(ETH_P_IPV6))
836 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
837 sizeof(key->ipv6.addr.src));
838 else
839 return;
840
841 if (key->ip.proto == IPPROTO_UDP)
842 src = udp_hdr(skb)->source;
843 else if (key->ip.proto == IPPROTO_TCP)
844 src = tcp_hdr(skb)->source;
845 else if (key->ip.proto == IPPROTO_SCTP)
846 src = sctp_hdr(skb)->source;
847 else
848 return;
849
850 key->tp.src = src;
851 } else {
852 __be16 dst;
853
854 key->ct_state |= OVS_CS_F_DST_NAT;
855 if (key->eth.type == htons(ETH_P_IP))
856 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
857 else if (key->eth.type == htons(ETH_P_IPV6))
858 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
859 sizeof(key->ipv6.addr.dst));
860 else
861 return;
862
863 if (key->ip.proto == IPPROTO_UDP)
864 dst = udp_hdr(skb)->dest;
865 else if (key->ip.proto == IPPROTO_TCP)
866 dst = tcp_hdr(skb)->dest;
867 else if (key->ip.proto == IPPROTO_SCTP)
868 dst = sctp_hdr(skb)->dest;
869 else
870 return;
871
872 key->tp.dst = dst;
873 }
874}
875
876
877static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
878 const struct ovs_conntrack_info *info,
879 struct sk_buff *skb, struct nf_conn *ct,
880 enum ip_conntrack_info ctinfo)
881{
882 enum nf_nat_manip_type maniptype;
883 int err;
884
885
886 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
887 return NF_ACCEPT;
888
889
890
891
892
893
894 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
895 ct->status & IPS_NAT_MASK &&
896 (ctinfo != IP_CT_RELATED || info->commit)) {
897
898 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
899
900
901
902
903 maniptype = ct->status & IPS_SRC_NAT
904 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
905 else
906 maniptype = ct->status & IPS_SRC_NAT
907 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
908 } else if (info->nat & OVS_CT_SRC_NAT) {
909 maniptype = NF_NAT_MANIP_SRC;
910 } else if (info->nat & OVS_CT_DST_NAT) {
911 maniptype = NF_NAT_MANIP_DST;
912 } else {
913 return NF_ACCEPT;
914 }
915 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
916
917 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
918 if (ct->status & IPS_SRC_NAT) {
919 if (maniptype == NF_NAT_MANIP_SRC)
920 maniptype = NF_NAT_MANIP_DST;
921 else
922 maniptype = NF_NAT_MANIP_SRC;
923
924 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range,
925 maniptype);
926 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
927 err = ovs_ct_nat_execute(skb, ct, ctinfo, NULL,
928 NF_NAT_MANIP_SRC);
929 }
930 }
931
932
933 if (err == NF_ACCEPT)
934 ovs_nat_update_key(key, skb, maniptype);
935
936 return err;
937}
938#else
939static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
940 const struct ovs_conntrack_info *info,
941 struct sk_buff *skb, struct nf_conn *ct,
942 enum ip_conntrack_info ctinfo)
943{
944 return NF_ACCEPT;
945}
946#endif
947
948
949
950
951
952
953
954static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
955 const struct ovs_conntrack_info *info,
956 struct sk_buff *skb)
957{
958
959
960
961
962
963 bool cached = skb_nfct_cached(net, key, info, skb);
964 enum ip_conntrack_info ctinfo;
965 struct nf_conn *ct;
966
967 if (!cached) {
968 struct nf_hook_state state = {
969 .hook = NF_INET_PRE_ROUTING,
970 .pf = info->family,
971 .net = net,
972 };
973 struct nf_conn *tmpl = info->ct;
974 int err;
975
976
977 if (tmpl) {
978 nf_conntrack_put(skb_nfct(skb));
979 nf_conntrack_get(&tmpl->ct_general);
980 nf_ct_set(skb, tmpl, IP_CT_NEW);
981 }
982
983 err = nf_conntrack_in(skb, &state);
984 if (err != NF_ACCEPT)
985 return -ENOENT;
986
987
988
989
990
991 key->ct_state = 0;
992
993
994 ovs_ct_update_key(skb, info, key, true, true);
995 }
996
997 ct = nf_ct_get(skb, &ctinfo);
998 if (ct) {
999 bool add_helper = false;
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
1012 (nf_ct_is_confirmed(ct) || info->commit) &&
1013 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
1014 return -EINVAL;
1015 }
1016
1017
1018
1019
1020
1021
1022
1023 if (info->commit && info->helper && !nfct_help(ct)) {
1024 int err = __nf_ct_try_assign_helper(ct, info->ct,
1025 GFP_ATOMIC);
1026 if (err)
1027 return err;
1028 add_helper = true;
1029
1030
1031 if (info->nat && !nfct_seqadj(ct)) {
1032 if (!nfct_seqadj_ext_add(ct))
1033 return -EINVAL;
1034 }
1035 }
1036
1037
1038
1039
1040
1041
1042
1043 if ((nf_ct_is_confirmed(ct) ? !cached || add_helper :
1044 info->commit) &&
1045 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
1046 return -EINVAL;
1047 }
1048
1049 if (nf_ct_protonum(ct) == IPPROTO_TCP &&
1050 nf_ct_is_confirmed(ct) && nf_conntrack_tcp_established(ct)) {
1051
1052
1053
1054 nf_ct_set_tcp_be_liberal(ct);
1055 }
1056 }
1057
1058 return 0;
1059}
1060
1061
1062static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
1063 const struct ovs_conntrack_info *info,
1064 struct sk_buff *skb)
1065{
1066 struct nf_conntrack_expect *exp;
1067
1068
1069
1070
1071
1072
1073
1074
1075 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
1076 if (exp) {
1077 u8 state;
1078
1079
1080
1081
1082 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
1083 __ovs_ct_update_key(key, state, &info->zone, exp->master);
1084 } else {
1085 struct nf_conn *ct;
1086 int err;
1087
1088 err = __ovs_ct_lookup(net, key, info, skb);
1089 if (err)
1090 return err;
1091
1092 ct = (struct nf_conn *)skb_nfct(skb);
1093 if (ct)
1094 nf_ct_deliver_cached_events(ct);
1095 }
1096
1097 return 0;
1098}
1099
1100static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
1101{
1102 size_t i;
1103
1104 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
1105 if (labels->ct_labels_32[i])
1106 return true;
1107
1108 return false;
1109}
1110
1111#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1112static struct hlist_head *ct_limit_hash_bucket(
1113 const struct ovs_ct_limit_info *info, u16 zone)
1114{
1115 return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
1116}
1117
1118
1119static void ct_limit_set(const struct ovs_ct_limit_info *info,
1120 struct ovs_ct_limit *new_ct_limit)
1121{
1122 struct ovs_ct_limit *ct_limit;
1123 struct hlist_head *head;
1124
1125 head = ct_limit_hash_bucket(info, new_ct_limit->zone);
1126 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1127 if (ct_limit->zone == new_ct_limit->zone) {
1128 hlist_replace_rcu(&ct_limit->hlist_node,
1129 &new_ct_limit->hlist_node);
1130 kfree_rcu(ct_limit, rcu);
1131 return;
1132 }
1133 }
1134
1135 hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
1136}
1137
1138
1139static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
1140{
1141 struct ovs_ct_limit *ct_limit;
1142 struct hlist_head *head;
1143 struct hlist_node *n;
1144
1145 head = ct_limit_hash_bucket(info, zone);
1146 hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
1147 if (ct_limit->zone == zone) {
1148 hlist_del_rcu(&ct_limit->hlist_node);
1149 kfree_rcu(ct_limit, rcu);
1150 return;
1151 }
1152 }
1153}
1154
1155
1156static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
1157{
1158 struct ovs_ct_limit *ct_limit;
1159 struct hlist_head *head;
1160
1161 head = ct_limit_hash_bucket(info, zone);
1162 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1163 if (ct_limit->zone == zone)
1164 return ct_limit->limit;
1165 }
1166
1167 return info->default_limit;
1168}
1169
1170static int ovs_ct_check_limit(struct net *net,
1171 const struct ovs_conntrack_info *info,
1172 const struct nf_conntrack_tuple *tuple)
1173{
1174 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1175 const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
1176 u32 per_zone_limit, connections;
1177 u32 conncount_key;
1178
1179 conncount_key = info->zone.id;
1180
1181 per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
1182 if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
1183 return 0;
1184
1185 connections = nf_conncount_count(net, ct_limit_info->data,
1186 &conncount_key, tuple, &info->zone);
1187 if (connections > per_zone_limit)
1188 return -ENOMEM;
1189
1190 return 0;
1191}
1192#endif
1193
1194
1195static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
1196 const struct ovs_conntrack_info *info,
1197 struct sk_buff *skb)
1198{
1199 enum ip_conntrack_info ctinfo;
1200 struct nf_conn *ct;
1201 int err;
1202
1203 err = __ovs_ct_lookup(net, key, info, skb);
1204 if (err)
1205 return err;
1206
1207
1208 ct = nf_ct_get(skb, &ctinfo);
1209 if (!ct)
1210 return 0;
1211
1212#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1213 if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
1214 if (!nf_ct_is_confirmed(ct)) {
1215 err = ovs_ct_check_limit(net, info,
1216 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1217 if (err) {
1218 net_warn_ratelimited("openvswitch: zone: %u "
1219 "exceeds conntrack limit\n",
1220 info->zone.id);
1221 return err;
1222 }
1223 }
1224 }
1225#endif
1226
1227
1228
1229
1230
1231
1232
1233
1234 if (info->have_eventmask) {
1235 struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
1236
1237 if (cache)
1238 cache->ctmask = info->eventmask;
1239 }
1240
1241
1242
1243
1244
1245 if (info->mark.mask) {
1246 err = ovs_ct_set_mark(ct, key, info->mark.value,
1247 info->mark.mask);
1248 if (err)
1249 return err;
1250 }
1251 if (!nf_ct_is_confirmed(ct)) {
1252 err = ovs_ct_init_labels(ct, key, &info->labels.value,
1253 &info->labels.mask);
1254 if (err)
1255 return err;
1256 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1257 labels_nonzero(&info->labels.mask)) {
1258 err = ovs_ct_set_labels(ct, key, &info->labels.value,
1259 &info->labels.mask);
1260 if (err)
1261 return err;
1262 }
1263
1264
1265
1266 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1267 return -EINVAL;
1268
1269 return 0;
1270}
1271
1272
1273
1274
1275
1276
1277
1278static int ovs_skb_network_trim(struct sk_buff *skb)
1279{
1280 unsigned int len;
1281 int err;
1282
1283 switch (skb->protocol) {
1284 case htons(ETH_P_IP):
1285 len = ntohs(ip_hdr(skb)->tot_len);
1286 break;
1287 case htons(ETH_P_IPV6):
1288 len = sizeof(struct ipv6hdr)
1289 + ntohs(ipv6_hdr(skb)->payload_len);
1290 break;
1291 default:
1292 len = skb->len;
1293 }
1294
1295 err = pskb_trim_rcsum(skb, len);
1296 if (err)
1297 kfree_skb(skb);
1298
1299 return err;
1300}
1301
1302
1303
1304
1305int ovs_ct_execute(struct net *net, struct sk_buff *skb,
1306 struct sw_flow_key *key,
1307 const struct ovs_conntrack_info *info)
1308{
1309 int nh_ofs;
1310 int err;
1311
1312
1313 nh_ofs = skb_network_offset(skb);
1314 skb_pull_rcsum(skb, nh_ofs);
1315
1316 err = ovs_skb_network_trim(skb);
1317 if (err)
1318 return err;
1319
1320 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
1321 err = handle_fragments(net, key, info->zone.id, skb);
1322 if (err)
1323 return err;
1324 }
1325
1326 if (info->commit)
1327 err = ovs_ct_commit(net, key, info, skb);
1328 else
1329 err = ovs_ct_lookup(net, key, info, skb);
1330
1331 skb_push_rcsum(skb, nh_ofs);
1332 if (err)
1333 kfree_skb(skb);
1334 return err;
1335}
1336
1337int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
1338{
1339 nf_conntrack_put(skb_nfct(skb));
1340 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1341 ovs_ct_fill_key(skb, key, false);
1342
1343 return 0;
1344}
1345
1346static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
1347 const struct sw_flow_key *key, bool log)
1348{
1349 struct nf_conntrack_helper *helper;
1350 struct nf_conn_help *help;
1351 int ret = 0;
1352
1353 helper = nf_conntrack_helper_try_module_get(name, info->family,
1354 key->ip.proto);
1355 if (!helper) {
1356 OVS_NLERR(log, "Unknown helper \"%s\"", name);
1357 return -EINVAL;
1358 }
1359
1360 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
1361 if (!help) {
1362 nf_conntrack_helper_put(helper);
1363 return -ENOMEM;
1364 }
1365
1366#ifdef CONFIG_NF_NAT_NEEDED
1367 if (info->nat) {
1368 ret = nf_nat_helper_try_module_get(name, info->family,
1369 key->ip.proto);
1370 if (ret) {
1371 nf_conntrack_helper_put(helper);
1372 OVS_NLERR(log, "Failed to load \"%s\" NAT helper, error: %d",
1373 name, ret);
1374 return ret;
1375 }
1376 }
1377#endif
1378 rcu_assign_pointer(help->helper, helper);
1379 info->helper = helper;
1380 return ret;
1381}
1382
1383#ifdef CONFIG_NF_NAT_NEEDED
1384static int parse_nat(const struct nlattr *attr,
1385 struct ovs_conntrack_info *info, bool log)
1386{
1387 struct nlattr *a;
1388 int rem;
1389 bool have_ip_max = false;
1390 bool have_proto_max = false;
1391 bool ip_vers = (info->family == NFPROTO_IPV6);
1392
1393 nla_for_each_nested(a, attr, rem) {
1394 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1395 [OVS_NAT_ATTR_SRC] = {0, 0},
1396 [OVS_NAT_ATTR_DST] = {0, 0},
1397 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1398 sizeof(struct in6_addr)},
1399 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1400 sizeof(struct in6_addr)},
1401 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1402 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1403 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1404 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1405 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1406 };
1407 int type = nla_type(a);
1408
1409 if (type > OVS_NAT_ATTR_MAX) {
1410 OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
1411 type, OVS_NAT_ATTR_MAX);
1412 return -EINVAL;
1413 }
1414
1415 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1416 OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
1417 type, nla_len(a),
1418 ovs_nat_attr_lens[type][ip_vers]);
1419 return -EINVAL;
1420 }
1421
1422 switch (type) {
1423 case OVS_NAT_ATTR_SRC:
1424 case OVS_NAT_ATTR_DST:
1425 if (info->nat) {
1426 OVS_NLERR(log, "Only one type of NAT may be specified");
1427 return -ERANGE;
1428 }
1429 info->nat |= OVS_CT_NAT;
1430 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1431 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1432 break;
1433
1434 case OVS_NAT_ATTR_IP_MIN:
1435 nla_memcpy(&info->range.min_addr, a,
1436 sizeof(info->range.min_addr));
1437 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1438 break;
1439
1440 case OVS_NAT_ATTR_IP_MAX:
1441 have_ip_max = true;
1442 nla_memcpy(&info->range.max_addr, a,
1443 sizeof(info->range.max_addr));
1444 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1445 break;
1446
1447 case OVS_NAT_ATTR_PROTO_MIN:
1448 info->range.min_proto.all = htons(nla_get_u16(a));
1449 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1450 break;
1451
1452 case OVS_NAT_ATTR_PROTO_MAX:
1453 have_proto_max = true;
1454 info->range.max_proto.all = htons(nla_get_u16(a));
1455 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1456 break;
1457
1458 case OVS_NAT_ATTR_PERSISTENT:
1459 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1460 break;
1461
1462 case OVS_NAT_ATTR_PROTO_HASH:
1463 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1464 break;
1465
1466 case OVS_NAT_ATTR_PROTO_RANDOM:
1467 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1468 break;
1469
1470 default:
1471 OVS_NLERR(log, "Unknown nat attribute (%d)", type);
1472 return -EINVAL;
1473 }
1474 }
1475
1476 if (rem > 0) {
1477 OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
1478 return -EINVAL;
1479 }
1480 if (!info->nat) {
1481
1482 if (info->range.flags) {
1483 OVS_NLERR(log,
1484 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1485 );
1486 return -EINVAL;
1487 }
1488 info->nat = OVS_CT_NAT;
1489 } else if (!info->commit) {
1490 OVS_NLERR(log,
1491 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1492 );
1493 return -EINVAL;
1494 }
1495
1496 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1497 memcpy(&info->range.max_addr, &info->range.min_addr,
1498 sizeof(info->range.max_addr));
1499 }
1500
1501 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1502 !have_proto_max) {
1503 info->range.max_proto.all = info->range.min_proto.all;
1504 }
1505 return 0;
1506}
1507#endif
1508
1509static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
1510 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
1511 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
1512 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1513 .maxlen = sizeof(u16) },
1514 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1515 .maxlen = sizeof(struct md_mark) },
1516 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1517 .maxlen = sizeof(struct md_labels) },
1518 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
1519 .maxlen = NF_CT_HELPER_NAME_LEN },
1520#ifdef CONFIG_NF_NAT_NEEDED
1521
1522 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1523#endif
1524 [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
1525 .maxlen = sizeof(u32) },
1526 [OVS_CT_ATTR_TIMEOUT] = { .minlen = 1,
1527 .maxlen = CTNL_TIMEOUT_NAME_MAX },
1528};
1529
1530static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
1531 const char **helper, bool log)
1532{
1533 struct nlattr *a;
1534 int rem;
1535
1536 nla_for_each_nested(a, attr, rem) {
1537 int type = nla_type(a);
1538 int maxlen;
1539 int minlen;
1540
1541 if (type > OVS_CT_ATTR_MAX) {
1542 OVS_NLERR(log,
1543 "Unknown conntrack attr (type=%d, max=%d)",
1544 type, OVS_CT_ATTR_MAX);
1545 return -EINVAL;
1546 }
1547
1548 maxlen = ovs_ct_attr_lens[type].maxlen;
1549 minlen = ovs_ct_attr_lens[type].minlen;
1550 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1551 OVS_NLERR(log,
1552 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1553 type, nla_len(a), maxlen);
1554 return -EINVAL;
1555 }
1556
1557 switch (type) {
1558 case OVS_CT_ATTR_FORCE_COMMIT:
1559 info->force = true;
1560
1561 case OVS_CT_ATTR_COMMIT:
1562 info->commit = true;
1563 break;
1564#ifdef CONFIG_NF_CONNTRACK_ZONES
1565 case OVS_CT_ATTR_ZONE:
1566 info->zone.id = nla_get_u16(a);
1567 break;
1568#endif
1569#ifdef CONFIG_NF_CONNTRACK_MARK
1570 case OVS_CT_ATTR_MARK: {
1571 struct md_mark *mark = nla_data(a);
1572
1573 if (!mark->mask) {
1574 OVS_NLERR(log, "ct_mark mask cannot be 0");
1575 return -EINVAL;
1576 }
1577 info->mark = *mark;
1578 break;
1579 }
1580#endif
1581#ifdef CONFIG_NF_CONNTRACK_LABELS
1582 case OVS_CT_ATTR_LABELS: {
1583 struct md_labels *labels = nla_data(a);
1584
1585 if (!labels_nonzero(&labels->mask)) {
1586 OVS_NLERR(log, "ct_labels mask cannot be 0");
1587 return -EINVAL;
1588 }
1589 info->labels = *labels;
1590 break;
1591 }
1592#endif
1593 case OVS_CT_ATTR_HELPER:
1594 *helper = nla_data(a);
1595 if (!memchr(*helper, '\0', nla_len(a))) {
1596 OVS_NLERR(log, "Invalid conntrack helper");
1597 return -EINVAL;
1598 }
1599 break;
1600#ifdef CONFIG_NF_NAT_NEEDED
1601 case OVS_CT_ATTR_NAT: {
1602 int err = parse_nat(a, info, log);
1603
1604 if (err)
1605 return err;
1606 break;
1607 }
1608#endif
1609 case OVS_CT_ATTR_EVENTMASK:
1610 info->have_eventmask = true;
1611 info->eventmask = nla_get_u32(a);
1612 break;
1613#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1614 case OVS_CT_ATTR_TIMEOUT:
1615 memcpy(info->timeout, nla_data(a), nla_len(a));
1616 if (!memchr(info->timeout, '\0', nla_len(a))) {
1617 OVS_NLERR(log, "Invalid conntrack timeout");
1618 return -EINVAL;
1619 }
1620 break;
1621#endif
1622
1623 default:
1624 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1625 type);
1626 return -EINVAL;
1627 }
1628 }
1629
1630#ifdef CONFIG_NF_CONNTRACK_MARK
1631 if (!info->commit && info->mark.mask) {
1632 OVS_NLERR(log,
1633 "Setting conntrack mark requires 'commit' flag.");
1634 return -EINVAL;
1635 }
1636#endif
1637#ifdef CONFIG_NF_CONNTRACK_LABELS
1638 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1639 OVS_NLERR(log,
1640 "Setting conntrack labels requires 'commit' flag.");
1641 return -EINVAL;
1642 }
1643#endif
1644 if (rem > 0) {
1645 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1646 return -EINVAL;
1647 }
1648
1649 return 0;
1650}
1651
1652bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
1653{
1654 if (attr == OVS_KEY_ATTR_CT_STATE)
1655 return true;
1656 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1657 attr == OVS_KEY_ATTR_CT_ZONE)
1658 return true;
1659 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1660 attr == OVS_KEY_ATTR_CT_MARK)
1661 return true;
1662 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1663 attr == OVS_KEY_ATTR_CT_LABELS) {
1664 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1665
1666 return ovs_net->xt_label;
1667 }
1668
1669 return false;
1670}
1671
1672int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1673 const struct sw_flow_key *key,
1674 struct sw_flow_actions **sfa, bool log)
1675{
1676 struct ovs_conntrack_info ct_info;
1677 const char *helper = NULL;
1678 u16 family;
1679 int err;
1680
1681 family = key_to_nfproto(key);
1682 if (family == NFPROTO_UNSPEC) {
1683 OVS_NLERR(log, "ct family unspecified");
1684 return -EINVAL;
1685 }
1686
1687 memset(&ct_info, 0, sizeof(ct_info));
1688 ct_info.family = family;
1689
1690 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1691 NF_CT_DEFAULT_ZONE_DIR, 0);
1692
1693 err = parse_ct(attr, &ct_info, &helper, log);
1694 if (err)
1695 return err;
1696
1697
1698 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1699 if (!ct_info.ct) {
1700 OVS_NLERR(log, "Failed to allocate conntrack template");
1701 return -ENOMEM;
1702 }
1703
1704 if (ct_info.timeout[0]) {
1705 if (nf_ct_set_timeout(net, ct_info.ct, family, key->ip.proto,
1706 ct_info.timeout))
1707 pr_info_ratelimited("Failed to associated timeout "
1708 "policy `%s'\n", ct_info.timeout);
1709 else
1710 ct_info.nf_ct_timeout = rcu_dereference(
1711 nf_ct_timeout_find(ct_info.ct)->timeout);
1712
1713 }
1714
1715 if (helper) {
1716 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1717 if (err)
1718 goto err_free_ct;
1719 }
1720
1721 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1722 sizeof(ct_info), log);
1723 if (err)
1724 goto err_free_ct;
1725
1726 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1727 nf_conntrack_get(&ct_info.ct->ct_general);
1728 return 0;
1729err_free_ct:
1730 __ovs_ct_free_action(&ct_info);
1731 return err;
1732}
1733
1734#ifdef CONFIG_NF_NAT_NEEDED
1735static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1736 struct sk_buff *skb)
1737{
1738 struct nlattr *start;
1739
1740 start = nla_nest_start_noflag(skb, OVS_CT_ATTR_NAT);
1741 if (!start)
1742 return false;
1743
1744 if (info->nat & OVS_CT_SRC_NAT) {
1745 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1746 return false;
1747 } else if (info->nat & OVS_CT_DST_NAT) {
1748 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1749 return false;
1750 } else {
1751 goto out;
1752 }
1753
1754 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
1755 if (IS_ENABLED(CONFIG_NF_NAT) &&
1756 info->family == NFPROTO_IPV4) {
1757 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1758 info->range.min_addr.ip) ||
1759 (info->range.max_addr.ip
1760 != info->range.min_addr.ip &&
1761 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1762 info->range.max_addr.ip))))
1763 return false;
1764 } else if (IS_ENABLED(CONFIG_IPV6) &&
1765 info->family == NFPROTO_IPV6) {
1766 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1767 &info->range.min_addr.in6) ||
1768 (memcmp(&info->range.max_addr.in6,
1769 &info->range.min_addr.in6,
1770 sizeof(info->range.max_addr.in6)) &&
1771 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1772 &info->range.max_addr.in6))))
1773 return false;
1774 } else {
1775 return false;
1776 }
1777 }
1778 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1779 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1780 ntohs(info->range.min_proto.all)) ||
1781 (info->range.max_proto.all != info->range.min_proto.all &&
1782 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1783 ntohs(info->range.max_proto.all)))))
1784 return false;
1785
1786 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1787 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1788 return false;
1789 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1790 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1791 return false;
1792 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1793 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1794 return false;
1795out:
1796 nla_nest_end(skb, start);
1797
1798 return true;
1799}
1800#endif
1801
1802int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1803 struct sk_buff *skb)
1804{
1805 struct nlattr *start;
1806
1807 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CT);
1808 if (!start)
1809 return -EMSGSIZE;
1810
1811 if (ct_info->commit && nla_put_flag(skb, ct_info->force
1812 ? OVS_CT_ATTR_FORCE_COMMIT
1813 : OVS_CT_ATTR_COMMIT))
1814 return -EMSGSIZE;
1815 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1816 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1817 return -EMSGSIZE;
1818 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
1819 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1820 &ct_info->mark))
1821 return -EMSGSIZE;
1822 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1823 labels_nonzero(&ct_info->labels.mask) &&
1824 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1825 &ct_info->labels))
1826 return -EMSGSIZE;
1827 if (ct_info->helper) {
1828 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1829 ct_info->helper->name))
1830 return -EMSGSIZE;
1831 }
1832 if (ct_info->have_eventmask &&
1833 nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
1834 return -EMSGSIZE;
1835 if (ct_info->timeout[0]) {
1836 if (nla_put_string(skb, OVS_CT_ATTR_TIMEOUT, ct_info->timeout))
1837 return -EMSGSIZE;
1838 }
1839
1840#ifdef CONFIG_NF_NAT_NEEDED
1841 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1842 return -EMSGSIZE;
1843#endif
1844 nla_nest_end(skb, start);
1845
1846 return 0;
1847}
1848
1849void ovs_ct_free_action(const struct nlattr *a)
1850{
1851 struct ovs_conntrack_info *ct_info = nla_data(a);
1852
1853 __ovs_ct_free_action(ct_info);
1854}
1855
1856static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1857{
1858 if (ct_info->helper) {
1859#ifdef CONFIG_NF_NAT_NEEDED
1860 if (ct_info->nat)
1861 nf_nat_helper_put(ct_info->helper);
1862#endif
1863 nf_conntrack_helper_put(ct_info->helper);
1864 }
1865 if (ct_info->ct) {
1866 if (ct_info->timeout[0])
1867 nf_ct_destroy_timeout(ct_info->ct);
1868 nf_ct_tmpl_free(ct_info->ct);
1869 }
1870}
1871
1872#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1873static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
1874{
1875 int i, err;
1876
1877 ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
1878 GFP_KERNEL);
1879 if (!ovs_net->ct_limit_info)
1880 return -ENOMEM;
1881
1882 ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
1883 ovs_net->ct_limit_info->limits =
1884 kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
1885 GFP_KERNEL);
1886 if (!ovs_net->ct_limit_info->limits) {
1887 kfree(ovs_net->ct_limit_info);
1888 return -ENOMEM;
1889 }
1890
1891 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
1892 INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
1893
1894 ovs_net->ct_limit_info->data =
1895 nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
1896
1897 if (IS_ERR(ovs_net->ct_limit_info->data)) {
1898 err = PTR_ERR(ovs_net->ct_limit_info->data);
1899 kfree(ovs_net->ct_limit_info->limits);
1900 kfree(ovs_net->ct_limit_info);
1901 pr_err("openvswitch: failed to init nf_conncount %d\n", err);
1902 return err;
1903 }
1904 return 0;
1905}
1906
1907static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
1908{
1909 const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
1910 int i;
1911
1912 nf_conncount_destroy(net, NFPROTO_INET, info->data);
1913 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
1914 struct hlist_head *head = &info->limits[i];
1915 struct ovs_ct_limit *ct_limit;
1916
1917 hlist_for_each_entry_rcu(ct_limit, head, hlist_node,
1918 lockdep_ovsl_is_held())
1919 kfree_rcu(ct_limit, rcu);
1920 }
1921 kfree(info->limits);
1922 kfree(info);
1923}
1924
1925static struct sk_buff *
1926ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
1927 struct ovs_header **ovs_reply_header)
1928{
1929 struct ovs_header *ovs_header = info->userhdr;
1930 struct sk_buff *skb;
1931
1932 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1933 if (!skb)
1934 return ERR_PTR(-ENOMEM);
1935
1936 *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
1937 info->snd_seq,
1938 &dp_ct_limit_genl_family, 0, cmd);
1939
1940 if (!*ovs_reply_header) {
1941 nlmsg_free(skb);
1942 return ERR_PTR(-EMSGSIZE);
1943 }
1944 (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
1945
1946 return skb;
1947}
1948
1949static bool check_zone_id(int zone_id, u16 *pzone)
1950{
1951 if (zone_id >= 0 && zone_id <= 65535) {
1952 *pzone = (u16)zone_id;
1953 return true;
1954 }
1955 return false;
1956}
1957
1958static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
1959 struct ovs_ct_limit_info *info)
1960{
1961 struct ovs_zone_limit *zone_limit;
1962 int rem;
1963 u16 zone;
1964
1965 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1966 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1967
1968 while (rem >= sizeof(*zone_limit)) {
1969 if (unlikely(zone_limit->zone_id ==
1970 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1971 ovs_lock();
1972 info->default_limit = zone_limit->limit;
1973 ovs_unlock();
1974 } else if (unlikely(!check_zone_id(
1975 zone_limit->zone_id, &zone))) {
1976 OVS_NLERR(true, "zone id is out of range");
1977 } else {
1978 struct ovs_ct_limit *ct_limit;
1979
1980 ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL);
1981 if (!ct_limit)
1982 return -ENOMEM;
1983
1984 ct_limit->zone = zone;
1985 ct_limit->limit = zone_limit->limit;
1986
1987 ovs_lock();
1988 ct_limit_set(info, ct_limit);
1989 ovs_unlock();
1990 }
1991 rem -= NLA_ALIGN(sizeof(*zone_limit));
1992 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
1993 NLA_ALIGN(sizeof(*zone_limit)));
1994 }
1995
1996 if (rem)
1997 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
1998
1999 return 0;
2000}
2001
2002static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
2003 struct ovs_ct_limit_info *info)
2004{
2005 struct ovs_zone_limit *zone_limit;
2006 int rem;
2007 u16 zone;
2008
2009 rem = NLA_ALIGN(nla_len(nla_zone_limit));
2010 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
2011
2012 while (rem >= sizeof(*zone_limit)) {
2013 if (unlikely(zone_limit->zone_id ==
2014 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
2015 ovs_lock();
2016 info->default_limit = OVS_CT_LIMIT_DEFAULT;
2017 ovs_unlock();
2018 } else if (unlikely(!check_zone_id(
2019 zone_limit->zone_id, &zone))) {
2020 OVS_NLERR(true, "zone id is out of range");
2021 } else {
2022 ovs_lock();
2023 ct_limit_del(info, zone);
2024 ovs_unlock();
2025 }
2026 rem -= NLA_ALIGN(sizeof(*zone_limit));
2027 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2028 NLA_ALIGN(sizeof(*zone_limit)));
2029 }
2030
2031 if (rem)
2032 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
2033
2034 return 0;
2035}
2036
2037static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
2038 struct sk_buff *reply)
2039{
2040 struct ovs_zone_limit zone_limit = {
2041 .zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE,
2042 .limit = info->default_limit,
2043 };
2044
2045 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2046}
2047
2048static int __ovs_ct_limit_get_zone_limit(struct net *net,
2049 struct nf_conncount_data *data,
2050 u16 zone_id, u32 limit,
2051 struct sk_buff *reply)
2052{
2053 struct nf_conntrack_zone ct_zone;
2054 struct ovs_zone_limit zone_limit;
2055 u32 conncount_key = zone_id;
2056
2057 zone_limit.zone_id = zone_id;
2058 zone_limit.limit = limit;
2059 nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
2060
2061 zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
2062 &ct_zone);
2063 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2064}
2065
2066static int ovs_ct_limit_get_zone_limit(struct net *net,
2067 struct nlattr *nla_zone_limit,
2068 struct ovs_ct_limit_info *info,
2069 struct sk_buff *reply)
2070{
2071 struct ovs_zone_limit *zone_limit;
2072 int rem, err;
2073 u32 limit;
2074 u16 zone;
2075
2076 rem = NLA_ALIGN(nla_len(nla_zone_limit));
2077 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
2078
2079 while (rem >= sizeof(*zone_limit)) {
2080 if (unlikely(zone_limit->zone_id ==
2081 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
2082 err = ovs_ct_limit_get_default_limit(info, reply);
2083 if (err)
2084 return err;
2085 } else if (unlikely(!check_zone_id(zone_limit->zone_id,
2086 &zone))) {
2087 OVS_NLERR(true, "zone id is out of range");
2088 } else {
2089 rcu_read_lock();
2090 limit = ct_limit_get(info, zone);
2091 rcu_read_unlock();
2092
2093 err = __ovs_ct_limit_get_zone_limit(
2094 net, info->data, zone, limit, reply);
2095 if (err)
2096 return err;
2097 }
2098 rem -= NLA_ALIGN(sizeof(*zone_limit));
2099 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2100 NLA_ALIGN(sizeof(*zone_limit)));
2101 }
2102
2103 if (rem)
2104 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
2105
2106 return 0;
2107}
2108
2109static int ovs_ct_limit_get_all_zone_limit(struct net *net,
2110 struct ovs_ct_limit_info *info,
2111 struct sk_buff *reply)
2112{
2113 struct ovs_ct_limit *ct_limit;
2114 struct hlist_head *head;
2115 int i, err = 0;
2116
2117 err = ovs_ct_limit_get_default_limit(info, reply);
2118 if (err)
2119 return err;
2120
2121 rcu_read_lock();
2122 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
2123 head = &info->limits[i];
2124 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
2125 err = __ovs_ct_limit_get_zone_limit(net, info->data,
2126 ct_limit->zone, ct_limit->limit, reply);
2127 if (err)
2128 goto exit_err;
2129 }
2130 }
2131
2132exit_err:
2133 rcu_read_unlock();
2134 return err;
2135}
2136
2137static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
2138{
2139 struct nlattr **a = info->attrs;
2140 struct sk_buff *reply;
2141 struct ovs_header *ovs_reply_header;
2142 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2143 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2144 int err;
2145
2146 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
2147 &ovs_reply_header);
2148 if (IS_ERR(reply))
2149 return PTR_ERR(reply);
2150
2151 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2152 err = -EINVAL;
2153 goto exit_err;
2154 }
2155
2156 err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2157 ct_limit_info);
2158 if (err)
2159 goto exit_err;
2160
2161 static_branch_enable(&ovs_ct_limit_enabled);
2162
2163 genlmsg_end(reply, ovs_reply_header);
2164 return genlmsg_reply(reply, info);
2165
2166exit_err:
2167 nlmsg_free(reply);
2168 return err;
2169}
2170
2171static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
2172{
2173 struct nlattr **a = info->attrs;
2174 struct sk_buff *reply;
2175 struct ovs_header *ovs_reply_header;
2176 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2177 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2178 int err;
2179
2180 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
2181 &ovs_reply_header);
2182 if (IS_ERR(reply))
2183 return PTR_ERR(reply);
2184
2185 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2186 err = -EINVAL;
2187 goto exit_err;
2188 }
2189
2190 err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2191 ct_limit_info);
2192 if (err)
2193 goto exit_err;
2194
2195 genlmsg_end(reply, ovs_reply_header);
2196 return genlmsg_reply(reply, info);
2197
2198exit_err:
2199 nlmsg_free(reply);
2200 return err;
2201}
2202
2203static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
2204{
2205 struct nlattr **a = info->attrs;
2206 struct nlattr *nla_reply;
2207 struct sk_buff *reply;
2208 struct ovs_header *ovs_reply_header;
2209 struct net *net = sock_net(skb->sk);
2210 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2211 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2212 int err;
2213
2214 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
2215 &ovs_reply_header);
2216 if (IS_ERR(reply))
2217 return PTR_ERR(reply);
2218
2219 nla_reply = nla_nest_start_noflag(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
2220 if (!nla_reply) {
2221 err = -EMSGSIZE;
2222 goto exit_err;
2223 }
2224
2225 if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2226 err = ovs_ct_limit_get_zone_limit(
2227 net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
2228 reply);
2229 if (err)
2230 goto exit_err;
2231 } else {
2232 err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
2233 reply);
2234 if (err)
2235 goto exit_err;
2236 }
2237
2238 nla_nest_end(reply, nla_reply);
2239 genlmsg_end(reply, ovs_reply_header);
2240 return genlmsg_reply(reply, info);
2241
2242exit_err:
2243 nlmsg_free(reply);
2244 return err;
2245}
2246
2247static const struct genl_small_ops ct_limit_genl_ops[] = {
2248 { .cmd = OVS_CT_LIMIT_CMD_SET,
2249 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2250 .flags = GENL_ADMIN_PERM,
2251
2252 .doit = ovs_ct_limit_cmd_set,
2253 },
2254 { .cmd = OVS_CT_LIMIT_CMD_DEL,
2255 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2256 .flags = GENL_ADMIN_PERM,
2257
2258 .doit = ovs_ct_limit_cmd_del,
2259 },
2260 { .cmd = OVS_CT_LIMIT_CMD_GET,
2261 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2262 .flags = 0,
2263 .doit = ovs_ct_limit_cmd_get,
2264 },
2265};
2266
2267static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
2268 .name = OVS_CT_LIMIT_MCGROUP,
2269};
2270
2271struct genl_family dp_ct_limit_genl_family __ro_after_init = {
2272 .hdrsize = sizeof(struct ovs_header),
2273 .name = OVS_CT_LIMIT_FAMILY,
2274 .version = OVS_CT_LIMIT_VERSION,
2275 .maxattr = OVS_CT_LIMIT_ATTR_MAX,
2276 .policy = ct_limit_policy,
2277 .netnsok = true,
2278 .parallel_ops = true,
2279 .small_ops = ct_limit_genl_ops,
2280 .n_small_ops = ARRAY_SIZE(ct_limit_genl_ops),
2281 .mcgrps = &ovs_ct_limit_multicast_group,
2282 .n_mcgrps = 1,
2283 .module = THIS_MODULE,
2284};
2285#endif
2286
2287int ovs_ct_init(struct net *net)
2288{
2289 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
2290 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2291
2292 if (nf_connlabels_get(net, n_bits - 1)) {
2293 ovs_net->xt_label = false;
2294 OVS_NLERR(true, "Failed to set connlabel length");
2295 } else {
2296 ovs_net->xt_label = true;
2297 }
2298
2299#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2300 return ovs_ct_limit_init(net, ovs_net);
2301#else
2302 return 0;
2303#endif
2304}
2305
2306void ovs_ct_exit(struct net *net)
2307{
2308 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2309
2310#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2311 ovs_ct_limit_exit(net, ovs_net);
2312#endif
2313
2314 if (ovs_net->xt_label)
2315 nf_connlabels_put(net);
2316}
2317