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 void (*netlink_bind)(int group);
84 struct module *module;
89 unsigned long masks[0];
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
97 static inline struct netlink_sock *nlk_sk(struct sock *sk)
99 return container_of(sk, struct netlink_sock, sk);
102 static inline int netlink_is_kernel(struct sock *sk)
104 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
107 struct nl_portid_hash {
108 struct hlist_head *table;
109 unsigned long rehash_time;
114 unsigned int entries;
115 unsigned int max_shift;
120 struct netlink_table {
121 struct nl_portid_hash hash;
122 struct hlist_head mc_list;
123 struct listeners __rcu *listeners;
126 struct mutex *cb_mutex;
127 struct module *module;
128 void (*bind)(int group);
132 static struct netlink_table *nl_table;
134 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
136 static int netlink_dump(struct sock *sk);
138 static DEFINE_RWLOCK(nl_table_lock);
139 static atomic_t nl_table_users = ATOMIC_INIT(0);
141 static ATOMIC_NOTIFIER_HEAD(netlink_chain);
143 static inline u32 netlink_group_mask(u32 group)
145 return group ? 1 << (group - 1) : 0;
148 static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
150 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
153 static void netlink_destroy_callback(struct netlink_callback *cb)
159 static void netlink_consume_callback(struct netlink_callback *cb)
161 consume_skb(cb->skb);
165 static void netlink_sock_destruct(struct sock *sk)
167 struct netlink_sock *nlk = nlk_sk(sk);
171 nlk->cb->done(nlk->cb);
173 module_put(nlk->cb->module);
174 netlink_destroy_callback(nlk->cb);
177 skb_queue_purge(&sk->sk_receive_queue);
179 if (!sock_flag(sk, SOCK_DEAD)) {
180 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
184 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
185 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
186 WARN_ON(nlk_sk(sk)->groups);
189 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
190 * SMP. Look, when several writers sleep and reader wakes them up, all but one
191 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
192 * this, _but_ remember, it adds useless work on UP machines.
195 void netlink_table_grab(void)
196 __acquires(nl_table_lock)
200 write_lock_irq(&nl_table_lock);
202 if (atomic_read(&nl_table_users)) {
203 DECLARE_WAITQUEUE(wait, current);
205 add_wait_queue_exclusive(&nl_table_wait, &wait);
207 set_current_state(TASK_UNINTERRUPTIBLE);
208 if (atomic_read(&nl_table_users) == 0)
210 write_unlock_irq(&nl_table_lock);
212 write_lock_irq(&nl_table_lock);
215 __set_current_state(TASK_RUNNING);
216 remove_wait_queue(&nl_table_wait, &wait);
220 void netlink_table_ungrab(void)
221 __releases(nl_table_lock)
223 write_unlock_irq(&nl_table_lock);
224 wake_up(&nl_table_wait);
228 netlink_lock_table(void)
230 /* read_lock() synchronizes us to netlink_table_grab */
232 read_lock(&nl_table_lock);
233 atomic_inc(&nl_table_users);
234 read_unlock(&nl_table_lock);
238 netlink_unlock_table(void)
240 if (atomic_dec_and_test(&nl_table_users))
241 wake_up(&nl_table_wait);
244 static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
246 struct nl_portid_hash *hash = &nl_table[protocol].hash;
247 struct hlist_head *head;
249 struct hlist_node *node;
251 read_lock(&nl_table_lock);
252 head = nl_portid_hashfn(hash, portid);
253 sk_for_each(sk, node, head) {
254 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
261 read_unlock(&nl_table_lock);
265 static struct hlist_head *nl_portid_hash_zalloc(size_t size)
267 if (size <= PAGE_SIZE)
268 return kzalloc(size, GFP_ATOMIC);
270 return (struct hlist_head *)
271 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
275 static void nl_portid_hash_free(struct hlist_head *table, size_t size)
277 if (size <= PAGE_SIZE)
280 free_pages((unsigned long)table, get_order(size));
283 static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
285 unsigned int omask, mask, shift;
287 struct hlist_head *otable, *table;
290 omask = mask = hash->mask;
291 osize = size = (mask + 1) * sizeof(*table);
295 if (++shift > hash->max_shift)
301 table = nl_portid_hash_zalloc(size);
305 otable = hash->table;
309 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
311 for (i = 0; i <= omask; i++) {
313 struct hlist_node *node, *tmp;
315 sk_for_each_safe(sk, node, tmp, &otable[i])
316 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
319 nl_portid_hash_free(otable, osize);
320 hash->rehash_time = jiffies + 10 * 60 * HZ;
324 static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
326 int avg = hash->entries >> hash->shift;
328 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
331 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
332 nl_portid_hash_rehash(hash, 0);
339 static const struct proto_ops netlink_ops;
342 netlink_update_listeners(struct sock *sk)
344 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
345 struct hlist_node *node;
349 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
351 sk_for_each_bound(sk, node, &tbl->mc_list) {
352 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
353 mask |= nlk_sk(sk)->groups[i];
355 tbl->listeners->masks[i] = mask;
357 /* this function is only called with the netlink table "grabbed", which
358 * makes sure updates are visible before bind or setsockopt return. */
361 static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
363 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
364 struct hlist_head *head;
365 int err = -EADDRINUSE;
367 struct hlist_node *node;
370 netlink_table_grab();
371 head = nl_portid_hashfn(hash, portid);
373 sk_for_each(osk, node, head) {
374 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
382 if (nlk_sk(sk)->portid)
386 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
389 if (len && nl_portid_hash_dilute(hash, len))
390 head = nl_portid_hashfn(hash, portid);
392 nlk_sk(sk)->portid = portid;
393 sk_add_node(sk, head);
397 netlink_table_ungrab();
401 static void netlink_remove(struct sock *sk)
403 netlink_table_grab();
404 if (sk_del_node_init(sk))
405 nl_table[sk->sk_protocol].hash.entries--;
406 if (nlk_sk(sk)->subscriptions)
407 __sk_del_bind_node(sk);
408 netlink_table_ungrab();
411 static struct proto netlink_proto = {
413 .owner = THIS_MODULE,
414 .obj_size = sizeof(struct netlink_sock),
417 static int __netlink_create(struct net *net, struct socket *sock,
418 struct mutex *cb_mutex, int protocol)
421 struct netlink_sock *nlk;
423 sock->ops = &netlink_ops;
425 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
429 sock_init_data(sock, sk);
433 nlk->cb_mutex = cb_mutex;
435 nlk->cb_mutex = &nlk->cb_def_mutex;
436 mutex_init(nlk->cb_mutex);
438 init_waitqueue_head(&nlk->wait);
440 sk->sk_destruct = netlink_sock_destruct;
441 sk->sk_protocol = protocol;
445 static int netlink_create(struct net *net, struct socket *sock, int protocol,
448 struct module *module = NULL;
449 struct mutex *cb_mutex;
450 struct netlink_sock *nlk;
451 void (*bind)(int group);
454 sock->state = SS_UNCONNECTED;
456 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
457 return -ESOCKTNOSUPPORT;
459 if (protocol < 0 || protocol >= MAX_LINKS)
460 return -EPROTONOSUPPORT;
462 netlink_lock_table();
463 #ifdef CONFIG_MODULES
464 if (!nl_table[protocol].registered) {
465 netlink_unlock_table();
466 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
467 netlink_lock_table();
470 if (nl_table[protocol].registered &&
471 try_module_get(nl_table[protocol].module))
472 module = nl_table[protocol].module;
474 err = -EPROTONOSUPPORT;
475 cb_mutex = nl_table[protocol].cb_mutex;
476 bind = nl_table[protocol].bind;
477 netlink_unlock_table();
482 err = __netlink_create(net, sock, cb_mutex, protocol);
487 sock_prot_inuse_add(net, &netlink_proto, 1);
490 nlk = nlk_sk(sock->sk);
491 nlk->module = module;
492 nlk->netlink_bind = bind;
501 static int netlink_release(struct socket *sock)
503 struct sock *sk = sock->sk;
504 struct netlink_sock *nlk;
514 * OK. Socket is unlinked, any packets that arrive now
519 wake_up_interruptible_all(&nlk->wait);
521 skb_queue_purge(&sk->sk_write_queue);
524 struct netlink_notify n = {
526 .protocol = sk->sk_protocol,
527 .portid = nlk->portid,
529 atomic_notifier_call_chain(&netlink_chain,
530 NETLINK_URELEASE, &n);
533 module_put(nlk->module);
535 netlink_table_grab();
536 if (netlink_is_kernel(sk)) {
537 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
538 if (--nl_table[sk->sk_protocol].registered == 0) {
539 kfree(nl_table[sk->sk_protocol].listeners);
540 nl_table[sk->sk_protocol].module = NULL;
541 nl_table[sk->sk_protocol].bind = NULL;
542 nl_table[sk->sk_protocol].flags = 0;
543 nl_table[sk->sk_protocol].registered = 0;
545 } else if (nlk->subscriptions) {
546 netlink_update_listeners(sk);
548 netlink_table_ungrab();
554 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
560 static int netlink_autobind(struct socket *sock)
562 struct sock *sk = sock->sk;
563 struct net *net = sock_net(sk);
564 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
565 struct hlist_head *head;
567 struct hlist_node *node;
568 s32 portid = task_tgid_vnr(current);
570 static s32 rover = -4097;
574 netlink_table_grab();
575 head = nl_portid_hashfn(hash, portid);
576 sk_for_each(osk, node, head) {
577 if (!net_eq(sock_net(osk), net))
579 if (nlk_sk(osk)->portid == portid) {
580 /* Bind collision, search negative portid values. */
584 netlink_table_ungrab();
588 netlink_table_ungrab();
590 err = netlink_insert(sk, net, portid);
591 if (err == -EADDRINUSE)
594 /* If 2 threads race to autobind, that is fine. */
601 static inline int netlink_capable(const struct socket *sock, unsigned int flag)
603 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
604 capable(CAP_NET_ADMIN);
608 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
610 struct netlink_sock *nlk = nlk_sk(sk);
612 if (nlk->subscriptions && !subscriptions)
613 __sk_del_bind_node(sk);
614 else if (!nlk->subscriptions && subscriptions)
615 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
616 nlk->subscriptions = subscriptions;
619 static int netlink_realloc_groups(struct sock *sk)
621 struct netlink_sock *nlk = nlk_sk(sk);
623 unsigned long *new_groups;
626 netlink_table_grab();
628 groups = nl_table[sk->sk_protocol].groups;
629 if (!nl_table[sk->sk_protocol].registered) {
634 if (nlk->ngroups >= groups)
637 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
638 if (new_groups == NULL) {
642 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
643 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
645 nlk->groups = new_groups;
646 nlk->ngroups = groups;
648 netlink_table_ungrab();
652 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
655 struct sock *sk = sock->sk;
656 struct net *net = sock_net(sk);
657 struct netlink_sock *nlk = nlk_sk(sk);
658 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
661 if (nladdr->nl_family != AF_NETLINK)
664 /* Only superuser is allowed to listen multicasts */
665 if (nladdr->nl_groups) {
666 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
668 err = netlink_realloc_groups(sk);
674 if (nladdr->nl_pid != nlk->portid)
677 err = nladdr->nl_pid ?
678 netlink_insert(sk, net, nladdr->nl_pid) :
679 netlink_autobind(sock);
684 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
687 netlink_table_grab();
688 netlink_update_subscriptions(sk, nlk->subscriptions +
689 hweight32(nladdr->nl_groups) -
690 hweight32(nlk->groups[0]));
691 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
692 netlink_update_listeners(sk);
693 netlink_table_ungrab();
695 if (nlk->netlink_bind && nlk->groups[0]) {
698 for (i=0; i<nlk->ngroups; i++) {
699 if (test_bit(i, nlk->groups))
700 nlk->netlink_bind(i);
707 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
711 struct sock *sk = sock->sk;
712 struct netlink_sock *nlk = nlk_sk(sk);
713 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
715 if (alen < sizeof(addr->sa_family))
718 if (addr->sa_family == AF_UNSPEC) {
719 sk->sk_state = NETLINK_UNCONNECTED;
724 if (addr->sa_family != AF_NETLINK)
727 /* Only superuser is allowed to send multicasts */
728 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
732 err = netlink_autobind(sock);
735 sk->sk_state = NETLINK_CONNECTED;
736 nlk->dst_portid = nladdr->nl_pid;
737 nlk->dst_group = ffs(nladdr->nl_groups);
743 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
744 int *addr_len, int peer)
746 struct sock *sk = sock->sk;
747 struct netlink_sock *nlk = nlk_sk(sk);
748 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
750 nladdr->nl_family = AF_NETLINK;
752 *addr_len = sizeof(*nladdr);
755 nladdr->nl_pid = nlk->dst_portid;
756 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
758 nladdr->nl_pid = nlk->portid;
759 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
764 static void netlink_overrun(struct sock *sk)
766 struct netlink_sock *nlk = nlk_sk(sk);
768 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
769 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
770 sk->sk_err = ENOBUFS;
771 sk->sk_error_report(sk);
774 atomic_inc(&sk->sk_drops);
777 static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
780 struct netlink_sock *nlk;
782 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
784 return ERR_PTR(-ECONNREFUSED);
786 /* Don't bother queuing skb if kernel socket has no input function */
788 if (sock->sk_state == NETLINK_CONNECTED &&
789 nlk->dst_portid != nlk_sk(ssk)->portid) {
791 return ERR_PTR(-ECONNREFUSED);
796 struct sock *netlink_getsockbyfilp(struct file *filp)
798 struct inode *inode = filp->f_path.dentry->d_inode;
801 if (!S_ISSOCK(inode->i_mode))
802 return ERR_PTR(-ENOTSOCK);
804 sock = SOCKET_I(inode)->sk;
805 if (sock->sk_family != AF_NETLINK)
806 return ERR_PTR(-EINVAL);
813 * Attach a skb to a netlink socket.
814 * The caller must hold a reference to the destination socket. On error, the
815 * reference is dropped. The skb is not send to the destination, just all
816 * all error checks are performed and memory in the queue is reserved.
818 * < 0: error. skb freed, reference to sock dropped.
820 * 1: repeat lookup - reference dropped while waiting for socket memory.
822 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
823 long *timeo, struct sock *ssk)
825 struct netlink_sock *nlk;
829 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
830 test_bit(0, &nlk->state)) {
831 DECLARE_WAITQUEUE(wait, current);
833 if (!ssk || netlink_is_kernel(ssk))
840 __set_current_state(TASK_INTERRUPTIBLE);
841 add_wait_queue(&nlk->wait, &wait);
843 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
844 test_bit(0, &nlk->state)) &&
845 !sock_flag(sk, SOCK_DEAD))
846 *timeo = schedule_timeout(*timeo);
848 __set_current_state(TASK_RUNNING);
849 remove_wait_queue(&nlk->wait, &wait);
852 if (signal_pending(current)) {
854 return sock_intr_errno(*timeo);
858 skb_set_owner_r(skb, sk);
862 static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
866 skb_queue_tail(&sk->sk_receive_queue, skb);
867 sk->sk_data_ready(sk, len);
871 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
873 int len = __netlink_sendskb(sk, skb);
879 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
885 static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
891 delta = skb->end - skb->tail;
892 if (delta * 2 < skb->truesize)
895 if (skb_shared(skb)) {
896 struct sk_buff *nskb = skb_clone(skb, allocation);
903 if (!pskb_expand_head(skb, 0, -delta, allocation))
904 skb->truesize -= delta;
909 static void netlink_rcv_wake(struct sock *sk)
911 struct netlink_sock *nlk = nlk_sk(sk);
913 if (skb_queue_empty(&sk->sk_receive_queue))
914 clear_bit(0, &nlk->state);
915 if (!test_bit(0, &nlk->state))
916 wake_up_interruptible(&nlk->wait);
919 static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
923 struct netlink_sock *nlk = nlk_sk(sk);
926 if (nlk->netlink_rcv != NULL) {
928 skb_set_owner_r(skb, sk);
929 NETLINK_CB(skb).ssk = ssk;
930 nlk->netlink_rcv(skb);
939 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
940 u32 portid, int nonblock)
946 skb = netlink_trim(skb, gfp_any());
948 timeo = sock_sndtimeo(ssk, nonblock);
950 sk = netlink_getsockbyportid(ssk, portid);
955 if (netlink_is_kernel(sk))
956 return netlink_unicast_kernel(sk, skb, ssk);
958 if (sk_filter(sk, skb)) {
965 err = netlink_attachskb(sk, skb, &timeo, ssk);
971 return netlink_sendskb(sk, skb);
973 EXPORT_SYMBOL(netlink_unicast);
975 int netlink_has_listeners(struct sock *sk, unsigned int group)
978 struct listeners *listeners;
980 BUG_ON(!netlink_is_kernel(sk));
983 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
985 if (group - 1 < nl_table[sk->sk_protocol].groups)
986 res = test_bit(group - 1, listeners->masks);
992 EXPORT_SYMBOL_GPL(netlink_has_listeners);
994 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
996 struct netlink_sock *nlk = nlk_sk(sk);
998 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
999 !test_bit(0, &nlk->state)) {
1000 skb_set_owner_r(skb, sk);
1001 __netlink_sendskb(sk, skb);
1002 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1007 struct netlink_broadcast_data {
1008 struct sock *exclude_sk;
1013 int delivery_failure;
1017 struct sk_buff *skb, *skb2;
1018 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1022 static int do_one_broadcast(struct sock *sk,
1023 struct netlink_broadcast_data *p)
1025 struct netlink_sock *nlk = nlk_sk(sk);
1028 if (p->exclude_sk == sk)
1031 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1032 !test_bit(p->group - 1, nlk->groups))
1035 if (!net_eq(sock_net(sk), p->net))
1039 netlink_overrun(sk);
1044 if (p->skb2 == NULL) {
1045 if (skb_shared(p->skb)) {
1046 p->skb2 = skb_clone(p->skb, p->allocation);
1048 p->skb2 = skb_get(p->skb);
1050 * skb ownership may have been set when
1051 * delivered to a previous socket.
1053 skb_orphan(p->skb2);
1056 if (p->skb2 == NULL) {
1057 netlink_overrun(sk);
1058 /* Clone failed. Notify ALL listeners. */
1060 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1061 p->delivery_failure = 1;
1062 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1065 } else if (sk_filter(sk, p->skb2)) {
1068 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1069 netlink_overrun(sk);
1070 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1071 p->delivery_failure = 1;
1073 p->congested |= val;
1083 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
1084 u32 group, gfp_t allocation,
1085 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1088 struct net *net = sock_net(ssk);
1089 struct netlink_broadcast_data info;
1090 struct hlist_node *node;
1093 skb = netlink_trim(skb, allocation);
1095 info.exclude_sk = ssk;
1097 info.portid = portid;
1100 info.delivery_failure = 0;
1103 info.allocation = allocation;
1106 info.tx_filter = filter;
1107 info.tx_data = filter_data;
1109 /* While we sleep in clone, do not allow to change socket list */
1111 netlink_lock_table();
1113 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1114 do_one_broadcast(sk, &info);
1118 netlink_unlock_table();
1120 if (info.delivery_failure) {
1121 kfree_skb(info.skb2);
1124 consume_skb(info.skb2);
1126 if (info.delivered) {
1127 if (info.congested && (allocation & __GFP_WAIT))
1133 EXPORT_SYMBOL(netlink_broadcast_filtered);
1135 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
1136 u32 group, gfp_t allocation)
1138 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
1141 EXPORT_SYMBOL(netlink_broadcast);
1143 struct netlink_set_err_data {
1144 struct sock *exclude_sk;
1150 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1152 struct netlink_sock *nlk = nlk_sk(sk);
1155 if (sk == p->exclude_sk)
1158 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
1161 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1162 !test_bit(p->group - 1, nlk->groups))
1165 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1170 sk->sk_err = p->code;
1171 sk->sk_error_report(sk);
1177 * netlink_set_err - report error to broadcast listeners
1178 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1179 * @portid: the PORTID of a process that we want to skip (if any)
1180 * @groups: the broadcast group that will notice the error
1181 * @code: error code, must be negative (as usual in kernelspace)
1183 * This function returns the number of broadcast listeners that have set the
1184 * NETLINK_RECV_NO_ENOBUFS socket option.
1186 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1188 struct netlink_set_err_data info;
1189 struct hlist_node *node;
1193 info.exclude_sk = ssk;
1194 info.portid = portid;
1196 /* sk->sk_err wants a positive error value */
1199 read_lock(&nl_table_lock);
1201 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1202 ret += do_one_set_err(sk, &info);
1204 read_unlock(&nl_table_lock);
1207 EXPORT_SYMBOL(netlink_set_err);
1209 /* must be called with netlink table grabbed */
1210 static void netlink_update_socket_mc(struct netlink_sock *nlk,
1214 int old, new = !!is_new, subscriptions;
1216 old = test_bit(group - 1, nlk->groups);
1217 subscriptions = nlk->subscriptions - old + new;
1219 __set_bit(group - 1, nlk->groups);
1221 __clear_bit(group - 1, nlk->groups);
1222 netlink_update_subscriptions(&nlk->sk, subscriptions);
1223 netlink_update_listeners(&nlk->sk);
1226 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1227 char __user *optval, unsigned int optlen)
1229 struct sock *sk = sock->sk;
1230 struct netlink_sock *nlk = nlk_sk(sk);
1231 unsigned int val = 0;
1234 if (level != SOL_NETLINK)
1235 return -ENOPROTOOPT;
1237 if (optlen >= sizeof(int) &&
1238 get_user(val, (unsigned int __user *)optval))
1242 case NETLINK_PKTINFO:
1244 nlk->flags |= NETLINK_RECV_PKTINFO;
1246 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1249 case NETLINK_ADD_MEMBERSHIP:
1250 case NETLINK_DROP_MEMBERSHIP: {
1251 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
1253 err = netlink_realloc_groups(sk);
1256 if (!val || val - 1 >= nlk->ngroups)
1258 netlink_table_grab();
1259 netlink_update_socket_mc(nlk, val,
1260 optname == NETLINK_ADD_MEMBERSHIP);
1261 netlink_table_ungrab();
1263 if (nlk->netlink_bind)
1264 nlk->netlink_bind(val);
1269 case NETLINK_BROADCAST_ERROR:
1271 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1273 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1276 case NETLINK_NO_ENOBUFS:
1278 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1279 clear_bit(0, &nlk->state);
1280 wake_up_interruptible(&nlk->wait);
1282 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
1292 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1293 char __user *optval, int __user *optlen)
1295 struct sock *sk = sock->sk;
1296 struct netlink_sock *nlk = nlk_sk(sk);
1299 if (level != SOL_NETLINK)
1300 return -ENOPROTOOPT;
1302 if (get_user(len, optlen))
1308 case NETLINK_PKTINFO:
1309 if (len < sizeof(int))
1312 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
1313 if (put_user(len, optlen) ||
1314 put_user(val, optval))
1318 case NETLINK_BROADCAST_ERROR:
1319 if (len < sizeof(int))
1322 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1323 if (put_user(len, optlen) ||
1324 put_user(val, optval))
1328 case NETLINK_NO_ENOBUFS:
1329 if (len < sizeof(int))
1332 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1333 if (put_user(len, optlen) ||
1334 put_user(val, optval))
1344 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1346 struct nl_pktinfo info;
1348 info.group = NETLINK_CB(skb).dst_group;
1349 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1352 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1353 struct msghdr *msg, size_t len)
1355 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1356 struct sock *sk = sock->sk;
1357 struct netlink_sock *nlk = nlk_sk(sk);
1358 struct sockaddr_nl *addr = msg->msg_name;
1361 struct sk_buff *skb;
1363 struct scm_cookie scm;
1365 if (msg->msg_flags&MSG_OOB)
1368 if (NULL == siocb->scm)
1371 err = scm_send(sock, msg, siocb->scm, true);
1375 if (msg->msg_namelen) {
1377 if (addr->nl_family != AF_NETLINK)
1379 dst_portid = addr->nl_pid;
1380 dst_group = ffs(addr->nl_groups);
1382 if ((dst_group || dst_portid) &&
1383 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
1386 dst_portid = nlk->dst_portid;
1387 dst_group = nlk->dst_group;
1391 err = netlink_autobind(sock);
1397 if (len > sk->sk_sndbuf - 32)
1400 skb = alloc_skb(len, GFP_KERNEL);
1404 NETLINK_CB(skb).portid = nlk->portid;
1405 NETLINK_CB(skb).dst_group = dst_group;
1406 NETLINK_CB(skb).creds = siocb->scm->creds;
1409 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1414 err = security_netlink_send(sk, skb);
1421 atomic_inc(&skb->users);
1422 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1424 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1427 scm_destroy(siocb->scm);
1431 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1432 struct msghdr *msg, size_t len,
1435 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1436 struct scm_cookie scm;
1437 struct sock *sk = sock->sk;
1438 struct netlink_sock *nlk = nlk_sk(sk);
1439 int noblock = flags&MSG_DONTWAIT;
1441 struct sk_buff *skb, *data_skb;
1449 skb = skb_recv_datagram(sk, flags, noblock, &err);
1455 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1456 if (unlikely(skb_shinfo(skb)->frag_list)) {
1458 * If this skb has a frag_list, then here that means that we
1459 * will have to use the frag_list skb's data for compat tasks
1460 * and the regular skb's data for normal (non-compat) tasks.
1462 * If we need to send the compat skb, assign it to the
1463 * 'data_skb' variable so that it will be used below for data
1464 * copying. We keep 'skb' for everything else, including
1465 * freeing both later.
1467 if (flags & MSG_CMSG_COMPAT)
1468 data_skb = skb_shinfo(skb)->frag_list;
1472 msg->msg_namelen = 0;
1474 copied = data_skb->len;
1476 msg->msg_flags |= MSG_TRUNC;
1480 skb_reset_transport_header(data_skb);
1481 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
1483 if (msg->msg_name) {
1484 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
1485 addr->nl_family = AF_NETLINK;
1487 addr->nl_pid = NETLINK_CB(skb).portid;
1488 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1489 msg->msg_namelen = sizeof(*addr);
1492 if (nlk->flags & NETLINK_RECV_PKTINFO)
1493 netlink_cmsg_recv_pktinfo(msg, skb);
1495 if (NULL == siocb->scm) {
1496 memset(&scm, 0, sizeof(scm));
1499 siocb->scm->creds = *NETLINK_CREDS(skb);
1500 if (flags & MSG_TRUNC)
1501 copied = data_skb->len;
1503 skb_free_datagram(sk, skb);
1505 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1506 ret = netlink_dump(sk);
1509 sk->sk_error_report(sk);
1513 scm_recv(sock, msg, siocb->scm, flags);
1515 netlink_rcv_wake(sk);
1516 return err ? : copied;
1519 static void netlink_data_ready(struct sock *sk, int len)
1525 * We export these functions to other modules. They provide a
1526 * complete set of kernel non-blocking support for message
1531 __netlink_kernel_create(struct net *net, int unit, struct module *module,
1532 struct netlink_kernel_cfg *cfg)
1534 struct socket *sock;
1536 struct netlink_sock *nlk;
1537 struct listeners *listeners = NULL;
1538 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1539 unsigned int groups;
1543 if (unit < 0 || unit >= MAX_LINKS)
1546 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1550 * We have to just have a reference on the net from sk, but don't
1551 * get_net it. Besides, we cannot get and then put the net here.
1552 * So we create one inside init_net and the move it to net.
1555 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1556 goto out_sock_release_nosk;
1559 sk_change_net(sk, net);
1561 if (!cfg || cfg->groups < 32)
1564 groups = cfg->groups;
1566 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
1568 goto out_sock_release;
1570 sk->sk_data_ready = netlink_data_ready;
1571 if (cfg && cfg->input)
1572 nlk_sk(sk)->netlink_rcv = cfg->input;
1574 if (netlink_insert(sk, net, 0))
1575 goto out_sock_release;
1578 nlk->flags |= NETLINK_KERNEL_SOCKET;
1580 netlink_table_grab();
1581 if (!nl_table[unit].registered) {
1582 nl_table[unit].groups = groups;
1583 rcu_assign_pointer(nl_table[unit].listeners, listeners);
1584 nl_table[unit].cb_mutex = cb_mutex;
1585 nl_table[unit].module = module;
1587 nl_table[unit].bind = cfg->bind;
1588 nl_table[unit].flags = cfg->flags;
1590 nl_table[unit].registered = 1;
1593 nl_table[unit].registered++;
1595 netlink_table_ungrab();
1600 netlink_kernel_release(sk);
1603 out_sock_release_nosk:
1607 EXPORT_SYMBOL(__netlink_kernel_create);
1610 netlink_kernel_release(struct sock *sk)
1612 sk_release_kernel(sk);
1614 EXPORT_SYMBOL(netlink_kernel_release);
1616 int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
1618 struct listeners *new, *old;
1619 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1624 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1625 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1628 old = rcu_dereference_protected(tbl->listeners, 1);
1629 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1630 rcu_assign_pointer(tbl->listeners, new);
1632 kfree_rcu(old, rcu);
1634 tbl->groups = groups;
1640 * netlink_change_ngroups - change number of multicast groups
1642 * This changes the number of multicast groups that are available
1643 * on a certain netlink family. Note that it is not possible to
1644 * change the number of groups to below 32. Also note that it does
1645 * not implicitly call netlink_clear_multicast_users() when the
1646 * number of groups is reduced.
1648 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1649 * @groups: The new number of groups.
1651 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1655 netlink_table_grab();
1656 err = __netlink_change_ngroups(sk, groups);
1657 netlink_table_ungrab();
1662 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1665 struct hlist_node *node;
1666 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1668 sk_for_each_bound(sk, node, &tbl->mc_list)
1669 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1673 * netlink_clear_multicast_users - kick off multicast listeners
1675 * This function removes all listeners from the given group.
1676 * @ksk: The kernel netlink socket, as returned by
1677 * netlink_kernel_create().
1678 * @group: The multicast group to clear.
1680 void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1682 netlink_table_grab();
1683 __netlink_clear_multicast_users(ksk, group);
1684 netlink_table_ungrab();
1688 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
1690 struct nlmsghdr *nlh;
1691 int size = NLMSG_LENGTH(len);
1693 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1694 nlh->nlmsg_type = type;
1695 nlh->nlmsg_len = size;
1696 nlh->nlmsg_flags = flags;
1697 nlh->nlmsg_pid = portid;
1698 nlh->nlmsg_seq = seq;
1699 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
1700 memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size);
1703 EXPORT_SYMBOL(__nlmsg_put);
1706 * It looks a bit ugly.
1707 * It would be better to create kernel thread.
1710 static int netlink_dump(struct sock *sk)
1712 struct netlink_sock *nlk = nlk_sk(sk);
1713 struct netlink_callback *cb;
1714 struct sk_buff *skb = NULL;
1715 struct nlmsghdr *nlh;
1716 int len, err = -ENOBUFS;
1719 mutex_lock(nlk->cb_mutex);
1727 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1729 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1733 len = cb->dump(skb, cb);
1736 mutex_unlock(nlk->cb_mutex);
1738 if (sk_filter(sk, skb))
1741 __netlink_sendskb(sk, skb);
1745 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1749 nl_dump_check_consistent(cb, nlh);
1751 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1753 if (sk_filter(sk, skb))
1756 __netlink_sendskb(sk, skb);
1761 mutex_unlock(nlk->cb_mutex);
1763 module_put(cb->module);
1764 netlink_consume_callback(cb);
1768 mutex_unlock(nlk->cb_mutex);
1773 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1774 const struct nlmsghdr *nlh,
1775 struct netlink_dump_control *control)
1777 struct netlink_callback *cb;
1779 struct netlink_sock *nlk;
1782 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
1786 cb->dump = control->dump;
1787 cb->done = control->done;
1789 cb->data = control->data;
1790 cb->module = control->module;
1791 cb->min_dump_alloc = control->min_dump_alloc;
1792 atomic_inc(&skb->users);
1795 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
1797 netlink_destroy_callback(cb);
1798 return -ECONNREFUSED;
1802 mutex_lock(nlk->cb_mutex);
1803 /* A dump is in progress... */
1805 mutex_unlock(nlk->cb_mutex);
1806 netlink_destroy_callback(cb);
1810 /* add reference of module which cb->dump belongs to */
1811 if (!try_module_get(cb->module)) {
1812 mutex_unlock(nlk->cb_mutex);
1813 netlink_destroy_callback(cb);
1814 ret = -EPROTONOSUPPORT;
1819 mutex_unlock(nlk->cb_mutex);
1821 ret = netlink_dump(sk);
1828 /* We successfully started a dump, by returning -EINTR we
1829 * signal not to send ACK even if it was requested.
1833 EXPORT_SYMBOL(__netlink_dump_start);
1835 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1837 struct sk_buff *skb;
1838 struct nlmsghdr *rep;
1839 struct nlmsgerr *errmsg;
1840 size_t payload = sizeof(*errmsg);
1842 /* error messages get the original request appened */
1844 payload += nlmsg_len(nlh);
1846 skb = nlmsg_new(payload, GFP_KERNEL);
1850 sk = netlink_lookup(sock_net(in_skb->sk),
1851 in_skb->sk->sk_protocol,
1852 NETLINK_CB(in_skb).portid);
1854 sk->sk_err = ENOBUFS;
1855 sk->sk_error_report(sk);
1861 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
1862 NLMSG_ERROR, payload, 0);
1863 errmsg = nlmsg_data(rep);
1864 errmsg->error = err;
1865 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
1866 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
1868 EXPORT_SYMBOL(netlink_ack);
1870 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
1873 struct nlmsghdr *nlh;
1876 while (skb->len >= nlmsg_total_size(0)) {
1879 nlh = nlmsg_hdr(skb);
1882 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
1885 /* Only requests are handled by the kernel */
1886 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1889 /* Skip control messages */
1890 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
1898 if (nlh->nlmsg_flags & NLM_F_ACK || err)
1899 netlink_ack(skb, nlh, err);
1902 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
1903 if (msglen > skb->len)
1905 skb_pull(skb, msglen);
1910 EXPORT_SYMBOL(netlink_rcv_skb);
1913 * nlmsg_notify - send a notification netlink message
1914 * @sk: netlink socket to use
1915 * @skb: notification message
1916 * @portid: destination netlink portid for reports or 0
1917 * @group: destination multicast group or 0
1918 * @report: 1 to report back, 0 to disable
1919 * @flags: allocation flags
1921 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
1922 unsigned int group, int report, gfp_t flags)
1927 int exclude_portid = 0;
1930 atomic_inc(&skb->users);
1931 exclude_portid = portid;
1934 /* errors reported via destination sk->sk_err, but propagate
1935 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
1936 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
1942 err2 = nlmsg_unicast(sk, skb, portid);
1943 if (!err || err == -ESRCH)
1949 EXPORT_SYMBOL(nlmsg_notify);
1951 #ifdef CONFIG_PROC_FS
1952 struct nl_seq_iter {
1953 struct seq_net_private p;
1958 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1960 struct nl_seq_iter *iter = seq->private;
1963 struct hlist_node *node;
1966 for (i = 0; i < MAX_LINKS; i++) {
1967 struct nl_portid_hash *hash = &nl_table[i].hash;
1969 for (j = 0; j <= hash->mask; j++) {
1970 sk_for_each(s, node, &hash->table[j]) {
1971 if (sock_net(s) != seq_file_net(seq))
1985 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1986 __acquires(nl_table_lock)
1988 read_lock(&nl_table_lock);
1989 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1992 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1995 struct nl_seq_iter *iter;
2000 if (v == SEQ_START_TOKEN)
2001 return netlink_seq_socket_idx(seq, 0);
2003 iter = seq->private;
2007 } while (s && sock_net(s) != seq_file_net(seq));
2012 j = iter->hash_idx + 1;
2015 struct nl_portid_hash *hash = &nl_table[i].hash;
2017 for (; j <= hash->mask; j++) {
2018 s = sk_head(&hash->table[j]);
2019 while (s && sock_net(s) != seq_file_net(seq))
2029 } while (++i < MAX_LINKS);
2034 static void netlink_seq_stop(struct seq_file *seq, void *v)
2035 __releases(nl_table_lock)
2037 read_unlock(&nl_table_lock);
2041 static int netlink_seq_show(struct seq_file *seq, void *v)
2043 if (v == SEQ_START_TOKEN) {
2045 "sk Eth Pid Groups "
2046 "Rmem Wmem Dump Locks Drops Inode\n");
2049 struct netlink_sock *nlk = nlk_sk(s);
2051 seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
2055 nlk->groups ? (u32)nlk->groups[0] : 0,
2056 sk_rmem_alloc_get(s),
2057 sk_wmem_alloc_get(s),
2059 atomic_read(&s->sk_refcnt),
2060 atomic_read(&s->sk_drops),
2068 static const struct seq_operations netlink_seq_ops = {
2069 .start = netlink_seq_start,
2070 .next = netlink_seq_next,
2071 .stop = netlink_seq_stop,
2072 .show = netlink_seq_show,
2076 static int netlink_seq_open(struct inode *inode, struct file *file)
2078 return seq_open_net(inode, file, &netlink_seq_ops,
2079 sizeof(struct nl_seq_iter));
2082 static const struct file_operations netlink_seq_fops = {
2083 .owner = THIS_MODULE,
2084 .open = netlink_seq_open,
2086 .llseek = seq_lseek,
2087 .release = seq_release_net,
2092 int netlink_register_notifier(struct notifier_block *nb)
2094 return atomic_notifier_chain_register(&netlink_chain, nb);
2096 EXPORT_SYMBOL(netlink_register_notifier);
2098 int netlink_unregister_notifier(struct notifier_block *nb)
2100 return atomic_notifier_chain_unregister(&netlink_chain, nb);
2102 EXPORT_SYMBOL(netlink_unregister_notifier);
2104 static const struct proto_ops netlink_ops = {
2105 .family = PF_NETLINK,
2106 .owner = THIS_MODULE,
2107 .release = netlink_release,
2108 .bind = netlink_bind,
2109 .connect = netlink_connect,
2110 .socketpair = sock_no_socketpair,
2111 .accept = sock_no_accept,
2112 .getname = netlink_getname,
2113 .poll = datagram_poll,
2114 .ioctl = sock_no_ioctl,
2115 .listen = sock_no_listen,
2116 .shutdown = sock_no_shutdown,
2117 .setsockopt = netlink_setsockopt,
2118 .getsockopt = netlink_getsockopt,
2119 .sendmsg = netlink_sendmsg,
2120 .recvmsg = netlink_recvmsg,
2121 .mmap = sock_no_mmap,
2122 .sendpage = sock_no_sendpage,
2125 static const struct net_proto_family netlink_family_ops = {
2126 .family = PF_NETLINK,
2127 .create = netlink_create,
2128 .owner = THIS_MODULE, /* for consistency 8) */
2131 static int __net_init netlink_net_init(struct net *net)
2133 #ifdef CONFIG_PROC_FS
2134 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
2140 static void __net_exit netlink_net_exit(struct net *net)
2142 #ifdef CONFIG_PROC_FS
2143 proc_net_remove(net, "netlink");
2147 static void __init netlink_add_usersock_entry(void)
2149 struct listeners *listeners;
2152 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2154 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2156 netlink_table_grab();
2158 nl_table[NETLINK_USERSOCK].groups = groups;
2159 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
2160 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2161 nl_table[NETLINK_USERSOCK].registered = 1;
2162 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
2164 netlink_table_ungrab();
2167 static struct pernet_operations __net_initdata netlink_net_ops = {
2168 .init = netlink_net_init,
2169 .exit = netlink_net_exit,
2172 static int __init netlink_proto_init(void)
2174 struct sk_buff *dummy_skb;
2176 unsigned long limit;
2178 int err = proto_register(&netlink_proto, 0);
2183 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
2185 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
2189 if (totalram_pages >= (128 * 1024))
2190 limit = totalram_pages >> (21 - PAGE_SHIFT);
2192 limit = totalram_pages >> (23 - PAGE_SHIFT);
2194 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2195 limit = (1UL << order) / sizeof(struct hlist_head);
2196 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
2198 for (i = 0; i < MAX_LINKS; i++) {
2199 struct nl_portid_hash *hash = &nl_table[i].hash;
2201 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
2204 nl_portid_hash_free(nl_table[i].hash.table,
2205 1 * sizeof(*hash->table));
2209 hash->max_shift = order;
2212 hash->rehash_time = jiffies;
2215 netlink_add_usersock_entry();
2217 sock_register(&netlink_family_ops);
2218 register_pernet_subsys(&netlink_net_ops);
2219 /* The netlink device handler may be needed early. */
2224 panic("netlink_init: Cannot allocate nl_table\n");
2227 core_initcall(netlink_proto_init);