forked from mirrors/linux
		
	netlink: add API to retrieve all group memberships
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 <dh.herrmann@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									e0df02e0c2
								
							
						
					
					
						commit
						b42be38b27
					
				
					 2 changed files with 31 additions and 8 deletions
				
			
		| 
						 | 
					@ -109,6 +109,7 @@ struct nlmsgerr {
 | 
				
			||||||
#define NETLINK_RX_RING			6
 | 
					#define NETLINK_RX_RING			6
 | 
				
			||||||
#define NETLINK_TX_RING			7
 | 
					#define NETLINK_TX_RING			7
 | 
				
			||||||
#define NETLINK_LISTEN_ALL_NSID		8
 | 
					#define NETLINK_LISTEN_ALL_NSID		8
 | 
				
			||||||
 | 
					#define NETLINK_LIST_MEMBERSHIPS	9
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct nl_pktinfo {
 | 
					struct nl_pktinfo {
 | 
				
			||||||
	__u32	group;
 | 
						__u32	group;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2290,6 +2290,28 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 | 
				
			||||||
			return -EFAULT;
 | 
								return -EFAULT;
 | 
				
			||||||
		err = 0;
 | 
							err = 0;
 | 
				
			||||||
		break;
 | 
							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:
 | 
						default:
 | 
				
			||||||
		err = -ENOPROTOOPT;
 | 
							err = -ENOPROTOOPT;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue