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_zones.h>
19 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
22 #include "conntrack.h"
24 #include "flow_netlink.h"
26 struct ovs_ct_len_tbl
{
31 /* Conntrack action context for execution. */
32 struct ovs_conntrack_info
{
33 struct nf_conntrack_zone zone
;
39 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
41 switch (ntohs(key
->eth
.type
)) {
47 return NFPROTO_UNSPEC
;
51 /* Map SKB connection state into the values used by flow definition. */
52 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
54 u8 ct_state
= OVS_CS_F_TRACKED
;
57 case IP_CT_ESTABLISHED_REPLY
:
58 case IP_CT_RELATED_REPLY
:
60 ct_state
|= OVS_CS_F_REPLY_DIR
;
67 case IP_CT_ESTABLISHED
:
68 case IP_CT_ESTABLISHED_REPLY
:
69 ct_state
|= OVS_CS_F_ESTABLISHED
;
72 case IP_CT_RELATED_REPLY
:
73 ct_state
|= OVS_CS_F_RELATED
;
77 ct_state
|= OVS_CS_F_NEW
;
86 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
87 const struct nf_conntrack_zone
*zone
)
89 key
->ct
.state
= state
;
90 key
->ct
.zone
= zone
->id
;
93 /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
94 * previously sent the packet to conntrack via the ct action.
96 static void ovs_ct_update_key(const struct sk_buff
*skb
,
97 struct sw_flow_key
*key
, bool post_ct
)
99 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
100 enum ip_conntrack_info ctinfo
;
104 ct
= nf_ct_get(skb
, &ctinfo
);
106 state
= ovs_ct_get_state(ctinfo
);
108 state
|= OVS_CS_F_RELATED
;
109 zone
= nf_ct_zone(ct
);
110 } else if (post_ct
) {
111 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
113 __ovs_ct_update_key(key
, state
, zone
);
116 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
118 ovs_ct_update_key(skb
, key
, false);
121 int ovs_ct_put_key(const struct sw_flow_key
*key
, struct sk_buff
*skb
)
123 if (nla_put_u8(skb
, OVS_KEY_ATTR_CT_STATE
, key
->ct
.state
))
126 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
127 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, key
->ct
.zone
))
133 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
134 u16 zone
, struct sk_buff
*skb
)
136 struct ovs_skb_cb ovs_cb
= *OVS_CB(skb
);
138 if (key
->eth
.type
== htons(ETH_P_IP
)) {
139 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
142 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
143 err
= ip_defrag(skb
, user
);
147 ovs_cb
.mru
= IPCB(skb
)->frag_max_size
;
148 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
149 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
150 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
151 struct sk_buff
*reasm
;
153 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
154 reasm
= nf_ct_frag6_gather(skb
, user
);
161 key
->ip
.proto
= ipv6_hdr(reasm
)->nexthdr
;
162 skb_morph(skb
, reasm
);
164 ovs_cb
.mru
= IP6CB(skb
)->frag_max_size
;
166 return -EPFNOSUPPORT
;
169 return -EPFNOSUPPORT
;
172 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
175 *OVS_CB(skb
) = ovs_cb
;
180 static struct nf_conntrack_expect
*
181 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
182 u16 proto
, const struct sk_buff
*skb
)
184 struct nf_conntrack_tuple tuple
;
186 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, &tuple
))
188 return __nf_ct_expect_find(net
, zone
, &tuple
);
191 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
192 static bool skb_nfct_cached(const struct net
*net
, const struct sk_buff
*skb
,
193 const struct ovs_conntrack_info
*info
)
195 enum ip_conntrack_info ctinfo
;
198 ct
= nf_ct_get(skb
, &ctinfo
);
201 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
203 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
209 static int __ovs_ct_lookup(struct net
*net
, const struct sw_flow_key
*key
,
210 const struct ovs_conntrack_info
*info
,
213 /* If we are recirculating packets to match on conntrack fields and
214 * committing with a separate conntrack action, then we don't need to
215 * actually run the packet through conntrack twice unless it's for a
218 if (!skb_nfct_cached(net
, skb
, info
)) {
219 struct nf_conn
*tmpl
= info
->ct
;
221 /* Associate skb with specified zone. */
224 nf_conntrack_put(skb
->nfct
);
225 nf_conntrack_get(&tmpl
->ct_general
);
226 skb
->nfct
= &tmpl
->ct_general
;
227 skb
->nfctinfo
= IP_CT_NEW
;
230 if (nf_conntrack_in(net
, info
->family
, NF_INET_PRE_ROUTING
,
238 /* Lookup connection and read fields into key. */
239 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
240 const struct ovs_conntrack_info
*info
,
243 struct nf_conntrack_expect
*exp
;
245 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
249 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
250 __ovs_ct_update_key(key
, state
, &info
->zone
);
254 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
258 ovs_ct_update_key(skb
, key
, true);
264 /* Lookup connection and confirm if unconfirmed. */
265 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
266 const struct ovs_conntrack_info
*info
,
272 state
= key
->ct
.state
;
273 if (key
->ct
.zone
== info
->zone
.id
&&
274 ((state
& OVS_CS_F_TRACKED
) && !(state
& OVS_CS_F_NEW
))) {
275 /* Previous lookup has shown that this connection is already
276 * tracked and committed. Skip committing.
281 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
284 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
287 ovs_ct_update_key(skb
, key
, true);
292 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
293 struct sw_flow_key
*key
,
294 const struct ovs_conntrack_info
*info
)
299 /* The conntrack module expects to be working at L3. */
300 nh_ofs
= skb_network_offset(skb
);
301 skb_pull(skb
, nh_ofs
);
303 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
304 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
309 if (info
->flags
& OVS_CT_F_COMMIT
)
310 err
= ovs_ct_commit(net
, key
, info
, skb
);
312 err
= ovs_ct_lookup(net
, key
, info
, skb
);
314 skb_push(skb
, nh_ofs
);
318 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
319 [OVS_CT_ATTR_FLAGS
] = { .minlen
= sizeof(u32
),
320 .maxlen
= sizeof(u32
) },
321 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
322 .maxlen
= sizeof(u16
) },
325 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
331 nla_for_each_nested(a
, attr
, rem
) {
332 int type
= nla_type(a
);
333 int maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
334 int minlen
= ovs_ct_attr_lens
[type
].minlen
;
336 if (type
> OVS_CT_ATTR_MAX
) {
338 "Unknown conntrack attr (type=%d, max=%d)",
339 type
, OVS_CT_ATTR_MAX
);
342 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
344 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
345 type
, nla_len(a
), maxlen
);
350 case OVS_CT_ATTR_FLAGS
:
351 info
->flags
= nla_get_u32(a
);
353 #ifdef CONFIG_NF_CONNTRACK_ZONES
354 case OVS_CT_ATTR_ZONE
:
355 info
->zone
.id
= nla_get_u16(a
);
359 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
366 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
373 bool ovs_ct_verify(enum ovs_key_attr attr
)
375 if (attr
== OVS_KEY_ATTR_CT_STATE
)
377 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
378 attr
== OVS_KEY_ATTR_CT_ZONE
)
384 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
385 const struct sw_flow_key
*key
,
386 struct sw_flow_actions
**sfa
, bool log
)
388 struct ovs_conntrack_info ct_info
;
392 family
= key_to_nfproto(key
);
393 if (family
== NFPROTO_UNSPEC
) {
394 OVS_NLERR(log
, "ct family unspecified");
398 memset(&ct_info
, 0, sizeof(ct_info
));
399 ct_info
.family
= family
;
401 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
402 NF_CT_DEFAULT_ZONE_DIR
, 0);
404 err
= parse_ct(attr
, &ct_info
, log
);
408 /* Set up template for tracking connections in specific zones. */
409 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
411 OVS_NLERR(log
, "Failed to allocate conntrack template");
415 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
416 sizeof(ct_info
), log
);
420 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
421 nf_conntrack_get(&ct_info
.ct
->ct_general
);
424 nf_conntrack_free(ct_info
.ct
);
428 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
431 struct nlattr
*start
;
433 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_CT
);
437 if (nla_put_u32(skb
, OVS_CT_ATTR_FLAGS
, ct_info
->flags
))
439 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
440 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
443 nla_nest_end(skb
, start
);
448 void ovs_ct_free_action(const struct nlattr
*a
)
450 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
453 nf_ct_put(ct_info
->ct
);