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