0c4ef92c0039e72e95e45c232bd09036631c2985
[openwrt/openwrt.git] / target / linux / generic / hack-4.19 / 645-netfilter-connmark-introduce-savedscp.patch
1 From f171924dcf1d0b31fb7bd1cff113d7a1f7f05ec2 Mon Sep 17 00:00:00 2001
2 From: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
3 Date: Sat, 23 Mar 2019 09:29:49 +0000
4 Subject: [PATCH] netfilter: connmark: introduce savedscp
5
6 savedscp is a method of storing the DSCP of an ip packet into conntrack
7 mark. In combination with a suitable tc filter action (act_ctinfo) DSCP
8 values are able to be stored in the mark on egress and restored on
9 ingress across links that otherwise alter or bleach DSCP.
10
11 This is useful for qdiscs such as CAKE which are able to shape according
12 to policies based on DSCP.
13
14 Ingress classification is traditionally a challenging task since
15 iptables rules haven't yet run and tc filter/eBPF programs are pre-NAT
16 lookups, hence are unable to see internal IPv4 addresses as used on the
17 typical home masquerading gateway.
18
19 x_tables CONNMARK savedscp action solves the problem of storing the DSCP
20 to the conntrack mark in a way suitable for the new act_ctinfo tc action
21 to restore.
22
23 The savedsp option accepts 2 parameters, a 32bit 'dscpmask' and a 32bit
24 'statemask'. The dscp mask must be a minimum of 6 contiguous bits and
25 represents the area where the DSCP will be stored in the connmark. The
26 state mask is a minimum 1 bit length mask that must not overlap with the
27 dscpmask. It represents a flag which is set when the DSCP has been
28 stored in the conntrack mark. This is useful to implement a 'one shot'
29 iptables based classification where the 'complicated' iptables rules are
30 only run once to classify the connection on initial (egress) packet and
31 subsequent packets are all marked/restored with the same DSCP. A state
32 mask of zero disables the setting of a status bit/s.
33
34 example syntax with a suitably modified iptables user space application:
35
36 iptables -A QOS_MARK_eth0 -t mangle -j CONNMARK --savedscp-mark 0xfc000000/0x01000000
37
38 Would store the DSCP in the top 6 bits of the 32bit mark field, and use
39 the LSB of the top byte as the 'DSCP has been stored' marker.
40
41 |----0xFC----conntrack mark----000000---|
42 | Bits 31-26 | bit 25 | bit24 |~~~ Bit 0|
43 | DSCP | unused | flag |unused |
44 |-----------------------0x01---000000---|
45 ^ ^
46 | |
47 ---| Conditional flag
48 | set this when dscp
49 |-ip diffserv-| stored in mark
50 | 6 bits |
51 |-------------|
52
53 an identically configured tc action to restore looks like:
54
55 tc filter show dev eth0 ingress
56 filter parent ffff: protocol all pref 10 u32 chain 0
57 filter parent ffff: protocol all pref 10 u32 chain 0 fh 800: ht divisor 1
58 filter parent ffff: protocol all pref 10 u32 chain 0 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1 not_in_hw
59 match 00000000/00000000 at 0
60 action order 1: ctinfo zone 0 pipe
61 index 2 ref 1 bind 1 dscp 0xfc000000/0x1000000
62
63 action order 2: mirred (Egress Redirect to device ifb4eth0) stolen
64 index 1 ref 1 bind 1
65
66 |----0xFC----conntrack mark----000000---|
67 | Bits 31-26 | bit 25 | bit24 |~~~ Bit 0|
68 | DSCP | unused | flag |unused |
69 |-----------------------0x01---000000---|
70 | |
71 | |
72 ---| Conditional flag
73 v only restore if set
74 |-ip diffserv-|
75 | 6 bits |
76 |-------------|
77
78 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
79 ---
80 include/uapi/linux/netfilter/xt_connmark.h | 3 ++-
81 net/netfilter/xt_connmark.c | 17 +++++++++++++++++
82 2 files changed, 19 insertions(+), 1 deletion(-)
83
84 --- a/include/uapi/linux/netfilter/xt_connmark.h
85 +++ b/include/uapi/linux/netfilter/xt_connmark.h
86 @@ -16,7 +16,8 @@
87 enum {
88 XT_CONNMARK_SET = 0,
89 XT_CONNMARK_SAVE,
90 - XT_CONNMARK_RESTORE
91 + XT_CONNMARK_RESTORE,
92 + XT_CONNMARK_SAVEDSCP
93 };
94
95 enum {
96 --- a/net/netfilter/xt_connmark.c
97 +++ b/net/netfilter/xt_connmark.c
98 @@ -42,6 +42,7 @@ connmark_tg_shift(struct sk_buff *skb, c
99 u_int32_t new_targetmark;
100 struct nf_conn *ct;
101 u_int32_t newmark;
102 + u_int8_t dscp;
103
104 ct = nf_ct_get(skb, &ctinfo);
105 if (ct == NULL)
106 @@ -74,6 +75,21 @@ connmark_tg_shift(struct sk_buff *skb, c
107 nf_conntrack_event_cache(IPCT_MARK, ct);
108 }
109 break;
110 + case XT_CONNMARK_SAVEDSCP:
111 + if (skb->protocol == htons(ETH_P_IP))
112 + dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
113 + else if (skb->protocol == htons(ETH_P_IPV6))
114 + dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
115 + else /* protocol doesn't have diffserv */
116 + break;
117 +
118 + newmark = (ct->mark & ~info->ctmark) |
119 + (info->ctmask | (dscp << info->shift_bits));
120 + if (ct->mark != newmark) {
121 + ct->mark = newmark;
122 + nf_conntrack_event_cache(IPCT_MARK, ct);
123 + }
124 + break;
125 case XT_CONNMARK_RESTORE:
126 new_targetmark = (ct->mark & info->ctmask);
127 if (info->shift_dir == D_SHIFT_RIGHT)
128 @@ -86,6 +102,7 @@ connmark_tg_shift(struct sk_buff *skb, c
129 skb->mark = newmark;
130 break;
131 }
132 +out:
133 return XT_CONTINUE;
134 }
135