openvswitch: Fix helper reference leak
[firefly-linux-kernel-4.4.55.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         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
105 static 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
114 static void ovs_ct_get_labels(const struct nf_conn *ct,
115                               struct ovs_key_ct_labels *labels)
116 {
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
122                 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);
127         } else {
128                 memset(labels, 0, OVS_CT_LABELS_LEN);
129         }
130 }
131
132 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
133                                 const struct nf_conntrack_zone *zone,
134                                 const struct nf_conn *ct)
135 {
136         key->ct.state = state;
137         key->ct.zone = zone->id;
138         key->ct.mark = ovs_ct_get_mark(ct);
139         ovs_ct_get_labels(ct, &key->ct.labels);
140 }
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  */
145 static 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);
156                 if (!nf_ct_is_confirmed(ct))
157                         state |= OVS_CS_F_NEW;
158                 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         }
164         __ovs_ct_update_key(key, state, zone, ct);
165 }
166
167 void 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
172 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
173 {
174         if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
175                 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
181         if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
182             nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
183                 return -EMSGSIZE;
184
185         if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
186             nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
187                     &key->ct.labels))
188                 return -EMSGSIZE;
189
190         return 0;
191 }
192
193 static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
194                            u32 ct_mark, u32 mask)
195 {
196 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
197         enum ip_conntrack_info ctinfo;
198         struct nf_conn *ct;
199         u32 new_mark;
200
201
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
214         return 0;
215 #else
216         return -ENOTSUPP;
217 #endif
218 }
219
220 static 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)
223 {
224         enum ip_conntrack_info ctinfo;
225         struct nf_conn_labels *cl;
226         struct nf_conn *ct;
227         int err;
228
229         /* 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         }
239         if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
240                 return -ENOSPC;
241
242         err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
243                                     OVS_CT_LABELS_LEN / sizeof(u32));
244         if (err)
245                 return err;
246
247         ovs_ct_get_labels(ct, &key->ct.labels);
248         return 0;
249 }
250
251 /* 'skb' should already be pulled to nh_ofs. */
252 static 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;
279                 int ofs;
280
281                 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
282                                        &frag_off);
283                 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
284                         pr_debug("proto header not found\n");
285                         return NF_ACCEPT;
286                 }
287                 protoff = ofs;
288                 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
298 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
299  * value if 'skb' is freed.
300  */
301 static 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));
311                 err = ip_defrag(net, skb, user);
312                 if (err)
313                         return err;
314
315                 ovs_cb.mru = IPCB(skb)->frag_max_size;
316 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
317         } else if (key->eth.type == htons(ETH_P_IPV6)) {
318                 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));
322                 reasm = nf_ct_frag6_gather(net, skb, user);
323                 if (!reasm)
324                         return -EINPROGRESS;
325
326                 if (skb == reasm) {
327                         kfree_skb(skb);
328                         return -EINVAL;
329                 }
330
331                 /* 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);
336
337                 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
338                 skb_morph(skb, reasm);
339                 skb->next = reasm->next;
340                 consume_skb(reasm);
341                 ovs_cb.mru = IP6CB(skb)->frag_max_size;
342 #endif
343         } else {
344                 kfree_skb(skb);
345                 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
356 static struct nf_conntrack_expect *
357 ovs_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
362         if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
363                 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. */
368 static 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;
381         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         }
388
389         return true;
390 }
391
392 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
393                            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;
416
417                 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
418                         WARN_ONCE(1, "helper rejected packet");
419                         return -EINVAL;
420                 }
421         }
422
423         ovs_ct_update_key(skb, key, true);
424
425         return 0;
426 }
427
428 /* Lookup connection and read fields into key. */
429 static 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;
440                 __ovs_ct_update_key(key, state, &info->zone, exp->master);
441         } else {
442                 int err;
443
444                 err = __ovs_ct_lookup(net, key, info, skb);
445                 if (err)
446                         return err;
447         }
448
449         return 0;
450 }
451
452 /* Lookup connection and confirm if unconfirmed. */
453 static 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
475         return 0;
476 }
477
478 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
479 {
480         size_t i;
481
482         for (i = 0; i < sizeof(*labels); i++)
483                 if (labels->ct_labels[i])
484                         return true;
485
486         return false;
487 }
488
489 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
490  * value if 'skb' is freed.
491  */
492 int 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
509         if (info->commit)
510                 err = ovs_ct_commit(net, key, info, skb);
511         else
512                 err = ovs_ct_lookup(net, key, info, skb);
513         if (err)
514                 goto err;
515
516         if (info->mark.mask) {
517                 err = ovs_ct_set_mark(skb, key, info->mark.value,
518                                       info->mark.mask);
519                 if (err)
520                         goto err;
521         }
522         if (labels_nonzero(&info->labels.mask))
523                 err = ovs_ct_set_labels(skb, key, &info->labels.value,
524                                         &info->labels.mask);
525 err:
526         skb_push(skb, nh_ofs);
527         if (err)
528                 kfree_skb(skb);
529         return err;
530 }
531
532 static 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
556 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
557         [OVS_CT_ATTR_COMMIT]    = { .minlen = 0, .maxlen = 0 },
558         [OVS_CT_ATTR_ZONE]      = { .minlen = sizeof(u16),
559                                     .maxlen = sizeof(u16) },
560         [OVS_CT_ATTR_MARK]      = { .minlen = sizeof(struct md_mark),
561                                     .maxlen = sizeof(struct md_mark) },
562         [OVS_CT_ATTR_LABELS]    = { .minlen = sizeof(struct md_labels),
563                                     .maxlen = sizeof(struct md_labels) },
564         [OVS_CT_ATTR_HELPER]    = { .minlen = 1,
565                                     .maxlen = NF_CT_HELPER_NAME_LEN }
566 };
567
568 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
569                     const char **helper, bool log)
570 {
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) {
593                 case OVS_CT_ATTR_COMMIT:
594                         info->commit = true;
595                         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
601 #ifdef CONFIG_NF_CONNTRACK_MARK
602                 case OVS_CT_ATTR_MARK: {
603                         struct md_mark *mark = nla_data(a);
604
605                         if (!mark->mask) {
606                                 OVS_NLERR(log, "ct_mark mask cannot be 0");
607                                 return -EINVAL;
608                         }
609                         info->mark = *mark;
610                         break;
611                 }
612 #endif
613 #ifdef CONFIG_NF_CONNTRACK_LABELS
614                 case OVS_CT_ATTR_LABELS: {
615                         struct md_labels *labels = nla_data(a);
616
617                         if (!labels_nonzero(&labels->mask)) {
618                                 OVS_NLERR(log, "ct_labels mask cannot be 0");
619                                 return -EINVAL;
620                         }
621                         info->labels = *labels;
622                         break;
623                 }
624 #endif
625                 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;
632                 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
647 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
648 {
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;
654         if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
655             attr == OVS_KEY_ATTR_CT_MARK)
656                 return true;
657         if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
658             attr == OVS_KEY_ATTR_CT_LABELS) {
659                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
660
661                 return ovs_net->xt_label;
662         }
663
664         return false;
665 }
666
667 int 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;
672         const char *helper = NULL;
673         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
688         err = parse_ct(attr, &ct_info, &helper, log);
689         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         }
698         if (helper) {
699                 err = ovs_ct_add_helper(&ct_info, helper, key, log);
700                 if (err)
701                         goto err_free_ct;
702         }
703
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;
712 err_free_ct:
713         __ovs_ct_free_action(&ct_info);
714         return err;
715 }
716
717 int 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
726         if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
727                 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;
731         if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
732             nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
733                     &ct_info->mark))
734                 return -EMSGSIZE;
735         if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
736             labels_nonzero(&ct_info->labels.mask) &&
737             nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
738                     &ct_info->labels))
739                 return -EMSGSIZE;
740         if (ct_info->helper) {
741                 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
742                                    ct_info->helper->name))
743                         return -EMSGSIZE;
744         }
745
746         nla_nest_end(skb, start);
747
748         return 0;
749 }
750
751 void ovs_ct_free_action(const struct nlattr *a)
752 {
753         struct ovs_conntrack_info *ct_info = nla_data(a);
754
755         __ovs_ct_free_action(ct_info);
756 }
757
758 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
759 {
760         if (ct_info->helper)
761                 module_put(ct_info->helper->me);
762         if (ct_info->ct)
763                 nf_ct_put(ct_info->ct);
764 }
765
766 void ovs_ct_init(struct net *net)
767 {
768         unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
769         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
779 void 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 }