1 From 147b0d133b53635db0cc572294840652c9c7b662 Mon Sep 17 00:00:00 2001
2 From: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
3 Date: Wed, 13 Mar 2019 20:54:49 +0000
4 Subject: [PATCH] net: sched: Backport Introduce act_ctinfo action
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 ctinfo is a new tc filter action module. It is designed to restore
10 information contained in firewall conntrack marks to other packet fields
11 and is typically used on packet ingress paths. At present it has two
12 independent sub-functions or operating modes, DSCP restoration mode &
13 skb mark restoration mode.
15 The DSCP restore mode:
17 This mode copies DSCP values that have been placed in the firewall
18 conntrack mark back into the IPv4/v6 diffserv fields of relevant
21 The DSCP restoration is intended for use and has been found useful for
22 restoring ingress classifications based on egress classifications across
23 links that bleach or otherwise change DSCP, typically home ISP Internet
24 links. Restoring DSCP on ingress on the WAN link allows qdiscs such as
25 but by no means limited to CAKE to shape inbound packets according to
26 policies that are easier to set & mark on egress.
28 Ingress classification is traditionally a challenging task since
29 iptables rules haven't yet run and tc filter/eBPF programs are pre-NAT
30 lookups, hence are unable to see internal IPv4 addresses as used on the
31 typical home masquerading gateway. Thus marking the connection in some
32 manner on egress for later restoration of classification on ingress is
35 Parameters related to DSCP restore mode:
37 dscpmask - a 32 bit mask of 6 contiguous bits and indicate bits of the
38 conntrack mark field contain the DSCP value to be restored.
40 statemask - a 32 bit mask of (usually) 1 bit length, outside the area
41 specified by dscpmask. This represents a conditional operation flag
42 whereby the DSCP is only restored if the flag is set. This is useful to
43 implement a 'one shot' iptables based classification where the
44 'complicated' iptables rules are only run once to classify the
45 connection on initial (egress) packet and subsequent packets are all
46 marked/restored with the same DSCP. A mask of zero disables the
47 conditional behaviour ie. the conntrack mark DSCP bits are always
48 restored to the ip diffserv field (assuming the conntrack entry is found
49 & the skb is an ipv4/ipv6 type)
51 e.g. dscpmask 0xfc000000 statemask 0x01000000
53 |----0xFC----conntrack mark----000000---|
54 | Bits 31-26 | bit 25 | bit24 |~~~ Bit 0|
55 | DSCP | unused | flag |unused |
56 |-----------------------0x01---000000---|
65 The skb mark restore mode (cpmark):
67 This mode copies the firewall conntrack mark to the skb's mark field.
68 It is completely the functional equivalent of the existing act_connmark
69 action with the additional feature of being able to apply a mask to the
72 Parameters related to skb mark restore mode:
74 mask - a 32 bit mask applied to the firewall conntrack mark to mask out
75 bits unwanted for restoration. This can be useful where the conntrack
76 mark is being used for different purposes by different applications. If
77 not specified and by default the whole mark field is copied (i.e.
78 default mask of 0xffffffff)
80 e.g. mask 0x00ffffff to mask out the top 8 bits being used by the
81 aforementioned DSCP restore mode.
83 |----0x00----conntrack mark----ffffff---|
85 | DSCP & flag| some value here |
86 |---------------------------------------|
90 |------------skb mark-------------------|
93 |---------------------------------------|
99 control - action related control (reclassify | pipe | drop | continue |
100 ok | goto chain <CHAIN_INDEX>)
102 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
103 Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
104 Acked-by: Cong Wang <xiyou.wangcong@gmail.com>
105 Signed-off-by: David S. Miller <davem@davemloft.net>
108 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
110 include/net/tc_act/tc_ctinfo.h | 33 +++
111 include/uapi/linux/pkt_cls.h | 3 +-
112 include/uapi/linux/tc_act/tc_ctinfo.h | 29 ++
113 net/sched/Kconfig | 13 +
114 net/sched/Makefile | 1 +
115 net/sched/act_ctinfo.c | 394 ++++++++++++++++++++++++++
116 6 files changed, 472 insertions(+), 1 deletion(-)
117 create mode 100644 include/net/tc_act/tc_ctinfo.h
118 create mode 100644 include/uapi/linux/tc_act/tc_ctinfo.h
119 create mode 100644 net/sched/act_ctinfo.c
122 +++ b/include/net/tc_act/tc_ctinfo.h
124 +/* SPDX-License-Identifier: GPL-2.0 */
125 +#ifndef __NET_TC_CTINFO_H
126 +#define __NET_TC_CTINFO_H
128 +#include <net/act_api.h>
130 +struct tcf_ctinfo_params {
131 + struct rcu_head rcu;
142 + struct tc_action common;
143 + struct tcf_ctinfo_params __rcu *params;
144 + u64 stats_dscp_set;
145 + u64 stats_dscp_error;
146 + u64 stats_cpmark_set;
150 + CTINFO_MODE_DSCP = BIT(0),
151 + CTINFO_MODE_CPMARK = BIT(1)
154 +#define to_ctinfo(a) ((struct tcf_ctinfo *)a)
156 +#endif /* __NET_TC_CTINFO_H */
157 --- a/include/uapi/linux/pkt_cls.h
158 +++ b/include/uapi/linux/pkt_cls.h
159 @@ -66,7 +66,8 @@ enum {
162 /* other actions go here */
168 #define TCA_ID_MAX __TCA_ID_MAX
170 +++ b/include/uapi/linux/tc_act/tc_ctinfo.h
172 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
173 +#ifndef __UAPI_TC_CTINFO_H
174 +#define __UAPI_TC_CTINFO_H
176 +#include <linux/types.h>
177 +#include <linux/pkt_cls.h>
189 + TCA_CTINFO_PARMS_DSCP_MASK,
190 + TCA_CTINFO_PARMS_DSCP_STATEMASK,
191 + TCA_CTINFO_PARMS_CPMARK_MASK,
192 + TCA_CTINFO_STATS_DSCP_SET,
193 + TCA_CTINFO_STATS_DSCP_ERROR,
194 + TCA_CTINFO_STATS_CPMARK_SET,
198 +#define TCA_CTINFO_MAX (__TCA_CTINFO_MAX - 1)
201 --- a/net/sched/Kconfig
202 +++ b/net/sched/Kconfig
203 @@ -808,6 +808,19 @@ config NET_ACT_CONNMARK
204 To compile this code as a module, choose M here: the
205 module will be called act_connmark.
207 +config NET_ACT_CTINFO
208 + tristate "Netfilter Connmark to DSCP Retriever"
209 + depends on NET_CLS_ACT && NETFILTER && IP_NF_IPTABLES
210 + depends on NF_CONNTRACK && NF_CONNTRACK_MARK
212 + Say Y here to allow transfer of a connmark stored DSCP into
217 + To compile this code as a module, choose M here: the
218 + module will be called act_ctinfo.
220 config NET_ACT_SKBMOD
221 tristate "skb data modification action"
222 depends on NET_CLS_ACT
223 --- a/net/sched/Makefile
224 +++ b/net/sched/Makefile
225 @@ -21,6 +21,7 @@ obj-$(CONFIG_NET_ACT_CSUM) += act_csum.o
226 obj-$(CONFIG_NET_ACT_VLAN) += act_vlan.o
227 obj-$(CONFIG_NET_ACT_BPF) += act_bpf.o
228 obj-$(CONFIG_NET_ACT_CONNMARK) += act_connmark.o
229 +obj-$(CONFIG_NET_ACT_CTINFO) += act_ctinfo.o
230 obj-$(CONFIG_NET_ACT_SKBMOD) += act_skbmod.o
231 obj-$(CONFIG_NET_ACT_IFE) += act_ife.o
232 obj-$(CONFIG_NET_IFE_SKBMARK) += act_meta_mark.o
234 +++ b/net/sched/act_ctinfo.c
236 +// SPDX-License-Identifier: GPL-2.0+
237 +/* net/sched/act_ctinfo.c netfilter ctinfo connmark actions
239 + * Copyright (c) 2019 Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
242 +#include <linux/module.h>
243 +#include <linux/init.h>
244 +#include <linux/kernel.h>
245 +#include <linux/skbuff.h>
246 +#include <linux/rtnetlink.h>
247 +#include <linux/pkt_cls.h>
248 +#include <linux/ip.h>
249 +#include <linux/ipv6.h>
250 +#include <net/netlink.h>
251 +#include <net/pkt_sched.h>
252 +#include <net/act_api.h>
253 +#include <net/pkt_cls.h>
254 +#include <uapi/linux/tc_act/tc_ctinfo.h>
255 +#include <net/tc_act/tc_ctinfo.h>
257 +#include <net/netfilter/nf_conntrack.h>
258 +#include <net/netfilter/nf_conntrack_core.h>
259 +#include <net/netfilter/nf_conntrack_ecache.h>
260 +#include <net/netfilter/nf_conntrack_zones.h>
262 +static struct tc_action_ops act_ctinfo_ops;
263 +static unsigned int ctinfo_net_id;
265 +static void tcf_ctinfo_dscp_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
266 + struct tcf_ctinfo_params *cp,
267 + struct sk_buff *skb, int wlen, int proto)
271 + newdscp = (((ct->mark & cp->dscpmask) >> cp->dscpmaskshift) << 2) &
274 + /* mark contains DSCP so restore DSCP bits from ct->mark into diffserv */
275 + /* using overlimits stats to count how many DSCP updates */
278 + dscp = ipv4_get_dsfield(ip_hdr(skb)) & ~INET_ECN_MASK;
279 + if (dscp != newdscp) {
280 + if (likely(!skb_try_make_writable(skb, wlen))) {
281 + ipv4_change_dsfield(ip_hdr(skb),
284 + ca->stats_dscp_set++;
286 + ca->stats_dscp_error++;
291 + dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & ~INET_ECN_MASK;
292 + if (dscp != newdscp) {
293 + if (likely(!skb_try_make_writable(skb, wlen))) {
294 + ipv6_change_dsfield(ipv6_hdr(skb),
297 + ca->stats_dscp_set++;
299 + ca->stats_dscp_error++;
308 +static void tcf_ctinfo_cpmark_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
309 + struct tcf_ctinfo_params *cp,
310 + struct sk_buff *skb)
312 + ca->stats_cpmark_set++;
313 + skb->mark = ct->mark & cp->cpmarkmask;
316 +static int tcf_ctinfo_act(struct sk_buff *skb, const struct tc_action *a,
317 + struct tcf_result *res)
319 + const struct nf_conntrack_tuple_hash *thash = NULL;
320 + struct tcf_ctinfo *ca = to_ctinfo(a);
321 + struct nf_conntrack_tuple tuple;
322 + struct nf_conntrack_zone zone;
323 + enum ip_conntrack_info ctinfo;
324 + struct tcf_ctinfo_params *cp;
325 + struct nf_conn *ct;
329 + cp = rcu_dereference_bh(ca->params);
331 + tcf_lastuse_update(&ca->tcf_tm);
332 + bstats_update(&ca->tcf_bstats, skb);
333 + action = READ_ONCE(ca->tcf_action);
335 + wlen = skb_network_offset(skb);
336 + if (tc_skb_protocol(skb) == htons(ETH_P_IP)) {
337 + wlen += sizeof(struct iphdr);
338 + if (!pskb_may_pull(skb, wlen))
341 + proto = NFPROTO_IPV4;
342 + } else if (tc_skb_protocol(skb) == htons(ETH_P_IPV6)) {
343 + wlen += sizeof(struct ipv6hdr);
344 + if (!pskb_may_pull(skb, wlen))
347 + proto = NFPROTO_IPV6;
352 + ct = nf_ct_get(skb, &ctinfo);
353 + if (!ct) { /* look harder, usually ingress */
354 + if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
355 + proto, cp->net, &tuple))
357 + zone.id = cp->zone;
358 + zone.dir = NF_CT_DEFAULT_ZONE_DIR;
360 + thash = nf_conntrack_find_get(cp->net, &zone, &tuple);
364 + ct = nf_ct_tuplehash_to_ctrack(thash);
367 + if (cp->mode & CTINFO_MODE_DSCP)
368 + if (!cp->dscpstatemask || (ct->mark & cp->dscpstatemask))
369 + tcf_ctinfo_dscp_set(ct, ca, cp, skb, wlen, proto);
371 + if (cp->mode & CTINFO_MODE_CPMARK)
372 + tcf_ctinfo_cpmark_set(ct, ca, cp, skb);
380 +static const struct nla_policy ctinfo_policy[TCA_CTINFO_MAX + 1] = {
381 + [TCA_CTINFO_ACT] = { .len = sizeof(struct
383 + [TCA_CTINFO_ZONE] = { .type = NLA_U16 },
384 + [TCA_CTINFO_PARMS_DSCP_MASK] = { .type = NLA_U32 },
385 + [TCA_CTINFO_PARMS_DSCP_STATEMASK] = { .type = NLA_U32 },
386 + [TCA_CTINFO_PARMS_CPMARK_MASK] = { .type = NLA_U32 },
389 +static int tcf_ctinfo_init(struct net *net, struct nlattr *nla,
390 + struct nlattr *est, struct tc_action **a,
393 + struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
394 + struct nlattr *tb[TCA_CTINFO_MAX + 1];
395 + struct tcf_ctinfo_params *cp_new;
396 +/* struct tcf_chain *goto_ch = NULL; */
397 + u32 dscpmask = 0, dscpstatemask;
398 + struct tc_ctinfo *actparm;
399 + struct tcf_ctinfo *ci;
406 + err = nla_parse_nested(tb, TCA_CTINFO_MAX, nla, ctinfo_policy, NULL);
410 + if (!tb[TCA_CTINFO_ACT])
412 + actparm = nla_data(tb[TCA_CTINFO_ACT]);
414 + /* do some basic validation here before dynamically allocating things */
415 + /* that we would otherwise have to clean up. */
416 + if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) {
417 + dscpmask = nla_get_u32(tb[TCA_CTINFO_PARMS_DSCP_MASK]);
418 + /* need contiguous 6 bit mask */
419 + dscpmaskshift = dscpmask ? __ffs(dscpmask) : 0;
420 + if ((~0 & (dscpmask >> dscpmaskshift)) != 0x3f)
422 + dscpstatemask = tb[TCA_CTINFO_PARMS_DSCP_STATEMASK] ?
423 + nla_get_u32(tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]) : 0;
424 + /* mask & statemask must not overlap */
425 + if (dscpmask & dscpstatemask)
428 + /* done the validation:now to the actual action allocation */
429 + err = tcf_idr_check(tn, actparm->index, a, bind);
431 + ret = tcf_idr_create(tn, actparm->index, est, a,
432 + &act_ctinfo_ops, bind, false);
434 + /* tcf_idr_cleanup(tn, actparm->index); */
437 + ret = ACT_P_CREATED;
438 + } else if (err > 0) {
439 + if (bind) /* don't override defaults */
442 + tcf_idr_release(*a, bind);
449 +/* err = tcf_action_check_ctrlact(actparm->action, tp, &goto_ch, extack);
454 + ci = to_ctinfo(*a);
456 + cp_new = kzalloc(sizeof(*cp_new), GFP_KERNEL);
457 + if (unlikely(!cp_new)) {
463 + cp_new->zone = tb[TCA_CTINFO_ZONE] ?
464 + nla_get_u16(tb[TCA_CTINFO_ZONE]) : 0;
466 + cp_new->dscpmask = dscpmask;
467 + cp_new->dscpmaskshift = dscpmaskshift;
468 + cp_new->dscpstatemask = dscpstatemask;
469 + cp_new->mode |= CTINFO_MODE_DSCP;
472 + if (tb[TCA_CTINFO_PARMS_CPMARK_MASK]) {
473 + cp_new->cpmarkmask = nla_get_u32(tb[TCA_CTINFO_PARMS_CPMARK_MASK]);
474 + cp_new->mode |= CTINFO_MODE_CPMARK;
477 + spin_lock_bh(&ci->tcf_lock);
478 +/* goto_ch = tcf_action_set_ctrlact(*a, actparm->action, goto_ch); */
479 + ci->tcf_action = actparm->action;
480 + rcu_swap_protected(ci->params, cp_new,
481 + lockdep_is_held(&ci->tcf_lock));
482 + spin_unlock_bh(&ci->tcf_lock);
485 + tcf_chain_put_by_act(goto_ch); */
487 + kfree_rcu(cp_new, rcu);
489 + if (ret == ACT_P_CREATED)
490 + tcf_idr_insert(tn, *a);
496 + tcf_chain_put_by_act(goto_ch); */
498 + tcf_idr_release(*a, bind);
502 +static int tcf_ctinfo_dump(struct sk_buff *skb, struct tc_action *a,
505 + struct tcf_ctinfo *ci = to_ctinfo(a);
506 + struct tc_ctinfo opt = {
507 + .index = ci->tcf_index,
508 + .refcnt = ci->tcf_refcnt - ref,
509 + .bindcnt = ci->tcf_bindcnt - bind,
511 + unsigned char *b = skb_tail_pointer(skb);
512 + struct tcf_ctinfo_params *cp;
515 + spin_lock_bh(&ci->tcf_lock);
516 + cp = rcu_dereference_protected(ci->params,
517 + lockdep_is_held(&ci->tcf_lock));
519 + tcf_tm_dump(&t, &ci->tcf_tm);
520 + if (nla_put_64bit(skb, TCA_CTINFO_TM, sizeof(t), &t, TCA_CTINFO_PAD))
521 + goto nla_put_failure;
523 + opt.action = ci->tcf_action;
524 + if (nla_put(skb, TCA_CTINFO_ACT, sizeof(opt), &opt))
525 + goto nla_put_failure;
527 + if (nla_put_u16(skb, TCA_CTINFO_ZONE, cp->zone))
528 + goto nla_put_failure;
530 + if (cp->mode & CTINFO_MODE_DSCP) {
531 + if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_MASK,
533 + goto nla_put_failure;
534 + if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_STATEMASK,
535 + cp->dscpstatemask))
536 + goto nla_put_failure;
539 + if (cp->mode & CTINFO_MODE_CPMARK) {
540 + if (nla_put_u32(skb, TCA_CTINFO_PARMS_CPMARK_MASK,
542 + goto nla_put_failure;
545 + if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_SET,
546 + ci->stats_dscp_set, TCA_CTINFO_PAD))
547 + goto nla_put_failure;
549 + if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_ERROR,
550 + ci->stats_dscp_error, TCA_CTINFO_PAD))
551 + goto nla_put_failure;
553 + if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_CPMARK_SET,
554 + ci->stats_cpmark_set, TCA_CTINFO_PAD))
555 + goto nla_put_failure;
557 + spin_unlock_bh(&ci->tcf_lock);
561 + spin_unlock_bh(&ci->tcf_lock);
562 + nlmsg_trim(skb, b);
566 +static int tcf_ctinfo_walker(struct net *net, struct sk_buff *skb,
567 + struct netlink_callback *cb, int type,
568 + const struct tc_action_ops *ops)
570 + struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
572 + return tcf_generic_walker(tn, skb, cb, type, ops);
575 +static int tcf_ctinfo_search(struct net *net, struct tc_action **a, u32 index)
577 + struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
579 + return tcf_idr_search(tn, a, index);
582 +static struct tc_action_ops act_ctinfo_ops = {
584 + .type = TCA_ID_CTINFO,
585 + .owner = THIS_MODULE,
586 + .act = tcf_ctinfo_act,
587 + .dump = tcf_ctinfo_dump,
588 + .init = tcf_ctinfo_init,
589 + .walk = tcf_ctinfo_walker,
590 + .lookup = tcf_ctinfo_search,
591 + .size = sizeof(struct tcf_ctinfo),
594 +static __net_init int ctinfo_init_net(struct net *net)
596 + struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
598 + return tc_action_net_init(net, tn, &act_ctinfo_ops);
601 +static void __net_exit ctinfo_exit_net(struct net *net)
603 + struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
605 + tc_action_net_exit(tn);
608 +static struct pernet_operations ctinfo_net_ops = {
609 + .init = ctinfo_init_net,
610 + .exit = ctinfo_exit_net,
611 + .id = &ctinfo_net_id,
612 + .size = sizeof(struct tc_action_net),
615 +static int __init ctinfo_init_module(void)
617 + return tcf_register_action(&act_ctinfo_ops, &ctinfo_net_ops);
620 +static void __exit ctinfo_cleanup_module(void)
622 + tcf_unregister_action(&act_ctinfo_ops, &ctinfo_net_ops);
625 +module_init(ctinfo_init_module);
626 +module_exit(ctinfo_cleanup_module);
627 +MODULE_AUTHOR("Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>");
628 +MODULE_DESCRIPTION("Conntrack mark to DSCP restoring");
629 +MODULE_LICENSE("GPL");