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