24 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/kernel.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/socket.h>
36 #include <linux/fcntl.h>
39 #include <linux/net.h>
41 #include <linux/slab.h>
42 #include <asm/uaccess.h>
44 #include <linux/netdevice.h>
45 #include <linux/rtnetlink.h>
52 #include <linux/random.h>
53 #include <linux/bitops.h>
55 #include <linux/types.h>
56 #include <linux/audit.h>
64 #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
65 #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
92 #define NETLINK_KERNEL_SOCKET 0x1
93 #define NETLINK_RECV_PKTINFO 0x2
94 #define NETLINK_BROADCAST_SEND_ERROR 0x4
95 #define NETLINK_RECV_NO_ENOBUFS 0x8
102 static inline int netlink_is_kernel(
struct sock *
sk)
136 static int netlink_dump(
struct sock *
sk);
141 #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
147 return group ? 1 << (group - 1) : 0;
152 return &hash->
table[jhash_1word(portid, hash->
rnd) & hash->
mask];
167 static void netlink_sock_destruct(
struct sock *
sk)
173 nlk->
cb->done(nlk->
cb);
175 module_put(nlk->
cb->module);
176 netlink_destroy_callback(nlk->
cb);
230 netlink_lock_table(
void)
240 netlink_unlock_table(
void)
254 head = nl_portid_hashfn(hash, portid);
256 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
285 static int nl_portid_hash_rehash(
struct nl_portid_hash *hash,
int grow)
287 unsigned int omask,
mask, shift;
292 omask = mask = hash->
mask;
293 osize = size = (mask + 1) *
sizeof(*table);
303 table = nl_portid_hash_zalloc(size);
307 otable = hash->
table;
313 for (i = 0; i <= omask; i++) {
318 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
321 nl_portid_hash_free(otable, osize);
326 static inline int nl_portid_hash_dilute(
struct nl_portid_hash *hash,
int len)
330 if (
unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
334 nl_portid_hash_rehash(hash, 0);
341 static const struct proto_ops netlink_ops;
344 netlink_update_listeners(
struct sock *sk)
360 mask |= nlk_sk(sk)->groups[
i];
368 static int netlink_insert(
struct sock *sk,
struct net *net,
u32 portid)
378 head = nl_portid_hashfn(hash, portid);
381 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
389 if (nlk_sk(sk)->portid)
396 if (len && nl_portid_hash_dilute(hash, len))
397 head = nl_portid_hashfn(hash, portid);
399 nlk_sk(sk)->portid = portid;
400 sk_add_node(sk, head);
408 static void netlink_remove(
struct sock *sk)
411 if (sk_del_node_init(sk))
413 if (nlk_sk(sk)->subscriptions)
414 __sk_del_bind_node(sk);
418 static struct proto netlink_proto = {
424 static int __netlink_create(
struct net *net,
struct socket *
sock,
430 sock->
ops = &netlink_ops;
452 static int netlink_create(
struct net *net,
struct socket *sock,
int protocol,
456 struct mutex *cb_mutex;
469 netlink_lock_table();
470 #ifdef CONFIG_MODULES
471 if (!nl_table[protocol].registered) {
472 netlink_unlock_table();
473 request_module(
"net-pf-%d-proto-%d",
PF_NETLINK, protocol);
474 netlink_lock_table();
477 if (nl_table[protocol].registered &&
478 try_module_get(nl_table[protocol].module))
484 netlink_unlock_table();
489 err = __netlink_create(net, sock, cb_mutex, protocol);
494 sock_prot_inuse_add(net, &netlink_proto, 1);
497 nlk = nlk_sk(sock->
sk);
508 static int netlink_release(
struct socket *sock)
510 struct sock *sk = sock->
sk;
537 NETLINK_URELEASE, &n);
543 if (netlink_is_kernel(sk)) {
546 struct listeners *old;
557 netlink_update_listeners(sk);
565 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
571 static int netlink_autobind(
struct socket *sock)
573 struct sock *sk = sock->
sk;
574 struct net *net = sock_net(sk);
581 static s32 rover = -4097;
586 head = nl_portid_hashfn(hash, portid);
588 if (!net_eq(sock_net(osk), net))
590 if (nlk_sk(osk)->portid == portid) {
601 err = netlink_insert(sk, net, portid);
612 static inline int netlink_capable(
const struct socket *sock,
unsigned int flag)
614 return (nl_table[sock->
sk->sk_protocol].
flags & flag) ||
619 netlink_update_subscriptions(
struct sock *sk,
unsigned int subscriptions)
624 __sk_del_bind_node(sk);
630 static int netlink_realloc_groups(
struct sock *sk)
634 unsigned long *new_groups;
649 if (new_groups ==
NULL) {
666 struct sock *sk = sock->
sk;
667 struct net *net = sock_net(sk);
679 err = netlink_realloc_groups(sk);
689 netlink_insert(sk, net, nladdr->
nl_pid) :
690 netlink_autobind(sock);
703 netlink_update_listeners(sk);
709 for (i=0; i<nlk->
ngroups; i++) {
718 static int netlink_connect(
struct socket *sock,
struct sockaddr *addr,
722 struct sock *sk = sock->
sk;
743 err = netlink_autobind(sock);
754 static int netlink_getname(
struct socket *sock,
struct sockaddr *addr,
755 int *addr_len,
int peer)
757 struct sock *sk = sock->
sk;
763 *addr_len =
sizeof(*nladdr);
775 static void netlink_overrun(
struct sock *sk)
788 static struct sock *netlink_getsockbyportid(
struct sock *ssk,
u32 portid)
793 sock = netlink_lookup(sock_net(ssk), ssk->
sk_protocol, portid);
815 sock = SOCKET_I(inode)->sk;
834 long *timeo,
struct sock *ssk)
844 if (!ssk || netlink_is_kernel(ssk))
865 return sock_intr_errno(*timeo);
869 skb_set_owner_r(skb, sk);
873 static int __netlink_sendskb(
struct sock *sk,
struct sk_buff *
skb)
884 int len = __netlink_sendskb(sk, skb);
906 if (skb_shared(skb)) {
920 static void netlink_rcv_wake(
struct sock *sk)
930 static int netlink_unicast_kernel(
struct sock *sk,
struct sk_buff *skb,
939 skb_set_owner_r(skb, sk);
951 u32 portid,
int nonblock)
957 skb = netlink_trim(skb, gfp_any());
959 timeo = sock_sndtimeo(ssk, nonblock);
961 sk = netlink_getsockbyportid(ssk, portid);
966 if (netlink_is_kernel(sk))
967 return netlink_unicast_kernel(sk, skb, ssk);
989 struct listeners *listeners;
991 BUG_ON(!netlink_is_kernel(sk));
1005 static int netlink_broadcast_deliver(
struct sock *sk,
struct sk_buff *skb)
1011 skb_set_owner_r(skb, sk);
1012 __netlink_sendskb(sk, skb);
1033 static int do_one_broadcast(
struct sock *sk,
1046 if (!net_eq(sock_net(sk), p->
net))
1050 netlink_overrun(sk);
1056 if (skb_shared(p->
skb)) {
1064 skb_orphan(p->
skb2);
1068 netlink_overrun(sk);
1079 }
else if ((val = netlink_broadcast_deliver(sk, p->
skb2)) < 0) {
1080 netlink_overrun(sk);
1099 struct net *net = sock_net(ssk);
1104 skb = netlink_trim(skb, allocation);
1122 netlink_lock_table();
1125 do_one_broadcast(sk, &info);
1129 netlink_unlock_table();
1169 if (!net_eq(sock_net(sk), sock_net(p->
exclude_sk)))
1204 info.exclude_sk = ssk;
1205 info.portid = portid;
1213 ret += do_one_set_err(sk, &
info);
1225 int old,
new = !!is_new, subscriptions;
1227 old =
test_bit(group - 1, nlk->groups);
1228 subscriptions = nlk->subscriptions - old +
new;
1233 netlink_update_subscriptions(&nlk->sk, subscriptions);
1234 netlink_update_listeners(&nlk->sk);
1237 static int netlink_setsockopt(
struct socket *sock,
int level,
int optname,
1238 char __user *optval,
unsigned int optlen)
1240 struct sock *sk = sock->
sk;
1242 unsigned int val = 0;
1248 if (optlen >=
sizeof(
int) &&
1249 get_user(val, (
unsigned int __user *)optval))
1264 err = netlink_realloc_groups(sk);
1267 if (!val || val - 1 >= nlk->
ngroups)
1270 netlink_update_socket_mc(nlk, val,
1303 static int netlink_getsockopt(
struct socket *sock,
int level,
int optname,
1304 char __user *optval,
int __user *optlen)
1306 struct sock *sk = sock->
sk;
1320 if (len <
sizeof(
int))
1330 if (len <
sizeof(
int))
1340 if (len <
sizeof(
int))
1355 static void netlink_cmsg_recv_pktinfo(
struct msghdr *
msg,
struct sk_buff *skb)
1364 struct msghdr *msg,
size_t len)
1366 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1367 struct sock *sk = sock->
sk;
1382 err = scm_send(sock, msg, siocb->
scm,
true);
1390 dst_portid = addr->
nl_pid;
1393 if ((dst_group || dst_portid) &&
1402 err = netlink_autobind(sock);
1438 scm_destroy(siocb->
scm);
1442 static int netlink_recvmsg(
struct kiocb *kiocb,
struct socket *sock,
1443 struct msghdr *msg,
size_t len,
1446 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1448 struct sock *sk = sock->
sk;
1466 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1467 if (
unlikely(skb_shinfo(skb)->frag_list)) {
1479 data_skb = skb_shinfo(skb)->frag_list;
1485 copied = data_skb->
len;
1491 skb_reset_transport_header(data_skb);
1504 netlink_cmsg_recv_pktinfo(msg, skb);
1507 memset(&scm, 0,
sizeof(scm));
1512 copied = data_skb->
len;
1517 ret = netlink_dump(sk);
1524 scm_recv(sock, msg, siocb->
scm, flags);
1526 netlink_rcv_wake(sk);
1527 return err ? : copied;
1530 static void netlink_data_ready(
struct sock *sk,
int len)
1548 struct listeners *listeners =
NULL;
1550 unsigned int groups;
1566 if (__netlink_create(&
init_net, sock, cb_mutex, unit) < 0)
1567 goto out_sock_release_nosk;
1570 sk_change_net(sk, net);
1572 if (!cfg || cfg->
groups < 32)
1579 goto out_sock_release;
1582 if (cfg && cfg->
input)
1583 nlk_sk(sk)->netlink_rcv = cfg->
input;
1585 if (netlink_insert(sk, net, 0))
1586 goto out_sock_release;
1592 if (!nl_table[unit].registered) {
1614 out_sock_release_nosk:
1629 struct listeners *
new, *old;
1680 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1710 if (!__builtin_constant_p(size) ||
NLMSG_ALIGN(size) - size != 0)
1721 static int netlink_dump(
struct sock *sk)
1744 len = cb->
dump(skb, cb);
1752 __netlink_sendskb(sk, skb);
1760 nl_dump_check_consistent(cb, nlh);
1762 memcpy(nlmsg_data(nlh), &len,
sizeof(len));
1767 __netlink_sendskb(sk, skb);
1775 netlink_consume_callback(cb);
1808 netlink_destroy_callback(cb);
1817 netlink_destroy_callback(cb);
1822 if (!try_module_get(cb->
module)) {
1824 netlink_destroy_callback(cb);
1832 ret = netlink_dump(sk);
1851 size_t payload =
sizeof(*errmsg);
1855 payload += nlmsg_len(nlh);
1861 sk = netlink_lookup(sock_net(in_skb->
sk),
1862 in_skb->
sk->sk_protocol,
1874 errmsg = nlmsg_data(rep);
1887 while (skb->
len >= nlmsg_total_size(0)) {
1890 nlh = nlmsg_hdr(skb);
1914 if (msglen > skb->
len)
1938 int exclude_portid = 0;
1942 exclude_portid = portid;
1947 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
1953 err2 = nlmsg_unicast(sk, skb, portid);
1954 if (!err || err == -
ESRCH)
1962 #ifdef CONFIG_PROC_FS
1963 struct nl_seq_iter {
1969 static struct sock *netlink_seq_socket_idx(
struct seq_file *seq, loff_t
pos)
1971 struct nl_seq_iter *iter = seq->
private;
1980 for (j = 0; j <= hash->
mask; j++) {
1982 if (sock_net(s) != seq_file_net(seq))
1996 static void *netlink_seq_start(
struct seq_file *seq, loff_t *pos)
2000 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) :
SEQ_START_TOKEN;
2003 static void *netlink_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
2006 struct nl_seq_iter *iter;
2012 return netlink_seq_socket_idx(seq, 0);
2018 }
while (s && sock_net(s) != seq_file_net(seq));
2023 j = iter->hash_idx + 1;
2028 for (; j <= hash->
mask; j++) {
2029 s = sk_head(&hash->
table[j]);
2030 while (s && sock_net(s) != seq_file_net(seq))
2040 }
while (++i < MAX_LINKS);
2045 static void netlink_seq_stop(
struct seq_file *seq,
void *v)
2052 static int netlink_seq_show(
struct seq_file *seq,
void *v)
2056 "sk Eth Pid Groups "
2057 "Rmem Wmem Dump Locks Drops Inode\n");
2062 seq_printf(seq,
"%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
2067 sk_rmem_alloc_get(s),
2068 sk_wmem_alloc_get(s),
2080 .
start = netlink_seq_start,
2081 .next = netlink_seq_next,
2082 .stop = netlink_seq_stop,
2083 .show = netlink_seq_show,
2090 sizeof(
struct nl_seq_iter));
2095 .open = netlink_seq_open,
2115 static const struct proto_ops netlink_ops = {
2118 .release = netlink_release,
2119 .bind = netlink_bind,
2120 .connect = netlink_connect,
2123 .getname = netlink_getname,
2128 .setsockopt = netlink_setsockopt,
2129 .getsockopt = netlink_getsockopt,
2130 .sendmsg = netlink_sendmsg,
2131 .recvmsg = netlink_recvmsg,
2138 .create = netlink_create,
2142 static int __net_init netlink_net_init(
struct net *net)
2144 #ifdef CONFIG_PROC_FS
2151 static void __net_exit netlink_net_exit(
struct net *net)
2153 #ifdef CONFIG_PROC_FS
2158 static void __init netlink_add_usersock_entry(
void)
2160 struct listeners *listeners;
2165 panic(
"netlink_add_usersock_entry: Cannot allocate listeners\n");
2179 .init = netlink_net_init,
2180 .exit = netlink_net_exit,
2183 static int __init netlink_proto_init(
void)
2187 unsigned long limit;
2196 nl_table = kcalloc(MAX_LINKS,
sizeof(*nl_table),
GFP_KERNEL);
2200 if (totalram_pages >= (128 * 1024))
2205 order = get_bitmask_order(limit) - 1 +
PAGE_SHIFT;
2207 order = get_bitmask_order(
min(limit, (
unsigned long)
UINT_MAX)) - 1;
2212 hash->
table = nl_portid_hash_zalloc(1 *
sizeof(*hash->
table));
2215 nl_portid_hash_free(nl_table[i].hash.
table,
2216 1 *
sizeof(*hash->
table));
2226 netlink_add_usersock_entry();
2235 panic(
"netlink_init: Cannot allocate nl_table\n");