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