Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp463447pxy; Wed, 28 Apr 2021 07:45:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0YioNeBnyIghclNVmfc7z8mv3ERXC8MQjqPJeuSAkk9qd2oTjkUC0QjEB9AeWEKtOFTHa X-Received: by 2002:a63:5809:: with SMTP id m9mr3197273pgb.62.1619621127437; Wed, 28 Apr 2021 07:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619621127; cv=none; d=google.com; s=arc-20160816; b=BhFYWrxHai5MlZTnGizvc0MNYSXQU/uzHON8W4m5od6amVOE9stguPWeCDwhtzE3dT 7tPAqYwQgzNpxRxyKHD+RLVyPOC6d9KX6qno7eXQTl8pu/2qvcwNB/MjPMsIsuq9Cdbp yuNQjzX63ehbDqepiEpBmMFeqbU9fDBYs99bAAcoYjnhhyozj8rRSJR7U9BLrPYuFpZ/ RFHcyNbRh+XWSttOXgUMLNnme40/7o+pPm73yIp7n28wCmt8miTxKux1oaC1hUDtyXY+ gOLuMvx7XlW+4GdcwxVpJmbL4dtQaTqltbO5csalacwMihk/SswworiaB/zEurZzyZMq 4HRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature:dkim-filter; bh=kIfBaSDcSRGgxC5FQL/wJdUMo3bX4qPVuhHsrP8+fWo=; b=jSIVTjFAtKOg0QDX+IIMm2RvO+VtRybzUikgGIVjrzkx7XVnbwKr8nqEO6O0scAB/E Ik8lSu+U3hwwBM6B/rxGzuM1DZgpUcUsqaDTRkRsuGE/ndI+4eiW+bzXH63HIBUDDojX JA0x3f6a6Uv5FlX36MiPhRTin0eBbHj/gMOYCtC+77CYXz1fqrQS3ApMb9XoFHMPOGt8 3a7pey612qL/opU6hFNp05ZIqWS503DNOZEzKCyqNxMaVr0PHUNWSskIq5FqwQrm4oD2 q/ImIYt9TI+zJBNNZxE1BXUDyYp8vIPalHo7clDD1um0MfAbzqYpJM6GaMu2+OnUtTyI uhRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=kIEr8Kjq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i16si3640185pgn.355.2021.04.28.07.45.13; Wed, 28 Apr 2021 07:45:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=kIEr8Kjq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238103AbhD1Mgd (ORCPT + 99 others); Wed, 28 Apr 2021 08:36:33 -0400 Received: from mx0.infotecs.ru ([91.244.183.115]:36032 "EHLO mx0.infotecs.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236973AbhD1Mgd (ORCPT ); Wed, 28 Apr 2021 08:36:33 -0400 Received: from mx0.infotecs-nt (localhost [127.0.0.1]) by mx0.infotecs.ru (Postfix) with ESMTP id AEC76138252B; Wed, 28 Apr 2021 15:35:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx0.infotecs.ru AEC76138252B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=infotecs.ru; s=mx; t=1619613346; bh=kIfBaSDcSRGgxC5FQL/wJdUMo3bX4qPVuhHsrP8+fWo=; h=Date:From:To:CC:Subject:From; b=kIEr8KjqSjw3j+a3crSwWrEGdv0JHuspTZD7wUK70VTaGNJwxHg7ps/QGy5UhmLVv XDxL8jX93yLij1bNJhiNKTeSI7lKnoba2IrkoyN3pgc1voBR/pNP4Oru0L4TZFvqD3 z2i4D/slGGb5/cLKZLiuBKIBibGmcLanwM+RVsbE= Received: from msk-exch-02.infotecs-nt (autodiscover.amonitoring.ru [10.0.7.192]) by mx0.infotecs-nt (Postfix) with ESMTP id ACD2A3170EE9; Wed, 28 Apr 2021 15:35:46 +0300 (MSK) Date: Wed, 28 Apr 2021 15:33:57 +0300 From: Pavel Balaev To: CC: "David S. Miller" , Jakub Kicinski , Jonathan Corbet , Hideaki YOSHIFUJI , David Ahern , Shuah Khan , Christophe JAILLET , , , Ido Schimmel Subject: [PATCH v6 net-next 2/3] net/ipv6: multipath routing: configurable seed Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline X-Originating-IP: [11.0.8.107] X-EXCLAIMER-MD-CONFIG: 208ac3cd-1ed4-4982-a353-bdefac89ac0a X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 163368 [Apr 28 2021] X-KLMS-AntiSpam-Version: 5.9.20.0 X-KLMS-AntiSpam-Envelope-From: BalaevPA@infotecs.ru X-KLMS-AntiSpam-Rate: 0 X-KLMS-AntiSpam-Status: not_detected X-KLMS-AntiSpam-Method: none X-KLMS-AntiSpam-Auth: dkim=none X-KLMS-AntiSpam-Info: LuaCore: 444 444 810d227eb39db4b6b1b4a1d2ddb57b8d8d084f93, {Tracking_from_domain_doesnt_match_to} X-MS-Exchange-Organization-SCL: -1 X-KLMS-AntiSpam-Interceptor-Info: scan successful X-KLMS-AntiPhishing: Clean, bases: 2021/04/28 11:08:00 X-KLMS-AntiVirus: Kaspersky Security for Linux Mail Server, version 8.0.3.30, bases: 2021/04/28 07:03:00 #16582427 X-KLMS-AntiVirus-Status: Clean, skipped Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ability for a user to assign seed value to IPv6 multipath route hashes. This patch adds sysctl variable: net.ipv6.fib_multipath_hash_seed. Signed-off-by: Pavel Balaev --- include/net/netns/ipv6.h | 3 ++ net/ipv6/route.c | 10 +++- net/ipv6/sysctl_net_ipv6.c | 96 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/include/net/netns/ipv6.h b/include/net/netns/ipv6.h index 808f0f79e..6bb383b0a 100644 --- a/include/net/netns/ipv6.h +++ b/include/net/netns/ipv6.h @@ -8,6 +8,7 @@ #ifndef __NETNS_IPV6_H__ #define __NETNS_IPV6_H__ #include +#include #include struct ctl_table_header; @@ -30,6 +31,7 @@ struct netns_sysctl_ipv6 { int ip6_rt_min_advmss; u8 bindv6only; u8 multipath_hash_policy; + u8 multipath_hash_seed; u8 flowlabel_consistency; u8 auto_flowlabels; int icmpv6_time; @@ -107,6 +109,7 @@ struct netns_ipv6 { struct fib_rules_ops *mr6_rules_ops; #endif #endif + siphash_key_t __rcu *multipath_hash_seed_ctx; atomic_t dev_addr_genid; atomic_t fib6_sernum; struct seg6_pernet_data *seg6_data; diff --git a/net/ipv6/route.c b/net/ipv6/route.c index 28801ae80..70c488812 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -2331,6 +2331,7 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, const struct sk_buff *skb, struct flow_keys *flkeys) { struct flow_keys hash_keys; + siphash_key_t *seed_ctx; u32 mhash; switch (ip6_multipath_hash_policy(net)) { @@ -2414,7 +2415,14 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, } break; } - mhash = flow_hash_from_keys(&hash_keys); + + rcu_read_lock(); + seed_ctx = rcu_dereference(net->ipv6.multipath_hash_seed_ctx); + if (seed_ctx) + mhash = flow_multipath_hash_from_keys(&hash_keys, seed_ctx); + else + mhash = flow_hash_from_keys(&hash_keys); + rcu_read_unlock(); return mhash >> 1; } diff --git a/net/ipv6/sysctl_net_ipv6.c b/net/ipv6/sysctl_net_ipv6.c index 27102c3d6..349251cb7 100644 --- a/net/ipv6/sysctl_net_ipv6.c +++ b/net/ipv6/sysctl_net_ipv6.c @@ -40,6 +40,94 @@ static int proc_rt6_multipath_hash_policy(struct ctl_table *table, int write, return ret; } +#define RT6_MULTIPATH_SEED_KEY_LENGTH sizeof(siphash_key_t) +#define RT6_MULTIPATH_SEED_RANDOM "random" +static int proc_rt6_multipath_hash_seed(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) +{ + struct net *net = container_of(table->data, struct net, + ipv6.sysctl.multipath_hash_seed); + /* maxlen to print the keys in hex (*2) and a comma in between keys. */ + struct ctl_table tbl = { + .maxlen = ((RT6_MULTIPATH_SEED_KEY_LENGTH * 2) + 2) + }; + siphash_key_t user_key, *ctx; + __le64 key[2]; + int ret; + + tbl.data = kmalloc(tbl.maxlen, GFP_KERNEL); + + if (!tbl.data) + return -ENOMEM; + + rcu_read_lock(); + ctx = rcu_dereference(net->ipv6.multipath_hash_seed_ctx); + if (ctx) { + put_unaligned_le64(ctx->key[0], &key[0]); + put_unaligned_le64(ctx->key[1], &key[1]); + user_key.key[0] = le64_to_cpu(key[0]); + user_key.key[1] = le64_to_cpu(key[1]); + + snprintf(tbl.data, tbl.maxlen, "%016llx,%016llx", + user_key.key[0], user_key.key[1]); + } else { + snprintf(tbl.data, tbl.maxlen, "%s", RT6_MULTIPATH_SEED_RANDOM); + } + rcu_read_unlock(); + + ret = proc_dostring(&tbl, write, buffer, lenp, ppos); + + if (write && ret == 0) { + siphash_key_t *new_ctx, *old_ctx; + + if (!strcmp(tbl.data, RT6_MULTIPATH_SEED_RANDOM)) { + rtnl_lock(); + old_ctx = rtnl_dereference(net->ipv6.multipath_hash_seed_ctx); + RCU_INIT_POINTER(net->ipv6.multipath_hash_seed_ctx, NULL); + rtnl_unlock(); + if (old_ctx) { + synchronize_net(); + kfree_sensitive(old_ctx); + } + + pr_debug("multipath hash seed set to random value\n"); + goto out; + } + + if (sscanf(tbl.data, "%llx,%llx", user_key.key, user_key.key + 1) != 2) { + ret = -EINVAL; + goto out; + } + + key[0] = cpu_to_le64(user_key.key[0]); + key[1] = cpu_to_le64(user_key.key[1]); + pr_debug("multipath hash seed set to 0x%llx,0x%llx\n", + user_key.key[0], user_key.key[1]); + + new_ctx = kmalloc(sizeof(*new_ctx), GFP_KERNEL); + if (!new_ctx) { + ret = -ENOMEM; + goto out; + } + + new_ctx->key[0] = get_unaligned_le64(&key[0]); + new_ctx->key[1] = get_unaligned_le64(&key[1]); + + rtnl_lock(); + old_ctx = rtnl_dereference(net->ipv6.multipath_hash_seed_ctx); + rcu_assign_pointer(net->ipv6.multipath_hash_seed_ctx, new_ctx); + rtnl_unlock(); + if (old_ctx) { + synchronize_net(); + kfree_sensitive(old_ctx); + } + } + +out: + kfree(tbl.data); + return ret; +} + static struct ctl_table ipv6_table_template[] = { { .procname = "bindv6only", @@ -151,6 +239,14 @@ static struct ctl_table ipv6_table_template[] = { .extra1 = SYSCTL_ZERO, .extra2 = &two, }, + { + .procname = "fib_multipath_hash_seed", + .data = &init_net.ipv6.sysctl.multipath_hash_seed, + /* maxlen to print the keys in hex (*2) and a comma in between keys. */ + .maxlen = (RT6_MULTIPATH_SEED_KEY_LENGTH * 2) + 2, + .mode = 0600, + .proc_handler = proc_rt6_multipath_hash_seed, + }, { .procname = "seg6_flowlabel", .data = &init_net.ipv6.sysctl.seg6_flowlabel, -- 2.31.1