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