blob: bfd7606c8be1fe01cd2542d36564ee6f7339defb [file] [log] [blame]
Joe Stringer7f8a4362015-08-26 11:31:48 -07001/*
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>
Jarno Rajahalme05752522016-03-10 10:54:23 -080016#include <linux/tcp.h>
17#include <linux/udp.h>
18#include <linux/sctp.h>
Joe Stringer7f8a4362015-08-26 11:31:48 -070019#include <net/ip.h>
20#include <net/netfilter/nf_conntrack_core.h>
Joe Stringercae3a262015-08-26 11:31:53 -070021#include <net/netfilter/nf_conntrack_helper.h>
Joe Stringerc2ac6672015-08-26 11:31:52 -070022#include <net/netfilter/nf_conntrack_labels.h>
Jarno Rajahalme05752522016-03-10 10:54:23 -080023#include <net/netfilter/nf_conntrack_seqadj.h>
Joe Stringer7f8a4362015-08-26 11:31:48 -070024#include <net/netfilter/nf_conntrack_zones.h>
25#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
26
Jarno Rajahalme05752522016-03-10 10:54:23 -080027#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
Joe Stringer7f8a4362015-08-26 11:31:48 -070033#include "datapath.h"
34#include "conntrack.h"
35#include "flow.h"
36#include "flow_netlink.h"
37
38struct ovs_ct_len_tbl {
Jarno Rajahalme05752522016-03-10 10:54:23 -080039 int maxlen;
40 int minlen;
Joe Stringer7f8a4362015-08-26 11:31:48 -070041};
42
Joe Stringer182e3042015-08-26 11:31:49 -070043/* Metadata mark for masked write to conntrack mark */
44struct md_mark {
45 u32 value;
46 u32 mask;
47};
48
Joe Stringerc2ac6672015-08-26 11:31:52 -070049/* Metadata label for masked write to conntrack label. */
Joe Stringer33db4122015-10-01 15:00:37 -070050struct md_labels {
51 struct ovs_key_ct_labels value;
52 struct ovs_key_ct_labels mask;
Joe Stringerc2ac6672015-08-26 11:31:52 -070053};
54
Jarno Rajahalme05752522016-03-10 10:54:23 -080055enum 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
Joe Stringer7f8a4362015-08-26 11:31:48 -070061/* Conntrack action context for execution. */
62struct ovs_conntrack_info {
Joe Stringercae3a262015-08-26 11:31:53 -070063 struct nf_conntrack_helper *helper;
Joe Stringer7f8a4362015-08-26 11:31:48 -070064 struct nf_conntrack_zone zone;
65 struct nf_conn *ct;
Joe Stringerab38a7b2015-10-06 11:00:01 -070066 u8 commit : 1;
Jarno Rajahalme05752522016-03-10 10:54:23 -080067 u8 nat : 3; /* enum ovs_ct_nat */
Joe Stringer7f8a4362015-08-26 11:31:48 -070068 u16 family;
Joe Stringer182e3042015-08-26 11:31:49 -070069 struct md_mark mark;
Joe Stringer33db4122015-10-01 15:00:37 -070070 struct md_labels labels;
Jarno Rajahalme05752522016-03-10 10:54:23 -080071#ifdef CONFIG_NF_NAT_NEEDED
72 struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */
73#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -070074};
75
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -080076static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
77
Joe Stringer2f3ab9f2015-12-09 14:07:39 -080078static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
79
Joe Stringer7f8a4362015-08-26 11:31:48 -070080static 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:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700100 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:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700116 ct_state |= OVS_CS_F_NEW;
117 break;
118 default:
119 break;
120 }
121
122 return ct_state;
123}
124
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700125static 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
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800134/* 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
Joe Stringer33db4122015-10-01 15:00:37 -0700139static void ovs_ct_get_labels(const struct nf_conn *ct,
140 struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700141{
142 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
143
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800144 if (cl)
145 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
146 else
Joe Stringer33db4122015-10-01 15:00:37 -0700147 memset(labels, 0, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700148}
149
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800150static 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
Joe Stringer7f8a4362015-08-26 11:31:48 -0700164static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
Joe Stringer182e3042015-08-26 11:31:49 -0700165 const struct nf_conntrack_zone *zone,
166 const struct nf_conn *ct)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700167{
168 key->ct.state = state;
169 key->ct.zone = zone->id;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700170 key->ct.mark = ovs_ct_get_mark(ct);
Joe Stringer33db4122015-10-01 15:00:37 -0700171 ovs_ct_get_labels(ct, &key->ct.labels);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800172
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;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700201}
202
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800203/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
Jarno Rajahalme05752522016-03-10 10:54:23 -0800204 * 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.
Joe Stringer7f8a4362015-08-26 11:31:48 -0700207 */
208static void ovs_ct_update_key(const struct sk_buff *skb,
Joe Stringerd1109862015-12-09 14:07:40 -0800209 const struct ovs_conntrack_info *info,
Jarno Rajahalme05752522016-03-10 10:54:23 -0800210 struct sw_flow_key *key, bool post_ct,
211 bool keep_nat_flags)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700212{
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);
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800221 /* All unconfirmed entries are NEW connections. */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700222 if (!nf_ct_is_confirmed(ct))
223 state |= OVS_CS_F_NEW;
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800224 /* OVS persists the related flag for the duration of the
225 * connection.
226 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700227 if (ct->master)
228 state |= OVS_CS_F_RELATED;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800229 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 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700237 zone = nf_ct_zone(ct);
238 } else if (post_ct) {
239 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
Joe Stringerd1109862015-12-09 14:07:40 -0800240 if (info)
241 zone = &info->zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700242 }
Joe Stringer182e3042015-08-26 11:31:49 -0700243 __ovs_ct_update_key(key, state, zone, ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700244}
245
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800246/* This is called to initialize CT key fields possibly coming in from the local
247 * stack.
248 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700249void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
250{
Jarno Rajahalme05752522016-03-10 10:54:23 -0800251 ovs_ct_update_key(skb, NULL, key, false, false);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700252}
253
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800254#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)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700260{
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800261 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct.state))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700262 return -EMSGSIZE;
263
264 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800265 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct.zone))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700266 return -EMSGSIZE;
267
Joe Stringer182e3042015-08-26 11:31:49 -0700268 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800269 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
Joe Stringer182e3042015-08-26 11:31:49 -0700270 return -EMSGSIZE;
271
Valentin Rothberg9723e6a2015-08-28 10:39:56 +0200272 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800273 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
274 &output->ct.labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700275 return -EMSGSIZE;
276
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800277 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
Joe Stringer182e3042015-08-26 11:31:49 -0700303 return 0;
304}
305
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800306static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
Joe Stringer182e3042015-08-26 11:31:49 -0700307 u32 ct_mark, u32 mask)
308{
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700309#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
Joe Stringer182e3042015-08-26 11:31:49 -0700310 u32 new_mark;
311
Joe Stringer182e3042015-08-26 11:31:49 -0700312 new_mark = ct_mark | (ct->mark & ~(mask));
313 if (ct->mark != new_mark) {
314 ct->mark = new_mark;
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800315 if (nf_ct_is_confirmed(ct))
316 nf_conntrack_event_cache(IPCT_MARK, ct);
Joe Stringer182e3042015-08-26 11:31:49 -0700317 key->ct.mark = new_mark;
318 }
319
Joe Stringer7f8a4362015-08-26 11:31:48 -0700320 return 0;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700321#else
322 return -ENOTSUPP;
323#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -0700324}
325
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800326static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700327{
Joe Stringerc2ac6672015-08-26 11:31:52 -0700328 struct nf_conn_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700329
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 }
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800335
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{
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -0800349 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. */
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800357
358 cl = ovs_ct_get_conn_labels(ct);
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800359 if (!cl)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700360 return -ENOSPC;
361
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -0800362 /* 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 }
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800375
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800376 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700377
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800378 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);
399
Joe Stringerc2ac6672015-08-26 11:31:52 -0700400 return 0;
401}
402
Joe Stringercae3a262015-08-26 11:31:53 -0700403/* '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;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800411 int err;
Joe Stringercae3a262015-08-26 11:31:53 -0700412
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;
Joe Stringercc570602015-09-14 11:14:50 -0700432 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700433
Joe Stringercc570602015-09-14 11:14:50 -0700434 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
435 &frag_off);
436 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700437 pr_debug("proto header not found\n");
438 return NF_ACCEPT;
439 }
Joe Stringercc570602015-09-14 11:14:50 -0700440 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700441 break;
442 }
443 default:
444 WARN_ONCE(1, "helper invoked on non-IP family!");
445 return NF_DROP;
446 }
447
Jarno Rajahalme05752522016-03-10 10:54:23 -0800448 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;
Joe Stringercae3a262015-08-26 11:31:53 -0700460}
461
Joe Stringer74c16612015-10-25 20:21:48 -0700462/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
463 * value if 'skb' is freed.
464 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700465static 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);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100469 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700470
471 if (key->eth.type == htons(ETH_P_IP)) {
472 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700473
474 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500475 err = ip_defrag(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700476 if (err)
477 return err;
478
479 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700480#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700481 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700482 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700483
Joe Stringer49e261a2016-04-18 14:51:47 -0700484 skb_orphan(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700485 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100486 err = nf_ct_frag6_gather(net, skb, user);
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800487 if (err) {
488 if (err != -EINPROGRESS)
489 kfree_skb(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100490 return err;
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800491 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700492
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100493 key->ip.proto = ipv6_hdr(skb)->nexthdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700494 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700495#endif
496 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700497 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700498 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
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500515 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700516 return NULL;
517 return __nf_ct_expect_find(net, zone, &tuple);
518}
519
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800520/* 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
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800538 * skb->_nfct lost due to an upcall to be recovered during actions execution.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800539 *
540 * Must be called with rcu_read_lock.
541 *
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800542 * On success, populates skb->_nfct and returns the connection. Returns NULL
543 * if there is no existing entry.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800544 */
545static struct nf_conn *
546ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800547 u8 l3num, struct sk_buff *skb, bool natted)
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800548{
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;
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800553 struct nf_conn *ct;
554 unsigned int dataoff;
555 u8 protonum;
556
557 l3proto = __nf_ct_l3proto_find(l3num);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800558 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);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800564 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
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800570 /* 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
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800581 /* 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
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800588 /* 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
Florian Westphalc74454f2017-01-23 18:21:57 +0100595 nf_ct_set(skb, ct, ovs_ct_get_info(h));
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800596 return ct;
597}
598
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800599/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800600static 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)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700604{
605 enum ip_conntrack_info ctinfo;
606 struct nf_conn *ct;
607
608 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800609 /* If no ct, check if we have evidence that an existing conntrack entry
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800610 * might be found for this skb. This happens when we lose a skb->_nfct
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800611 * 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)
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800617 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
618 !!(key->ct.state
619 & OVS_CS_F_NAT_MASK));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700620 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;
Joe Stringercae3a262015-08-26 11:31:53 -0700626 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 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700633
634 return true;
635}
636
Jarno Rajahalme05752522016-03-10 10:54:23 -0800637#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);
Lance Richardson75f01a42017-01-12 19:33:18 -0500650 skb_pull_rcsum(skb, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800651
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:
Arnd Bergmann99b72482016-03-18 14:33:45 +0100661 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
662 skb->protocol == htons(ETH_P_IP) &&
Jarno Rajahalme05752522016-03-10 10:54:23 -0800663 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;
Arnd Bergmann99b72482016-03-18 14:33:45 +0100668 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
669 skb->protocol == htons(ETH_P_IPV6)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800670 __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 }
Jarno Rajahalme05752522016-03-10 10:54:23 -0800684 }
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);
Lance Richardson75f01a42017-01-12 19:33:18 -0500715 skb_postpush_rcsum(skb, skb->data, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800716
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.
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700791 * Make sure new expected connections (IP_CT_RELATED) are NATted only
792 * when committing.
Jarno Rajahalme05752522016-03-10 10:54:23 -0800793 */
794 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
795 ct->status & IPS_NAT_MASK &&
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700796 (ctinfo != IP_CT_RELATED || info->commit)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800797 /* 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
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800833/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800834 * not done already. Update key with new CT state after passing the packet
835 * through conntrack.
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800836 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800837 * set to NULL and 0 will be returned.
838 */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700839static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700840 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 */
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800848 bool cached = skb_nfct_cached(net, key, info, skb);
849 enum ip_conntrack_info ctinfo;
850 struct nf_conn *ct;
851
852 if (!cached) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700853 struct nf_conn *tmpl = info->ct;
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800854 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700855
856 /* Associate skb with specified zone. */
857 if (tmpl) {
Florian Westphalcb9c6832017-01-23 18:21:56 +0100858 if (skb_nfct(skb))
859 nf_conntrack_put(skb_nfct(skb));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700860 nf_conntrack_get(&tmpl->ct_general);
Florian Westphalc74454f2017-01-23 18:21:57 +0100861 nf_ct_set(skb, tmpl, IP_CT_NEW);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700862 }
863
Pablo Neira Ayuso08733a02016-11-03 10:56:43 +0100864 err = nf_conntrack_in(net, info->family,
865 NF_INET_PRE_ROUTING, skb);
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800866 if (err != NF_ACCEPT)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700867 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700868
Jarno Rajahalme05752522016-03-10 10:54:23 -0800869 /* 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);
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800877 }
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800878
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800879 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800880 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
Joe Stringer16ec3d42016-05-11 10:29:26 -0700897 /* 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
Jarno Rajahalme05752522016-03-10 10:54:23 -0800910 /* 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 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700919 }
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
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800931 /* 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 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700938 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
939 if (exp) {
940 u8 state;
941
Jarno Rajahalme05752522016-03-10 10:54:23 -0800942 /* NOTE: New connections are NATted and Helped only when
943 * committed, so we are not calling into NAT here.
944 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700945 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700946 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200947 } 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
Florian Westphalcb9c6832017-01-23 18:21:56 +0100955 ct = (struct nf_conn *)skb_nfct(skb);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200956 if (ct)
957 nf_ct_deliver_cached_events(ct);
958 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700959
960 return 0;
961}
962
Joe Stringer33db4122015-10-01 15:00:37 -0700963static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700964{
965 size_t i;
966
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800967 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
968 if (labels->ct_labels_32[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700969 return true;
970
971 return false;
972}
973
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700974/* 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{
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800979 enum ip_conntrack_info ctinfo;
980 struct nf_conn *ct;
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700981 int err;
982
983 err = __ovs_ct_lookup(net, key, info, skb);
984 if (err)
985 return err;
986
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800987 /* 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
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700992 /* 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) {
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800997 err = ovs_ct_set_mark(ct, key, info->mark.value,
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700998 info->mark.mask);
999 if (err)
1000 return err;
1001 }
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -08001002 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);
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001010 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
Joe Stringer74c16612015-10-25 20:21:48 -07001022/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1023 * value if 'skb' is freed.
1024 */
Joe Stringer7f8a4362015-08-26 11:31:48 -07001025int 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);
Lance Richardson75f01a42017-01-12 19:33:18 -05001034 skb_pull_rcsum(skb, nh_ofs);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001035
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
Joe Stringerab38a7b2015-10-06 11:00:01 -07001042 if (info->commit)
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001043 err = ovs_ct_commit(net, key, info, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001044 else
1045 err = ovs_ct_lookup(net, key, info, skb);
1046
1047 skb_push(skb, nh_ofs);
Lance Richardson75f01a42017-01-12 19:33:18 -05001048 skb_postpush_rcsum(skb, skb->data, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -07001049 if (err)
1050 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001051 return err;
1052}
1053
Joe Stringercae3a262015-08-26 11:31:53 -07001054static 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
Jarno Rajahalme05752522016-03-10 10:54:23 -08001078#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:
Haishuang Yanac71b462016-03-28 18:08:59 +08001134 nla_memcpy(&info->range.min_addr, a,
1135 sizeof(info->range.min_addr));
Jarno Rajahalme05752522016-03-10 10:54:23 -08001136 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
Joe Stringer7f8a4362015-08-26 11:31:48 -07001208static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001209 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -07001210 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1211 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -07001212 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1213 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -07001214 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1215 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -07001216 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
Jarno Rajahalme05752522016-03-10 10:54:23 -08001217 .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
Joe Stringer7f8a4362015-08-26 11:31:48 -07001222};
1223
1224static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -07001225 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001226{
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) {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001249 case OVS_CT_ATTR_COMMIT:
1250 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001251 break;
1252#ifdef CONFIG_NF_CONNTRACK_ZONES
1253 case OVS_CT_ATTR_ZONE:
1254 info->zone.id = nla_get_u16(a);
1255 break;
1256#endif
Joe Stringer182e3042015-08-26 11:31:49 -07001257#ifdef CONFIG_NF_CONNTRACK_MARK
1258 case OVS_CT_ATTR_MARK: {
1259 struct md_mark *mark = nla_data(a);
1260
Joe Stringere754ec62015-10-19 19:19:00 -07001261 if (!mark->mask) {
1262 OVS_NLERR(log, "ct_mark mask cannot be 0");
1263 return -EINVAL;
1264 }
Joe Stringer182e3042015-08-26 11:31:49 -07001265 info->mark = *mark;
1266 break;
1267 }
1268#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -07001269#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -07001270 case OVS_CT_ATTR_LABELS: {
1271 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001272
Joe Stringere754ec62015-10-19 19:19:00 -07001273 if (!labels_nonzero(&labels->mask)) {
1274 OVS_NLERR(log, "ct_labels mask cannot be 0");
1275 return -EINVAL;
1276 }
Joe Stringer33db4122015-10-01 15:00:37 -07001277 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001278 break;
1279 }
1280#endif
Joe Stringercae3a262015-08-26 11:31:53 -07001281 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;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001288#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
Joe Stringer7f8a4362015-08-26 11:31:48 -07001297 default:
1298 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1299 type);
1300 return -EINVAL;
1301 }
1302 }
1303
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001304#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
Joe Stringer7f8a4362015-08-26 11:31:48 -07001318 if (rem > 0) {
1319 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1320 return -EINVAL;
1321 }
1322
1323 return 0;
1324}
1325
Joe Stringerc2ac6672015-08-26 11:31:52 -07001326bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001327{
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;
Joe Stringer182e3042015-08-26 11:31:49 -07001333 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1334 attr == OVS_KEY_ATTR_CT_MARK)
1335 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001336 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001337 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001338 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1339
1340 return ovs_net->xt_label;
1341 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001342
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;
Joe Stringercae3a262015-08-26 11:31:53 -07001351 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001352 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
Joe Stringercae3a262015-08-26 11:31:53 -07001367 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001368 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 }
Joe Stringer90c7afc962015-12-23 14:39:27 -08001377
1378 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1379 nf_conntrack_get(&ct_info.ct->ct_general);
1380
Joe Stringercae3a262015-08-26 11:31:53 -07001381 if (helper) {
1382 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1383 if (err)
1384 goto err_free_ct;
1385 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001386
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
Joe Stringer7f8a4362015-08-26 11:31:48 -07001392 return 0;
1393err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001394 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001395 return err;
1396}
1397
Jarno Rajahalme05752522016-03-10 10:54:23 -08001398#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) {
Arnd Bergmann99b72482016-03-18 14:33:45 +01001419 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1420 info->family == NFPROTO_IPV4) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001421 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;
Arnd Bergmann99b72482016-03-18 14:33:45 +01001428 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1429 info->family == NFPROTO_IPV6) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001430 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;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001438 } 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
Joe Stringer7f8a4362015-08-26 11:31:48 -07001466int 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
Joe Stringerab38a7b2015-10-06 11:00:01 -07001475 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001476 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;
Joe Stringere754ec62015-10-19 19:19:00 -07001480 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -07001481 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1482 &ct_info->mark))
1483 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001484 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -07001485 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001486 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1487 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -07001488 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -07001489 if (ct_info->helper) {
1490 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1491 ct_info->helper->name))
1492 return -EMSGSIZE;
1493 }
Jarno Rajahalme05752522016-03-10 10:54:23 -08001494#ifdef CONFIG_NF_NAT_NEEDED
1495 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1496 return -EMSGSIZE;
1497#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001498 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
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001507 __ovs_ct_free_action(ct_info);
1508}
1509
1510static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1511{
Joe Stringercae3a262015-08-26 11:31:53 -07001512 if (ct_info->helper)
1513 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001514 if (ct_info->ct)
Joe Stringer76644232016-09-01 18:01:47 -07001515 nf_ct_tmpl_free(ct_info->ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001516}
Joe Stringerc2ac6672015-08-26 11:31:52 -07001517
1518void ovs_ct_init(struct net *net)
1519{
Joe Stringer33db4122015-10-01 15:00:37 -07001520 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001521 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1522
Florian Westphaladff6c62016-04-12 18:14:25 +02001523 if (nf_connlabels_get(net, n_bits - 1)) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001524 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}