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/module.h>
15 #include <linux/openvswitch.h>
16 #include <linux/tcp.h>
17 #include <linux/udp.h>
18 #include <linux/sctp.h>
19 #include <linux/static_key.h>
21 #include <net/genetlink.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_count.h>
24 #include <net/netfilter/nf_conntrack_helper.h>
25 #include <net/netfilter/nf_conntrack_labels.h>
26 #include <net/netfilter/nf_conntrack_seqadj.h>
27 #include <net/netfilter/nf_conntrack_timeout.h>
28 #include <net/netfilter/nf_conntrack_zones.h>
29 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
30 #include <net/ipv6_frag.h>
32 #if IS_ENABLED(CONFIG_NF_NAT)
33 #include <net/netfilter/nf_nat.h>
37 #include "conntrack.h"
39 #include "flow_netlink.h"
41 struct ovs_ct_len_tbl
{
46 /* Metadata mark for masked write to conntrack mark */
52 /* Metadata label for masked write to conntrack label. */
54 struct ovs_key_ct_labels value
;
55 struct ovs_key_ct_labels mask
;
59 OVS_CT_NAT
= 1 << 0, /* NAT for committed connections only. */
60 OVS_CT_SRC_NAT
= 1 << 1, /* Source NAT for NEW connections. */
61 OVS_CT_DST_NAT
= 1 << 2, /* Destination NAT for NEW connections. */
64 /* Conntrack action context for execution. */
65 struct ovs_conntrack_info
{
66 struct nf_conntrack_helper
*helper
;
67 struct nf_conntrack_zone zone
;
70 u8 nat
: 3; /* enum ovs_ct_nat */
72 u8 have_eventmask
: 1;
74 u32 eventmask
; /* Mask of 1 << IPCT_*. */
76 struct md_labels labels
;
77 char timeout
[CTNL_TIMEOUT_NAME_MAX
];
78 #if IS_ENABLED(CONFIG_NF_NAT)
79 struct nf_nat_range2 range
; /* Only present for SRC NAT and DST NAT. */
83 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
84 #define OVS_CT_LIMIT_UNLIMITED 0
85 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
86 #define CT_LIMIT_HASH_BUCKETS 512
87 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled
);
90 /* Elements in ovs_ct_limit_info->limits hash table */
91 struct hlist_node hlist_node
;
97 struct ovs_ct_limit_info
{
99 struct hlist_head
*limits
;
100 struct nf_conncount_data
*data
;
103 static const struct nla_policy ct_limit_policy
[OVS_CT_LIMIT_ATTR_MAX
+ 1] = {
104 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NLA_NESTED
, },
108 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
);
110 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
);
112 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
114 switch (ntohs(key
->eth
.type
)) {
120 return NFPROTO_UNSPEC
;
124 /* Map SKB connection state into the values used by flow definition. */
125 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
127 u8 ct_state
= OVS_CS_F_TRACKED
;
130 case IP_CT_ESTABLISHED_REPLY
:
131 case IP_CT_RELATED_REPLY
:
132 ct_state
|= OVS_CS_F_REPLY_DIR
;
139 case IP_CT_ESTABLISHED
:
140 case IP_CT_ESTABLISHED_REPLY
:
141 ct_state
|= OVS_CS_F_ESTABLISHED
;
144 case IP_CT_RELATED_REPLY
:
145 ct_state
|= OVS_CS_F_RELATED
;
148 ct_state
|= OVS_CS_F_NEW
;
157 static u32
ovs_ct_get_mark(const struct nf_conn
*ct
)
159 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
160 return ct
? ct
->mark
: 0;
166 /* Guard against conntrack labels max size shrinking below 128 bits. */
167 #if NF_CT_LABELS_MAX_SIZE < 16
168 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
171 static void ovs_ct_get_labels(const struct nf_conn
*ct
,
172 struct ovs_key_ct_labels
*labels
)
174 struct nf_conn_labels
*cl
= ct
? nf_ct_labels_find(ct
) : NULL
;
177 memcpy(labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
179 memset(labels
, 0, OVS_CT_LABELS_LEN
);
182 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key
*key
,
183 const struct nf_conntrack_tuple
*orig
,
186 key
->ct_orig_proto
= orig
->dst
.protonum
;
187 if (orig
->dst
.protonum
== icmp_proto
) {
188 key
->ct
.orig_tp
.src
= htons(orig
->dst
.u
.icmp
.type
);
189 key
->ct
.orig_tp
.dst
= htons(orig
->dst
.u
.icmp
.code
);
191 key
->ct
.orig_tp
.src
= orig
->src
.u
.all
;
192 key
->ct
.orig_tp
.dst
= orig
->dst
.u
.all
;
196 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
197 const struct nf_conntrack_zone
*zone
,
198 const struct nf_conn
*ct
)
200 key
->ct_state
= state
;
201 key
->ct_zone
= zone
->id
;
202 key
->ct
.mark
= ovs_ct_get_mark(ct
);
203 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
206 const struct nf_conntrack_tuple
*orig
;
208 /* Use the master if we have one. */
211 orig
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
213 /* IP version must match with the master connection. */
214 if (key
->eth
.type
== htons(ETH_P_IP
) &&
215 nf_ct_l3num(ct
) == NFPROTO_IPV4
) {
216 key
->ipv4
.ct_orig
.src
= orig
->src
.u3
.ip
;
217 key
->ipv4
.ct_orig
.dst
= orig
->dst
.u3
.ip
;
218 __ovs_ct_update_key_orig_tp(key
, orig
, IPPROTO_ICMP
);
220 } else if (key
->eth
.type
== htons(ETH_P_IPV6
) &&
221 !sw_flow_key_is_nd(key
) &&
222 nf_ct_l3num(ct
) == NFPROTO_IPV6
) {
223 key
->ipv6
.ct_orig
.src
= orig
->src
.u3
.in6
;
224 key
->ipv6
.ct_orig
.dst
= orig
->dst
.u3
.in6
;
225 __ovs_ct_update_key_orig_tp(key
, orig
, NEXTHDR_ICMP
);
229 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
230 * original direction key fields.
232 key
->ct_orig_proto
= 0;
235 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
236 * previously sent the packet to conntrack via the ct action. If
237 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
238 * initialized from the connection status.
240 static void ovs_ct_update_key(const struct sk_buff
*skb
,
241 const struct ovs_conntrack_info
*info
,
242 struct sw_flow_key
*key
, bool post_ct
,
245 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
246 enum ip_conntrack_info ctinfo
;
250 ct
= nf_ct_get(skb
, &ctinfo
);
252 state
= ovs_ct_get_state(ctinfo
);
253 /* All unconfirmed entries are NEW connections. */
254 if (!nf_ct_is_confirmed(ct
))
255 state
|= OVS_CS_F_NEW
;
256 /* OVS persists the related flag for the duration of the
260 state
|= OVS_CS_F_RELATED
;
261 if (keep_nat_flags
) {
262 state
|= key
->ct_state
& OVS_CS_F_NAT_MASK
;
264 if (ct
->status
& IPS_SRC_NAT
)
265 state
|= OVS_CS_F_SRC_NAT
;
266 if (ct
->status
& IPS_DST_NAT
)
267 state
|= OVS_CS_F_DST_NAT
;
269 zone
= nf_ct_zone(ct
);
270 } else if (post_ct
) {
271 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
275 __ovs_ct_update_key(key
, state
, zone
, ct
);
278 /* This is called to initialize CT key fields possibly coming in from the local
281 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
283 ovs_ct_update_key(skb
, NULL
, key
, false, false);
286 #define IN6_ADDR_INITIALIZER(ADDR) \
287 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
288 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
290 int ovs_ct_put_key(const struct sw_flow_key
*swkey
,
291 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
293 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, output
->ct_state
))
296 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
297 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, output
->ct_zone
))
300 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
301 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, output
->ct
.mark
))
304 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
305 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(output
->ct
.labels
),
309 if (swkey
->ct_orig_proto
) {
310 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
311 struct ovs_key_ct_tuple_ipv4 orig
= {
312 output
->ipv4
.ct_orig
.src
,
313 output
->ipv4
.ct_orig
.dst
,
314 output
->ct
.orig_tp
.src
,
315 output
->ct
.orig_tp
.dst
,
316 output
->ct_orig_proto
,
318 if (nla_put(skb
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
319 sizeof(orig
), &orig
))
321 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
322 struct ovs_key_ct_tuple_ipv6 orig
= {
323 IN6_ADDR_INITIALIZER(output
->ipv6
.ct_orig
.src
),
324 IN6_ADDR_INITIALIZER(output
->ipv6
.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_IPV6
,
330 sizeof(orig
), &orig
))
338 static int ovs_ct_set_mark(struct nf_conn
*ct
, struct sw_flow_key
*key
,
339 u32 ct_mark
, u32 mask
)
341 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
344 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
345 if (ct
->mark
!= new_mark
) {
347 if (nf_ct_is_confirmed(ct
))
348 nf_conntrack_event_cache(IPCT_MARK
, ct
);
349 key
->ct
.mark
= new_mark
;
358 static struct nf_conn_labels
*ovs_ct_get_conn_labels(struct nf_conn
*ct
)
360 struct nf_conn_labels
*cl
;
362 cl
= nf_ct_labels_find(ct
);
364 nf_ct_labels_ext_add(ct
);
365 cl
= nf_ct_labels_find(ct
);
371 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
372 * since the new connection is not yet confirmed, and thus no-one else has
373 * access to it's labels, we simply write them over.
375 static int ovs_ct_init_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
376 const struct ovs_key_ct_labels
*labels
,
377 const struct ovs_key_ct_labels
*mask
)
379 struct nf_conn_labels
*cl
, *master_cl
;
380 bool have_mask
= labels_nonzero(mask
);
382 /* Inherit master's labels to the related connection? */
383 master_cl
= ct
->master
? nf_ct_labels_find(ct
->master
) : NULL
;
385 if (!master_cl
&& !have_mask
)
386 return 0; /* Nothing to do. */
388 cl
= ovs_ct_get_conn_labels(ct
);
392 /* Inherit the master's labels, if any. */
397 u32
*dst
= (u32
*)cl
->bits
;
400 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
401 dst
[i
] = (dst
[i
] & ~mask
->ct_labels_32
[i
]) |
402 (labels
->ct_labels_32
[i
]
403 & mask
->ct_labels_32
[i
]);
406 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
407 * IPCT_LABEL bit is set in the event cache.
409 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
411 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
416 static int ovs_ct_set_labels(struct nf_conn
*ct
, struct sw_flow_key
*key
,
417 const struct ovs_key_ct_labels
*labels
,
418 const struct ovs_key_ct_labels
*mask
)
420 struct nf_conn_labels
*cl
;
423 cl
= ovs_ct_get_conn_labels(ct
);
427 err
= nf_connlabels_replace(ct
, labels
->ct_labels_32
,
429 OVS_CT_LABELS_LEN_32
);
433 memcpy(&key
->ct
.labels
, cl
->bits
, OVS_CT_LABELS_LEN
);
438 /* 'skb' should already be pulled to nh_ofs. */
439 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
441 const struct nf_conntrack_helper
*helper
;
442 const struct nf_conn_help
*help
;
443 enum ip_conntrack_info ctinfo
;
444 unsigned int protoff
;
448 ct
= nf_ct_get(skb
, &ctinfo
);
449 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
452 help
= nfct_help(ct
);
456 helper
= rcu_dereference(help
->helper
);
462 protoff
= ip_hdrlen(skb
);
465 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
469 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
471 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
472 pr_debug("proto header not found\n");
479 WARN_ONCE(1, "helper invoked on non-IP family!");
483 err
= helper
->help(skb
, protoff
, ct
, ctinfo
);
484 if (err
!= NF_ACCEPT
)
487 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
488 * FTP with NAT) adusting the TCP payload size when mangling IP
489 * addresses and/or port numbers in the text-based control connection.
491 if (test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
) &&
492 !nf_ct_seq_adjust(skb
, ct
, ctinfo
, protoff
))
497 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
498 * value if 'skb' is freed.
500 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
501 u16 zone
, struct sk_buff
*skb
)
503 struct ovs_skb_cb ovs_cb
= *OVS_CB(skb
);
506 if (key
->eth
.type
== htons(ETH_P_IP
)) {
507 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
509 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
510 err
= ip_defrag(net
, skb
, user
);
514 ovs_cb
.mru
= IPCB(skb
)->frag_max_size
;
515 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
516 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
517 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
519 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
520 err
= nf_ct_frag6_gather(net
, skb
, user
);
522 if (err
!= -EINPROGRESS
)
527 key
->ip
.proto
= ipv6_hdr(skb
)->nexthdr
;
528 ovs_cb
.mru
= IP6CB(skb
)->frag_max_size
;
532 return -EPFNOSUPPORT
;
535 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
538 *OVS_CB(skb
) = ovs_cb
;
543 static struct nf_conntrack_expect
*
544 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
545 u16 proto
, const struct sk_buff
*skb
)
547 struct nf_conntrack_tuple tuple
;
548 struct nf_conntrack_expect
*exp
;
550 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
553 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
555 struct nf_conntrack_tuple_hash
*h
;
557 /* Delete existing conntrack entry, if it clashes with the
558 * expectation. This can happen since conntrack ALGs do not
559 * check for clashes between (new) expectations and existing
560 * conntrack entries. nf_conntrack_in() will check the
561 * expectations only if a conntrack entry can not be found,
562 * which can lead to OVS finding the expectation (here) in the
563 * init direction, but which will not be removed by the
564 * nf_conntrack_in() call, if a matching conntrack entry is
565 * found instead. In this case all init direction packets
566 * would be reported as new related packets, while reply
567 * direction packets would be reported as un-related
568 * established packets.
570 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
572 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
574 nf_ct_delete(ct
, 0, 0);
575 nf_conntrack_put(&ct
->ct_general
);
582 /* This replicates logic from nf_conntrack_core.c that is not exported. */
583 static enum ip_conntrack_info
584 ovs_ct_get_info(const struct nf_conntrack_tuple_hash
*h
)
586 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
588 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
589 return IP_CT_ESTABLISHED_REPLY
;
590 /* Once we've had two way comms, always ESTABLISHED. */
591 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
592 return IP_CT_ESTABLISHED
;
593 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
594 return IP_CT_RELATED
;
598 /* Find an existing connection which this packet belongs to without
599 * re-attributing statistics or modifying the connection state. This allows an
600 * skb->_nfct lost due to an upcall to be recovered during actions execution.
602 * Must be called with rcu_read_lock.
604 * On success, populates skb->_nfct and returns the connection. Returns NULL
605 * if there is no existing entry.
607 static struct nf_conn
*
608 ovs_ct_find_existing(struct net
*net
, const struct nf_conntrack_zone
*zone
,
609 u8 l3num
, struct sk_buff
*skb
, bool natted
)
611 struct nf_conntrack_tuple tuple
;
612 struct nf_conntrack_tuple_hash
*h
;
615 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), l3num
,
617 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
621 /* Must invert the tuple if skb has been transformed by NAT. */
623 struct nf_conntrack_tuple inverse
;
625 if (!nf_ct_invert_tuple(&inverse
, &tuple
)) {
626 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
632 /* look for tuple match */
633 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
635 return NULL
; /* Not found. */
637 ct
= nf_ct_tuplehash_to_ctrack(h
);
639 /* Inverted packet tuple matches the reverse direction conntrack tuple,
640 * select the other tuplehash to get the right 'ctinfo' bits for this
644 h
= &ct
->tuplehash
[!h
->tuple
.dst
.dir
];
646 nf_ct_set(skb
, ct
, ovs_ct_get_info(h
));
651 struct nf_conn
*ovs_ct_executed(struct net
*net
,
652 const struct sw_flow_key
*key
,
653 const struct ovs_conntrack_info
*info
,
657 struct nf_conn
*ct
= NULL
;
659 /* If no ct, check if we have evidence that an existing conntrack entry
660 * might be found for this skb. This happens when we lose a skb->_nfct
661 * due to an upcall, or if the direction is being forced. If the
662 * connection was not confirmed, it is not cached and needs to be run
663 * through conntrack again.
665 *ct_executed
= (key
->ct_state
& OVS_CS_F_TRACKED
) &&
666 !(key
->ct_state
& OVS_CS_F_INVALID
) &&
667 (key
->ct_zone
== info
->zone
.id
);
669 if (*ct_executed
|| (!key
->ct_state
&& info
->force
)) {
670 ct
= ovs_ct_find_existing(net
, &info
->zone
, info
->family
, skb
,
678 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
679 static bool skb_nfct_cached(struct net
*net
,
680 const struct sw_flow_key
*key
,
681 const struct ovs_conntrack_info
*info
,
684 enum ip_conntrack_info ctinfo
;
686 bool ct_executed
= true;
688 ct
= nf_ct_get(skb
, &ctinfo
);
690 ct
= ovs_ct_executed(net
, key
, info
, skb
, &ct_executed
);
693 nf_ct_get(skb
, &ctinfo
);
697 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
699 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
702 struct nf_conn_help
*help
;
704 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
705 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
708 /* Force conntrack entry direction to the current packet? */
709 if (info
->force
&& CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
) {
710 /* Delete the conntrack entry if confirmed, else just release
713 if (nf_ct_is_confirmed(ct
))
714 nf_ct_delete(ct
, 0, 0);
716 nf_conntrack_put(&ct
->ct_general
);
717 nf_ct_set(skb
, NULL
, 0);
724 #if IS_ENABLED(CONFIG_NF_NAT)
725 /* Modelled after nf_nat_ipv[46]_fn().
726 * range is only used for new, uninitialized NAT state.
727 * Returns either NF_ACCEPT or NF_DROP.
729 static int ovs_ct_nat_execute(struct sk_buff
*skb
, struct nf_conn
*ct
,
730 enum ip_conntrack_info ctinfo
,
731 const struct nf_nat_range2
*range
,
732 enum nf_nat_manip_type maniptype
)
734 int hooknum
, nh_off
, err
= NF_ACCEPT
;
736 nh_off
= skb_network_offset(skb
);
737 skb_pull_rcsum(skb
, nh_off
);
739 /* See HOOK2MANIP(). */
740 if (maniptype
== NF_NAT_MANIP_SRC
)
741 hooknum
= NF_INET_LOCAL_IN
; /* Source NAT */
743 hooknum
= NF_INET_LOCAL_OUT
; /* Destination NAT */
747 case IP_CT_RELATED_REPLY
:
748 if (IS_ENABLED(CONFIG_NF_NAT
) &&
749 skb
->protocol
== htons(ETH_P_IP
) &&
750 ip_hdr(skb
)->protocol
== IPPROTO_ICMP
) {
751 if (!nf_nat_icmp_reply_translation(skb
, ct
, ctinfo
,
755 } else if (IS_ENABLED(CONFIG_IPV6
) &&
756 skb
->protocol
== htons(ETH_P_IPV6
)) {
758 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
759 int hdrlen
= ipv6_skip_exthdr(skb
,
760 sizeof(struct ipv6hdr
),
761 &nexthdr
, &frag_off
);
763 if (hdrlen
>= 0 && nexthdr
== IPPROTO_ICMPV6
) {
764 if (!nf_nat_icmpv6_reply_translation(skb
, ct
,
772 /* Non-ICMP, fall thru to initialize if needed. */
775 /* Seen it before? This can happen for loopback, retrans,
778 if (!nf_nat_initialized(ct
, maniptype
)) {
779 /* Initialize according to the NAT action. */
780 err
= (range
&& range
->flags
& NF_NAT_RANGE_MAP_IPS
)
781 /* Action is set up to establish a new
784 ? nf_nat_setup_info(ct
, range
, maniptype
)
785 : nf_nat_alloc_null_binding(ct
, hooknum
);
786 if (err
!= NF_ACCEPT
)
791 case IP_CT_ESTABLISHED
:
792 case IP_CT_ESTABLISHED_REPLY
:
800 err
= nf_nat_packet(ct
, ctinfo
, hooknum
, skb
);
802 skb_push(skb
, nh_off
);
803 skb_postpush_rcsum(skb
, skb
->data
, nh_off
);
808 static void ovs_nat_update_key(struct sw_flow_key
*key
,
809 const struct sk_buff
*skb
,
810 enum nf_nat_manip_type maniptype
)
812 if (maniptype
== NF_NAT_MANIP_SRC
) {
815 key
->ct_state
|= OVS_CS_F_SRC_NAT
;
816 if (key
->eth
.type
== htons(ETH_P_IP
))
817 key
->ipv4
.addr
.src
= ip_hdr(skb
)->saddr
;
818 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
819 memcpy(&key
->ipv6
.addr
.src
, &ipv6_hdr(skb
)->saddr
,
820 sizeof(key
->ipv6
.addr
.src
));
824 if (key
->ip
.proto
== IPPROTO_UDP
)
825 src
= udp_hdr(skb
)->source
;
826 else if (key
->ip
.proto
== IPPROTO_TCP
)
827 src
= tcp_hdr(skb
)->source
;
828 else if (key
->ip
.proto
== IPPROTO_SCTP
)
829 src
= sctp_hdr(skb
)->source
;
837 key
->ct_state
|= OVS_CS_F_DST_NAT
;
838 if (key
->eth
.type
== htons(ETH_P_IP
))
839 key
->ipv4
.addr
.dst
= ip_hdr(skb
)->daddr
;
840 else if (key
->eth
.type
== htons(ETH_P_IPV6
))
841 memcpy(&key
->ipv6
.addr
.dst
, &ipv6_hdr(skb
)->daddr
,
842 sizeof(key
->ipv6
.addr
.dst
));
846 if (key
->ip
.proto
== IPPROTO_UDP
)
847 dst
= udp_hdr(skb
)->dest
;
848 else if (key
->ip
.proto
== IPPROTO_TCP
)
849 dst
= tcp_hdr(skb
)->dest
;
850 else if (key
->ip
.proto
== IPPROTO_SCTP
)
851 dst
= sctp_hdr(skb
)->dest
;
859 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
860 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
861 const struct ovs_conntrack_info
*info
,
862 struct sk_buff
*skb
, struct nf_conn
*ct
,
863 enum ip_conntrack_info ctinfo
)
865 enum nf_nat_manip_type maniptype
;
868 /* Add NAT extension if not confirmed yet. */
869 if (!nf_ct_is_confirmed(ct
) && !nf_ct_nat_ext_add(ct
))
870 return NF_ACCEPT
; /* Can't NAT. */
872 /* Determine NAT type.
873 * Check if the NAT type can be deduced from the tracked connection.
874 * Make sure new expected connections (IP_CT_RELATED) are NATted only
877 if (info
->nat
& OVS_CT_NAT
&& ctinfo
!= IP_CT_NEW
&&
878 ct
->status
& IPS_NAT_MASK
&&
879 (ctinfo
!= IP_CT_RELATED
|| info
->commit
)) {
880 /* NAT an established or related connection like before. */
881 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
)
882 /* This is the REPLY direction for a connection
883 * for which NAT was applied in the forward
884 * direction. Do the reverse NAT.
886 maniptype
= ct
->status
& IPS_SRC_NAT
887 ? NF_NAT_MANIP_DST
: NF_NAT_MANIP_SRC
;
889 maniptype
= ct
->status
& IPS_SRC_NAT
890 ? NF_NAT_MANIP_SRC
: NF_NAT_MANIP_DST
;
891 } else if (info
->nat
& OVS_CT_SRC_NAT
) {
892 maniptype
= NF_NAT_MANIP_SRC
;
893 } else if (info
->nat
& OVS_CT_DST_NAT
) {
894 maniptype
= NF_NAT_MANIP_DST
;
896 return NF_ACCEPT
; /* Connection is not NATed. */
898 err
= ovs_ct_nat_execute(skb
, ct
, ctinfo
, &info
->range
, maniptype
);
900 /* Mark NAT done if successful and update the flow key. */
901 if (err
== NF_ACCEPT
)
902 ovs_nat_update_key(key
, skb
, maniptype
);
906 #else /* !CONFIG_NF_NAT */
907 static int ovs_ct_nat(struct net
*net
, struct sw_flow_key
*key
,
908 const struct ovs_conntrack_info
*info
,
909 struct sk_buff
*skb
, struct nf_conn
*ct
,
910 enum ip_conntrack_info ctinfo
)
916 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
917 * not done already. Update key with new CT state after passing the packet
919 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
920 * set to NULL and 0 will be returned.
922 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
923 const struct ovs_conntrack_info
*info
,
926 /* If we are recirculating packets to match on conntrack fields and
927 * committing with a separate conntrack action, then we don't need to
928 * actually run the packet through conntrack twice unless it's for a
931 bool cached
= skb_nfct_cached(net
, key
, info
, skb
);
932 enum ip_conntrack_info ctinfo
;
936 struct nf_hook_state state
= {
937 .hook
= NF_INET_PRE_ROUTING
,
941 struct nf_conn
*tmpl
= info
->ct
;
944 /* Associate skb with specified zone. */
947 nf_conntrack_put(skb_nfct(skb
));
948 nf_conntrack_get(&tmpl
->ct_general
);
949 nf_ct_set(skb
, tmpl
, IP_CT_NEW
);
952 err
= nf_conntrack_in(skb
, &state
);
953 if (err
!= NF_ACCEPT
)
956 /* Clear CT state NAT flags to mark that we have not yet done
957 * NAT after the nf_conntrack_in() call. We can actually clear
958 * the whole state, as it will be re-initialized below.
962 /* Update the key, but keep the NAT flags. */
963 ovs_ct_update_key(skb
, info
, key
, true, true);
966 ct
= nf_ct_get(skb
, &ctinfo
);
968 /* Packets starting a new connection must be NATted before the
969 * helper, so that the helper knows about the NAT. We enforce
970 * this by delaying both NAT and helper calls for unconfirmed
971 * connections until the committing CT action. For later
972 * packets NAT and Helper may be called in either order.
974 * NAT will be done only if the CT action has NAT, and only
975 * once per packet (per zone), as guarded by the NAT bits in
978 if (info
->nat
&& !(key
->ct_state
& OVS_CS_F_NAT_MASK
) &&
979 (nf_ct_is_confirmed(ct
) || info
->commit
) &&
980 ovs_ct_nat(net
, key
, info
, skb
, ct
, ctinfo
) != NF_ACCEPT
) {
984 /* Userspace may decide to perform a ct lookup without a helper
985 * specified followed by a (recirculate and) commit with one.
986 * Therefore, for unconfirmed connections which we will commit,
987 * we need to attach the helper here.
989 if (!nf_ct_is_confirmed(ct
) && info
->commit
&&
990 info
->helper
&& !nfct_help(ct
)) {
991 int err
= __nf_ct_try_assign_helper(ct
, info
->ct
,
996 /* helper installed, add seqadj if NAT is required */
997 if (info
->nat
&& !nfct_seqadj(ct
)) {
998 if (!nfct_seqadj_ext_add(ct
))
1003 /* Call the helper only if:
1004 * - nf_conntrack_in() was executed above ("!cached") for a
1005 * confirmed connection, or
1006 * - When committing an unconfirmed connection.
1008 if ((nf_ct_is_confirmed(ct
) ? !cached
: info
->commit
) &&
1009 ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
1017 /* Lookup connection and read fields into key. */
1018 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
1019 const struct ovs_conntrack_info
*info
,
1020 struct sk_buff
*skb
)
1022 struct nf_conntrack_expect
*exp
;
1024 /* If we pass an expected packet through nf_conntrack_in() the
1025 * expectation is typically removed, but the packet could still be
1026 * lost in upcall processing. To prevent this from happening we
1027 * perform an explicit expectation lookup. Expected connections are
1028 * always new, and will be passed through conntrack only when they are
1029 * committed, as it is OK to remove the expectation at that time.
1031 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
1035 /* NOTE: New connections are NATted and Helped only when
1036 * committed, so we are not calling into NAT here.
1038 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
1039 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
1044 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1048 ct
= (struct nf_conn
*)skb_nfct(skb
);
1050 nf_ct_deliver_cached_events(ct
);
1056 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
1060 for (i
= 0; i
< OVS_CT_LABELS_LEN_32
; i
++)
1061 if (labels
->ct_labels_32
[i
])
1067 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1068 static struct hlist_head
*ct_limit_hash_bucket(
1069 const struct ovs_ct_limit_info
*info
, u16 zone
)
1071 return &info
->limits
[zone
& (CT_LIMIT_HASH_BUCKETS
- 1)];
1074 /* Call with ovs_mutex */
1075 static void ct_limit_set(const struct ovs_ct_limit_info
*info
,
1076 struct ovs_ct_limit
*new_ct_limit
)
1078 struct ovs_ct_limit
*ct_limit
;
1079 struct hlist_head
*head
;
1081 head
= ct_limit_hash_bucket(info
, new_ct_limit
->zone
);
1082 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1083 if (ct_limit
->zone
== new_ct_limit
->zone
) {
1084 hlist_replace_rcu(&ct_limit
->hlist_node
,
1085 &new_ct_limit
->hlist_node
);
1086 kfree_rcu(ct_limit
, rcu
);
1091 hlist_add_head_rcu(&new_ct_limit
->hlist_node
, head
);
1094 /* Call with ovs_mutex */
1095 static void ct_limit_del(const struct ovs_ct_limit_info
*info
, u16 zone
)
1097 struct ovs_ct_limit
*ct_limit
;
1098 struct hlist_head
*head
;
1099 struct hlist_node
*n
;
1101 head
= ct_limit_hash_bucket(info
, zone
);
1102 hlist_for_each_entry_safe(ct_limit
, n
, head
, hlist_node
) {
1103 if (ct_limit
->zone
== zone
) {
1104 hlist_del_rcu(&ct_limit
->hlist_node
);
1105 kfree_rcu(ct_limit
, rcu
);
1111 /* Call with RCU read lock */
1112 static u32
ct_limit_get(const struct ovs_ct_limit_info
*info
, u16 zone
)
1114 struct ovs_ct_limit
*ct_limit
;
1115 struct hlist_head
*head
;
1117 head
= ct_limit_hash_bucket(info
, zone
);
1118 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
1119 if (ct_limit
->zone
== zone
)
1120 return ct_limit
->limit
;
1123 return info
->default_limit
;
1126 static int ovs_ct_check_limit(struct net
*net
,
1127 const struct ovs_conntrack_info
*info
,
1128 const struct nf_conntrack_tuple
*tuple
)
1130 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1131 const struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
1132 u32 per_zone_limit
, connections
;
1135 conncount_key
= info
->zone
.id
;
1137 per_zone_limit
= ct_limit_get(ct_limit_info
, info
->zone
.id
);
1138 if (per_zone_limit
== OVS_CT_LIMIT_UNLIMITED
)
1141 connections
= nf_conncount_count(net
, ct_limit_info
->data
,
1142 &conncount_key
, tuple
, &info
->zone
);
1143 if (connections
> per_zone_limit
)
1150 /* Lookup connection and confirm if unconfirmed. */
1151 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
1152 const struct ovs_conntrack_info
*info
,
1153 struct sk_buff
*skb
)
1155 enum ip_conntrack_info ctinfo
;
1159 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
1163 /* The connection could be invalid, in which case this is a no-op.*/
1164 ct
= nf_ct_get(skb
, &ctinfo
);
1168 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1169 if (static_branch_unlikely(&ovs_ct_limit_enabled
)) {
1170 if (!nf_ct_is_confirmed(ct
)) {
1171 err
= ovs_ct_check_limit(net
, info
,
1172 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
1174 net_warn_ratelimited("openvswitch: zone: %u "
1175 "exceeds conntrack limit\n",
1183 /* Set the conntrack event mask if given. NEW and DELETE events have
1184 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1185 * typically would receive many kinds of updates. Setting the event
1186 * mask allows those events to be filtered. The set event mask will
1187 * remain in effect for the lifetime of the connection unless changed
1188 * by a further CT action with both the commit flag and the eventmask
1190 if (info
->have_eventmask
) {
1191 struct nf_conntrack_ecache
*cache
= nf_ct_ecache_find(ct
);
1194 cache
->ctmask
= info
->eventmask
;
1197 /* Apply changes before confirming the connection so that the initial
1198 * conntrack NEW netlink event carries the values given in the CT
1201 if (info
->mark
.mask
) {
1202 err
= ovs_ct_set_mark(ct
, key
, info
->mark
.value
,
1207 if (!nf_ct_is_confirmed(ct
)) {
1208 err
= ovs_ct_init_labels(ct
, key
, &info
->labels
.value
,
1209 &info
->labels
.mask
);
1212 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1213 labels_nonzero(&info
->labels
.mask
)) {
1214 err
= ovs_ct_set_labels(ct
, key
, &info
->labels
.value
,
1215 &info
->labels
.mask
);
1219 /* This will take care of sending queued events even if the connection
1220 * is already confirmed.
1222 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
1228 /* Trim the skb to the length specified by the IP/IPv6 header,
1229 * removing any trailing lower-layer padding. This prepares the skb
1230 * for higher-layer processing that assumes skb->len excludes padding
1231 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1232 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1234 static int ovs_skb_network_trim(struct sk_buff
*skb
)
1239 switch (skb
->protocol
) {
1240 case htons(ETH_P_IP
):
1241 len
= ntohs(ip_hdr(skb
)->tot_len
);
1243 case htons(ETH_P_IPV6
):
1244 len
= sizeof(struct ipv6hdr
)
1245 + ntohs(ipv6_hdr(skb
)->payload_len
);
1251 err
= pskb_trim_rcsum(skb
, len
);
1258 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1259 * value if 'skb' is freed.
1261 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
1262 struct sw_flow_key
*key
,
1263 const struct ovs_conntrack_info
*info
)
1268 /* The conntrack module expects to be working at L3. */
1269 nh_ofs
= skb_network_offset(skb
);
1270 skb_pull_rcsum(skb
, nh_ofs
);
1272 err
= ovs_skb_network_trim(skb
);
1276 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
1277 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
1283 err
= ovs_ct_commit(net
, key
, info
, skb
);
1285 err
= ovs_ct_lookup(net
, key
, info
, skb
);
1287 skb_push(skb
, nh_ofs
);
1288 skb_postpush_rcsum(skb
, skb
->data
, nh_ofs
);
1294 int ovs_ct_clear(struct sk_buff
*skb
, struct sw_flow_key
*key
)
1296 if (skb_nfct(skb
)) {
1297 nf_conntrack_put(skb_nfct(skb
));
1298 nf_ct_set(skb
, NULL
, IP_CT_UNTRACKED
);
1299 ovs_ct_fill_key(skb
, key
);
1305 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
1306 const struct sw_flow_key
*key
, bool log
)
1308 struct nf_conntrack_helper
*helper
;
1309 struct nf_conn_help
*help
;
1312 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
1315 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
1319 help
= nf_ct_helper_ext_add(info
->ct
, GFP_KERNEL
);
1321 nf_conntrack_helper_put(helper
);
1325 #if IS_ENABLED(CONFIG_NF_NAT)
1327 ret
= nf_nat_helper_try_module_get(name
, info
->family
,
1330 nf_conntrack_helper_put(helper
);
1331 OVS_NLERR(log
, "Failed to load \"%s\" NAT helper, error: %d",
1337 rcu_assign_pointer(help
->helper
, helper
);
1338 info
->helper
= helper
;
1342 #if IS_ENABLED(CONFIG_NF_NAT)
1343 static int parse_nat(const struct nlattr
*attr
,
1344 struct ovs_conntrack_info
*info
, bool log
)
1348 bool have_ip_max
= false;
1349 bool have_proto_max
= false;
1350 bool ip_vers
= (info
->family
== NFPROTO_IPV6
);
1352 nla_for_each_nested(a
, attr
, rem
) {
1353 static const int ovs_nat_attr_lens
[OVS_NAT_ATTR_MAX
+ 1][2] = {
1354 [OVS_NAT_ATTR_SRC
] = {0, 0},
1355 [OVS_NAT_ATTR_DST
] = {0, 0},
1356 [OVS_NAT_ATTR_IP_MIN
] = {sizeof(struct in_addr
),
1357 sizeof(struct in6_addr
)},
1358 [OVS_NAT_ATTR_IP_MAX
] = {sizeof(struct in_addr
),
1359 sizeof(struct in6_addr
)},
1360 [OVS_NAT_ATTR_PROTO_MIN
] = {sizeof(u16
), sizeof(u16
)},
1361 [OVS_NAT_ATTR_PROTO_MAX
] = {sizeof(u16
), sizeof(u16
)},
1362 [OVS_NAT_ATTR_PERSISTENT
] = {0, 0},
1363 [OVS_NAT_ATTR_PROTO_HASH
] = {0, 0},
1364 [OVS_NAT_ATTR_PROTO_RANDOM
] = {0, 0},
1366 int type
= nla_type(a
);
1368 if (type
> OVS_NAT_ATTR_MAX
) {
1369 OVS_NLERR(log
, "Unknown NAT attribute (type=%d, max=%d)",
1370 type
, OVS_NAT_ATTR_MAX
);
1374 if (nla_len(a
) != ovs_nat_attr_lens
[type
][ip_vers
]) {
1375 OVS_NLERR(log
, "NAT attribute type %d has unexpected length (%d != %d)",
1377 ovs_nat_attr_lens
[type
][ip_vers
]);
1382 case OVS_NAT_ATTR_SRC
:
1383 case OVS_NAT_ATTR_DST
:
1385 OVS_NLERR(log
, "Only one type of NAT may be specified");
1388 info
->nat
|= OVS_CT_NAT
;
1389 info
->nat
|= ((type
== OVS_NAT_ATTR_SRC
)
1390 ? OVS_CT_SRC_NAT
: OVS_CT_DST_NAT
);
1393 case OVS_NAT_ATTR_IP_MIN
:
1394 nla_memcpy(&info
->range
.min_addr
, a
,
1395 sizeof(info
->range
.min_addr
));
1396 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1399 case OVS_NAT_ATTR_IP_MAX
:
1401 nla_memcpy(&info
->range
.max_addr
, a
,
1402 sizeof(info
->range
.max_addr
));
1403 info
->range
.flags
|= NF_NAT_RANGE_MAP_IPS
;
1406 case OVS_NAT_ATTR_PROTO_MIN
:
1407 info
->range
.min_proto
.all
= htons(nla_get_u16(a
));
1408 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1411 case OVS_NAT_ATTR_PROTO_MAX
:
1412 have_proto_max
= true;
1413 info
->range
.max_proto
.all
= htons(nla_get_u16(a
));
1414 info
->range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
1417 case OVS_NAT_ATTR_PERSISTENT
:
1418 info
->range
.flags
|= NF_NAT_RANGE_PERSISTENT
;
1421 case OVS_NAT_ATTR_PROTO_HASH
:
1422 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM
;
1425 case OVS_NAT_ATTR_PROTO_RANDOM
:
1426 info
->range
.flags
|= NF_NAT_RANGE_PROTO_RANDOM_FULLY
;
1430 OVS_NLERR(log
, "Unknown nat attribute (%d)", type
);
1436 OVS_NLERR(log
, "NAT attribute has %d unknown bytes", rem
);
1440 /* Do not allow flags if no type is given. */
1441 if (info
->range
.flags
) {
1443 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1447 info
->nat
= OVS_CT_NAT
; /* NAT existing connections. */
1448 } else if (!info
->commit
) {
1450 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1454 /* Allow missing IP_MAX. */
1455 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
&& !have_ip_max
) {
1456 memcpy(&info
->range
.max_addr
, &info
->range
.min_addr
,
1457 sizeof(info
->range
.max_addr
));
1459 /* Allow missing PROTO_MAX. */
1460 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1462 info
->range
.max_proto
.all
= info
->range
.min_proto
.all
;
1468 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
1469 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1470 [OVS_CT_ATTR_FORCE_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
1471 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
1472 .maxlen
= sizeof(u16
) },
1473 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
1474 .maxlen
= sizeof(struct md_mark
) },
1475 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
1476 .maxlen
= sizeof(struct md_labels
) },
1477 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
1478 .maxlen
= NF_CT_HELPER_NAME_LEN
},
1479 #if IS_ENABLED(CONFIG_NF_NAT)
1480 /* NAT length is checked when parsing the nested attributes. */
1481 [OVS_CT_ATTR_NAT
] = { .minlen
= 0, .maxlen
= INT_MAX
},
1483 [OVS_CT_ATTR_EVENTMASK
] = { .minlen
= sizeof(u32
),
1484 .maxlen
= sizeof(u32
) },
1485 [OVS_CT_ATTR_TIMEOUT
] = { .minlen
= 1,
1486 .maxlen
= CTNL_TIMEOUT_NAME_MAX
},
1489 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
1490 const char **helper
, bool log
)
1495 nla_for_each_nested(a
, attr
, rem
) {
1496 int type
= nla_type(a
);
1500 if (type
> OVS_CT_ATTR_MAX
) {
1502 "Unknown conntrack attr (type=%d, max=%d)",
1503 type
, OVS_CT_ATTR_MAX
);
1507 maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
1508 minlen
= ovs_ct_attr_lens
[type
].minlen
;
1509 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
1511 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1512 type
, nla_len(a
), maxlen
);
1517 case OVS_CT_ATTR_FORCE_COMMIT
:
1520 case OVS_CT_ATTR_COMMIT
:
1521 info
->commit
= true;
1523 #ifdef CONFIG_NF_CONNTRACK_ZONES
1524 case OVS_CT_ATTR_ZONE
:
1525 info
->zone
.id
= nla_get_u16(a
);
1528 #ifdef CONFIG_NF_CONNTRACK_MARK
1529 case OVS_CT_ATTR_MARK
: {
1530 struct md_mark
*mark
= nla_data(a
);
1533 OVS_NLERR(log
, "ct_mark mask cannot be 0");
1540 #ifdef CONFIG_NF_CONNTRACK_LABELS
1541 case OVS_CT_ATTR_LABELS
: {
1542 struct md_labels
*labels
= nla_data(a
);
1544 if (!labels_nonzero(&labels
->mask
)) {
1545 OVS_NLERR(log
, "ct_labels mask cannot be 0");
1548 info
->labels
= *labels
;
1552 case OVS_CT_ATTR_HELPER
:
1553 *helper
= nla_data(a
);
1554 if (!memchr(*helper
, '\0', nla_len(a
))) {
1555 OVS_NLERR(log
, "Invalid conntrack helper");
1559 #if IS_ENABLED(CONFIG_NF_NAT)
1560 case OVS_CT_ATTR_NAT
: {
1561 int err
= parse_nat(a
, info
, log
);
1568 case OVS_CT_ATTR_EVENTMASK
:
1569 info
->have_eventmask
= true;
1570 info
->eventmask
= nla_get_u32(a
);
1572 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1573 case OVS_CT_ATTR_TIMEOUT
:
1574 memcpy(info
->timeout
, nla_data(a
), nla_len(a
));
1575 if (!memchr(info
->timeout
, '\0', nla_len(a
))) {
1576 OVS_NLERR(log
, "Invalid conntrack helper");
1583 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
1589 #ifdef CONFIG_NF_CONNTRACK_MARK
1590 if (!info
->commit
&& info
->mark
.mask
) {
1592 "Setting conntrack mark requires 'commit' flag.");
1596 #ifdef CONFIG_NF_CONNTRACK_LABELS
1597 if (!info
->commit
&& labels_nonzero(&info
->labels
.mask
)) {
1599 "Setting conntrack labels requires 'commit' flag.");
1604 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
1611 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
1613 if (attr
== OVS_KEY_ATTR_CT_STATE
)
1615 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1616 attr
== OVS_KEY_ATTR_CT_ZONE
)
1618 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
1619 attr
== OVS_KEY_ATTR_CT_MARK
)
1621 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1622 attr
== OVS_KEY_ATTR_CT_LABELS
) {
1623 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1625 return ovs_net
->xt_label
;
1631 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
1632 const struct sw_flow_key
*key
,
1633 struct sw_flow_actions
**sfa
, bool log
)
1635 struct ovs_conntrack_info ct_info
;
1636 const char *helper
= NULL
;
1640 family
= key_to_nfproto(key
);
1641 if (family
== NFPROTO_UNSPEC
) {
1642 OVS_NLERR(log
, "ct family unspecified");
1646 memset(&ct_info
, 0, sizeof(ct_info
));
1647 ct_info
.family
= family
;
1649 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
1650 NF_CT_DEFAULT_ZONE_DIR
, 0);
1652 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
1656 /* Set up template for tracking connections in specific zones. */
1657 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
1659 OVS_NLERR(log
, "Failed to allocate conntrack template");
1663 if (ct_info
.timeout
[0]) {
1664 if (nf_ct_set_timeout(net
, ct_info
.ct
, family
, key
->ip
.proto
,
1666 pr_info_ratelimited("Failed to associated timeout "
1667 "policy `%s'\n", ct_info
.timeout
);
1671 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
1676 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
1677 sizeof(ct_info
), log
);
1681 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
1682 nf_conntrack_get(&ct_info
.ct
->ct_general
);
1685 __ovs_ct_free_action(&ct_info
);
1689 #if IS_ENABLED(CONFIG_NF_NAT)
1690 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info
*info
,
1691 struct sk_buff
*skb
)
1693 struct nlattr
*start
;
1695 start
= nla_nest_start_noflag(skb
, OVS_CT_ATTR_NAT
);
1699 if (info
->nat
& OVS_CT_SRC_NAT
) {
1700 if (nla_put_flag(skb
, OVS_NAT_ATTR_SRC
))
1702 } else if (info
->nat
& OVS_CT_DST_NAT
) {
1703 if (nla_put_flag(skb
, OVS_NAT_ATTR_DST
))
1709 if (info
->range
.flags
& NF_NAT_RANGE_MAP_IPS
) {
1710 if (IS_ENABLED(CONFIG_NF_NAT
) &&
1711 info
->family
== NFPROTO_IPV4
) {
1712 if (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1713 info
->range
.min_addr
.ip
) ||
1714 (info
->range
.max_addr
.ip
1715 != info
->range
.min_addr
.ip
&&
1716 (nla_put_in_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1717 info
->range
.max_addr
.ip
))))
1719 } else if (IS_ENABLED(CONFIG_IPV6
) &&
1720 info
->family
== NFPROTO_IPV6
) {
1721 if (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MIN
,
1722 &info
->range
.min_addr
.in6
) ||
1723 (memcmp(&info
->range
.max_addr
.in6
,
1724 &info
->range
.min_addr
.in6
,
1725 sizeof(info
->range
.max_addr
.in6
)) &&
1726 (nla_put_in6_addr(skb
, OVS_NAT_ATTR_IP_MAX
,
1727 &info
->range
.max_addr
.in6
))))
1733 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_SPECIFIED
&&
1734 (nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MIN
,
1735 ntohs(info
->range
.min_proto
.all
)) ||
1736 (info
->range
.max_proto
.all
!= info
->range
.min_proto
.all
&&
1737 nla_put_u16(skb
, OVS_NAT_ATTR_PROTO_MAX
,
1738 ntohs(info
->range
.max_proto
.all
)))))
1741 if (info
->range
.flags
& NF_NAT_RANGE_PERSISTENT
&&
1742 nla_put_flag(skb
, OVS_NAT_ATTR_PERSISTENT
))
1744 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM
&&
1745 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_HASH
))
1747 if (info
->range
.flags
& NF_NAT_RANGE_PROTO_RANDOM_FULLY
&&
1748 nla_put_flag(skb
, OVS_NAT_ATTR_PROTO_RANDOM
))
1751 nla_nest_end(skb
, start
);
1757 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
1758 struct sk_buff
*skb
)
1760 struct nlattr
*start
;
1762 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_CT
);
1766 if (ct_info
->commit
&& nla_put_flag(skb
, ct_info
->force
1767 ? OVS_CT_ATTR_FORCE_COMMIT
1768 : OVS_CT_ATTR_COMMIT
))
1770 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
1771 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
1773 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
1774 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
1777 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
1778 labels_nonzero(&ct_info
->labels
.mask
) &&
1779 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
1782 if (ct_info
->helper
) {
1783 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
1784 ct_info
->helper
->name
))
1787 if (ct_info
->have_eventmask
&&
1788 nla_put_u32(skb
, OVS_CT_ATTR_EVENTMASK
, ct_info
->eventmask
))
1790 if (ct_info
->timeout
[0]) {
1791 if (nla_put_string(skb
, OVS_CT_ATTR_TIMEOUT
, ct_info
->timeout
))
1795 #if IS_ENABLED(CONFIG_NF_NAT)
1796 if (ct_info
->nat
&& !ovs_ct_nat_to_attr(ct_info
, skb
))
1799 nla_nest_end(skb
, start
);
1804 void ovs_ct_free_action(const struct nlattr
*a
)
1806 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
1808 __ovs_ct_free_action(ct_info
);
1811 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
)
1813 if (ct_info
->helper
) {
1814 #if IS_ENABLED(CONFIG_NF_NAT)
1816 nf_nat_helper_put(ct_info
->helper
);
1818 nf_conntrack_helper_put(ct_info
->helper
);
1821 if (ct_info
->timeout
[0])
1822 nf_ct_destroy_timeout(ct_info
->ct
);
1823 nf_ct_tmpl_free(ct_info
->ct
);
1827 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1828 static int ovs_ct_limit_init(struct net
*net
, struct ovs_net
*ovs_net
)
1832 ovs_net
->ct_limit_info
= kmalloc(sizeof(*ovs_net
->ct_limit_info
),
1834 if (!ovs_net
->ct_limit_info
)
1837 ovs_net
->ct_limit_info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1838 ovs_net
->ct_limit_info
->limits
=
1839 kmalloc_array(CT_LIMIT_HASH_BUCKETS
, sizeof(struct hlist_head
),
1841 if (!ovs_net
->ct_limit_info
->limits
) {
1842 kfree(ovs_net
->ct_limit_info
);
1846 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; i
++)
1847 INIT_HLIST_HEAD(&ovs_net
->ct_limit_info
->limits
[i
]);
1849 ovs_net
->ct_limit_info
->data
=
1850 nf_conncount_init(net
, NFPROTO_INET
, sizeof(u32
));
1852 if (IS_ERR(ovs_net
->ct_limit_info
->data
)) {
1853 err
= PTR_ERR(ovs_net
->ct_limit_info
->data
);
1854 kfree(ovs_net
->ct_limit_info
->limits
);
1855 kfree(ovs_net
->ct_limit_info
);
1856 pr_err("openvswitch: failed to init nf_conncount %d\n", err
);
1862 static void ovs_ct_limit_exit(struct net
*net
, struct ovs_net
*ovs_net
)
1864 const struct ovs_ct_limit_info
*info
= ovs_net
->ct_limit_info
;
1867 nf_conncount_destroy(net
, NFPROTO_INET
, info
->data
);
1868 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
1869 struct hlist_head
*head
= &info
->limits
[i
];
1870 struct ovs_ct_limit
*ct_limit
;
1872 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
)
1873 kfree_rcu(ct_limit
, rcu
);
1875 kfree(ovs_net
->ct_limit_info
->limits
);
1876 kfree(ovs_net
->ct_limit_info
);
1879 static struct sk_buff
*
1880 ovs_ct_limit_cmd_reply_start(struct genl_info
*info
, u8 cmd
,
1881 struct ovs_header
**ovs_reply_header
)
1883 struct ovs_header
*ovs_header
= info
->userhdr
;
1884 struct sk_buff
*skb
;
1886 skb
= genlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1888 return ERR_PTR(-ENOMEM
);
1890 *ovs_reply_header
= genlmsg_put(skb
, info
->snd_portid
,
1892 &dp_ct_limit_genl_family
, 0, cmd
);
1894 if (!*ovs_reply_header
) {
1896 return ERR_PTR(-EMSGSIZE
);
1898 (*ovs_reply_header
)->dp_ifindex
= ovs_header
->dp_ifindex
;
1903 static bool check_zone_id(int zone_id
, u16
*pzone
)
1905 if (zone_id
>= 0 && zone_id
<= 65535) {
1906 *pzone
= (u16
)zone_id
;
1912 static int ovs_ct_limit_set_zone_limit(struct nlattr
*nla_zone_limit
,
1913 struct ovs_ct_limit_info
*info
)
1915 struct ovs_zone_limit
*zone_limit
;
1919 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1920 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1922 while (rem
>= sizeof(*zone_limit
)) {
1923 if (unlikely(zone_limit
->zone_id
==
1924 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1926 info
->default_limit
= zone_limit
->limit
;
1928 } else if (unlikely(!check_zone_id(
1929 zone_limit
->zone_id
, &zone
))) {
1930 OVS_NLERR(true, "zone id is out of range");
1932 struct ovs_ct_limit
*ct_limit
;
1934 ct_limit
= kmalloc(sizeof(*ct_limit
), GFP_KERNEL
);
1938 ct_limit
->zone
= zone
;
1939 ct_limit
->limit
= zone_limit
->limit
;
1942 ct_limit_set(info
, ct_limit
);
1945 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
1946 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
1947 NLA_ALIGN(sizeof(*zone_limit
)));
1951 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem
);
1956 static int ovs_ct_limit_del_zone_limit(struct nlattr
*nla_zone_limit
,
1957 struct ovs_ct_limit_info
*info
)
1959 struct ovs_zone_limit
*zone_limit
;
1963 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
1964 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
1966 while (rem
>= sizeof(*zone_limit
)) {
1967 if (unlikely(zone_limit
->zone_id
==
1968 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
1970 info
->default_limit
= OVS_CT_LIMIT_DEFAULT
;
1972 } else if (unlikely(!check_zone_id(
1973 zone_limit
->zone_id
, &zone
))) {
1974 OVS_NLERR(true, "zone id is out of range");
1977 ct_limit_del(info
, zone
);
1980 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
1981 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
1982 NLA_ALIGN(sizeof(*zone_limit
)));
1986 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem
);
1991 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info
*info
,
1992 struct sk_buff
*reply
)
1994 struct ovs_zone_limit zone_limit
;
1997 zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
1998 zone_limit
.limit
= info
->default_limit
;
1999 err
= nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2006 static int __ovs_ct_limit_get_zone_limit(struct net
*net
,
2007 struct nf_conncount_data
*data
,
2008 u16 zone_id
, u32 limit
,
2009 struct sk_buff
*reply
)
2011 struct nf_conntrack_zone ct_zone
;
2012 struct ovs_zone_limit zone_limit
;
2013 u32 conncount_key
= zone_id
;
2015 zone_limit
.zone_id
= zone_id
;
2016 zone_limit
.limit
= limit
;
2017 nf_ct_zone_init(&ct_zone
, zone_id
, NF_CT_DEFAULT_ZONE_DIR
, 0);
2019 zone_limit
.count
= nf_conncount_count(net
, data
, &conncount_key
, NULL
,
2021 return nla_put_nohdr(reply
, sizeof(zone_limit
), &zone_limit
);
2024 static int ovs_ct_limit_get_zone_limit(struct net
*net
,
2025 struct nlattr
*nla_zone_limit
,
2026 struct ovs_ct_limit_info
*info
,
2027 struct sk_buff
*reply
)
2029 struct ovs_zone_limit
*zone_limit
;
2034 rem
= NLA_ALIGN(nla_len(nla_zone_limit
));
2035 zone_limit
= (struct ovs_zone_limit
*)nla_data(nla_zone_limit
);
2037 while (rem
>= sizeof(*zone_limit
)) {
2038 if (unlikely(zone_limit
->zone_id
==
2039 OVS_ZONE_LIMIT_DEFAULT_ZONE
)) {
2040 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2043 } else if (unlikely(!check_zone_id(zone_limit
->zone_id
,
2045 OVS_NLERR(true, "zone id is out of range");
2048 limit
= ct_limit_get(info
, zone
);
2051 err
= __ovs_ct_limit_get_zone_limit(
2052 net
, info
->data
, zone
, limit
, reply
);
2056 rem
-= NLA_ALIGN(sizeof(*zone_limit
));
2057 zone_limit
= (struct ovs_zone_limit
*)((u8
*)zone_limit
+
2058 NLA_ALIGN(sizeof(*zone_limit
)));
2062 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem
);
2067 static int ovs_ct_limit_get_all_zone_limit(struct net
*net
,
2068 struct ovs_ct_limit_info
*info
,
2069 struct sk_buff
*reply
)
2071 struct ovs_ct_limit
*ct_limit
;
2072 struct hlist_head
*head
;
2075 err
= ovs_ct_limit_get_default_limit(info
, reply
);
2080 for (i
= 0; i
< CT_LIMIT_HASH_BUCKETS
; ++i
) {
2081 head
= &info
->limits
[i
];
2082 hlist_for_each_entry_rcu(ct_limit
, head
, hlist_node
) {
2083 err
= __ovs_ct_limit_get_zone_limit(net
, info
->data
,
2084 ct_limit
->zone
, ct_limit
->limit
, reply
);
2095 static int ovs_ct_limit_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2097 struct nlattr
**a
= info
->attrs
;
2098 struct sk_buff
*reply
;
2099 struct ovs_header
*ovs_reply_header
;
2100 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2101 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2104 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_SET
,
2107 return PTR_ERR(reply
);
2109 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2114 err
= ovs_ct_limit_set_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2119 static_branch_enable(&ovs_ct_limit_enabled
);
2121 genlmsg_end(reply
, ovs_reply_header
);
2122 return genlmsg_reply(reply
, info
);
2129 static int ovs_ct_limit_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2131 struct nlattr
**a
= info
->attrs
;
2132 struct sk_buff
*reply
;
2133 struct ovs_header
*ovs_reply_header
;
2134 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
2135 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2138 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_DEL
,
2141 return PTR_ERR(reply
);
2143 if (!a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2148 err
= ovs_ct_limit_del_zone_limit(a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
],
2153 genlmsg_end(reply
, ovs_reply_header
);
2154 return genlmsg_reply(reply
, info
);
2161 static int ovs_ct_limit_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2163 struct nlattr
**a
= info
->attrs
;
2164 struct nlattr
*nla_reply
;
2165 struct sk_buff
*reply
;
2166 struct ovs_header
*ovs_reply_header
;
2167 struct net
*net
= sock_net(skb
->sk
);
2168 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2169 struct ovs_ct_limit_info
*ct_limit_info
= ovs_net
->ct_limit_info
;
2172 reply
= ovs_ct_limit_cmd_reply_start(info
, OVS_CT_LIMIT_CMD_GET
,
2175 return PTR_ERR(reply
);
2177 nla_reply
= nla_nest_start_noflag(reply
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2183 if (a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2184 err
= ovs_ct_limit_get_zone_limit(
2185 net
, a
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
], ct_limit_info
,
2190 err
= ovs_ct_limit_get_all_zone_limit(net
, ct_limit_info
,
2196 nla_nest_end(reply
, nla_reply
);
2197 genlmsg_end(reply
, ovs_reply_header
);
2198 return genlmsg_reply(reply
, info
);
2205 static struct genl_ops ct_limit_genl_ops
[] = {
2206 { .cmd
= OVS_CT_LIMIT_CMD_SET
,
2207 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2208 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2210 .doit
= ovs_ct_limit_cmd_set
,
2212 { .cmd
= OVS_CT_LIMIT_CMD_DEL
,
2213 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2214 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
2216 .doit
= ovs_ct_limit_cmd_del
,
2218 { .cmd
= OVS_CT_LIMIT_CMD_GET
,
2219 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2220 .flags
= 0, /* OK for unprivileged users. */
2221 .doit
= ovs_ct_limit_cmd_get
,
2225 static const struct genl_multicast_group ovs_ct_limit_multicast_group
= {
2226 .name
= OVS_CT_LIMIT_MCGROUP
,
2229 struct genl_family dp_ct_limit_genl_family __ro_after_init
= {
2230 .hdrsize
= sizeof(struct ovs_header
),
2231 .name
= OVS_CT_LIMIT_FAMILY
,
2232 .version
= OVS_CT_LIMIT_VERSION
,
2233 .maxattr
= OVS_CT_LIMIT_ATTR_MAX
,
2234 .policy
= ct_limit_policy
,
2236 .parallel_ops
= true,
2237 .ops
= ct_limit_genl_ops
,
2238 .n_ops
= ARRAY_SIZE(ct_limit_genl_ops
),
2239 .mcgrps
= &ovs_ct_limit_multicast_group
,
2241 .module
= THIS_MODULE
,
2245 int ovs_ct_init(struct net
*net
)
2247 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
2248 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2250 if (nf_connlabels_get(net
, n_bits
- 1)) {
2251 ovs_net
->xt_label
= false;
2252 OVS_NLERR(true, "Failed to set connlabel length");
2254 ovs_net
->xt_label
= true;
2257 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2258 return ovs_ct_limit_init(net
, ovs_net
);
2264 void ovs_ct_exit(struct net
*net
)
2266 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2268 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2269 ovs_ct_limit_exit(net
, ovs_net
);
2272 if (ovs_net
->xt_label
)
2273 nf_connlabels_put(net
);