]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/openvswitch/conntrack.c
a5ec34f8502f013161bb08e9cb9d9960559a9e67
[mirror_ubuntu-bionic-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 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. */
69 static 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
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 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);
154 if (!nf_ct_is_confirmed(ct))
155 state |= OVS_CS_F_NEW;
156 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 }
162 __ovs_ct_update_key(key, state, zone, ct);
163 }
164
165 void 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
170 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
171 {
172 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
173 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
179 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
180 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
181 return -EMSGSIZE;
182
183 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
184 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
185 &key->ct.labels))
186 return -EMSGSIZE;
187
188 return 0;
189 }
190
191 static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
192 u32 ct_mark, u32 mask)
193 {
194 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
195 enum ip_conntrack_info ctinfo;
196 struct nf_conn *ct;
197 u32 new_mark;
198
199
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
212 return 0;
213 #else
214 return -ENOTSUPP;
215 #endif
216 }
217
218 static 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)
221 {
222 enum ip_conntrack_info ctinfo;
223 struct nf_conn_labels *cl;
224 struct nf_conn *ct;
225 int err;
226
227 /* 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 }
237 if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
238 return -ENOSPC;
239
240 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
241 OVS_CT_LABELS_LEN / sizeof(u32));
242 if (err)
243 return err;
244
245 ovs_ct_get_labels(ct, &key->ct.labels);
246 return 0;
247 }
248
249 /* 'skb' should already be pulled to nh_ofs. */
250 static 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;
277 int ofs;
278
279 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
280 &frag_off);
281 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
282 pr_debug("proto header not found\n");
283 return NF_ACCEPT;
284 }
285 protoff = ofs;
286 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
296 static int handle_fragments(struct net *net, struct sw_flow_key *key,
297 u16 zone, struct sk_buff *skb)
298 {
299 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
300
301 if (key->eth.type == htons(ETH_P_IP)) {
302 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
303 int err;
304
305 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
306 err = ip_defrag(skb, user);
307 if (err)
308 return err;
309
310 ovs_cb.mru = IPCB(skb)->frag_max_size;
311 } else if (key->eth.type == htons(ETH_P_IPV6)) {
312 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
313 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
314 struct sk_buff *reasm;
315
316 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
317 reasm = nf_ct_frag6_gather(skb, user);
318 if (!reasm)
319 return -EINPROGRESS;
320
321 if (skb == reasm)
322 return -EINVAL;
323
324 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
325 skb_morph(skb, reasm);
326 consume_skb(reasm);
327 ovs_cb.mru = IP6CB(skb)->frag_max_size;
328 #else
329 return -EPFNOSUPPORT;
330 #endif
331 } else {
332 return -EPFNOSUPPORT;
333 }
334
335 key->ip.frag = OVS_FRAG_TYPE_NONE;
336 skb_clear_hash(skb);
337 skb->ignore_df = 1;
338 *OVS_CB(skb) = ovs_cb;
339
340 return 0;
341 }
342
343 static struct nf_conntrack_expect *
344 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
345 u16 proto, const struct sk_buff *skb)
346 {
347 struct nf_conntrack_tuple tuple;
348
349 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
350 return NULL;
351 return __nf_ct_expect_find(net, zone, &tuple);
352 }
353
354 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
355 static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
356 const struct ovs_conntrack_info *info)
357 {
358 enum ip_conntrack_info ctinfo;
359 struct nf_conn *ct;
360
361 ct = nf_ct_get(skb, &ctinfo);
362 if (!ct)
363 return false;
364 if (!net_eq(net, read_pnet(&ct->ct_net)))
365 return false;
366 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
367 return false;
368 if (info->helper) {
369 struct nf_conn_help *help;
370
371 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
372 if (help && rcu_access_pointer(help->helper) != info->helper)
373 return false;
374 }
375
376 return true;
377 }
378
379 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
380 const struct ovs_conntrack_info *info,
381 struct sk_buff *skb)
382 {
383 /* If we are recirculating packets to match on conntrack fields and
384 * committing with a separate conntrack action, then we don't need to
385 * actually run the packet through conntrack twice unless it's for a
386 * different zone.
387 */
388 if (!skb_nfct_cached(net, skb, info)) {
389 struct nf_conn *tmpl = info->ct;
390
391 /* Associate skb with specified zone. */
392 if (tmpl) {
393 if (skb->nfct)
394 nf_conntrack_put(skb->nfct);
395 nf_conntrack_get(&tmpl->ct_general);
396 skb->nfct = &tmpl->ct_general;
397 skb->nfctinfo = IP_CT_NEW;
398 }
399
400 if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
401 skb) != NF_ACCEPT)
402 return -ENOENT;
403
404 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
405 WARN_ONCE(1, "helper rejected packet");
406 return -EINVAL;
407 }
408 }
409
410 ovs_ct_update_key(skb, key, true);
411
412 return 0;
413 }
414
415 /* Lookup connection and read fields into key. */
416 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
417 const struct ovs_conntrack_info *info,
418 struct sk_buff *skb)
419 {
420 struct nf_conntrack_expect *exp;
421
422 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
423 if (exp) {
424 u8 state;
425
426 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
427 __ovs_ct_update_key(key, state, &info->zone, exp->master);
428 } else {
429 int err;
430
431 err = __ovs_ct_lookup(net, key, info, skb);
432 if (err)
433 return err;
434 }
435
436 return 0;
437 }
438
439 /* Lookup connection and confirm if unconfirmed. */
440 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
441 const struct ovs_conntrack_info *info,
442 struct sk_buff *skb)
443 {
444 u8 state;
445 int err;
446
447 state = key->ct.state;
448 if (key->ct.zone == info->zone.id &&
449 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
450 /* Previous lookup has shown that this connection is already
451 * tracked and committed. Skip committing.
452 */
453 return 0;
454 }
455
456 err = __ovs_ct_lookup(net, key, info, skb);
457 if (err)
458 return err;
459 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
460 return -EINVAL;
461
462 return 0;
463 }
464
465 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
466 {
467 size_t i;
468
469 for (i = 0; i < sizeof(*labels); i++)
470 if (labels->ct_labels[i])
471 return true;
472
473 return false;
474 }
475
476 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
477 struct sw_flow_key *key,
478 const struct ovs_conntrack_info *info)
479 {
480 int nh_ofs;
481 int err;
482
483 /* The conntrack module expects to be working at L3. */
484 nh_ofs = skb_network_offset(skb);
485 skb_pull(skb, nh_ofs);
486
487 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
488 err = handle_fragments(net, key, info->zone.id, skb);
489 if (err)
490 return err;
491 }
492
493 if (info->commit)
494 err = ovs_ct_commit(net, key, info, skb);
495 else
496 err = ovs_ct_lookup(net, key, info, skb);
497 if (err)
498 goto err;
499
500 if (info->mark.mask) {
501 err = ovs_ct_set_mark(skb, key, info->mark.value,
502 info->mark.mask);
503 if (err)
504 goto err;
505 }
506 if (labels_nonzero(&info->labels.mask))
507 err = ovs_ct_set_labels(skb, key, &info->labels.value,
508 &info->labels.mask);
509 err:
510 skb_push(skb, nh_ofs);
511 return err;
512 }
513
514 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
515 const struct sw_flow_key *key, bool log)
516 {
517 struct nf_conntrack_helper *helper;
518 struct nf_conn_help *help;
519
520 helper = nf_conntrack_helper_try_module_get(name, info->family,
521 key->ip.proto);
522 if (!helper) {
523 OVS_NLERR(log, "Unknown helper \"%s\"", name);
524 return -EINVAL;
525 }
526
527 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
528 if (!help) {
529 module_put(helper->me);
530 return -ENOMEM;
531 }
532
533 rcu_assign_pointer(help->helper, helper);
534 info->helper = helper;
535 return 0;
536 }
537
538 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
539 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
540 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
541 .maxlen = sizeof(u16) },
542 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
543 .maxlen = sizeof(struct md_mark) },
544 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
545 .maxlen = sizeof(struct md_labels) },
546 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
547 .maxlen = NF_CT_HELPER_NAME_LEN }
548 };
549
550 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
551 const char **helper, bool log)
552 {
553 struct nlattr *a;
554 int rem;
555
556 nla_for_each_nested(a, attr, rem) {
557 int type = nla_type(a);
558 int maxlen = ovs_ct_attr_lens[type].maxlen;
559 int minlen = ovs_ct_attr_lens[type].minlen;
560
561 if (type > OVS_CT_ATTR_MAX) {
562 OVS_NLERR(log,
563 "Unknown conntrack attr (type=%d, max=%d)",
564 type, OVS_CT_ATTR_MAX);
565 return -EINVAL;
566 }
567 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
568 OVS_NLERR(log,
569 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
570 type, nla_len(a), maxlen);
571 return -EINVAL;
572 }
573
574 switch (type) {
575 case OVS_CT_ATTR_COMMIT:
576 info->commit = true;
577 break;
578 #ifdef CONFIG_NF_CONNTRACK_ZONES
579 case OVS_CT_ATTR_ZONE:
580 info->zone.id = nla_get_u16(a);
581 break;
582 #endif
583 #ifdef CONFIG_NF_CONNTRACK_MARK
584 case OVS_CT_ATTR_MARK: {
585 struct md_mark *mark = nla_data(a);
586
587 if (!mark->mask) {
588 OVS_NLERR(log, "ct_mark mask cannot be 0");
589 return -EINVAL;
590 }
591 info->mark = *mark;
592 break;
593 }
594 #endif
595 #ifdef CONFIG_NF_CONNTRACK_LABELS
596 case OVS_CT_ATTR_LABELS: {
597 struct md_labels *labels = nla_data(a);
598
599 if (!labels_nonzero(&labels->mask)) {
600 OVS_NLERR(log, "ct_labels mask cannot be 0");
601 return -EINVAL;
602 }
603 info->labels = *labels;
604 break;
605 }
606 #endif
607 case OVS_CT_ATTR_HELPER:
608 *helper = nla_data(a);
609 if (!memchr(*helper, '\0', nla_len(a))) {
610 OVS_NLERR(log, "Invalid conntrack helper");
611 return -EINVAL;
612 }
613 break;
614 default:
615 OVS_NLERR(log, "Unknown conntrack attr (%d)",
616 type);
617 return -EINVAL;
618 }
619 }
620
621 if (rem > 0) {
622 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
623 return -EINVAL;
624 }
625
626 return 0;
627 }
628
629 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
630 {
631 if (attr == OVS_KEY_ATTR_CT_STATE)
632 return true;
633 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
634 attr == OVS_KEY_ATTR_CT_ZONE)
635 return true;
636 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
637 attr == OVS_KEY_ATTR_CT_MARK)
638 return true;
639 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
640 attr == OVS_KEY_ATTR_CT_LABELS) {
641 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
642
643 return ovs_net->xt_label;
644 }
645
646 return false;
647 }
648
649 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
650 const struct sw_flow_key *key,
651 struct sw_flow_actions **sfa, bool log)
652 {
653 struct ovs_conntrack_info ct_info;
654 const char *helper = NULL;
655 u16 family;
656 int err;
657
658 family = key_to_nfproto(key);
659 if (family == NFPROTO_UNSPEC) {
660 OVS_NLERR(log, "ct family unspecified");
661 return -EINVAL;
662 }
663
664 memset(&ct_info, 0, sizeof(ct_info));
665 ct_info.family = family;
666
667 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
668 NF_CT_DEFAULT_ZONE_DIR, 0);
669
670 err = parse_ct(attr, &ct_info, &helper, log);
671 if (err)
672 return err;
673
674 /* Set up template for tracking connections in specific zones. */
675 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
676 if (!ct_info.ct) {
677 OVS_NLERR(log, "Failed to allocate conntrack template");
678 return -ENOMEM;
679 }
680 if (helper) {
681 err = ovs_ct_add_helper(&ct_info, helper, key, log);
682 if (err)
683 goto err_free_ct;
684 }
685
686 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
687 sizeof(ct_info), log);
688 if (err)
689 goto err_free_ct;
690
691 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
692 nf_conntrack_get(&ct_info.ct->ct_general);
693 return 0;
694 err_free_ct:
695 nf_conntrack_free(ct_info.ct);
696 return err;
697 }
698
699 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
700 struct sk_buff *skb)
701 {
702 struct nlattr *start;
703
704 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
705 if (!start)
706 return -EMSGSIZE;
707
708 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
709 return -EMSGSIZE;
710 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
711 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
712 return -EMSGSIZE;
713 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
714 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
715 &ct_info->mark))
716 return -EMSGSIZE;
717 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
718 labels_nonzero(&ct_info->labels.mask) &&
719 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
720 &ct_info->labels))
721 return -EMSGSIZE;
722 if (ct_info->helper) {
723 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
724 ct_info->helper->name))
725 return -EMSGSIZE;
726 }
727
728 nla_nest_end(skb, start);
729
730 return 0;
731 }
732
733 void ovs_ct_free_action(const struct nlattr *a)
734 {
735 struct ovs_conntrack_info *ct_info = nla_data(a);
736
737 if (ct_info->helper)
738 module_put(ct_info->helper->me);
739 if (ct_info->ct)
740 nf_ct_put(ct_info->ct);
741 }
742
743 void ovs_ct_init(struct net *net)
744 {
745 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
746 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
747
748 if (nf_connlabels_get(net, n_bits)) {
749 ovs_net->xt_label = false;
750 OVS_NLERR(true, "Failed to set connlabel length");
751 } else {
752 ovs_net->xt_label = true;
753 }
754 }
755
756 void ovs_ct_exit(struct net *net)
757 {
758 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
759
760 if (ovs_net->xt_label)
761 nf_connlabels_put(net);
762 }