1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Nicira, Inc.
6 #include <linux/module.h>
7 #include <linux/openvswitch.h>
10 #include <linux/sctp.h>
11 #include <linux/static_key.h>
12 #include <linux/string_helpers.h>
14 #include <net/genetlink.h>
15 #include <net/netfilter/nf_conntrack_core.h>
16 #include <net/netfilter/nf_conntrack_count.h>
17 #include <net/netfilter/nf_conntrack_helper.h>
18 #include <net/netfilter/nf_conntrack_labels.h>
19 #include <net/netfilter/nf_conntrack_seqadj.h>
20 #include <net/netfilter/nf_conntrack_timeout.h>
21 #include <net/netfilter/nf_conntrack_zones.h>
22 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
23 #include <net/ipv6_frag.h>
25 #if IS_ENABLED(CONFIG_NF_NAT)
26 #include <net/netfilter/nf_nat.h>
29 #include <net/netfilter/nf_conntrack_act_ct.h>
32 #include "conntrack.h"
34 #include "flow_netlink.h"
36 struct ovs_ct_len_tbl {
41 /* Metadata mark for masked write to conntrack mark */
47 /* Metadata label for masked write to conntrack label. */
49 struct ovs_key_ct_labels value;
50 struct ovs_key_ct_labels mask;
54 OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
55 OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
56 OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
59 /* Conntrack action context for execution. */
60 struct ovs_conntrack_info {
61 struct nf_conntrack_helper *helper;
62 struct nf_conntrack_zone zone;
65 u8 nat : 3; /* enum ovs_ct_nat */
67 u8 have_eventmask : 1;
69 u32 eventmask; /* Mask of 1 << IPCT_*. */
71 struct md_labels labels;
72 char timeout[CTNL_TIMEOUT_NAME_MAX];
73 struct nf_ct_timeout *nf_ct_timeout;
74 #if IS_ENABLED(CONFIG_NF_NAT)
75 struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */
79 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
80 #define OVS_CT_LIMIT_UNLIMITED 0
81 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
82 #define CT_LIMIT_HASH_BUCKETS 512
83 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled);
86 /* Elements in ovs_ct_limit_info->limits hash table */
87 struct hlist_node hlist_node;
93 struct ovs_ct_limit_info {
95 struct hlist_head *limits;
96 struct nf_conncount_data *data;
99 static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = {
100 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, },
104 static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
106 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
108 static u16 key_to_nfproto(const struct sw_flow_key *key)
110 switch (ntohs(key->eth.type)) {
116 return NFPROTO_UNSPEC;
120 /* Map SKB connection state into the values used by flow definition. */
121 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
123 u8 ct_state = OVS_CS_F_TRACKED;
126 case IP_CT_ESTABLISHED_REPLY:
127 case IP_CT_RELATED_REPLY:
128 ct_state |= OVS_CS_F_REPLY_DIR;
135 case IP_CT_ESTABLISHED:
136 case IP_CT_ESTABLISHED_REPLY:
137 ct_state |= OVS_CS_F_ESTABLISHED;
140 case IP_CT_RELATED_REPLY:
141 ct_state |= OVS_CS_F_RELATED;
144 ct_state |= OVS_CS_F_NEW;
153 static u32 ovs_ct_get_mark(const struct nf_conn *ct)
155 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
156 return ct ? READ_ONCE(ct->mark) : 0;
162 /* Guard against conntrack labels max size shrinking below 128 bits. */
163 #if NF_CT_LABELS_MAX_SIZE < 16
164 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
167 static void ovs_ct_get_labels(const struct nf_conn *ct,
168 struct ovs_key_ct_labels *labels)
170 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
173 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
175 memset(labels, 0, OVS_CT_LABELS_LEN);
178 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
179 const struct nf_conntrack_tuple *orig,
182 key->ct_orig_proto = orig->dst.protonum;
183 if (orig->dst.protonum == icmp_proto) {
184 key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
185 key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
187 key->ct.orig_tp.src = orig->src.u.all;
188 key->ct.orig_tp.dst = orig->dst.u.all;
192 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
193 const struct nf_conntrack_zone *zone,
194 const struct nf_conn *ct)
196 key->ct_state = state;
197 key->ct_zone = zone->id;
198 key->ct.mark = ovs_ct_get_mark(ct);
199 ovs_ct_get_labels(ct, &key->ct.labels);
202 const struct nf_conntrack_tuple *orig;
204 /* Use the master if we have one. */
207 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
209 /* IP version must match with the master connection. */
210 if (key->eth.type == htons(ETH_P_IP) &&
211 nf_ct_l3num(ct) == NFPROTO_IPV4) {
212 key->ipv4.ct_orig.src = orig->src.u3.ip;
213 key->ipv4.ct_orig.dst = orig->dst.u3.ip;
214 __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
216 } else if (key->eth.type == htons(ETH_P_IPV6) &&
217 !sw_flow_key_is_nd(key) &&
218 nf_ct_l3num(ct) == NFPROTO_IPV6) {
219 key->ipv6.ct_orig.src = orig->src.u3.in6;
220 key->ipv6.ct_orig.dst = orig->dst.u3.in6;
221 __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
225 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
226 * original direction key fields.
228 key->ct_orig_proto = 0;
231 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
232 * previously sent the packet to conntrack via the ct action. If
233 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
234 * initialized from the connection status.
236 static void ovs_ct_update_key(const struct sk_buff *skb,
237 const struct ovs_conntrack_info *info,
238 struct sw_flow_key *key, bool post_ct,
241 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
242 enum ip_conntrack_info ctinfo;
246 ct = nf_ct_get(skb, &ctinfo);
248 state = ovs_ct_get_state(ctinfo);
249 /* All unconfirmed entries are NEW connections. */
250 if (!nf_ct_is_confirmed(ct))
251 state |= OVS_CS_F_NEW;
252 /* OVS persists the related flag for the duration of the
256 state |= OVS_CS_F_RELATED;
257 if (keep_nat_flags) {
258 state |= key->ct_state & OVS_CS_F_NAT_MASK;
260 if (ct->status & IPS_SRC_NAT)
261 state |= OVS_CS_F_SRC_NAT;
262 if (ct->status & IPS_DST_NAT)
263 state |= OVS_CS_F_DST_NAT;
265 zone = nf_ct_zone(ct);
266 } else if (post_ct) {
267 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
271 __ovs_ct_update_key(key, state, zone, ct);
274 /* This is called to initialize CT key fields possibly coming in from the local
277 void ovs_ct_fill_key(const struct sk_buff *skb,
278 struct sw_flow_key *key,
281 ovs_ct_update_key(skb, NULL, key, post_ct, false);
284 int ovs_ct_put_key(const struct sw_flow_key *swkey,
285 const struct sw_flow_key *output, struct sk_buff *skb)
287 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
290 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
291 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
294 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
295 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
298 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
299 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
303 if (swkey->ct_orig_proto) {
304 if (swkey->eth.type == htons(ETH_P_IP)) {
305 struct ovs_key_ct_tuple_ipv4 orig;
307 memset(&orig, 0, sizeof(orig));
308 orig.ipv4_src = output->ipv4.ct_orig.src;
309 orig.ipv4_dst = output->ipv4.ct_orig.dst;
310 orig.src_port = output->ct.orig_tp.src;
311 orig.dst_port = output->ct.orig_tp.dst;
312 orig.ipv4_proto = output->ct_orig_proto;
314 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
315 sizeof(orig), &orig))
317 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
318 struct ovs_key_ct_tuple_ipv6 orig;
320 memset(&orig, 0, sizeof(orig));
321 memcpy(orig.ipv6_src, output->ipv6.ct_orig.src.s6_addr32,
322 sizeof(orig.ipv6_src));
323 memcpy(orig.ipv6_dst, output->ipv6.ct_orig.dst.s6_addr32,
324 sizeof(orig.ipv6_dst));
325 orig.src_port = output->ct.orig_tp.src;
326 orig.dst_port = output->ct.orig_tp.dst;
327 orig.ipv6_proto = output->ct_orig_proto;
329 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
330 sizeof(orig), &orig))
338 static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
339 u32 ct_mark, u32 mask)
341 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
344 new_mark = ct_mark | (READ_ONCE(ct->mark) & ~(mask));
345 if (READ_ONCE(ct->mark) != new_mark) {
346 WRITE_ONCE(ct->mark, new_mark);
347 if (nf_ct_is_confirmed(ct))
348 nf_conntrack_event_cache(IPCT_MARK, ct);
349 key->ct.mark = new_mark;
358 static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
360 struct nf_conn_labels *cl;
362 cl = nf_ct_labels_find(ct);
364 nf_ct_labels_ext_add(ct);
365 cl = nf_ct_labels_find(ct);
371 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
372 * since the new connection is not yet confirmed, and thus no-one else has
373 * access to it's labels, we simply write them over.
375 static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
376 const struct ovs_key_ct_labels *labels,
377 const struct ovs_key_ct_labels *mask)
379 struct nf_conn_labels *cl, *master_cl;
380 bool have_mask = labels_nonzero(mask);
382 /* Inherit master's labels to the related connection? */
383 master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
385 if (!master_cl && !have_mask)
386 return 0; /* Nothing to do. */
388 cl = ovs_ct_get_conn_labels(ct);
392 /* Inherit the master's labels, if any. */
397 u32 *dst = (u32 *)cl->bits;
400 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
401 dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
402 (labels->ct_labels_32[i]
403 & mask->ct_labels_32[i]);
406 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
407 * IPCT_LABEL bit is set in the event cache.
409 nf_conntrack_event_cache(IPCT_LABEL, ct);
411 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
416 static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
417 const struct ovs_key_ct_labels *labels,
418 const struct ovs_key_ct_labels *mask)
420 struct nf_conn_labels *cl;
423 cl = ovs_ct_get_conn_labels(ct);
427 err = nf_connlabels_replace(ct, labels->ct_labels_32,
429 OVS_CT_LABELS_LEN_32);
433 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
438 static int ovs_ct_handle_fragments(struct net *net, struct sw_flow_key *key,
439 u16 zone, int family, struct sk_buff *skb)
441 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
444 err = nf_ct_handle_fragments(net, skb, zone, family, &key->ip.proto, &ovs_cb.mru);
448 /* The key extracted from the fragment that completed this datagram
449 * likely didn't have an L4 header, so regenerate it.
451 ovs_flow_key_update_l3l4(skb, key);
452 key->ip.frag = OVS_FRAG_TYPE_NONE;
453 *OVS_CB(skb) = ovs_cb;
458 static struct nf_conntrack_expect *
459 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
460 u16 proto, const struct sk_buff *skb)
462 struct nf_conntrack_tuple tuple;
463 struct nf_conntrack_expect *exp;
465 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
468 exp = __nf_ct_expect_find(net, zone, &tuple);
470 struct nf_conntrack_tuple_hash *h;
472 /* Delete existing conntrack entry, if it clashes with the
473 * expectation. This can happen since conntrack ALGs do not
474 * check for clashes between (new) expectations and existing
475 * conntrack entries. nf_conntrack_in() will check the
476 * expectations only if a conntrack entry can not be found,
477 * which can lead to OVS finding the expectation (here) in the
478 * init direction, but which will not be removed by the
479 * nf_conntrack_in() call, if a matching conntrack entry is
480 * found instead. In this case all init direction packets
481 * would be reported as new related packets, while reply
482 * direction packets would be reported as un-related
483 * established packets.
485 h = nf_conntrack_find_get(net, zone, &tuple);
487 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
489 nf_ct_delete(ct, 0, 0);
497 /* This replicates logic from nf_conntrack_core.c that is not exported. */
498 static enum ip_conntrack_info
499 ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
501 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
503 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
504 return IP_CT_ESTABLISHED_REPLY;
505 /* Once we've had two way comms, always ESTABLISHED. */
506 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
507 return IP_CT_ESTABLISHED;
508 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
509 return IP_CT_RELATED;
513 /* Find an existing connection which this packet belongs to without
514 * re-attributing statistics or modifying the connection state. This allows an
515 * skb->_nfct lost due to an upcall to be recovered during actions execution.
517 * Must be called with rcu_read_lock.
519 * On success, populates skb->_nfct and returns the connection. Returns NULL
520 * if there is no existing entry.
522 static struct nf_conn *
523 ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
524 u8 l3num, struct sk_buff *skb, bool natted)
526 struct nf_conntrack_tuple tuple;
527 struct nf_conntrack_tuple_hash *h;
530 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num,
532 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
536 /* Must invert the tuple if skb has been transformed by NAT. */
538 struct nf_conntrack_tuple inverse;
540 if (!nf_ct_invert_tuple(&inverse, &tuple)) {
541 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
547 /* look for tuple match */
548 h = nf_conntrack_find_get(net, zone, &tuple);
550 return NULL; /* Not found. */
552 ct = nf_ct_tuplehash_to_ctrack(h);
554 /* Inverted packet tuple matches the reverse direction conntrack tuple,
555 * select the other tuplehash to get the right 'ctinfo' bits for this
559 h = &ct->tuplehash[!h->tuple.dst.dir];
561 nf_ct_set(skb, ct, ovs_ct_get_info(h));
566 struct nf_conn *ovs_ct_executed(struct net *net,
567 const struct sw_flow_key *key,
568 const struct ovs_conntrack_info *info,
572 struct nf_conn *ct = NULL;
574 /* If no ct, check if we have evidence that an existing conntrack entry
575 * might be found for this skb. This happens when we lose a skb->_nfct
576 * due to an upcall, or if the direction is being forced. If the
577 * connection was not confirmed, it is not cached and needs to be run
578 * through conntrack again.
580 *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
581 !(key->ct_state & OVS_CS_F_INVALID) &&
582 (key->ct_zone == info->zone.id);
584 if (*ct_executed || (!key->ct_state && info->force)) {
585 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
593 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
594 static bool skb_nfct_cached(struct net *net,
595 const struct sw_flow_key *key,
596 const struct ovs_conntrack_info *info,
599 enum ip_conntrack_info ctinfo;
601 bool ct_executed = true;
603 ct = nf_ct_get(skb, &ctinfo);
605 ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
608 nf_ct_get(skb, &ctinfo);
612 if (!net_eq(net, read_pnet(&ct->ct_net)))
614 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
617 struct nf_conn_help *help;
619 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
620 if (help && rcu_access_pointer(help->helper) != info->helper)
623 if (info->nf_ct_timeout) {
624 struct nf_conn_timeout *timeout_ext;
626 timeout_ext = nf_ct_timeout_find(ct);
627 if (!timeout_ext || info->nf_ct_timeout !=
628 rcu_dereference(timeout_ext->timeout))
631 /* Force conntrack entry direction to the current packet? */
632 if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
633 /* Delete the conntrack entry if confirmed, else just release
636 if (nf_ct_is_confirmed(ct))
637 nf_ct_delete(ct, 0, 0);
640 nf_ct_set(skb, NULL, 0);
647 #if IS_ENABLED(CONFIG_NF_NAT)
648 static void ovs_nat_update_key(struct sw_flow_key *key,
649 const struct sk_buff *skb,
650 enum nf_nat_manip_type maniptype)
652 if (maniptype == NF_NAT_MANIP_SRC) {
655 key->ct_state |= OVS_CS_F_SRC_NAT;
656 if (key->eth.type == htons(ETH_P_IP))
657 key->ipv4.addr.src = ip_hdr(skb)->saddr;
658 else if (key->eth.type == htons(ETH_P_IPV6))
659 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
660 sizeof(key->ipv6.addr.src));
664 if (key->ip.proto == IPPROTO_UDP)
665 src = udp_hdr(skb)->source;
666 else if (key->ip.proto == IPPROTO_TCP)
667 src = tcp_hdr(skb)->source;
668 else if (key->ip.proto == IPPROTO_SCTP)
669 src = sctp_hdr(skb)->source;
677 key->ct_state |= OVS_CS_F_DST_NAT;
678 if (key->eth.type == htons(ETH_P_IP))
679 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
680 else if (key->eth.type == htons(ETH_P_IPV6))
681 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
682 sizeof(key->ipv6.addr.dst));
686 if (key->ip.proto == IPPROTO_UDP)
687 dst = udp_hdr(skb)->dest;
688 else if (key->ip.proto == IPPROTO_TCP)
689 dst = tcp_hdr(skb)->dest;
690 else if (key->ip.proto == IPPROTO_SCTP)
691 dst = sctp_hdr(skb)->dest;
699 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
700 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
701 const struct ovs_conntrack_info *info,
702 struct sk_buff *skb, struct nf_conn *ct,
703 enum ip_conntrack_info ctinfo)
707 if (!(info->nat & OVS_CT_NAT))
709 if (info->nat & OVS_CT_SRC_NAT)
710 action |= BIT(NF_NAT_MANIP_SRC);
711 if (info->nat & OVS_CT_DST_NAT)
712 action |= BIT(NF_NAT_MANIP_DST);
714 err = nf_ct_nat(skb, ct, ctinfo, &action, &info->range, info->commit);
716 if (action & BIT(NF_NAT_MANIP_SRC))
717 ovs_nat_update_key(key, skb, NF_NAT_MANIP_SRC);
718 if (action & BIT(NF_NAT_MANIP_DST))
719 ovs_nat_update_key(key, skb, NF_NAT_MANIP_DST);
723 #else /* !CONFIG_NF_NAT */
724 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
725 const struct ovs_conntrack_info *info,
726 struct sk_buff *skb, struct nf_conn *ct,
727 enum ip_conntrack_info ctinfo)
733 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
734 * not done already. Update key with new CT state after passing the packet
736 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
737 * set to NULL and 0 will be returned.
739 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
740 const struct ovs_conntrack_info *info,
743 /* If we are recirculating packets to match on conntrack fields and
744 * committing with a separate conntrack action, then we don't need to
745 * actually run the packet through conntrack twice unless it's for a
748 bool cached = skb_nfct_cached(net, key, info, skb);
749 enum ip_conntrack_info ctinfo;
753 struct nf_hook_state state = {
754 .hook = NF_INET_PRE_ROUTING,
758 struct nf_conn *tmpl = info->ct;
761 /* Associate skb with specified zone. */
763 ct = nf_ct_get(skb, &ctinfo);
765 nf_conntrack_get(&tmpl->ct_general);
766 nf_ct_set(skb, tmpl, IP_CT_NEW);
769 err = nf_conntrack_in(skb, &state);
770 if (err != NF_ACCEPT)
773 /* Clear CT state NAT flags to mark that we have not yet done
774 * NAT after the nf_conntrack_in() call. We can actually clear
775 * the whole state, as it will be re-initialized below.
779 /* Update the key, but keep the NAT flags. */
780 ovs_ct_update_key(skb, info, key, true, true);
783 ct = nf_ct_get(skb, &ctinfo);
785 bool add_helper = false;
787 /* Packets starting a new connection must be NATted before the
788 * helper, so that the helper knows about the NAT. We enforce
789 * this by delaying both NAT and helper calls for unconfirmed
790 * connections until the committing CT action. For later
791 * packets NAT and Helper may be called in either order.
793 * NAT will be done only if the CT action has NAT, and only
794 * once per packet (per zone), as guarded by the NAT bits in
797 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
798 (nf_ct_is_confirmed(ct) || info->commit) &&
799 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
803 /* Userspace may decide to perform a ct lookup without a helper
804 * specified followed by a (recirculate and) commit with one,
805 * or attach a helper in a later commit. Therefore, for
806 * connections which we will commit, we may need to attach
809 if (!nf_ct_is_confirmed(ct) && info->commit &&
810 info->helper && !nfct_help(ct)) {
811 int err = __nf_ct_try_assign_helper(ct, info->ct,
817 /* helper installed, add seqadj if NAT is required */
818 if (info->nat && !nfct_seqadj(ct)) {
819 if (!nfct_seqadj_ext_add(ct))
824 /* Call the helper only if:
825 * - nf_conntrack_in() was executed above ("!cached") or a
826 * helper was just attached ("add_helper") for a confirmed
828 * - When committing an unconfirmed connection.
830 if ((nf_ct_is_confirmed(ct) ? !cached || add_helper :
832 nf_ct_helper(skb, ct, ctinfo, info->family) != NF_ACCEPT) {
836 if (nf_ct_protonum(ct) == IPPROTO_TCP &&
837 nf_ct_is_confirmed(ct) && nf_conntrack_tcp_established(ct)) {
838 /* Be liberal for tcp packets so that out-of-window
839 * packets are not marked invalid.
841 nf_ct_set_tcp_be_liberal(ct);
844 nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
850 /* Lookup connection and read fields into key. */
851 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
852 const struct ovs_conntrack_info *info,
855 struct nf_conntrack_expect *exp;
857 /* If we pass an expected packet through nf_conntrack_in() the
858 * expectation is typically removed, but the packet could still be
859 * lost in upcall processing. To prevent this from happening we
860 * perform an explicit expectation lookup. Expected connections are
861 * always new, and will be passed through conntrack only when they are
862 * committed, as it is OK to remove the expectation at that time.
864 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
868 /* NOTE: New connections are NATted and Helped only when
869 * committed, so we are not calling into NAT here.
871 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
872 __ovs_ct_update_key(key, state, &info->zone, exp->master);
877 err = __ovs_ct_lookup(net, key, info, skb);
881 ct = (struct nf_conn *)skb_nfct(skb);
883 nf_ct_deliver_cached_events(ct);
889 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
893 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
894 if (labels->ct_labels_32[i])
900 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
901 static struct hlist_head *ct_limit_hash_bucket(
902 const struct ovs_ct_limit_info *info, u16 zone)
904 return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
907 /* Call with ovs_mutex */
908 static void ct_limit_set(const struct ovs_ct_limit_info *info,
909 struct ovs_ct_limit *new_ct_limit)
911 struct ovs_ct_limit *ct_limit;
912 struct hlist_head *head;
914 head = ct_limit_hash_bucket(info, new_ct_limit->zone);
915 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
916 if (ct_limit->zone == new_ct_limit->zone) {
917 hlist_replace_rcu(&ct_limit->hlist_node,
918 &new_ct_limit->hlist_node);
919 kfree_rcu(ct_limit, rcu);
924 hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
927 /* Call with ovs_mutex */
928 static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
930 struct ovs_ct_limit *ct_limit;
931 struct hlist_head *head;
932 struct hlist_node *n;
934 head = ct_limit_hash_bucket(info, zone);
935 hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
936 if (ct_limit->zone == zone) {
937 hlist_del_rcu(&ct_limit->hlist_node);
938 kfree_rcu(ct_limit, rcu);
944 /* Call with RCU read lock */
945 static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
947 struct ovs_ct_limit *ct_limit;
948 struct hlist_head *head;
950 head = ct_limit_hash_bucket(info, zone);
951 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
952 if (ct_limit->zone == zone)
953 return ct_limit->limit;
956 return info->default_limit;
959 static int ovs_ct_check_limit(struct net *net,
960 const struct ovs_conntrack_info *info,
961 const struct nf_conntrack_tuple *tuple)
963 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
964 const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
965 u32 per_zone_limit, connections;
968 conncount_key = info->zone.id;
970 per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
971 if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
974 connections = nf_conncount_count(net, ct_limit_info->data,
975 &conncount_key, tuple, &info->zone);
976 if (connections > per_zone_limit)
983 /* Lookup connection and confirm if unconfirmed. */
984 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
985 const struct ovs_conntrack_info *info,
988 enum ip_conntrack_info ctinfo;
992 err = __ovs_ct_lookup(net, key, info, skb);
996 /* The connection could be invalid, in which case this is a no-op.*/
997 ct = nf_ct_get(skb, &ctinfo);
1001 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1002 if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
1003 if (!nf_ct_is_confirmed(ct)) {
1004 err = ovs_ct_check_limit(net, info,
1005 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1007 net_warn_ratelimited("openvswitch: zone: %u "
1008 "exceeds conntrack limit\n",
1016 /* Set the conntrack event mask if given. NEW and DELETE events have
1017 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1018 * typically would receive many kinds of updates. Setting the event
1019 * mask allows those events to be filtered. The set event mask will
1020 * remain in effect for the lifetime of the connection unless changed
1021 * by a further CT action with both the commit flag and the eventmask
1023 if (info->have_eventmask) {
1024 struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
1027 cache->ctmask = info->eventmask;
1030 /* Apply changes before confirming the connection so that the initial
1031 * conntrack NEW netlink event carries the values given in the CT
1034 if (info->mark.mask) {
1035 err = ovs_ct_set_mark(ct, key, info->mark.value,
1040 if (!nf_ct_is_confirmed(ct)) {
1041 err = ovs_ct_init_labels(ct, key, &info->labels.value,
1042 &info->labels.mask);
1046 nf_conn_act_ct_ext_add(ct);
1047 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1048 labels_nonzero(&info->labels.mask)) {
1049 err = ovs_ct_set_labels(ct, key, &info->labels.value,
1050 &info->labels.mask);
1054 /* This will take care of sending queued events even if the connection
1055 * is already confirmed.
1057 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1063 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1064 * value if 'skb' is freed.
1066 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
1067 struct sw_flow_key *key,
1068 const struct ovs_conntrack_info *info)
1073 /* The conntrack module expects to be working at L3. */
1074 nh_ofs = skb_network_offset(skb);
1075 skb_pull_rcsum(skb, nh_ofs);
1077 err = nf_ct_skb_network_trim(skb, info->family);
1083 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
1084 err = ovs_ct_handle_fragments(net, key, info->zone.id,
1091 err = ovs_ct_commit(net, key, info, skb);
1093 err = ovs_ct_lookup(net, key, info, skb);
1095 skb_push_rcsum(skb, nh_ofs);
1101 int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
1103 enum ip_conntrack_info ctinfo;
1106 ct = nf_ct_get(skb, &ctinfo);
1109 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1112 ovs_ct_fill_key(skb, key, false);
1117 #if IS_ENABLED(CONFIG_NF_NAT)
1118 static int parse_nat(const struct nlattr *attr,
1119 struct ovs_conntrack_info *info, bool log)
1123 bool have_ip_max = false;
1124 bool have_proto_max = false;
1125 bool ip_vers = (info->family == NFPROTO_IPV6);
1127 nla_for_each_nested(a, attr, rem) {
1128 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1129 [OVS_NAT_ATTR_SRC] = {0, 0},
1130 [OVS_NAT_ATTR_DST] = {0, 0},
1131 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1132 sizeof(struct in6_addr)},
1133 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1134 sizeof(struct in6_addr)},
1135 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1136 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1137 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1138 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1139 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1141 int type = nla_type(a);
1143 if (type > OVS_NAT_ATTR_MAX) {
1144 OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
1145 type, OVS_NAT_ATTR_MAX);
1149 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1150 OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
1152 ovs_nat_attr_lens[type][ip_vers]);
1157 case OVS_NAT_ATTR_SRC:
1158 case OVS_NAT_ATTR_DST:
1160 OVS_NLERR(log, "Only one type of NAT may be specified");
1163 info->nat |= OVS_CT_NAT;
1164 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1165 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1168 case OVS_NAT_ATTR_IP_MIN:
1169 nla_memcpy(&info->range.min_addr, a,
1170 sizeof(info->range.min_addr));
1171 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1174 case OVS_NAT_ATTR_IP_MAX:
1176 nla_memcpy(&info->range.max_addr, a,
1177 sizeof(info->range.max_addr));
1178 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1181 case OVS_NAT_ATTR_PROTO_MIN:
1182 info->range.min_proto.all = htons(nla_get_u16(a));
1183 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1186 case OVS_NAT_ATTR_PROTO_MAX:
1187 have_proto_max = true;
1188 info->range.max_proto.all = htons(nla_get_u16(a));
1189 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1192 case OVS_NAT_ATTR_PERSISTENT:
1193 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1196 case OVS_NAT_ATTR_PROTO_HASH:
1197 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1200 case OVS_NAT_ATTR_PROTO_RANDOM:
1201 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1205 OVS_NLERR(log, "Unknown nat attribute (%d)", type);
1211 OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
1215 /* Do not allow flags if no type is given. */
1216 if (info->range.flags) {
1218 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1222 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1223 } else if (!info->commit) {
1225 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1229 /* Allow missing IP_MAX. */
1230 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1231 memcpy(&info->range.max_addr, &info->range.min_addr,
1232 sizeof(info->range.max_addr));
1234 /* Allow missing PROTO_MAX. */
1235 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1237 info->range.max_proto.all = info->range.min_proto.all;
1243 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
1244 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
1245 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
1246 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1247 .maxlen = sizeof(u16) },
1248 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1249 .maxlen = sizeof(struct md_mark) },
1250 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1251 .maxlen = sizeof(struct md_labels) },
1252 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
1253 .maxlen = NF_CT_HELPER_NAME_LEN },
1254 #if IS_ENABLED(CONFIG_NF_NAT)
1255 /* NAT length is checked when parsing the nested attributes. */
1256 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1258 [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
1259 .maxlen = sizeof(u32) },
1260 [OVS_CT_ATTR_TIMEOUT] = { .minlen = 1,
1261 .maxlen = CTNL_TIMEOUT_NAME_MAX },
1264 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
1265 const char **helper, bool log)
1270 nla_for_each_nested(a, attr, rem) {
1271 int type = nla_type(a);
1275 if (type > OVS_CT_ATTR_MAX) {
1277 "Unknown conntrack attr (type=%d, max=%d)",
1278 type, OVS_CT_ATTR_MAX);
1282 maxlen = ovs_ct_attr_lens[type].maxlen;
1283 minlen = ovs_ct_attr_lens[type].minlen;
1284 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1286 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1287 type, nla_len(a), maxlen);
1292 case OVS_CT_ATTR_FORCE_COMMIT:
1295 case OVS_CT_ATTR_COMMIT:
1296 info->commit = true;
1298 #ifdef CONFIG_NF_CONNTRACK_ZONES
1299 case OVS_CT_ATTR_ZONE:
1300 info->zone.id = nla_get_u16(a);
1303 #ifdef CONFIG_NF_CONNTRACK_MARK
1304 case OVS_CT_ATTR_MARK: {
1305 struct md_mark *mark = nla_data(a);
1308 OVS_NLERR(log, "ct_mark mask cannot be 0");
1315 #ifdef CONFIG_NF_CONNTRACK_LABELS
1316 case OVS_CT_ATTR_LABELS: {
1317 struct md_labels *labels = nla_data(a);
1319 if (!labels_nonzero(&labels->mask)) {
1320 OVS_NLERR(log, "ct_labels mask cannot be 0");
1323 info->labels = *labels;
1327 case OVS_CT_ATTR_HELPER:
1328 *helper = nla_data(a);
1329 if (!string_is_terminated(*helper, nla_len(a))) {
1330 OVS_NLERR(log, "Invalid conntrack helper");
1334 #if IS_ENABLED(CONFIG_NF_NAT)
1335 case OVS_CT_ATTR_NAT: {
1336 int err = parse_nat(a, info, log);
1343 case OVS_CT_ATTR_EVENTMASK:
1344 info->have_eventmask = true;
1345 info->eventmask = nla_get_u32(a);
1347 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1348 case OVS_CT_ATTR_TIMEOUT:
1349 memcpy(info->timeout, nla_data(a), nla_len(a));
1350 if (!string_is_terminated(info->timeout, nla_len(a))) {
1351 OVS_NLERR(log, "Invalid conntrack timeout");
1358 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1364 #ifdef CONFIG_NF_CONNTRACK_MARK
1365 if (!info->commit && info->mark.mask) {
1367 "Setting conntrack mark requires 'commit' flag.");
1371 #ifdef CONFIG_NF_CONNTRACK_LABELS
1372 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1374 "Setting conntrack labels requires 'commit' flag.");
1379 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1386 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
1388 if (attr == OVS_KEY_ATTR_CT_STATE)
1390 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1391 attr == OVS_KEY_ATTR_CT_ZONE)
1393 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1394 attr == OVS_KEY_ATTR_CT_MARK)
1396 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1397 attr == OVS_KEY_ATTR_CT_LABELS) {
1398 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1400 return ovs_net->xt_label;
1406 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1407 const struct sw_flow_key *key,
1408 struct sw_flow_actions **sfa, bool log)
1410 struct ovs_conntrack_info ct_info;
1411 const char *helper = NULL;
1415 family = key_to_nfproto(key);
1416 if (family == NFPROTO_UNSPEC) {
1417 OVS_NLERR(log, "ct family unspecified");
1421 memset(&ct_info, 0, sizeof(ct_info));
1422 ct_info.family = family;
1424 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1425 NF_CT_DEFAULT_ZONE_DIR, 0);
1427 err = parse_ct(attr, &ct_info, &helper, log);
1431 /* Set up template for tracking connections in specific zones. */
1432 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1434 OVS_NLERR(log, "Failed to allocate conntrack template");
1438 if (ct_info.timeout[0]) {
1439 if (nf_ct_set_timeout(net, ct_info.ct, family, key->ip.proto,
1441 pr_info_ratelimited("Failed to associated timeout "
1442 "policy `%s'\n", ct_info.timeout);
1444 ct_info.nf_ct_timeout = rcu_dereference(
1445 nf_ct_timeout_find(ct_info.ct)->timeout);
1450 err = nf_ct_add_helper(ct_info.ct, helper, ct_info.family,
1451 key->ip.proto, ct_info.nat, &ct_info.helper);
1453 OVS_NLERR(log, "Failed to add %s helper %d", helper, err);
1458 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1459 sizeof(ct_info), log);
1463 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1466 __ovs_ct_free_action(&ct_info);
1470 #if IS_ENABLED(CONFIG_NF_NAT)
1471 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1472 struct sk_buff *skb)
1474 struct nlattr *start;
1476 start = nla_nest_start_noflag(skb, OVS_CT_ATTR_NAT);
1480 if (info->nat & OVS_CT_SRC_NAT) {
1481 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1483 } else if (info->nat & OVS_CT_DST_NAT) {
1484 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1490 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
1491 if (IS_ENABLED(CONFIG_NF_NAT) &&
1492 info->family == NFPROTO_IPV4) {
1493 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1494 info->range.min_addr.ip) ||
1495 (info->range.max_addr.ip
1496 != info->range.min_addr.ip &&
1497 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1498 info->range.max_addr.ip))))
1500 } else if (IS_ENABLED(CONFIG_IPV6) &&
1501 info->family == NFPROTO_IPV6) {
1502 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1503 &info->range.min_addr.in6) ||
1504 (memcmp(&info->range.max_addr.in6,
1505 &info->range.min_addr.in6,
1506 sizeof(info->range.max_addr.in6)) &&
1507 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1508 &info->range.max_addr.in6))))
1514 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1515 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1516 ntohs(info->range.min_proto.all)) ||
1517 (info->range.max_proto.all != info->range.min_proto.all &&
1518 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1519 ntohs(info->range.max_proto.all)))))
1522 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1523 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1525 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1526 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1528 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1529 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1532 nla_nest_end(skb, start);
1538 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1539 struct sk_buff *skb)
1541 struct nlattr *start;
1543 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CT);
1547 if (ct_info->commit && nla_put_flag(skb, ct_info->force
1548 ? OVS_CT_ATTR_FORCE_COMMIT
1549 : OVS_CT_ATTR_COMMIT))
1551 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1552 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1554 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
1555 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1558 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1559 labels_nonzero(&ct_info->labels.mask) &&
1560 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1563 if (ct_info->helper) {
1564 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1565 ct_info->helper->name))
1568 if (ct_info->have_eventmask &&
1569 nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
1571 if (ct_info->timeout[0]) {
1572 if (nla_put_string(skb, OVS_CT_ATTR_TIMEOUT, ct_info->timeout))
1576 #if IS_ENABLED(CONFIG_NF_NAT)
1577 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1580 nla_nest_end(skb, start);
1585 void ovs_ct_free_action(const struct nlattr *a)
1587 struct ovs_conntrack_info *ct_info = nla_data(a);
1589 __ovs_ct_free_action(ct_info);
1592 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1594 if (ct_info->helper) {
1595 #if IS_ENABLED(CONFIG_NF_NAT)
1597 nf_nat_helper_put(ct_info->helper);
1599 nf_conntrack_helper_put(ct_info->helper);
1602 if (ct_info->timeout[0])
1603 nf_ct_destroy_timeout(ct_info->ct);
1604 nf_ct_tmpl_free(ct_info->ct);
1608 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1609 static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
1613 ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
1615 if (!ovs_net->ct_limit_info)
1618 ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
1619 ovs_net->ct_limit_info->limits =
1620 kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
1622 if (!ovs_net->ct_limit_info->limits) {
1623 kfree(ovs_net->ct_limit_info);
1627 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
1628 INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
1630 ovs_net->ct_limit_info->data =
1631 nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
1633 if (IS_ERR(ovs_net->ct_limit_info->data)) {
1634 err = PTR_ERR(ovs_net->ct_limit_info->data);
1635 kfree(ovs_net->ct_limit_info->limits);
1636 kfree(ovs_net->ct_limit_info);
1637 pr_err("openvswitch: failed to init nf_conncount %d\n", err);
1643 static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
1645 const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
1648 nf_conncount_destroy(net, NFPROTO_INET, info->data);
1649 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
1650 struct hlist_head *head = &info->limits[i];
1651 struct ovs_ct_limit *ct_limit;
1653 hlist_for_each_entry_rcu(ct_limit, head, hlist_node,
1654 lockdep_ovsl_is_held())
1655 kfree_rcu(ct_limit, rcu);
1657 kfree(info->limits);
1661 static struct sk_buff *
1662 ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
1663 struct ovs_header **ovs_reply_header)
1665 struct ovs_header *ovs_header = info->userhdr;
1666 struct sk_buff *skb;
1668 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1670 return ERR_PTR(-ENOMEM);
1672 *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
1674 &dp_ct_limit_genl_family, 0, cmd);
1676 if (!*ovs_reply_header) {
1678 return ERR_PTR(-EMSGSIZE);
1680 (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
1685 static bool check_zone_id(int zone_id, u16 *pzone)
1687 if (zone_id >= 0 && zone_id <= 65535) {
1688 *pzone = (u16)zone_id;
1694 static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
1695 struct ovs_ct_limit_info *info)
1697 struct ovs_zone_limit *zone_limit;
1701 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1702 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1704 while (rem >= sizeof(*zone_limit)) {
1705 if (unlikely(zone_limit->zone_id ==
1706 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1708 info->default_limit = zone_limit->limit;
1710 } else if (unlikely(!check_zone_id(
1711 zone_limit->zone_id, &zone))) {
1712 OVS_NLERR(true, "zone id is out of range");
1714 struct ovs_ct_limit *ct_limit;
1716 ct_limit = kmalloc(sizeof(*ct_limit),
1717 GFP_KERNEL_ACCOUNT);
1721 ct_limit->zone = zone;
1722 ct_limit->limit = zone_limit->limit;
1725 ct_limit_set(info, ct_limit);
1728 rem -= NLA_ALIGN(sizeof(*zone_limit));
1729 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
1730 NLA_ALIGN(sizeof(*zone_limit)));
1734 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
1739 static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
1740 struct ovs_ct_limit_info *info)
1742 struct ovs_zone_limit *zone_limit;
1746 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1747 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1749 while (rem >= sizeof(*zone_limit)) {
1750 if (unlikely(zone_limit->zone_id ==
1751 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1753 info->default_limit = OVS_CT_LIMIT_DEFAULT;
1755 } else if (unlikely(!check_zone_id(
1756 zone_limit->zone_id, &zone))) {
1757 OVS_NLERR(true, "zone id is out of range");
1760 ct_limit_del(info, zone);
1763 rem -= NLA_ALIGN(sizeof(*zone_limit));
1764 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
1765 NLA_ALIGN(sizeof(*zone_limit)));
1769 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
1774 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
1775 struct sk_buff *reply)
1777 struct ovs_zone_limit zone_limit = {
1778 .zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE,
1779 .limit = info->default_limit,
1782 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
1785 static int __ovs_ct_limit_get_zone_limit(struct net *net,
1786 struct nf_conncount_data *data,
1787 u16 zone_id, u32 limit,
1788 struct sk_buff *reply)
1790 struct nf_conntrack_zone ct_zone;
1791 struct ovs_zone_limit zone_limit;
1792 u32 conncount_key = zone_id;
1794 zone_limit.zone_id = zone_id;
1795 zone_limit.limit = limit;
1796 nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
1798 zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
1800 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
1803 static int ovs_ct_limit_get_zone_limit(struct net *net,
1804 struct nlattr *nla_zone_limit,
1805 struct ovs_ct_limit_info *info,
1806 struct sk_buff *reply)
1808 struct ovs_zone_limit *zone_limit;
1813 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1814 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1816 while (rem >= sizeof(*zone_limit)) {
1817 if (unlikely(zone_limit->zone_id ==
1818 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1819 err = ovs_ct_limit_get_default_limit(info, reply);
1822 } else if (unlikely(!check_zone_id(zone_limit->zone_id,
1824 OVS_NLERR(true, "zone id is out of range");
1827 limit = ct_limit_get(info, zone);
1830 err = __ovs_ct_limit_get_zone_limit(
1831 net, info->data, zone, limit, reply);
1835 rem -= NLA_ALIGN(sizeof(*zone_limit));
1836 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
1837 NLA_ALIGN(sizeof(*zone_limit)));
1841 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
1846 static int ovs_ct_limit_get_all_zone_limit(struct net *net,
1847 struct ovs_ct_limit_info *info,
1848 struct sk_buff *reply)
1850 struct ovs_ct_limit *ct_limit;
1851 struct hlist_head *head;
1854 err = ovs_ct_limit_get_default_limit(info, reply);
1859 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
1860 head = &info->limits[i];
1861 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1862 err = __ovs_ct_limit_get_zone_limit(net, info->data,
1863 ct_limit->zone, ct_limit->limit, reply);
1874 static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
1876 struct nlattr **a = info->attrs;
1877 struct sk_buff *reply;
1878 struct ovs_header *ovs_reply_header;
1879 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1880 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
1883 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
1886 return PTR_ERR(reply);
1888 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
1893 err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
1898 static_branch_enable(&ovs_ct_limit_enabled);
1900 genlmsg_end(reply, ovs_reply_header);
1901 return genlmsg_reply(reply, info);
1908 static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
1910 struct nlattr **a = info->attrs;
1911 struct sk_buff *reply;
1912 struct ovs_header *ovs_reply_header;
1913 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1914 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
1917 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
1920 return PTR_ERR(reply);
1922 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
1927 err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
1932 genlmsg_end(reply, ovs_reply_header);
1933 return genlmsg_reply(reply, info);
1940 static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
1942 struct nlattr **a = info->attrs;
1943 struct nlattr *nla_reply;
1944 struct sk_buff *reply;
1945 struct ovs_header *ovs_reply_header;
1946 struct net *net = sock_net(skb->sk);
1947 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1948 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
1951 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
1954 return PTR_ERR(reply);
1956 nla_reply = nla_nest_start_noflag(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
1962 if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
1963 err = ovs_ct_limit_get_zone_limit(
1964 net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
1969 err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
1975 nla_nest_end(reply, nla_reply);
1976 genlmsg_end(reply, ovs_reply_header);
1977 return genlmsg_reply(reply, info);
1984 static const struct genl_small_ops ct_limit_genl_ops[] = {
1985 { .cmd = OVS_CT_LIMIT_CMD_SET,
1986 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1987 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN
1990 .doit = ovs_ct_limit_cmd_set,
1992 { .cmd = OVS_CT_LIMIT_CMD_DEL,
1993 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1994 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN
1997 .doit = ovs_ct_limit_cmd_del,
1999 { .cmd = OVS_CT_LIMIT_CMD_GET,
2000 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2001 .flags = 0, /* OK for unprivileged users. */
2002 .doit = ovs_ct_limit_cmd_get,
2006 static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
2007 .name = OVS_CT_LIMIT_MCGROUP,
2010 struct genl_family dp_ct_limit_genl_family __ro_after_init = {
2011 .hdrsize = sizeof(struct ovs_header),
2012 .name = OVS_CT_LIMIT_FAMILY,
2013 .version = OVS_CT_LIMIT_VERSION,
2014 .maxattr = OVS_CT_LIMIT_ATTR_MAX,
2015 .policy = ct_limit_policy,
2017 .parallel_ops = true,
2018 .small_ops = ct_limit_genl_ops,
2019 .n_small_ops = ARRAY_SIZE(ct_limit_genl_ops),
2020 .resv_start_op = OVS_CT_LIMIT_CMD_GET + 1,
2021 .mcgrps = &ovs_ct_limit_multicast_group,
2023 .module = THIS_MODULE,
2027 int ovs_ct_init(struct net *net)
2029 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
2030 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2032 if (nf_connlabels_get(net, n_bits - 1)) {
2033 ovs_net->xt_label = false;
2034 OVS_NLERR(true, "Failed to set connlabel length");
2036 ovs_net->xt_label = true;
2039 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2040 return ovs_ct_limit_init(net, ovs_net);
2046 void ovs_ct_exit(struct net *net)
2048 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2050 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2051 ovs_ct_limit_exit(net, ovs_net);
2054 if (ovs_net->xt_label)
2055 nf_connlabels_put(net);