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