blob: 02d6f38f786960edaf2a9dca340221865314ce14 [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>
Florian Westphal33336cd2019-04-26 08:41:06 -070026#include <net/ipv6_frag.h>
Joe Stringer7f8a4362015-08-26 11:31:48 -070027
Jarno Rajahalme05752522016-03-10 10:54:23 -080028#ifdef CONFIG_NF_NAT_NEEDED
29#include <linux/netfilter/nf_nat.h>
30#include <net/netfilter/nf_nat_core.h>
31#include <net/netfilter/nf_nat_l3proto.h>
32#endif
33
Joe Stringer7f8a4362015-08-26 11:31:48 -070034#include "datapath.h"
35#include "conntrack.h"
36#include "flow.h"
37#include "flow_netlink.h"
38
39struct ovs_ct_len_tbl {
Jarno Rajahalme05752522016-03-10 10:54:23 -080040 int maxlen;
41 int minlen;
Joe Stringer7f8a4362015-08-26 11:31:48 -070042};
43
Joe Stringer182e3042015-08-26 11:31:49 -070044/* Metadata mark for masked write to conntrack mark */
45struct md_mark {
46 u32 value;
47 u32 mask;
48};
49
Joe Stringerc2ac6672015-08-26 11:31:52 -070050/* Metadata label for masked write to conntrack label. */
Joe Stringer33db4122015-10-01 15:00:37 -070051struct md_labels {
52 struct ovs_key_ct_labels value;
53 struct ovs_key_ct_labels mask;
Joe Stringerc2ac6672015-08-26 11:31:52 -070054};
55
Jarno Rajahalme05752522016-03-10 10:54:23 -080056enum ovs_ct_nat {
57 OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
58 OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
59 OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
60};
61
Joe Stringer7f8a4362015-08-26 11:31:48 -070062/* Conntrack action context for execution. */
63struct ovs_conntrack_info {
Joe Stringercae3a262015-08-26 11:31:53 -070064 struct nf_conntrack_helper *helper;
Joe Stringer7f8a4362015-08-26 11:31:48 -070065 struct nf_conntrack_zone zone;
66 struct nf_conn *ct;
Joe Stringerab38a7b2015-10-06 11:00:01 -070067 u8 commit : 1;
Jarno Rajahalme05752522016-03-10 10:54:23 -080068 u8 nat : 3; /* enum ovs_ct_nat */
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
Joe Stringer2f3ab9f2015-12-09 14:07:39 -080077static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
78
Joe Stringer7f8a4362015-08-26 11:31:48 -070079static u16 key_to_nfproto(const struct sw_flow_key *key)
80{
81 switch (ntohs(key->eth.type)) {
82 case ETH_P_IP:
83 return NFPROTO_IPV4;
84 case ETH_P_IPV6:
85 return NFPROTO_IPV6;
86 default:
87 return NFPROTO_UNSPEC;
88 }
89}
90
91/* Map SKB connection state into the values used by flow definition. */
92static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
93{
94 u8 ct_state = OVS_CS_F_TRACKED;
95
96 switch (ctinfo) {
97 case IP_CT_ESTABLISHED_REPLY:
98 case IP_CT_RELATED_REPLY:
Joe Stringer7f8a4362015-08-26 11:31:48 -070099 ct_state |= OVS_CS_F_REPLY_DIR;
100 break;
101 default:
102 break;
103 }
104
105 switch (ctinfo) {
106 case IP_CT_ESTABLISHED:
107 case IP_CT_ESTABLISHED_REPLY:
108 ct_state |= OVS_CS_F_ESTABLISHED;
109 break;
110 case IP_CT_RELATED:
111 case IP_CT_RELATED_REPLY:
112 ct_state |= OVS_CS_F_RELATED;
113 break;
114 case IP_CT_NEW:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700115 ct_state |= OVS_CS_F_NEW;
116 break;
117 default:
118 break;
119 }
120
121 return ct_state;
122}
123
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700124static u32 ovs_ct_get_mark(const struct nf_conn *ct)
125{
126#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
127 return ct ? ct->mark : 0;
128#else
129 return 0;
130#endif
131}
132
Joe Stringer33db4122015-10-01 15:00:37 -0700133static void ovs_ct_get_labels(const struct nf_conn *ct,
134 struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700135{
136 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
137
138 if (cl) {
Florian Westphal23014012016-07-21 12:51:16 +0200139 size_t len = sizeof(cl->bits);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700140
Joe Stringer33db4122015-10-01 15:00:37 -0700141 if (len > OVS_CT_LABELS_LEN)
142 len = OVS_CT_LABELS_LEN;
143 else if (len < OVS_CT_LABELS_LEN)
144 memset(labels, 0, OVS_CT_LABELS_LEN);
145 memcpy(labels, cl->bits, len);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700146 } else {
Joe Stringer33db4122015-10-01 15:00:37 -0700147 memset(labels, 0, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700148 }
149}
150
Joe Stringer7f8a4362015-08-26 11:31:48 -0700151static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
Joe Stringer182e3042015-08-26 11:31:49 -0700152 const struct nf_conntrack_zone *zone,
153 const struct nf_conn *ct)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700154{
155 key->ct.state = state;
156 key->ct.zone = zone->id;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700157 key->ct.mark = ovs_ct_get_mark(ct);
Joe Stringer33db4122015-10-01 15:00:37 -0700158 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700159}
160
Jarno Rajahalme05752522016-03-10 10:54:23 -0800161/* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
162 * previously sent the packet to conntrack via the ct action. If
163 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
164 * initialized from the connection status.
Joe Stringer7f8a4362015-08-26 11:31:48 -0700165 */
166static void ovs_ct_update_key(const struct sk_buff *skb,
Joe Stringerd1109862015-12-09 14:07:40 -0800167 const struct ovs_conntrack_info *info,
Jarno Rajahalme05752522016-03-10 10:54:23 -0800168 struct sw_flow_key *key, bool post_ct,
169 bool keep_nat_flags)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700170{
171 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
172 enum ip_conntrack_info ctinfo;
173 struct nf_conn *ct;
174 u8 state = 0;
175
176 ct = nf_ct_get(skb, &ctinfo);
177 if (ct) {
178 state = ovs_ct_get_state(ctinfo);
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800179 /* All unconfirmed entries are NEW connections. */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700180 if (!nf_ct_is_confirmed(ct))
181 state |= OVS_CS_F_NEW;
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800182 /* OVS persists the related flag for the duration of the
183 * connection.
184 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700185 if (ct->master)
186 state |= OVS_CS_F_RELATED;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800187 if (keep_nat_flags) {
188 state |= key->ct.state & OVS_CS_F_NAT_MASK;
189 } else {
190 if (ct->status & IPS_SRC_NAT)
191 state |= OVS_CS_F_SRC_NAT;
192 if (ct->status & IPS_DST_NAT)
193 state |= OVS_CS_F_DST_NAT;
194 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700195 zone = nf_ct_zone(ct);
196 } else if (post_ct) {
197 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
Joe Stringerd1109862015-12-09 14:07:40 -0800198 if (info)
199 zone = &info->zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700200 }
Joe Stringer182e3042015-08-26 11:31:49 -0700201 __ovs_ct_update_key(key, state, zone, ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700202}
203
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800204/* This is called to initialize CT key fields possibly coming in from the local
205 * stack.
206 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700207void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
208{
Jarno Rajahalme05752522016-03-10 10:54:23 -0800209 ovs_ct_update_key(skb, NULL, key, false, false);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700210}
211
212int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
213{
Joe Stringerfbccce52015-10-06 11:00:00 -0700214 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700215 return -EMSGSIZE;
216
217 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
218 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
219 return -EMSGSIZE;
220
Joe Stringer182e3042015-08-26 11:31:49 -0700221 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
222 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
223 return -EMSGSIZE;
224
Valentin Rothberg9723e6a2015-08-28 10:39:56 +0200225 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700226 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
227 &key->ct.labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700228 return -EMSGSIZE;
229
Joe Stringer182e3042015-08-26 11:31:49 -0700230 return 0;
231}
232
233static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
234 u32 ct_mark, u32 mask)
235{
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700236#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
Joe Stringer182e3042015-08-26 11:31:49 -0700237 enum ip_conntrack_info ctinfo;
238 struct nf_conn *ct;
239 u32 new_mark;
240
Joe Stringer182e3042015-08-26 11:31:49 -0700241 /* The connection could be invalid, in which case set_mark is no-op. */
242 ct = nf_ct_get(skb, &ctinfo);
243 if (!ct)
244 return 0;
245
246 new_mark = ct_mark | (ct->mark & ~(mask));
247 if (ct->mark != new_mark) {
248 ct->mark = new_mark;
249 nf_conntrack_event_cache(IPCT_MARK, ct);
250 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;
266 int err;
267
Joe Stringerc2ac6672015-08-26 11:31:52 -0700268 /* The connection could be invalid, in which case set_label is no-op.*/
269 ct = nf_ct_get(skb, &ctinfo);
270 if (!ct)
271 return 0;
272
273 cl = nf_ct_labels_find(ct);
274 if (!cl) {
275 nf_ct_labels_ext_add(ct);
276 cl = nf_ct_labels_find(ct);
277 }
Florian Westphal23014012016-07-21 12:51:16 +0200278 if (!cl || sizeof(cl->bits) < OVS_CT_LABELS_LEN)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700279 return -ENOSPC;
280
Joe Stringer33db4122015-10-01 15:00:37 -0700281 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
282 OVS_CT_LABELS_LEN / sizeof(u32));
Joe Stringerc2ac6672015-08-26 11:31:52 -0700283 if (err)
284 return err;
285
Joe Stringer33db4122015-10-01 15:00:37 -0700286 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700287 return 0;
288}
289
Joe Stringercae3a262015-08-26 11:31:53 -0700290/* 'skb' should already be pulled to nh_ofs. */
291static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
292{
293 const struct nf_conntrack_helper *helper;
294 const struct nf_conn_help *help;
295 enum ip_conntrack_info ctinfo;
296 unsigned int protoff;
297 struct nf_conn *ct;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800298 int err;
Joe Stringercae3a262015-08-26 11:31:53 -0700299
300 ct = nf_ct_get(skb, &ctinfo);
301 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
302 return NF_ACCEPT;
303
304 help = nfct_help(ct);
305 if (!help)
306 return NF_ACCEPT;
307
308 helper = rcu_dereference(help->helper);
309 if (!helper)
310 return NF_ACCEPT;
311
312 switch (proto) {
313 case NFPROTO_IPV4:
314 protoff = ip_hdrlen(skb);
315 break;
316 case NFPROTO_IPV6: {
317 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
318 __be16 frag_off;
Joe Stringercc570602015-09-14 11:14:50 -0700319 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700320
Joe Stringercc570602015-09-14 11:14:50 -0700321 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
322 &frag_off);
323 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700324 pr_debug("proto header not found\n");
325 return NF_ACCEPT;
326 }
Joe Stringercc570602015-09-14 11:14:50 -0700327 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700328 break;
329 }
330 default:
331 WARN_ONCE(1, "helper invoked on non-IP family!");
332 return NF_DROP;
333 }
334
Jarno Rajahalme05752522016-03-10 10:54:23 -0800335 err = helper->help(skb, protoff, ct, ctinfo);
336 if (err != NF_ACCEPT)
337 return err;
338
339 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
340 * FTP with NAT) adusting the TCP payload size when mangling IP
341 * addresses and/or port numbers in the text-based control connection.
342 */
343 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
344 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
345 return NF_DROP;
346 return NF_ACCEPT;
Joe Stringercae3a262015-08-26 11:31:53 -0700347}
348
Joe Stringer74c16612015-10-25 20:21:48 -0700349/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
350 * value if 'skb' is freed.
351 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700352static int handle_fragments(struct net *net, struct sw_flow_key *key,
353 u16 zone, struct sk_buff *skb)
354{
355 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100356 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700357
358 if (key->eth.type == htons(ETH_P_IP)) {
359 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700360
361 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500362 err = ip_defrag(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700363 if (err)
364 return err;
365
366 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700367#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700368 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700369 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700370
371 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100372 err = nf_ct_frag6_gather(net, skb, user);
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800373 if (err) {
374 if (err != -EINPROGRESS)
375 kfree_skb(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100376 return err;
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800377 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700378
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100379 key->ip.proto = ipv6_hdr(skb)->nexthdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700380 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700381#endif
382 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700383 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700384 return -EPFNOSUPPORT;
385 }
386
387 key->ip.frag = OVS_FRAG_TYPE_NONE;
388 skb_clear_hash(skb);
389 skb->ignore_df = 1;
390 *OVS_CB(skb) = ovs_cb;
391
392 return 0;
393}
394
395static struct nf_conntrack_expect *
396ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
397 u16 proto, const struct sk_buff *skb)
398{
399 struct nf_conntrack_tuple tuple;
Jarno Rajahalmed2e269c2017-04-14 14:26:38 -0700400 struct nf_conntrack_expect *exp;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700401
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500402 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700403 return NULL;
Jarno Rajahalmed2e269c2017-04-14 14:26:38 -0700404
405 exp = __nf_ct_expect_find(net, zone, &tuple);
406 if (exp) {
407 struct nf_conntrack_tuple_hash *h;
408
409 /* Delete existing conntrack entry, if it clashes with the
410 * expectation. This can happen since conntrack ALGs do not
411 * check for clashes between (new) expectations and existing
412 * conntrack entries. nf_conntrack_in() will check the
413 * expectations only if a conntrack entry can not be found,
414 * which can lead to OVS finding the expectation (here) in the
415 * init direction, but which will not be removed by the
416 * nf_conntrack_in() call, if a matching conntrack entry is
417 * found instead. In this case all init direction packets
418 * would be reported as new related packets, while reply
419 * direction packets would be reported as un-related
420 * established packets.
421 */
422 h = nf_conntrack_find_get(net, zone, &tuple);
423 if (h) {
424 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
425
426 nf_ct_delete(ct, 0, 0);
427 nf_conntrack_put(&ct->ct_general);
428 }
429 }
430
431 return exp;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700432}
433
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800434/* This replicates logic from nf_conntrack_core.c that is not exported. */
435static enum ip_conntrack_info
436ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
437{
438 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
439
440 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
441 return IP_CT_ESTABLISHED_REPLY;
442 /* Once we've had two way comms, always ESTABLISHED. */
443 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
444 return IP_CT_ESTABLISHED;
445 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
446 return IP_CT_RELATED;
447 return IP_CT_NEW;
448}
449
450/* Find an existing connection which this packet belongs to without
451 * re-attributing statistics or modifying the connection state. This allows an
452 * skb->nfct lost due to an upcall to be recovered during actions execution.
453 *
454 * Must be called with rcu_read_lock.
455 *
456 * On success, populates skb->nfct and skb->nfctinfo, and returns the
457 * connection. Returns NULL if there is no existing entry.
458 */
459static struct nf_conn *
460ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
461 u8 l3num, struct sk_buff *skb)
462{
463 struct nf_conntrack_l3proto *l3proto;
464 struct nf_conntrack_l4proto *l4proto;
465 struct nf_conntrack_tuple tuple;
466 struct nf_conntrack_tuple_hash *h;
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800467 struct nf_conn *ct;
468 unsigned int dataoff;
469 u8 protonum;
470
471 l3proto = __nf_ct_l3proto_find(l3num);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800472 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
473 &protonum) <= 0) {
474 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
475 return NULL;
476 }
477 l4proto = __nf_ct_l4proto_find(l3num, protonum);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800478 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
479 protonum, net, &tuple, l3proto, l4proto)) {
480 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
481 return NULL;
482 }
483
484 /* 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 Rajahalme289f2252016-03-10 10:54:20 -0800491 skb->nfct = &ct->ct_general;
Jarno Rajahalmebce91f82016-08-01 19:36:07 -0700492 skb->nfctinfo = ovs_ct_get_info(h);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800493 return ct;
494}
495
Joe Stringer7f8a4362015-08-26 11:31:48 -0700496/* Determine whether skb->nfct is equal to the result of conntrack lookup. */
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800497static bool skb_nfct_cached(struct net *net,
498 const struct sw_flow_key *key,
499 const struct ovs_conntrack_info *info,
500 struct sk_buff *skb)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700501{
502 enum ip_conntrack_info ctinfo;
503 struct nf_conn *ct;
504
505 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800506 /* If no ct, check if we have evidence that an existing conntrack entry
507 * might be found for this skb. This happens when we lose a skb->nfct
508 * due to an upcall. If the connection was not confirmed, it is not
509 * cached and needs to be run through conntrack again.
510 */
511 if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
512 !(key->ct.state & OVS_CS_F_INVALID) &&
513 key->ct.zone == info->zone.id)
514 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700515 if (!ct)
516 return false;
517 if (!net_eq(net, read_pnet(&ct->ct_net)))
518 return false;
519 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
520 return false;
Joe Stringercae3a262015-08-26 11:31:53 -0700521 if (info->helper) {
522 struct nf_conn_help *help;
523
524 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
525 if (help && rcu_access_pointer(help->helper) != info->helper)
526 return false;
527 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700528
529 return true;
530}
531
Jarno Rajahalme05752522016-03-10 10:54:23 -0800532#ifdef CONFIG_NF_NAT_NEEDED
533/* Modelled after nf_nat_ipv[46]_fn().
534 * range is only used for new, uninitialized NAT state.
535 * Returns either NF_ACCEPT or NF_DROP.
536 */
537static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
538 enum ip_conntrack_info ctinfo,
539 const struct nf_nat_range *range,
540 enum nf_nat_manip_type maniptype)
541{
542 int hooknum, nh_off, err = NF_ACCEPT;
543
544 nh_off = skb_network_offset(skb);
Lance Richardson18767ac2017-01-12 19:33:18 -0500545 skb_pull_rcsum(skb, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800546
547 /* See HOOK2MANIP(). */
548 if (maniptype == NF_NAT_MANIP_SRC)
549 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
550 else
551 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
552
553 switch (ctinfo) {
554 case IP_CT_RELATED:
555 case IP_CT_RELATED_REPLY:
Arnd Bergmann99b72482016-03-18 14:33:45 +0100556 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
557 skb->protocol == htons(ETH_P_IP) &&
Jarno Rajahalme05752522016-03-10 10:54:23 -0800558 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
559 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
560 hooknum))
561 err = NF_DROP;
562 goto push;
Arnd Bergmann99b72482016-03-18 14:33:45 +0100563 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
564 skb->protocol == htons(ETH_P_IPV6)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800565 __be16 frag_off;
566 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
567 int hdrlen = ipv6_skip_exthdr(skb,
568 sizeof(struct ipv6hdr),
569 &nexthdr, &frag_off);
570
571 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
572 if (!nf_nat_icmpv6_reply_translation(skb, ct,
573 ctinfo,
574 hooknum,
575 hdrlen))
576 err = NF_DROP;
577 goto push;
578 }
Jarno Rajahalme05752522016-03-10 10:54:23 -0800579 }
580 /* Non-ICMP, fall thru to initialize if needed. */
581 case IP_CT_NEW:
582 /* Seen it before? This can happen for loopback, retrans,
583 * or local packets.
584 */
585 if (!nf_nat_initialized(ct, maniptype)) {
586 /* Initialize according to the NAT action. */
587 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
588 /* Action is set up to establish a new
589 * mapping.
590 */
591 ? nf_nat_setup_info(ct, range, maniptype)
592 : nf_nat_alloc_null_binding(ct, hooknum);
593 if (err != NF_ACCEPT)
594 goto push;
595 }
596 break;
597
598 case IP_CT_ESTABLISHED:
599 case IP_CT_ESTABLISHED_REPLY:
600 break;
601
602 default:
603 err = NF_DROP;
604 goto push;
605 }
606
607 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
608push:
609 skb_push(skb, nh_off);
Lance Richardson18767ac2017-01-12 19:33:18 -0500610 skb_postpush_rcsum(skb, skb->data, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800611
612 return err;
613}
614
615static void ovs_nat_update_key(struct sw_flow_key *key,
616 const struct sk_buff *skb,
617 enum nf_nat_manip_type maniptype)
618{
619 if (maniptype == NF_NAT_MANIP_SRC) {
620 __be16 src;
621
622 key->ct.state |= OVS_CS_F_SRC_NAT;
623 if (key->eth.type == htons(ETH_P_IP))
624 key->ipv4.addr.src = ip_hdr(skb)->saddr;
625 else if (key->eth.type == htons(ETH_P_IPV6))
626 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
627 sizeof(key->ipv6.addr.src));
628 else
629 return;
630
631 if (key->ip.proto == IPPROTO_UDP)
632 src = udp_hdr(skb)->source;
633 else if (key->ip.proto == IPPROTO_TCP)
634 src = tcp_hdr(skb)->source;
635 else if (key->ip.proto == IPPROTO_SCTP)
636 src = sctp_hdr(skb)->source;
637 else
638 return;
639
640 key->tp.src = src;
641 } else {
642 __be16 dst;
643
644 key->ct.state |= OVS_CS_F_DST_NAT;
645 if (key->eth.type == htons(ETH_P_IP))
646 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
647 else if (key->eth.type == htons(ETH_P_IPV6))
648 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
649 sizeof(key->ipv6.addr.dst));
650 else
651 return;
652
653 if (key->ip.proto == IPPROTO_UDP)
654 dst = udp_hdr(skb)->dest;
655 else if (key->ip.proto == IPPROTO_TCP)
656 dst = tcp_hdr(skb)->dest;
657 else if (key->ip.proto == IPPROTO_SCTP)
658 dst = sctp_hdr(skb)->dest;
659 else
660 return;
661
662 key->tp.dst = dst;
663 }
664}
665
666/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
667static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
668 const struct ovs_conntrack_info *info,
669 struct sk_buff *skb, struct nf_conn *ct,
670 enum ip_conntrack_info ctinfo)
671{
672 enum nf_nat_manip_type maniptype;
673 int err;
674
675 if (nf_ct_is_untracked(ct)) {
676 /* A NAT action may only be performed on tracked packets. */
677 return NF_ACCEPT;
678 }
679
680 /* Add NAT extension if not confirmed yet. */
681 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
682 return NF_ACCEPT; /* Can't NAT. */
683
684 /* Determine NAT type.
685 * Check if the NAT type can be deduced from the tracked connection.
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700686 * Make sure new expected connections (IP_CT_RELATED) are NATted only
687 * when committing.
Jarno Rajahalme05752522016-03-10 10:54:23 -0800688 */
689 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
690 ct->status & IPS_NAT_MASK &&
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700691 (ctinfo != IP_CT_RELATED || info->commit)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800692 /* NAT an established or related connection like before. */
693 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
694 /* This is the REPLY direction for a connection
695 * for which NAT was applied in the forward
696 * direction. Do the reverse NAT.
697 */
698 maniptype = ct->status & IPS_SRC_NAT
699 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
700 else
701 maniptype = ct->status & IPS_SRC_NAT
702 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
703 } else if (info->nat & OVS_CT_SRC_NAT) {
704 maniptype = NF_NAT_MANIP_SRC;
705 } else if (info->nat & OVS_CT_DST_NAT) {
706 maniptype = NF_NAT_MANIP_DST;
707 } else {
708 return NF_ACCEPT; /* Connection is not NATed. */
709 }
710 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
711
712 /* Mark NAT done if successful and update the flow key. */
713 if (err == NF_ACCEPT)
714 ovs_nat_update_key(key, skb, maniptype);
715
716 return err;
717}
718#else /* !CONFIG_NF_NAT_NEEDED */
719static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
720 const struct ovs_conntrack_info *info,
721 struct sk_buff *skb, struct nf_conn *ct,
722 enum ip_conntrack_info ctinfo)
723{
724 return NF_ACCEPT;
725}
726#endif
727
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800728/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800729 * not done already. Update key with new CT state after passing the packet
730 * through conntrack.
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800731 * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
732 * set to NULL and 0 will be returned.
733 */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700734static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700735 const struct ovs_conntrack_info *info,
736 struct sk_buff *skb)
737{
738 /* If we are recirculating packets to match on conntrack fields and
739 * committing with a separate conntrack action, then we don't need to
740 * actually run the packet through conntrack twice unless it's for a
741 * different zone.
742 */
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800743 bool cached = skb_nfct_cached(net, key, info, skb);
744 enum ip_conntrack_info ctinfo;
745 struct nf_conn *ct;
746
747 if (!cached) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700748 struct nf_conn *tmpl = info->ct;
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800749 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700750
751 /* Associate skb with specified zone. */
752 if (tmpl) {
753 if (skb->nfct)
754 nf_conntrack_put(skb->nfct);
755 nf_conntrack_get(&tmpl->ct_general);
756 skb->nfct = &tmpl->ct_general;
757 skb->nfctinfo = IP_CT_NEW;
758 }
759
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800760 /* Repeat if requested, see nf_iterate(). */
761 do {
762 err = nf_conntrack_in(net, info->family,
763 NF_INET_PRE_ROUTING, skb);
764 } while (err == NF_REPEAT);
765
766 if (err != NF_ACCEPT)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700767 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700768
Jarno Rajahalme05752522016-03-10 10:54:23 -0800769 /* Clear CT state NAT flags to mark that we have not yet done
770 * NAT after the nf_conntrack_in() call. We can actually clear
771 * the whole state, as it will be re-initialized below.
772 */
773 key->ct.state = 0;
774
775 /* Update the key, but keep the NAT flags. */
776 ovs_ct_update_key(skb, info, key, true, true);
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800777 }
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800778
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800779 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800780 if (ct) {
781 /* Packets starting a new connection must be NATted before the
782 * helper, so that the helper knows about the NAT. We enforce
783 * this by delaying both NAT and helper calls for unconfirmed
784 * connections until the committing CT action. For later
785 * packets NAT and Helper may be called in either order.
786 *
787 * NAT will be done only if the CT action has NAT, and only
788 * once per packet (per zone), as guarded by the NAT bits in
789 * the key->ct.state.
790 */
791 if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
792 (nf_ct_is_confirmed(ct) || info->commit) &&
793 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
794 return -EINVAL;
795 }
796
Joe Stringer16ec3d42016-05-11 10:29:26 -0700797 /* Userspace may decide to perform a ct lookup without a helper
798 * specified followed by a (recirculate and) commit with one.
799 * Therefore, for unconfirmed connections which we will commit,
800 * we need to attach the helper here.
801 */
802 if (!nf_ct_is_confirmed(ct) && info->commit &&
803 info->helper && !nfct_help(ct)) {
804 int err = __nf_ct_try_assign_helper(ct, info->ct,
805 GFP_ATOMIC);
806 if (err)
807 return err;
808 }
809
Jarno Rajahalme05752522016-03-10 10:54:23 -0800810 /* Call the helper only if:
811 * - nf_conntrack_in() was executed above ("!cached") for a
812 * confirmed connection, or
813 * - When committing an unconfirmed connection.
814 */
815 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
816 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
817 return -EINVAL;
818 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700819 }
820
821 return 0;
822}
823
824/* Lookup connection and read fields into key. */
825static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
826 const struct ovs_conntrack_info *info,
827 struct sk_buff *skb)
828{
829 struct nf_conntrack_expect *exp;
830
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800831 /* If we pass an expected packet through nf_conntrack_in() the
832 * expectation is typically removed, but the packet could still be
833 * lost in upcall processing. To prevent this from happening we
834 * perform an explicit expectation lookup. Expected connections are
835 * always new, and will be passed through conntrack only when they are
836 * committed, as it is OK to remove the expectation at that time.
837 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700838 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
839 if (exp) {
840 u8 state;
841
Jarno Rajahalme05752522016-03-10 10:54:23 -0800842 /* NOTE: New connections are NATted and Helped only when
843 * committed, so we are not calling into NAT here.
844 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700845 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700846 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200847 } else {
848 struct nf_conn *ct;
849 int err;
850
851 err = __ovs_ct_lookup(net, key, info, skb);
852 if (err)
853 return err;
854
855 ct = (struct nf_conn *)skb->nfct;
856 if (ct)
857 nf_ct_deliver_cached_events(ct);
858 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700859
860 return 0;
861}
862
Joe Stringer33db4122015-10-01 15:00:37 -0700863static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700864{
865 size_t i;
866
Joe Stringer33db4122015-10-01 15:00:37 -0700867 for (i = 0; i < sizeof(*labels); i++)
868 if (labels->ct_labels[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700869 return true;
870
871 return false;
872}
873
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700874/* Lookup connection and confirm if unconfirmed. */
875static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
876 const struct ovs_conntrack_info *info,
877 struct sk_buff *skb)
878{
879 int err;
880
881 err = __ovs_ct_lookup(net, key, info, skb);
882 if (err)
883 return err;
884
885 /* Apply changes before confirming the connection so that the initial
886 * conntrack NEW netlink event carries the values given in the CT
887 * action.
888 */
889 if (info->mark.mask) {
890 err = ovs_ct_set_mark(skb, key, info->mark.value,
891 info->mark.mask);
892 if (err)
893 return err;
894 }
895 if (labels_nonzero(&info->labels.mask)) {
896 err = ovs_ct_set_labels(skb, key, &info->labels.value,
897 &info->labels.mask);
898 if (err)
899 return err;
900 }
901 /* This will take care of sending queued events even if the connection
902 * is already confirmed.
903 */
904 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
905 return -EINVAL;
906
907 return 0;
908}
909
Ed Swierkbfd188f2018-01-31 18:48:02 -0800910/* Trim the skb to the length specified by the IP/IPv6 header,
911 * removing any trailing lower-layer padding. This prepares the skb
912 * for higher-layer processing that assumes skb->len excludes padding
913 * (such as nf_ip_checksum). The caller needs to pull the skb to the
914 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
915 */
916static int ovs_skb_network_trim(struct sk_buff *skb)
917{
918 unsigned int len;
919 int err;
920
921 switch (skb->protocol) {
922 case htons(ETH_P_IP):
923 len = ntohs(ip_hdr(skb)->tot_len);
924 break;
925 case htons(ETH_P_IPV6):
926 len = sizeof(struct ipv6hdr)
927 + ntohs(ipv6_hdr(skb)->payload_len);
928 break;
929 default:
930 len = skb->len;
931 }
932
933 err = pskb_trim_rcsum(skb, len);
934 if (err)
935 kfree_skb(skb);
936
937 return err;
938}
939
Joe Stringer74c16612015-10-25 20:21:48 -0700940/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
941 * value if 'skb' is freed.
942 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700943int ovs_ct_execute(struct net *net, struct sk_buff *skb,
944 struct sw_flow_key *key,
945 const struct ovs_conntrack_info *info)
946{
947 int nh_ofs;
948 int err;
949
950 /* The conntrack module expects to be working at L3. */
951 nh_ofs = skb_network_offset(skb);
Lance Richardson18767ac2017-01-12 19:33:18 -0500952 skb_pull_rcsum(skb, nh_ofs);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700953
Ed Swierkbfd188f2018-01-31 18:48:02 -0800954 err = ovs_skb_network_trim(skb);
955 if (err)
956 return err;
957
Joe Stringer7f8a4362015-08-26 11:31:48 -0700958 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
959 err = handle_fragments(net, key, info->zone.id, skb);
960 if (err)
961 return err;
962 }
963
Joe Stringerab38a7b2015-10-06 11:00:01 -0700964 if (info->commit)
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700965 err = ovs_ct_commit(net, key, info, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700966 else
967 err = ovs_ct_lookup(net, key, info, skb);
968
969 skb_push(skb, nh_ofs);
Lance Richardson18767ac2017-01-12 19:33:18 -0500970 skb_postpush_rcsum(skb, skb->data, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -0700971 if (err)
972 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700973 return err;
974}
975
Joe Stringercae3a262015-08-26 11:31:53 -0700976static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
977 const struct sw_flow_key *key, bool log)
978{
979 struct nf_conntrack_helper *helper;
980 struct nf_conn_help *help;
981
982 helper = nf_conntrack_helper_try_module_get(name, info->family,
983 key->ip.proto);
984 if (!helper) {
985 OVS_NLERR(log, "Unknown helper \"%s\"", name);
986 return -EINVAL;
987 }
988
989 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
990 if (!help) {
991 module_put(helper->me);
992 return -ENOMEM;
993 }
994
995 rcu_assign_pointer(help->helper, helper);
996 info->helper = helper;
997 return 0;
998}
999
Jarno Rajahalme05752522016-03-10 10:54:23 -08001000#ifdef CONFIG_NF_NAT_NEEDED
1001static int parse_nat(const struct nlattr *attr,
1002 struct ovs_conntrack_info *info, bool log)
1003{
1004 struct nlattr *a;
1005 int rem;
1006 bool have_ip_max = false;
1007 bool have_proto_max = false;
1008 bool ip_vers = (info->family == NFPROTO_IPV6);
1009
1010 nla_for_each_nested(a, attr, rem) {
1011 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1012 [OVS_NAT_ATTR_SRC] = {0, 0},
1013 [OVS_NAT_ATTR_DST] = {0, 0},
1014 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1015 sizeof(struct in6_addr)},
1016 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1017 sizeof(struct in6_addr)},
1018 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1019 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1020 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1021 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1022 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1023 };
1024 int type = nla_type(a);
1025
1026 if (type > OVS_NAT_ATTR_MAX) {
1027 OVS_NLERR(log,
1028 "Unknown NAT attribute (type=%d, max=%d).\n",
1029 type, OVS_NAT_ATTR_MAX);
1030 return -EINVAL;
1031 }
1032
1033 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1034 OVS_NLERR(log,
1035 "NAT attribute type %d has unexpected length (%d != %d).\n",
1036 type, nla_len(a),
1037 ovs_nat_attr_lens[type][ip_vers]);
1038 return -EINVAL;
1039 }
1040
1041 switch (type) {
1042 case OVS_NAT_ATTR_SRC:
1043 case OVS_NAT_ATTR_DST:
1044 if (info->nat) {
1045 OVS_NLERR(log,
1046 "Only one type of NAT may be specified.\n"
1047 );
1048 return -ERANGE;
1049 }
1050 info->nat |= OVS_CT_NAT;
1051 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1052 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1053 break;
1054
1055 case OVS_NAT_ATTR_IP_MIN:
Haishuang Yanac71b462016-03-28 18:08:59 +08001056 nla_memcpy(&info->range.min_addr, a,
1057 sizeof(info->range.min_addr));
Jarno Rajahalme05752522016-03-10 10:54:23 -08001058 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1059 break;
1060
1061 case OVS_NAT_ATTR_IP_MAX:
1062 have_ip_max = true;
1063 nla_memcpy(&info->range.max_addr, a,
1064 sizeof(info->range.max_addr));
1065 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1066 break;
1067
1068 case OVS_NAT_ATTR_PROTO_MIN:
1069 info->range.min_proto.all = htons(nla_get_u16(a));
1070 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1071 break;
1072
1073 case OVS_NAT_ATTR_PROTO_MAX:
1074 have_proto_max = true;
1075 info->range.max_proto.all = htons(nla_get_u16(a));
1076 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1077 break;
1078
1079 case OVS_NAT_ATTR_PERSISTENT:
1080 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1081 break;
1082
1083 case OVS_NAT_ATTR_PROTO_HASH:
1084 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1085 break;
1086
1087 case OVS_NAT_ATTR_PROTO_RANDOM:
1088 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1089 break;
1090
1091 default:
1092 OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
1093 return -EINVAL;
1094 }
1095 }
1096
1097 if (rem > 0) {
1098 OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
1099 return -EINVAL;
1100 }
1101 if (!info->nat) {
1102 /* Do not allow flags if no type is given. */
1103 if (info->range.flags) {
1104 OVS_NLERR(log,
1105 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1106 );
1107 return -EINVAL;
1108 }
1109 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1110 } else if (!info->commit) {
1111 OVS_NLERR(log,
1112 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1113 );
1114 return -EINVAL;
1115 }
1116 /* Allow missing IP_MAX. */
1117 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1118 memcpy(&info->range.max_addr, &info->range.min_addr,
1119 sizeof(info->range.max_addr));
1120 }
1121 /* Allow missing PROTO_MAX. */
1122 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1123 !have_proto_max) {
1124 info->range.max_proto.all = info->range.min_proto.all;
1125 }
1126 return 0;
1127}
1128#endif
1129
Joe Stringer7f8a4362015-08-26 11:31:48 -07001130static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001131 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -07001132 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1133 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -07001134 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1135 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -07001136 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1137 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -07001138 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
Jarno Rajahalme05752522016-03-10 10:54:23 -08001139 .maxlen = NF_CT_HELPER_NAME_LEN },
1140#ifdef CONFIG_NF_NAT_NEEDED
1141 /* NAT length is checked when parsing the nested attributes. */
1142 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1143#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001144};
1145
1146static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -07001147 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001148{
1149 struct nlattr *a;
1150 int rem;
1151
1152 nla_for_each_nested(a, attr, rem) {
1153 int type = nla_type(a);
Liping Zhangd53ff382017-07-23 17:52:23 +08001154 int maxlen;
1155 int minlen;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001156
1157 if (type > OVS_CT_ATTR_MAX) {
1158 OVS_NLERR(log,
1159 "Unknown conntrack attr (type=%d, max=%d)",
1160 type, OVS_CT_ATTR_MAX);
1161 return -EINVAL;
1162 }
Liping Zhangd53ff382017-07-23 17:52:23 +08001163
1164 maxlen = ovs_ct_attr_lens[type].maxlen;
1165 minlen = ovs_ct_attr_lens[type].minlen;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001166 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1167 OVS_NLERR(log,
1168 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1169 type, nla_len(a), maxlen);
1170 return -EINVAL;
1171 }
1172
1173 switch (type) {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001174 case OVS_CT_ATTR_COMMIT:
1175 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001176 break;
1177#ifdef CONFIG_NF_CONNTRACK_ZONES
1178 case OVS_CT_ATTR_ZONE:
1179 info->zone.id = nla_get_u16(a);
1180 break;
1181#endif
Joe Stringer182e3042015-08-26 11:31:49 -07001182#ifdef CONFIG_NF_CONNTRACK_MARK
1183 case OVS_CT_ATTR_MARK: {
1184 struct md_mark *mark = nla_data(a);
1185
Joe Stringere754ec62015-10-19 19:19:00 -07001186 if (!mark->mask) {
1187 OVS_NLERR(log, "ct_mark mask cannot be 0");
1188 return -EINVAL;
1189 }
Joe Stringer182e3042015-08-26 11:31:49 -07001190 info->mark = *mark;
1191 break;
1192 }
1193#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -07001194#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -07001195 case OVS_CT_ATTR_LABELS: {
1196 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001197
Joe Stringere754ec62015-10-19 19:19:00 -07001198 if (!labels_nonzero(&labels->mask)) {
1199 OVS_NLERR(log, "ct_labels mask cannot be 0");
1200 return -EINVAL;
1201 }
Joe Stringer33db4122015-10-01 15:00:37 -07001202 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001203 break;
1204 }
1205#endif
Joe Stringercae3a262015-08-26 11:31:53 -07001206 case OVS_CT_ATTR_HELPER:
1207 *helper = nla_data(a);
1208 if (!memchr(*helper, '\0', nla_len(a))) {
1209 OVS_NLERR(log, "Invalid conntrack helper");
1210 return -EINVAL;
1211 }
1212 break;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001213#ifdef CONFIG_NF_NAT_NEEDED
1214 case OVS_CT_ATTR_NAT: {
1215 int err = parse_nat(a, info, log);
1216
1217 if (err)
1218 return err;
1219 break;
1220 }
1221#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001222 default:
1223 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1224 type);
1225 return -EINVAL;
1226 }
1227 }
1228
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001229#ifdef CONFIG_NF_CONNTRACK_MARK
1230 if (!info->commit && info->mark.mask) {
1231 OVS_NLERR(log,
1232 "Setting conntrack mark requires 'commit' flag.");
1233 return -EINVAL;
1234 }
1235#endif
1236#ifdef CONFIG_NF_CONNTRACK_LABELS
1237 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1238 OVS_NLERR(log,
1239 "Setting conntrack labels requires 'commit' flag.");
1240 return -EINVAL;
1241 }
1242#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001243 if (rem > 0) {
1244 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1245 return -EINVAL;
1246 }
1247
1248 return 0;
1249}
1250
Joe Stringerc2ac6672015-08-26 11:31:52 -07001251bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001252{
1253 if (attr == OVS_KEY_ATTR_CT_STATE)
1254 return true;
1255 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1256 attr == OVS_KEY_ATTR_CT_ZONE)
1257 return true;
Joe Stringer182e3042015-08-26 11:31:49 -07001258 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1259 attr == OVS_KEY_ATTR_CT_MARK)
1260 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001261 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001262 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001263 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1264
1265 return ovs_net->xt_label;
1266 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001267
1268 return false;
1269}
1270
1271int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1272 const struct sw_flow_key *key,
1273 struct sw_flow_actions **sfa, bool log)
1274{
1275 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -07001276 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001277 u16 family;
1278 int err;
1279
1280 family = key_to_nfproto(key);
1281 if (family == NFPROTO_UNSPEC) {
1282 OVS_NLERR(log, "ct family unspecified");
1283 return -EINVAL;
1284 }
1285
1286 memset(&ct_info, 0, sizeof(ct_info));
1287 ct_info.family = family;
1288
1289 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1290 NF_CT_DEFAULT_ZONE_DIR, 0);
1291
Joe Stringercae3a262015-08-26 11:31:53 -07001292 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001293 if (err)
1294 return err;
1295
1296 /* Set up template for tracking connections in specific zones. */
1297 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1298 if (!ct_info.ct) {
1299 OVS_NLERR(log, "Failed to allocate conntrack template");
1300 return -ENOMEM;
1301 }
Joe Stringer90c7afc962015-12-23 14:39:27 -08001302
1303 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1304 nf_conntrack_get(&ct_info.ct->ct_general);
1305
Joe Stringercae3a262015-08-26 11:31:53 -07001306 if (helper) {
1307 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1308 if (err)
1309 goto err_free_ct;
1310 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001311
1312 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1313 sizeof(ct_info), log);
1314 if (err)
1315 goto err_free_ct;
1316
Joe Stringer7f8a4362015-08-26 11:31:48 -07001317 return 0;
1318err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001319 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001320 return err;
1321}
1322
Jarno Rajahalme05752522016-03-10 10:54:23 -08001323#ifdef CONFIG_NF_NAT_NEEDED
1324static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1325 struct sk_buff *skb)
1326{
1327 struct nlattr *start;
1328
1329 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1330 if (!start)
1331 return false;
1332
1333 if (info->nat & OVS_CT_SRC_NAT) {
1334 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1335 return false;
1336 } else if (info->nat & OVS_CT_DST_NAT) {
1337 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1338 return false;
1339 } else {
1340 goto out;
1341 }
1342
1343 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
Arnd Bergmann99b72482016-03-18 14:33:45 +01001344 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1345 info->family == NFPROTO_IPV4) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001346 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1347 info->range.min_addr.ip) ||
1348 (info->range.max_addr.ip
1349 != info->range.min_addr.ip &&
1350 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1351 info->range.max_addr.ip))))
1352 return false;
Arnd Bergmann99b72482016-03-18 14:33:45 +01001353 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1354 info->family == NFPROTO_IPV6) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001355 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1356 &info->range.min_addr.in6) ||
1357 (memcmp(&info->range.max_addr.in6,
1358 &info->range.min_addr.in6,
1359 sizeof(info->range.max_addr.in6)) &&
1360 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1361 &info->range.max_addr.in6))))
1362 return false;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001363 } else {
1364 return false;
1365 }
1366 }
1367 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1368 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1369 ntohs(info->range.min_proto.all)) ||
1370 (info->range.max_proto.all != info->range.min_proto.all &&
1371 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1372 ntohs(info->range.max_proto.all)))))
1373 return false;
1374
1375 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1376 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1377 return false;
1378 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1379 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1380 return false;
1381 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1382 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1383 return false;
1384out:
1385 nla_nest_end(skb, start);
1386
1387 return true;
1388}
1389#endif
1390
Joe Stringer7f8a4362015-08-26 11:31:48 -07001391int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1392 struct sk_buff *skb)
1393{
1394 struct nlattr *start;
1395
1396 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1397 if (!start)
1398 return -EMSGSIZE;
1399
Joe Stringerab38a7b2015-10-06 11:00:01 -07001400 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001401 return -EMSGSIZE;
1402 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1403 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1404 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -07001405 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -07001406 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1407 &ct_info->mark))
1408 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001409 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -07001410 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001411 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1412 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -07001413 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -07001414 if (ct_info->helper) {
1415 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1416 ct_info->helper->name))
1417 return -EMSGSIZE;
1418 }
Jarno Rajahalme05752522016-03-10 10:54:23 -08001419#ifdef CONFIG_NF_NAT_NEEDED
1420 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1421 return -EMSGSIZE;
1422#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001423 nla_nest_end(skb, start);
1424
1425 return 0;
1426}
1427
1428void ovs_ct_free_action(const struct nlattr *a)
1429{
1430 struct ovs_conntrack_info *ct_info = nla_data(a);
1431
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001432 __ovs_ct_free_action(ct_info);
1433}
1434
1435static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1436{
Joe Stringercae3a262015-08-26 11:31:53 -07001437 if (ct_info->helper)
1438 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001439 if (ct_info->ct)
Joe Stringer76644232016-09-01 18:01:47 -07001440 nf_ct_tmpl_free(ct_info->ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001441}
Joe Stringerc2ac6672015-08-26 11:31:52 -07001442
1443void ovs_ct_init(struct net *net)
1444{
Joe Stringer33db4122015-10-01 15:00:37 -07001445 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001446 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1447
Florian Westphaladff6c62016-04-12 18:14:25 +02001448 if (nf_connlabels_get(net, n_bits - 1)) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001449 ovs_net->xt_label = false;
1450 OVS_NLERR(true, "Failed to set connlabel length");
1451 } else {
1452 ovs_net->xt_label = true;
1453 }
1454}
1455
1456void ovs_ct_exit(struct net *net)
1457{
1458 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1459
1460 if (ovs_net->xt_label)
1461 nf_connlabels_put(net);
1462}