From patchwork Fri Jun 12 16:15:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 483672 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 67F06140187 for ; Sat, 13 Jun 2015 02:16:23 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=jBrKYY4b; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755414AbbFLQQT (ORCPT ); Fri, 12 Jun 2015 12:16:19 -0400 Received: from mail-wg0-f49.google.com ([74.125.82.49]:33748 "EHLO mail-wg0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755323AbbFLQQR (ORCPT ); Fri, 12 Jun 2015 12:16:17 -0400 Received: by wgez8 with SMTP id z8so28020901wge.0; Fri, 12 Jun 2015 09:16:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=ZQrXureUSV2nuRZ+5ec79LXCo51Ikh7eaPjyRbUHD74=; b=jBrKYY4bBt4soKjXF2sOZZPOIb9NN99QB/RU8wMMB2EtnmGtwMvLYIitEDiLJebbIF 2tvttIk4LqPAGzcC7JlW1909ng9xwJzS261fm6R6f8Xdb0TeUgTOJZH2qNmH2HihyM4o NIC2wNYtDDZhvuE4g2aMrViA4I3DdgRVG+7F8Xv2qiJwjN4HQ8tnHC3fhnETgzI9Z8Po xu5nT/qrxug2s/33DepC+E63zE1PgqYMux3f7eQaj8Gd7cxy/v/C9+QU+YZCwvVrhqV5 YaeP+i3D4wgLgfT2cXHauthwW8StvyhKNFA+NmCH5eeEnqRSn6jBlY2ztka0nzFLRAKb UPxQ== X-Received: by 10.180.95.67 with SMTP id di3mr8222357wib.78.1434125775741; Fri, 12 Jun 2015 09:16:15 -0700 (PDT) Received: from david-t2.fritz.box (p548950C6.dip0.t-ipconnect.de. [84.137.80.198]) by mx.google.com with ESMTPSA id dz4sm3421974wib.17.2015.06.12.09.16.14 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Jun 2015 09:16:14 -0700 (PDT) From: David Herrmann To: netdev@vger.kernel.org Cc: "David S. Miller" , Thomas Graf , Herbert Xu , linux-kernel@vger.kernel.org, David Herrmann Subject: [PATCH] netlink: add API to retrieve all group memberships Date: Fri, 12 Jun 2015 18:15:38 +0200 Message-Id: <1434125738-1990-1-git-send-email-dh.herrmann@gmail.com> X-Mailer: git-send-email 2.4.2 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch adds getsockopt(SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS) to retrieve all groups a socket is a member of. Currently, we have to use getsockname() and look at the nl.nl_groups bitmask. However, this mask is limited to 32 groups. Hence, similar to NETLINK_ADD_MEMBERSHIP and NETLINK_DROP_MEMBERSHIP, this adds a separate sockopt to manager higher groups IDs than 32. This new NETLINK_LIST_MEMBERSHIPS option takes a pointer to __u32 and the size of the array. The array is filled with the full membership-set of the socket, and the required array size is returned in optlen. Hence, user-space can retry with a properly sized array in case it was too small. Signed-off-by: David Herrmann --- include/uapi/linux/netlink.h | 15 ++++++++------- net/netlink/af_netlink.c | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/netlink.h b/include/uapi/linux/netlink.h index 1a85940..e38094f 100644 --- a/include/uapi/linux/netlink.h +++ b/include/uapi/linux/netlink.h @@ -101,13 +101,14 @@ struct nlmsgerr { struct nlmsghdr msg; }; -#define NETLINK_ADD_MEMBERSHIP 1 -#define NETLINK_DROP_MEMBERSHIP 2 -#define NETLINK_PKTINFO 3 -#define NETLINK_BROADCAST_ERROR 4 -#define NETLINK_NO_ENOBUFS 5 -#define NETLINK_RX_RING 6 -#define NETLINK_TX_RING 7 +#define NETLINK_ADD_MEMBERSHIP 1 +#define NETLINK_DROP_MEMBERSHIP 2 +#define NETLINK_PKTINFO 3 +#define NETLINK_BROADCAST_ERROR 4 +#define NETLINK_NO_ENOBUFS 5 +#define NETLINK_RX_RING 6 +#define NETLINK_TX_RING 7 +#define NETLINK_LIST_MEMBERSHIPS 8 struct nl_pktinfo { __u32 group; diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index bf6e766..b84dbe7 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -2254,6 +2254,28 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname, return -EFAULT; err = 0; break; + case NETLINK_LIST_MEMBERSHIPS: { + int pos, idx, shift; + + err = 0; + netlink_table_grab(); + for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) { + if (len - pos < sizeof(u32)) + break; + + idx = pos / sizeof(unsigned long); + shift = (pos % sizeof(unsigned long)) * 8; + if (put_user((u32)(nlk->groups[idx] >> shift), + (u32 __user *)(optval + pos))) { + err = -EFAULT; + break; + } + } + if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen)) + err = -EFAULT; + netlink_table_ungrab(); + break; + } default: err = -ENOPROTOOPT; }