blob: fe2a410ce70adb4051621bbe4f327aea4204a598 [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
230static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
231 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 enum ip_conntrack_info ctinfo;
235 struct nf_conn *ct;
236 u32 new_mark;
237
Joe Stringer182e3042015-08-26 11:31:49 -0700238 /* The connection could be invalid, in which case set_mark is no-op. */
239 ct = nf_ct_get(skb, &ctinfo);
240 if (!ct)
241 return 0;
242
243 new_mark = ct_mark | (ct->mark & ~(mask));
244 if (ct->mark != new_mark) {
245 ct->mark = new_mark;
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800246 if (nf_ct_is_confirmed(ct))
247 nf_conntrack_event_cache(IPCT_MARK, ct);
Joe Stringer182e3042015-08-26 11:31:49 -0700248 key->ct.mark = new_mark;
249 }
250
Joe Stringer7f8a4362015-08-26 11:31:48 -0700251 return 0;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700252#else
253 return -ENOTSUPP;
254#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -0700255}
256
Joe Stringer33db4122015-10-01 15:00:37 -0700257static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
258 const struct ovs_key_ct_labels *labels,
259 const struct ovs_key_ct_labels *mask)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700260{
261 enum ip_conntrack_info ctinfo;
262 struct nf_conn_labels *cl;
263 struct nf_conn *ct;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700264
Joe Stringerc2ac6672015-08-26 11:31:52 -0700265 /* The connection could be invalid, in which case set_label is no-op.*/
266 ct = nf_ct_get(skb, &ctinfo);
267 if (!ct)
268 return 0;
269
270 cl = nf_ct_labels_find(ct);
271 if (!cl) {
272 nf_ct_labels_ext_add(ct);
273 cl = nf_ct_labels_find(ct);
274 }
Jarno Rajahalmeb87cec32017-02-09 11:21:56 -0800275 if (!cl)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700276 return -ENOSPC;
277
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800278 if (nf_ct_is_confirmed(ct)) {
279 /* Triggers a change event, which makes sense only for
280 * confirmed connections.
281 */
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800282 int err = nf_connlabels_replace(ct, labels->ct_labels_32,
283 mask->ct_labels_32,
284 OVS_CT_LABELS_LEN_32);
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800285 if (err)
286 return err;
287 } else {
288 u32 *dst = (u32 *)cl->bits;
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800289 const u32 *msk = mask->ct_labels_32;
290 const u32 *lbl = labels->ct_labels_32;
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800291 int i;
292
293 /* No-one else has access to the non-confirmed entry, copy
294 * labels over, keeping any bits we are not explicitly setting.
295 */
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800296 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
Jarno Rajahalme193e3092017-02-09 11:21:54 -0800297 dst[i] = (dst[i] & ~msk[i]) | (lbl[i] & msk[i]);
298 }
Joe Stringerc2ac6672015-08-26 11:31:52 -0700299
Joe Stringer33db4122015-10-01 15:00:37 -0700300 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700301 return 0;
302}
303
Joe Stringercae3a262015-08-26 11:31:53 -0700304/* 'skb' should already be pulled to nh_ofs. */
305static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
306{
307 const struct nf_conntrack_helper *helper;
308 const struct nf_conn_help *help;
309 enum ip_conntrack_info ctinfo;
310 unsigned int protoff;
311 struct nf_conn *ct;
Jarno Rajahalme05752522016-03-10 10:54:23 -0800312 int err;
Joe Stringercae3a262015-08-26 11:31:53 -0700313
314 ct = nf_ct_get(skb, &ctinfo);
315 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
316 return NF_ACCEPT;
317
318 help = nfct_help(ct);
319 if (!help)
320 return NF_ACCEPT;
321
322 helper = rcu_dereference(help->helper);
323 if (!helper)
324 return NF_ACCEPT;
325
326 switch (proto) {
327 case NFPROTO_IPV4:
328 protoff = ip_hdrlen(skb);
329 break;
330 case NFPROTO_IPV6: {
331 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
332 __be16 frag_off;
Joe Stringercc570602015-09-14 11:14:50 -0700333 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700334
Joe Stringercc570602015-09-14 11:14:50 -0700335 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
336 &frag_off);
337 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700338 pr_debug("proto header not found\n");
339 return NF_ACCEPT;
340 }
Joe Stringercc570602015-09-14 11:14:50 -0700341 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700342 break;
343 }
344 default:
345 WARN_ONCE(1, "helper invoked on non-IP family!");
346 return NF_DROP;
347 }
348
Jarno Rajahalme05752522016-03-10 10:54:23 -0800349 err = helper->help(skb, protoff, ct, ctinfo);
350 if (err != NF_ACCEPT)
351 return err;
352
353 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
354 * FTP with NAT) adusting the TCP payload size when mangling IP
355 * addresses and/or port numbers in the text-based control connection.
356 */
357 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
358 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
359 return NF_DROP;
360 return NF_ACCEPT;
Joe Stringercae3a262015-08-26 11:31:53 -0700361}
362
Joe Stringer74c16612015-10-25 20:21:48 -0700363/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
364 * value if 'skb' is freed.
365 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700366static int handle_fragments(struct net *net, struct sw_flow_key *key,
367 u16 zone, struct sk_buff *skb)
368{
369 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100370 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700371
372 if (key->eth.type == htons(ETH_P_IP)) {
373 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700374
375 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500376 err = ip_defrag(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700377 if (err)
378 return err;
379
380 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700381#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700382 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700383 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700384
Joe Stringer49e261a2016-04-18 14:51:47 -0700385 skb_orphan(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700386 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100387 err = nf_ct_frag6_gather(net, skb, user);
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800388 if (err) {
389 if (err != -EINPROGRESS)
390 kfree_skb(skb);
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100391 return err;
Daniele Di Proiettof92a80a2016-11-28 15:43:53 -0800392 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700393
Florian Westphaldaaa7d62015-11-18 23:32:40 +0100394 key->ip.proto = ipv6_hdr(skb)->nexthdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700395 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700396#endif
397 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700398 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700399 return -EPFNOSUPPORT;
400 }
401
402 key->ip.frag = OVS_FRAG_TYPE_NONE;
403 skb_clear_hash(skb);
404 skb->ignore_df = 1;
405 *OVS_CB(skb) = ovs_cb;
406
407 return 0;
408}
409
410static struct nf_conntrack_expect *
411ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
412 u16 proto, const struct sk_buff *skb)
413{
414 struct nf_conntrack_tuple tuple;
415
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500416 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700417 return NULL;
418 return __nf_ct_expect_find(net, zone, &tuple);
419}
420
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800421/* This replicates logic from nf_conntrack_core.c that is not exported. */
422static enum ip_conntrack_info
423ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
424{
425 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
426
427 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
428 return IP_CT_ESTABLISHED_REPLY;
429 /* Once we've had two way comms, always ESTABLISHED. */
430 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
431 return IP_CT_ESTABLISHED;
432 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
433 return IP_CT_RELATED;
434 return IP_CT_NEW;
435}
436
437/* Find an existing connection which this packet belongs to without
438 * re-attributing statistics or modifying the connection state. This allows an
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800439 * skb->_nfct lost due to an upcall to be recovered during actions execution.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800440 *
441 * Must be called with rcu_read_lock.
442 *
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800443 * On success, populates skb->_nfct and returns the connection. Returns NULL
444 * if there is no existing entry.
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800445 */
446static struct nf_conn *
447ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800448 u8 l3num, struct sk_buff *skb, bool natted)
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800449{
450 struct nf_conntrack_l3proto *l3proto;
451 struct nf_conntrack_l4proto *l4proto;
452 struct nf_conntrack_tuple tuple;
453 struct nf_conntrack_tuple_hash *h;
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800454 struct nf_conn *ct;
455 unsigned int dataoff;
456 u8 protonum;
457
458 l3proto = __nf_ct_l3proto_find(l3num);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800459 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
460 &protonum) <= 0) {
461 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
462 return NULL;
463 }
464 l4proto = __nf_ct_l4proto_find(l3num, protonum);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800465 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
466 protonum, net, &tuple, l3proto, l4proto)) {
467 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
468 return NULL;
469 }
470
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800471 /* Must invert the tuple if skb has been transformed by NAT. */
472 if (natted) {
473 struct nf_conntrack_tuple inverse;
474
475 if (!nf_ct_invert_tuple(&inverse, &tuple, l3proto, l4proto)) {
476 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
477 return NULL;
478 }
479 tuple = inverse;
480 }
481
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800482 /* look for tuple match */
483 h = nf_conntrack_find_get(net, zone, &tuple);
484 if (!h)
485 return NULL; /* Not found. */
486
487 ct = nf_ct_tuplehash_to_ctrack(h);
488
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800489 /* Inverted packet tuple matches the reverse direction conntrack tuple,
490 * select the other tuplehash to get the right 'ctinfo' bits for this
491 * packet.
492 */
493 if (natted)
494 h = &ct->tuplehash[!h->tuple.dst.dir];
495
Florian Westphalc74454f2017-01-23 18:21:57 +0100496 nf_ct_set(skb, ct, ovs_ct_get_info(h));
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800497 return ct;
498}
499
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800500/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800501static bool skb_nfct_cached(struct net *net,
502 const struct sw_flow_key *key,
503 const struct ovs_conntrack_info *info,
504 struct sk_buff *skb)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700505{
506 enum ip_conntrack_info ctinfo;
507 struct nf_conn *ct;
508
509 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800510 /* If no ct, check if we have evidence that an existing conntrack entry
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800511 * might be found for this skb. This happens when we lose a skb->_nfct
Jarno Rajahalme289f2252016-03-10 10:54:20 -0800512 * due to an upcall. If the connection was not confirmed, it is not
513 * cached and needs to be run through conntrack again.
514 */
515 if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
516 !(key->ct.state & OVS_CS_F_INVALID) &&
517 key->ct.zone == info->zone.id)
Jarno Rajahalme9ff464d2017-02-09 11:21:53 -0800518 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
519 !!(key->ct.state
520 & OVS_CS_F_NAT_MASK));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700521 if (!ct)
522 return false;
523 if (!net_eq(net, read_pnet(&ct->ct_net)))
524 return false;
525 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
526 return false;
Joe Stringercae3a262015-08-26 11:31:53 -0700527 if (info->helper) {
528 struct nf_conn_help *help;
529
530 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
531 if (help && rcu_access_pointer(help->helper) != info->helper)
532 return false;
533 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700534
535 return true;
536}
537
Jarno Rajahalme05752522016-03-10 10:54:23 -0800538#ifdef CONFIG_NF_NAT_NEEDED
539/* Modelled after nf_nat_ipv[46]_fn().
540 * range is only used for new, uninitialized NAT state.
541 * Returns either NF_ACCEPT or NF_DROP.
542 */
543static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
544 enum ip_conntrack_info ctinfo,
545 const struct nf_nat_range *range,
546 enum nf_nat_manip_type maniptype)
547{
548 int hooknum, nh_off, err = NF_ACCEPT;
549
550 nh_off = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -0500551 skb_pull_rcsum(skb, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800552
553 /* See HOOK2MANIP(). */
554 if (maniptype == NF_NAT_MANIP_SRC)
555 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
556 else
557 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
558
559 switch (ctinfo) {
560 case IP_CT_RELATED:
561 case IP_CT_RELATED_REPLY:
Arnd Bergmann99b72482016-03-18 14:33:45 +0100562 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
563 skb->protocol == htons(ETH_P_IP) &&
Jarno Rajahalme05752522016-03-10 10:54:23 -0800564 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
565 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
566 hooknum))
567 err = NF_DROP;
568 goto push;
Arnd Bergmann99b72482016-03-18 14:33:45 +0100569 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
570 skb->protocol == htons(ETH_P_IPV6)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800571 __be16 frag_off;
572 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
573 int hdrlen = ipv6_skip_exthdr(skb,
574 sizeof(struct ipv6hdr),
575 &nexthdr, &frag_off);
576
577 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
578 if (!nf_nat_icmpv6_reply_translation(skb, ct,
579 ctinfo,
580 hooknum,
581 hdrlen))
582 err = NF_DROP;
583 goto push;
584 }
Jarno Rajahalme05752522016-03-10 10:54:23 -0800585 }
586 /* Non-ICMP, fall thru to initialize if needed. */
587 case IP_CT_NEW:
588 /* Seen it before? This can happen for loopback, retrans,
589 * or local packets.
590 */
591 if (!nf_nat_initialized(ct, maniptype)) {
592 /* Initialize according to the NAT action. */
593 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
594 /* Action is set up to establish a new
595 * mapping.
596 */
597 ? nf_nat_setup_info(ct, range, maniptype)
598 : nf_nat_alloc_null_binding(ct, hooknum);
599 if (err != NF_ACCEPT)
600 goto push;
601 }
602 break;
603
604 case IP_CT_ESTABLISHED:
605 case IP_CT_ESTABLISHED_REPLY:
606 break;
607
608 default:
609 err = NF_DROP;
610 goto push;
611 }
612
613 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
614push:
615 skb_push(skb, nh_off);
Lance Richardson75f01a42017-01-12 19:33:18 -0500616 skb_postpush_rcsum(skb, skb->data, nh_off);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800617
618 return err;
619}
620
621static void ovs_nat_update_key(struct sw_flow_key *key,
622 const struct sk_buff *skb,
623 enum nf_nat_manip_type maniptype)
624{
625 if (maniptype == NF_NAT_MANIP_SRC) {
626 __be16 src;
627
628 key->ct.state |= OVS_CS_F_SRC_NAT;
629 if (key->eth.type == htons(ETH_P_IP))
630 key->ipv4.addr.src = ip_hdr(skb)->saddr;
631 else if (key->eth.type == htons(ETH_P_IPV6))
632 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
633 sizeof(key->ipv6.addr.src));
634 else
635 return;
636
637 if (key->ip.proto == IPPROTO_UDP)
638 src = udp_hdr(skb)->source;
639 else if (key->ip.proto == IPPROTO_TCP)
640 src = tcp_hdr(skb)->source;
641 else if (key->ip.proto == IPPROTO_SCTP)
642 src = sctp_hdr(skb)->source;
643 else
644 return;
645
646 key->tp.src = src;
647 } else {
648 __be16 dst;
649
650 key->ct.state |= OVS_CS_F_DST_NAT;
651 if (key->eth.type == htons(ETH_P_IP))
652 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
653 else if (key->eth.type == htons(ETH_P_IPV6))
654 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
655 sizeof(key->ipv6.addr.dst));
656 else
657 return;
658
659 if (key->ip.proto == IPPROTO_UDP)
660 dst = udp_hdr(skb)->dest;
661 else if (key->ip.proto == IPPROTO_TCP)
662 dst = tcp_hdr(skb)->dest;
663 else if (key->ip.proto == IPPROTO_SCTP)
664 dst = sctp_hdr(skb)->dest;
665 else
666 return;
667
668 key->tp.dst = dst;
669 }
670}
671
672/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
673static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
674 const struct ovs_conntrack_info *info,
675 struct sk_buff *skb, struct nf_conn *ct,
676 enum ip_conntrack_info ctinfo)
677{
678 enum nf_nat_manip_type maniptype;
679 int err;
680
681 if (nf_ct_is_untracked(ct)) {
682 /* A NAT action may only be performed on tracked packets. */
683 return NF_ACCEPT;
684 }
685
686 /* Add NAT extension if not confirmed yet. */
687 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
688 return NF_ACCEPT; /* Can't NAT. */
689
690 /* Determine NAT type.
691 * Check if the NAT type can be deduced from the tracked connection.
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700692 * Make sure new expected connections (IP_CT_RELATED) are NATted only
693 * when committing.
Jarno Rajahalme05752522016-03-10 10:54:23 -0800694 */
695 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
696 ct->status & IPS_NAT_MASK &&
Jarno Rajahalme5745b0b2016-03-21 11:15:19 -0700697 (ctinfo != IP_CT_RELATED || info->commit)) {
Jarno Rajahalme05752522016-03-10 10:54:23 -0800698 /* NAT an established or related connection like before. */
699 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
700 /* This is the REPLY direction for a connection
701 * for which NAT was applied in the forward
702 * direction. Do the reverse NAT.
703 */
704 maniptype = ct->status & IPS_SRC_NAT
705 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
706 else
707 maniptype = ct->status & IPS_SRC_NAT
708 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
709 } else if (info->nat & OVS_CT_SRC_NAT) {
710 maniptype = NF_NAT_MANIP_SRC;
711 } else if (info->nat & OVS_CT_DST_NAT) {
712 maniptype = NF_NAT_MANIP_DST;
713 } else {
714 return NF_ACCEPT; /* Connection is not NATed. */
715 }
716 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
717
718 /* Mark NAT done if successful and update the flow key. */
719 if (err == NF_ACCEPT)
720 ovs_nat_update_key(key, skb, maniptype);
721
722 return err;
723}
724#else /* !CONFIG_NF_NAT_NEEDED */
725static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
726 const struct ovs_conntrack_info *info,
727 struct sk_buff *skb, struct nf_conn *ct,
728 enum ip_conntrack_info ctinfo)
729{
730 return NF_ACCEPT;
731}
732#endif
733
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800734/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800735 * not done already. Update key with new CT state after passing the packet
736 * through conntrack.
Jarno Rajahalme5e17da62017-02-09 11:21:52 -0800737 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800738 * set to NULL and 0 will be returned.
739 */
Joe Stringer4f0909e2015-10-19 19:18:59 -0700740static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700741 const struct ovs_conntrack_info *info,
742 struct sk_buff *skb)
743{
744 /* If we are recirculating packets to match on conntrack fields and
745 * committing with a separate conntrack action, then we don't need to
746 * actually run the packet through conntrack twice unless it's for a
747 * different zone.
748 */
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800749 bool cached = skb_nfct_cached(net, key, info, skb);
750 enum ip_conntrack_info ctinfo;
751 struct nf_conn *ct;
752
753 if (!cached) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700754 struct nf_conn *tmpl = info->ct;
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800755 int err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700756
757 /* Associate skb with specified zone. */
758 if (tmpl) {
Florian Westphalcb9c6832017-01-23 18:21:56 +0100759 if (skb_nfct(skb))
760 nf_conntrack_put(skb_nfct(skb));
Joe Stringer7f8a4362015-08-26 11:31:48 -0700761 nf_conntrack_get(&tmpl->ct_general);
Florian Westphalc74454f2017-01-23 18:21:57 +0100762 nf_ct_set(skb, tmpl, IP_CT_NEW);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700763 }
764
Pablo Neira Ayuso08733a02016-11-03 10:56:43 +0100765 err = nf_conntrack_in(net, info->family,
766 NF_INET_PRE_ROUTING, skb);
Jarno Rajahalme5b6b9292016-03-10 10:54:21 -0800767 if (err != NF_ACCEPT)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700768 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700769
Jarno Rajahalme05752522016-03-10 10:54:23 -0800770 /* Clear CT state NAT flags to mark that we have not yet done
771 * NAT after the nf_conntrack_in() call. We can actually clear
772 * the whole state, as it will be re-initialized below.
773 */
774 key->ct.state = 0;
775
776 /* Update the key, but keep the NAT flags. */
777 ovs_ct_update_key(skb, info, key, true, true);
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800778 }
Jarno Rajahalme394e9102016-03-10 10:54:19 -0800779
Jarno Rajahalme28b6e0c2016-03-10 10:54:22 -0800780 ct = nf_ct_get(skb, &ctinfo);
Jarno Rajahalme05752522016-03-10 10:54:23 -0800781 if (ct) {
782 /* Packets starting a new connection must be NATted before the
783 * helper, so that the helper knows about the NAT. We enforce
784 * this by delaying both NAT and helper calls for unconfirmed
785 * connections until the committing CT action. For later
786 * packets NAT and Helper may be called in either order.
787 *
788 * NAT will be done only if the CT action has NAT, and only
789 * once per packet (per zone), as guarded by the NAT bits in
790 * the key->ct.state.
791 */
792 if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
793 (nf_ct_is_confirmed(ct) || info->commit) &&
794 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
795 return -EINVAL;
796 }
797
Joe Stringer16ec3d42016-05-11 10:29:26 -0700798 /* Userspace may decide to perform a ct lookup without a helper
799 * specified followed by a (recirculate and) commit with one.
800 * Therefore, for unconfirmed connections which we will commit,
801 * we need to attach the helper here.
802 */
803 if (!nf_ct_is_confirmed(ct) && info->commit &&
804 info->helper && !nfct_help(ct)) {
805 int err = __nf_ct_try_assign_helper(ct, info->ct,
806 GFP_ATOMIC);
807 if (err)
808 return err;
809 }
810
Jarno Rajahalme05752522016-03-10 10:54:23 -0800811 /* Call the helper only if:
812 * - nf_conntrack_in() was executed above ("!cached") for a
813 * confirmed connection, or
814 * - When committing an unconfirmed connection.
815 */
816 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
817 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
818 return -EINVAL;
819 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700820 }
821
822 return 0;
823}
824
825/* Lookup connection and read fields into key. */
826static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
827 const struct ovs_conntrack_info *info,
828 struct sk_buff *skb)
829{
830 struct nf_conntrack_expect *exp;
831
Jarno Rajahalme9f13ded2016-03-10 10:54:18 -0800832 /* If we pass an expected packet through nf_conntrack_in() the
833 * expectation is typically removed, but the packet could still be
834 * lost in upcall processing. To prevent this from happening we
835 * perform an explicit expectation lookup. Expected connections are
836 * always new, and will be passed through conntrack only when they are
837 * committed, as it is OK to remove the expectation at that time.
838 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700839 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
840 if (exp) {
841 u8 state;
842
Jarno Rajahalme05752522016-03-10 10:54:23 -0800843 /* NOTE: New connections are NATted and Helped only when
844 * committed, so we are not calling into NAT here.
845 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700846 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700847 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200848 } else {
849 struct nf_conn *ct;
850 int err;
851
852 err = __ovs_ct_lookup(net, key, info, skb);
853 if (err)
854 return err;
855
Florian Westphalcb9c6832017-01-23 18:21:56 +0100856 ct = (struct nf_conn *)skb_nfct(skb);
Samuel Gauthierd913d3a2016-06-28 17:22:26 +0200857 if (ct)
858 nf_ct_deliver_cached_events(ct);
859 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700860
861 return 0;
862}
863
Joe Stringer33db4122015-10-01 15:00:37 -0700864static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700865{
866 size_t i;
867
Jarno Rajahalmecb80d582017-02-09 11:21:55 -0800868 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
869 if (labels->ct_labels_32[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700870 return true;
871
872 return false;
873}
874
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700875/* Lookup connection and confirm if unconfirmed. */
876static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
877 const struct ovs_conntrack_info *info,
878 struct sk_buff *skb)
879{
880 int err;
881
882 err = __ovs_ct_lookup(net, key, info, skb);
883 if (err)
884 return err;
885
886 /* Apply changes before confirming the connection so that the initial
887 * conntrack NEW netlink event carries the values given in the CT
888 * action.
889 */
890 if (info->mark.mask) {
891 err = ovs_ct_set_mark(skb, key, info->mark.value,
892 info->mark.mask);
893 if (err)
894 return err;
895 }
896 if (labels_nonzero(&info->labels.mask)) {
897 err = ovs_ct_set_labels(skb, key, &info->labels.value,
898 &info->labels.mask);
899 if (err)
900 return err;
901 }
902 /* This will take care of sending queued events even if the connection
903 * is already confirmed.
904 */
905 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
906 return -EINVAL;
907
908 return 0;
909}
910
Joe Stringer74c16612015-10-25 20:21:48 -0700911/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
912 * value if 'skb' is freed.
913 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700914int ovs_ct_execute(struct net *net, struct sk_buff *skb,
915 struct sw_flow_key *key,
916 const struct ovs_conntrack_info *info)
917{
918 int nh_ofs;
919 int err;
920
921 /* The conntrack module expects to be working at L3. */
922 nh_ofs = skb_network_offset(skb);
Lance Richardson75f01a42017-01-12 19:33:18 -0500923 skb_pull_rcsum(skb, nh_ofs);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700924
925 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
926 err = handle_fragments(net, key, info->zone.id, skb);
927 if (err)
928 return err;
929 }
930
Joe Stringerab38a7b2015-10-06 11:00:01 -0700931 if (info->commit)
Jarno Rajahalme7d904c72016-06-21 14:59:38 -0700932 err = ovs_ct_commit(net, key, info, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700933 else
934 err = ovs_ct_lookup(net, key, info, skb);
935
936 skb_push(skb, nh_ofs);
Lance Richardson75f01a42017-01-12 19:33:18 -0500937 skb_postpush_rcsum(skb, skb->data, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -0700938 if (err)
939 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700940 return err;
941}
942
Joe Stringercae3a262015-08-26 11:31:53 -0700943static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
944 const struct sw_flow_key *key, bool log)
945{
946 struct nf_conntrack_helper *helper;
947 struct nf_conn_help *help;
948
949 helper = nf_conntrack_helper_try_module_get(name, info->family,
950 key->ip.proto);
951 if (!helper) {
952 OVS_NLERR(log, "Unknown helper \"%s\"", name);
953 return -EINVAL;
954 }
955
956 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
957 if (!help) {
958 module_put(helper->me);
959 return -ENOMEM;
960 }
961
962 rcu_assign_pointer(help->helper, helper);
963 info->helper = helper;
964 return 0;
965}
966
Jarno Rajahalme05752522016-03-10 10:54:23 -0800967#ifdef CONFIG_NF_NAT_NEEDED
968static int parse_nat(const struct nlattr *attr,
969 struct ovs_conntrack_info *info, bool log)
970{
971 struct nlattr *a;
972 int rem;
973 bool have_ip_max = false;
974 bool have_proto_max = false;
975 bool ip_vers = (info->family == NFPROTO_IPV6);
976
977 nla_for_each_nested(a, attr, rem) {
978 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
979 [OVS_NAT_ATTR_SRC] = {0, 0},
980 [OVS_NAT_ATTR_DST] = {0, 0},
981 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
982 sizeof(struct in6_addr)},
983 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
984 sizeof(struct in6_addr)},
985 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
986 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
987 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
988 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
989 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
990 };
991 int type = nla_type(a);
992
993 if (type > OVS_NAT_ATTR_MAX) {
994 OVS_NLERR(log,
995 "Unknown NAT attribute (type=%d, max=%d).\n",
996 type, OVS_NAT_ATTR_MAX);
997 return -EINVAL;
998 }
999
1000 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1001 OVS_NLERR(log,
1002 "NAT attribute type %d has unexpected length (%d != %d).\n",
1003 type, nla_len(a),
1004 ovs_nat_attr_lens[type][ip_vers]);
1005 return -EINVAL;
1006 }
1007
1008 switch (type) {
1009 case OVS_NAT_ATTR_SRC:
1010 case OVS_NAT_ATTR_DST:
1011 if (info->nat) {
1012 OVS_NLERR(log,
1013 "Only one type of NAT may be specified.\n"
1014 );
1015 return -ERANGE;
1016 }
1017 info->nat |= OVS_CT_NAT;
1018 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1019 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1020 break;
1021
1022 case OVS_NAT_ATTR_IP_MIN:
Haishuang Yanac71b462016-03-28 18:08:59 +08001023 nla_memcpy(&info->range.min_addr, a,
1024 sizeof(info->range.min_addr));
Jarno Rajahalme05752522016-03-10 10:54:23 -08001025 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1026 break;
1027
1028 case OVS_NAT_ATTR_IP_MAX:
1029 have_ip_max = true;
1030 nla_memcpy(&info->range.max_addr, a,
1031 sizeof(info->range.max_addr));
1032 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1033 break;
1034
1035 case OVS_NAT_ATTR_PROTO_MIN:
1036 info->range.min_proto.all = htons(nla_get_u16(a));
1037 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1038 break;
1039
1040 case OVS_NAT_ATTR_PROTO_MAX:
1041 have_proto_max = true;
1042 info->range.max_proto.all = htons(nla_get_u16(a));
1043 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1044 break;
1045
1046 case OVS_NAT_ATTR_PERSISTENT:
1047 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1048 break;
1049
1050 case OVS_NAT_ATTR_PROTO_HASH:
1051 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1052 break;
1053
1054 case OVS_NAT_ATTR_PROTO_RANDOM:
1055 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1056 break;
1057
1058 default:
1059 OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
1060 return -EINVAL;
1061 }
1062 }
1063
1064 if (rem > 0) {
1065 OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
1066 return -EINVAL;
1067 }
1068 if (!info->nat) {
1069 /* Do not allow flags if no type is given. */
1070 if (info->range.flags) {
1071 OVS_NLERR(log,
1072 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1073 );
1074 return -EINVAL;
1075 }
1076 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1077 } else if (!info->commit) {
1078 OVS_NLERR(log,
1079 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1080 );
1081 return -EINVAL;
1082 }
1083 /* Allow missing IP_MAX. */
1084 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1085 memcpy(&info->range.max_addr, &info->range.min_addr,
1086 sizeof(info->range.max_addr));
1087 }
1088 /* Allow missing PROTO_MAX. */
1089 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1090 !have_proto_max) {
1091 info->range.max_proto.all = info->range.min_proto.all;
1092 }
1093 return 0;
1094}
1095#endif
1096
Joe Stringer7f8a4362015-08-26 11:31:48 -07001097static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001098 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -07001099 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1100 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -07001101 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1102 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -07001103 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1104 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -07001105 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
Jarno Rajahalme05752522016-03-10 10:54:23 -08001106 .maxlen = NF_CT_HELPER_NAME_LEN },
1107#ifdef CONFIG_NF_NAT_NEEDED
1108 /* NAT length is checked when parsing the nested attributes. */
1109 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1110#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001111};
1112
1113static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -07001114 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001115{
1116 struct nlattr *a;
1117 int rem;
1118
1119 nla_for_each_nested(a, attr, rem) {
1120 int type = nla_type(a);
1121 int maxlen = ovs_ct_attr_lens[type].maxlen;
1122 int minlen = ovs_ct_attr_lens[type].minlen;
1123
1124 if (type > OVS_CT_ATTR_MAX) {
1125 OVS_NLERR(log,
1126 "Unknown conntrack attr (type=%d, max=%d)",
1127 type, OVS_CT_ATTR_MAX);
1128 return -EINVAL;
1129 }
1130 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1131 OVS_NLERR(log,
1132 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1133 type, nla_len(a), maxlen);
1134 return -EINVAL;
1135 }
1136
1137 switch (type) {
Joe Stringerab38a7b2015-10-06 11:00:01 -07001138 case OVS_CT_ATTR_COMMIT:
1139 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001140 break;
1141#ifdef CONFIG_NF_CONNTRACK_ZONES
1142 case OVS_CT_ATTR_ZONE:
1143 info->zone.id = nla_get_u16(a);
1144 break;
1145#endif
Joe Stringer182e3042015-08-26 11:31:49 -07001146#ifdef CONFIG_NF_CONNTRACK_MARK
1147 case OVS_CT_ATTR_MARK: {
1148 struct md_mark *mark = nla_data(a);
1149
Joe Stringere754ec62015-10-19 19:19:00 -07001150 if (!mark->mask) {
1151 OVS_NLERR(log, "ct_mark mask cannot be 0");
1152 return -EINVAL;
1153 }
Joe Stringer182e3042015-08-26 11:31:49 -07001154 info->mark = *mark;
1155 break;
1156 }
1157#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -07001158#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -07001159 case OVS_CT_ATTR_LABELS: {
1160 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001161
Joe Stringere754ec62015-10-19 19:19:00 -07001162 if (!labels_nonzero(&labels->mask)) {
1163 OVS_NLERR(log, "ct_labels mask cannot be 0");
1164 return -EINVAL;
1165 }
Joe Stringer33db4122015-10-01 15:00:37 -07001166 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001167 break;
1168 }
1169#endif
Joe Stringercae3a262015-08-26 11:31:53 -07001170 case OVS_CT_ATTR_HELPER:
1171 *helper = nla_data(a);
1172 if (!memchr(*helper, '\0', nla_len(a))) {
1173 OVS_NLERR(log, "Invalid conntrack helper");
1174 return -EINVAL;
1175 }
1176 break;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001177#ifdef CONFIG_NF_NAT_NEEDED
1178 case OVS_CT_ATTR_NAT: {
1179 int err = parse_nat(a, info, log);
1180
1181 if (err)
1182 return err;
1183 break;
1184 }
1185#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001186 default:
1187 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1188 type);
1189 return -EINVAL;
1190 }
1191 }
1192
Jarno Rajahalme7d904c72016-06-21 14:59:38 -07001193#ifdef CONFIG_NF_CONNTRACK_MARK
1194 if (!info->commit && info->mark.mask) {
1195 OVS_NLERR(log,
1196 "Setting conntrack mark requires 'commit' flag.");
1197 return -EINVAL;
1198 }
1199#endif
1200#ifdef CONFIG_NF_CONNTRACK_LABELS
1201 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1202 OVS_NLERR(log,
1203 "Setting conntrack labels requires 'commit' flag.");
1204 return -EINVAL;
1205 }
1206#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001207 if (rem > 0) {
1208 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1209 return -EINVAL;
1210 }
1211
1212 return 0;
1213}
1214
Joe Stringerc2ac6672015-08-26 11:31:52 -07001215bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -07001216{
1217 if (attr == OVS_KEY_ATTR_CT_STATE)
1218 return true;
1219 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1220 attr == OVS_KEY_ATTR_CT_ZONE)
1221 return true;
Joe Stringer182e3042015-08-26 11:31:49 -07001222 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1223 attr == OVS_KEY_ATTR_CT_MARK)
1224 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001225 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001226 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001227 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1228
1229 return ovs_net->xt_label;
1230 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001231
1232 return false;
1233}
1234
1235int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1236 const struct sw_flow_key *key,
1237 struct sw_flow_actions **sfa, bool log)
1238{
1239 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -07001240 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001241 u16 family;
1242 int err;
1243
1244 family = key_to_nfproto(key);
1245 if (family == NFPROTO_UNSPEC) {
1246 OVS_NLERR(log, "ct family unspecified");
1247 return -EINVAL;
1248 }
1249
1250 memset(&ct_info, 0, sizeof(ct_info));
1251 ct_info.family = family;
1252
1253 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1254 NF_CT_DEFAULT_ZONE_DIR, 0);
1255
Joe Stringercae3a262015-08-26 11:31:53 -07001256 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001257 if (err)
1258 return err;
1259
1260 /* Set up template for tracking connections in specific zones. */
1261 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1262 if (!ct_info.ct) {
1263 OVS_NLERR(log, "Failed to allocate conntrack template");
1264 return -ENOMEM;
1265 }
Joe Stringer90c7afc962015-12-23 14:39:27 -08001266
1267 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1268 nf_conntrack_get(&ct_info.ct->ct_general);
1269
Joe Stringercae3a262015-08-26 11:31:53 -07001270 if (helper) {
1271 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1272 if (err)
1273 goto err_free_ct;
1274 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001275
1276 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1277 sizeof(ct_info), log);
1278 if (err)
1279 goto err_free_ct;
1280
Joe Stringer7f8a4362015-08-26 11:31:48 -07001281 return 0;
1282err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001283 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001284 return err;
1285}
1286
Jarno Rajahalme05752522016-03-10 10:54:23 -08001287#ifdef CONFIG_NF_NAT_NEEDED
1288static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1289 struct sk_buff *skb)
1290{
1291 struct nlattr *start;
1292
1293 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1294 if (!start)
1295 return false;
1296
1297 if (info->nat & OVS_CT_SRC_NAT) {
1298 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1299 return false;
1300 } else if (info->nat & OVS_CT_DST_NAT) {
1301 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1302 return false;
1303 } else {
1304 goto out;
1305 }
1306
1307 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
Arnd Bergmann99b72482016-03-18 14:33:45 +01001308 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1309 info->family == NFPROTO_IPV4) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001310 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1311 info->range.min_addr.ip) ||
1312 (info->range.max_addr.ip
1313 != info->range.min_addr.ip &&
1314 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1315 info->range.max_addr.ip))))
1316 return false;
Arnd Bergmann99b72482016-03-18 14:33:45 +01001317 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1318 info->family == NFPROTO_IPV6) {
Jarno Rajahalme05752522016-03-10 10:54:23 -08001319 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1320 &info->range.min_addr.in6) ||
1321 (memcmp(&info->range.max_addr.in6,
1322 &info->range.min_addr.in6,
1323 sizeof(info->range.max_addr.in6)) &&
1324 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1325 &info->range.max_addr.in6))))
1326 return false;
Jarno Rajahalme05752522016-03-10 10:54:23 -08001327 } else {
1328 return false;
1329 }
1330 }
1331 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1332 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1333 ntohs(info->range.min_proto.all)) ||
1334 (info->range.max_proto.all != info->range.min_proto.all &&
1335 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1336 ntohs(info->range.max_proto.all)))))
1337 return false;
1338
1339 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1340 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1341 return false;
1342 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1343 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
1344 return false;
1345 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1346 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1347 return false;
1348out:
1349 nla_nest_end(skb, start);
1350
1351 return true;
1352}
1353#endif
1354
Joe Stringer7f8a4362015-08-26 11:31:48 -07001355int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1356 struct sk_buff *skb)
1357{
1358 struct nlattr *start;
1359
1360 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1361 if (!start)
1362 return -EMSGSIZE;
1363
Joe Stringerab38a7b2015-10-06 11:00:01 -07001364 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001365 return -EMSGSIZE;
1366 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1367 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1368 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -07001369 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -07001370 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1371 &ct_info->mark))
1372 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001373 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -07001374 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -07001375 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1376 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -07001377 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -07001378 if (ct_info->helper) {
1379 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1380 ct_info->helper->name))
1381 return -EMSGSIZE;
1382 }
Jarno Rajahalme05752522016-03-10 10:54:23 -08001383#ifdef CONFIG_NF_NAT_NEEDED
1384 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1385 return -EMSGSIZE;
1386#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -07001387 nla_nest_end(skb, start);
1388
1389 return 0;
1390}
1391
1392void ovs_ct_free_action(const struct nlattr *a)
1393{
1394 struct ovs_conntrack_info *ct_info = nla_data(a);
1395
Joe Stringer2f3ab9f2015-12-09 14:07:39 -08001396 __ovs_ct_free_action(ct_info);
1397}
1398
1399static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1400{
Joe Stringercae3a262015-08-26 11:31:53 -07001401 if (ct_info->helper)
1402 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001403 if (ct_info->ct)
Joe Stringer76644232016-09-01 18:01:47 -07001404 nf_ct_tmpl_free(ct_info->ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001405}
Joe Stringerc2ac6672015-08-26 11:31:52 -07001406
1407void ovs_ct_init(struct net *net)
1408{
Joe Stringer33db4122015-10-01 15:00:37 -07001409 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001410 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1411
Florian Westphaladff6c62016-04-12 18:14:25 +02001412 if (nf_connlabels_get(net, n_bits - 1)) {
Joe Stringerc2ac6672015-08-26 11:31:52 -07001413 ovs_net->xt_label = false;
1414 OVS_NLERR(true, "Failed to set connlabel length");
1415 } else {
1416 ovs_net->xt_label = true;
1417 }
1418}
1419
1420void ovs_ct_exit(struct net *net)
1421{
1422 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1423
1424 if (ovs_net->xt_label)
1425 nf_connlabels_put(net);
1426}