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