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_zones.h>
25 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
28 #include "conntrack.h"
30 #include "flow_netlink.h"
32 struct ovs_ct_len_tbl {
37 /* Conntrack action context for execution. */
38 struct ovs_conntrack_info {
39 struct nf_conntrack_zone zone;
45 static u16 key_to_nfproto(const struct sw_flow_key *key)
47 switch (ntohs(key->eth.type)) {
53 return NFPROTO_UNSPEC;
57 /* Map SKB connection state into the values used by flow definition. */
58 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
60 u8 ct_state = OVS_CS_F_TRACKED;
63 case IP_CT_ESTABLISHED_REPLY:
64 case IP_CT_RELATED_REPLY:
66 ct_state |= OVS_CS_F_REPLY_DIR;
73 case IP_CT_ESTABLISHED:
74 case IP_CT_ESTABLISHED_REPLY:
75 ct_state |= OVS_CS_F_ESTABLISHED;
78 case IP_CT_RELATED_REPLY:
79 ct_state |= OVS_CS_F_RELATED;
83 ct_state |= OVS_CS_F_NEW;
92 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
93 const struct nf_conntrack_zone *zone)
95 key->ct.state = state;
96 key->ct.zone = zone->id;
99 /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
100 * previously sent the packet to conntrack via the ct action.
102 static void ovs_ct_update_key(const struct sk_buff *skb,
103 struct sw_flow_key *key, bool post_ct)
105 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
106 enum ip_conntrack_info ctinfo;
110 ct = nf_ct_get(skb, &ctinfo);
112 state = ovs_ct_get_state(ctinfo);
114 state |= OVS_CS_F_RELATED;
115 zone = nf_ct_zone(ct);
116 } else if (post_ct) {
117 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
119 __ovs_ct_update_key(key, state, zone);
122 void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
124 ovs_ct_update_key(skb, key, false);
127 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
129 if (nla_put_u8(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
132 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
133 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
139 static int handle_fragments(struct net *net, struct sw_flow_key *key,
140 u16 zone, struct sk_buff *skb)
142 struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
145 OVS_NLERR(true, "%s: skb has no dev; dropping", __func__);
149 if (key->eth.type == htons(ETH_P_IP)) {
150 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
153 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
154 err = ip_defrag(skb, user);
158 ovs_cb.mru = IPCB(skb)->frag_max_size;
159 } else if (key->eth.type == htons(ETH_P_IPV6)) {
160 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
161 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
162 struct sk_buff *reasm;
164 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
165 reasm = nf_ct_frag6_gather(skb, user);
172 key->ip.proto = ipv6_hdr(reasm)->nexthdr;
173 skb_morph(skb, reasm);
175 ovs_cb.mru = IP6CB(skb)->frag_max_size;
177 return -EPFNOSUPPORT;
178 #endif /* IP frag support */
180 return -EPFNOSUPPORT;
183 key->ip.frag = OVS_FRAG_TYPE_NONE;
186 *OVS_CB(skb) = ovs_cb;
191 static struct nf_conntrack_expect *
192 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
193 u16 proto, const struct sk_buff *skb)
195 struct nf_conntrack_tuple tuple;
197 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
199 return __nf_ct_expect_find(net, zone, &tuple);
202 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
203 static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
204 const struct ovs_conntrack_info *info)
206 enum ip_conntrack_info ctinfo;
209 ct = nf_ct_get(skb, &ctinfo);
212 if (!net_eq(net, read_pnet(&ct->ct_net)))
214 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
220 static int __ovs_ct_lookup(struct net *net, const struct sw_flow_key *key,
221 const struct ovs_conntrack_info *info,
224 /* If we are recirculating packets to match on conntrack fields and
225 * committing with a separate conntrack action, then we don't need to
226 * actually run the packet through conntrack twice unless it's for a
229 if (!skb_nfct_cached(net, skb, info)) {
230 struct nf_conn *tmpl = info->ct;
232 /* Associate skb with specified zone. */
235 nf_conntrack_put(skb->nfct);
236 nf_conntrack_get(&tmpl->ct_general);
237 skb->nfct = &tmpl->ct_general;
238 skb->nfctinfo = IP_CT_NEW;
241 if (nf_conntrack_in(net, info->family, NF_INET_FORWARD,
249 /* Lookup connection and read fields into key. */
250 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
251 const struct ovs_conntrack_info *info,
254 struct nf_conntrack_expect *exp;
256 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
260 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
261 __ovs_ct_update_key(key, state, &info->zone);
265 err = __ovs_ct_lookup(net, key, info, skb);
269 ovs_ct_update_key(skb, key, true);
275 /* Lookup connection and confirm if unconfirmed. */
276 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
277 const struct ovs_conntrack_info *info,
283 state = key->ct.state;
284 if (key->ct.zone == info->zone.id &&
285 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
286 /* Previous lookup has shown that this connection is already
287 * tracked and committed. Skip committing.
292 err = __ovs_ct_lookup(net, key, info, skb);
295 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
298 ovs_ct_update_key(skb, key, true);
303 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
304 struct sw_flow_key *key,
305 const struct ovs_conntrack_info *info)
310 /* The conntrack module expects to be working at L3. */
311 nh_ofs = skb_network_offset(skb);
312 skb_pull(skb, nh_ofs);
314 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
315 err = handle_fragments(net, key, info->zone.id, skb);
320 if (info->flags & OVS_CT_F_COMMIT)
321 err = ovs_ct_commit(net, key, info, skb);
323 err = ovs_ct_lookup(net, key, info, skb);
325 skb_push(skb, nh_ofs);
329 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
330 [OVS_CT_ATTR_FLAGS] = { .minlen = sizeof(u32),
331 .maxlen = sizeof(u32) },
332 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
333 .maxlen = sizeof(u16) },
336 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
342 nla_for_each_nested(a, attr, rem) {
343 int type = nla_type(a);
344 int maxlen = ovs_ct_attr_lens[type].maxlen;
345 int minlen = ovs_ct_attr_lens[type].minlen;
347 if (type > OVS_CT_ATTR_MAX) {
349 "Unknown conntrack attr (type=%d, max=%d)",
350 type, OVS_CT_ATTR_MAX);
353 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
355 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
356 type, nla_len(a), maxlen);
361 case OVS_CT_ATTR_FLAGS:
362 info->flags = nla_get_u32(a);
364 #ifdef CONFIG_NF_CONNTRACK_ZONES
365 case OVS_CT_ATTR_ZONE:
366 info->zone.id = nla_get_u16(a);
370 OVS_NLERR(log, "Unknown conntrack attr (%d)",
377 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
384 bool ovs_ct_verify(enum ovs_key_attr attr)
386 if (attr == OVS_KEY_ATTR_CT_STATE)
388 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
389 attr == OVS_KEY_ATTR_CT_ZONE)
395 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
396 const struct sw_flow_key *key,
397 struct sw_flow_actions **sfa, bool log)
399 struct ovs_conntrack_info ct_info;
403 family = key_to_nfproto(key);
404 if (family == NFPROTO_UNSPEC) {
405 OVS_NLERR(log, "ct family unspecified");
409 memset(&ct_info, 0, sizeof(ct_info));
410 ct_info.family = family;
412 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
413 NF_CT_DEFAULT_ZONE_DIR, 0);
415 err = parse_ct(attr, &ct_info, log);
419 /* Set up template for tracking connections in specific zones. */
420 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
422 OVS_NLERR(log, "Failed to allocate conntrack template");
426 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
427 sizeof(ct_info), log);
431 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
432 nf_conntrack_get(&ct_info.ct->ct_general);
435 nf_conntrack_free(ct_info.ct);
439 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
442 struct nlattr *start;
444 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
448 if (nla_put_u32(skb, OVS_CT_ATTR_FLAGS, ct_info->flags))
450 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
451 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
454 nla_nest_end(skb, start);
459 void ovs_ct_free_action(const struct nlattr *a)
461 struct ovs_conntrack_info *ct_info = nla_data(a);
464 nf_ct_tmpl_free(ct_info->ct);
467 #endif /* CONFIG_NF_CONNTRACK && LINUX > 3.9 */