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