From patchwork Tue Apr 9 14:23:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin 'ldir' Darbyshire-Bryant X-Patchwork-Id: 1082315 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=darbyshire-bryant.me.uk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=darbyshire-bryant.me.uk header.i=@darbyshire-bryant.me.uk header.b="tHfDQ3Ep"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44dqKN5jSCz9sSg for ; Wed, 10 Apr 2019 00:23:52 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726475AbfDIOXw (ORCPT ); Tue, 9 Apr 2019 10:23:52 -0400 Received: from mail-eopbgr20073.outbound.protection.outlook.com ([40.107.2.73]:26686 "EHLO EUR02-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726401AbfDIOXw (ORCPT ); Tue, 9 Apr 2019 10:23:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=darbyshire-bryant.me.uk; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jiJnH8DvRZ89uoZ1jdiKLCD6rdO3z6LKABdqA/hXdQU=; b=tHfDQ3EpzyGytASbj6rlo9Xeqx5/JzaEMQ703WwCOqfWssAi+pgJSVQkOY7xbQeEI6Yey3CXYAxVFi19rPBpe4ShrVWFlU4NEYjSJ5RnTGnfwfmVvfYgoHdsXU/5Ape04JfpHC1QQ0IrxjPVH61ewkT8mKkt2YFMhUpxeDbA51E= Received: from VI1PR0302MB2750.eurprd03.prod.outlook.com (10.171.105.143) by VI1PR0302MB2639.eurprd03.prod.outlook.com (10.171.104.136) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1771.13; Tue, 9 Apr 2019 14:23:47 +0000 Received: from VI1PR0302MB2750.eurprd03.prod.outlook.com ([fe80::b584:8ced:9d52:d88e]) by VI1PR0302MB2750.eurprd03.prod.outlook.com ([fe80::b584:8ced:9d52:d88e%5]) with mapi id 15.20.1771.016; Tue, 9 Apr 2019 14:23:47 +0000 From: Kevin 'ldir' Darbyshire-Bryant To: Kevin 'ldir' Darbyshire-Bryant CC: "netfilter-devel@vger.kernel.org" , "pablo@netfilter.org" Subject: [RFC nf-next v2 1/2] netfilter: connmark: introduce savedscp Thread-Topic: [RFC nf-next v2 1/2] netfilter: connmark: introduce savedscp Thread-Index: AQHU7t/XZZNa/M1kFUOfQp3g6ArKEw== Date: Tue, 9 Apr 2019 14:23:46 +0000 Message-ID: <20190409142333.68403-2-ldir@darbyshire-bryant.me.uk> References: <20190409142333.68403-1-ldir@darbyshire-bryant.me.uk> In-Reply-To: <20190409142333.68403-1-ldir@darbyshire-bryant.me.uk> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: LO2P265CA0462.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a2::18) To VI1PR0302MB2750.eurprd03.prod.outlook.com (2603:10a6:800:e2::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=ldir@darbyshire-bryant.me.uk; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.20.1 (Apple Git-117) x-originating-ip: [2a02:c7f:1240:ee00::dc83] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: b8e1f3e9-fa1c-4fd4-11f3-08d6bcf6fa1e x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(7021145)(8989299)(4534185)(7022145)(4603075)(4627221)(201702281549075)(8990200)(5600139)(711020)(4605104)(2017052603328)(7193020); SRVR:VI1PR0302MB2639; x-ms-traffictypediagnostic: VI1PR0302MB2639: x-microsoft-antispam-prvs: x-forefront-prvs: 000227DA0C x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(39830400003)(366004)(396003)(376002)(346002)(136003)(189003)(199004)(7736002)(386003)(81156014)(81166006)(71200400001)(8676002)(53936002)(478600001)(25786009)(6200100001)(305945005)(6862004)(4326008)(256004)(74482002)(14444005)(76176011)(102836004)(486006)(1076003)(14454004)(86362001)(36756003)(11346002)(2616005)(446003)(6506007)(71190400001)(46003)(476003)(186003)(6116002)(54906003)(7049001)(316002)(37006003)(2906002)(52116002)(5660300002)(6486002)(6512007)(8936002)(99286004)(106356001)(105586002)(97736004)(50226002)(6436002)(68736007); DIR:OUT; SFP:1101; SCL:1; SRVR:VI1PR0302MB2639; H:VI1PR0302MB2750.eurprd03.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: darbyshire-bryant.me.uk does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: sHebR/DeZArqQPDXraT25hTlUpJNx7BDduGOKaEnl7onoTeDCGDUlzmVc5uOCQlpygNjcz1jE6JjJXGjQeVyC5PSMXMWp1MT6wTJC+c1ga4ySTAeqH8RJJV1jvAWjeu8aQ4s/IFfbeDsGkoVG7R0DiYLczbCYRN1N98FrgzQ39/mI0mU17LQMcBBI+rbTATSLuMRs34bnaKpPFLTX9ocxdJsmKcr93NQBIlRygrXV9wAgWa+lOqkSjyG979BcjJVe8rkIMoEqBnMDoH4dseXk8HXIog7LGFNy0+sm++b92aabj4A7aadu48y25EvYs8LxCHGF+xbt7O3BeeUab+5QyeE8RsvGe7Nbm4Jvj2ZxF5FNJfn5sVfJFsJ5AtohssNSBiBojemmZsoBvGpDJrTN9ZAPtJbQxSLP/7+6hG2afI= MIME-Version: 1.0 X-OriginatorOrg: darbyshire-bryant.me.uk X-MS-Exchange-CrossTenant-Network-Message-Id: b8e1f3e9-fa1c-4fd4-11f3-08d6bcf6fa1e X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2019 14:23:46.8599 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 9151708b-c553-406f-8e56-694f435154a4 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0302MB2639 Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org savedscp is a method of storing the DSCP of an ip packet into conntrack mark. In combination with a suitable tc filter action (act_ctinfo) DSCP values are able to be stored in the mark on egress and restored on ingress across links that otherwise alter or bleach DSCP. This is useful for qdiscs such as CAKE which are able to shape according to policies based on DSCP. Ingress classification is traditionally a challenging task since iptables rules haven't yet run and tc filter/eBPF programs are pre-NAT lookups, hence are unable to see internal IPv4 addresses as used on the typical home masquerading gateway. x_tables CONNMARK savedscp action solves the problem of storing the DSCP to the conntrack mark in a way suitable for the new act_ctinfo tc action to restore. The savedsp option accepts 2 parameters, a 32bit 'dscpmask' and a 32bit 'statemask'. The dscp mask must be a minimum of 6 contiguous bits and represents the area where the DSCP will be stored in the connmark. The state mask is a minimum 1 bit length mask that must not overlap with the dscpmask. It represents a flag which is set when the DSCP has been stored in the conntrack mark. This is useful to implement a 'one shot' iptables based classification where the 'complicated' iptables rules are only run once to classify the connection on initial (egress) packet and subsequent packets are all marked/restored with the same DSCP. A state mask of zero disables the setting of a status bit/s. example syntax with a suitably modified iptables user space application: iptables -A QOS_MARK_eth0 -t mangle -j CONNMARK --savedscp-mark 0xfc000000/0x01000000 Would store the DSCP in the top 6 bits of the 32bit mark field, and use the LSB of the top byte as the 'DSCP has been stored' marker. |----0xFC----conntrack mark----000000---| | Bits 31-26 | bit 25 | bit24 |~~~ Bit 0| | DSCP | unused | flag |unused | |-----------------------0x01---000000---| ^ ^ | | ---| Conditional flag | set this when dscp |-ip diffserv-| stored in mark | 6 bits | |-------------| an identically configured tc action to restore looks like: tc filter show dev eth0 ingress filter parent ffff: protocol all pref 10 u32 chain 0 filter parent ffff: protocol all pref 10 u32 chain 0 fh 800: ht divisor 1 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 match 00000000/00000000 at 0 action order 1: ctinfo zone 0 pipe index 2 ref 1 bind 1 dscp 0xfc000000/0x1000000 action order 2: mirred (Egress Redirect to device ifb4eth0) stolen index 1 ref 1 bind 1 |----0xFC----conntrack mark----000000---| | Bits 31-26 | bit 25 | bit24 |~~~ Bit 0| | DSCP | unused | flag |unused | |-----------------------0x01---000000---| | | | | ---| Conditional flag v only restore if set |-ip diffserv-| | 6 bits | |-------------| Signed-off-by: Kevin Darbyshire-Bryant --- v2 - reword commit message swap the dscpmask/statemask order so it matches the tc action order this also matches the value[/mask] convention in that dscpmask is non optional whereas the statemask is optional. include/uapi/linux/netfilter/xt_connmark.h | 3 ++- net/netfilter/xt_connmark.c | 30 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/netfilter/xt_connmark.h b/include/uapi/linux/netfilter/xt_connmark.h index 1aa5c955ee1e..24272cac2d37 100644 --- a/include/uapi/linux/netfilter/xt_connmark.h +++ b/include/uapi/linux/netfilter/xt_connmark.h @@ -16,7 +16,8 @@ enum { XT_CONNMARK_SET = 0, XT_CONNMARK_SAVE, - XT_CONNMARK_RESTORE + XT_CONNMARK_RESTORE, + XT_CONNMARK_SAVEDSCP }; enum { diff --git a/net/netfilter/xt_connmark.c b/net/netfilter/xt_connmark.c index 29c38aa7f726..6c63cf476342 100644 --- a/net/netfilter/xt_connmark.c +++ b/net/netfilter/xt_connmark.c @@ -42,6 +42,7 @@ connmark_tg_shift(struct sk_buff *skb, const struct xt_connmark_tginfo2 *info) u_int32_t new_targetmark; struct nf_conn *ct; u_int32_t newmark; + u8 dscp; ct = nf_ct_get(skb, &ctinfo); if (ct == NULL) @@ -74,6 +75,34 @@ connmark_tg_shift(struct sk_buff *skb, const struct xt_connmark_tginfo2 *info) nf_conntrack_event_cache(IPCT_MARK, ct); } break; + case XT_CONNMARK_SAVEDSCP: + if (!info->ctmark) + goto out; + + if (skb->protocol == htons(ETH_P_IP)) { + if (skb->len < sizeof(struct iphdr)) + goto out; + + dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2; + + } else if (skb->protocol == htons(ETH_P_IPV6)) { + if (skb->len < sizeof(struct ipv6hdr)) + goto out; + + dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; + + } else { /* protocol doesn't have diffserv - get out! */ + goto out; + } + + newmark = (ct->mark & ~info->ctmark) ^ + (info->ctmask | (dscp << info->shift_bits)); + + if (ct->mark != newmark) { + ct->mark = newmark; + nf_conntrack_event_cache(IPCT_MARK, ct); + } + break; case XT_CONNMARK_RESTORE: new_targetmark = (ct->mark & info->ctmask); if (info->shift_dir == D_SHIFT_RIGHT) @@ -86,6 +115,7 @@ connmark_tg_shift(struct sk_buff *skb, const struct xt_connmark_tginfo2 *info) skb->mark = newmark; break; } +out: return XT_CONTINUE; }