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 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_labels(const struct nf_conn
*ct
,
113 struct ovs_key_ct_labels
*labels
)
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_LABELS_LEN
)
121 len
= OVS_CT_LABELS_LEN
;
122 else if (len
< OVS_CT_LABELS_LEN
)
123 memset(labels
, 0, OVS_CT_LABELS_LEN
);
124 memcpy(labels
, cl
->bits
, len
);
126 memset(labels
, 0, OVS_CT_LABELS_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_labels(ct
, &key
->ct
.labels
);
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
);
154 if (!nf_ct_is_confirmed(ct
))
155 state
|= OVS_CS_F_NEW
;
157 state
|= OVS_CS_F_RELATED
;
158 zone
= nf_ct_zone(ct
);
159 } else if (post_ct
) {
160 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
162 __ovs_ct_update_key(key
, state
, zone
, ct
);
165 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
167 ovs_ct_update_key(skb
, key
, false);
170 int ovs_ct_put_key(const struct sw_flow_key
*key
, struct sk_buff
*skb
)
172 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, key
->ct
.state
))
175 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
176 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, key
->ct
.zone
))
179 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
180 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, key
->ct
.mark
))
183 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
184 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(key
->ct
.labels
),
191 static int ovs_ct_set_mark(struct sk_buff
*skb
, struct sw_flow_key
*key
,
192 u32 ct_mark
, u32 mask
)
194 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
195 enum ip_conntrack_info ctinfo
;
200 /* The connection could be invalid, in which case set_mark is no-op. */
201 ct
= nf_ct_get(skb
, &ctinfo
);
205 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
206 if (ct
->mark
!= new_mark
) {
208 nf_conntrack_event_cache(IPCT_MARK
, ct
);
209 key
->ct
.mark
= new_mark
;
218 static int ovs_ct_set_labels(struct sk_buff
*skb
, struct sw_flow_key
*key
,
219 const struct ovs_key_ct_labels
*labels
,
220 const struct ovs_key_ct_labels
*mask
)
222 enum ip_conntrack_info ctinfo
;
223 struct nf_conn_labels
*cl
;
227 /* The connection could be invalid, in which case set_label is no-op.*/
228 ct
= nf_ct_get(skb
, &ctinfo
);
232 cl
= nf_ct_labels_find(ct
);
234 nf_ct_labels_ext_add(ct
);
235 cl
= nf_ct_labels_find(ct
);
237 if (!cl
|| cl
->words
* sizeof(long) < OVS_CT_LABELS_LEN
)
240 err
= nf_connlabels_replace(ct
, (u32
*)labels
, (u32
*)mask
,
241 OVS_CT_LABELS_LEN
/ sizeof(u32
));
245 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
249 /* 'skb' should already be pulled to nh_ofs. */
250 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
252 const struct nf_conntrack_helper
*helper
;
253 const struct nf_conn_help
*help
;
254 enum ip_conntrack_info ctinfo
;
255 unsigned int protoff
;
258 ct
= nf_ct_get(skb
, &ctinfo
);
259 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
262 help
= nfct_help(ct
);
266 helper
= rcu_dereference(help
->helper
);
272 protoff
= ip_hdrlen(skb
);
275 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
279 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
281 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
282 pr_debug("proto header not found\n");
289 WARN_ONCE(1, "helper invoked on non-IP family!");
293 return helper
->help(skb
, protoff
, ct
, ctinfo
);
296 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
297 * value if 'skb' is freed.
299 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
300 u16 zone
, struct sk_buff
*skb
)
302 struct ovs_skb_cb ovs_cb
= *OVS_CB(skb
);
304 if (key
->eth
.type
== htons(ETH_P_IP
)) {
305 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
308 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
309 err
= ip_defrag(net
, skb
, user
);
313 ovs_cb
.mru
= IPCB(skb
)->frag_max_size
;
314 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
315 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
316 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
317 struct sk_buff
*reasm
;
319 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
320 reasm
= nf_ct_frag6_gather(net
, skb
, user
);
329 /* Don't free 'skb' even though it is one of the original
330 * fragments, as we're going to morph it into the head.
333 nf_ct_frag6_consume_orig(reasm
);
335 key
->ip
.proto
= ipv6_hdr(reasm
)->nexthdr
;
336 skb_morph(skb
, reasm
);
337 skb
->next
= reasm
->next
;
339 ovs_cb
.mru
= IP6CB(skb
)->frag_max_size
;
343 return -EPFNOSUPPORT
;
346 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
349 *OVS_CB(skb
) = ovs_cb
;
354 static struct nf_conntrack_expect
*
355 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
356 u16 proto
, const struct sk_buff
*skb
)
358 struct nf_conntrack_tuple tuple
;
360 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
362 return __nf_ct_expect_find(net
, zone
, &tuple
);
365 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
366 static bool skb_nfct_cached(const struct net
*net
, const struct sk_buff
*skb
,
367 const struct ovs_conntrack_info
*info
)
369 enum ip_conntrack_info ctinfo
;
372 ct
= nf_ct_get(skb
, &ctinfo
);
375 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
377 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
380 struct nf_conn_help
*help
;
382 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
383 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
390 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
391 const struct ovs_conntrack_info
*info
,
394 /* If we are recirculating packets to match on conntrack fields and
395 * committing with a separate conntrack action, then we don't need to
396 * actually run the packet through conntrack twice unless it's for a
399 if (!skb_nfct_cached(net
, skb
, info
)) {
400 struct nf_conn
*tmpl
= info
->ct
;
402 /* Associate skb with specified zone. */
405 nf_conntrack_put(skb
->nfct
);
406 nf_conntrack_get(&tmpl
->ct_general
);
407 skb
->nfct
= &tmpl
->ct_general
;
408 skb
->nfctinfo
= IP_CT_NEW
;
411 if (nf_conntrack_in(net
, info
->family
, NF_INET_PRE_ROUTING
,
415 if (ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
416 WARN_ONCE(1, "helper rejected packet");
421 ovs_ct_update_key(skb
, key
, true);
426 /* Lookup connection and read fields into key. */
427 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
428 const struct ovs_conntrack_info
*info
,
431 struct nf_conntrack_expect
*exp
;
433 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
437 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
438 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
442 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
450 /* Lookup connection and confirm if unconfirmed. */
451 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
452 const struct ovs_conntrack_info
*info
,
458 state
= key
->ct
.state
;
459 if (key
->ct
.zone
== info
->zone
.id
&&
460 ((state
& OVS_CS_F_TRACKED
) && !(state
& OVS_CS_F_NEW
))) {
461 /* Previous lookup has shown that this connection is already
462 * tracked and committed. Skip committing.
467 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
470 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
476 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
480 for (i
= 0; i
< sizeof(*labels
); i
++)
481 if (labels
->ct_labels
[i
])
487 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
488 * value if 'skb' is freed.
490 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
491 struct sw_flow_key
*key
,
492 const struct ovs_conntrack_info
*info
)
497 /* The conntrack module expects to be working at L3. */
498 nh_ofs
= skb_network_offset(skb
);
499 skb_pull(skb
, nh_ofs
);
501 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
502 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
508 err
= ovs_ct_commit(net
, key
, info
, skb
);
510 err
= ovs_ct_lookup(net
, key
, info
, skb
);
514 if (info
->mark
.mask
) {
515 err
= ovs_ct_set_mark(skb
, key
, info
->mark
.value
,
520 if (labels_nonzero(&info
->labels
.mask
))
521 err
= ovs_ct_set_labels(skb
, key
, &info
->labels
.value
,
524 skb_push(skb
, nh_ofs
);
530 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
531 const struct sw_flow_key
*key
, bool log
)
533 struct nf_conntrack_helper
*helper
;
534 struct nf_conn_help
*help
;
536 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
539 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
543 help
= nf_ct_helper_ext_add(info
->ct
, helper
, GFP_KERNEL
);
545 module_put(helper
->me
);
549 rcu_assign_pointer(help
->helper
, helper
);
550 info
->helper
= helper
;
554 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
555 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
556 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
557 .maxlen
= sizeof(u16
) },
558 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
559 .maxlen
= sizeof(struct md_mark
) },
560 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
561 .maxlen
= sizeof(struct md_labels
) },
562 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
563 .maxlen
= NF_CT_HELPER_NAME_LEN
}
566 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
567 const char **helper
, bool log
)
572 nla_for_each_nested(a
, attr
, rem
) {
573 int type
= nla_type(a
);
574 int maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
575 int minlen
= ovs_ct_attr_lens
[type
].minlen
;
577 if (type
> OVS_CT_ATTR_MAX
) {
579 "Unknown conntrack attr (type=%d, max=%d)",
580 type
, OVS_CT_ATTR_MAX
);
583 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
585 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
586 type
, nla_len(a
), maxlen
);
591 case OVS_CT_ATTR_COMMIT
:
594 #ifdef CONFIG_NF_CONNTRACK_ZONES
595 case OVS_CT_ATTR_ZONE
:
596 info
->zone
.id
= nla_get_u16(a
);
599 #ifdef CONFIG_NF_CONNTRACK_MARK
600 case OVS_CT_ATTR_MARK
: {
601 struct md_mark
*mark
= nla_data(a
);
604 OVS_NLERR(log
, "ct_mark mask cannot be 0");
611 #ifdef CONFIG_NF_CONNTRACK_LABELS
612 case OVS_CT_ATTR_LABELS
: {
613 struct md_labels
*labels
= nla_data(a
);
615 if (!labels_nonzero(&labels
->mask
)) {
616 OVS_NLERR(log
, "ct_labels mask cannot be 0");
619 info
->labels
= *labels
;
623 case OVS_CT_ATTR_HELPER
:
624 *helper
= nla_data(a
);
625 if (!memchr(*helper
, '\0', nla_len(a
))) {
626 OVS_NLERR(log
, "Invalid conntrack helper");
631 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
638 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
645 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
647 if (attr
== OVS_KEY_ATTR_CT_STATE
)
649 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
650 attr
== OVS_KEY_ATTR_CT_ZONE
)
652 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
653 attr
== OVS_KEY_ATTR_CT_MARK
)
655 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
656 attr
== OVS_KEY_ATTR_CT_LABELS
) {
657 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
659 return ovs_net
->xt_label
;
665 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
666 const struct sw_flow_key
*key
,
667 struct sw_flow_actions
**sfa
, bool log
)
669 struct ovs_conntrack_info ct_info
;
670 const char *helper
= NULL
;
674 family
= key_to_nfproto(key
);
675 if (family
== NFPROTO_UNSPEC
) {
676 OVS_NLERR(log
, "ct family unspecified");
680 memset(&ct_info
, 0, sizeof(ct_info
));
681 ct_info
.family
= family
;
683 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
684 NF_CT_DEFAULT_ZONE_DIR
, 0);
686 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
690 /* Set up template for tracking connections in specific zones. */
691 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
693 OVS_NLERR(log
, "Failed to allocate conntrack template");
697 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
702 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
703 sizeof(ct_info
), log
);
707 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
708 nf_conntrack_get(&ct_info
.ct
->ct_general
);
711 nf_conntrack_free(ct_info
.ct
);
715 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
718 struct nlattr
*start
;
720 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_CT
);
724 if (ct_info
->commit
&& nla_put_flag(skb
, OVS_CT_ATTR_COMMIT
))
726 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
727 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
729 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
730 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
733 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
734 labels_nonzero(&ct_info
->labels
.mask
) &&
735 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
738 if (ct_info
->helper
) {
739 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
740 ct_info
->helper
->name
))
744 nla_nest_end(skb
, start
);
749 void ovs_ct_free_action(const struct nlattr
*a
)
751 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
754 module_put(ct_info
->helper
->me
);
756 nf_ct_put(ct_info
->ct
);
759 void ovs_ct_init(struct net
*net
)
761 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
762 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
764 if (nf_connlabels_get(net
, n_bits
)) {
765 ovs_net
->xt_label
= false;
766 OVS_NLERR(true, "Failed to set connlabel length");
768 ovs_net
->xt_label
= true;
772 void ovs_ct_exit(struct net
*net
)
774 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
776 if (ovs_net
->xt_label
)
777 nf_connlabels_put(net
);