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>
34 #include <net/ipv6_frag.h>
36 #ifdef CONFIG_NF_NAT_NEEDED
37 #include <linux/netfilter/nf_nat.h>
38 #include <net/netfilter/nf_nat_core.h>
39 #include <net/netfilter/nf_nat_l3proto.h>
43 #include "conntrack.h"
45 #include "flow_netlink.h"
48 #ifndef HAVE_NF_NAT_RANGE2
49 #define nf_nat_range2 nf_nat_range
52 struct ovs_ct_len_tbl
{
57 /* Metadata mark for masked write to conntrack mark */
63 /* Metadata label for masked write to conntrack label. */
65 struct ovs_key_ct_labels value
;
66 struct ovs_key_ct_labels mask
;
70 OVS_CT_NAT
= 1 << 0, /* NAT for committed connections only. */
71 OVS_CT_SRC_NAT
= 1 << 1, /* Source NAT for NEW connections. */
72 OVS_CT_DST_NAT
= 1 << 2, /* Destination NAT for NEW connections. */
75 /* Conntrack action context for execution. */
76 struct ovs_conntrack_info
{
77 struct nf_conntrack_helper
*helper
;
78 struct nf_conntrack_zone zone
;
81 u8 nat
: 3; /* enum ovs_ct_nat */
82 u8 random_fully_compat
: 1; /* bool */
84 u8 have_eventmask
: 1;
86 u32 eventmask
; /* Mask of 1 << IPCT_*. */
88 struct md_labels labels
;
89 #ifdef CONFIG_NF_NAT_NEEDED
90 struct nf_nat_range2 range
; /* Only present for SRC NAT and DST NAT. */
94 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
95 #define OVS_CT_LIMIT_UNLIMITED 0
96 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
97 #define CT_LIMIT_HASH_BUCKETS 512
98 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled
);
100 struct ovs_ct_limit
{
101 /* Elements in ovs_ct_limit_info->limits hash table */
102 struct hlist_node hlist_node
;
108 struct ovs_ct_limit_info
{
110 struct hlist_head
*limits
;
111 struct nf_conncount_data
*data
;
114 static const struct nla_policy ct_limit_policy
[OVS_CT_LIMIT_ATTR_MAX
+ 1] = {
115 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NLA_NESTED
, },
119 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
);
121 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
);
123 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
125 switch (ntohs(key
->eth
.type
)) {
131 return NFPROTO_UNSPEC
;
135 /* Map SKB connection state into the values used by flow definition. */
136 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
138 u8 ct_state
= OVS_CS_F_TRACKED
;
141 case IP_CT_ESTABLISHED_REPLY
:
142 case IP_CT_RELATED_REPLY
:
143 ct_state
|= OVS_CS_F_REPLY_DIR
;
150 case IP_CT_ESTABLISHED
:
151 case IP_CT_ESTABLISHED_REPLY
:
152 ct_state
|= OVS_CS_F_ESTABLISHED
;
155 case IP_CT_RELATED_REPLY
:
156 ct_state
|= OVS_CS_F_RELATED
;
159 ct_state
|= OVS_CS_F_NEW
;
168 static u32
ovs_ct_get_mark(const struct nf_conn
*ct
)
170 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
171 return ct
? ct
->mark
: 0;
177 /* Guard against conntrack labels max size shrinking below 128 bits. */
178 #if NF_CT_LABELS_MAX_SIZE < 16
179 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
182 static void ovs_ct_get_labels(const struct nf_conn
*ct
,
183 struct ovs_key_ct_labels
*labels
)
185 struct nf_conn_labels
*cl
= ct
? nf_ct_labels_find(ct
) : NULL
;
188 memcpy(labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
190 memset(labels
, 0, OVS_CT_LABELS_LEN
);
193 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key
*key
,
194 const struct nf_conntrack_tuple
*orig
,
197 key
->ct_orig_proto
= orig
->dst
.protonum
;
198 if (orig
->dst
.protonum
== icmp_proto
) {
199 key
->ct
.orig_tp
.src
= htons(orig
->dst
.u
.icmp
.type
);
200 key
->ct
.orig_tp
.dst
= htons(orig
->dst
.u
.icmp
.code
);
202 key
->ct
.orig_tp
.src
= orig
->src
.u
.all
;
203 key
->ct
.orig_tp
.dst
= orig
->dst
.u
.all
;
207 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
208 const struct nf_conntrack_zone
*zone
,
209 const struct nf_conn
*ct
)
211 key
->ct_state
= state
;
212 key
->ct_zone
= zone
->id
;
213 key
->ct
.mark
= ovs_ct_get_mark(ct
);
214 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
217 const struct nf_conntrack_tuple
*orig
;
219 /* Use the master if we have one. */
222 orig
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
224 /* IP version must match with the master connection. */
225 if (key
->eth
.type
== htons(ETH_P_IP
) &&
226 nf_ct_l3num(ct
) == NFPROTO_IPV4
) {
227 key
->ipv4
.ct_orig
.src
= orig
->src
.u3
.ip
;
228 key
->ipv4
.ct_orig
.dst
= orig
->dst
.u3
.ip
;
229 __ovs_ct_update_key_orig_tp(key
, orig
, IPPROTO_ICMP
);
231 } else if (key
->eth
.type
== htons(ETH_P_IPV6
) &&
232 !sw_flow_key_is_nd(key
) &&
233 nf_ct_l3num(ct
) == NFPROTO_IPV6
) {
234 key
->ipv6
.ct_orig
.src
= orig
->src
.u3
.in6
;
235 key
->ipv6
.ct_orig
.dst
= orig
->dst
.u3
.in6
;
236 __ovs_ct_update_key_orig_tp(key
, orig
, NEXTHDR_ICMP
);
240 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
241 * original direction key fields.
243 key
->ct_orig_proto
= 0;
246 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
247 * previously sent the packet to conntrack via the ct action. If
248 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
249 * initialized from the connection status.
251 static void ovs_ct_update_key(const struct sk_buff
*skb
,
252 const struct ovs_conntrack_info
*info
,
253 struct sw_flow_key
*key
, bool post_ct
,
256 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
257 enum ip_conntrack_info ctinfo
;
261 ct
= nf_ct_get(skb
, &ctinfo
);
263 state
= ovs_ct_get_state(ctinfo
);
264 /* All unconfirmed entries are NEW connections. */
265 if (!nf_ct_is_confirmed(ct
))
266 state
|= OVS_CS_F_NEW
;
267 /* OVS persists the related flag for the duration of the
271 state
|= OVS_CS_F_RELATED
;
272 if (keep_nat_flags
) {
273 state
|= key
->ct_state
& OVS_CS_F_NAT_MASK
;
275 if (ct
->status
& IPS_SRC_NAT
)
276 state
|= OVS_CS_F_SRC_NAT
;
277 if (ct
->status
& IPS_DST_NAT
)
278 state
|= OVS_CS_F_DST_NAT
;
280 zone
= nf_ct_zone(ct
);
281 } else if (post_ct
) {
282 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
286 __ovs_ct_update_key(key
, state
, zone
, ct
);
289 /* This is called to initialize CT key fields possibly coming in from the local
292 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
294 ovs_ct_update_key(skb
, NULL
, key
, false, false);
297 #define IN6_ADDR_INITIALIZER(ADDR) \
298 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
299 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
301 int ovs_ct_put_key(const struct sw_flow_key
*swkey
,
302 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
304 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, output
->ct_state
))
307 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
308 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, output
->ct_zone
))
311 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
312 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, output
->ct
.mark
))
315 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
316 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(output
->ct
.labels
),
320 if (swkey
->ct_orig_proto
) {
321 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
322 struct ovs_key_ct_tuple_ipv4 orig
= {
323 output
->ipv4
.ct_orig
.src
,
324 output
->ipv4
.ct_orig
.dst
,
325 output
->ct
.orig_tp
.src
,
326 output
->ct
.orig_tp
.dst
,
327 output
->ct_orig_proto
,
329 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
330 sizeof(orig
), &orig
))
332 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
333 struct ovs_key_ct_tuple_ipv6 orig
= {
334 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.src
),
335 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.dst
),
336 output
->ct
.orig_tp
.src
,
337 output
->ct
.orig_tp
.dst
,
338 output
->ct_orig_proto
,
340 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
341 sizeof(orig
), &orig
))
349 static int ovs_ct_set_mark(struct nf_conn
*ct
, struct sw_flow_key
*key
,
350 u32 ct_mark
, u32 mask
)
352 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
355 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
356 if (ct
->mark
!= new_mark
) {
358 if (nf_ct_is_confirmed(ct
))
359 nf_conntrack_event_cache(IPCT_MARK
, ct
);
360 key
->ct
.mark
= new_mark
;
369 static struct nf_conn_labels
*ovs_ct_get_conn_labels(struct nf_conn
*ct
)
371 struct nf_conn_labels
*cl
;
373 cl
= nf_ct_labels_find(ct
);
375 nf_ct_labels_ext_add(ct
);
376 cl
= nf_ct_labels_find(ct
);
382 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
383 * since the new connection is not yet confirmed, and thus no-one else has
384 * access to it's labels, we simply write them over.
386 static int ovs_ct_init_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
387 const struct ovs_key_ct_labels
*labels
,
388 const struct ovs_key_ct_labels
*mask
)
390 struct nf_conn_labels
*cl
, *master_cl
;
391 bool have_mask
= labels_nonzero(mask
);
393 /* Inherit master's labels to the related connection? */
394 master_cl
= ct
->master
? nf_ct_labels_find(ct
->master
) : NULL
;
396 if (!master_cl
&& !have_mask
)
397 return 0; /* Nothing to do. */
399 cl
= ovs_ct_get_conn_labels(ct
);
403 /* Inherit the master's labels, if any. Must use memcpy for backport
404 * as struct assignment only copies the length field in older
408 memcpy(cl
->bits
, master_cl
->bits
, OVS_CT_LABELS_LEN
);
411 u32
*dst
= (u32
*)cl
->bits
;
414 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
415 dst
[i
] = (dst
[i
] & ~mask
->ct_labels_32
[i
]) |
416 (labels
->ct_labels_32
[i
]
417 & mask
->ct_labels_32
[i
]);
420 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
421 * IPCT_LABEL bit is set in the event cache.
423 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
425 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
430 static int ovs_ct_set_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
431 const struct ovs_key_ct_labels
*labels
,
432 const struct ovs_key_ct_labels
*mask
)
434 struct nf_conn_labels
*cl
;
437 cl
= ovs_ct_get_conn_labels(ct
);
441 err
= nf_connlabels_replace(ct
, labels
->ct_labels_32
,
443 OVS_CT_LABELS_LEN_32
);
447 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
452 /* 'skb' should already be pulled to nh_ofs. */
453 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
455 const struct nf_conntrack_helper
*helper
;
456 const struct nf_conn_help
*help
;
457 enum ip_conntrack_info ctinfo
;
458 unsigned int protoff
;
463 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
464 bool dst_set
= false;
465 struct rtable rt
= { .rt_flags
= 0 };
468 ct
= nf_ct_get(skb
, &ctinfo
);
469 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
472 help
= nfct_help(ct
);
476 helper
= rcu_dereference(help
->helper
);
482 protoff
= ip_hdrlen(skb
);
488 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
489 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
491 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
492 pr_debug("proto header not found\n");
499 WARN_ONCE(1, "helper invoked on non-IP family!");
503 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
504 /* Linux 4.5 and older depend on skb_dst being set when recalculating
505 * checksums after NAT helper has mangled TCP or UDP packet payload.
506 * skb_dst is cast to a rtable struct and the flags examined.
507 * Forcing these flags to have RTCF_LOCAL not set ensures checksum mod
508 * is carried out in the same way as kernel versions > 4.5
510 if (ct
->status
& IPS_NAT_MASK
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
513 skb_dst_set(skb
, &rt
.dst
);
516 err
= helper
->help(skb
, protoff
, ct
, ctinfo
);
517 if (err
!= NF_ACCEPT
)
520 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
522 skb_dst_set(skb
, NULL
);
525 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
526 * FTP with NAT) adusting the TCP payload size when mangling IP
527 * addresses and/or port numbers in the text-based control connection.
529 if (test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
) &&
530 !nf_ct_seq_adjust(skb
, ct
, ctinfo
, protoff
))
535 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
536 * value if 'skb' is freed.
538 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
539 u16 zone
, struct sk_buff
*skb
)
541 struct ovs_gso_cb ovs_cb
= *OVS_GSO_CB(skb
);
544 if (key
->eth
.type
== htons(ETH_P_IP
)) {
545 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
547 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
548 err
= ip_defrag(net
, skb
, user
);
552 ovs_cb
.dp_cb
.mru
= IPCB(skb
)->frag_max_size
;
553 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
554 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
555 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
557 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
558 err
= nf_ct_frag6_gather(net
, skb
, user
);
560 if (err
!= -EINPROGRESS
)
565 key
->ip
.proto
= ipv6_hdr(skb
)->nexthdr
;
566 ovs_cb
.dp_cb
.mru
= IP6CB(skb
)->frag_max_size
;
567 #endif /* IP frag support */
570 return -EPFNOSUPPORT
;
573 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
576 *OVS_GSO_CB(skb
) = ovs_cb
;
581 static struct nf_conntrack_expect
*
582 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
583 u16 proto
, const struct sk_buff
*skb
)
585 struct nf_conntrack_tuple tuple
;
586 struct nf_conntrack_expect
*exp
;
588 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
591 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
593 struct nf_conntrack_tuple_hash
*h
;
595 /* Delete existing conntrack entry, if it clashes with the
596 * expectation. This can happen since conntrack ALGs do not
597 * check for clashes between (new) expectations and existing
598 * conntrack entries. nf_conntrack_in() will check the
599 * expectations only if a conntrack entry can not be found,
600 * which can lead to OVS finding the expectation (here) in the
601 * init direction, but which will not be removed by the
602 * nf_conntrack_in() call, if a matching conntrack entry is
603 * found instead. In this case all init direction packets
604 * would be reported as new related packets, while reply
605 * direction packets would be reported as un-related
606 * established packets.
608 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
610 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
612 nf_ct_delete(ct
, 0, 0);
613 nf_conntrack_put(&ct
->ct_general
);
620 /* This replicates logic from nf_conntrack_core.c that is not exported. */
621 static enum ip_conntrack_info
622 ovs_ct_get_info(const struct nf_conntrack_tuple_hash
*h
)
624 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
626 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
627 return IP_CT_ESTABLISHED_REPLY
;
628 /* Once we've had two way comms, always ESTABLISHED. */
629 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
630 return IP_CT_ESTABLISHED
;
631 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
632 return IP_CT_RELATED
;
636 /* Find an existing connection which this packet belongs to without
637 * re-attributing statistics or modifying the connection state. This allows an
638 * skb->_nfct lost due to an upcall to be recovered during actions execution.
640 * Must be called with rcu_read_lock.
642 * On success, populates skb->_nfct and returns the connection. Returns NULL
643 * if there is no existing entry.
645 static struct nf_conn
*
646 ovs_ct_find_existing(struct net
*net
, const struct nf_conntrack_zone
*zone
,
647 u8 l3num
, struct sk_buff
*skb
, bool natted
)
649 struct nf_conntrack_tuple tuple
;
650 struct nf_conntrack_tuple_hash
*h
;
653 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), l3num
,
655 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
659 /* Must invert the tuple if skb has been transformed by NAT. */
661 struct nf_conntrack_tuple inverse
;
663 if (!nf_ct_invert_tuplepr(&inverse
, &tuple
)) {
664 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
670 /* look for tuple match */
671 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
673 return NULL
; /* Not found. */
675 ct
= nf_ct_tuplehash_to_ctrack(h
);
677 /* Inverted packet tuple matches the reverse direction conntrack tuple,
678 * select the other tuplehash to get the right 'ctinfo' bits for this
682 h
= &ct
->tuplehash
[!h
->tuple
.dst
.dir
];
684 nf_ct_set(skb
, ct
, ovs_ct_get_info(h
));
689 struct nf_conn
*ovs_ct_executed(struct net
*net
,
690 const struct sw_flow_key
*key
,
691 const struct ovs_conntrack_info
*info
,
695 struct nf_conn
*ct
= NULL
;
697 /* If no ct, check if we have evidence that an existing conntrack entry
698 * might be found for this skb. This happens when we lose a skb->_nfct
699 * due to an upcall, or if the direction is being forced. If the
700 * connection was not confirmed, it is not cached and needs to be run
701 * through conntrack again.
703 *ct_executed
= (key
->ct_state
& OVS_CS_F_TRACKED
) &&
704 !(key
->ct_state
& OVS_CS_F_INVALID
) &&
705 (key
->ct_zone
== info
->zone
.id
);
707 if (*ct_executed
|| (!key
->ct_state
&& info
->force
)) {
708 ct
= ovs_ct_find_existing(net
, &info
->zone
, info
->family
, skb
,
716 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
717 static bool skb_nfct_cached(struct net
*net
,
718 const struct sw_flow_key
*key
,
719 const struct ovs_conntrack_info
*info
,
722 enum ip_conntrack_info ctinfo
;
724 bool ct_executed
= true;
726 ct
= nf_ct_get(skb
, &ctinfo
);
728 ct
= ovs_ct_executed(net
, key
, info
, skb
, &ct_executed
);
731 nf_ct_get(skb
, &ctinfo
);
735 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
737 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
740 struct nf_conn_help
*help
;
742 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
743 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
746 /* Force conntrack entry direction to the current packet? */
747 if (info
->force
&& CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
) {
748 /* Delete the conntrack entry if confirmed, else just release
751 if (nf_ct_is_confirmed(ct
))
752 nf_ct_delete(ct
, 0, 0);
754 nf_conntrack_put(&ct
->ct_general
);
755 nf_ct_set(skb
, NULL
, 0);
762 #ifdef CONFIG_NF_NAT_NEEDED
763 /* Modelled after nf_nat_ipv[46]_fn().
764 * range is only used for new, uninitialized NAT state.
765 * Returns either NF_ACCEPT or NF_DROP.
767 static int ovs_ct_nat_execute(struct sk_buff
*skb
, struct nf_conn
*ct
,
768 enum ip_conntrack_info ctinfo
,
769 const struct nf_nat_range2
*range
,
770 enum nf_nat_manip_type maniptype
)
772 int hooknum
, nh_off
, err
= NF_ACCEPT
;
774 nh_off
= skb_network_offset(skb
);
775 skb_pull_rcsum(skb
, nh_off
);
777 /* See HOOK2MANIP(). */
778 if (maniptype
== NF_NAT_MANIP_SRC
)
779 hooknum
= NF_INET_LOCAL_IN
; /* Source NAT */
781 hooknum
= NF_INET_LOCAL_OUT
; /* Destination NAT */
785 case IP_CT_RELATED_REPLY
:
786 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
787 skb
->protocol
== htons(ETH_P_IP
) &&
788 ip_hdr(skb
)->protocol
== IPPROTO_ICMP
) {
789 if (!nf_nat_icmp_reply_translation(skb
, ct
, ctinfo
,
793 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
794 skb
->protocol
== htons(ETH_P_IPV6
)) {
796 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
797 int hdrlen
= ipv6_skip_exthdr(skb
,
798 sizeof(struct ipv6hdr
),
799 &nexthdr
, &frag_off
);
801 if (hdrlen
>= 0 && nexthdr
== IPPROTO_ICMPV6
) {
802 if (!nf_nat_icmpv6_reply_translation(skb
, ct
,
810 /* Non-ICMP, fall thru to initialize if needed. */
812 /* Seen it before? This can happen for loopback, retrans,
815 if (!nf_nat_initialized(ct
, maniptype
)) {
816 /* Initialize according to the NAT action. */
817 err
= (range
&& range
->flags
& NF_NAT_RANGE_MAP_IPS
)
818 /* Action is set up to establish a new
821 ? nf_nat_setup_info(ct
, range
, maniptype
)
822 : nf_nat_alloc_null_binding(ct
, hooknum
);
823 if (err
!= NF_ACCEPT
)
828 case IP_CT_ESTABLISHED
:
829 case IP_CT_ESTABLISHED_REPLY
:
837 err
= nf_nat_packet(ct
, ctinfo
, hooknum
, skb
);
839 skb_push(skb
, nh_off
);
840 skb_postpush_rcsum(skb
, skb
->data
, nh_off
);
845 static void ovs_nat_update_key(struct sw_flow_key
*key
,
846 const struct sk_buff
*skb
,
847 enum nf_nat_manip_type maniptype
)
849 if (maniptype
== NF_NAT_MANIP_SRC
) {
852 key
->ct_state
|= OVS_CS_F_SRC_NAT
;
853 if (key
->eth
.type
== htons(ETH_P_IP
))
854 key
->ipv4
.addr
.src
= ip_hdr(skb
)->saddr
;
855 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
856 memcpy(&key
->ipv6
.addr
.src
, &ipv6_hdr(skb
)->saddr
,
857 sizeof(key
->ipv6
.addr
.src
));
861 if (key
->ip
.proto
== IPPROTO_UDP
)
862 src
= udp_hdr(skb
)->source
;
863 else if (key
->ip
.proto
== IPPROTO_TCP
)
864 src
= tcp_hdr(skb
)->source
;
865 else if (key
->ip
.proto
== IPPROTO_SCTP
)
866 src
= sctp_hdr(skb
)->source
;
874 key
->ct_state
|= OVS_CS_F_DST_NAT
;
875 if (key
->eth
.type
== htons(ETH_P_IP
))
876 key
->ipv4
.addr
.dst
= ip_hdr(skb
)->daddr
;
877 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
878 memcpy(&key
->ipv6
.addr
.dst
, &ipv6_hdr(skb
)->daddr
,
879 sizeof(key
->ipv6
.addr
.dst
));
883 if (key
->ip
.proto
== IPPROTO_UDP
)
884 dst
= udp_hdr(skb
)->dest
;
885 else if (key
->ip
.proto
== IPPROTO_TCP
)
886 dst
= tcp_hdr(skb
)->dest
;
887 else if (key
->ip
.proto
== IPPROTO_SCTP
)
888 dst
= sctp_hdr(skb
)->dest
;
896 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
897 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
898 const struct ovs_conntrack_info
*info
,
899 struct sk_buff
*skb
, struct nf_conn
*ct
,
900 enum ip_conntrack_info ctinfo
)
902 enum nf_nat_manip_type maniptype
;
905 #ifdef HAVE_NF_CT_IS_UNTRACKED
906 if (nf_ct_is_untracked(ct
)) {
907 /* A NAT action may only be performed on tracked packets. */
910 #endif /* HAVE_NF_CT_IS_UNTRACKED */
912 /* Add NAT extension if not confirmed yet. */
913 if (!nf_ct_is_confirmed(ct
) && !nf_ct_nat_ext_add(ct
))
914 return NF_ACCEPT
; /* Can't NAT. */
916 /* Determine NAT type.
917 * Check if the NAT type can be deduced from the tracked connection.
918 * Make sure new expected connections (IP_CT_RELATED) are NATted only
921 if (info
->nat
& OVS_CT_NAT
&& ctinfo
!= IP_CT_NEW
&&
922 ct
->status
& IPS_NAT_MASK
&&
923 (ctinfo
!= IP_CT_RELATED
|| info
->commit
)) {
924 /* NAT an established or related connection like before. */
925 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
)
926 /* This is the REPLY direction for a connection
927 * for which NAT was applied in the forward
928 * direction. Do the reverse NAT.
930 maniptype
= ct
->status
& IPS_SRC_NAT
931 ? NF_NAT_MANIP_DST
: NF_NAT_MANIP_SRC
;
933 maniptype
= ct
->status
& IPS_SRC_NAT
934 ? NF_NAT_MANIP_SRC
: NF_NAT_MANIP_DST
;
935 } else if (info
->nat
& OVS_CT_SRC_NAT
) {
936 maniptype
= NF_NAT_MANIP_SRC
;
937 } else if (info
->nat
& OVS_CT_DST_NAT
) {
938 maniptype
= NF_NAT_MANIP_DST
;
940 return NF_ACCEPT
; /* Connection is not NATed. */
942 err
= ovs_ct_nat_execute(skb
, ct
, ctinfo
, &info
->range
, maniptype
);
944 /* Mark NAT done if successful and update the flow key. */
945 if (err
== NF_ACCEPT
)
946 ovs_nat_update_key(key
, skb
, maniptype
);
950 #else /* !CONFIG_NF_NAT_NEEDED */
951 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
952 const struct ovs_conntrack_info
*info
,
953 struct sk_buff
*skb
, struct nf_conn
*ct
,
954 enum ip_conntrack_info ctinfo
)
960 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
961 * not done already. Update key with new CT state after passing the packet
963 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
964 * set to NULL and 0 will be returned.
966 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
967 const struct ovs_conntrack_info
*info
,
970 /* If we are recirculating packets to match on conntrack fields and
971 * committing with a separate conntrack action, then we don't need to
972 * actually run the packet through conntrack twice unless it's for a
975 bool cached
= skb_nfct_cached(net
, key
, info
, skb
);
976 enum ip_conntrack_info ctinfo
;
980 struct nf_hook_state state
= {
981 .hook
= NF_INET_PRE_ROUTING
,
985 struct nf_conn
*tmpl
= info
->ct
;
988 /* Associate skb with specified zone. */
991 nf_conntrack_put(skb_nfct(skb
));
992 nf_conntrack_get(&tmpl
->ct_general
);
993 nf_ct_set(skb
, tmpl
, IP_CT_NEW
);
996 err
= nf_conntrack_in(skb
, &state
);
997 if (err
!= NF_ACCEPT
)
1000 /* Clear CT state NAT flags to mark that we have not yet done
1001 * NAT after the nf_conntrack_in() call. We can actually clear
1002 * the whole state, as it will be re-initialized below.
1006 /* Update the key, but keep the NAT flags. */
1007 ovs_ct_update_key(skb
, info
, key
, true, true);
1010 ct
= nf_ct_get(skb
, &ctinfo
);
1012 /* Packets starting a new connection must be NATted before the
1013 * helper, so that the helper knows about the NAT. We enforce
1014 * this by delaying both NAT and helper calls for unconfirmed
1015 * connections until the committing CT action. For later
1016 * packets NAT and Helper may be called in either order.
1018 * NAT will be done only if the CT action has NAT, and only
1019 * once per packet (per zone), as guarded by the NAT bits in
1020 * the key->ct_state.
1022 if (info
->nat
&& !(key
->ct_state
& OVS_CS_F_NAT_MASK
) &&
1023 (nf_ct_is_confirmed(ct
) || info
->commit
) &&
1024 ovs_ct_nat(net
, key
, info
, skb
, ct
, ctinfo
) != NF_ACCEPT
) {
1028 /* Userspace may decide to perform a ct lookup without a helper
1029 * specified followed by a (recirculate and) commit with one.
1030 * Therefore, for unconfirmed connections which we will commit,
1031 * we need to attach the helper here.
1033 if (!nf_ct_is_confirmed(ct
) && info
->commit
&&
1034 info
->helper
&& !nfct_help(ct
)) {
1035 int err
= __nf_ct_try_assign_helper(ct
, info
->ct
,
1041 /* Call the helper only if:
1042 * - nf_conntrack_in() was executed above ("!cached") for a
1043 * confirmed connection, or
1044 * - When committing an unconfirmed connection.
1046 if ((nf_ct_is_confirmed(ct
) ? !cached
: info
->commit
) &&
1047 ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
1055 /* Lookup connection and read fields into key. */
1056 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
1057 const struct ovs_conntrack_info
*info
,
1058 struct sk_buff
*skb
)
1060 struct nf_conntrack_expect
*exp
;
1062 /* If we pass an expected packet through nf_conntrack_in() the
1063 * expectation is typically removed, but the packet could still be
1064 * lost in upcall processing. To prevent this from happening we
1065 * perform an explicit expectation lookup. Expected connections are
1066 * always new, and will be passed through conntrack only when they are
1067 * committed, as it is OK to remove the expectation at that time.
1069 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
1073 /* NOTE: New connections are NATted and Helped only when
1074 * committed, so we are not calling into NAT here.
1076 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
1077 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
1082 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1086 ct
= (struct nf_conn
*)skb_nfct(skb
);
1088 nf_ct_deliver_cached_events(ct
);
1094 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
1098 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
1099 if (labels
->ct_labels_32
[i
])
1105 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1106 static struct hlist_head
*ct_limit_hash_bucket(
1107 const struct ovs_ct_limit_info
*info
, u16 zone
)
1109 return &info
->limits
[zone
& (CT_LIMIT_HASH_BUCKETS
- 1)];
1112 /* Call with ovs_mutex */
1113 static void ct_limit_set(const struct ovs_ct_limit_info
*info
,
1114 struct ovs_ct_limit
*new_ct_limit
)
1116 struct ovs_ct_limit
*ct_limit
;
1117 struct hlist_head
*head
;
1119 head
= ct_limit_hash_bucket(info
, new_ct_limit
->zone
);
1120 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1121 if (ct_limit
->zone
== new_ct_limit
->zone
) {
1122 hlist_replace_rcu(&ct_limit
->hlist_node
,
1123 &new_ct_limit
->hlist_node
);
1124 kfree_rcu(ct_limit
, rcu
);
1129 hlist_add_head_rcu(&new_ct_limit
->hlist_node
, head
);
1132 /* Call with ovs_mutex */
1133 static void ct_limit_del(const struct ovs_ct_limit_info
*info
, u16 zone
)
1135 struct ovs_ct_limit
*ct_limit
;
1136 struct hlist_head
*head
;
1137 struct hlist_node
*n
;
1139 head
= ct_limit_hash_bucket(info
, zone
);
1140 hlist_for_each_entry_safe(ct_limit
, n
, head
, hlist_node
) {
1141 if (ct_limit
->zone
== zone
) {
1142 hlist_del_rcu(&ct_limit
->hlist_node
);
1143 kfree_rcu(ct_limit
, rcu
);
1149 /* Call with RCU read lock */
1150 static u32
ct_limit_get(const struct ovs_ct_limit_info
*info
, u16 zone
)
1152 struct ovs_ct_limit
*ct_limit
;
1153 struct hlist_head
*head
;
1155 head
= ct_limit_hash_bucket(info
, zone
);
1156 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1157 if (ct_limit
->zone
== zone
)
1158 return ct_limit
->limit
;
1161 return info
->default_limit
;
1164 static int ovs_ct_check_limit(struct net
*net
,
1165 const struct ovs_conntrack_info
*info
,
1166 const struct nf_conntrack_tuple
*tuple
)
1168 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1169 const struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
1170 u32 per_zone_limit
, connections
;
1173 conncount_key
= info
->zone
.id
;
1175 per_zone_limit
= ct_limit_get(ct_limit_info
, info
->zone
.id
);
1176 if (per_zone_limit
== OVS_CT_LIMIT_UNLIMITED
)
1179 connections
= nf_conncount_count(net
, ct_limit_info
->data
,
1180 &conncount_key
, tuple
, &info
->zone
);
1181 if (connections
> per_zone_limit
)
1188 /* Lookup connection and confirm if unconfirmed. */
1189 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
1190 const struct ovs_conntrack_info
*info
,
1191 struct sk_buff
*skb
)
1193 enum ip_conntrack_info ctinfo
;
1197 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1201 /* The connection could be invalid, in which case this is a no-op.*/
1202 ct
= nf_ct_get(skb
, &ctinfo
);
1206 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1207 if (static_branch_unlikely(&ovs_ct_limit_enabled
)) {
1208 if (!nf_ct_is_confirmed(ct
)) {
1209 err
= ovs_ct_check_limit(net
, info
,
1210 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
1212 net_warn_ratelimited("openvswitch: zone: %u "
1213 "exceeds conntrack limit\n",
1221 /* Set the conntrack event mask if given. NEW and DELETE events have
1222 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1223 * typically would receive many kinds of updates. Setting the event
1224 * mask allows those events to be filtered. The set event mask will
1225 * remain in effect for the lifetime of the connection unless changed
1226 * by a further CT action with both the commit flag and the eventmask
1228 if (info
->have_eventmask
) {
1229 struct nf_conntrack_ecache
*cache
= nf_ct_ecache_find(ct
);
1232 cache
->ctmask
= info
->eventmask
;
1235 /* Apply changes before confirming the connection so that the initial
1236 * conntrack NEW netlink event carries the values given in the CT
1239 if (info
->mark
.mask
) {
1240 err
= ovs_ct_set_mark(ct
, key
, info
->mark
.value
,
1245 if (!nf_ct_is_confirmed(ct
)) {
1246 err
= ovs_ct_init_labels(ct
, key
, &info
->labels
.value
,
1247 &info
->labels
.mask
);
1250 } else if (labels_nonzero(&info
->labels
.mask
)) {
1251 err
= ovs_ct_set_labels(ct
, key
, &info
->labels
.value
,
1252 &info
->labels
.mask
);
1256 /* This will take care of sending queued events even if the connection
1257 * is already confirmed.
1259 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
1265 /* Trim the skb to the length specified by the IP/IPv6 header,
1266 * removing any trailing lower-layer padding. This prepares the skb
1267 * for higher-layer processing that assumes skb->len excludes padding
1268 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1269 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1271 static int ovs_skb_network_trim(struct sk_buff
*skb
)
1276 switch (skb
->protocol
) {
1277 case htons(ETH_P_IP
):
1278 len
= ntohs(ip_hdr(skb
)->tot_len
);
1280 case htons(ETH_P_IPV6
):
1281 len
= sizeof(struct ipv6hdr
)
1282 + ntohs(ipv6_hdr(skb
)->payload_len
);
1288 err
= pskb_trim_rcsum(skb
, len
);
1295 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1296 * value if 'skb' is freed.
1298 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
1299 struct sw_flow_key
*key
,
1300 const struct ovs_conntrack_info
*info
)
1305 /* The conntrack module expects to be working at L3. */
1306 nh_ofs
= skb_network_offset(skb
);
1307 skb_pull_rcsum(skb
, nh_ofs
);
1309 err
= ovs_skb_network_trim(skb
);
1313 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
1314 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
1320 err
= ovs_ct_commit(net
, key
, info
, skb
);
1322 err
= ovs_ct_lookup(net
, key
, info
, skb
);
1324 skb_push(skb
, nh_ofs
);
1325 skb_postpush_rcsum(skb
, skb
->data
, nh_ofs
);
1331 int ovs_ct_clear(struct sk_buff
*skb
, struct sw_flow_key
*key
)
1333 if (skb_nfct(skb
)) {
1334 nf_conntrack_put(skb_nfct(skb
));
1335 #ifdef HAVE_IP_CT_UNTRACKED
1336 nf_ct_set(skb
, NULL
, IP_CT_UNTRACKED
);
1338 nf_ct_set(skb
, NULL
, 0);
1340 ovs_ct_fill_key(skb
, key
);
1346 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
1347 const struct sw_flow_key
*key
, bool log
)
1349 struct nf_conntrack_helper
*helper
;
1350 struct nf_conn_help
*help
;
1352 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
1355 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
1359 help
= nf_ct_helper_ext_add(info
->ct
, helper
, GFP_KERNEL
);
1361 nf_conntrack_helper_put(helper
);
1365 rcu_assign_pointer(help
->helper
, helper
);
1366 info
->helper
= helper
;
1369 request_module("ip_nat_%s", name
);
1374 #ifdef CONFIG_NF_NAT_NEEDED
1375 static int parse_nat(const struct nlattr
*attr
,
1376 struct ovs_conntrack_info
*info
, bool log
)
1380 bool have_ip_max
= false;
1381 bool have_proto_max
= false;
1382 bool ip_vers
= (info
->family
== NFPROTO_IPV6
);
1384 nla_for_each_nested(a
, attr
, rem
) {
1385 static const int ovs_nat_attr_lens
[OVS_NAT_ATTR_MAX
+ 1][2] = {
1386 [OVS_NAT_ATTR_SRC
] = {0, 0},
1387 [OVS_NAT_ATTR_DST
] = {0, 0},
1388 [OVS_NAT_ATTR_IP_MIN
] = {sizeof(struct in_addr
),
1389 sizeof(struct in6_addr
)},
1390 [OVS_NAT_ATTR_IP_MAX
] = {sizeof(struct in_addr
),
1391 sizeof(struct in6_addr
)},
1392 [OVS_NAT_ATTR_PROTO_MIN
] = {sizeof(u16
), sizeof(u16
)},
1393 [OVS_NAT_ATTR_PROTO_MAX
] = {sizeof(u16
), sizeof(u16
)},
1394 [OVS_NAT_ATTR_PERSISTENT
] = {0, 0},
1395 [OVS_NAT_ATTR_PROTO_HASH
] = {0, 0},
1396 [OVS_NAT_ATTR_PROTO_RANDOM
] = {0, 0},
1398 int type
= nla_type(a
);
1400 if (type
> OVS_NAT_ATTR_MAX
) {
1401 OVS_NLERR(log
, "Unknown NAT attribute (type=%d, max=%d)",
1402 type
, OVS_NAT_ATTR_MAX
);
1406 if (nla_len(a
) != ovs_nat_attr_lens
[type
][ip_vers
]) {
1407 OVS_NLERR(log
, "NAT attribute type %d has unexpected length (%d != %d)",
1409 ovs_nat_attr_lens
[type
][ip_vers
]);
1414 case OVS_NAT_ATTR_SRC
:
1415 case OVS_NAT_ATTR_DST
:
1417 OVS_NLERR(log
, "Only one type of NAT may be specified");
1420 info
->nat
|= OVS_CT_NAT
;
1421 info
->nat
|= ((type
== OVS_NAT_ATTR_SRC
)
1422 ? OVS_CT_SRC_NAT
: OVS_CT_DST_NAT
);
1425 case OVS_NAT_ATTR_IP_MIN
:
1426 nla_memcpy(&info
->range
.min_addr
, a
,
1427 sizeof(info
->range
.min_addr
));
1428 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1431 case OVS_NAT_ATTR_IP_MAX
:
1433 nla_memcpy(&info
->range
.max_addr
, a
,
1434 sizeof(info
->range
.max_addr
));
1435 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1438 case OVS_NAT_ATTR_PROTO_MIN
:
1439 info
->range
.min_proto
.all
= htons(nla_get_u16(a
));
1440 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1443 case OVS_NAT_ATTR_PROTO_MAX
:
1444 have_proto_max
= true;
1445 info
->range
.max_proto
.all
= htons(nla_get_u16(a
));
1446 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1449 case OVS_NAT_ATTR_PERSISTENT
:
1450 info
->range
.flags
|= NF_NAT_RANGE_PERSISTENT
;
1453 case OVS_NAT_ATTR_PROTO_HASH
:
1454 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1457 case OVS_NAT_ATTR_PROTO_RANDOM
:
1458 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1459 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM_FULLY
;
1461 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1462 info
->random_fully_compat
= true;
1467 OVS_NLERR(log
, "Unknown nat attribute (%d)", type
);
1473 OVS_NLERR(log
, "NAT attribute has %d unknown bytes", rem
);
1477 /* Do not allow flags if no type is given. */
1478 if (info
->range
.flags
) {
1480 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1484 info
->nat
= OVS_CT_NAT
; /* NAT existing connections. */
1485 } else if (!info
->commit
) {
1487 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1491 /* Allow missing IP_MAX. */
1492 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
&& !have_ip_max
) {
1493 memcpy(&info
->range
.max_addr
, &info
->range
.min_addr
,
1494 sizeof(info
->range
.max_addr
));
1496 /* Allow missing PROTO_MAX. */
1497 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1499 info
->range
.max_proto
.all
= info
->range
.min_proto
.all
;
1505 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
1506 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1507 [OVS_CT_ATTR_FORCE_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1508 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
1509 .maxlen
= sizeof(u16
) },
1510 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
1511 .maxlen
= sizeof(struct md_mark
) },
1512 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
1513 .maxlen
= sizeof(struct md_labels
) },
1514 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
1515 .maxlen
= NF_CT_HELPER_NAME_LEN
},
1516 #ifdef CONFIG_NF_NAT_NEEDED
1517 /* NAT length is checked when parsing the nested attributes. */
1518 [OVS_CT_ATTR_NAT
] = { .minlen
= 0, .maxlen
= INT_MAX
},
1520 [OVS_CT_ATTR_EVENTMASK
] = { .minlen
= sizeof(u32
),
1521 .maxlen
= sizeof(u32
) },
1524 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
1525 const char **helper
, bool log
)
1530 nla_for_each_nested(a
, attr
, rem
) {
1531 int type
= nla_type(a
);
1535 if (type
> OVS_CT_ATTR_MAX
) {
1537 "Unknown conntrack attr (type=%d, max=%d)",
1538 type
, OVS_CT_ATTR_MAX
);
1542 maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
1543 minlen
= ovs_ct_attr_lens
[type
].minlen
;
1544 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
1546 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1547 type
, nla_len(a
), maxlen
);
1552 case OVS_CT_ATTR_FORCE_COMMIT
:
1555 case OVS_CT_ATTR_COMMIT
:
1556 info
->commit
= true;
1558 #ifdef CONFIG_NF_CONNTRACK_ZONES
1559 case OVS_CT_ATTR_ZONE
:
1560 info
->zone
.id
= nla_get_u16(a
);
1563 #ifdef CONFIG_NF_CONNTRACK_MARK
1564 case OVS_CT_ATTR_MARK
: {
1565 struct md_mark
*mark
= nla_data(a
);
1568 OVS_NLERR(log
, "ct_mark mask cannot be 0");
1575 #ifdef CONFIG_NF_CONNTRACK_LABELS
1576 case OVS_CT_ATTR_LABELS
: {
1577 struct md_labels
*labels
= nla_data(a
);
1579 if (!labels_nonzero(&labels
->mask
)) {
1580 OVS_NLERR(log
, "ct_labels mask cannot be 0");
1583 info
->labels
= *labels
;
1587 case OVS_CT_ATTR_HELPER
:
1588 *helper
= nla_data(a
);
1589 if (!memchr(*helper
, '\0', nla_len(a
))) {
1590 OVS_NLERR(log
, "Invalid conntrack helper");
1594 #ifdef CONFIG_NF_NAT_NEEDED
1595 case OVS_CT_ATTR_NAT
: {
1596 int err
= parse_nat(a
, info
, log
);
1603 case OVS_CT_ATTR_EVENTMASK
:
1604 info
->have_eventmask
= true;
1605 info
->eventmask
= nla_get_u32(a
);
1609 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
1615 #ifdef CONFIG_NF_CONNTRACK_MARK
1616 if (!info
->commit
&& info
->mark
.mask
) {
1618 "Setting conntrack mark requires 'commit' flag.");
1622 #ifdef CONFIG_NF_CONNTRACK_LABELS
1623 if (!info
->commit
&& labels_nonzero(&info
->labels
.mask
)) {
1625 "Setting conntrack labels requires 'commit' flag.");
1630 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
1637 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
1639 if (attr
== OVS_KEY_ATTR_CT_STATE
)
1641 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1642 attr
== OVS_KEY_ATTR_CT_ZONE
)
1644 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
1645 attr
== OVS_KEY_ATTR_CT_MARK
)
1647 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1648 attr
== OVS_KEY_ATTR_CT_LABELS
) {
1649 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1651 return ovs_net
->xt_label
;
1657 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
1658 const struct sw_flow_key
*key
,
1659 struct sw_flow_actions
**sfa
, bool log
)
1661 struct ovs_conntrack_info ct_info
;
1662 const char *helper
= NULL
;
1666 family
= key_to_nfproto(key
);
1667 if (family
== NFPROTO_UNSPEC
) {
1668 OVS_NLERR(log
, "ct family unspecified");
1672 memset(&ct_info
, 0, sizeof(ct_info
));
1673 ct_info
.family
= family
;
1675 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
1676 NF_CT_DEFAULT_ZONE_DIR
, 0);
1678 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
1682 /* Set up template for tracking connections in specific zones. */
1683 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
1685 OVS_NLERR(log
, "Failed to allocate conntrack template");
1689 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
1694 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
1695 sizeof(ct_info
), log
);
1699 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
1700 nf_conntrack_get(&ct_info
.ct
->ct_general
);
1703 __ovs_ct_free_action(&ct_info
);
1707 #ifdef CONFIG_NF_NAT_NEEDED
1708 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info
*info
,
1709 struct sk_buff
*skb
)
1711 struct nlattr
*start
;
1713 start
= nla_nest_start(skb
, OVS_CT_ATTR_NAT
);
1717 if (info
->nat
& OVS_CT_SRC_NAT
) {
1718 if (nla_put_flag(skb
, OVS_NAT_ATTR_SRC
))
1720 } else if (info
->nat
& OVS_CT_DST_NAT
) {
1721 if (nla_put_flag(skb
, OVS_NAT_ATTR_DST
))
1727 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
) {
1728 if (IS_ENABLED(CONFIG_NF_NAT_IPV4
) &&
1729 info
->family
== NFPROTO_IPV4
) {
1730 if (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1731 info
->range
.min_addr
.ip
) ||
1732 (info
->range
.max_addr
.ip
1733 != info
->range
.min_addr
.ip
&&
1734 (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1735 info
->range
.max_addr
.ip
))))
1737 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6
) &&
1738 info
->family
== NFPROTO_IPV6
) {
1739 if (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1740 &info
->range
.min_addr
.in6
) ||
1741 (memcmp(&info
->range
.max_addr
.in6
,
1742 &info
->range
.min_addr
.in6
,
1743 sizeof(info
->range
.max_addr
.in6
)) &&
1744 (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1745 &info
->range
.max_addr
.in6
))))
1751 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1752 (nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MIN
,
1753 ntohs(info
->range
.min_proto
.all
)) ||
1754 (info
->range
.max_proto
.all
!= info
->range
.min_proto
.all
&&
1755 nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MAX
,
1756 ntohs(info
->range
.max_proto
.all
)))))
1759 if (info
->range
.flags
& NF_NAT_RANGE_PERSISTENT
&&
1760 nla_put_flag(skb
, OVS_NAT_ATTR_PERSISTENT
))
1762 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM
&&
1763 nla_put_flag(skb
, info
->random_fully_compat
1764 ? OVS_NAT_ATTR_PROTO_RANDOM
1765 : OVS_NAT_ATTR_PROTO_HASH
))
1767 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1768 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM_FULLY
&&
1769 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_RANDOM
))
1773 nla_nest_end(skb
, start
);
1779 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
1780 struct sk_buff
*skb
)
1782 struct nlattr
*start
;
1784 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_CT
);
1788 if (ct_info
->commit
&& nla_put_flag(skb
, ct_info
->force
1789 ? OVS_CT_ATTR_FORCE_COMMIT
1790 : OVS_CT_ATTR_COMMIT
))
1792 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1793 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
1795 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
1796 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
1799 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1800 labels_nonzero(&ct_info
->labels
.mask
) &&
1801 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
1804 if (ct_info
->helper
) {
1805 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
1806 ct_info
->helper
->name
))
1809 if (ct_info
->have_eventmask
&&
1810 nla_put_u32(skb
, OVS_CT_ATTR_EVENTMASK
, ct_info
->eventmask
))
1813 #ifdef CONFIG_NF_NAT_NEEDED
1814 if (ct_info
->nat
&& !ovs_ct_nat_to_attr(ct_info
, skb
))
1817 nla_nest_end(skb
, start
);
1822 void ovs_ct_free_action(const struct nlattr
*a
)
1824 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
1826 __ovs_ct_free_action(ct_info
);
1829 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
)
1831 if (ct_info
->helper
)
1832 nf_conntrack_helper_put(ct_info
->helper
);
1834 nf_ct_tmpl_free(ct_info
->ct
);
1837 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1838 static int ovs_ct_limit_init(struct net
*net
, struct ovs_net
*ovs_net
)
1842 ovs_net
->ct_limit_info
= kmalloc(sizeof(*ovs_net
->ct_limit_info
),
1844 if (!ovs_net
->ct_limit_info
)
1847 ovs_net
->ct_limit_info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1848 ovs_net
->ct_limit_info
->limits
=
1849 kmalloc_array(CT_LIMIT_HASH_BUCKETS
, sizeof(struct hlist_head
),
1851 if (!ovs_net
->ct_limit_info
->limits
) {
1852 kfree(ovs_net
->ct_limit_info
);
1856 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; i
++)
1857 INIT_HLIST_HEAD(&ovs_net
->ct_limit_info
->limits
[i
]);
1859 ovs_net
->ct_limit_info
->data
=
1860 nf_conncount_init(net
, NFPROTO_INET
, sizeof(u32
));
1862 if (IS_ERR(ovs_net
->ct_limit_info
->data
)) {
1863 err
= PTR_ERR(ovs_net
->ct_limit_info
->data
);
1864 kfree(ovs_net
->ct_limit_info
->limits
);
1865 kfree(ovs_net
->ct_limit_info
);
1866 pr_err("openvswitch: failed to init nf_conncount %d\n", err
);
1872 static void ovs_ct_limit_exit(struct net
*net
, struct ovs_net
*ovs_net
)
1874 const struct ovs_ct_limit_info
*info
= ovs_net
->ct_limit_info
;
1877 nf_conncount_destroy(net
, NFPROTO_INET
, info
->data
);
1878 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
1879 struct hlist_head
*head
= &info
->limits
[i
];
1880 struct ovs_ct_limit
*ct_limit
;
1882 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
)
1883 kfree_rcu(ct_limit
, rcu
);
1885 kfree(ovs_net
->ct_limit_info
->limits
);
1886 kfree(ovs_net
->ct_limit_info
);
1889 static struct sk_buff
*
1890 ovs_ct_limit_cmd_reply_start(struct genl_info
*info
, u8 cmd
,
1891 struct ovs_header
**ovs_reply_header
)
1893 struct ovs_header
*ovs_header
= info
->userhdr
;
1894 struct sk_buff
*skb
;
1896 skb
= genlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1898 return ERR_PTR(-ENOMEM
);
1900 *ovs_reply_header
= genlmsg_put(skb
, info
->snd_portid
,
1902 &dp_ct_limit_genl_family
, 0, cmd
);
1904 if (!*ovs_reply_header
) {
1906 return ERR_PTR(-EMSGSIZE
);
1908 (*ovs_reply_header
)->dp_ifindex
= ovs_header
->dp_ifindex
;
1913 static bool check_zone_id(int zone_id
, u16
*pzone
)
1915 if (zone_id
>= 0 && zone_id
<= 65535) {
1916 *pzone
= (u16
)zone_id
;
1922 static int ovs_ct_limit_set_zone_limit(struct nlattr
*nla_zone_limit
,
1923 struct ovs_ct_limit_info
*info
)
1925 struct ovs_zone_limit
*zone_limit
;
1929 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1930 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1932 while (rem
>= sizeof(*zone_limit
)) {
1933 if (unlikely(zone_limit
->zone_id
==
1934 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1936 info
->default_limit
= zone_limit
->limit
;
1938 } else if (unlikely(!check_zone_id(
1939 zone_limit
->zone_id
, &zone
))) {
1940 OVS_NLERR(true, "zone id is out of range");
1942 struct ovs_ct_limit
*ct_limit
;
1944 ct_limit
= kmalloc(sizeof(*ct_limit
), GFP_KERNEL
);
1948 ct_limit
->zone
= zone
;
1949 ct_limit
->limit
= zone_limit
->limit
;
1952 ct_limit_set(info
, ct_limit
);
1955 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
1956 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
1957 NLA_ALIGN(sizeof(*zone_limit
)));
1961 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem
);
1966 static int ovs_ct_limit_del_zone_limit(struct nlattr
*nla_zone_limit
,
1967 struct ovs_ct_limit_info
*info
)
1969 struct ovs_zone_limit
*zone_limit
;
1973 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1974 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1976 while (rem
>= sizeof(*zone_limit
)) {
1977 if (unlikely(zone_limit
->zone_id
==
1978 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1980 info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1982 } else if (unlikely(!check_zone_id(
1983 zone_limit
->zone_id
, &zone
))) {
1984 OVS_NLERR(true, "zone id is out of range");
1987 ct_limit_del(info
, zone
);
1990 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
1991 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
1992 NLA_ALIGN(sizeof(*zone_limit
)));
1996 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem
);
2001 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info
*info
,
2002 struct sk_buff
*reply
)
2004 struct ovs_zone_limit zone_limit
;
2007 zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2008 zone_limit
.limit
= info
->default_limit
;
2009 err
= nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2016 static int __ovs_ct_limit_get_zone_limit(struct net
*net
,
2017 struct nf_conncount_data
*data
,
2018 u16 zone_id
, u32 limit
,
2019 struct sk_buff
*reply
)
2021 struct nf_conntrack_zone ct_zone
;
2022 struct ovs_zone_limit zone_limit
;
2023 u32 conncount_key
= zone_id
;
2025 zone_limit
.zone_id
= zone_id
;
2026 zone_limit
.limit
= limit
;
2027 nf_ct_zone_init(&ct_zone
, zone_id
, NF_CT_DEFAULT_ZONE_DIR
, 0);
2029 zone_limit
.count
= nf_conncount_count(net
, data
, &conncount_key
, NULL
,
2031 return nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2034 static int ovs_ct_limit_get_zone_limit(struct net
*net
,
2035 struct nlattr
*nla_zone_limit
,
2036 struct ovs_ct_limit_info
*info
,
2037 struct sk_buff
*reply
)
2039 struct ovs_zone_limit
*zone_limit
;
2044 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2045 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2047 while (rem
>= sizeof(*zone_limit
)) {
2048 if (unlikely(zone_limit
->zone_id
==
2049 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2050 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2053 } else if (unlikely(!check_zone_id(zone_limit
->zone_id
,
2055 OVS_NLERR(true, "zone id is out of range");
2058 limit
= ct_limit_get(info
, zone
);
2061 err
= __ovs_ct_limit_get_zone_limit(
2062 net
, info
->data
, zone
, limit
, reply
);
2066 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2067 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2068 NLA_ALIGN(sizeof(*zone_limit
)));
2072 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem
);
2077 static int ovs_ct_limit_get_all_zone_limit(struct net
*net
,
2078 struct ovs_ct_limit_info
*info
,
2079 struct sk_buff
*reply
)
2081 struct ovs_ct_limit
*ct_limit
;
2082 struct hlist_head
*head
;
2085 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2090 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
2091 head
= &info
->limits
[i
];
2092 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
2093 err
= __ovs_ct_limit_get_zone_limit(net
, info
->data
,
2094 ct_limit
->zone
, ct_limit
->limit
, reply
);
2105 static int ovs_ct_limit_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2107 struct nlattr
**a
= info
->attrs
;
2108 struct sk_buff
*reply
;
2109 struct ovs_header
*ovs_reply_header
;
2110 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2111 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2114 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_SET
,
2117 return PTR_ERR(reply
);
2119 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2124 err
= ovs_ct_limit_set_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2129 static_branch_enable(&ovs_ct_limit_enabled
);
2131 genlmsg_end(reply
, ovs_reply_header
);
2132 return genlmsg_reply(reply
, info
);
2139 static int ovs_ct_limit_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2141 struct nlattr
**a
= info
->attrs
;
2142 struct sk_buff
*reply
;
2143 struct ovs_header
*ovs_reply_header
;
2144 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2145 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2148 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_DEL
,
2151 return PTR_ERR(reply
);
2153 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2158 err
= ovs_ct_limit_del_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2163 genlmsg_end(reply
, ovs_reply_header
);
2164 return genlmsg_reply(reply
, info
);
2171 static int ovs_ct_limit_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2173 struct nlattr
**a
= info
->attrs
;
2174 struct nlattr
*nla_reply
;
2175 struct sk_buff
*reply
;
2176 struct ovs_header
*ovs_reply_header
;
2177 struct net
*net
= sock_net(skb
->sk
);
2178 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2179 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2182 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_GET
,
2185 return PTR_ERR(reply
);
2187 nla_reply
= nla_nest_start(reply
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2189 if (a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2190 err
= ovs_ct_limit_get_zone_limit(
2191 net
, a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
], ct_limit_info
,
2196 err
= ovs_ct_limit_get_all_zone_limit(net
, ct_limit_info
,
2202 nla_nest_end(reply
, nla_reply
);
2203 genlmsg_end(reply
, ovs_reply_header
);
2204 return genlmsg_reply(reply
, info
);
2211 static struct genl_ops ct_limit_genl_ops
[] = {
2212 { .cmd
= OVS_CT_LIMIT_CMD_SET
,
2213 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2215 .policy
= ct_limit_policy
,
2216 .doit
= ovs_ct_limit_cmd_set
,
2218 { .cmd
= OVS_CT_LIMIT_CMD_DEL
,
2219 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2221 .policy
= ct_limit_policy
,
2222 .doit
= ovs_ct_limit_cmd_del
,
2224 { .cmd
= OVS_CT_LIMIT_CMD_GET
,
2225 .flags
= 0, /* OK for unprivileged users. */
2226 .policy
= ct_limit_policy
,
2227 .doit
= ovs_ct_limit_cmd_get
,
2231 static const struct genl_multicast_group ovs_ct_limit_multicast_group
= {
2232 .name
= OVS_CT_LIMIT_MCGROUP
,
2235 struct genl_family dp_ct_limit_genl_family __ro_after_init
= {
2236 .hdrsize
= sizeof(struct ovs_header
),
2237 .name
= OVS_CT_LIMIT_FAMILY
,
2238 .version
= OVS_CT_LIMIT_VERSION
,
2239 .maxattr
= OVS_CT_LIMIT_ATTR_MAX
,
2241 .parallel_ops
= true,
2242 .ops
= ct_limit_genl_ops
,
2243 .n_ops
= ARRAY_SIZE(ct_limit_genl_ops
),
2244 .mcgrps
= &ovs_ct_limit_multicast_group
,
2246 .module
= THIS_MODULE
,
2250 int ovs_ct_init(struct net
*net
)
2252 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
2253 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2255 if (nf_connlabels_get(net
, n_bits
- 1)) {
2256 ovs_net
->xt_label
= false;
2257 OVS_NLERR(true, "Failed to set connlabel length");
2259 ovs_net
->xt_label
= true;
2262 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2263 return ovs_ct_limit_init(net
, ovs_net
);
2269 void ovs_ct_exit(struct net
*net
)
2271 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2273 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2274 ovs_ct_limit_exit(net
, ovs_net
);
2277 if (ovs_net
->xt_label
)
2278 nf_connlabels_put(net
);
2281 #endif /* CONFIG_NF_CONNTRACK */