]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/conntrack.c
cirrus: Use FreeBSD 12.2.
[mirror_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 /* fall through */
848 case IP_CT_NEW:
849 /* Seen it before? This can happen for loopback, retrans,
850 * or local packets.
851 */
852 if (!nf_nat_initialized(ct, maniptype)) {
853 /* Initialize according to the NAT action. */
854 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
855 /* Action is set up to establish a new
856 * mapping.
857 */
858 ? nf_nat_setup_info(ct, range, maniptype)
859 : nf_nat_alloc_null_binding(ct, hooknum);
860 if (err != NF_ACCEPT)
861 goto push;
862 }
863 break;
864
865 case IP_CT_ESTABLISHED:
866 case IP_CT_ESTABLISHED_REPLY:
867 break;
868
869 default:
870 err = NF_DROP;
871 goto push;
872 }
873
874 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
875 push:
876 skb_push(skb, nh_off);
877 skb_postpush_rcsum(skb, skb->data, nh_off);
878
879 return err;
880 }
881
882 static void ovs_nat_update_key(struct sw_flow_key *key,
883 const struct sk_buff *skb,
884 enum nf_nat_manip_type maniptype)
885 {
886 if (maniptype == NF_NAT_MANIP_SRC) {
887 __be16 src;
888
889 key->ct_state |= OVS_CS_F_SRC_NAT;
890 if (key->eth.type == htons(ETH_P_IP))
891 key->ipv4.addr.src = ip_hdr(skb)->saddr;
892 else if (key->eth.type == htons(ETH_P_IPV6))
893 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
894 sizeof(key->ipv6.addr.src));
895 else
896 return;
897
898 if (key->ip.proto == IPPROTO_UDP)
899 src = udp_hdr(skb)->source;
900 else if (key->ip.proto == IPPROTO_TCP)
901 src = tcp_hdr(skb)->source;
902 else if (key->ip.proto == IPPROTO_SCTP)
903 src = sctp_hdr(skb)->source;
904 else
905 return;
906
907 key->tp.src = src;
908 } else {
909 __be16 dst;
910
911 key->ct_state |= OVS_CS_F_DST_NAT;
912 if (key->eth.type == htons(ETH_P_IP))
913 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
914 else if (key->eth.type == htons(ETH_P_IPV6))
915 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
916 sizeof(key->ipv6.addr.dst));
917 else
918 return;
919
920 if (key->ip.proto == IPPROTO_UDP)
921 dst = udp_hdr(skb)->dest;
922 else if (key->ip.proto == IPPROTO_TCP)
923 dst = tcp_hdr(skb)->dest;
924 else if (key->ip.proto == IPPROTO_SCTP)
925 dst = sctp_hdr(skb)->dest;
926 else
927 return;
928
929 key->tp.dst = dst;
930 }
931 }
932
933 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
934 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
935 const struct ovs_conntrack_info *info,
936 struct sk_buff *skb, struct nf_conn *ct,
937 enum ip_conntrack_info ctinfo)
938 {
939 enum nf_nat_manip_type maniptype;
940 int err;
941
942 #ifdef HAVE_NF_CT_IS_UNTRACKED
943 if (nf_ct_is_untracked(ct)) {
944 /* A NAT action may only be performed on tracked packets. */
945 return NF_ACCEPT;
946 }
947 #endif /* HAVE_NF_CT_IS_UNTRACKED */
948
949 /* Add NAT extension if not confirmed yet. */
950 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
951 return NF_ACCEPT; /* Can't NAT. */
952
953 /* Determine NAT type.
954 * Check if the NAT type can be deduced from the tracked connection.
955 * Make sure new expected connections (IP_CT_RELATED) are NATted only
956 * when committing.
957 */
958 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
959 ct->status & IPS_NAT_MASK &&
960 (ctinfo != IP_CT_RELATED || info->commit)) {
961 /* NAT an established or related connection like before. */
962 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
963 /* This is the REPLY direction for a connection
964 * for which NAT was applied in the forward
965 * direction. Do the reverse NAT.
966 */
967 maniptype = ct->status & IPS_SRC_NAT
968 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
969 else
970 maniptype = ct->status & IPS_SRC_NAT
971 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
972 } else if (info->nat & OVS_CT_SRC_NAT) {
973 maniptype = NF_NAT_MANIP_SRC;
974 } else if (info->nat & OVS_CT_DST_NAT) {
975 maniptype = NF_NAT_MANIP_DST;
976 } else {
977 return NF_ACCEPT; /* Connection is not NATed. */
978 }
979 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
980
981 if (err == NF_ACCEPT &&
982 ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
983 if (maniptype == NF_NAT_MANIP_SRC)
984 maniptype = NF_NAT_MANIP_DST;
985 else
986 maniptype = NF_NAT_MANIP_SRC;
987
988 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range,
989 maniptype);
990 }
991
992 /* Mark NAT done if successful and update the flow key. */
993 if (err == NF_ACCEPT)
994 ovs_nat_update_key(key, skb, maniptype);
995
996 return err;
997 }
998 #else /* !CONFIG_NF_NAT_NEEDED */
999 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
1000 const struct ovs_conntrack_info *info,
1001 struct sk_buff *skb, struct nf_conn *ct,
1002 enum ip_conntrack_info ctinfo)
1003 {
1004 return NF_ACCEPT;
1005 }
1006 #endif
1007
1008 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
1009 * not done already. Update key with new CT state after passing the packet
1010 * through conntrack.
1011 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
1012 * set to NULL and 0 will be returned.
1013 */
1014 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
1015 const struct ovs_conntrack_info *info,
1016 struct sk_buff *skb)
1017 {
1018 /* If we are recirculating packets to match on conntrack fields and
1019 * committing with a separate conntrack action, then we don't need to
1020 * actually run the packet through conntrack twice unless it's for a
1021 * different zone.
1022 */
1023 bool cached = skb_nfct_cached(net, key, info, skb);
1024 enum ip_conntrack_info ctinfo;
1025 struct nf_conn *ct;
1026
1027 if (!cached) {
1028 struct nf_hook_state state = {
1029 .hook = NF_INET_PRE_ROUTING,
1030 .pf = info->family,
1031 .net = net,
1032 };
1033 struct nf_conn *tmpl = info->ct;
1034 int err;
1035
1036 /* Associate skb with specified zone. */
1037 if (tmpl) {
1038 if (skb_nfct(skb))
1039 nf_conntrack_put(skb_nfct(skb));
1040 nf_conntrack_get(&tmpl->ct_general);
1041 nf_ct_set(skb, tmpl, IP_CT_NEW);
1042 }
1043
1044 err = nf_conntrack_in(skb, &state);
1045 if (err != NF_ACCEPT)
1046 return -ENOENT;
1047
1048 /* Clear CT state NAT flags to mark that we have not yet done
1049 * NAT after the nf_conntrack_in() call. We can actually clear
1050 * the whole state, as it will be re-initialized below.
1051 */
1052 key->ct_state = 0;
1053
1054 /* Update the key, but keep the NAT flags. */
1055 ovs_ct_update_key(skb, info, key, true, true);
1056 }
1057
1058 ct = nf_ct_get(skb, &ctinfo);
1059 if (ct) {
1060 bool add_helper = false;
1061
1062 /* Packets starting a new connection must be NATted before the
1063 * helper, so that the helper knows about the NAT. We enforce
1064 * this by delaying both NAT and helper calls for unconfirmed
1065 * connections until the committing CT action. For later
1066 * packets NAT and Helper may be called in either order.
1067 *
1068 * NAT will be done only if the CT action has NAT, and only
1069 * once per packet (per zone), as guarded by the NAT bits in
1070 * the key->ct_state.
1071 */
1072 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
1073 (nf_ct_is_confirmed(ct) || info->commit) &&
1074 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
1075 return -EINVAL;
1076 }
1077
1078 /* Userspace may decide to perform a ct lookup without a helper
1079 * specified followed by a (recirculate and) commit with one,
1080 * or attach a helper in a later commit. Therefore, for
1081 * connections which we will commit, we may need to attach
1082 * the helper here.
1083 */
1084 if (info->commit && info->helper && !nfct_help(ct)) {
1085 int err = __nf_ct_try_assign_helper(ct, info->ct,
1086 GFP_ATOMIC);
1087 if (err)
1088 return err;
1089 add_helper = true;
1090
1091 /* helper installed, add seqadj if NAT is required */
1092 if (info->nat && !nfct_seqadj(ct)) {
1093 if (!nfct_seqadj_ext_add(ct))
1094 return -EINVAL;
1095 }
1096 }
1097
1098 /* Call the helper only if:
1099 * - nf_conntrack_in() was executed above ("!cached") or a
1100 * helper was just attached ("add_helper") for a confirmed
1101 * connection, or
1102 * - When committing an unconfirmed connection.
1103 */
1104 if ((nf_ct_is_confirmed(ct) ? !cached || add_helper :
1105 info->commit) &&
1106 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
1107 return -EINVAL;
1108 }
1109 }
1110
1111 return 0;
1112 }
1113
1114 /* Lookup connection and read fields into key. */
1115 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
1116 const struct ovs_conntrack_info *info,
1117 struct sk_buff *skb)
1118 {
1119 struct nf_conntrack_expect *exp;
1120
1121 /* If we pass an expected packet through nf_conntrack_in() the
1122 * expectation is typically removed, but the packet could still be
1123 * lost in upcall processing. To prevent this from happening we
1124 * perform an explicit expectation lookup. Expected connections are
1125 * always new, and will be passed through conntrack only when they are
1126 * committed, as it is OK to remove the expectation at that time.
1127 */
1128 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
1129 if (exp) {
1130 u8 state;
1131
1132 /* NOTE: New connections are NATted and Helped only when
1133 * committed, so we are not calling into NAT here.
1134 */
1135 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
1136 __ovs_ct_update_key(key, state, &info->zone, exp->master);
1137 } else {
1138 struct nf_conn *ct;
1139 int err;
1140
1141 err = __ovs_ct_lookup(net, key, info, skb);
1142 if (err)
1143 return err;
1144
1145 ct = (struct nf_conn *)skb_nfct(skb);
1146 if (ct)
1147 nf_ct_deliver_cached_events(ct);
1148 }
1149
1150 return 0;
1151 }
1152
1153 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
1154 {
1155 size_t i;
1156
1157 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
1158 if (labels->ct_labels_32[i])
1159 return true;
1160
1161 return false;
1162 }
1163
1164 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1165 static struct hlist_head *ct_limit_hash_bucket(
1166 const struct ovs_ct_limit_info *info, u16 zone)
1167 {
1168 return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
1169 }
1170
1171 /* Call with ovs_mutex */
1172 static void ct_limit_set(const struct ovs_ct_limit_info *info,
1173 struct ovs_ct_limit *new_ct_limit)
1174 {
1175 struct ovs_ct_limit *ct_limit;
1176 struct hlist_head *head;
1177
1178 head = ct_limit_hash_bucket(info, new_ct_limit->zone);
1179 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1180 if (ct_limit->zone == new_ct_limit->zone) {
1181 hlist_replace_rcu(&ct_limit->hlist_node,
1182 &new_ct_limit->hlist_node);
1183 kfree_rcu(ct_limit, rcu);
1184 return;
1185 }
1186 }
1187
1188 hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
1189 }
1190
1191 /* Call with ovs_mutex */
1192 static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
1193 {
1194 struct ovs_ct_limit *ct_limit;
1195 struct hlist_head *head;
1196 struct hlist_node *n;
1197
1198 head = ct_limit_hash_bucket(info, zone);
1199 hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
1200 if (ct_limit->zone == zone) {
1201 hlist_del_rcu(&ct_limit->hlist_node);
1202 kfree_rcu(ct_limit, rcu);
1203 return;
1204 }
1205 }
1206 }
1207
1208 /* Call with RCU read lock */
1209 static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
1210 {
1211 struct ovs_ct_limit *ct_limit;
1212 struct hlist_head *head;
1213
1214 head = ct_limit_hash_bucket(info, zone);
1215 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1216 if (ct_limit->zone == zone)
1217 return ct_limit->limit;
1218 }
1219
1220 return info->default_limit;
1221 }
1222
1223 static int ovs_ct_check_limit(struct net *net,
1224 const struct ovs_conntrack_info *info,
1225 const struct nf_conntrack_tuple *tuple)
1226 {
1227 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1228 const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
1229 u32 per_zone_limit, connections;
1230 u32 conncount_key;
1231
1232 conncount_key = info->zone.id;
1233
1234 per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
1235 if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
1236 return 0;
1237
1238 connections = nf_conncount_count(net, ct_limit_info->data,
1239 &conncount_key, tuple, &info->zone);
1240 if (connections > per_zone_limit)
1241 return -ENOMEM;
1242
1243 return 0;
1244 }
1245 #endif
1246
1247 /* Lookup connection and confirm if unconfirmed. */
1248 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
1249 const struct ovs_conntrack_info *info,
1250 struct sk_buff *skb)
1251 {
1252 enum ip_conntrack_info ctinfo;
1253 struct nf_conn *ct;
1254 int err;
1255
1256 err = __ovs_ct_lookup(net, key, info, skb);
1257 if (err)
1258 return err;
1259
1260 /* The connection could be invalid, in which case this is a no-op.*/
1261 ct = nf_ct_get(skb, &ctinfo);
1262 if (!ct)
1263 return 0;
1264
1265 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1266 if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
1267 if (!nf_ct_is_confirmed(ct)) {
1268 err = ovs_ct_check_limit(net, info,
1269 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1270 if (err) {
1271 net_warn_ratelimited("openvswitch: zone: %u "
1272 "exceeds conntrack limit\n",
1273 info->zone.id);
1274 return err;
1275 }
1276 }
1277 }
1278 #endif
1279
1280 /* Set the conntrack event mask if given. NEW and DELETE events have
1281 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1282 * typically would receive many kinds of updates. Setting the event
1283 * mask allows those events to be filtered. The set event mask will
1284 * remain in effect for the lifetime of the connection unless changed
1285 * by a further CT action with both the commit flag and the eventmask
1286 * option. */
1287 if (info->have_eventmask) {
1288 struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
1289
1290 if (cache)
1291 cache->ctmask = info->eventmask;
1292 }
1293
1294 /* Apply changes before confirming the connection so that the initial
1295 * conntrack NEW netlink event carries the values given in the CT
1296 * action.
1297 */
1298 if (info->mark.mask) {
1299 err = ovs_ct_set_mark(ct, key, info->mark.value,
1300 info->mark.mask);
1301 if (err)
1302 return err;
1303 }
1304 if (!nf_ct_is_confirmed(ct)) {
1305 err = ovs_ct_init_labels(ct, key, &info->labels.value,
1306 &info->labels.mask);
1307 if (err)
1308 return err;
1309 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1310 labels_nonzero(&info->labels.mask)) {
1311 err = ovs_ct_set_labels(ct, key, &info->labels.value,
1312 &info->labels.mask);
1313 if (err)
1314 return err;
1315 }
1316 /* This will take care of sending queued events even if the connection
1317 * is already confirmed.
1318 */
1319 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1320 return -EINVAL;
1321
1322 return 0;
1323 }
1324
1325 /* Trim the skb to the length specified by the IP/IPv6 header,
1326 * removing any trailing lower-layer padding. This prepares the skb
1327 * for higher-layer processing that assumes skb->len excludes padding
1328 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1329 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1330 */
1331 static int ovs_skb_network_trim(struct sk_buff *skb)
1332 {
1333 unsigned int len;
1334 int err;
1335
1336 switch (skb->protocol) {
1337 case htons(ETH_P_IP):
1338 len = ntohs(ip_hdr(skb)->tot_len);
1339 break;
1340 case htons(ETH_P_IPV6):
1341 len = sizeof(struct ipv6hdr)
1342 + ntohs(ipv6_hdr(skb)->payload_len);
1343 break;
1344 default:
1345 len = skb->len;
1346 }
1347
1348 err = pskb_trim_rcsum(skb, len);
1349 if (err)
1350 kfree_skb(skb);
1351
1352 return err;
1353 }
1354
1355 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1356 * value if 'skb' is freed.
1357 */
1358 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
1359 struct sw_flow_key *key,
1360 const struct ovs_conntrack_info *info)
1361 {
1362 int nh_ofs;
1363 int err;
1364
1365 /* The conntrack module expects to be working at L3. */
1366 nh_ofs = skb_network_offset(skb);
1367 skb_pull_rcsum(skb, nh_ofs);
1368
1369 err = ovs_skb_network_trim(skb);
1370 if (err)
1371 return err;
1372
1373 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
1374 err = handle_fragments(net, key, info->zone.id, skb);
1375 if (err)
1376 return err;
1377 }
1378
1379 if (info->commit)
1380 err = ovs_ct_commit(net, key, info, skb);
1381 else
1382 err = ovs_ct_lookup(net, key, info, skb);
1383
1384 skb_push(skb, nh_ofs);
1385 skb_postpush_rcsum(skb, skb->data, nh_ofs);
1386 if (err)
1387 kfree_skb(skb);
1388 return err;
1389 }
1390
1391 int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
1392 {
1393 if (skb_nfct(skb)) {
1394 nf_conntrack_put(skb_nfct(skb));
1395 #ifdef HAVE_IP_CT_UNTRACKED
1396 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1397 #else
1398 nf_ct_set(skb, NULL, 0);
1399 #endif
1400 ovs_ct_fill_key(skb, key);
1401 }
1402
1403 return 0;
1404 }
1405
1406 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
1407 const struct sw_flow_key *key, bool log)
1408 {
1409 struct nf_conntrack_helper *helper;
1410 struct nf_conn_help *help;
1411 int ret = 0;
1412
1413 helper = nf_conntrack_helper_try_module_get(name, info->family,
1414 key->ip.proto);
1415 if (!helper) {
1416 OVS_NLERR(log, "Unknown helper \"%s\"", name);
1417 return -EINVAL;
1418 }
1419
1420 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
1421 if (!help) {
1422 nf_conntrack_helper_put(helper);
1423 return -ENOMEM;
1424 }
1425
1426 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1427 if (info->nat) {
1428 ret = nf_nat_helper_try_module_get(name, info->family,
1429 key->ip.proto);
1430 if (ret) {
1431 nf_conntrack_helper_put(helper);
1432 OVS_NLERR(log, "Failed to load \"%s\" NAT helper, error: %d",
1433 name, ret);
1434 return ret;
1435 }
1436 }
1437 #endif
1438
1439 rcu_assign_pointer(help->helper, helper);
1440 info->helper = helper;
1441 return ret;
1442 }
1443
1444 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1445 static int parse_nat(const struct nlattr *attr,
1446 struct ovs_conntrack_info *info, bool log)
1447 {
1448 struct nlattr *a;
1449 int rem;
1450 bool have_ip_max = false;
1451 bool have_proto_max = false;
1452 bool ip_vers = (info->family == NFPROTO_IPV6);
1453
1454 nla_for_each_nested(a, attr, rem) {
1455 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1456 [OVS_NAT_ATTR_SRC] = {0, 0},
1457 [OVS_NAT_ATTR_DST] = {0, 0},
1458 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1459 sizeof(struct in6_addr)},
1460 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1461 sizeof(struct in6_addr)},
1462 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1463 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1464 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1465 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1466 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1467 };
1468 int type = nla_type(a);
1469
1470 if (type > OVS_NAT_ATTR_MAX) {
1471 OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
1472 type, OVS_NAT_ATTR_MAX);
1473 return -EINVAL;
1474 }
1475
1476 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1477 OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
1478 type, nla_len(a),
1479 ovs_nat_attr_lens[type][ip_vers]);
1480 return -EINVAL;
1481 }
1482
1483 switch (type) {
1484 case OVS_NAT_ATTR_SRC:
1485 case OVS_NAT_ATTR_DST:
1486 if (info->nat) {
1487 OVS_NLERR(log, "Only one type of NAT may be specified");
1488 return -ERANGE;
1489 }
1490 info->nat |= OVS_CT_NAT;
1491 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1492 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1493 break;
1494
1495 case OVS_NAT_ATTR_IP_MIN:
1496 nla_memcpy(&info->range.min_addr, a,
1497 sizeof(info->range.min_addr));
1498 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1499 break;
1500
1501 case OVS_NAT_ATTR_IP_MAX:
1502 have_ip_max = true;
1503 nla_memcpy(&info->range.max_addr, a,
1504 sizeof(info->range.max_addr));
1505 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1506 break;
1507
1508 case OVS_NAT_ATTR_PROTO_MIN:
1509 info->range.min_proto.all = htons(nla_get_u16(a));
1510 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1511 break;
1512
1513 case OVS_NAT_ATTR_PROTO_MAX:
1514 have_proto_max = true;
1515 info->range.max_proto.all = htons(nla_get_u16(a));
1516 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1517 break;
1518
1519 case OVS_NAT_ATTR_PERSISTENT:
1520 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1521 break;
1522
1523 case OVS_NAT_ATTR_PROTO_HASH:
1524 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1525 break;
1526
1527 case OVS_NAT_ATTR_PROTO_RANDOM:
1528 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1529 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1530 #else
1531 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1532 info->random_fully_compat = true;
1533 #endif
1534 break;
1535
1536 default:
1537 OVS_NLERR(log, "Unknown nat attribute (%d)", type);
1538 return -EINVAL;
1539 }
1540 }
1541
1542 if (rem > 0) {
1543 OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
1544 return -EINVAL;
1545 }
1546 if (!info->nat) {
1547 /* Do not allow flags if no type is given. */
1548 if (info->range.flags) {
1549 OVS_NLERR(log,
1550 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1551 );
1552 return -EINVAL;
1553 }
1554 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1555 } else if (!info->commit) {
1556 OVS_NLERR(log,
1557 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1558 );
1559 return -EINVAL;
1560 }
1561 /* Allow missing IP_MAX. */
1562 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1563 memcpy(&info->range.max_addr, &info->range.min_addr,
1564 sizeof(info->range.max_addr));
1565 }
1566 /* Allow missing PROTO_MAX. */
1567 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1568 !have_proto_max) {
1569 info->range.max_proto.all = info->range.min_proto.all;
1570 }
1571 return 0;
1572 }
1573 #endif
1574
1575 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
1576 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
1577 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
1578 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1579 .maxlen = sizeof(u16) },
1580 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1581 .maxlen = sizeof(struct md_mark) },
1582 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1583 .maxlen = sizeof(struct md_labels) },
1584 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
1585 .maxlen = NF_CT_HELPER_NAME_LEN },
1586 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1587 /* NAT length is checked when parsing the nested attributes. */
1588 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1589 #endif
1590 [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
1591 .maxlen = sizeof(u32) },
1592 [OVS_CT_ATTR_TIMEOUT] = { .minlen = 1,
1593 .maxlen = CTNL_TIMEOUT_NAME_MAX },
1594 };
1595
1596 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
1597 const char **helper, bool log)
1598 {
1599 struct nlattr *a;
1600 int rem;
1601
1602 nla_for_each_nested(a, attr, rem) {
1603 int type = nla_type(a);
1604 int maxlen;
1605 int minlen;
1606
1607 if (type > OVS_CT_ATTR_MAX) {
1608 OVS_NLERR(log,
1609 "Unknown conntrack attr (type=%d, max=%d)",
1610 type, OVS_CT_ATTR_MAX);
1611 return -EINVAL;
1612 }
1613
1614 maxlen = ovs_ct_attr_lens[type].maxlen;
1615 minlen = ovs_ct_attr_lens[type].minlen;
1616 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1617 OVS_NLERR(log,
1618 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1619 type, nla_len(a), maxlen);
1620 return -EINVAL;
1621 }
1622
1623 switch (type) {
1624 case OVS_CT_ATTR_FORCE_COMMIT:
1625 info->force = true;
1626 /* fall through. */
1627 case OVS_CT_ATTR_COMMIT:
1628 info->commit = true;
1629 break;
1630 #ifdef CONFIG_NF_CONNTRACK_ZONES
1631 case OVS_CT_ATTR_ZONE:
1632 info->zone.id = nla_get_u16(a);
1633 break;
1634 #endif
1635 #ifdef CONFIG_NF_CONNTRACK_MARK
1636 case OVS_CT_ATTR_MARK: {
1637 struct md_mark *mark = nla_data(a);
1638
1639 if (!mark->mask) {
1640 OVS_NLERR(log, "ct_mark mask cannot be 0");
1641 return -EINVAL;
1642 }
1643 info->mark = *mark;
1644 break;
1645 }
1646 #endif
1647 #ifdef CONFIG_NF_CONNTRACK_LABELS
1648 case OVS_CT_ATTR_LABELS: {
1649 struct md_labels *labels = nla_data(a);
1650
1651 if (!labels_nonzero(&labels->mask)) {
1652 OVS_NLERR(log, "ct_labels mask cannot be 0");
1653 return -EINVAL;
1654 }
1655 info->labels = *labels;
1656 break;
1657 }
1658 #endif
1659 case OVS_CT_ATTR_HELPER:
1660 *helper = nla_data(a);
1661 if (!memchr(*helper, '\0', nla_len(a))) {
1662 OVS_NLERR(log, "Invalid conntrack helper");
1663 return -EINVAL;
1664 }
1665 break;
1666 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1667 case OVS_CT_ATTR_NAT: {
1668 int err = parse_nat(a, info, log);
1669
1670 if (err)
1671 return err;
1672 break;
1673 }
1674 #endif
1675 case OVS_CT_ATTR_EVENTMASK:
1676 info->have_eventmask = true;
1677 info->eventmask = nla_get_u32(a);
1678 break;
1679 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1680 case OVS_CT_ATTR_TIMEOUT:
1681 memcpy(info->timeout, nla_data(a), nla_len(a));
1682 if (!memchr(info->timeout, '\0', nla_len(a))) {
1683 OVS_NLERR(log, "Invalid conntrack timeout");
1684 return -EINVAL;
1685 }
1686 break;
1687 #endif
1688
1689 default:
1690 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1691 type);
1692 return -EINVAL;
1693 }
1694 }
1695
1696 #ifdef CONFIG_NF_CONNTRACK_MARK
1697 if (!info->commit && info->mark.mask) {
1698 OVS_NLERR(log,
1699 "Setting conntrack mark requires 'commit' flag.");
1700 return -EINVAL;
1701 }
1702 #endif
1703 #ifdef CONFIG_NF_CONNTRACK_LABELS
1704 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1705 OVS_NLERR(log,
1706 "Setting conntrack labels requires 'commit' flag.");
1707 return -EINVAL;
1708 }
1709 #endif
1710 if (rem > 0) {
1711 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1712 return -EINVAL;
1713 }
1714
1715 return 0;
1716 }
1717
1718 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
1719 {
1720 if (attr == OVS_KEY_ATTR_CT_STATE)
1721 return true;
1722 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1723 attr == OVS_KEY_ATTR_CT_ZONE)
1724 return true;
1725 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1726 attr == OVS_KEY_ATTR_CT_MARK)
1727 return true;
1728 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1729 attr == OVS_KEY_ATTR_CT_LABELS) {
1730 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1731
1732 return ovs_net->xt_label;
1733 }
1734
1735 return false;
1736 }
1737
1738 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1739 const struct sw_flow_key *key,
1740 struct sw_flow_actions **sfa, bool log)
1741 {
1742 struct ovs_conntrack_info ct_info;
1743 const char *helper = NULL;
1744 u16 family;
1745 int err;
1746
1747 family = key_to_nfproto(key);
1748 if (family == NFPROTO_UNSPEC) {
1749 OVS_NLERR(log, "ct family unspecified");
1750 return -EINVAL;
1751 }
1752
1753 memset(&ct_info, 0, sizeof(ct_info));
1754 ct_info.family = family;
1755
1756 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1757 NF_CT_DEFAULT_ZONE_DIR, 0);
1758
1759 err = parse_ct(attr, &ct_info, &helper, log);
1760 if (err)
1761 return err;
1762
1763 /* Set up template for tracking connections in specific zones. */
1764 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1765 if (!ct_info.ct) {
1766 OVS_NLERR(log, "Failed to allocate conntrack template");
1767 return -ENOMEM;
1768 }
1769
1770 if (ct_info.timeout[0]) {
1771 if (nf_ct_set_timeout(net, ct_info.ct, family, key->ip.proto,
1772 ct_info.timeout))
1773 pr_info_ratelimited("Failed to associated timeout "
1774 "policy `%s'\n", ct_info.timeout);
1775 else
1776 ct_info.nf_ct_timeout = rcu_dereference(
1777 nf_ct_timeout_find(ct_info.ct)->timeout);
1778
1779 }
1780
1781 if (helper) {
1782 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1783 if (err)
1784 goto err_free_ct;
1785 }
1786
1787 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1788 sizeof(ct_info), log);
1789 if (err)
1790 goto err_free_ct;
1791
1792 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1793 nf_conntrack_get(&ct_info.ct->ct_general);
1794 return 0;
1795 err_free_ct:
1796 __ovs_ct_free_action(&ct_info);
1797 return err;
1798 }
1799
1800 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1801 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1802 struct sk_buff *skb)
1803 {
1804 struct nlattr *start;
1805
1806 start = nla_nest_start_noflag(skb, OVS_CT_ATTR_NAT);
1807 if (!start)
1808 return false;
1809
1810 if (info->nat & OVS_CT_SRC_NAT) {
1811 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1812 return false;
1813 } else if (info->nat & OVS_CT_DST_NAT) {
1814 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1815 return false;
1816 } else {
1817 goto out;
1818 }
1819
1820 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
1821 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1822 info->family == NFPROTO_IPV4) {
1823 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1824 info->range.min_addr.ip) ||
1825 (info->range.max_addr.ip
1826 != info->range.min_addr.ip &&
1827 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1828 info->range.max_addr.ip))))
1829 return false;
1830 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1831 info->family == NFPROTO_IPV6) {
1832 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1833 &info->range.min_addr.in6) ||
1834 (memcmp(&info->range.max_addr.in6,
1835 &info->range.min_addr.in6,
1836 sizeof(info->range.max_addr.in6)) &&
1837 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1838 &info->range.max_addr.in6))))
1839 return false;
1840 } else {
1841 return false;
1842 }
1843 }
1844 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1845 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1846 ntohs(info->range.min_proto.all)) ||
1847 (info->range.max_proto.all != info->range.min_proto.all &&
1848 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1849 ntohs(info->range.max_proto.all)))))
1850 return false;
1851
1852 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1853 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1854 return false;
1855 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1856 nla_put_flag(skb, info->random_fully_compat
1857 ? OVS_NAT_ATTR_PROTO_RANDOM
1858 : OVS_NAT_ATTR_PROTO_HASH))
1859 return false;
1860 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1861 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1862 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1863 return false;
1864 #endif
1865 out:
1866 nla_nest_end(skb, start);
1867
1868 return true;
1869 }
1870 #endif
1871
1872 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1873 struct sk_buff *skb)
1874 {
1875 struct nlattr *start;
1876
1877 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CT);
1878 if (!start)
1879 return -EMSGSIZE;
1880
1881 if (ct_info->commit && nla_put_flag(skb, ct_info->force
1882 ? OVS_CT_ATTR_FORCE_COMMIT
1883 : OVS_CT_ATTR_COMMIT))
1884 return -EMSGSIZE;
1885 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1886 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1887 return -EMSGSIZE;
1888 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
1889 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1890 &ct_info->mark))
1891 return -EMSGSIZE;
1892 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1893 labels_nonzero(&ct_info->labels.mask) &&
1894 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1895 &ct_info->labels))
1896 return -EMSGSIZE;
1897 if (ct_info->helper) {
1898 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1899 ct_info->helper->name))
1900 return -EMSGSIZE;
1901 }
1902 if (ct_info->have_eventmask &&
1903 nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
1904 return -EMSGSIZE;
1905 if (ct_info->timeout[0]) {
1906 if (nla_put_string(skb, OVS_CT_ATTR_TIMEOUT, ct_info->timeout))
1907 return -EMSGSIZE;
1908 }
1909
1910 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1911 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1912 return -EMSGSIZE;
1913 #endif
1914 nla_nest_end(skb, start);
1915
1916 return 0;
1917 }
1918
1919 void ovs_ct_free_action(const struct nlattr *a)
1920 {
1921 struct ovs_conntrack_info *ct_info = nla_data(a);
1922
1923 __ovs_ct_free_action(ct_info);
1924 }
1925
1926 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1927 {
1928 if (ct_info->helper) {
1929 #if IS_ENABLED(CONFIG_NF_NAT_NEEDED)
1930 if (ct_info->nat)
1931 nf_nat_helper_put(ct_info->helper);
1932 #endif
1933 nf_conntrack_helper_put(ct_info->helper);
1934 }
1935 if (ct_info->ct) {
1936 if (ct_info->timeout[0])
1937 nf_ct_destroy_timeout(ct_info->ct);
1938 nf_ct_tmpl_free(ct_info->ct);
1939 }
1940 }
1941
1942 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1943 static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
1944 {
1945 int i, err;
1946
1947 ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
1948 GFP_KERNEL);
1949 if (!ovs_net->ct_limit_info)
1950 return -ENOMEM;
1951
1952 ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
1953 ovs_net->ct_limit_info->limits =
1954 kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
1955 GFP_KERNEL);
1956 if (!ovs_net->ct_limit_info->limits) {
1957 kfree(ovs_net->ct_limit_info);
1958 return -ENOMEM;
1959 }
1960
1961 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
1962 INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
1963
1964 ovs_net->ct_limit_info->data =
1965 nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
1966
1967 if (IS_ERR(ovs_net->ct_limit_info->data)) {
1968 err = PTR_ERR(ovs_net->ct_limit_info->data);
1969 kfree(ovs_net->ct_limit_info->limits);
1970 kfree(ovs_net->ct_limit_info);
1971 pr_err("openvswitch: failed to init nf_conncount %d\n", err);
1972 return err;
1973 }
1974 return 0;
1975 }
1976
1977 static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
1978 {
1979 const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
1980 int i;
1981
1982 nf_conncount_destroy(net, NFPROTO_INET, info->data);
1983 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
1984 struct hlist_head *head = &info->limits[i];
1985 struct ovs_ct_limit *ct_limit;
1986
1987 hlist_for_each_entry_rcu(ct_limit, head, hlist_node,
1988 lockdep_ovsl_is_held())
1989 kfree_rcu(ct_limit, rcu);
1990 }
1991 kfree(ovs_net->ct_limit_info->limits);
1992 kfree(ovs_net->ct_limit_info);
1993 }
1994
1995 static struct sk_buff *
1996 ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
1997 struct ovs_header **ovs_reply_header)
1998 {
1999 struct ovs_header *ovs_header = info->userhdr;
2000 struct sk_buff *skb;
2001
2002 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2003 if (!skb)
2004 return ERR_PTR(-ENOMEM);
2005
2006 *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
2007 info->snd_seq,
2008 &dp_ct_limit_genl_family, 0, cmd);
2009
2010 if (!*ovs_reply_header) {
2011 nlmsg_free(skb);
2012 return ERR_PTR(-EMSGSIZE);
2013 }
2014 (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
2015
2016 return skb;
2017 }
2018
2019 static bool check_zone_id(int zone_id, u16 *pzone)
2020 {
2021 if (zone_id >= 0 && zone_id <= 65535) {
2022 *pzone = (u16)zone_id;
2023 return true;
2024 }
2025 return false;
2026 }
2027
2028 static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
2029 struct ovs_ct_limit_info *info)
2030 {
2031 struct ovs_zone_limit *zone_limit;
2032 int rem;
2033 u16 zone;
2034
2035 rem = NLA_ALIGN(nla_len(nla_zone_limit));
2036 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
2037
2038 while (rem >= sizeof(*zone_limit)) {
2039 if (unlikely(zone_limit->zone_id ==
2040 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
2041 ovs_lock();
2042 info->default_limit = zone_limit->limit;
2043 ovs_unlock();
2044 } else if (unlikely(!check_zone_id(
2045 zone_limit->zone_id, &zone))) {
2046 OVS_NLERR(true, "zone id is out of range");
2047 } else {
2048 struct ovs_ct_limit *ct_limit;
2049
2050 ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL);
2051 if (!ct_limit)
2052 return -ENOMEM;
2053
2054 ct_limit->zone = zone;
2055 ct_limit->limit = zone_limit->limit;
2056
2057 ovs_lock();
2058 ct_limit_set(info, ct_limit);
2059 ovs_unlock();
2060 }
2061 rem -= NLA_ALIGN(sizeof(*zone_limit));
2062 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2063 NLA_ALIGN(sizeof(*zone_limit)));
2064 }
2065
2066 if (rem)
2067 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
2068
2069 return 0;
2070 }
2071
2072 static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
2073 struct ovs_ct_limit_info *info)
2074 {
2075 struct ovs_zone_limit *zone_limit;
2076 int rem;
2077 u16 zone;
2078
2079 rem = NLA_ALIGN(nla_len(nla_zone_limit));
2080 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
2081
2082 while (rem >= sizeof(*zone_limit)) {
2083 if (unlikely(zone_limit->zone_id ==
2084 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
2085 ovs_lock();
2086 info->default_limit = OVS_CT_LIMIT_DEFAULT;
2087 ovs_unlock();
2088 } else if (unlikely(!check_zone_id(
2089 zone_limit->zone_id, &zone))) {
2090 OVS_NLERR(true, "zone id is out of range");
2091 } else {
2092 ovs_lock();
2093 ct_limit_del(info, zone);
2094 ovs_unlock();
2095 }
2096 rem -= NLA_ALIGN(sizeof(*zone_limit));
2097 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2098 NLA_ALIGN(sizeof(*zone_limit)));
2099 }
2100
2101 if (rem)
2102 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
2103
2104 return 0;
2105 }
2106
2107 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
2108 struct sk_buff *reply)
2109 {
2110 struct ovs_zone_limit zone_limit;
2111 int err;
2112
2113 zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
2114 zone_limit.limit = info->default_limit;
2115 err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2116 if (err)
2117 return err;
2118
2119 return 0;
2120 }
2121
2122 static int __ovs_ct_limit_get_zone_limit(struct net *net,
2123 struct nf_conncount_data *data,
2124 u16 zone_id, u32 limit,
2125 struct sk_buff *reply)
2126 {
2127 struct nf_conntrack_zone ct_zone;
2128 struct ovs_zone_limit zone_limit;
2129 u32 conncount_key = zone_id;
2130
2131 zone_limit.zone_id = zone_id;
2132 zone_limit.limit = limit;
2133 nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
2134
2135 zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
2136 &ct_zone);
2137 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2138 }
2139
2140 static int ovs_ct_limit_get_zone_limit(struct net *net,
2141 struct nlattr *nla_zone_limit,
2142 struct ovs_ct_limit_info *info,
2143 struct sk_buff *reply)
2144 {
2145 struct ovs_zone_limit *zone_limit;
2146 int rem, err;
2147 u32 limit;
2148 u16 zone;
2149
2150 rem = NLA_ALIGN(nla_len(nla_zone_limit));
2151 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
2152
2153 while (rem >= sizeof(*zone_limit)) {
2154 if (unlikely(zone_limit->zone_id ==
2155 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
2156 err = ovs_ct_limit_get_default_limit(info, reply);
2157 if (err)
2158 return err;
2159 } else if (unlikely(!check_zone_id(zone_limit->zone_id,
2160 &zone))) {
2161 OVS_NLERR(true, "zone id is out of range");
2162 } else {
2163 rcu_read_lock();
2164 limit = ct_limit_get(info, zone);
2165 rcu_read_unlock();
2166
2167 err = __ovs_ct_limit_get_zone_limit(
2168 net, info->data, zone, limit, reply);
2169 if (err)
2170 return err;
2171 }
2172 rem -= NLA_ALIGN(sizeof(*zone_limit));
2173 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2174 NLA_ALIGN(sizeof(*zone_limit)));
2175 }
2176
2177 if (rem)
2178 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
2179
2180 return 0;
2181 }
2182
2183 static int ovs_ct_limit_get_all_zone_limit(struct net *net,
2184 struct ovs_ct_limit_info *info,
2185 struct sk_buff *reply)
2186 {
2187 struct ovs_ct_limit *ct_limit;
2188 struct hlist_head *head;
2189 int i, err = 0;
2190
2191 err = ovs_ct_limit_get_default_limit(info, reply);
2192 if (err)
2193 return err;
2194
2195 rcu_read_lock();
2196 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
2197 head = &info->limits[i];
2198 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
2199 err = __ovs_ct_limit_get_zone_limit(net, info->data,
2200 ct_limit->zone, ct_limit->limit, reply);
2201 if (err)
2202 goto exit_err;
2203 }
2204 }
2205
2206 exit_err:
2207 rcu_read_unlock();
2208 return err;
2209 }
2210
2211 static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
2212 {
2213 struct nlattr **a = info->attrs;
2214 struct sk_buff *reply;
2215 struct ovs_header *ovs_reply_header;
2216 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2217 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2218 int err;
2219
2220 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
2221 &ovs_reply_header);
2222 if (IS_ERR(reply))
2223 return PTR_ERR(reply);
2224
2225 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2226 err = -EINVAL;
2227 goto exit_err;
2228 }
2229
2230 err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2231 ct_limit_info);
2232 if (err)
2233 goto exit_err;
2234
2235 static_branch_enable(&ovs_ct_limit_enabled);
2236
2237 genlmsg_end(reply, ovs_reply_header);
2238 return genlmsg_reply(reply, info);
2239
2240 exit_err:
2241 nlmsg_free(reply);
2242 return err;
2243 }
2244
2245 static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
2246 {
2247 struct nlattr **a = info->attrs;
2248 struct sk_buff *reply;
2249 struct ovs_header *ovs_reply_header;
2250 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2251 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2252 int err;
2253
2254 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
2255 &ovs_reply_header);
2256 if (IS_ERR(reply))
2257 return PTR_ERR(reply);
2258
2259 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2260 err = -EINVAL;
2261 goto exit_err;
2262 }
2263
2264 err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2265 ct_limit_info);
2266 if (err)
2267 goto exit_err;
2268
2269 genlmsg_end(reply, ovs_reply_header);
2270 return genlmsg_reply(reply, info);
2271
2272 exit_err:
2273 nlmsg_free(reply);
2274 return err;
2275 }
2276
2277 static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
2278 {
2279 struct nlattr **a = info->attrs;
2280 struct nlattr *nla_reply;
2281 struct sk_buff *reply;
2282 struct ovs_header *ovs_reply_header;
2283 struct net *net = sock_net(skb->sk);
2284 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2285 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2286 int err;
2287
2288 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
2289 &ovs_reply_header);
2290 if (IS_ERR(reply))
2291 return PTR_ERR(reply);
2292
2293 nla_reply = nla_nest_start_noflag(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
2294 if (!nla_reply) {
2295 err = -EMSGSIZE;
2296 goto exit_err;
2297 }
2298
2299 if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2300 err = ovs_ct_limit_get_zone_limit(
2301 net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
2302 reply);
2303 if (err)
2304 goto exit_err;
2305 } else {
2306 err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
2307 reply);
2308 if (err)
2309 goto exit_err;
2310 }
2311
2312 nla_nest_end(reply, nla_reply);
2313 genlmsg_end(reply, ovs_reply_header);
2314 return genlmsg_reply(reply, info);
2315
2316 exit_err:
2317 nlmsg_free(reply);
2318 return err;
2319 }
2320
2321 static struct genl_ops ct_limit_genl_ops[] = {
2322 { .cmd = OVS_CT_LIMIT_CMD_SET,
2323 #ifdef HAVE_GENL_VALIDATE_FLAGS
2324 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2325 #endif
2326 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
2327 * privilege. */
2328 #ifdef HAVE_GENL_OPS_POLICY
2329 .policy = ct_limit_policy,
2330 #endif
2331 .doit = ovs_ct_limit_cmd_set,
2332 },
2333 { .cmd = OVS_CT_LIMIT_CMD_DEL,
2334 #ifdef HAVE_GENL_VALIDATE_FLAGS
2335 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2336 #endif
2337 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
2338 * privilege. */
2339 #ifdef HAVE_GENL_OPS_POLICY
2340 .policy = ct_limit_policy,
2341 #endif
2342 .doit = ovs_ct_limit_cmd_del,
2343 },
2344 { .cmd = OVS_CT_LIMIT_CMD_GET,
2345 #ifdef HAVE_GENL_VALIDATE_FLAGS
2346 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2347 #endif
2348 .flags = 0, /* OK for unprivileged users. */
2349 #ifdef HAVE_GENL_OPS_POLICY
2350 .policy = ct_limit_policy,
2351 #endif
2352 .doit = ovs_ct_limit_cmd_get,
2353 },
2354 };
2355
2356 static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
2357 .name = OVS_CT_LIMIT_MCGROUP,
2358 };
2359
2360 struct genl_family dp_ct_limit_genl_family __ro_after_init = {
2361 .hdrsize = sizeof(struct ovs_header),
2362 .name = OVS_CT_LIMIT_FAMILY,
2363 .version = OVS_CT_LIMIT_VERSION,
2364 .maxattr = OVS_CT_LIMIT_ATTR_MAX,
2365 #ifndef HAVE_GENL_OPS_POLICY
2366 .policy = ct_limit_policy,
2367 #endif
2368 .netnsok = true,
2369 .parallel_ops = true,
2370 .ops = ct_limit_genl_ops,
2371 .n_ops = ARRAY_SIZE(ct_limit_genl_ops),
2372 .mcgrps = &ovs_ct_limit_multicast_group,
2373 .n_mcgrps = 1,
2374 .module = THIS_MODULE,
2375 };
2376 #endif
2377
2378 int ovs_ct_init(struct net *net)
2379 {
2380 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
2381 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2382
2383 if (nf_connlabels_get(net, n_bits - 1)) {
2384 ovs_net->xt_label = false;
2385 OVS_NLERR(true, "Failed to set connlabel length");
2386 } else {
2387 ovs_net->xt_label = true;
2388 }
2389
2390 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2391 return ovs_ct_limit_init(net, ovs_net);
2392 #else
2393 return 0;
2394 #endif
2395 }
2396
2397 void ovs_ct_exit(struct net *net)
2398 {
2399 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2400
2401 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2402 ovs_ct_limit_exit(net, ovs_net);
2403 #endif
2404
2405 if (ovs_net->xt_label)
2406 nf_connlabels_put(net);
2407 }
2408
2409 #endif /* CONFIG_NF_CONNTRACK */