]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/conntrack.c
datapath: Use new header file net/ipv6_frag.h
[mirror_ovs.git] / datapath / conntrack.c
CommitLineData
a94ebc39
JS
1/*
2 * Copyright (c) 2015 Nicira, Inc.
3 *
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.
7 *
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.
12 */
13
14#include <linux/kconfig.h>
15#include <linux/version.h>
16
8063e095 17#if IS_ENABLED(CONFIG_NF_CONNTRACK)
a94ebc39
JS
18
19#include <linux/module.h>
20#include <linux/openvswitch.h>
f8f97cdc
JR
21#include <linux/tcp.h>
22#include <linux/udp.h>
23#include <linux/sctp.h>
cb2a5486 24#include <linux/static_key.h>
a94ebc39 25#include <net/ip.h>
cb2a5486 26#include <net/genetlink.h>
a94ebc39 27#include <net/netfilter/nf_conntrack_core.h>
cb2a5486 28#include <net/netfilter/nf_conntrack_count.h>
11251c17 29#include <net/netfilter/nf_conntrack_helper.h>
038e34ab 30#include <net/netfilter/nf_conntrack_labels.h>
f8f97cdc 31#include <net/netfilter/nf_conntrack_seqadj.h>
a94ebc39
JS
32#include <net/netfilter/nf_conntrack_zones.h>
33#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
7857a9b4 34#include <net/ipv6_frag.h>
a94ebc39 35
f8f97cdc
JR
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>
40#endif
41
a94ebc39
JS
42#include "datapath.h"
43#include "conntrack.h"
44#include "flow.h"
45#include "flow_netlink.h"
86c2eb45 46#include "gso.h"
a94ebc39 47
fdec3c17
YHW
48#ifndef HAVE_NF_NAT_RANGE2
49#define nf_nat_range2 nf_nat_range
50#endif
51
a94ebc39 52struct ovs_ct_len_tbl {
f8f97cdc
JR
53 int maxlen;
54 int minlen;
a94ebc39
JS
55};
56
372ce973
JS
57/* Metadata mark for masked write to conntrack mark */
58struct md_mark {
59 u32 value;
60 u32 mask;
61};
62
038e34ab 63/* Metadata label for masked write to conntrack label. */
c05e2094
JS
64struct md_labels {
65 struct ovs_key_ct_labels value;
66 struct ovs_key_ct_labels mask;
038e34ab
JS
67};
68
f8f97cdc
JR
69enum ovs_ct_nat {
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. */
73};
74
a94ebc39
JS
75/* Conntrack action context for execution. */
76struct ovs_conntrack_info {
11251c17 77 struct nf_conntrack_helper *helper;
a94ebc39
JS
78 struct nf_conntrack_zone zone;
79 struct nf_conn *ct;
c05e2094 80 u8 commit : 1;
f8f97cdc 81 u8 nat : 3; /* enum ovs_ct_nat */
9f1de150 82 u8 random_fully_compat : 1; /* bool */
b80e259f 83 u8 force : 1;
8e83854c 84 u8 have_eventmask : 1;
a94ebc39 85 u16 family;
8e83854c 86 u32 eventmask; /* Mask of 1 << IPCT_*. */
372ce973 87 struct md_mark mark;
c05e2094 88 struct md_labels labels;
f8f97cdc 89#ifdef CONFIG_NF_NAT_NEEDED
fdec3c17 90 struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */
f8f97cdc 91#endif
a94ebc39
JS
92};
93
cb2a5486
YHW
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
98static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled);
99
100struct ovs_ct_limit {
101 /* Elements in ovs_ct_limit_info->limits hash table */
102 struct hlist_node hlist_node;
103 struct rcu_head rcu;
104 u16 zone;
105 u32 limit;
106};
107
108struct ovs_ct_limit_info {
109 u32 default_limit;
110 struct hlist_head *limits;
111 struct nf_conncount_data *data;
112};
113
114static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = {
115 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, },
116};
117#endif
118
7162cae4
JR
119static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
120
11251c17
JS
121static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
122
a94ebc39
JS
123static u16 key_to_nfproto(const struct sw_flow_key *key)
124{
125 switch (ntohs(key->eth.type)) {
126 case ETH_P_IP:
127 return NFPROTO_IPV4;
128 case ETH_P_IPV6:
129 return NFPROTO_IPV6;
130 default:
131 return NFPROTO_UNSPEC;
132 }
133}
134
135/* Map SKB connection state into the values used by flow definition. */
136static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
137{
138 u8 ct_state = OVS_CS_F_TRACKED;
139
140 switch (ctinfo) {
141 case IP_CT_ESTABLISHED_REPLY:
142 case IP_CT_RELATED_REPLY:
a94ebc39
JS
143 ct_state |= OVS_CS_F_REPLY_DIR;
144 break;
145 default:
146 break;
147 }
148
149 switch (ctinfo) {
150 case IP_CT_ESTABLISHED:
151 case IP_CT_ESTABLISHED_REPLY:
152 ct_state |= OVS_CS_F_ESTABLISHED;
153 break;
154 case IP_CT_RELATED:
155 case IP_CT_RELATED_REPLY:
156 ct_state |= OVS_CS_F_RELATED;
157 break;
158 case IP_CT_NEW:
a94ebc39
JS
159 ct_state |= OVS_CS_F_NEW;
160 break;
161 default:
162 break;
163 }
164
165 return ct_state;
166}
167
c05e2094
JS
168static u32 ovs_ct_get_mark(const struct nf_conn *ct)
169{
170#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
171 return ct ? ct->mark : 0;
172#else
173 return 0;
174#endif
175}
176
237f9413
JR
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
180#endif
181
c05e2094
JS
182static void ovs_ct_get_labels(const struct nf_conn *ct,
183 struct ovs_key_ct_labels *labels)
038e34ab
JS
184{
185 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
186
237f9413
JR
187 if (cl)
188 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
189 else
c05e2094 190 memset(labels, 0, OVS_CT_LABELS_LEN);
038e34ab
JS
191}
192
c30b4cea
JR
193static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
194 const struct nf_conntrack_tuple *orig,
195 u8 icmp_proto)
196{
1963a885 197 key->ct_orig_proto = orig->dst.protonum;
c30b4cea
JR
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);
201 } else {
202 key->ct.orig_tp.src = orig->src.u.all;
203 key->ct.orig_tp.dst = orig->dst.u.all;
204 }
205}
206
a94ebc39 207static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
372ce973
JS
208 const struct nf_conntrack_zone *zone,
209 const struct nf_conn *ct)
a94ebc39 210{
1963a885
JR
211 key->ct_state = state;
212 key->ct_zone = zone->id;
c05e2094
JS
213 key->ct.mark = ovs_ct_get_mark(ct);
214 ovs_ct_get_labels(ct, &key->ct.labels);
c30b4cea
JR
215
216 if (ct) {
217 const struct nf_conntrack_tuple *orig;
218
219 /* Use the master if we have one. */
220 if (ct->master)
221 ct = ct->master;
222 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
223
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);
230 return;
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);
237 return;
238 }
239 }
1963a885 240 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
c30b4cea
JR
241 * original direction key fields.
242 */
1963a885 243 key->ct_orig_proto = 0;
a94ebc39
JS
244}
245
e3c42eb8 246/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
f8f97cdc
JR
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.
a94ebc39
JS
250 */
251static void ovs_ct_update_key(const struct sk_buff *skb,
f23593a1 252 const struct ovs_conntrack_info *info,
f8f97cdc
JR
253 struct sw_flow_key *key, bool post_ct,
254 bool keep_nat_flags)
a94ebc39
JS
255{
256 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
257 enum ip_conntrack_info ctinfo;
258 struct nf_conn *ct;
259 u8 state = 0;
260
261 ct = nf_ct_get(skb, &ctinfo);
262 if (ct) {
263 state = ovs_ct_get_state(ctinfo);
b0f251cd 264 /* All unconfirmed entries are NEW connections. */
c05e2094
JS
265 if (!nf_ct_is_confirmed(ct))
266 state |= OVS_CS_F_NEW;
b0f251cd
JR
267 /* OVS persists the related flag for the duration of the
268 * connection.
269 */
a94ebc39
JS
270 if (ct->master)
271 state |= OVS_CS_F_RELATED;
f8f97cdc 272 if (keep_nat_flags) {
1963a885 273 state |= key->ct_state & OVS_CS_F_NAT_MASK;
f8f97cdc
JR
274 } else {
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;
279 }
a94ebc39
JS
280 zone = nf_ct_zone(ct);
281 } else if (post_ct) {
282 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
f23593a1
JS
283 if (info)
284 zone = &info->zone;
a94ebc39 285 }
372ce973 286 __ovs_ct_update_key(key, state, zone, ct);
a94ebc39
JS
287}
288
b0f251cd
JR
289/* This is called to initialize CT key fields possibly coming in from the local
290 * stack.
291 */
a94ebc39
JS
292void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
293{
f8f97cdc 294 ovs_ct_update_key(skb, NULL, key, false, false);
a94ebc39
JS
295}
296
c30b4cea
JR
297#define IN6_ADDR_INITIALIZER(ADDR) \
298 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
299 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
300
301int ovs_ct_put_key(const struct sw_flow_key *swkey,
302 const struct sw_flow_key *output, struct sk_buff *skb)
a94ebc39 303{
1963a885 304 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
a94ebc39
JS
305 return -EMSGSIZE;
306
307 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1963a885 308 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
a94ebc39
JS
309 return -EMSGSIZE;
310
372ce973 311 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
c30b4cea 312 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
372ce973
JS
313 return -EMSGSIZE;
314
c05e2094 315 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
c30b4cea
JR
316 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
317 &output->ct.labels))
038e34ab
JS
318 return -EMSGSIZE;
319
1963a885 320 if (swkey->ct_orig_proto) {
c30b4cea
JR
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,
1963a885 327 output->ct_orig_proto,
c30b4cea
JR
328 };
329 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
330 sizeof(orig), &orig))
331 return -EMSGSIZE;
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,
1963a885 338 output->ct_orig_proto,
c30b4cea
JR
339 };
340 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
341 sizeof(orig), &orig))
342 return -EMSGSIZE;
343 }
344 }
345
372ce973
JS
346 return 0;
347}
348
efd40994 349static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
372ce973
JS
350 u32 ct_mark, u32 mask)
351{
c05e2094 352#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
372ce973
JS
353 u32 new_mark;
354
372ce973
JS
355 new_mark = ct_mark | (ct->mark & ~(mask));
356 if (ct->mark != new_mark) {
357 ct->mark = new_mark;
1b22e621
JR
358 if (nf_ct_is_confirmed(ct))
359 nf_conntrack_event_cache(IPCT_MARK, ct);
372ce973
JS
360 key->ct.mark = new_mark;
361 }
362
a94ebc39 363 return 0;
c05e2094
JS
364#else
365 return -ENOTSUPP;
366#endif
a94ebc39
JS
367}
368
efd40994 369static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
038e34ab 370{
038e34ab 371 struct nf_conn_labels *cl;
038e34ab
JS
372
373 cl = nf_ct_labels_find(ct);
374 if (!cl) {
375 nf_ct_labels_ext_add(ct);
376 cl = nf_ct_labels_find(ct);
377 }
9f04eabe 378
efd40994
JR
379 return cl;
380}
381
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.
385 */
386static 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)
389{
7162cae4
JR
390 struct nf_conn_labels *cl, *master_cl;
391 bool have_mask = labels_nonzero(mask);
392
393 /* Inherit master's labels to the related connection? */
394 master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
395
396 if (!master_cl && !have_mask)
397 return 0; /* Nothing to do. */
efd40994
JR
398
399 cl = ovs_ct_get_conn_labels(ct);
400 if (!cl)
038e34ab
JS
401 return -ENOSPC;
402
28033773
JR
403 /* Inherit the master's labels, if any. Must use memcpy for backport
404 * as struct assignment only copies the length field in older
405 * kernels.
406 */
7162cae4 407 if (master_cl)
28033773 408 memcpy(cl->bits, master_cl->bits, OVS_CT_LABELS_LEN);
7162cae4
JR
409
410 if (have_mask) {
411 u32 *dst = (u32 *)cl->bits;
412 int i;
413
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]);
418 }
1b22e621 419
efd40994 420 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
100f4e14 421 * IPCT_LABEL bit is set in the event cache.
efd40994
JR
422 */
423 nf_conntrack_event_cache(IPCT_LABEL, ct);
1b22e621 424
efd40994
JR
425 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
426
427 return 0;
428}
429
430static 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)
433{
434 struct nf_conn_labels *cl;
435 int err;
436
437 cl = ovs_ct_get_conn_labels(ct);
438 if (!cl)
439 return -ENOSPC;
440
441 err = nf_connlabels_replace(ct, labels->ct_labels_32,
442 mask->ct_labels_32,
443 OVS_CT_LABELS_LEN_32);
444 if (err)
445 return err;
446
447 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
038e34ab 448
038e34ab
JS
449 return 0;
450}
451
11251c17
JS
452/* 'skb' should already be pulled to nh_ofs. */
453static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
454{
455 const struct nf_conntrack_helper *helper;
456 const struct nf_conn_help *help;
457 enum ip_conntrack_info ctinfo;
458 unsigned int protoff;
459 struct nf_conn *ct;
4cc85f28 460 u8 nexthdr;
f8f97cdc 461 int err;
11251c17 462
3d47fa46
JH
463#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
464 bool dst_set = false;
465 struct rtable rt = { .rt_flags = 0 };
466#endif
467
11251c17
JS
468 ct = nf_ct_get(skb, &ctinfo);
469 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
470 return NF_ACCEPT;
471
472 help = nfct_help(ct);
473 if (!help)
474 return NF_ACCEPT;
475
476 helper = rcu_dereference(help->helper);
477 if (!helper)
478 return NF_ACCEPT;
479
480 switch (proto) {
481 case NFPROTO_IPV4:
482 protoff = ip_hdrlen(skb);
483 break;
484 case NFPROTO_IPV6: {
11251c17 485 __be16 frag_off;
c05e2094 486 int ofs;
11251c17 487
4cc85f28 488 nexthdr = ipv6_hdr(skb)->nexthdr;
c05e2094
JS
489 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
490 &frag_off);
491 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
11251c17
JS
492 pr_debug("proto header not found\n");
493 return NF_ACCEPT;
494 }
c05e2094 495 protoff = ofs;
11251c17
JS
496 break;
497 }
498 default:
499 WARN_ONCE(1, "helper invoked on non-IP family!");
500 return NF_DROP;
501 }
502
4cc85f28
JR
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.
3d47fa46
JH
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
4cc85f28 509 */
3d47fa46
JH
510 if (ct->status & IPS_NAT_MASK && skb->ip_summed != CHECKSUM_PARTIAL
511 && !skb_dst(skb)) {
512 dst_set = true;
513 skb_dst_set(skb, &rt.dst);
4cc85f28
JR
514 }
515#endif
f8f97cdc
JR
516 err = helper->help(skb, protoff, ct, ctinfo);
517 if (err != NF_ACCEPT)
518 return err;
519
3d47fa46
JH
520#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
521 if (dst_set)
522 skb_dst_set(skb, NULL);
523#endif
524
f8f97cdc
JR
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.
528 */
529 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
530 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
531 return NF_DROP;
532 return NF_ACCEPT;
11251c17
JS
533}
534
c05e2094
JS
535/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
536 * value if 'skb' is freed.
537 */
a94ebc39
JS
538static int handle_fragments(struct net *net, struct sw_flow_key *key,
539 u16 zone, struct sk_buff *skb)
540{
86c2eb45 541 struct ovs_gso_cb ovs_cb = *OVS_GSO_CB(skb);
2e602ea3 542 int err;
a94ebc39 543
a94ebc39
JS
544 if (key->eth.type == htons(ETH_P_IP)) {
545 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
a94ebc39
JS
546
547 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
39c0ff22 548 err = ip_defrag(net, skb, user);
a94ebc39
JS
549 if (err)
550 return err;
551
86c2eb45 552 ovs_cb.dp_cb.mru = IPCB(skb)->frag_max_size;
a94ebc39 553#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
c05e2094 554 } else if (key->eth.type == htons(ETH_P_IPV6)) {
a94ebc39 555 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
a94ebc39
JS
556
557 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
2e602ea3 558 err = nf_ct_frag6_gather(net, skb, user);
6b4fe5a9
DDP
559 if (err) {
560 if (err != -EINPROGRESS)
561 kfree_skb(skb);
2e602ea3 562 return err;
6b4fe5a9 563 }
a94ebc39 564
2e602ea3 565 key->ip.proto = ipv6_hdr(skb)->nexthdr;
86c2eb45 566 ovs_cb.dp_cb.mru = IP6CB(skb)->frag_max_size;
a94ebc39
JS
567#endif /* IP frag support */
568 } else {
c05e2094 569 kfree_skb(skb);
a94ebc39
JS
570 return -EPFNOSUPPORT;
571 }
572
573 key->ip.frag = OVS_FRAG_TYPE_NONE;
574 skb_clear_hash(skb);
575 skb->ignore_df = 1;
86c2eb45 576 *OVS_GSO_CB(skb) = ovs_cb;
a94ebc39
JS
577
578 return 0;
579}
580
581static struct nf_conntrack_expect *
582ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
583 u16 proto, const struct sk_buff *skb)
584{
585 struct nf_conntrack_tuple tuple;
3618f466 586 struct nf_conntrack_expect *exp;
a94ebc39 587
fa67f8e0 588 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
a94ebc39 589 return NULL;
3618f466
JR
590
591 exp = __nf_ct_expect_find(net, zone, &tuple);
592 if (exp) {
593 struct nf_conntrack_tuple_hash *h;
594
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.
607 */
608 h = nf_conntrack_find_get(net, zone, &tuple);
609 if (h) {
610 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
611
612 nf_ct_delete(ct, 0, 0);
613 nf_conntrack_put(&ct->ct_general);
614 }
615 }
616
617 return exp;
a94ebc39
JS
618}
619
3dd9e118
JR
620/* This replicates logic from nf_conntrack_core.c that is not exported. */
621static enum ip_conntrack_info
622ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
623{
624 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
625
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;
633 return IP_CT_NEW;
634}
635
636/* Find an existing connection which this packet belongs to without
637 * re-attributing statistics or modifying the connection state. This allows an
e3c42eb8 638 * skb->_nfct lost due to an upcall to be recovered during actions execution.
3dd9e118
JR
639 *
640 * Must be called with rcu_read_lock.
641 *
e3c42eb8
JR
642 * On success, populates skb->_nfct and returns the connection. Returns NULL
643 * if there is no existing entry.
3dd9e118
JR
644 */
645static struct nf_conn *
646ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
c0324e37 647 u8 l3num, struct sk_buff *skb, bool natted)
3dd9e118 648{
b81164d9
FW
649 const struct nf_conntrack_l3proto *l3proto;
650 const struct nf_conntrack_l4proto *l4proto;
3dd9e118
JR
651 struct nf_conntrack_tuple tuple;
652 struct nf_conntrack_tuple_hash *h;
3dd9e118
JR
653 struct nf_conn *ct;
654 unsigned int dataoff;
655 u8 protonum;
656
657 l3proto = __nf_ct_l3proto_find(l3num);
3dd9e118
JR
658 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
659 &protonum) <= 0) {
660 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
661 return NULL;
662 }
663 l4proto = __nf_ct_l4proto_find(l3num, protonum);
3dd9e118
JR
664 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
665 protonum, net, &tuple, l3proto, l4proto)) {
666 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
667 return NULL;
668 }
669
c0324e37
JR
670 /* Must invert the tuple if skb has been transformed by NAT. */
671 if (natted) {
672 struct nf_conntrack_tuple inverse;
673
674 if (!nf_ct_invert_tuple(&inverse, &tuple, l3proto, l4proto)) {
675 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
676 return NULL;
677 }
678 tuple = inverse;
679 }
680
3dd9e118
JR
681 /* look for tuple match */
682 h = nf_conntrack_find_get(net, zone, &tuple);
683 if (!h)
684 return NULL; /* Not found. */
685
686 ct = nf_ct_tuplehash_to_ctrack(h);
687
c0324e37
JR
688 /* Inverted packet tuple matches the reverse direction conntrack tuple,
689 * select the other tuplehash to get the right 'ctinfo' bits for this
690 * packet.
691 */
692 if (natted)
693 h = &ct->tuplehash[!h->tuple.dst.dir];
694
dfa791b2 695 nf_ct_set(skb, ct, ovs_ct_get_info(h));
3dd9e118
JR
696 return ct;
697}
698
4e7b535d
GR
699static
700struct nf_conn *ovs_ct_executed(struct net *net,
701 const struct sw_flow_key *key,
702 const struct ovs_conntrack_info *info,
703 struct sk_buff *skb,
704 bool *ct_executed)
705{
706 struct nf_conn *ct = NULL;
707
708 /* If no ct, check if we have evidence that an existing conntrack entry
709 * might be found for this skb. This happens when we lose a skb->_nfct
710 * due to an upcall, or if the direction is being forced. If the
711 * connection was not confirmed, it is not cached and needs to be run
712 * through conntrack again.
713 */
714 *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
715 !(key->ct_state & OVS_CS_F_INVALID) &&
716 (key->ct_zone == info->zone.id);
717
718 if (*ct_executed || (!key->ct_state && info->force)) {
719 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
720 !!(key->ct_state &
721 OVS_CS_F_NAT_MASK));
722 }
723
724 return ct;
725}
726
e3c42eb8 727/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
3dd9e118
JR
728static bool skb_nfct_cached(struct net *net,
729 const struct sw_flow_key *key,
730 const struct ovs_conntrack_info *info,
731 struct sk_buff *skb)
a94ebc39
JS
732{
733 enum ip_conntrack_info ctinfo;
734 struct nf_conn *ct;
4e7b535d 735 bool ct_executed = true;
a94ebc39
JS
736
737 ct = nf_ct_get(skb, &ctinfo);
738 if (!ct)
4e7b535d
GR
739 ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
740
741 if (ct)
742 nf_ct_get(skb, &ctinfo);
743 else
a94ebc39 744 return false;
4e7b535d 745
a94ebc39
JS
746 if (!net_eq(net, read_pnet(&ct->ct_net)))
747 return false;
748 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
749 return false;
11251c17
JS
750 if (info->helper) {
751 struct nf_conn_help *help;
752
753 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
754 if (help && rcu_access_pointer(help->helper) != info->helper)
755 return false;
756 }
b80e259f
JR
757 /* Force conntrack entry direction to the current packet? */
758 if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
759 /* Delete the conntrack entry if confirmed, else just release
760 * the reference.
761 */
762 if (nf_ct_is_confirmed(ct))
763 nf_ct_delete(ct, 0, 0);
ceb0a8a9
JR
764
765 nf_conntrack_put(&ct->ct_general);
b80e259f
JR
766 nf_ct_set(skb, NULL, 0);
767 return false;
768 }
a94ebc39 769
4e7b535d 770 return ct_executed;
a94ebc39
JS
771}
772
f8f97cdc
JR
773#ifdef CONFIG_NF_NAT_NEEDED
774/* Modelled after nf_nat_ipv[46]_fn().
775 * range is only used for new, uninitialized NAT state.
776 * Returns either NF_ACCEPT or NF_DROP.
777 */
778static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
779 enum ip_conntrack_info ctinfo,
fdec3c17 780 const struct nf_nat_range2 *range,
f8f97cdc
JR
781 enum nf_nat_manip_type maniptype)
782{
783 int hooknum, nh_off, err = NF_ACCEPT;
784
785 nh_off = skb_network_offset(skb);
073c7b86 786 skb_pull_rcsum(skb, nh_off);
f8f97cdc
JR
787
788 /* See HOOK2MANIP(). */
789 if (maniptype == NF_NAT_MANIP_SRC)
790 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
791 else
792 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
793
794 switch (ctinfo) {
795 case IP_CT_RELATED:
796 case IP_CT_RELATED_REPLY:
90b01477
AB
797 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
798 skb->protocol == htons(ETH_P_IP) &&
f8f97cdc
JR
799 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
800 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
801 hooknum))
802 err = NF_DROP;
803 goto push;
90b01477
AB
804 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
805 skb->protocol == htons(ETH_P_IPV6)) {
f8f97cdc
JR
806 __be16 frag_off;
807 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
808 int hdrlen = ipv6_skip_exthdr(skb,
809 sizeof(struct ipv6hdr),
810 &nexthdr, &frag_off);
811
812 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
813 if (!nf_nat_icmpv6_reply_translation(skb, ct,
814 ctinfo,
815 hooknum,
816 hdrlen))
817 err = NF_DROP;
818 goto push;
819 }
f8f97cdc
JR
820 }
821 /* Non-ICMP, fall thru to initialize if needed. */
822 case IP_CT_NEW:
823 /* Seen it before? This can happen for loopback, retrans,
824 * or local packets.
825 */
826 if (!nf_nat_initialized(ct, maniptype)) {
827 /* Initialize according to the NAT action. */
828 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
829 /* Action is set up to establish a new
830 * mapping.
831 */
832 ? nf_nat_setup_info(ct, range, maniptype)
833 : nf_nat_alloc_null_binding(ct, hooknum);
834 if (err != NF_ACCEPT)
835 goto push;
836 }
837 break;
838
839 case IP_CT_ESTABLISHED:
840 case IP_CT_ESTABLISHED_REPLY:
841 break;
842
843 default:
844 err = NF_DROP;
845 goto push;
846 }
847
848 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
849push:
850 skb_push(skb, nh_off);
073c7b86 851 skb_postpush_rcsum(skb, skb->data, nh_off);
f8f97cdc
JR
852
853 return err;
854}
855
856static void ovs_nat_update_key(struct sw_flow_key *key,
857 const struct sk_buff *skb,
858 enum nf_nat_manip_type maniptype)
859{
860 if (maniptype == NF_NAT_MANIP_SRC) {
861 __be16 src;
862
1963a885 863 key->ct_state |= OVS_CS_F_SRC_NAT;
f8f97cdc
JR
864 if (key->eth.type == htons(ETH_P_IP))
865 key->ipv4.addr.src = ip_hdr(skb)->saddr;
866 else if (key->eth.type == htons(ETH_P_IPV6))
867 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
868 sizeof(key->ipv6.addr.src));
869 else
870 return;
871
872 if (key->ip.proto == IPPROTO_UDP)
873 src = udp_hdr(skb)->source;
874 else if (key->ip.proto == IPPROTO_TCP)
875 src = tcp_hdr(skb)->source;
876 else if (key->ip.proto == IPPROTO_SCTP)
877 src = sctp_hdr(skb)->source;
878 else
879 return;
880
881 key->tp.src = src;
882 } else {
883 __be16 dst;
884
1963a885 885 key->ct_state |= OVS_CS_F_DST_NAT;
f8f97cdc
JR
886 if (key->eth.type == htons(ETH_P_IP))
887 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
888 else if (key->eth.type == htons(ETH_P_IPV6))
889 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
890 sizeof(key->ipv6.addr.dst));
891 else
892 return;
893
894 if (key->ip.proto == IPPROTO_UDP)
895 dst = udp_hdr(skb)->dest;
896 else if (key->ip.proto == IPPROTO_TCP)
897 dst = tcp_hdr(skb)->dest;
898 else if (key->ip.proto == IPPROTO_SCTP)
899 dst = sctp_hdr(skb)->dest;
900 else
901 return;
902
903 key->tp.dst = dst;
904 }
905}
906
907/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
908static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
909 const struct ovs_conntrack_info *info,
910 struct sk_buff *skb, struct nf_conn *ct,
911 enum ip_conntrack_info ctinfo)
912{
913 enum nf_nat_manip_type maniptype;
914 int err;
915
56ec5593 916#ifdef HAVE_NF_CT_IS_UNTRACKED
f8f97cdc
JR
917 if (nf_ct_is_untracked(ct)) {
918 /* A NAT action may only be performed on tracked packets. */
919 return NF_ACCEPT;
920 }
56ec5593 921#endif /* HAVE_NF_CT_IS_UNTRACKED */
f8f97cdc
JR
922
923 /* Add NAT extension if not confirmed yet. */
924 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
925 return NF_ACCEPT; /* Can't NAT. */
926
927 /* Determine NAT type.
928 * Check if the NAT type can be deduced from the tracked connection.
d2e8b514
JR
929 * Make sure new expected connections (IP_CT_RELATED) are NATted only
930 * when committing.
f8f97cdc
JR
931 */
932 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
933 ct->status & IPS_NAT_MASK &&
d2e8b514 934 (ctinfo != IP_CT_RELATED || info->commit)) {
f8f97cdc
JR
935 /* NAT an established or related connection like before. */
936 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
937 /* This is the REPLY direction for a connection
938 * for which NAT was applied in the forward
939 * direction. Do the reverse NAT.
940 */
941 maniptype = ct->status & IPS_SRC_NAT
942 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
943 else
944 maniptype = ct->status & IPS_SRC_NAT
945 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
946 } else if (info->nat & OVS_CT_SRC_NAT) {
947 maniptype = NF_NAT_MANIP_SRC;
948 } else if (info->nat & OVS_CT_DST_NAT) {
949 maniptype = NF_NAT_MANIP_DST;
950 } else {
951 return NF_ACCEPT; /* Connection is not NATed. */
952 }
953 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
954
955 /* Mark NAT done if successful and update the flow key. */
956 if (err == NF_ACCEPT)
957 ovs_nat_update_key(key, skb, maniptype);
958
959 return err;
960}
961#else /* !CONFIG_NF_NAT_NEEDED */
962static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
963 const struct ovs_conntrack_info *info,
964 struct sk_buff *skb, struct nf_conn *ct,
965 enum ip_conntrack_info ctinfo)
966{
967 return NF_ACCEPT;
968}
969#endif
970
b0f251cd 971/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
a04a5794
JR
972 * not done already. Update key with new CT state after passing the packet
973 * through conntrack.
e3c42eb8 974 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
b0f251cd
JR
975 * set to NULL and 0 will be returned.
976 */
c05e2094 977static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
a94ebc39
JS
978 const struct ovs_conntrack_info *info,
979 struct sk_buff *skb)
980{
981 /* If we are recirculating packets to match on conntrack fields and
982 * committing with a separate conntrack action, then we don't need to
983 * actually run the packet through conntrack twice unless it's for a
984 * different zone.
985 */
b21d237e
JR
986 bool cached = skb_nfct_cached(net, key, info, skb);
987 enum ip_conntrack_info ctinfo;
988 struct nf_conn *ct;
989
990 if (!cached) {
4fdec898
FW
991 struct nf_hook_state state = {
992 .hook = NF_INET_PRE_ROUTING,
993 .pf = info->family,
994 .net = net,
995 };
a94ebc39 996 struct nf_conn *tmpl = info->ct;
9bf67b92 997 int err;
a94ebc39
JS
998
999 /* Associate skb with specified zone. */
1000 if (tmpl) {
d3c313c1
FW
1001 if (skb_nfct(skb))
1002 nf_conntrack_put(skb_nfct(skb));
a94ebc39 1003 nf_conntrack_get(&tmpl->ct_general);
dfa791b2 1004 nf_ct_set(skb, tmpl, IP_CT_NEW);
a94ebc39
JS
1005 }
1006
4fdec898 1007 err = nf_conntrack_in(skb, &state);
9bf67b92 1008 if (err != NF_ACCEPT)
a94ebc39 1009 return -ENOENT;
11251c17 1010
f8f97cdc
JR
1011 /* Clear CT state NAT flags to mark that we have not yet done
1012 * NAT after the nf_conntrack_in() call. We can actually clear
1013 * the whole state, as it will be re-initialized below.
1014 */
1963a885 1015 key->ct_state = 0;
f8f97cdc
JR
1016
1017 /* Update the key, but keep the NAT flags. */
1018 ovs_ct_update_key(skb, info, key, true, true);
b21d237e 1019 }
a04a5794 1020
b21d237e 1021 ct = nf_ct_get(skb, &ctinfo);
f8f97cdc
JR
1022 if (ct) {
1023 /* Packets starting a new connection must be NATted before the
1024 * helper, so that the helper knows about the NAT. We enforce
1025 * this by delaying both NAT and helper calls for unconfirmed
1026 * connections until the committing CT action. For later
1027 * packets NAT and Helper may be called in either order.
1028 *
1029 * NAT will be done only if the CT action has NAT, and only
1030 * once per packet (per zone), as guarded by the NAT bits in
1963a885 1031 * the key->ct_state.
f8f97cdc 1032 */
1963a885 1033 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
f8f97cdc
JR
1034 (nf_ct_is_confirmed(ct) || info->commit) &&
1035 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
1036 return -EINVAL;
1037 }
1038
b87a5aac
JS
1039 /* Userspace may decide to perform a ct lookup without a helper
1040 * specified followed by a (recirculate and) commit with one.
1041 * Therefore, for unconfirmed connections which we will commit,
1042 * we need to attach the helper here.
1043 */
1044 if (!nf_ct_is_confirmed(ct) && info->commit &&
1045 info->helper && !nfct_help(ct)) {
1046 int err = __nf_ct_try_assign_helper(ct, info->ct,
1047 GFP_ATOMIC);
1048 if (err)
1049 return err;
1050 }
1051
f8f97cdc
JR
1052 /* Call the helper only if:
1053 * - nf_conntrack_in() was executed above ("!cached") for a
1054 * confirmed connection, or
1055 * - When committing an unconfirmed connection.
1056 */
1057 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
1058 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
1059 return -EINVAL;
1060 }
a94ebc39
JS
1061 }
1062
1063 return 0;
1064}
1065
1066/* Lookup connection and read fields into key. */
1067static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
1068 const struct ovs_conntrack_info *info,
1069 struct sk_buff *skb)
1070{
1071 struct nf_conntrack_expect *exp;
1072
b0f251cd
JR
1073 /* If we pass an expected packet through nf_conntrack_in() the
1074 * expectation is typically removed, but the packet could still be
1075 * lost in upcall processing. To prevent this from happening we
1076 * perform an explicit expectation lookup. Expected connections are
1077 * always new, and will be passed through conntrack only when they are
1078 * committed, as it is OK to remove the expectation at that time.
1079 */
a94ebc39
JS
1080 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
1081 if (exp) {
1082 u8 state;
1083
f8f97cdc
JR
1084 /* NOTE: New connections are NATted and Helped only when
1085 * committed, so we are not calling into NAT here.
1086 */
a94ebc39 1087 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
372ce973 1088 __ovs_ct_update_key(key, state, &info->zone, exp->master);
f325530e
PS
1089 } else {
1090 struct nf_conn *ct;
1091 int err;
1092
1093 err = __ovs_ct_lookup(net, key, info, skb);
1094 if (err)
1095 return err;
1096
d3c313c1 1097 ct = (struct nf_conn *)skb_nfct(skb);
f325530e
PS
1098 if (ct)
1099 nf_ct_deliver_cached_events(ct);
1100 }
a94ebc39
JS
1101
1102 return 0;
1103}
1104
c05e2094 1105static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
038e34ab
JS
1106{
1107 size_t i;
1108
83495bd9
JR
1109 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
1110 if (labels->ct_labels_32[i])
038e34ab
JS
1111 return true;
1112
1113 return false;
1114}
1115
cb2a5486
YHW
1116#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1117static struct hlist_head *ct_limit_hash_bucket(
1118 const struct ovs_ct_limit_info *info, u16 zone)
1119{
1120 return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
1121}
1122
1123/* Call with ovs_mutex */
1124static void ct_limit_set(const struct ovs_ct_limit_info *info,
1125 struct ovs_ct_limit *new_ct_limit)
1126{
1127 struct ovs_ct_limit *ct_limit;
1128 struct hlist_head *head;
1129
1130 head = ct_limit_hash_bucket(info, new_ct_limit->zone);
1131 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1132 if (ct_limit->zone == new_ct_limit->zone) {
1133 hlist_replace_rcu(&ct_limit->hlist_node,
1134 &new_ct_limit->hlist_node);
1135 kfree_rcu(ct_limit, rcu);
1136 return;
1137 }
1138 }
1139
1140 hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
1141}
1142
1143/* Call with ovs_mutex */
1144static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
1145{
1146 struct ovs_ct_limit *ct_limit;
1147 struct hlist_head *head;
1148 struct hlist_node *n;
1149
1150 head = ct_limit_hash_bucket(info, zone);
1151 hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
1152 if (ct_limit->zone == zone) {
1153 hlist_del_rcu(&ct_limit->hlist_node);
1154 kfree_rcu(ct_limit, rcu);
1155 return;
1156 }
1157 }
1158}
1159
1160/* Call with RCU read lock */
1161static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
1162{
1163 struct ovs_ct_limit *ct_limit;
1164 struct hlist_head *head;
1165
1166 head = ct_limit_hash_bucket(info, zone);
1167 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1168 if (ct_limit->zone == zone)
1169 return ct_limit->limit;
1170 }
1171
1172 return info->default_limit;
1173}
1174
1175static int ovs_ct_check_limit(struct net *net,
1176 const struct ovs_conntrack_info *info,
1177 const struct nf_conntrack_tuple *tuple)
1178{
1179 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1180 const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
1181 u32 per_zone_limit, connections;
1182 u32 conncount_key;
1183
1184 conncount_key = info->zone.id;
1185
1186 per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
1187 if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
1188 return 0;
1189
1190 connections = nf_conncount_count(net, ct_limit_info->data,
1191 &conncount_key, tuple, &info->zone);
1192 if (connections > per_zone_limit)
1193 return -ENOMEM;
1194
1195 return 0;
1196}
1197#endif
1198
39a6542b
JR
1199/* Lookup connection and confirm if unconfirmed. */
1200static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
1201 const struct ovs_conntrack_info *info,
1202 struct sk_buff *skb)
1203{
efd40994
JR
1204 enum ip_conntrack_info ctinfo;
1205 struct nf_conn *ct;
39a6542b
JR
1206 int err;
1207
1208 err = __ovs_ct_lookup(net, key, info, skb);
1209 if (err)
1210 return err;
1211
efd40994
JR
1212 /* The connection could be invalid, in which case this is a no-op.*/
1213 ct = nf_ct_get(skb, &ctinfo);
1214 if (!ct)
1215 return 0;
1216
cb2a5486
YHW
1217#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1218 if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
1219 if (!nf_ct_is_confirmed(ct)) {
1220 err = ovs_ct_check_limit(net, info,
1221 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1222 if (err) {
1223 net_warn_ratelimited("openvswitch: zone: %u "
56913238 1224 "exceeds conntrack limit\n",
cb2a5486
YHW
1225 info->zone.id);
1226 return err;
1227 }
1228 }
1229 }
1230#endif
1231
8e83854c
JR
1232 /* Set the conntrack event mask if given. NEW and DELETE events have
1233 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1234 * typically would receive many kinds of updates. Setting the event
1235 * mask allows those events to be filtered. The set event mask will
1236 * remain in effect for the lifetime of the connection unless changed
1237 * by a further CT action with both the commit flag and the eventmask
1238 * option. */
1239 if (info->have_eventmask) {
1240 struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
1241
1242 if (cache)
1243 cache->ctmask = info->eventmask;
1244 }
1245
39a6542b
JR
1246 /* Apply changes before confirming the connection so that the initial
1247 * conntrack NEW netlink event carries the values given in the CT
1248 * action.
1249 */
1250 if (info->mark.mask) {
efd40994 1251 err = ovs_ct_set_mark(ct, key, info->mark.value,
39a6542b
JR
1252 info->mark.mask);
1253 if (err)
1254 return err;
1255 }
7162cae4
JR
1256 if (!nf_ct_is_confirmed(ct)) {
1257 err = ovs_ct_init_labels(ct, key, &info->labels.value,
1258 &info->labels.mask);
1259 if (err)
1260 return err;
1261 } else if (labels_nonzero(&info->labels.mask)) {
1262 err = ovs_ct_set_labels(ct, key, &info->labels.value,
1263 &info->labels.mask);
39a6542b
JR
1264 if (err)
1265 return err;
1266 }
1267 /* This will take care of sending queued events even if the connection
1268 * is already confirmed.
1269 */
1270 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1271 return -EINVAL;
1272
1273 return 0;
1274}
1275
c21adacd
ES
1276/* Trim the skb to the length specified by the IP/IPv6 header,
1277 * removing any trailing lower-layer padding. This prepares the skb
1278 * for higher-layer processing that assumes skb->len excludes padding
1279 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1280 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1281 */
1282static int ovs_skb_network_trim(struct sk_buff *skb)
1283{
1284 unsigned int len;
1285 int err;
1286
1287 switch (skb->protocol) {
1288 case htons(ETH_P_IP):
1289 len = ntohs(ip_hdr(skb)->tot_len);
1290 break;
1291 case htons(ETH_P_IPV6):
1292 len = sizeof(struct ipv6hdr)
1293 + ntohs(ipv6_hdr(skb)->payload_len);
1294 break;
1295 default:
1296 len = skb->len;
1297 }
1298
1299 err = pskb_trim_rcsum(skb, len);
1300 if (err)
1301 kfree_skb(skb);
1302
1303 return err;
1304}
1305
c05e2094
JS
1306/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1307 * value if 'skb' is freed.
1308 */
a94ebc39
JS
1309int ovs_ct_execute(struct net *net, struct sk_buff *skb,
1310 struct sw_flow_key *key,
1311 const struct ovs_conntrack_info *info)
1312{
1313 int nh_ofs;
1314 int err;
1315
1316 /* The conntrack module expects to be working at L3. */
1317 nh_ofs = skb_network_offset(skb);
073c7b86 1318 skb_pull_rcsum(skb, nh_ofs);
a94ebc39 1319
c21adacd
ES
1320 err = ovs_skb_network_trim(skb);
1321 if (err)
1322 return err;
1323
a94ebc39
JS
1324 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
1325 err = handle_fragments(net, key, info->zone.id, skb);
1326 if (err)
1327 return err;
1328 }
1329
c05e2094 1330 if (info->commit)
39a6542b 1331 err = ovs_ct_commit(net, key, info, skb);
a94ebc39
JS
1332 else
1333 err = ovs_ct_lookup(net, key, info, skb);
1334
1335 skb_push(skb, nh_ofs);
073c7b86 1336 skb_postpush_rcsum(skb, skb->data, nh_ofs);
c05e2094
JS
1337 if (err)
1338 kfree_skb(skb);
a94ebc39
JS
1339 return err;
1340}
1341
0cdfdddd
EG
1342int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
1343{
1344 if (skb_nfct(skb)) {
1345 nf_conntrack_put(skb_nfct(skb));
1346#ifdef HAVE_IP_CT_UNTRACKED
1347 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1348#else
1349 nf_ct_set(skb, NULL, 0);
1350#endif
1351 ovs_ct_fill_key(skb, key);
1352 }
1353
1354 return 0;
1355}
1356
11251c17
JS
1357static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
1358 const struct sw_flow_key *key, bool log)
1359{
1360 struct nf_conntrack_helper *helper;
1361 struct nf_conn_help *help;
1362
1363 helper = nf_conntrack_helper_try_module_get(name, info->family,
1364 key->ip.proto);
1365 if (!helper) {
1366 OVS_NLERR(log, "Unknown helper \"%s\"", name);
1367 return -EINVAL;
1368 }
1369
1370 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
1371 if (!help) {
ac8e3c6d 1372 nf_conntrack_helper_put(helper);
11251c17
JS
1373 return -ENOMEM;
1374 }
1375
1376 rcu_assign_pointer(help->helper, helper);
1377 info->helper = helper;
15d536e7
FL
1378
1379 if (info->nat)
1380 request_module("ip_nat_%s", name);
1381
11251c17
JS
1382 return 0;
1383}
1384
f8f97cdc
JR
1385#ifdef CONFIG_NF_NAT_NEEDED
1386static int parse_nat(const struct nlattr *attr,
1387 struct ovs_conntrack_info *info, bool log)
1388{
1389 struct nlattr *a;
1390 int rem;
1391 bool have_ip_max = false;
1392 bool have_proto_max = false;
1393 bool ip_vers = (info->family == NFPROTO_IPV6);
1394
1395 nla_for_each_nested(a, attr, rem) {
1396 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1397 [OVS_NAT_ATTR_SRC] = {0, 0},
1398 [OVS_NAT_ATTR_DST] = {0, 0},
1399 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1400 sizeof(struct in6_addr)},
1401 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1402 sizeof(struct in6_addr)},
1403 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1404 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1405 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1406 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1407 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1408 };
1409 int type = nla_type(a);
1410
1411 if (type > OVS_NAT_ATTR_MAX) {
72ef85ed 1412 OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
f8f97cdc
JR
1413 type, OVS_NAT_ATTR_MAX);
1414 return -EINVAL;
1415 }
1416
1417 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
72ef85ed 1418 OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
f8f97cdc
JR
1419 type, nla_len(a),
1420 ovs_nat_attr_lens[type][ip_vers]);
1421 return -EINVAL;
1422 }
1423
1424 switch (type) {
1425 case OVS_NAT_ATTR_SRC:
1426 case OVS_NAT_ATTR_DST:
1427 if (info->nat) {
72ef85ed 1428 OVS_NLERR(log, "Only one type of NAT may be specified");
f8f97cdc
JR
1429 return -ERANGE;
1430 }
1431 info->nat |= OVS_CT_NAT;
1432 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1433 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1434 break;
1435
1436 case OVS_NAT_ATTR_IP_MIN:
70e71d27
HY
1437 nla_memcpy(&info->range.min_addr, a,
1438 sizeof(info->range.min_addr));
f8f97cdc
JR
1439 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1440 break;
1441
1442 case OVS_NAT_ATTR_IP_MAX:
1443 have_ip_max = true;
1444 nla_memcpy(&info->range.max_addr, a,
1445 sizeof(info->range.max_addr));
1446 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1447 break;
1448
1449 case OVS_NAT_ATTR_PROTO_MIN:
1450 info->range.min_proto.all = htons(nla_get_u16(a));
1451 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1452 break;
1453
1454 case OVS_NAT_ATTR_PROTO_MAX:
1455 have_proto_max = true;
1456 info->range.max_proto.all = htons(nla_get_u16(a));
1457 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1458 break;
1459
1460 case OVS_NAT_ATTR_PERSISTENT:
1461 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1462 break;
1463
1464 case OVS_NAT_ATTR_PROTO_HASH:
1465 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1466 break;
1467
1468 case OVS_NAT_ATTR_PROTO_RANDOM:
9f1de150 1469#ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
f8f97cdc 1470 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
9f1de150
JR
1471#else
1472 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1473 info->random_fully_compat = true;
1474#endif
f8f97cdc
JR
1475 break;
1476
1477 default:
72ef85ed 1478 OVS_NLERR(log, "Unknown nat attribute (%d)", type);
f8f97cdc
JR
1479 return -EINVAL;
1480 }
1481 }
1482
1483 if (rem > 0) {
72ef85ed 1484 OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
f8f97cdc
JR
1485 return -EINVAL;
1486 }
1487 if (!info->nat) {
1488 /* Do not allow flags if no type is given. */
1489 if (info->range.flags) {
1490 OVS_NLERR(log,
e7c57cc3 1491 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
f8f97cdc
JR
1492 );
1493 return -EINVAL;
1494 }
1495 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1496 } else if (!info->commit) {
1497 OVS_NLERR(log,
e7c57cc3 1498 "NAT attributes may be specified only when CT COMMIT flag is also specified."
f8f97cdc
JR
1499 );
1500 return -EINVAL;
1501 }
1502 /* Allow missing IP_MAX. */
1503 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1504 memcpy(&info->range.max_addr, &info->range.min_addr,
1505 sizeof(info->range.max_addr));
1506 }
1507 /* Allow missing PROTO_MAX. */
1508 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1509 !have_proto_max) {
1510 info->range.max_proto.all = info->range.min_proto.all;
1511 }
1512 return 0;
1513}
1514#endif
1515
a94ebc39 1516static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
c05e2094 1517 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
b80e259f 1518 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
a94ebc39
JS
1519 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1520 .maxlen = sizeof(u16) },
372ce973
JS
1521 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1522 .maxlen = sizeof(struct md_mark) },
c05e2094
JS
1523 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1524 .maxlen = sizeof(struct md_labels) },
11251c17 1525 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
f8f97cdc
JR
1526 .maxlen = NF_CT_HELPER_NAME_LEN },
1527#ifdef CONFIG_NF_NAT_NEEDED
1528 /* NAT length is checked when parsing the nested attributes. */
1529 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1530#endif
8e83854c
JR
1531 [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
1532 .maxlen = sizeof(u32) },
a94ebc39
JS
1533};
1534
1535static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
11251c17 1536 const char **helper, bool log)
a94ebc39
JS
1537{
1538 struct nlattr *a;
1539 int rem;
1540
1541 nla_for_each_nested(a, attr, rem) {
1542 int type = nla_type(a);
fb296f42
GR
1543 int maxlen;
1544 int minlen;
a94ebc39
JS
1545
1546 if (type > OVS_CT_ATTR_MAX) {
1547 OVS_NLERR(log,
1548 "Unknown conntrack attr (type=%d, max=%d)",
1549 type, OVS_CT_ATTR_MAX);
1550 return -EINVAL;
1551 }
fb296f42
GR
1552
1553 maxlen = ovs_ct_attr_lens[type].maxlen;
1554 minlen = ovs_ct_attr_lens[type].minlen;
a94ebc39
JS
1555 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1556 OVS_NLERR(log,
1557 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1558 type, nla_len(a), maxlen);
1559 return -EINVAL;
1560 }
1561
1562 switch (type) {
b80e259f
JR
1563 case OVS_CT_ATTR_FORCE_COMMIT:
1564 info->force = true;
1565 /* fall through. */
c05e2094
JS
1566 case OVS_CT_ATTR_COMMIT:
1567 info->commit = true;
a94ebc39
JS
1568 break;
1569#ifdef CONFIG_NF_CONNTRACK_ZONES
1570 case OVS_CT_ATTR_ZONE:
1571 info->zone.id = nla_get_u16(a);
1572 break;
372ce973
JS
1573#endif
1574#ifdef CONFIG_NF_CONNTRACK_MARK
1575 case OVS_CT_ATTR_MARK: {
1576 struct md_mark *mark = nla_data(a);
1577
c05e2094
JS
1578 if (!mark->mask) {
1579 OVS_NLERR(log, "ct_mark mask cannot be 0");
1580 return -EINVAL;
1581 }
372ce973
JS
1582 info->mark = *mark;
1583 break;
1584 }
038e34ab
JS
1585#endif
1586#ifdef CONFIG_NF_CONNTRACK_LABELS
c05e2094
JS
1587 case OVS_CT_ATTR_LABELS: {
1588 struct md_labels *labels = nla_data(a);
038e34ab 1589
c05e2094
JS
1590 if (!labels_nonzero(&labels->mask)) {
1591 OVS_NLERR(log, "ct_labels mask cannot be 0");
1592 return -EINVAL;
1593 }
1594 info->labels = *labels;
038e34ab
JS
1595 break;
1596 }
a94ebc39 1597#endif
11251c17
JS
1598 case OVS_CT_ATTR_HELPER:
1599 *helper = nla_data(a);
1600 if (!memchr(*helper, '\0', nla_len(a))) {
1601 OVS_NLERR(log, "Invalid conntrack helper");
1602 return -EINVAL;
1603 }
1604 break;
f8f97cdc
JR
1605#ifdef CONFIG_NF_NAT_NEEDED
1606 case OVS_CT_ATTR_NAT: {
1607 int err = parse_nat(a, info, log);
1608
1609 if (err)
1610 return err;
1611 break;
1612 }
1613#endif
8e83854c
JR
1614 case OVS_CT_ATTR_EVENTMASK:
1615 info->have_eventmask = true;
1616 info->eventmask = nla_get_u32(a);
1617 break;
1618
a94ebc39
JS
1619 default:
1620 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1621 type);
1622 return -EINVAL;
1623 }
1624 }
1625
39a6542b
JR
1626#ifdef CONFIG_NF_CONNTRACK_MARK
1627 if (!info->commit && info->mark.mask) {
1628 OVS_NLERR(log,
1629 "Setting conntrack mark requires 'commit' flag.");
1630 return -EINVAL;
1631 }
1632#endif
1633#ifdef CONFIG_NF_CONNTRACK_LABELS
1634 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1635 OVS_NLERR(log,
1636 "Setting conntrack labels requires 'commit' flag.");
1637 return -EINVAL;
1638 }
1639#endif
a94ebc39
JS
1640 if (rem > 0) {
1641 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1642 return -EINVAL;
1643 }
1644
1645 return 0;
1646}
1647
038e34ab 1648bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
a94ebc39
JS
1649{
1650 if (attr == OVS_KEY_ATTR_CT_STATE)
1651 return true;
1652 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1653 attr == OVS_KEY_ATTR_CT_ZONE)
1654 return true;
372ce973
JS
1655 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1656 attr == OVS_KEY_ATTR_CT_MARK)
1657 return true;
038e34ab 1658 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
c05e2094 1659 attr == OVS_KEY_ATTR_CT_LABELS) {
038e34ab
JS
1660 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1661
1662 return ovs_net->xt_label;
1663 }
a94ebc39
JS
1664
1665 return false;
1666}
1667
1668int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1669 const struct sw_flow_key *key,
1670 struct sw_flow_actions **sfa, bool log)
1671{
1672 struct ovs_conntrack_info ct_info;
11251c17 1673 const char *helper = NULL;
a94ebc39
JS
1674 u16 family;
1675 int err;
1676
1677 family = key_to_nfproto(key);
1678 if (family == NFPROTO_UNSPEC) {
1679 OVS_NLERR(log, "ct family unspecified");
1680 return -EINVAL;
1681 }
1682
1683 memset(&ct_info, 0, sizeof(ct_info));
1684 ct_info.family = family;
1685
1686 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1687 NF_CT_DEFAULT_ZONE_DIR, 0);
1688
11251c17 1689 err = parse_ct(attr, &ct_info, &helper, log);
a94ebc39
JS
1690 if (err)
1691 return err;
1692
1693 /* Set up template for tracking connections in specific zones. */
1694 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1695 if (!ct_info.ct) {
1696 OVS_NLERR(log, "Failed to allocate conntrack template");
1697 return -ENOMEM;
1698 }
11251c17
JS
1699 if (helper) {
1700 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1701 if (err)
1702 goto err_free_ct;
1703 }
a94ebc39
JS
1704
1705 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1706 sizeof(ct_info), log);
1707 if (err)
1708 goto err_free_ct;
1709
50aa6e68
FL
1710 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1711 nf_conntrack_get(&ct_info.ct->ct_general);
a94ebc39
JS
1712 return 0;
1713err_free_ct:
11251c17 1714 __ovs_ct_free_action(&ct_info);
a94ebc39
JS
1715 return err;
1716}
1717
f8f97cdc
JR
1718#ifdef CONFIG_NF_NAT_NEEDED
1719static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1720 struct sk_buff *skb)
1721{
1722 struct nlattr *start;
1723
1724 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1725 if (!start)
1726 return false;
1727
1728 if (info->nat & OVS_CT_SRC_NAT) {
1729 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1730 return false;
1731 } else if (info->nat & OVS_CT_DST_NAT) {
1732 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1733 return false;
1734 } else {
1735 goto out;
1736 }
1737
1738 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
90b01477
AB
1739 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1740 info->family == NFPROTO_IPV4) {
f8f97cdc
JR
1741 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1742 info->range.min_addr.ip) ||
1743 (info->range.max_addr.ip
1744 != info->range.min_addr.ip &&
1745 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1746 info->range.max_addr.ip))))
1747 return false;
90b01477
AB
1748 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1749 info->family == NFPROTO_IPV6) {
f8f97cdc
JR
1750 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1751 &info->range.min_addr.in6) ||
1752 (memcmp(&info->range.max_addr.in6,
1753 &info->range.min_addr.in6,
1754 sizeof(info->range.max_addr.in6)) &&
1755 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1756 &info->range.max_addr.in6))))
1757 return false;
f8f97cdc
JR
1758 } else {
1759 return false;
1760 }
1761 }
1762 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1763 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1764 ntohs(info->range.min_proto.all)) ||
1765 (info->range.max_proto.all != info->range.min_proto.all &&
1766 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1767 ntohs(info->range.max_proto.all)))))
1768 return false;
1769
1770 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1771 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1772 return false;
1773 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
9f1de150
JR
1774 nla_put_flag(skb, info->random_fully_compat
1775 ? OVS_NAT_ATTR_PROTO_RANDOM
1776 : OVS_NAT_ATTR_PROTO_HASH))
f8f97cdc 1777 return false;
9f1de150 1778#ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
f8f97cdc
JR
1779 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1780 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1781 return false;
9f1de150 1782#endif
f8f97cdc
JR
1783out:
1784 nla_nest_end(skb, start);
1785
1786 return true;
1787}
1788#endif
1789
a94ebc39
JS
1790int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1791 struct sk_buff *skb)
1792{
1793 struct nlattr *start;
1794
1795 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1796 if (!start)
1797 return -EMSGSIZE;
1798
b80e259f
JR
1799 if (ct_info->commit && nla_put_flag(skb, ct_info->force
1800 ? OVS_CT_ATTR_FORCE_COMMIT
1801 : OVS_CT_ATTR_COMMIT))
a94ebc39
JS
1802 return -EMSGSIZE;
1803 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1804 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1805 return -EMSGSIZE;
c05e2094 1806 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
372ce973
JS
1807 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1808 &ct_info->mark))
1809 return -EMSGSIZE;
038e34ab 1810 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
c05e2094
JS
1811 labels_nonzero(&ct_info->labels.mask) &&
1812 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1813 &ct_info->labels))
038e34ab 1814 return -EMSGSIZE;
11251c17
JS
1815 if (ct_info->helper) {
1816 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1817 ct_info->helper->name))
1818 return -EMSGSIZE;
1819 }
8e83854c
JR
1820 if (ct_info->have_eventmask &&
1821 nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
1822 return -EMSGSIZE;
1823
f8f97cdc
JR
1824#ifdef CONFIG_NF_NAT_NEEDED
1825 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1826 return -EMSGSIZE;
1827#endif
a94ebc39
JS
1828 nla_nest_end(skb, start);
1829
1830 return 0;
1831}
1832
1833void ovs_ct_free_action(const struct nlattr *a)
1834{
1835 struct ovs_conntrack_info *ct_info = nla_data(a);
1836
11251c17
JS
1837 __ovs_ct_free_action(ct_info);
1838}
1839
1840static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1841{
1842 if (ct_info->helper)
ac8e3c6d 1843 nf_conntrack_helper_put(ct_info->helper);
a94ebc39
JS
1844 if (ct_info->ct)
1845 nf_ct_tmpl_free(ct_info->ct);
1846}
1847
cb2a5486
YHW
1848#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1849static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
1850{
1851 int i, err;
1852
1853 ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
1854 GFP_KERNEL);
1855 if (!ovs_net->ct_limit_info)
1856 return -ENOMEM;
1857
1858 ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
1859 ovs_net->ct_limit_info->limits =
1860 kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
1861 GFP_KERNEL);
1862 if (!ovs_net->ct_limit_info->limits) {
1863 kfree(ovs_net->ct_limit_info);
1864 return -ENOMEM;
1865 }
1866
1867 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
1868 INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
1869
1870 ovs_net->ct_limit_info->data =
1871 nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
1872
1873 if (IS_ERR(ovs_net->ct_limit_info->data)) {
1874 err = PTR_ERR(ovs_net->ct_limit_info->data);
1875 kfree(ovs_net->ct_limit_info->limits);
1876 kfree(ovs_net->ct_limit_info);
1877 pr_err("openvswitch: failed to init nf_conncount %d\n", err);
1878 return err;
1879 }
1880 return 0;
1881}
1882
1883static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
1884{
1885 const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
1886 int i;
1887
1888 nf_conncount_destroy(net, NFPROTO_INET, info->data);
1889 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
1890 struct hlist_head *head = &info->limits[i];
1891 struct ovs_ct_limit *ct_limit;
1892
1893 hlist_for_each_entry_rcu(ct_limit, head, hlist_node)
1894 kfree_rcu(ct_limit, rcu);
1895 }
1896 kfree(ovs_net->ct_limit_info->limits);
1897 kfree(ovs_net->ct_limit_info);
1898}
1899
1900static struct sk_buff *
1901ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
1902 struct ovs_header **ovs_reply_header)
1903{
1904 struct ovs_header *ovs_header = info->userhdr;
1905 struct sk_buff *skb;
1906
1907 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1908 if (!skb)
1909 return ERR_PTR(-ENOMEM);
1910
1911 *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
1912 info->snd_seq,
1913 &dp_ct_limit_genl_family, 0, cmd);
1914
1915 if (!*ovs_reply_header) {
1916 nlmsg_free(skb);
1917 return ERR_PTR(-EMSGSIZE);
1918 }
1919 (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
1920
1921 return skb;
1922}
1923
1924static bool check_zone_id(int zone_id, u16 *pzone)
1925{
1926 if (zone_id >= 0 && zone_id <= 65535) {
1927 *pzone = (u16)zone_id;
1928 return true;
1929 }
1930 return false;
1931}
1932
1933static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
1934 struct ovs_ct_limit_info *info)
1935{
1936 struct ovs_zone_limit *zone_limit;
1937 int rem;
1938 u16 zone;
1939
1940 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1941 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1942
1943 while (rem >= sizeof(*zone_limit)) {
1944 if (unlikely(zone_limit->zone_id ==
1945 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1946 ovs_lock();
1947 info->default_limit = zone_limit->limit;
1948 ovs_unlock();
1949 } else if (unlikely(!check_zone_id(
1950 zone_limit->zone_id, &zone))) {
1951 OVS_NLERR(true, "zone id is out of range");
1952 } else {
1953 struct ovs_ct_limit *ct_limit;
1954
1955 ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL);
1956 if (!ct_limit)
1957 return -ENOMEM;
1958
1959 ct_limit->zone = zone;
1960 ct_limit->limit = zone_limit->limit;
1961
1962 ovs_lock();
1963 ct_limit_set(info, ct_limit);
1964 ovs_unlock();
1965 }
1966 rem -= NLA_ALIGN(sizeof(*zone_limit));
1967 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
1968 NLA_ALIGN(sizeof(*zone_limit)));
1969 }
1970
1971 if (rem)
1972 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
1973
1974 return 0;
1975}
1976
1977static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
1978 struct ovs_ct_limit_info *info)
1979{
1980 struct ovs_zone_limit *zone_limit;
1981 int rem;
1982 u16 zone;
1983
1984 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1985 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1986
1987 while (rem >= sizeof(*zone_limit)) {
1988 if (unlikely(zone_limit->zone_id ==
1989 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1990 ovs_lock();
1991 info->default_limit = OVS_CT_LIMIT_DEFAULT;
1992 ovs_unlock();
1993 } else if (unlikely(!check_zone_id(
1994 zone_limit->zone_id, &zone))) {
1995 OVS_NLERR(true, "zone id is out of range");
1996 } else {
1997 ovs_lock();
1998 ct_limit_del(info, zone);
1999 ovs_unlock();
2000 }
2001 rem -= NLA_ALIGN(sizeof(*zone_limit));
2002 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2003 NLA_ALIGN(sizeof(*zone_limit)));
2004 }
2005
2006 if (rem)
2007 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
2008
2009 return 0;
2010}
2011
2012static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
2013 struct sk_buff *reply)
2014{
2015 struct ovs_zone_limit zone_limit;
2016 int err;
2017
2018 zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
2019 zone_limit.limit = info->default_limit;
2020 err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2021 if (err)
2022 return err;
2023
2024 return 0;
2025}
2026
2027static int __ovs_ct_limit_get_zone_limit(struct net *net,
2028 struct nf_conncount_data *data,
2029 u16 zone_id, u32 limit,
2030 struct sk_buff *reply)
2031{
2032 struct nf_conntrack_zone ct_zone;
2033 struct ovs_zone_limit zone_limit;
2034 u32 conncount_key = zone_id;
2035
2036 zone_limit.zone_id = zone_id;
2037 zone_limit.limit = limit;
2038 nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
2039
2040 zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
2041 &ct_zone);
2042 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2043}
2044
2045static int ovs_ct_limit_get_zone_limit(struct net *net,
2046 struct nlattr *nla_zone_limit,
2047 struct ovs_ct_limit_info *info,
2048 struct sk_buff *reply)
2049{
2050 struct ovs_zone_limit *zone_limit;
2051 int rem, err;
2052 u32 limit;
2053 u16 zone;
2054
2055 rem = NLA_ALIGN(nla_len(nla_zone_limit));
2056 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
2057
2058 while (rem >= sizeof(*zone_limit)) {
2059 if (unlikely(zone_limit->zone_id ==
2060 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
2061 err = ovs_ct_limit_get_default_limit(info, reply);
2062 if (err)
2063 return err;
2064 } else if (unlikely(!check_zone_id(zone_limit->zone_id,
2065 &zone))) {
2066 OVS_NLERR(true, "zone id is out of range");
2067 } else {
2068 rcu_read_lock();
2069 limit = ct_limit_get(info, zone);
2070 rcu_read_unlock();
2071
2072 err = __ovs_ct_limit_get_zone_limit(
2073 net, info->data, zone, limit, reply);
2074 if (err)
2075 return err;
2076 }
2077 rem -= NLA_ALIGN(sizeof(*zone_limit));
2078 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2079 NLA_ALIGN(sizeof(*zone_limit)));
2080 }
2081
2082 if (rem)
2083 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
2084
2085 return 0;
2086}
2087
2088static int ovs_ct_limit_get_all_zone_limit(struct net *net,
2089 struct ovs_ct_limit_info *info,
2090 struct sk_buff *reply)
2091{
2092 struct ovs_ct_limit *ct_limit;
2093 struct hlist_head *head;
2094 int i, err = 0;
2095
2096 err = ovs_ct_limit_get_default_limit(info, reply);
2097 if (err)
2098 return err;
2099
2100 rcu_read_lock();
2101 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
2102 head = &info->limits[i];
2103 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
2104 err = __ovs_ct_limit_get_zone_limit(net, info->data,
2105 ct_limit->zone, ct_limit->limit, reply);
2106 if (err)
2107 goto exit_err;
2108 }
2109 }
2110
2111exit_err:
2112 rcu_read_unlock();
2113 return err;
2114}
2115
2116static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
2117{
2118 struct nlattr **a = info->attrs;
2119 struct sk_buff *reply;
2120 struct ovs_header *ovs_reply_header;
2121 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2122 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2123 int err;
2124
2125 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
2126 &ovs_reply_header);
2127 if (IS_ERR(reply))
2128 return PTR_ERR(reply);
2129
2130 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2131 err = -EINVAL;
2132 goto exit_err;
2133 }
2134
2135 err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2136 ct_limit_info);
2137 if (err)
2138 goto exit_err;
2139
2140 static_branch_enable(&ovs_ct_limit_enabled);
2141
2142 genlmsg_end(reply, ovs_reply_header);
2143 return genlmsg_reply(reply, info);
2144
2145exit_err:
2146 nlmsg_free(reply);
2147 return err;
2148}
2149
2150static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
2151{
2152 struct nlattr **a = info->attrs;
2153 struct sk_buff *reply;
2154 struct ovs_header *ovs_reply_header;
2155 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2156 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2157 int err;
2158
2159 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
2160 &ovs_reply_header);
2161 if (IS_ERR(reply))
2162 return PTR_ERR(reply);
2163
2164 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2165 err = -EINVAL;
2166 goto exit_err;
2167 }
2168
2169 err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2170 ct_limit_info);
2171 if (err)
2172 goto exit_err;
2173
2174 genlmsg_end(reply, ovs_reply_header);
2175 return genlmsg_reply(reply, info);
2176
2177exit_err:
2178 nlmsg_free(reply);
2179 return err;
2180}
2181
2182static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
2183{
2184 struct nlattr **a = info->attrs;
2185 struct nlattr *nla_reply;
2186 struct sk_buff *reply;
2187 struct ovs_header *ovs_reply_header;
2188 struct net *net = sock_net(skb->sk);
2189 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2190 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2191 int err;
2192
2193 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
2194 &ovs_reply_header);
2195 if (IS_ERR(reply))
2196 return PTR_ERR(reply);
2197
2198 nla_reply = nla_nest_start(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
2199
2200 if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2201 err = ovs_ct_limit_get_zone_limit(
2202 net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
2203 reply);
2204 if (err)
2205 goto exit_err;
2206 } else {
2207 err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
2208 reply);
2209 if (err)
2210 goto exit_err;
2211 }
2212
2213 nla_nest_end(reply, nla_reply);
2214 genlmsg_end(reply, ovs_reply_header);
2215 return genlmsg_reply(reply, info);
2216
2217exit_err:
2218 nlmsg_free(reply);
2219 return err;
2220}
2221
2222static struct genl_ops ct_limit_genl_ops[] = {
2223 { .cmd = OVS_CT_LIMIT_CMD_SET,
2224 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
2225 * privilege. */
2226 .policy = ct_limit_policy,
2227 .doit = ovs_ct_limit_cmd_set,
2228 },
2229 { .cmd = OVS_CT_LIMIT_CMD_DEL,
2230 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
2231 * privilege. */
2232 .policy = ct_limit_policy,
2233 .doit = ovs_ct_limit_cmd_del,
2234 },
2235 { .cmd = OVS_CT_LIMIT_CMD_GET,
2236 .flags = 0, /* OK for unprivileged users. */
2237 .policy = ct_limit_policy,
2238 .doit = ovs_ct_limit_cmd_get,
2239 },
2240};
2241
2242static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
2243 .name = OVS_CT_LIMIT_MCGROUP,
2244};
2245
2246struct genl_family dp_ct_limit_genl_family __ro_after_init = {
2247 .hdrsize = sizeof(struct ovs_header),
2248 .name = OVS_CT_LIMIT_FAMILY,
2249 .version = OVS_CT_LIMIT_VERSION,
2250 .maxattr = OVS_CT_LIMIT_ATTR_MAX,
2251 .netnsok = true,
2252 .parallel_ops = true,
2253 .ops = ct_limit_genl_ops,
2254 .n_ops = ARRAY_SIZE(ct_limit_genl_ops),
2255 .mcgrps = &ovs_ct_limit_multicast_group,
2256 .n_mcgrps = 1,
2257 .module = THIS_MODULE,
2258};
2259#endif
2260
2261int ovs_ct_init(struct net *net)
038e34ab 2262{
c05e2094 2263 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
038e34ab
JS
2264 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2265
7f2ab8cd 2266 if (nf_connlabels_get(net, n_bits - 1)) {
038e34ab
JS
2267 ovs_net->xt_label = false;
2268 OVS_NLERR(true, "Failed to set connlabel length");
2269 } else {
2270 ovs_net->xt_label = true;
2271 }
cb2a5486
YHW
2272
2273#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2274 return ovs_ct_limit_init(net, ovs_net);
2275#else
2276 return 0;
2277#endif
038e34ab
JS
2278}
2279
2280void ovs_ct_exit(struct net *net)
2281{
2282 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2283
cb2a5486
YHW
2284#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2285 ovs_ct_limit_exit(net, ovs_net);
2286#endif
2287
038e34ab
JS
2288 if (ovs_net->xt_label)
2289 nf_connlabels_put(net);
2290}
2291
8063e095 2292#endif /* CONFIG_NF_CONNTRACK */