Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[deliverable/linux.git] / net / openvswitch / conntrack.c
CommitLineData
7f8a436e
JS
1/*
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>
cae3a262 18#include <net/netfilter/nf_conntrack_helper.h>
c2ac6673 19#include <net/netfilter/nf_conntrack_labels.h>
7f8a436e
JS
20#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
182e3042
JS
33/* Metadata mark for masked write to conntrack mark */
34struct md_mark {
35 u32 value;
36 u32 mask;
37};
38
c2ac6673
JS
39/* Metadata label for masked write to conntrack label. */
40struct md_label {
41 struct ovs_key_ct_label value;
42 struct ovs_key_ct_label mask;
43};
44
7f8a436e
JS
45/* Conntrack action context for execution. */
46struct ovs_conntrack_info {
cae3a262 47 struct nf_conntrack_helper *helper;
7f8a436e
JS
48 struct nf_conntrack_zone zone;
49 struct nf_conn *ct;
50 u32 flags;
51 u16 family;
182e3042 52 struct md_mark mark;
c2ac6673 53 struct md_label label;
7f8a436e
JS
54};
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
c2ac6673
JS
103static void ovs_ct_get_label(const struct nf_conn *ct,
104 struct ovs_key_ct_label *label)
105{
106 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
107
108 if (cl) {
109 size_t len = cl->words * sizeof(long);
110
111 if (len > OVS_CT_LABEL_LEN)
112 len = OVS_CT_LABEL_LEN;
113 else if (len < OVS_CT_LABEL_LEN)
114 memset(label, 0, OVS_CT_LABEL_LEN);
115 memcpy(label, cl->bits, len);
116 } else {
117 memset(label, 0, OVS_CT_LABEL_LEN);
118 }
119}
120
7f8a436e 121static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
182e3042
JS
122 const struct nf_conntrack_zone *zone,
123 const struct nf_conn *ct)
7f8a436e
JS
124{
125 key->ct.state = state;
126 key->ct.zone = zone->id;
182e3042 127 key->ct.mark = ct ? ct->mark : 0;
c2ac6673 128 ovs_ct_get_label(ct, &key->ct.label);
7f8a436e
JS
129}
130
131/* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
132 * previously sent the packet to conntrack via the ct action.
133 */
134static void ovs_ct_update_key(const struct sk_buff *skb,
135 struct sw_flow_key *key, bool post_ct)
136{
137 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
138 enum ip_conntrack_info ctinfo;
139 struct nf_conn *ct;
140 u8 state = 0;
141
142 ct = nf_ct_get(skb, &ctinfo);
143 if (ct) {
144 state = ovs_ct_get_state(ctinfo);
145 if (ct->master)
146 state |= OVS_CS_F_RELATED;
147 zone = nf_ct_zone(ct);
148 } else if (post_ct) {
149 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
150 }
182e3042 151 __ovs_ct_update_key(key, state, zone, ct);
7f8a436e
JS
152}
153
154void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
155{
156 ovs_ct_update_key(skb, key, false);
157}
158
159int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
160{
161 if (nla_put_u8(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
162 return -EMSGSIZE;
163
164 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
165 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
166 return -EMSGSIZE;
167
182e3042
JS
168 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
169 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
170 return -EMSGSIZE;
171
9723e6ab 172 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
c2ac6673
JS
173 nla_put(skb, OVS_KEY_ATTR_CT_LABEL, sizeof(key->ct.label),
174 &key->ct.label))
175 return -EMSGSIZE;
176
182e3042
JS
177 return 0;
178}
179
180static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
181 u32 ct_mark, u32 mask)
182{
183 enum ip_conntrack_info ctinfo;
184 struct nf_conn *ct;
185 u32 new_mark;
186
187 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK))
188 return -ENOTSUPP;
189
190 /* The connection could be invalid, in which case set_mark is no-op. */
191 ct = nf_ct_get(skb, &ctinfo);
192 if (!ct)
193 return 0;
194
195 new_mark = ct_mark | (ct->mark & ~(mask));
196 if (ct->mark != new_mark) {
197 ct->mark = new_mark;
198 nf_conntrack_event_cache(IPCT_MARK, ct);
199 key->ct.mark = new_mark;
200 }
201
7f8a436e
JS
202 return 0;
203}
204
c2ac6673
JS
205static int ovs_ct_set_label(struct sk_buff *skb, struct sw_flow_key *key,
206 const struct ovs_key_ct_label *label,
207 const struct ovs_key_ct_label *mask)
208{
209 enum ip_conntrack_info ctinfo;
210 struct nf_conn_labels *cl;
211 struct nf_conn *ct;
212 int err;
213
214 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS))
215 return -ENOTSUPP;
216
217 /* The connection could be invalid, in which case set_label is no-op.*/
218 ct = nf_ct_get(skb, &ctinfo);
219 if (!ct)
220 return 0;
221
222 cl = nf_ct_labels_find(ct);
223 if (!cl) {
224 nf_ct_labels_ext_add(ct);
225 cl = nf_ct_labels_find(ct);
226 }
227 if (!cl || cl->words * sizeof(long) < OVS_CT_LABEL_LEN)
228 return -ENOSPC;
229
230 err = nf_connlabels_replace(ct, (u32 *)label, (u32 *)mask,
231 OVS_CT_LABEL_LEN / sizeof(u32));
232 if (err)
233 return err;
234
235 ovs_ct_get_label(ct, &key->ct.label);
236 return 0;
237}
238
cae3a262
JS
239/* 'skb' should already be pulled to nh_ofs. */
240static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
241{
242 const struct nf_conntrack_helper *helper;
243 const struct nf_conn_help *help;
244 enum ip_conntrack_info ctinfo;
245 unsigned int protoff;
246 struct nf_conn *ct;
247
248 ct = nf_ct_get(skb, &ctinfo);
249 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
250 return NF_ACCEPT;
251
252 help = nfct_help(ct);
253 if (!help)
254 return NF_ACCEPT;
255
256 helper = rcu_dereference(help->helper);
257 if (!helper)
258 return NF_ACCEPT;
259
260 switch (proto) {
261 case NFPROTO_IPV4:
262 protoff = ip_hdrlen(skb);
263 break;
264 case NFPROTO_IPV6: {
265 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
266 __be16 frag_off;
267
268 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
269 &nexthdr, &frag_off);
270 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
271 pr_debug("proto header not found\n");
272 return NF_ACCEPT;
273 }
274 break;
275 }
276 default:
277 WARN_ONCE(1, "helper invoked on non-IP family!");
278 return NF_DROP;
279 }
280
281 return helper->help(skb, protoff, ct, ctinfo);
282}
283
7f8a436e
JS
284static int handle_fragments(struct net *net, struct sw_flow_key *key,
285 u16 zone, struct sk_buff *skb)
286{
287 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
288
289 if (key->eth.type == htons(ETH_P_IP)) {
290 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
291 int err;
292
293 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
294 err = ip_defrag(skb, user);
295 if (err)
296 return err;
297
298 ovs_cb.mru = IPCB(skb)->frag_max_size;
299 } else if (key->eth.type == htons(ETH_P_IPV6)) {
300#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
301 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
302 struct sk_buff *reasm;
303
304 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
305 reasm = nf_ct_frag6_gather(skb, user);
306 if (!reasm)
307 return -EINPROGRESS;
308
309 if (skb == reasm)
310 return -EINVAL;
311
312 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
313 skb_morph(skb, reasm);
314 consume_skb(reasm);
315 ovs_cb.mru = IP6CB(skb)->frag_max_size;
316#else
317 return -EPFNOSUPPORT;
318#endif
319 } else {
320 return -EPFNOSUPPORT;
321 }
322
323 key->ip.frag = OVS_FRAG_TYPE_NONE;
324 skb_clear_hash(skb);
325 skb->ignore_df = 1;
326 *OVS_CB(skb) = ovs_cb;
327
328 return 0;
329}
330
331static struct nf_conntrack_expect *
332ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
333 u16 proto, const struct sk_buff *skb)
334{
335 struct nf_conntrack_tuple tuple;
336
337 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
338 return NULL;
339 return __nf_ct_expect_find(net, zone, &tuple);
340}
341
342/* Determine whether skb->nfct is equal to the result of conntrack lookup. */
343static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
344 const struct ovs_conntrack_info *info)
345{
346 enum ip_conntrack_info ctinfo;
347 struct nf_conn *ct;
348
349 ct = nf_ct_get(skb, &ctinfo);
350 if (!ct)
351 return false;
352 if (!net_eq(net, read_pnet(&ct->ct_net)))
353 return false;
354 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
355 return false;
cae3a262
JS
356 if (info->helper) {
357 struct nf_conn_help *help;
358
359 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
360 if (help && rcu_access_pointer(help->helper) != info->helper)
361 return false;
362 }
7f8a436e
JS
363
364 return true;
365}
366
367static int __ovs_ct_lookup(struct net *net, const struct sw_flow_key *key,
368 const struct ovs_conntrack_info *info,
369 struct sk_buff *skb)
370{
371 /* If we are recirculating packets to match on conntrack fields and
372 * committing with a separate conntrack action, then we don't need to
373 * actually run the packet through conntrack twice unless it's for a
374 * different zone.
375 */
376 if (!skb_nfct_cached(net, skb, info)) {
377 struct nf_conn *tmpl = info->ct;
378
379 /* Associate skb with specified zone. */
380 if (tmpl) {
381 if (skb->nfct)
382 nf_conntrack_put(skb->nfct);
383 nf_conntrack_get(&tmpl->ct_general);
384 skb->nfct = &tmpl->ct_general;
385 skb->nfctinfo = IP_CT_NEW;
386 }
387
388 if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
389 skb) != NF_ACCEPT)
390 return -ENOENT;
cae3a262
JS
391
392 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
393 WARN_ONCE(1, "helper rejected packet");
394 return -EINVAL;
395 }
7f8a436e
JS
396 }
397
398 return 0;
399}
400
401/* Lookup connection and read fields into key. */
402static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
403 const struct ovs_conntrack_info *info,
404 struct sk_buff *skb)
405{
406 struct nf_conntrack_expect *exp;
407
408 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
409 if (exp) {
410 u8 state;
411
412 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
182e3042 413 __ovs_ct_update_key(key, state, &info->zone, exp->master);
7f8a436e
JS
414 } else {
415 int err;
416
417 err = __ovs_ct_lookup(net, key, info, skb);
418 if (err)
419 return err;
420
421 ovs_ct_update_key(skb, key, true);
422 }
423
424 return 0;
425}
426
427/* Lookup connection and confirm if unconfirmed. */
428static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
429 const struct ovs_conntrack_info *info,
430 struct sk_buff *skb)
431{
432 u8 state;
433 int err;
434
435 state = key->ct.state;
436 if (key->ct.zone == info->zone.id &&
437 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
438 /* Previous lookup has shown that this connection is already
439 * tracked and committed. Skip committing.
440 */
441 return 0;
442 }
443
444 err = __ovs_ct_lookup(net, key, info, skb);
445 if (err)
446 return err;
447 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
448 return -EINVAL;
449
450 ovs_ct_update_key(skb, key, true);
451
452 return 0;
453}
454
c2ac6673
JS
455static bool label_nonzero(const struct ovs_key_ct_label *label)
456{
457 size_t i;
458
459 for (i = 0; i < sizeof(*label); i++)
460 if (label->ct_label[i])
461 return true;
462
463 return false;
464}
465
7f8a436e
JS
466int ovs_ct_execute(struct net *net, struct sk_buff *skb,
467 struct sw_flow_key *key,
468 const struct ovs_conntrack_info *info)
469{
470 int nh_ofs;
471 int err;
472
473 /* The conntrack module expects to be working at L3. */
474 nh_ofs = skb_network_offset(skb);
475 skb_pull(skb, nh_ofs);
476
477 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
478 err = handle_fragments(net, key, info->zone.id, skb);
479 if (err)
480 return err;
481 }
482
483 if (info->flags & OVS_CT_F_COMMIT)
484 err = ovs_ct_commit(net, key, info, skb);
485 else
486 err = ovs_ct_lookup(net, key, info, skb);
182e3042
JS
487 if (err)
488 goto err;
7f8a436e 489
c2ac6673 490 if (info->mark.mask) {
182e3042
JS
491 err = ovs_ct_set_mark(skb, key, info->mark.value,
492 info->mark.mask);
c2ac6673
JS
493 if (err)
494 goto err;
495 }
496 if (label_nonzero(&info->label.mask))
497 err = ovs_ct_set_label(skb, key, &info->label.value,
498 &info->label.mask);
182e3042 499err:
7f8a436e
JS
500 skb_push(skb, nh_ofs);
501 return err;
502}
503
cae3a262
JS
504static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
505 const struct sw_flow_key *key, bool log)
506{
507 struct nf_conntrack_helper *helper;
508 struct nf_conn_help *help;
509
510 helper = nf_conntrack_helper_try_module_get(name, info->family,
511 key->ip.proto);
512 if (!helper) {
513 OVS_NLERR(log, "Unknown helper \"%s\"", name);
514 return -EINVAL;
515 }
516
517 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
518 if (!help) {
519 module_put(helper->me);
520 return -ENOMEM;
521 }
522
523 rcu_assign_pointer(help->helper, helper);
524 info->helper = helper;
525 return 0;
526}
527
7f8a436e
JS
528static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
529 [OVS_CT_ATTR_FLAGS] = { .minlen = sizeof(u32),
530 .maxlen = sizeof(u32) },
531 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
532 .maxlen = sizeof(u16) },
182e3042
JS
533 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
534 .maxlen = sizeof(struct md_mark) },
c2ac6673
JS
535 [OVS_CT_ATTR_LABEL] = { .minlen = sizeof(struct md_label),
536 .maxlen = sizeof(struct md_label) },
cae3a262
JS
537 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
538 .maxlen = NF_CT_HELPER_NAME_LEN }
7f8a436e
JS
539};
540
541static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
cae3a262 542 const char **helper, bool log)
7f8a436e
JS
543{
544 struct nlattr *a;
545 int rem;
546
547 nla_for_each_nested(a, attr, rem) {
548 int type = nla_type(a);
549 int maxlen = ovs_ct_attr_lens[type].maxlen;
550 int minlen = ovs_ct_attr_lens[type].minlen;
551
552 if (type > OVS_CT_ATTR_MAX) {
553 OVS_NLERR(log,
554 "Unknown conntrack attr (type=%d, max=%d)",
555 type, OVS_CT_ATTR_MAX);
556 return -EINVAL;
557 }
558 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
559 OVS_NLERR(log,
560 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
561 type, nla_len(a), maxlen);
562 return -EINVAL;
563 }
564
565 switch (type) {
566 case OVS_CT_ATTR_FLAGS:
567 info->flags = nla_get_u32(a);
568 break;
569#ifdef CONFIG_NF_CONNTRACK_ZONES
570 case OVS_CT_ATTR_ZONE:
571 info->zone.id = nla_get_u16(a);
572 break;
182e3042
JS
573#endif
574#ifdef CONFIG_NF_CONNTRACK_MARK
575 case OVS_CT_ATTR_MARK: {
576 struct md_mark *mark = nla_data(a);
577
578 info->mark = *mark;
579 break;
580 }
c2ac6673
JS
581#endif
582#ifdef CONFIG_NF_CONNTRACK_LABELS
583 case OVS_CT_ATTR_LABEL: {
584 struct md_label *label = nla_data(a);
585
586 info->label = *label;
587 break;
588 }
7f8a436e 589#endif
cae3a262
JS
590 case OVS_CT_ATTR_HELPER:
591 *helper = nla_data(a);
592 if (!memchr(*helper, '\0', nla_len(a))) {
593 OVS_NLERR(log, "Invalid conntrack helper");
594 return -EINVAL;
595 }
596 break;
7f8a436e
JS
597 default:
598 OVS_NLERR(log, "Unknown conntrack attr (%d)",
599 type);
600 return -EINVAL;
601 }
602 }
603
604 if (rem > 0) {
605 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
606 return -EINVAL;
607 }
608
609 return 0;
610}
611
c2ac6673 612bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
7f8a436e
JS
613{
614 if (attr == OVS_KEY_ATTR_CT_STATE)
615 return true;
616 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
617 attr == OVS_KEY_ATTR_CT_ZONE)
618 return true;
182e3042
JS
619 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
620 attr == OVS_KEY_ATTR_CT_MARK)
621 return true;
c2ac6673
JS
622 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
623 attr == OVS_KEY_ATTR_CT_LABEL) {
624 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
625
626 return ovs_net->xt_label;
627 }
7f8a436e
JS
628
629 return false;
630}
631
632int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
633 const struct sw_flow_key *key,
634 struct sw_flow_actions **sfa, bool log)
635{
636 struct ovs_conntrack_info ct_info;
cae3a262 637 const char *helper = NULL;
7f8a436e
JS
638 u16 family;
639 int err;
640
641 family = key_to_nfproto(key);
642 if (family == NFPROTO_UNSPEC) {
643 OVS_NLERR(log, "ct family unspecified");
644 return -EINVAL;
645 }
646
647 memset(&ct_info, 0, sizeof(ct_info));
648 ct_info.family = family;
649
650 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
651 NF_CT_DEFAULT_ZONE_DIR, 0);
652
cae3a262 653 err = parse_ct(attr, &ct_info, &helper, log);
7f8a436e
JS
654 if (err)
655 return err;
656
657 /* Set up template for tracking connections in specific zones. */
658 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
659 if (!ct_info.ct) {
660 OVS_NLERR(log, "Failed to allocate conntrack template");
661 return -ENOMEM;
662 }
cae3a262
JS
663 if (helper) {
664 err = ovs_ct_add_helper(&ct_info, helper, key, log);
665 if (err)
666 goto err_free_ct;
667 }
7f8a436e
JS
668
669 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
670 sizeof(ct_info), log);
671 if (err)
672 goto err_free_ct;
673
674 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
675 nf_conntrack_get(&ct_info.ct->ct_general);
676 return 0;
677err_free_ct:
678 nf_conntrack_free(ct_info.ct);
679 return err;
680}
681
682int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
683 struct sk_buff *skb)
684{
685 struct nlattr *start;
686
687 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
688 if (!start)
689 return -EMSGSIZE;
690
691 if (nla_put_u32(skb, OVS_CT_ATTR_FLAGS, ct_info->flags))
692 return -EMSGSIZE;
693 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
694 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
695 return -EMSGSIZE;
182e3042
JS
696 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
697 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
698 &ct_info->mark))
699 return -EMSGSIZE;
c2ac6673
JS
700 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
701 nla_put(skb, OVS_CT_ATTR_LABEL, sizeof(ct_info->label),
702 &ct_info->label))
703 return -EMSGSIZE;
cae3a262
JS
704 if (ct_info->helper) {
705 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
706 ct_info->helper->name))
707 return -EMSGSIZE;
708 }
7f8a436e
JS
709
710 nla_nest_end(skb, start);
711
712 return 0;
713}
714
715void ovs_ct_free_action(const struct nlattr *a)
716{
717 struct ovs_conntrack_info *ct_info = nla_data(a);
718
cae3a262
JS
719 if (ct_info->helper)
720 module_put(ct_info->helper->me);
7f8a436e
JS
721 if (ct_info->ct)
722 nf_ct_put(ct_info->ct);
723}
c2ac6673
JS
724
725void ovs_ct_init(struct net *net)
726{
727 unsigned int n_bits = sizeof(struct ovs_key_ct_label) * BITS_PER_BYTE;
728 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
729
730 if (nf_connlabels_get(net, n_bits)) {
731 ovs_net->xt_label = false;
732 OVS_NLERR(true, "Failed to set connlabel length");
733 } else {
734 ovs_net->xt_label = true;
735 }
736}
737
738void ovs_ct_exit(struct net *net)
739{
740 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
741
742 if (ovs_net->xt_label)
743 nf_connlabels_put(net);
744}
This page took 0.053034 seconds and 5 git commands to generate.