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_zones.h>
33 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
35 #ifdef CONFIG_NF_NAT_NEEDED
36 #include <linux/netfilter/nf_nat.h>
37 #include <net/netfilter/nf_nat_core.h>
38 #include <net/netfilter/nf_nat_l3proto.h>
42 #include "conntrack.h"
44 #include "flow_netlink.h"
47 #ifndef HAVE_NF_NAT_RANGE2
48 #define nf_nat_range2 nf_nat_range
51 struct ovs_ct_len_tbl
{
56 /* Metadata mark for masked write to conntrack mark */
62 /* Metadata label for masked write to conntrack label. */
64 struct ovs_key_ct_labels value
;
65 struct ovs_key_ct_labels mask
;
69 OVS_CT_NAT
= 1 << 0, /* NAT for committed connections only. */
70 OVS_CT_SRC_NAT
= 1 << 1, /* Source NAT for NEW connections. */
71 OVS_CT_DST_NAT
= 1 << 2, /* Destination NAT for NEW connections. */
74 /* Conntrack action context for execution. */
75 struct ovs_conntrack_info
{
76 struct nf_conntrack_helper
*helper
;
77 struct nf_conntrack_zone zone
;
80 u8 nat
: 3; /* enum ovs_ct_nat */
81 u8 random_fully_compat
: 1; /* bool */
83 u8 have_eventmask
: 1;
85 u32 eventmask
; /* Mask of 1 << IPCT_*. */
87 struct md_labels labels
;
88 #ifdef CONFIG_NF_NAT_NEEDED
89 struct nf_nat_range2 range
; /* Only present for SRC NAT and DST NAT. */
93 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
94 #define OVS_CT_LIMIT_UNLIMITED 0
95 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
96 #define CT_LIMIT_HASH_BUCKETS 512
97 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled
);
100 /* Elements in ovs_ct_limit_info->limits hash table */
101 struct hlist_node hlist_node
;
107 struct ovs_ct_limit_info
{
109 struct hlist_head
*limits
;
110 struct nf_conncount_data
*data
;
113 static const struct nla_policy ct_limit_policy
[OVS_CT_LIMIT_ATTR_MAX
+ 1] = {
114 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NLA_NESTED
, },
118 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
);
120 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
);
122 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
124 switch (ntohs(key
->eth
.type
)) {
130 return NFPROTO_UNSPEC
;
134 /* Map SKB connection state into the values used by flow definition. */
135 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
137 u8 ct_state
= OVS_CS_F_TRACKED
;
140 case IP_CT_ESTABLISHED_REPLY
:
141 case IP_CT_RELATED_REPLY
:
142 ct_state
|= OVS_CS_F_REPLY_DIR
;
149 case IP_CT_ESTABLISHED
:
150 case IP_CT_ESTABLISHED_REPLY
:
151 ct_state
|= OVS_CS_F_ESTABLISHED
;
154 case IP_CT_RELATED_REPLY
:
155 ct_state
|= OVS_CS_F_RELATED
;
158 ct_state
|= OVS_CS_F_NEW
;
167 static u32
ovs_ct_get_mark(const struct nf_conn
*ct
)
169 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
170 return ct
? ct
->mark
: 0;
176 /* Guard against conntrack labels max size shrinking below 128 bits. */
177 #if NF_CT_LABELS_MAX_SIZE < 16
178 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
181 static void ovs_ct_get_labels(const struct nf_conn
*ct
,
182 struct ovs_key_ct_labels
*labels
)
184 struct nf_conn_labels
*cl
= ct
? nf_ct_labels_find(ct
) : NULL
;
187 memcpy(labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
189 memset(labels
, 0, OVS_CT_LABELS_LEN
);
192 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key
*key
,
193 const struct nf_conntrack_tuple
*orig
,
196 key
->ct_orig_proto
= orig
->dst
.protonum
;
197 if (orig
->dst
.protonum
== icmp_proto
) {
198 key
->ct
.orig_tp
.src
= htons(orig
->dst
.u
.icmp
.type
);
199 key
->ct
.orig_tp
.dst
= htons(orig
->dst
.u
.icmp
.code
);
201 key
->ct
.orig_tp
.src
= orig
->src
.u
.all
;
202 key
->ct
.orig_tp
.dst
= orig
->dst
.u
.all
;
206 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
207 const struct nf_conntrack_zone
*zone
,
208 const struct nf_conn
*ct
)
210 key
->ct_state
= state
;
211 key
->ct_zone
= zone
->id
;
212 key
->ct
.mark
= ovs_ct_get_mark(ct
);
213 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
216 const struct nf_conntrack_tuple
*orig
;
218 /* Use the master if we have one. */
221 orig
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
223 /* IP version must match with the master connection. */
224 if (key
->eth
.type
== htons(ETH_P_IP
) &&
225 nf_ct_l3num(ct
) == NFPROTO_IPV4
) {
226 key
->ipv4
.ct_orig
.src
= orig
->src
.u3
.ip
;
227 key
->ipv4
.ct_orig
.dst
= orig
->dst
.u3
.ip
;
228 __ovs_ct_update_key_orig_tp(key
, orig
, IPPROTO_ICMP
);
230 } else if (key
->eth
.type
== htons(ETH_P_IPV6
) &&
231 !sw_flow_key_is_nd(key
) &&
232 nf_ct_l3num(ct
) == NFPROTO_IPV6
) {
233 key
->ipv6
.ct_orig
.src
= orig
->src
.u3
.in6
;
234 key
->ipv6
.ct_orig
.dst
= orig
->dst
.u3
.in6
;
235 __ovs_ct_update_key_orig_tp(key
, orig
, NEXTHDR_ICMP
);
239 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
240 * original direction key fields.
242 key
->ct_orig_proto
= 0;
245 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
246 * previously sent the packet to conntrack via the ct action. If
247 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
248 * initialized from the connection status.
250 static void ovs_ct_update_key(const struct sk_buff
*skb
,
251 const struct ovs_conntrack_info
*info
,
252 struct sw_flow_key
*key
, bool post_ct
,
255 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
256 enum ip_conntrack_info ctinfo
;
260 ct
= nf_ct_get(skb
, &ctinfo
);
262 state
= ovs_ct_get_state(ctinfo
);
263 /* All unconfirmed entries are NEW connections. */
264 if (!nf_ct_is_confirmed(ct
))
265 state
|= OVS_CS_F_NEW
;
266 /* OVS persists the related flag for the duration of the
270 state
|= OVS_CS_F_RELATED
;
271 if (keep_nat_flags
) {
272 state
|= key
->ct_state
& OVS_CS_F_NAT_MASK
;
274 if (ct
->status
& IPS_SRC_NAT
)
275 state
|= OVS_CS_F_SRC_NAT
;
276 if (ct
->status
& IPS_DST_NAT
)
277 state
|= OVS_CS_F_DST_NAT
;
279 zone
= nf_ct_zone(ct
);
280 } else if (post_ct
) {
281 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
285 __ovs_ct_update_key(key
, state
, zone
, ct
);
288 /* This is called to initialize CT key fields possibly coming in from the local
291 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
293 ovs_ct_update_key(skb
, NULL
, key
, false, false);
296 #define IN6_ADDR_INITIALIZER(ADDR) \
297 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
298 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
300 int ovs_ct_put_key(const struct sw_flow_key
*swkey
,
301 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
303 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, output
->ct_state
))
306 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
307 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, output
->ct_zone
))
310 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
311 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, output
->ct
.mark
))
314 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
315 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(output
->ct
.labels
),
319 if (swkey
->ct_orig_proto
) {
320 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
321 struct ovs_key_ct_tuple_ipv4 orig
= {
322 output
->ipv4
.ct_orig
.src
,
323 output
->ipv4
.ct_orig
.dst
,
324 output
->ct
.orig_tp
.src
,
325 output
->ct
.orig_tp
.dst
,
326 output
->ct_orig_proto
,
328 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
329 sizeof(orig
), &orig
))
331 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
332 struct ovs_key_ct_tuple_ipv6 orig
= {
333 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.src
),
334 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.dst
),
335 output
->ct
.orig_tp
.src
,
336 output
->ct
.orig_tp
.dst
,
337 output
->ct_orig_proto
,
339 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
340 sizeof(orig
), &orig
))
348 static int ovs_ct_set_mark(struct nf_conn
*ct
, struct sw_flow_key
*key
,
349 u32 ct_mark
, u32 mask
)
351 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
354 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
355 if (ct
->mark
!= new_mark
) {
357 if (nf_ct_is_confirmed(ct
))
358 nf_conntrack_event_cache(IPCT_MARK
, ct
);
359 key
->ct
.mark
= new_mark
;
368 static struct nf_conn_labels
*ovs_ct_get_conn_labels(struct nf_conn
*ct
)
370 struct nf_conn_labels
*cl
;
372 cl
= nf_ct_labels_find(ct
);
374 nf_ct_labels_ext_add(ct
);
375 cl
= nf_ct_labels_find(ct
);
381 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
382 * since the new connection is not yet confirmed, and thus no-one else has
383 * access to it's labels, we simply write them over.
385 static int ovs_ct_init_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
386 const struct ovs_key_ct_labels
*labels
,
387 const struct ovs_key_ct_labels
*mask
)
389 struct nf_conn_labels
*cl
, *master_cl
;
390 bool have_mask
= labels_nonzero(mask
);
392 /* Inherit master's labels to the related connection? */
393 master_cl
= ct
->master
? nf_ct_labels_find(ct
->master
) : NULL
;
395 if (!master_cl
&& !have_mask
)
396 return 0; /* Nothing to do. */
398 cl
= ovs_ct_get_conn_labels(ct
);
402 /* Inherit the master's labels, if any. Must use memcpy for backport
403 * as struct assignment only copies the length field in older
407 memcpy(cl
->bits
, master_cl
->bits
, OVS_CT_LABELS_LEN
);
410 u32
*dst
= (u32
*)cl
->bits
;
413 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
414 dst
[i
] = (dst
[i
] & ~mask
->ct_labels_32
[i
]) |
415 (labels
->ct_labels_32
[i
]
416 & mask
->ct_labels_32
[i
]);
419 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
420 * IPCT_LABEL bit is set in the event cache.
422 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
424 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
429 static int ovs_ct_set_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
430 const struct ovs_key_ct_labels
*labels
,
431 const struct ovs_key_ct_labels
*mask
)
433 struct nf_conn_labels
*cl
;
436 cl
= ovs_ct_get_conn_labels(ct
);
440 err
= nf_connlabels_replace(ct
, labels
->ct_labels_32
,
442 OVS_CT_LABELS_LEN_32
);
446 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
451 /* 'skb' should already be pulled to nh_ofs. */
452 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
454 const struct nf_conntrack_helper
*helper
;
455 const struct nf_conn_help
*help
;
456 enum ip_conntrack_info ctinfo
;
457 unsigned int protoff
;
462 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
463 bool dst_set
= false;
464 struct rtable rt
= { .rt_flags
= 0 };
467 ct
= nf_ct_get(skb
, &ctinfo
);
468 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
471 help
= nfct_help(ct
);
475 helper
= rcu_dereference(help
->helper
);
481 protoff
= ip_hdrlen(skb
);
487 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
488 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
490 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
491 pr_debug("proto header not found\n");
498 WARN_ONCE(1, "helper invoked on non-IP family!");
502 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
503 /* Linux 4.5 and older depend on skb_dst being set when recalculating
504 * checksums after NAT helper has mangled TCP or UDP packet payload.
505 * skb_dst is cast to a rtable struct and the flags examined.
506 * Forcing these flags to have RTCF_LOCAL not set ensures checksum mod
507 * is carried out in the same way as kernel versions > 4.5
509 if (ct
->status
& IPS_NAT_MASK
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
512 skb_dst_set(skb
, &rt
.dst
);
515 err
= helper
->help(skb
, protoff
, ct
, ctinfo
);
516 if (err
!= NF_ACCEPT
)
519 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
521 skb_dst_set(skb
, NULL
);
524 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
525 * FTP with NAT) adusting the TCP payload size when mangling IP
526 * addresses and/or port numbers in the text-based control connection.
528 if (test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
) &&
529 !nf_ct_seq_adjust(skb
, ct
, ctinfo
, protoff
))
534 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
535 * value if 'skb' is freed.
537 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
538 u16 zone
, struct sk_buff
*skb
)
540 struct ovs_gso_cb ovs_cb
= *OVS_GSO_CB(skb
);
543 if (key
->eth
.type
== htons(ETH_P_IP
)) {
544 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
546 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
547 err
= ip_defrag(net
, skb
, user
);
551 ovs_cb
.dp_cb
.mru
= IPCB(skb
)->frag_max_size
;
552 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
553 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
554 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
556 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
557 err
= nf_ct_frag6_gather(net
, skb
, user
);
559 if (err
!= -EINPROGRESS
)
564 key
->ip
.proto
= ipv6_hdr(skb
)->nexthdr
;
565 ovs_cb
.dp_cb
.mru
= IP6CB(skb
)->frag_max_size
;
566 #endif /* IP frag support */
569 return -EPFNOSUPPORT
;
572 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
575 *OVS_GSO_CB(skb
) = ovs_cb
;
580 static struct nf_conntrack_expect
*
581 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
582 u16 proto
, const struct sk_buff
*skb
)
584 struct nf_conntrack_tuple tuple
;
585 struct nf_conntrack_expect
*exp
;
587 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
590 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
592 struct nf_conntrack_tuple_hash
*h
;
594 /* Delete existing conntrack entry, if it clashes with the
595 * expectation. This can happen since conntrack ALGs do not
596 * check for clashes between (new) expectations and existing
597 * conntrack entries. nf_conntrack_in() will check the
598 * expectations only if a conntrack entry can not be found,
599 * which can lead to OVS finding the expectation (here) in the
600 * init direction, but which will not be removed by the
601 * nf_conntrack_in() call, if a matching conntrack entry is
602 * found instead. In this case all init direction packets
603 * would be reported as new related packets, while reply
604 * direction packets would be reported as un-related
605 * established packets.
607 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
609 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
611 nf_ct_delete(ct
, 0, 0);
612 nf_conntrack_put(&ct
->ct_general
);
619 /* This replicates logic from nf_conntrack_core.c that is not exported. */
620 static enum ip_conntrack_info
621 ovs_ct_get_info(const struct nf_conntrack_tuple_hash
*h
)
623 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
625 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
626 return IP_CT_ESTABLISHED_REPLY
;
627 /* Once we've had two way comms, always ESTABLISHED. */
628 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
629 return IP_CT_ESTABLISHED
;
630 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
631 return IP_CT_RELATED
;
635 /* Find an existing connection which this packet belongs to without
636 * re-attributing statistics or modifying the connection state. This allows an
637 * skb->_nfct lost due to an upcall to be recovered during actions execution.
639 * Must be called with rcu_read_lock.
641 * On success, populates skb->_nfct and returns the connection. Returns NULL
642 * if there is no existing entry.
644 static struct nf_conn
*
645 ovs_ct_find_existing(struct net
*net
, const struct nf_conntrack_zone
*zone
,
646 u8 l3num
, struct sk_buff
*skb
, bool natted
)
648 const struct nf_conntrack_l3proto
*l3proto
;
649 const struct nf_conntrack_l4proto
*l4proto
;
650 struct nf_conntrack_tuple tuple
;
651 struct nf_conntrack_tuple_hash
*h
;
653 unsigned int dataoff
;
656 l3proto
= __nf_ct_l3proto_find(l3num
);
657 if (l3proto
->get_l4proto(skb
, skb_network_offset(skb
), &dataoff
,
659 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
662 l4proto
= __nf_ct_l4proto_find(l3num
, protonum
);
663 if (!nf_ct_get_tuple(skb
, skb_network_offset(skb
), dataoff
, l3num
,
664 protonum
, net
, &tuple
, l3proto
, l4proto
)) {
665 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
669 /* Must invert the tuple if skb has been transformed by NAT. */
671 struct nf_conntrack_tuple inverse
;
673 if (!nf_ct_invert_tuple(&inverse
, &tuple
, l3proto
, l4proto
)) {
674 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
680 /* look for tuple match */
681 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
683 return NULL
; /* Not found. */
685 ct
= nf_ct_tuplehash_to_ctrack(h
);
687 /* Inverted packet tuple matches the reverse direction conntrack tuple,
688 * select the other tuplehash to get the right 'ctinfo' bits for this
692 h
= &ct
->tuplehash
[!h
->tuple
.dst
.dir
];
694 nf_ct_set(skb
, ct
, ovs_ct_get_info(h
));
699 struct nf_conn
*ovs_ct_executed(struct net
*net
,
700 const struct sw_flow_key
*key
,
701 const struct ovs_conntrack_info
*info
,
705 struct nf_conn
*ct
= NULL
;
707 /* If no ct, check if we have evidence that an existing conntrack entry
708 * might be found for this skb. This happens when we lose a skb->_nfct
709 * due to an upcall, or if the direction is being forced. If the
710 * connection was not confirmed, it is not cached and needs to be run
711 * through conntrack again.
713 *ct_executed
= (key
->ct_state
& OVS_CS_F_TRACKED
) &&
714 !(key
->ct_state
& OVS_CS_F_INVALID
) &&
715 (key
->ct_zone
== info
->zone
.id
);
717 if (*ct_executed
|| (!key
->ct_state
&& info
->force
)) {
718 ct
= ovs_ct_find_existing(net
, &info
->zone
, info
->family
, skb
,
726 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
727 static bool skb_nfct_cached(struct net
*net
,
728 const struct sw_flow_key
*key
,
729 const struct ovs_conntrack_info
*info
,
732 enum ip_conntrack_info ctinfo
;
734 bool ct_executed
= true;
736 ct
= nf_ct_get(skb
, &ctinfo
);
738 ct
= ovs_ct_executed(net
, key
, info
, skb
, &ct_executed
);
741 nf_ct_get(skb
, &ctinfo
);
745 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
747 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
750 struct nf_conn_help
*help
;
752 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
753 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
756 /* Force conntrack entry direction to the current packet? */
757 if (info
->force
&& CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
) {
758 /* Delete the conntrack entry if confirmed, else just release
761 if (nf_ct_is_confirmed(ct
))
762 nf_ct_delete(ct
, 0, 0);
764 nf_conntrack_put(&ct
->ct_general
);
765 nf_ct_set(skb
, NULL
, 0);
772 #ifdef CONFIG_NF_NAT_NEEDED
773 /* Modelled after nf_nat_ipv[46]_fn().
774 * range is only used for new, uninitialized NAT state.
775 * Returns either NF_ACCEPT or NF_DROP.
777 static int ovs_ct_nat_execute(struct sk_buff
*skb
, struct nf_conn
*ct
,
778 enum ip_conntrack_info ctinfo
,
779 const struct nf_nat_range2
*range
,
780 enum nf_nat_manip_type maniptype
)
782 int hooknum
, nh_off
, err
= NF_ACCEPT
;
784 nh_off
= skb_network_offset(skb
);
785 skb_pull_rcsum(skb
, nh_off
);
787 /* See HOOK2MANIP(). */
788 if (maniptype
== NF_NAT_MANIP_SRC
)
789 hooknum
= NF_INET_LOCAL_IN
; /* Source NAT */
791 hooknum
= NF_INET_LOCAL_OUT
; /* Destination NAT */
795 case IP_CT_RELATED_REPLY
:
796 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
797 skb
->protocol
== htons(ETH_P_IP
) &&
798 ip_hdr(skb
)->protocol
== IPPROTO_ICMP
) {
799 if (!nf_nat_icmp_reply_translation(skb
, ct
, ctinfo
,
803 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
804 skb
->protocol
== htons(ETH_P_IPV6
)) {
806 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
807 int hdrlen
= ipv6_skip_exthdr(skb
,
808 sizeof(struct ipv6hdr
),
809 &nexthdr
, &frag_off
);
811 if (hdrlen
>= 0 && nexthdr
== IPPROTO_ICMPV6
) {
812 if (!nf_nat_icmpv6_reply_translation(skb
, ct
,
820 /* Non-ICMP, fall thru to initialize if needed. */
822 /* Seen it before? This can happen for loopback, retrans,
825 if (!nf_nat_initialized(ct
, maniptype
)) {
826 /* Initialize according to the NAT action. */
827 err
= (range
&& range
->flags
& NF_NAT_RANGE_MAP_IPS
)
828 /* Action is set up to establish a new
831 ? nf_nat_setup_info(ct
, range
, maniptype
)
832 : nf_nat_alloc_null_binding(ct
, hooknum
);
833 if (err
!= NF_ACCEPT
)
838 case IP_CT_ESTABLISHED
:
839 case IP_CT_ESTABLISHED_REPLY
:
847 err
= nf_nat_packet(ct
, ctinfo
, hooknum
, skb
);
849 skb_push(skb
, nh_off
);
850 skb_postpush_rcsum(skb
, skb
->data
, nh_off
);
855 static void ovs_nat_update_key(struct sw_flow_key
*key
,
856 const struct sk_buff
*skb
,
857 enum nf_nat_manip_type maniptype
)
859 if (maniptype
== NF_NAT_MANIP_SRC
) {
862 key
->ct_state
|= OVS_CS_F_SRC_NAT
;
863 if (key
->eth
.type
== htons(ETH_P_IP
))
864 key
->ipv4
.addr
.src
= ip_hdr(skb
)->saddr
;
865 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
866 memcpy(&key
->ipv6
.addr
.src
, &ipv6_hdr(skb
)->saddr
,
867 sizeof(key
->ipv6
.addr
.src
));
871 if (key
->ip
.proto
== IPPROTO_UDP
)
872 src
= udp_hdr(skb
)->source
;
873 else if (key
->ip
.proto
== IPPROTO_TCP
)
874 src
= tcp_hdr(skb
)->source
;
875 else if (key
->ip
.proto
== IPPROTO_SCTP
)
876 src
= sctp_hdr(skb
)->source
;
884 key
->ct_state
|= OVS_CS_F_DST_NAT
;
885 if (key
->eth
.type
== htons(ETH_P_IP
))
886 key
->ipv4
.addr
.dst
= ip_hdr(skb
)->daddr
;
887 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
888 memcpy(&key
->ipv6
.addr
.dst
, &ipv6_hdr(skb
)->daddr
,
889 sizeof(key
->ipv6
.addr
.dst
));
893 if (key
->ip
.proto
== IPPROTO_UDP
)
894 dst
= udp_hdr(skb
)->dest
;
895 else if (key
->ip
.proto
== IPPROTO_TCP
)
896 dst
= tcp_hdr(skb
)->dest
;
897 else if (key
->ip
.proto
== IPPROTO_SCTP
)
898 dst
= sctp_hdr(skb
)->dest
;
906 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
907 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
908 const struct ovs_conntrack_info
*info
,
909 struct sk_buff
*skb
, struct nf_conn
*ct
,
910 enum ip_conntrack_info ctinfo
)
912 enum nf_nat_manip_type maniptype
;
915 #ifdef HAVE_NF_CT_IS_UNTRACKED
916 if (nf_ct_is_untracked(ct
)) {
917 /* A NAT action may only be performed on tracked packets. */
920 #endif /* HAVE_NF_CT_IS_UNTRACKED */
922 /* Add NAT extension if not confirmed yet. */
923 if (!nf_ct_is_confirmed(ct
) && !nf_ct_nat_ext_add(ct
))
924 return NF_ACCEPT
; /* Can't NAT. */
926 /* Determine NAT type.
927 * Check if the NAT type can be deduced from the tracked connection.
928 * Make sure new expected connections (IP_CT_RELATED) are NATted only
931 if (info
->nat
& OVS_CT_NAT
&& ctinfo
!= IP_CT_NEW
&&
932 ct
->status
& IPS_NAT_MASK
&&
933 (ctinfo
!= IP_CT_RELATED
|| info
->commit
)) {
934 /* NAT an established or related connection like before. */
935 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
)
936 /* This is the REPLY direction for a connection
937 * for which NAT was applied in the forward
938 * direction. Do the reverse NAT.
940 maniptype
= ct
->status
& IPS_SRC_NAT
941 ? NF_NAT_MANIP_DST
: NF_NAT_MANIP_SRC
;
943 maniptype
= ct
->status
& IPS_SRC_NAT
944 ? NF_NAT_MANIP_SRC
: NF_NAT_MANIP_DST
;
945 } else if (info
->nat
& OVS_CT_SRC_NAT
) {
946 maniptype
= NF_NAT_MANIP_SRC
;
947 } else if (info
->nat
& OVS_CT_DST_NAT
) {
948 maniptype
= NF_NAT_MANIP_DST
;
950 return NF_ACCEPT
; /* Connection is not NATed. */
952 err
= ovs_ct_nat_execute(skb
, ct
, ctinfo
, &info
->range
, maniptype
);
954 /* Mark NAT done if successful and update the flow key. */
955 if (err
== NF_ACCEPT
)
956 ovs_nat_update_key(key
, skb
, maniptype
);
960 #else /* !CONFIG_NF_NAT_NEEDED */
961 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
962 const struct ovs_conntrack_info
*info
,
963 struct sk_buff
*skb
, struct nf_conn
*ct
,
964 enum ip_conntrack_info ctinfo
)
970 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
971 * not done already. Update key with new CT state after passing the packet
973 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
974 * set to NULL and 0 will be returned.
976 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
977 const struct ovs_conntrack_info
*info
,
980 /* If we are recirculating packets to match on conntrack fields and
981 * committing with a separate conntrack action, then we don't need to
982 * actually run the packet through conntrack twice unless it's for a
985 bool cached
= skb_nfct_cached(net
, key
, info
, skb
);
986 enum ip_conntrack_info ctinfo
;
990 struct nf_hook_state state
= {
991 .hook
= NF_INET_PRE_ROUTING
,
995 struct nf_conn
*tmpl
= info
->ct
;
998 /* Associate skb with specified zone. */
1001 nf_conntrack_put(skb_nfct(skb
));
1002 nf_conntrack_get(&tmpl
->ct_general
);
1003 nf_ct_set(skb
, tmpl
, IP_CT_NEW
);
1006 err
= nf_conntrack_in(skb
, &state
);
1007 if (err
!= NF_ACCEPT
)
1010 /* Clear CT state NAT flags to mark that we have not yet done
1011 * NAT after the nf_conntrack_in() call. We can actually clear
1012 * the whole state, as it will be re-initialized below.
1016 /* Update the key, but keep the NAT flags. */
1017 ovs_ct_update_key(skb
, info
, key
, true, true);
1020 ct
= nf_ct_get(skb
, &ctinfo
);
1022 /* Packets starting a new connection must be NATted before the
1023 * helper, so that the helper knows about the NAT. We enforce
1024 * this by delaying both NAT and helper calls for unconfirmed
1025 * connections until the committing CT action. For later
1026 * packets NAT and Helper may be called in either order.
1028 * NAT will be done only if the CT action has NAT, and only
1029 * once per packet (per zone), as guarded by the NAT bits in
1030 * the key->ct_state.
1032 if (info
->nat
&& !(key
->ct_state
& OVS_CS_F_NAT_MASK
) &&
1033 (nf_ct_is_confirmed(ct
) || info
->commit
) &&
1034 ovs_ct_nat(net
, key
, info
, skb
, ct
, ctinfo
) != NF_ACCEPT
) {
1038 /* Userspace may decide to perform a ct lookup without a helper
1039 * specified followed by a (recirculate and) commit with one.
1040 * Therefore, for unconfirmed connections which we will commit,
1041 * we need to attach the helper here.
1043 if (!nf_ct_is_confirmed(ct
) && info
->commit
&&
1044 info
->helper
&& !nfct_help(ct
)) {
1045 int err
= __nf_ct_try_assign_helper(ct
, info
->ct
,
1051 /* Call the helper only if:
1052 * - nf_conntrack_in() was executed above ("!cached") for a
1053 * confirmed connection, or
1054 * - When committing an unconfirmed connection.
1056 if ((nf_ct_is_confirmed(ct
) ? !cached
: info
->commit
) &&
1057 ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
1065 /* Lookup connection and read fields into key. */
1066 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
1067 const struct ovs_conntrack_info
*info
,
1068 struct sk_buff
*skb
)
1070 struct nf_conntrack_expect
*exp
;
1072 /* If we pass an expected packet through nf_conntrack_in() the
1073 * expectation is typically removed, but the packet could still be
1074 * lost in upcall processing. To prevent this from happening we
1075 * perform an explicit expectation lookup. Expected connections are
1076 * always new, and will be passed through conntrack only when they are
1077 * committed, as it is OK to remove the expectation at that time.
1079 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
1083 /* NOTE: New connections are NATted and Helped only when
1084 * committed, so we are not calling into NAT here.
1086 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
1087 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
1092 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1096 ct
= (struct nf_conn
*)skb_nfct(skb
);
1098 nf_ct_deliver_cached_events(ct
);
1104 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
1108 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
1109 if (labels
->ct_labels_32
[i
])
1115 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1116 static struct hlist_head
*ct_limit_hash_bucket(
1117 const struct ovs_ct_limit_info
*info
, u16 zone
)
1119 return &info
->limits
[zone
& (CT_LIMIT_HASH_BUCKETS
- 1)];
1122 /* Call with ovs_mutex */
1123 static void ct_limit_set(const struct ovs_ct_limit_info
*info
,
1124 struct ovs_ct_limit
*new_ct_limit
)
1126 struct ovs_ct_limit
*ct_limit
;
1127 struct hlist_head
*head
;
1129 head
= ct_limit_hash_bucket(info
, new_ct_limit
->zone
);
1130 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1131 if (ct_limit
->zone
== new_ct_limit
->zone
) {
1132 hlist_replace_rcu(&ct_limit
->hlist_node
,
1133 &new_ct_limit
->hlist_node
);
1134 kfree_rcu(ct_limit
, rcu
);
1139 hlist_add_head_rcu(&new_ct_limit
->hlist_node
, head
);
1142 /* Call with ovs_mutex */
1143 static void ct_limit_del(const struct ovs_ct_limit_info
*info
, u16 zone
)
1145 struct ovs_ct_limit
*ct_limit
;
1146 struct hlist_head
*head
;
1147 struct hlist_node
*n
;
1149 head
= ct_limit_hash_bucket(info
, zone
);
1150 hlist_for_each_entry_safe(ct_limit
, n
, head
, hlist_node
) {
1151 if (ct_limit
->zone
== zone
) {
1152 hlist_del_rcu(&ct_limit
->hlist_node
);
1153 kfree_rcu(ct_limit
, rcu
);
1159 /* Call with RCU read lock */
1160 static u32
ct_limit_get(const struct ovs_ct_limit_info
*info
, u16 zone
)
1162 struct ovs_ct_limit
*ct_limit
;
1163 struct hlist_head
*head
;
1165 head
= ct_limit_hash_bucket(info
, zone
);
1166 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1167 if (ct_limit
->zone
== zone
)
1168 return ct_limit
->limit
;
1171 return info
->default_limit
;
1174 static int ovs_ct_check_limit(struct net
*net
,
1175 const struct ovs_conntrack_info
*info
,
1176 const struct nf_conntrack_tuple
*tuple
)
1178 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1179 const struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
1180 u32 per_zone_limit
, connections
;
1183 conncount_key
= info
->zone
.id
;
1185 per_zone_limit
= ct_limit_get(ct_limit_info
, info
->zone
.id
);
1186 if (per_zone_limit
== OVS_CT_LIMIT_UNLIMITED
)
1189 connections
= nf_conncount_count(net
, ct_limit_info
->data
,
1190 &conncount_key
, tuple
, &info
->zone
);
1191 if (connections
> per_zone_limit
)
1198 /* Lookup connection and confirm if unconfirmed. */
1199 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
1200 const struct ovs_conntrack_info
*info
,
1201 struct sk_buff
*skb
)
1203 enum ip_conntrack_info ctinfo
;
1207 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1211 /* The connection could be invalid, in which case this is a no-op.*/
1212 ct
= nf_ct_get(skb
, &ctinfo
);
1216 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1217 if (static_branch_unlikely(&ovs_ct_limit_enabled
)) {
1218 if (!nf_ct_is_confirmed(ct
)) {
1219 err
= ovs_ct_check_limit(net
, info
,
1220 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
1222 net_warn_ratelimited("openvswitch: zone: %u "
1223 "exceeds conntrack limit\n",
1231 /* Set the conntrack event mask if given. NEW and DELETE events have
1232 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1233 * typically would receive many kinds of updates. Setting the event
1234 * mask allows those events to be filtered. The set event mask will
1235 * remain in effect for the lifetime of the connection unless changed
1236 * by a further CT action with both the commit flag and the eventmask
1238 if (info
->have_eventmask
) {
1239 struct nf_conntrack_ecache
*cache
= nf_ct_ecache_find(ct
);
1242 cache
->ctmask
= info
->eventmask
;
1245 /* Apply changes before confirming the connection so that the initial
1246 * conntrack NEW netlink event carries the values given in the CT
1249 if (info
->mark
.mask
) {
1250 err
= ovs_ct_set_mark(ct
, key
, info
->mark
.value
,
1255 if (!nf_ct_is_confirmed(ct
)) {
1256 err
= ovs_ct_init_labels(ct
, key
, &info
->labels
.value
,
1257 &info
->labels
.mask
);
1260 } else if (labels_nonzero(&info
->labels
.mask
)) {
1261 err
= ovs_ct_set_labels(ct
, key
, &info
->labels
.value
,
1262 &info
->labels
.mask
);
1266 /* This will take care of sending queued events even if the connection
1267 * is already confirmed.
1269 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
1275 /* Trim the skb to the length specified by the IP/IPv6 header,
1276 * removing any trailing lower-layer padding. This prepares the skb
1277 * for higher-layer processing that assumes skb->len excludes padding
1278 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1279 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1281 static int ovs_skb_network_trim(struct sk_buff
*skb
)
1286 switch (skb
->protocol
) {
1287 case htons(ETH_P_IP
):
1288 len
= ntohs(ip_hdr(skb
)->tot_len
);
1290 case htons(ETH_P_IPV6
):
1291 len
= sizeof(struct ipv6hdr
)
1292 + ntohs(ipv6_hdr(skb
)->payload_len
);
1298 err
= pskb_trim_rcsum(skb
, len
);
1305 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1306 * value if 'skb' is freed.
1308 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
1309 struct sw_flow_key
*key
,
1310 const struct ovs_conntrack_info
*info
)
1315 /* The conntrack module expects to be working at L3. */
1316 nh_ofs
= skb_network_offset(skb
);
1317 skb_pull_rcsum(skb
, nh_ofs
);
1319 err
= ovs_skb_network_trim(skb
);
1323 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
1324 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
1330 err
= ovs_ct_commit(net
, key
, info
, skb
);
1332 err
= ovs_ct_lookup(net
, key
, info
, skb
);
1334 skb_push(skb
, nh_ofs
);
1335 skb_postpush_rcsum(skb
, skb
->data
, nh_ofs
);
1341 int ovs_ct_clear(struct sk_buff
*skb
, struct sw_flow_key
*key
)
1343 if (skb_nfct(skb
)) {
1344 nf_conntrack_put(skb_nfct(skb
));
1345 #ifdef HAVE_IP_CT_UNTRACKED
1346 nf_ct_set(skb
, NULL
, IP_CT_UNTRACKED
);
1348 nf_ct_set(skb
, NULL
, 0);
1350 ovs_ct_fill_key(skb
, key
);
1356 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
1357 const struct sw_flow_key
*key
, bool log
)
1359 struct nf_conntrack_helper
*helper
;
1360 struct nf_conn_help
*help
;
1362 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
1365 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
1369 help
= nf_ct_helper_ext_add(info
->ct
, helper
, GFP_KERNEL
);
1371 nf_conntrack_helper_put(helper
);
1375 rcu_assign_pointer(help
->helper
, helper
);
1376 info
->helper
= helper
;
1379 request_module("ip_nat_%s", name
);
1384 #ifdef CONFIG_NF_NAT_NEEDED
1385 static int parse_nat(const struct nlattr
*attr
,
1386 struct ovs_conntrack_info
*info
, bool log
)
1390 bool have_ip_max
= false;
1391 bool have_proto_max
= false;
1392 bool ip_vers
= (info
->family
== NFPROTO_IPV6
);
1394 nla_for_each_nested(a
, attr
, rem
) {
1395 static const int ovs_nat_attr_lens
[OVS_NAT_ATTR_MAX
+ 1][2] = {
1396 [OVS_NAT_ATTR_SRC
] = {0, 0},
1397 [OVS_NAT_ATTR_DST
] = {0, 0},
1398 [OVS_NAT_ATTR_IP_MIN
] = {sizeof(struct in_addr
),
1399 sizeof(struct in6_addr
)},
1400 [OVS_NAT_ATTR_IP_MAX
] = {sizeof(struct in_addr
),
1401 sizeof(struct in6_addr
)},
1402 [OVS_NAT_ATTR_PROTO_MIN
] = {sizeof(u16
), sizeof(u16
)},
1403 [OVS_NAT_ATTR_PROTO_MAX
] = {sizeof(u16
), sizeof(u16
)},
1404 [OVS_NAT_ATTR_PERSISTENT
] = {0, 0},
1405 [OVS_NAT_ATTR_PROTO_HASH
] = {0, 0},
1406 [OVS_NAT_ATTR_PROTO_RANDOM
] = {0, 0},
1408 int type
= nla_type(a
);
1410 if (type
> OVS_NAT_ATTR_MAX
) {
1411 OVS_NLERR(log
, "Unknown NAT attribute (type=%d, max=%d)",
1412 type
, OVS_NAT_ATTR_MAX
);
1416 if (nla_len(a
) != ovs_nat_attr_lens
[type
][ip_vers
]) {
1417 OVS_NLERR(log
, "NAT attribute type %d has unexpected length (%d != %d)",
1419 ovs_nat_attr_lens
[type
][ip_vers
]);
1424 case OVS_NAT_ATTR_SRC
:
1425 case OVS_NAT_ATTR_DST
:
1427 OVS_NLERR(log
, "Only one type of NAT may be specified");
1430 info
->nat
|= OVS_CT_NAT
;
1431 info
->nat
|= ((type
== OVS_NAT_ATTR_SRC
)
1432 ? OVS_CT_SRC_NAT
: OVS_CT_DST_NAT
);
1435 case OVS_NAT_ATTR_IP_MIN
:
1436 nla_memcpy(&info
->range
.min_addr
, a
,
1437 sizeof(info
->range
.min_addr
));
1438 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1441 case OVS_NAT_ATTR_IP_MAX
:
1443 nla_memcpy(&info
->range
.max_addr
, a
,
1444 sizeof(info
->range
.max_addr
));
1445 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1448 case OVS_NAT_ATTR_PROTO_MIN
:
1449 info
->range
.min_proto
.all
= htons(nla_get_u16(a
));
1450 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1453 case OVS_NAT_ATTR_PROTO_MAX
:
1454 have_proto_max
= true;
1455 info
->range
.max_proto
.all
= htons(nla_get_u16(a
));
1456 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1459 case OVS_NAT_ATTR_PERSISTENT
:
1460 info
->range
.flags
|= NF_NAT_RANGE_PERSISTENT
;
1463 case OVS_NAT_ATTR_PROTO_HASH
:
1464 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1467 case OVS_NAT_ATTR_PROTO_RANDOM
:
1468 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1469 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM_FULLY
;
1471 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1472 info
->random_fully_compat
= true;
1477 OVS_NLERR(log
, "Unknown nat attribute (%d)", type
);
1483 OVS_NLERR(log
, "NAT attribute has %d unknown bytes", rem
);
1487 /* Do not allow flags if no type is given. */
1488 if (info
->range
.flags
) {
1490 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1494 info
->nat
= OVS_CT_NAT
; /* NAT existing connections. */
1495 } else if (!info
->commit
) {
1497 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1501 /* Allow missing IP_MAX. */
1502 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
&& !have_ip_max
) {
1503 memcpy(&info
->range
.max_addr
, &info
->range
.min_addr
,
1504 sizeof(info
->range
.max_addr
));
1506 /* Allow missing PROTO_MAX. */
1507 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1509 info
->range
.max_proto
.all
= info
->range
.min_proto
.all
;
1515 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
1516 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1517 [OVS_CT_ATTR_FORCE_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1518 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
1519 .maxlen
= sizeof(u16
) },
1520 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
1521 .maxlen
= sizeof(struct md_mark
) },
1522 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
1523 .maxlen
= sizeof(struct md_labels
) },
1524 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
1525 .maxlen
= NF_CT_HELPER_NAME_LEN
},
1526 #ifdef CONFIG_NF_NAT_NEEDED
1527 /* NAT length is checked when parsing the nested attributes. */
1528 [OVS_CT_ATTR_NAT
] = { .minlen
= 0, .maxlen
= INT_MAX
},
1530 [OVS_CT_ATTR_EVENTMASK
] = { .minlen
= sizeof(u32
),
1531 .maxlen
= sizeof(u32
) },
1534 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
1535 const char **helper
, bool log
)
1540 nla_for_each_nested(a
, attr
, rem
) {
1541 int type
= nla_type(a
);
1545 if (type
> OVS_CT_ATTR_MAX
) {
1547 "Unknown conntrack attr (type=%d, max=%d)",
1548 type
, OVS_CT_ATTR_MAX
);
1552 maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
1553 minlen
= ovs_ct_attr_lens
[type
].minlen
;
1554 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
1556 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1557 type
, nla_len(a
), maxlen
);
1562 case OVS_CT_ATTR_FORCE_COMMIT
:
1565 case OVS_CT_ATTR_COMMIT
:
1566 info
->commit
= true;
1568 #ifdef CONFIG_NF_CONNTRACK_ZONES
1569 case OVS_CT_ATTR_ZONE
:
1570 info
->zone
.id
= nla_get_u16(a
);
1573 #ifdef CONFIG_NF_CONNTRACK_MARK
1574 case OVS_CT_ATTR_MARK
: {
1575 struct md_mark
*mark
= nla_data(a
);
1578 OVS_NLERR(log
, "ct_mark mask cannot be 0");
1585 #ifdef CONFIG_NF_CONNTRACK_LABELS
1586 case OVS_CT_ATTR_LABELS
: {
1587 struct md_labels
*labels
= nla_data(a
);
1589 if (!labels_nonzero(&labels
->mask
)) {
1590 OVS_NLERR(log
, "ct_labels mask cannot be 0");
1593 info
->labels
= *labels
;
1597 case OVS_CT_ATTR_HELPER
:
1598 *helper
= nla_data(a
);
1599 if (!memchr(*helper
, '\0', nla_len(a
))) {
1600 OVS_NLERR(log
, "Invalid conntrack helper");
1604 #ifdef CONFIG_NF_NAT_NEEDED
1605 case OVS_CT_ATTR_NAT
: {
1606 int err
= parse_nat(a
, info
, log
);
1613 case OVS_CT_ATTR_EVENTMASK
:
1614 info
->have_eventmask
= true;
1615 info
->eventmask
= nla_get_u32(a
);
1619 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
1625 #ifdef CONFIG_NF_CONNTRACK_MARK
1626 if (!info
->commit
&& info
->mark
.mask
) {
1628 "Setting conntrack mark requires 'commit' flag.");
1632 #ifdef CONFIG_NF_CONNTRACK_LABELS
1633 if (!info
->commit
&& labels_nonzero(&info
->labels
.mask
)) {
1635 "Setting conntrack labels requires 'commit' flag.");
1640 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
1647 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
1649 if (attr
== OVS_KEY_ATTR_CT_STATE
)
1651 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1652 attr
== OVS_KEY_ATTR_CT_ZONE
)
1654 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
1655 attr
== OVS_KEY_ATTR_CT_MARK
)
1657 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1658 attr
== OVS_KEY_ATTR_CT_LABELS
) {
1659 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1661 return ovs_net
->xt_label
;
1667 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
1668 const struct sw_flow_key
*key
,
1669 struct sw_flow_actions
**sfa
, bool log
)
1671 struct ovs_conntrack_info ct_info
;
1672 const char *helper
= NULL
;
1676 family
= key_to_nfproto(key
);
1677 if (family
== NFPROTO_UNSPEC
) {
1678 OVS_NLERR(log
, "ct family unspecified");
1682 memset(&ct_info
, 0, sizeof(ct_info
));
1683 ct_info
.family
= family
;
1685 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
1686 NF_CT_DEFAULT_ZONE_DIR
, 0);
1688 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
1692 /* Set up template for tracking connections in specific zones. */
1693 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
1695 OVS_NLERR(log
, "Failed to allocate conntrack template");
1699 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
1704 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
1705 sizeof(ct_info
), log
);
1709 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
1710 nf_conntrack_get(&ct_info
.ct
->ct_general
);
1713 __ovs_ct_free_action(&ct_info
);
1717 #ifdef CONFIG_NF_NAT_NEEDED
1718 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info
*info
,
1719 struct sk_buff
*skb
)
1721 struct nlattr
*start
;
1723 start
= nla_nest_start(skb
, OVS_CT_ATTR_NAT
);
1727 if (info
->nat
& OVS_CT_SRC_NAT
) {
1728 if (nla_put_flag(skb
, OVS_NAT_ATTR_SRC
))
1730 } else if (info
->nat
& OVS_CT_DST_NAT
) {
1731 if (nla_put_flag(skb
, OVS_NAT_ATTR_DST
))
1737 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
) {
1738 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
1739 info
->family
== NFPROTO_IPV4
) {
1740 if (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1741 info
->range
.min_addr
.ip
) ||
1742 (info
->range
.max_addr
.ip
1743 != info
->range
.min_addr
.ip
&&
1744 (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1745 info
->range
.max_addr
.ip
))))
1747 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
1748 info
->family
== NFPROTO_IPV6
) {
1749 if (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1750 &info
->range
.min_addr
.in6
) ||
1751 (memcmp(&info
->range
.max_addr
.in6
,
1752 &info
->range
.min_addr
.in6
,
1753 sizeof(info
->range
.max_addr
.in6
)) &&
1754 (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1755 &info
->range
.max_addr
.in6
))))
1761 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1762 (nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MIN
,
1763 ntohs(info
->range
.min_proto
.all
)) ||
1764 (info
->range
.max_proto
.all
!= info
->range
.min_proto
.all
&&
1765 nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MAX
,
1766 ntohs(info
->range
.max_proto
.all
)))))
1769 if (info
->range
.flags
& NF_NAT_RANGE_PERSISTENT
&&
1770 nla_put_flag(skb
, OVS_NAT_ATTR_PERSISTENT
))
1772 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM
&&
1773 nla_put_flag(skb
, info
->random_fully_compat
1774 ? OVS_NAT_ATTR_PROTO_RANDOM
1775 : OVS_NAT_ATTR_PROTO_HASH
))
1777 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1778 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM_FULLY
&&
1779 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_RANDOM
))
1783 nla_nest_end(skb
, start
);
1789 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
1790 struct sk_buff
*skb
)
1792 struct nlattr
*start
;
1794 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_CT
);
1798 if (ct_info
->commit
&& nla_put_flag(skb
, ct_info
->force
1799 ? OVS_CT_ATTR_FORCE_COMMIT
1800 : OVS_CT_ATTR_COMMIT
))
1802 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1803 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
1805 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
1806 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
1809 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1810 labels_nonzero(&ct_info
->labels
.mask
) &&
1811 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
1814 if (ct_info
->helper
) {
1815 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
1816 ct_info
->helper
->name
))
1819 if (ct_info
->have_eventmask
&&
1820 nla_put_u32(skb
, OVS_CT_ATTR_EVENTMASK
, ct_info
->eventmask
))
1823 #ifdef CONFIG_NF_NAT_NEEDED
1824 if (ct_info
->nat
&& !ovs_ct_nat_to_attr(ct_info
, skb
))
1827 nla_nest_end(skb
, start
);
1832 void ovs_ct_free_action(const struct nlattr
*a
)
1834 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
1836 __ovs_ct_free_action(ct_info
);
1839 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
)
1841 if (ct_info
->helper
)
1842 nf_conntrack_helper_put(ct_info
->helper
);
1844 nf_ct_tmpl_free(ct_info
->ct
);
1847 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1848 static int ovs_ct_limit_init(struct net
*net
, struct ovs_net
*ovs_net
)
1852 ovs_net
->ct_limit_info
= kmalloc(sizeof(*ovs_net
->ct_limit_info
),
1854 if (!ovs_net
->ct_limit_info
)
1857 ovs_net
->ct_limit_info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1858 ovs_net
->ct_limit_info
->limits
=
1859 kmalloc_array(CT_LIMIT_HASH_BUCKETS
, sizeof(struct hlist_head
),
1861 if (!ovs_net
->ct_limit_info
->limits
) {
1862 kfree(ovs_net
->ct_limit_info
);
1866 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; i
++)
1867 INIT_HLIST_HEAD(&ovs_net
->ct_limit_info
->limits
[i
]);
1869 ovs_net
->ct_limit_info
->data
=
1870 nf_conncount_init(net
, NFPROTO_INET
, sizeof(u32
));
1872 if (IS_ERR(ovs_net
->ct_limit_info
->data
)) {
1873 err
= PTR_ERR(ovs_net
->ct_limit_info
->data
);
1874 kfree(ovs_net
->ct_limit_info
->limits
);
1875 kfree(ovs_net
->ct_limit_info
);
1876 pr_err("openvswitch: failed to init nf_conncount %d\n", err
);
1882 static void ovs_ct_limit_exit(struct net
*net
, struct ovs_net
*ovs_net
)
1884 const struct ovs_ct_limit_info
*info
= ovs_net
->ct_limit_info
;
1887 nf_conncount_destroy(net
, NFPROTO_INET
, info
->data
);
1888 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
1889 struct hlist_head
*head
= &info
->limits
[i
];
1890 struct ovs_ct_limit
*ct_limit
;
1892 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
)
1893 kfree_rcu(ct_limit
, rcu
);
1895 kfree(ovs_net
->ct_limit_info
->limits
);
1896 kfree(ovs_net
->ct_limit_info
);
1899 static struct sk_buff
*
1900 ovs_ct_limit_cmd_reply_start(struct genl_info
*info
, u8 cmd
,
1901 struct ovs_header
**ovs_reply_header
)
1903 struct ovs_header
*ovs_header
= info
->userhdr
;
1904 struct sk_buff
*skb
;
1906 skb
= genlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1908 return ERR_PTR(-ENOMEM
);
1910 *ovs_reply_header
= genlmsg_put(skb
, info
->snd_portid
,
1912 &dp_ct_limit_genl_family
, 0, cmd
);
1914 if (!*ovs_reply_header
) {
1916 return ERR_PTR(-EMSGSIZE
);
1918 (*ovs_reply_header
)->dp_ifindex
= ovs_header
->dp_ifindex
;
1923 static bool check_zone_id(int zone_id
, u16
*pzone
)
1925 if (zone_id
>= 0 && zone_id
<= 65535) {
1926 *pzone
= (u16
)zone_id
;
1932 static int ovs_ct_limit_set_zone_limit(struct nlattr
*nla_zone_limit
,
1933 struct ovs_ct_limit_info
*info
)
1935 struct ovs_zone_limit
*zone_limit
;
1939 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1940 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1942 while (rem
>= sizeof(*zone_limit
)) {
1943 if (unlikely(zone_limit
->zone_id
==
1944 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1946 info
->default_limit
= zone_limit
->limit
;
1948 } else if (unlikely(!check_zone_id(
1949 zone_limit
->zone_id
, &zone
))) {
1950 OVS_NLERR(true, "zone id is out of range");
1952 struct ovs_ct_limit
*ct_limit
;
1954 ct_limit
= kmalloc(sizeof(*ct_limit
), GFP_KERNEL
);
1958 ct_limit
->zone
= zone
;
1959 ct_limit
->limit
= zone_limit
->limit
;
1962 ct_limit_set(info
, ct_limit
);
1965 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
1966 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
1967 NLA_ALIGN(sizeof(*zone_limit
)));
1971 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem
);
1976 static int ovs_ct_limit_del_zone_limit(struct nlattr
*nla_zone_limit
,
1977 struct ovs_ct_limit_info
*info
)
1979 struct ovs_zone_limit
*zone_limit
;
1983 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1984 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1986 while (rem
>= sizeof(*zone_limit
)) {
1987 if (unlikely(zone_limit
->zone_id
==
1988 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1990 info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1992 } else if (unlikely(!check_zone_id(
1993 zone_limit
->zone_id
, &zone
))) {
1994 OVS_NLERR(true, "zone id is out of range");
1997 ct_limit_del(info
, zone
);
2000 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2001 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2002 NLA_ALIGN(sizeof(*zone_limit
)));
2006 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem
);
2011 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info
*info
,
2012 struct sk_buff
*reply
)
2014 struct ovs_zone_limit zone_limit
;
2017 zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2018 zone_limit
.limit
= info
->default_limit
;
2019 err
= nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2026 static int __ovs_ct_limit_get_zone_limit(struct net
*net
,
2027 struct nf_conncount_data
*data
,
2028 u16 zone_id
, u32 limit
,
2029 struct sk_buff
*reply
)
2031 struct nf_conntrack_zone ct_zone
;
2032 struct ovs_zone_limit zone_limit
;
2033 u32 conncount_key
= zone_id
;
2035 zone_limit
.zone_id
= zone_id
;
2036 zone_limit
.limit
= limit
;
2037 nf_ct_zone_init(&ct_zone
, zone_id
, NF_CT_DEFAULT_ZONE_DIR
, 0);
2039 zone_limit
.count
= nf_conncount_count(net
, data
, &conncount_key
, NULL
,
2041 return nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2044 static int ovs_ct_limit_get_zone_limit(struct net
*net
,
2045 struct nlattr
*nla_zone_limit
,
2046 struct ovs_ct_limit_info
*info
,
2047 struct sk_buff
*reply
)
2049 struct ovs_zone_limit
*zone_limit
;
2054 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2055 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2057 while (rem
>= sizeof(*zone_limit
)) {
2058 if (unlikely(zone_limit
->zone_id
==
2059 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2060 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2063 } else if (unlikely(!check_zone_id(zone_limit
->zone_id
,
2065 OVS_NLERR(true, "zone id is out of range");
2068 limit
= ct_limit_get(info
, zone
);
2071 err
= __ovs_ct_limit_get_zone_limit(
2072 net
, info
->data
, zone
, limit
, reply
);
2076 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2077 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2078 NLA_ALIGN(sizeof(*zone_limit
)));
2082 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem
);
2087 static int ovs_ct_limit_get_all_zone_limit(struct net
*net
,
2088 struct ovs_ct_limit_info
*info
,
2089 struct sk_buff
*reply
)
2091 struct ovs_ct_limit
*ct_limit
;
2092 struct hlist_head
*head
;
2095 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2100 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
2101 head
= &info
->limits
[i
];
2102 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
2103 err
= __ovs_ct_limit_get_zone_limit(net
, info
->data
,
2104 ct_limit
->zone
, ct_limit
->limit
, reply
);
2115 static int ovs_ct_limit_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2117 struct nlattr
**a
= info
->attrs
;
2118 struct sk_buff
*reply
;
2119 struct ovs_header
*ovs_reply_header
;
2120 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2121 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2124 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_SET
,
2127 return PTR_ERR(reply
);
2129 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2134 err
= ovs_ct_limit_set_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2139 static_branch_enable(&ovs_ct_limit_enabled
);
2141 genlmsg_end(reply
, ovs_reply_header
);
2142 return genlmsg_reply(reply
, info
);
2149 static int ovs_ct_limit_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2151 struct nlattr
**a
= info
->attrs
;
2152 struct sk_buff
*reply
;
2153 struct ovs_header
*ovs_reply_header
;
2154 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2155 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2158 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_DEL
,
2161 return PTR_ERR(reply
);
2163 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2168 err
= ovs_ct_limit_del_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2173 genlmsg_end(reply
, ovs_reply_header
);
2174 return genlmsg_reply(reply
, info
);
2181 static int ovs_ct_limit_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2183 struct nlattr
**a
= info
->attrs
;
2184 struct nlattr
*nla_reply
;
2185 struct sk_buff
*reply
;
2186 struct ovs_header
*ovs_reply_header
;
2187 struct net
*net
= sock_net(skb
->sk
);
2188 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2189 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2192 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_GET
,
2195 return PTR_ERR(reply
);
2197 nla_reply
= nla_nest_start(reply
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2199 if (a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2200 err
= ovs_ct_limit_get_zone_limit(
2201 net
, a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
], ct_limit_info
,
2206 err
= ovs_ct_limit_get_all_zone_limit(net
, ct_limit_info
,
2212 nla_nest_end(reply
, nla_reply
);
2213 genlmsg_end(reply
, ovs_reply_header
);
2214 return genlmsg_reply(reply
, info
);
2221 static struct genl_ops ct_limit_genl_ops
[] = {
2222 { .cmd
= OVS_CT_LIMIT_CMD_SET
,
2223 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2225 .policy
= ct_limit_policy
,
2226 .doit
= ovs_ct_limit_cmd_set
,
2228 { .cmd
= OVS_CT_LIMIT_CMD_DEL
,
2229 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2231 .policy
= ct_limit_policy
,
2232 .doit
= ovs_ct_limit_cmd_del
,
2234 { .cmd
= OVS_CT_LIMIT_CMD_GET
,
2235 .flags
= 0, /* OK for unprivileged users. */
2236 .policy
= ct_limit_policy
,
2237 .doit
= ovs_ct_limit_cmd_get
,
2241 static const struct genl_multicast_group ovs_ct_limit_multicast_group
= {
2242 .name
= OVS_CT_LIMIT_MCGROUP
,
2245 struct genl_family dp_ct_limit_genl_family __ro_after_init
= {
2246 .hdrsize
= sizeof(struct ovs_header
),
2247 .name
= OVS_CT_LIMIT_FAMILY
,
2248 .version
= OVS_CT_LIMIT_VERSION
,
2249 .maxattr
= OVS_CT_LIMIT_ATTR_MAX
,
2251 .parallel_ops
= true,
2252 .ops
= ct_limit_genl_ops
,
2253 .n_ops
= ARRAY_SIZE(ct_limit_genl_ops
),
2254 .mcgrps
= &ovs_ct_limit_multicast_group
,
2256 .module
= THIS_MODULE
,
2260 int ovs_ct_init(struct net
*net
)
2262 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
2263 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2265 if (nf_connlabels_get(net
, n_bits
- 1)) {
2266 ovs_net
->xt_label
= false;
2267 OVS_NLERR(true, "Failed to set connlabel length");
2269 ovs_net
->xt_label
= true;
2272 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2273 return ovs_ct_limit_init(net
, ovs_net
);
2279 void ovs_ct_exit(struct net
*net
)
2281 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2283 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2284 ovs_ct_limit_exit(net
, ovs_net
);
2287 if (ovs_net
->xt_label
)
2288 nf_connlabels_put(net
);
2291 #endif /* CONFIG_NF_CONNTRACK */