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