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/module.h>
15 #include <linux/openvswitch.h>
17 #include <net/netfilter/nf_conntrack_core.h>
18 #include <net/netfilter/nf_conntrack_helper.h>
19 #include <net/netfilter/nf_conntrack_labels.h>
20 #include <net/netfilter/nf_conntrack_zones.h>
21 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
24 #include "conntrack.h"
26 #include "flow_netlink.h"
28 struct ovs_ct_len_tbl {
33 /* Metadata mark for masked write to conntrack mark */
39 /* Metadata label for masked write to conntrack label. */
41 struct ovs_key_ct_labels value;
42 struct ovs_key_ct_labels mask;
45 /* Conntrack action context for execution. */
46 struct ovs_conntrack_info {
47 struct nf_conntrack_helper *helper;
48 struct nf_conntrack_zone zone;
53 struct md_labels labels;
56 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
58 static u16 key_to_nfproto(const struct sw_flow_key *key)
60 switch (ntohs(key->eth.type)) {
66 return NFPROTO_UNSPEC;
70 /* Map SKB connection state into the values used by flow definition. */
71 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
73 u8 ct_state = OVS_CS_F_TRACKED;
76 case IP_CT_ESTABLISHED_REPLY:
77 case IP_CT_RELATED_REPLY:
79 ct_state |= OVS_CS_F_REPLY_DIR;
86 case IP_CT_ESTABLISHED:
87 case IP_CT_ESTABLISHED_REPLY:
88 ct_state |= OVS_CS_F_ESTABLISHED;
91 case IP_CT_RELATED_REPLY:
92 ct_state |= OVS_CS_F_RELATED;
96 ct_state |= OVS_CS_F_NEW;
105 static u32 ovs_ct_get_mark(const struct nf_conn *ct)
107 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
108 return ct ? ct->mark : 0;
114 static void ovs_ct_get_labels(const struct nf_conn *ct,
115 struct ovs_key_ct_labels *labels)
117 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
120 size_t len = cl->words * sizeof(long);
122 if (len > OVS_CT_LABELS_LEN)
123 len = OVS_CT_LABELS_LEN;
124 else if (len < OVS_CT_LABELS_LEN)
125 memset(labels, 0, OVS_CT_LABELS_LEN);
126 memcpy(labels, cl->bits, len);
128 memset(labels, 0, OVS_CT_LABELS_LEN);
132 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
133 const struct nf_conntrack_zone *zone,
134 const struct nf_conn *ct)
136 key->ct.state = state;
137 key->ct.zone = zone->id;
138 key->ct.mark = ovs_ct_get_mark(ct);
139 ovs_ct_get_labels(ct, &key->ct.labels);
142 /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
143 * previously sent the packet to conntrack via the ct action.
145 static void ovs_ct_update_key(const struct sk_buff *skb,
146 struct sw_flow_key *key, bool post_ct)
148 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
149 enum ip_conntrack_info ctinfo;
153 ct = nf_ct_get(skb, &ctinfo);
155 state = ovs_ct_get_state(ctinfo);
156 if (!nf_ct_is_confirmed(ct))
157 state |= OVS_CS_F_NEW;
159 state |= OVS_CS_F_RELATED;
160 zone = nf_ct_zone(ct);
161 } else if (post_ct) {
162 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
164 __ovs_ct_update_key(key, state, zone, ct);
167 void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
169 ovs_ct_update_key(skb, key, false);
172 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
174 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
177 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
178 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
181 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
182 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
185 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
186 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
193 static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
194 u32 ct_mark, u32 mask)
196 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
197 enum ip_conntrack_info ctinfo;
202 /* The connection could be invalid, in which case set_mark is no-op. */
203 ct = nf_ct_get(skb, &ctinfo);
207 new_mark = ct_mark | (ct->mark & ~(mask));
208 if (ct->mark != new_mark) {
210 nf_conntrack_event_cache(IPCT_MARK, ct);
211 key->ct.mark = new_mark;
220 static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
221 const struct ovs_key_ct_labels *labels,
222 const struct ovs_key_ct_labels *mask)
224 enum ip_conntrack_info ctinfo;
225 struct nf_conn_labels *cl;
229 /* The connection could be invalid, in which case set_label is no-op.*/
230 ct = nf_ct_get(skb, &ctinfo);
234 cl = nf_ct_labels_find(ct);
236 nf_ct_labels_ext_add(ct);
237 cl = nf_ct_labels_find(ct);
239 if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
242 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
243 OVS_CT_LABELS_LEN / sizeof(u32));
247 ovs_ct_get_labels(ct, &key->ct.labels);
251 /* 'skb' should already be pulled to nh_ofs. */
252 static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
254 const struct nf_conntrack_helper *helper;
255 const struct nf_conn_help *help;
256 enum ip_conntrack_info ctinfo;
257 unsigned int protoff;
260 ct = nf_ct_get(skb, &ctinfo);
261 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
264 help = nfct_help(ct);
268 helper = rcu_dereference(help->helper);
274 protoff = ip_hdrlen(skb);
277 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
281 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
283 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
284 pr_debug("proto header not found\n");
291 WARN_ONCE(1, "helper invoked on non-IP family!");
295 return helper->help(skb, protoff, ct, ctinfo);
298 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
299 * value if 'skb' is freed.
301 static int handle_fragments(struct net *net, struct sw_flow_key *key,
302 u16 zone, struct sk_buff *skb)
304 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
306 if (key->eth.type == htons(ETH_P_IP)) {
307 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
310 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
311 err = ip_defrag(net, skb, user);
315 ovs_cb.mru = IPCB(skb)->frag_max_size;
316 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
317 } else if (key->eth.type == htons(ETH_P_IPV6)) {
318 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
319 struct sk_buff *reasm;
321 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
322 reasm = nf_ct_frag6_gather(net, skb, user);
331 /* Don't free 'skb' even though it is one of the original
332 * fragments, as we're going to morph it into the head.
335 nf_ct_frag6_consume_orig(reasm);
337 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
338 skb_morph(skb, reasm);
339 skb->next = reasm->next;
341 ovs_cb.mru = IP6CB(skb)->frag_max_size;
345 return -EPFNOSUPPORT;
348 key->ip.frag = OVS_FRAG_TYPE_NONE;
351 *OVS_CB(skb) = ovs_cb;
356 static struct nf_conntrack_expect *
357 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
358 u16 proto, const struct sk_buff *skb)
360 struct nf_conntrack_tuple tuple;
362 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
364 return __nf_ct_expect_find(net, zone, &tuple);
367 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
368 static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
369 const struct ovs_conntrack_info *info)
371 enum ip_conntrack_info ctinfo;
374 ct = nf_ct_get(skb, &ctinfo);
377 if (!net_eq(net, read_pnet(&ct->ct_net)))
379 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
382 struct nf_conn_help *help;
384 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
385 if (help && rcu_access_pointer(help->helper) != info->helper)
392 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
393 const struct ovs_conntrack_info *info,
396 /* If we are recirculating packets to match on conntrack fields and
397 * committing with a separate conntrack action, then we don't need to
398 * actually run the packet through conntrack twice unless it's for a
401 if (!skb_nfct_cached(net, skb, info)) {
402 struct nf_conn *tmpl = info->ct;
404 /* Associate skb with specified zone. */
407 nf_conntrack_put(skb->nfct);
408 nf_conntrack_get(&tmpl->ct_general);
409 skb->nfct = &tmpl->ct_general;
410 skb->nfctinfo = IP_CT_NEW;
413 if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
417 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
418 WARN_ONCE(1, "helper rejected packet");
423 ovs_ct_update_key(skb, key, true);
428 /* Lookup connection and read fields into key. */
429 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
430 const struct ovs_conntrack_info *info,
433 struct nf_conntrack_expect *exp;
435 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
439 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
440 __ovs_ct_update_key(key, state, &info->zone, exp->master);
444 err = __ovs_ct_lookup(net, key, info, skb);
452 /* Lookup connection and confirm if unconfirmed. */
453 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
454 const struct ovs_conntrack_info *info,
460 state = key->ct.state;
461 if (key->ct.zone == info->zone.id &&
462 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
463 /* Previous lookup has shown that this connection is already
464 * tracked and committed. Skip committing.
469 err = __ovs_ct_lookup(net, key, info, skb);
472 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
478 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
482 for (i = 0; i < sizeof(*labels); i++)
483 if (labels->ct_labels[i])
489 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
490 * value if 'skb' is freed.
492 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
493 struct sw_flow_key *key,
494 const struct ovs_conntrack_info *info)
499 /* The conntrack module expects to be working at L3. */
500 nh_ofs = skb_network_offset(skb);
501 skb_pull(skb, nh_ofs);
503 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
504 err = handle_fragments(net, key, info->zone.id, skb);
510 err = ovs_ct_commit(net, key, info, skb);
512 err = ovs_ct_lookup(net, key, info, skb);
516 if (info->mark.mask) {
517 err = ovs_ct_set_mark(skb, key, info->mark.value,
522 if (labels_nonzero(&info->labels.mask))
523 err = ovs_ct_set_labels(skb, key, &info->labels.value,
526 skb_push(skb, nh_ofs);
532 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
533 const struct sw_flow_key *key, bool log)
535 struct nf_conntrack_helper *helper;
536 struct nf_conn_help *help;
538 helper = nf_conntrack_helper_try_module_get(name, info->family,
541 OVS_NLERR(log, "Unknown helper \"%s\"", name);
545 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
547 module_put(helper->me);
551 rcu_assign_pointer(help->helper, helper);
552 info->helper = helper;
556 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
557 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
558 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
559 .maxlen = sizeof(u16) },
560 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
561 .maxlen = sizeof(struct md_mark) },
562 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
563 .maxlen = sizeof(struct md_labels) },
564 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
565 .maxlen = NF_CT_HELPER_NAME_LEN }
568 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
569 const char **helper, bool log)
574 nla_for_each_nested(a, attr, rem) {
575 int type = nla_type(a);
576 int maxlen = ovs_ct_attr_lens[type].maxlen;
577 int minlen = ovs_ct_attr_lens[type].minlen;
579 if (type > OVS_CT_ATTR_MAX) {
581 "Unknown conntrack attr (type=%d, max=%d)",
582 type, OVS_CT_ATTR_MAX);
585 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
587 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
588 type, nla_len(a), maxlen);
593 case OVS_CT_ATTR_COMMIT:
596 #ifdef CONFIG_NF_CONNTRACK_ZONES
597 case OVS_CT_ATTR_ZONE:
598 info->zone.id = nla_get_u16(a);
601 #ifdef CONFIG_NF_CONNTRACK_MARK
602 case OVS_CT_ATTR_MARK: {
603 struct md_mark *mark = nla_data(a);
606 OVS_NLERR(log, "ct_mark mask cannot be 0");
613 #ifdef CONFIG_NF_CONNTRACK_LABELS
614 case OVS_CT_ATTR_LABELS: {
615 struct md_labels *labels = nla_data(a);
617 if (!labels_nonzero(&labels->mask)) {
618 OVS_NLERR(log, "ct_labels mask cannot be 0");
621 info->labels = *labels;
625 case OVS_CT_ATTR_HELPER:
626 *helper = nla_data(a);
627 if (!memchr(*helper, '\0', nla_len(a))) {
628 OVS_NLERR(log, "Invalid conntrack helper");
633 OVS_NLERR(log, "Unknown conntrack attr (%d)",
640 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
647 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
649 if (attr == OVS_KEY_ATTR_CT_STATE)
651 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
652 attr == OVS_KEY_ATTR_CT_ZONE)
654 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
655 attr == OVS_KEY_ATTR_CT_MARK)
657 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
658 attr == OVS_KEY_ATTR_CT_LABELS) {
659 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
661 return ovs_net->xt_label;
667 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
668 const struct sw_flow_key *key,
669 struct sw_flow_actions **sfa, bool log)
671 struct ovs_conntrack_info ct_info;
672 const char *helper = NULL;
676 family = key_to_nfproto(key);
677 if (family == NFPROTO_UNSPEC) {
678 OVS_NLERR(log, "ct family unspecified");
682 memset(&ct_info, 0, sizeof(ct_info));
683 ct_info.family = family;
685 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
686 NF_CT_DEFAULT_ZONE_DIR, 0);
688 err = parse_ct(attr, &ct_info, &helper, log);
692 /* Set up template for tracking connections in specific zones. */
693 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
695 OVS_NLERR(log, "Failed to allocate conntrack template");
699 err = ovs_ct_add_helper(&ct_info, helper, key, log);
704 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
705 sizeof(ct_info), log);
709 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
710 nf_conntrack_get(&ct_info.ct->ct_general);
713 __ovs_ct_free_action(&ct_info);
717 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
720 struct nlattr *start;
722 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
726 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
728 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
729 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
731 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
732 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
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),
740 if (ct_info->helper) {
741 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
742 ct_info->helper->name))
746 nla_nest_end(skb, start);
751 void ovs_ct_free_action(const struct nlattr *a)
753 struct ovs_conntrack_info *ct_info = nla_data(a);
755 __ovs_ct_free_action(ct_info);
758 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
761 module_put(ct_info->helper->me);
763 nf_ct_put(ct_info->ct);
766 void ovs_ct_init(struct net *net)
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);
771 if (nf_connlabels_get(net, n_bits)) {
772 ovs_net->xt_label = false;
773 OVS_NLERR(true, "Failed to set connlabel length");
775 ovs_net->xt_label = true;
779 void ovs_ct_exit(struct net *net)
781 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
783 if (ovs_net->xt_label)
784 nf_connlabels_put(net);