blob: b5dcc0abde6607d2e6e819d75319f78604509555 [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
56static u16 key_to_nfproto(const struct sw_flow_key *key)
57{
58 switch (ntohs(key->eth.type)) {
59 case ETH_P_IP:
60 return NFPROTO_IPV4;
61 case ETH_P_IPV6:
62 return NFPROTO_IPV6;
63 default:
64 return NFPROTO_UNSPEC;
65 }
66}
67
68/* Map SKB connection state into the values used by flow definition. */
69static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
70{
71 u8 ct_state = OVS_CS_F_TRACKED;
72
73 switch (ctinfo) {
74 case IP_CT_ESTABLISHED_REPLY:
75 case IP_CT_RELATED_REPLY:
76 case IP_CT_NEW_REPLY:
77 ct_state |= OVS_CS_F_REPLY_DIR;
78 break;
79 default:
80 break;
81 }
82
83 switch (ctinfo) {
84 case IP_CT_ESTABLISHED:
85 case IP_CT_ESTABLISHED_REPLY:
86 ct_state |= OVS_CS_F_ESTABLISHED;
87 break;
88 case IP_CT_RELATED:
89 case IP_CT_RELATED_REPLY:
90 ct_state |= OVS_CS_F_RELATED;
91 break;
92 case IP_CT_NEW:
93 case IP_CT_NEW_REPLY:
94 ct_state |= OVS_CS_F_NEW;
95 break;
96 default:
97 break;
98 }
99
100 return ct_state;
101}
102
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700103static u32 ovs_ct_get_mark(const struct nf_conn *ct)
104{
105#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
106 return ct ? ct->mark : 0;
107#else
108 return 0;
109#endif
110}
111
Joe Stringer33db4122015-10-01 15:00:37 -0700112static void ovs_ct_get_labels(const struct nf_conn *ct,
113 struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700114{
115 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
116
117 if (cl) {
118 size_t len = cl->words * sizeof(long);
119
Joe Stringer33db4122015-10-01 15:00:37 -0700120 if (len > OVS_CT_LABELS_LEN)
121 len = OVS_CT_LABELS_LEN;
122 else if (len < OVS_CT_LABELS_LEN)
123 memset(labels, 0, OVS_CT_LABELS_LEN);
124 memcpy(labels, cl->bits, len);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700125 } else {
Joe Stringer33db4122015-10-01 15:00:37 -0700126 memset(labels, 0, OVS_CT_LABELS_LEN);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700127 }
128}
129
Joe Stringer7f8a4362015-08-26 11:31:48 -0700130static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
Joe Stringer182e3042015-08-26 11:31:49 -0700131 const struct nf_conntrack_zone *zone,
132 const struct nf_conn *ct)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700133{
134 key->ct.state = state;
135 key->ct.zone = zone->id;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700136 key->ct.mark = ovs_ct_get_mark(ct);
Joe Stringer33db4122015-10-01 15:00:37 -0700137 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700138}
139
140/* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
141 * previously sent the packet to conntrack via the ct action.
142 */
143static void ovs_ct_update_key(const struct sk_buff *skb,
144 struct sw_flow_key *key, bool post_ct)
145{
146 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
147 enum ip_conntrack_info ctinfo;
148 struct nf_conn *ct;
149 u8 state = 0;
150
151 ct = nf_ct_get(skb, &ctinfo);
152 if (ct) {
153 state = ovs_ct_get_state(ctinfo);
Joe Stringer4f0909e2015-10-19 19:18:59 -0700154 if (!nf_ct_is_confirmed(ct))
155 state |= OVS_CS_F_NEW;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700156 if (ct->master)
157 state |= OVS_CS_F_RELATED;
158 zone = nf_ct_zone(ct);
159 } else if (post_ct) {
160 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
161 }
Joe Stringer182e3042015-08-26 11:31:49 -0700162 __ovs_ct_update_key(key, state, zone, ct);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700163}
164
165void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
166{
167 ovs_ct_update_key(skb, key, false);
168}
169
170int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
171{
Joe Stringerfbccce52015-10-06 11:00:00 -0700172 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700173 return -EMSGSIZE;
174
175 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
176 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
177 return -EMSGSIZE;
178
Joe Stringer182e3042015-08-26 11:31:49 -0700179 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
180 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
181 return -EMSGSIZE;
182
Valentin Rothberg9723e6a2015-08-28 10:39:56 +0200183 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700184 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
185 &key->ct.labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700186 return -EMSGSIZE;
187
Joe Stringer182e3042015-08-26 11:31:49 -0700188 return 0;
189}
190
191static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
192 u32 ct_mark, u32 mask)
193{
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700194#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
Joe Stringer182e3042015-08-26 11:31:49 -0700195 enum ip_conntrack_info ctinfo;
196 struct nf_conn *ct;
197 u32 new_mark;
198
Joe Stringer182e3042015-08-26 11:31:49 -0700199
200 /* The connection could be invalid, in which case set_mark is no-op. */
201 ct = nf_ct_get(skb, &ctinfo);
202 if (!ct)
203 return 0;
204
205 new_mark = ct_mark | (ct->mark & ~(mask));
206 if (ct->mark != new_mark) {
207 ct->mark = new_mark;
208 nf_conntrack_event_cache(IPCT_MARK, ct);
209 key->ct.mark = new_mark;
210 }
211
Joe Stringer7f8a4362015-08-26 11:31:48 -0700212 return 0;
Joe Stringer0d5cdef2015-08-28 19:22:11 -0700213#else
214 return -ENOTSUPP;
215#endif
Joe Stringer7f8a4362015-08-26 11:31:48 -0700216}
217
Joe Stringer33db4122015-10-01 15:00:37 -0700218static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
219 const struct ovs_key_ct_labels *labels,
220 const struct ovs_key_ct_labels *mask)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700221{
222 enum ip_conntrack_info ctinfo;
223 struct nf_conn_labels *cl;
224 struct nf_conn *ct;
225 int err;
226
Joe Stringerc2ac6672015-08-26 11:31:52 -0700227 /* The connection could be invalid, in which case set_label is no-op.*/
228 ct = nf_ct_get(skb, &ctinfo);
229 if (!ct)
230 return 0;
231
232 cl = nf_ct_labels_find(ct);
233 if (!cl) {
234 nf_ct_labels_ext_add(ct);
235 cl = nf_ct_labels_find(ct);
236 }
Joe Stringer33db4122015-10-01 15:00:37 -0700237 if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700238 return -ENOSPC;
239
Joe Stringer33db4122015-10-01 15:00:37 -0700240 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
241 OVS_CT_LABELS_LEN / sizeof(u32));
Joe Stringerc2ac6672015-08-26 11:31:52 -0700242 if (err)
243 return err;
244
Joe Stringer33db4122015-10-01 15:00:37 -0700245 ovs_ct_get_labels(ct, &key->ct.labels);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700246 return 0;
247}
248
Joe Stringercae3a262015-08-26 11:31:53 -0700249/* 'skb' should already be pulled to nh_ofs. */
250static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
251{
252 const struct nf_conntrack_helper *helper;
253 const struct nf_conn_help *help;
254 enum ip_conntrack_info ctinfo;
255 unsigned int protoff;
256 struct nf_conn *ct;
257
258 ct = nf_ct_get(skb, &ctinfo);
259 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
260 return NF_ACCEPT;
261
262 help = nfct_help(ct);
263 if (!help)
264 return NF_ACCEPT;
265
266 helper = rcu_dereference(help->helper);
267 if (!helper)
268 return NF_ACCEPT;
269
270 switch (proto) {
271 case NFPROTO_IPV4:
272 protoff = ip_hdrlen(skb);
273 break;
274 case NFPROTO_IPV6: {
275 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
276 __be16 frag_off;
Joe Stringercc570602015-09-14 11:14:50 -0700277 int ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700278
Joe Stringercc570602015-09-14 11:14:50 -0700279 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
280 &frag_off);
281 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
Joe Stringercae3a262015-08-26 11:31:53 -0700282 pr_debug("proto header not found\n");
283 return NF_ACCEPT;
284 }
Joe Stringercc570602015-09-14 11:14:50 -0700285 protoff = ofs;
Joe Stringercae3a262015-08-26 11:31:53 -0700286 break;
287 }
288 default:
289 WARN_ONCE(1, "helper invoked on non-IP family!");
290 return NF_DROP;
291 }
292
293 return helper->help(skb, protoff, ct, ctinfo);
294}
295
Joe Stringer74c16612015-10-25 20:21:48 -0700296/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
297 * value if 'skb' is freed.
298 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700299static int handle_fragments(struct net *net, struct sw_flow_key *key,
300 u16 zone, struct sk_buff *skb)
301{
302 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
303
304 if (key->eth.type == htons(ETH_P_IP)) {
305 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
306 int err;
307
308 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
309 err = ip_defrag(skb, user);
310 if (err)
311 return err;
312
313 ovs_cb.mru = IPCB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700314#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
Joe Stringer74c16612015-10-25 20:21:48 -0700315 } else if (key->eth.type == htons(ETH_P_IPV6)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700316 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
317 struct sk_buff *reasm;
318
319 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
320 reasm = nf_ct_frag6_gather(skb, user);
321 if (!reasm)
322 return -EINPROGRESS;
323
Joe Stringer74c16612015-10-25 20:21:48 -0700324 if (skb == reasm) {
325 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700326 return -EINVAL;
Joe Stringer74c16612015-10-25 20:21:48 -0700327 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700328
329 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
330 skb_morph(skb, reasm);
331 consume_skb(reasm);
332 ovs_cb.mru = IP6CB(skb)->frag_max_size;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700333#endif
334 } else {
Joe Stringer74c16612015-10-25 20:21:48 -0700335 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700336 return -EPFNOSUPPORT;
337 }
338
339 key->ip.frag = OVS_FRAG_TYPE_NONE;
340 skb_clear_hash(skb);
341 skb->ignore_df = 1;
342 *OVS_CB(skb) = ovs_cb;
343
344 return 0;
345}
346
347static struct nf_conntrack_expect *
348ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
349 u16 proto, const struct sk_buff *skb)
350{
351 struct nf_conntrack_tuple tuple;
352
353 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
354 return NULL;
355 return __nf_ct_expect_find(net, zone, &tuple);
356}
357
358/* Determine whether skb->nfct is equal to the result of conntrack lookup. */
359static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
360 const struct ovs_conntrack_info *info)
361{
362 enum ip_conntrack_info ctinfo;
363 struct nf_conn *ct;
364
365 ct = nf_ct_get(skb, &ctinfo);
366 if (!ct)
367 return false;
368 if (!net_eq(net, read_pnet(&ct->ct_net)))
369 return false;
370 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
371 return false;
Joe Stringercae3a262015-08-26 11:31:53 -0700372 if (info->helper) {
373 struct nf_conn_help *help;
374
375 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
376 if (help && rcu_access_pointer(help->helper) != info->helper)
377 return false;
378 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700379
380 return true;
381}
382
Joe Stringer4f0909e2015-10-19 19:18:59 -0700383static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
Joe Stringer7f8a4362015-08-26 11:31:48 -0700384 const struct ovs_conntrack_info *info,
385 struct sk_buff *skb)
386{
387 /* If we are recirculating packets to match on conntrack fields and
388 * committing with a separate conntrack action, then we don't need to
389 * actually run the packet through conntrack twice unless it's for a
390 * different zone.
391 */
392 if (!skb_nfct_cached(net, skb, info)) {
393 struct nf_conn *tmpl = info->ct;
394
395 /* Associate skb with specified zone. */
396 if (tmpl) {
397 if (skb->nfct)
398 nf_conntrack_put(skb->nfct);
399 nf_conntrack_get(&tmpl->ct_general);
400 skb->nfct = &tmpl->ct_general;
401 skb->nfctinfo = IP_CT_NEW;
402 }
403
404 if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
405 skb) != NF_ACCEPT)
406 return -ENOENT;
Joe Stringercae3a262015-08-26 11:31:53 -0700407
408 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
409 WARN_ONCE(1, "helper rejected packet");
410 return -EINVAL;
411 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700412 }
413
Joe Stringer4f0909e2015-10-19 19:18:59 -0700414 ovs_ct_update_key(skb, key, true);
415
Joe Stringer7f8a4362015-08-26 11:31:48 -0700416 return 0;
417}
418
419/* Lookup connection and read fields into key. */
420static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
421 const struct ovs_conntrack_info *info,
422 struct sk_buff *skb)
423{
424 struct nf_conntrack_expect *exp;
425
426 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
427 if (exp) {
428 u8 state;
429
430 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
Joe Stringer182e3042015-08-26 11:31:49 -0700431 __ovs_ct_update_key(key, state, &info->zone, exp->master);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700432 } else {
433 int err;
434
435 err = __ovs_ct_lookup(net, key, info, skb);
436 if (err)
437 return err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700438 }
439
440 return 0;
441}
442
443/* Lookup connection and confirm if unconfirmed. */
444static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
445 const struct ovs_conntrack_info *info,
446 struct sk_buff *skb)
447{
448 u8 state;
449 int err;
450
451 state = key->ct.state;
452 if (key->ct.zone == info->zone.id &&
453 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
454 /* Previous lookup has shown that this connection is already
455 * tracked and committed. Skip committing.
456 */
457 return 0;
458 }
459
460 err = __ovs_ct_lookup(net, key, info, skb);
461 if (err)
462 return err;
463 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
464 return -EINVAL;
465
Joe Stringer7f8a4362015-08-26 11:31:48 -0700466 return 0;
467}
468
Joe Stringer33db4122015-10-01 15:00:37 -0700469static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
Joe Stringerc2ac6672015-08-26 11:31:52 -0700470{
471 size_t i;
472
Joe Stringer33db4122015-10-01 15:00:37 -0700473 for (i = 0; i < sizeof(*labels); i++)
474 if (labels->ct_labels[i])
Joe Stringerc2ac6672015-08-26 11:31:52 -0700475 return true;
476
477 return false;
478}
479
Joe Stringer74c16612015-10-25 20:21:48 -0700480/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
481 * value if 'skb' is freed.
482 */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700483int ovs_ct_execute(struct net *net, struct sk_buff *skb,
484 struct sw_flow_key *key,
485 const struct ovs_conntrack_info *info)
486{
487 int nh_ofs;
488 int err;
489
490 /* The conntrack module expects to be working at L3. */
491 nh_ofs = skb_network_offset(skb);
492 skb_pull(skb, nh_ofs);
493
494 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
495 err = handle_fragments(net, key, info->zone.id, skb);
496 if (err)
497 return err;
498 }
499
Joe Stringerab38a7b2015-10-06 11:00:01 -0700500 if (info->commit)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700501 err = ovs_ct_commit(net, key, info, skb);
502 else
503 err = ovs_ct_lookup(net, key, info, skb);
Joe Stringer182e3042015-08-26 11:31:49 -0700504 if (err)
505 goto err;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700506
Joe Stringerc2ac6672015-08-26 11:31:52 -0700507 if (info->mark.mask) {
Joe Stringer182e3042015-08-26 11:31:49 -0700508 err = ovs_ct_set_mark(skb, key, info->mark.value,
509 info->mark.mask);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700510 if (err)
511 goto err;
512 }
Joe Stringer33db4122015-10-01 15:00:37 -0700513 if (labels_nonzero(&info->labels.mask))
514 err = ovs_ct_set_labels(skb, key, &info->labels.value,
515 &info->labels.mask);
Joe Stringer182e3042015-08-26 11:31:49 -0700516err:
Joe Stringer7f8a4362015-08-26 11:31:48 -0700517 skb_push(skb, nh_ofs);
Joe Stringer74c16612015-10-25 20:21:48 -0700518 if (err)
519 kfree_skb(skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700520 return err;
521}
522
Joe Stringercae3a262015-08-26 11:31:53 -0700523static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
524 const struct sw_flow_key *key, bool log)
525{
526 struct nf_conntrack_helper *helper;
527 struct nf_conn_help *help;
528
529 helper = nf_conntrack_helper_try_module_get(name, info->family,
530 key->ip.proto);
531 if (!helper) {
532 OVS_NLERR(log, "Unknown helper \"%s\"", name);
533 return -EINVAL;
534 }
535
536 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
537 if (!help) {
538 module_put(helper->me);
539 return -ENOMEM;
540 }
541
542 rcu_assign_pointer(help->helper, helper);
543 info->helper = helper;
544 return 0;
545}
546
Joe Stringer7f8a4362015-08-26 11:31:48 -0700547static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
Joe Stringerab38a7b2015-10-06 11:00:01 -0700548 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700549 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
550 .maxlen = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700551 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
552 .maxlen = sizeof(struct md_mark) },
Joe Stringer33db4122015-10-01 15:00:37 -0700553 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
554 .maxlen = sizeof(struct md_labels) },
Joe Stringercae3a262015-08-26 11:31:53 -0700555 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
556 .maxlen = NF_CT_HELPER_NAME_LEN }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700557};
558
559static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
Joe Stringercae3a262015-08-26 11:31:53 -0700560 const char **helper, bool log)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700561{
562 struct nlattr *a;
563 int rem;
564
565 nla_for_each_nested(a, attr, rem) {
566 int type = nla_type(a);
567 int maxlen = ovs_ct_attr_lens[type].maxlen;
568 int minlen = ovs_ct_attr_lens[type].minlen;
569
570 if (type > OVS_CT_ATTR_MAX) {
571 OVS_NLERR(log,
572 "Unknown conntrack attr (type=%d, max=%d)",
573 type, OVS_CT_ATTR_MAX);
574 return -EINVAL;
575 }
576 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
577 OVS_NLERR(log,
578 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
579 type, nla_len(a), maxlen);
580 return -EINVAL;
581 }
582
583 switch (type) {
Joe Stringerab38a7b2015-10-06 11:00:01 -0700584 case OVS_CT_ATTR_COMMIT:
585 info->commit = true;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700586 break;
587#ifdef CONFIG_NF_CONNTRACK_ZONES
588 case OVS_CT_ATTR_ZONE:
589 info->zone.id = nla_get_u16(a);
590 break;
591#endif
Joe Stringer182e3042015-08-26 11:31:49 -0700592#ifdef CONFIG_NF_CONNTRACK_MARK
593 case OVS_CT_ATTR_MARK: {
594 struct md_mark *mark = nla_data(a);
595
Joe Stringere754ec62015-10-19 19:19:00 -0700596 if (!mark->mask) {
597 OVS_NLERR(log, "ct_mark mask cannot be 0");
598 return -EINVAL;
599 }
Joe Stringer182e3042015-08-26 11:31:49 -0700600 info->mark = *mark;
601 break;
602 }
603#endif
Joe Stringerc2ac6672015-08-26 11:31:52 -0700604#ifdef CONFIG_NF_CONNTRACK_LABELS
Joe Stringer33db4122015-10-01 15:00:37 -0700605 case OVS_CT_ATTR_LABELS: {
606 struct md_labels *labels = nla_data(a);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700607
Joe Stringere754ec62015-10-19 19:19:00 -0700608 if (!labels_nonzero(&labels->mask)) {
609 OVS_NLERR(log, "ct_labels mask cannot be 0");
610 return -EINVAL;
611 }
Joe Stringer33db4122015-10-01 15:00:37 -0700612 info->labels = *labels;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700613 break;
614 }
615#endif
Joe Stringercae3a262015-08-26 11:31:53 -0700616 case OVS_CT_ATTR_HELPER:
617 *helper = nla_data(a);
618 if (!memchr(*helper, '\0', nla_len(a))) {
619 OVS_NLERR(log, "Invalid conntrack helper");
620 return -EINVAL;
621 }
622 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700623 default:
624 OVS_NLERR(log, "Unknown conntrack attr (%d)",
625 type);
626 return -EINVAL;
627 }
628 }
629
630 if (rem > 0) {
631 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
632 return -EINVAL;
633 }
634
635 return 0;
636}
637
Joe Stringerc2ac6672015-08-26 11:31:52 -0700638bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
Joe Stringer7f8a4362015-08-26 11:31:48 -0700639{
640 if (attr == OVS_KEY_ATTR_CT_STATE)
641 return true;
642 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
643 attr == OVS_KEY_ATTR_CT_ZONE)
644 return true;
Joe Stringer182e3042015-08-26 11:31:49 -0700645 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
646 attr == OVS_KEY_ATTR_CT_MARK)
647 return true;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700648 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700649 attr == OVS_KEY_ATTR_CT_LABELS) {
Joe Stringerc2ac6672015-08-26 11:31:52 -0700650 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
651
652 return ovs_net->xt_label;
653 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700654
655 return false;
656}
657
658int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
659 const struct sw_flow_key *key,
660 struct sw_flow_actions **sfa, bool log)
661{
662 struct ovs_conntrack_info ct_info;
Joe Stringercae3a262015-08-26 11:31:53 -0700663 const char *helper = NULL;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700664 u16 family;
665 int err;
666
667 family = key_to_nfproto(key);
668 if (family == NFPROTO_UNSPEC) {
669 OVS_NLERR(log, "ct family unspecified");
670 return -EINVAL;
671 }
672
673 memset(&ct_info, 0, sizeof(ct_info));
674 ct_info.family = family;
675
676 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
677 NF_CT_DEFAULT_ZONE_DIR, 0);
678
Joe Stringercae3a262015-08-26 11:31:53 -0700679 err = parse_ct(attr, &ct_info, &helper, log);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700680 if (err)
681 return err;
682
683 /* Set up template for tracking connections in specific zones. */
684 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
685 if (!ct_info.ct) {
686 OVS_NLERR(log, "Failed to allocate conntrack template");
687 return -ENOMEM;
688 }
Joe Stringercae3a262015-08-26 11:31:53 -0700689 if (helper) {
690 err = ovs_ct_add_helper(&ct_info, helper, key, log);
691 if (err)
692 goto err_free_ct;
693 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700694
695 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
696 sizeof(ct_info), log);
697 if (err)
698 goto err_free_ct;
699
700 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
701 nf_conntrack_get(&ct_info.ct->ct_general);
702 return 0;
703err_free_ct:
704 nf_conntrack_free(ct_info.ct);
705 return err;
706}
707
708int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
709 struct sk_buff *skb)
710{
711 struct nlattr *start;
712
713 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
714 if (!start)
715 return -EMSGSIZE;
716
Joe Stringerab38a7b2015-10-06 11:00:01 -0700717 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
Joe Stringer7f8a4362015-08-26 11:31:48 -0700718 return -EMSGSIZE;
719 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
720 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
721 return -EMSGSIZE;
Joe Stringere754ec62015-10-19 19:19:00 -0700722 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
Joe Stringer182e3042015-08-26 11:31:49 -0700723 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
724 &ct_info->mark))
725 return -EMSGSIZE;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700726 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
Joe Stringere754ec62015-10-19 19:19:00 -0700727 labels_nonzero(&ct_info->labels.mask) &&
Joe Stringer33db4122015-10-01 15:00:37 -0700728 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
729 &ct_info->labels))
Joe Stringerc2ac6672015-08-26 11:31:52 -0700730 return -EMSGSIZE;
Joe Stringercae3a262015-08-26 11:31:53 -0700731 if (ct_info->helper) {
732 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
733 ct_info->helper->name))
734 return -EMSGSIZE;
735 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700736
737 nla_nest_end(skb, start);
738
739 return 0;
740}
741
742void ovs_ct_free_action(const struct nlattr *a)
743{
744 struct ovs_conntrack_info *ct_info = nla_data(a);
745
Joe Stringercae3a262015-08-26 11:31:53 -0700746 if (ct_info->helper)
747 module_put(ct_info->helper->me);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700748 if (ct_info->ct)
749 nf_ct_put(ct_info->ct);
750}
Joe Stringerc2ac6672015-08-26 11:31:52 -0700751
752void ovs_ct_init(struct net *net)
753{
Joe Stringer33db4122015-10-01 15:00:37 -0700754 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700755 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
756
757 if (nf_connlabels_get(net, n_bits)) {
758 ovs_net->xt_label = false;
759 OVS_NLERR(true, "Failed to set connlabel length");
760 } else {
761 ovs_net->xt_label = true;
762 }
763}
764
765void ovs_ct_exit(struct net *net)
766{
767 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
768
769 if (ovs_net->xt_label)
770 nf_connlabels_put(net);
771}