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