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