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