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 IS_ENABLED(CONFIG_NF_CONNTRACK)
19 #include <linux/module.h>
20 #include <linux/openvswitch.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_helper.h>
24 #include <net/netfilter/nf_conntrack_labels.h>
25 #include <net/netfilter/nf_conntrack_zones.h>
26 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
29 #include "conntrack.h"
31 #include "flow_netlink.h"
34 struct ovs_ct_len_tbl
{
39 /* Metadata mark for masked write to conntrack mark */
45 /* Metadata label for masked write to conntrack label. */
47 struct ovs_key_ct_labels value
;
48 struct ovs_key_ct_labels mask
;
51 /* Conntrack action context for execution. */
52 struct ovs_conntrack_info
{
53 struct nf_conntrack_helper
*helper
;
54 struct nf_conntrack_zone zone
;
59 struct md_labels labels
;
62 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
);
64 static u16
key_to_nfproto(const struct sw_flow_key
*key
)
66 switch (ntohs(key
->eth
.type
)) {
72 return NFPROTO_UNSPEC
;
76 /* Map SKB connection state into the values used by flow definition. */
77 static u8
ovs_ct_get_state(enum ip_conntrack_info ctinfo
)
79 u8 ct_state
= OVS_CS_F_TRACKED
;
82 case IP_CT_ESTABLISHED_REPLY
:
83 case IP_CT_RELATED_REPLY
:
85 ct_state
|= OVS_CS_F_REPLY_DIR
;
92 case IP_CT_ESTABLISHED
:
93 case IP_CT_ESTABLISHED_REPLY
:
94 ct_state
|= OVS_CS_F_ESTABLISHED
;
97 case IP_CT_RELATED_REPLY
:
98 ct_state
|= OVS_CS_F_RELATED
;
101 case IP_CT_NEW_REPLY
:
102 ct_state
|= OVS_CS_F_NEW
;
111 static u32
ovs_ct_get_mark(const struct nf_conn
*ct
)
113 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
114 return ct
? ct
->mark
: 0;
120 static void ovs_ct_get_labels(const struct nf_conn
*ct
,
121 struct ovs_key_ct_labels
*labels
)
123 struct nf_conn_labels
*cl
= ct
? nf_ct_labels_find(ct
) : NULL
;
126 size_t len
= cl
->words
* sizeof(long);
128 if (len
> OVS_CT_LABELS_LEN
)
129 len
= OVS_CT_LABELS_LEN
;
130 else if (len
< OVS_CT_LABELS_LEN
)
131 memset(labels
, 0, OVS_CT_LABELS_LEN
);
132 memcpy(labels
, cl
->bits
, len
);
134 memset(labels
, 0, OVS_CT_LABELS_LEN
);
138 static void __ovs_ct_update_key(struct sw_flow_key
*key
, u8 state
,
139 const struct nf_conntrack_zone
*zone
,
140 const struct nf_conn
*ct
)
142 key
->ct
.state
= state
;
143 key
->ct
.zone
= zone
->id
;
144 key
->ct
.mark
= ovs_ct_get_mark(ct
);
145 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
148 /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
149 * previously sent the packet to conntrack via the ct action.
151 static void ovs_ct_update_key(const struct sk_buff
*skb
,
152 const struct ovs_conntrack_info
*info
,
153 struct sw_flow_key
*key
, bool post_ct
)
155 const struct nf_conntrack_zone
*zone
= &nf_ct_zone_dflt
;
156 enum ip_conntrack_info ctinfo
;
160 ct
= nf_ct_get(skb
, &ctinfo
);
162 state
= ovs_ct_get_state(ctinfo
);
163 if (!nf_ct_is_confirmed(ct
))
164 state
|= OVS_CS_F_NEW
;
166 state
|= OVS_CS_F_RELATED
;
167 zone
= nf_ct_zone(ct
);
168 } else if (post_ct
) {
169 state
= OVS_CS_F_TRACKED
| OVS_CS_F_INVALID
;
173 __ovs_ct_update_key(key
, state
, zone
, ct
);
176 void ovs_ct_fill_key(const struct sk_buff
*skb
, struct sw_flow_key
*key
)
178 ovs_ct_update_key(skb
, NULL
, key
, false);
181 int ovs_ct_put_key(const struct sw_flow_key
*key
, struct sk_buff
*skb
)
183 if (nla_put_u32(skb
, OVS_KEY_ATTR_CT_STATE
, key
->ct
.state
))
186 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
187 nla_put_u16(skb
, OVS_KEY_ATTR_CT_ZONE
, key
->ct
.zone
))
190 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
191 nla_put_u32(skb
, OVS_KEY_ATTR_CT_MARK
, key
->ct
.mark
))
194 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
195 nla_put(skb
, OVS_KEY_ATTR_CT_LABELS
, sizeof(key
->ct
.labels
),
202 static int ovs_ct_set_mark(struct sk_buff
*skb
, struct sw_flow_key
*key
,
203 u32 ct_mark
, u32 mask
)
205 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
206 enum ip_conntrack_info ctinfo
;
211 /* The connection could be invalid, in which case set_mark is no-op. */
212 ct
= nf_ct_get(skb
, &ctinfo
);
216 new_mark
= ct_mark
| (ct
->mark
& ~(mask
));
217 if (ct
->mark
!= new_mark
) {
219 nf_conntrack_event_cache(IPCT_MARK
, ct
);
220 key
->ct
.mark
= new_mark
;
229 static int ovs_ct_set_labels(struct sk_buff
*skb
, struct sw_flow_key
*key
,
230 const struct ovs_key_ct_labels
*labels
,
231 const struct ovs_key_ct_labels
*mask
)
233 enum ip_conntrack_info ctinfo
;
234 struct nf_conn_labels
*cl
;
238 /* The connection could be invalid, in which case set_label is no-op.*/
239 ct
= nf_ct_get(skb
, &ctinfo
);
243 cl
= nf_ct_labels_find(ct
);
245 nf_ct_labels_ext_add(ct
);
246 cl
= nf_ct_labels_find(ct
);
248 if (!cl
|| cl
->words
* sizeof(long) < OVS_CT_LABELS_LEN
)
251 err
= nf_connlabels_replace(ct
, (u32
*)labels
, (u32
*)mask
,
252 OVS_CT_LABELS_LEN
/ sizeof(u32
));
256 ovs_ct_get_labels(ct
, &key
->ct
.labels
);
260 /* 'skb' should already be pulled to nh_ofs. */
261 static int ovs_ct_helper(struct sk_buff
*skb
, u16 proto
)
263 const struct nf_conntrack_helper
*helper
;
264 const struct nf_conn_help
*help
;
265 enum ip_conntrack_info ctinfo
;
266 unsigned int protoff
;
269 ct
= nf_ct_get(skb
, &ctinfo
);
270 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
273 help
= nfct_help(ct
);
277 helper
= rcu_dereference(help
->helper
);
283 protoff
= ip_hdrlen(skb
);
286 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
290 ofs
= ipv6_skip_exthdr(skb
, sizeof(struct ipv6hdr
), &nexthdr
,
292 if (ofs
< 0 || (frag_off
& htons(~0x7)) != 0) {
293 pr_debug("proto header not found\n");
300 WARN_ONCE(1, "helper invoked on non-IP family!");
304 return helper
->help(skb
, protoff
, ct
, ctinfo
);
307 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
308 * value if 'skb' is freed.
310 static int handle_fragments(struct net
*net
, struct sw_flow_key
*key
,
311 u16 zone
, struct sk_buff
*skb
)
313 struct ovs_gso_cb ovs_cb
= *OVS_GSO_CB(skb
);
316 OVS_NLERR(true, "%s: skb has no dev; dropping", __func__
);
320 if (key
->eth
.type
== htons(ETH_P_IP
)) {
321 enum ip_defrag_users user
= IP_DEFRAG_CONNTRACK_IN
+ zone
;
324 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
325 err
= ip_defrag(net
, skb
, user
);
329 ovs_cb
.dp_cb
.mru
= IPCB(skb
)->frag_max_size
;
330 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
331 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
332 enum ip6_defrag_users user
= IP6_DEFRAG_CONNTRACK_IN
+ zone
;
333 struct sk_buff
*reasm
;
335 memset(IP6CB(skb
), 0, sizeof(struct inet6_skb_parm
));
336 reasm
= nf_ct_frag6_gather(net
, skb
, user
);
345 /* Don't free 'skb' even though it is one of the original
346 * fragments, as we're going to morph it into the head.
349 nf_ct_frag6_consume_orig(reasm
);
351 key
->ip
.proto
= ipv6_hdr(reasm
)->nexthdr
;
352 skb_morph(skb
, reasm
);
353 skb
->next
= reasm
->next
;
355 ovs_cb
.dp_cb
.mru
= IP6CB(skb
)->frag_max_size
;
356 #endif /* IP frag support */
359 return -EPFNOSUPPORT
;
362 key
->ip
.frag
= OVS_FRAG_TYPE_NONE
;
365 *OVS_GSO_CB(skb
) = ovs_cb
;
370 static struct nf_conntrack_expect
*
371 ovs_ct_expect_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
372 u16 proto
, const struct sk_buff
*skb
)
374 struct nf_conntrack_tuple tuple
;
376 if (!nf_ct_get_tuplepr(skb
, skb_network_offset(skb
), proto
, net
, &tuple
))
378 return __nf_ct_expect_find(net
, zone
, &tuple
);
381 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
382 static bool skb_nfct_cached(const struct net
*net
, const struct sk_buff
*skb
,
383 const struct ovs_conntrack_info
*info
)
385 enum ip_conntrack_info ctinfo
;
388 ct
= nf_ct_get(skb
, &ctinfo
);
391 if (!net_eq(net
, read_pnet(&ct
->ct_net
)))
393 if (!nf_ct_zone_equal_any(info
->ct
, nf_ct_zone(ct
)))
396 struct nf_conn_help
*help
;
398 help
= nf_ct_ext_find(ct
, NF_CT_EXT_HELPER
);
399 if (help
&& rcu_access_pointer(help
->helper
) != info
->helper
)
406 static int __ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
407 const struct ovs_conntrack_info
*info
,
410 /* If we are recirculating packets to match on conntrack fields and
411 * committing with a separate conntrack action, then we don't need to
412 * actually run the packet through conntrack twice unless it's for a
415 if (!skb_nfct_cached(net
, skb
, info
)) {
416 struct nf_conn
*tmpl
= info
->ct
;
418 /* Associate skb with specified zone. */
421 nf_conntrack_put(skb
->nfct
);
422 nf_conntrack_get(&tmpl
->ct_general
);
423 skb
->nfct
= &tmpl
->ct_general
;
424 skb
->nfctinfo
= IP_CT_NEW
;
427 if (nf_conntrack_in(net
, info
->family
, NF_INET_FORWARD
,
431 if (ovs_ct_helper(skb
, info
->family
) != NF_ACCEPT
) {
432 WARN_ONCE(1, "helper rejected packet");
437 ovs_ct_update_key(skb
, info
, key
, true);
442 /* Lookup connection and read fields into key. */
443 static int ovs_ct_lookup(struct net
*net
, struct sw_flow_key
*key
,
444 const struct ovs_conntrack_info
*info
,
447 struct nf_conntrack_expect
*exp
;
449 exp
= ovs_ct_expect_find(net
, &info
->zone
, info
->family
, skb
);
453 state
= OVS_CS_F_TRACKED
| OVS_CS_F_NEW
| OVS_CS_F_RELATED
;
454 __ovs_ct_update_key(key
, state
, &info
->zone
, exp
->master
);
458 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
466 /* Lookup connection and confirm if unconfirmed. */
467 static int ovs_ct_commit(struct net
*net
, struct sw_flow_key
*key
,
468 const struct ovs_conntrack_info
*info
,
474 state
= key
->ct
.state
;
475 if (key
->ct
.zone
== info
->zone
.id
&&
476 ((state
& OVS_CS_F_TRACKED
) && !(state
& OVS_CS_F_NEW
))) {
477 /* Previous lookup has shown that this connection is already
478 * tracked and committed. Skip committing.
483 err
= __ovs_ct_lookup(net
, key
, info
, skb
);
486 if (nf_conntrack_confirm(skb
) != NF_ACCEPT
)
492 static bool labels_nonzero(const struct ovs_key_ct_labels
*labels
)
496 for (i
= 0; i
< sizeof(*labels
); i
++)
497 if (labels
->ct_labels
[i
])
503 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
504 * value if 'skb' is freed.
506 int ovs_ct_execute(struct net
*net
, struct sk_buff
*skb
,
507 struct sw_flow_key
*key
,
508 const struct ovs_conntrack_info
*info
)
513 /* The conntrack module expects to be working at L3. */
514 nh_ofs
= skb_network_offset(skb
);
515 skb_pull(skb
, nh_ofs
);
517 if (key
->ip
.frag
!= OVS_FRAG_TYPE_NONE
) {
518 err
= handle_fragments(net
, key
, info
->zone
.id
, skb
);
524 err
= ovs_ct_commit(net
, key
, info
, skb
);
526 err
= ovs_ct_lookup(net
, key
, info
, skb
);
530 if (info
->mark
.mask
) {
531 err
= ovs_ct_set_mark(skb
, key
, info
->mark
.value
,
536 if (labels_nonzero(&info
->labels
.mask
))
537 err
= ovs_ct_set_labels(skb
, key
, &info
->labels
.value
,
540 skb_push(skb
, nh_ofs
);
546 static int ovs_ct_add_helper(struct ovs_conntrack_info
*info
, const char *name
,
547 const struct sw_flow_key
*key
, bool log
)
549 struct nf_conntrack_helper
*helper
;
550 struct nf_conn_help
*help
;
552 helper
= nf_conntrack_helper_try_module_get(name
, info
->family
,
555 OVS_NLERR(log
, "Unknown helper \"%s\"", name
);
559 help
= nf_ct_helper_ext_add(info
->ct
, helper
, GFP_KERNEL
);
561 module_put(helper
->me
);
565 rcu_assign_pointer(help
->helper
, helper
);
566 info
->helper
= helper
;
570 static const struct ovs_ct_len_tbl ovs_ct_attr_lens
[OVS_CT_ATTR_MAX
+ 1] = {
571 [OVS_CT_ATTR_COMMIT
] = { .minlen
= 0, .maxlen
= 0 },
572 [OVS_CT_ATTR_ZONE
] = { .minlen
= sizeof(u16
),
573 .maxlen
= sizeof(u16
) },
574 [OVS_CT_ATTR_MARK
] = { .minlen
= sizeof(struct md_mark
),
575 .maxlen
= sizeof(struct md_mark
) },
576 [OVS_CT_ATTR_LABELS
] = { .minlen
= sizeof(struct md_labels
),
577 .maxlen
= sizeof(struct md_labels
) },
578 [OVS_CT_ATTR_HELPER
] = { .minlen
= 1,
579 .maxlen
= NF_CT_HELPER_NAME_LEN
}
582 static int parse_ct(const struct nlattr
*attr
, struct ovs_conntrack_info
*info
,
583 const char **helper
, bool log
)
588 nla_for_each_nested(a
, attr
, rem
) {
589 int type
= nla_type(a
);
590 int maxlen
= ovs_ct_attr_lens
[type
].maxlen
;
591 int minlen
= ovs_ct_attr_lens
[type
].minlen
;
593 if (type
> OVS_CT_ATTR_MAX
) {
595 "Unknown conntrack attr (type=%d, max=%d)",
596 type
, OVS_CT_ATTR_MAX
);
599 if (nla_len(a
) < minlen
|| nla_len(a
) > maxlen
) {
601 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
602 type
, nla_len(a
), maxlen
);
607 case OVS_CT_ATTR_COMMIT
:
610 #ifdef CONFIG_NF_CONNTRACK_ZONES
611 case OVS_CT_ATTR_ZONE
:
612 info
->zone
.id
= nla_get_u16(a
);
615 #ifdef CONFIG_NF_CONNTRACK_MARK
616 case OVS_CT_ATTR_MARK
: {
617 struct md_mark
*mark
= nla_data(a
);
620 OVS_NLERR(log
, "ct_mark mask cannot be 0");
627 #ifdef CONFIG_NF_CONNTRACK_LABELS
628 case OVS_CT_ATTR_LABELS
: {
629 struct md_labels
*labels
= nla_data(a
);
631 if (!labels_nonzero(&labels
->mask
)) {
632 OVS_NLERR(log
, "ct_labels mask cannot be 0");
635 info
->labels
= *labels
;
639 case OVS_CT_ATTR_HELPER
:
640 *helper
= nla_data(a
);
641 if (!memchr(*helper
, '\0', nla_len(a
))) {
642 OVS_NLERR(log
, "Invalid conntrack helper");
647 OVS_NLERR(log
, "Unknown conntrack attr (%d)",
654 OVS_NLERR(log
, "Conntrack attr has %d unknown bytes", rem
);
661 bool ovs_ct_verify(struct net
*net
, enum ovs_key_attr attr
)
663 if (attr
== OVS_KEY_ATTR_CT_STATE
)
665 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
666 attr
== OVS_KEY_ATTR_CT_ZONE
)
668 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) &&
669 attr
== OVS_KEY_ATTR_CT_MARK
)
671 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
672 attr
== OVS_KEY_ATTR_CT_LABELS
) {
673 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
675 return ovs_net
->xt_label
;
681 int ovs_ct_copy_action(struct net
*net
, const struct nlattr
*attr
,
682 const struct sw_flow_key
*key
,
683 struct sw_flow_actions
**sfa
, bool log
)
685 struct ovs_conntrack_info ct_info
;
686 const char *helper
= NULL
;
690 family
= key_to_nfproto(key
);
691 if (family
== NFPROTO_UNSPEC
) {
692 OVS_NLERR(log
, "ct family unspecified");
696 memset(&ct_info
, 0, sizeof(ct_info
));
697 ct_info
.family
= family
;
699 nf_ct_zone_init(&ct_info
.zone
, NF_CT_DEFAULT_ZONE_ID
,
700 NF_CT_DEFAULT_ZONE_DIR
, 0);
702 err
= parse_ct(attr
, &ct_info
, &helper
, log
);
706 /* Set up template for tracking connections in specific zones. */
707 ct_info
.ct
= nf_ct_tmpl_alloc(net
, &ct_info
.zone
, GFP_KERNEL
);
709 OVS_NLERR(log
, "Failed to allocate conntrack template");
713 err
= ovs_ct_add_helper(&ct_info
, helper
, key
, log
);
718 err
= ovs_nla_add_action(sfa
, OVS_ACTION_ATTR_CT
, &ct_info
,
719 sizeof(ct_info
), log
);
723 __set_bit(IPS_CONFIRMED_BIT
, &ct_info
.ct
->status
);
724 nf_conntrack_get(&ct_info
.ct
->ct_general
);
727 __ovs_ct_free_action(&ct_info
);
731 int ovs_ct_action_to_attr(const struct ovs_conntrack_info
*ct_info
,
734 struct nlattr
*start
;
736 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_CT
);
740 if (ct_info
->commit
&& nla_put_flag(skb
, OVS_CT_ATTR_COMMIT
))
742 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES
) &&
743 nla_put_u16(skb
, OVS_CT_ATTR_ZONE
, ct_info
->zone
.id
))
745 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK
) && ct_info
->mark
.mask
&&
746 nla_put(skb
, OVS_CT_ATTR_MARK
, sizeof(ct_info
->mark
),
749 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS
) &&
750 labels_nonzero(&ct_info
->labels
.mask
) &&
751 nla_put(skb
, OVS_CT_ATTR_LABELS
, sizeof(ct_info
->labels
),
754 if (ct_info
->helper
) {
755 if (nla_put_string(skb
, OVS_CT_ATTR_HELPER
,
756 ct_info
->helper
->name
))
760 nla_nest_end(skb
, start
);
765 void ovs_ct_free_action(const struct nlattr
*a
)
767 struct ovs_conntrack_info
*ct_info
= nla_data(a
);
769 __ovs_ct_free_action(ct_info
);
772 static void __ovs_ct_free_action(struct ovs_conntrack_info
*ct_info
)
775 module_put(ct_info
->helper
->me
);
777 nf_ct_tmpl_free(ct_info
->ct
);
780 void ovs_ct_init(struct net
*net
)
782 unsigned int n_bits
= sizeof(struct ovs_key_ct_labels
) * BITS_PER_BYTE
;
783 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
785 if (nf_connlabels_get(net
, n_bits
)) {
786 ovs_net
->xt_label
= false;
787 OVS_NLERR(true, "Failed to set connlabel length");
789 ovs_net
->xt_label
= true;
793 void ovs_ct_exit(struct net
*net
)
795 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
797 if (ovs_net
->xt_label
)
798 nf_connlabels_put(net
);
801 #endif /* CONFIG_NF_CONNTRACK */