blob: f23934ccce2014d872c81eabd67e03e26c3a8cf7 [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
Joe Stringer2f3ab9f2015-12-09 14:07:39 -080076static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
77
Joe Stringer7f8a4362015-08-26 11:31:48 -070078static u16 key_to_nfproto(const struct sw_flow_key *key)
79{
80 switch (ntohs(key->eth.type)) {
81 case ETH_P_IP:
82 return NFPROTO_IPV4;
83 case ETH_P_IPV6:
84 return NFPROTO_IPV6;
85 default:
86 return NFPROTO_UNSPEC;
87 }
88}
89
90/* Map SKB connection state into the values used by flow definition. */
91static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
92{
93 u8 ct_state = OVS_CS_F_TRACKED;
94
95 switch (ctinfo) {
96 case IP_CT_ESTABLISHED_REPLY:
97 case IP_CT_RELATED_REPLY:
Joe Stringer7f8a4362015-08-26 11:31:48 -070098 ct_state |= OVS_CS_F_REPLY_DIR;
99 break;
100 default:
101 break;
102 }
103
104 switch (ctinfo) {
105 case IP_CT_ESTABLISHED:
106 case IP_CT_ESTABLISHED_REPLY:
107 ct_state |= OVS_CS_F_ESTABLISHED;
108 break;
109 case IP_CT_RELATED:
110 case IP_CT_RELATED_REPLY:
111 ct_state |= OVS_CS_F_RELATED;
112 break;
113 case IP_CT_NEW:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700114 ct_state |= OVS_CS_F_NEW;
115 break;
116 default:
117 break;
118 }
119
120 return ct_state;
121}
122
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700123static u32 ovs_ct_get_mark(const struct nf_conn *ct)
124{
125#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
126 return ct ? ct->mark : 0;
127#else
128 return 0;
129#endif
130}
131
Joe Stringer33db4122015-10-01 15:00:37 -0700132static void ovs_ct_get_labels(const struct nf_conn *ct,
133 struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700134{
135 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
136
137 if (cl) {
Florian Westphal23014012016-07-21 12:51:16 +0200138 size_t len = sizeof(cl->bits);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700139
Joe Stringer33db4122015-10-01 15:00:37 -0700140 if (len > OVS_CT_LABELS_LEN)
141 len = OVS_CT_LABELS_LEN;
142 else if (len < OVS_CT_LABELS_LEN)
143 memset(labels, 0, OVS_CT_LABELS_LEN);
144 memcpy(labels, cl->bits, len);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700145 } else {
Joe Stringer33db4122015-10-01 15:00:37 -0700146 memset(labels, 0, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700147 }
148}
149
Joe Stringer7f8a4362015-08-26 11:31:48 -0700150static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
Joe Stringer182e3042015-08-26 11:31:49 -0700151 const struct nf_conntrack_zone *zone,
152 const struct nf_conn *ct)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700153{
154 key->ct.state = state;
155 key->ct.zone = zone->id;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700156 key->ct.mark = ovs_ct_get_mark(ct);
Joe Stringer33db4122015-10-01 15:00:37 -0700157 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700158}
159
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800160/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
Jarno Rajahalme05752522016-03-10 10:54:23 -0800161 * previously sent the packet to conntrack via the ct action. If
162 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
163 * initialized from the connection status.
Joe Stringer7f8a4362015-08-26 11:31:48 -0700164 */
165static void ovs_ct_update_key(const struct sk_buff *skb,
Joe Stringerd1109862015-12-09 14:07:40 -0800166 const struct ovs_conntrack_info *info,
Jarno Rajahalme05752522016-03-10 10:54:23 -0800167 struct sw_flow_key *key, bool post_ct,
168 bool keep_nat_flags)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700169{
170 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
171 enum ip_conntrack_info ctinfo;
172 struct nf_conn *ct;
173 u8 state = 0;
174
175 ct = nf_ct_get(skb, &ctinfo);
176 if (ct) {
177 state = ovs_ct_get_state(ctinfo);
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800178 /* All unconfirmed entries are NEW connections. */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700179 if (!nf_ct_is_confirmed(ct))
180 state |= OVS_CS_F_NEW;
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800181 /* OVS persists the related flag for the duration of the
182 * connection.
183 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700184 if (ct->master)
185 state |= OVS_CS_F_RELATED;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800186 if (keep_nat_flags) {
187 state |= key->ct.state & OVS_CS_F_NAT_MASK;
188 } else {
189 if (ct->status & IPS_SRC_NAT)
190 state |= OVS_CS_F_SRC_NAT;
191 if (ct->status & IPS_DST_NAT)
192 state |= OVS_CS_F_DST_NAT;
193 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700194 zone = nf_ct_zone(ct);
195 } else if (post_ct) {
196 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
Joe Stringerd1109862015-12-09 14:07:40 -0800197 if (info)
198 zone = &info->zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700199 }
Joe Stringer182e3042015-08-26 11:31:49 -0700200 __ovs_ct_update_key(key, state, zone, ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700201}
202
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800203/* This is called to initialize CT key fields possibly coming in from the local
204 * stack.
205 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700206void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
207{
Jarno Rajahalme05752522016-03-10 10:54:23 -0800208 ovs_ct_update_key(skb, NULL, key, false, false);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700209}
210
211int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
212{
Joe Stringerfbccce52015-10-06 11:00:00 -0700213 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700214 return -EMSGSIZE;
215
216 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
217 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
218 return -EMSGSIZE;
219
Joe Stringer182e3042015-08-26 11:31:49 -0700220 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
221 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
222 return -EMSGSIZE;
223
Valentin Rothberg9723e6a2015-08-28 10:39:56 +0200224 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700225 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
226 &key->ct.labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700227 return -EMSGSIZE;
228
Joe Stringer182e3042015-08-26 11:31:49 -0700229 return 0;
230}
231
232static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
233 u32 ct_mark, u32 mask)
234{
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700235#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
Joe Stringer182e3042015-08-26 11:31:49 -0700236 enum ip_conntrack_info ctinfo;
237 struct nf_conn *ct;
238 u32 new_mark;
239
Joe Stringer182e3042015-08-26 11:31:49 -0700240 /* The connection could be invalid, in which case set_mark is no-op. */
241 ct = nf_ct_get(skb, &ctinfo);
242 if (!ct)
243 return 0;
244
245 new_mark = ct_mark | (ct->mark & ~(mask));
246 if (ct->mark != new_mark) {
247 ct->mark = new_mark;
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800248 if (nf_ct_is_confirmed(ct))
249 nf_conntrack_event_cache(IPCT_MARK, ct);
Joe Stringer182e3042015-08-26 11:31:49 -0700250 key->ct.mark = new_mark;
251 }
252
Joe Stringer7f8a4362015-08-26 11:31:48 -0700253 return 0;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700254#else
255 return -ENOTSUPP;
256#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -0700257}
258
Joe Stringer33db4122015-10-01 15:00:37 -0700259static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
260 const struct ovs_key_ct_labels *labels,
261 const struct ovs_key_ct_labels *mask)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700262{
263 enum ip_conntrack_info ctinfo;
264 struct nf_conn_labels *cl;
265 struct nf_conn *ct;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700266
Joe Stringerc2ac6672015-08-26 11:31:52 -0700267 /* The connection could be invalid, in which case set_label is no-op.*/
268 ct = nf_ct_get(skb, &ctinfo);
269 if (!ct)
270 return 0;
271
272 cl = nf_ct_labels_find(ct);
273 if (!cl) {
274 nf_ct_labels_ext_add(ct);
275 cl = nf_ct_labels_find(ct);
276 }
Florian Westphal23014012016-07-21 12:51:16 +0200277 if (!cl || sizeof(cl->bits) < OVS_CT_LABELS_LEN)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700278 return -ENOSPC;
279
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800280 if (nf_ct_is_confirmed(ct)) {
281 /* Triggers a change event, which makes sense only for
282 * confirmed connections.
283 */
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800284 int err = nf_connlabels_replace(ct, labels->ct_labels_32,
285 mask->ct_labels_32,
286 OVS_CT_LABELS_LEN_32);
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800287 if (err)
288 return err;
289 } else {
290 u32 *dst = (u32 *)cl->bits;
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800291 const u32 *msk = mask->ct_labels_32;
292 const u32 *lbl = labels->ct_labels_32;
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800293 int i;
294
295 /* No-one else has access to the non-confirmed entry, copy
296 * labels over, keeping any bits we are not explicitly setting.
297 */
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800298 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800299 dst[i] = (dst[i] & ~msk[i]) | (lbl[i] & msk[i]);
300 }
Joe Stringerc2ac6672015-08-26 11:31:52 -0700301
Joe Stringer33db4122015-10-01 15:00:37 -0700302 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700303 return 0;
304}
305
Joe Stringercae3a262015-08-26 11:31:53 -0700306/* 'skb' should already be pulled to nh_ofs. */
307static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
308{
309 const struct nf_conntrack_helper *helper;
310 const struct nf_conn_help *help;
311 enum ip_conntrack_info ctinfo;
312 unsigned int protoff;
313 struct nf_conn *ct;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800314 int err;
Joe Stringercae3a262015-08-26 11:31:53 -0700315
316 ct = nf_ct_get(skb, &ctinfo);
317 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
318 return NF_ACCEPT;
319
320 help = nfct_help(ct);
321 if (!help)
322 return NF_ACCEPT;
323
324 helper = rcu_dereference(help->helper);
325 if (!helper)
326 return NF_ACCEPT;
327
328 switch (proto) {
329 case NFPROTO_IPV4:
330 protoff = ip_hdrlen(skb);
331 break;
332 case NFPROTO_IPV6: {
333 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
334 __be16 frag_off;
Joe Stringercc570602015-09-14 11:14:50 -0700335 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700336
Joe Stringercc570602015-09-14 11:14:50 -0700337 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
338 &frag_off);
339 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700340 pr_debug("proto header not found\n");
341 return NF_ACCEPT;
342 }
Joe Stringercc570602015-09-14 11:14:50 -0700343 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700344 break;
345 }
346 default:
347 WARN_ONCE(1, "helper invoked on non-IP family!");
348 return NF_DROP;
349 }
350
Jarno Rajahalme05752522016-03-10 10:54:23 -0800351 err = helper->help(skb, protoff, ct, ctinfo);
352 if (err != NF_ACCEPT)
353 return err;
354
355 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
356 * FTP with NAT) adusting the TCP payload size when mangling IP
357 * addresses and/or port numbers in the text-based control connection.
358 */
359 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
360 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
361 return NF_DROP;
362 return NF_ACCEPT;
Joe Stringercae3a262015-08-26 11:31:53 -0700363}
364
Joe Stringer74c16612015-10-25 20:21:48 -0700365/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
366 * value if 'skb' is freed.
367 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700368static int handle_fragments(struct net *net, struct sw_flow_key *key,
369 u16 zone, struct sk_buff *skb)
370{
371 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100372 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700373
374 if (key->eth.type == htons(ETH_P_IP)) {
375 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700376
377 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500378 err = ip_defrag(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700379 if (err)
380 return err;
381
382 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700383#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700384 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700385 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700386
Joe Stringer49e261a2016-04-18 14:51:47 -0700387 skb_orphan(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700388 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100389 err = nf_ct_frag6_gather(net, skb, user);
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800390 if (err) {
391 if (err != -EINPROGRESS)
392 kfree_skb(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100393 return err;
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800394 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700395
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100396 key->ip.proto = ipv6_hdr(skb)->nexthdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700397 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700398#endif
399 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700400 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700401 return -EPFNOSUPPORT;
402 }
403
404 key->ip.frag = OVS_FRAG_TYPE_NONE;
405 skb_clear_hash(skb);
406 skb->ignore_df = 1;
407 *OVS_CB(skb) = ovs_cb;
408
409 return 0;
410}
411
412static struct nf_conntrack_expect *
413ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
414 u16 proto, const struct sk_buff *skb)
415{
416 struct nf_conntrack_tuple tuple;
417
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500418 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700419 return NULL;
420 return __nf_ct_expect_find(net, zone, &tuple);
421}
422
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800423/* This replicates logic from nf_conntrack_core.c that is not exported. */
424static enum ip_conntrack_info
425ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
426{
427 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
428
429 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
430 return IP_CT_ESTABLISHED_REPLY;
431 /* Once we've had two way comms, always ESTABLISHED. */
432 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
433 return IP_CT_ESTABLISHED;
434 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
435 return IP_CT_RELATED;
436 return IP_CT_NEW;
437}
438
439/* Find an existing connection which this packet belongs to without
440 * re-attributing statistics or modifying the connection state. This allows an
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800441 * skb->_nfct lost due to an upcall to be recovered during actions execution.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800442 *
443 * Must be called with rcu_read_lock.
444 *
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800445 * On success, populates skb->_nfct and returns the connection. Returns NULL
446 * if there is no existing entry.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800447 */
448static struct nf_conn *
449ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800450 u8 l3num, struct sk_buff *skb, bool natted)
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800451{
452 struct nf_conntrack_l3proto *l3proto;
453 struct nf_conntrack_l4proto *l4proto;
454 struct nf_conntrack_tuple tuple;
455 struct nf_conntrack_tuple_hash *h;
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800456 struct nf_conn *ct;
457 unsigned int dataoff;
458 u8 protonum;
459
460 l3proto = __nf_ct_l3proto_find(l3num);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800461 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
462 &protonum) <= 0) {
463 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
464 return NULL;
465 }
466 l4proto = __nf_ct_l4proto_find(l3num, protonum);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800467 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
468 protonum, net, &tuple, l3proto, l4proto)) {
469 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
470 return NULL;
471 }
472
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800473 /* Must invert the tuple if skb has been transformed by NAT. */
474 if (natted) {
475 struct nf_conntrack_tuple inverse;
476
477 if (!nf_ct_invert_tuple(&inverse, &tuple, l3proto, l4proto)) {
478 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
479 return NULL;
480 }
481 tuple = inverse;
482 }
483
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800484 /* look for tuple match */
485 h = nf_conntrack_find_get(net, zone, &tuple);
486 if (!h)
487 return NULL; /* Not found. */
488
489 ct = nf_ct_tuplehash_to_ctrack(h);
490
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800491 /* Inverted packet tuple matches the reverse direction conntrack tuple,
492 * select the other tuplehash to get the right 'ctinfo' bits for this
493 * packet.
494 */
495 if (natted)
496 h = &ct->tuplehash[!h->tuple.dst.dir];
497
Florian Westphalc74454f2017-01-23 18:21:57 +0100498 nf_ct_set(skb, ct, ovs_ct_get_info(h));
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800499 return ct;
500}
501
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800502/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800503static bool skb_nfct_cached(struct net *net,
504 const struct sw_flow_key *key,
505 const struct ovs_conntrack_info *info,
506 struct sk_buff *skb)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700507{
508 enum ip_conntrack_info ctinfo;
509 struct nf_conn *ct;
510
511 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800512 /* If no ct, check if we have evidence that an existing conntrack entry
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800513 * might be found for this skb. This happens when we lose a skb->_nfct
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800514 * due to an upcall. If the connection was not confirmed, it is not
515 * cached and needs to be run through conntrack again.
516 */
517 if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
518 !(key->ct.state & OVS_CS_F_INVALID) &&
519 key->ct.zone == info->zone.id)
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800520 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
521 !!(key->ct.state
522 & OVS_CS_F_NAT_MASK));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700523 if (!ct)
524 return false;
525 if (!net_eq(net, read_pnet(&ct->ct_net)))
526 return false;
527 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
528 return false;
Joe Stringercae3a262015-08-26 11:31:53 -0700529 if (info->helper) {
530 struct nf_conn_help *help;
531
532 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
533 if (help && rcu_access_pointer(help->helper) != info->helper)
534 return false;
535 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700536
537 return true;
538}
539
Jarno Rajahalme05752522016-03-10 10:54:23 -0800540#ifdef CONFIG_NF_NAT_NEEDED
541/* Modelled after nf_nat_ipv[46]_fn().
542 * range is only used for new, uninitialized NAT state.
543 * Returns either NF_ACCEPT or NF_DROP.
544 */
545static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
546 enum ip_conntrack_info ctinfo,
547 const struct nf_nat_range *range,
548 enum nf_nat_manip_type maniptype)
549{
550 int hooknum, nh_off, err = NF_ACCEPT;
551
552 nh_off = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -0500553 skb_pull_rcsum(skb, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800554
555 /* See HOOK2MANIP(). */
556 if (maniptype == NF_NAT_MANIP_SRC)
557 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
558 else
559 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
560
561 switch (ctinfo) {
562 case IP_CT_RELATED:
563 case IP_CT_RELATED_REPLY:
Arnd Bergmann99b72482016-03-18 14:33:45 +0100564 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
565 skb->protocol == htons(ETH_P_IP) &&
Jarno Rajahalme05752522016-03-10 10:54:23 -0800566 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
567 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
568 hooknum))
569 err = NF_DROP;
570 goto push;
Arnd Bergmann99b72482016-03-18 14:33:45 +0100571 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
572 skb->protocol == htons(ETH_P_IPV6)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800573 __be16 frag_off;
574 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
575 int hdrlen = ipv6_skip_exthdr(skb,
576 sizeof(struct ipv6hdr),
577 &nexthdr, &frag_off);
578
579 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
580 if (!nf_nat_icmpv6_reply_translation(skb, ct,
581 ctinfo,
582 hooknum,
583 hdrlen))
584 err = NF_DROP;
585 goto push;
586 }
Jarno Rajahalme05752522016-03-10 10:54:23 -0800587 }
588 /* Non-ICMP, fall thru to initialize if needed. */
589 case IP_CT_NEW:
590 /* Seen it before? This can happen for loopback, retrans,
591 * or local packets.
592 */
593 if (!nf_nat_initialized(ct, maniptype)) {
594 /* Initialize according to the NAT action. */
595 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
596 /* Action is set up to establish a new
597 * mapping.
598 */
599 ? nf_nat_setup_info(ct, range, maniptype)
600 : nf_nat_alloc_null_binding(ct, hooknum);
601 if (err != NF_ACCEPT)
602 goto push;
603 }
604 break;
605
606 case IP_CT_ESTABLISHED:
607 case IP_CT_ESTABLISHED_REPLY:
608 break;
609
610 default:
611 err = NF_DROP;
612 goto push;
613 }
614
615 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
616push:
617 skb_push(skb, nh_off);
Lance Richardson75f01a42017-01-12 19:33:18 -0500618 skb_postpush_rcsum(skb, skb->data, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800619
620 return err;
621}
622
623static void ovs_nat_update_key(struct sw_flow_key *key,
624 const struct sk_buff *skb,
625 enum nf_nat_manip_type maniptype)
626{
627 if (maniptype == NF_NAT_MANIP_SRC) {
628 __be16 src;
629
630 key->ct.state |= OVS_CS_F_SRC_NAT;
631 if (key->eth.type == htons(ETH_P_IP))
632 key->ipv4.addr.src = ip_hdr(skb)->saddr;
633 else if (key->eth.type == htons(ETH_P_IPV6))
634 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
635 sizeof(key->ipv6.addr.src));
636 else
637 return;
638
639 if (key->ip.proto == IPPROTO_UDP)
640 src = udp_hdr(skb)->source;
641 else if (key->ip.proto == IPPROTO_TCP)
642 src = tcp_hdr(skb)->source;
643 else if (key->ip.proto == IPPROTO_SCTP)
644 src = sctp_hdr(skb)->source;
645 else
646 return;
647
648 key->tp.src = src;
649 } else {
650 __be16 dst;
651
652 key->ct.state |= OVS_CS_F_DST_NAT;
653 if (key->eth.type == htons(ETH_P_IP))
654 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
655 else if (key->eth.type == htons(ETH_P_IPV6))
656 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
657 sizeof(key->ipv6.addr.dst));
658 else
659 return;
660
661 if (key->ip.proto == IPPROTO_UDP)
662 dst = udp_hdr(skb)->dest;
663 else if (key->ip.proto == IPPROTO_TCP)
664 dst = tcp_hdr(skb)->dest;
665 else if (key->ip.proto == IPPROTO_SCTP)
666 dst = sctp_hdr(skb)->dest;
667 else
668 return;
669
670 key->tp.dst = dst;
671 }
672}
673
674/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
675static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
676 const struct ovs_conntrack_info *info,
677 struct sk_buff *skb, struct nf_conn *ct,
678 enum ip_conntrack_info ctinfo)
679{
680 enum nf_nat_manip_type maniptype;
681 int err;
682
683 if (nf_ct_is_untracked(ct)) {
684 /* A NAT action may only be performed on tracked packets. */
685 return NF_ACCEPT;
686 }
687
688 /* Add NAT extension if not confirmed yet. */
689 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
690 return NF_ACCEPT; /* Can't NAT. */
691
692 /* Determine NAT type.
693 * Check if the NAT type can be deduced from the tracked connection.
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700694 * Make sure new expected connections (IP_CT_RELATED) are NATted only
695 * when committing.
Jarno Rajahalme05752522016-03-10 10:54:23 -0800696 */
697 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
698 ct->status & IPS_NAT_MASK &&
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700699 (ctinfo != IP_CT_RELATED || info->commit)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800700 /* NAT an established or related connection like before. */
701 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
702 /* This is the REPLY direction for a connection
703 * for which NAT was applied in the forward
704 * direction. Do the reverse NAT.
705 */
706 maniptype = ct->status & IPS_SRC_NAT
707 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
708 else
709 maniptype = ct->status & IPS_SRC_NAT
710 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
711 } else if (info->nat & OVS_CT_SRC_NAT) {
712 maniptype = NF_NAT_MANIP_SRC;
713 } else if (info->nat & OVS_CT_DST_NAT) {
714 maniptype = NF_NAT_MANIP_DST;
715 } else {
716 return NF_ACCEPT; /* Connection is not NATed. */
717 }
718 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
719
720 /* Mark NAT done if successful and update the flow key. */
721 if (err == NF_ACCEPT)
722 ovs_nat_update_key(key, skb, maniptype);
723
724 return err;
725}
726#else /* !CONFIG_NF_NAT_NEEDED */
727static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
728 const struct ovs_conntrack_info *info,
729 struct sk_buff *skb, struct nf_conn *ct,
730 enum ip_conntrack_info ctinfo)
731{
732 return NF_ACCEPT;
733}
734#endif
735
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800736/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800737 * not done already. Update key with new CT state after passing the packet
738 * through conntrack.
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800739 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800740 * set to NULL and 0 will be returned.
741 */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700742static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700743 const struct ovs_conntrack_info *info,
744 struct sk_buff *skb)
745{
746 /* If we are recirculating packets to match on conntrack fields and
747 * committing with a separate conntrack action, then we don't need to
748 * actually run the packet through conntrack twice unless it's for a
749 * different zone.
750 */
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800751 bool cached = skb_nfct_cached(net, key, info, skb);
752 enum ip_conntrack_info ctinfo;
753 struct nf_conn *ct;
754
755 if (!cached) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700756 struct nf_conn *tmpl = info->ct;
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800757 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700758
759 /* Associate skb with specified zone. */
760 if (tmpl) {
Florian Westphalcb9c6832017-01-23 18:21:56 +0100761 if (skb_nfct(skb))
762 nf_conntrack_put(skb_nfct(skb));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700763 nf_conntrack_get(&tmpl->ct_general);
Florian Westphalc74454f2017-01-23 18:21:57 +0100764 nf_ct_set(skb, tmpl, IP_CT_NEW);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700765 }
766
Pablo Neira Ayuso08733a02016-11-03 10:56:43 +0100767 err = nf_conntrack_in(net, info->family,
768 NF_INET_PRE_ROUTING, skb);
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800769 if (err != NF_ACCEPT)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700770 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700771
Jarno Rajahalme05752522016-03-10 10:54:23 -0800772 /* Clear CT state NAT flags to mark that we have not yet done
773 * NAT after the nf_conntrack_in() call. We can actually clear
774 * the whole state, as it will be re-initialized below.
775 */
776 key->ct.state = 0;
777
778 /* Update the key, but keep the NAT flags. */
779 ovs_ct_update_key(skb, info, key, true, true);
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800780 }
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800781
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800782 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800783 if (ct) {
784 /* Packets starting a new connection must be NATted before the
785 * helper, so that the helper knows about the NAT. We enforce
786 * this by delaying both NAT and helper calls for unconfirmed
787 * connections until the committing CT action. For later
788 * packets NAT and Helper may be called in either order.
789 *
790 * NAT will be done only if the CT action has NAT, and only
791 * once per packet (per zone), as guarded by the NAT bits in
792 * the key->ct.state.
793 */
794 if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
795 (nf_ct_is_confirmed(ct) || info->commit) &&
796 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
797 return -EINVAL;
798 }
799
Joe Stringer16ec3d42016-05-11 10:29:26 -0700800 /* Userspace may decide to perform a ct lookup without a helper
801 * specified followed by a (recirculate and) commit with one.
802 * Therefore, for unconfirmed connections which we will commit,
803 * we need to attach the helper here.
804 */
805 if (!nf_ct_is_confirmed(ct) && info->commit &&
806 info->helper && !nfct_help(ct)) {
807 int err = __nf_ct_try_assign_helper(ct, info->ct,
808 GFP_ATOMIC);
809 if (err)
810 return err;
811 }
812
Jarno Rajahalme05752522016-03-10 10:54:23 -0800813 /* Call the helper only if:
814 * - nf_conntrack_in() was executed above ("!cached") for a
815 * confirmed connection, or
816 * - When committing an unconfirmed connection.
817 */
818 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
819 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
820 return -EINVAL;
821 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700822 }
823
824 return 0;
825}
826
827/* Lookup connection and read fields into key. */
828static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
829 const struct ovs_conntrack_info *info,
830 struct sk_buff *skb)
831{
832 struct nf_conntrack_expect *exp;
833
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800834 /* If we pass an expected packet through nf_conntrack_in() the
835 * expectation is typically removed, but the packet could still be
836 * lost in upcall processing. To prevent this from happening we
837 * perform an explicit expectation lookup. Expected connections are
838 * always new, and will be passed through conntrack only when they are
839 * committed, as it is OK to remove the expectation at that time.
840 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700841 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
842 if (exp) {
843 u8 state;
844
Jarno Rajahalme05752522016-03-10 10:54:23 -0800845 /* NOTE: New connections are NATted and Helped only when
846 * committed, so we are not calling into NAT here.
847 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700848 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700849 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200850 } else {
851 struct nf_conn *ct;
852 int err;
853
854 err = __ovs_ct_lookup(net, key, info, skb);
855 if (err)
856 return err;
857
Florian Westphalcb9c6832017-01-23 18:21:56 +0100858 ct = (struct nf_conn *)skb_nfct(skb);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200859 if (ct)
860 nf_ct_deliver_cached_events(ct);
861 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700862
863 return 0;
864}
865
Joe Stringer33db4122015-10-01 15:00:37 -0700866static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700867{
868 size_t i;
869
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800870 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
871 if (labels->ct_labels_32[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700872 return true;
873
874 return false;
875}
876
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700877/* Lookup connection and confirm if unconfirmed. */
878static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
879 const struct ovs_conntrack_info *info,
880 struct sk_buff *skb)
881{
882 int err;
883
884 err = __ovs_ct_lookup(net, key, info, skb);
885 if (err)
886 return err;
887
888 /* Apply changes before confirming the connection so that the initial
889 * conntrack NEW netlink event carries the values given in the CT
890 * action.
891 */
892 if (info->mark.mask) {
893 err = ovs_ct_set_mark(skb, key, info->mark.value,
894 info->mark.mask);
895 if (err)
896 return err;
897 }
898 if (labels_nonzero(&info->labels.mask)) {
899 err = ovs_ct_set_labels(skb, key, &info->labels.value,
900 &info->labels.mask);
901 if (err)
902 return err;
903 }
904 /* This will take care of sending queued events even if the connection
905 * is already confirmed.
906 */
907 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
908 return -EINVAL;
909
910 return 0;
911}
912
Joe Stringer74c16612015-10-25 20:21:48 -0700913/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
914 * value if 'skb' is freed.
915 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700916int ovs_ct_execute(struct net *net, struct sk_buff *skb,
917 struct sw_flow_key *key,
918 const struct ovs_conntrack_info *info)
919{
920 int nh_ofs;
921 int err;
922
923 /* The conntrack module expects to be working at L3. */
924 nh_ofs = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -0500925 skb_pull_rcsum(skb, nh_ofs);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700926
927 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
928 err = handle_fragments(net, key, info->zone.id, skb);
929 if (err)
930 return err;
931 }
932
Joe Stringerab38a7b2015-10-06 11:00:01 -0700933 if (info->commit)
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700934 err = ovs_ct_commit(net, key, info, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700935 else
936 err = ovs_ct_lookup(net, key, info, skb);
937
938 skb_push(skb, nh_ofs);
Lance Richardson75f01a42017-01-12 19:33:18 -0500939 skb_postpush_rcsum(skb, skb->data, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -0700940 if (err)
941 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700942 return err;
943}
944
Joe Stringercae3a262015-08-26 11:31:53 -0700945static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
946 const struct sw_flow_key *key, bool log)
947{
948 struct nf_conntrack_helper *helper;
949 struct nf_conn_help *help;
950
951 helper = nf_conntrack_helper_try_module_get(name, info->family,
952 key->ip.proto);
953 if (!helper) {
954 OVS_NLERR(log, "Unknown helper \"%s\"", name);
955 return -EINVAL;
956 }
957
958 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
959 if (!help) {
960 module_put(helper->me);
961 return -ENOMEM;
962 }
963
964 rcu_assign_pointer(help->helper, helper);
965 info->helper = helper;
966 return 0;
967}
968
Jarno Rajahalme05752522016-03-10 10:54:23 -0800969#ifdef CONFIG_NF_NAT_NEEDED
970static int parse_nat(const struct nlattr *attr,
971 struct ovs_conntrack_info *info, bool log)
972{
973 struct nlattr *a;
974 int rem;
975 bool have_ip_max = false;
976 bool have_proto_max = false;
977 bool ip_vers = (info->family == NFPROTO_IPV6);
978
979 nla_for_each_nested(a, attr, rem) {
980 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
981 [OVS_NAT_ATTR_SRC] = {0, 0},
982 [OVS_NAT_ATTR_DST] = {0, 0},
983 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
984 sizeof(struct in6_addr)},
985 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
986 sizeof(struct in6_addr)},
987 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
988 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
989 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
990 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
991 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
992 };
993 int type = nla_type(a);
994
995 if (type > OVS_NAT_ATTR_MAX) {
996 OVS_NLERR(log,
997 "Unknown NAT attribute (type=%d, max=%d).\n",
998 type, OVS_NAT_ATTR_MAX);
999 return -EINVAL;
1000 }
1001
1002 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1003 OVS_NLERR(log,
1004 "NAT attribute type %d has unexpected length (%d != %d).\n",
1005 type, nla_len(a),
1006 ovs_nat_attr_lens[type][ip_vers]);
1007 return -EINVAL;
1008 }
1009
1010 switch (type) {
1011 case OVS_NAT_ATTR_SRC:
1012 case OVS_NAT_ATTR_DST:
1013 if (info->nat) {
1014 OVS_NLERR(log,
1015 "Only one type of NAT may be specified.\n"
1016 );
1017 return -ERANGE;
1018 }
1019 info->nat |= OVS_CT_NAT;
1020 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1021 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1022 break;
1023
1024 case OVS_NAT_ATTR_IP_MIN:
Haishuang Yanac71b462016-03-28 18:08:59 +08001025 nla_memcpy(&info->range.min_addr, a,
1026 sizeof(info->range.min_addr));
Jarno Rajahalme05752522016-03-10 10:54:23 -08001027 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1028 break;
1029
1030 case OVS_NAT_ATTR_IP_MAX:
1031 have_ip_max = true;
1032 nla_memcpy(&info->range.max_addr, a,
1033 sizeof(info->range.max_addr));
1034 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1035 break;
1036
1037 case OVS_NAT_ATTR_PROTO_MIN:
1038 info->range.min_proto.all = htons(nla_get_u16(a));
1039 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1040 break;
1041
1042 case OVS_NAT_ATTR_PROTO_MAX:
1043 have_proto_max = true;
1044 info->range.max_proto.all = htons(nla_get_u16(a));
1045 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1046 break;
1047
1048 case OVS_NAT_ATTR_PERSISTENT:
1049 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1050 break;
1051
1052 case OVS_NAT_ATTR_PROTO_HASH:
1053 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1054 break;
1055
1056 case OVS_NAT_ATTR_PROTO_RANDOM:
1057 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1058 break;
1059
1060 default:
1061 OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
1062 return -EINVAL;
1063 }
1064 }
1065
1066 if (rem > 0) {
1067 OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
1068 return -EINVAL;
1069 }
1070 if (!info->nat) {
1071 /* Do not allow flags if no type is given. */
1072 if (info->range.flags) {
1073 OVS_NLERR(log,
1074 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1075 );
1076 return -EINVAL;
1077 }
1078 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1079 } else if (!info->commit) {
1080 OVS_NLERR(log,
1081 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1082 );
1083 return -EINVAL;
1084 }
1085 /* Allow missing IP_MAX. */
1086 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1087 memcpy(&info->range.max_addr, &info->range.min_addr,
1088 sizeof(info->range.max_addr));
1089 }
1090 /* Allow missing PROTO_MAX. */
1091 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1092 !have_proto_max) {
1093 info->range.max_proto.all = info->range.min_proto.all;
1094 }
1095 return 0;
1096}
1097#endif
1098
Joe Stringer7f8a4362015-08-26 11:31:48 -07001099static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001100 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -07001101 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1102 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -07001103 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1104 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -07001105 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1106 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -07001107 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
Jarno Rajahalme05752522016-03-10 10:54:23 -08001108 .maxlen = NF_CT_HELPER_NAME_LEN },
1109#ifdef CONFIG_NF_NAT_NEEDED
1110 /* NAT length is checked when parsing the nested attributes. */
1111 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1112#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001113};
1114
1115static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -07001116 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001117{
1118 struct nlattr *a;
1119 int rem;
1120
1121 nla_for_each_nested(a, attr, rem) {
1122 int type = nla_type(a);
1123 int maxlen = ovs_ct_attr_lens[type].maxlen;
1124 int minlen = ovs_ct_attr_lens[type].minlen;
1125
1126 if (type > OVS_CT_ATTR_MAX) {
1127 OVS_NLERR(log,
1128 "Unknown conntrack attr (type=%d, max=%d)",
1129 type, OVS_CT_ATTR_MAX);
1130 return -EINVAL;
1131 }
1132 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1133 OVS_NLERR(log,
1134 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1135 type, nla_len(a), maxlen);
1136 return -EINVAL;
1137 }
1138
1139 switch (type) {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001140 case OVS_CT_ATTR_COMMIT:
1141 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001142 break;
1143#ifdef CONFIG_NF_CONNTRACK_ZONES
1144 case OVS_CT_ATTR_ZONE:
1145 info->zone.id = nla_get_u16(a);
1146 break;
1147#endif
Joe Stringer182e3042015-08-26 11:31:49 -07001148#ifdef CONFIG_NF_CONNTRACK_MARK
1149 case OVS_CT_ATTR_MARK: {
1150 struct md_mark *mark = nla_data(a);
1151
Joe Stringere754ec62015-10-19 19:19:00 -07001152 if (!mark->mask) {
1153 OVS_NLERR(log, "ct_mark mask cannot be 0");
1154 return -EINVAL;
1155 }
Joe Stringer182e3042015-08-26 11:31:49 -07001156 info->mark = *mark;
1157 break;
1158 }
1159#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -07001160#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -07001161 case OVS_CT_ATTR_LABELS: {
1162 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001163
Joe Stringere754ec62015-10-19 19:19:00 -07001164 if (!labels_nonzero(&labels->mask)) {
1165 OVS_NLERR(log, "ct_labels mask cannot be 0");
1166 return -EINVAL;
1167 }
Joe Stringer33db4122015-10-01 15:00:37 -07001168 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001169 break;
1170 }
1171#endif
Joe Stringercae3a262015-08-26 11:31:53 -07001172 case OVS_CT_ATTR_HELPER:
1173 *helper = nla_data(a);
1174 if (!memchr(*helper, '\0', nla_len(a))) {
1175 OVS_NLERR(log, "Invalid conntrack helper");
1176 return -EINVAL;
1177 }
1178 break;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001179#ifdef CONFIG_NF_NAT_NEEDED
1180 case OVS_CT_ATTR_NAT: {
1181 int err = parse_nat(a, info, log);
1182
1183 if (err)
1184 return err;
1185 break;
1186 }
1187#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001188 default:
1189 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1190 type);
1191 return -EINVAL;
1192 }
1193 }
1194
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001195#ifdef CONFIG_NF_CONNTRACK_MARK
1196 if (!info->commit && info->mark.mask) {
1197 OVS_NLERR(log,
1198 "Setting conntrack mark requires 'commit' flag.");
1199 return -EINVAL;
1200 }
1201#endif
1202#ifdef CONFIG_NF_CONNTRACK_LABELS
1203 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1204 OVS_NLERR(log,
1205 "Setting conntrack labels requires 'commit' flag.");
1206 return -EINVAL;
1207 }
1208#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001209 if (rem > 0) {
1210 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1211 return -EINVAL;
1212 }
1213
1214 return 0;
1215}
1216
Joe Stringerc2ac6672015-08-26 11:31:52 -07001217bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001218{
1219 if (attr == OVS_KEY_ATTR_CT_STATE)
1220 return true;
1221 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1222 attr == OVS_KEY_ATTR_CT_ZONE)
1223 return true;
Joe Stringer182e3042015-08-26 11:31:49 -07001224 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1225 attr == OVS_KEY_ATTR_CT_MARK)
1226 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001227 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001228 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001229 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1230
1231 return ovs_net->xt_label;
1232 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001233
1234 return false;
1235}
1236
1237int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1238 const struct sw_flow_key *key,
1239 struct sw_flow_actions **sfa, bool log)
1240{
1241 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -07001242 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001243 u16 family;
1244 int err;
1245
1246 family = key_to_nfproto(key);
1247 if (family == NFPROTO_UNSPEC) {
1248 OVS_NLERR(log, "ct family unspecified");
1249 return -EINVAL;
1250 }
1251
1252 memset(&ct_info, 0, sizeof(ct_info));
1253 ct_info.family = family;
1254
1255 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1256 NF_CT_DEFAULT_ZONE_DIR, 0);
1257
Joe Stringercae3a262015-08-26 11:31:53 -07001258 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001259 if (err)
1260 return err;
1261
1262 /* Set up template for tracking connections in specific zones. */
1263 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1264 if (!ct_info.ct) {
1265 OVS_NLERR(log, "Failed to allocate conntrack template");
1266 return -ENOMEM;
1267 }
Joe Stringer90c7afc962015-12-23 14:39:27 -08001268
1269 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1270 nf_conntrack_get(&ct_info.ct->ct_general);
1271
Joe Stringercae3a262015-08-26 11:31:53 -07001272 if (helper) {
1273 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1274 if (err)
1275 goto err_free_ct;
1276 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001277
1278 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1279 sizeof(ct_info), log);
1280 if (err)
1281 goto err_free_ct;
1282
Joe Stringer7f8a4362015-08-26 11:31:48 -07001283 return 0;
1284err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001285 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001286 return err;
1287}
1288
Jarno Rajahalme05752522016-03-10 10:54:23 -08001289#ifdef CONFIG_NF_NAT_NEEDED
1290static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1291 struct sk_buff *skb)
1292{
1293 struct nlattr *start;
1294
1295 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1296 if (!start)
1297 return false;
1298
1299 if (info->nat & OVS_CT_SRC_NAT) {
1300 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1301 return false;
1302 } else if (info->nat & OVS_CT_DST_NAT) {
1303 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1304 return false;
1305 } else {
1306 goto out;
1307 }
1308
1309 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
Arnd Bergmann99b72482016-03-18 14:33:45 +01001310 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1311 info->family == NFPROTO_IPV4) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001312 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1313 info->range.min_addr.ip) ||
1314 (info->range.max_addr.ip
1315 != info->range.min_addr.ip &&
1316 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1317 info->range.max_addr.ip))))
1318 return false;
Arnd Bergmann99b72482016-03-18 14:33:45 +01001319 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1320 info->family == NFPROTO_IPV6) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001321 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1322 &info->range.min_addr.in6) ||
1323 (memcmp(&info->range.max_addr.in6,
1324 &info->range.min_addr.in6,
1325 sizeof(info->range.max_addr.in6)) &&
1326 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1327 &info->range.max_addr.in6))))
1328 return false;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001329 } else {
1330 return false;
1331 }
1332 }
1333 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1334 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1335 ntohs(info->range.min_proto.all)) ||
1336 (info->range.max_proto.all != info->range.min_proto.all &&
1337 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1338 ntohs(info->range.max_proto.all)))))
1339 return false;
1340
1341 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1342 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1343 return false;
1344 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1345 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1346 return false;
1347 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1348 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1349 return false;
1350out:
1351 nla_nest_end(skb, start);
1352
1353 return true;
1354}
1355#endif
1356
Joe Stringer7f8a4362015-08-26 11:31:48 -07001357int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1358 struct sk_buff *skb)
1359{
1360 struct nlattr *start;
1361
1362 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1363 if (!start)
1364 return -EMSGSIZE;
1365
Joe Stringerab38a7b2015-10-06 11:00:01 -07001366 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001367 return -EMSGSIZE;
1368 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1369 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1370 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -07001371 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -07001372 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1373 &ct_info->mark))
1374 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001375 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -07001376 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001377 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1378 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -07001379 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -07001380 if (ct_info->helper) {
1381 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1382 ct_info->helper->name))
1383 return -EMSGSIZE;
1384 }
Jarno Rajahalme05752522016-03-10 10:54:23 -08001385#ifdef CONFIG_NF_NAT_NEEDED
1386 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1387 return -EMSGSIZE;
1388#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001389 nla_nest_end(skb, start);
1390
1391 return 0;
1392}
1393
1394void ovs_ct_free_action(const struct nlattr *a)
1395{
1396 struct ovs_conntrack_info *ct_info = nla_data(a);
1397
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001398 __ovs_ct_free_action(ct_info);
1399}
1400
1401static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1402{
Joe Stringercae3a262015-08-26 11:31:53 -07001403 if (ct_info->helper)
1404 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001405 if (ct_info->ct)
Joe Stringer76644232016-09-01 18:01:47 -07001406 nf_ct_tmpl_free(ct_info->ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001407}
Joe Stringerc2ac6672015-08-26 11:31:52 -07001408
1409void ovs_ct_init(struct net *net)
1410{
Joe Stringer33db4122015-10-01 15:00:37 -07001411 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001412 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1413
Florian Westphaladff6c62016-04-12 18:14:25 +02001414 if (nf_connlabels_get(net, n_bits - 1)) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001415 ovs_net->xt_label = false;
1416 OVS_NLERR(true, "Failed to set connlabel length");
1417 } else {
1418 ovs_net->xt_label = true;
1419 }
1420}
1421
1422void ovs_ct_exit(struct net *net)
1423{
1424 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1425
1426 if (ovs_net->xt_label)
1427 nf_connlabels_put(net);
1428}