2 * Copyright (c) 2015 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
14 #include <linux/kconfig.h>
15 #include <linux/version.h>
17 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
19 #include <linux/module.h>
20 #include <linux/openvswitch.h>
21 #include <linux/tcp.h>
22 #include <linux/udp.h>
23 #include <linux/sctp.h>
24 #include <linux/static_key.h>
26 #include <net/genetlink.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_count.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_labels.h>
31 #include <net/netfilter/nf_conntrack_seqadj.h>
32 #include <net/netfilter/nf_conntrack_timeout.h>
33 #include <net/netfilter/nf_conntrack_zones.h>
34 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
35 #include <net/ipv6_frag.h>
37 #ifdef CONFIG_NF_NAT_NEEDED
38 /* Starting from upstream commit 3bf195ae6037 ("netfilter: nat: merge
39 * nf_nat_ipv4,6 into nat core") in kernel 5.1. nf_nat_ipv4,6 are merged
40 * into nf_nat. In order to keep backward compatibility, we keep the config
41 * checking as is for the old kernel, and replace them with marco for the
43 #ifdef HAVE_UPSTREAM_NF_NAT
44 #include <net/netfilter/nf_nat.h>
45 #define CONFIG_NF_NAT_IPV4 CONFIG_NF_NAT
46 #define CONFIG_NF_NAT_IPV6 CONFIG_IPV6
48 #include <linux/netfilter/nf_nat.h>
49 #include <net/netfilter/nf_nat_core.h>
50 #include <net/netfilter/nf_nat_l3proto.h>
51 #endif /* HAVE_UPSTREAM_NF_NAT */
52 #endif /* CONFIG_NF_NAT_NEEDED */
55 #include "conntrack.h"
57 #include "flow_netlink.h"
60 #ifndef HAVE_NF_NAT_RANGE2
61 #define nf_nat_range2 nf_nat_range
64 struct ovs_ct_len_tbl
{
69 /* Metadata mark for masked write to conntrack mark */
75 /* Metadata label for masked write to conntrack label. */
77 struct ovs_key_ct_labels value
;
78 struct ovs_key_ct_labels mask
;
82 OVS_CT_NAT
= 1 << 0, /* NAT for committed connections only. */
83 OVS_CT_SRC_NAT
= 1 << 1, /* Source NAT for NEW connections. */
84 OVS_CT_DST_NAT
= 1 << 2, /* Destination NAT for NEW connections. */
87 /* Conntrack action context for execution. */
88 struct ovs_conntrack_info
{
89 struct nf_conntrack_helper
*helper
;
90 struct nf_conntrack_zone zone
;
93 u8 nat
: 3; /* enum ovs_ct_nat */
94 u8 random_fully_compat
: 1; /* bool */
96 u8 have_eventmask
: 1;
98 u32 eventmask
; /* Mask of 1 << IPCT_*. */
100 struct md_labels labels
;
101 char timeout
[CTNL_TIMEOUT_NAME_MAX
];
102 struct nf_ct_timeout
*nf_ct_timeout
;
103 #ifdef CONFIG_NF_NAT_NEEDED
104 struct nf_nat_range2 range
; /* Only present for SRC NAT and DST NAT. */
108 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
109 #define OVS_CT_LIMIT_UNLIMITED 0
110 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
111 #define CT_LIMIT_HASH_BUCKETS 512
112 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled
);
114 struct ovs_ct_limit
{
115 /* Elements in ovs_ct_limit_info->limits hash table */
116 struct hlist_node hlist_node
;
122 struct ovs_ct_limit_info
{
124 struct hlist_head
*limits
;
125 struct nf_conncount_data
*data
;
128 static const struct nla_policy ct_limit_policy
[OVS_CT_LIMIT_ATTR_MAX
+ 1] = {
129 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NLA_NESTED
, },
133 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
);
135 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
);
137 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
139 switch (ntohs(key
->eth
.type
)) {
145 return NFPROTO_UNSPEC
;
149 /* Map SKB connection state into the values used by flow definition. */
150 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
152 u8 ct_state
= OVS_CS_F_TRACKED
;
155 case IP_CT_ESTABLISHED_REPLY
:
156 case IP_CT_RELATED_REPLY
:
157 ct_state
|= OVS_CS_F_REPLY_DIR
;
164 case IP_CT_ESTABLISHED
:
165 case IP_CT_ESTABLISHED_REPLY
:
166 ct_state
|= OVS_CS_F_ESTABLISHED
;
169 case IP_CT_RELATED_REPLY
:
170 ct_state
|= OVS_CS_F_RELATED
;
173 ct_state
|= OVS_CS_F_NEW
;
182 static u32
ovs_ct_get_mark(const struct nf_conn
*ct
)
184 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
185 return ct
? ct
->mark
: 0;
191 /* Guard against conntrack labels max size shrinking below 128 bits. */
192 #if NF_CT_LABELS_MAX_SIZE < 16
193 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
196 static void ovs_ct_get_labels(const struct nf_conn
*ct
,
197 struct ovs_key_ct_labels
*labels
)
199 struct nf_conn_labels
*cl
= ct
? nf_ct_labels_find(ct
) : NULL
;
202 memcpy(labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
204 memset(labels
, 0, OVS_CT_LABELS_LEN
);
207 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key
*key
,
208 const struct nf_conntrack_tuple
*orig
,
211 key
->ct_orig_proto
= orig
->dst
.protonum
;
212 if (orig
->dst
.protonum
== icmp_proto
) {
213 key
->ct
.orig_tp
.src
= htons(orig
->dst
.u
.icmp
.type
);
214 key
->ct
.orig_tp
.dst
= htons(orig
->dst
.u
.icmp
.code
);
216 key
->ct
.orig_tp
.src
= orig
->src
.u
.all
;
217 key
->ct
.orig_tp
.dst
= orig
->dst
.u
.all
;
221 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
222 const struct nf_conntrack_zone
*zone
,
223 const struct nf_conn
*ct
)
225 key
->ct_state
= state
;
226 key
->ct_zone
= zone
->id
;
227 key
->ct
.mark
= ovs_ct_get_mark(ct
);
228 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
231 const struct nf_conntrack_tuple
*orig
;
233 /* Use the master if we have one. */
236 orig
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
238 /* IP version must match with the master connection. */
239 if (key
->eth
.type
== htons(ETH_P_IP
) &&
240 nf_ct_l3num(ct
) == NFPROTO_IPV4
) {
241 key
->ipv4
.ct_orig
.src
= orig
->src
.u3
.ip
;
242 key
->ipv4
.ct_orig
.dst
= orig
->dst
.u3
.ip
;
243 __ovs_ct_update_key_orig_tp(key
, orig
, IPPROTO_ICMP
);
245 } else if (key
->eth
.type
== htons(ETH_P_IPV6
) &&
246 !sw_flow_key_is_nd(key
) &&
247 nf_ct_l3num(ct
) == NFPROTO_IPV6
) {
248 key
->ipv6
.ct_orig
.src
= orig
->src
.u3
.in6
;
249 key
->ipv6
.ct_orig
.dst
= orig
->dst
.u3
.in6
;
250 __ovs_ct_update_key_orig_tp(key
, orig
, NEXTHDR_ICMP
);
254 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
255 * original direction key fields.
257 key
->ct_orig_proto
= 0;
260 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
261 * previously sent the packet to conntrack via the ct action. If
262 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
263 * initialized from the connection status.
265 static void ovs_ct_update_key(const struct sk_buff
*skb
,
266 const struct ovs_conntrack_info
*info
,
267 struct sw_flow_key
*key
, bool post_ct
,
270 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
271 enum ip_conntrack_info ctinfo
;
275 ct
= nf_ct_get(skb
, &ctinfo
);
277 state
= ovs_ct_get_state(ctinfo
);
278 /* All unconfirmed entries are NEW connections. */
279 if (!nf_ct_is_confirmed(ct
))
280 state
|= OVS_CS_F_NEW
;
281 /* OVS persists the related flag for the duration of the
285 state
|= OVS_CS_F_RELATED
;
286 if (keep_nat_flags
) {
287 state
|= key
->ct_state
& OVS_CS_F_NAT_MASK
;
289 if (ct
->status
& IPS_SRC_NAT
)
290 state
|= OVS_CS_F_SRC_NAT
;
291 if (ct
->status
& IPS_DST_NAT
)
292 state
|= OVS_CS_F_DST_NAT
;
294 zone
= nf_ct_zone(ct
);
295 } else if (post_ct
) {
296 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
300 __ovs_ct_update_key(key
, state
, zone
, ct
);
303 /* This is called to initialize CT key fields possibly coming in from the local
306 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
308 ovs_ct_update_key(skb
, NULL
, key
, false, false);
311 #define IN6_ADDR_INITIALIZER(ADDR) \
312 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
313 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
315 int ovs_ct_put_key(const struct sw_flow_key
*swkey
,
316 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
318 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, output
->ct_state
))
321 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
322 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, output
->ct_zone
))
325 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
326 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, output
->ct
.mark
))
329 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
330 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(output
->ct
.labels
),
334 if (swkey
->ct_orig_proto
) {
335 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
336 struct ovs_key_ct_tuple_ipv4 orig
= {
337 output
->ipv4
.ct_orig
.src
,
338 output
->ipv4
.ct_orig
.dst
,
339 output
->ct
.orig_tp
.src
,
340 output
->ct
.orig_tp
.dst
,
341 output
->ct_orig_proto
,
343 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
344 sizeof(orig
), &orig
))
346 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
347 struct ovs_key_ct_tuple_ipv6 orig
= {
348 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.src
),
349 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.dst
),
350 output
->ct
.orig_tp
.src
,
351 output
->ct
.orig_tp
.dst
,
352 output
->ct_orig_proto
,
354 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
355 sizeof(orig
), &orig
))
363 static int ovs_ct_set_mark(struct nf_conn
*ct
, struct sw_flow_key
*key
,
364 u32 ct_mark
, u32 mask
)
366 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
369 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
370 if (ct
->mark
!= new_mark
) {
372 if (nf_ct_is_confirmed(ct
))
373 nf_conntrack_event_cache(IPCT_MARK
, ct
);
374 key
->ct
.mark
= new_mark
;
383 static struct nf_conn_labels
*ovs_ct_get_conn_labels(struct nf_conn
*ct
)
385 struct nf_conn_labels
*cl
;
387 cl
= nf_ct_labels_find(ct
);
389 nf_ct_labels_ext_add(ct
);
390 cl
= nf_ct_labels_find(ct
);
396 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
397 * since the new connection is not yet confirmed, and thus no-one else has
398 * access to it's labels, we simply write them over.
400 static int ovs_ct_init_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
401 const struct ovs_key_ct_labels
*labels
,
402 const struct ovs_key_ct_labels
*mask
)
404 struct nf_conn_labels
*cl
, *master_cl
;
405 bool have_mask
= labels_nonzero(mask
);
407 /* Inherit master's labels to the related connection? */
408 master_cl
= ct
->master
? nf_ct_labels_find(ct
->master
) : NULL
;
410 if (!master_cl
&& !have_mask
)
411 return 0; /* Nothing to do. */
413 cl
= ovs_ct_get_conn_labels(ct
);
417 /* Inherit the master's labels, if any. Must use memcpy for backport
418 * as struct assignment only copies the length field in older
422 memcpy(cl
->bits
, master_cl
->bits
, OVS_CT_LABELS_LEN
);
425 u32
*dst
= (u32
*)cl
->bits
;
428 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
429 dst
[i
] = (dst
[i
] & ~mask
->ct_labels_32
[i
]) |
430 (labels
->ct_labels_32
[i
]
431 & mask
->ct_labels_32
[i
]);
434 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
435 * IPCT_LABEL bit is set in the event cache.
437 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
439 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
444 static int ovs_ct_set_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
445 const struct ovs_key_ct_labels
*labels
,
446 const struct ovs_key_ct_labels
*mask
)
448 struct nf_conn_labels
*cl
;
451 cl
= ovs_ct_get_conn_labels(ct
);
455 err
= nf_connlabels_replace(ct
, labels
->ct_labels_32
,
457 OVS_CT_LABELS_LEN_32
);
461 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
466 /* 'skb' should already be pulled to nh_ofs. */
467 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
469 const struct nf_conntrack_helper
*helper
;
470 const struct nf_conn_help
*help
;
471 enum ip_conntrack_info ctinfo
;
472 unsigned int protoff
;
477 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
478 bool dst_set
= false;
479 struct rtable rt
= { .rt_flags
= 0 };
482 ct
= nf_ct_get(skb
, &ctinfo
);
483 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
486 help
= nfct_help(ct
);
490 helper
= rcu_dereference(help
->helper
);
496 protoff
= ip_hdrlen(skb
);
502 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
503 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
505 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
506 pr_debug("proto header not found\n");
513 WARN_ONCE(1, "helper invoked on non-IP family!");
517 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
518 /* Linux 4.5 and older depend on skb_dst being set when recalculating
519 * checksums after NAT helper has mangled TCP or UDP packet payload.
520 * skb_dst is cast to a rtable struct and the flags examined.
521 * Forcing these flags to have RTCF_LOCAL not set ensures checksum mod
522 * is carried out in the same way as kernel versions > 4.5
524 if (ct
->status
& IPS_NAT_MASK
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
527 skb_dst_set(skb
, &rt
.dst
);
530 err
= helper
->help(skb
, protoff
, ct
, ctinfo
);
531 if (err
!= NF_ACCEPT
)
534 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
536 skb_dst_set(skb
, NULL
);
539 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
540 * FTP with NAT) adusting the TCP payload size when mangling IP
541 * addresses and/or port numbers in the text-based control connection.
543 if (test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
) &&
544 !nf_ct_seq_adjust(skb
, ct
, ctinfo
, protoff
))
549 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
550 * value if 'skb' is freed.
552 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
553 u16 zone
, struct sk_buff
*skb
)
555 struct ovs_gso_cb ovs_cb
= *OVS_GSO_CB(skb
);
558 if (key
->eth
.type
== htons(ETH_P_IP
)) {
559 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
561 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
562 err
= ip_defrag(net
, skb
, user
);
566 ovs_cb
.dp_cb
.mru
= IPCB(skb
)->frag_max_size
;
567 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
568 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
569 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
571 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
572 err
= nf_ct_frag6_gather(net
, skb
, user
);
574 if (err
!= -EINPROGRESS
)
579 key
->ip
.proto
= ipv6_hdr(skb
)->nexthdr
;
580 ovs_cb
.dp_cb
.mru
= IP6CB(skb
)->frag_max_size
;
581 #endif /* IP frag support */
584 return -EPFNOSUPPORT
;
587 /* The key extracted from the fragment that completed this datagram
588 * likely didn't have an L4 header, so regenerate it.
590 ovs_flow_key_update_l3l4(skb
, key
);
592 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
595 *OVS_GSO_CB(skb
) = ovs_cb
;
600 static struct nf_conntrack_expect
*
601 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
602 u16 proto
, const struct sk_buff
*skb
)
604 struct nf_conntrack_tuple tuple
;
605 struct nf_conntrack_expect
*exp
;
607 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
610 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
612 struct nf_conntrack_tuple_hash
*h
;
614 /* Delete existing conntrack entry, if it clashes with the
615 * expectation. This can happen since conntrack ALGs do not
616 * check for clashes between (new) expectations and existing
617 * conntrack entries. nf_conntrack_in() will check the
618 * expectations only if a conntrack entry can not be found,
619 * which can lead to OVS finding the expectation (here) in the
620 * init direction, but which will not be removed by the
621 * nf_conntrack_in() call, if a matching conntrack entry is
622 * found instead. In this case all init direction packets
623 * would be reported as new related packets, while reply
624 * direction packets would be reported as un-related
625 * established packets.
627 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
629 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
631 nf_ct_delete(ct
, 0, 0);
632 nf_conntrack_put(&ct
->ct_general
);
639 /* This replicates logic from nf_conntrack_core.c that is not exported. */
640 static enum ip_conntrack_info
641 ovs_ct_get_info(const struct nf_conntrack_tuple_hash
*h
)
643 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
645 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
646 return IP_CT_ESTABLISHED_REPLY
;
647 /* Once we've had two way comms, always ESTABLISHED. */
648 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
649 return IP_CT_ESTABLISHED
;
650 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
651 return IP_CT_RELATED
;
655 /* Find an existing connection which this packet belongs to without
656 * re-attributing statistics or modifying the connection state. This allows an
657 * skb->_nfct lost due to an upcall to be recovered during actions execution.
659 * Must be called with rcu_read_lock.
661 * On success, populates skb->_nfct and returns the connection. Returns NULL
662 * if there is no existing entry.
664 static struct nf_conn
*
665 ovs_ct_find_existing(struct net
*net
, const struct nf_conntrack_zone
*zone
,
666 u8 l3num
, struct sk_buff
*skb
, bool natted
)
668 struct nf_conntrack_tuple tuple
;
669 struct nf_conntrack_tuple_hash
*h
;
672 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), l3num
,
674 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
678 /* Must invert the tuple if skb has been transformed by NAT. */
680 struct nf_conntrack_tuple inverse
;
682 if (!rpl_nf_ct_invert_tuple(&inverse
, &tuple
)) {
683 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
689 /* look for tuple match */
690 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
692 return NULL
; /* Not found. */
694 ct
= nf_ct_tuplehash_to_ctrack(h
);
696 /* Inverted packet tuple matches the reverse direction conntrack tuple,
697 * select the other tuplehash to get the right 'ctinfo' bits for this
701 h
= &ct
->tuplehash
[!h
->tuple
.dst
.dir
];
703 nf_ct_set(skb
, ct
, ovs_ct_get_info(h
));
708 struct nf_conn
*ovs_ct_executed(struct net
*net
,
709 const struct sw_flow_key
*key
,
710 const struct ovs_conntrack_info
*info
,
714 struct nf_conn
*ct
= NULL
;
716 /* If no ct, check if we have evidence that an existing conntrack entry
717 * might be found for this skb. This happens when we lose a skb->_nfct
718 * due to an upcall, or if the direction is being forced. If the
719 * connection was not confirmed, it is not cached and needs to be run
720 * through conntrack again.
722 *ct_executed
= (key
->ct_state
& OVS_CS_F_TRACKED
) &&
723 !(key
->ct_state
& OVS_CS_F_INVALID
) &&
724 (key
->ct_zone
== info
->zone
.id
);
726 if (*ct_executed
|| (!key
->ct_state
&& info
->force
)) {
727 ct
= ovs_ct_find_existing(net
, &info
->zone
, info
->family
, skb
,
735 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
736 static bool skb_nfct_cached(struct net
*net
,
737 const struct sw_flow_key
*key
,
738 const struct ovs_conntrack_info
*info
,
741 enum ip_conntrack_info ctinfo
;
743 bool ct_executed
= true;
745 ct
= nf_ct_get(skb
, &ctinfo
);
747 ct
= ovs_ct_executed(net
, key
, info
, skb
, &ct_executed
);
750 nf_ct_get(skb
, &ctinfo
);
754 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
756 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
759 struct nf_conn_help
*help
;
761 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
762 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
765 if (info
->nf_ct_timeout
) {
766 struct nf_conn_timeout
*timeout_ext
;
768 timeout_ext
= nf_ct_timeout_find(ct
);
769 if (!timeout_ext
|| info
->nf_ct_timeout
!=
770 rcu_dereference(timeout_ext
->timeout
))
773 /* Force conntrack entry direction to the current packet? */
774 if (info
->force
&& CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
) {
775 /* Delete the conntrack entry if confirmed, else just release
778 if (nf_ct_is_confirmed(ct
))
779 nf_ct_delete(ct
, 0, 0);
781 nf_conntrack_put(&ct
->ct_general
);
782 nf_ct_set(skb
, NULL
, 0);
789 #ifdef CONFIG_NF_NAT_NEEDED
790 /* Modelled after nf_nat_ipv[46]_fn().
791 * range is only used for new, uninitialized NAT state.
792 * Returns either NF_ACCEPT or NF_DROP.
794 static int ovs_ct_nat_execute(struct sk_buff
*skb
, struct nf_conn
*ct
,
795 enum ip_conntrack_info ctinfo
,
796 const struct nf_nat_range2
*range
,
797 enum nf_nat_manip_type maniptype
)
799 int hooknum
, nh_off
, err
= NF_ACCEPT
;
801 nh_off
= skb_network_offset(skb
);
802 skb_pull_rcsum(skb
, nh_off
);
804 /* See HOOK2MANIP(). */
805 if (maniptype
== NF_NAT_MANIP_SRC
)
806 hooknum
= NF_INET_LOCAL_IN
; /* Source NAT */
808 hooknum
= NF_INET_LOCAL_OUT
; /* Destination NAT */
812 case IP_CT_RELATED_REPLY
:
813 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
814 skb
->protocol
== htons(ETH_P_IP
) &&
815 ip_hdr(skb
)->protocol
== IPPROTO_ICMP
) {
816 if (!nf_nat_icmp_reply_translation(skb
, ct
, ctinfo
,
820 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
821 skb
->protocol
== htons(ETH_P_IPV6
)) {
823 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
824 int hdrlen
= ipv6_skip_exthdr(skb
,
825 sizeof(struct ipv6hdr
),
826 &nexthdr
, &frag_off
);
828 if (hdrlen
>= 0 && nexthdr
== IPPROTO_ICMPV6
) {
829 if (!nf_nat_icmpv6_reply_translation(skb
, ct
,
837 /* Non-ICMP, fall thru to initialize if needed. */
839 /* Seen it before? This can happen for loopback, retrans,
842 if (!nf_nat_initialized(ct
, maniptype
)) {
843 /* Initialize according to the NAT action. */
844 err
= (range
&& range
->flags
& NF_NAT_RANGE_MAP_IPS
)
845 /* Action is set up to establish a new
848 ? nf_nat_setup_info(ct
, range
, maniptype
)
849 : nf_nat_alloc_null_binding(ct
, hooknum
);
850 if (err
!= NF_ACCEPT
)
855 case IP_CT_ESTABLISHED
:
856 case IP_CT_ESTABLISHED_REPLY
:
864 err
= nf_nat_packet(ct
, ctinfo
, hooknum
, skb
);
866 skb_push(skb
, nh_off
);
867 skb_postpush_rcsum(skb
, skb
->data
, nh_off
);
872 static void ovs_nat_update_key(struct sw_flow_key
*key
,
873 const struct sk_buff
*skb
,
874 enum nf_nat_manip_type maniptype
)
876 if (maniptype
== NF_NAT_MANIP_SRC
) {
879 key
->ct_state
|= OVS_CS_F_SRC_NAT
;
880 if (key
->eth
.type
== htons(ETH_P_IP
))
881 key
->ipv4
.addr
.src
= ip_hdr(skb
)->saddr
;
882 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
883 memcpy(&key
->ipv6
.addr
.src
, &ipv6_hdr(skb
)->saddr
,
884 sizeof(key
->ipv6
.addr
.src
));
888 if (key
->ip
.proto
== IPPROTO_UDP
)
889 src
= udp_hdr(skb
)->source
;
890 else if (key
->ip
.proto
== IPPROTO_TCP
)
891 src
= tcp_hdr(skb
)->source
;
892 else if (key
->ip
.proto
== IPPROTO_SCTP
)
893 src
= sctp_hdr(skb
)->source
;
901 key
->ct_state
|= OVS_CS_F_DST_NAT
;
902 if (key
->eth
.type
== htons(ETH_P_IP
))
903 key
->ipv4
.addr
.dst
= ip_hdr(skb
)->daddr
;
904 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
905 memcpy(&key
->ipv6
.addr
.dst
, &ipv6_hdr(skb
)->daddr
,
906 sizeof(key
->ipv6
.addr
.dst
));
910 if (key
->ip
.proto
== IPPROTO_UDP
)
911 dst
= udp_hdr(skb
)->dest
;
912 else if (key
->ip
.proto
== IPPROTO_TCP
)
913 dst
= tcp_hdr(skb
)->dest
;
914 else if (key
->ip
.proto
== IPPROTO_SCTP
)
915 dst
= sctp_hdr(skb
)->dest
;
923 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
924 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
925 const struct ovs_conntrack_info
*info
,
926 struct sk_buff
*skb
, struct nf_conn
*ct
,
927 enum ip_conntrack_info ctinfo
)
929 enum nf_nat_manip_type maniptype
;
932 #ifdef HAVE_NF_CT_IS_UNTRACKED
933 if (nf_ct_is_untracked(ct
)) {
934 /* A NAT action may only be performed on tracked packets. */
937 #endif /* HAVE_NF_CT_IS_UNTRACKED */
939 /* Add NAT extension if not confirmed yet. */
940 if (!nf_ct_is_confirmed(ct
) && !nf_ct_nat_ext_add(ct
))
941 return NF_ACCEPT
; /* Can't NAT. */
943 /* Determine NAT type.
944 * Check if the NAT type can be deduced from the tracked connection.
945 * Make sure new expected connections (IP_CT_RELATED) are NATted only
948 if (info
->nat
& OVS_CT_NAT
&& ctinfo
!= IP_CT_NEW
&&
949 ct
->status
& IPS_NAT_MASK
&&
950 (ctinfo
!= IP_CT_RELATED
|| info
->commit
)) {
951 /* NAT an established or related connection like before. */
952 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
)
953 /* This is the REPLY direction for a connection
954 * for which NAT was applied in the forward
955 * direction. Do the reverse NAT.
957 maniptype
= ct
->status
& IPS_SRC_NAT
958 ? NF_NAT_MANIP_DST
: NF_NAT_MANIP_SRC
;
960 maniptype
= ct
->status
& IPS_SRC_NAT
961 ? NF_NAT_MANIP_SRC
: NF_NAT_MANIP_DST
;
962 } else if (info
->nat
& OVS_CT_SRC_NAT
) {
963 maniptype
= NF_NAT_MANIP_SRC
;
964 } else if (info
->nat
& OVS_CT_DST_NAT
) {
965 maniptype
= NF_NAT_MANIP_DST
;
967 return NF_ACCEPT
; /* Connection is not NATed. */
969 err
= ovs_ct_nat_execute(skb
, ct
, ctinfo
, &info
->range
, maniptype
);
971 /* Mark NAT done if successful and update the flow key. */
972 if (err
== NF_ACCEPT
)
973 ovs_nat_update_key(key
, skb
, maniptype
);
977 #else /* !CONFIG_NF_NAT_NEEDED */
978 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
979 const struct ovs_conntrack_info
*info
,
980 struct sk_buff
*skb
, struct nf_conn
*ct
,
981 enum ip_conntrack_info ctinfo
)
987 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
988 * not done already. Update key with new CT state after passing the packet
990 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
991 * set to NULL and 0 will be returned.
993 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
994 const struct ovs_conntrack_info
*info
,
997 /* If we are recirculating packets to match on conntrack fields and
998 * committing with a separate conntrack action, then we don't need to
999 * actually run the packet through conntrack twice unless it's for a
1002 bool cached
= skb_nfct_cached(net
, key
, info
, skb
);
1003 enum ip_conntrack_info ctinfo
;
1007 struct nf_hook_state state
= {
1008 .hook
= NF_INET_PRE_ROUTING
,
1012 struct nf_conn
*tmpl
= info
->ct
;
1015 /* Associate skb with specified zone. */
1018 nf_conntrack_put(skb_nfct(skb
));
1019 nf_conntrack_get(&tmpl
->ct_general
);
1020 nf_ct_set(skb
, tmpl
, IP_CT_NEW
);
1023 err
= nf_conntrack_in(skb
, &state
);
1024 if (err
!= NF_ACCEPT
)
1027 /* Clear CT state NAT flags to mark that we have not yet done
1028 * NAT after the nf_conntrack_in() call. We can actually clear
1029 * the whole state, as it will be re-initialized below.
1033 /* Update the key, but keep the NAT flags. */
1034 ovs_ct_update_key(skb
, info
, key
, true, true);
1037 ct
= nf_ct_get(skb
, &ctinfo
);
1039 /* Packets starting a new connection must be NATted before the
1040 * helper, so that the helper knows about the NAT. We enforce
1041 * this by delaying both NAT and helper calls for unconfirmed
1042 * connections until the committing CT action. For later
1043 * packets NAT and Helper may be called in either order.
1045 * NAT will be done only if the CT action has NAT, and only
1046 * once per packet (per zone), as guarded by the NAT bits in
1047 * the key->ct_state.
1049 if (info
->nat
&& !(key
->ct_state
& OVS_CS_F_NAT_MASK
) &&
1050 (nf_ct_is_confirmed(ct
) || info
->commit
) &&
1051 ovs_ct_nat(net
, key
, info
, skb
, ct
, ctinfo
) != NF_ACCEPT
) {
1055 /* Userspace may decide to perform a ct lookup without a helper
1056 * specified followed by a (recirculate and) commit with one.
1057 * Therefore, for unconfirmed connections which we will commit,
1058 * we need to attach the helper here.
1060 if (!nf_ct_is_confirmed(ct
) && info
->commit
&&
1061 info
->helper
&& !nfct_help(ct
)) {
1062 int err
= __nf_ct_try_assign_helper(ct
, info
->ct
,
1068 /* Call the helper only if:
1069 * - nf_conntrack_in() was executed above ("!cached") for a
1070 * confirmed connection, or
1071 * - When committing an unconfirmed connection.
1073 if ((nf_ct_is_confirmed(ct
) ? !cached
: info
->commit
) &&
1074 ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
1082 /* Lookup connection and read fields into key. */
1083 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
1084 const struct ovs_conntrack_info
*info
,
1085 struct sk_buff
*skb
)
1087 struct nf_conntrack_expect
*exp
;
1089 /* If we pass an expected packet through nf_conntrack_in() the
1090 * expectation is typically removed, but the packet could still be
1091 * lost in upcall processing. To prevent this from happening we
1092 * perform an explicit expectation lookup. Expected connections are
1093 * always new, and will be passed through conntrack only when they are
1094 * committed, as it is OK to remove the expectation at that time.
1096 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
1100 /* NOTE: New connections are NATted and Helped only when
1101 * committed, so we are not calling into NAT here.
1103 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
1104 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
1109 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1113 ct
= (struct nf_conn
*)skb_nfct(skb
);
1115 nf_ct_deliver_cached_events(ct
);
1121 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
1125 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
1126 if (labels
->ct_labels_32
[i
])
1132 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1133 static struct hlist_head
*ct_limit_hash_bucket(
1134 const struct ovs_ct_limit_info
*info
, u16 zone
)
1136 return &info
->limits
[zone
& (CT_LIMIT_HASH_BUCKETS
- 1)];
1139 /* Call with ovs_mutex */
1140 static void ct_limit_set(const struct ovs_ct_limit_info
*info
,
1141 struct ovs_ct_limit
*new_ct_limit
)
1143 struct ovs_ct_limit
*ct_limit
;
1144 struct hlist_head
*head
;
1146 head
= ct_limit_hash_bucket(info
, new_ct_limit
->zone
);
1147 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1148 if (ct_limit
->zone
== new_ct_limit
->zone
) {
1149 hlist_replace_rcu(&ct_limit
->hlist_node
,
1150 &new_ct_limit
->hlist_node
);
1151 kfree_rcu(ct_limit
, rcu
);
1156 hlist_add_head_rcu(&new_ct_limit
->hlist_node
, head
);
1159 /* Call with ovs_mutex */
1160 static void ct_limit_del(const struct ovs_ct_limit_info
*info
, u16 zone
)
1162 struct ovs_ct_limit
*ct_limit
;
1163 struct hlist_head
*head
;
1164 struct hlist_node
*n
;
1166 head
= ct_limit_hash_bucket(info
, zone
);
1167 hlist_for_each_entry_safe(ct_limit
, n
, head
, hlist_node
) {
1168 if (ct_limit
->zone
== zone
) {
1169 hlist_del_rcu(&ct_limit
->hlist_node
);
1170 kfree_rcu(ct_limit
, rcu
);
1176 /* Call with RCU read lock */
1177 static u32
ct_limit_get(const struct ovs_ct_limit_info
*info
, u16 zone
)
1179 struct ovs_ct_limit
*ct_limit
;
1180 struct hlist_head
*head
;
1182 head
= ct_limit_hash_bucket(info
, zone
);
1183 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1184 if (ct_limit
->zone
== zone
)
1185 return ct_limit
->limit
;
1188 return info
->default_limit
;
1191 static int ovs_ct_check_limit(struct net
*net
,
1192 const struct ovs_conntrack_info
*info
,
1193 const struct nf_conntrack_tuple
*tuple
)
1195 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1196 const struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
1197 u32 per_zone_limit
, connections
;
1200 conncount_key
= info
->zone
.id
;
1202 per_zone_limit
= ct_limit_get(ct_limit_info
, info
->zone
.id
);
1203 if (per_zone_limit
== OVS_CT_LIMIT_UNLIMITED
)
1206 connections
= nf_conncount_count(net
, ct_limit_info
->data
,
1207 &conncount_key
, tuple
, &info
->zone
);
1208 if (connections
> per_zone_limit
)
1215 /* Lookup connection and confirm if unconfirmed. */
1216 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
1217 const struct ovs_conntrack_info
*info
,
1218 struct sk_buff
*skb
)
1220 enum ip_conntrack_info ctinfo
;
1224 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1228 /* The connection could be invalid, in which case this is a no-op.*/
1229 ct
= nf_ct_get(skb
, &ctinfo
);
1233 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1234 if (static_branch_unlikely(&ovs_ct_limit_enabled
)) {
1235 if (!nf_ct_is_confirmed(ct
)) {
1236 err
= ovs_ct_check_limit(net
, info
,
1237 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
1239 net_warn_ratelimited("openvswitch: zone: %u "
1240 "exceeds conntrack limit\n",
1248 /* Set the conntrack event mask if given. NEW and DELETE events have
1249 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1250 * typically would receive many kinds of updates. Setting the event
1251 * mask allows those events to be filtered. The set event mask will
1252 * remain in effect for the lifetime of the connection unless changed
1253 * by a further CT action with both the commit flag and the eventmask
1255 if (info
->have_eventmask
) {
1256 struct nf_conntrack_ecache
*cache
= nf_ct_ecache_find(ct
);
1259 cache
->ctmask
= info
->eventmask
;
1262 /* Apply changes before confirming the connection so that the initial
1263 * conntrack NEW netlink event carries the values given in the CT
1266 if (info
->mark
.mask
) {
1267 err
= ovs_ct_set_mark(ct
, key
, info
->mark
.value
,
1272 if (!nf_ct_is_confirmed(ct
)) {
1273 err
= ovs_ct_init_labels(ct
, key
, &info
->labels
.value
,
1274 &info
->labels
.mask
);
1277 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1278 labels_nonzero(&info
->labels
.mask
)) {
1279 err
= ovs_ct_set_labels(ct
, key
, &info
->labels
.value
,
1280 &info
->labels
.mask
);
1284 /* This will take care of sending queued events even if the connection
1285 * is already confirmed.
1287 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
1293 /* Trim the skb to the length specified by the IP/IPv6 header,
1294 * removing any trailing lower-layer padding. This prepares the skb
1295 * for higher-layer processing that assumes skb->len excludes padding
1296 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1297 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1299 static int ovs_skb_network_trim(struct sk_buff
*skb
)
1304 switch (skb
->protocol
) {
1305 case htons(ETH_P_IP
):
1306 len
= ntohs(ip_hdr(skb
)->tot_len
);
1308 case htons(ETH_P_IPV6
):
1309 len
= sizeof(struct ipv6hdr
)
1310 + ntohs(ipv6_hdr(skb
)->payload_len
);
1316 err
= pskb_trim_rcsum(skb
, len
);
1323 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1324 * value if 'skb' is freed.
1326 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
1327 struct sw_flow_key
*key
,
1328 const struct ovs_conntrack_info
*info
)
1333 /* The conntrack module expects to be working at L3. */
1334 nh_ofs
= skb_network_offset(skb
);
1335 skb_pull_rcsum(skb
, nh_ofs
);
1337 err
= ovs_skb_network_trim(skb
);
1341 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
1342 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
1348 err
= ovs_ct_commit(net
, key
, info
, skb
);
1350 err
= ovs_ct_lookup(net
, key
, info
, skb
);
1352 skb_push(skb
, nh_ofs
);
1353 skb_postpush_rcsum(skb
, skb
->data
, nh_ofs
);
1359 int ovs_ct_clear(struct sk_buff
*skb
, struct sw_flow_key
*key
)
1361 if (skb_nfct(skb
)) {
1362 nf_conntrack_put(skb_nfct(skb
));
1363 #ifdef HAVE_IP_CT_UNTRACKED
1364 nf_ct_set(skb
, NULL
, IP_CT_UNTRACKED
);
1366 nf_ct_set(skb
, NULL
, 0);
1368 ovs_ct_fill_key(skb
, key
);
1374 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
1375 const struct sw_flow_key
*key
, bool log
)
1377 struct nf_conntrack_helper
*helper
;
1378 struct nf_conn_help
*help
;
1380 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
1383 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
1387 help
= nf_ct_helper_ext_add(info
->ct
, helper
, GFP_KERNEL
);
1389 nf_conntrack_helper_put(helper
);
1393 rcu_assign_pointer(help
->helper
, helper
);
1394 info
->helper
= helper
;
1397 request_module("ip_nat_%s", name
);
1402 #ifdef CONFIG_NF_NAT_NEEDED
1403 static int parse_nat(const struct nlattr
*attr
,
1404 struct ovs_conntrack_info
*info
, bool log
)
1408 bool have_ip_max
= false;
1409 bool have_proto_max
= false;
1410 bool ip_vers
= (info
->family
== NFPROTO_IPV6
);
1412 nla_for_each_nested(a
, attr
, rem
) {
1413 static const int ovs_nat_attr_lens
[OVS_NAT_ATTR_MAX
+ 1][2] = {
1414 [OVS_NAT_ATTR_SRC
] = {0, 0},
1415 [OVS_NAT_ATTR_DST
] = {0, 0},
1416 [OVS_NAT_ATTR_IP_MIN
] = {sizeof(struct in_addr
),
1417 sizeof(struct in6_addr
)},
1418 [OVS_NAT_ATTR_IP_MAX
] = {sizeof(struct in_addr
),
1419 sizeof(struct in6_addr
)},
1420 [OVS_NAT_ATTR_PROTO_MIN
] = {sizeof(u16
), sizeof(u16
)},
1421 [OVS_NAT_ATTR_PROTO_MAX
] = {sizeof(u16
), sizeof(u16
)},
1422 [OVS_NAT_ATTR_PERSISTENT
] = {0, 0},
1423 [OVS_NAT_ATTR_PROTO_HASH
] = {0, 0},
1424 [OVS_NAT_ATTR_PROTO_RANDOM
] = {0, 0},
1426 int type
= nla_type(a
);
1428 if (type
> OVS_NAT_ATTR_MAX
) {
1429 OVS_NLERR(log
, "Unknown NAT attribute (type=%d, max=%d)",
1430 type
, OVS_NAT_ATTR_MAX
);
1434 if (nla_len(a
) != ovs_nat_attr_lens
[type
][ip_vers
]) {
1435 OVS_NLERR(log
, "NAT attribute type %d has unexpected length (%d != %d)",
1437 ovs_nat_attr_lens
[type
][ip_vers
]);
1442 case OVS_NAT_ATTR_SRC
:
1443 case OVS_NAT_ATTR_DST
:
1445 OVS_NLERR(log
, "Only one type of NAT may be specified");
1448 info
->nat
|= OVS_CT_NAT
;
1449 info
->nat
|= ((type
== OVS_NAT_ATTR_SRC
)
1450 ? OVS_CT_SRC_NAT
: OVS_CT_DST_NAT
);
1453 case OVS_NAT_ATTR_IP_MIN
:
1454 nla_memcpy(&info
->range
.min_addr
, a
,
1455 sizeof(info
->range
.min_addr
));
1456 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1459 case OVS_NAT_ATTR_IP_MAX
:
1461 nla_memcpy(&info
->range
.max_addr
, a
,
1462 sizeof(info
->range
.max_addr
));
1463 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1466 case OVS_NAT_ATTR_PROTO_MIN
:
1467 info
->range
.min_proto
.all
= htons(nla_get_u16(a
));
1468 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1471 case OVS_NAT_ATTR_PROTO_MAX
:
1472 have_proto_max
= true;
1473 info
->range
.max_proto
.all
= htons(nla_get_u16(a
));
1474 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1477 case OVS_NAT_ATTR_PERSISTENT
:
1478 info
->range
.flags
|= NF_NAT_RANGE_PERSISTENT
;
1481 case OVS_NAT_ATTR_PROTO_HASH
:
1482 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1485 case OVS_NAT_ATTR_PROTO_RANDOM
:
1486 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1487 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM_FULLY
;
1489 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1490 info
->random_fully_compat
= true;
1495 OVS_NLERR(log
, "Unknown nat attribute (%d)", type
);
1501 OVS_NLERR(log
, "NAT attribute has %d unknown bytes", rem
);
1505 /* Do not allow flags if no type is given. */
1506 if (info
->range
.flags
) {
1508 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1512 info
->nat
= OVS_CT_NAT
; /* NAT existing connections. */
1513 } else if (!info
->commit
) {
1515 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1519 /* Allow missing IP_MAX. */
1520 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
&& !have_ip_max
) {
1521 memcpy(&info
->range
.max_addr
, &info
->range
.min_addr
,
1522 sizeof(info
->range
.max_addr
));
1524 /* Allow missing PROTO_MAX. */
1525 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1527 info
->range
.max_proto
.all
= info
->range
.min_proto
.all
;
1533 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
1534 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1535 [OVS_CT_ATTR_FORCE_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1536 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
1537 .maxlen
= sizeof(u16
) },
1538 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
1539 .maxlen
= sizeof(struct md_mark
) },
1540 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
1541 .maxlen
= sizeof(struct md_labels
) },
1542 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
1543 .maxlen
= NF_CT_HELPER_NAME_LEN
},
1544 #ifdef CONFIG_NF_NAT_NEEDED
1545 /* NAT length is checked when parsing the nested attributes. */
1546 [OVS_CT_ATTR_NAT
] = { .minlen
= 0, .maxlen
= INT_MAX
},
1548 [OVS_CT_ATTR_EVENTMASK
] = { .minlen
= sizeof(u32
),
1549 .maxlen
= sizeof(u32
) },
1550 [OVS_CT_ATTR_TIMEOUT
] = { .minlen
= 1,
1551 .maxlen
= CTNL_TIMEOUT_NAME_MAX
},
1554 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
1555 const char **helper
, bool log
)
1560 nla_for_each_nested(a
, attr
, rem
) {
1561 int type
= nla_type(a
);
1565 if (type
> OVS_CT_ATTR_MAX
) {
1567 "Unknown conntrack attr (type=%d, max=%d)",
1568 type
, OVS_CT_ATTR_MAX
);
1572 maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
1573 minlen
= ovs_ct_attr_lens
[type
].minlen
;
1574 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
1576 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1577 type
, nla_len(a
), maxlen
);
1582 case OVS_CT_ATTR_FORCE_COMMIT
:
1585 case OVS_CT_ATTR_COMMIT
:
1586 info
->commit
= true;
1588 #ifdef CONFIG_NF_CONNTRACK_ZONES
1589 case OVS_CT_ATTR_ZONE
:
1590 info
->zone
.id
= nla_get_u16(a
);
1593 #ifdef CONFIG_NF_CONNTRACK_MARK
1594 case OVS_CT_ATTR_MARK
: {
1595 struct md_mark
*mark
= nla_data(a
);
1598 OVS_NLERR(log
, "ct_mark mask cannot be 0");
1605 #ifdef CONFIG_NF_CONNTRACK_LABELS
1606 case OVS_CT_ATTR_LABELS
: {
1607 struct md_labels
*labels
= nla_data(a
);
1609 if (!labels_nonzero(&labels
->mask
)) {
1610 OVS_NLERR(log
, "ct_labels mask cannot be 0");
1613 info
->labels
= *labels
;
1617 case OVS_CT_ATTR_HELPER
:
1618 *helper
= nla_data(a
);
1619 if (!memchr(*helper
, '\0', nla_len(a
))) {
1620 OVS_NLERR(log
, "Invalid conntrack helper");
1624 #ifdef CONFIG_NF_NAT_NEEDED
1625 case OVS_CT_ATTR_NAT
: {
1626 int err
= parse_nat(a
, info
, log
);
1633 case OVS_CT_ATTR_EVENTMASK
:
1634 info
->have_eventmask
= true;
1635 info
->eventmask
= nla_get_u32(a
);
1637 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1638 case OVS_CT_ATTR_TIMEOUT
:
1639 memcpy(info
->timeout
, nla_data(a
), nla_len(a
));
1640 if (!memchr(info
->timeout
, '\0', nla_len(a
))) {
1641 OVS_NLERR(log
, "Invalid conntrack helper");
1648 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
1654 #ifdef CONFIG_NF_CONNTRACK_MARK
1655 if (!info
->commit
&& info
->mark
.mask
) {
1657 "Setting conntrack mark requires 'commit' flag.");
1661 #ifdef CONFIG_NF_CONNTRACK_LABELS
1662 if (!info
->commit
&& labels_nonzero(&info
->labels
.mask
)) {
1664 "Setting conntrack labels requires 'commit' flag.");
1669 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
1676 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
1678 if (attr
== OVS_KEY_ATTR_CT_STATE
)
1680 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1681 attr
== OVS_KEY_ATTR_CT_ZONE
)
1683 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
1684 attr
== OVS_KEY_ATTR_CT_MARK
)
1686 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1687 attr
== OVS_KEY_ATTR_CT_LABELS
) {
1688 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1690 return ovs_net
->xt_label
;
1696 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
1697 const struct sw_flow_key
*key
,
1698 struct sw_flow_actions
**sfa
, bool log
)
1700 struct ovs_conntrack_info ct_info
;
1701 const char *helper
= NULL
;
1705 family
= key_to_nfproto(key
);
1706 if (family
== NFPROTO_UNSPEC
) {
1707 OVS_NLERR(log
, "ct family unspecified");
1711 memset(&ct_info
, 0, sizeof(ct_info
));
1712 ct_info
.family
= family
;
1714 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
1715 NF_CT_DEFAULT_ZONE_DIR
, 0);
1717 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
1721 /* Set up template for tracking connections in specific zones. */
1722 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
1724 OVS_NLERR(log
, "Failed to allocate conntrack template");
1728 if (ct_info
.timeout
[0]) {
1729 if (nf_ct_set_timeout(net
, ct_info
.ct
, family
, key
->ip
.proto
,
1731 pr_info_ratelimited("Failed to associated timeout "
1732 "policy `%s'\n", ct_info
.timeout
);
1734 ct_info
.nf_ct_timeout
= rcu_dereference(
1735 nf_ct_timeout_find(ct_info
.ct
)->timeout
);
1740 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
1745 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
1746 sizeof(ct_info
), log
);
1750 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
1751 nf_conntrack_get(&ct_info
.ct
->ct_general
);
1754 __ovs_ct_free_action(&ct_info
);
1758 #ifdef CONFIG_NF_NAT_NEEDED
1759 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info
*info
,
1760 struct sk_buff
*skb
)
1762 struct nlattr
*start
;
1764 start
= nla_nest_start(skb
, OVS_CT_ATTR_NAT
);
1768 if (info
->nat
& OVS_CT_SRC_NAT
) {
1769 if (nla_put_flag(skb
, OVS_NAT_ATTR_SRC
))
1771 } else if (info
->nat
& OVS_CT_DST_NAT
) {
1772 if (nla_put_flag(skb
, OVS_NAT_ATTR_DST
))
1778 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
) {
1779 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
1780 info
->family
== NFPROTO_IPV4
) {
1781 if (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1782 info
->range
.min_addr
.ip
) ||
1783 (info
->range
.max_addr
.ip
1784 != info
->range
.min_addr
.ip
&&
1785 (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1786 info
->range
.max_addr
.ip
))))
1788 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
1789 info
->family
== NFPROTO_IPV6
) {
1790 if (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1791 &info
->range
.min_addr
.in6
) ||
1792 (memcmp(&info
->range
.max_addr
.in6
,
1793 &info
->range
.min_addr
.in6
,
1794 sizeof(info
->range
.max_addr
.in6
)) &&
1795 (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1796 &info
->range
.max_addr
.in6
))))
1802 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1803 (nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MIN
,
1804 ntohs(info
->range
.min_proto
.all
)) ||
1805 (info
->range
.max_proto
.all
!= info
->range
.min_proto
.all
&&
1806 nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MAX
,
1807 ntohs(info
->range
.max_proto
.all
)))))
1810 if (info
->range
.flags
& NF_NAT_RANGE_PERSISTENT
&&
1811 nla_put_flag(skb
, OVS_NAT_ATTR_PERSISTENT
))
1813 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM
&&
1814 nla_put_flag(skb
, info
->random_fully_compat
1815 ? OVS_NAT_ATTR_PROTO_RANDOM
1816 : OVS_NAT_ATTR_PROTO_HASH
))
1818 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1819 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM_FULLY
&&
1820 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_RANDOM
))
1824 nla_nest_end(skb
, start
);
1830 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
1831 struct sk_buff
*skb
)
1833 struct nlattr
*start
;
1835 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_CT
);
1839 if (ct_info
->commit
&& nla_put_flag(skb
, ct_info
->force
1840 ? OVS_CT_ATTR_FORCE_COMMIT
1841 : OVS_CT_ATTR_COMMIT
))
1843 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1844 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
1846 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
1847 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
1850 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1851 labels_nonzero(&ct_info
->labels
.mask
) &&
1852 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
1855 if (ct_info
->helper
) {
1856 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
1857 ct_info
->helper
->name
))
1860 if (ct_info
->have_eventmask
&&
1861 nla_put_u32(skb
, OVS_CT_ATTR_EVENTMASK
, ct_info
->eventmask
))
1863 if (ct_info
->timeout
[0]) {
1864 if (nla_put_string(skb
, OVS_CT_ATTR_TIMEOUT
, ct_info
->timeout
))
1868 #ifdef CONFIG_NF_NAT_NEEDED
1869 if (ct_info
->nat
&& !ovs_ct_nat_to_attr(ct_info
, skb
))
1872 nla_nest_end(skb
, start
);
1877 void ovs_ct_free_action(const struct nlattr
*a
)
1879 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
1881 __ovs_ct_free_action(ct_info
);
1884 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
)
1886 if (ct_info
->helper
)
1887 nf_conntrack_helper_put(ct_info
->helper
);
1889 if (ct_info
->timeout
[0])
1890 nf_ct_destroy_timeout(ct_info
->ct
);
1891 nf_ct_tmpl_free(ct_info
->ct
);
1895 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1896 static int ovs_ct_limit_init(struct net
*net
, struct ovs_net
*ovs_net
)
1900 ovs_net
->ct_limit_info
= kmalloc(sizeof(*ovs_net
->ct_limit_info
),
1902 if (!ovs_net
->ct_limit_info
)
1905 ovs_net
->ct_limit_info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1906 ovs_net
->ct_limit_info
->limits
=
1907 kmalloc_array(CT_LIMIT_HASH_BUCKETS
, sizeof(struct hlist_head
),
1909 if (!ovs_net
->ct_limit_info
->limits
) {
1910 kfree(ovs_net
->ct_limit_info
);
1914 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; i
++)
1915 INIT_HLIST_HEAD(&ovs_net
->ct_limit_info
->limits
[i
]);
1917 ovs_net
->ct_limit_info
->data
=
1918 nf_conncount_init(net
, NFPROTO_INET
, sizeof(u32
));
1920 if (IS_ERR(ovs_net
->ct_limit_info
->data
)) {
1921 err
= PTR_ERR(ovs_net
->ct_limit_info
->data
);
1922 kfree(ovs_net
->ct_limit_info
->limits
);
1923 kfree(ovs_net
->ct_limit_info
);
1924 pr_err("openvswitch: failed to init nf_conncount %d\n", err
);
1930 static void ovs_ct_limit_exit(struct net
*net
, struct ovs_net
*ovs_net
)
1932 const struct ovs_ct_limit_info
*info
= ovs_net
->ct_limit_info
;
1935 nf_conncount_destroy(net
, NFPROTO_INET
, info
->data
);
1936 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
1937 struct hlist_head
*head
= &info
->limits
[i
];
1938 struct ovs_ct_limit
*ct_limit
;
1940 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
)
1941 kfree_rcu(ct_limit
, rcu
);
1943 kfree(ovs_net
->ct_limit_info
->limits
);
1944 kfree(ovs_net
->ct_limit_info
);
1947 static struct sk_buff
*
1948 ovs_ct_limit_cmd_reply_start(struct genl_info
*info
, u8 cmd
,
1949 struct ovs_header
**ovs_reply_header
)
1951 struct ovs_header
*ovs_header
= info
->userhdr
;
1952 struct sk_buff
*skb
;
1954 skb
= genlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1956 return ERR_PTR(-ENOMEM
);
1958 *ovs_reply_header
= genlmsg_put(skb
, info
->snd_portid
,
1960 &dp_ct_limit_genl_family
, 0, cmd
);
1962 if (!*ovs_reply_header
) {
1964 return ERR_PTR(-EMSGSIZE
);
1966 (*ovs_reply_header
)->dp_ifindex
= ovs_header
->dp_ifindex
;
1971 static bool check_zone_id(int zone_id
, u16
*pzone
)
1973 if (zone_id
>= 0 && zone_id
<= 65535) {
1974 *pzone
= (u16
)zone_id
;
1980 static int ovs_ct_limit_set_zone_limit(struct nlattr
*nla_zone_limit
,
1981 struct ovs_ct_limit_info
*info
)
1983 struct ovs_zone_limit
*zone_limit
;
1987 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1988 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1990 while (rem
>= sizeof(*zone_limit
)) {
1991 if (unlikely(zone_limit
->zone_id
==
1992 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1994 info
->default_limit
= zone_limit
->limit
;
1996 } else if (unlikely(!check_zone_id(
1997 zone_limit
->zone_id
, &zone
))) {
1998 OVS_NLERR(true, "zone id is out of range");
2000 struct ovs_ct_limit
*ct_limit
;
2002 ct_limit
= kmalloc(sizeof(*ct_limit
), GFP_KERNEL
);
2006 ct_limit
->zone
= zone
;
2007 ct_limit
->limit
= zone_limit
->limit
;
2010 ct_limit_set(info
, ct_limit
);
2013 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2014 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2015 NLA_ALIGN(sizeof(*zone_limit
)));
2019 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem
);
2024 static int ovs_ct_limit_del_zone_limit(struct nlattr
*nla_zone_limit
,
2025 struct ovs_ct_limit_info
*info
)
2027 struct ovs_zone_limit
*zone_limit
;
2031 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2032 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2034 while (rem
>= sizeof(*zone_limit
)) {
2035 if (unlikely(zone_limit
->zone_id
==
2036 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2038 info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
2040 } else if (unlikely(!check_zone_id(
2041 zone_limit
->zone_id
, &zone
))) {
2042 OVS_NLERR(true, "zone id is out of range");
2045 ct_limit_del(info
, zone
);
2048 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2049 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2050 NLA_ALIGN(sizeof(*zone_limit
)));
2054 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem
);
2059 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info
*info
,
2060 struct sk_buff
*reply
)
2062 struct ovs_zone_limit zone_limit
;
2065 zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2066 zone_limit
.limit
= info
->default_limit
;
2067 err
= nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2074 static int __ovs_ct_limit_get_zone_limit(struct net
*net
,
2075 struct nf_conncount_data
*data
,
2076 u16 zone_id
, u32 limit
,
2077 struct sk_buff
*reply
)
2079 struct nf_conntrack_zone ct_zone
;
2080 struct ovs_zone_limit zone_limit
;
2081 u32 conncount_key
= zone_id
;
2083 zone_limit
.zone_id
= zone_id
;
2084 zone_limit
.limit
= limit
;
2085 nf_ct_zone_init(&ct_zone
, zone_id
, NF_CT_DEFAULT_ZONE_DIR
, 0);
2087 zone_limit
.count
= nf_conncount_count(net
, data
, &conncount_key
, NULL
,
2089 return nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2092 static int ovs_ct_limit_get_zone_limit(struct net
*net
,
2093 struct nlattr
*nla_zone_limit
,
2094 struct ovs_ct_limit_info
*info
,
2095 struct sk_buff
*reply
)
2097 struct ovs_zone_limit
*zone_limit
;
2102 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2103 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2105 while (rem
>= sizeof(*zone_limit
)) {
2106 if (unlikely(zone_limit
->zone_id
==
2107 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2108 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2111 } else if (unlikely(!check_zone_id(zone_limit
->zone_id
,
2113 OVS_NLERR(true, "zone id is out of range");
2116 limit
= ct_limit_get(info
, zone
);
2119 err
= __ovs_ct_limit_get_zone_limit(
2120 net
, info
->data
, zone
, limit
, reply
);
2124 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2125 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2126 NLA_ALIGN(sizeof(*zone_limit
)));
2130 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem
);
2135 static int ovs_ct_limit_get_all_zone_limit(struct net
*net
,
2136 struct ovs_ct_limit_info
*info
,
2137 struct sk_buff
*reply
)
2139 struct ovs_ct_limit
*ct_limit
;
2140 struct hlist_head
*head
;
2143 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2148 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
2149 head
= &info
->limits
[i
];
2150 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
2151 err
= __ovs_ct_limit_get_zone_limit(net
, info
->data
,
2152 ct_limit
->zone
, ct_limit
->limit
, reply
);
2163 static int ovs_ct_limit_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2165 struct nlattr
**a
= info
->attrs
;
2166 struct sk_buff
*reply
;
2167 struct ovs_header
*ovs_reply_header
;
2168 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2169 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2172 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_SET
,
2175 return PTR_ERR(reply
);
2177 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2182 err
= ovs_ct_limit_set_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2187 static_branch_enable(&ovs_ct_limit_enabled
);
2189 genlmsg_end(reply
, ovs_reply_header
);
2190 return genlmsg_reply(reply
, info
);
2197 static int ovs_ct_limit_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2199 struct nlattr
**a
= info
->attrs
;
2200 struct sk_buff
*reply
;
2201 struct ovs_header
*ovs_reply_header
;
2202 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2203 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2206 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_DEL
,
2209 return PTR_ERR(reply
);
2211 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2216 err
= ovs_ct_limit_del_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2221 genlmsg_end(reply
, ovs_reply_header
);
2222 return genlmsg_reply(reply
, info
);
2229 static int ovs_ct_limit_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2231 struct nlattr
**a
= info
->attrs
;
2232 struct nlattr
*nla_reply
;
2233 struct sk_buff
*reply
;
2234 struct ovs_header
*ovs_reply_header
;
2235 struct net
*net
= sock_net(skb
->sk
);
2236 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2237 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2240 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_GET
,
2243 return PTR_ERR(reply
);
2245 nla_reply
= nla_nest_start(reply
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2247 if (a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2248 err
= ovs_ct_limit_get_zone_limit(
2249 net
, a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
], ct_limit_info
,
2254 err
= ovs_ct_limit_get_all_zone_limit(net
, ct_limit_info
,
2260 nla_nest_end(reply
, nla_reply
);
2261 genlmsg_end(reply
, ovs_reply_header
);
2262 return genlmsg_reply(reply
, info
);
2269 static struct genl_ops ct_limit_genl_ops
[] = {
2270 { .cmd
= OVS_CT_LIMIT_CMD_SET
,
2271 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2273 .policy
= ct_limit_policy
,
2274 .doit
= ovs_ct_limit_cmd_set
,
2276 { .cmd
= OVS_CT_LIMIT_CMD_DEL
,
2277 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2279 .policy
= ct_limit_policy
,
2280 .doit
= ovs_ct_limit_cmd_del
,
2282 { .cmd
= OVS_CT_LIMIT_CMD_GET
,
2283 .flags
= 0, /* OK for unprivileged users. */
2284 .policy
= ct_limit_policy
,
2285 .doit
= ovs_ct_limit_cmd_get
,
2289 static const struct genl_multicast_group ovs_ct_limit_multicast_group
= {
2290 .name
= OVS_CT_LIMIT_MCGROUP
,
2293 struct genl_family dp_ct_limit_genl_family __ro_after_init
= {
2294 .hdrsize
= sizeof(struct ovs_header
),
2295 .name
= OVS_CT_LIMIT_FAMILY
,
2296 .version
= OVS_CT_LIMIT_VERSION
,
2297 .maxattr
= OVS_CT_LIMIT_ATTR_MAX
,
2299 .parallel_ops
= true,
2300 .ops
= ct_limit_genl_ops
,
2301 .n_ops
= ARRAY_SIZE(ct_limit_genl_ops
),
2302 .mcgrps
= &ovs_ct_limit_multicast_group
,
2304 .module
= THIS_MODULE
,
2308 int ovs_ct_init(struct net
*net
)
2310 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
2311 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2313 if (nf_connlabels_get(net
, n_bits
- 1)) {
2314 ovs_net
->xt_label
= false;
2315 OVS_NLERR(true, "Failed to set connlabel length");
2317 ovs_net
->xt_label
= true;
2320 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2321 return ovs_ct_limit_init(net
, ovs_net
);
2327 void ovs_ct_exit(struct net
*net
)
2329 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2331 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2332 ovs_ct_limit_exit(net
, ovs_net
);
2335 if (ovs_net
->xt_label
)
2336 nf_connlabels_put(net
);
2339 #endif /* CONFIG_NF_CONNTRACK */