blob: cff76ffcaac28f1034458da6ef17e64562f44635 [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
Dumitru Cearac5543f32020-10-07 17:48:03 +0200712 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
713 if (ct->status & IPS_SRC_NAT) {
714 if (maniptype == NF_NAT_MANIP_SRC)
715 maniptype = NF_NAT_MANIP_DST;
716 else
717 maniptype = NF_NAT_MANIP_SRC;
Aaron Conoled1c79f92019-12-03 16:34:13 -0500718
Dumitru Cearac5543f32020-10-07 17:48:03 +0200719 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range,
720 maniptype);
721 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
722 err = ovs_ct_nat_execute(skb, ct, ctinfo, NULL,
723 NF_NAT_MANIP_SRC);
724 }
Aaron Conoled1c79f92019-12-03 16:34:13 -0500725 }
726
Jarno Rajahalme05752522016-03-10 10:54:23 -0800727 /* Mark NAT done if successful and update the flow key. */
728 if (err == NF_ACCEPT)
729 ovs_nat_update_key(key, skb, maniptype);
730
731 return err;
732}
733#else /* !CONFIG_NF_NAT_NEEDED */
734static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
735 const struct ovs_conntrack_info *info,
736 struct sk_buff *skb, struct nf_conn *ct,
737 enum ip_conntrack_info ctinfo)
738{
739 return NF_ACCEPT;
740}
741#endif
742
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800743/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800744 * not done already. Update key with new CT state after passing the packet
745 * through conntrack.
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800746 * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
747 * set to NULL and 0 will be returned.
748 */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700749static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700750 const struct ovs_conntrack_info *info,
751 struct sk_buff *skb)
752{
753 /* If we are recirculating packets to match on conntrack fields and
754 * committing with a separate conntrack action, then we don't need to
755 * actually run the packet through conntrack twice unless it's for a
756 * different zone.
757 */
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800758 bool cached = skb_nfct_cached(net, key, info, skb);
759 enum ip_conntrack_info ctinfo;
760 struct nf_conn *ct;
761
762 if (!cached) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700763 struct nf_conn *tmpl = info->ct;
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800764 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700765
766 /* Associate skb with specified zone. */
767 if (tmpl) {
768 if (skb->nfct)
769 nf_conntrack_put(skb->nfct);
770 nf_conntrack_get(&tmpl->ct_general);
771 skb->nfct = &tmpl->ct_general;
772 skb->nfctinfo = IP_CT_NEW;
773 }
774
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800775 /* Repeat if requested, see nf_iterate(). */
776 do {
777 err = nf_conntrack_in(net, info->family,
778 NF_INET_PRE_ROUTING, skb);
779 } while (err == NF_REPEAT);
780
781 if (err != NF_ACCEPT)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700782 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700783
Jarno Rajahalme05752522016-03-10 10:54:23 -0800784 /* Clear CT state NAT flags to mark that we have not yet done
785 * NAT after the nf_conntrack_in() call. We can actually clear
786 * the whole state, as it will be re-initialized below.
787 */
788 key->ct.state = 0;
789
790 /* Update the key, but keep the NAT flags. */
791 ovs_ct_update_key(skb, info, key, true, true);
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800792 }
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800793
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800794 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800795 if (ct) {
796 /* Packets starting a new connection must be NATted before the
797 * helper, so that the helper knows about the NAT. We enforce
798 * this by delaying both NAT and helper calls for unconfirmed
799 * connections until the committing CT action. For later
800 * packets NAT and Helper may be called in either order.
801 *
802 * NAT will be done only if the CT action has NAT, and only
803 * once per packet (per zone), as guarded by the NAT bits in
804 * the key->ct.state.
805 */
806 if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
807 (nf_ct_is_confirmed(ct) || info->commit) &&
808 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
809 return -EINVAL;
810 }
811
Joe Stringer16ec3d42016-05-11 10:29:26 -0700812 /* Userspace may decide to perform a ct lookup without a helper
813 * specified followed by a (recirculate and) commit with one.
814 * Therefore, for unconfirmed connections which we will commit,
815 * we need to attach the helper here.
816 */
817 if (!nf_ct_is_confirmed(ct) && info->commit &&
818 info->helper && !nfct_help(ct)) {
819 int err = __nf_ct_try_assign_helper(ct, info->ct,
820 GFP_ATOMIC);
821 if (err)
822 return err;
823 }
824
Jarno Rajahalme05752522016-03-10 10:54:23 -0800825 /* Call the helper only if:
826 * - nf_conntrack_in() was executed above ("!cached") for a
827 * confirmed connection, or
828 * - When committing an unconfirmed connection.
829 */
830 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
831 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
832 return -EINVAL;
833 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700834 }
835
836 return 0;
837}
838
839/* Lookup connection and read fields into key. */
840static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
841 const struct ovs_conntrack_info *info,
842 struct sk_buff *skb)
843{
844 struct nf_conntrack_expect *exp;
845
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800846 /* If we pass an expected packet through nf_conntrack_in() the
847 * expectation is typically removed, but the packet could still be
848 * lost in upcall processing. To prevent this from happening we
849 * perform an explicit expectation lookup. Expected connections are
850 * always new, and will be passed through conntrack only when they are
851 * committed, as it is OK to remove the expectation at that time.
852 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700853 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
854 if (exp) {
855 u8 state;
856
Jarno Rajahalme05752522016-03-10 10:54:23 -0800857 /* NOTE: New connections are NATted and Helped only when
858 * committed, so we are not calling into NAT here.
859 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700860 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700861 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200862 } else {
863 struct nf_conn *ct;
864 int err;
865
866 err = __ovs_ct_lookup(net, key, info, skb);
867 if (err)
868 return err;
869
870 ct = (struct nf_conn *)skb->nfct;
871 if (ct)
872 nf_ct_deliver_cached_events(ct);
873 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700874
875 return 0;
876}
877
Joe Stringer33db4122015-10-01 15:00:37 -0700878static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700879{
880 size_t i;
881
Joe Stringer33db4122015-10-01 15:00:37 -0700882 for (i = 0; i < sizeof(*labels); i++)
883 if (labels->ct_labels[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700884 return true;
885
886 return false;
887}
888
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700889/* Lookup connection and confirm if unconfirmed. */
890static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
891 const struct ovs_conntrack_info *info,
892 struct sk_buff *skb)
893{
894 int err;
895
896 err = __ovs_ct_lookup(net, key, info, skb);
897 if (err)
898 return err;
899
900 /* Apply changes before confirming the connection so that the initial
901 * conntrack NEW netlink event carries the values given in the CT
902 * action.
903 */
904 if (info->mark.mask) {
905 err = ovs_ct_set_mark(skb, key, info->mark.value,
906 info->mark.mask);
907 if (err)
908 return err;
909 }
910 if (labels_nonzero(&info->labels.mask)) {
911 err = ovs_ct_set_labels(skb, key, &info->labels.value,
912 &info->labels.mask);
913 if (err)
914 return err;
915 }
916 /* This will take care of sending queued events even if the connection
917 * is already confirmed.
918 */
919 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
920 return -EINVAL;
921
922 return 0;
923}
924
Ed Swierkbfd188f2018-01-31 18:48:02 -0800925/* Trim the skb to the length specified by the IP/IPv6 header,
926 * removing any trailing lower-layer padding. This prepares the skb
927 * for higher-layer processing that assumes skb->len excludes padding
928 * (such as nf_ip_checksum). The caller needs to pull the skb to the
929 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
930 */
931static int ovs_skb_network_trim(struct sk_buff *skb)
932{
933 unsigned int len;
934 int err;
935
936 switch (skb->protocol) {
937 case htons(ETH_P_IP):
938 len = ntohs(ip_hdr(skb)->tot_len);
939 break;
940 case htons(ETH_P_IPV6):
941 len = sizeof(struct ipv6hdr)
942 + ntohs(ipv6_hdr(skb)->payload_len);
943 break;
944 default:
945 len = skb->len;
946 }
947
948 err = pskb_trim_rcsum(skb, len);
949 if (err)
950 kfree_skb(skb);
951
952 return err;
953}
954
Joe Stringer74c16612015-10-25 20:21:48 -0700955/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
956 * value if 'skb' is freed.
957 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700958int ovs_ct_execute(struct net *net, struct sk_buff *skb,
959 struct sw_flow_key *key,
960 const struct ovs_conntrack_info *info)
961{
962 int nh_ofs;
963 int err;
964
965 /* The conntrack module expects to be working at L3. */
966 nh_ofs = skb_network_offset(skb);
Lance Richardson18767ac2017-01-12 19:33:18 -0500967 skb_pull_rcsum(skb, nh_ofs);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700968
Ed Swierkbfd188f2018-01-31 18:48:02 -0800969 err = ovs_skb_network_trim(skb);
970 if (err)
971 return err;
972
Joe Stringer7f8a4362015-08-26 11:31:48 -0700973 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
974 err = handle_fragments(net, key, info->zone.id, skb);
975 if (err)
976 return err;
977 }
978
Joe Stringerab38a7b2015-10-06 11:00:01 -0700979 if (info->commit)
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700980 err = ovs_ct_commit(net, key, info, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700981 else
982 err = ovs_ct_lookup(net, key, info, skb);
983
984 skb_push(skb, nh_ofs);
Lance Richardson18767ac2017-01-12 19:33:18 -0500985 skb_postpush_rcsum(skb, skb->data, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -0700986 if (err)
987 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700988 return err;
989}
990
Joe Stringercae3a262015-08-26 11:31:53 -0700991static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
992 const struct sw_flow_key *key, bool log)
993{
994 struct nf_conntrack_helper *helper;
995 struct nf_conn_help *help;
996
997 helper = nf_conntrack_helper_try_module_get(name, info->family,
998 key->ip.proto);
999 if (!helper) {
1000 OVS_NLERR(log, "Unknown helper \"%s\"", name);
1001 return -EINVAL;
1002 }
1003
1004 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
1005 if (!help) {
1006 module_put(helper->me);
1007 return -ENOMEM;
1008 }
1009
1010 rcu_assign_pointer(help->helper, helper);
1011 info->helper = helper;
1012 return 0;
1013}
1014
Jarno Rajahalme05752522016-03-10 10:54:23 -08001015#ifdef CONFIG_NF_NAT_NEEDED
1016static int parse_nat(const struct nlattr *attr,
1017 struct ovs_conntrack_info *info, bool log)
1018{
1019 struct nlattr *a;
1020 int rem;
1021 bool have_ip_max = false;
1022 bool have_proto_max = false;
1023 bool ip_vers = (info->family == NFPROTO_IPV6);
1024
1025 nla_for_each_nested(a, attr, rem) {
1026 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1027 [OVS_NAT_ATTR_SRC] = {0, 0},
1028 [OVS_NAT_ATTR_DST] = {0, 0},
1029 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1030 sizeof(struct in6_addr)},
1031 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1032 sizeof(struct in6_addr)},
1033 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1034 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1035 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1036 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1037 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1038 };
1039 int type = nla_type(a);
1040
1041 if (type > OVS_NAT_ATTR_MAX) {
1042 OVS_NLERR(log,
1043 "Unknown NAT attribute (type=%d, max=%d).\n",
1044 type, OVS_NAT_ATTR_MAX);
1045 return -EINVAL;
1046 }
1047
1048 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1049 OVS_NLERR(log,
1050 "NAT attribute type %d has unexpected length (%d != %d).\n",
1051 type, nla_len(a),
1052 ovs_nat_attr_lens[type][ip_vers]);
1053 return -EINVAL;
1054 }
1055
1056 switch (type) {
1057 case OVS_NAT_ATTR_SRC:
1058 case OVS_NAT_ATTR_DST:
1059 if (info->nat) {
1060 OVS_NLERR(log,
1061 "Only one type of NAT may be specified.\n"
1062 );
1063 return -ERANGE;
1064 }
1065 info->nat |= OVS_CT_NAT;
1066 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1067 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1068 break;
1069
1070 case OVS_NAT_ATTR_IP_MIN:
Haishuang Yanac71b462016-03-28 18:08:59 +08001071 nla_memcpy(&info->range.min_addr, a,
1072 sizeof(info->range.min_addr));
Jarno Rajahalme05752522016-03-10 10:54:23 -08001073 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1074 break;
1075
1076 case OVS_NAT_ATTR_IP_MAX:
1077 have_ip_max = true;
1078 nla_memcpy(&info->range.max_addr, a,
1079 sizeof(info->range.max_addr));
1080 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1081 break;
1082
1083 case OVS_NAT_ATTR_PROTO_MIN:
1084 info->range.min_proto.all = htons(nla_get_u16(a));
1085 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1086 break;
1087
1088 case OVS_NAT_ATTR_PROTO_MAX:
1089 have_proto_max = true;
1090 info->range.max_proto.all = htons(nla_get_u16(a));
1091 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1092 break;
1093
1094 case OVS_NAT_ATTR_PERSISTENT:
1095 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1096 break;
1097
1098 case OVS_NAT_ATTR_PROTO_HASH:
1099 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1100 break;
1101
1102 case OVS_NAT_ATTR_PROTO_RANDOM:
1103 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1104 break;
1105
1106 default:
1107 OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
1108 return -EINVAL;
1109 }
1110 }
1111
1112 if (rem > 0) {
1113 OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
1114 return -EINVAL;
1115 }
1116 if (!info->nat) {
1117 /* Do not allow flags if no type is given. */
1118 if (info->range.flags) {
1119 OVS_NLERR(log,
1120 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1121 );
1122 return -EINVAL;
1123 }
1124 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1125 } else if (!info->commit) {
1126 OVS_NLERR(log,
1127 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1128 );
1129 return -EINVAL;
1130 }
1131 /* Allow missing IP_MAX. */
1132 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1133 memcpy(&info->range.max_addr, &info->range.min_addr,
1134 sizeof(info->range.max_addr));
1135 }
1136 /* Allow missing PROTO_MAX. */
1137 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1138 !have_proto_max) {
1139 info->range.max_proto.all = info->range.min_proto.all;
1140 }
1141 return 0;
1142}
1143#endif
1144
Joe Stringer7f8a4362015-08-26 11:31:48 -07001145static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001146 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -07001147 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1148 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -07001149 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1150 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -07001151 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1152 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -07001153 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
Jarno Rajahalme05752522016-03-10 10:54:23 -08001154 .maxlen = NF_CT_HELPER_NAME_LEN },
1155#ifdef CONFIG_NF_NAT_NEEDED
1156 /* NAT length is checked when parsing the nested attributes. */
1157 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1158#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001159};
1160
1161static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -07001162 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001163{
1164 struct nlattr *a;
1165 int rem;
1166
1167 nla_for_each_nested(a, attr, rem) {
1168 int type = nla_type(a);
Liping Zhangd53ff382017-07-23 17:52:23 +08001169 int maxlen;
1170 int minlen;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001171
1172 if (type > OVS_CT_ATTR_MAX) {
1173 OVS_NLERR(log,
1174 "Unknown conntrack attr (type=%d, max=%d)",
1175 type, OVS_CT_ATTR_MAX);
1176 return -EINVAL;
1177 }
Liping Zhangd53ff382017-07-23 17:52:23 +08001178
1179 maxlen = ovs_ct_attr_lens[type].maxlen;
1180 minlen = ovs_ct_attr_lens[type].minlen;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001181 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1182 OVS_NLERR(log,
1183 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1184 type, nla_len(a), maxlen);
1185 return -EINVAL;
1186 }
1187
1188 switch (type) {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001189 case OVS_CT_ATTR_COMMIT:
1190 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001191 break;
1192#ifdef CONFIG_NF_CONNTRACK_ZONES
1193 case OVS_CT_ATTR_ZONE:
1194 info->zone.id = nla_get_u16(a);
1195 break;
1196#endif
Joe Stringer182e3042015-08-26 11:31:49 -07001197#ifdef CONFIG_NF_CONNTRACK_MARK
1198 case OVS_CT_ATTR_MARK: {
1199 struct md_mark *mark = nla_data(a);
1200
Joe Stringere754ec62015-10-19 19:19:00 -07001201 if (!mark->mask) {
1202 OVS_NLERR(log, "ct_mark mask cannot be 0");
1203 return -EINVAL;
1204 }
Joe Stringer182e3042015-08-26 11:31:49 -07001205 info->mark = *mark;
1206 break;
1207 }
1208#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -07001209#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -07001210 case OVS_CT_ATTR_LABELS: {
1211 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001212
Joe Stringere754ec62015-10-19 19:19:00 -07001213 if (!labels_nonzero(&labels->mask)) {
1214 OVS_NLERR(log, "ct_labels mask cannot be 0");
1215 return -EINVAL;
1216 }
Joe Stringer33db4122015-10-01 15:00:37 -07001217 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001218 break;
1219 }
1220#endif
Joe Stringercae3a262015-08-26 11:31:53 -07001221 case OVS_CT_ATTR_HELPER:
1222 *helper = nla_data(a);
1223 if (!memchr(*helper, '\0', nla_len(a))) {
1224 OVS_NLERR(log, "Invalid conntrack helper");
1225 return -EINVAL;
1226 }
1227 break;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001228#ifdef CONFIG_NF_NAT_NEEDED
1229 case OVS_CT_ATTR_NAT: {
1230 int err = parse_nat(a, info, log);
1231
1232 if (err)
1233 return err;
1234 break;
1235 }
1236#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001237 default:
1238 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1239 type);
1240 return -EINVAL;
1241 }
1242 }
1243
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001244#ifdef CONFIG_NF_CONNTRACK_MARK
1245 if (!info->commit && info->mark.mask) {
1246 OVS_NLERR(log,
1247 "Setting conntrack mark requires 'commit' flag.");
1248 return -EINVAL;
1249 }
1250#endif
1251#ifdef CONFIG_NF_CONNTRACK_LABELS
1252 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1253 OVS_NLERR(log,
1254 "Setting conntrack labels requires 'commit' flag.");
1255 return -EINVAL;
1256 }
1257#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001258 if (rem > 0) {
1259 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1260 return -EINVAL;
1261 }
1262
1263 return 0;
1264}
1265
Joe Stringerc2ac6672015-08-26 11:31:52 -07001266bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001267{
1268 if (attr == OVS_KEY_ATTR_CT_STATE)
1269 return true;
1270 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1271 attr == OVS_KEY_ATTR_CT_ZONE)
1272 return true;
Joe Stringer182e3042015-08-26 11:31:49 -07001273 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1274 attr == OVS_KEY_ATTR_CT_MARK)
1275 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001276 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001277 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001278 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1279
1280 return ovs_net->xt_label;
1281 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001282
1283 return false;
1284}
1285
1286int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1287 const struct sw_flow_key *key,
1288 struct sw_flow_actions **sfa, bool log)
1289{
1290 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -07001291 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001292 u16 family;
1293 int err;
1294
1295 family = key_to_nfproto(key);
1296 if (family == NFPROTO_UNSPEC) {
1297 OVS_NLERR(log, "ct family unspecified");
1298 return -EINVAL;
1299 }
1300
1301 memset(&ct_info, 0, sizeof(ct_info));
1302 ct_info.family = family;
1303
1304 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1305 NF_CT_DEFAULT_ZONE_DIR, 0);
1306
Joe Stringercae3a262015-08-26 11:31:53 -07001307 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001308 if (err)
1309 return err;
1310
1311 /* Set up template for tracking connections in specific zones. */
1312 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1313 if (!ct_info.ct) {
1314 OVS_NLERR(log, "Failed to allocate conntrack template");
1315 return -ENOMEM;
1316 }
Joe Stringer90c7afc962015-12-23 14:39:27 -08001317
1318 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1319 nf_conntrack_get(&ct_info.ct->ct_general);
1320
Joe Stringercae3a262015-08-26 11:31:53 -07001321 if (helper) {
1322 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1323 if (err)
1324 goto err_free_ct;
1325 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001326
1327 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1328 sizeof(ct_info), log);
1329 if (err)
1330 goto err_free_ct;
1331
Joe Stringer7f8a4362015-08-26 11:31:48 -07001332 return 0;
1333err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001334 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001335 return err;
1336}
1337
Jarno Rajahalme05752522016-03-10 10:54:23 -08001338#ifdef CONFIG_NF_NAT_NEEDED
1339static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1340 struct sk_buff *skb)
1341{
1342 struct nlattr *start;
1343
1344 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1345 if (!start)
1346 return false;
1347
1348 if (info->nat & OVS_CT_SRC_NAT) {
1349 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1350 return false;
1351 } else if (info->nat & OVS_CT_DST_NAT) {
1352 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1353 return false;
1354 } else {
1355 goto out;
1356 }
1357
1358 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
Arnd Bergmann99b72482016-03-18 14:33:45 +01001359 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1360 info->family == NFPROTO_IPV4) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001361 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1362 info->range.min_addr.ip) ||
1363 (info->range.max_addr.ip
1364 != info->range.min_addr.ip &&
1365 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1366 info->range.max_addr.ip))))
1367 return false;
Arnd Bergmann99b72482016-03-18 14:33:45 +01001368 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1369 info->family == NFPROTO_IPV6) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001370 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1371 &info->range.min_addr.in6) ||
1372 (memcmp(&info->range.max_addr.in6,
1373 &info->range.min_addr.in6,
1374 sizeof(info->range.max_addr.in6)) &&
1375 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1376 &info->range.max_addr.in6))))
1377 return false;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001378 } else {
1379 return false;
1380 }
1381 }
1382 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1383 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1384 ntohs(info->range.min_proto.all)) ||
1385 (info->range.max_proto.all != info->range.min_proto.all &&
1386 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1387 ntohs(info->range.max_proto.all)))))
1388 return false;
1389
1390 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1391 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1392 return false;
1393 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1394 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1395 return false;
1396 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1397 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1398 return false;
1399out:
1400 nla_nest_end(skb, start);
1401
1402 return true;
1403}
1404#endif
1405
Joe Stringer7f8a4362015-08-26 11:31:48 -07001406int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1407 struct sk_buff *skb)
1408{
1409 struct nlattr *start;
1410
1411 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1412 if (!start)
1413 return -EMSGSIZE;
1414
Joe Stringerab38a7b2015-10-06 11:00:01 -07001415 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001416 return -EMSGSIZE;
1417 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1418 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1419 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -07001420 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -07001421 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1422 &ct_info->mark))
1423 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001424 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -07001425 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001426 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1427 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -07001428 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -07001429 if (ct_info->helper) {
1430 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1431 ct_info->helper->name))
1432 return -EMSGSIZE;
1433 }
Jarno Rajahalme05752522016-03-10 10:54:23 -08001434#ifdef CONFIG_NF_NAT_NEEDED
1435 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1436 return -EMSGSIZE;
1437#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001438 nla_nest_end(skb, start);
1439
1440 return 0;
1441}
1442
1443void ovs_ct_free_action(const struct nlattr *a)
1444{
1445 struct ovs_conntrack_info *ct_info = nla_data(a);
1446
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001447 __ovs_ct_free_action(ct_info);
1448}
1449
1450static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1451{
Joe Stringercae3a262015-08-26 11:31:53 -07001452 if (ct_info->helper)
1453 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001454 if (ct_info->ct)
Joe Stringer76644232016-09-01 18:01:47 -07001455 nf_ct_tmpl_free(ct_info->ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001456}
Joe Stringerc2ac6672015-08-26 11:31:52 -07001457
1458void ovs_ct_init(struct net *net)
1459{
Joe Stringer33db4122015-10-01 15:00:37 -07001460 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001461 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1462
Florian Westphaladff6c62016-04-12 18:14:25 +02001463 if (nf_connlabels_get(net, n_bits - 1)) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001464 ovs_net->xt_label = false;
1465 OVS_NLERR(true, "Failed to set connlabel length");
1466 } else {
1467 ovs_net->xt_label = true;
1468 }
1469}
1470
1471void ovs_ct_exit(struct net *net)
1472{
1473 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1474
1475 if (ovs_net->xt_label)
1476 nf_connlabels_put(net);
1477}