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_label value;
42 struct ovs_key_ct_label 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_label label;
56 static u16 key_to_nfproto(const struct sw_flow_key *key)
58 switch (ntohs(key->eth.type)) {
64 return NFPROTO_UNSPEC;
68 /* Map SKB connection state into the values used by flow definition. */
69 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
71 u8 ct_state = OVS_CS_F_TRACKED;
74 case IP_CT_ESTABLISHED_REPLY:
75 case IP_CT_RELATED_REPLY:
77 ct_state |= OVS_CS_F_REPLY_DIR;
84 case IP_CT_ESTABLISHED:
85 case IP_CT_ESTABLISHED_REPLY:
86 ct_state |= OVS_CS_F_ESTABLISHED;
89 case IP_CT_RELATED_REPLY:
90 ct_state |= OVS_CS_F_RELATED;
94 ct_state |= OVS_CS_F_NEW;
103 static u32 ovs_ct_get_mark(const struct nf_conn *ct)
105 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
106 return ct ? ct->mark : 0;
112 static void ovs_ct_get_label(const struct nf_conn *ct,
113 struct ovs_key_ct_label *label)
115 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
118 size_t len = cl->words * sizeof(long);
120 if (len > OVS_CT_LABEL_LEN)
121 len = OVS_CT_LABEL_LEN;
122 else if (len < OVS_CT_LABEL_LEN)
123 memset(label, 0, OVS_CT_LABEL_LEN);
124 memcpy(label, cl->bits, len);
126 memset(label, 0, OVS_CT_LABEL_LEN);
130 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
131 const struct nf_conntrack_zone *zone,
132 const struct nf_conn *ct)
134 key->ct.state = state;
135 key->ct.zone = zone->id;
136 key->ct.mark = ovs_ct_get_mark(ct);
137 ovs_ct_get_label(ct, &key->ct.label);
140 /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
141 * previously sent the packet to conntrack via the ct action.
143 static void ovs_ct_update_key(const struct sk_buff *skb,
144 struct sw_flow_key *key, bool post_ct)
146 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
147 enum ip_conntrack_info ctinfo;
151 ct = nf_ct_get(skb, &ctinfo);
153 state = ovs_ct_get_state(ctinfo);
155 state |= OVS_CS_F_RELATED;
156 zone = nf_ct_zone(ct);
157 } else if (post_ct) {
158 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
160 __ovs_ct_update_key(key, state, zone, ct);
163 void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
165 ovs_ct_update_key(skb, key, false);
168 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
170 if (nla_put_u8(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
173 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
174 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
177 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
178 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
181 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
182 nla_put(skb, OVS_KEY_ATTR_CT_LABEL, sizeof(key->ct.label),
189 static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
190 u32 ct_mark, u32 mask)
192 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
193 enum ip_conntrack_info ctinfo;
198 /* The connection could be invalid, in which case set_mark is no-op. */
199 ct = nf_ct_get(skb, &ctinfo);
203 new_mark = ct_mark | (ct->mark & ~(mask));
204 if (ct->mark != new_mark) {
206 nf_conntrack_event_cache(IPCT_MARK, ct);
207 key->ct.mark = new_mark;
216 static int ovs_ct_set_label(struct sk_buff *skb, struct sw_flow_key *key,
217 const struct ovs_key_ct_label *label,
218 const struct ovs_key_ct_label *mask)
220 enum ip_conntrack_info ctinfo;
221 struct nf_conn_labels *cl;
225 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS))
228 /* The connection could be invalid, in which case set_label is no-op.*/
229 ct = nf_ct_get(skb, &ctinfo);
233 cl = nf_ct_labels_find(ct);
235 nf_ct_labels_ext_add(ct);
236 cl = nf_ct_labels_find(ct);
238 if (!cl || cl->words * sizeof(long) < OVS_CT_LABEL_LEN)
241 err = nf_connlabels_replace(ct, (u32 *)label, (u32 *)mask,
242 OVS_CT_LABEL_LEN / sizeof(u32));
246 ovs_ct_get_label(ct, &key->ct.label);
250 /* 'skb' should already be pulled to nh_ofs. */
251 static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
253 const struct nf_conntrack_helper *helper;
254 const struct nf_conn_help *help;
255 enum ip_conntrack_info ctinfo;
256 unsigned int protoff;
259 ct = nf_ct_get(skb, &ctinfo);
260 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
263 help = nfct_help(ct);
267 helper = rcu_dereference(help->helper);
273 protoff = ip_hdrlen(skb);
276 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
279 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
280 &nexthdr, &frag_off);
281 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
282 pr_debug("proto header not found\n");
288 WARN_ONCE(1, "helper invoked on non-IP family!");
292 return helper->help(skb, protoff, ct, ctinfo);
295 static int handle_fragments(struct net *net, struct sw_flow_key *key,
296 u16 zone, struct sk_buff *skb)
298 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
300 if (key->eth.type == htons(ETH_P_IP)) {
301 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
304 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
305 err = ip_defrag(skb, user);
309 ovs_cb.mru = IPCB(skb)->frag_max_size;
310 } else if (key->eth.type == htons(ETH_P_IPV6)) {
311 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
312 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
313 struct sk_buff *reasm;
315 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
316 reasm = nf_ct_frag6_gather(skb, user);
323 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
324 skb_morph(skb, reasm);
326 ovs_cb.mru = IP6CB(skb)->frag_max_size;
328 return -EPFNOSUPPORT;
331 return -EPFNOSUPPORT;
334 key->ip.frag = OVS_FRAG_TYPE_NONE;
337 *OVS_CB(skb) = ovs_cb;
342 static struct nf_conntrack_expect *
343 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
344 u16 proto, const struct sk_buff *skb)
346 struct nf_conntrack_tuple tuple;
348 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
350 return __nf_ct_expect_find(net, zone, &tuple);
353 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
354 static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
355 const struct ovs_conntrack_info *info)
357 enum ip_conntrack_info ctinfo;
360 ct = nf_ct_get(skb, &ctinfo);
363 if (!net_eq(net, read_pnet(&ct->ct_net)))
365 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
368 struct nf_conn_help *help;
370 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
371 if (help && rcu_access_pointer(help->helper) != info->helper)
378 static int __ovs_ct_lookup(struct net *net, const struct sw_flow_key *key,
379 const struct ovs_conntrack_info *info,
382 /* If we are recirculating packets to match on conntrack fields and
383 * committing with a separate conntrack action, then we don't need to
384 * actually run the packet through conntrack twice unless it's for a
387 if (!skb_nfct_cached(net, skb, info)) {
388 struct nf_conn *tmpl = info->ct;
390 /* Associate skb with specified zone. */
393 nf_conntrack_put(skb->nfct);
394 nf_conntrack_get(&tmpl->ct_general);
395 skb->nfct = &tmpl->ct_general;
396 skb->nfctinfo = IP_CT_NEW;
399 if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
403 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
404 WARN_ONCE(1, "helper rejected packet");
412 /* Lookup connection and read fields into key. */
413 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
414 const struct ovs_conntrack_info *info,
417 struct nf_conntrack_expect *exp;
419 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
423 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
424 __ovs_ct_update_key(key, state, &info->zone, exp->master);
428 err = __ovs_ct_lookup(net, key, info, skb);
432 ovs_ct_update_key(skb, key, true);
438 /* Lookup connection and confirm if unconfirmed. */
439 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
440 const struct ovs_conntrack_info *info,
446 state = key->ct.state;
447 if (key->ct.zone == info->zone.id &&
448 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
449 /* Previous lookup has shown that this connection is already
450 * tracked and committed. Skip committing.
455 err = __ovs_ct_lookup(net, key, info, skb);
458 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
461 ovs_ct_update_key(skb, key, true);
466 static bool label_nonzero(const struct ovs_key_ct_label *label)
470 for (i = 0; i < sizeof(*label); i++)
471 if (label->ct_label[i])
477 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
478 struct sw_flow_key *key,
479 const struct ovs_conntrack_info *info)
484 /* The conntrack module expects to be working at L3. */
485 nh_ofs = skb_network_offset(skb);
486 skb_pull(skb, nh_ofs);
488 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
489 err = handle_fragments(net, key, info->zone.id, skb);
494 if (info->flags & OVS_CT_F_COMMIT)
495 err = ovs_ct_commit(net, key, info, skb);
497 err = ovs_ct_lookup(net, key, info, skb);
501 if (info->mark.mask) {
502 err = ovs_ct_set_mark(skb, key, info->mark.value,
507 if (label_nonzero(&info->label.mask))
508 err = ovs_ct_set_label(skb, key, &info->label.value,
511 skb_push(skb, nh_ofs);
515 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
516 const struct sw_flow_key *key, bool log)
518 struct nf_conntrack_helper *helper;
519 struct nf_conn_help *help;
521 helper = nf_conntrack_helper_try_module_get(name, info->family,
524 OVS_NLERR(log, "Unknown helper \"%s\"", name);
528 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
530 module_put(helper->me);
534 rcu_assign_pointer(help->helper, helper);
535 info->helper = helper;
539 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
540 [OVS_CT_ATTR_FLAGS] = { .minlen = sizeof(u32),
541 .maxlen = sizeof(u32) },
542 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
543 .maxlen = sizeof(u16) },
544 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
545 .maxlen = sizeof(struct md_mark) },
546 [OVS_CT_ATTR_LABEL] = { .minlen = sizeof(struct md_label),
547 .maxlen = sizeof(struct md_label) },
548 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
549 .maxlen = NF_CT_HELPER_NAME_LEN }
552 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
553 const char **helper, bool log)
558 nla_for_each_nested(a, attr, rem) {
559 int type = nla_type(a);
560 int maxlen = ovs_ct_attr_lens[type].maxlen;
561 int minlen = ovs_ct_attr_lens[type].minlen;
563 if (type > OVS_CT_ATTR_MAX) {
565 "Unknown conntrack attr (type=%d, max=%d)",
566 type, OVS_CT_ATTR_MAX);
569 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
571 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
572 type, nla_len(a), maxlen);
577 case OVS_CT_ATTR_FLAGS:
578 info->flags = nla_get_u32(a);
580 #ifdef CONFIG_NF_CONNTRACK_ZONES
581 case OVS_CT_ATTR_ZONE:
582 info->zone.id = nla_get_u16(a);
585 #ifdef CONFIG_NF_CONNTRACK_MARK
586 case OVS_CT_ATTR_MARK: {
587 struct md_mark *mark = nla_data(a);
593 #ifdef CONFIG_NF_CONNTRACK_LABELS
594 case OVS_CT_ATTR_LABEL: {
595 struct md_label *label = nla_data(a);
597 info->label = *label;
601 case OVS_CT_ATTR_HELPER:
602 *helper = nla_data(a);
603 if (!memchr(*helper, '\0', nla_len(a))) {
604 OVS_NLERR(log, "Invalid conntrack helper");
609 OVS_NLERR(log, "Unknown conntrack attr (%d)",
616 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
623 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
625 if (attr == OVS_KEY_ATTR_CT_STATE)
627 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
628 attr == OVS_KEY_ATTR_CT_ZONE)
630 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
631 attr == OVS_KEY_ATTR_CT_MARK)
633 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
634 attr == OVS_KEY_ATTR_CT_LABEL) {
635 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
637 return ovs_net->xt_label;
643 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
644 const struct sw_flow_key *key,
645 struct sw_flow_actions **sfa, bool log)
647 struct ovs_conntrack_info ct_info;
648 const char *helper = NULL;
652 family = key_to_nfproto(key);
653 if (family == NFPROTO_UNSPEC) {
654 OVS_NLERR(log, "ct family unspecified");
658 memset(&ct_info, 0, sizeof(ct_info));
659 ct_info.family = family;
661 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
662 NF_CT_DEFAULT_ZONE_DIR, 0);
664 err = parse_ct(attr, &ct_info, &helper, log);
668 /* Set up template for tracking connections in specific zones. */
669 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
671 OVS_NLERR(log, "Failed to allocate conntrack template");
675 err = ovs_ct_add_helper(&ct_info, helper, key, log);
680 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
681 sizeof(ct_info), log);
685 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
686 nf_conntrack_get(&ct_info.ct->ct_general);
689 nf_conntrack_free(ct_info.ct);
693 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
696 struct nlattr *start;
698 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
702 if (nla_put_u32(skb, OVS_CT_ATTR_FLAGS, ct_info->flags))
704 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
705 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
707 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
708 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
711 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
712 nla_put(skb, OVS_CT_ATTR_LABEL, sizeof(ct_info->label),
715 if (ct_info->helper) {
716 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
717 ct_info->helper->name))
721 nla_nest_end(skb, start);
726 void ovs_ct_free_action(const struct nlattr *a)
728 struct ovs_conntrack_info *ct_info = nla_data(a);
731 module_put(ct_info->helper->me);
733 nf_ct_put(ct_info->ct);
736 void ovs_ct_init(struct net *net)
738 unsigned int n_bits = sizeof(struct ovs_key_ct_label) * BITS_PER_BYTE;
739 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
741 if (nf_connlabels_get(net, n_bits)) {
742 ovs_net->xt_label = false;
743 OVS_NLERR(true, "Failed to set connlabel length");
745 ovs_net->xt_label = true;
749 void ovs_ct_exit(struct net *net)
751 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
753 if (ovs_net->xt_label)
754 nf_connlabels_put(net);