blob: c92a5795dcdad6c243424a43dac465ab76bc579f [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 */
Jarno Rajahalmedd41d332017-02-09 11:22:00 -080068 u8 force : 1;
Joe Stringer7f8a4362015-08-26 11:31:48 -070069 u16 family;
Joe Stringer182e3042015-08-26 11:31:49 -070070 struct md_mark mark;
Joe Stringer33db4122015-10-01 15:00:37 -070071 struct md_labels labels;
Jarno Rajahalme05752522016-03-10 10:54:23 -080072#ifdef CONFIG_NF_NAT_NEEDED
73 struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */
74#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -070075};
76
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -080077static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
78
Joe Stringer2f3ab9f2015-12-09 14:07:39 -080079static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
80
Joe Stringer7f8a4362015-08-26 11:31:48 -070081static u16 key_to_nfproto(const struct sw_flow_key *key)
82{
83 switch (ntohs(key->eth.type)) {
84 case ETH_P_IP:
85 return NFPROTO_IPV4;
86 case ETH_P_IPV6:
87 return NFPROTO_IPV6;
88 default:
89 return NFPROTO_UNSPEC;
90 }
91}
92
93/* Map SKB connection state into the values used by flow definition. */
94static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
95{
96 u8 ct_state = OVS_CS_F_TRACKED;
97
98 switch (ctinfo) {
99 case IP_CT_ESTABLISHED_REPLY:
100 case IP_CT_RELATED_REPLY:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700101 ct_state |= OVS_CS_F_REPLY_DIR;
102 break;
103 default:
104 break;
105 }
106
107 switch (ctinfo) {
108 case IP_CT_ESTABLISHED:
109 case IP_CT_ESTABLISHED_REPLY:
110 ct_state |= OVS_CS_F_ESTABLISHED;
111 break;
112 case IP_CT_RELATED:
113 case IP_CT_RELATED_REPLY:
114 ct_state |= OVS_CS_F_RELATED;
115 break;
116 case IP_CT_NEW:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700117 ct_state |= OVS_CS_F_NEW;
118 break;
119 default:
120 break;
121 }
122
123 return ct_state;
124}
125
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700126static u32 ovs_ct_get_mark(const struct nf_conn *ct)
127{
128#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
129 return ct ? ct->mark : 0;
130#else
131 return 0;
132#endif
133}
134
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800135/* Guard against conntrack labels max size shrinking below 128 bits. */
136#if NF_CT_LABELS_MAX_SIZE < 16
137#error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
138#endif
139
Joe Stringer33db4122015-10-01 15:00:37 -0700140static void ovs_ct_get_labels(const struct nf_conn *ct,
141 struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700142{
143 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
144
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800145 if (cl)
146 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
147 else
Joe Stringer33db4122015-10-01 15:00:37 -0700148 memset(labels, 0, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700149}
150
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800151static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
152 const struct nf_conntrack_tuple *orig,
153 u8 icmp_proto)
154{
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800155 key->ct_orig_proto = orig->dst.protonum;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800156 if (orig->dst.protonum == icmp_proto) {
157 key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
158 key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
159 } else {
160 key->ct.orig_tp.src = orig->src.u.all;
161 key->ct.orig_tp.dst = orig->dst.u.all;
162 }
163}
164
Joe Stringer7f8a4362015-08-26 11:31:48 -0700165static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
Joe Stringer182e3042015-08-26 11:31:49 -0700166 const struct nf_conntrack_zone *zone,
167 const struct nf_conn *ct)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700168{
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800169 key->ct_state = state;
170 key->ct_zone = zone->id;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700171 key->ct.mark = ovs_ct_get_mark(ct);
Joe Stringer33db4122015-10-01 15:00:37 -0700172 ovs_ct_get_labels(ct, &key->ct.labels);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800173
174 if (ct) {
175 const struct nf_conntrack_tuple *orig;
176
177 /* Use the master if we have one. */
178 if (ct->master)
179 ct = ct->master;
180 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
181
182 /* IP version must match with the master connection. */
183 if (key->eth.type == htons(ETH_P_IP) &&
184 nf_ct_l3num(ct) == NFPROTO_IPV4) {
185 key->ipv4.ct_orig.src = orig->src.u3.ip;
186 key->ipv4.ct_orig.dst = orig->dst.u3.ip;
187 __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
188 return;
189 } else if (key->eth.type == htons(ETH_P_IPV6) &&
190 !sw_flow_key_is_nd(key) &&
191 nf_ct_l3num(ct) == NFPROTO_IPV6) {
192 key->ipv6.ct_orig.src = orig->src.u3.in6;
193 key->ipv6.ct_orig.dst = orig->dst.u3.in6;
194 __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
195 return;
196 }
197 }
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800198 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800199 * original direction key fields.
200 */
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800201 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700202}
203
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800204/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
Jarno Rajahalme05752522016-03-10 10:54:23 -0800205 * previously sent the packet to conntrack via the ct action. If
206 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
207 * initialized from the connection status.
Joe Stringer7f8a4362015-08-26 11:31:48 -0700208 */
209static void ovs_ct_update_key(const struct sk_buff *skb,
Joe Stringerd1109862015-12-09 14:07:40 -0800210 const struct ovs_conntrack_info *info,
Jarno Rajahalme05752522016-03-10 10:54:23 -0800211 struct sw_flow_key *key, bool post_ct,
212 bool keep_nat_flags)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700213{
214 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
215 enum ip_conntrack_info ctinfo;
216 struct nf_conn *ct;
217 u8 state = 0;
218
219 ct = nf_ct_get(skb, &ctinfo);
220 if (ct) {
221 state = ovs_ct_get_state(ctinfo);
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800222 /* All unconfirmed entries are NEW connections. */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700223 if (!nf_ct_is_confirmed(ct))
224 state |= OVS_CS_F_NEW;
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800225 /* OVS persists the related flag for the duration of the
226 * connection.
227 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700228 if (ct->master)
229 state |= OVS_CS_F_RELATED;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800230 if (keep_nat_flags) {
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800231 state |= key->ct_state & OVS_CS_F_NAT_MASK;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800232 } else {
233 if (ct->status & IPS_SRC_NAT)
234 state |= OVS_CS_F_SRC_NAT;
235 if (ct->status & IPS_DST_NAT)
236 state |= OVS_CS_F_DST_NAT;
237 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700238 zone = nf_ct_zone(ct);
239 } else if (post_ct) {
240 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
Joe Stringerd1109862015-12-09 14:07:40 -0800241 if (info)
242 zone = &info->zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700243 }
Joe Stringer182e3042015-08-26 11:31:49 -0700244 __ovs_ct_update_key(key, state, zone, ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700245}
246
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800247/* This is called to initialize CT key fields possibly coming in from the local
248 * stack.
249 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700250void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
251{
Jarno Rajahalme05752522016-03-10 10:54:23 -0800252 ovs_ct_update_key(skb, NULL, key, false, false);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700253}
254
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800255#define IN6_ADDR_INITIALIZER(ADDR) \
256 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
257 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
258
259int ovs_ct_put_key(const struct sw_flow_key *swkey,
260 const struct sw_flow_key *output, struct sk_buff *skb)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700261{
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800262 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700263 return -EMSGSIZE;
264
265 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800266 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700267 return -EMSGSIZE;
268
Joe Stringer182e3042015-08-26 11:31:49 -0700269 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800270 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
Joe Stringer182e3042015-08-26 11:31:49 -0700271 return -EMSGSIZE;
272
Valentin Rothberg9723e6a2015-08-28 10:39:56 +0200273 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800274 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
275 &output->ct.labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700276 return -EMSGSIZE;
277
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800278 if (swkey->ct_orig_proto) {
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800279 if (swkey->eth.type == htons(ETH_P_IP)) {
280 struct ovs_key_ct_tuple_ipv4 orig = {
281 output->ipv4.ct_orig.src,
282 output->ipv4.ct_orig.dst,
283 output->ct.orig_tp.src,
284 output->ct.orig_tp.dst,
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800285 output->ct_orig_proto,
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800286 };
287 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
288 sizeof(orig), &orig))
289 return -EMSGSIZE;
290 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
291 struct ovs_key_ct_tuple_ipv6 orig = {
292 IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.src),
293 IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.dst),
294 output->ct.orig_tp.src,
295 output->ct.orig_tp.dst,
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800296 output->ct_orig_proto,
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800297 };
298 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
299 sizeof(orig), &orig))
300 return -EMSGSIZE;
301 }
302 }
303
Joe Stringer182e3042015-08-26 11:31:49 -0700304 return 0;
305}
306
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800307static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
Joe Stringer182e3042015-08-26 11:31:49 -0700308 u32 ct_mark, u32 mask)
309{
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700310#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
Joe Stringer182e3042015-08-26 11:31:49 -0700311 u32 new_mark;
312
Joe Stringer182e3042015-08-26 11:31:49 -0700313 new_mark = ct_mark | (ct->mark & ~(mask));
314 if (ct->mark != new_mark) {
315 ct->mark = new_mark;
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800316 if (nf_ct_is_confirmed(ct))
317 nf_conntrack_event_cache(IPCT_MARK, ct);
Joe Stringer182e3042015-08-26 11:31:49 -0700318 key->ct.mark = new_mark;
319 }
320
Joe Stringer7f8a4362015-08-26 11:31:48 -0700321 return 0;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700322#else
323 return -ENOTSUPP;
324#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -0700325}
326
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800327static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700328{
Joe Stringerc2ac6672015-08-26 11:31:52 -0700329 struct nf_conn_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700330
331 cl = nf_ct_labels_find(ct);
332 if (!cl) {
333 nf_ct_labels_ext_add(ct);
334 cl = nf_ct_labels_find(ct);
335 }
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800336
337 return cl;
338}
339
340/* Initialize labels for a new, yet to be committed conntrack entry. Note that
341 * since the new connection is not yet confirmed, and thus no-one else has
Jarno Rajahalme2317c6b2017-02-17 18:11:58 -0800342 * access to it's labels, we simply write them over.
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800343 */
344static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
345 const struct ovs_key_ct_labels *labels,
346 const struct ovs_key_ct_labels *mask)
347{
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -0800348 struct nf_conn_labels *cl, *master_cl;
349 bool have_mask = labels_nonzero(mask);
350
351 /* Inherit master's labels to the related connection? */
352 master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
353
354 if (!master_cl && !have_mask)
355 return 0; /* Nothing to do. */
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800356
357 cl = ovs_ct_get_conn_labels(ct);
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800358 if (!cl)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700359 return -ENOSPC;
360
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -0800361 /* Inherit the master's labels, if any. */
362 if (master_cl)
363 *cl = *master_cl;
364
365 if (have_mask) {
366 u32 *dst = (u32 *)cl->bits;
367 int i;
368
369 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
370 dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
371 (labels->ct_labels_32[i]
372 & mask->ct_labels_32[i]);
373 }
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800374
Jarno Rajahalme2317c6b2017-02-17 18:11:58 -0800375 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
376 * IPCT_LABEL bit it set in the event cache.
377 */
378 nf_conntrack_event_cache(IPCT_LABEL, ct);
379
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800380 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700381
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800382 return 0;
383}
384
385static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
386 const struct ovs_key_ct_labels *labels,
387 const struct ovs_key_ct_labels *mask)
388{
389 struct nf_conn_labels *cl;
390 int err;
391
392 cl = ovs_ct_get_conn_labels(ct);
393 if (!cl)
394 return -ENOSPC;
395
396 err = nf_connlabels_replace(ct, labels->ct_labels_32,
397 mask->ct_labels_32,
398 OVS_CT_LABELS_LEN_32);
399 if (err)
400 return err;
401
402 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
403
Joe Stringerc2ac6672015-08-26 11:31:52 -0700404 return 0;
405}
406
Joe Stringercae3a262015-08-26 11:31:53 -0700407/* 'skb' should already be pulled to nh_ofs. */
408static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
409{
410 const struct nf_conntrack_helper *helper;
411 const struct nf_conn_help *help;
412 enum ip_conntrack_info ctinfo;
413 unsigned int protoff;
414 struct nf_conn *ct;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800415 int err;
Joe Stringercae3a262015-08-26 11:31:53 -0700416
417 ct = nf_ct_get(skb, &ctinfo);
418 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
419 return NF_ACCEPT;
420
421 help = nfct_help(ct);
422 if (!help)
423 return NF_ACCEPT;
424
425 helper = rcu_dereference(help->helper);
426 if (!helper)
427 return NF_ACCEPT;
428
429 switch (proto) {
430 case NFPROTO_IPV4:
431 protoff = ip_hdrlen(skb);
432 break;
433 case NFPROTO_IPV6: {
434 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
435 __be16 frag_off;
Joe Stringercc570602015-09-14 11:14:50 -0700436 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700437
Joe Stringercc570602015-09-14 11:14:50 -0700438 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
439 &frag_off);
440 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700441 pr_debug("proto header not found\n");
442 return NF_ACCEPT;
443 }
Joe Stringercc570602015-09-14 11:14:50 -0700444 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700445 break;
446 }
447 default:
448 WARN_ONCE(1, "helper invoked on non-IP family!");
449 return NF_DROP;
450 }
451
Jarno Rajahalme05752522016-03-10 10:54:23 -0800452 err = helper->help(skb, protoff, ct, ctinfo);
453 if (err != NF_ACCEPT)
454 return err;
455
456 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
457 * FTP with NAT) adusting the TCP payload size when mangling IP
458 * addresses and/or port numbers in the text-based control connection.
459 */
460 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
461 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
462 return NF_DROP;
463 return NF_ACCEPT;
Joe Stringercae3a262015-08-26 11:31:53 -0700464}
465
Joe Stringer74c16612015-10-25 20:21:48 -0700466/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
467 * value if 'skb' is freed.
468 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700469static int handle_fragments(struct net *net, struct sw_flow_key *key,
470 u16 zone, struct sk_buff *skb)
471{
472 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100473 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700474
475 if (key->eth.type == htons(ETH_P_IP)) {
476 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700477
478 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500479 err = ip_defrag(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700480 if (err)
481 return err;
482
483 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700484#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700485 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700486 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700487
488 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100489 err = nf_ct_frag6_gather(net, skb, user);
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800490 if (err) {
491 if (err != -EINPROGRESS)
492 kfree_skb(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100493 return err;
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800494 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700495
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100496 key->ip.proto = ipv6_hdr(skb)->nexthdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700497 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700498#endif
499 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700500 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700501 return -EPFNOSUPPORT;
502 }
503
504 key->ip.frag = OVS_FRAG_TYPE_NONE;
505 skb_clear_hash(skb);
506 skb->ignore_df = 1;
507 *OVS_CB(skb) = ovs_cb;
508
509 return 0;
510}
511
512static struct nf_conntrack_expect *
513ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
514 u16 proto, const struct sk_buff *skb)
515{
516 struct nf_conntrack_tuple tuple;
Jarno Rajahalmecf5d7092017-04-14 14:26:38 -0700517 struct nf_conntrack_expect *exp;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700518
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500519 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700520 return NULL;
Jarno Rajahalmecf5d7092017-04-14 14:26:38 -0700521
522 exp = __nf_ct_expect_find(net, zone, &tuple);
523 if (exp) {
524 struct nf_conntrack_tuple_hash *h;
525
526 /* Delete existing conntrack entry, if it clashes with the
527 * expectation. This can happen since conntrack ALGs do not
528 * check for clashes between (new) expectations and existing
529 * conntrack entries. nf_conntrack_in() will check the
530 * expectations only if a conntrack entry can not be found,
531 * which can lead to OVS finding the expectation (here) in the
532 * init direction, but which will not be removed by the
533 * nf_conntrack_in() call, if a matching conntrack entry is
534 * found instead. In this case all init direction packets
535 * would be reported as new related packets, while reply
536 * direction packets would be reported as un-related
537 * established packets.
538 */
539 h = nf_conntrack_find_get(net, zone, &tuple);
540 if (h) {
541 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
542
543 nf_ct_delete(ct, 0, 0);
544 nf_conntrack_put(&ct->ct_general);
545 }
546 }
547
548 return exp;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700549}
550
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800551/* This replicates logic from nf_conntrack_core.c that is not exported. */
552static enum ip_conntrack_info
553ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
554{
555 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
556
557 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
558 return IP_CT_ESTABLISHED_REPLY;
559 /* Once we've had two way comms, always ESTABLISHED. */
560 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
561 return IP_CT_ESTABLISHED;
562 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
563 return IP_CT_RELATED;
564 return IP_CT_NEW;
565}
566
567/* Find an existing connection which this packet belongs to without
568 * re-attributing statistics or modifying the connection state. This allows an
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800569 * skb->_nfct lost due to an upcall to be recovered during actions execution.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800570 *
571 * Must be called with rcu_read_lock.
572 *
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800573 * On success, populates skb->_nfct and returns the connection. Returns NULL
574 * if there is no existing entry.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800575 */
576static struct nf_conn *
577ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800578 u8 l3num, struct sk_buff *skb, bool natted)
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800579{
580 struct nf_conntrack_l3proto *l3proto;
581 struct nf_conntrack_l4proto *l4proto;
582 struct nf_conntrack_tuple tuple;
583 struct nf_conntrack_tuple_hash *h;
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800584 struct nf_conn *ct;
585 unsigned int dataoff;
586 u8 protonum;
587
588 l3proto = __nf_ct_l3proto_find(l3num);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800589 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
590 &protonum) <= 0) {
591 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
592 return NULL;
593 }
594 l4proto = __nf_ct_l4proto_find(l3num, protonum);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800595 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
596 protonum, net, &tuple, l3proto, l4proto)) {
597 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
598 return NULL;
599 }
600
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800601 /* Must invert the tuple if skb has been transformed by NAT. */
602 if (natted) {
603 struct nf_conntrack_tuple inverse;
604
605 if (!nf_ct_invert_tuple(&inverse, &tuple, l3proto, l4proto)) {
606 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
607 return NULL;
608 }
609 tuple = inverse;
610 }
611
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800612 /* look for tuple match */
613 h = nf_conntrack_find_get(net, zone, &tuple);
614 if (!h)
615 return NULL; /* Not found. */
616
617 ct = nf_ct_tuplehash_to_ctrack(h);
618
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800619 /* Inverted packet tuple matches the reverse direction conntrack tuple,
620 * select the other tuplehash to get the right 'ctinfo' bits for this
621 * packet.
622 */
623 if (natted)
624 h = &ct->tuplehash[!h->tuple.dst.dir];
625
Florian Westphalc74454f2017-01-23 18:21:57 +0100626 nf_ct_set(skb, ct, ovs_ct_get_info(h));
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800627 return ct;
628}
629
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800630/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800631static bool skb_nfct_cached(struct net *net,
632 const struct sw_flow_key *key,
633 const struct ovs_conntrack_info *info,
634 struct sk_buff *skb)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700635{
636 enum ip_conntrack_info ctinfo;
637 struct nf_conn *ct;
638
639 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800640 /* If no ct, check if we have evidence that an existing conntrack entry
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800641 * might be found for this skb. This happens when we lose a skb->_nfct
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800642 * due to an upcall. If the connection was not confirmed, it is not
643 * cached and needs to be run through conntrack again.
644 */
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800645 if (!ct && key->ct_state & OVS_CS_F_TRACKED &&
646 !(key->ct_state & OVS_CS_F_INVALID) &&
647 key->ct_zone == info->zone.id) {
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800648 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800649 !!(key->ct_state
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800650 & OVS_CS_F_NAT_MASK));
Jarno Rajahalmedd41d332017-02-09 11:22:00 -0800651 if (ct)
652 nf_ct_get(skb, &ctinfo);
653 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700654 if (!ct)
655 return false;
656 if (!net_eq(net, read_pnet(&ct->ct_net)))
657 return false;
658 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
659 return false;
Joe Stringercae3a262015-08-26 11:31:53 -0700660 if (info->helper) {
661 struct nf_conn_help *help;
662
663 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
664 if (help && rcu_access_pointer(help->helper) != info->helper)
665 return false;
666 }
Jarno Rajahalmedd41d332017-02-09 11:22:00 -0800667 /* Force conntrack entry direction to the current packet? */
668 if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
669 /* Delete the conntrack entry if confirmed, else just release
670 * the reference.
671 */
672 if (nf_ct_is_confirmed(ct))
673 nf_ct_delete(ct, 0, 0);
Jarno Rajahalmeb768b162017-03-28 11:25:26 -0700674
675 nf_conntrack_put(&ct->ct_general);
Jarno Rajahalmedd41d332017-02-09 11:22:00 -0800676 nf_ct_set(skb, NULL, 0);
677 return false;
678 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700679
680 return true;
681}
682
Jarno Rajahalme05752522016-03-10 10:54:23 -0800683#ifdef CONFIG_NF_NAT_NEEDED
684/* Modelled after nf_nat_ipv[46]_fn().
685 * range is only used for new, uninitialized NAT state.
686 * Returns either NF_ACCEPT or NF_DROP.
687 */
688static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
689 enum ip_conntrack_info ctinfo,
690 const struct nf_nat_range *range,
691 enum nf_nat_manip_type maniptype)
692{
693 int hooknum, nh_off, err = NF_ACCEPT;
694
695 nh_off = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -0500696 skb_pull_rcsum(skb, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800697
698 /* See HOOK2MANIP(). */
699 if (maniptype == NF_NAT_MANIP_SRC)
700 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
701 else
702 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
703
704 switch (ctinfo) {
705 case IP_CT_RELATED:
706 case IP_CT_RELATED_REPLY:
Arnd Bergmann99b72482016-03-18 14:33:45 +0100707 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
708 skb->protocol == htons(ETH_P_IP) &&
Jarno Rajahalme05752522016-03-10 10:54:23 -0800709 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
710 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
711 hooknum))
712 err = NF_DROP;
713 goto push;
Arnd Bergmann99b72482016-03-18 14:33:45 +0100714 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
715 skb->protocol == htons(ETH_P_IPV6)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800716 __be16 frag_off;
717 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
718 int hdrlen = ipv6_skip_exthdr(skb,
719 sizeof(struct ipv6hdr),
720 &nexthdr, &frag_off);
721
722 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
723 if (!nf_nat_icmpv6_reply_translation(skb, ct,
724 ctinfo,
725 hooknum,
726 hdrlen))
727 err = NF_DROP;
728 goto push;
729 }
Jarno Rajahalme05752522016-03-10 10:54:23 -0800730 }
731 /* Non-ICMP, fall thru to initialize if needed. */
732 case IP_CT_NEW:
733 /* Seen it before? This can happen for loopback, retrans,
734 * or local packets.
735 */
736 if (!nf_nat_initialized(ct, maniptype)) {
737 /* Initialize according to the NAT action. */
738 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
739 /* Action is set up to establish a new
740 * mapping.
741 */
742 ? nf_nat_setup_info(ct, range, maniptype)
743 : nf_nat_alloc_null_binding(ct, hooknum);
744 if (err != NF_ACCEPT)
745 goto push;
746 }
747 break;
748
749 case IP_CT_ESTABLISHED:
750 case IP_CT_ESTABLISHED_REPLY:
751 break;
752
753 default:
754 err = NF_DROP;
755 goto push;
756 }
757
758 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
759push:
760 skb_push(skb, nh_off);
Lance Richardson75f01a42017-01-12 19:33:18 -0500761 skb_postpush_rcsum(skb, skb->data, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800762
763 return err;
764}
765
766static void ovs_nat_update_key(struct sw_flow_key *key,
767 const struct sk_buff *skb,
768 enum nf_nat_manip_type maniptype)
769{
770 if (maniptype == NF_NAT_MANIP_SRC) {
771 __be16 src;
772
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800773 key->ct_state |= OVS_CS_F_SRC_NAT;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800774 if (key->eth.type == htons(ETH_P_IP))
775 key->ipv4.addr.src = ip_hdr(skb)->saddr;
776 else if (key->eth.type == htons(ETH_P_IPV6))
777 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
778 sizeof(key->ipv6.addr.src));
779 else
780 return;
781
782 if (key->ip.proto == IPPROTO_UDP)
783 src = udp_hdr(skb)->source;
784 else if (key->ip.proto == IPPROTO_TCP)
785 src = tcp_hdr(skb)->source;
786 else if (key->ip.proto == IPPROTO_SCTP)
787 src = sctp_hdr(skb)->source;
788 else
789 return;
790
791 key->tp.src = src;
792 } else {
793 __be16 dst;
794
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800795 key->ct_state |= OVS_CS_F_DST_NAT;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800796 if (key->eth.type == htons(ETH_P_IP))
797 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
798 else if (key->eth.type == htons(ETH_P_IPV6))
799 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
800 sizeof(key->ipv6.addr.dst));
801 else
802 return;
803
804 if (key->ip.proto == IPPROTO_UDP)
805 dst = udp_hdr(skb)->dest;
806 else if (key->ip.proto == IPPROTO_TCP)
807 dst = tcp_hdr(skb)->dest;
808 else if (key->ip.proto == IPPROTO_SCTP)
809 dst = sctp_hdr(skb)->dest;
810 else
811 return;
812
813 key->tp.dst = dst;
814 }
815}
816
817/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
818static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
819 const struct ovs_conntrack_info *info,
820 struct sk_buff *skb, struct nf_conn *ct,
821 enum ip_conntrack_info ctinfo)
822{
823 enum nf_nat_manip_type maniptype;
824 int err;
825
826 if (nf_ct_is_untracked(ct)) {
827 /* A NAT action may only be performed on tracked packets. */
828 return NF_ACCEPT;
829 }
830
831 /* Add NAT extension if not confirmed yet. */
832 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
833 return NF_ACCEPT; /* Can't NAT. */
834
835 /* Determine NAT type.
836 * Check if the NAT type can be deduced from the tracked connection.
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700837 * Make sure new expected connections (IP_CT_RELATED) are NATted only
838 * when committing.
Jarno Rajahalme05752522016-03-10 10:54:23 -0800839 */
840 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
841 ct->status & IPS_NAT_MASK &&
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700842 (ctinfo != IP_CT_RELATED || info->commit)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800843 /* NAT an established or related connection like before. */
844 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
845 /* This is the REPLY direction for a connection
846 * for which NAT was applied in the forward
847 * direction. Do the reverse NAT.
848 */
849 maniptype = ct->status & IPS_SRC_NAT
850 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
851 else
852 maniptype = ct->status & IPS_SRC_NAT
853 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
854 } else if (info->nat & OVS_CT_SRC_NAT) {
855 maniptype = NF_NAT_MANIP_SRC;
856 } else if (info->nat & OVS_CT_DST_NAT) {
857 maniptype = NF_NAT_MANIP_DST;
858 } else {
859 return NF_ACCEPT; /* Connection is not NATed. */
860 }
861 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
862
863 /* Mark NAT done if successful and update the flow key. */
864 if (err == NF_ACCEPT)
865 ovs_nat_update_key(key, skb, maniptype);
866
867 return err;
868}
869#else /* !CONFIG_NF_NAT_NEEDED */
870static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
871 const struct ovs_conntrack_info *info,
872 struct sk_buff *skb, struct nf_conn *ct,
873 enum ip_conntrack_info ctinfo)
874{
875 return NF_ACCEPT;
876}
877#endif
878
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800879/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800880 * not done already. Update key with new CT state after passing the packet
881 * through conntrack.
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800882 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800883 * set to NULL and 0 will be returned.
884 */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700885static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700886 const struct ovs_conntrack_info *info,
887 struct sk_buff *skb)
888{
889 /* If we are recirculating packets to match on conntrack fields and
890 * committing with a separate conntrack action, then we don't need to
891 * actually run the packet through conntrack twice unless it's for a
892 * different zone.
893 */
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800894 bool cached = skb_nfct_cached(net, key, info, skb);
895 enum ip_conntrack_info ctinfo;
896 struct nf_conn *ct;
897
898 if (!cached) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700899 struct nf_conn *tmpl = info->ct;
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800900 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700901
902 /* Associate skb with specified zone. */
903 if (tmpl) {
Florian Westphalcb9c6832017-01-23 18:21:56 +0100904 if (skb_nfct(skb))
905 nf_conntrack_put(skb_nfct(skb));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700906 nf_conntrack_get(&tmpl->ct_general);
Florian Westphalc74454f2017-01-23 18:21:57 +0100907 nf_ct_set(skb, tmpl, IP_CT_NEW);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700908 }
909
Pablo Neira Ayuso08733a02016-11-03 10:56:43 +0100910 err = nf_conntrack_in(net, info->family,
911 NF_INET_PRE_ROUTING, skb);
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800912 if (err != NF_ACCEPT)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700913 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700914
Jarno Rajahalme05752522016-03-10 10:54:23 -0800915 /* Clear CT state NAT flags to mark that we have not yet done
916 * NAT after the nf_conntrack_in() call. We can actually clear
917 * the whole state, as it will be re-initialized below.
918 */
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800919 key->ct_state = 0;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800920
921 /* Update the key, but keep the NAT flags. */
922 ovs_ct_update_key(skb, info, key, true, true);
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800923 }
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800924
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800925 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800926 if (ct) {
927 /* Packets starting a new connection must be NATted before the
928 * helper, so that the helper knows about the NAT. We enforce
929 * this by delaying both NAT and helper calls for unconfirmed
930 * connections until the committing CT action. For later
931 * packets NAT and Helper may be called in either order.
932 *
933 * NAT will be done only if the CT action has NAT, and only
934 * once per packet (per zone), as guarded by the NAT bits in
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800935 * the key->ct_state.
Jarno Rajahalme05752522016-03-10 10:54:23 -0800936 */
Jarno Rajahalme316d4d72017-02-09 11:22:01 -0800937 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
Jarno Rajahalme05752522016-03-10 10:54:23 -0800938 (nf_ct_is_confirmed(ct) || info->commit) &&
939 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
940 return -EINVAL;
941 }
942
Joe Stringer16ec3d42016-05-11 10:29:26 -0700943 /* Userspace may decide to perform a ct lookup without a helper
944 * specified followed by a (recirculate and) commit with one.
945 * Therefore, for unconfirmed connections which we will commit,
946 * we need to attach the helper here.
947 */
948 if (!nf_ct_is_confirmed(ct) && info->commit &&
949 info->helper && !nfct_help(ct)) {
950 int err = __nf_ct_try_assign_helper(ct, info->ct,
951 GFP_ATOMIC);
952 if (err)
953 return err;
954 }
955
Jarno Rajahalme05752522016-03-10 10:54:23 -0800956 /* Call the helper only if:
957 * - nf_conntrack_in() was executed above ("!cached") for a
958 * confirmed connection, or
959 * - When committing an unconfirmed connection.
960 */
961 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
962 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
963 return -EINVAL;
964 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700965 }
966
967 return 0;
968}
969
970/* Lookup connection and read fields into key. */
971static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
972 const struct ovs_conntrack_info *info,
973 struct sk_buff *skb)
974{
975 struct nf_conntrack_expect *exp;
976
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800977 /* If we pass an expected packet through nf_conntrack_in() the
978 * expectation is typically removed, but the packet could still be
979 * lost in upcall processing. To prevent this from happening we
980 * perform an explicit expectation lookup. Expected connections are
981 * always new, and will be passed through conntrack only when they are
982 * committed, as it is OK to remove the expectation at that time.
983 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700984 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
985 if (exp) {
986 u8 state;
987
Jarno Rajahalme05752522016-03-10 10:54:23 -0800988 /* NOTE: New connections are NATted and Helped only when
989 * committed, so we are not calling into NAT here.
990 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700991 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700992 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200993 } else {
994 struct nf_conn *ct;
995 int err;
996
997 err = __ovs_ct_lookup(net, key, info, skb);
998 if (err)
999 return err;
1000
Florian Westphalcb9c6832017-01-23 18:21:56 +01001001 ct = (struct nf_conn *)skb_nfct(skb);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +02001002 if (ct)
1003 nf_ct_deliver_cached_events(ct);
1004 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001005
1006 return 0;
1007}
1008
Joe Stringer33db4122015-10-01 15:00:37 -07001009static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -07001010{
1011 size_t i;
1012
Jarno Rajahalmecb80d582017-02-09 11:21:55 -08001013 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
1014 if (labels->ct_labels_32[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -07001015 return true;
1016
1017 return false;
1018}
1019
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001020/* Lookup connection and confirm if unconfirmed. */
1021static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
1022 const struct ovs_conntrack_info *info,
1023 struct sk_buff *skb)
1024{
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -08001025 enum ip_conntrack_info ctinfo;
1026 struct nf_conn *ct;
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001027 int err;
1028
1029 err = __ovs_ct_lookup(net, key, info, skb);
1030 if (err)
1031 return err;
1032
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -08001033 /* The connection could be invalid, in which case this is a no-op.*/
1034 ct = nf_ct_get(skb, &ctinfo);
1035 if (!ct)
1036 return 0;
1037
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001038 /* Apply changes before confirming the connection so that the initial
1039 * conntrack NEW netlink event carries the values given in the CT
1040 * action.
1041 */
1042 if (info->mark.mask) {
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -08001043 err = ovs_ct_set_mark(ct, key, info->mark.value,
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001044 info->mark.mask);
1045 if (err)
1046 return err;
1047 }
Jarno Rajahalme09aa98a2017-02-09 11:21:58 -08001048 if (!nf_ct_is_confirmed(ct)) {
1049 err = ovs_ct_init_labels(ct, key, &info->labels.value,
1050 &info->labels.mask);
1051 if (err)
1052 return err;
1053 } else if (labels_nonzero(&info->labels.mask)) {
1054 err = ovs_ct_set_labels(ct, key, &info->labels.value,
1055 &info->labels.mask);
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001056 if (err)
1057 return err;
1058 }
1059 /* This will take care of sending queued events even if the connection
1060 * is already confirmed.
1061 */
1062 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1063 return -EINVAL;
1064
1065 return 0;
1066}
1067
Joe Stringer74c16612015-10-25 20:21:48 -07001068/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1069 * value if 'skb' is freed.
1070 */
Joe Stringer7f8a4362015-08-26 11:31:48 -07001071int ovs_ct_execute(struct net *net, struct sk_buff *skb,
1072 struct sw_flow_key *key,
1073 const struct ovs_conntrack_info *info)
1074{
1075 int nh_ofs;
1076 int err;
1077
1078 /* The conntrack module expects to be working at L3. */
1079 nh_ofs = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -05001080 skb_pull_rcsum(skb, nh_ofs);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001081
1082 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
1083 err = handle_fragments(net, key, info->zone.id, skb);
1084 if (err)
1085 return err;
1086 }
1087
Joe Stringerab38a7b2015-10-06 11:00:01 -07001088 if (info->commit)
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001089 err = ovs_ct_commit(net, key, info, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001090 else
1091 err = ovs_ct_lookup(net, key, info, skb);
1092
1093 skb_push(skb, nh_ofs);
Lance Richardson75f01a42017-01-12 19:33:18 -05001094 skb_postpush_rcsum(skb, skb->data, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -07001095 if (err)
1096 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001097 return err;
1098}
1099
Joe Stringercae3a262015-08-26 11:31:53 -07001100static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
1101 const struct sw_flow_key *key, bool log)
1102{
1103 struct nf_conntrack_helper *helper;
1104 struct nf_conn_help *help;
1105
1106 helper = nf_conntrack_helper_try_module_get(name, info->family,
1107 key->ip.proto);
1108 if (!helper) {
1109 OVS_NLERR(log, "Unknown helper \"%s\"", name);
1110 return -EINVAL;
1111 }
1112
1113 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
1114 if (!help) {
1115 module_put(helper->me);
1116 return -ENOMEM;
1117 }
1118
1119 rcu_assign_pointer(help->helper, helper);
1120 info->helper = helper;
1121 return 0;
1122}
1123
Jarno Rajahalme05752522016-03-10 10:54:23 -08001124#ifdef CONFIG_NF_NAT_NEEDED
1125static int parse_nat(const struct nlattr *attr,
1126 struct ovs_conntrack_info *info, bool log)
1127{
1128 struct nlattr *a;
1129 int rem;
1130 bool have_ip_max = false;
1131 bool have_proto_max = false;
1132 bool ip_vers = (info->family == NFPROTO_IPV6);
1133
1134 nla_for_each_nested(a, attr, rem) {
1135 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1136 [OVS_NAT_ATTR_SRC] = {0, 0},
1137 [OVS_NAT_ATTR_DST] = {0, 0},
1138 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1139 sizeof(struct in6_addr)},
1140 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1141 sizeof(struct in6_addr)},
1142 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1143 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1144 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1145 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1146 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1147 };
1148 int type = nla_type(a);
1149
1150 if (type > OVS_NAT_ATTR_MAX) {
1151 OVS_NLERR(log,
1152 "Unknown NAT attribute (type=%d, max=%d).\n",
1153 type, OVS_NAT_ATTR_MAX);
1154 return -EINVAL;
1155 }
1156
1157 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1158 OVS_NLERR(log,
1159 "NAT attribute type %d has unexpected length (%d != %d).\n",
1160 type, nla_len(a),
1161 ovs_nat_attr_lens[type][ip_vers]);
1162 return -EINVAL;
1163 }
1164
1165 switch (type) {
1166 case OVS_NAT_ATTR_SRC:
1167 case OVS_NAT_ATTR_DST:
1168 if (info->nat) {
1169 OVS_NLERR(log,
1170 "Only one type of NAT may be specified.\n"
1171 );
1172 return -ERANGE;
1173 }
1174 info->nat |= OVS_CT_NAT;
1175 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1176 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1177 break;
1178
1179 case OVS_NAT_ATTR_IP_MIN:
Haishuang Yanac71b462016-03-28 18:08:59 +08001180 nla_memcpy(&info->range.min_addr, a,
1181 sizeof(info->range.min_addr));
Jarno Rajahalme05752522016-03-10 10:54:23 -08001182 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1183 break;
1184
1185 case OVS_NAT_ATTR_IP_MAX:
1186 have_ip_max = true;
1187 nla_memcpy(&info->range.max_addr, a,
1188 sizeof(info->range.max_addr));
1189 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1190 break;
1191
1192 case OVS_NAT_ATTR_PROTO_MIN:
1193 info->range.min_proto.all = htons(nla_get_u16(a));
1194 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1195 break;
1196
1197 case OVS_NAT_ATTR_PROTO_MAX:
1198 have_proto_max = true;
1199 info->range.max_proto.all = htons(nla_get_u16(a));
1200 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1201 break;
1202
1203 case OVS_NAT_ATTR_PERSISTENT:
1204 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1205 break;
1206
1207 case OVS_NAT_ATTR_PROTO_HASH:
1208 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1209 break;
1210
1211 case OVS_NAT_ATTR_PROTO_RANDOM:
1212 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1213 break;
1214
1215 default:
1216 OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
1217 return -EINVAL;
1218 }
1219 }
1220
1221 if (rem > 0) {
1222 OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
1223 return -EINVAL;
1224 }
1225 if (!info->nat) {
1226 /* Do not allow flags if no type is given. */
1227 if (info->range.flags) {
1228 OVS_NLERR(log,
1229 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1230 );
1231 return -EINVAL;
1232 }
1233 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1234 } else if (!info->commit) {
1235 OVS_NLERR(log,
1236 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1237 );
1238 return -EINVAL;
1239 }
1240 /* Allow missing IP_MAX. */
1241 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1242 memcpy(&info->range.max_addr, &info->range.min_addr,
1243 sizeof(info->range.max_addr));
1244 }
1245 /* Allow missing PROTO_MAX. */
1246 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1247 !have_proto_max) {
1248 info->range.max_proto.all = info->range.min_proto.all;
1249 }
1250 return 0;
1251}
1252#endif
1253
Joe Stringer7f8a4362015-08-26 11:31:48 -07001254static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001255 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Jarno Rajahalmedd41d332017-02-09 11:22:00 -08001256 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -07001257 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1258 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -07001259 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1260 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -07001261 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1262 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -07001263 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
Jarno Rajahalme05752522016-03-10 10:54:23 -08001264 .maxlen = NF_CT_HELPER_NAME_LEN },
1265#ifdef CONFIG_NF_NAT_NEEDED
1266 /* NAT length is checked when parsing the nested attributes. */
1267 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1268#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001269};
1270
1271static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -07001272 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001273{
1274 struct nlattr *a;
1275 int rem;
1276
1277 nla_for_each_nested(a, attr, rem) {
1278 int type = nla_type(a);
1279 int maxlen = ovs_ct_attr_lens[type].maxlen;
1280 int minlen = ovs_ct_attr_lens[type].minlen;
1281
1282 if (type > OVS_CT_ATTR_MAX) {
1283 OVS_NLERR(log,
1284 "Unknown conntrack attr (type=%d, max=%d)",
1285 type, OVS_CT_ATTR_MAX);
1286 return -EINVAL;
1287 }
1288 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1289 OVS_NLERR(log,
1290 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1291 type, nla_len(a), maxlen);
1292 return -EINVAL;
1293 }
1294
1295 switch (type) {
Jarno Rajahalmedd41d332017-02-09 11:22:00 -08001296 case OVS_CT_ATTR_FORCE_COMMIT:
1297 info->force = true;
1298 /* fall through. */
Joe Stringerab38a7b2015-10-06 11:00:01 -07001299 case OVS_CT_ATTR_COMMIT:
1300 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001301 break;
1302#ifdef CONFIG_NF_CONNTRACK_ZONES
1303 case OVS_CT_ATTR_ZONE:
1304 info->zone.id = nla_get_u16(a);
1305 break;
1306#endif
Joe Stringer182e3042015-08-26 11:31:49 -07001307#ifdef CONFIG_NF_CONNTRACK_MARK
1308 case OVS_CT_ATTR_MARK: {
1309 struct md_mark *mark = nla_data(a);
1310
Joe Stringere754ec62015-10-19 19:19:00 -07001311 if (!mark->mask) {
1312 OVS_NLERR(log, "ct_mark mask cannot be 0");
1313 return -EINVAL;
1314 }
Joe Stringer182e3042015-08-26 11:31:49 -07001315 info->mark = *mark;
1316 break;
1317 }
1318#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -07001319#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -07001320 case OVS_CT_ATTR_LABELS: {
1321 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001322
Joe Stringere754ec62015-10-19 19:19:00 -07001323 if (!labels_nonzero(&labels->mask)) {
1324 OVS_NLERR(log, "ct_labels mask cannot be 0");
1325 return -EINVAL;
1326 }
Joe Stringer33db4122015-10-01 15:00:37 -07001327 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001328 break;
1329 }
1330#endif
Joe Stringercae3a262015-08-26 11:31:53 -07001331 case OVS_CT_ATTR_HELPER:
1332 *helper = nla_data(a);
1333 if (!memchr(*helper, '\0', nla_len(a))) {
1334 OVS_NLERR(log, "Invalid conntrack helper");
1335 return -EINVAL;
1336 }
1337 break;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001338#ifdef CONFIG_NF_NAT_NEEDED
1339 case OVS_CT_ATTR_NAT: {
1340 int err = parse_nat(a, info, log);
1341
1342 if (err)
1343 return err;
1344 break;
1345 }
1346#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001347 default:
1348 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1349 type);
1350 return -EINVAL;
1351 }
1352 }
1353
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001354#ifdef CONFIG_NF_CONNTRACK_MARK
1355 if (!info->commit && info->mark.mask) {
1356 OVS_NLERR(log,
1357 "Setting conntrack mark requires 'commit' flag.");
1358 return -EINVAL;
1359 }
1360#endif
1361#ifdef CONFIG_NF_CONNTRACK_LABELS
1362 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1363 OVS_NLERR(log,
1364 "Setting conntrack labels requires 'commit' flag.");
1365 return -EINVAL;
1366 }
1367#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001368 if (rem > 0) {
1369 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1370 return -EINVAL;
1371 }
1372
1373 return 0;
1374}
1375
Joe Stringerc2ac6672015-08-26 11:31:52 -07001376bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001377{
1378 if (attr == OVS_KEY_ATTR_CT_STATE)
1379 return true;
1380 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1381 attr == OVS_KEY_ATTR_CT_ZONE)
1382 return true;
Joe Stringer182e3042015-08-26 11:31:49 -07001383 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1384 attr == OVS_KEY_ATTR_CT_MARK)
1385 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001386 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001387 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001388 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1389
1390 return ovs_net->xt_label;
1391 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001392
1393 return false;
1394}
1395
1396int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1397 const struct sw_flow_key *key,
1398 struct sw_flow_actions **sfa, bool log)
1399{
1400 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -07001401 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001402 u16 family;
1403 int err;
1404
1405 family = key_to_nfproto(key);
1406 if (family == NFPROTO_UNSPEC) {
1407 OVS_NLERR(log, "ct family unspecified");
1408 return -EINVAL;
1409 }
1410
1411 memset(&ct_info, 0, sizeof(ct_info));
1412 ct_info.family = family;
1413
1414 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1415 NF_CT_DEFAULT_ZONE_DIR, 0);
1416
Joe Stringercae3a262015-08-26 11:31:53 -07001417 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001418 if (err)
1419 return err;
1420
1421 /* Set up template for tracking connections in specific zones. */
1422 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1423 if (!ct_info.ct) {
1424 OVS_NLERR(log, "Failed to allocate conntrack template");
1425 return -ENOMEM;
1426 }
Joe Stringer90c7afc962015-12-23 14:39:27 -08001427
1428 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1429 nf_conntrack_get(&ct_info.ct->ct_general);
1430
Joe Stringercae3a262015-08-26 11:31:53 -07001431 if (helper) {
1432 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1433 if (err)
1434 goto err_free_ct;
1435 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001436
1437 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1438 sizeof(ct_info), log);
1439 if (err)
1440 goto err_free_ct;
1441
Joe Stringer7f8a4362015-08-26 11:31:48 -07001442 return 0;
1443err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001444 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001445 return err;
1446}
1447
Jarno Rajahalme05752522016-03-10 10:54:23 -08001448#ifdef CONFIG_NF_NAT_NEEDED
1449static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1450 struct sk_buff *skb)
1451{
1452 struct nlattr *start;
1453
1454 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1455 if (!start)
1456 return false;
1457
1458 if (info->nat & OVS_CT_SRC_NAT) {
1459 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1460 return false;
1461 } else if (info->nat & OVS_CT_DST_NAT) {
1462 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1463 return false;
1464 } else {
1465 goto out;
1466 }
1467
1468 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
Arnd Bergmann99b72482016-03-18 14:33:45 +01001469 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1470 info->family == NFPROTO_IPV4) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001471 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1472 info->range.min_addr.ip) ||
1473 (info->range.max_addr.ip
1474 != info->range.min_addr.ip &&
1475 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1476 info->range.max_addr.ip))))
1477 return false;
Arnd Bergmann99b72482016-03-18 14:33:45 +01001478 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1479 info->family == NFPROTO_IPV6) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001480 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1481 &info->range.min_addr.in6) ||
1482 (memcmp(&info->range.max_addr.in6,
1483 &info->range.min_addr.in6,
1484 sizeof(info->range.max_addr.in6)) &&
1485 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1486 &info->range.max_addr.in6))))
1487 return false;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001488 } else {
1489 return false;
1490 }
1491 }
1492 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1493 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1494 ntohs(info->range.min_proto.all)) ||
1495 (info->range.max_proto.all != info->range.min_proto.all &&
1496 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1497 ntohs(info->range.max_proto.all)))))
1498 return false;
1499
1500 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1501 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1502 return false;
1503 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1504 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1505 return false;
1506 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1507 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1508 return false;
1509out:
1510 nla_nest_end(skb, start);
1511
1512 return true;
1513}
1514#endif
1515
Joe Stringer7f8a4362015-08-26 11:31:48 -07001516int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1517 struct sk_buff *skb)
1518{
1519 struct nlattr *start;
1520
1521 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1522 if (!start)
1523 return -EMSGSIZE;
1524
Jarno Rajahalmedd41d332017-02-09 11:22:00 -08001525 if (ct_info->commit && nla_put_flag(skb, ct_info->force
1526 ? OVS_CT_ATTR_FORCE_COMMIT
1527 : OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001528 return -EMSGSIZE;
1529 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1530 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1531 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -07001532 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -07001533 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1534 &ct_info->mark))
1535 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001536 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -07001537 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001538 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1539 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -07001540 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -07001541 if (ct_info->helper) {
1542 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1543 ct_info->helper->name))
1544 return -EMSGSIZE;
1545 }
Jarno Rajahalme05752522016-03-10 10:54:23 -08001546#ifdef CONFIG_NF_NAT_NEEDED
1547 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1548 return -EMSGSIZE;
1549#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001550 nla_nest_end(skb, start);
1551
1552 return 0;
1553}
1554
1555void ovs_ct_free_action(const struct nlattr *a)
1556{
1557 struct ovs_conntrack_info *ct_info = nla_data(a);
1558
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001559 __ovs_ct_free_action(ct_info);
1560}
1561
1562static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1563{
Joe Stringercae3a262015-08-26 11:31:53 -07001564 if (ct_info->helper)
1565 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001566 if (ct_info->ct)
Joe Stringer76644232016-09-01 18:01:47 -07001567 nf_ct_tmpl_free(ct_info->ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001568}
Joe Stringerc2ac6672015-08-26 11:31:52 -07001569
1570void ovs_ct_init(struct net *net)
1571{
Joe Stringer33db4122015-10-01 15:00:37 -07001572 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001573 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1574
Florian Westphaladff6c62016-04-12 18:14:25 +02001575 if (nf_connlabels_get(net, n_bits - 1)) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001576 ovs_net->xt_label = false;
1577 OVS_NLERR(true, "Failed to set connlabel length");
1578 } else {
1579 ovs_net->xt_label = true;
1580 }
1581}
1582
1583void ovs_ct_exit(struct net *net)
1584{
1585 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1586
1587 if (ovs_net->xt_label)
1588 nf_connlabels_put(net);
1589}