2 * Copyright (c) 2015 Nicira, Inc.
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.
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.
14 #include <linux/kconfig.h>
15 #include <linux/version.h>
17 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,9,0) && \
18 IS_ENABLED(CONFIG_NF_CONNTRACK)
20 #include <linux/module.h>
21 #include <linux/openvswitch.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>
30 #include "conntrack.h"
32 #include "flow_netlink.h"
34 struct ovs_ct_len_tbl {
39 /* Metadata mark for masked write to conntrack mark */
45 /* Metadata label for masked write to conntrack label. */
47 struct ovs_key_ct_labels value;
48 struct ovs_key_ct_labels mask;
51 /* Conntrack action context for execution. */
52 struct ovs_conntrack_info {
53 struct nf_conntrack_helper *helper;
54 struct nf_conntrack_zone zone;
59 struct md_labels labels;
62 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
64 static u16 key_to_nfproto(const struct sw_flow_key *key)
66 switch (ntohs(key->eth.type)) {
72 return NFPROTO_UNSPEC;
76 /* Map SKB connection state into the values used by flow definition. */
77 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
79 u8 ct_state = OVS_CS_F_TRACKED;
82 case IP_CT_ESTABLISHED_REPLY:
83 case IP_CT_RELATED_REPLY:
85 ct_state |= OVS_CS_F_REPLY_DIR;
92 case IP_CT_ESTABLISHED:
93 case IP_CT_ESTABLISHED_REPLY:
94 ct_state |= OVS_CS_F_ESTABLISHED;
97 case IP_CT_RELATED_REPLY:
98 ct_state |= OVS_CS_F_RELATED;
101 case IP_CT_NEW_REPLY:
102 ct_state |= OVS_CS_F_NEW;
111 static u32 ovs_ct_get_mark(const struct nf_conn *ct)
113 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
114 return ct ? ct->mark : 0;
120 static void ovs_ct_get_labels(const struct nf_conn *ct,
121 struct ovs_key_ct_labels *labels)
123 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
126 size_t len = cl->words * sizeof(long);
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);
134 memset(labels, 0, OVS_CT_LABELS_LEN);
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)
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);
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.
151 static void ovs_ct_update_key(const struct sk_buff *skb,
152 struct sw_flow_key *key, bool post_ct)
154 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
155 enum ip_conntrack_info ctinfo;
159 ct = nf_ct_get(skb, &ctinfo);
161 state = ovs_ct_get_state(ctinfo);
162 if (!nf_ct_is_confirmed(ct))
163 state |= OVS_CS_F_NEW;
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;
170 __ovs_ct_update_key(key, state, zone, ct);
173 void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
175 ovs_ct_update_key(skb, key, false);
178 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
180 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
183 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
184 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
187 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
188 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
191 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
192 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
199 static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
200 u32 ct_mark, u32 mask)
202 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
203 enum ip_conntrack_info ctinfo;
208 /* The connection could be invalid, in which case set_mark is no-op. */
209 ct = nf_ct_get(skb, &ctinfo);
213 new_mark = ct_mark | (ct->mark & ~(mask));
214 if (ct->mark != new_mark) {
216 nf_conntrack_event_cache(IPCT_MARK, ct);
217 key->ct.mark = new_mark;
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)
230 enum ip_conntrack_info ctinfo;
231 struct nf_conn_labels *cl;
235 /* The connection could be invalid, in which case set_label is no-op.*/
236 ct = nf_ct_get(skb, &ctinfo);
240 cl = nf_ct_labels_find(ct);
242 nf_ct_labels_ext_add(ct);
243 cl = nf_ct_labels_find(ct);
245 if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
248 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
249 OVS_CT_LABELS_LEN / sizeof(u32));
253 ovs_ct_get_labels(ct, &key->ct.labels);
257 /* 'skb' should already be pulled to nh_ofs. */
258 static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
260 const struct nf_conntrack_helper *helper;
261 const struct nf_conn_help *help;
262 enum ip_conntrack_info ctinfo;
263 unsigned int protoff;
266 ct = nf_ct_get(skb, &ctinfo);
267 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
270 help = nfct_help(ct);
274 helper = rcu_dereference(help->helper);
280 protoff = ip_hdrlen(skb);
283 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
287 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
289 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
290 pr_debug("proto header not found\n");
297 WARN_ONCE(1, "helper invoked on non-IP family!");
301 return helper->help(skb, protoff, ct, ctinfo);
304 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
305 * value if 'skb' is freed.
307 static int handle_fragments(struct net *net, struct sw_flow_key *key,
308 u16 zone, struct sk_buff *skb)
310 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
313 OVS_NLERR(true, "%s: skb has no dev; dropping", __func__);
317 if (key->eth.type == htons(ETH_P_IP)) {
318 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
321 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
322 err = ip_defrag(skb, user);
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;
332 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
333 reasm = nf_ct_frag6_gather(skb, user);
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.
346 nf_ct_frag6_consume_orig(reasm);
348 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
349 skb_morph(skb, reasm);
350 skb->next = reasm->next;
352 ovs_cb.mru = IP6CB(skb)->frag_max_size;
353 #endif /* IP frag support */
356 return -EPFNOSUPPORT;
359 key->ip.frag = OVS_FRAG_TYPE_NONE;
362 *OVS_CB(skb) = ovs_cb;
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)
371 struct nf_conntrack_tuple tuple;
373 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
375 return __nf_ct_expect_find(net, zone, &tuple);
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)
382 enum ip_conntrack_info ctinfo;
385 ct = nf_ct_get(skb, &ctinfo);
388 if (!net_eq(net, read_pnet(&ct->ct_net)))
390 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
393 struct nf_conn_help *help;
395 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
396 if (help && rcu_access_pointer(help->helper) != info->helper)
403 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
404 const struct ovs_conntrack_info *info,
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
412 if (!skb_nfct_cached(net, skb, info)) {
413 struct nf_conn *tmpl = info->ct;
415 /* Associate skb with specified zone. */
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;
424 if (nf_conntrack_in(net, info->family, NF_INET_FORWARD,
428 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
429 WARN_ONCE(1, "helper rejected packet");
434 ovs_ct_update_key(skb, key, true);
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,
444 struct nf_conntrack_expect *exp;
446 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
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);
455 err = __ovs_ct_lookup(net, key, info, skb);
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,
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.
480 err = __ovs_ct_lookup(net, key, info, skb);
483 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
489 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
493 for (i = 0; i < sizeof(*labels); i++)
494 if (labels->ct_labels[i])
500 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
501 * value if 'skb' is freed.
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)
510 /* The conntrack module expects to be working at L3. */
511 nh_ofs = skb_network_offset(skb);
512 skb_pull(skb, nh_ofs);
514 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
515 err = handle_fragments(net, key, info->zone.id, skb);
521 err = ovs_ct_commit(net, key, info, skb);
523 err = ovs_ct_lookup(net, key, info, skb);
527 if (info->mark.mask) {
528 err = ovs_ct_set_mark(skb, key, info->mark.value,
533 if (labels_nonzero(&info->labels.mask))
534 err = ovs_ct_set_labels(skb, key, &info->labels.value,
537 skb_push(skb, nh_ofs);
543 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
544 const struct sw_flow_key *key, bool log)
546 struct nf_conntrack_helper *helper;
547 struct nf_conn_help *help;
549 helper = nf_conntrack_helper_try_module_get(name, info->family,
552 OVS_NLERR(log, "Unknown helper \"%s\"", name);
556 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
558 module_put(helper->me);
562 rcu_assign_pointer(help->helper, helper);
563 info->helper = helper;
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 }
579 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
580 const char **helper, bool log)
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;
590 if (type > OVS_CT_ATTR_MAX) {
592 "Unknown conntrack attr (type=%d, max=%d)",
593 type, OVS_CT_ATTR_MAX);
596 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
598 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
599 type, nla_len(a), maxlen);
604 case OVS_CT_ATTR_COMMIT:
607 #ifdef CONFIG_NF_CONNTRACK_ZONES
608 case OVS_CT_ATTR_ZONE:
609 info->zone.id = nla_get_u16(a);
612 #ifdef CONFIG_NF_CONNTRACK_MARK
613 case OVS_CT_ATTR_MARK: {
614 struct md_mark *mark = nla_data(a);
617 OVS_NLERR(log, "ct_mark mask cannot be 0");
624 #ifdef CONFIG_NF_CONNTRACK_LABELS
625 case OVS_CT_ATTR_LABELS: {
626 struct md_labels *labels = nla_data(a);
628 if (!labels_nonzero(&labels->mask)) {
629 OVS_NLERR(log, "ct_labels mask cannot be 0");
632 info->labels = *labels;
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");
644 OVS_NLERR(log, "Unknown conntrack attr (%d)",
651 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
658 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
660 if (attr == OVS_KEY_ATTR_CT_STATE)
662 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
663 attr == OVS_KEY_ATTR_CT_ZONE)
665 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
666 attr == OVS_KEY_ATTR_CT_MARK)
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);
672 return ovs_net->xt_label;
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)
682 struct ovs_conntrack_info ct_info;
683 const char *helper = NULL;
687 family = key_to_nfproto(key);
688 if (family == NFPROTO_UNSPEC) {
689 OVS_NLERR(log, "ct family unspecified");
693 memset(&ct_info, 0, sizeof(ct_info));
694 ct_info.family = family;
696 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
697 NF_CT_DEFAULT_ZONE_DIR, 0);
699 err = parse_ct(attr, &ct_info, &helper, log);
703 /* Set up template for tracking connections in specific zones. */
704 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
706 OVS_NLERR(log, "Failed to allocate conntrack template");
710 err = ovs_ct_add_helper(&ct_info, helper, key, log);
715 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
716 sizeof(ct_info), log);
720 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
721 nf_conntrack_get(&ct_info.ct->ct_general);
724 __ovs_ct_free_action(&ct_info);
728 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
731 struct nlattr *start;
733 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
737 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
739 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
740 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
742 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
743 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
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),
751 if (ct_info->helper) {
752 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
753 ct_info->helper->name))
757 nla_nest_end(skb, start);
762 void ovs_ct_free_action(const struct nlattr *a)
764 struct ovs_conntrack_info *ct_info = nla_data(a);
766 __ovs_ct_free_action(ct_info);
769 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
772 module_put(ct_info->helper->me);
774 nf_ct_tmpl_free(ct_info->ct);
777 void ovs_ct_init(struct net *net)
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);
782 if (nf_connlabels_get(net, n_bits)) {
783 ovs_net->xt_label = false;
784 OVS_NLERR(true, "Failed to set connlabel length");
786 ovs_net->xt_label = true;
790 void ovs_ct_exit(struct net *net)
792 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
794 if (ovs_net->xt_label)
795 nf_connlabels_put(net);
798 #endif /* CONFIG_NF_CONNTRACK && LINUX > 3.9 */