2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 * Patrick McHardy <kaber@trash.net>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
14 * added netlink_proto_exit
15 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
16 * use nlk_sk, as sk->protinfo is on a diet 8)
17 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
18 * - inc module use count of module that owns
19 * the kernel socket in case userspace opens
20 * socket of same protocol
21 * - remove all module support, since netlink is
22 * mandatory if CONFIG_NET=y these days
25 #include <linux/module.h>
27 #include <linux/capability.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/socket.h>
37 #include <linux/fcntl.h>
38 #include <linux/termios.h>
39 #include <linux/sockios.h>
40 #include <linux/net.h>
42 #include <linux/slab.h>
43 #include <linux/uaccess.h>
44 #include <linux/skbuff.h>
45 #include <linux/netdevice.h>
46 #include <linux/rtnetlink.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <linux/jhash.h>
52 #include <linux/jiffies.h>
53 #include <linux/random.h>
54 #include <linux/bitops.h>
56 #include <linux/types.h>
57 #include <linux/audit.h>
58 #include <linux/mutex.h>
59 #include <linux/vmalloc.h>
60 #include <linux/if_arp.h>
61 #include <linux/rhashtable.h>
62 #include <asm/cacheflush.h>
63 #include <linux/hash.h>
64 #include <linux/genetlink.h>
65 #include <linux/net_namespace.h>
66 #include <linux/nospec.h>
68 #include <net/net_namespace.h>
71 #include <net/netlink.h>
73 #include "af_netlink.h"
77 unsigned long masks[0];
81 #define NETLINK_S_CONGESTED 0x0
83 static inline int netlink_is_kernel(struct sock *sk)
85 return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
88 struct netlink_table *nl_table __read_mostly;
89 EXPORT_SYMBOL_GPL(nl_table);
91 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
93 static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS];
95 static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = {
98 "nlk_cb_mutex-USERSOCK",
99 "nlk_cb_mutex-FIREWALL",
100 "nlk_cb_mutex-SOCK_DIAG",
101 "nlk_cb_mutex-NFLOG",
103 "nlk_cb_mutex-SELINUX",
104 "nlk_cb_mutex-ISCSI",
105 "nlk_cb_mutex-AUDIT",
106 "nlk_cb_mutex-FIB_LOOKUP",
107 "nlk_cb_mutex-CONNECTOR",
108 "nlk_cb_mutex-NETFILTER",
109 "nlk_cb_mutex-IP6_FW",
110 "nlk_cb_mutex-DNRTMSG",
111 "nlk_cb_mutex-KOBJECT_UEVENT",
112 "nlk_cb_mutex-GENERIC",
114 "nlk_cb_mutex-SCSITRANSPORT",
115 "nlk_cb_mutex-ECRYPTFS",
117 "nlk_cb_mutex-CRYPTO",
128 "nlk_cb_mutex-MAX_LINKS"
131 static int netlink_dump(struct sock *sk);
132 static void netlink_skb_destructor(struct sk_buff *skb);
134 /* nl_table locking explained:
135 * Lookup and traversal are protected with an RCU read-side lock. Insertion
136 * and removal are protected with per bucket lock while using RCU list
137 * modification primitives and may run in parallel to RCU protected lookups.
138 * Destruction of the Netlink socket may only occur *after* nl_table_lock has
139 * been acquired * either during or after the socket has been removed from
140 * the list and after an RCU grace period.
142 DEFINE_RWLOCK(nl_table_lock);
143 EXPORT_SYMBOL_GPL(nl_table_lock);
144 static atomic_t nl_table_users = ATOMIC_INIT(0);
146 #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
148 static BLOCKING_NOTIFIER_HEAD(netlink_chain);
150 static DEFINE_SPINLOCK(netlink_tap_lock);
151 static struct list_head netlink_tap_all __read_mostly;
153 static const struct rhashtable_params netlink_rhashtable_params;
155 static inline u32 netlink_group_mask(u32 group)
159 return group ? 1 << (group - 1) : 0;
162 static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb,
165 unsigned int len = skb_end_offset(skb);
168 new = alloc_skb(len, gfp_mask);
172 NETLINK_CB(new).portid = NETLINK_CB(skb).portid;
173 NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group;
174 NETLINK_CB(new).creds = NETLINK_CB(skb).creds;
176 skb_put_data(new, skb->data, len);
180 int netlink_add_tap(struct netlink_tap *nt)
182 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
185 spin_lock(&netlink_tap_lock);
186 list_add_rcu(&nt->list, &netlink_tap_all);
187 spin_unlock(&netlink_tap_lock);
189 __module_get(nt->module);
193 EXPORT_SYMBOL_GPL(netlink_add_tap);
195 static int __netlink_remove_tap(struct netlink_tap *nt)
198 struct netlink_tap *tmp;
200 spin_lock(&netlink_tap_lock);
202 list_for_each_entry(tmp, &netlink_tap_all, list) {
204 list_del_rcu(&nt->list);
210 pr_warn("__netlink_remove_tap: %p not found\n", nt);
212 spin_unlock(&netlink_tap_lock);
215 module_put(nt->module);
217 return found ? 0 : -ENODEV;
220 int netlink_remove_tap(struct netlink_tap *nt)
224 ret = __netlink_remove_tap(nt);
229 EXPORT_SYMBOL_GPL(netlink_remove_tap);
231 static bool netlink_filter_tap(const struct sk_buff *skb)
233 struct sock *sk = skb->sk;
235 /* We take the more conservative approach and
236 * whitelist socket protocols that may pass.
238 switch (sk->sk_protocol) {
240 case NETLINK_USERSOCK:
241 case NETLINK_SOCK_DIAG:
244 case NETLINK_FIB_LOOKUP:
245 case NETLINK_NETFILTER:
246 case NETLINK_GENERIC:
253 static int __netlink_deliver_tap_skb(struct sk_buff *skb,
254 struct net_device *dev)
256 struct sk_buff *nskb;
257 struct sock *sk = skb->sk;
260 if (!net_eq(dev_net(dev), sock_net(sk)))
265 if (is_vmalloc_addr(skb->head))
266 nskb = netlink_to_full_skb(skb, GFP_ATOMIC);
268 nskb = skb_clone(skb, GFP_ATOMIC);
271 nskb->protocol = htons((u16) sk->sk_protocol);
272 nskb->pkt_type = netlink_is_kernel(sk) ?
273 PACKET_KERNEL : PACKET_USER;
274 skb_reset_network_header(nskb);
275 ret = dev_queue_xmit(nskb);
276 if (unlikely(ret > 0))
277 ret = net_xmit_errno(ret);
284 static void __netlink_deliver_tap(struct sk_buff *skb)
287 struct netlink_tap *tmp;
289 if (!netlink_filter_tap(skb))
292 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
293 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
299 static void netlink_deliver_tap(struct sk_buff *skb)
303 if (unlikely(!list_empty(&netlink_tap_all)))
304 __netlink_deliver_tap(skb);
309 static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
312 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
313 netlink_deliver_tap(skb);
316 static void netlink_overrun(struct sock *sk)
318 struct netlink_sock *nlk = nlk_sk(sk);
320 if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
321 if (!test_and_set_bit(NETLINK_S_CONGESTED,
322 &nlk_sk(sk)->state)) {
323 sk->sk_err = ENOBUFS;
324 sk->sk_error_report(sk);
327 atomic_inc(&sk->sk_drops);
330 static void netlink_rcv_wake(struct sock *sk)
332 struct netlink_sock *nlk = nlk_sk(sk);
334 if (skb_queue_empty(&sk->sk_receive_queue))
335 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
336 if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
337 wake_up_interruptible(&nlk->wait);
340 static void netlink_skb_destructor(struct sk_buff *skb)
342 if (is_vmalloc_addr(skb->head)) {
344 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
353 static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
355 WARN_ON(skb->sk != NULL);
357 skb->destructor = netlink_skb_destructor;
358 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
359 sk_mem_charge(sk, skb->truesize);
362 static void netlink_sock_destruct(struct sock *sk)
364 struct netlink_sock *nlk = nlk_sk(sk);
366 if (nlk->cb_running) {
368 nlk->cb.done(&nlk->cb);
369 module_put(nlk->cb.module);
370 kfree_skb(nlk->cb.skb);
373 skb_queue_purge(&sk->sk_receive_queue);
375 if (!sock_flag(sk, SOCK_DEAD)) {
376 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
380 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
381 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
382 WARN_ON(nlk_sk(sk)->groups);
385 static void netlink_sock_destruct_work(struct work_struct *work)
387 struct netlink_sock *nlk = container_of(work, struct netlink_sock,
393 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
394 * SMP. Look, when several writers sleep and reader wakes them up, all but one
395 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
396 * this, _but_ remember, it adds useless work on UP machines.
399 void netlink_table_grab(void)
400 __acquires(nl_table_lock)
404 write_lock_irq(&nl_table_lock);
406 if (atomic_read(&nl_table_users)) {
407 DECLARE_WAITQUEUE(wait, current);
409 add_wait_queue_exclusive(&nl_table_wait, &wait);
411 set_current_state(TASK_UNINTERRUPTIBLE);
412 if (atomic_read(&nl_table_users) == 0)
414 write_unlock_irq(&nl_table_lock);
416 write_lock_irq(&nl_table_lock);
419 __set_current_state(TASK_RUNNING);
420 remove_wait_queue(&nl_table_wait, &wait);
424 void netlink_table_ungrab(void)
425 __releases(nl_table_lock)
427 write_unlock_irq(&nl_table_lock);
428 wake_up(&nl_table_wait);
432 netlink_lock_table(void)
436 /* read_lock() synchronizes us to netlink_table_grab */
438 read_lock_irqsave(&nl_table_lock, flags);
439 atomic_inc(&nl_table_users);
440 read_unlock_irqrestore(&nl_table_lock, flags);
444 netlink_unlock_table(void)
446 if (atomic_dec_and_test(&nl_table_users))
447 wake_up(&nl_table_wait);
450 struct netlink_compare_arg
456 /* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
457 #define netlink_compare_arg_len \
458 (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
460 static inline int netlink_compare(struct rhashtable_compare_arg *arg,
463 const struct netlink_compare_arg *x = arg->key;
464 const struct netlink_sock *nlk = ptr;
466 return nlk->portid != x->portid ||
467 !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
470 static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
471 struct net *net, u32 portid)
473 memset(arg, 0, sizeof(*arg));
474 write_pnet(&arg->pnet, net);
475 arg->portid = portid;
478 static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
481 struct netlink_compare_arg arg;
483 netlink_compare_arg_init(&arg, net, portid);
484 return rhashtable_lookup_fast(&table->hash, &arg,
485 netlink_rhashtable_params);
488 static int __netlink_insert(struct netlink_table *table, struct sock *sk)
490 struct netlink_compare_arg arg;
492 netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
493 return rhashtable_lookup_insert_key(&table->hash, &arg,
495 netlink_rhashtable_params);
498 static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
500 struct netlink_table *table = &nl_table[protocol];
504 sk = __netlink_lookup(table, portid, net);
512 static const struct proto_ops netlink_ops;
515 netlink_update_listeners(struct sock *sk)
517 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
520 struct listeners *listeners;
522 listeners = nl_deref_protected(tbl->listeners);
526 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
528 sk_for_each_bound(sk, &tbl->mc_list) {
529 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
530 mask |= nlk_sk(sk)->groups[i];
532 listeners->masks[i] = mask;
534 /* this function is only called with the netlink table "grabbed", which
535 * makes sure updates are visible before bind or setsockopt return. */
538 static int netlink_insert(struct sock *sk, u32 portid)
540 struct netlink_table *table = &nl_table[sk->sk_protocol];
545 err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY;
546 if (nlk_sk(sk)->bound)
550 if (BITS_PER_LONG > 32 &&
551 unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
554 nlk_sk(sk)->portid = portid;
557 err = __netlink_insert(table, sk);
559 /* In case the hashtable backend returns with -EBUSY
560 * from here, it must not escape to the caller.
562 if (unlikely(err == -EBUSY))
570 /* We need to ensure that the socket is hashed and visible. */
572 /* Paired with lockless reads from netlink_bind(),
573 * netlink_connect() and netlink_sendmsg().
575 WRITE_ONCE(nlk_sk(sk)->bound, portid);
582 static void netlink_remove(struct sock *sk)
584 struct netlink_table *table;
586 table = &nl_table[sk->sk_protocol];
587 if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
588 netlink_rhashtable_params)) {
589 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
593 netlink_table_grab();
594 if (nlk_sk(sk)->subscriptions) {
595 __sk_del_bind_node(sk);
596 netlink_update_listeners(sk);
598 if (sk->sk_protocol == NETLINK_GENERIC)
599 atomic_inc(&genl_sk_destructing_cnt);
600 netlink_table_ungrab();
603 static struct proto netlink_proto = {
605 .owner = THIS_MODULE,
606 .obj_size = sizeof(struct netlink_sock),
609 static int __netlink_create(struct net *net, struct socket *sock,
610 struct mutex *cb_mutex, int protocol,
614 struct netlink_sock *nlk;
616 sock->ops = &netlink_ops;
618 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
622 sock_init_data(sock, sk);
626 nlk->cb_mutex = cb_mutex;
628 nlk->cb_mutex = &nlk->cb_def_mutex;
629 mutex_init(nlk->cb_mutex);
630 lockdep_set_class_and_name(nlk->cb_mutex,
631 nlk_cb_mutex_keys + protocol,
632 nlk_cb_mutex_key_strings[protocol]);
634 init_waitqueue_head(&nlk->wait);
636 sk->sk_destruct = netlink_sock_destruct;
637 sk->sk_protocol = protocol;
641 static int netlink_create(struct net *net, struct socket *sock, int protocol,
644 struct module *module = NULL;
645 struct mutex *cb_mutex;
646 struct netlink_sock *nlk;
647 int (*bind)(struct net *net, int group);
648 void (*unbind)(struct net *net, int group);
651 sock->state = SS_UNCONNECTED;
653 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
654 return -ESOCKTNOSUPPORT;
656 if (protocol < 0 || protocol >= MAX_LINKS)
657 return -EPROTONOSUPPORT;
658 protocol = array_index_nospec(protocol, MAX_LINKS);
660 netlink_lock_table();
661 #ifdef CONFIG_MODULES
662 if (!nl_table[protocol].registered) {
663 netlink_unlock_table();
664 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
665 netlink_lock_table();
668 if (nl_table[protocol].registered &&
669 try_module_get(nl_table[protocol].module))
670 module = nl_table[protocol].module;
672 err = -EPROTONOSUPPORT;
673 cb_mutex = nl_table[protocol].cb_mutex;
674 bind = nl_table[protocol].bind;
675 unbind = nl_table[protocol].unbind;
676 netlink_unlock_table();
681 err = __netlink_create(net, sock, cb_mutex, protocol, kern);
686 sock_prot_inuse_add(net, &netlink_proto, 1);
689 nlk = nlk_sk(sock->sk);
690 nlk->module = module;
691 nlk->netlink_bind = bind;
692 nlk->netlink_unbind = unbind;
701 static void deferred_put_nlk_sk(struct rcu_head *head)
703 struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
704 struct sock *sk = &nlk->sk;
709 if (!refcount_dec_and_test(&sk->sk_refcnt))
712 if (nlk->cb_running && nlk->cb.done) {
713 INIT_WORK(&nlk->work, netlink_sock_destruct_work);
714 schedule_work(&nlk->work);
721 static int netlink_release(struct socket *sock)
723 struct sock *sk = sock->sk;
724 struct netlink_sock *nlk;
734 * OK. Socket is unlinked, any packets that arrive now
738 /* must not acquire netlink_table_lock in any way again before unbind
739 * and notifying genetlink is done as otherwise it might deadlock
741 if (nlk->netlink_unbind) {
744 for (i = 0; i < nlk->ngroups; i++)
745 if (test_bit(i, nlk->groups))
746 nlk->netlink_unbind(sock_net(sk), i + 1);
748 if (sk->sk_protocol == NETLINK_GENERIC &&
749 atomic_dec_return(&genl_sk_destructing_cnt) == 0)
750 wake_up(&genl_sk_destructing_waitq);
753 wake_up_interruptible_all(&nlk->wait);
755 skb_queue_purge(&sk->sk_write_queue);
757 if (nlk->portid && nlk->bound) {
758 struct netlink_notify n = {
760 .protocol = sk->sk_protocol,
761 .portid = nlk->portid,
763 blocking_notifier_call_chain(&netlink_chain,
764 NETLINK_URELEASE, &n);
767 module_put(nlk->module);
769 if (netlink_is_kernel(sk)) {
770 netlink_table_grab();
771 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
772 if (--nl_table[sk->sk_protocol].registered == 0) {
773 struct listeners *old;
775 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
776 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
778 nl_table[sk->sk_protocol].module = NULL;
779 nl_table[sk->sk_protocol].bind = NULL;
780 nl_table[sk->sk_protocol].unbind = NULL;
781 nl_table[sk->sk_protocol].flags = 0;
782 nl_table[sk->sk_protocol].registered = 0;
784 netlink_table_ungrab();
788 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
790 call_rcu(&nlk->rcu, deferred_put_nlk_sk);
794 static int netlink_autobind(struct socket *sock)
796 struct sock *sk = sock->sk;
797 struct net *net = sock_net(sk);
798 struct netlink_table *table = &nl_table[sk->sk_protocol];
799 s32 portid = task_tgid_vnr(current);
807 ok = !__netlink_lookup(table, portid, net);
810 /* Bind collision, search negative portid values. */
812 /* rover will be in range [S32_MIN, -4097] */
813 rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
814 else if (rover >= -4096)
820 err = netlink_insert(sk, portid);
821 if (err == -EADDRINUSE)
824 /* If 2 threads race to autobind, that is fine. */
832 * __netlink_ns_capable - General netlink message capability test
833 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
834 * @user_ns: The user namespace of the capability to use
835 * @cap: The capability to use
837 * Test to see if the opener of the socket we received the message
838 * from had when the netlink socket was created and the sender of the
839 * message has has the capability @cap in the user namespace @user_ns.
841 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
842 struct user_namespace *user_ns, int cap)
844 return ((nsp->flags & NETLINK_SKB_DST) ||
845 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
846 ns_capable(user_ns, cap);
848 EXPORT_SYMBOL(__netlink_ns_capable);
851 * netlink_ns_capable - General netlink message capability test
852 * @skb: socket buffer holding a netlink command from userspace
853 * @user_ns: The user namespace of the capability to use
854 * @cap: The capability to use
856 * Test to see if the opener of the socket we received the message
857 * from had when the netlink socket was created and the sender of the
858 * message has has the capability @cap in the user namespace @user_ns.
860 bool netlink_ns_capable(const struct sk_buff *skb,
861 struct user_namespace *user_ns, int cap)
863 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
865 EXPORT_SYMBOL(netlink_ns_capable);
868 * netlink_capable - Netlink global message capability test
869 * @skb: socket buffer holding a netlink command from userspace
870 * @cap: The capability to use
872 * Test to see if the opener of the socket we received the message
873 * from had when the netlink socket was created and the sender of the
874 * message has has the capability @cap in all user namespaces.
876 bool netlink_capable(const struct sk_buff *skb, int cap)
878 return netlink_ns_capable(skb, &init_user_ns, cap);
880 EXPORT_SYMBOL(netlink_capable);
883 * netlink_net_capable - Netlink network namespace message capability test
884 * @skb: socket buffer holding a netlink command from userspace
885 * @cap: The capability to use
887 * Test to see if the opener of the socket we received the message
888 * from had when the netlink socket was created and the sender of the
889 * message has has the capability @cap over the network namespace of
890 * the socket we received the message from.
892 bool netlink_net_capable(const struct sk_buff *skb, int cap)
894 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
896 EXPORT_SYMBOL(netlink_net_capable);
898 static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
900 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
901 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
905 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
907 struct netlink_sock *nlk = nlk_sk(sk);
909 if (nlk->subscriptions && !subscriptions)
910 __sk_del_bind_node(sk);
911 else if (!nlk->subscriptions && subscriptions)
912 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
913 nlk->subscriptions = subscriptions;
916 static int netlink_realloc_groups(struct sock *sk)
918 struct netlink_sock *nlk = nlk_sk(sk);
920 unsigned long *new_groups;
923 netlink_table_grab();
925 groups = nl_table[sk->sk_protocol].groups;
926 if (!nl_table[sk->sk_protocol].registered) {
931 if (nlk->ngroups >= groups)
934 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
935 if (new_groups == NULL) {
939 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
940 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
942 nlk->groups = new_groups;
943 nlk->ngroups = groups;
945 netlink_table_ungrab();
949 static void netlink_undo_bind(int group, long unsigned int groups,
952 struct netlink_sock *nlk = nlk_sk(sk);
955 if (!nlk->netlink_unbind)
958 for (undo = 0; undo < group; undo++)
959 if (test_bit(undo, &groups))
960 nlk->netlink_unbind(sock_net(sk), undo + 1);
963 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
966 struct sock *sk = sock->sk;
967 struct net *net = sock_net(sk);
968 struct netlink_sock *nlk = nlk_sk(sk);
969 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
971 long unsigned int groups = nladdr->nl_groups;
974 if (addr_len < sizeof(struct sockaddr_nl))
977 if (nladdr->nl_family != AF_NETLINK)
980 /* Only superuser is allowed to listen multicasts */
982 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
984 err = netlink_realloc_groups(sk);
989 if (nlk->ngroups == 0)
991 else if (nlk->ngroups < 8*sizeof(groups))
992 groups &= (1UL << nlk->ngroups) - 1;
994 /* Paired with WRITE_ONCE() in netlink_insert() */
995 bound = READ_ONCE(nlk->bound);
997 /* Ensure nlk->portid is up-to-date. */
1000 if (nladdr->nl_pid != nlk->portid)
1004 netlink_lock_table();
1005 if (nlk->netlink_bind && groups) {
1008 /* nl_groups is a u32, so cap the maximum groups we can bind */
1009 for (group = 0; group < BITS_PER_TYPE(u32); group++) {
1010 if (!test_bit(group, &groups))
1012 err = nlk->netlink_bind(net, group + 1);
1015 netlink_undo_bind(group, groups, sk);
1020 /* No need for barriers here as we return to user-space without
1021 * using any of the bound attributes.
1024 err = nladdr->nl_pid ?
1025 netlink_insert(sk, nladdr->nl_pid) :
1026 netlink_autobind(sock);
1028 netlink_undo_bind(BITS_PER_TYPE(u32), groups, sk);
1033 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
1035 netlink_unlock_table();
1037 netlink_table_grab();
1038 netlink_update_subscriptions(sk, nlk->subscriptions +
1040 hweight32(nlk->groups[0]));
1041 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
1042 netlink_update_listeners(sk);
1043 netlink_table_ungrab();
1048 netlink_unlock_table();
1052 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1053 int alen, int flags)
1056 struct sock *sk = sock->sk;
1057 struct netlink_sock *nlk = nlk_sk(sk);
1058 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1060 if (alen < sizeof(addr->sa_family))
1063 if (addr->sa_family == AF_UNSPEC) {
1064 /* paired with READ_ONCE() in netlink_getsockbyportid() */
1065 WRITE_ONCE(sk->sk_state, NETLINK_UNCONNECTED);
1066 /* dst_portid and dst_group can be read locklessly */
1067 WRITE_ONCE(nlk->dst_portid, 0);
1068 WRITE_ONCE(nlk->dst_group, 0);
1071 if (addr->sa_family != AF_NETLINK)
1074 if (alen < sizeof(struct sockaddr_nl))
1077 if ((nladdr->nl_groups || nladdr->nl_pid) &&
1078 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
1081 /* No need for barriers here as we return to user-space without
1082 * using any of the bound attributes.
1083 * Paired with WRITE_ONCE() in netlink_insert().
1085 if (!READ_ONCE(nlk->bound))
1086 err = netlink_autobind(sock);
1089 /* paired with READ_ONCE() in netlink_getsockbyportid() */
1090 WRITE_ONCE(sk->sk_state, NETLINK_CONNECTED);
1091 /* dst_portid and dst_group can be read locklessly */
1092 WRITE_ONCE(nlk->dst_portid, nladdr->nl_pid);
1093 WRITE_ONCE(nlk->dst_group, ffs(nladdr->nl_groups));
1099 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1100 int *addr_len, int peer)
1102 struct sock *sk = sock->sk;
1103 struct netlink_sock *nlk = nlk_sk(sk);
1104 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
1106 nladdr->nl_family = AF_NETLINK;
1108 *addr_len = sizeof(*nladdr);
1111 /* Paired with WRITE_ONCE() in netlink_connect() */
1112 nladdr->nl_pid = READ_ONCE(nlk->dst_portid);
1113 nladdr->nl_groups = netlink_group_mask(READ_ONCE(nlk->dst_group));
1115 nladdr->nl_pid = nlk->portid;
1116 netlink_lock_table();
1117 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
1118 netlink_unlock_table();
1123 static int netlink_ioctl(struct socket *sock, unsigned int cmd,
1126 /* try to hand this ioctl down to the NIC drivers.
1128 return -ENOIOCTLCMD;
1131 static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
1134 struct netlink_sock *nlk;
1136 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
1138 return ERR_PTR(-ECONNREFUSED);
1140 /* Don't bother queuing skb if kernel socket has no input function */
1142 /* dst_portid and sk_state can be changed in netlink_connect() */
1143 if (READ_ONCE(sock->sk_state) == NETLINK_CONNECTED &&
1144 READ_ONCE(nlk->dst_portid) != nlk_sk(ssk)->portid) {
1146 return ERR_PTR(-ECONNREFUSED);
1151 struct sock *netlink_getsockbyfilp(struct file *filp)
1153 struct inode *inode = file_inode(filp);
1156 if (!S_ISSOCK(inode->i_mode))
1157 return ERR_PTR(-ENOTSOCK);
1159 sock = SOCKET_I(inode)->sk;
1160 if (sock->sk_family != AF_NETLINK)
1161 return ERR_PTR(-EINVAL);
1167 static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1170 struct sk_buff *skb;
1173 if (size <= NLMSG_GOODSIZE || broadcast)
1174 return alloc_skb(size, GFP_KERNEL);
1176 size = SKB_DATA_ALIGN(size) +
1177 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1179 data = vmalloc(size);
1183 skb = __build_skb(data, size);
1187 skb->destructor = netlink_skb_destructor;
1193 * Attach a skb to a netlink socket.
1194 * The caller must hold a reference to the destination socket. On error, the
1195 * reference is dropped. The skb is not send to the destination, just all
1196 * all error checks are performed and memory in the queue is reserved.
1198 * < 0: error. skb freed, reference to sock dropped.
1200 * 1: repeat lookup - reference dropped while waiting for socket memory.
1202 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
1203 long *timeo, struct sock *ssk)
1205 struct netlink_sock *nlk;
1209 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1210 test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
1211 DECLARE_WAITQUEUE(wait, current);
1213 if (!ssk || netlink_is_kernel(ssk))
1214 netlink_overrun(sk);
1220 __set_current_state(TASK_INTERRUPTIBLE);
1221 add_wait_queue(&nlk->wait, &wait);
1223 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1224 test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
1225 !sock_flag(sk, SOCK_DEAD))
1226 *timeo = schedule_timeout(*timeo);
1228 __set_current_state(TASK_RUNNING);
1229 remove_wait_queue(&nlk->wait, &wait);
1232 if (signal_pending(current)) {
1234 return sock_intr_errno(*timeo);
1238 netlink_skb_set_owner_r(skb, sk);
1242 static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1246 netlink_deliver_tap(skb);
1248 skb_queue_tail(&sk->sk_receive_queue, skb);
1249 sk->sk_data_ready(sk);
1253 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1255 int len = __netlink_sendskb(sk, skb);
1261 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1267 static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
1271 WARN_ON(skb->sk != NULL);
1272 delta = skb->end - skb->tail;
1273 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
1276 if (skb_shared(skb)) {
1277 struct sk_buff *nskb = skb_clone(skb, allocation);
1284 pskb_expand_head(skb, 0, -delta,
1285 (allocation & ~__GFP_DIRECT_RECLAIM) |
1286 __GFP_NOWARN | __GFP_NORETRY);
1290 static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1294 struct netlink_sock *nlk = nlk_sk(sk);
1296 ret = -ECONNREFUSED;
1297 if (nlk->netlink_rcv != NULL) {
1299 netlink_skb_set_owner_r(skb, sk);
1300 NETLINK_CB(skb).sk = ssk;
1301 netlink_deliver_tap_kernel(sk, ssk, skb);
1302 nlk->netlink_rcv(skb);
1311 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
1312 u32 portid, int nonblock)
1318 skb = netlink_trim(skb, gfp_any());
1320 timeo = sock_sndtimeo(ssk, nonblock);
1322 sk = netlink_getsockbyportid(ssk, portid);
1327 if (netlink_is_kernel(sk))
1328 return netlink_unicast_kernel(sk, skb, ssk);
1330 if (sk_filter(sk, skb)) {
1337 err = netlink_attachskb(sk, skb, &timeo, ssk);
1343 return netlink_sendskb(sk, skb);
1345 EXPORT_SYMBOL(netlink_unicast);
1347 int netlink_has_listeners(struct sock *sk, unsigned int group)
1350 struct listeners *listeners;
1352 BUG_ON(!netlink_is_kernel(sk));
1355 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1357 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
1358 res = test_bit(group - 1, listeners->masks);
1364 EXPORT_SYMBOL_GPL(netlink_has_listeners);
1366 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
1368 struct netlink_sock *nlk = nlk_sk(sk);
1370 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
1371 !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
1372 netlink_skb_set_owner_r(skb, sk);
1373 __netlink_sendskb(sk, skb);
1374 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1379 struct netlink_broadcast_data {
1380 struct sock *exclude_sk;
1385 int delivery_failure;
1389 struct sk_buff *skb, *skb2;
1390 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1394 static void do_one_broadcast(struct sock *sk,
1395 struct netlink_broadcast_data *p)
1397 struct netlink_sock *nlk = nlk_sk(sk);
1400 if (p->exclude_sk == sk)
1403 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1404 !test_bit(p->group - 1, nlk->groups))
1407 if (!net_eq(sock_net(sk), p->net)) {
1408 if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
1411 if (!peernet_has_id(sock_net(sk), p->net))
1414 if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
1420 netlink_overrun(sk);
1425 if (p->skb2 == NULL) {
1426 if (skb_shared(p->skb)) {
1427 p->skb2 = skb_clone(p->skb, p->allocation);
1429 p->skb2 = skb_get(p->skb);
1431 * skb ownership may have been set when
1432 * delivered to a previous socket.
1434 skb_orphan(p->skb2);
1437 if (p->skb2 == NULL) {
1438 netlink_overrun(sk);
1439 /* Clone failed. Notify ALL listeners. */
1441 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
1442 p->delivery_failure = 1;
1445 if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1450 if (sk_filter(sk, p->skb2)) {
1455 NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
1456 if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED)
1457 NETLINK_CB(p->skb2).nsid_is_set = true;
1458 val = netlink_broadcast_deliver(sk, p->skb2);
1460 netlink_overrun(sk);
1461 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
1462 p->delivery_failure = 1;
1464 p->congested |= val;
1472 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
1473 u32 group, gfp_t allocation,
1474 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1477 struct net *net = sock_net(ssk);
1478 struct netlink_broadcast_data info;
1481 skb = netlink_trim(skb, allocation);
1483 info.exclude_sk = ssk;
1485 info.portid = portid;
1488 info.delivery_failure = 0;
1491 info.allocation = allocation;
1494 info.tx_filter = filter;
1495 info.tx_data = filter_data;
1497 /* While we sleep in clone, do not allow to change socket list */
1499 netlink_lock_table();
1501 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1502 do_one_broadcast(sk, &info);
1506 netlink_unlock_table();
1508 if (info.delivery_failure) {
1509 kfree_skb(info.skb2);
1512 consume_skb(info.skb2);
1514 if (info.delivered) {
1515 if (info.congested && gfpflags_allow_blocking(allocation))
1521 EXPORT_SYMBOL(netlink_broadcast_filtered);
1523 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
1524 u32 group, gfp_t allocation)
1526 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
1529 EXPORT_SYMBOL(netlink_broadcast);
1531 struct netlink_set_err_data {
1532 struct sock *exclude_sk;
1538 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1540 struct netlink_sock *nlk = nlk_sk(sk);
1543 if (sk == p->exclude_sk)
1546 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
1549 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1550 !test_bit(p->group - 1, nlk->groups))
1553 if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
1558 sk->sk_err = p->code;
1559 sk->sk_error_report(sk);
1565 * netlink_set_err - report error to broadcast listeners
1566 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1567 * @portid: the PORTID of a process that we want to skip (if any)
1568 * @group: the broadcast group that will notice the error
1569 * @code: error code, must be negative (as usual in kernelspace)
1571 * This function returns the number of broadcast listeners that have set the
1572 * NETLINK_NO_ENOBUFS socket option.
1574 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1576 struct netlink_set_err_data info;
1577 unsigned long flags;
1581 info.exclude_sk = ssk;
1582 info.portid = portid;
1584 /* sk->sk_err wants a positive error value */
1587 read_lock_irqsave(&nl_table_lock, flags);
1589 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1590 ret += do_one_set_err(sk, &info);
1592 read_unlock_irqrestore(&nl_table_lock, flags);
1595 EXPORT_SYMBOL(netlink_set_err);
1597 /* must be called with netlink table grabbed */
1598 static void netlink_update_socket_mc(struct netlink_sock *nlk,
1602 int old, new = !!is_new, subscriptions;
1604 old = test_bit(group - 1, nlk->groups);
1605 subscriptions = nlk->subscriptions - old + new;
1607 __set_bit(group - 1, nlk->groups);
1609 __clear_bit(group - 1, nlk->groups);
1610 netlink_update_subscriptions(&nlk->sk, subscriptions);
1611 netlink_update_listeners(&nlk->sk);
1614 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1615 char __user *optval, unsigned int optlen)
1617 struct sock *sk = sock->sk;
1618 struct netlink_sock *nlk = nlk_sk(sk);
1619 unsigned int val = 0;
1622 if (level != SOL_NETLINK)
1623 return -ENOPROTOOPT;
1625 if (optlen >= sizeof(int) &&
1626 get_user(val, (unsigned int __user *)optval))
1630 case NETLINK_PKTINFO:
1632 nlk->flags |= NETLINK_F_RECV_PKTINFO;
1634 nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
1637 case NETLINK_ADD_MEMBERSHIP:
1638 case NETLINK_DROP_MEMBERSHIP: {
1639 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
1641 err = netlink_realloc_groups(sk);
1644 if (!val || val - 1 >= nlk->ngroups)
1646 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
1647 err = nlk->netlink_bind(sock_net(sk), val);
1651 netlink_table_grab();
1652 netlink_update_socket_mc(nlk, val,
1653 optname == NETLINK_ADD_MEMBERSHIP);
1654 netlink_table_ungrab();
1655 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
1656 nlk->netlink_unbind(sock_net(sk), val);
1661 case NETLINK_BROADCAST_ERROR:
1663 nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
1665 nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
1668 case NETLINK_NO_ENOBUFS:
1670 nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
1671 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
1672 wake_up_interruptible(&nlk->wait);
1674 nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
1678 case NETLINK_LISTEN_ALL_NSID:
1679 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
1683 nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
1685 nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
1688 case NETLINK_CAP_ACK:
1690 nlk->flags |= NETLINK_F_CAP_ACK;
1692 nlk->flags &= ~NETLINK_F_CAP_ACK;
1695 case NETLINK_EXT_ACK:
1697 nlk->flags |= NETLINK_F_EXT_ACK;
1699 nlk->flags &= ~NETLINK_F_EXT_ACK;
1708 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1709 char __user *optval, int __user *optlen)
1711 struct sock *sk = sock->sk;
1712 struct netlink_sock *nlk = nlk_sk(sk);
1715 if (level != SOL_NETLINK)
1716 return -ENOPROTOOPT;
1718 if (get_user(len, optlen))
1724 case NETLINK_PKTINFO:
1725 if (len < sizeof(int))
1728 val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
1729 if (put_user(len, optlen) ||
1730 put_user(val, optval))
1734 case NETLINK_BROADCAST_ERROR:
1735 if (len < sizeof(int))
1738 val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
1739 if (put_user(len, optlen) ||
1740 put_user(val, optval))
1744 case NETLINK_NO_ENOBUFS:
1745 if (len < sizeof(int))
1748 val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
1749 if (put_user(len, optlen) ||
1750 put_user(val, optval))
1754 case NETLINK_LIST_MEMBERSHIPS: {
1755 int pos, idx, shift;
1758 netlink_lock_table();
1759 for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
1760 if (len - pos < sizeof(u32))
1763 idx = pos / sizeof(unsigned long);
1764 shift = (pos % sizeof(unsigned long)) * 8;
1765 if (put_user((u32)(nlk->groups[idx] >> shift),
1766 (u32 __user *)(optval + pos))) {
1771 if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
1773 netlink_unlock_table();
1776 case NETLINK_CAP_ACK:
1777 if (len < sizeof(int))
1780 val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
1781 if (put_user(len, optlen) ||
1782 put_user(val, optval))
1786 case NETLINK_EXT_ACK:
1787 if (len < sizeof(int))
1790 val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
1791 if (put_user(len, optlen) || put_user(val, optval))
1801 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1803 struct nl_pktinfo info;
1805 info.group = NETLINK_CB(skb).dst_group;
1806 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1809 static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
1810 struct sk_buff *skb)
1812 if (!NETLINK_CB(skb).nsid_is_set)
1815 put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
1816 &NETLINK_CB(skb).nsid);
1819 static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1821 struct sock *sk = sock->sk;
1822 struct netlink_sock *nlk = nlk_sk(sk);
1823 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
1826 struct sk_buff *skb;
1828 struct scm_cookie scm;
1829 u32 netlink_skb_flags = 0;
1831 if (msg->msg_flags&MSG_OOB)
1835 pr_warn_once("Zero length message leads to an empty skb\n");
1839 err = scm_send(sock, msg, &scm, true);
1843 if (msg->msg_namelen) {
1845 if (msg->msg_namelen < sizeof(struct sockaddr_nl))
1847 if (addr->nl_family != AF_NETLINK)
1849 dst_portid = addr->nl_pid;
1850 dst_group = ffs(addr->nl_groups);
1852 if ((dst_group || dst_portid) &&
1853 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
1855 netlink_skb_flags |= NETLINK_SKB_DST;
1857 /* Paired with WRITE_ONCE() in netlink_connect() */
1858 dst_portid = READ_ONCE(nlk->dst_portid);
1859 dst_group = READ_ONCE(nlk->dst_group);
1862 /* Paired with WRITE_ONCE() in netlink_insert() */
1863 if (!READ_ONCE(nlk->bound)) {
1864 err = netlink_autobind(sock);
1868 /* Ensure nlk is hashed and visible. */
1873 if (len > sk->sk_sndbuf - 32)
1876 skb = netlink_alloc_large_skb(len, dst_group);
1880 NETLINK_CB(skb).portid = nlk->portid;
1881 NETLINK_CB(skb).dst_group = dst_group;
1882 NETLINK_CB(skb).creds = scm.creds;
1883 NETLINK_CB(skb).flags = netlink_skb_flags;
1886 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
1891 err = security_netlink_send(sk, skb);
1898 refcount_inc(&skb->users);
1899 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1901 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1908 static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1911 struct scm_cookie scm;
1912 struct sock *sk = sock->sk;
1913 struct netlink_sock *nlk = nlk_sk(sk);
1914 int noblock = flags&MSG_DONTWAIT;
1916 struct sk_buff *skb, *data_skb;
1924 skb = skb_recv_datagram(sk, flags, noblock, &err);
1930 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1931 if (unlikely(skb_shinfo(skb)->frag_list)) {
1933 * If this skb has a frag_list, then here that means that we
1934 * will have to use the frag_list skb's data for compat tasks
1935 * and the regular skb's data for normal (non-compat) tasks.
1937 * If we need to send the compat skb, assign it to the
1938 * 'data_skb' variable so that it will be used below for data
1939 * copying. We keep 'skb' for everything else, including
1940 * freeing both later.
1942 if (flags & MSG_CMSG_COMPAT)
1943 data_skb = skb_shinfo(skb)->frag_list;
1947 /* Record the max length of recvmsg() calls for future allocations */
1948 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
1949 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
1950 SKB_WITH_OVERHEAD(32768));
1952 copied = data_skb->len;
1954 msg->msg_flags |= MSG_TRUNC;
1958 err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
1960 if (msg->msg_name) {
1961 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
1962 addr->nl_family = AF_NETLINK;
1964 addr->nl_pid = NETLINK_CB(skb).portid;
1965 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1966 msg->msg_namelen = sizeof(*addr);
1969 if (nlk->flags & NETLINK_F_RECV_PKTINFO)
1970 netlink_cmsg_recv_pktinfo(msg, skb);
1971 if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
1972 netlink_cmsg_listen_all_nsid(sk, msg, skb);
1974 memset(&scm, 0, sizeof(scm));
1975 scm.creds = *NETLINK_CREDS(skb);
1976 if (flags & MSG_TRUNC)
1977 copied = data_skb->len;
1979 skb_free_datagram(sk, skb);
1981 if (READ_ONCE(nlk->cb_running) &&
1982 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1983 ret = netlink_dump(sk);
1986 sk->sk_error_report(sk);
1990 scm_recv(sock, msg, &scm, flags);
1992 netlink_rcv_wake(sk);
1993 return err ? : copied;
1996 static void netlink_data_ready(struct sock *sk)
2002 * We export these functions to other modules. They provide a
2003 * complete set of kernel non-blocking support for message
2008 __netlink_kernel_create(struct net *net, int unit, struct module *module,
2009 struct netlink_kernel_cfg *cfg)
2011 struct socket *sock;
2013 struct netlink_sock *nlk;
2014 struct listeners *listeners = NULL;
2015 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2016 unsigned int groups;
2020 if (unit < 0 || unit >= MAX_LINKS)
2023 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2026 if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
2027 goto out_sock_release_nosk;
2031 if (!cfg || cfg->groups < 32)
2034 groups = cfg->groups;
2036 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2038 goto out_sock_release;
2040 sk->sk_data_ready = netlink_data_ready;
2041 if (cfg && cfg->input)
2042 nlk_sk(sk)->netlink_rcv = cfg->input;
2044 if (netlink_insert(sk, 0))
2045 goto out_sock_release;
2048 nlk->flags |= NETLINK_F_KERNEL_SOCKET;
2050 netlink_table_grab();
2051 if (!nl_table[unit].registered) {
2052 nl_table[unit].groups = groups;
2053 rcu_assign_pointer(nl_table[unit].listeners, listeners);
2054 nl_table[unit].cb_mutex = cb_mutex;
2055 nl_table[unit].module = module;
2057 nl_table[unit].bind = cfg->bind;
2058 nl_table[unit].unbind = cfg->unbind;
2059 nl_table[unit].flags = cfg->flags;
2061 nl_table[unit].compare = cfg->compare;
2063 nl_table[unit].registered = 1;
2066 nl_table[unit].registered++;
2068 netlink_table_ungrab();
2073 netlink_kernel_release(sk);
2076 out_sock_release_nosk:
2080 EXPORT_SYMBOL(__netlink_kernel_create);
2083 netlink_kernel_release(struct sock *sk)
2085 if (sk == NULL || sk->sk_socket == NULL)
2088 sock_release(sk->sk_socket);
2090 EXPORT_SYMBOL(netlink_kernel_release);
2092 int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
2094 struct listeners *new, *old;
2095 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
2100 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
2101 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2104 old = nl_deref_protected(tbl->listeners);
2105 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2106 rcu_assign_pointer(tbl->listeners, new);
2108 kfree_rcu(old, rcu);
2110 tbl->groups = groups;
2116 * netlink_change_ngroups - change number of multicast groups
2118 * This changes the number of multicast groups that are available
2119 * on a certain netlink family. Note that it is not possible to
2120 * change the number of groups to below 32. Also note that it does
2121 * not implicitly call netlink_clear_multicast_users() when the
2122 * number of groups is reduced.
2124 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2125 * @groups: The new number of groups.
2127 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2131 netlink_table_grab();
2132 err = __netlink_change_ngroups(sk, groups);
2133 netlink_table_ungrab();
2138 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2141 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2143 sk_for_each_bound(sk, &tbl->mc_list)
2144 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2148 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
2150 struct nlmsghdr *nlh;
2151 int size = nlmsg_msg_size(len);
2153 nlh = skb_put(skb, NLMSG_ALIGN(size));
2154 nlh->nlmsg_type = type;
2155 nlh->nlmsg_len = size;
2156 nlh->nlmsg_flags = flags;
2157 nlh->nlmsg_pid = portid;
2158 nlh->nlmsg_seq = seq;
2159 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
2160 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
2163 EXPORT_SYMBOL(__nlmsg_put);
2166 * It looks a bit ugly.
2167 * It would be better to create kernel thread.
2170 static int netlink_dump(struct sock *sk)
2172 struct netlink_sock *nlk = nlk_sk(sk);
2173 struct netlink_callback *cb;
2174 struct sk_buff *skb = NULL;
2175 struct nlmsghdr *nlh;
2176 struct module *module;
2181 mutex_lock(nlk->cb_mutex);
2182 if (!nlk->cb_running) {
2187 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2190 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2191 * required, but it makes sense to _attempt_ a 16K bytes allocation
2192 * to reduce number of system calls on dump operations, if user
2193 * ever provided a big enough buffer.
2196 alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2198 if (alloc_min_size < nlk->max_recvmsg_len) {
2199 alloc_size = nlk->max_recvmsg_len;
2200 skb = alloc_skb(alloc_size,
2201 (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
2202 __GFP_NOWARN | __GFP_NORETRY);
2205 alloc_size = alloc_min_size;
2206 skb = alloc_skb(alloc_size, GFP_KERNEL);
2211 /* Trim skb to allocated size. User is expected to provide buffer as
2212 * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
2213 * netlink_recvmsg())). dump will pack as many smaller messages as
2214 * could fit within the allocated skb. skb is typically allocated
2215 * with larger space than required (could be as much as near 2x the
2216 * requested size with align to next power of 2 approach). Allowing
2217 * dump to use the excess space makes it difficult for a user to have a
2218 * reasonable static buffer based on the expected largest dump of a
2219 * single netdev. The outcome is MSG_TRUNC error.
2221 skb_reserve(skb, skb_tailroom(skb) - alloc_size);
2223 /* Make sure malicious BPF programs can not read unitialized memory
2224 * from skb->head -> skb->data
2226 skb_reset_network_header(skb);
2227 skb_reset_mac_header(skb);
2229 netlink_skb_set_owner_r(skb, sk);
2231 if (nlk->dump_done_errno > 0)
2232 nlk->dump_done_errno = cb->dump(skb, cb);
2234 if (nlk->dump_done_errno > 0 ||
2235 skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) {
2236 mutex_unlock(nlk->cb_mutex);
2238 if (sk_filter(sk, skb))
2241 __netlink_sendskb(sk, skb);
2245 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE,
2246 sizeof(nlk->dump_done_errno), NLM_F_MULTI);
2250 nl_dump_check_consistent(cb, nlh);
2252 memcpy(nlmsg_data(nlh), &nlk->dump_done_errno,
2253 sizeof(nlk->dump_done_errno));
2255 if (sk_filter(sk, skb))
2258 __netlink_sendskb(sk, skb);
2263 WRITE_ONCE(nlk->cb_running, false);
2264 module = cb->module;
2266 mutex_unlock(nlk->cb_mutex);
2272 mutex_unlock(nlk->cb_mutex);
2277 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2278 const struct nlmsghdr *nlh,
2279 struct netlink_dump_control *control)
2281 struct netlink_callback *cb;
2283 struct netlink_sock *nlk;
2286 refcount_inc(&skb->users);
2288 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
2290 ret = -ECONNREFUSED;
2295 mutex_lock(nlk->cb_mutex);
2296 /* A dump is in progress... */
2297 if (nlk->cb_running) {
2301 /* add reference of module which cb->dump belongs to */
2302 if (!try_module_get(control->module)) {
2303 ret = -EPROTONOSUPPORT;
2308 memset(cb, 0, sizeof(*cb));
2309 cb->start = control->start;
2310 cb->dump = control->dump;
2311 cb->done = control->done;
2313 cb->data = control->data;
2314 cb->module = control->module;
2315 cb->min_dump_alloc = control->min_dump_alloc;
2319 ret = cb->start(cb);
2324 WRITE_ONCE(nlk->cb_running, true);
2325 nlk->dump_done_errno = INT_MAX;
2327 mutex_unlock(nlk->cb_mutex);
2329 ret = netlink_dump(sk);
2336 /* We successfully started a dump, by returning -EINTR we
2337 * signal not to send ACK even if it was requested.
2342 module_put(control->module);
2345 mutex_unlock(nlk->cb_mutex);
2350 EXPORT_SYMBOL(__netlink_dump_start);
2352 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
2353 const struct netlink_ext_ack *extack)
2355 struct sk_buff *skb;
2356 struct nlmsghdr *rep;
2357 struct nlmsgerr *errmsg;
2358 size_t payload = sizeof(*errmsg);
2360 struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
2361 unsigned int flags = 0;
2362 bool nlk_has_extack = nlk->flags & NETLINK_F_EXT_ACK;
2364 /* Error messages get the original request appened, unless the user
2365 * requests to cap the error message, and get extra error data if
2369 if (!(nlk->flags & NETLINK_F_CAP_ACK))
2370 payload += nlmsg_len(nlh);
2372 flags |= NLM_F_CAPPED;
2373 if (nlk_has_extack && extack) {
2375 tlvlen += nla_total_size(strlen(extack->_msg) + 1);
2376 if (extack->bad_attr)
2377 tlvlen += nla_total_size(sizeof(u32));
2380 flags |= NLM_F_CAPPED;
2382 if (nlk_has_extack && extack && extack->cookie_len)
2383 tlvlen += nla_total_size(extack->cookie_len);
2387 flags |= NLM_F_ACK_TLVS;
2389 skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
2393 sk = netlink_lookup(sock_net(in_skb->sk),
2394 in_skb->sk->sk_protocol,
2395 NETLINK_CB(in_skb).portid);
2397 sk->sk_err = ENOBUFS;
2398 sk->sk_error_report(sk);
2404 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
2405 NLMSG_ERROR, payload, flags);
2406 errmsg = nlmsg_data(rep);
2407 errmsg->error = err;
2408 memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
2410 if (nlk_has_extack && extack) {
2413 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
2415 if (extack->bad_attr &&
2416 !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
2417 (u8 *)extack->bad_attr >= in_skb->data +
2419 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
2420 (u8 *)extack->bad_attr -
2423 if (extack->cookie_len)
2424 WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
2430 nlmsg_end(skb, rep);
2432 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
2434 EXPORT_SYMBOL(netlink_ack);
2436 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
2438 struct netlink_ext_ack *))
2440 struct netlink_ext_ack extack;
2441 struct nlmsghdr *nlh;
2444 while (skb->len >= nlmsg_total_size(0)) {
2447 memset(&extack, 0, sizeof(extack));
2448 nlh = nlmsg_hdr(skb);
2451 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
2454 /* Only requests are handled by the kernel */
2455 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
2458 /* Skip control messages */
2459 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
2462 err = cb(skb, nlh, &extack);
2467 if (nlh->nlmsg_flags & NLM_F_ACK || err)
2468 netlink_ack(skb, nlh, err, &extack);
2471 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
2472 if (msglen > skb->len)
2474 skb_pull(skb, msglen);
2479 EXPORT_SYMBOL(netlink_rcv_skb);
2482 * nlmsg_notify - send a notification netlink message
2483 * @sk: netlink socket to use
2484 * @skb: notification message
2485 * @portid: destination netlink portid for reports or 0
2486 * @group: destination multicast group or 0
2487 * @report: 1 to report back, 0 to disable
2488 * @flags: allocation flags
2490 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
2491 unsigned int group, int report, gfp_t flags)
2496 int exclude_portid = 0;
2499 refcount_inc(&skb->users);
2500 exclude_portid = portid;
2503 /* errors reported via destination sk->sk_err, but propagate
2504 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
2505 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
2513 err2 = nlmsg_unicast(sk, skb, portid);
2520 EXPORT_SYMBOL(nlmsg_notify);
2522 #ifdef CONFIG_PROC_FS
2523 struct nl_seq_iter {
2524 struct seq_net_private p;
2525 struct rhashtable_iter hti;
2529 static int netlink_walk_start(struct nl_seq_iter *iter)
2533 err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
2536 iter->link = MAX_LINKS;
2540 err = rhashtable_walk_start(&iter->hti);
2541 return err == -EAGAIN ? 0 : err;
2544 static void netlink_walk_stop(struct nl_seq_iter *iter)
2546 rhashtable_walk_stop(&iter->hti);
2547 rhashtable_walk_exit(&iter->hti);
2550 static void *__netlink_seq_next(struct seq_file *seq)
2552 struct nl_seq_iter *iter = seq->private;
2553 struct netlink_sock *nlk;
2559 nlk = rhashtable_walk_next(&iter->hti);
2562 if (PTR_ERR(nlk) == -EAGAIN)
2571 netlink_walk_stop(iter);
2572 if (++iter->link >= MAX_LINKS)
2575 err = netlink_walk_start(iter);
2577 return ERR_PTR(err);
2579 } while (sock_net(&nlk->sk) != seq_file_net(seq));
2584 static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
2586 struct nl_seq_iter *iter = seq->private;
2587 void *obj = SEQ_START_TOKEN;
2593 err = netlink_walk_start(iter);
2595 return ERR_PTR(err);
2597 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
2598 obj = __netlink_seq_next(seq);
2603 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2606 return __netlink_seq_next(seq);
2609 static void netlink_seq_stop(struct seq_file *seq, void *v)
2611 struct nl_seq_iter *iter = seq->private;
2613 if (iter->link >= MAX_LINKS)
2616 netlink_walk_stop(iter);
2620 static int netlink_seq_show(struct seq_file *seq, void *v)
2622 if (v == SEQ_START_TOKEN) {
2624 "sk Eth Pid Groups "
2625 "Rmem Wmem Dump Locks Drops Inode\n");
2628 struct netlink_sock *nlk = nlk_sk(s);
2630 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
2634 nlk->groups ? (u32)nlk->groups[0] : 0,
2635 sk_rmem_alloc_get(s),
2636 sk_wmem_alloc_get(s),
2637 READ_ONCE(nlk->cb_running),
2638 refcount_read(&s->sk_refcnt),
2639 atomic_read(&s->sk_drops),
2647 static const struct seq_operations netlink_seq_ops = {
2648 .start = netlink_seq_start,
2649 .next = netlink_seq_next,
2650 .stop = netlink_seq_stop,
2651 .show = netlink_seq_show,
2655 static int netlink_seq_open(struct inode *inode, struct file *file)
2657 return seq_open_net(inode, file, &netlink_seq_ops,
2658 sizeof(struct nl_seq_iter));
2661 static const struct file_operations netlink_seq_fops = {
2662 .owner = THIS_MODULE,
2663 .open = netlink_seq_open,
2665 .llseek = seq_lseek,
2666 .release = seq_release_net,
2671 int netlink_register_notifier(struct notifier_block *nb)
2673 return blocking_notifier_chain_register(&netlink_chain, nb);
2675 EXPORT_SYMBOL(netlink_register_notifier);
2677 int netlink_unregister_notifier(struct notifier_block *nb)
2679 return blocking_notifier_chain_unregister(&netlink_chain, nb);
2681 EXPORT_SYMBOL(netlink_unregister_notifier);
2683 static const struct proto_ops netlink_ops = {
2684 .family = PF_NETLINK,
2685 .owner = THIS_MODULE,
2686 .release = netlink_release,
2687 .bind = netlink_bind,
2688 .connect = netlink_connect,
2689 .socketpair = sock_no_socketpair,
2690 .accept = sock_no_accept,
2691 .getname = netlink_getname,
2692 .poll = datagram_poll,
2693 .ioctl = netlink_ioctl,
2694 .listen = sock_no_listen,
2695 .shutdown = sock_no_shutdown,
2696 .setsockopt = netlink_setsockopt,
2697 .getsockopt = netlink_getsockopt,
2698 .sendmsg = netlink_sendmsg,
2699 .recvmsg = netlink_recvmsg,
2700 .mmap = sock_no_mmap,
2701 .sendpage = sock_no_sendpage,
2704 static const struct net_proto_family netlink_family_ops = {
2705 .family = PF_NETLINK,
2706 .create = netlink_create,
2707 .owner = THIS_MODULE, /* for consistency 8) */
2710 static int __net_init netlink_net_init(struct net *net)
2712 #ifdef CONFIG_PROC_FS
2713 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
2719 static void __net_exit netlink_net_exit(struct net *net)
2721 #ifdef CONFIG_PROC_FS
2722 remove_proc_entry("netlink", net->proc_net);
2726 static void __init netlink_add_usersock_entry(void)
2728 struct listeners *listeners;
2731 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2733 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2735 netlink_table_grab();
2737 nl_table[NETLINK_USERSOCK].groups = groups;
2738 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
2739 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2740 nl_table[NETLINK_USERSOCK].registered = 1;
2741 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
2743 netlink_table_ungrab();
2746 static struct pernet_operations __net_initdata netlink_net_ops = {
2747 .init = netlink_net_init,
2748 .exit = netlink_net_exit,
2751 static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
2753 const struct netlink_sock *nlk = data;
2754 struct netlink_compare_arg arg;
2756 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
2757 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
2760 static const struct rhashtable_params netlink_rhashtable_params = {
2761 .head_offset = offsetof(struct netlink_sock, node),
2762 .key_len = netlink_compare_arg_len,
2763 .obj_hashfn = netlink_hash,
2764 .obj_cmpfn = netlink_compare,
2765 .automatic_shrinking = true,
2768 static int __init netlink_proto_init(void)
2771 int err = proto_register(&netlink_proto, 0);
2776 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
2778 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
2782 for (i = 0; i < MAX_LINKS; i++) {
2783 if (rhashtable_init(&nl_table[i].hash,
2784 &netlink_rhashtable_params) < 0) {
2786 rhashtable_destroy(&nl_table[i].hash);
2792 INIT_LIST_HEAD(&netlink_tap_all);
2794 netlink_add_usersock_entry();
2796 sock_register(&netlink_family_ops);
2797 register_pernet_subsys(&netlink_net_ops);
2798 /* The netlink device handler may be needed early. */
2803 panic("netlink_init: Cannot allocate nl_table\n");
2806 core_initcall(netlink_proto_init);