blob: 7c5bb98c22c61681f91571fd3a2d776259dbcca3 [file] [log] [blame]
Joe Stringer7f8a4362015-08-26 11:31:48 -07001/*
2 * Copyright (c) 2015 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13
14#include <linux/module.h>
15#include <linux/openvswitch.h>
Jarno Rajahalme05752522016-03-10 10:54:23 -080016#include <linux/tcp.h>
17#include <linux/udp.h>
18#include <linux/sctp.h>
Joe Stringer7f8a4362015-08-26 11:31:48 -070019#include <net/ip.h>
20#include <net/netfilter/nf_conntrack_core.h>
Joe Stringercae3a262015-08-26 11:31:53 -070021#include <net/netfilter/nf_conntrack_helper.h>
Joe Stringerc2ac6672015-08-26 11:31:52 -070022#include <net/netfilter/nf_conntrack_labels.h>
Jarno Rajahalme05752522016-03-10 10:54:23 -080023#include <net/netfilter/nf_conntrack_seqadj.h>
Joe Stringer7f8a4362015-08-26 11:31:48 -070024#include <net/netfilter/nf_conntrack_zones.h>
25#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
26
Jarno Rajahalme05752522016-03-10 10:54:23 -080027#ifdef CONFIG_NF_NAT_NEEDED
28#include <linux/netfilter/nf_nat.h>
29#include <net/netfilter/nf_nat_core.h>
30#include <net/netfilter/nf_nat_l3proto.h>
31#endif
32
Joe Stringer7f8a4362015-08-26 11:31:48 -070033#include "datapath.h"
34#include "conntrack.h"
35#include "flow.h"
36#include "flow_netlink.h"
37
38struct ovs_ct_len_tbl {
Jarno Rajahalme05752522016-03-10 10:54:23 -080039 int maxlen;
40 int minlen;
Joe Stringer7f8a4362015-08-26 11:31:48 -070041};
42
Joe Stringer182e3042015-08-26 11:31:49 -070043/* Metadata mark for masked write to conntrack mark */
44struct md_mark {
45 u32 value;
46 u32 mask;
47};
48
Joe Stringerc2ac6672015-08-26 11:31:52 -070049/* Metadata label for masked write to conntrack label. */
Joe Stringer33db4122015-10-01 15:00:37 -070050struct md_labels {
51 struct ovs_key_ct_labels value;
52 struct ovs_key_ct_labels mask;
Joe Stringerc2ac6672015-08-26 11:31:52 -070053};
54
Jarno Rajahalme05752522016-03-10 10:54:23 -080055enum ovs_ct_nat {
56 OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
57 OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
58 OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
59};
60
Joe Stringer7f8a4362015-08-26 11:31:48 -070061/* Conntrack action context for execution. */
62struct ovs_conntrack_info {
Joe Stringercae3a262015-08-26 11:31:53 -070063 struct nf_conntrack_helper *helper;
Joe Stringer7f8a4362015-08-26 11:31:48 -070064 struct nf_conntrack_zone zone;
65 struct nf_conn *ct;
Joe Stringerab38a7b2015-10-06 11:00:01 -070066 u8 commit : 1;
Jarno Rajahalme05752522016-03-10 10:54:23 -080067 u8 nat : 3; /* enum ovs_ct_nat */
Joe Stringer7f8a4362015-08-26 11:31:48 -070068 u16 family;
Joe Stringer182e3042015-08-26 11:31:49 -070069 struct md_mark mark;
Joe Stringer33db4122015-10-01 15:00:37 -070070 struct md_labels labels;
Jarno Rajahalme05752522016-03-10 10:54:23 -080071#ifdef CONFIG_NF_NAT_NEEDED
72 struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */
73#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -070074};
75
Joe Stringer2f3ab9f2015-12-09 14:07:39 -080076static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
77
Joe Stringer7f8a4362015-08-26 11:31:48 -070078static u16 key_to_nfproto(const struct sw_flow_key *key)
79{
80 switch (ntohs(key->eth.type)) {
81 case ETH_P_IP:
82 return NFPROTO_IPV4;
83 case ETH_P_IPV6:
84 return NFPROTO_IPV6;
85 default:
86 return NFPROTO_UNSPEC;
87 }
88}
89
90/* Map SKB connection state into the values used by flow definition. */
91static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
92{
93 u8 ct_state = OVS_CS_F_TRACKED;
94
95 switch (ctinfo) {
96 case IP_CT_ESTABLISHED_REPLY:
97 case IP_CT_RELATED_REPLY:
Joe Stringer7f8a4362015-08-26 11:31:48 -070098 ct_state |= OVS_CS_F_REPLY_DIR;
99 break;
100 default:
101 break;
102 }
103
104 switch (ctinfo) {
105 case IP_CT_ESTABLISHED:
106 case IP_CT_ESTABLISHED_REPLY:
107 ct_state |= OVS_CS_F_ESTABLISHED;
108 break;
109 case IP_CT_RELATED:
110 case IP_CT_RELATED_REPLY:
111 ct_state |= OVS_CS_F_RELATED;
112 break;
113 case IP_CT_NEW:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700114 ct_state |= OVS_CS_F_NEW;
115 break;
116 default:
117 break;
118 }
119
120 return ct_state;
121}
122
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700123static u32 ovs_ct_get_mark(const struct nf_conn *ct)
124{
125#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
126 return ct ? ct->mark : 0;
127#else
128 return 0;
129#endif
130}
131
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800132/* Guard against conntrack labels max size shrinking below 128 bits. */
133#if NF_CT_LABELS_MAX_SIZE < 16
134#error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
135#endif
136
Joe Stringer33db4122015-10-01 15:00:37 -0700137static void ovs_ct_get_labels(const struct nf_conn *ct,
138 struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700139{
140 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
141
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800142 if (cl)
143 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
144 else
Joe Stringer33db4122015-10-01 15:00:37 -0700145 memset(labels, 0, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700146}
147
Joe Stringer7f8a4362015-08-26 11:31:48 -0700148static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
Joe Stringer182e3042015-08-26 11:31:49 -0700149 const struct nf_conntrack_zone *zone,
150 const struct nf_conn *ct)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700151{
152 key->ct.state = state;
153 key->ct.zone = zone->id;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700154 key->ct.mark = ovs_ct_get_mark(ct);
Joe Stringer33db4122015-10-01 15:00:37 -0700155 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700156}
157
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800158/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
Jarno Rajahalme05752522016-03-10 10:54:23 -0800159 * previously sent the packet to conntrack via the ct action. If
160 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
161 * initialized from the connection status.
Joe Stringer7f8a4362015-08-26 11:31:48 -0700162 */
163static void ovs_ct_update_key(const struct sk_buff *skb,
Joe Stringerd1109862015-12-09 14:07:40 -0800164 const struct ovs_conntrack_info *info,
Jarno Rajahalme05752522016-03-10 10:54:23 -0800165 struct sw_flow_key *key, bool post_ct,
166 bool keep_nat_flags)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700167{
168 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
169 enum ip_conntrack_info ctinfo;
170 struct nf_conn *ct;
171 u8 state = 0;
172
173 ct = nf_ct_get(skb, &ctinfo);
174 if (ct) {
175 state = ovs_ct_get_state(ctinfo);
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800176 /* All unconfirmed entries are NEW connections. */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700177 if (!nf_ct_is_confirmed(ct))
178 state |= OVS_CS_F_NEW;
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800179 /* OVS persists the related flag for the duration of the
180 * connection.
181 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700182 if (ct->master)
183 state |= OVS_CS_F_RELATED;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800184 if (keep_nat_flags) {
185 state |= key->ct.state & OVS_CS_F_NAT_MASK;
186 } else {
187 if (ct->status & IPS_SRC_NAT)
188 state |= OVS_CS_F_SRC_NAT;
189 if (ct->status & IPS_DST_NAT)
190 state |= OVS_CS_F_DST_NAT;
191 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700192 zone = nf_ct_zone(ct);
193 } else if (post_ct) {
194 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
Joe Stringerd1109862015-12-09 14:07:40 -0800195 if (info)
196 zone = &info->zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700197 }
Joe Stringer182e3042015-08-26 11:31:49 -0700198 __ovs_ct_update_key(key, state, zone, ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700199}
200
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800201/* This is called to initialize CT key fields possibly coming in from the local
202 * stack.
203 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700204void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
205{
Jarno Rajahalme05752522016-03-10 10:54:23 -0800206 ovs_ct_update_key(skb, NULL, key, false, false);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700207}
208
209int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
210{
Joe Stringerfbccce52015-10-06 11:00:00 -0700211 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700212 return -EMSGSIZE;
213
214 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
215 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
216 return -EMSGSIZE;
217
Joe Stringer182e3042015-08-26 11:31:49 -0700218 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
219 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
220 return -EMSGSIZE;
221
Valentin Rothberg9723e6a2015-08-28 10:39:56 +0200222 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700223 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
224 &key->ct.labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700225 return -EMSGSIZE;
226
Joe Stringer182e3042015-08-26 11:31:49 -0700227 return 0;
228}
229
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800230static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
Joe Stringer182e3042015-08-26 11:31:49 -0700231 u32 ct_mark, u32 mask)
232{
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700233#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
Joe Stringer182e3042015-08-26 11:31:49 -0700234 u32 new_mark;
235
Joe Stringer182e3042015-08-26 11:31:49 -0700236 new_mark = ct_mark | (ct->mark & ~(mask));
237 if (ct->mark != new_mark) {
238 ct->mark = new_mark;
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800239 if (nf_ct_is_confirmed(ct))
240 nf_conntrack_event_cache(IPCT_MARK, ct);
Joe Stringer182e3042015-08-26 11:31:49 -0700241 key->ct.mark = new_mark;
242 }
243
Joe Stringer7f8a4362015-08-26 11:31:48 -0700244 return 0;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700245#else
246 return -ENOTSUPP;
247#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -0700248}
249
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800250static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700251{
Joe Stringerc2ac6672015-08-26 11:31:52 -0700252 struct nf_conn_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700253
254 cl = nf_ct_labels_find(ct);
255 if (!cl) {
256 nf_ct_labels_ext_add(ct);
257 cl = nf_ct_labels_find(ct);
258 }
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800259
260 return cl;
261}
262
263/* Initialize labels for a new, yet to be committed conntrack entry. Note that
264 * since the new connection is not yet confirmed, and thus no-one else has
265 * access to it's labels, we simply write them over. Also, we refrain from
266 * triggering events, as receiving change events before the create event would
267 * be confusing.
268 */
269static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
270 const struct ovs_key_ct_labels *labels,
271 const struct ovs_key_ct_labels *mask)
272{
273 struct nf_conn_labels *cl;
274 u32 *dst;
275 int i;
276
277 cl = ovs_ct_get_conn_labels(ct);
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800278 if (!cl)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700279 return -ENOSPC;
280
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800281 dst = (u32 *)cl->bits;
282 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
283 dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
284 (labels->ct_labels_32[i] & mask->ct_labels_32[i]);
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800285
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800286 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700287
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800288 return 0;
289}
290
291static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
292 const struct ovs_key_ct_labels *labels,
293 const struct ovs_key_ct_labels *mask)
294{
295 struct nf_conn_labels *cl;
296 int err;
297
298 cl = ovs_ct_get_conn_labels(ct);
299 if (!cl)
300 return -ENOSPC;
301
302 err = nf_connlabels_replace(ct, labels->ct_labels_32,
303 mask->ct_labels_32,
304 OVS_CT_LABELS_LEN_32);
305 if (err)
306 return err;
307
308 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
309
Joe Stringerc2ac6672015-08-26 11:31:52 -0700310 return 0;
311}
312
Joe Stringercae3a262015-08-26 11:31:53 -0700313/* 'skb' should already be pulled to nh_ofs. */
314static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
315{
316 const struct nf_conntrack_helper *helper;
317 const struct nf_conn_help *help;
318 enum ip_conntrack_info ctinfo;
319 unsigned int protoff;
320 struct nf_conn *ct;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800321 int err;
Joe Stringercae3a262015-08-26 11:31:53 -0700322
323 ct = nf_ct_get(skb, &ctinfo);
324 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
325 return NF_ACCEPT;
326
327 help = nfct_help(ct);
328 if (!help)
329 return NF_ACCEPT;
330
331 helper = rcu_dereference(help->helper);
332 if (!helper)
333 return NF_ACCEPT;
334
335 switch (proto) {
336 case NFPROTO_IPV4:
337 protoff = ip_hdrlen(skb);
338 break;
339 case NFPROTO_IPV6: {
340 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
341 __be16 frag_off;
Joe Stringercc570602015-09-14 11:14:50 -0700342 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700343
Joe Stringercc570602015-09-14 11:14:50 -0700344 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
345 &frag_off);
346 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700347 pr_debug("proto header not found\n");
348 return NF_ACCEPT;
349 }
Joe Stringercc570602015-09-14 11:14:50 -0700350 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700351 break;
352 }
353 default:
354 WARN_ONCE(1, "helper invoked on non-IP family!");
355 return NF_DROP;
356 }
357
Jarno Rajahalme05752522016-03-10 10:54:23 -0800358 err = helper->help(skb, protoff, ct, ctinfo);
359 if (err != NF_ACCEPT)
360 return err;
361
362 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
363 * FTP with NAT) adusting the TCP payload size when mangling IP
364 * addresses and/or port numbers in the text-based control connection.
365 */
366 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
367 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
368 return NF_DROP;
369 return NF_ACCEPT;
Joe Stringercae3a262015-08-26 11:31:53 -0700370}
371
Joe Stringer74c16612015-10-25 20:21:48 -0700372/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
373 * value if 'skb' is freed.
374 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700375static int handle_fragments(struct net *net, struct sw_flow_key *key,
376 u16 zone, struct sk_buff *skb)
377{
378 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100379 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700380
381 if (key->eth.type == htons(ETH_P_IP)) {
382 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700383
384 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500385 err = ip_defrag(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700386 if (err)
387 return err;
388
389 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700390#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700391 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700392 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700393
Joe Stringer49e261a2016-04-18 14:51:47 -0700394 skb_orphan(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700395 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100396 err = nf_ct_frag6_gather(net, skb, user);
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800397 if (err) {
398 if (err != -EINPROGRESS)
399 kfree_skb(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100400 return err;
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800401 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700402
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100403 key->ip.proto = ipv6_hdr(skb)->nexthdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700404 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700405#endif
406 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700407 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700408 return -EPFNOSUPPORT;
409 }
410
411 key->ip.frag = OVS_FRAG_TYPE_NONE;
412 skb_clear_hash(skb);
413 skb->ignore_df = 1;
414 *OVS_CB(skb) = ovs_cb;
415
416 return 0;
417}
418
419static struct nf_conntrack_expect *
420ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
421 u16 proto, const struct sk_buff *skb)
422{
423 struct nf_conntrack_tuple tuple;
424
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500425 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700426 return NULL;
427 return __nf_ct_expect_find(net, zone, &tuple);
428}
429
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800430/* This replicates logic from nf_conntrack_core.c that is not exported. */
431static enum ip_conntrack_info
432ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
433{
434 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
435
436 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
437 return IP_CT_ESTABLISHED_REPLY;
438 /* Once we've had two way comms, always ESTABLISHED. */
439 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
440 return IP_CT_ESTABLISHED;
441 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
442 return IP_CT_RELATED;
443 return IP_CT_NEW;
444}
445
446/* Find an existing connection which this packet belongs to without
447 * re-attributing statistics or modifying the connection state. This allows an
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800448 * skb->_nfct lost due to an upcall to be recovered during actions execution.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800449 *
450 * Must be called with rcu_read_lock.
451 *
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800452 * On success, populates skb->_nfct and returns the connection. Returns NULL
453 * if there is no existing entry.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800454 */
455static struct nf_conn *
456ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800457 u8 l3num, struct sk_buff *skb, bool natted)
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800458{
459 struct nf_conntrack_l3proto *l3proto;
460 struct nf_conntrack_l4proto *l4proto;
461 struct nf_conntrack_tuple tuple;
462 struct nf_conntrack_tuple_hash *h;
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800463 struct nf_conn *ct;
464 unsigned int dataoff;
465 u8 protonum;
466
467 l3proto = __nf_ct_l3proto_find(l3num);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800468 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
469 &protonum) <= 0) {
470 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
471 return NULL;
472 }
473 l4proto = __nf_ct_l4proto_find(l3num, protonum);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800474 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
475 protonum, net, &tuple, l3proto, l4proto)) {
476 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
477 return NULL;
478 }
479
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800480 /* Must invert the tuple if skb has been transformed by NAT. */
481 if (natted) {
482 struct nf_conntrack_tuple inverse;
483
484 if (!nf_ct_invert_tuple(&inverse, &tuple, l3proto, l4proto)) {
485 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
486 return NULL;
487 }
488 tuple = inverse;
489 }
490
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800491 /* look for tuple match */
492 h = nf_conntrack_find_get(net, zone, &tuple);
493 if (!h)
494 return NULL; /* Not found. */
495
496 ct = nf_ct_tuplehash_to_ctrack(h);
497
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800498 /* Inverted packet tuple matches the reverse direction conntrack tuple,
499 * select the other tuplehash to get the right 'ctinfo' bits for this
500 * packet.
501 */
502 if (natted)
503 h = &ct->tuplehash[!h->tuple.dst.dir];
504
Florian Westphalc74454f2017-01-23 18:21:57 +0100505 nf_ct_set(skb, ct, ovs_ct_get_info(h));
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800506 return ct;
507}
508
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800509/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800510static bool skb_nfct_cached(struct net *net,
511 const struct sw_flow_key *key,
512 const struct ovs_conntrack_info *info,
513 struct sk_buff *skb)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700514{
515 enum ip_conntrack_info ctinfo;
516 struct nf_conn *ct;
517
518 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800519 /* If no ct, check if we have evidence that an existing conntrack entry
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800520 * might be found for this skb. This happens when we lose a skb->_nfct
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800521 * due to an upcall. If the connection was not confirmed, it is not
522 * cached and needs to be run through conntrack again.
523 */
524 if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
525 !(key->ct.state & OVS_CS_F_INVALID) &&
526 key->ct.zone == info->zone.id)
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800527 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
528 !!(key->ct.state
529 & OVS_CS_F_NAT_MASK));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700530 if (!ct)
531 return false;
532 if (!net_eq(net, read_pnet(&ct->ct_net)))
533 return false;
534 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
535 return false;
Joe Stringercae3a262015-08-26 11:31:53 -0700536 if (info->helper) {
537 struct nf_conn_help *help;
538
539 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
540 if (help && rcu_access_pointer(help->helper) != info->helper)
541 return false;
542 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700543
544 return true;
545}
546
Jarno Rajahalme05752522016-03-10 10:54:23 -0800547#ifdef CONFIG_NF_NAT_NEEDED
548/* Modelled after nf_nat_ipv[46]_fn().
549 * range is only used for new, uninitialized NAT state.
550 * Returns either NF_ACCEPT or NF_DROP.
551 */
552static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
553 enum ip_conntrack_info ctinfo,
554 const struct nf_nat_range *range,
555 enum nf_nat_manip_type maniptype)
556{
557 int hooknum, nh_off, err = NF_ACCEPT;
558
559 nh_off = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -0500560 skb_pull_rcsum(skb, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800561
562 /* See HOOK2MANIP(). */
563 if (maniptype == NF_NAT_MANIP_SRC)
564 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
565 else
566 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
567
568 switch (ctinfo) {
569 case IP_CT_RELATED:
570 case IP_CT_RELATED_REPLY:
Arnd Bergmann99b72482016-03-18 14:33:45 +0100571 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
572 skb->protocol == htons(ETH_P_IP) &&
Jarno Rajahalme05752522016-03-10 10:54:23 -0800573 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
574 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
575 hooknum))
576 err = NF_DROP;
577 goto push;
Arnd Bergmann99b72482016-03-18 14:33:45 +0100578 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
579 skb->protocol == htons(ETH_P_IPV6)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800580 __be16 frag_off;
581 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
582 int hdrlen = ipv6_skip_exthdr(skb,
583 sizeof(struct ipv6hdr),
584 &nexthdr, &frag_off);
585
586 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
587 if (!nf_nat_icmpv6_reply_translation(skb, ct,
588 ctinfo,
589 hooknum,
590 hdrlen))
591 err = NF_DROP;
592 goto push;
593 }
Jarno Rajahalme05752522016-03-10 10:54:23 -0800594 }
595 /* Non-ICMP, fall thru to initialize if needed. */
596 case IP_CT_NEW:
597 /* Seen it before? This can happen for loopback, retrans,
598 * or local packets.
599 */
600 if (!nf_nat_initialized(ct, maniptype)) {
601 /* Initialize according to the NAT action. */
602 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
603 /* Action is set up to establish a new
604 * mapping.
605 */
606 ? nf_nat_setup_info(ct, range, maniptype)
607 : nf_nat_alloc_null_binding(ct, hooknum);
608 if (err != NF_ACCEPT)
609 goto push;
610 }
611 break;
612
613 case IP_CT_ESTABLISHED:
614 case IP_CT_ESTABLISHED_REPLY:
615 break;
616
617 default:
618 err = NF_DROP;
619 goto push;
620 }
621
622 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
623push:
624 skb_push(skb, nh_off);
Lance Richardson75f01a42017-01-12 19:33:18 -0500625 skb_postpush_rcsum(skb, skb->data, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800626
627 return err;
628}
629
630static void ovs_nat_update_key(struct sw_flow_key *key,
631 const struct sk_buff *skb,
632 enum nf_nat_manip_type maniptype)
633{
634 if (maniptype == NF_NAT_MANIP_SRC) {
635 __be16 src;
636
637 key->ct.state |= OVS_CS_F_SRC_NAT;
638 if (key->eth.type == htons(ETH_P_IP))
639 key->ipv4.addr.src = ip_hdr(skb)->saddr;
640 else if (key->eth.type == htons(ETH_P_IPV6))
641 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
642 sizeof(key->ipv6.addr.src));
643 else
644 return;
645
646 if (key->ip.proto == IPPROTO_UDP)
647 src = udp_hdr(skb)->source;
648 else if (key->ip.proto == IPPROTO_TCP)
649 src = tcp_hdr(skb)->source;
650 else if (key->ip.proto == IPPROTO_SCTP)
651 src = sctp_hdr(skb)->source;
652 else
653 return;
654
655 key->tp.src = src;
656 } else {
657 __be16 dst;
658
659 key->ct.state |= OVS_CS_F_DST_NAT;
660 if (key->eth.type == htons(ETH_P_IP))
661 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
662 else if (key->eth.type == htons(ETH_P_IPV6))
663 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
664 sizeof(key->ipv6.addr.dst));
665 else
666 return;
667
668 if (key->ip.proto == IPPROTO_UDP)
669 dst = udp_hdr(skb)->dest;
670 else if (key->ip.proto == IPPROTO_TCP)
671 dst = tcp_hdr(skb)->dest;
672 else if (key->ip.proto == IPPROTO_SCTP)
673 dst = sctp_hdr(skb)->dest;
674 else
675 return;
676
677 key->tp.dst = dst;
678 }
679}
680
681/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
682static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
683 const struct ovs_conntrack_info *info,
684 struct sk_buff *skb, struct nf_conn *ct,
685 enum ip_conntrack_info ctinfo)
686{
687 enum nf_nat_manip_type maniptype;
688 int err;
689
690 if (nf_ct_is_untracked(ct)) {
691 /* A NAT action may only be performed on tracked packets. */
692 return NF_ACCEPT;
693 }
694
695 /* Add NAT extension if not confirmed yet. */
696 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
697 return NF_ACCEPT; /* Can't NAT. */
698
699 /* Determine NAT type.
700 * Check if the NAT type can be deduced from the tracked connection.
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700701 * Make sure new expected connections (IP_CT_RELATED) are NATted only
702 * when committing.
Jarno Rajahalme05752522016-03-10 10:54:23 -0800703 */
704 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
705 ct->status & IPS_NAT_MASK &&
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700706 (ctinfo != IP_CT_RELATED || info->commit)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800707 /* NAT an established or related connection like before. */
708 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
709 /* This is the REPLY direction for a connection
710 * for which NAT was applied in the forward
711 * direction. Do the reverse NAT.
712 */
713 maniptype = ct->status & IPS_SRC_NAT
714 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
715 else
716 maniptype = ct->status & IPS_SRC_NAT
717 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
718 } else if (info->nat & OVS_CT_SRC_NAT) {
719 maniptype = NF_NAT_MANIP_SRC;
720 } else if (info->nat & OVS_CT_DST_NAT) {
721 maniptype = NF_NAT_MANIP_DST;
722 } else {
723 return NF_ACCEPT; /* Connection is not NATed. */
724 }
725 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
726
727 /* 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 Rajahalme5e17da62017-02-09 11:21:52 -0800746 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800747 * 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) {
Florian Westphalcb9c6832017-01-23 18:21:56 +0100768 if (skb_nfct(skb))
769 nf_conntrack_put(skb_nfct(skb));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700770 nf_conntrack_get(&tmpl->ct_general);
Florian Westphalc74454f2017-01-23 18:21:57 +0100771 nf_ct_set(skb, tmpl, IP_CT_NEW);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700772 }
773
Pablo Neira Ayuso08733a02016-11-03 10:56:43 +0100774 err = nf_conntrack_in(net, info->family,
775 NF_INET_PRE_ROUTING, skb);
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800776 if (err != NF_ACCEPT)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700777 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700778
Jarno Rajahalme05752522016-03-10 10:54:23 -0800779 /* Clear CT state NAT flags to mark that we have not yet done
780 * NAT after the nf_conntrack_in() call. We can actually clear
781 * the whole state, as it will be re-initialized below.
782 */
783 key->ct.state = 0;
784
785 /* Update the key, but keep the NAT flags. */
786 ovs_ct_update_key(skb, info, key, true, true);
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800787 }
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800788
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800789 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800790 if (ct) {
791 /* Packets starting a new connection must be NATted before the
792 * helper, so that the helper knows about the NAT. We enforce
793 * this by delaying both NAT and helper calls for unconfirmed
794 * connections until the committing CT action. For later
795 * packets NAT and Helper may be called in either order.
796 *
797 * NAT will be done only if the CT action has NAT, and only
798 * once per packet (per zone), as guarded by the NAT bits in
799 * the key->ct.state.
800 */
801 if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
802 (nf_ct_is_confirmed(ct) || info->commit) &&
803 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
804 return -EINVAL;
805 }
806
Joe Stringer16ec3d42016-05-11 10:29:26 -0700807 /* Userspace may decide to perform a ct lookup without a helper
808 * specified followed by a (recirculate and) commit with one.
809 * Therefore, for unconfirmed connections which we will commit,
810 * we need to attach the helper here.
811 */
812 if (!nf_ct_is_confirmed(ct) && info->commit &&
813 info->helper && !nfct_help(ct)) {
814 int err = __nf_ct_try_assign_helper(ct, info->ct,
815 GFP_ATOMIC);
816 if (err)
817 return err;
818 }
819
Jarno Rajahalme05752522016-03-10 10:54:23 -0800820 /* Call the helper only if:
821 * - nf_conntrack_in() was executed above ("!cached") for a
822 * confirmed connection, or
823 * - When committing an unconfirmed connection.
824 */
825 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
826 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
827 return -EINVAL;
828 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700829 }
830
831 return 0;
832}
833
834/* Lookup connection and read fields into key. */
835static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
836 const struct ovs_conntrack_info *info,
837 struct sk_buff *skb)
838{
839 struct nf_conntrack_expect *exp;
840
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800841 /* If we pass an expected packet through nf_conntrack_in() the
842 * expectation is typically removed, but the packet could still be
843 * lost in upcall processing. To prevent this from happening we
844 * perform an explicit expectation lookup. Expected connections are
845 * always new, and will be passed through conntrack only when they are
846 * committed, as it is OK to remove the expectation at that time.
847 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700848 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
849 if (exp) {
850 u8 state;
851
Jarno Rajahalme05752522016-03-10 10:54:23 -0800852 /* NOTE: New connections are NATted and Helped only when
853 * committed, so we are not calling into NAT here.
854 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700855 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700856 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200857 } else {
858 struct nf_conn *ct;
859 int err;
860
861 err = __ovs_ct_lookup(net, key, info, skb);
862 if (err)
863 return err;
864
Florian Westphalcb9c6832017-01-23 18:21:56 +0100865 ct = (struct nf_conn *)skb_nfct(skb);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200866 if (ct)
867 nf_ct_deliver_cached_events(ct);
868 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700869
870 return 0;
871}
872
Joe Stringer33db4122015-10-01 15:00:37 -0700873static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700874{
875 size_t i;
876
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800877 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
878 if (labels->ct_labels_32[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700879 return true;
880
881 return false;
882}
883
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700884/* Lookup connection and confirm if unconfirmed. */
885static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
886 const struct ovs_conntrack_info *info,
887 struct sk_buff *skb)
888{
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800889 enum ip_conntrack_info ctinfo;
890 struct nf_conn *ct;
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700891 int err;
892
893 err = __ovs_ct_lookup(net, key, info, skb);
894 if (err)
895 return err;
896
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800897 /* The connection could be invalid, in which case this is a no-op.*/
898 ct = nf_ct_get(skb, &ctinfo);
899 if (!ct)
900 return 0;
901
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700902 /* Apply changes before confirming the connection so that the initial
903 * conntrack NEW netlink event carries the values given in the CT
904 * action.
905 */
906 if (info->mark.mask) {
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800907 err = ovs_ct_set_mark(ct, key, info->mark.value,
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700908 info->mark.mask);
909 if (err)
910 return err;
911 }
912 if (labels_nonzero(&info->labels.mask)) {
Jarno Rajahalme6ffcea72017-02-09 11:21:57 -0800913 if (!nf_ct_is_confirmed(ct))
914 err = ovs_ct_init_labels(ct, key, &info->labels.value,
915 &info->labels.mask);
916 else
917 err = ovs_ct_set_labels(ct, key, &info->labels.value,
918 &info->labels.mask);
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700919 if (err)
920 return err;
921 }
922 /* This will take care of sending queued events even if the connection
923 * is already confirmed.
924 */
925 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
926 return -EINVAL;
927
928 return 0;
929}
930
Joe Stringer74c16612015-10-25 20:21:48 -0700931/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
932 * value if 'skb' is freed.
933 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700934int ovs_ct_execute(struct net *net, struct sk_buff *skb,
935 struct sw_flow_key *key,
936 const struct ovs_conntrack_info *info)
937{
938 int nh_ofs;
939 int err;
940
941 /* The conntrack module expects to be working at L3. */
942 nh_ofs = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -0500943 skb_pull_rcsum(skb, nh_ofs);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700944
945 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
946 err = handle_fragments(net, key, info->zone.id, skb);
947 if (err)
948 return err;
949 }
950
Joe Stringerab38a7b2015-10-06 11:00:01 -0700951 if (info->commit)
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700952 err = ovs_ct_commit(net, key, info, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700953 else
954 err = ovs_ct_lookup(net, key, info, skb);
955
956 skb_push(skb, nh_ofs);
Lance Richardson75f01a42017-01-12 19:33:18 -0500957 skb_postpush_rcsum(skb, skb->data, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -0700958 if (err)
959 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700960 return err;
961}
962
Joe Stringercae3a262015-08-26 11:31:53 -0700963static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
964 const struct sw_flow_key *key, bool log)
965{
966 struct nf_conntrack_helper *helper;
967 struct nf_conn_help *help;
968
969 helper = nf_conntrack_helper_try_module_get(name, info->family,
970 key->ip.proto);
971 if (!helper) {
972 OVS_NLERR(log, "Unknown helper \"%s\"", name);
973 return -EINVAL;
974 }
975
976 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
977 if (!help) {
978 module_put(helper->me);
979 return -ENOMEM;
980 }
981
982 rcu_assign_pointer(help->helper, helper);
983 info->helper = helper;
984 return 0;
985}
986
Jarno Rajahalme05752522016-03-10 10:54:23 -0800987#ifdef CONFIG_NF_NAT_NEEDED
988static int parse_nat(const struct nlattr *attr,
989 struct ovs_conntrack_info *info, bool log)
990{
991 struct nlattr *a;
992 int rem;
993 bool have_ip_max = false;
994 bool have_proto_max = false;
995 bool ip_vers = (info->family == NFPROTO_IPV6);
996
997 nla_for_each_nested(a, attr, rem) {
998 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
999 [OVS_NAT_ATTR_SRC] = {0, 0},
1000 [OVS_NAT_ATTR_DST] = {0, 0},
1001 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1002 sizeof(struct in6_addr)},
1003 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1004 sizeof(struct in6_addr)},
1005 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1006 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1007 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1008 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1009 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1010 };
1011 int type = nla_type(a);
1012
1013 if (type > OVS_NAT_ATTR_MAX) {
1014 OVS_NLERR(log,
1015 "Unknown NAT attribute (type=%d, max=%d).\n",
1016 type, OVS_NAT_ATTR_MAX);
1017 return -EINVAL;
1018 }
1019
1020 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1021 OVS_NLERR(log,
1022 "NAT attribute type %d has unexpected length (%d != %d).\n",
1023 type, nla_len(a),
1024 ovs_nat_attr_lens[type][ip_vers]);
1025 return -EINVAL;
1026 }
1027
1028 switch (type) {
1029 case OVS_NAT_ATTR_SRC:
1030 case OVS_NAT_ATTR_DST:
1031 if (info->nat) {
1032 OVS_NLERR(log,
1033 "Only one type of NAT may be specified.\n"
1034 );
1035 return -ERANGE;
1036 }
1037 info->nat |= OVS_CT_NAT;
1038 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1039 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1040 break;
1041
1042 case OVS_NAT_ATTR_IP_MIN:
Haishuang Yanac71b462016-03-28 18:08:59 +08001043 nla_memcpy(&info->range.min_addr, a,
1044 sizeof(info->range.min_addr));
Jarno Rajahalme05752522016-03-10 10:54:23 -08001045 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1046 break;
1047
1048 case OVS_NAT_ATTR_IP_MAX:
1049 have_ip_max = true;
1050 nla_memcpy(&info->range.max_addr, a,
1051 sizeof(info->range.max_addr));
1052 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1053 break;
1054
1055 case OVS_NAT_ATTR_PROTO_MIN:
1056 info->range.min_proto.all = htons(nla_get_u16(a));
1057 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1058 break;
1059
1060 case OVS_NAT_ATTR_PROTO_MAX:
1061 have_proto_max = true;
1062 info->range.max_proto.all = htons(nla_get_u16(a));
1063 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1064 break;
1065
1066 case OVS_NAT_ATTR_PERSISTENT:
1067 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1068 break;
1069
1070 case OVS_NAT_ATTR_PROTO_HASH:
1071 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1072 break;
1073
1074 case OVS_NAT_ATTR_PROTO_RANDOM:
1075 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1076 break;
1077
1078 default:
1079 OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
1080 return -EINVAL;
1081 }
1082 }
1083
1084 if (rem > 0) {
1085 OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
1086 return -EINVAL;
1087 }
1088 if (!info->nat) {
1089 /* Do not allow flags if no type is given. */
1090 if (info->range.flags) {
1091 OVS_NLERR(log,
1092 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1093 );
1094 return -EINVAL;
1095 }
1096 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1097 } else if (!info->commit) {
1098 OVS_NLERR(log,
1099 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1100 );
1101 return -EINVAL;
1102 }
1103 /* Allow missing IP_MAX. */
1104 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1105 memcpy(&info->range.max_addr, &info->range.min_addr,
1106 sizeof(info->range.max_addr));
1107 }
1108 /* Allow missing PROTO_MAX. */
1109 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1110 !have_proto_max) {
1111 info->range.max_proto.all = info->range.min_proto.all;
1112 }
1113 return 0;
1114}
1115#endif
1116
Joe Stringer7f8a4362015-08-26 11:31:48 -07001117static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001118 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -07001119 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1120 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -07001121 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1122 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -07001123 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1124 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -07001125 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
Jarno Rajahalme05752522016-03-10 10:54:23 -08001126 .maxlen = NF_CT_HELPER_NAME_LEN },
1127#ifdef CONFIG_NF_NAT_NEEDED
1128 /* NAT length is checked when parsing the nested attributes. */
1129 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1130#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001131};
1132
1133static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -07001134 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001135{
1136 struct nlattr *a;
1137 int rem;
1138
1139 nla_for_each_nested(a, attr, rem) {
1140 int type = nla_type(a);
1141 int maxlen = ovs_ct_attr_lens[type].maxlen;
1142 int minlen = ovs_ct_attr_lens[type].minlen;
1143
1144 if (type > OVS_CT_ATTR_MAX) {
1145 OVS_NLERR(log,
1146 "Unknown conntrack attr (type=%d, max=%d)",
1147 type, OVS_CT_ATTR_MAX);
1148 return -EINVAL;
1149 }
1150 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1151 OVS_NLERR(log,
1152 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1153 type, nla_len(a), maxlen);
1154 return -EINVAL;
1155 }
1156
1157 switch (type) {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001158 case OVS_CT_ATTR_COMMIT:
1159 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001160 break;
1161#ifdef CONFIG_NF_CONNTRACK_ZONES
1162 case OVS_CT_ATTR_ZONE:
1163 info->zone.id = nla_get_u16(a);
1164 break;
1165#endif
Joe Stringer182e3042015-08-26 11:31:49 -07001166#ifdef CONFIG_NF_CONNTRACK_MARK
1167 case OVS_CT_ATTR_MARK: {
1168 struct md_mark *mark = nla_data(a);
1169
Joe Stringere754ec62015-10-19 19:19:00 -07001170 if (!mark->mask) {
1171 OVS_NLERR(log, "ct_mark mask cannot be 0");
1172 return -EINVAL;
1173 }
Joe Stringer182e3042015-08-26 11:31:49 -07001174 info->mark = *mark;
1175 break;
1176 }
1177#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -07001178#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -07001179 case OVS_CT_ATTR_LABELS: {
1180 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001181
Joe Stringere754ec62015-10-19 19:19:00 -07001182 if (!labels_nonzero(&labels->mask)) {
1183 OVS_NLERR(log, "ct_labels mask cannot be 0");
1184 return -EINVAL;
1185 }
Joe Stringer33db4122015-10-01 15:00:37 -07001186 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001187 break;
1188 }
1189#endif
Joe Stringercae3a262015-08-26 11:31:53 -07001190 case OVS_CT_ATTR_HELPER:
1191 *helper = nla_data(a);
1192 if (!memchr(*helper, '\0', nla_len(a))) {
1193 OVS_NLERR(log, "Invalid conntrack helper");
1194 return -EINVAL;
1195 }
1196 break;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001197#ifdef CONFIG_NF_NAT_NEEDED
1198 case OVS_CT_ATTR_NAT: {
1199 int err = parse_nat(a, info, log);
1200
1201 if (err)
1202 return err;
1203 break;
1204 }
1205#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001206 default:
1207 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1208 type);
1209 return -EINVAL;
1210 }
1211 }
1212
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001213#ifdef CONFIG_NF_CONNTRACK_MARK
1214 if (!info->commit && info->mark.mask) {
1215 OVS_NLERR(log,
1216 "Setting conntrack mark requires 'commit' flag.");
1217 return -EINVAL;
1218 }
1219#endif
1220#ifdef CONFIG_NF_CONNTRACK_LABELS
1221 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1222 OVS_NLERR(log,
1223 "Setting conntrack labels requires 'commit' flag.");
1224 return -EINVAL;
1225 }
1226#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001227 if (rem > 0) {
1228 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1229 return -EINVAL;
1230 }
1231
1232 return 0;
1233}
1234
Joe Stringerc2ac6672015-08-26 11:31:52 -07001235bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001236{
1237 if (attr == OVS_KEY_ATTR_CT_STATE)
1238 return true;
1239 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1240 attr == OVS_KEY_ATTR_CT_ZONE)
1241 return true;
Joe Stringer182e3042015-08-26 11:31:49 -07001242 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1243 attr == OVS_KEY_ATTR_CT_MARK)
1244 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001245 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001246 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001247 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1248
1249 return ovs_net->xt_label;
1250 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001251
1252 return false;
1253}
1254
1255int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1256 const struct sw_flow_key *key,
1257 struct sw_flow_actions **sfa, bool log)
1258{
1259 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -07001260 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001261 u16 family;
1262 int err;
1263
1264 family = key_to_nfproto(key);
1265 if (family == NFPROTO_UNSPEC) {
1266 OVS_NLERR(log, "ct family unspecified");
1267 return -EINVAL;
1268 }
1269
1270 memset(&ct_info, 0, sizeof(ct_info));
1271 ct_info.family = family;
1272
1273 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1274 NF_CT_DEFAULT_ZONE_DIR, 0);
1275
Joe Stringercae3a262015-08-26 11:31:53 -07001276 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001277 if (err)
1278 return err;
1279
1280 /* Set up template for tracking connections in specific zones. */
1281 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1282 if (!ct_info.ct) {
1283 OVS_NLERR(log, "Failed to allocate conntrack template");
1284 return -ENOMEM;
1285 }
Joe Stringer90c7afc962015-12-23 14:39:27 -08001286
1287 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1288 nf_conntrack_get(&ct_info.ct->ct_general);
1289
Joe Stringercae3a262015-08-26 11:31:53 -07001290 if (helper) {
1291 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1292 if (err)
1293 goto err_free_ct;
1294 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001295
1296 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1297 sizeof(ct_info), log);
1298 if (err)
1299 goto err_free_ct;
1300
Joe Stringer7f8a4362015-08-26 11:31:48 -07001301 return 0;
1302err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001303 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001304 return err;
1305}
1306
Jarno Rajahalme05752522016-03-10 10:54:23 -08001307#ifdef CONFIG_NF_NAT_NEEDED
1308static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1309 struct sk_buff *skb)
1310{
1311 struct nlattr *start;
1312
1313 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1314 if (!start)
1315 return false;
1316
1317 if (info->nat & OVS_CT_SRC_NAT) {
1318 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1319 return false;
1320 } else if (info->nat & OVS_CT_DST_NAT) {
1321 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1322 return false;
1323 } else {
1324 goto out;
1325 }
1326
1327 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
Arnd Bergmann99b72482016-03-18 14:33:45 +01001328 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1329 info->family == NFPROTO_IPV4) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001330 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1331 info->range.min_addr.ip) ||
1332 (info->range.max_addr.ip
1333 != info->range.min_addr.ip &&
1334 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1335 info->range.max_addr.ip))))
1336 return false;
Arnd Bergmann99b72482016-03-18 14:33:45 +01001337 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1338 info->family == NFPROTO_IPV6) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001339 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1340 &info->range.min_addr.in6) ||
1341 (memcmp(&info->range.max_addr.in6,
1342 &info->range.min_addr.in6,
1343 sizeof(info->range.max_addr.in6)) &&
1344 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1345 &info->range.max_addr.in6))))
1346 return false;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001347 } else {
1348 return false;
1349 }
1350 }
1351 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1352 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1353 ntohs(info->range.min_proto.all)) ||
1354 (info->range.max_proto.all != info->range.min_proto.all &&
1355 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1356 ntohs(info->range.max_proto.all)))))
1357 return false;
1358
1359 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1360 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1361 return false;
1362 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1363 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1364 return false;
1365 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1366 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1367 return false;
1368out:
1369 nla_nest_end(skb, start);
1370
1371 return true;
1372}
1373#endif
1374
Joe Stringer7f8a4362015-08-26 11:31:48 -07001375int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1376 struct sk_buff *skb)
1377{
1378 struct nlattr *start;
1379
1380 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1381 if (!start)
1382 return -EMSGSIZE;
1383
Joe Stringerab38a7b2015-10-06 11:00:01 -07001384 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001385 return -EMSGSIZE;
1386 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1387 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1388 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -07001389 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -07001390 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1391 &ct_info->mark))
1392 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001393 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -07001394 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001395 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1396 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -07001397 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -07001398 if (ct_info->helper) {
1399 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1400 ct_info->helper->name))
1401 return -EMSGSIZE;
1402 }
Jarno Rajahalme05752522016-03-10 10:54:23 -08001403#ifdef CONFIG_NF_NAT_NEEDED
1404 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1405 return -EMSGSIZE;
1406#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001407 nla_nest_end(skb, start);
1408
1409 return 0;
1410}
1411
1412void ovs_ct_free_action(const struct nlattr *a)
1413{
1414 struct ovs_conntrack_info *ct_info = nla_data(a);
1415
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001416 __ovs_ct_free_action(ct_info);
1417}
1418
1419static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1420{
Joe Stringercae3a262015-08-26 11:31:53 -07001421 if (ct_info->helper)
1422 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001423 if (ct_info->ct)
Joe Stringer76644232016-09-01 18:01:47 -07001424 nf_ct_tmpl_free(ct_info->ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001425}
Joe Stringerc2ac6672015-08-26 11:31:52 -07001426
1427void ovs_ct_init(struct net *net)
1428{
Joe Stringer33db4122015-10-01 15:00:37 -07001429 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001430 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1431
Florian Westphaladff6c62016-04-12 18:14:25 +02001432 if (nf_connlabels_get(net, n_bits - 1)) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001433 ovs_net->xt_label = false;
1434 OVS_NLERR(true, "Failed to set connlabel length");
1435 } else {
1436 ovs_net->xt_label = true;
1437 }
1438}
1439
1440void ovs_ct_exit(struct net *net)
1441{
1442 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1443
1444 if (ovs_net->xt_label)
1445 nf_connlabels_put(net);
1446}