blob: a39ef1a048fde0f9995e09bf79169206f4be458e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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/module.h>
15#include <linux/openvswitch.h>
16#include <linux/tcp.h>
17#include <linux/udp.h>
18#include <linux/sctp.h>
19#include <net/ip.h>
20#include <net/netfilter/nf_conntrack_core.h>
21#include <net/netfilter/nf_conntrack_helper.h>
22#include <net/netfilter/nf_conntrack_labels.h>
23#include <net/netfilter/nf_conntrack_seqadj.h>
24#include <net/netfilter/nf_conntrack_zones.h>
25#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
26#include <net/ipv6_frag.h>
27
28#ifdef CONFIG_NF_NAT_NEEDED
29#include <linux/netfilter/nf_nat.h>
30#include <net/netfilter/nf_nat_core.h>
31#include <net/netfilter/nf_nat_l3proto.h>
32#endif
33
34#include "datapath.h"
35#include "conntrack.h"
36#include "flow.h"
37#include "flow_netlink.h"
38
39struct ovs_ct_len_tbl {
40 int maxlen;
41 int minlen;
42};
43
44/* Metadata mark for masked write to conntrack mark */
45struct md_mark {
46 u32 value;
47 u32 mask;
48};
49
50/* Metadata label for masked write to conntrack label. */
51struct md_labels {
52 struct ovs_key_ct_labels value;
53 struct ovs_key_ct_labels mask;
54};
55
56enum ovs_ct_nat {
57 OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
58 OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
59 OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
60};
61
62/* Conntrack action context for execution. */
63struct ovs_conntrack_info {
64 struct nf_conntrack_helper *helper;
65 struct nf_conntrack_zone zone;
66 struct nf_conn *ct;
67 u8 commit : 1;
68 u8 nat : 3; /* enum ovs_ct_nat */
69 u8 force : 1;
70 u8 have_eventmask : 1;
71 u16 family;
72 u32 eventmask; /* Mask of 1 << IPCT_*. */
73 struct md_mark mark;
74 struct md_labels labels;
75#ifdef CONFIG_NF_NAT_NEEDED
76 struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */
77#endif
78};
79
80static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
81
82static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
83
84static u16 key_to_nfproto(const struct sw_flow_key *key)
85{
86 switch (ntohs(key->eth.type)) {
87 case ETH_P_IP:
88 return NFPROTO_IPV4;
89 case ETH_P_IPV6:
90 return NFPROTO_IPV6;
91 default:
92 return NFPROTO_UNSPEC;
93 }
94}
95
96/* Map SKB connection state into the values used by flow definition. */
97static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
98{
99 u8 ct_state = OVS_CS_F_TRACKED;
100
101 switch (ctinfo) {
102 case IP_CT_ESTABLISHED_REPLY:
103 case IP_CT_RELATED_REPLY:
104 ct_state |= OVS_CS_F_REPLY_DIR;
105 break;
106 default:
107 break;
108 }
109
110 switch (ctinfo) {
111 case IP_CT_ESTABLISHED:
112 case IP_CT_ESTABLISHED_REPLY:
113 ct_state |= OVS_CS_F_ESTABLISHED;
114 break;
115 case IP_CT_RELATED:
116 case IP_CT_RELATED_REPLY:
117 ct_state |= OVS_CS_F_RELATED;
118 break;
119 case IP_CT_NEW:
120 ct_state |= OVS_CS_F_NEW;
121 break;
122 default:
123 break;
124 }
125
126 return ct_state;
127}
128
129static u32 ovs_ct_get_mark(const struct nf_conn *ct)
130{
131#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
132 return ct ? ct->mark : 0;
133#else
134 return 0;
135#endif
136}
137
138/* Guard against conntrack labels max size shrinking below 128 bits. */
139#if NF_CT_LABELS_MAX_SIZE < 16
140#error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
141#endif
142
143static void ovs_ct_get_labels(const struct nf_conn *ct,
144 struct ovs_key_ct_labels *labels)
145{
146 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
147
148 if (cl)
149 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
150 else
151 memset(labels, 0, OVS_CT_LABELS_LEN);
152}
153
154static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
155 const struct nf_conntrack_tuple *orig,
156 u8 icmp_proto)
157{
158 key->ct_orig_proto = orig->dst.protonum;
159 if (orig->dst.protonum == icmp_proto) {
160 key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
161 key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
162 } else {
163 key->ct.orig_tp.src = orig->src.u.all;
164 key->ct.orig_tp.dst = orig->dst.u.all;
165 }
166}
167
168static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
169 const struct nf_conntrack_zone *zone,
170 const struct nf_conn *ct)
171{
172 key->ct_state = state;
173 key->ct_zone = zone->id;
174 key->ct.mark = ovs_ct_get_mark(ct);
175 ovs_ct_get_labels(ct, &key->ct.labels);
176
177 if (ct) {
178 const struct nf_conntrack_tuple *orig;
179
180 /* Use the master if we have one. */
181 if (ct->master)
182 ct = ct->master;
183 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
184
185 /* IP version must match with the master connection. */
186 if (key->eth.type == htons(ETH_P_IP) &&
187 nf_ct_l3num(ct) == NFPROTO_IPV4) {
188 key->ipv4.ct_orig.src = orig->src.u3.ip;
189 key->ipv4.ct_orig.dst = orig->dst.u3.ip;
190 __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
191 return;
192 } else if (key->eth.type == htons(ETH_P_IPV6) &&
193 !sw_flow_key_is_nd(key) &&
194 nf_ct_l3num(ct) == NFPROTO_IPV6) {
195 key->ipv6.ct_orig.src = orig->src.u3.in6;
196 key->ipv6.ct_orig.dst = orig->dst.u3.in6;
197 __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
198 return;
199 }
200 }
201 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
202 * original direction key fields.
203 */
204 key->ct_orig_proto = 0;
205}
206
207/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
208 * previously sent the packet to conntrack via the ct action. If
209 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
210 * initialized from the connection status.
211 */
212static void ovs_ct_update_key(const struct sk_buff *skb,
213 const struct ovs_conntrack_info *info,
214 struct sw_flow_key *key, bool post_ct,
215 bool keep_nat_flags)
216{
217 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
218 enum ip_conntrack_info ctinfo;
219 struct nf_conn *ct;
220 u8 state = 0;
221
222 ct = nf_ct_get(skb, &ctinfo);
223 if (ct) {
224 state = ovs_ct_get_state(ctinfo);
225 /* All unconfirmed entries are NEW connections. */
226 if (!nf_ct_is_confirmed(ct))
227 state |= OVS_CS_F_NEW;
228 /* OVS persists the related flag for the duration of the
229 * connection.
230 */
231 if (ct->master)
232 state |= OVS_CS_F_RELATED;
233 if (keep_nat_flags) {
234 state |= key->ct_state & OVS_CS_F_NAT_MASK;
235 } else {
236 if (ct->status & IPS_SRC_NAT)
237 state |= OVS_CS_F_SRC_NAT;
238 if (ct->status & IPS_DST_NAT)
239 state |= OVS_CS_F_DST_NAT;
240 }
241 zone = nf_ct_zone(ct);
242 } else if (post_ct) {
243 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
244 if (info)
245 zone = &info->zone;
246 }
247 __ovs_ct_update_key(key, state, zone, ct);
248}
249
250/* This is called to initialize CT key fields possibly coming in from the local
251 * stack.
252 */
253void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
254{
255 ovs_ct_update_key(skb, NULL, key, false, false);
256}
257
258int ovs_ct_put_key(const struct sw_flow_key *swkey,
259 const struct sw_flow_key *output, struct sk_buff *skb)
260{
261 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
262 return -EMSGSIZE;
263
264 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
265 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
266 return -EMSGSIZE;
267
268 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
269 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
270 return -EMSGSIZE;
271
272 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
273 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
274 &output->ct.labels))
275 return -EMSGSIZE;
276
277 if (swkey->ct_orig_proto) {
278 if (swkey->eth.type == htons(ETH_P_IP)) {
279 struct ovs_key_ct_tuple_ipv4 orig;
280
281 memset(&orig, 0, sizeof(orig));
282 orig.ipv4_src = output->ipv4.ct_orig.src;
283 orig.ipv4_dst = output->ipv4.ct_orig.dst;
284 orig.src_port = output->ct.orig_tp.src;
285 orig.dst_port = output->ct.orig_tp.dst;
286 orig.ipv4_proto = output->ct_orig_proto;
287
288 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
289 sizeof(orig), &orig))
290 return -EMSGSIZE;
291 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
292 struct ovs_key_ct_tuple_ipv6 orig;
293
294 memset(&orig, 0, sizeof(orig));
295 memcpy(orig.ipv6_src, output->ipv6.ct_orig.src.s6_addr32,
296 sizeof(orig.ipv6_src));
297 memcpy(orig.ipv6_dst, output->ipv6.ct_orig.dst.s6_addr32,
298 sizeof(orig.ipv6_dst));
299 orig.src_port = output->ct.orig_tp.src;
300 orig.dst_port = output->ct.orig_tp.dst;
301 orig.ipv6_proto = output->ct_orig_proto;
302
303 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
304 sizeof(orig), &orig))
305 return -EMSGSIZE;
306 }
307 }
308
309 return 0;
310}
311
312static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
313 u32 ct_mark, u32 mask)
314{
315#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
316 u32 new_mark;
317
318 new_mark = ct_mark | (ct->mark & ~(mask));
319 if (ct->mark != new_mark) {
320 ct->mark = new_mark;
321 if (nf_ct_is_confirmed(ct))
322 nf_conntrack_event_cache(IPCT_MARK, ct);
323 key->ct.mark = new_mark;
324 }
325
326 return 0;
327#else
328 return -ENOTSUPP;
329#endif
330}
331
332static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
333{
334 struct nf_conn_labels *cl;
335
336 cl = nf_ct_labels_find(ct);
337 if (!cl) {
338 nf_ct_labels_ext_add(ct);
339 cl = nf_ct_labels_find(ct);
340 }
341
342 return cl;
343}
344
345/* Initialize labels for a new, yet to be committed conntrack entry. Note that
346 * since the new connection is not yet confirmed, and thus no-one else has
347 * access to it's labels, we simply write them over.
348 */
349static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
350 const struct ovs_key_ct_labels *labels,
351 const struct ovs_key_ct_labels *mask)
352{
353 struct nf_conn_labels *cl, *master_cl;
354 bool have_mask = labels_nonzero(mask);
355
356 /* Inherit master's labels to the related connection? */
357 master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
358
359 if (!master_cl && !have_mask)
360 return 0; /* Nothing to do. */
361
362 cl = ovs_ct_get_conn_labels(ct);
363 if (!cl)
364 return -ENOSPC;
365
366 /* Inherit the master's labels, if any. */
367 if (master_cl)
368 *cl = *master_cl;
369
370 if (have_mask) {
371 u32 *dst = (u32 *)cl->bits;
372 int i;
373
374 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
375 dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
376 (labels->ct_labels_32[i]
377 & mask->ct_labels_32[i]);
378 }
379
380 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
381 * IPCT_LABEL bit is set in the event cache.
382 */
383 nf_conntrack_event_cache(IPCT_LABEL, ct);
384
385 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
386
387 return 0;
388}
389
390static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
391 const struct ovs_key_ct_labels *labels,
392 const struct ovs_key_ct_labels *mask)
393{
394 struct nf_conn_labels *cl;
395 int err;
396
397 cl = ovs_ct_get_conn_labels(ct);
398 if (!cl)
399 return -ENOSPC;
400
401 err = nf_connlabels_replace(ct, labels->ct_labels_32,
402 mask->ct_labels_32,
403 OVS_CT_LABELS_LEN_32);
404 if (err)
405 return err;
406
407 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
408
409 return 0;
410}
411
412/* 'skb' should already be pulled to nh_ofs. */
413static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
414{
415 const struct nf_conntrack_helper *helper;
416 const struct nf_conn_help *help;
417 enum ip_conntrack_info ctinfo;
418 unsigned int protoff;
419 struct nf_conn *ct;
420 int err;
421
422 ct = nf_ct_get(skb, &ctinfo);
423 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
424 return NF_ACCEPT;
425
426 help = nfct_help(ct);
427 if (!help)
428 return NF_ACCEPT;
429
430 helper = rcu_dereference(help->helper);
431 if (!helper)
432 return NF_ACCEPT;
433
434 switch (proto) {
435 case NFPROTO_IPV4:
436 protoff = ip_hdrlen(skb);
437 break;
438 case NFPROTO_IPV6: {
439 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
440 __be16 frag_off;
441 int ofs;
442
443 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
444 &frag_off);
445 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
446 pr_debug("proto header not found\n");
447 return NF_ACCEPT;
448 }
449 protoff = ofs;
450 break;
451 }
452 default:
453 WARN_ONCE(1, "helper invoked on non-IP family!");
454 return NF_DROP;
455 }
456
457 err = helper->help(skb, protoff, ct, ctinfo);
458 if (err != NF_ACCEPT)
459 return err;
460
461 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
462 * FTP with NAT) adusting the TCP payload size when mangling IP
463 * addresses and/or port numbers in the text-based control connection.
464 */
465 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
466 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
467 return NF_DROP;
468 return NF_ACCEPT;
469}
470
471/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
472 * value if 'skb' is freed.
473 */
474static int handle_fragments(struct net *net, struct sw_flow_key *key,
475 u16 zone, struct sk_buff *skb)
476{
477 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
478 int err;
479
480 if (key->eth.type == htons(ETH_P_IP)) {
481 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
482
483 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
484 err = ip_defrag(net, skb, user);
485 if (err)
486 return err;
487
488 ovs_cb.mru = IPCB(skb)->frag_max_size;
489#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
490 } else if (key->eth.type == htons(ETH_P_IPV6)) {
491 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
492
493 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
494 err = nf_ct_frag6_gather(net, skb, user);
495 if (err) {
496 if (err != -EINPROGRESS)
497 kfree_skb(skb);
498 return err;
499 }
500
501 key->ip.proto = ipv6_hdr(skb)->nexthdr;
502 ovs_cb.mru = IP6CB(skb)->frag_max_size;
503#endif
504 } else {
505 kfree_skb(skb);
506 return -EPFNOSUPPORT;
507 }
508
509 key->ip.frag = OVS_FRAG_TYPE_NONE;
510 skb_clear_hash(skb);
511 skb->ignore_df = 1;
512 *OVS_CB(skb) = ovs_cb;
513
514 return 0;
515}
516
517static struct nf_conntrack_expect *
518ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
519 u16 proto, const struct sk_buff *skb)
520{
521 struct nf_conntrack_tuple tuple;
522 struct nf_conntrack_expect *exp;
523
524 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
525 return NULL;
526
527 exp = __nf_ct_expect_find(net, zone, &tuple);
528 if (exp) {
529 struct nf_conntrack_tuple_hash *h;
530
531 /* Delete existing conntrack entry, if it clashes with the
532 * expectation. This can happen since conntrack ALGs do not
533 * check for clashes between (new) expectations and existing
534 * conntrack entries. nf_conntrack_in() will check the
535 * expectations only if a conntrack entry can not be found,
536 * which can lead to OVS finding the expectation (here) in the
537 * init direction, but which will not be removed by the
538 * nf_conntrack_in() call, if a matching conntrack entry is
539 * found instead. In this case all init direction packets
540 * would be reported as new related packets, while reply
541 * direction packets would be reported as un-related
542 * established packets.
543 */
544 h = nf_conntrack_find_get(net, zone, &tuple);
545 if (h) {
546 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
547
548 nf_ct_delete(ct, 0, 0);
549 nf_conntrack_put(&ct->ct_general);
550 }
551 }
552
553 return exp;
554}
555
556/* This replicates logic from nf_conntrack_core.c that is not exported. */
557static enum ip_conntrack_info
558ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
559{
560 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
561
562 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
563 return IP_CT_ESTABLISHED_REPLY;
564 /* Once we've had two way comms, always ESTABLISHED. */
565 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
566 return IP_CT_ESTABLISHED;
567 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
568 return IP_CT_RELATED;
569 return IP_CT_NEW;
570}
571
572/* Find an existing connection which this packet belongs to without
573 * re-attributing statistics or modifying the connection state. This allows an
574 * skb->_nfct lost due to an upcall to be recovered during actions execution.
575 *
576 * Must be called with rcu_read_lock.
577 *
578 * On success, populates skb->_nfct and returns the connection. Returns NULL
579 * if there is no existing entry.
580 */
581static struct nf_conn *
582ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
583 u8 l3num, struct sk_buff *skb, bool natted)
584{
585 const struct nf_conntrack_l3proto *l3proto;
586 const struct nf_conntrack_l4proto *l4proto;
587 struct nf_conntrack_tuple tuple;
588 struct nf_conntrack_tuple_hash *h;
589 struct nf_conn *ct;
590 unsigned int dataoff;
591 u8 protonum;
592
593 l3proto = __nf_ct_l3proto_find(l3num);
594 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
595 &protonum) <= 0) {
596 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
597 return NULL;
598 }
599 l4proto = __nf_ct_l4proto_find(l3num, protonum);
600 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
601 protonum, net, &tuple, l3proto, l4proto)) {
602 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
603 return NULL;
604 }
605
606 /* Must invert the tuple if skb has been transformed by NAT. */
607 if (natted) {
608 struct nf_conntrack_tuple inverse;
609
610 if (!nf_ct_invert_tuple(&inverse, &tuple, l3proto, l4proto)) {
611 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
612 return NULL;
613 }
614 tuple = inverse;
615 }
616
617 /* look for tuple match */
618 h = nf_conntrack_find_get(net, zone, &tuple);
619 if (!h)
620 return NULL; /* Not found. */
621
622 ct = nf_ct_tuplehash_to_ctrack(h);
623
624 /* Inverted packet tuple matches the reverse direction conntrack tuple,
625 * select the other tuplehash to get the right 'ctinfo' bits for this
626 * packet.
627 */
628 if (natted)
629 h = &ct->tuplehash[!h->tuple.dst.dir];
630
631 nf_ct_set(skb, ct, ovs_ct_get_info(h));
632 return ct;
633}
634
635static
636struct nf_conn *ovs_ct_executed(struct net *net,
637 const struct sw_flow_key *key,
638 const struct ovs_conntrack_info *info,
639 struct sk_buff *skb,
640 bool *ct_executed)
641{
642 struct nf_conn *ct = NULL;
643
644 /* If no ct, check if we have evidence that an existing conntrack entry
645 * might be found for this skb. This happens when we lose a skb->_nfct
646 * due to an upcall, or if the direction is being forced. If the
647 * connection was not confirmed, it is not cached and needs to be run
648 * through conntrack again.
649 */
650 *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
651 !(key->ct_state & OVS_CS_F_INVALID) &&
652 (key->ct_zone == info->zone.id);
653
654 if (*ct_executed || (!key->ct_state && info->force)) {
655 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
656 !!(key->ct_state &
657 OVS_CS_F_NAT_MASK));
658 }
659
660 return ct;
661}
662
663/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
664static bool skb_nfct_cached(struct net *net,
665 const struct sw_flow_key *key,
666 const struct ovs_conntrack_info *info,
667 struct sk_buff *skb)
668{
669 enum ip_conntrack_info ctinfo;
670 struct nf_conn *ct;
671 bool ct_executed = true;
672
673 ct = nf_ct_get(skb, &ctinfo);
674 if (!ct)
675 ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
676
677 if (ct)
678 nf_ct_get(skb, &ctinfo);
679 else
680 return false;
681
682 if (!net_eq(net, read_pnet(&ct->ct_net)))
683 return false;
684 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
685 return false;
686 if (info->helper) {
687 struct nf_conn_help *help;
688
689 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
690 if (help && rcu_access_pointer(help->helper) != info->helper)
691 return false;
692 }
693 /* Force conntrack entry direction to the current packet? */
694 if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
695 /* Delete the conntrack entry if confirmed, else just release
696 * the reference.
697 */
698 if (nf_ct_is_confirmed(ct))
699 nf_ct_delete(ct, 0, 0);
700
701 nf_conntrack_put(&ct->ct_general);
702 nf_ct_set(skb, NULL, 0);
703 return false;
704 }
705
706 return ct_executed;
707}
708
709#ifdef CONFIG_NF_NAT_NEEDED
710/* Modelled after nf_nat_ipv[46]_fn().
711 * range is only used for new, uninitialized NAT state.
712 * Returns either NF_ACCEPT or NF_DROP.
713 */
714static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
715 enum ip_conntrack_info ctinfo,
716 const struct nf_nat_range *range,
717 enum nf_nat_manip_type maniptype)
718{
719 int hooknum, nh_off, err = NF_ACCEPT;
720
721 nh_off = skb_network_offset(skb);
722 skb_pull_rcsum(skb, nh_off);
723
724 /* See HOOK2MANIP(). */
725 if (maniptype == NF_NAT_MANIP_SRC)
726 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
727 else
728 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
729
730 switch (ctinfo) {
731 case IP_CT_RELATED:
732 case IP_CT_RELATED_REPLY:
733 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
734 skb->protocol == htons(ETH_P_IP) &&
735 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
736 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
737 hooknum))
738 err = NF_DROP;
739 goto push;
740 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
741 skb->protocol == htons(ETH_P_IPV6)) {
742 __be16 frag_off;
743 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
744 int hdrlen = ipv6_skip_exthdr(skb,
745 sizeof(struct ipv6hdr),
746 &nexthdr, &frag_off);
747
748 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
749 if (!nf_nat_icmpv6_reply_translation(skb, ct,
750 ctinfo,
751 hooknum,
752 hdrlen))
753 err = NF_DROP;
754 goto push;
755 }
756 }
757 /* Non-ICMP, fall thru to initialize if needed. */
758 case IP_CT_NEW:
759 /* Seen it before? This can happen for loopback, retrans,
760 * or local packets.
761 */
762 if (!nf_nat_initialized(ct, maniptype)) {
763 /* Initialize according to the NAT action. */
764 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
765 /* Action is set up to establish a new
766 * mapping.
767 */
768 ? nf_nat_setup_info(ct, range, maniptype)
769 : nf_nat_alloc_null_binding(ct, hooknum);
770 if (err != NF_ACCEPT)
771 goto push;
772 }
773 break;
774
775 case IP_CT_ESTABLISHED:
776 case IP_CT_ESTABLISHED_REPLY:
777 break;
778
779 default:
780 err = NF_DROP;
781 goto push;
782 }
783
784 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
785push:
786 skb_push(skb, nh_off);
787 skb_postpush_rcsum(skb, skb->data, nh_off);
788
789 return err;
790}
791
792static void ovs_nat_update_key(struct sw_flow_key *key,
793 const struct sk_buff *skb,
794 enum nf_nat_manip_type maniptype)
795{
796 if (maniptype == NF_NAT_MANIP_SRC) {
797 __be16 src;
798
799 key->ct_state |= OVS_CS_F_SRC_NAT;
800 if (key->eth.type == htons(ETH_P_IP))
801 key->ipv4.addr.src = ip_hdr(skb)->saddr;
802 else if (key->eth.type == htons(ETH_P_IPV6))
803 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
804 sizeof(key->ipv6.addr.src));
805 else
806 return;
807
808 if (key->ip.proto == IPPROTO_UDP)
809 src = udp_hdr(skb)->source;
810 else if (key->ip.proto == IPPROTO_TCP)
811 src = tcp_hdr(skb)->source;
812 else if (key->ip.proto == IPPROTO_SCTP)
813 src = sctp_hdr(skb)->source;
814 else
815 return;
816
817 key->tp.src = src;
818 } else {
819 __be16 dst;
820
821 key->ct_state |= OVS_CS_F_DST_NAT;
822 if (key->eth.type == htons(ETH_P_IP))
823 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
824 else if (key->eth.type == htons(ETH_P_IPV6))
825 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
826 sizeof(key->ipv6.addr.dst));
827 else
828 return;
829
830 if (key->ip.proto == IPPROTO_UDP)
831 dst = udp_hdr(skb)->dest;
832 else if (key->ip.proto == IPPROTO_TCP)
833 dst = tcp_hdr(skb)->dest;
834 else if (key->ip.proto == IPPROTO_SCTP)
835 dst = sctp_hdr(skb)->dest;
836 else
837 return;
838
839 key->tp.dst = dst;
840 }
841}
842
843/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
844static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
845 const struct ovs_conntrack_info *info,
846 struct sk_buff *skb, struct nf_conn *ct,
847 enum ip_conntrack_info ctinfo)
848{
849 enum nf_nat_manip_type maniptype;
850 int err;
851
852 /* Add NAT extension if not confirmed yet. */
853 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
854 return NF_ACCEPT; /* Can't NAT. */
855
856 /* Determine NAT type.
857 * Check if the NAT type can be deduced from the tracked connection.
858 * Make sure new expected connections (IP_CT_RELATED) are NATted only
859 * when committing.
860 */
861 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
862 ct->status & IPS_NAT_MASK &&
863 (ctinfo != IP_CT_RELATED || info->commit)) {
864 /* NAT an established or related connection like before. */
865 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
866 /* This is the REPLY direction for a connection
867 * for which NAT was applied in the forward
868 * direction. Do the reverse NAT.
869 */
870 maniptype = ct->status & IPS_SRC_NAT
871 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
872 else
873 maniptype = ct->status & IPS_SRC_NAT
874 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
875 } else if (info->nat & OVS_CT_SRC_NAT) {
876 maniptype = NF_NAT_MANIP_SRC;
877 } else if (info->nat & OVS_CT_DST_NAT) {
878 maniptype = NF_NAT_MANIP_DST;
879 } else {
880 return NF_ACCEPT; /* Connection is not NATed. */
881 }
882 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
883
884 if (err == NF_ACCEPT &&
885 ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
886 if (maniptype == NF_NAT_MANIP_SRC)
887 maniptype = NF_NAT_MANIP_DST;
888 else
889 maniptype = NF_NAT_MANIP_SRC;
890
891 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range,
892 maniptype);
893 }
894
895 /* Mark NAT done if successful and update the flow key. */
896 if (err == NF_ACCEPT)
897 ovs_nat_update_key(key, skb, maniptype);
898
899 return err;
900}
901#else /* !CONFIG_NF_NAT_NEEDED */
902static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
903 const struct ovs_conntrack_info *info,
904 struct sk_buff *skb, struct nf_conn *ct,
905 enum ip_conntrack_info ctinfo)
906{
907 return NF_ACCEPT;
908}
909#endif
910
911/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
912 * not done already. Update key with new CT state after passing the packet
913 * through conntrack.
914 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
915 * set to NULL and 0 will be returned.
916 */
917static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
918 const struct ovs_conntrack_info *info,
919 struct sk_buff *skb)
920{
921 /* If we are recirculating packets to match on conntrack fields and
922 * committing with a separate conntrack action, then we don't need to
923 * actually run the packet through conntrack twice unless it's for a
924 * different zone.
925 */
926 bool cached = skb_nfct_cached(net, key, info, skb);
927 enum ip_conntrack_info ctinfo;
928 struct nf_conn *ct;
929
930 if (!cached) {
931 struct nf_conn *tmpl = info->ct;
932 int err;
933
934 /* Associate skb with specified zone. */
935 if (tmpl) {
936 if (skb_nfct(skb))
937 nf_conntrack_put(skb_nfct(skb));
938 nf_conntrack_get(&tmpl->ct_general);
939 nf_ct_set(skb, tmpl, IP_CT_NEW);
940 }
941
942 err = nf_conntrack_in(net, info->family,
943 NF_INET_PRE_ROUTING, skb);
944 if (err != NF_ACCEPT)
945 return -ENOENT;
946
947 /* Clear CT state NAT flags to mark that we have not yet done
948 * NAT after the nf_conntrack_in() call. We can actually clear
949 * the whole state, as it will be re-initialized below.
950 */
951 key->ct_state = 0;
952
953 /* Update the key, but keep the NAT flags. */
954 ovs_ct_update_key(skb, info, key, true, true);
955 }
956
957 ct = nf_ct_get(skb, &ctinfo);
958 if (ct) {
959 /* Packets starting a new connection must be NATted before the
960 * helper, so that the helper knows about the NAT. We enforce
961 * this by delaying both NAT and helper calls for unconfirmed
962 * connections until the committing CT action. For later
963 * packets NAT and Helper may be called in either order.
964 *
965 * NAT will be done only if the CT action has NAT, and only
966 * once per packet (per zone), as guarded by the NAT bits in
967 * the key->ct_state.
968 */
969 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
970 (nf_ct_is_confirmed(ct) || info->commit) &&
971 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
972 return -EINVAL;
973 }
974
975 /* Userspace may decide to perform a ct lookup without a helper
976 * specified followed by a (recirculate and) commit with one.
977 * Therefore, for unconfirmed connections which we will commit,
978 * we need to attach the helper here.
979 */
980 if (!nf_ct_is_confirmed(ct) && info->commit &&
981 info->helper && !nfct_help(ct)) {
982 int err = __nf_ct_try_assign_helper(ct, info->ct,
983 GFP_ATOMIC);
984 if (err)
985 return err;
986 }
987
988 /* Call the helper only if:
989 * - nf_conntrack_in() was executed above ("!cached") for a
990 * confirmed connection, or
991 * - When committing an unconfirmed connection.
992 */
993 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
994 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
995 return -EINVAL;
996 }
997 }
998
999 return 0;
1000}
1001
1002/* Lookup connection and read fields into key. */
1003static 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 struct nf_conntrack_expect *exp;
1008
1009 /* If we pass an expected packet through nf_conntrack_in() the
1010 * expectation is typically removed, but the packet could still be
1011 * lost in upcall processing. To prevent this from happening we
1012 * perform an explicit expectation lookup. Expected connections are
1013 * always new, and will be passed through conntrack only when they are
1014 * committed, as it is OK to remove the expectation at that time.
1015 */
1016 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
1017 if (exp) {
1018 u8 state;
1019
1020 /* NOTE: New connections are NATted and Helped only when
1021 * committed, so we are not calling into NAT here.
1022 */
1023 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
1024 __ovs_ct_update_key(key, state, &info->zone, exp->master);
1025 } else {
1026 struct nf_conn *ct;
1027 int err;
1028
1029 err = __ovs_ct_lookup(net, key, info, skb);
1030 if (err)
1031 return err;
1032
1033 ct = (struct nf_conn *)skb_nfct(skb);
1034 if (ct)
1035 nf_ct_deliver_cached_events(ct);
1036 }
1037
1038 return 0;
1039}
1040
1041static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
1042{
1043 size_t i;
1044
1045 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
1046 if (labels->ct_labels_32[i])
1047 return true;
1048
1049 return false;
1050}
1051
1052/* Lookup connection and confirm if unconfirmed. */
1053static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
1054 const struct ovs_conntrack_info *info,
1055 struct sk_buff *skb)
1056{
1057 enum ip_conntrack_info ctinfo;
1058 struct nf_conn *ct;
1059 int err;
1060
1061 err = __ovs_ct_lookup(net, key, info, skb);
1062 if (err)
1063 return err;
1064
1065 /* The connection could be invalid, in which case this is a no-op.*/
1066 ct = nf_ct_get(skb, &ctinfo);
1067 if (!ct)
1068 return 0;
1069
1070 /* Set the conntrack event mask if given. NEW and DELETE events have
1071 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1072 * typically would receive many kinds of updates. Setting the event
1073 * mask allows those events to be filtered. The set event mask will
1074 * remain in effect for the lifetime of the connection unless changed
1075 * by a further CT action with both the commit flag and the eventmask
1076 * option. */
1077 if (info->have_eventmask) {
1078 struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
1079
1080 if (cache)
1081 cache->ctmask = info->eventmask;
1082 }
1083
1084 /* Apply changes before confirming the connection so that the initial
1085 * conntrack NEW netlink event carries the values given in the CT
1086 * action.
1087 */
1088 if (info->mark.mask) {
1089 err = ovs_ct_set_mark(ct, key, info->mark.value,
1090 info->mark.mask);
1091 if (err)
1092 return err;
1093 }
1094 if (!nf_ct_is_confirmed(ct)) {
1095 err = ovs_ct_init_labels(ct, key, &info->labels.value,
1096 &info->labels.mask);
1097 if (err)
1098 return err;
1099 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1100 labels_nonzero(&info->labels.mask)) {
1101 err = ovs_ct_set_labels(ct, key, &info->labels.value,
1102 &info->labels.mask);
1103 if (err)
1104 return err;
1105 }
1106 /* This will take care of sending queued events even if the connection
1107 * is already confirmed.
1108 */
1109 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1110 return -EINVAL;
1111
1112 return 0;
1113}
1114
1115/* Trim the skb to the length specified by the IP/IPv6 header,
1116 * removing any trailing lower-layer padding. This prepares the skb
1117 * for higher-layer processing that assumes skb->len excludes padding
1118 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1119 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1120 */
1121static int ovs_skb_network_trim(struct sk_buff *skb)
1122{
1123 unsigned int len;
1124 int err;
1125
1126 switch (skb->protocol) {
1127 case htons(ETH_P_IP):
1128 len = ntohs(ip_hdr(skb)->tot_len);
1129 break;
1130 case htons(ETH_P_IPV6):
1131 len = sizeof(struct ipv6hdr)
1132 + ntohs(ipv6_hdr(skb)->payload_len);
1133 break;
1134 default:
1135 len = skb->len;
1136 }
1137
1138 err = pskb_trim_rcsum(skb, len);
1139 if (err)
1140 kfree_skb(skb);
1141
1142 return err;
1143}
1144
1145/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1146 * value if 'skb' is freed.
1147 */
1148int ovs_ct_execute(struct net *net, struct sk_buff *skb,
1149 struct sw_flow_key *key,
1150 const struct ovs_conntrack_info *info)
1151{
1152 int nh_ofs;
1153 int err;
1154
1155 /* The conntrack module expects to be working at L3. */
1156 nh_ofs = skb_network_offset(skb);
1157 skb_pull_rcsum(skb, nh_ofs);
1158
1159 err = ovs_skb_network_trim(skb);
1160 if (err)
1161 return err;
1162
1163 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
1164 err = handle_fragments(net, key, info->zone.id, skb);
1165 if (err)
1166 return err;
1167 }
1168
1169 if (info->commit)
1170 err = ovs_ct_commit(net, key, info, skb);
1171 else
1172 err = ovs_ct_lookup(net, key, info, skb);
1173
1174 skb_push(skb, nh_ofs);
1175 skb_postpush_rcsum(skb, skb->data, nh_ofs);
1176 if (err)
1177 kfree_skb(skb);
1178 return err;
1179}
1180
1181static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
1182 const struct sw_flow_key *key, bool log)
1183{
1184 struct nf_conntrack_helper *helper;
1185 struct nf_conn_help *help;
1186
1187 helper = nf_conntrack_helper_try_module_get(name, info->family,
1188 key->ip.proto);
1189 if (!helper) {
1190 OVS_NLERR(log, "Unknown helper \"%s\"", name);
1191 return -EINVAL;
1192 }
1193
1194 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
1195 if (!help) {
1196 nf_conntrack_helper_put(helper);
1197 return -ENOMEM;
1198 }
1199
1200 rcu_assign_pointer(help->helper, helper);
1201 info->helper = helper;
1202 return 0;
1203}
1204
1205#ifdef CONFIG_NF_NAT_NEEDED
1206static int parse_nat(const struct nlattr *attr,
1207 struct ovs_conntrack_info *info, bool log)
1208{
1209 struct nlattr *a;
1210 int rem;
1211 bool have_ip_max = false;
1212 bool have_proto_max = false;
1213 bool ip_vers = (info->family == NFPROTO_IPV6);
1214
1215 nla_for_each_nested(a, attr, rem) {
1216 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1217 [OVS_NAT_ATTR_SRC] = {0, 0},
1218 [OVS_NAT_ATTR_DST] = {0, 0},
1219 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1220 sizeof(struct in6_addr)},
1221 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1222 sizeof(struct in6_addr)},
1223 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1224 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1225 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1226 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1227 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1228 };
1229 int type = nla_type(a);
1230
1231 if (type > OVS_NAT_ATTR_MAX) {
1232 OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
1233 type, OVS_NAT_ATTR_MAX);
1234 return -EINVAL;
1235 }
1236
1237 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1238 OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
1239 type, nla_len(a),
1240 ovs_nat_attr_lens[type][ip_vers]);
1241 return -EINVAL;
1242 }
1243
1244 switch (type) {
1245 case OVS_NAT_ATTR_SRC:
1246 case OVS_NAT_ATTR_DST:
1247 if (info->nat) {
1248 OVS_NLERR(log, "Only one type of NAT may be specified");
1249 return -ERANGE;
1250 }
1251 info->nat |= OVS_CT_NAT;
1252 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1253 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1254 break;
1255
1256 case OVS_NAT_ATTR_IP_MIN:
1257 nla_memcpy(&info->range.min_addr, a,
1258 sizeof(info->range.min_addr));
1259 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1260 break;
1261
1262 case OVS_NAT_ATTR_IP_MAX:
1263 have_ip_max = true;
1264 nla_memcpy(&info->range.max_addr, a,
1265 sizeof(info->range.max_addr));
1266 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1267 break;
1268
1269 case OVS_NAT_ATTR_PROTO_MIN:
1270 info->range.min_proto.all = htons(nla_get_u16(a));
1271 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1272 break;
1273
1274 case OVS_NAT_ATTR_PROTO_MAX:
1275 have_proto_max = true;
1276 info->range.max_proto.all = htons(nla_get_u16(a));
1277 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1278 break;
1279
1280 case OVS_NAT_ATTR_PERSISTENT:
1281 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1282 break;
1283
1284 case OVS_NAT_ATTR_PROTO_HASH:
1285 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1286 break;
1287
1288 case OVS_NAT_ATTR_PROTO_RANDOM:
1289 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1290 break;
1291
1292 default:
1293 OVS_NLERR(log, "Unknown nat attribute (%d)", type);
1294 return -EINVAL;
1295 }
1296 }
1297
1298 if (rem > 0) {
1299 OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
1300 return -EINVAL;
1301 }
1302 if (!info->nat) {
1303 /* Do not allow flags if no type is given. */
1304 if (info->range.flags) {
1305 OVS_NLERR(log,
1306 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1307 );
1308 return -EINVAL;
1309 }
1310 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1311 } else if (!info->commit) {
1312 OVS_NLERR(log,
1313 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1314 );
1315 return -EINVAL;
1316 }
1317 /* Allow missing IP_MAX. */
1318 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1319 memcpy(&info->range.max_addr, &info->range.min_addr,
1320 sizeof(info->range.max_addr));
1321 }
1322 /* Allow missing PROTO_MAX. */
1323 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1324 !have_proto_max) {
1325 info->range.max_proto.all = info->range.min_proto.all;
1326 }
1327 return 0;
1328}
1329#endif
1330
1331static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
1332 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
1333 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
1334 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1335 .maxlen = sizeof(u16) },
1336 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1337 .maxlen = sizeof(struct md_mark) },
1338 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1339 .maxlen = sizeof(struct md_labels) },
1340 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
1341 .maxlen = NF_CT_HELPER_NAME_LEN },
1342#ifdef CONFIG_NF_NAT_NEEDED
1343 /* NAT length is checked when parsing the nested attributes. */
1344 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1345#endif
1346 [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
1347 .maxlen = sizeof(u32) },
1348};
1349
1350static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
1351 const char **helper, bool log)
1352{
1353 struct nlattr *a;
1354 int rem;
1355
1356 nla_for_each_nested(a, attr, rem) {
1357 int type = nla_type(a);
1358 int maxlen;
1359 int minlen;
1360
1361 if (type > OVS_CT_ATTR_MAX) {
1362 OVS_NLERR(log,
1363 "Unknown conntrack attr (type=%d, max=%d)",
1364 type, OVS_CT_ATTR_MAX);
1365 return -EINVAL;
1366 }
1367
1368 maxlen = ovs_ct_attr_lens[type].maxlen;
1369 minlen = ovs_ct_attr_lens[type].minlen;
1370 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1371 OVS_NLERR(log,
1372 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1373 type, nla_len(a), maxlen);
1374 return -EINVAL;
1375 }
1376
1377 switch (type) {
1378 case OVS_CT_ATTR_FORCE_COMMIT:
1379 info->force = true;
1380 /* fall through. */
1381 case OVS_CT_ATTR_COMMIT:
1382 info->commit = true;
1383 break;
1384#ifdef CONFIG_NF_CONNTRACK_ZONES
1385 case OVS_CT_ATTR_ZONE:
1386 info->zone.id = nla_get_u16(a);
1387 break;
1388#endif
1389#ifdef CONFIG_NF_CONNTRACK_MARK
1390 case OVS_CT_ATTR_MARK: {
1391 struct md_mark *mark = nla_data(a);
1392
1393 if (!mark->mask) {
1394 OVS_NLERR(log, "ct_mark mask cannot be 0");
1395 return -EINVAL;
1396 }
1397 info->mark = *mark;
1398 break;
1399 }
1400#endif
1401#ifdef CONFIG_NF_CONNTRACK_LABELS
1402 case OVS_CT_ATTR_LABELS: {
1403 struct md_labels *labels = nla_data(a);
1404
1405 if (!labels_nonzero(&labels->mask)) {
1406 OVS_NLERR(log, "ct_labels mask cannot be 0");
1407 return -EINVAL;
1408 }
1409 info->labels = *labels;
1410 break;
1411 }
1412#endif
1413 case OVS_CT_ATTR_HELPER:
1414 *helper = nla_data(a);
1415 if (!memchr(*helper, '\0', nla_len(a))) {
1416 OVS_NLERR(log, "Invalid conntrack helper");
1417 return -EINVAL;
1418 }
1419 break;
1420#ifdef CONFIG_NF_NAT_NEEDED
1421 case OVS_CT_ATTR_NAT: {
1422 int err = parse_nat(a, info, log);
1423
1424 if (err)
1425 return err;
1426 break;
1427 }
1428#endif
1429 case OVS_CT_ATTR_EVENTMASK:
1430 info->have_eventmask = true;
1431 info->eventmask = nla_get_u32(a);
1432 break;
1433
1434 default:
1435 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1436 type);
1437 return -EINVAL;
1438 }
1439 }
1440
1441#ifdef CONFIG_NF_CONNTRACK_MARK
1442 if (!info->commit && info->mark.mask) {
1443 OVS_NLERR(log,
1444 "Setting conntrack mark requires 'commit' flag.");
1445 return -EINVAL;
1446 }
1447#endif
1448#ifdef CONFIG_NF_CONNTRACK_LABELS
1449 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1450 OVS_NLERR(log,
1451 "Setting conntrack labels requires 'commit' flag.");
1452 return -EINVAL;
1453 }
1454#endif
1455 if (rem > 0) {
1456 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1457 return -EINVAL;
1458 }
1459
1460 return 0;
1461}
1462
1463bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
1464{
1465 if (attr == OVS_KEY_ATTR_CT_STATE)
1466 return true;
1467 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1468 attr == OVS_KEY_ATTR_CT_ZONE)
1469 return true;
1470 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1471 attr == OVS_KEY_ATTR_CT_MARK)
1472 return true;
1473 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1474 attr == OVS_KEY_ATTR_CT_LABELS) {
1475 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1476
1477 return ovs_net->xt_label;
1478 }
1479
1480 return false;
1481}
1482
1483int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1484 const struct sw_flow_key *key,
1485 struct sw_flow_actions **sfa, bool log)
1486{
1487 struct ovs_conntrack_info ct_info;
1488 const char *helper = NULL;
1489 u16 family;
1490 int err;
1491
1492 family = key_to_nfproto(key);
1493 if (family == NFPROTO_UNSPEC) {
1494 OVS_NLERR(log, "ct family unspecified");
1495 return -EINVAL;
1496 }
1497
1498 memset(&ct_info, 0, sizeof(ct_info));
1499 ct_info.family = family;
1500
1501 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1502 NF_CT_DEFAULT_ZONE_DIR, 0);
1503
1504 err = parse_ct(attr, &ct_info, &helper, log);
1505 if (err)
1506 return err;
1507
1508 /* Set up template for tracking connections in specific zones. */
1509 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1510 if (!ct_info.ct) {
1511 OVS_NLERR(log, "Failed to allocate conntrack template");
1512 return -ENOMEM;
1513 }
1514
1515 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1516 nf_conntrack_get(&ct_info.ct->ct_general);
1517
1518 if (helper) {
1519 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1520 if (err)
1521 goto err_free_ct;
1522 }
1523
1524 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1525 sizeof(ct_info), log);
1526 if (err)
1527 goto err_free_ct;
1528
1529 return 0;
1530err_free_ct:
1531 __ovs_ct_free_action(&ct_info);
1532 return err;
1533}
1534
1535#ifdef CONFIG_NF_NAT_NEEDED
1536static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1537 struct sk_buff *skb)
1538{
1539 struct nlattr *start;
1540
1541 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1542 if (!start)
1543 return false;
1544
1545 if (info->nat & OVS_CT_SRC_NAT) {
1546 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1547 return false;
1548 } else if (info->nat & OVS_CT_DST_NAT) {
1549 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1550 return false;
1551 } else {
1552 goto out;
1553 }
1554
1555 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
1556 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1557 info->family == NFPROTO_IPV4) {
1558 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1559 info->range.min_addr.ip) ||
1560 (info->range.max_addr.ip
1561 != info->range.min_addr.ip &&
1562 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1563 info->range.max_addr.ip))))
1564 return false;
1565 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1566 info->family == NFPROTO_IPV6) {
1567 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1568 &info->range.min_addr.in6) ||
1569 (memcmp(&info->range.max_addr.in6,
1570 &info->range.min_addr.in6,
1571 sizeof(info->range.max_addr.in6)) &&
1572 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1573 &info->range.max_addr.in6))))
1574 return false;
1575 } else {
1576 return false;
1577 }
1578 }
1579 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1580 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1581 ntohs(info->range.min_proto.all)) ||
1582 (info->range.max_proto.all != info->range.min_proto.all &&
1583 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1584 ntohs(info->range.max_proto.all)))))
1585 return false;
1586
1587 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1588 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1589 return false;
1590 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1591 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1592 return false;
1593 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1594 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1595 return false;
1596out:
1597 nla_nest_end(skb, start);
1598
1599 return true;
1600}
1601#endif
1602
1603int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1604 struct sk_buff *skb)
1605{
1606 struct nlattr *start;
1607
1608 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1609 if (!start)
1610 return -EMSGSIZE;
1611
1612 if (ct_info->commit && nla_put_flag(skb, ct_info->force
1613 ? OVS_CT_ATTR_FORCE_COMMIT
1614 : OVS_CT_ATTR_COMMIT))
1615 return -EMSGSIZE;
1616 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1617 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1618 return -EMSGSIZE;
1619 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
1620 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1621 &ct_info->mark))
1622 return -EMSGSIZE;
1623 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1624 labels_nonzero(&ct_info->labels.mask) &&
1625 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1626 &ct_info->labels))
1627 return -EMSGSIZE;
1628 if (ct_info->helper) {
1629 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1630 ct_info->helper->name))
1631 return -EMSGSIZE;
1632 }
1633 if (ct_info->have_eventmask &&
1634 nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
1635 return -EMSGSIZE;
1636
1637#ifdef CONFIG_NF_NAT_NEEDED
1638 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1639 return -EMSGSIZE;
1640#endif
1641 nla_nest_end(skb, start);
1642
1643 return 0;
1644}
1645
1646void ovs_ct_free_action(const struct nlattr *a)
1647{
1648 struct ovs_conntrack_info *ct_info = nla_data(a);
1649
1650 __ovs_ct_free_action(ct_info);
1651}
1652
1653static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1654{
1655 if (ct_info->helper)
1656 nf_conntrack_helper_put(ct_info->helper);
1657 if (ct_info->ct)
1658 nf_ct_tmpl_free(ct_info->ct);
1659}
1660
1661void ovs_ct_init(struct net *net)
1662{
1663 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
1664 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1665
1666 if (nf_connlabels_get(net, n_bits - 1)) {
1667 ovs_net->xt_label = false;
1668 OVS_NLERR(true, "Failed to set connlabel length");
1669 } else {
1670 ovs_net->xt_label = true;
1671 }
1672}
1673
1674void ovs_ct_exit(struct net *net)
1675{
1676 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1677
1678 if (ovs_net->xt_label)
1679 nf_connlabels_put(net);
1680}