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 #include "af_netlink.h"
68 unsigned long masks[0];
72 #define NETLINK_CONGESTED 0x0
75 #define NETLINK_KERNEL_SOCKET 0x1
76 #define NETLINK_RECV_PKTINFO 0x2
77 #define NETLINK_BROADCAST_SEND_ERROR 0x4
78 #define NETLINK_RECV_NO_ENOBUFS 0x8
80 static inline int netlink_is_kernel(struct sock *sk)
82 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
85 struct netlink_table *nl_table;
86 EXPORT_SYMBOL_GPL(nl_table);
88 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
90 static int netlink_dump(struct sock *sk);
92 DEFINE_RWLOCK(nl_table_lock);
93 EXPORT_SYMBOL_GPL(nl_table_lock);
94 static atomic_t nl_table_users = ATOMIC_INIT(0);
96 #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
98 static ATOMIC_NOTIFIER_HEAD(netlink_chain);
100 static inline u32 netlink_group_mask(u32 group)
102 return group ? 1 << (group - 1) : 0;
105 static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
107 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
110 static void netlink_destroy_callback(struct netlink_callback *cb)
116 static void netlink_consume_callback(struct netlink_callback *cb)
118 consume_skb(cb->skb);
122 static void netlink_skb_destructor(struct sk_buff *skb)
127 static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
129 WARN_ON(skb->sk != NULL);
131 skb->destructor = netlink_skb_destructor;
132 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
133 sk_mem_charge(sk, skb->truesize);
136 static void netlink_sock_destruct(struct sock *sk)
138 struct netlink_sock *nlk = nlk_sk(sk);
142 nlk->cb->done(nlk->cb);
144 module_put(nlk->cb->module);
145 netlink_destroy_callback(nlk->cb);
148 skb_queue_purge(&sk->sk_receive_queue);
150 if (!sock_flag(sk, SOCK_DEAD)) {
151 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
155 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
156 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
157 WARN_ON(nlk_sk(sk)->groups);
160 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
161 * SMP. Look, when several writers sleep and reader wakes them up, all but one
162 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
163 * this, _but_ remember, it adds useless work on UP machines.
166 void netlink_table_grab(void)
167 __acquires(nl_table_lock)
171 write_lock_irq(&nl_table_lock);
173 if (atomic_read(&nl_table_users)) {
174 DECLARE_WAITQUEUE(wait, current);
176 add_wait_queue_exclusive(&nl_table_wait, &wait);
178 set_current_state(TASK_UNINTERRUPTIBLE);
179 if (atomic_read(&nl_table_users) == 0)
181 write_unlock_irq(&nl_table_lock);
183 write_lock_irq(&nl_table_lock);
186 __set_current_state(TASK_RUNNING);
187 remove_wait_queue(&nl_table_wait, &wait);
191 void netlink_table_ungrab(void)
192 __releases(nl_table_lock)
194 write_unlock_irq(&nl_table_lock);
195 wake_up(&nl_table_wait);
199 netlink_lock_table(void)
201 /* read_lock() synchronizes us to netlink_table_grab */
203 read_lock(&nl_table_lock);
204 atomic_inc(&nl_table_users);
205 read_unlock(&nl_table_lock);
209 netlink_unlock_table(void)
211 if (atomic_dec_and_test(&nl_table_users))
212 wake_up(&nl_table_wait);
215 static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
217 struct nl_portid_hash *hash = &nl_table[protocol].hash;
218 struct hlist_head *head;
221 read_lock(&nl_table_lock);
222 head = nl_portid_hashfn(hash, portid);
223 sk_for_each(sk, head) {
224 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
231 read_unlock(&nl_table_lock);
235 static struct hlist_head *nl_portid_hash_zalloc(size_t size)
237 if (size <= PAGE_SIZE)
238 return kzalloc(size, GFP_ATOMIC);
240 return (struct hlist_head *)
241 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
245 static void nl_portid_hash_free(struct hlist_head *table, size_t size)
247 if (size <= PAGE_SIZE)
250 free_pages((unsigned long)table, get_order(size));
253 static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
255 unsigned int omask, mask, shift;
257 struct hlist_head *otable, *table;
260 omask = mask = hash->mask;
261 osize = size = (mask + 1) * sizeof(*table);
265 if (++shift > hash->max_shift)
271 table = nl_portid_hash_zalloc(size);
275 otable = hash->table;
279 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
281 for (i = 0; i <= omask; i++) {
283 struct hlist_node *tmp;
285 sk_for_each_safe(sk, tmp, &otable[i])
286 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
289 nl_portid_hash_free(otable, osize);
290 hash->rehash_time = jiffies + 10 * 60 * HZ;
294 static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
296 int avg = hash->entries >> hash->shift;
298 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
301 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
302 nl_portid_hash_rehash(hash, 0);
309 static const struct proto_ops netlink_ops;
312 netlink_update_listeners(struct sock *sk)
314 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
317 struct listeners *listeners;
319 listeners = nl_deref_protected(tbl->listeners);
323 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
325 sk_for_each_bound(sk, &tbl->mc_list) {
326 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
327 mask |= nlk_sk(sk)->groups[i];
329 listeners->masks[i] = mask;
331 /* this function is only called with the netlink table "grabbed", which
332 * makes sure updates are visible before bind or setsockopt return. */
335 static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
337 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
338 struct hlist_head *head;
339 int err = -EADDRINUSE;
343 netlink_table_grab();
344 head = nl_portid_hashfn(hash, portid);
346 sk_for_each(osk, head) {
347 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
355 if (nlk_sk(sk)->portid)
359 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
362 if (len && nl_portid_hash_dilute(hash, len))
363 head = nl_portid_hashfn(hash, portid);
365 nlk_sk(sk)->portid = portid;
366 sk_add_node(sk, head);
370 netlink_table_ungrab();
374 static void netlink_remove(struct sock *sk)
376 netlink_table_grab();
377 if (sk_del_node_init(sk))
378 nl_table[sk->sk_protocol].hash.entries--;
379 if (nlk_sk(sk)->subscriptions)
380 __sk_del_bind_node(sk);
381 netlink_table_ungrab();
384 static struct proto netlink_proto = {
386 .owner = THIS_MODULE,
387 .obj_size = sizeof(struct netlink_sock),
390 static int __netlink_create(struct net *net, struct socket *sock,
391 struct mutex *cb_mutex, int protocol)
394 struct netlink_sock *nlk;
396 sock->ops = &netlink_ops;
398 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
402 sock_init_data(sock, sk);
406 nlk->cb_mutex = cb_mutex;
408 nlk->cb_mutex = &nlk->cb_def_mutex;
409 mutex_init(nlk->cb_mutex);
411 init_waitqueue_head(&nlk->wait);
413 sk->sk_destruct = netlink_sock_destruct;
414 sk->sk_protocol = protocol;
418 static int netlink_create(struct net *net, struct socket *sock, int protocol,
421 struct module *module = NULL;
422 struct mutex *cb_mutex;
423 struct netlink_sock *nlk;
424 void (*bind)(int group);
427 sock->state = SS_UNCONNECTED;
429 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
430 return -ESOCKTNOSUPPORT;
432 if (protocol < 0 || protocol >= MAX_LINKS)
433 return -EPROTONOSUPPORT;
435 netlink_lock_table();
436 #ifdef CONFIG_MODULES
437 if (!nl_table[protocol].registered) {
438 netlink_unlock_table();
439 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
440 netlink_lock_table();
443 if (nl_table[protocol].registered &&
444 try_module_get(nl_table[protocol].module))
445 module = nl_table[protocol].module;
447 err = -EPROTONOSUPPORT;
448 cb_mutex = nl_table[protocol].cb_mutex;
449 bind = nl_table[protocol].bind;
450 netlink_unlock_table();
455 err = __netlink_create(net, sock, cb_mutex, protocol);
460 sock_prot_inuse_add(net, &netlink_proto, 1);
463 nlk = nlk_sk(sock->sk);
464 nlk->module = module;
465 nlk->netlink_bind = bind;
474 static int netlink_release(struct socket *sock)
476 struct sock *sk = sock->sk;
477 struct netlink_sock *nlk;
487 * OK. Socket is unlinked, any packets that arrive now
492 wake_up_interruptible_all(&nlk->wait);
494 skb_queue_purge(&sk->sk_write_queue);
497 struct netlink_notify n = {
499 .protocol = sk->sk_protocol,
500 .portid = nlk->portid,
502 atomic_notifier_call_chain(&netlink_chain,
503 NETLINK_URELEASE, &n);
506 module_put(nlk->module);
508 netlink_table_grab();
509 if (netlink_is_kernel(sk)) {
510 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
511 if (--nl_table[sk->sk_protocol].registered == 0) {
512 struct listeners *old;
514 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
515 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
517 nl_table[sk->sk_protocol].module = NULL;
518 nl_table[sk->sk_protocol].bind = NULL;
519 nl_table[sk->sk_protocol].flags = 0;
520 nl_table[sk->sk_protocol].registered = 0;
522 } else if (nlk->subscriptions) {
523 netlink_update_listeners(sk);
525 netlink_table_ungrab();
531 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
537 static int netlink_autobind(struct socket *sock)
539 struct sock *sk = sock->sk;
540 struct net *net = sock_net(sk);
541 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
542 struct hlist_head *head;
544 s32 portid = task_tgid_vnr(current);
546 static s32 rover = -4097;
550 netlink_table_grab();
551 head = nl_portid_hashfn(hash, portid);
552 sk_for_each(osk, head) {
553 if (!net_eq(sock_net(osk), net))
555 if (nlk_sk(osk)->portid == portid) {
556 /* Bind collision, search negative portid values. */
560 netlink_table_ungrab();
564 netlink_table_ungrab();
566 err = netlink_insert(sk, net, portid);
567 if (err == -EADDRINUSE)
570 /* If 2 threads race to autobind, that is fine. */
577 static inline int netlink_capable(const struct socket *sock, unsigned int flag)
579 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
580 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
584 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
586 struct netlink_sock *nlk = nlk_sk(sk);
588 if (nlk->subscriptions && !subscriptions)
589 __sk_del_bind_node(sk);
590 else if (!nlk->subscriptions && subscriptions)
591 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
592 nlk->subscriptions = subscriptions;
595 static int netlink_realloc_groups(struct sock *sk)
597 struct netlink_sock *nlk = nlk_sk(sk);
599 unsigned long *new_groups;
602 netlink_table_grab();
604 groups = nl_table[sk->sk_protocol].groups;
605 if (!nl_table[sk->sk_protocol].registered) {
610 if (nlk->ngroups >= groups)
613 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
614 if (new_groups == NULL) {
618 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
619 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
621 nlk->groups = new_groups;
622 nlk->ngroups = groups;
624 netlink_table_ungrab();
628 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
631 struct sock *sk = sock->sk;
632 struct net *net = sock_net(sk);
633 struct netlink_sock *nlk = nlk_sk(sk);
634 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
637 if (addr_len < sizeof(struct sockaddr_nl))
640 if (nladdr->nl_family != AF_NETLINK)
643 /* Only superuser is allowed to listen multicasts */
644 if (nladdr->nl_groups) {
645 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
647 err = netlink_realloc_groups(sk);
653 if (nladdr->nl_pid != nlk->portid)
656 err = nladdr->nl_pid ?
657 netlink_insert(sk, net, nladdr->nl_pid) :
658 netlink_autobind(sock);
663 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
666 netlink_table_grab();
667 netlink_update_subscriptions(sk, nlk->subscriptions +
668 hweight32(nladdr->nl_groups) -
669 hweight32(nlk->groups[0]));
670 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
671 netlink_update_listeners(sk);
672 netlink_table_ungrab();
674 if (nlk->netlink_bind && nlk->groups[0]) {
677 for (i=0; i<nlk->ngroups; i++) {
678 if (test_bit(i, nlk->groups))
679 nlk->netlink_bind(i);
686 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
690 struct sock *sk = sock->sk;
691 struct netlink_sock *nlk = nlk_sk(sk);
692 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
694 if (alen < sizeof(addr->sa_family))
697 if (addr->sa_family == AF_UNSPEC) {
698 sk->sk_state = NETLINK_UNCONNECTED;
703 if (addr->sa_family != AF_NETLINK)
706 /* Only superuser is allowed to send multicasts */
707 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
711 err = netlink_autobind(sock);
714 sk->sk_state = NETLINK_CONNECTED;
715 nlk->dst_portid = nladdr->nl_pid;
716 nlk->dst_group = ffs(nladdr->nl_groups);
722 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
723 int *addr_len, int peer)
725 struct sock *sk = sock->sk;
726 struct netlink_sock *nlk = nlk_sk(sk);
727 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
729 nladdr->nl_family = AF_NETLINK;
731 *addr_len = sizeof(*nladdr);
734 nladdr->nl_pid = nlk->dst_portid;
735 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
737 nladdr->nl_pid = nlk->portid;
738 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
743 static void netlink_overrun(struct sock *sk)
745 struct netlink_sock *nlk = nlk_sk(sk);
747 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
748 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
749 sk->sk_err = ENOBUFS;
750 sk->sk_error_report(sk);
753 atomic_inc(&sk->sk_drops);
756 static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
759 struct netlink_sock *nlk;
761 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
763 return ERR_PTR(-ECONNREFUSED);
765 /* Don't bother queuing skb if kernel socket has no input function */
767 if (sock->sk_state == NETLINK_CONNECTED &&
768 nlk->dst_portid != nlk_sk(ssk)->portid) {
770 return ERR_PTR(-ECONNREFUSED);
775 struct sock *netlink_getsockbyfilp(struct file *filp)
777 struct inode *inode = file_inode(filp);
780 if (!S_ISSOCK(inode->i_mode))
781 return ERR_PTR(-ENOTSOCK);
783 sock = SOCKET_I(inode)->sk;
784 if (sock->sk_family != AF_NETLINK)
785 return ERR_PTR(-EINVAL);
792 * Attach a skb to a netlink socket.
793 * The caller must hold a reference to the destination socket. On error, the
794 * reference is dropped. The skb is not send to the destination, just all
795 * all error checks are performed and memory in the queue is reserved.
797 * < 0: error. skb freed, reference to sock dropped.
799 * 1: repeat lookup - reference dropped while waiting for socket memory.
801 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
802 long *timeo, struct sock *ssk)
804 struct netlink_sock *nlk;
808 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
809 test_bit(NETLINK_CONGESTED, &nlk->state)) {
810 DECLARE_WAITQUEUE(wait, current);
812 if (!ssk || netlink_is_kernel(ssk))
819 __set_current_state(TASK_INTERRUPTIBLE);
820 add_wait_queue(&nlk->wait, &wait);
822 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
823 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
824 !sock_flag(sk, SOCK_DEAD))
825 *timeo = schedule_timeout(*timeo);
827 __set_current_state(TASK_RUNNING);
828 remove_wait_queue(&nlk->wait, &wait);
831 if (signal_pending(current)) {
833 return sock_intr_errno(*timeo);
837 netlink_skb_set_owner_r(skb, sk);
841 static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
845 skb_queue_tail(&sk->sk_receive_queue, skb);
846 sk->sk_data_ready(sk, len);
850 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
852 int len = __netlink_sendskb(sk, skb);
858 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
864 static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
868 WARN_ON(skb->sk != NULL);
870 delta = skb->end - skb->tail;
871 if (delta * 2 < skb->truesize)
874 if (skb_shared(skb)) {
875 struct sk_buff *nskb = skb_clone(skb, allocation);
882 if (!pskb_expand_head(skb, 0, -delta, allocation))
883 skb->truesize -= delta;
888 static void netlink_rcv_wake(struct sock *sk)
890 struct netlink_sock *nlk = nlk_sk(sk);
892 if (skb_queue_empty(&sk->sk_receive_queue))
893 clear_bit(NETLINK_CONGESTED, &nlk->state);
894 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
895 wake_up_interruptible(&nlk->wait);
898 static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
902 struct netlink_sock *nlk = nlk_sk(sk);
905 if (nlk->netlink_rcv != NULL) {
907 netlink_skb_set_owner_r(skb, sk);
908 NETLINK_CB(skb).sk = ssk;
909 nlk->netlink_rcv(skb);
918 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
919 u32 portid, int nonblock)
925 skb = netlink_trim(skb, gfp_any());
927 timeo = sock_sndtimeo(ssk, nonblock);
929 sk = netlink_getsockbyportid(ssk, portid);
934 if (netlink_is_kernel(sk))
935 return netlink_unicast_kernel(sk, skb, ssk);
937 if (sk_filter(sk, skb)) {
944 err = netlink_attachskb(sk, skb, &timeo, ssk);
950 return netlink_sendskb(sk, skb);
952 EXPORT_SYMBOL(netlink_unicast);
954 int netlink_has_listeners(struct sock *sk, unsigned int group)
957 struct listeners *listeners;
959 BUG_ON(!netlink_is_kernel(sk));
962 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
964 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
965 res = test_bit(group - 1, listeners->masks);
971 EXPORT_SYMBOL_GPL(netlink_has_listeners);
973 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
975 struct netlink_sock *nlk = nlk_sk(sk);
977 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
978 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
979 netlink_skb_set_owner_r(skb, sk);
980 __netlink_sendskb(sk, skb);
981 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
986 struct netlink_broadcast_data {
987 struct sock *exclude_sk;
992 int delivery_failure;
996 struct sk_buff *skb, *skb2;
997 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1001 static int do_one_broadcast(struct sock *sk,
1002 struct netlink_broadcast_data *p)
1004 struct netlink_sock *nlk = nlk_sk(sk);
1007 if (p->exclude_sk == sk)
1010 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1011 !test_bit(p->group - 1, nlk->groups))
1014 if (!net_eq(sock_net(sk), p->net))
1018 netlink_overrun(sk);
1023 if (p->skb2 == NULL) {
1024 if (skb_shared(p->skb)) {
1025 p->skb2 = skb_clone(p->skb, p->allocation);
1027 p->skb2 = skb_get(p->skb);
1029 * skb ownership may have been set when
1030 * delivered to a previous socket.
1032 skb_orphan(p->skb2);
1035 if (p->skb2 == NULL) {
1036 netlink_overrun(sk);
1037 /* Clone failed. Notify ALL listeners. */
1039 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1040 p->delivery_failure = 1;
1041 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1044 } else if (sk_filter(sk, p->skb2)) {
1047 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1048 netlink_overrun(sk);
1049 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1050 p->delivery_failure = 1;
1052 p->congested |= val;
1062 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
1063 u32 group, gfp_t allocation,
1064 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1067 struct net *net = sock_net(ssk);
1068 struct netlink_broadcast_data info;
1071 skb = netlink_trim(skb, allocation);
1073 info.exclude_sk = ssk;
1075 info.portid = portid;
1078 info.delivery_failure = 0;
1081 info.allocation = allocation;
1084 info.tx_filter = filter;
1085 info.tx_data = filter_data;
1087 /* While we sleep in clone, do not allow to change socket list */
1089 netlink_lock_table();
1091 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1092 do_one_broadcast(sk, &info);
1096 netlink_unlock_table();
1098 if (info.delivery_failure) {
1099 kfree_skb(info.skb2);
1102 consume_skb(info.skb2);
1104 if (info.delivered) {
1105 if (info.congested && (allocation & __GFP_WAIT))
1111 EXPORT_SYMBOL(netlink_broadcast_filtered);
1113 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
1114 u32 group, gfp_t allocation)
1116 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
1119 EXPORT_SYMBOL(netlink_broadcast);
1121 struct netlink_set_err_data {
1122 struct sock *exclude_sk;
1128 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1130 struct netlink_sock *nlk = nlk_sk(sk);
1133 if (sk == p->exclude_sk)
1136 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
1139 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1140 !test_bit(p->group - 1, nlk->groups))
1143 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1148 sk->sk_err = p->code;
1149 sk->sk_error_report(sk);
1155 * netlink_set_err - report error to broadcast listeners
1156 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1157 * @portid: the PORTID of a process that we want to skip (if any)
1158 * @groups: the broadcast group that will notice the error
1159 * @code: error code, must be negative (as usual in kernelspace)
1161 * This function returns the number of broadcast listeners that have set the
1162 * NETLINK_RECV_NO_ENOBUFS socket option.
1164 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1166 struct netlink_set_err_data info;
1170 info.exclude_sk = ssk;
1171 info.portid = portid;
1173 /* sk->sk_err wants a positive error value */
1176 read_lock(&nl_table_lock);
1178 sk_for_each_bound(sk, &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_CFG_F_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();
1240 if (nlk->netlink_bind)
1241 nlk->netlink_bind(val);
1246 case NETLINK_BROADCAST_ERROR:
1248 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1250 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1253 case NETLINK_NO_ENOBUFS:
1255 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1256 clear_bit(NETLINK_CONGESTED, &nlk->state);
1257 wake_up_interruptible(&nlk->wait);
1259 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
1269 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1270 char __user *optval, int __user *optlen)
1272 struct sock *sk = sock->sk;
1273 struct netlink_sock *nlk = nlk_sk(sk);
1276 if (level != SOL_NETLINK)
1277 return -ENOPROTOOPT;
1279 if (get_user(len, optlen))
1285 case NETLINK_PKTINFO:
1286 if (len < sizeof(int))
1289 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
1290 if (put_user(len, optlen) ||
1291 put_user(val, optval))
1295 case NETLINK_BROADCAST_ERROR:
1296 if (len < sizeof(int))
1299 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1300 if (put_user(len, optlen) ||
1301 put_user(val, optval))
1305 case NETLINK_NO_ENOBUFS:
1306 if (len < sizeof(int))
1309 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1310 if (put_user(len, optlen) ||
1311 put_user(val, optval))
1321 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1323 struct nl_pktinfo info;
1325 info.group = NETLINK_CB(skb).dst_group;
1326 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1329 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1330 struct msghdr *msg, size_t len)
1332 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1333 struct sock *sk = sock->sk;
1334 struct netlink_sock *nlk = nlk_sk(sk);
1335 struct sockaddr_nl *addr = msg->msg_name;
1338 struct sk_buff *skb;
1340 struct scm_cookie scm;
1342 if (msg->msg_flags&MSG_OOB)
1345 if (NULL == siocb->scm)
1348 err = scm_send(sock, msg, siocb->scm, true);
1352 if (msg->msg_namelen) {
1354 if (addr->nl_family != AF_NETLINK)
1356 dst_portid = addr->nl_pid;
1357 dst_group = ffs(addr->nl_groups);
1359 if ((dst_group || dst_portid) &&
1360 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
1363 dst_portid = nlk->dst_portid;
1364 dst_group = nlk->dst_group;
1368 err = netlink_autobind(sock);
1374 if (len > sk->sk_sndbuf - 32)
1377 skb = alloc_skb(len, GFP_KERNEL);
1381 NETLINK_CB(skb).portid = nlk->portid;
1382 NETLINK_CB(skb).dst_group = dst_group;
1383 NETLINK_CB(skb).creds = siocb->scm->creds;
1386 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1391 err = security_netlink_send(sk, skb);
1398 atomic_inc(&skb->users);
1399 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1401 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1404 scm_destroy(siocb->scm);
1408 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1409 struct msghdr *msg, size_t len,
1412 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1413 struct scm_cookie scm;
1414 struct sock *sk = sock->sk;
1415 struct netlink_sock *nlk = nlk_sk(sk);
1416 int noblock = flags&MSG_DONTWAIT;
1418 struct sk_buff *skb, *data_skb;
1426 skb = skb_recv_datagram(sk, flags, noblock, &err);
1432 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1433 if (unlikely(skb_shinfo(skb)->frag_list)) {
1435 * If this skb has a frag_list, then here that means that we
1436 * will have to use the frag_list skb's data for compat tasks
1437 * and the regular skb's data for normal (non-compat) tasks.
1439 * If we need to send the compat skb, assign it to the
1440 * 'data_skb' variable so that it will be used below for data
1441 * copying. We keep 'skb' for everything else, including
1442 * freeing both later.
1444 if (flags & MSG_CMSG_COMPAT)
1445 data_skb = skb_shinfo(skb)->frag_list;
1449 msg->msg_namelen = 0;
1451 copied = data_skb->len;
1453 msg->msg_flags |= MSG_TRUNC;
1457 skb_reset_transport_header(data_skb);
1458 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
1460 if (msg->msg_name) {
1461 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
1462 addr->nl_family = AF_NETLINK;
1464 addr->nl_pid = NETLINK_CB(skb).portid;
1465 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1466 msg->msg_namelen = sizeof(*addr);
1469 if (nlk->flags & NETLINK_RECV_PKTINFO)
1470 netlink_cmsg_recv_pktinfo(msg, skb);
1472 if (NULL == siocb->scm) {
1473 memset(&scm, 0, sizeof(scm));
1476 siocb->scm->creds = *NETLINK_CREDS(skb);
1477 if (flags & MSG_TRUNC)
1478 copied = data_skb->len;
1480 skb_free_datagram(sk, skb);
1482 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1483 ret = netlink_dump(sk);
1486 sk->sk_error_report(sk);
1490 scm_recv(sock, msg, siocb->scm, flags);
1492 netlink_rcv_wake(sk);
1493 return err ? : copied;
1496 static void netlink_data_ready(struct sock *sk, int len)
1502 * We export these functions to other modules. They provide a
1503 * complete set of kernel non-blocking support for message
1508 __netlink_kernel_create(struct net *net, int unit, struct module *module,
1509 struct netlink_kernel_cfg *cfg)
1511 struct socket *sock;
1513 struct netlink_sock *nlk;
1514 struct listeners *listeners = NULL;
1515 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1516 unsigned int groups;
1520 if (unit < 0 || unit >= MAX_LINKS)
1523 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1527 * We have to just have a reference on the net from sk, but don't
1528 * get_net it. Besides, we cannot get and then put the net here.
1529 * So we create one inside init_net and the move it to net.
1532 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1533 goto out_sock_release_nosk;
1536 sk_change_net(sk, net);
1538 if (!cfg || cfg->groups < 32)
1541 groups = cfg->groups;
1543 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
1545 goto out_sock_release;
1547 sk->sk_data_ready = netlink_data_ready;
1548 if (cfg && cfg->input)
1549 nlk_sk(sk)->netlink_rcv = cfg->input;
1551 if (netlink_insert(sk, net, 0))
1552 goto out_sock_release;
1555 nlk->flags |= NETLINK_KERNEL_SOCKET;
1557 netlink_table_grab();
1558 if (!nl_table[unit].registered) {
1559 nl_table[unit].groups = groups;
1560 rcu_assign_pointer(nl_table[unit].listeners, listeners);
1561 nl_table[unit].cb_mutex = cb_mutex;
1562 nl_table[unit].module = module;
1564 nl_table[unit].bind = cfg->bind;
1565 nl_table[unit].flags = cfg->flags;
1567 nl_table[unit].registered = 1;
1570 nl_table[unit].registered++;
1572 netlink_table_ungrab();
1577 netlink_kernel_release(sk);
1580 out_sock_release_nosk:
1584 EXPORT_SYMBOL(__netlink_kernel_create);
1587 netlink_kernel_release(struct sock *sk)
1589 sk_release_kernel(sk);
1591 EXPORT_SYMBOL(netlink_kernel_release);
1593 int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
1595 struct listeners *new, *old;
1596 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1601 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1602 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1605 old = nl_deref_protected(tbl->listeners);
1606 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1607 rcu_assign_pointer(tbl->listeners, new);
1609 kfree_rcu(old, rcu);
1611 tbl->groups = groups;
1617 * netlink_change_ngroups - change number of multicast groups
1619 * This changes the number of multicast groups that are available
1620 * on a certain netlink family. Note that it is not possible to
1621 * change the number of groups to below 32. Also note that it does
1622 * not implicitly call netlink_clear_multicast_users() when the
1623 * number of groups is reduced.
1625 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1626 * @groups: The new number of groups.
1628 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1632 netlink_table_grab();
1633 err = __netlink_change_ngroups(sk, groups);
1634 netlink_table_ungrab();
1639 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1642 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1644 sk_for_each_bound(sk, &tbl->mc_list)
1645 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1649 * netlink_clear_multicast_users - kick off multicast listeners
1651 * This function removes all listeners from the given group.
1652 * @ksk: The kernel netlink socket, as returned by
1653 * netlink_kernel_create().
1654 * @group: The multicast group to clear.
1656 void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1658 netlink_table_grab();
1659 __netlink_clear_multicast_users(ksk, group);
1660 netlink_table_ungrab();
1664 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
1666 struct nlmsghdr *nlh;
1667 int size = nlmsg_msg_size(len);
1669 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1670 nlh->nlmsg_type = type;
1671 nlh->nlmsg_len = size;
1672 nlh->nlmsg_flags = flags;
1673 nlh->nlmsg_pid = portid;
1674 nlh->nlmsg_seq = seq;
1675 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
1676 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
1679 EXPORT_SYMBOL(__nlmsg_put);
1682 * It looks a bit ugly.
1683 * It would be better to create kernel thread.
1686 static int netlink_dump(struct sock *sk)
1688 struct netlink_sock *nlk = nlk_sk(sk);
1689 struct netlink_callback *cb;
1690 struct sk_buff *skb = NULL;
1691 struct nlmsghdr *nlh;
1692 int len, err = -ENOBUFS;
1695 mutex_lock(nlk->cb_mutex);
1703 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1705 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1709 len = cb->dump(skb, cb);
1712 mutex_unlock(nlk->cb_mutex);
1714 if (sk_filter(sk, skb))
1717 __netlink_sendskb(sk, skb);
1721 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1725 nl_dump_check_consistent(cb, nlh);
1727 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1729 if (sk_filter(sk, skb))
1732 __netlink_sendskb(sk, skb);
1737 mutex_unlock(nlk->cb_mutex);
1739 module_put(cb->module);
1740 netlink_consume_callback(cb);
1744 mutex_unlock(nlk->cb_mutex);
1749 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1750 const struct nlmsghdr *nlh,
1751 struct netlink_dump_control *control)
1753 struct netlink_callback *cb;
1755 struct netlink_sock *nlk;
1758 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
1762 cb->dump = control->dump;
1763 cb->done = control->done;
1765 cb->data = control->data;
1766 cb->module = control->module;
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).portid);
1773 netlink_destroy_callback(cb);
1774 return -ECONNREFUSED;
1778 mutex_lock(nlk->cb_mutex);
1779 /* A dump is in progress... */
1781 mutex_unlock(nlk->cb_mutex);
1782 netlink_destroy_callback(cb);
1786 /* add reference of module which cb->dump belongs to */
1787 if (!try_module_get(cb->module)) {
1788 mutex_unlock(nlk->cb_mutex);
1789 netlink_destroy_callback(cb);
1790 ret = -EPROTONOSUPPORT;
1795 mutex_unlock(nlk->cb_mutex);
1797 ret = netlink_dump(sk);
1804 /* We successfully started a dump, by returning -EINTR we
1805 * signal not to send ACK even if it was requested.
1809 EXPORT_SYMBOL(__netlink_dump_start);
1811 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1813 struct sk_buff *skb;
1814 struct nlmsghdr *rep;
1815 struct nlmsgerr *errmsg;
1816 size_t payload = sizeof(*errmsg);
1818 /* error messages get the original request appened */
1820 payload += nlmsg_len(nlh);
1822 skb = nlmsg_new(payload, GFP_KERNEL);
1826 sk = netlink_lookup(sock_net(in_skb->sk),
1827 in_skb->sk->sk_protocol,
1828 NETLINK_CB(in_skb).portid);
1830 sk->sk_err = ENOBUFS;
1831 sk->sk_error_report(sk);
1837 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
1838 NLMSG_ERROR, payload, 0);
1839 errmsg = nlmsg_data(rep);
1840 errmsg->error = err;
1841 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
1842 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
1844 EXPORT_SYMBOL(netlink_ack);
1846 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
1849 struct nlmsghdr *nlh;
1852 while (skb->len >= nlmsg_total_size(0)) {
1855 nlh = nlmsg_hdr(skb);
1858 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
1861 /* Only requests are handled by the kernel */
1862 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1865 /* Skip control messages */
1866 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
1874 if (nlh->nlmsg_flags & NLM_F_ACK || err)
1875 netlink_ack(skb, nlh, err);
1878 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
1879 if (msglen > skb->len)
1881 skb_pull(skb, msglen);
1886 EXPORT_SYMBOL(netlink_rcv_skb);
1889 * nlmsg_notify - send a notification netlink message
1890 * @sk: netlink socket to use
1891 * @skb: notification message
1892 * @portid: destination netlink portid for reports or 0
1893 * @group: destination multicast group or 0
1894 * @report: 1 to report back, 0 to disable
1895 * @flags: allocation flags
1897 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
1898 unsigned int group, int report, gfp_t flags)
1903 int exclude_portid = 0;
1906 atomic_inc(&skb->users);
1907 exclude_portid = portid;
1910 /* errors reported via destination sk->sk_err, but propagate
1911 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
1912 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
1918 err2 = nlmsg_unicast(sk, skb, portid);
1919 if (!err || err == -ESRCH)
1925 EXPORT_SYMBOL(nlmsg_notify);
1927 #ifdef CONFIG_PROC_FS
1928 struct nl_seq_iter {
1929 struct seq_net_private p;
1934 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1936 struct nl_seq_iter *iter = seq->private;
1941 for (i = 0; i < MAX_LINKS; i++) {
1942 struct nl_portid_hash *hash = &nl_table[i].hash;
1944 for (j = 0; j <= hash->mask; j++) {
1945 sk_for_each(s, &hash->table[j]) {
1946 if (sock_net(s) != seq_file_net(seq))
1960 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1961 __acquires(nl_table_lock)
1963 read_lock(&nl_table_lock);
1964 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1967 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1970 struct nl_seq_iter *iter;
1975 if (v == SEQ_START_TOKEN)
1976 return netlink_seq_socket_idx(seq, 0);
1978 iter = seq->private;
1982 } while (s && sock_net(s) != seq_file_net(seq));
1987 j = iter->hash_idx + 1;
1990 struct nl_portid_hash *hash = &nl_table[i].hash;
1992 for (; j <= hash->mask; j++) {
1993 s = sk_head(&hash->table[j]);
1994 while (s && sock_net(s) != seq_file_net(seq))
2004 } while (++i < MAX_LINKS);
2009 static void netlink_seq_stop(struct seq_file *seq, void *v)
2010 __releases(nl_table_lock)
2012 read_unlock(&nl_table_lock);
2016 static int netlink_seq_show(struct seq_file *seq, void *v)
2018 if (v == SEQ_START_TOKEN) {
2020 "sk Eth Pid Groups "
2021 "Rmem Wmem Dump Locks Drops Inode\n");
2024 struct netlink_sock *nlk = nlk_sk(s);
2026 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
2030 nlk->groups ? (u32)nlk->groups[0] : 0,
2031 sk_rmem_alloc_get(s),
2032 sk_wmem_alloc_get(s),
2034 atomic_read(&s->sk_refcnt),
2035 atomic_read(&s->sk_drops),
2043 static const struct seq_operations netlink_seq_ops = {
2044 .start = netlink_seq_start,
2045 .next = netlink_seq_next,
2046 .stop = netlink_seq_stop,
2047 .show = netlink_seq_show,
2051 static int netlink_seq_open(struct inode *inode, struct file *file)
2053 return seq_open_net(inode, file, &netlink_seq_ops,
2054 sizeof(struct nl_seq_iter));
2057 static const struct file_operations netlink_seq_fops = {
2058 .owner = THIS_MODULE,
2059 .open = netlink_seq_open,
2061 .llseek = seq_lseek,
2062 .release = seq_release_net,
2067 int netlink_register_notifier(struct notifier_block *nb)
2069 return atomic_notifier_chain_register(&netlink_chain, nb);
2071 EXPORT_SYMBOL(netlink_register_notifier);
2073 int netlink_unregister_notifier(struct notifier_block *nb)
2075 return atomic_notifier_chain_unregister(&netlink_chain, nb);
2077 EXPORT_SYMBOL(netlink_unregister_notifier);
2079 static const struct proto_ops netlink_ops = {
2080 .family = PF_NETLINK,
2081 .owner = THIS_MODULE,
2082 .release = netlink_release,
2083 .bind = netlink_bind,
2084 .connect = netlink_connect,
2085 .socketpair = sock_no_socketpair,
2086 .accept = sock_no_accept,
2087 .getname = netlink_getname,
2088 .poll = datagram_poll,
2089 .ioctl = sock_no_ioctl,
2090 .listen = sock_no_listen,
2091 .shutdown = sock_no_shutdown,
2092 .setsockopt = netlink_setsockopt,
2093 .getsockopt = netlink_getsockopt,
2094 .sendmsg = netlink_sendmsg,
2095 .recvmsg = netlink_recvmsg,
2096 .mmap = sock_no_mmap,
2097 .sendpage = sock_no_sendpage,
2100 static const struct net_proto_family netlink_family_ops = {
2101 .family = PF_NETLINK,
2102 .create = netlink_create,
2103 .owner = THIS_MODULE, /* for consistency 8) */
2106 static int __net_init netlink_net_init(struct net *net)
2108 #ifdef CONFIG_PROC_FS
2109 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
2115 static void __net_exit netlink_net_exit(struct net *net)
2117 #ifdef CONFIG_PROC_FS
2118 remove_proc_entry("netlink", net->proc_net);
2122 static void __init netlink_add_usersock_entry(void)
2124 struct listeners *listeners;
2127 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2129 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2131 netlink_table_grab();
2133 nl_table[NETLINK_USERSOCK].groups = groups;
2134 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
2135 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2136 nl_table[NETLINK_USERSOCK].registered = 1;
2137 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
2139 netlink_table_ungrab();
2142 static struct pernet_operations __net_initdata netlink_net_ops = {
2143 .init = netlink_net_init,
2144 .exit = netlink_net_exit,
2147 static int __init netlink_proto_init(void)
2150 unsigned long limit;
2152 int err = proto_register(&netlink_proto, 0);
2157 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
2159 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
2163 if (totalram_pages >= (128 * 1024))
2164 limit = totalram_pages >> (21 - PAGE_SHIFT);
2166 limit = totalram_pages >> (23 - PAGE_SHIFT);
2168 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2169 limit = (1UL << order) / sizeof(struct hlist_head);
2170 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
2172 for (i = 0; i < MAX_LINKS; i++) {
2173 struct nl_portid_hash *hash = &nl_table[i].hash;
2175 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
2178 nl_portid_hash_free(nl_table[i].hash.table,
2179 1 * sizeof(*hash->table));
2183 hash->max_shift = order;
2186 hash->rehash_time = jiffies;
2189 netlink_add_usersock_entry();
2191 sock_register(&netlink_family_ops);
2192 register_pernet_subsys(&netlink_net_ops);
2193 /* The netlink device handler may be needed early. */
2198 panic("netlink_init: Cannot allocate nl_table\n");
2201 core_initcall(netlink_proto_init);