Skip to content

Commit b57dc7c

Browse files
Paul Blakeydavem330
Paul Blakey
authored andcommitted
net/sched: Introduce action ct
Allow sending a packet to conntrack module for connection tracking. The packet will be marked with conntrack connection's state, and any metadata such as conntrack mark and label. This state metadata can later be matched against with tc classifers, for example with the flower classifier as below. In addition to committing new connections the user can optionally specific a zone to track within, set a mark/label and configure nat with an address range and port range. Usage is as follows: $ tc qdisc add dev ens1f0_0 ingress $ tc qdisc add dev ens1f0_1 ingress $ tc filter add dev ens1f0_0 ingress \ prio 1 chain 0 proto ip \ flower ip_proto tcp ct_state -trk \ action ct zone 2 pipe \ action goto chain 2 $ tc filter add dev ens1f0_0 ingress \ prio 1 chain 2 proto ip \ flower ct_state +trk+new \ action ct zone 2 commit mark 0xbb nat src addr 5.5.5.7 pipe \ action mirred egress redirect dev ens1f0_1 $ tc filter add dev ens1f0_0 ingress \ prio 1 chain 2 proto ip \ flower ct_zone 2 ct_mark 0xbb ct_state +trk+est \ action ct nat pipe \ action mirred egress redirect dev ens1f0_1 $ tc filter add dev ens1f0_1 ingress \ prio 1 chain 0 proto ip \ flower ip_proto tcp ct_state -trk \ action ct zone 2 pipe \ action goto chain 1 $ tc filter add dev ens1f0_1 ingress \ prio 1 chain 1 proto ip \ flower ct_zone 2 ct_mark 0xbb ct_state +trk+est \ action ct nat pipe \ action mirred egress redirect dev ens1f0_0 Signed-off-by: Paul Blakey <[email protected]> Signed-off-by: Marcelo Ricardo Leitner <[email protected]> Signed-off-by: Yossi Kuperman <[email protected]> Acked-by: Jiri Pirko <[email protected]> Changelog: V5->V6: Added CONFIG_NF_DEFRAG_IPV6 in handle fragments ipv6 case V4->V5: Reordered nf_conntrack_put() in tcf_ct_skb_nfct_cached() V3->V4: Added strict_start_type for act_ct policy V2->V3: Fixed david's comments: Removed extra newline after rcu in tcf_ct_params , and indent of break in act_ct.c V1->V2: Fixed parsing of ranges TCA_CT_NAT_IPV6_MAX as 'else' case overwritten ipv4 max Refactored NAT_PORT_MIN_MAX range handling as well Added ipv4/ipv6 defragmentation Removed extra skb pull push of nw offset in exectute nat Refactored tcf_ct_skb_network_trim after pull Removed TCA_ACT_CT define Signed-off-by: David S. Miller <[email protected]>
1 parent f108c88 commit b57dc7c

File tree

8 files changed

+1111
-0
lines changed

8 files changed

+1111
-0
lines changed

include/net/flow_offload.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ enum flow_action_id {
129129
FLOW_ACTION_QUEUE,
130130
FLOW_ACTION_SAMPLE,
131131
FLOW_ACTION_POLICE,
132+
FLOW_ACTION_CT,
132133
};
133134

134135
/* This is mirroring enum pedit_header_type definition for easy mapping between
@@ -178,6 +179,10 @@ struct flow_action_entry {
178179
s64 burst;
179180
u64 rate_bytes_ps;
180181
} police;
182+
struct { /* FLOW_ACTION_CT */
183+
int action;
184+
u16 zone;
185+
} ct;
181186
};
182187
};
183188

include/net/tc_act/tc_ct.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __NET_TC_CT_H
3+
#define __NET_TC_CT_H
4+
5+
#include <net/act_api.h>
6+
#include <uapi/linux/tc_act/tc_ct.h>
7+
8+
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
9+
#include <net/netfilter/nf_nat.h>
10+
#include <net/netfilter/nf_conntrack_labels.h>
11+
12+
struct tcf_ct_params {
13+
struct nf_conn *tmpl;
14+
u16 zone;
15+
16+
u32 mark;
17+
u32 mark_mask;
18+
19+
u32 labels[NF_CT_LABELS_MAX_SIZE / sizeof(u32)];
20+
u32 labels_mask[NF_CT_LABELS_MAX_SIZE / sizeof(u32)];
21+
22+
struct nf_nat_range2 range;
23+
bool ipv4_range;
24+
25+
u16 ct_action;
26+
27+
struct rcu_head rcu;
28+
};
29+
30+
struct tcf_ct {
31+
struct tc_action common;
32+
struct tcf_ct_params __rcu *params;
33+
};
34+
35+
#define to_ct(a) ((struct tcf_ct *)a)
36+
#define to_ct_params(a) ((struct tcf_ct_params *) \
37+
rtnl_dereference((to_ct(a)->params)))
38+
39+
static inline uint16_t tcf_ct_zone(const struct tc_action *a)
40+
{
41+
return to_ct_params(a)->zone;
42+
}
43+
44+
static inline int tcf_ct_action(const struct tc_action *a)
45+
{
46+
return to_ct_params(a)->ct_action;
47+
}
48+
49+
#else
50+
static inline uint16_t tcf_ct_zone(const struct tc_action *a) { return 0; }
51+
static inline int tcf_ct_action(const struct tc_action *a) { return 0; }
52+
#endif /* CONFIG_NF_CONNTRACK */
53+
54+
static inline bool is_tcf_ct(const struct tc_action *a)
55+
{
56+
#if defined(CONFIG_NET_CLS_ACT) && IS_ENABLED(CONFIG_NF_CONNTRACK)
57+
if (a->ops && a->ops->id == TCA_ID_CT)
58+
return true;
59+
#endif
60+
return false;
61+
}
62+
63+
#endif /* __NET_TC_CT_H */

include/uapi/linux/pkt_cls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ enum tca_id {
106106
TCA_ID_SAMPLE = TCA_ACT_SAMPLE,
107107
TCA_ID_CTINFO,
108108
TCA_ID_MPLS,
109+
TCA_ID_CT,
109110
/* other actions go here */
110111
__TCA_ID_MAX = 255
111112
};

include/uapi/linux/tc_act/tc_ct.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
#ifndef __UAPI_TC_CT_H
3+
#define __UAPI_TC_CT_H
4+
5+
#include <linux/types.h>
6+
#include <linux/pkt_cls.h>
7+
8+
enum {
9+
TCA_CT_UNSPEC,
10+
TCA_CT_PARMS,
11+
TCA_CT_TM,
12+
TCA_CT_ACTION, /* u16 */
13+
TCA_CT_ZONE, /* u16 */
14+
TCA_CT_MARK, /* u32 */
15+
TCA_CT_MARK_MASK, /* u32 */
16+
TCA_CT_LABELS, /* u128 */
17+
TCA_CT_LABELS_MASK, /* u128 */
18+
TCA_CT_NAT_IPV4_MIN, /* be32 */
19+
TCA_CT_NAT_IPV4_MAX, /* be32 */
20+
TCA_CT_NAT_IPV6_MIN, /* struct in6_addr */
21+
TCA_CT_NAT_IPV6_MAX, /* struct in6_addr */
22+
TCA_CT_NAT_PORT_MIN, /* be16 */
23+
TCA_CT_NAT_PORT_MAX, /* be16 */
24+
TCA_CT_PAD,
25+
__TCA_CT_MAX
26+
};
27+
28+
#define TCA_CT_MAX (__TCA_CT_MAX - 1)
29+
30+
#define TCA_CT_ACT_COMMIT (1 << 0)
31+
#define TCA_CT_ACT_FORCE (1 << 1)
32+
#define TCA_CT_ACT_CLEAR (1 << 2)
33+
#define TCA_CT_ACT_NAT (1 << 3)
34+
#define TCA_CT_ACT_NAT_SRC (1 << 4)
35+
#define TCA_CT_ACT_NAT_DST (1 << 5)
36+
37+
struct tc_ct {
38+
tc_gen;
39+
};
40+
41+
#endif /* __UAPI_TC_CT_H */

net/sched/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,17 @@ config NET_ACT_TUNNEL_KEY
940940
To compile this code as a module, choose M here: the
941941
module will be called act_tunnel_key.
942942

943+
config NET_ACT_CT
944+
tristate "connection tracking tc action"
945+
depends on NET_CLS_ACT && NF_CONNTRACK
946+
help
947+
Say Y here to allow sending the packets to conntrack module.
948+
949+
If unsure, say N.
950+
951+
To compile this code as a module, choose M here: the
952+
module will be called act_ct.
953+
943954
config NET_IFE_SKBMARK
944955
tristate "Support to encoding decoding skb mark on IFE action"
945956
depends on NET_ACT_IFE

net/sched/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ obj-$(CONFIG_NET_IFE_SKBMARK) += act_meta_mark.o
2929
obj-$(CONFIG_NET_IFE_SKBPRIO) += act_meta_skbprio.o
3030
obj-$(CONFIG_NET_IFE_SKBTCINDEX) += act_meta_skbtcindex.o
3131
obj-$(CONFIG_NET_ACT_TUNNEL_KEY)+= act_tunnel_key.o
32+
obj-$(CONFIG_NET_ACT_CT) += act_ct.o
3233
obj-$(CONFIG_NET_SCH_FIFO) += sch_fifo.o
3334
obj-$(CONFIG_NET_SCH_CBQ) += sch_cbq.o
3435
obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o

0 commit comments

Comments
 (0)