2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
15 * use nlk_sk, as sk->protinfo is on a diet 8)
16 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17 * - inc module use count of module that owns
18 * the kernel socket in case userspace opens
19 * socket of same protocol
20 * - remove all module support, since netlink is
21 * mandatory if CONFIG_NET=y these days
24 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/kernel.h>
28 #include <linux/init.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>
37 #include <linux/termios.h>
38 #include <linux/sockios.h>
39 #include <linux/net.h>
41 #include <linux/slab.h>
42 #include <asm/uaccess.h>
43 #include <linux/skbuff.h>
44 #include <linux/netdevice.h>
45 #include <linux/rtnetlink.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
48 #include <linux/notifier.h>
49 #include <linux/security.h>
50 #include <linux/jhash.h>
51 #include <linux/jiffies.h>
52 #include <linux/random.h>
53 #include <linux/bitops.h>
55 #include <linux/types.h>
56 #include <linux/audit.h>
57 #include <linux/mutex.h>
59 #include <net/net_namespace.h>
62 #include <net/netlink.h>
64 #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
65 #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
68 /* struct sock has to be the first member of netlink_sock */
76 unsigned long *groups;
78 wait_queue_head_t wait;
79 struct netlink_callback *cb;
80 struct mutex *cb_mutex;
81 struct mutex cb_def_mutex;
82 void (*netlink_rcv)(struct sk_buff *skb);
83 struct module *module;
88 unsigned long masks[0];
91 #define NETLINK_KERNEL_SOCKET 0x1
92 #define NETLINK_RECV_PKTINFO 0x2
93 #define NETLINK_BROADCAST_SEND_ERROR 0x4
94 #define NETLINK_RECV_NO_ENOBUFS 0x8
96 static inline struct netlink_sock *nlk_sk(struct sock *sk)
98 return container_of(sk, struct netlink_sock, sk);
101 static inline int netlink_is_kernel(struct sock *sk)
103 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
107 struct hlist_head *table;
108 unsigned long rehash_time;
113 unsigned int entries;
114 unsigned int max_shift;
119 struct netlink_table {
120 struct nl_pid_hash hash;
121 struct hlist_head mc_list;
122 struct listeners __rcu *listeners;
123 unsigned int nl_nonroot;
125 struct mutex *cb_mutex;
126 struct module *module;
130 static struct netlink_table *nl_table;
132 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
134 static int netlink_dump(struct sock *sk);
135 static void netlink_destroy_callback(struct netlink_callback *cb);
137 static DEFINE_RWLOCK(nl_table_lock);
138 static atomic_t nl_table_users = ATOMIC_INIT(0);
140 #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
142 static ATOMIC_NOTIFIER_HEAD(netlink_chain);
144 static inline u32 netlink_group_mask(u32 group)
146 return group ? 1 << (group - 1) : 0;
149 static inline struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
151 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
154 static void netlink_sock_destruct(struct sock *sk)
156 struct netlink_sock *nlk = nlk_sk(sk);
160 nlk->cb->done(nlk->cb);
161 netlink_destroy_callback(nlk->cb);
164 skb_queue_purge(&sk->sk_receive_queue);
166 if (!sock_flag(sk, SOCK_DEAD)) {
167 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
171 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
172 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
173 WARN_ON(nlk_sk(sk)->groups);
176 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
177 * SMP. Look, when several writers sleep and reader wakes them up, all but one
178 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
179 * this, _but_ remember, it adds useless work on UP machines.
182 void netlink_table_grab(void)
183 __acquires(nl_table_lock)
187 write_lock_irq(&nl_table_lock);
189 if (atomic_read(&nl_table_users)) {
190 DECLARE_WAITQUEUE(wait, current);
192 add_wait_queue_exclusive(&nl_table_wait, &wait);
194 set_current_state(TASK_UNINTERRUPTIBLE);
195 if (atomic_read(&nl_table_users) == 0)
197 write_unlock_irq(&nl_table_lock);
199 write_lock_irq(&nl_table_lock);
202 __set_current_state(TASK_RUNNING);
203 remove_wait_queue(&nl_table_wait, &wait);
207 void netlink_table_ungrab(void)
208 __releases(nl_table_lock)
210 write_unlock_irq(&nl_table_lock);
211 wake_up(&nl_table_wait);
215 netlink_lock_table(void)
217 /* read_lock() synchronizes us to netlink_table_grab */
219 read_lock(&nl_table_lock);
220 atomic_inc(&nl_table_users);
221 read_unlock(&nl_table_lock);
225 netlink_unlock_table(void)
227 if (atomic_dec_and_test(&nl_table_users))
228 wake_up(&nl_table_wait);
231 static struct sock *netlink_lookup(struct net *net, int protocol, u32 pid)
233 struct nl_pid_hash *hash = &nl_table[protocol].hash;
234 struct hlist_head *head;
236 struct hlist_node *node;
238 read_lock(&nl_table_lock);
239 head = nl_pid_hashfn(hash, pid);
240 sk_for_each(sk, node, head) {
241 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
248 read_unlock(&nl_table_lock);
252 static struct hlist_head *nl_pid_hash_zalloc(size_t size)
254 if (size <= PAGE_SIZE)
255 return kzalloc(size, GFP_ATOMIC);
257 return (struct hlist_head *)
258 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
262 static void nl_pid_hash_free(struct hlist_head *table, size_t size)
264 if (size <= PAGE_SIZE)
267 free_pages((unsigned long)table, get_order(size));
270 static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
272 unsigned int omask, mask, shift;
274 struct hlist_head *otable, *table;
277 omask = mask = hash->mask;
278 osize = size = (mask + 1) * sizeof(*table);
282 if (++shift > hash->max_shift)
288 table = nl_pid_hash_zalloc(size);
292 otable = hash->table;
296 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
298 for (i = 0; i <= omask; i++) {
300 struct hlist_node *node, *tmp;
302 sk_for_each_safe(sk, node, tmp, &otable[i])
303 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
306 nl_pid_hash_free(otable, osize);
307 hash->rehash_time = jiffies + 10 * 60 * HZ;
311 static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
313 int avg = hash->entries >> hash->shift;
315 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
318 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
319 nl_pid_hash_rehash(hash, 0);
326 static const struct proto_ops netlink_ops;
329 netlink_update_listeners(struct sock *sk)
331 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
332 struct hlist_node *node;
335 struct listeners *listeners;
337 listeners = nl_deref_protected(tbl->listeners);
341 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
343 sk_for_each_bound(sk, node, &tbl->mc_list) {
344 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
345 mask |= nlk_sk(sk)->groups[i];
347 listeners->masks[i] = mask;
349 /* this function is only called with the netlink table "grabbed", which
350 * makes sure updates are visible before bind or setsockopt return. */
353 static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
355 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
356 struct hlist_head *head;
357 int err = -EADDRINUSE;
359 struct hlist_node *node;
362 netlink_table_grab();
363 head = nl_pid_hashfn(hash, pid);
365 sk_for_each(osk, node, head) {
366 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
378 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
381 if (len && nl_pid_hash_dilute(hash, len))
382 head = nl_pid_hashfn(hash, pid);
384 nlk_sk(sk)->pid = pid;
385 sk_add_node(sk, head);
389 netlink_table_ungrab();
393 static void netlink_remove(struct sock *sk)
395 netlink_table_grab();
396 if (sk_del_node_init(sk))
397 nl_table[sk->sk_protocol].hash.entries--;
398 if (nlk_sk(sk)->subscriptions)
399 __sk_del_bind_node(sk);
400 netlink_table_ungrab();
403 static struct proto netlink_proto = {
405 .owner = THIS_MODULE,
406 .obj_size = sizeof(struct netlink_sock),
409 static int __netlink_create(struct net *net, struct socket *sock,
410 struct mutex *cb_mutex, int protocol)
413 struct netlink_sock *nlk;
415 sock->ops = &netlink_ops;
417 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
421 sock_init_data(sock, sk);
425 nlk->cb_mutex = cb_mutex;
427 nlk->cb_mutex = &nlk->cb_def_mutex;
428 mutex_init(nlk->cb_mutex);
430 init_waitqueue_head(&nlk->wait);
432 sk->sk_destruct = netlink_sock_destruct;
433 sk->sk_protocol = protocol;
437 static int netlink_create(struct net *net, struct socket *sock, int protocol,
440 struct module *module = NULL;
441 struct mutex *cb_mutex;
442 struct netlink_sock *nlk;
445 sock->state = SS_UNCONNECTED;
447 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
448 return -ESOCKTNOSUPPORT;
450 if (protocol < 0 || protocol >= MAX_LINKS)
451 return -EPROTONOSUPPORT;
453 netlink_lock_table();
454 #ifdef CONFIG_MODULES
455 if (!nl_table[protocol].registered) {
456 netlink_unlock_table();
457 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
458 netlink_lock_table();
461 if (nl_table[protocol].registered &&
462 try_module_get(nl_table[protocol].module))
463 module = nl_table[protocol].module;
465 err = -EPROTONOSUPPORT;
466 cb_mutex = nl_table[protocol].cb_mutex;
467 netlink_unlock_table();
472 err = __netlink_create(net, sock, cb_mutex, protocol);
477 sock_prot_inuse_add(net, &netlink_proto, 1);
480 nlk = nlk_sk(sock->sk);
481 nlk->module = module;
490 static int netlink_release(struct socket *sock)
492 struct sock *sk = sock->sk;
493 struct netlink_sock *nlk;
503 * OK. Socket is unlinked, any packets that arrive now
508 wake_up_interruptible_all(&nlk->wait);
510 skb_queue_purge(&sk->sk_write_queue);
513 struct netlink_notify n = {
515 .protocol = sk->sk_protocol,
518 atomic_notifier_call_chain(&netlink_chain,
519 NETLINK_URELEASE, &n);
522 module_put(nlk->module);
524 netlink_table_grab();
525 if (netlink_is_kernel(sk)) {
526 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
527 if (--nl_table[sk->sk_protocol].registered == 0) {
528 struct listeners *old;
530 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
531 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
533 nl_table[sk->sk_protocol].module = NULL;
534 nl_table[sk->sk_protocol].registered = 0;
536 } else if (nlk->subscriptions)
537 netlink_update_listeners(sk);
538 netlink_table_ungrab();
544 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
550 static int netlink_autobind(struct socket *sock)
552 struct sock *sk = sock->sk;
553 struct net *net = sock_net(sk);
554 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
555 struct hlist_head *head;
557 struct hlist_node *node;
558 s32 pid = task_tgid_vnr(current);
560 static s32 rover = -4097;
564 netlink_table_grab();
565 head = nl_pid_hashfn(hash, pid);
566 sk_for_each(osk, node, head) {
567 if (!net_eq(sock_net(osk), net))
569 if (nlk_sk(osk)->pid == pid) {
570 /* Bind collision, search negative pid values. */
574 netlink_table_ungrab();
578 netlink_table_ungrab();
580 err = netlink_insert(sk, net, pid);
581 if (err == -EADDRINUSE)
584 /* If 2 threads race to autobind, that is fine. */
591 static inline int netlink_capable(const struct socket *sock, unsigned int flag)
593 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
594 capable(CAP_NET_ADMIN);
598 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
600 struct netlink_sock *nlk = nlk_sk(sk);
602 if (nlk->subscriptions && !subscriptions)
603 __sk_del_bind_node(sk);
604 else if (!nlk->subscriptions && subscriptions)
605 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
606 nlk->subscriptions = subscriptions;
609 static int netlink_realloc_groups(struct sock *sk)
611 struct netlink_sock *nlk = nlk_sk(sk);
613 unsigned long *new_groups;
616 netlink_table_grab();
618 groups = nl_table[sk->sk_protocol].groups;
619 if (!nl_table[sk->sk_protocol].registered) {
624 if (nlk->ngroups >= groups)
627 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
628 if (new_groups == NULL) {
632 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
633 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
635 nlk->groups = new_groups;
636 nlk->ngroups = groups;
638 netlink_table_ungrab();
642 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
645 struct sock *sk = sock->sk;
646 struct net *net = sock_net(sk);
647 struct netlink_sock *nlk = nlk_sk(sk);
648 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
651 if (nladdr->nl_family != AF_NETLINK)
654 /* Only superuser is allowed to listen multicasts */
655 if (nladdr->nl_groups) {
656 if (!netlink_capable(sock, NL_NONROOT_RECV))
658 err = netlink_realloc_groups(sk);
664 if (nladdr->nl_pid != nlk->pid)
667 err = nladdr->nl_pid ?
668 netlink_insert(sk, net, nladdr->nl_pid) :
669 netlink_autobind(sock);
674 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
677 netlink_table_grab();
678 netlink_update_subscriptions(sk, nlk->subscriptions +
679 hweight32(nladdr->nl_groups) -
680 hweight32(nlk->groups[0]));
681 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
682 netlink_update_listeners(sk);
683 netlink_table_ungrab();
688 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
692 struct sock *sk = sock->sk;
693 struct netlink_sock *nlk = nlk_sk(sk);
694 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
696 if (alen < sizeof(addr->sa_family))
699 if (addr->sa_family == AF_UNSPEC) {
700 sk->sk_state = NETLINK_UNCONNECTED;
705 if (addr->sa_family != AF_NETLINK)
708 /* Only superuser is allowed to send multicasts */
709 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
713 err = netlink_autobind(sock);
716 sk->sk_state = NETLINK_CONNECTED;
717 nlk->dst_pid = nladdr->nl_pid;
718 nlk->dst_group = ffs(nladdr->nl_groups);
724 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
725 int *addr_len, int peer)
727 struct sock *sk = sock->sk;
728 struct netlink_sock *nlk = nlk_sk(sk);
729 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
731 nladdr->nl_family = AF_NETLINK;
733 *addr_len = sizeof(*nladdr);
736 nladdr->nl_pid = nlk->dst_pid;
737 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
739 nladdr->nl_pid = nlk->pid;
740 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
745 static void netlink_overrun(struct sock *sk)
747 struct netlink_sock *nlk = nlk_sk(sk);
749 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
750 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
751 sk->sk_err = ENOBUFS;
752 sk->sk_error_report(sk);
755 atomic_inc(&sk->sk_drops);
758 static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
761 struct netlink_sock *nlk;
763 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
765 return ERR_PTR(-ECONNREFUSED);
767 /* Don't bother queuing skb if kernel socket has no input function */
769 if (sock->sk_state == NETLINK_CONNECTED &&
770 nlk->dst_pid != nlk_sk(ssk)->pid) {
772 return ERR_PTR(-ECONNREFUSED);
777 struct sock *netlink_getsockbyfilp(struct file *filp)
779 struct inode *inode = filp->f_path.dentry->d_inode;
782 if (!S_ISSOCK(inode->i_mode))
783 return ERR_PTR(-ENOTSOCK);
785 sock = SOCKET_I(inode)->sk;
786 if (sock->sk_family != AF_NETLINK)
787 return ERR_PTR(-EINVAL);
794 * Attach a skb to a netlink socket.
795 * The caller must hold a reference to the destination socket. On error, the
796 * reference is dropped. The skb is not send to the destination, just all
797 * all error checks are performed and memory in the queue is reserved.
799 * < 0: error. skb freed, reference to sock dropped.
801 * 1: repeat lookup - reference dropped while waiting for socket memory.
803 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
804 long *timeo, struct sock *ssk)
806 struct netlink_sock *nlk;
810 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
811 test_bit(0, &nlk->state)) {
812 DECLARE_WAITQUEUE(wait, current);
814 if (!ssk || netlink_is_kernel(ssk))
821 __set_current_state(TASK_INTERRUPTIBLE);
822 add_wait_queue(&nlk->wait, &wait);
824 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
825 test_bit(0, &nlk->state)) &&
826 !sock_flag(sk, SOCK_DEAD))
827 *timeo = schedule_timeout(*timeo);
829 __set_current_state(TASK_RUNNING);
830 remove_wait_queue(&nlk->wait, &wait);
833 if (signal_pending(current)) {
835 return sock_intr_errno(*timeo);
839 skb_set_owner_r(skb, sk);
843 static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
847 skb_queue_tail(&sk->sk_receive_queue, skb);
848 sk->sk_data_ready(sk, len);
852 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
854 int len = __netlink_sendskb(sk, skb);
860 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
866 static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
872 delta = skb->end - skb->tail;
873 if (delta * 2 < skb->truesize)
876 if (skb_shared(skb)) {
877 struct sk_buff *nskb = skb_clone(skb, allocation);
884 if (!pskb_expand_head(skb, 0, -delta, allocation))
885 skb->truesize -= delta;
890 static void netlink_rcv_wake(struct sock *sk)
892 struct netlink_sock *nlk = nlk_sk(sk);
894 if (skb_queue_empty(&sk->sk_receive_queue))
895 clear_bit(0, &nlk->state);
896 if (!test_bit(0, &nlk->state))
897 wake_up_interruptible(&nlk->wait);
900 static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
903 struct netlink_sock *nlk = nlk_sk(sk);
906 if (nlk->netlink_rcv != NULL) {
908 skb_set_owner_r(skb, sk);
909 nlk->netlink_rcv(skb);
916 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
917 u32 pid, int nonblock)
923 skb = netlink_trim(skb, gfp_any());
925 timeo = sock_sndtimeo(ssk, nonblock);
927 sk = netlink_getsockbypid(ssk, pid);
932 if (netlink_is_kernel(sk))
933 return netlink_unicast_kernel(sk, skb);
935 if (sk_filter(sk, skb)) {
942 err = netlink_attachskb(sk, skb, &timeo, ssk);
948 return netlink_sendskb(sk, skb);
950 EXPORT_SYMBOL(netlink_unicast);
952 int netlink_has_listeners(struct sock *sk, unsigned int group)
955 struct listeners *listeners;
957 BUG_ON(!netlink_is_kernel(sk));
960 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
962 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
963 res = test_bit(group - 1, listeners->masks);
969 EXPORT_SYMBOL_GPL(netlink_has_listeners);
971 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
973 struct netlink_sock *nlk = nlk_sk(sk);
975 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
976 !test_bit(0, &nlk->state)) {
977 skb_set_owner_r(skb, sk);
978 __netlink_sendskb(sk, skb);
979 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
984 struct netlink_broadcast_data {
985 struct sock *exclude_sk;
990 int delivery_failure;
994 struct sk_buff *skb, *skb2;
995 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
999 static int do_one_broadcast(struct sock *sk,
1000 struct netlink_broadcast_data *p)
1002 struct netlink_sock *nlk = nlk_sk(sk);
1005 if (p->exclude_sk == sk)
1008 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1009 !test_bit(p->group - 1, nlk->groups))
1012 if (!net_eq(sock_net(sk), p->net))
1016 netlink_overrun(sk);
1021 if (p->skb2 == NULL) {
1022 if (skb_shared(p->skb)) {
1023 p->skb2 = skb_clone(p->skb, p->allocation);
1025 p->skb2 = skb_get(p->skb);
1027 * skb ownership may have been set when
1028 * delivered to a previous socket.
1030 skb_orphan(p->skb2);
1033 if (p->skb2 == NULL) {
1034 netlink_overrun(sk);
1035 /* Clone failed. Notify ALL listeners. */
1037 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1038 p->delivery_failure = 1;
1039 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1042 } else if (sk_filter(sk, p->skb2)) {
1045 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1046 netlink_overrun(sk);
1047 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1048 p->delivery_failure = 1;
1050 p->congested |= val;
1060 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 pid,
1061 u32 group, gfp_t allocation,
1062 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1065 struct net *net = sock_net(ssk);
1066 struct netlink_broadcast_data info;
1067 struct hlist_node *node;
1070 skb = netlink_trim(skb, allocation);
1072 info.exclude_sk = ssk;
1077 info.delivery_failure = 0;
1080 info.allocation = allocation;
1083 info.tx_filter = filter;
1084 info.tx_data = filter_data;
1086 /* While we sleep in clone, do not allow to change socket list */
1088 netlink_lock_table();
1090 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1091 do_one_broadcast(sk, &info);
1095 netlink_unlock_table();
1097 if (info.delivery_failure) {
1098 kfree_skb(info.skb2);
1101 consume_skb(info.skb2);
1103 if (info.delivered) {
1104 if (info.congested && (allocation & __GFP_WAIT))
1110 EXPORT_SYMBOL(netlink_broadcast_filtered);
1112 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
1113 u32 group, gfp_t allocation)
1115 return netlink_broadcast_filtered(ssk, skb, pid, group, allocation,
1118 EXPORT_SYMBOL(netlink_broadcast);
1120 struct netlink_set_err_data {
1121 struct sock *exclude_sk;
1127 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1129 struct netlink_sock *nlk = nlk_sk(sk);
1132 if (sk == p->exclude_sk)
1135 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
1138 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1139 !test_bit(p->group - 1, nlk->groups))
1142 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1147 sk->sk_err = p->code;
1148 sk->sk_error_report(sk);
1154 * netlink_set_err - report error to broadcast listeners
1155 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1156 * @pid: the PID of a process that we want to skip (if any)
1157 * @groups: the broadcast group that will notice the error
1158 * @code: error code, must be negative (as usual in kernelspace)
1160 * This function returns the number of broadcast listeners that have set the
1161 * NETLINK_RECV_NO_ENOBUFS socket option.
1163 int netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
1165 struct netlink_set_err_data info;
1166 struct hlist_node *node;
1170 info.exclude_sk = ssk;
1173 /* sk->sk_err wants a positive error value */
1176 read_lock(&nl_table_lock);
1178 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1179 ret += do_one_set_err(sk, &info);
1181 read_unlock(&nl_table_lock);
1184 EXPORT_SYMBOL(netlink_set_err);
1186 /* must be called with netlink table grabbed */
1187 static void netlink_update_socket_mc(struct netlink_sock *nlk,
1191 int old, new = !!is_new, subscriptions;
1193 old = test_bit(group - 1, nlk->groups);
1194 subscriptions = nlk->subscriptions - old + new;
1196 __set_bit(group - 1, nlk->groups);
1198 __clear_bit(group - 1, nlk->groups);
1199 netlink_update_subscriptions(&nlk->sk, subscriptions);
1200 netlink_update_listeners(&nlk->sk);
1203 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1204 char __user *optval, unsigned int optlen)
1206 struct sock *sk = sock->sk;
1207 struct netlink_sock *nlk = nlk_sk(sk);
1208 unsigned int val = 0;
1211 if (level != SOL_NETLINK)
1212 return -ENOPROTOOPT;
1214 if (optlen >= sizeof(int) &&
1215 get_user(val, (unsigned int __user *)optval))
1219 case NETLINK_PKTINFO:
1221 nlk->flags |= NETLINK_RECV_PKTINFO;
1223 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1226 case NETLINK_ADD_MEMBERSHIP:
1227 case NETLINK_DROP_MEMBERSHIP: {
1228 if (!netlink_capable(sock, NL_NONROOT_RECV))
1230 err = netlink_realloc_groups(sk);
1233 if (!val || val - 1 >= nlk->ngroups)
1235 netlink_table_grab();
1236 netlink_update_socket_mc(nlk, val,
1237 optname == NETLINK_ADD_MEMBERSHIP);
1238 netlink_table_ungrab();
1242 case NETLINK_BROADCAST_ERROR:
1244 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1246 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1249 case NETLINK_NO_ENOBUFS:
1251 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1252 clear_bit(0, &nlk->state);
1253 wake_up_interruptible(&nlk->wait);
1255 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
1264 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1265 char __user *optval, int __user *optlen)
1267 struct sock *sk = sock->sk;
1268 struct netlink_sock *nlk = nlk_sk(sk);
1271 if (level != SOL_NETLINK)
1272 return -ENOPROTOOPT;
1274 if (get_user(len, optlen))
1280 case NETLINK_PKTINFO:
1281 if (len < sizeof(int))
1284 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
1285 if (put_user(len, optlen) ||
1286 put_user(val, optval))
1290 case NETLINK_BROADCAST_ERROR:
1291 if (len < sizeof(int))
1294 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1295 if (put_user(len, optlen) ||
1296 put_user(val, optval))
1300 case NETLINK_NO_ENOBUFS:
1301 if (len < sizeof(int))
1304 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1305 if (put_user(len, optlen) ||
1306 put_user(val, optval))
1316 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1318 struct nl_pktinfo info;
1320 info.group = NETLINK_CB(skb).dst_group;
1321 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1324 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1325 struct msghdr *msg, size_t len)
1327 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1328 struct sock *sk = sock->sk;
1329 struct netlink_sock *nlk = nlk_sk(sk);
1330 struct sockaddr_nl *addr = msg->msg_name;
1333 struct sk_buff *skb;
1335 struct scm_cookie scm;
1337 if (msg->msg_flags&MSG_OOB)
1340 if (NULL == siocb->scm)
1343 err = scm_send(sock, msg, siocb->scm);
1347 if (msg->msg_namelen) {
1349 if (addr->nl_family != AF_NETLINK)
1351 dst_pid = addr->nl_pid;
1352 dst_group = ffs(addr->nl_groups);
1354 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
1357 dst_pid = nlk->dst_pid;
1358 dst_group = nlk->dst_group;
1362 err = netlink_autobind(sock);
1368 if (len > sk->sk_sndbuf - 32)
1371 skb = alloc_skb(len, GFP_KERNEL);
1375 NETLINK_CB(skb).pid = nlk->pid;
1376 NETLINK_CB(skb).dst_group = dst_group;
1377 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1380 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1385 err = security_netlink_send(sk, skb);
1392 atomic_inc(&skb->users);
1393 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
1395 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1398 scm_destroy(siocb->scm);
1402 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1403 struct msghdr *msg, size_t len,
1406 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1407 struct scm_cookie scm;
1408 struct sock *sk = sock->sk;
1409 struct netlink_sock *nlk = nlk_sk(sk);
1410 int noblock = flags&MSG_DONTWAIT;
1412 struct sk_buff *skb, *data_skb;
1420 skb = skb_recv_datagram(sk, flags, noblock, &err);
1426 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1427 if (unlikely(skb_shinfo(skb)->frag_list)) {
1429 * If this skb has a frag_list, then here that means that we
1430 * will have to use the frag_list skb's data for compat tasks
1431 * and the regular skb's data for normal (non-compat) tasks.
1433 * If we need to send the compat skb, assign it to the
1434 * 'data_skb' variable so that it will be used below for data
1435 * copying. We keep 'skb' for everything else, including
1436 * freeing both later.
1438 if (flags & MSG_CMSG_COMPAT)
1439 data_skb = skb_shinfo(skb)->frag_list;
1443 msg->msg_namelen = 0;
1445 copied = data_skb->len;
1447 msg->msg_flags |= MSG_TRUNC;
1451 skb_reset_transport_header(data_skb);
1452 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
1454 if (msg->msg_name) {
1455 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
1456 addr->nl_family = AF_NETLINK;
1458 addr->nl_pid = NETLINK_CB(skb).pid;
1459 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1460 msg->msg_namelen = sizeof(*addr);
1463 if (nlk->flags & NETLINK_RECV_PKTINFO)
1464 netlink_cmsg_recv_pktinfo(msg, skb);
1466 if (NULL == siocb->scm) {
1467 memset(&scm, 0, sizeof(scm));
1470 siocb->scm->creds = *NETLINK_CREDS(skb);
1471 if (flags & MSG_TRUNC)
1472 copied = data_skb->len;
1474 skb_free_datagram(sk, skb);
1476 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1477 ret = netlink_dump(sk);
1480 sk->sk_error_report(sk);
1484 scm_recv(sock, msg, siocb->scm, flags);
1486 netlink_rcv_wake(sk);
1487 return err ? : copied;
1490 static void netlink_data_ready(struct sock *sk, int len)
1496 * We export these functions to other modules. They provide a
1497 * complete set of kernel non-blocking support for message
1502 netlink_kernel_create(struct net *net, int unit, unsigned int groups,
1503 void (*input)(struct sk_buff *skb),
1504 struct mutex *cb_mutex, struct module *module)
1506 struct socket *sock;
1508 struct netlink_sock *nlk;
1509 struct listeners *listeners = NULL;
1513 if (unit < 0 || unit >= MAX_LINKS)
1516 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1520 * We have to just have a reference on the net from sk, but don't
1521 * get_net it. Besides, we cannot get and then put the net here.
1522 * So we create one inside init_net and the move it to net.
1525 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1526 goto out_sock_release_nosk;
1529 sk_change_net(sk, net);
1534 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
1536 goto out_sock_release;
1538 sk->sk_data_ready = netlink_data_ready;
1540 nlk_sk(sk)->netlink_rcv = input;
1542 if (netlink_insert(sk, net, 0))
1543 goto out_sock_release;
1546 nlk->flags |= NETLINK_KERNEL_SOCKET;
1548 netlink_table_grab();
1549 if (!nl_table[unit].registered) {
1550 nl_table[unit].groups = groups;
1551 rcu_assign_pointer(nl_table[unit].listeners, listeners);
1552 nl_table[unit].cb_mutex = cb_mutex;
1553 nl_table[unit].module = module;
1554 nl_table[unit].registered = 1;
1557 nl_table[unit].registered++;
1559 netlink_table_ungrab();
1564 netlink_kernel_release(sk);
1567 out_sock_release_nosk:
1571 EXPORT_SYMBOL(netlink_kernel_create);
1575 netlink_kernel_release(struct sock *sk)
1577 sk_release_kernel(sk);
1579 EXPORT_SYMBOL(netlink_kernel_release);
1581 int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
1583 struct listeners *new, *old;
1584 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1589 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1590 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1593 old = nl_deref_protected(tbl->listeners);
1594 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1595 rcu_assign_pointer(tbl->listeners, new);
1597 kfree_rcu(old, rcu);
1599 tbl->groups = groups;
1605 * netlink_change_ngroups - change number of multicast groups
1607 * This changes the number of multicast groups that are available
1608 * on a certain netlink family. Note that it is not possible to
1609 * change the number of groups to below 32. Also note that it does
1610 * not implicitly call netlink_clear_multicast_users() when the
1611 * number of groups is reduced.
1613 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1614 * @groups: The new number of groups.
1616 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1620 netlink_table_grab();
1621 err = __netlink_change_ngroups(sk, groups);
1622 netlink_table_ungrab();
1627 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1630 struct hlist_node *node;
1631 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1633 sk_for_each_bound(sk, node, &tbl->mc_list)
1634 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1638 * netlink_clear_multicast_users - kick off multicast listeners
1640 * This function removes all listeners from the given group.
1641 * @ksk: The kernel netlink socket, as returned by
1642 * netlink_kernel_create().
1643 * @group: The multicast group to clear.
1645 void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1647 netlink_table_grab();
1648 __netlink_clear_multicast_users(ksk, group);
1649 netlink_table_ungrab();
1652 void netlink_set_nonroot(int protocol, unsigned int flags)
1654 if ((unsigned int)protocol < MAX_LINKS)
1655 nl_table[protocol].nl_nonroot = flags;
1657 EXPORT_SYMBOL(netlink_set_nonroot);
1659 static void netlink_destroy_callback(struct netlink_callback *cb)
1666 __nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, int type, int len, int flags)
1668 struct nlmsghdr *nlh;
1669 int size = NLMSG_LENGTH(len);
1671 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1672 nlh->nlmsg_type = type;
1673 nlh->nlmsg_len = size;
1674 nlh->nlmsg_flags = flags;
1675 nlh->nlmsg_pid = pid;
1676 nlh->nlmsg_seq = seq;
1677 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
1678 memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size);
1681 EXPORT_SYMBOL(__nlmsg_put);
1684 * It looks a bit ugly.
1685 * It would be better to create kernel thread.
1688 static int netlink_dump(struct sock *sk)
1690 struct netlink_sock *nlk = nlk_sk(sk);
1691 struct netlink_callback *cb;
1692 struct sk_buff *skb = NULL;
1693 struct nlmsghdr *nlh;
1694 int len, err = -ENOBUFS;
1697 mutex_lock(nlk->cb_mutex);
1705 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1707 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1711 len = cb->dump(skb, cb);
1714 mutex_unlock(nlk->cb_mutex);
1716 if (sk_filter(sk, skb))
1719 __netlink_sendskb(sk, skb);
1723 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1727 nl_dump_check_consistent(cb, nlh);
1729 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1731 if (sk_filter(sk, skb))
1734 __netlink_sendskb(sk, skb);
1739 mutex_unlock(nlk->cb_mutex);
1741 netlink_destroy_callback(cb);
1745 mutex_unlock(nlk->cb_mutex);
1750 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1751 const struct nlmsghdr *nlh,
1752 struct netlink_dump_control *control)
1754 struct netlink_callback *cb;
1756 struct netlink_sock *nlk;
1759 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
1763 cb->dump = control->dump;
1764 cb->done = control->done;
1766 cb->data = control->data;
1767 cb->min_dump_alloc = control->min_dump_alloc;
1768 atomic_inc(&skb->users);
1771 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
1773 netlink_destroy_callback(cb);
1774 return -ECONNREFUSED;
1777 /* A dump is in progress... */
1778 mutex_lock(nlk->cb_mutex);
1780 mutex_unlock(nlk->cb_mutex);
1781 netlink_destroy_callback(cb);
1786 mutex_unlock(nlk->cb_mutex);
1788 ret = netlink_dump(sk);
1795 /* We successfully started a dump, by returning -EINTR we
1796 * signal not to send ACK even if it was requested.
1800 EXPORT_SYMBOL(netlink_dump_start);
1802 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1804 struct sk_buff *skb;
1805 struct nlmsghdr *rep;
1806 struct nlmsgerr *errmsg;
1807 size_t payload = sizeof(*errmsg);
1809 /* error messages get the original request appened */
1811 payload += nlmsg_len(nlh);
1813 skb = nlmsg_new(payload, GFP_KERNEL);
1817 sk = netlink_lookup(sock_net(in_skb->sk),
1818 in_skb->sk->sk_protocol,
1819 NETLINK_CB(in_skb).pid);
1821 sk->sk_err = ENOBUFS;
1822 sk->sk_error_report(sk);
1828 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
1829 NLMSG_ERROR, payload, 0);
1830 errmsg = nlmsg_data(rep);
1831 errmsg->error = err;
1832 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
1833 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1835 EXPORT_SYMBOL(netlink_ack);
1837 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
1840 struct nlmsghdr *nlh;
1843 while (skb->len >= nlmsg_total_size(0)) {
1846 nlh = nlmsg_hdr(skb);
1849 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
1852 /* Only requests are handled by the kernel */
1853 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1856 /* Skip control messages */
1857 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
1865 if (nlh->nlmsg_flags & NLM_F_ACK || err)
1866 netlink_ack(skb, nlh, err);
1869 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
1870 if (msglen > skb->len)
1872 skb_pull(skb, msglen);
1877 EXPORT_SYMBOL(netlink_rcv_skb);
1880 * nlmsg_notify - send a notification netlink message
1881 * @sk: netlink socket to use
1882 * @skb: notification message
1883 * @pid: destination netlink pid for reports or 0
1884 * @group: destination multicast group or 0
1885 * @report: 1 to report back, 0 to disable
1886 * @flags: allocation flags
1888 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
1889 unsigned int group, int report, gfp_t flags)
1894 int exclude_pid = 0;
1897 atomic_inc(&skb->users);
1901 /* errors reported via destination sk->sk_err, but propagate
1902 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
1903 err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
1909 err2 = nlmsg_unicast(sk, skb, pid);
1910 if (!err || err == -ESRCH)
1916 EXPORT_SYMBOL(nlmsg_notify);
1918 #ifdef CONFIG_PROC_FS
1919 struct nl_seq_iter {
1920 struct seq_net_private p;
1925 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1927 struct nl_seq_iter *iter = seq->private;
1930 struct hlist_node *node;
1933 for (i = 0; i < MAX_LINKS; i++) {
1934 struct nl_pid_hash *hash = &nl_table[i].hash;
1936 for (j = 0; j <= hash->mask; j++) {
1937 sk_for_each(s, node, &hash->table[j]) {
1938 if (sock_net(s) != seq_file_net(seq))
1952 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1953 __acquires(nl_table_lock)
1955 read_lock(&nl_table_lock);
1956 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1959 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1962 struct nl_seq_iter *iter;
1967 if (v == SEQ_START_TOKEN)
1968 return netlink_seq_socket_idx(seq, 0);
1970 iter = seq->private;
1974 } while (s && sock_net(s) != seq_file_net(seq));
1979 j = iter->hash_idx + 1;
1982 struct nl_pid_hash *hash = &nl_table[i].hash;
1984 for (; j <= hash->mask; j++) {
1985 s = sk_head(&hash->table[j]);
1986 while (s && sock_net(s) != seq_file_net(seq))
1996 } while (++i < MAX_LINKS);
2001 static void netlink_seq_stop(struct seq_file *seq, void *v)
2002 __releases(nl_table_lock)
2004 read_unlock(&nl_table_lock);
2008 static int netlink_seq_show(struct seq_file *seq, void *v)
2010 if (v == SEQ_START_TOKEN)
2012 "sk Eth Pid Groups "
2013 "Rmem Wmem Dump Locks Drops Inode\n");
2016 struct netlink_sock *nlk = nlk_sk(s);
2018 seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
2022 nlk->groups ? (u32)nlk->groups[0] : 0,
2023 sk_rmem_alloc_get(s),
2024 sk_wmem_alloc_get(s),
2026 atomic_read(&s->sk_refcnt),
2027 atomic_read(&s->sk_drops),
2035 static const struct seq_operations netlink_seq_ops = {
2036 .start = netlink_seq_start,
2037 .next = netlink_seq_next,
2038 .stop = netlink_seq_stop,
2039 .show = netlink_seq_show,
2043 static int netlink_seq_open(struct inode *inode, struct file *file)
2045 return seq_open_net(inode, file, &netlink_seq_ops,
2046 sizeof(struct nl_seq_iter));
2049 static const struct file_operations netlink_seq_fops = {
2050 .owner = THIS_MODULE,
2051 .open = netlink_seq_open,
2053 .llseek = seq_lseek,
2054 .release = seq_release_net,
2059 int netlink_register_notifier(struct notifier_block *nb)
2061 return atomic_notifier_chain_register(&netlink_chain, nb);
2063 EXPORT_SYMBOL(netlink_register_notifier);
2065 int netlink_unregister_notifier(struct notifier_block *nb)
2067 return atomic_notifier_chain_unregister(&netlink_chain, nb);
2069 EXPORT_SYMBOL(netlink_unregister_notifier);
2071 static const struct proto_ops netlink_ops = {
2072 .family = PF_NETLINK,
2073 .owner = THIS_MODULE,
2074 .release = netlink_release,
2075 .bind = netlink_bind,
2076 .connect = netlink_connect,
2077 .socketpair = sock_no_socketpair,
2078 .accept = sock_no_accept,
2079 .getname = netlink_getname,
2080 .poll = datagram_poll,
2081 .ioctl = sock_no_ioctl,
2082 .listen = sock_no_listen,
2083 .shutdown = sock_no_shutdown,
2084 .setsockopt = netlink_setsockopt,
2085 .getsockopt = netlink_getsockopt,
2086 .sendmsg = netlink_sendmsg,
2087 .recvmsg = netlink_recvmsg,
2088 .mmap = sock_no_mmap,
2089 .sendpage = sock_no_sendpage,
2092 static const struct net_proto_family netlink_family_ops = {
2093 .family = PF_NETLINK,
2094 .create = netlink_create,
2095 .owner = THIS_MODULE, /* for consistency 8) */
2098 static int __net_init netlink_net_init(struct net *net)
2100 #ifdef CONFIG_PROC_FS
2101 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
2107 static void __net_exit netlink_net_exit(struct net *net)
2109 #ifdef CONFIG_PROC_FS
2110 proc_net_remove(net, "netlink");
2114 static void __init netlink_add_usersock_entry(void)
2116 struct listeners *listeners;
2119 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2121 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2123 netlink_table_grab();
2125 nl_table[NETLINK_USERSOCK].groups = groups;
2126 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
2127 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2128 nl_table[NETLINK_USERSOCK].registered = 1;
2130 netlink_table_ungrab();
2133 static struct pernet_operations __net_initdata netlink_net_ops = {
2134 .init = netlink_net_init,
2135 .exit = netlink_net_exit,
2138 static int __init netlink_proto_init(void)
2140 struct sk_buff *dummy_skb;
2142 unsigned long limit;
2144 int err = proto_register(&netlink_proto, 0);
2149 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
2151 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
2155 if (totalram_pages >= (128 * 1024))
2156 limit = totalram_pages >> (21 - PAGE_SHIFT);
2158 limit = totalram_pages >> (23 - PAGE_SHIFT);
2160 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2161 limit = (1UL << order) / sizeof(struct hlist_head);
2162 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
2164 for (i = 0; i < MAX_LINKS; i++) {
2165 struct nl_pid_hash *hash = &nl_table[i].hash;
2167 hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
2170 nl_pid_hash_free(nl_table[i].hash.table,
2171 1 * sizeof(*hash->table));
2175 hash->max_shift = order;
2178 hash->rehash_time = jiffies;
2181 netlink_add_usersock_entry();
2183 sock_register(&netlink_family_ops);
2184 register_pernet_subsys(&netlink_net_ops);
2185 /* The netlink device handler may be needed early. */
2190 panic("netlink_init: Cannot allocate nl_table\n");
2193 core_initcall(netlink_proto_init);