1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Nicira, Inc.
6 #include <linux/module.h>
7 #include <linux/openvswitch.h>
10 #include <linux/sctp.h>
11 #include <linux/static_key.h>
13 #include <net/genetlink.h>
14 #include <net/netfilter/nf_conntrack_core.h>
15 #include <net/netfilter/nf_conntrack_count.h>
16 #include <net/netfilter/nf_conntrack_helper.h>
17 #include <net/netfilter/nf_conntrack_labels.h>
18 #include <net/netfilter/nf_conntrack_seqadj.h>
19 #include <net/netfilter/nf_conntrack_timeout.h>
20 #include <net/netfilter/nf_conntrack_zones.h>
21 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
22 #include <net/ipv6_frag.h>
24 #if IS_ENABLED(CONFIG_NF_NAT)
25 #include <net/netfilter/nf_nat.h>
29 #include "conntrack.h"
31 #include "flow_netlink.h"
33 struct ovs_ct_len_tbl
{
38 /* Metadata mark for masked write to conntrack mark */
44 /* Metadata label for masked write to conntrack label. */
46 struct ovs_key_ct_labels value
;
47 struct ovs_key_ct_labels mask
;
51 OVS_CT_NAT
= 1 << 0, /* NAT for committed connections only. */
52 OVS_CT_SRC_NAT
= 1 << 1, /* Source NAT for NEW connections. */
53 OVS_CT_DST_NAT
= 1 << 2, /* Destination NAT for NEW connections. */
56 /* Conntrack action context for execution. */
57 struct ovs_conntrack_info
{
58 struct nf_conntrack_helper
*helper
;
59 struct nf_conntrack_zone zone
;
62 u8 nat
: 3; /* enum ovs_ct_nat */
64 u8 have_eventmask
: 1;
66 u32 eventmask
; /* Mask of 1 << IPCT_*. */
68 struct md_labels labels
;
69 char timeout
[CTNL_TIMEOUT_NAME_MAX
];
70 #if IS_ENABLED(CONFIG_NF_NAT)
71 struct nf_nat_range2 range
; /* Only present for SRC NAT and DST NAT. */
75 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
76 #define OVS_CT_LIMIT_UNLIMITED 0
77 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
78 #define CT_LIMIT_HASH_BUCKETS 512
79 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled
);
82 /* Elements in ovs_ct_limit_info->limits hash table */
83 struct hlist_node hlist_node
;
89 struct ovs_ct_limit_info
{
91 struct hlist_head
*limits
;
92 struct nf_conncount_data
*data
;
95 static const struct nla_policy ct_limit_policy
[OVS_CT_LIMIT_ATTR_MAX
+ 1] = {
96 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NLA_NESTED
, },
100 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
);
102 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
);
104 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
106 switch (ntohs(key
->eth
.type
)) {
112 return NFPROTO_UNSPEC
;
116 /* Map SKB connection state into the values used by flow definition. */
117 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
119 u8 ct_state
= OVS_CS_F_TRACKED
;
122 case IP_CT_ESTABLISHED_REPLY
:
123 case IP_CT_RELATED_REPLY
:
124 ct_state
|= OVS_CS_F_REPLY_DIR
;
131 case IP_CT_ESTABLISHED
:
132 case IP_CT_ESTABLISHED_REPLY
:
133 ct_state
|= OVS_CS_F_ESTABLISHED
;
136 case IP_CT_RELATED_REPLY
:
137 ct_state
|= OVS_CS_F_RELATED
;
140 ct_state
|= OVS_CS_F_NEW
;
149 static u32
ovs_ct_get_mark(const struct nf_conn
*ct
)
151 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
152 return ct
? ct
->mark
: 0;
158 /* Guard against conntrack labels max size shrinking below 128 bits. */
159 #if NF_CT_LABELS_MAX_SIZE < 16
160 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
163 static void ovs_ct_get_labels(const struct nf_conn
*ct
,
164 struct ovs_key_ct_labels
*labels
)
166 struct nf_conn_labels
*cl
= ct
? nf_ct_labels_find(ct
) : NULL
;
169 memcpy(labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
171 memset(labels
, 0, OVS_CT_LABELS_LEN
);
174 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key
*key
,
175 const struct nf_conntrack_tuple
*orig
,
178 key
->ct_orig_proto
= orig
->dst
.protonum
;
179 if (orig
->dst
.protonum
== icmp_proto
) {
180 key
->ct
.orig_tp
.src
= htons(orig
->dst
.u
.icmp
.type
);
181 key
->ct
.orig_tp
.dst
= htons(orig
->dst
.u
.icmp
.code
);
183 key
->ct
.orig_tp
.src
= orig
->src
.u
.all
;
184 key
->ct
.orig_tp
.dst
= orig
->dst
.u
.all
;
188 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
189 const struct nf_conntrack_zone
*zone
,
190 const struct nf_conn
*ct
)
192 key
->ct_state
= state
;
193 key
->ct_zone
= zone
->id
;
194 key
->ct
.mark
= ovs_ct_get_mark(ct
);
195 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
198 const struct nf_conntrack_tuple
*orig
;
200 /* Use the master if we have one. */
203 orig
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
205 /* IP version must match with the master connection. */
206 if (key
->eth
.type
== htons(ETH_P_IP
) &&
207 nf_ct_l3num(ct
) == NFPROTO_IPV4
) {
208 key
->ipv4
.ct_orig
.src
= orig
->src
.u3
.ip
;
209 key
->ipv4
.ct_orig
.dst
= orig
->dst
.u3
.ip
;
210 __ovs_ct_update_key_orig_tp(key
, orig
, IPPROTO_ICMP
);
212 } else if (key
->eth
.type
== htons(ETH_P_IPV6
) &&
213 !sw_flow_key_is_nd(key
) &&
214 nf_ct_l3num(ct
) == NFPROTO_IPV6
) {
215 key
->ipv6
.ct_orig
.src
= orig
->src
.u3
.in6
;
216 key
->ipv6
.ct_orig
.dst
= orig
->dst
.u3
.in6
;
217 __ovs_ct_update_key_orig_tp(key
, orig
, NEXTHDR_ICMP
);
221 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
222 * original direction key fields.
224 key
->ct_orig_proto
= 0;
227 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
228 * previously sent the packet to conntrack via the ct action. If
229 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
230 * initialized from the connection status.
232 static void ovs_ct_update_key(const struct sk_buff
*skb
,
233 const struct ovs_conntrack_info
*info
,
234 struct sw_flow_key
*key
, bool post_ct
,
237 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
238 enum ip_conntrack_info ctinfo
;
242 ct
= nf_ct_get(skb
, &ctinfo
);
244 state
= ovs_ct_get_state(ctinfo
);
245 /* All unconfirmed entries are NEW connections. */
246 if (!nf_ct_is_confirmed(ct
))
247 state
|= OVS_CS_F_NEW
;
248 /* OVS persists the related flag for the duration of the
252 state
|= OVS_CS_F_RELATED
;
253 if (keep_nat_flags
) {
254 state
|= key
->ct_state
& OVS_CS_F_NAT_MASK
;
256 if (ct
->status
& IPS_SRC_NAT
)
257 state
|= OVS_CS_F_SRC_NAT
;
258 if (ct
->status
& IPS_DST_NAT
)
259 state
|= OVS_CS_F_DST_NAT
;
261 zone
= nf_ct_zone(ct
);
262 } else if (post_ct
) {
263 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
267 __ovs_ct_update_key(key
, state
, zone
, ct
);
270 /* This is called to initialize CT key fields possibly coming in from the local
273 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
275 ovs_ct_update_key(skb
, NULL
, key
, false, false);
278 #define IN6_ADDR_INITIALIZER(ADDR) \
279 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
280 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
282 int ovs_ct_put_key(const struct sw_flow_key
*swkey
,
283 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
285 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, output
->ct_state
))
288 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
289 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, output
->ct_zone
))
292 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
293 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, output
->ct
.mark
))
296 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
297 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(output
->ct
.labels
),
301 if (swkey
->ct_orig_proto
) {
302 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
303 struct ovs_key_ct_tuple_ipv4 orig
= {
304 output
->ipv4
.ct_orig
.src
,
305 output
->ipv4
.ct_orig
.dst
,
306 output
->ct
.orig_tp
.src
,
307 output
->ct
.orig_tp
.dst
,
308 output
->ct_orig_proto
,
310 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
311 sizeof(orig
), &orig
))
313 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
314 struct ovs_key_ct_tuple_ipv6 orig
= {
315 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.src
),
316 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.dst
),
317 output
->ct
.orig_tp
.src
,
318 output
->ct
.orig_tp
.dst
,
319 output
->ct_orig_proto
,
321 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
322 sizeof(orig
), &orig
))
330 static int ovs_ct_set_mark(struct nf_conn
*ct
, struct sw_flow_key
*key
,
331 u32 ct_mark
, u32 mask
)
333 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
336 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
337 if (ct
->mark
!= new_mark
) {
339 if (nf_ct_is_confirmed(ct
))
340 nf_conntrack_event_cache(IPCT_MARK
, ct
);
341 key
->ct
.mark
= new_mark
;
350 static struct nf_conn_labels
*ovs_ct_get_conn_labels(struct nf_conn
*ct
)
352 struct nf_conn_labels
*cl
;
354 cl
= nf_ct_labels_find(ct
);
356 nf_ct_labels_ext_add(ct
);
357 cl
= nf_ct_labels_find(ct
);
363 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
364 * since the new connection is not yet confirmed, and thus no-one else has
365 * access to it's labels, we simply write them over.
367 static int ovs_ct_init_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
368 const struct ovs_key_ct_labels
*labels
,
369 const struct ovs_key_ct_labels
*mask
)
371 struct nf_conn_labels
*cl
, *master_cl
;
372 bool have_mask
= labels_nonzero(mask
);
374 /* Inherit master's labels to the related connection? */
375 master_cl
= ct
->master
? nf_ct_labels_find(ct
->master
) : NULL
;
377 if (!master_cl
&& !have_mask
)
378 return 0; /* Nothing to do. */
380 cl
= ovs_ct_get_conn_labels(ct
);
384 /* Inherit the master's labels, if any. */
389 u32
*dst
= (u32
*)cl
->bits
;
392 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
393 dst
[i
] = (dst
[i
] & ~mask
->ct_labels_32
[i
]) |
394 (labels
->ct_labels_32
[i
]
395 & mask
->ct_labels_32
[i
]);
398 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
399 * IPCT_LABEL bit is set in the event cache.
401 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
403 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
408 static int ovs_ct_set_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
409 const struct ovs_key_ct_labels
*labels
,
410 const struct ovs_key_ct_labels
*mask
)
412 struct nf_conn_labels
*cl
;
415 cl
= ovs_ct_get_conn_labels(ct
);
419 err
= nf_connlabels_replace(ct
, labels
->ct_labels_32
,
421 OVS_CT_LABELS_LEN_32
);
425 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
430 /* 'skb' should already be pulled to nh_ofs. */
431 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
433 const struct nf_conntrack_helper
*helper
;
434 const struct nf_conn_help
*help
;
435 enum ip_conntrack_info ctinfo
;
436 unsigned int protoff
;
440 ct
= nf_ct_get(skb
, &ctinfo
);
441 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
444 help
= nfct_help(ct
);
448 helper
= rcu_dereference(help
->helper
);
454 protoff
= ip_hdrlen(skb
);
457 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
461 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
463 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
464 pr_debug("proto header not found\n");
471 WARN_ONCE(1, "helper invoked on non-IP family!");
475 err
= helper
->help(skb
, protoff
, ct
, ctinfo
);
476 if (err
!= NF_ACCEPT
)
479 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
480 * FTP with NAT) adusting the TCP payload size when mangling IP
481 * addresses and/or port numbers in the text-based control connection.
483 if (test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
) &&
484 !nf_ct_seq_adjust(skb
, ct
, ctinfo
, protoff
))
489 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
490 * value if 'skb' is freed.
492 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
493 u16 zone
, struct sk_buff
*skb
)
495 struct ovs_skb_cb ovs_cb
= *OVS_CB(skb
);
498 if (key
->eth
.type
== htons(ETH_P_IP
)) {
499 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
501 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
502 err
= ip_defrag(net
, skb
, user
);
506 ovs_cb
.mru
= IPCB(skb
)->frag_max_size
;
507 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
508 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
509 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
511 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
512 err
= nf_ct_frag6_gather(net
, skb
, user
);
514 if (err
!= -EINPROGRESS
)
519 key
->ip
.proto
= ipv6_hdr(skb
)->nexthdr
;
520 ovs_cb
.mru
= IP6CB(skb
)->frag_max_size
;
524 return -EPFNOSUPPORT
;
527 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
530 *OVS_CB(skb
) = ovs_cb
;
535 static struct nf_conntrack_expect
*
536 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
537 u16 proto
, const struct sk_buff
*skb
)
539 struct nf_conntrack_tuple tuple
;
540 struct nf_conntrack_expect
*exp
;
542 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
545 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
547 struct nf_conntrack_tuple_hash
*h
;
549 /* Delete existing conntrack entry, if it clashes with the
550 * expectation. This can happen since conntrack ALGs do not
551 * check for clashes between (new) expectations and existing
552 * conntrack entries. nf_conntrack_in() will check the
553 * expectations only if a conntrack entry can not be found,
554 * which can lead to OVS finding the expectation (here) in the
555 * init direction, but which will not be removed by the
556 * nf_conntrack_in() call, if a matching conntrack entry is
557 * found instead. In this case all init direction packets
558 * would be reported as new related packets, while reply
559 * direction packets would be reported as un-related
560 * established packets.
562 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
564 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
566 nf_ct_delete(ct
, 0, 0);
567 nf_conntrack_put(&ct
->ct_general
);
574 /* This replicates logic from nf_conntrack_core.c that is not exported. */
575 static enum ip_conntrack_info
576 ovs_ct_get_info(const struct nf_conntrack_tuple_hash
*h
)
578 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
580 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
581 return IP_CT_ESTABLISHED_REPLY
;
582 /* Once we've had two way comms, always ESTABLISHED. */
583 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
584 return IP_CT_ESTABLISHED
;
585 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
586 return IP_CT_RELATED
;
590 /* Find an existing connection which this packet belongs to without
591 * re-attributing statistics or modifying the connection state. This allows an
592 * skb->_nfct lost due to an upcall to be recovered during actions execution.
594 * Must be called with rcu_read_lock.
596 * On success, populates skb->_nfct and returns the connection. Returns NULL
597 * if there is no existing entry.
599 static struct nf_conn
*
600 ovs_ct_find_existing(struct net
*net
, const struct nf_conntrack_zone
*zone
,
601 u8 l3num
, struct sk_buff
*skb
, bool natted
)
603 struct nf_conntrack_tuple tuple
;
604 struct nf_conntrack_tuple_hash
*h
;
607 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), l3num
,
609 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
613 /* Must invert the tuple if skb has been transformed by NAT. */
615 struct nf_conntrack_tuple inverse
;
617 if (!nf_ct_invert_tuple(&inverse
, &tuple
)) {
618 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
624 /* look for tuple match */
625 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
627 return NULL
; /* Not found. */
629 ct
= nf_ct_tuplehash_to_ctrack(h
);
631 /* Inverted packet tuple matches the reverse direction conntrack tuple,
632 * select the other tuplehash to get the right 'ctinfo' bits for this
636 h
= &ct
->tuplehash
[!h
->tuple
.dst
.dir
];
638 nf_ct_set(skb
, ct
, ovs_ct_get_info(h
));
643 struct nf_conn
*ovs_ct_executed(struct net
*net
,
644 const struct sw_flow_key
*key
,
645 const struct ovs_conntrack_info
*info
,
649 struct nf_conn
*ct
= NULL
;
651 /* If no ct, check if we have evidence that an existing conntrack entry
652 * might be found for this skb. This happens when we lose a skb->_nfct
653 * due to an upcall, or if the direction is being forced. If the
654 * connection was not confirmed, it is not cached and needs to be run
655 * through conntrack again.
657 *ct_executed
= (key
->ct_state
& OVS_CS_F_TRACKED
) &&
658 !(key
->ct_state
& OVS_CS_F_INVALID
) &&
659 (key
->ct_zone
== info
->zone
.id
);
661 if (*ct_executed
|| (!key
->ct_state
&& info
->force
)) {
662 ct
= ovs_ct_find_existing(net
, &info
->zone
, info
->family
, skb
,
670 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
671 static bool skb_nfct_cached(struct net
*net
,
672 const struct sw_flow_key
*key
,
673 const struct ovs_conntrack_info
*info
,
676 enum ip_conntrack_info ctinfo
;
678 bool ct_executed
= true;
680 ct
= nf_ct_get(skb
, &ctinfo
);
682 ct
= ovs_ct_executed(net
, key
, info
, skb
, &ct_executed
);
685 nf_ct_get(skb
, &ctinfo
);
689 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
691 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
694 struct nf_conn_help
*help
;
696 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
697 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
700 /* Force conntrack entry direction to the current packet? */
701 if (info
->force
&& CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
) {
702 /* Delete the conntrack entry if confirmed, else just release
705 if (nf_ct_is_confirmed(ct
))
706 nf_ct_delete(ct
, 0, 0);
708 nf_conntrack_put(&ct
->ct_general
);
709 nf_ct_set(skb
, NULL
, 0);
716 #if IS_ENABLED(CONFIG_NF_NAT)
717 /* Modelled after nf_nat_ipv[46]_fn().
718 * range is only used for new, uninitialized NAT state.
719 * Returns either NF_ACCEPT or NF_DROP.
721 static int ovs_ct_nat_execute(struct sk_buff
*skb
, struct nf_conn
*ct
,
722 enum ip_conntrack_info ctinfo
,
723 const struct nf_nat_range2
*range
,
724 enum nf_nat_manip_type maniptype
)
726 int hooknum
, nh_off
, err
= NF_ACCEPT
;
728 nh_off
= skb_network_offset(skb
);
729 skb_pull_rcsum(skb
, nh_off
);
731 /* See HOOK2MANIP(). */
732 if (maniptype
== NF_NAT_MANIP_SRC
)
733 hooknum
= NF_INET_LOCAL_IN
; /* Source NAT */
735 hooknum
= NF_INET_LOCAL_OUT
; /* Destination NAT */
739 case IP_CT_RELATED_REPLY
:
740 if (IS_ENABLED(CONFIG_NF_NAT
) &&
741 skb
->protocol
== htons(ETH_P_IP
) &&
742 ip_hdr(skb
)->protocol
== IPPROTO_ICMP
) {
743 if (!nf_nat_icmp_reply_translation(skb
, ct
, ctinfo
,
747 } else if (IS_ENABLED(CONFIG_IPV6
) &&
748 skb
->protocol
== htons(ETH_P_IPV6
)) {
750 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
751 int hdrlen
= ipv6_skip_exthdr(skb
,
752 sizeof(struct ipv6hdr
),
753 &nexthdr
, &frag_off
);
755 if (hdrlen
>= 0 && nexthdr
== IPPROTO_ICMPV6
) {
756 if (!nf_nat_icmpv6_reply_translation(skb
, ct
,
764 /* Non-ICMP, fall thru to initialize if needed. */
767 /* Seen it before? This can happen for loopback, retrans,
770 if (!nf_nat_initialized(ct
, maniptype
)) {
771 /* Initialize according to the NAT action. */
772 err
= (range
&& range
->flags
& NF_NAT_RANGE_MAP_IPS
)
773 /* Action is set up to establish a new
776 ? nf_nat_setup_info(ct
, range
, maniptype
)
777 : nf_nat_alloc_null_binding(ct
, hooknum
);
778 if (err
!= NF_ACCEPT
)
783 case IP_CT_ESTABLISHED
:
784 case IP_CT_ESTABLISHED_REPLY
:
792 err
= nf_nat_packet(ct
, ctinfo
, hooknum
, skb
);
794 skb_push(skb
, nh_off
);
795 skb_postpush_rcsum(skb
, skb
->data
, nh_off
);
800 static void ovs_nat_update_key(struct sw_flow_key
*key
,
801 const struct sk_buff
*skb
,
802 enum nf_nat_manip_type maniptype
)
804 if (maniptype
== NF_NAT_MANIP_SRC
) {
807 key
->ct_state
|= OVS_CS_F_SRC_NAT
;
808 if (key
->eth
.type
== htons(ETH_P_IP
))
809 key
->ipv4
.addr
.src
= ip_hdr(skb
)->saddr
;
810 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
811 memcpy(&key
->ipv6
.addr
.src
, &ipv6_hdr(skb
)->saddr
,
812 sizeof(key
->ipv6
.addr
.src
));
816 if (key
->ip
.proto
== IPPROTO_UDP
)
817 src
= udp_hdr(skb
)->source
;
818 else if (key
->ip
.proto
== IPPROTO_TCP
)
819 src
= tcp_hdr(skb
)->source
;
820 else if (key
->ip
.proto
== IPPROTO_SCTP
)
821 src
= sctp_hdr(skb
)->source
;
829 key
->ct_state
|= OVS_CS_F_DST_NAT
;
830 if (key
->eth
.type
== htons(ETH_P_IP
))
831 key
->ipv4
.addr
.dst
= ip_hdr(skb
)->daddr
;
832 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
833 memcpy(&key
->ipv6
.addr
.dst
, &ipv6_hdr(skb
)->daddr
,
834 sizeof(key
->ipv6
.addr
.dst
));
838 if (key
->ip
.proto
== IPPROTO_UDP
)
839 dst
= udp_hdr(skb
)->dest
;
840 else if (key
->ip
.proto
== IPPROTO_TCP
)
841 dst
= tcp_hdr(skb
)->dest
;
842 else if (key
->ip
.proto
== IPPROTO_SCTP
)
843 dst
= sctp_hdr(skb
)->dest
;
851 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
852 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
853 const struct ovs_conntrack_info
*info
,
854 struct sk_buff
*skb
, struct nf_conn
*ct
,
855 enum ip_conntrack_info ctinfo
)
857 enum nf_nat_manip_type maniptype
;
860 /* Add NAT extension if not confirmed yet. */
861 if (!nf_ct_is_confirmed(ct
) && !nf_ct_nat_ext_add(ct
))
862 return NF_ACCEPT
; /* Can't NAT. */
864 /* Determine NAT type.
865 * Check if the NAT type can be deduced from the tracked connection.
866 * Make sure new expected connections (IP_CT_RELATED) are NATted only
869 if (info
->nat
& OVS_CT_NAT
&& ctinfo
!= IP_CT_NEW
&&
870 ct
->status
& IPS_NAT_MASK
&&
871 (ctinfo
!= IP_CT_RELATED
|| info
->commit
)) {
872 /* NAT an established or related connection like before. */
873 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
)
874 /* This is the REPLY direction for a connection
875 * for which NAT was applied in the forward
876 * direction. Do the reverse NAT.
878 maniptype
= ct
->status
& IPS_SRC_NAT
879 ? NF_NAT_MANIP_DST
: NF_NAT_MANIP_SRC
;
881 maniptype
= ct
->status
& IPS_SRC_NAT
882 ? NF_NAT_MANIP_SRC
: NF_NAT_MANIP_DST
;
883 } else if (info
->nat
& OVS_CT_SRC_NAT
) {
884 maniptype
= NF_NAT_MANIP_SRC
;
885 } else if (info
->nat
& OVS_CT_DST_NAT
) {
886 maniptype
= NF_NAT_MANIP_DST
;
888 return NF_ACCEPT
; /* Connection is not NATed. */
890 err
= ovs_ct_nat_execute(skb
, ct
, ctinfo
, &info
->range
, maniptype
);
892 /* Mark NAT done if successful and update the flow key. */
893 if (err
== NF_ACCEPT
)
894 ovs_nat_update_key(key
, skb
, maniptype
);
898 #else /* !CONFIG_NF_NAT */
899 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
900 const struct ovs_conntrack_info
*info
,
901 struct sk_buff
*skb
, struct nf_conn
*ct
,
902 enum ip_conntrack_info ctinfo
)
908 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
909 * not done already. Update key with new CT state after passing the packet
911 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
912 * set to NULL and 0 will be returned.
914 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
915 const struct ovs_conntrack_info
*info
,
918 /* If we are recirculating packets to match on conntrack fields and
919 * committing with a separate conntrack action, then we don't need to
920 * actually run the packet through conntrack twice unless it's for a
923 bool cached
= skb_nfct_cached(net
, key
, info
, skb
);
924 enum ip_conntrack_info ctinfo
;
928 struct nf_hook_state state
= {
929 .hook
= NF_INET_PRE_ROUTING
,
933 struct nf_conn
*tmpl
= info
->ct
;
936 /* Associate skb with specified zone. */
939 nf_conntrack_put(skb_nfct(skb
));
940 nf_conntrack_get(&tmpl
->ct_general
);
941 nf_ct_set(skb
, tmpl
, IP_CT_NEW
);
944 err
= nf_conntrack_in(skb
, &state
);
945 if (err
!= NF_ACCEPT
)
948 /* Clear CT state NAT flags to mark that we have not yet done
949 * NAT after the nf_conntrack_in() call. We can actually clear
950 * the whole state, as it will be re-initialized below.
954 /* Update the key, but keep the NAT flags. */
955 ovs_ct_update_key(skb
, info
, key
, true, true);
958 ct
= nf_ct_get(skb
, &ctinfo
);
960 /* Packets starting a new connection must be NATted before the
961 * helper, so that the helper knows about the NAT. We enforce
962 * this by delaying both NAT and helper calls for unconfirmed
963 * connections until the committing CT action. For later
964 * packets NAT and Helper may be called in either order.
966 * NAT will be done only if the CT action has NAT, and only
967 * once per packet (per zone), as guarded by the NAT bits in
970 if (info
->nat
&& !(key
->ct_state
& OVS_CS_F_NAT_MASK
) &&
971 (nf_ct_is_confirmed(ct
) || info
->commit
) &&
972 ovs_ct_nat(net
, key
, info
, skb
, ct
, ctinfo
) != NF_ACCEPT
) {
976 /* Userspace may decide to perform a ct lookup without a helper
977 * specified followed by a (recirculate and) commit with one.
978 * Therefore, for unconfirmed connections which we will commit,
979 * we need to attach the helper here.
981 if (!nf_ct_is_confirmed(ct
) && info
->commit
&&
982 info
->helper
&& !nfct_help(ct
)) {
983 int err
= __nf_ct_try_assign_helper(ct
, info
->ct
,
988 /* helper installed, add seqadj if NAT is required */
989 if (info
->nat
&& !nfct_seqadj(ct
)) {
990 if (!nfct_seqadj_ext_add(ct
))
995 /* Call the helper only if:
996 * - nf_conntrack_in() was executed above ("!cached") for a
997 * confirmed connection, or
998 * - When committing an unconfirmed connection.
1000 if ((nf_ct_is_confirmed(ct
) ? !cached
: info
->commit
) &&
1001 ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
1009 /* Lookup connection and read fields into key. */
1010 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
1011 const struct ovs_conntrack_info
*info
,
1012 struct sk_buff
*skb
)
1014 struct nf_conntrack_expect
*exp
;
1016 /* If we pass an expected packet through nf_conntrack_in() the
1017 * expectation is typically removed, but the packet could still be
1018 * lost in upcall processing. To prevent this from happening we
1019 * perform an explicit expectation lookup. Expected connections are
1020 * always new, and will be passed through conntrack only when they are
1021 * committed, as it is OK to remove the expectation at that time.
1023 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
1027 /* NOTE: New connections are NATted and Helped only when
1028 * committed, so we are not calling into NAT here.
1030 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
1031 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
1036 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1040 ct
= (struct nf_conn
*)skb_nfct(skb
);
1042 nf_ct_deliver_cached_events(ct
);
1048 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
1052 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
1053 if (labels
->ct_labels_32
[i
])
1059 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1060 static struct hlist_head
*ct_limit_hash_bucket(
1061 const struct ovs_ct_limit_info
*info
, u16 zone
)
1063 return &info
->limits
[zone
& (CT_LIMIT_HASH_BUCKETS
- 1)];
1066 /* Call with ovs_mutex */
1067 static void ct_limit_set(const struct ovs_ct_limit_info
*info
,
1068 struct ovs_ct_limit
*new_ct_limit
)
1070 struct ovs_ct_limit
*ct_limit
;
1071 struct hlist_head
*head
;
1073 head
= ct_limit_hash_bucket(info
, new_ct_limit
->zone
);
1074 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1075 if (ct_limit
->zone
== new_ct_limit
->zone
) {
1076 hlist_replace_rcu(&ct_limit
->hlist_node
,
1077 &new_ct_limit
->hlist_node
);
1078 kfree_rcu(ct_limit
, rcu
);
1083 hlist_add_head_rcu(&new_ct_limit
->hlist_node
, head
);
1086 /* Call with ovs_mutex */
1087 static void ct_limit_del(const struct ovs_ct_limit_info
*info
, u16 zone
)
1089 struct ovs_ct_limit
*ct_limit
;
1090 struct hlist_head
*head
;
1091 struct hlist_node
*n
;
1093 head
= ct_limit_hash_bucket(info
, zone
);
1094 hlist_for_each_entry_safe(ct_limit
, n
, head
, hlist_node
) {
1095 if (ct_limit
->zone
== zone
) {
1096 hlist_del_rcu(&ct_limit
->hlist_node
);
1097 kfree_rcu(ct_limit
, rcu
);
1103 /* Call with RCU read lock */
1104 static u32
ct_limit_get(const struct ovs_ct_limit_info
*info
, u16 zone
)
1106 struct ovs_ct_limit
*ct_limit
;
1107 struct hlist_head
*head
;
1109 head
= ct_limit_hash_bucket(info
, zone
);
1110 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1111 if (ct_limit
->zone
== zone
)
1112 return ct_limit
->limit
;
1115 return info
->default_limit
;
1118 static int ovs_ct_check_limit(struct net
*net
,
1119 const struct ovs_conntrack_info
*info
,
1120 const struct nf_conntrack_tuple
*tuple
)
1122 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1123 const struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
1124 u32 per_zone_limit
, connections
;
1127 conncount_key
= info
->zone
.id
;
1129 per_zone_limit
= ct_limit_get(ct_limit_info
, info
->zone
.id
);
1130 if (per_zone_limit
== OVS_CT_LIMIT_UNLIMITED
)
1133 connections
= nf_conncount_count(net
, ct_limit_info
->data
,
1134 &conncount_key
, tuple
, &info
->zone
);
1135 if (connections
> per_zone_limit
)
1142 /* Lookup connection and confirm if unconfirmed. */
1143 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
1144 const struct ovs_conntrack_info
*info
,
1145 struct sk_buff
*skb
)
1147 enum ip_conntrack_info ctinfo
;
1151 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1155 /* The connection could be invalid, in which case this is a no-op.*/
1156 ct
= nf_ct_get(skb
, &ctinfo
);
1160 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1161 if (static_branch_unlikely(&ovs_ct_limit_enabled
)) {
1162 if (!nf_ct_is_confirmed(ct
)) {
1163 err
= ovs_ct_check_limit(net
, info
,
1164 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
1166 net_warn_ratelimited("openvswitch: zone: %u "
1167 "exceeds conntrack limit\n",
1175 /* Set the conntrack event mask if given. NEW and DELETE events have
1176 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1177 * typically would receive many kinds of updates. Setting the event
1178 * mask allows those events to be filtered. The set event mask will
1179 * remain in effect for the lifetime of the connection unless changed
1180 * by a further CT action with both the commit flag and the eventmask
1182 if (info
->have_eventmask
) {
1183 struct nf_conntrack_ecache
*cache
= nf_ct_ecache_find(ct
);
1186 cache
->ctmask
= info
->eventmask
;
1189 /* Apply changes before confirming the connection so that the initial
1190 * conntrack NEW netlink event carries the values given in the CT
1193 if (info
->mark
.mask
) {
1194 err
= ovs_ct_set_mark(ct
, key
, info
->mark
.value
,
1199 if (!nf_ct_is_confirmed(ct
)) {
1200 err
= ovs_ct_init_labels(ct
, key
, &info
->labels
.value
,
1201 &info
->labels
.mask
);
1204 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1205 labels_nonzero(&info
->labels
.mask
)) {
1206 err
= ovs_ct_set_labels(ct
, key
, &info
->labels
.value
,
1207 &info
->labels
.mask
);
1211 /* This will take care of sending queued events even if the connection
1212 * is already confirmed.
1214 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
1220 /* Trim the skb to the length specified by the IP/IPv6 header,
1221 * removing any trailing lower-layer padding. This prepares the skb
1222 * for higher-layer processing that assumes skb->len excludes padding
1223 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1224 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1226 static int ovs_skb_network_trim(struct sk_buff
*skb
)
1231 switch (skb
->protocol
) {
1232 case htons(ETH_P_IP
):
1233 len
= ntohs(ip_hdr(skb
)->tot_len
);
1235 case htons(ETH_P_IPV6
):
1236 len
= sizeof(struct ipv6hdr
)
1237 + ntohs(ipv6_hdr(skb
)->payload_len
);
1243 err
= pskb_trim_rcsum(skb
, len
);
1250 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1251 * value if 'skb' is freed.
1253 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
1254 struct sw_flow_key
*key
,
1255 const struct ovs_conntrack_info
*info
)
1260 /* The conntrack module expects to be working at L3. */
1261 nh_ofs
= skb_network_offset(skb
);
1262 skb_pull_rcsum(skb
, nh_ofs
);
1264 err
= ovs_skb_network_trim(skb
);
1268 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
1269 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
1275 err
= ovs_ct_commit(net
, key
, info
, skb
);
1277 err
= ovs_ct_lookup(net
, key
, info
, skb
);
1279 skb_push(skb
, nh_ofs
);
1280 skb_postpush_rcsum(skb
, skb
->data
, nh_ofs
);
1286 int ovs_ct_clear(struct sk_buff
*skb
, struct sw_flow_key
*key
)
1288 if (skb_nfct(skb
)) {
1289 nf_conntrack_put(skb_nfct(skb
));
1290 nf_ct_set(skb
, NULL
, IP_CT_UNTRACKED
);
1291 ovs_ct_fill_key(skb
, key
);
1297 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
1298 const struct sw_flow_key
*key
, bool log
)
1300 struct nf_conntrack_helper
*helper
;
1301 struct nf_conn_help
*help
;
1304 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
1307 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
1311 help
= nf_ct_helper_ext_add(info
->ct
, GFP_KERNEL
);
1313 nf_conntrack_helper_put(helper
);
1317 #if IS_ENABLED(CONFIG_NF_NAT)
1319 ret
= nf_nat_helper_try_module_get(name
, info
->family
,
1322 nf_conntrack_helper_put(helper
);
1323 OVS_NLERR(log
, "Failed to load \"%s\" NAT helper, error: %d",
1329 rcu_assign_pointer(help
->helper
, helper
);
1330 info
->helper
= helper
;
1334 #if IS_ENABLED(CONFIG_NF_NAT)
1335 static int parse_nat(const struct nlattr
*attr
,
1336 struct ovs_conntrack_info
*info
, bool log
)
1340 bool have_ip_max
= false;
1341 bool have_proto_max
= false;
1342 bool ip_vers
= (info
->family
== NFPROTO_IPV6
);
1344 nla_for_each_nested(a
, attr
, rem
) {
1345 static const int ovs_nat_attr_lens
[OVS_NAT_ATTR_MAX
+ 1][2] = {
1346 [OVS_NAT_ATTR_SRC
] = {0, 0},
1347 [OVS_NAT_ATTR_DST
] = {0, 0},
1348 [OVS_NAT_ATTR_IP_MIN
] = {sizeof(struct in_addr
),
1349 sizeof(struct in6_addr
)},
1350 [OVS_NAT_ATTR_IP_MAX
] = {sizeof(struct in_addr
),
1351 sizeof(struct in6_addr
)},
1352 [OVS_NAT_ATTR_PROTO_MIN
] = {sizeof(u16
), sizeof(u16
)},
1353 [OVS_NAT_ATTR_PROTO_MAX
] = {sizeof(u16
), sizeof(u16
)},
1354 [OVS_NAT_ATTR_PERSISTENT
] = {0, 0},
1355 [OVS_NAT_ATTR_PROTO_HASH
] = {0, 0},
1356 [OVS_NAT_ATTR_PROTO_RANDOM
] = {0, 0},
1358 int type
= nla_type(a
);
1360 if (type
> OVS_NAT_ATTR_MAX
) {
1361 OVS_NLERR(log
, "Unknown NAT attribute (type=%d, max=%d)",
1362 type
, OVS_NAT_ATTR_MAX
);
1366 if (nla_len(a
) != ovs_nat_attr_lens
[type
][ip_vers
]) {
1367 OVS_NLERR(log
, "NAT attribute type %d has unexpected length (%d != %d)",
1369 ovs_nat_attr_lens
[type
][ip_vers
]);
1374 case OVS_NAT_ATTR_SRC
:
1375 case OVS_NAT_ATTR_DST
:
1377 OVS_NLERR(log
, "Only one type of NAT may be specified");
1380 info
->nat
|= OVS_CT_NAT
;
1381 info
->nat
|= ((type
== OVS_NAT_ATTR_SRC
)
1382 ? OVS_CT_SRC_NAT
: OVS_CT_DST_NAT
);
1385 case OVS_NAT_ATTR_IP_MIN
:
1386 nla_memcpy(&info
->range
.min_addr
, a
,
1387 sizeof(info
->range
.min_addr
));
1388 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1391 case OVS_NAT_ATTR_IP_MAX
:
1393 nla_memcpy(&info
->range
.max_addr
, a
,
1394 sizeof(info
->range
.max_addr
));
1395 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1398 case OVS_NAT_ATTR_PROTO_MIN
:
1399 info
->range
.min_proto
.all
= htons(nla_get_u16(a
));
1400 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1403 case OVS_NAT_ATTR_PROTO_MAX
:
1404 have_proto_max
= true;
1405 info
->range
.max_proto
.all
= htons(nla_get_u16(a
));
1406 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1409 case OVS_NAT_ATTR_PERSISTENT
:
1410 info
->range
.flags
|= NF_NAT_RANGE_PERSISTENT
;
1413 case OVS_NAT_ATTR_PROTO_HASH
:
1414 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1417 case OVS_NAT_ATTR_PROTO_RANDOM
:
1418 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM_FULLY
;
1422 OVS_NLERR(log
, "Unknown nat attribute (%d)", type
);
1428 OVS_NLERR(log
, "NAT attribute has %d unknown bytes", rem
);
1432 /* Do not allow flags if no type is given. */
1433 if (info
->range
.flags
) {
1435 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1439 info
->nat
= OVS_CT_NAT
; /* NAT existing connections. */
1440 } else if (!info
->commit
) {
1442 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1446 /* Allow missing IP_MAX. */
1447 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
&& !have_ip_max
) {
1448 memcpy(&info
->range
.max_addr
, &info
->range
.min_addr
,
1449 sizeof(info
->range
.max_addr
));
1451 /* Allow missing PROTO_MAX. */
1452 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1454 info
->range
.max_proto
.all
= info
->range
.min_proto
.all
;
1460 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
1461 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1462 [OVS_CT_ATTR_FORCE_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1463 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
1464 .maxlen
= sizeof(u16
) },
1465 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
1466 .maxlen
= sizeof(struct md_mark
) },
1467 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
1468 .maxlen
= sizeof(struct md_labels
) },
1469 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
1470 .maxlen
= NF_CT_HELPER_NAME_LEN
},
1471 #if IS_ENABLED(CONFIG_NF_NAT)
1472 /* NAT length is checked when parsing the nested attributes. */
1473 [OVS_CT_ATTR_NAT
] = { .minlen
= 0, .maxlen
= INT_MAX
},
1475 [OVS_CT_ATTR_EVENTMASK
] = { .minlen
= sizeof(u32
),
1476 .maxlen
= sizeof(u32
) },
1477 [OVS_CT_ATTR_TIMEOUT
] = { .minlen
= 1,
1478 .maxlen
= CTNL_TIMEOUT_NAME_MAX
},
1481 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
1482 const char **helper
, bool log
)
1487 nla_for_each_nested(a
, attr
, rem
) {
1488 int type
= nla_type(a
);
1492 if (type
> OVS_CT_ATTR_MAX
) {
1494 "Unknown conntrack attr (type=%d, max=%d)",
1495 type
, OVS_CT_ATTR_MAX
);
1499 maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
1500 minlen
= ovs_ct_attr_lens
[type
].minlen
;
1501 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
1503 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1504 type
, nla_len(a
), maxlen
);
1509 case OVS_CT_ATTR_FORCE_COMMIT
:
1512 case OVS_CT_ATTR_COMMIT
:
1513 info
->commit
= true;
1515 #ifdef CONFIG_NF_CONNTRACK_ZONES
1516 case OVS_CT_ATTR_ZONE
:
1517 info
->zone
.id
= nla_get_u16(a
);
1520 #ifdef CONFIG_NF_CONNTRACK_MARK
1521 case OVS_CT_ATTR_MARK
: {
1522 struct md_mark
*mark
= nla_data(a
);
1525 OVS_NLERR(log
, "ct_mark mask cannot be 0");
1532 #ifdef CONFIG_NF_CONNTRACK_LABELS
1533 case OVS_CT_ATTR_LABELS
: {
1534 struct md_labels
*labels
= nla_data(a
);
1536 if (!labels_nonzero(&labels
->mask
)) {
1537 OVS_NLERR(log
, "ct_labels mask cannot be 0");
1540 info
->labels
= *labels
;
1544 case OVS_CT_ATTR_HELPER
:
1545 *helper
= nla_data(a
);
1546 if (!memchr(*helper
, '\0', nla_len(a
))) {
1547 OVS_NLERR(log
, "Invalid conntrack helper");
1551 #if IS_ENABLED(CONFIG_NF_NAT)
1552 case OVS_CT_ATTR_NAT
: {
1553 int err
= parse_nat(a
, info
, log
);
1560 case OVS_CT_ATTR_EVENTMASK
:
1561 info
->have_eventmask
= true;
1562 info
->eventmask
= nla_get_u32(a
);
1564 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1565 case OVS_CT_ATTR_TIMEOUT
:
1566 memcpy(info
->timeout
, nla_data(a
), nla_len(a
));
1567 if (!memchr(info
->timeout
, '\0', nla_len(a
))) {
1568 OVS_NLERR(log
, "Invalid conntrack helper");
1575 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
1581 #ifdef CONFIG_NF_CONNTRACK_MARK
1582 if (!info
->commit
&& info
->mark
.mask
) {
1584 "Setting conntrack mark requires 'commit' flag.");
1588 #ifdef CONFIG_NF_CONNTRACK_LABELS
1589 if (!info
->commit
&& labels_nonzero(&info
->labels
.mask
)) {
1591 "Setting conntrack labels requires 'commit' flag.");
1596 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
1603 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
1605 if (attr
== OVS_KEY_ATTR_CT_STATE
)
1607 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1608 attr
== OVS_KEY_ATTR_CT_ZONE
)
1610 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
1611 attr
== OVS_KEY_ATTR_CT_MARK
)
1613 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1614 attr
== OVS_KEY_ATTR_CT_LABELS
) {
1615 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1617 return ovs_net
->xt_label
;
1623 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
1624 const struct sw_flow_key
*key
,
1625 struct sw_flow_actions
**sfa
, bool log
)
1627 struct ovs_conntrack_info ct_info
;
1628 const char *helper
= NULL
;
1632 family
= key_to_nfproto(key
);
1633 if (family
== NFPROTO_UNSPEC
) {
1634 OVS_NLERR(log
, "ct family unspecified");
1638 memset(&ct_info
, 0, sizeof(ct_info
));
1639 ct_info
.family
= family
;
1641 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
1642 NF_CT_DEFAULT_ZONE_DIR
, 0);
1644 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
1648 /* Set up template for tracking connections in specific zones. */
1649 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
1651 OVS_NLERR(log
, "Failed to allocate conntrack template");
1655 if (ct_info
.timeout
[0]) {
1656 if (nf_ct_set_timeout(net
, ct_info
.ct
, family
, key
->ip
.proto
,
1658 pr_info_ratelimited("Failed to associated timeout "
1659 "policy `%s'\n", ct_info
.timeout
);
1663 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
1668 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
1669 sizeof(ct_info
), log
);
1673 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
1674 nf_conntrack_get(&ct_info
.ct
->ct_general
);
1677 __ovs_ct_free_action(&ct_info
);
1681 #if IS_ENABLED(CONFIG_NF_NAT)
1682 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info
*info
,
1683 struct sk_buff
*skb
)
1685 struct nlattr
*start
;
1687 start
= nla_nest_start_noflag(skb
, OVS_CT_ATTR_NAT
);
1691 if (info
->nat
& OVS_CT_SRC_NAT
) {
1692 if (nla_put_flag(skb
, OVS_NAT_ATTR_SRC
))
1694 } else if (info
->nat
& OVS_CT_DST_NAT
) {
1695 if (nla_put_flag(skb
, OVS_NAT_ATTR_DST
))
1701 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
) {
1702 if (IS_ENABLED(CONFIG_NF_NAT
) &&
1703 info
->family
== NFPROTO_IPV4
) {
1704 if (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1705 info
->range
.min_addr
.ip
) ||
1706 (info
->range
.max_addr
.ip
1707 != info
->range
.min_addr
.ip
&&
1708 (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1709 info
->range
.max_addr
.ip
))))
1711 } else if (IS_ENABLED(CONFIG_IPV6
) &&
1712 info
->family
== NFPROTO_IPV6
) {
1713 if (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1714 &info
->range
.min_addr
.in6
) ||
1715 (memcmp(&info
->range
.max_addr
.in6
,
1716 &info
->range
.min_addr
.in6
,
1717 sizeof(info
->range
.max_addr
.in6
)) &&
1718 (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1719 &info
->range
.max_addr
.in6
))))
1725 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1726 (nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MIN
,
1727 ntohs(info
->range
.min_proto
.all
)) ||
1728 (info
->range
.max_proto
.all
!= info
->range
.min_proto
.all
&&
1729 nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MAX
,
1730 ntohs(info
->range
.max_proto
.all
)))))
1733 if (info
->range
.flags
& NF_NAT_RANGE_PERSISTENT
&&
1734 nla_put_flag(skb
, OVS_NAT_ATTR_PERSISTENT
))
1736 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM
&&
1737 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_HASH
))
1739 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM_FULLY
&&
1740 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_RANDOM
))
1743 nla_nest_end(skb
, start
);
1749 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
1750 struct sk_buff
*skb
)
1752 struct nlattr
*start
;
1754 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_CT
);
1758 if (ct_info
->commit
&& nla_put_flag(skb
, ct_info
->force
1759 ? OVS_CT_ATTR_FORCE_COMMIT
1760 : OVS_CT_ATTR_COMMIT
))
1762 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1763 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
1765 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
1766 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
1769 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1770 labels_nonzero(&ct_info
->labels
.mask
) &&
1771 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
1774 if (ct_info
->helper
) {
1775 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
1776 ct_info
->helper
->name
))
1779 if (ct_info
->have_eventmask
&&
1780 nla_put_u32(skb
, OVS_CT_ATTR_EVENTMASK
, ct_info
->eventmask
))
1782 if (ct_info
->timeout
[0]) {
1783 if (nla_put_string(skb
, OVS_CT_ATTR_TIMEOUT
, ct_info
->timeout
))
1787 #if IS_ENABLED(CONFIG_NF_NAT)
1788 if (ct_info
->nat
&& !ovs_ct_nat_to_attr(ct_info
, skb
))
1791 nla_nest_end(skb
, start
);
1796 void ovs_ct_free_action(const struct nlattr
*a
)
1798 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
1800 __ovs_ct_free_action(ct_info
);
1803 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
)
1805 if (ct_info
->helper
) {
1806 #if IS_ENABLED(CONFIG_NF_NAT)
1808 nf_nat_helper_put(ct_info
->helper
);
1810 nf_conntrack_helper_put(ct_info
->helper
);
1813 if (ct_info
->timeout
[0])
1814 nf_ct_destroy_timeout(ct_info
->ct
);
1815 nf_ct_tmpl_free(ct_info
->ct
);
1819 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1820 static int ovs_ct_limit_init(struct net
*net
, struct ovs_net
*ovs_net
)
1824 ovs_net
->ct_limit_info
= kmalloc(sizeof(*ovs_net
->ct_limit_info
),
1826 if (!ovs_net
->ct_limit_info
)
1829 ovs_net
->ct_limit_info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1830 ovs_net
->ct_limit_info
->limits
=
1831 kmalloc_array(CT_LIMIT_HASH_BUCKETS
, sizeof(struct hlist_head
),
1833 if (!ovs_net
->ct_limit_info
->limits
) {
1834 kfree(ovs_net
->ct_limit_info
);
1838 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; i
++)
1839 INIT_HLIST_HEAD(&ovs_net
->ct_limit_info
->limits
[i
]);
1841 ovs_net
->ct_limit_info
->data
=
1842 nf_conncount_init(net
, NFPROTO_INET
, sizeof(u32
));
1844 if (IS_ERR(ovs_net
->ct_limit_info
->data
)) {
1845 err
= PTR_ERR(ovs_net
->ct_limit_info
->data
);
1846 kfree(ovs_net
->ct_limit_info
->limits
);
1847 kfree(ovs_net
->ct_limit_info
);
1848 pr_err("openvswitch: failed to init nf_conncount %d\n", err
);
1854 static void ovs_ct_limit_exit(struct net
*net
, struct ovs_net
*ovs_net
)
1856 const struct ovs_ct_limit_info
*info
= ovs_net
->ct_limit_info
;
1859 nf_conncount_destroy(net
, NFPROTO_INET
, info
->data
);
1860 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
1861 struct hlist_head
*head
= &info
->limits
[i
];
1862 struct ovs_ct_limit
*ct_limit
;
1864 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
)
1865 kfree_rcu(ct_limit
, rcu
);
1867 kfree(ovs_net
->ct_limit_info
->limits
);
1868 kfree(ovs_net
->ct_limit_info
);
1871 static struct sk_buff
*
1872 ovs_ct_limit_cmd_reply_start(struct genl_info
*info
, u8 cmd
,
1873 struct ovs_header
**ovs_reply_header
)
1875 struct ovs_header
*ovs_header
= info
->userhdr
;
1876 struct sk_buff
*skb
;
1878 skb
= genlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1880 return ERR_PTR(-ENOMEM
);
1882 *ovs_reply_header
= genlmsg_put(skb
, info
->snd_portid
,
1884 &dp_ct_limit_genl_family
, 0, cmd
);
1886 if (!*ovs_reply_header
) {
1888 return ERR_PTR(-EMSGSIZE
);
1890 (*ovs_reply_header
)->dp_ifindex
= ovs_header
->dp_ifindex
;
1895 static bool check_zone_id(int zone_id
, u16
*pzone
)
1897 if (zone_id
>= 0 && zone_id
<= 65535) {
1898 *pzone
= (u16
)zone_id
;
1904 static int ovs_ct_limit_set_zone_limit(struct nlattr
*nla_zone_limit
,
1905 struct ovs_ct_limit_info
*info
)
1907 struct ovs_zone_limit
*zone_limit
;
1911 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1912 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1914 while (rem
>= sizeof(*zone_limit
)) {
1915 if (unlikely(zone_limit
->zone_id
==
1916 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1918 info
->default_limit
= zone_limit
->limit
;
1920 } else if (unlikely(!check_zone_id(
1921 zone_limit
->zone_id
, &zone
))) {
1922 OVS_NLERR(true, "zone id is out of range");
1924 struct ovs_ct_limit
*ct_limit
;
1926 ct_limit
= kmalloc(sizeof(*ct_limit
), GFP_KERNEL
);
1930 ct_limit
->zone
= zone
;
1931 ct_limit
->limit
= zone_limit
->limit
;
1934 ct_limit_set(info
, ct_limit
);
1937 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
1938 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
1939 NLA_ALIGN(sizeof(*zone_limit
)));
1943 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem
);
1948 static int ovs_ct_limit_del_zone_limit(struct nlattr
*nla_zone_limit
,
1949 struct ovs_ct_limit_info
*info
)
1951 struct ovs_zone_limit
*zone_limit
;
1955 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1956 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1958 while (rem
>= sizeof(*zone_limit
)) {
1959 if (unlikely(zone_limit
->zone_id
==
1960 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1962 info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1964 } else if (unlikely(!check_zone_id(
1965 zone_limit
->zone_id
, &zone
))) {
1966 OVS_NLERR(true, "zone id is out of range");
1969 ct_limit_del(info
, zone
);
1972 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
1973 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
1974 NLA_ALIGN(sizeof(*zone_limit
)));
1978 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem
);
1983 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info
*info
,
1984 struct sk_buff
*reply
)
1986 struct ovs_zone_limit zone_limit
;
1989 zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
1990 zone_limit
.limit
= info
->default_limit
;
1991 err
= nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
1998 static int __ovs_ct_limit_get_zone_limit(struct net
*net
,
1999 struct nf_conncount_data
*data
,
2000 u16 zone_id
, u32 limit
,
2001 struct sk_buff
*reply
)
2003 struct nf_conntrack_zone ct_zone
;
2004 struct ovs_zone_limit zone_limit
;
2005 u32 conncount_key
= zone_id
;
2007 zone_limit
.zone_id
= zone_id
;
2008 zone_limit
.limit
= limit
;
2009 nf_ct_zone_init(&ct_zone
, zone_id
, NF_CT_DEFAULT_ZONE_DIR
, 0);
2011 zone_limit
.count
= nf_conncount_count(net
, data
, &conncount_key
, NULL
,
2013 return nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2016 static int ovs_ct_limit_get_zone_limit(struct net
*net
,
2017 struct nlattr
*nla_zone_limit
,
2018 struct ovs_ct_limit_info
*info
,
2019 struct sk_buff
*reply
)
2021 struct ovs_zone_limit
*zone_limit
;
2026 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2027 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2029 while (rem
>= sizeof(*zone_limit
)) {
2030 if (unlikely(zone_limit
->zone_id
==
2031 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2032 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2035 } else if (unlikely(!check_zone_id(zone_limit
->zone_id
,
2037 OVS_NLERR(true, "zone id is out of range");
2040 limit
= ct_limit_get(info
, zone
);
2043 err
= __ovs_ct_limit_get_zone_limit(
2044 net
, info
->data
, zone
, limit
, reply
);
2048 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2049 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2050 NLA_ALIGN(sizeof(*zone_limit
)));
2054 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem
);
2059 static int ovs_ct_limit_get_all_zone_limit(struct net
*net
,
2060 struct ovs_ct_limit_info
*info
,
2061 struct sk_buff
*reply
)
2063 struct ovs_ct_limit
*ct_limit
;
2064 struct hlist_head
*head
;
2067 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2072 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
2073 head
= &info
->limits
[i
];
2074 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
2075 err
= __ovs_ct_limit_get_zone_limit(net
, info
->data
,
2076 ct_limit
->zone
, ct_limit
->limit
, reply
);
2087 static int ovs_ct_limit_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2089 struct nlattr
**a
= info
->attrs
;
2090 struct sk_buff
*reply
;
2091 struct ovs_header
*ovs_reply_header
;
2092 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2093 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2096 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_SET
,
2099 return PTR_ERR(reply
);
2101 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2106 err
= ovs_ct_limit_set_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2111 static_branch_enable(&ovs_ct_limit_enabled
);
2113 genlmsg_end(reply
, ovs_reply_header
);
2114 return genlmsg_reply(reply
, info
);
2121 static int ovs_ct_limit_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2123 struct nlattr
**a
= info
->attrs
;
2124 struct sk_buff
*reply
;
2125 struct ovs_header
*ovs_reply_header
;
2126 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2127 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2130 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_DEL
,
2133 return PTR_ERR(reply
);
2135 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2140 err
= ovs_ct_limit_del_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2145 genlmsg_end(reply
, ovs_reply_header
);
2146 return genlmsg_reply(reply
, info
);
2153 static int ovs_ct_limit_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2155 struct nlattr
**a
= info
->attrs
;
2156 struct nlattr
*nla_reply
;
2157 struct sk_buff
*reply
;
2158 struct ovs_header
*ovs_reply_header
;
2159 struct net
*net
= sock_net(skb
->sk
);
2160 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2161 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2164 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_GET
,
2167 return PTR_ERR(reply
);
2169 nla_reply
= nla_nest_start_noflag(reply
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2175 if (a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2176 err
= ovs_ct_limit_get_zone_limit(
2177 net
, a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
], ct_limit_info
,
2182 err
= ovs_ct_limit_get_all_zone_limit(net
, ct_limit_info
,
2188 nla_nest_end(reply
, nla_reply
);
2189 genlmsg_end(reply
, ovs_reply_header
);
2190 return genlmsg_reply(reply
, info
);
2197 static struct genl_ops ct_limit_genl_ops
[] = {
2198 { .cmd
= OVS_CT_LIMIT_CMD_SET
,
2199 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2200 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2202 .doit
= ovs_ct_limit_cmd_set
,
2204 { .cmd
= OVS_CT_LIMIT_CMD_DEL
,
2205 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2206 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2208 .doit
= ovs_ct_limit_cmd_del
,
2210 { .cmd
= OVS_CT_LIMIT_CMD_GET
,
2211 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2212 .flags
= 0, /* OK for unprivileged users. */
2213 .doit
= ovs_ct_limit_cmd_get
,
2217 static const struct genl_multicast_group ovs_ct_limit_multicast_group
= {
2218 .name
= OVS_CT_LIMIT_MCGROUP
,
2221 struct genl_family dp_ct_limit_genl_family __ro_after_init
= {
2222 .hdrsize
= sizeof(struct ovs_header
),
2223 .name
= OVS_CT_LIMIT_FAMILY
,
2224 .version
= OVS_CT_LIMIT_VERSION
,
2225 .maxattr
= OVS_CT_LIMIT_ATTR_MAX
,
2226 .policy
= ct_limit_policy
,
2228 .parallel_ops
= true,
2229 .ops
= ct_limit_genl_ops
,
2230 .n_ops
= ARRAY_SIZE(ct_limit_genl_ops
),
2231 .mcgrps
= &ovs_ct_limit_multicast_group
,
2233 .module
= THIS_MODULE
,
2237 int ovs_ct_init(struct net
*net
)
2239 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
2240 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2242 if (nf_connlabels_get(net
, n_bits
- 1)) {
2243 ovs_net
->xt_label
= false;
2244 OVS_NLERR(true, "Failed to set connlabel length");
2246 ovs_net
->xt_label
= true;
2249 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2250 return ovs_ct_limit_init(net
, ovs_net
);
2256 void ovs_ct_exit(struct net
*net
)
2258 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2260 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2261 ovs_ct_limit_exit(net
, ovs_net
);
2264 if (ovs_net
->xt_label
)
2265 nf_connlabels_put(net
);