blob: 585a5aa81f89925f2877d639efa96545a5a173fe [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>
16#include <net/ip.h>
17#include <net/netfilter/nf_conntrack_core.h>
Joe Stringercae3a262015-08-26 11:31:53 -070018#include <net/netfilter/nf_conntrack_helper.h>
Joe Stringerc2ac6672015-08-26 11:31:52 -070019#include <net/netfilter/nf_conntrack_labels.h>
Joe Stringer7f8a4362015-08-26 11:31:48 -070020#include <net/netfilter/nf_conntrack_zones.h>
21#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
22
23#include "datapath.h"
24#include "conntrack.h"
25#include "flow.h"
26#include "flow_netlink.h"
27
28struct ovs_ct_len_tbl {
29 size_t maxlen;
30 size_t minlen;
31};
32
Joe Stringer182e3042015-08-26 11:31:49 -070033/* Metadata mark for masked write to conntrack mark */
34struct md_mark {
35 u32 value;
36 u32 mask;
37};
38
Joe Stringerc2ac6672015-08-26 11:31:52 -070039/* Metadata label for masked write to conntrack label. */
Joe Stringer33db4122015-10-01 15:00:37 -070040struct md_labels {
41 struct ovs_key_ct_labels value;
42 struct ovs_key_ct_labels mask;
Joe Stringerc2ac6672015-08-26 11:31:52 -070043};
44
Joe Stringer7f8a4362015-08-26 11:31:48 -070045/* Conntrack action context for execution. */
46struct ovs_conntrack_info {
Joe Stringercae3a262015-08-26 11:31:53 -070047 struct nf_conntrack_helper *helper;
Joe Stringer7f8a4362015-08-26 11:31:48 -070048 struct nf_conntrack_zone zone;
49 struct nf_conn *ct;
Joe Stringerab38a7b2015-10-06 11:00:01 -070050 u8 commit : 1;
Joe Stringer7f8a4362015-08-26 11:31:48 -070051 u16 family;
Joe Stringer182e3042015-08-26 11:31:49 -070052 struct md_mark mark;
Joe Stringer33db4122015-10-01 15:00:37 -070053 struct md_labels labels;
Joe Stringer7f8a4362015-08-26 11:31:48 -070054};
55
Joe Stringer2f3ab9f2015-12-09 14:07:39 -080056static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
57
Joe Stringer7f8a4362015-08-26 11:31:48 -070058static u16 key_to_nfproto(const struct sw_flow_key *key)
59{
60 switch (ntohs(key->eth.type)) {
61 case ETH_P_IP:
62 return NFPROTO_IPV4;
63 case ETH_P_IPV6:
64 return NFPROTO_IPV6;
65 default:
66 return NFPROTO_UNSPEC;
67 }
68}
69
70/* Map SKB connection state into the values used by flow definition. */
71static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
72{
73 u8 ct_state = OVS_CS_F_TRACKED;
74
75 switch (ctinfo) {
76 case IP_CT_ESTABLISHED_REPLY:
77 case IP_CT_RELATED_REPLY:
78 case IP_CT_NEW_REPLY:
79 ct_state |= OVS_CS_F_REPLY_DIR;
80 break;
81 default:
82 break;
83 }
84
85 switch (ctinfo) {
86 case IP_CT_ESTABLISHED:
87 case IP_CT_ESTABLISHED_REPLY:
88 ct_state |= OVS_CS_F_ESTABLISHED;
89 break;
90 case IP_CT_RELATED:
91 case IP_CT_RELATED_REPLY:
92 ct_state |= OVS_CS_F_RELATED;
93 break;
94 case IP_CT_NEW:
95 case IP_CT_NEW_REPLY:
96 ct_state |= OVS_CS_F_NEW;
97 break;
98 default:
99 break;
100 }
101
102 return ct_state;
103}
104
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700105static u32 ovs_ct_get_mark(const struct nf_conn *ct)
106{
107#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
108 return ct ? ct->mark : 0;
109#else
110 return 0;
111#endif
112}
113
Joe Stringer33db4122015-10-01 15:00:37 -0700114static void ovs_ct_get_labels(const struct nf_conn *ct,
115 struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700116{
117 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
118
119 if (cl) {
120 size_t len = cl->words * sizeof(long);
121
Joe Stringer33db4122015-10-01 15:00:37 -0700122 if (len > OVS_CT_LABELS_LEN)
123 len = OVS_CT_LABELS_LEN;
124 else if (len < OVS_CT_LABELS_LEN)
125 memset(labels, 0, OVS_CT_LABELS_LEN);
126 memcpy(labels, cl->bits, len);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700127 } else {
Joe Stringer33db4122015-10-01 15:00:37 -0700128 memset(labels, 0, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700129 }
130}
131
Joe Stringer7f8a4362015-08-26 11:31:48 -0700132static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
Joe Stringer182e3042015-08-26 11:31:49 -0700133 const struct nf_conntrack_zone *zone,
134 const struct nf_conn *ct)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700135{
136 key->ct.state = state;
137 key->ct.zone = zone->id;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700138 key->ct.mark = ovs_ct_get_mark(ct);
Joe Stringer33db4122015-10-01 15:00:37 -0700139 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700140}
141
142/* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
143 * previously sent the packet to conntrack via the ct action.
144 */
145static void ovs_ct_update_key(const struct sk_buff *skb,
146 struct sw_flow_key *key, bool post_ct)
147{
148 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
149 enum ip_conntrack_info ctinfo;
150 struct nf_conn *ct;
151 u8 state = 0;
152
153 ct = nf_ct_get(skb, &ctinfo);
154 if (ct) {
155 state = ovs_ct_get_state(ctinfo);
Joe Stringer4f0909e2015-10-19 19:18:59 -0700156 if (!nf_ct_is_confirmed(ct))
157 state |= OVS_CS_F_NEW;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700158 if (ct->master)
159 state |= OVS_CS_F_RELATED;
160 zone = nf_ct_zone(ct);
161 } else if (post_ct) {
162 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
163 }
Joe Stringer182e3042015-08-26 11:31:49 -0700164 __ovs_ct_update_key(key, state, zone, ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700165}
166
167void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
168{
169 ovs_ct_update_key(skb, key, false);
170}
171
172int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
173{
Joe Stringerfbccce52015-10-06 11:00:00 -0700174 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700175 return -EMSGSIZE;
176
177 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
178 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
179 return -EMSGSIZE;
180
Joe Stringer182e3042015-08-26 11:31:49 -0700181 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
182 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
183 return -EMSGSIZE;
184
Valentin Rothberg9723e6a2015-08-28 10:39:56 +0200185 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700186 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
187 &key->ct.labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700188 return -EMSGSIZE;
189
Joe Stringer182e3042015-08-26 11:31:49 -0700190 return 0;
191}
192
193static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
194 u32 ct_mark, u32 mask)
195{
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700196#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
Joe Stringer182e3042015-08-26 11:31:49 -0700197 enum ip_conntrack_info ctinfo;
198 struct nf_conn *ct;
199 u32 new_mark;
200
Joe Stringer182e3042015-08-26 11:31:49 -0700201
202 /* The connection could be invalid, in which case set_mark is no-op. */
203 ct = nf_ct_get(skb, &ctinfo);
204 if (!ct)
205 return 0;
206
207 new_mark = ct_mark | (ct->mark & ~(mask));
208 if (ct->mark != new_mark) {
209 ct->mark = new_mark;
210 nf_conntrack_event_cache(IPCT_MARK, ct);
211 key->ct.mark = new_mark;
212 }
213
Joe Stringer7f8a4362015-08-26 11:31:48 -0700214 return 0;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700215#else
216 return -ENOTSUPP;
217#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -0700218}
219
Joe Stringer33db4122015-10-01 15:00:37 -0700220static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
221 const struct ovs_key_ct_labels *labels,
222 const struct ovs_key_ct_labels *mask)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700223{
224 enum ip_conntrack_info ctinfo;
225 struct nf_conn_labels *cl;
226 struct nf_conn *ct;
227 int err;
228
Joe Stringerc2ac6672015-08-26 11:31:52 -0700229 /* The connection could be invalid, in which case set_label is no-op.*/
230 ct = nf_ct_get(skb, &ctinfo);
231 if (!ct)
232 return 0;
233
234 cl = nf_ct_labels_find(ct);
235 if (!cl) {
236 nf_ct_labels_ext_add(ct);
237 cl = nf_ct_labels_find(ct);
238 }
Joe Stringer33db4122015-10-01 15:00:37 -0700239 if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700240 return -ENOSPC;
241
Joe Stringer33db4122015-10-01 15:00:37 -0700242 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
243 OVS_CT_LABELS_LEN / sizeof(u32));
Joe Stringerc2ac6672015-08-26 11:31:52 -0700244 if (err)
245 return err;
246
Joe Stringer33db4122015-10-01 15:00:37 -0700247 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700248 return 0;
249}
250
Joe Stringercae3a262015-08-26 11:31:53 -0700251/* 'skb' should already be pulled to nh_ofs. */
252static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
253{
254 const struct nf_conntrack_helper *helper;
255 const struct nf_conn_help *help;
256 enum ip_conntrack_info ctinfo;
257 unsigned int protoff;
258 struct nf_conn *ct;
259
260 ct = nf_ct_get(skb, &ctinfo);
261 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
262 return NF_ACCEPT;
263
264 help = nfct_help(ct);
265 if (!help)
266 return NF_ACCEPT;
267
268 helper = rcu_dereference(help->helper);
269 if (!helper)
270 return NF_ACCEPT;
271
272 switch (proto) {
273 case NFPROTO_IPV4:
274 protoff = ip_hdrlen(skb);
275 break;
276 case NFPROTO_IPV6: {
277 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
278 __be16 frag_off;
Joe Stringercc570602015-09-14 11:14:50 -0700279 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700280
Joe Stringercc570602015-09-14 11:14:50 -0700281 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
282 &frag_off);
283 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700284 pr_debug("proto header not found\n");
285 return NF_ACCEPT;
286 }
Joe Stringercc570602015-09-14 11:14:50 -0700287 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700288 break;
289 }
290 default:
291 WARN_ONCE(1, "helper invoked on non-IP family!");
292 return NF_DROP;
293 }
294
295 return helper->help(skb, protoff, ct, ctinfo);
296}
297
Joe Stringer74c16612015-10-25 20:21:48 -0700298/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
299 * value if 'skb' is freed.
300 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700301static int handle_fragments(struct net *net, struct sw_flow_key *key,
302 u16 zone, struct sk_buff *skb)
303{
304 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
305
306 if (key->eth.type == htons(ETH_P_IP)) {
307 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
308 int err;
309
310 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500311 err = ip_defrag(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700312 if (err)
313 return err;
314
315 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700316#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700317 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700318 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
319 struct sk_buff *reasm;
320
321 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
Eric W. Biedermanb7277592015-10-09 13:44:55 -0500322 reasm = nf_ct_frag6_gather(net, skb, user);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700323 if (!reasm)
324 return -EINPROGRESS;
325
Joe Stringer74c16612015-10-25 20:21:48 -0700326 if (skb == reasm) {
327 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700328 return -EINVAL;
Joe Stringer74c16612015-10-25 20:21:48 -0700329 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700330
Joe Stringer6f5cade2015-10-25 20:21:50 -0700331 /* Don't free 'skb' even though it is one of the original
332 * fragments, as we're going to morph it into the head.
333 */
334 skb_get(skb);
335 nf_ct_frag6_consume_orig(reasm);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700336
337 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
338 skb_morph(skb, reasm);
Joe Stringer6f5cade2015-10-25 20:21:50 -0700339 skb->next = reasm->next;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700340 consume_skb(reasm);
341 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700342#endif
343 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700344 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700345 return -EPFNOSUPPORT;
346 }
347
348 key->ip.frag = OVS_FRAG_TYPE_NONE;
349 skb_clear_hash(skb);
350 skb->ignore_df = 1;
351 *OVS_CB(skb) = ovs_cb;
352
353 return 0;
354}
355
356static struct nf_conntrack_expect *
357ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
358 u16 proto, const struct sk_buff *skb)
359{
360 struct nf_conntrack_tuple tuple;
361
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500362 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700363 return NULL;
364 return __nf_ct_expect_find(net, zone, &tuple);
365}
366
367/* Determine whether skb->nfct is equal to the result of conntrack lookup. */
368static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
369 const struct ovs_conntrack_info *info)
370{
371 enum ip_conntrack_info ctinfo;
372 struct nf_conn *ct;
373
374 ct = nf_ct_get(skb, &ctinfo);
375 if (!ct)
376 return false;
377 if (!net_eq(net, read_pnet(&ct->ct_net)))
378 return false;
379 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
380 return false;
Joe Stringercae3a262015-08-26 11:31:53 -0700381 if (info->helper) {
382 struct nf_conn_help *help;
383
384 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
385 if (help && rcu_access_pointer(help->helper) != info->helper)
386 return false;
387 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700388
389 return true;
390}
391
Joe Stringer4f0909e2015-10-19 19:18:59 -0700392static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700393 const struct ovs_conntrack_info *info,
394 struct sk_buff *skb)
395{
396 /* If we are recirculating packets to match on conntrack fields and
397 * committing with a separate conntrack action, then we don't need to
398 * actually run the packet through conntrack twice unless it's for a
399 * different zone.
400 */
401 if (!skb_nfct_cached(net, skb, info)) {
402 struct nf_conn *tmpl = info->ct;
403
404 /* Associate skb with specified zone. */
405 if (tmpl) {
406 if (skb->nfct)
407 nf_conntrack_put(skb->nfct);
408 nf_conntrack_get(&tmpl->ct_general);
409 skb->nfct = &tmpl->ct_general;
410 skb->nfctinfo = IP_CT_NEW;
411 }
412
413 if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
414 skb) != NF_ACCEPT)
415 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700416
417 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
418 WARN_ONCE(1, "helper rejected packet");
419 return -EINVAL;
420 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700421 }
422
Joe Stringer4f0909e2015-10-19 19:18:59 -0700423 ovs_ct_update_key(skb, key, true);
424
Joe Stringer7f8a4362015-08-26 11:31:48 -0700425 return 0;
426}
427
428/* Lookup connection and read fields into key. */
429static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
430 const struct ovs_conntrack_info *info,
431 struct sk_buff *skb)
432{
433 struct nf_conntrack_expect *exp;
434
435 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
436 if (exp) {
437 u8 state;
438
439 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700440 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700441 } else {
442 int err;
443
444 err = __ovs_ct_lookup(net, key, info, skb);
445 if (err)
446 return err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700447 }
448
449 return 0;
450}
451
452/* Lookup connection and confirm if unconfirmed. */
453static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
454 const struct ovs_conntrack_info *info,
455 struct sk_buff *skb)
456{
457 u8 state;
458 int err;
459
460 state = key->ct.state;
461 if (key->ct.zone == info->zone.id &&
462 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
463 /* Previous lookup has shown that this connection is already
464 * tracked and committed. Skip committing.
465 */
466 return 0;
467 }
468
469 err = __ovs_ct_lookup(net, key, info, skb);
470 if (err)
471 return err;
472 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
473 return -EINVAL;
474
Joe Stringer7f8a4362015-08-26 11:31:48 -0700475 return 0;
476}
477
Joe Stringer33db4122015-10-01 15:00:37 -0700478static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700479{
480 size_t i;
481
Joe Stringer33db4122015-10-01 15:00:37 -0700482 for (i = 0; i < sizeof(*labels); i++)
483 if (labels->ct_labels[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700484 return true;
485
486 return false;
487}
488
Joe Stringer74c16612015-10-25 20:21:48 -0700489/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
490 * value if 'skb' is freed.
491 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700492int ovs_ct_execute(struct net *net, struct sk_buff *skb,
493 struct sw_flow_key *key,
494 const struct ovs_conntrack_info *info)
495{
496 int nh_ofs;
497 int err;
498
499 /* The conntrack module expects to be working at L3. */
500 nh_ofs = skb_network_offset(skb);
501 skb_pull(skb, nh_ofs);
502
503 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
504 err = handle_fragments(net, key, info->zone.id, skb);
505 if (err)
506 return err;
507 }
508
Joe Stringerab38a7b2015-10-06 11:00:01 -0700509 if (info->commit)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700510 err = ovs_ct_commit(net, key, info, skb);
511 else
512 err = ovs_ct_lookup(net, key, info, skb);
Joe Stringer182e3042015-08-26 11:31:49 -0700513 if (err)
514 goto err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700515
Joe Stringerc2ac6672015-08-26 11:31:52 -0700516 if (info->mark.mask) {
Joe Stringer182e3042015-08-26 11:31:49 -0700517 err = ovs_ct_set_mark(skb, key, info->mark.value,
518 info->mark.mask);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700519 if (err)
520 goto err;
521 }
Joe Stringer33db4122015-10-01 15:00:37 -0700522 if (labels_nonzero(&info->labels.mask))
523 err = ovs_ct_set_labels(skb, key, &info->labels.value,
524 &info->labels.mask);
Joe Stringer182e3042015-08-26 11:31:49 -0700525err:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700526 skb_push(skb, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -0700527 if (err)
528 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700529 return err;
530}
531
Joe Stringercae3a262015-08-26 11:31:53 -0700532static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
533 const struct sw_flow_key *key, bool log)
534{
535 struct nf_conntrack_helper *helper;
536 struct nf_conn_help *help;
537
538 helper = nf_conntrack_helper_try_module_get(name, info->family,
539 key->ip.proto);
540 if (!helper) {
541 OVS_NLERR(log, "Unknown helper \"%s\"", name);
542 return -EINVAL;
543 }
544
545 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
546 if (!help) {
547 module_put(helper->me);
548 return -ENOMEM;
549 }
550
551 rcu_assign_pointer(help->helper, helper);
552 info->helper = helper;
553 return 0;
554}
555
Joe Stringer7f8a4362015-08-26 11:31:48 -0700556static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -0700557 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700558 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
559 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700560 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
561 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -0700562 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
563 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -0700564 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
565 .maxlen = NF_CT_HELPER_NAME_LEN }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700566};
567
568static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -0700569 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700570{
571 struct nlattr *a;
572 int rem;
573
574 nla_for_each_nested(a, attr, rem) {
575 int type = nla_type(a);
576 int maxlen = ovs_ct_attr_lens[type].maxlen;
577 int minlen = ovs_ct_attr_lens[type].minlen;
578
579 if (type > OVS_CT_ATTR_MAX) {
580 OVS_NLERR(log,
581 "Unknown conntrack attr (type=%d, max=%d)",
582 type, OVS_CT_ATTR_MAX);
583 return -EINVAL;
584 }
585 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
586 OVS_NLERR(log,
587 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
588 type, nla_len(a), maxlen);
589 return -EINVAL;
590 }
591
592 switch (type) {
Joe Stringerab38a7b2015-10-06 11:00:01 -0700593 case OVS_CT_ATTR_COMMIT:
594 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700595 break;
596#ifdef CONFIG_NF_CONNTRACK_ZONES
597 case OVS_CT_ATTR_ZONE:
598 info->zone.id = nla_get_u16(a);
599 break;
600#endif
Joe Stringer182e3042015-08-26 11:31:49 -0700601#ifdef CONFIG_NF_CONNTRACK_MARK
602 case OVS_CT_ATTR_MARK: {
603 struct md_mark *mark = nla_data(a);
604
Joe Stringere754ec62015-10-19 19:19:00 -0700605 if (!mark->mask) {
606 OVS_NLERR(log, "ct_mark mask cannot be 0");
607 return -EINVAL;
608 }
Joe Stringer182e3042015-08-26 11:31:49 -0700609 info->mark = *mark;
610 break;
611 }
612#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -0700613#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -0700614 case OVS_CT_ATTR_LABELS: {
615 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700616
Joe Stringere754ec62015-10-19 19:19:00 -0700617 if (!labels_nonzero(&labels->mask)) {
618 OVS_NLERR(log, "ct_labels mask cannot be 0");
619 return -EINVAL;
620 }
Joe Stringer33db4122015-10-01 15:00:37 -0700621 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700622 break;
623 }
624#endif
Joe Stringercae3a262015-08-26 11:31:53 -0700625 case OVS_CT_ATTR_HELPER:
626 *helper = nla_data(a);
627 if (!memchr(*helper, '\0', nla_len(a))) {
628 OVS_NLERR(log, "Invalid conntrack helper");
629 return -EINVAL;
630 }
631 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700632 default:
633 OVS_NLERR(log, "Unknown conntrack attr (%d)",
634 type);
635 return -EINVAL;
636 }
637 }
638
639 if (rem > 0) {
640 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
641 return -EINVAL;
642 }
643
644 return 0;
645}
646
Joe Stringerc2ac6672015-08-26 11:31:52 -0700647bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700648{
649 if (attr == OVS_KEY_ATTR_CT_STATE)
650 return true;
651 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
652 attr == OVS_KEY_ATTR_CT_ZONE)
653 return true;
Joe Stringer182e3042015-08-26 11:31:49 -0700654 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
655 attr == OVS_KEY_ATTR_CT_MARK)
656 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700657 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700658 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -0700659 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
660
661 return ovs_net->xt_label;
662 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700663
664 return false;
665}
666
667int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
668 const struct sw_flow_key *key,
669 struct sw_flow_actions **sfa, bool log)
670{
671 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -0700672 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700673 u16 family;
674 int err;
675
676 family = key_to_nfproto(key);
677 if (family == NFPROTO_UNSPEC) {
678 OVS_NLERR(log, "ct family unspecified");
679 return -EINVAL;
680 }
681
682 memset(&ct_info, 0, sizeof(ct_info));
683 ct_info.family = family;
684
685 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
686 NF_CT_DEFAULT_ZONE_DIR, 0);
687
Joe Stringercae3a262015-08-26 11:31:53 -0700688 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700689 if (err)
690 return err;
691
692 /* Set up template for tracking connections in specific zones. */
693 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
694 if (!ct_info.ct) {
695 OVS_NLERR(log, "Failed to allocate conntrack template");
696 return -ENOMEM;
697 }
Joe Stringercae3a262015-08-26 11:31:53 -0700698 if (helper) {
699 err = ovs_ct_add_helper(&ct_info, helper, key, log);
700 if (err)
701 goto err_free_ct;
702 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700703
704 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
705 sizeof(ct_info), log);
706 if (err)
707 goto err_free_ct;
708
709 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
710 nf_conntrack_get(&ct_info.ct->ct_general);
711 return 0;
712err_free_ct:
Joe Stringer2f3ab9f2015-12-09 14:07:39 -0800713 __ovs_ct_free_action(&ct_info);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700714 return err;
715}
716
717int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
718 struct sk_buff *skb)
719{
720 struct nlattr *start;
721
722 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
723 if (!start)
724 return -EMSGSIZE;
725
Joe Stringerab38a7b2015-10-06 11:00:01 -0700726 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700727 return -EMSGSIZE;
728 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
729 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
730 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -0700731 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -0700732 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
733 &ct_info->mark))
734 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700735 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -0700736 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700737 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
738 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700739 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -0700740 if (ct_info->helper) {
741 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
742 ct_info->helper->name))
743 return -EMSGSIZE;
744 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700745
746 nla_nest_end(skb, start);
747
748 return 0;
749}
750
751void ovs_ct_free_action(const struct nlattr *a)
752{
753 struct ovs_conntrack_info *ct_info = nla_data(a);
754
Joe Stringer2f3ab9f2015-12-09 14:07:39 -0800755 __ovs_ct_free_action(ct_info);
756}
757
758static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
759{
Joe Stringercae3a262015-08-26 11:31:53 -0700760 if (ct_info->helper)
761 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700762 if (ct_info->ct)
763 nf_ct_put(ct_info->ct);
764}
Joe Stringerc2ac6672015-08-26 11:31:52 -0700765
766void ovs_ct_init(struct net *net)
767{
Joe Stringer33db4122015-10-01 15:00:37 -0700768 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700769 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
770
771 if (nf_connlabels_get(net, n_bits)) {
772 ovs_net->xt_label = false;
773 OVS_NLERR(true, "Failed to set connlabel length");
774 } else {
775 ovs_net->xt_label = true;
776 }
777}
778
779void ovs_ct_exit(struct net *net)
780{
781 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
782
783 if (ovs_net->xt_label)
784 nf_connlabels_put(net);
785}