Skip to content

Commit 9785e10

Browse files
ummakynesdavem330
authored andcommitted
netlink: kill netlink_set_nonroot
Replace netlink_set_nonroot by one new field `flags' in struct netlink_kernel_cfg that is passed to netlink_kernel_create. This patch also renames NL_NONROOT_* to NL_CFG_F_NONROOT_* since now the flags field in nl_table is generic (so we can add more flags if needed in the future). Also adjust all callers in the net-next tree to use these flags instead of netlink_set_nonroot. Signed-off-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 16fa9e1 commit 9785e10

File tree

6 files changed

+21
-25
lines changed

6 files changed

+21
-25
lines changed

include/linux/netlink.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,16 @@ struct netlink_skb_parms {
176176
extern void netlink_table_grab(void);
177177
extern void netlink_table_ungrab(void);
178178

179+
#define NL_CFG_F_NONROOT_RECV (1 << 0)
180+
#define NL_CFG_F_NONROOT_SEND (1 << 1)
181+
179182
/* optional Netlink kernel configuration parameters */
180183
struct netlink_kernel_cfg {
181184
unsigned int groups;
182185
void (*input)(struct sk_buff *skb);
183186
struct mutex *cb_mutex;
184187
void (*bind)(int group);
188+
unsigned int flags;
185189
};
186190

187191
extern struct sock *netlink_kernel_create(struct net *net, int unit,
@@ -260,11 +264,6 @@ extern int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
260264
const struct nlmsghdr *nlh,
261265
struct netlink_dump_control *control);
262266

263-
264-
#define NL_NONROOT_RECV 0x1
265-
#define NL_NONROOT_SEND 0x2
266-
extern void netlink_set_nonroot(int protocol, unsigned flag);
267-
268267
#endif /* __KERNEL__ */
269268

270269
#endif /* __LINUX_NETLINK_H */

lib/kobject_uevent.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ static int uevent_net_init(struct net *net)
375375
struct uevent_sock *ue_sk;
376376
struct netlink_kernel_cfg cfg = {
377377
.groups = 1,
378+
.flags = NL_CFG_F_NONROOT_RECV,
378379
};
379380

380381
ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
@@ -422,7 +423,6 @@ static struct pernet_operations uevent_net_ops = {
422423

423424
static int __init kobject_uevent_init(void)
424425
{
425-
netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
426426
return register_pernet_subsys(&uevent_net_ops);
427427
}
428428

net/core/rtnetlink.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,7 @@ static int __net_init rtnetlink_net_init(struct net *net)
23812381
.groups = RTNLGRP_MAX,
23822382
.input = rtnetlink_rcv,
23832383
.cb_mutex = &rtnl_mutex,
2384+
.flags = NL_CFG_F_NONROOT_RECV,
23842385
};
23852386

23862387
sk = netlink_kernel_create(net, NETLINK_ROUTE, THIS_MODULE, &cfg);
@@ -2416,7 +2417,6 @@ void __init rtnetlink_init(void)
24162417
if (register_pernet_subsys(&rtnetlink_net_ops))
24172418
panic("rtnetlink_init: cannot initialize rtnetlink\n");
24182419

2419-
netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
24202420
register_netdevice_notifier(&rtnetlink_dev_notifier);
24212421

24222422
rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,

net/netlink/af_netlink.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct netlink_table {
121121
struct nl_pid_hash hash;
122122
struct hlist_head mc_list;
123123
struct listeners __rcu *listeners;
124-
unsigned int nl_nonroot;
124+
unsigned int flags;
125125
unsigned int groups;
126126
struct mutex *cb_mutex;
127127
struct module *module;
@@ -536,6 +536,8 @@ static int netlink_release(struct socket *sock)
536536
if (--nl_table[sk->sk_protocol].registered == 0) {
537537
kfree(nl_table[sk->sk_protocol].listeners);
538538
nl_table[sk->sk_protocol].module = NULL;
539+
nl_table[sk->sk_protocol].bind = NULL;
540+
nl_table[sk->sk_protocol].flags = 0;
539541
nl_table[sk->sk_protocol].registered = 0;
540542
}
541543
} else if (nlk->subscriptions) {
@@ -596,7 +598,7 @@ static int netlink_autobind(struct socket *sock)
596598

597599
static inline int netlink_capable(const struct socket *sock, unsigned int flag)
598600
{
599-
return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
601+
return (nl_table[sock->sk->sk_protocol].flags & flag) ||
600602
capable(CAP_NET_ADMIN);
601603
}
602604

@@ -659,7 +661,7 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
659661

660662
/* Only superuser is allowed to listen multicasts */
661663
if (nladdr->nl_groups) {
662-
if (!netlink_capable(sock, NL_NONROOT_RECV))
664+
if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
663665
return -EPERM;
664666
err = netlink_realloc_groups(sk);
665667
if (err)
@@ -721,7 +723,7 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr,
721723
return -EINVAL;
722724

723725
/* Only superuser is allowed to send multicasts */
724-
if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
726+
if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
725727
return -EPERM;
726728

727729
if (!nlk->pid)
@@ -1244,7 +1246,7 @@ static int netlink_setsockopt(struct socket *sock, int level, int optname,
12441246
break;
12451247
case NETLINK_ADD_MEMBERSHIP:
12461248
case NETLINK_DROP_MEMBERSHIP: {
1247-
if (!netlink_capable(sock, NL_NONROOT_RECV))
1249+
if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
12481250
return -EPERM;
12491251
err = netlink_realloc_groups(sk);
12501252
if (err)
@@ -1376,7 +1378,7 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
13761378
dst_group = ffs(addr->nl_groups);
13771379
err = -EPERM;
13781380
if ((dst_group || dst_pid) &&
1379-
!netlink_capable(sock, NL_NONROOT_SEND))
1381+
!netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
13801382
goto out;
13811383
} else {
13821384
dst_pid = nlk->dst_pid;
@@ -1580,7 +1582,10 @@ netlink_kernel_create(struct net *net, int unit,
15801582
rcu_assign_pointer(nl_table[unit].listeners, listeners);
15811583
nl_table[unit].cb_mutex = cb_mutex;
15821584
nl_table[unit].module = module;
1583-
nl_table[unit].bind = cfg ? cfg->bind : NULL;
1585+
if (cfg) {
1586+
nl_table[unit].bind = cfg->bind;
1587+
nl_table[unit].flags = cfg->flags;
1588+
}
15841589
nl_table[unit].registered = 1;
15851590
} else {
15861591
kfree(listeners);
@@ -1679,13 +1684,6 @@ void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
16791684
netlink_table_ungrab();
16801685
}
16811686

1682-
void netlink_set_nonroot(int protocol, unsigned int flags)
1683-
{
1684-
if ((unsigned int)protocol < MAX_LINKS)
1685-
nl_table[protocol].nl_nonroot = flags;
1686-
}
1687-
EXPORT_SYMBOL(netlink_set_nonroot);
1688-
16891687
struct nlmsghdr *
16901688
__nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, int type, int len, int flags)
16911689
{
@@ -2150,7 +2148,7 @@ static void __init netlink_add_usersock_entry(void)
21502148
rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
21512149
nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
21522150
nl_table[NETLINK_USERSOCK].registered = 1;
2153-
nl_table[NETLINK_USERSOCK].nl_nonroot = NL_NONROOT_SEND;
2151+
nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
21542152

21552153
netlink_table_ungrab();
21562154
}

net/netlink/genetlink.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ static int __net_init genl_pernet_init(struct net *net)
918918
struct netlink_kernel_cfg cfg = {
919919
.input = genl_rcv,
920920
.cb_mutex = &genl_mutex,
921+
.flags = NL_CFG_F_NONROOT_RECV,
921922
};
922923

923924
/* we'll bump the group number right afterwards */
@@ -955,8 +956,6 @@ static int __init genl_init(void)
955956
if (err < 0)
956957
goto problem;
957958

958-
netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
959-
960959
err = register_pernet_subsys(&genl_pernet_ops);
961960
if (err)
962961
goto problem;

security/selinux/netlink.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ static int __init selnl_init(void)
113113
{
114114
struct netlink_kernel_cfg cfg = {
115115
.groups = SELNLGRP_MAX,
116+
.flags = NL_CFG_F_NONROOT_RECV,
116117
};
117118

118119
selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX,
119120
THIS_MODULE, &cfg);
120121
if (selnl == NULL)
121122
panic("SELinux: Cannot create netlink socket.");
122-
netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
123123
return 0;
124124
}
125125

0 commit comments

Comments
 (0)