Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp1596551ybm; Sat, 30 May 2020 14:12:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzl5Moaa/gii/5CB268gf0bGaBcHMxtZAWuuU2361Gb6K/LnDYAT9/Ml7vhZim1jnw0U9wS X-Received: by 2002:a17:906:cd06:: with SMTP id oz6mr13872632ejb.434.1590873137246; Sat, 30 May 2020 14:12:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1590873137; cv=pass; d=google.com; s=arc-20160816; b=0aN1BZe3UROolxQqx1nH20SOY3CB49WxNXRqdlbqsPlLalU0wsYgo7DaKV3hXP/Qnj 6y+DP4rl0/4uNjQR9EMOeJqoWh5TRBeCzTFeFLFw70AYCrQZstlCaN/7NX+1uY1SzrRi X0nt4HdVN51T85KGylxjwORbjk1wl/KMGdZYQ3VfRaNpnzWAwMm0d2C98sl72R7HiIIg /raPjuSUjxYcI4lWcddL/NKwI9MxX2MkolvVvligtHBrP0ZEMcqHEHGACkK0XIY4+UH0 me6qlveW1NHwlmryG3PBuh4BQeVvOe++XmIDpouZQbGc9D3jStvYytoxPFL/FVRY4wBo O1Eg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=DI1K48FdjscSVBk8PZUJ20aalFwmOv7Pe3ZnJA4sbN8=; b=COAYyQcdM+li0rY4ar8jNNsa+Ke9yr+lU/JybnDCTI1fYep2GaUugizA7a7adh0QV8 0WWv3CnoPesQxw2soHCxNsQOuVWNO7p/xBbNA+iTVB/ERDT+axojgJIgUr0zsLJQOTKP tPobIpJ0X6GmV1dlXkdtf15/1qvu0AuwQz2432P79QJaS8YymfcbgTO9Yo8sGbwHXKrc 6eN57Jv/u1wa56THwUyv0JwjjWb9lKJy15+84hqcyhCLFBNXSvjdmkgOagLMb8QcCSTv IEIsgzMJJ25utuS1W3NchxozWtVb9w81/RqEoT+Mk2S0HzsV26LXxwgSh2ty3QmLpoE1 bUgQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ikelte.onmicrosoft.com header.s=selector2-ikelte-onmicrosoft-com header.b=ItNxO3jP; arc=pass (i=1 spf=pass spfdomain=inf.elte.hu dkim=pass dkdomain=inf.elte.hu dmarc=pass fromdomain=inf.elte.hu); 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s16si8003769ejr.669.2020.05.30.14.11.54; Sat, 30 May 2020 14:12:17 -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=@ikelte.onmicrosoft.com header.s=selector2-ikelte-onmicrosoft-com header.b=ItNxO3jP; arc=pass (i=1 spf=pass spfdomain=inf.elte.hu dkim=pass dkdomain=inf.elte.hu dmarc=pass fromdomain=inf.elte.hu); 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729434AbgE3VJ6 (ORCPT + 99 others); Sat, 30 May 2020 17:09:58 -0400 Received: from mail-eopbgr30074.outbound.protection.outlook.com ([40.107.3.74]:2551 "EHLO EUR03-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729098AbgE3VJ5 (ORCPT ); Sat, 30 May 2020 17:09:57 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kIZpRcJXSj0YDpQNXK0eaLAVvgknDegtmf4Cg0wBO8ImIKFo68sbhfg9cn0UbRN6xpfJAKPL3QCLo8KGY9Nwc+OY0xvkg4ldj0GPtNuCgR8MvJhwmFFTEY5ckT/gwJKHX0ipW58liwu9FhYEvzZ18EnK2LcAdKT9OVu0wTaM/pHWWj8hNv0wMfuOSfHkFGq1+OvdEvBONAMGcS/as//QslQfrsuQIEOUgU3QJXuHl1zY0IhQvZrGRnI8vWdcw7rgKFRw2iRE2cDox9OaGpDmvZHMD9vt49BR2w2bCFIDk+IdshWzFzPxJkbPIgAzXf7SsJlLb5PMgeIR0u88IgFRQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DI1K48FdjscSVBk8PZUJ20aalFwmOv7Pe3ZnJA4sbN8=; b=M9y7GKmdL0pcrnP7cKLl/9Ljl+MFf2vDcmqmABVczbHhCQySNWS+bHp4GS8dRDKBUqjgrDMB6TptfNLadl/nkis4WKFd7re1UI1yAct5GKJKgST2BlXFXU38PFAuWaQCid+OwvNdtnlgXkhQPkTCjt89kWjWQaA0ndM3Ik+RqkAtp6dmcK84FfWhpAYxoKLmGK5Tarwq1ea5BZo/Z/w0T48cPD3SKCsv8KrjfvbJ8d00clXGG2sH0VuCnuTsE05TkaMCKGVq4I1RpO0pjBgyjDwEvbCfG048Ho5cUVZlbNby2duCfcwAYLh0aC+9TXYMhMOSLcWCOitrqRHxRuRZuw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=inf.elte.hu; dmarc=pass action=none header.from=inf.elte.hu; dkim=pass header.d=inf.elte.hu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ikelte.onmicrosoft.com; s=selector2-ikelte-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DI1K48FdjscSVBk8PZUJ20aalFwmOv7Pe3ZnJA4sbN8=; b=ItNxO3jPqyD0mLTU+kZDOC/6D9wLGDllaD/CW5QA4fPw/eRNjQR5F0JmOfXsoJy2Pwlgb9ANNVc/ekRfF6RTT1zFgdTPv7Z+ksPCRiFXGIE67/MugiAbtxePmpRvE8i1qv2I6fTPk8TLUmtGqNEPSiTFC9Vc3K1Wtkzb0B77un8= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=inf.elte.hu; Received: from DB8PR10MB2652.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:10:ab::20) by DB8PR10MB3034.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:10:e1::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3045.19; Sat, 30 May 2020 21:09:19 +0000 Received: from DB8PR10MB2652.EURPRD10.PROD.OUTLOOK.COM ([fe80::285b:6f31:7d11:5c53]) by DB8PR10MB2652.EURPRD10.PROD.OUTLOOK.COM ([fe80::285b:6f31:7d11:5c53%5]) with mapi id 15.20.3045.022; Sat, 30 May 2020 21:09:19 +0000 From: Ferenc Fejes To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Andrii Nakryiko , Stanislav Fomichev , Martin KaFai Lau , "David S . Miller" , Ferenc Fejes Subject: [PATCH v2 net-next 2/3] bpf: Allow SO_BINDTODEVICE opt in bpf_setsockopt Date: Sat, 30 May 2020 23:09:01 +0200 Message-Id: <4149e304867b8d5a606a305bc59e29b063e51f49.1590871065.git.fejes@inf.elte.hu> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: Content-Type: text/plain X-ClientProxiedBy: VI1PR0202CA0016.eurprd02.prod.outlook.com (2603:10a6:803:14::29) To DB8PR10MB2652.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:10:ab::20) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (89.133.95.17) by VI1PR0202CA0016.eurprd02.prod.outlook.com (2603:10a6:803:14::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3045.17 via Frontend Transport; Sat, 30 May 2020 21:09:16 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [89.133.95.17] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 463c9d86-ad73-4a42-c1e4-08d804ddb6a7 X-MS-TrafficTypeDiagnostic: DB8PR10MB3034: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4502; X-Forefront-PRVS: 041963B986 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KDVqftCLwissOmNRjJtkxy1a8XNJv+vNVUoC4rTegqGIqJRefKOUF7Fay4l/hTD7aBKtypzZsh9oKW0+DbF2ViwoKc5CqXlG+7WdHRnrBhPJDLsc+8DJo6/JTPvKjj6EQpAkpKP2EbT+cmhGjDyGYwcILnH1UdPisTv1j+NcHF8+BPUSh72fMUoi/YWbXzmyvJ6diAbzCLlhOY0iDtDH/cS9mYPazI6vr+EMlHs2gfu1I01z3iSR4OfpM5dJdgLWW9O3ZWJ5joz7km2HfjWunfmLyFQfyGoOAN9KCggEb9jnqqTR/6GwytDXuwKbGUtTwQWWOcDXzxnR6yloA5AYs6yijqZAS5OSp4zNMk0pCNRq+YyIBxmhu54NJOSizn7W X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB8PR10MB2652.EURPRD10.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFTY:;SFS:(376002)(346002)(136003)(396003)(39840400004)(366004)(66556008)(6486002)(69590400007)(66946007)(66476007)(478600001)(83380400001)(107886003)(8936002)(6506007)(8676002)(52116002)(6666004)(5660300002)(26005)(2616005)(6916009)(316002)(956004)(786003)(86362001)(4326008)(2906002)(6512007)(16526019)(54906003)(186003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: PwjkVAtTbUKp7lbTdgKiSwU9jmCFCpq9A93qcS5JiOior1zUvg5lVD4k9f2k0qDvOw4OqtPBfpUGcPIpVXPQCy+5dRxAp1sSMOIMO2FFfNct3FYDF2MWaC9et2TZNPwctlbrneJ9eagdvEgtIW9MF4C6MPre7biJjKvBLebtK6MxCcf3maJCvbbCCJ/OXbdjZORK/9WyQaIH0h9b79ETIU6tpNvd81tA9E0UPzmWIYR1bmm0it+TA+3cjsNytWhLHgay2DII8N5iVScHuGQQT2PGCeivM9l5LNNZ0n3prRlBwSpO7gEUyhn4jsax9BF7m3Fr8qfxuyFyY6hSk1bhI9qSQBclMNobVBYuxhrc+izDVUK5b5VK2Rprc51IrViKhag+uUSXd5x4l+lJvK4EHFFjjPPlrlHLZooQ3ALAcUh8ZicEKzPNzGzKqPsQnU5mO8XrFPYwNB+7GmiYZNS5lrx/tePfK600y/X2bXT27FA= X-OriginatorOrg: inf.elte.hu X-MS-Exchange-CrossTenant-Network-Message-Id: 463c9d86-ad73-4a42-c1e4-08d804ddb6a7 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2020 21:09:17.2795 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0133bb48-f790-4560-a64d-ac46a472fbbc X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: PV4wbXzjBHlYLqbSFGZwMsYjQh1piAYdAlg0J0pRxg0J5/985HNaAsoDdPRPhp7oyEuyuYclCYd/sbRVrNVMzQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR10MB3034 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Extending the supported sockopts in bpf_setsockopt with SO_BINDTODEVICE. We call sock_bindtoindex with parameter lock_sk = false in this context because we already owning the socket. Signed-off-by: Ferenc Fejes --- net/core/filter.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/net/core/filter.c b/net/core/filter.c index bd2853d23b50..a0958c5ef127 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4248,6 +4248,9 @@ static const struct bpf_func_proto bpf_get_socket_uid_proto = { static int _bpf_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen, u32 flags) { + char devname[IFNAMSIZ]; + struct net *net; + int ifindex; int ret = 0; int val; @@ -4257,7 +4260,7 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname, sock_owned_by_me(sk); if (level == SOL_SOCKET) { - if (optlen != sizeof(int)) + if (optlen != sizeof(int) && optname != SO_BINDTODEVICE) return -EINVAL; val = *((int *)optval); @@ -4298,6 +4301,29 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname, sk_dst_reset(sk); } break; + case SO_BINDTODEVICE: + ret = -ENOPROTOOPT; +#ifdef CONFIG_NETDEVICES + optlen = min_t(long, optlen, IFNAMSIZ - 1); + strncpy(devname, optval, optlen); + devname[optlen] = 0; + + ifindex = 0; + if (devname[0] != '\0') { + struct net_device *dev; + + ret = -ENODEV; + + net = sock_net(sk); + dev = dev_get_by_name(net, devname); + if (!dev) + break; + ifindex = dev->ifindex; + dev_put(dev); + } + ret = sock_bindtoindex(sk, ifindex, false); +#endif + break; default: ret = -EINVAL; } -- 2.17.1