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 /* Upstream commit 4806e975729f ("netfilter: replace NF_NAT_NEEDED with
38 * IS_ENABLED(CONFIG_NF_NAT)") replaces the config checking on NF_NAT_NEEDED
39 * with CONFIG_NF_NAT. We will replace the checking on NF_NAT_NEEDED for the
40 * newer kernel with the marco in order to keep backward compatiblity.
42 #ifndef HAVE_CONFIG_NF_NAT_NEEDED
43 #define CONFIG_NF_NAT_NEEDED CONFIG_NF_NAT
46 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
47 /* Starting from upstream commit 3bf195ae6037 ("netfilter: nat: merge
48 * nf_nat_ipv4,6 into nat core") in kernel 5.1. nf_nat_ipv4,6 are merged
49 * into nf_nat. In order to keep backward compatibility, we keep the config
50 * checking as is for the old kernel, and replace them with marco for the
52 #ifdef HAVE_UPSTREAM_NF_NAT
53 #include <net/netfilter/nf_nat.h>
54 #define CONFIG_NF_NAT_IPV4 CONFIG_NF_NAT
55 #define CONFIG_NF_NAT_IPV6 CONFIG_IPV6
57 #include <linux/netfilter/nf_nat.h>
58 #include <net/netfilter/nf_nat_core.h>
59 #include <net/netfilter/nf_nat_l3proto.h>
60 #endif /* HAVE_UPSTREAM_NF_NAT */
61 #endif /* CONFIG_NF_NAT_NEEDED */
64 #include "conntrack.h"
66 #include "flow_netlink.h"
69 #ifndef HAVE_NF_NAT_RANGE2
70 #define nf_nat_range2 nf_nat_range
73 struct ovs_ct_len_tbl
{
78 /* Metadata mark for masked write to conntrack mark */
84 /* Metadata label for masked write to conntrack label. */
86 struct ovs_key_ct_labels value
;
87 struct ovs_key_ct_labels mask
;
91 OVS_CT_NAT
= 1 << 0, /* NAT for committed connections only. */
92 OVS_CT_SRC_NAT
= 1 << 1, /* Source NAT for NEW connections. */
93 OVS_CT_DST_NAT
= 1 << 2, /* Destination NAT for NEW connections. */
96 /* Conntrack action context for execution. */
97 struct ovs_conntrack_info
{
98 struct nf_conntrack_helper
*helper
;
99 struct nf_conntrack_zone zone
;
102 u8 nat
: 3; /* enum ovs_ct_nat */
103 u8 random_fully_compat
: 1; /* bool */
105 u8 have_eventmask
: 1;
107 u32 eventmask
; /* Mask of 1 << IPCT_*. */
109 struct md_labels labels
;
110 char timeout
[CTNL_TIMEOUT_NAME_MAX
];
111 struct nf_ct_timeout
*nf_ct_timeout
;
112 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
113 struct nf_nat_range2 range
; /* Only present for SRC NAT and DST NAT. */
117 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
118 #define OVS_CT_LIMIT_UNLIMITED 0
119 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
120 #define CT_LIMIT_HASH_BUCKETS 512
121 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled
);
123 struct ovs_ct_limit
{
124 /* Elements in ovs_ct_limit_info->limits hash table */
125 struct hlist_node hlist_node
;
131 struct ovs_ct_limit_info
{
133 struct hlist_head
*limits
;
134 struct nf_conncount_data
*data
;
137 static const struct nla_policy ct_limit_policy
[OVS_CT_LIMIT_ATTR_MAX
+ 1] = {
138 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NLA_NESTED
, },
142 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
);
144 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
);
146 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
148 switch (ntohs(key
->eth
.type
)) {
154 return NFPROTO_UNSPEC
;
158 /* Map SKB connection state into the values used by flow definition. */
159 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
161 u8 ct_state
= OVS_CS_F_TRACKED
;
164 case IP_CT_ESTABLISHED_REPLY
:
165 case IP_CT_RELATED_REPLY
:
166 ct_state
|= OVS_CS_F_REPLY_DIR
;
173 case IP_CT_ESTABLISHED
:
174 case IP_CT_ESTABLISHED_REPLY
:
175 ct_state
|= OVS_CS_F_ESTABLISHED
;
178 case IP_CT_RELATED_REPLY
:
179 ct_state
|= OVS_CS_F_RELATED
;
182 ct_state
|= OVS_CS_F_NEW
;
191 static u32
ovs_ct_get_mark(const struct nf_conn
*ct
)
193 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
194 return ct
? ct
->mark
: 0;
200 /* Guard against conntrack labels max size shrinking below 128 bits. */
201 #if NF_CT_LABELS_MAX_SIZE < 16
202 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
205 static void ovs_ct_get_labels(const struct nf_conn
*ct
,
206 struct ovs_key_ct_labels
*labels
)
208 struct nf_conn_labels
*cl
= ct
? nf_ct_labels_find(ct
) : NULL
;
211 memcpy(labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
213 memset(labels
, 0, OVS_CT_LABELS_LEN
);
216 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key
*key
,
217 const struct nf_conntrack_tuple
*orig
,
220 key
->ct_orig_proto
= orig
->dst
.protonum
;
221 if (orig
->dst
.protonum
== icmp_proto
) {
222 key
->ct
.orig_tp
.src
= htons(orig
->dst
.u
.icmp
.type
);
223 key
->ct
.orig_tp
.dst
= htons(orig
->dst
.u
.icmp
.code
);
225 key
->ct
.orig_tp
.src
= orig
->src
.u
.all
;
226 key
->ct
.orig_tp
.dst
= orig
->dst
.u
.all
;
230 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
231 const struct nf_conntrack_zone
*zone
,
232 const struct nf_conn
*ct
)
234 key
->ct_state
= state
;
235 key
->ct_zone
= zone
->id
;
236 key
->ct
.mark
= ovs_ct_get_mark(ct
);
237 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
240 const struct nf_conntrack_tuple
*orig
;
242 /* Use the master if we have one. */
245 orig
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
247 /* IP version must match with the master connection. */
248 if (key
->eth
.type
== htons(ETH_P_IP
) &&
249 nf_ct_l3num(ct
) == NFPROTO_IPV4
) {
250 key
->ipv4
.ct_orig
.src
= orig
->src
.u3
.ip
;
251 key
->ipv4
.ct_orig
.dst
= orig
->dst
.u3
.ip
;
252 __ovs_ct_update_key_orig_tp(key
, orig
, IPPROTO_ICMP
);
254 } else if (key
->eth
.type
== htons(ETH_P_IPV6
) &&
255 !sw_flow_key_is_nd(key
) &&
256 nf_ct_l3num(ct
) == NFPROTO_IPV6
) {
257 key
->ipv6
.ct_orig
.src
= orig
->src
.u3
.in6
;
258 key
->ipv6
.ct_orig
.dst
= orig
->dst
.u3
.in6
;
259 __ovs_ct_update_key_orig_tp(key
, orig
, NEXTHDR_ICMP
);
263 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
264 * original direction key fields.
266 key
->ct_orig_proto
= 0;
269 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
270 * previously sent the packet to conntrack via the ct action. If
271 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
272 * initialized from the connection status.
274 static void ovs_ct_update_key(const struct sk_buff
*skb
,
275 const struct ovs_conntrack_info
*info
,
276 struct sw_flow_key
*key
, bool post_ct
,
279 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
280 enum ip_conntrack_info ctinfo
;
284 ct
= nf_ct_get(skb
, &ctinfo
);
286 state
= ovs_ct_get_state(ctinfo
);
287 /* All unconfirmed entries are NEW connections. */
288 if (!nf_ct_is_confirmed(ct
))
289 state
|= OVS_CS_F_NEW
;
290 /* OVS persists the related flag for the duration of the
294 state
|= OVS_CS_F_RELATED
;
295 if (keep_nat_flags
) {
296 state
|= key
->ct_state
& OVS_CS_F_NAT_MASK
;
298 if (ct
->status
& IPS_SRC_NAT
)
299 state
|= OVS_CS_F_SRC_NAT
;
300 if (ct
->status
& IPS_DST_NAT
)
301 state
|= OVS_CS_F_DST_NAT
;
303 zone
= nf_ct_zone(ct
);
304 } else if (post_ct
) {
305 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
309 __ovs_ct_update_key(key
, state
, zone
, ct
);
312 /* This is called to initialize CT key fields possibly coming in from the local
315 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
317 ovs_ct_update_key(skb
, NULL
, key
, false, false);
320 #define IN6_ADDR_INITIALIZER(ADDR) \
321 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
322 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
324 int ovs_ct_put_key(const struct sw_flow_key
*swkey
,
325 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
327 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, output
->ct_state
))
330 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
331 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, output
->ct_zone
))
334 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
335 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, output
->ct
.mark
))
338 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
339 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(output
->ct
.labels
),
343 if (swkey
->ct_orig_proto
) {
344 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
345 struct ovs_key_ct_tuple_ipv4 orig
= {
346 output
->ipv4
.ct_orig
.src
,
347 output
->ipv4
.ct_orig
.dst
,
348 output
->ct
.orig_tp
.src
,
349 output
->ct
.orig_tp
.dst
,
350 output
->ct_orig_proto
,
352 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
353 sizeof(orig
), &orig
))
355 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
356 struct ovs_key_ct_tuple_ipv6 orig
= {
357 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.src
),
358 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.dst
),
359 output
->ct
.orig_tp
.src
,
360 output
->ct
.orig_tp
.dst
,
361 output
->ct_orig_proto
,
363 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
364 sizeof(orig
), &orig
))
372 static int ovs_ct_set_mark(struct nf_conn
*ct
, struct sw_flow_key
*key
,
373 u32 ct_mark
, u32 mask
)
375 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
378 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
379 if (ct
->mark
!= new_mark
) {
381 if (nf_ct_is_confirmed(ct
))
382 nf_conntrack_event_cache(IPCT_MARK
, ct
);
383 key
->ct
.mark
= new_mark
;
392 static struct nf_conn_labels
*ovs_ct_get_conn_labels(struct nf_conn
*ct
)
394 struct nf_conn_labels
*cl
;
396 cl
= nf_ct_labels_find(ct
);
398 nf_ct_labels_ext_add(ct
);
399 cl
= nf_ct_labels_find(ct
);
405 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
406 * since the new connection is not yet confirmed, and thus no-one else has
407 * access to it's labels, we simply write them over.
409 static int ovs_ct_init_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
410 const struct ovs_key_ct_labels
*labels
,
411 const struct ovs_key_ct_labels
*mask
)
413 struct nf_conn_labels
*cl
, *master_cl
;
414 bool have_mask
= labels_nonzero(mask
);
416 /* Inherit master's labels to the related connection? */
417 master_cl
= ct
->master
? nf_ct_labels_find(ct
->master
) : NULL
;
419 if (!master_cl
&& !have_mask
)
420 return 0; /* Nothing to do. */
422 cl
= ovs_ct_get_conn_labels(ct
);
426 /* Inherit the master's labels, if any. Must use memcpy for backport
427 * as struct assignment only copies the length field in older
431 memcpy(cl
->bits
, master_cl
->bits
, OVS_CT_LABELS_LEN
);
434 u32
*dst
= (u32
*)cl
->bits
;
437 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
438 dst
[i
] = (dst
[i
] & ~mask
->ct_labels_32
[i
]) |
439 (labels
->ct_labels_32
[i
]
440 & mask
->ct_labels_32
[i
]);
443 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
444 * IPCT_LABEL bit is set in the event cache.
446 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
448 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
453 static int ovs_ct_set_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
454 const struct ovs_key_ct_labels
*labels
,
455 const struct ovs_key_ct_labels
*mask
)
457 struct nf_conn_labels
*cl
;
460 cl
= ovs_ct_get_conn_labels(ct
);
464 err
= nf_connlabels_replace(ct
, labels
->ct_labels_32
,
466 OVS_CT_LABELS_LEN_32
);
470 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
475 /* 'skb' should already be pulled to nh_ofs. */
476 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
478 const struct nf_conntrack_helper
*helper
;
479 const struct nf_conn_help
*help
;
480 enum ip_conntrack_info ctinfo
;
481 unsigned int protoff
;
486 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
487 bool dst_set
= false;
488 struct rtable rt
= { .rt_flags
= 0 };
491 ct
= nf_ct_get(skb
, &ctinfo
);
492 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
495 help
= nfct_help(ct
);
499 helper
= rcu_dereference(help
->helper
);
505 protoff
= ip_hdrlen(skb
);
511 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
512 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
514 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
515 pr_debug("proto header not found\n");
522 WARN_ONCE(1, "helper invoked on non-IP family!");
526 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
527 /* Linux 4.5 and older depend on skb_dst being set when recalculating
528 * checksums after NAT helper has mangled TCP or UDP packet payload.
529 * skb_dst is cast to a rtable struct and the flags examined.
530 * Forcing these flags to have RTCF_LOCAL not set ensures checksum mod
531 * is carried out in the same way as kernel versions > 4.5
533 if (ct
->status
& IPS_NAT_MASK
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
536 skb_dst_set(skb
, &rt
.dst
);
539 err
= helper
->help(skb
, protoff
, ct
, ctinfo
);
540 if (err
!= NF_ACCEPT
)
543 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
545 skb_dst_set(skb
, NULL
);
548 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
549 * FTP with NAT) adusting the TCP payload size when mangling IP
550 * addresses and/or port numbers in the text-based control connection.
552 if (test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
) &&
553 !nf_ct_seq_adjust(skb
, ct
, ctinfo
, protoff
))
558 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
559 * value if 'skb' is freed.
561 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
562 u16 zone
, struct sk_buff
*skb
)
564 struct ovs_gso_cb ovs_cb
= *OVS_GSO_CB(skb
);
567 if (key
->eth
.type
== htons(ETH_P_IP
)) {
568 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
570 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
571 err
= ip_defrag(net
, skb
, user
);
575 ovs_cb
.dp_cb
.mru
= IPCB(skb
)->frag_max_size
;
576 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
577 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
578 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
580 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
581 err
= nf_ct_frag6_gather(net
, skb
, user
);
583 if (err
!= -EINPROGRESS
)
588 key
->ip
.proto
= ipv6_hdr(skb
)->nexthdr
;
589 ovs_cb
.dp_cb
.mru
= IP6CB(skb
)->frag_max_size
;
590 #endif /* IP frag support */
593 return -EPFNOSUPPORT
;
596 /* The key extracted from the fragment that completed this datagram
597 * likely didn't have an L4 header, so regenerate it.
599 ovs_flow_key_update_l3l4(skb
, key
);
601 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
604 *OVS_GSO_CB(skb
) = ovs_cb
;
609 static struct nf_conntrack_expect
*
610 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
611 u16 proto
, const struct sk_buff
*skb
)
613 struct nf_conntrack_tuple tuple
;
614 struct nf_conntrack_expect
*exp
;
616 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
619 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
621 struct nf_conntrack_tuple_hash
*h
;
623 /* Delete existing conntrack entry, if it clashes with the
624 * expectation. This can happen since conntrack ALGs do not
625 * check for clashes between (new) expectations and existing
626 * conntrack entries. nf_conntrack_in() will check the
627 * expectations only if a conntrack entry can not be found,
628 * which can lead to OVS finding the expectation (here) in the
629 * init direction, but which will not be removed by the
630 * nf_conntrack_in() call, if a matching conntrack entry is
631 * found instead. In this case all init direction packets
632 * would be reported as new related packets, while reply
633 * direction packets would be reported as un-related
634 * established packets.
636 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
638 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
640 nf_ct_delete(ct
, 0, 0);
641 nf_conntrack_put(&ct
->ct_general
);
648 /* This replicates logic from nf_conntrack_core.c that is not exported. */
649 static enum ip_conntrack_info
650 ovs_ct_get_info(const struct nf_conntrack_tuple_hash
*h
)
652 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
654 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
655 return IP_CT_ESTABLISHED_REPLY
;
656 /* Once we've had two way comms, always ESTABLISHED. */
657 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
658 return IP_CT_ESTABLISHED
;
659 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
660 return IP_CT_RELATED
;
664 /* Find an existing connection which this packet belongs to without
665 * re-attributing statistics or modifying the connection state. This allows an
666 * skb->_nfct lost due to an upcall to be recovered during actions execution.
668 * Must be called with rcu_read_lock.
670 * On success, populates skb->_nfct and returns the connection. Returns NULL
671 * if there is no existing entry.
673 static struct nf_conn
*
674 ovs_ct_find_existing(struct net
*net
, const struct nf_conntrack_zone
*zone
,
675 u8 l3num
, struct sk_buff
*skb
, bool natted
)
677 struct nf_conntrack_tuple tuple
;
678 struct nf_conntrack_tuple_hash
*h
;
681 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), l3num
,
683 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
687 /* Must invert the tuple if skb has been transformed by NAT. */
689 struct nf_conntrack_tuple inverse
;
691 if (!rpl_nf_ct_invert_tuple(&inverse
, &tuple
)) {
692 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
698 /* look for tuple match */
699 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
701 return NULL
; /* Not found. */
703 ct
= nf_ct_tuplehash_to_ctrack(h
);
705 /* Inverted packet tuple matches the reverse direction conntrack tuple,
706 * select the other tuplehash to get the right 'ctinfo' bits for this
710 h
= &ct
->tuplehash
[!h
->tuple
.dst
.dir
];
712 nf_ct_set(skb
, ct
, ovs_ct_get_info(h
));
717 struct nf_conn
*ovs_ct_executed(struct net
*net
,
718 const struct sw_flow_key
*key
,
719 const struct ovs_conntrack_info
*info
,
723 struct nf_conn
*ct
= NULL
;
725 /* If no ct, check if we have evidence that an existing conntrack entry
726 * might be found for this skb. This happens when we lose a skb->_nfct
727 * due to an upcall, or if the direction is being forced. If the
728 * connection was not confirmed, it is not cached and needs to be run
729 * through conntrack again.
731 *ct_executed
= (key
->ct_state
& OVS_CS_F_TRACKED
) &&
732 !(key
->ct_state
& OVS_CS_F_INVALID
) &&
733 (key
->ct_zone
== info
->zone
.id
);
735 if (*ct_executed
|| (!key
->ct_state
&& info
->force
)) {
736 ct
= ovs_ct_find_existing(net
, &info
->zone
, info
->family
, skb
,
744 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
745 static bool skb_nfct_cached(struct net
*net
,
746 const struct sw_flow_key
*key
,
747 const struct ovs_conntrack_info
*info
,
750 enum ip_conntrack_info ctinfo
;
752 bool ct_executed
= true;
754 ct
= nf_ct_get(skb
, &ctinfo
);
756 ct
= ovs_ct_executed(net
, key
, info
, skb
, &ct_executed
);
759 nf_ct_get(skb
, &ctinfo
);
763 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
765 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
768 struct nf_conn_help
*help
;
770 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
771 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
774 if (info
->nf_ct_timeout
) {
775 struct nf_conn_timeout
*timeout_ext
;
777 timeout_ext
= nf_ct_timeout_find(ct
);
778 if (!timeout_ext
|| info
->nf_ct_timeout
!=
779 rcu_dereference(timeout_ext
->timeout
))
782 /* Force conntrack entry direction to the current packet? */
783 if (info
->force
&& CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
) {
784 /* Delete the conntrack entry if confirmed, else just release
787 if (nf_ct_is_confirmed(ct
))
788 nf_ct_delete(ct
, 0, 0);
790 nf_conntrack_put(&ct
->ct_general
);
791 nf_ct_set(skb
, NULL
, 0);
798 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
799 /* Modelled after nf_nat_ipv[46]_fn().
800 * range is only used for new, uninitialized NAT state.
801 * Returns either NF_ACCEPT or NF_DROP.
803 static int ovs_ct_nat_execute(struct sk_buff
*skb
, struct nf_conn
*ct
,
804 enum ip_conntrack_info ctinfo
,
805 const struct nf_nat_range2
*range
,
806 enum nf_nat_manip_type maniptype
)
808 int hooknum
, nh_off
, err
= NF_ACCEPT
;
810 nh_off
= skb_network_offset(skb
);
811 skb_pull_rcsum(skb
, nh_off
);
813 /* See HOOK2MANIP(). */
814 if (maniptype
== NF_NAT_MANIP_SRC
)
815 hooknum
= NF_INET_LOCAL_IN
; /* Source NAT */
817 hooknum
= NF_INET_LOCAL_OUT
; /* Destination NAT */
821 case IP_CT_RELATED_REPLY
:
822 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
823 skb
->protocol
== htons(ETH_P_IP
) &&
824 ip_hdr(skb
)->protocol
== IPPROTO_ICMP
) {
825 if (!nf_nat_icmp_reply_translation(skb
, ct
, ctinfo
,
829 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
830 skb
->protocol
== htons(ETH_P_IPV6
)) {
832 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
833 int hdrlen
= ipv6_skip_exthdr(skb
,
834 sizeof(struct ipv6hdr
),
835 &nexthdr
, &frag_off
);
837 if (hdrlen
>= 0 && nexthdr
== IPPROTO_ICMPV6
) {
838 if (!nf_nat_icmpv6_reply_translation(skb
, ct
,
846 /* Non-ICMP, fall thru to initialize if needed. */
848 /* Seen it before? This can happen for loopback, retrans,
851 if (!nf_nat_initialized(ct
, maniptype
)) {
852 /* Initialize according to the NAT action. */
853 err
= (range
&& range
->flags
& NF_NAT_RANGE_MAP_IPS
)
854 /* Action is set up to establish a new
857 ? nf_nat_setup_info(ct
, range
, maniptype
)
858 : nf_nat_alloc_null_binding(ct
, hooknum
);
859 if (err
!= NF_ACCEPT
)
864 case IP_CT_ESTABLISHED
:
865 case IP_CT_ESTABLISHED_REPLY
:
873 err
= nf_nat_packet(ct
, ctinfo
, hooknum
, skb
);
875 skb_push(skb
, nh_off
);
876 skb_postpush_rcsum(skb
, skb
->data
, nh_off
);
881 static void ovs_nat_update_key(struct sw_flow_key
*key
,
882 const struct sk_buff
*skb
,
883 enum nf_nat_manip_type maniptype
)
885 if (maniptype
== NF_NAT_MANIP_SRC
) {
888 key
->ct_state
|= OVS_CS_F_SRC_NAT
;
889 if (key
->eth
.type
== htons(ETH_P_IP
))
890 key
->ipv4
.addr
.src
= ip_hdr(skb
)->saddr
;
891 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
892 memcpy(&key
->ipv6
.addr
.src
, &ipv6_hdr(skb
)->saddr
,
893 sizeof(key
->ipv6
.addr
.src
));
897 if (key
->ip
.proto
== IPPROTO_UDP
)
898 src
= udp_hdr(skb
)->source
;
899 else if (key
->ip
.proto
== IPPROTO_TCP
)
900 src
= tcp_hdr(skb
)->source
;
901 else if (key
->ip
.proto
== IPPROTO_SCTP
)
902 src
= sctp_hdr(skb
)->source
;
910 key
->ct_state
|= OVS_CS_F_DST_NAT
;
911 if (key
->eth
.type
== htons(ETH_P_IP
))
912 key
->ipv4
.addr
.dst
= ip_hdr(skb
)->daddr
;
913 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
914 memcpy(&key
->ipv6
.addr
.dst
, &ipv6_hdr(skb
)->daddr
,
915 sizeof(key
->ipv6
.addr
.dst
));
919 if (key
->ip
.proto
== IPPROTO_UDP
)
920 dst
= udp_hdr(skb
)->dest
;
921 else if (key
->ip
.proto
== IPPROTO_TCP
)
922 dst
= tcp_hdr(skb
)->dest
;
923 else if (key
->ip
.proto
== IPPROTO_SCTP
)
924 dst
= sctp_hdr(skb
)->dest
;
932 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
933 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
934 const struct ovs_conntrack_info
*info
,
935 struct sk_buff
*skb
, struct nf_conn
*ct
,
936 enum ip_conntrack_info ctinfo
)
938 enum nf_nat_manip_type maniptype
;
941 #ifdef HAVE_NF_CT_IS_UNTRACKED
942 if (nf_ct_is_untracked(ct
)) {
943 /* A NAT action may only be performed on tracked packets. */
946 #endif /* HAVE_NF_CT_IS_UNTRACKED */
948 /* Add NAT extension if not confirmed yet. */
949 if (!nf_ct_is_confirmed(ct
) && !nf_ct_nat_ext_add(ct
))
950 return NF_ACCEPT
; /* Can't NAT. */
952 /* Determine NAT type.
953 * Check if the NAT type can be deduced from the tracked connection.
954 * Make sure new expected connections (IP_CT_RELATED) are NATted only
957 if (info
->nat
& OVS_CT_NAT
&& ctinfo
!= IP_CT_NEW
&&
958 ct
->status
& IPS_NAT_MASK
&&
959 (ctinfo
!= IP_CT_RELATED
|| info
->commit
)) {
960 /* NAT an established or related connection like before. */
961 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
)
962 /* This is the REPLY direction for a connection
963 * for which NAT was applied in the forward
964 * direction. Do the reverse NAT.
966 maniptype
= ct
->status
& IPS_SRC_NAT
967 ? NF_NAT_MANIP_DST
: NF_NAT_MANIP_SRC
;
969 maniptype
= ct
->status
& IPS_SRC_NAT
970 ? NF_NAT_MANIP_SRC
: NF_NAT_MANIP_DST
;
971 } else if (info
->nat
& OVS_CT_SRC_NAT
) {
972 maniptype
= NF_NAT_MANIP_SRC
;
973 } else if (info
->nat
& OVS_CT_DST_NAT
) {
974 maniptype
= NF_NAT_MANIP_DST
;
976 return NF_ACCEPT
; /* Connection is not NATed. */
978 err
= ovs_ct_nat_execute(skb
, ct
, ctinfo
, &info
->range
, maniptype
);
980 /* Mark NAT done if successful and update the flow key. */
981 if (err
== NF_ACCEPT
)
982 ovs_nat_update_key(key
, skb
, maniptype
);
986 #else /* !CONFIG_NF_NAT_NEEDED */
987 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
988 const struct ovs_conntrack_info
*info
,
989 struct sk_buff
*skb
, struct nf_conn
*ct
,
990 enum ip_conntrack_info ctinfo
)
996 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
997 * not done already. Update key with new CT state after passing the packet
999 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
1000 * set to NULL and 0 will be returned.
1002 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
1003 const struct ovs_conntrack_info
*info
,
1004 struct sk_buff
*skb
)
1006 /* If we are recirculating packets to match on conntrack fields and
1007 * committing with a separate conntrack action, then we don't need to
1008 * actually run the packet through conntrack twice unless it's for a
1011 bool cached
= skb_nfct_cached(net
, key
, info
, skb
);
1012 enum ip_conntrack_info ctinfo
;
1016 struct nf_hook_state state
= {
1017 .hook
= NF_INET_PRE_ROUTING
,
1021 struct nf_conn
*tmpl
= info
->ct
;
1024 /* Associate skb with specified zone. */
1027 nf_conntrack_put(skb_nfct(skb
));
1028 nf_conntrack_get(&tmpl
->ct_general
);
1029 nf_ct_set(skb
, tmpl
, IP_CT_NEW
);
1032 err
= nf_conntrack_in(skb
, &state
);
1033 if (err
!= NF_ACCEPT
)
1036 /* Clear CT state NAT flags to mark that we have not yet done
1037 * NAT after the nf_conntrack_in() call. We can actually clear
1038 * the whole state, as it will be re-initialized below.
1042 /* Update the key, but keep the NAT flags. */
1043 ovs_ct_update_key(skb
, info
, key
, true, true);
1046 ct
= nf_ct_get(skb
, &ctinfo
);
1048 /* Packets starting a new connection must be NATted before the
1049 * helper, so that the helper knows about the NAT. We enforce
1050 * this by delaying both NAT and helper calls for unconfirmed
1051 * connections until the committing CT action. For later
1052 * packets NAT and Helper may be called in either order.
1054 * NAT will be done only if the CT action has NAT, and only
1055 * once per packet (per zone), as guarded by the NAT bits in
1056 * the key->ct_state.
1058 if (info
->nat
&& !(key
->ct_state
& OVS_CS_F_NAT_MASK
) &&
1059 (nf_ct_is_confirmed(ct
) || info
->commit
) &&
1060 ovs_ct_nat(net
, key
, info
, skb
, ct
, ctinfo
) != NF_ACCEPT
) {
1064 /* Userspace may decide to perform a ct lookup without a helper
1065 * specified followed by a (recirculate and) commit with one.
1066 * Therefore, for unconfirmed connections which we will commit,
1067 * we need to attach the helper here.
1069 if (!nf_ct_is_confirmed(ct
) && info
->commit
&&
1070 info
->helper
&& !nfct_help(ct
)) {
1071 int err
= __nf_ct_try_assign_helper(ct
, info
->ct
,
1076 /* helper installed, add seqadj if NAT is required */
1077 if (info
->nat
&& !nfct_seqadj(ct
)) {
1078 if (!nfct_seqadj_ext_add(ct
))
1083 /* Call the helper only if:
1084 * - nf_conntrack_in() was executed above ("!cached") for a
1085 * confirmed connection, or
1086 * - When committing an unconfirmed connection.
1088 if ((nf_ct_is_confirmed(ct
) ? !cached
: info
->commit
) &&
1089 ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
1097 /* Lookup connection and read fields into key. */
1098 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
1099 const struct ovs_conntrack_info
*info
,
1100 struct sk_buff
*skb
)
1102 struct nf_conntrack_expect
*exp
;
1104 /* If we pass an expected packet through nf_conntrack_in() the
1105 * expectation is typically removed, but the packet could still be
1106 * lost in upcall processing. To prevent this from happening we
1107 * perform an explicit expectation lookup. Expected connections are
1108 * always new, and will be passed through conntrack only when they are
1109 * committed, as it is OK to remove the expectation at that time.
1111 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
1115 /* NOTE: New connections are NATted and Helped only when
1116 * committed, so we are not calling into NAT here.
1118 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
1119 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
1124 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1128 ct
= (struct nf_conn
*)skb_nfct(skb
);
1130 nf_ct_deliver_cached_events(ct
);
1136 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
1140 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
1141 if (labels
->ct_labels_32
[i
])
1147 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1148 static struct hlist_head
*ct_limit_hash_bucket(
1149 const struct ovs_ct_limit_info
*info
, u16 zone
)
1151 return &info
->limits
[zone
& (CT_LIMIT_HASH_BUCKETS
- 1)];
1154 /* Call with ovs_mutex */
1155 static void ct_limit_set(const struct ovs_ct_limit_info
*info
,
1156 struct ovs_ct_limit
*new_ct_limit
)
1158 struct ovs_ct_limit
*ct_limit
;
1159 struct hlist_head
*head
;
1161 head
= ct_limit_hash_bucket(info
, new_ct_limit
->zone
);
1162 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1163 if (ct_limit
->zone
== new_ct_limit
->zone
) {
1164 hlist_replace_rcu(&ct_limit
->hlist_node
,
1165 &new_ct_limit
->hlist_node
);
1166 kfree_rcu(ct_limit
, rcu
);
1171 hlist_add_head_rcu(&new_ct_limit
->hlist_node
, head
);
1174 /* Call with ovs_mutex */
1175 static void ct_limit_del(const struct ovs_ct_limit_info
*info
, u16 zone
)
1177 struct ovs_ct_limit
*ct_limit
;
1178 struct hlist_head
*head
;
1179 struct hlist_node
*n
;
1181 head
= ct_limit_hash_bucket(info
, zone
);
1182 hlist_for_each_entry_safe(ct_limit
, n
, head
, hlist_node
) {
1183 if (ct_limit
->zone
== zone
) {
1184 hlist_del_rcu(&ct_limit
->hlist_node
);
1185 kfree_rcu(ct_limit
, rcu
);
1191 /* Call with RCU read lock */
1192 static u32
ct_limit_get(const struct ovs_ct_limit_info
*info
, u16 zone
)
1194 struct ovs_ct_limit
*ct_limit
;
1195 struct hlist_head
*head
;
1197 head
= ct_limit_hash_bucket(info
, zone
);
1198 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1199 if (ct_limit
->zone
== zone
)
1200 return ct_limit
->limit
;
1203 return info
->default_limit
;
1206 static int ovs_ct_check_limit(struct net
*net
,
1207 const struct ovs_conntrack_info
*info
,
1208 const struct nf_conntrack_tuple
*tuple
)
1210 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1211 const struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
1212 u32 per_zone_limit
, connections
;
1215 conncount_key
= info
->zone
.id
;
1217 per_zone_limit
= ct_limit_get(ct_limit_info
, info
->zone
.id
);
1218 if (per_zone_limit
== OVS_CT_LIMIT_UNLIMITED
)
1221 connections
= nf_conncount_count(net
, ct_limit_info
->data
,
1222 &conncount_key
, tuple
, &info
->zone
);
1223 if (connections
> per_zone_limit
)
1230 /* Lookup connection and confirm if unconfirmed. */
1231 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
1232 const struct ovs_conntrack_info
*info
,
1233 struct sk_buff
*skb
)
1235 enum ip_conntrack_info ctinfo
;
1239 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1243 /* The connection could be invalid, in which case this is a no-op.*/
1244 ct
= nf_ct_get(skb
, &ctinfo
);
1248 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1249 if (static_branch_unlikely(&ovs_ct_limit_enabled
)) {
1250 if (!nf_ct_is_confirmed(ct
)) {
1251 err
= ovs_ct_check_limit(net
, info
,
1252 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
1254 net_warn_ratelimited("openvswitch: zone: %u "
1255 "exceeds conntrack limit\n",
1263 /* Set the conntrack event mask if given. NEW and DELETE events have
1264 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1265 * typically would receive many kinds of updates. Setting the event
1266 * mask allows those events to be filtered. The set event mask will
1267 * remain in effect for the lifetime of the connection unless changed
1268 * by a further CT action with both the commit flag and the eventmask
1270 if (info
->have_eventmask
) {
1271 struct nf_conntrack_ecache
*cache
= nf_ct_ecache_find(ct
);
1274 cache
->ctmask
= info
->eventmask
;
1277 /* Apply changes before confirming the connection so that the initial
1278 * conntrack NEW netlink event carries the values given in the CT
1281 if (info
->mark
.mask
) {
1282 err
= ovs_ct_set_mark(ct
, key
, info
->mark
.value
,
1287 if (!nf_ct_is_confirmed(ct
)) {
1288 err
= ovs_ct_init_labels(ct
, key
, &info
->labels
.value
,
1289 &info
->labels
.mask
);
1292 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1293 labels_nonzero(&info
->labels
.mask
)) {
1294 err
= ovs_ct_set_labels(ct
, key
, &info
->labels
.value
,
1295 &info
->labels
.mask
);
1299 /* This will take care of sending queued events even if the connection
1300 * is already confirmed.
1302 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
1308 /* Trim the skb to the length specified by the IP/IPv6 header,
1309 * removing any trailing lower-layer padding. This prepares the skb
1310 * for higher-layer processing that assumes skb->len excludes padding
1311 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1312 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1314 static int ovs_skb_network_trim(struct sk_buff
*skb
)
1319 switch (skb
->protocol
) {
1320 case htons(ETH_P_IP
):
1321 len
= ntohs(ip_hdr(skb
)->tot_len
);
1323 case htons(ETH_P_IPV6
):
1324 len
= sizeof(struct ipv6hdr
)
1325 + ntohs(ipv6_hdr(skb
)->payload_len
);
1331 err
= pskb_trim_rcsum(skb
, len
);
1338 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1339 * value if 'skb' is freed.
1341 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
1342 struct sw_flow_key
*key
,
1343 const struct ovs_conntrack_info
*info
)
1348 /* The conntrack module expects to be working at L3. */
1349 nh_ofs
= skb_network_offset(skb
);
1350 skb_pull_rcsum(skb
, nh_ofs
);
1352 err
= ovs_skb_network_trim(skb
);
1356 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
1357 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
1363 err
= ovs_ct_commit(net
, key
, info
, skb
);
1365 err
= ovs_ct_lookup(net
, key
, info
, skb
);
1367 skb_push(skb
, nh_ofs
);
1368 skb_postpush_rcsum(skb
, skb
->data
, nh_ofs
);
1374 int ovs_ct_clear(struct sk_buff
*skb
, struct sw_flow_key
*key
)
1376 if (skb_nfct(skb
)) {
1377 nf_conntrack_put(skb_nfct(skb
));
1378 #ifdef HAVE_IP_CT_UNTRACKED
1379 nf_ct_set(skb
, NULL
, IP_CT_UNTRACKED
);
1381 nf_ct_set(skb
, NULL
, 0);
1383 ovs_ct_fill_key(skb
, key
);
1389 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
1390 const struct sw_flow_key
*key
, bool log
)
1392 struct nf_conntrack_helper
*helper
;
1393 struct nf_conn_help
*help
;
1396 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
1399 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
1403 help
= nf_ct_helper_ext_add(info
->ct
, helper
, GFP_KERNEL
);
1405 nf_conntrack_helper_put(helper
);
1409 #ifdef CONFIG_NF_NAT_NEEDED
1411 ret
= nf_nat_helper_try_module_get(name
, info
->family
,
1414 nf_conntrack_helper_put(helper
);
1415 OVS_NLERR(log
, "Failed to load \"%s\" NAT helper, error: %d",
1422 rcu_assign_pointer(help
->helper
, helper
);
1423 info
->helper
= helper
;
1427 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1428 static int parse_nat(const struct nlattr
*attr
,
1429 struct ovs_conntrack_info
*info
, bool log
)
1433 bool have_ip_max
= false;
1434 bool have_proto_max
= false;
1435 bool ip_vers
= (info
->family
== NFPROTO_IPV6
);
1437 nla_for_each_nested(a
, attr
, rem
) {
1438 static const int ovs_nat_attr_lens
[OVS_NAT_ATTR_MAX
+ 1][2] = {
1439 [OVS_NAT_ATTR_SRC
] = {0, 0},
1440 [OVS_NAT_ATTR_DST
] = {0, 0},
1441 [OVS_NAT_ATTR_IP_MIN
] = {sizeof(struct in_addr
),
1442 sizeof(struct in6_addr
)},
1443 [OVS_NAT_ATTR_IP_MAX
] = {sizeof(struct in_addr
),
1444 sizeof(struct in6_addr
)},
1445 [OVS_NAT_ATTR_PROTO_MIN
] = {sizeof(u16
), sizeof(u16
)},
1446 [OVS_NAT_ATTR_PROTO_MAX
] = {sizeof(u16
), sizeof(u16
)},
1447 [OVS_NAT_ATTR_PERSISTENT
] = {0, 0},
1448 [OVS_NAT_ATTR_PROTO_HASH
] = {0, 0},
1449 [OVS_NAT_ATTR_PROTO_RANDOM
] = {0, 0},
1451 int type
= nla_type(a
);
1453 if (type
> OVS_NAT_ATTR_MAX
) {
1454 OVS_NLERR(log
, "Unknown NAT attribute (type=%d, max=%d)",
1455 type
, OVS_NAT_ATTR_MAX
);
1459 if (nla_len(a
) != ovs_nat_attr_lens
[type
][ip_vers
]) {
1460 OVS_NLERR(log
, "NAT attribute type %d has unexpected length (%d != %d)",
1462 ovs_nat_attr_lens
[type
][ip_vers
]);
1467 case OVS_NAT_ATTR_SRC
:
1468 case OVS_NAT_ATTR_DST
:
1470 OVS_NLERR(log
, "Only one type of NAT may be specified");
1473 info
->nat
|= OVS_CT_NAT
;
1474 info
->nat
|= ((type
== OVS_NAT_ATTR_SRC
)
1475 ? OVS_CT_SRC_NAT
: OVS_CT_DST_NAT
);
1478 case OVS_NAT_ATTR_IP_MIN
:
1479 nla_memcpy(&info
->range
.min_addr
, a
,
1480 sizeof(info
->range
.min_addr
));
1481 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1484 case OVS_NAT_ATTR_IP_MAX
:
1486 nla_memcpy(&info
->range
.max_addr
, a
,
1487 sizeof(info
->range
.max_addr
));
1488 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1491 case OVS_NAT_ATTR_PROTO_MIN
:
1492 info
->range
.min_proto
.all
= htons(nla_get_u16(a
));
1493 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1496 case OVS_NAT_ATTR_PROTO_MAX
:
1497 have_proto_max
= true;
1498 info
->range
.max_proto
.all
= htons(nla_get_u16(a
));
1499 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1502 case OVS_NAT_ATTR_PERSISTENT
:
1503 info
->range
.flags
|= NF_NAT_RANGE_PERSISTENT
;
1506 case OVS_NAT_ATTR_PROTO_HASH
:
1507 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1510 case OVS_NAT_ATTR_PROTO_RANDOM
:
1511 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1512 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM_FULLY
;
1514 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1515 info
->random_fully_compat
= true;
1520 OVS_NLERR(log
, "Unknown nat attribute (%d)", type
);
1526 OVS_NLERR(log
, "NAT attribute has %d unknown bytes", rem
);
1530 /* Do not allow flags if no type is given. */
1531 if (info
->range
.flags
) {
1533 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1537 info
->nat
= OVS_CT_NAT
; /* NAT existing connections. */
1538 } else if (!info
->commit
) {
1540 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1544 /* Allow missing IP_MAX. */
1545 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
&& !have_ip_max
) {
1546 memcpy(&info
->range
.max_addr
, &info
->range
.min_addr
,
1547 sizeof(info
->range
.max_addr
));
1549 /* Allow missing PROTO_MAX. */
1550 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1552 info
->range
.max_proto
.all
= info
->range
.min_proto
.all
;
1558 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
1559 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1560 [OVS_CT_ATTR_FORCE_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1561 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
1562 .maxlen
= sizeof(u16
) },
1563 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
1564 .maxlen
= sizeof(struct md_mark
) },
1565 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
1566 .maxlen
= sizeof(struct md_labels
) },
1567 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
1568 .maxlen
= NF_CT_HELPER_NAME_LEN
},
1569 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1570 /* NAT length is checked when parsing the nested attributes. */
1571 [OVS_CT_ATTR_NAT
] = { .minlen
= 0, .maxlen
= INT_MAX
},
1573 [OVS_CT_ATTR_EVENTMASK
] = { .minlen
= sizeof(u32
),
1574 .maxlen
= sizeof(u32
) },
1575 [OVS_CT_ATTR_TIMEOUT
] = { .minlen
= 1,
1576 .maxlen
= CTNL_TIMEOUT_NAME_MAX
},
1579 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
1580 const char **helper
, bool log
)
1585 nla_for_each_nested(a
, attr
, rem
) {
1586 int type
= nla_type(a
);
1590 if (type
> OVS_CT_ATTR_MAX
) {
1592 "Unknown conntrack attr (type=%d, max=%d)",
1593 type
, OVS_CT_ATTR_MAX
);
1597 maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
1598 minlen
= ovs_ct_attr_lens
[type
].minlen
;
1599 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
1601 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1602 type
, nla_len(a
), maxlen
);
1607 case OVS_CT_ATTR_FORCE_COMMIT
:
1610 case OVS_CT_ATTR_COMMIT
:
1611 info
->commit
= true;
1613 #ifdef CONFIG_NF_CONNTRACK_ZONES
1614 case OVS_CT_ATTR_ZONE
:
1615 info
->zone
.id
= nla_get_u16(a
);
1618 #ifdef CONFIG_NF_CONNTRACK_MARK
1619 case OVS_CT_ATTR_MARK
: {
1620 struct md_mark
*mark
= nla_data(a
);
1623 OVS_NLERR(log
, "ct_mark mask cannot be 0");
1630 #ifdef CONFIG_NF_CONNTRACK_LABELS
1631 case OVS_CT_ATTR_LABELS
: {
1632 struct md_labels
*labels
= nla_data(a
);
1634 if (!labels_nonzero(&labels
->mask
)) {
1635 OVS_NLERR(log
, "ct_labels mask cannot be 0");
1638 info
->labels
= *labels
;
1642 case OVS_CT_ATTR_HELPER
:
1643 *helper
= nla_data(a
);
1644 if (!memchr(*helper
, '\0', nla_len(a
))) {
1645 OVS_NLERR(log
, "Invalid conntrack helper");
1649 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1650 case OVS_CT_ATTR_NAT
: {
1651 int err
= parse_nat(a
, info
, log
);
1658 case OVS_CT_ATTR_EVENTMASK
:
1659 info
->have_eventmask
= true;
1660 info
->eventmask
= nla_get_u32(a
);
1662 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1663 case OVS_CT_ATTR_TIMEOUT
:
1664 memcpy(info
->timeout
, nla_data(a
), nla_len(a
));
1665 if (!memchr(info
->timeout
, '\0', nla_len(a
))) {
1666 OVS_NLERR(log
, "Invalid conntrack helper");
1673 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
1679 #ifdef CONFIG_NF_CONNTRACK_MARK
1680 if (!info
->commit
&& info
->mark
.mask
) {
1682 "Setting conntrack mark requires 'commit' flag.");
1686 #ifdef CONFIG_NF_CONNTRACK_LABELS
1687 if (!info
->commit
&& labels_nonzero(&info
->labels
.mask
)) {
1689 "Setting conntrack labels requires 'commit' flag.");
1694 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
1701 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
1703 if (attr
== OVS_KEY_ATTR_CT_STATE
)
1705 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1706 attr
== OVS_KEY_ATTR_CT_ZONE
)
1708 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
1709 attr
== OVS_KEY_ATTR_CT_MARK
)
1711 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1712 attr
== OVS_KEY_ATTR_CT_LABELS
) {
1713 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1715 return ovs_net
->xt_label
;
1721 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
1722 const struct sw_flow_key
*key
,
1723 struct sw_flow_actions
**sfa
, bool log
)
1725 struct ovs_conntrack_info ct_info
;
1726 const char *helper
= NULL
;
1730 family
= key_to_nfproto(key
);
1731 if (family
== NFPROTO_UNSPEC
) {
1732 OVS_NLERR(log
, "ct family unspecified");
1736 memset(&ct_info
, 0, sizeof(ct_info
));
1737 ct_info
.family
= family
;
1739 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
1740 NF_CT_DEFAULT_ZONE_DIR
, 0);
1742 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
1746 /* Set up template for tracking connections in specific zones. */
1747 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
1749 OVS_NLERR(log
, "Failed to allocate conntrack template");
1753 if (ct_info
.timeout
[0]) {
1754 if (nf_ct_set_timeout(net
, ct_info
.ct
, family
, key
->ip
.proto
,
1756 pr_info_ratelimited("Failed to associated timeout "
1757 "policy `%s'\n", ct_info
.timeout
);
1759 ct_info
.nf_ct_timeout
= rcu_dereference(
1760 nf_ct_timeout_find(ct_info
.ct
)->timeout
);
1765 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
1770 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
1771 sizeof(ct_info
), log
);
1775 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
1776 nf_conntrack_get(&ct_info
.ct
->ct_general
);
1779 __ovs_ct_free_action(&ct_info
);
1783 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1784 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info
*info
,
1785 struct sk_buff
*skb
)
1787 struct nlattr
*start
;
1789 start
= nla_nest_start_noflag(skb
, OVS_CT_ATTR_NAT
);
1793 if (info
->nat
& OVS_CT_SRC_NAT
) {
1794 if (nla_put_flag(skb
, OVS_NAT_ATTR_SRC
))
1796 } else if (info
->nat
& OVS_CT_DST_NAT
) {
1797 if (nla_put_flag(skb
, OVS_NAT_ATTR_DST
))
1803 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
) {
1804 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
1805 info
->family
== NFPROTO_IPV4
) {
1806 if (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1807 info
->range
.min_addr
.ip
) ||
1808 (info
->range
.max_addr
.ip
1809 != info
->range
.min_addr
.ip
&&
1810 (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1811 info
->range
.max_addr
.ip
))))
1813 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
1814 info
->family
== NFPROTO_IPV6
) {
1815 if (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1816 &info
->range
.min_addr
.in6
) ||
1817 (memcmp(&info
->range
.max_addr
.in6
,
1818 &info
->range
.min_addr
.in6
,
1819 sizeof(info
->range
.max_addr
.in6
)) &&
1820 (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1821 &info
->range
.max_addr
.in6
))))
1827 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1828 (nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MIN
,
1829 ntohs(info
->range
.min_proto
.all
)) ||
1830 (info
->range
.max_proto
.all
!= info
->range
.min_proto
.all
&&
1831 nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MAX
,
1832 ntohs(info
->range
.max_proto
.all
)))))
1835 if (info
->range
.flags
& NF_NAT_RANGE_PERSISTENT
&&
1836 nla_put_flag(skb
, OVS_NAT_ATTR_PERSISTENT
))
1838 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM
&&
1839 nla_put_flag(skb
, info
->random_fully_compat
1840 ? OVS_NAT_ATTR_PROTO_RANDOM
1841 : OVS_NAT_ATTR_PROTO_HASH
))
1843 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1844 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM_FULLY
&&
1845 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_RANDOM
))
1849 nla_nest_end(skb
, start
);
1855 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
1856 struct sk_buff
*skb
)
1858 struct nlattr
*start
;
1860 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_CT
);
1864 if (ct_info
->commit
&& nla_put_flag(skb
, ct_info
->force
1865 ? OVS_CT_ATTR_FORCE_COMMIT
1866 : OVS_CT_ATTR_COMMIT
))
1868 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1869 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
1871 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
1872 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
1875 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1876 labels_nonzero(&ct_info
->labels
.mask
) &&
1877 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
1880 if (ct_info
->helper
) {
1881 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
1882 ct_info
->helper
->name
))
1885 if (ct_info
->have_eventmask
&&
1886 nla_put_u32(skb
, OVS_CT_ATTR_EVENTMASK
, ct_info
->eventmask
))
1888 if (ct_info
->timeout
[0]) {
1889 if (nla_put_string(skb
, OVS_CT_ATTR_TIMEOUT
, ct_info
->timeout
))
1893 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1894 if (ct_info
->nat
&& !ovs_ct_nat_to_attr(ct_info
, skb
))
1897 nla_nest_end(skb
, start
);
1902 void ovs_ct_free_action(const struct nlattr
*a
)
1904 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
1906 __ovs_ct_free_action(ct_info
);
1909 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
)
1911 if (ct_info
->helper
) {
1912 #ifdef CONFIG_NF_NAT_NEEDED
1914 nf_nat_helper_put(ct_info
->helper
);
1916 nf_conntrack_helper_put(ct_info
->helper
);
1919 if (ct_info
->timeout
[0])
1920 nf_ct_destroy_timeout(ct_info
->ct
);
1921 nf_ct_tmpl_free(ct_info
->ct
);
1925 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1926 static int ovs_ct_limit_init(struct net
*net
, struct ovs_net
*ovs_net
)
1930 ovs_net
->ct_limit_info
= kmalloc(sizeof(*ovs_net
->ct_limit_info
),
1932 if (!ovs_net
->ct_limit_info
)
1935 ovs_net
->ct_limit_info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1936 ovs_net
->ct_limit_info
->limits
=
1937 kmalloc_array(CT_LIMIT_HASH_BUCKETS
, sizeof(struct hlist_head
),
1939 if (!ovs_net
->ct_limit_info
->limits
) {
1940 kfree(ovs_net
->ct_limit_info
);
1944 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; i
++)
1945 INIT_HLIST_HEAD(&ovs_net
->ct_limit_info
->limits
[i
]);
1947 ovs_net
->ct_limit_info
->data
=
1948 nf_conncount_init(net
, NFPROTO_INET
, sizeof(u32
));
1950 if (IS_ERR(ovs_net
->ct_limit_info
->data
)) {
1951 err
= PTR_ERR(ovs_net
->ct_limit_info
->data
);
1952 kfree(ovs_net
->ct_limit_info
->limits
);
1953 kfree(ovs_net
->ct_limit_info
);
1954 pr_err("openvswitch: failed to init nf_conncount %d\n", err
);
1960 static void ovs_ct_limit_exit(struct net
*net
, struct ovs_net
*ovs_net
)
1962 const struct ovs_ct_limit_info
*info
= ovs_net
->ct_limit_info
;
1965 nf_conncount_destroy(net
, NFPROTO_INET
, info
->data
);
1966 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
1967 struct hlist_head
*head
= &info
->limits
[i
];
1968 struct ovs_ct_limit
*ct_limit
;
1970 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
)
1971 kfree_rcu(ct_limit
, rcu
);
1973 kfree(ovs_net
->ct_limit_info
->limits
);
1974 kfree(ovs_net
->ct_limit_info
);
1977 static struct sk_buff
*
1978 ovs_ct_limit_cmd_reply_start(struct genl_info
*info
, u8 cmd
,
1979 struct ovs_header
**ovs_reply_header
)
1981 struct ovs_header
*ovs_header
= info
->userhdr
;
1982 struct sk_buff
*skb
;
1984 skb
= genlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1986 return ERR_PTR(-ENOMEM
);
1988 *ovs_reply_header
= genlmsg_put(skb
, info
->snd_portid
,
1990 &dp_ct_limit_genl_family
, 0, cmd
);
1992 if (!*ovs_reply_header
) {
1994 return ERR_PTR(-EMSGSIZE
);
1996 (*ovs_reply_header
)->dp_ifindex
= ovs_header
->dp_ifindex
;
2001 static bool check_zone_id(int zone_id
, u16
*pzone
)
2003 if (zone_id
>= 0 && zone_id
<= 65535) {
2004 *pzone
= (u16
)zone_id
;
2010 static int ovs_ct_limit_set_zone_limit(struct nlattr
*nla_zone_limit
,
2011 struct ovs_ct_limit_info
*info
)
2013 struct ovs_zone_limit
*zone_limit
;
2017 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2018 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2020 while (rem
>= sizeof(*zone_limit
)) {
2021 if (unlikely(zone_limit
->zone_id
==
2022 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2024 info
->default_limit
= zone_limit
->limit
;
2026 } else if (unlikely(!check_zone_id(
2027 zone_limit
->zone_id
, &zone
))) {
2028 OVS_NLERR(true, "zone id is out of range");
2030 struct ovs_ct_limit
*ct_limit
;
2032 ct_limit
= kmalloc(sizeof(*ct_limit
), GFP_KERNEL
);
2036 ct_limit
->zone
= zone
;
2037 ct_limit
->limit
= zone_limit
->limit
;
2040 ct_limit_set(info
, ct_limit
);
2043 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2044 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2045 NLA_ALIGN(sizeof(*zone_limit
)));
2049 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem
);
2054 static int ovs_ct_limit_del_zone_limit(struct nlattr
*nla_zone_limit
,
2055 struct ovs_ct_limit_info
*info
)
2057 struct ovs_zone_limit
*zone_limit
;
2061 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2062 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2064 while (rem
>= sizeof(*zone_limit
)) {
2065 if (unlikely(zone_limit
->zone_id
==
2066 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2068 info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
2070 } else if (unlikely(!check_zone_id(
2071 zone_limit
->zone_id
, &zone
))) {
2072 OVS_NLERR(true, "zone id is out of range");
2075 ct_limit_del(info
, zone
);
2078 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2079 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2080 NLA_ALIGN(sizeof(*zone_limit
)));
2084 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem
);
2089 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info
*info
,
2090 struct sk_buff
*reply
)
2092 struct ovs_zone_limit zone_limit
;
2095 zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2096 zone_limit
.limit
= info
->default_limit
;
2097 err
= nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2104 static int __ovs_ct_limit_get_zone_limit(struct net
*net
,
2105 struct nf_conncount_data
*data
,
2106 u16 zone_id
, u32 limit
,
2107 struct sk_buff
*reply
)
2109 struct nf_conntrack_zone ct_zone
;
2110 struct ovs_zone_limit zone_limit
;
2111 u32 conncount_key
= zone_id
;
2113 zone_limit
.zone_id
= zone_id
;
2114 zone_limit
.limit
= limit
;
2115 nf_ct_zone_init(&ct_zone
, zone_id
, NF_CT_DEFAULT_ZONE_DIR
, 0);
2117 zone_limit
.count
= nf_conncount_count(net
, data
, &conncount_key
, NULL
,
2119 return nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2122 static int ovs_ct_limit_get_zone_limit(struct net
*net
,
2123 struct nlattr
*nla_zone_limit
,
2124 struct ovs_ct_limit_info
*info
,
2125 struct sk_buff
*reply
)
2127 struct ovs_zone_limit
*zone_limit
;
2132 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2133 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2135 while (rem
>= sizeof(*zone_limit
)) {
2136 if (unlikely(zone_limit
->zone_id
==
2137 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2138 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2141 } else if (unlikely(!check_zone_id(zone_limit
->zone_id
,
2143 OVS_NLERR(true, "zone id is out of range");
2146 limit
= ct_limit_get(info
, zone
);
2149 err
= __ovs_ct_limit_get_zone_limit(
2150 net
, info
->data
, zone
, limit
, reply
);
2154 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2155 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2156 NLA_ALIGN(sizeof(*zone_limit
)));
2160 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem
);
2165 static int ovs_ct_limit_get_all_zone_limit(struct net
*net
,
2166 struct ovs_ct_limit_info
*info
,
2167 struct sk_buff
*reply
)
2169 struct ovs_ct_limit
*ct_limit
;
2170 struct hlist_head
*head
;
2173 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2178 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
2179 head
= &info
->limits
[i
];
2180 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
2181 err
= __ovs_ct_limit_get_zone_limit(net
, info
->data
,
2182 ct_limit
->zone
, ct_limit
->limit
, reply
);
2193 static int ovs_ct_limit_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2195 struct nlattr
**a
= info
->attrs
;
2196 struct sk_buff
*reply
;
2197 struct ovs_header
*ovs_reply_header
;
2198 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2199 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2202 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_SET
,
2205 return PTR_ERR(reply
);
2207 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2212 err
= ovs_ct_limit_set_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2217 static_branch_enable(&ovs_ct_limit_enabled
);
2219 genlmsg_end(reply
, ovs_reply_header
);
2220 return genlmsg_reply(reply
, info
);
2227 static int ovs_ct_limit_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2229 struct nlattr
**a
= info
->attrs
;
2230 struct sk_buff
*reply
;
2231 struct ovs_header
*ovs_reply_header
;
2232 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2233 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2236 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_DEL
,
2239 return PTR_ERR(reply
);
2241 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2246 err
= ovs_ct_limit_del_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2251 genlmsg_end(reply
, ovs_reply_header
);
2252 return genlmsg_reply(reply
, info
);
2259 static int ovs_ct_limit_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2261 struct nlattr
**a
= info
->attrs
;
2262 struct nlattr
*nla_reply
;
2263 struct sk_buff
*reply
;
2264 struct ovs_header
*ovs_reply_header
;
2265 struct net
*net
= sock_net(skb
->sk
);
2266 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2267 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2270 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_GET
,
2273 return PTR_ERR(reply
);
2275 nla_reply
= nla_nest_start_noflag(reply
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2277 if (a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2278 err
= ovs_ct_limit_get_zone_limit(
2279 net
, a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
], ct_limit_info
,
2284 err
= ovs_ct_limit_get_all_zone_limit(net
, ct_limit_info
,
2290 nla_nest_end(reply
, nla_reply
);
2291 genlmsg_end(reply
, ovs_reply_header
);
2292 return genlmsg_reply(reply
, info
);
2299 static struct genl_ops ct_limit_genl_ops
[] = {
2300 { .cmd
= OVS_CT_LIMIT_CMD_SET
,
2301 #ifdef HAVE_GENL_VALIDATE_FLAGS
2302 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2304 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2306 .policy
= ct_limit_policy
,
2307 .doit
= ovs_ct_limit_cmd_set
,
2309 { .cmd
= OVS_CT_LIMIT_CMD_DEL
,
2310 #ifdef HAVE_GENL_VALIDATE_FLAGS
2311 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2313 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2315 .policy
= ct_limit_policy
,
2316 .doit
= ovs_ct_limit_cmd_del
,
2318 { .cmd
= OVS_CT_LIMIT_CMD_GET
,
2319 #ifdef HAVE_GENL_VALIDATE_FLAGS
2320 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2322 .flags
= 0, /* OK for unprivileged users. */
2323 .policy
= ct_limit_policy
,
2324 .doit
= ovs_ct_limit_cmd_get
,
2328 static const struct genl_multicast_group ovs_ct_limit_multicast_group
= {
2329 .name
= OVS_CT_LIMIT_MCGROUP
,
2332 struct genl_family dp_ct_limit_genl_family __ro_after_init
= {
2333 .hdrsize
= sizeof(struct ovs_header
),
2334 .name
= OVS_CT_LIMIT_FAMILY
,
2335 .version
= OVS_CT_LIMIT_VERSION
,
2336 .maxattr
= OVS_CT_LIMIT_ATTR_MAX
,
2338 .parallel_ops
= true,
2339 .ops
= ct_limit_genl_ops
,
2340 .n_ops
= ARRAY_SIZE(ct_limit_genl_ops
),
2341 .mcgrps
= &ovs_ct_limit_multicast_group
,
2343 .module
= THIS_MODULE
,
2347 int ovs_ct_init(struct net
*net
)
2349 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
2350 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2352 if (nf_connlabels_get(net
, n_bits
- 1)) {
2353 ovs_net
->xt_label
= false;
2354 OVS_NLERR(true, "Failed to set connlabel length");
2356 ovs_net
->xt_label
= true;
2359 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2360 return ovs_ct_limit_init(net
, ovs_net
);
2366 void ovs_ct_exit(struct net
*net
)
2368 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2370 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2371 ovs_ct_limit_exit(net
, ovs_net
);
2374 if (ovs_net
->xt_label
)
2375 nf_connlabels_put(net
);
2378 #endif /* CONFIG_NF_CONNTRACK */