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)
157 return group ? 1 << (group - 1) : 0;
160 static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb,
163 unsigned int len = skb_end_offset(skb);
166 new = alloc_skb(len, gfp_mask);
170 NETLINK_CB(new).portid = NETLINK_CB(skb).portid;
171 NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group;
172 NETLINK_CB(new).creds = NETLINK_CB(skb).creds;
174 skb_put_data(new, skb->data, len);
178 int netlink_add_tap(struct netlink_tap *nt)
180 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
183 spin_lock(&netlink_tap_lock);
184 list_add_rcu(&nt->list, &netlink_tap_all);
185 spin_unlock(&netlink_tap_lock);
187 __module_get(nt->module);
191 EXPORT_SYMBOL_GPL(netlink_add_tap);
193 static int __netlink_remove_tap(struct netlink_tap *nt)
196 struct netlink_tap *tmp;
198 spin_lock(&netlink_tap_lock);
200 list_for_each_entry(tmp, &netlink_tap_all, list) {
202 list_del_rcu(&nt->list);
208 pr_warn("__netlink_remove_tap: %p not found\n", nt);
210 spin_unlock(&netlink_tap_lock);
213 module_put(nt->module);
215 return found ? 0 : -ENODEV;
218 int netlink_remove_tap(struct netlink_tap *nt)
222 ret = __netlink_remove_tap(nt);
227 EXPORT_SYMBOL_GPL(netlink_remove_tap);
229 static bool netlink_filter_tap(const struct sk_buff *skb)
231 struct sock *sk = skb->sk;
233 /* We take the more conservative approach and
234 * whitelist socket protocols that may pass.
236 switch (sk->sk_protocol) {
238 case NETLINK_USERSOCK:
239 case NETLINK_SOCK_DIAG:
242 case NETLINK_FIB_LOOKUP:
243 case NETLINK_NETFILTER:
244 case NETLINK_GENERIC:
251 static int __netlink_deliver_tap_skb(struct sk_buff *skb,
252 struct net_device *dev)
254 struct sk_buff *nskb;
255 struct sock *sk = skb->sk;
258 if (!net_eq(dev_net(dev), sock_net(sk)))
263 if (is_vmalloc_addr(skb->head))
264 nskb = netlink_to_full_skb(skb, GFP_ATOMIC);
266 nskb = skb_clone(skb, GFP_ATOMIC);
269 nskb->protocol = htons((u16) sk->sk_protocol);
270 nskb->pkt_type = netlink_is_kernel(sk) ?
271 PACKET_KERNEL : PACKET_USER;
272 skb_reset_network_header(nskb);
273 ret = dev_queue_xmit(nskb);
274 if (unlikely(ret > 0))
275 ret = net_xmit_errno(ret);
282 static void __netlink_deliver_tap(struct sk_buff *skb)
285 struct netlink_tap *tmp;
287 if (!netlink_filter_tap(skb))
290 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
291 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
297 static void netlink_deliver_tap(struct sk_buff *skb)
301 if (unlikely(!list_empty(&netlink_tap_all)))
302 __netlink_deliver_tap(skb);
307 static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
310 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
311 netlink_deliver_tap(skb);
314 static void netlink_overrun(struct sock *sk)
316 struct netlink_sock *nlk = nlk_sk(sk);
318 if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
319 if (!test_and_set_bit(NETLINK_S_CONGESTED,
320 &nlk_sk(sk)->state)) {
321 sk->sk_err = ENOBUFS;
322 sk->sk_error_report(sk);
325 atomic_inc(&sk->sk_drops);
328 static void netlink_rcv_wake(struct sock *sk)
330 struct netlink_sock *nlk = nlk_sk(sk);
332 if (skb_queue_empty(&sk->sk_receive_queue))
333 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
334 if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
335 wake_up_interruptible(&nlk->wait);
338 static void netlink_skb_destructor(struct sk_buff *skb)
340 if (is_vmalloc_addr(skb->head)) {
342 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
351 static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
353 WARN_ON(skb->sk != NULL);
355 skb->destructor = netlink_skb_destructor;
356 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
357 sk_mem_charge(sk, skb->truesize);
360 static void netlink_sock_destruct(struct sock *sk)
362 struct netlink_sock *nlk = nlk_sk(sk);
364 if (nlk->cb_running) {
366 nlk->cb.done(&nlk->cb);
367 module_put(nlk->cb.module);
368 kfree_skb(nlk->cb.skb);
371 skb_queue_purge(&sk->sk_receive_queue);
373 if (!sock_flag(sk, SOCK_DEAD)) {
374 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
378 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
379 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
380 WARN_ON(nlk_sk(sk)->groups);
383 static void netlink_sock_destruct_work(struct work_struct *work)
385 struct netlink_sock *nlk = container_of(work, struct netlink_sock,
391 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
392 * SMP. Look, when several writers sleep and reader wakes them up, all but one
393 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
394 * this, _but_ remember, it adds useless work on UP machines.
397 void netlink_table_grab(void)
398 __acquires(nl_table_lock)
402 write_lock_irq(&nl_table_lock);
404 if (atomic_read(&nl_table_users)) {
405 DECLARE_WAITQUEUE(wait, current);
407 add_wait_queue_exclusive(&nl_table_wait, &wait);
409 set_current_state(TASK_UNINTERRUPTIBLE);
410 if (atomic_read(&nl_table_users) == 0)
412 write_unlock_irq(&nl_table_lock);
414 write_lock_irq(&nl_table_lock);
417 __set_current_state(TASK_RUNNING);
418 remove_wait_queue(&nl_table_wait, &wait);
422 void netlink_table_ungrab(void)
423 __releases(nl_table_lock)
425 write_unlock_irq(&nl_table_lock);
426 wake_up(&nl_table_wait);
430 netlink_lock_table(void)
434 /* read_lock() synchronizes us to netlink_table_grab */
436 read_lock_irqsave(&nl_table_lock, flags);
437 atomic_inc(&nl_table_users);
438 read_unlock_irqrestore(&nl_table_lock, flags);
442 netlink_unlock_table(void)
444 if (atomic_dec_and_test(&nl_table_users))
445 wake_up(&nl_table_wait);
448 struct netlink_compare_arg
454 /* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
455 #define netlink_compare_arg_len \
456 (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
458 static inline int netlink_compare(struct rhashtable_compare_arg *arg,
461 const struct netlink_compare_arg *x = arg->key;
462 const struct netlink_sock *nlk = ptr;
464 return nlk->portid != x->portid ||
465 !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
468 static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
469 struct net *net, u32 portid)
471 memset(arg, 0, sizeof(*arg));
472 write_pnet(&arg->pnet, net);
473 arg->portid = portid;
476 static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
479 struct netlink_compare_arg arg;
481 netlink_compare_arg_init(&arg, net, portid);
482 return rhashtable_lookup_fast(&table->hash, &arg,
483 netlink_rhashtable_params);
486 static int __netlink_insert(struct netlink_table *table, struct sock *sk)
488 struct netlink_compare_arg arg;
490 netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
491 return rhashtable_lookup_insert_key(&table->hash, &arg,
493 netlink_rhashtable_params);
496 static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
498 struct netlink_table *table = &nl_table[protocol];
502 sk = __netlink_lookup(table, portid, net);
510 static const struct proto_ops netlink_ops;
513 netlink_update_listeners(struct sock *sk)
515 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
518 struct listeners *listeners;
520 listeners = nl_deref_protected(tbl->listeners);
524 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
526 sk_for_each_bound(sk, &tbl->mc_list) {
527 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
528 mask |= nlk_sk(sk)->groups[i];
530 listeners->masks[i] = mask;
532 /* this function is only called with the netlink table "grabbed", which
533 * makes sure updates are visible before bind or setsockopt return. */
536 static int netlink_insert(struct sock *sk, u32 portid)
538 struct netlink_table *table = &nl_table[sk->sk_protocol];
543 err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY;
544 if (nlk_sk(sk)->bound)
548 if (BITS_PER_LONG > 32 &&
549 unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
552 nlk_sk(sk)->portid = portid;
555 err = __netlink_insert(table, sk);
557 /* In case the hashtable backend returns with -EBUSY
558 * from here, it must not escape to the caller.
560 if (unlikely(err == -EBUSY))
568 /* We need to ensure that the socket is hashed and visible. */
570 /* Paired with lockless reads from netlink_bind(),
571 * netlink_connect() and netlink_sendmsg().
573 WRITE_ONCE(nlk_sk(sk)->bound, portid);
580 static void netlink_remove(struct sock *sk)
582 struct netlink_table *table;
584 table = &nl_table[sk->sk_protocol];
585 if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
586 netlink_rhashtable_params)) {
587 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
591 netlink_table_grab();
592 if (nlk_sk(sk)->subscriptions) {
593 __sk_del_bind_node(sk);
594 netlink_update_listeners(sk);
596 if (sk->sk_protocol == NETLINK_GENERIC)
597 atomic_inc(&genl_sk_destructing_cnt);
598 netlink_table_ungrab();
601 static struct proto netlink_proto = {
603 .owner = THIS_MODULE,
604 .obj_size = sizeof(struct netlink_sock),
607 static int __netlink_create(struct net *net, struct socket *sock,
608 struct mutex *cb_mutex, int protocol,
612 struct netlink_sock *nlk;
614 sock->ops = &netlink_ops;
616 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
620 sock_init_data(sock, sk);
624 nlk->cb_mutex = cb_mutex;
626 nlk->cb_mutex = &nlk->cb_def_mutex;
627 mutex_init(nlk->cb_mutex);
628 lockdep_set_class_and_name(nlk->cb_mutex,
629 nlk_cb_mutex_keys + protocol,
630 nlk_cb_mutex_key_strings[protocol]);
632 init_waitqueue_head(&nlk->wait);
634 sk->sk_destruct = netlink_sock_destruct;
635 sk->sk_protocol = protocol;
639 static int netlink_create(struct net *net, struct socket *sock, int protocol,
642 struct module *module = NULL;
643 struct mutex *cb_mutex;
644 struct netlink_sock *nlk;
645 int (*bind)(struct net *net, int group);
646 void (*unbind)(struct net *net, int group);
649 sock->state = SS_UNCONNECTED;
651 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
652 return -ESOCKTNOSUPPORT;
654 if (protocol < 0 || protocol >= MAX_LINKS)
655 return -EPROTONOSUPPORT;
656 protocol = array_index_nospec(protocol, MAX_LINKS);
658 netlink_lock_table();
659 #ifdef CONFIG_MODULES
660 if (!nl_table[protocol].registered) {
661 netlink_unlock_table();
662 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
663 netlink_lock_table();
666 if (nl_table[protocol].registered &&
667 try_module_get(nl_table[protocol].module))
668 module = nl_table[protocol].module;
670 err = -EPROTONOSUPPORT;
671 cb_mutex = nl_table[protocol].cb_mutex;
672 bind = nl_table[protocol].bind;
673 unbind = nl_table[protocol].unbind;
674 netlink_unlock_table();
679 err = __netlink_create(net, sock, cb_mutex, protocol, kern);
684 sock_prot_inuse_add(net, &netlink_proto, 1);
687 nlk = nlk_sk(sock->sk);
688 nlk->module = module;
689 nlk->netlink_bind = bind;
690 nlk->netlink_unbind = unbind;
699 static void deferred_put_nlk_sk(struct rcu_head *head)
701 struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
702 struct sock *sk = &nlk->sk;
707 if (!refcount_dec_and_test(&sk->sk_refcnt))
710 if (nlk->cb_running && nlk->cb.done) {
711 INIT_WORK(&nlk->work, netlink_sock_destruct_work);
712 schedule_work(&nlk->work);
719 static int netlink_release(struct socket *sock)
721 struct sock *sk = sock->sk;
722 struct netlink_sock *nlk;
732 * OK. Socket is unlinked, any packets that arrive now
736 /* must not acquire netlink_table_lock in any way again before unbind
737 * and notifying genetlink is done as otherwise it might deadlock
739 if (nlk->netlink_unbind) {
742 for (i = 0; i < nlk->ngroups; i++)
743 if (test_bit(i, nlk->groups))
744 nlk->netlink_unbind(sock_net(sk), i + 1);
746 if (sk->sk_protocol == NETLINK_GENERIC &&
747 atomic_dec_return(&genl_sk_destructing_cnt) == 0)
748 wake_up(&genl_sk_destructing_waitq);
751 wake_up_interruptible_all(&nlk->wait);
753 skb_queue_purge(&sk->sk_write_queue);
755 if (nlk->portid && nlk->bound) {
756 struct netlink_notify n = {
758 .protocol = sk->sk_protocol,
759 .portid = nlk->portid,
761 blocking_notifier_call_chain(&netlink_chain,
762 NETLINK_URELEASE, &n);
765 module_put(nlk->module);
767 if (netlink_is_kernel(sk)) {
768 netlink_table_grab();
769 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
770 if (--nl_table[sk->sk_protocol].registered == 0) {
771 struct listeners *old;
773 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
774 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
776 nl_table[sk->sk_protocol].module = NULL;
777 nl_table[sk->sk_protocol].bind = NULL;
778 nl_table[sk->sk_protocol].unbind = NULL;
779 nl_table[sk->sk_protocol].flags = 0;
780 nl_table[sk->sk_protocol].registered = 0;
782 netlink_table_ungrab();
786 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
788 call_rcu(&nlk->rcu, deferred_put_nlk_sk);
792 static int netlink_autobind(struct socket *sock)
794 struct sock *sk = sock->sk;
795 struct net *net = sock_net(sk);
796 struct netlink_table *table = &nl_table[sk->sk_protocol];
797 s32 portid = task_tgid_vnr(current);
805 ok = !__netlink_lookup(table, portid, net);
808 /* Bind collision, search negative portid values. */
810 /* rover will be in range [S32_MIN, -4097] */
811 rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
812 else if (rover >= -4096)
818 err = netlink_insert(sk, portid);
819 if (err == -EADDRINUSE)
822 /* If 2 threads race to autobind, that is fine. */
830 * __netlink_ns_capable - General netlink message capability test
831 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
832 * @user_ns: The user namespace of the capability to use
833 * @cap: The capability to use
835 * Test to see if the opener of the socket we received the message
836 * from had when the netlink socket was created and the sender of the
837 * message has has the capability @cap in the user namespace @user_ns.
839 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
840 struct user_namespace *user_ns, int cap)
842 return ((nsp->flags & NETLINK_SKB_DST) ||
843 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
844 ns_capable(user_ns, cap);
846 EXPORT_SYMBOL(__netlink_ns_capable);
849 * netlink_ns_capable - General netlink message capability test
850 * @skb: socket buffer holding a netlink command from userspace
851 * @user_ns: The user namespace of the capability to use
852 * @cap: The capability to use
854 * Test to see if the opener of the socket we received the message
855 * from had when the netlink socket was created and the sender of the
856 * message has has the capability @cap in the user namespace @user_ns.
858 bool netlink_ns_capable(const struct sk_buff *skb,
859 struct user_namespace *user_ns, int cap)
861 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
863 EXPORT_SYMBOL(netlink_ns_capable);
866 * netlink_capable - Netlink global message capability test
867 * @skb: socket buffer holding a netlink command from userspace
868 * @cap: The capability to use
870 * Test to see if the opener of the socket we received the message
871 * from had when the netlink socket was created and the sender of the
872 * message has has the capability @cap in all user namespaces.
874 bool netlink_capable(const struct sk_buff *skb, int cap)
876 return netlink_ns_capable(skb, &init_user_ns, cap);
878 EXPORT_SYMBOL(netlink_capable);
881 * netlink_net_capable - Netlink network namespace message capability test
882 * @skb: socket buffer holding a netlink command from userspace
883 * @cap: The capability to use
885 * Test to see if the opener of the socket we received the message
886 * from had when the netlink socket was created and the sender of the
887 * message has has the capability @cap over the network namespace of
888 * the socket we received the message from.
890 bool netlink_net_capable(const struct sk_buff *skb, int cap)
892 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
894 EXPORT_SYMBOL(netlink_net_capable);
896 static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
898 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
899 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
903 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
905 struct netlink_sock *nlk = nlk_sk(sk);
907 if (nlk->subscriptions && !subscriptions)
908 __sk_del_bind_node(sk);
909 else if (!nlk->subscriptions && subscriptions)
910 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
911 nlk->subscriptions = subscriptions;
914 static int netlink_realloc_groups(struct sock *sk)
916 struct netlink_sock *nlk = nlk_sk(sk);
918 unsigned long *new_groups;
921 netlink_table_grab();
923 groups = nl_table[sk->sk_protocol].groups;
924 if (!nl_table[sk->sk_protocol].registered) {
929 if (nlk->ngroups >= groups)
932 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
933 if (new_groups == NULL) {
937 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
938 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
940 nlk->groups = new_groups;
941 nlk->ngroups = groups;
943 netlink_table_ungrab();
947 static void netlink_undo_bind(int group, long unsigned int groups,
950 struct netlink_sock *nlk = nlk_sk(sk);
953 if (!nlk->netlink_unbind)
956 for (undo = 0; undo < group; undo++)
957 if (test_bit(undo, &groups))
958 nlk->netlink_unbind(sock_net(sk), undo + 1);
961 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
964 struct sock *sk = sock->sk;
965 struct net *net = sock_net(sk);
966 struct netlink_sock *nlk = nlk_sk(sk);
967 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
969 long unsigned int groups = nladdr->nl_groups;
972 if (addr_len < sizeof(struct sockaddr_nl))
975 if (nladdr->nl_family != AF_NETLINK)
978 /* Only superuser is allowed to listen multicasts */
980 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
982 err = netlink_realloc_groups(sk);
987 if (nlk->ngroups == 0)
989 else if (nlk->ngroups < 8*sizeof(groups))
990 groups &= (1UL << nlk->ngroups) - 1;
992 /* Paired with WRITE_ONCE() in netlink_insert() */
993 bound = READ_ONCE(nlk->bound);
995 /* Ensure nlk->portid is up-to-date. */
998 if (nladdr->nl_pid != nlk->portid)
1002 netlink_lock_table();
1003 if (nlk->netlink_bind && groups) {
1006 /* nl_groups is a u32, so cap the maximum groups we can bind */
1007 for (group = 0; group < BITS_PER_TYPE(u32); group++) {
1008 if (!test_bit(group, &groups))
1010 err = nlk->netlink_bind(net, group + 1);
1013 netlink_undo_bind(group, groups, sk);
1018 /* No need for barriers here as we return to user-space without
1019 * using any of the bound attributes.
1022 err = nladdr->nl_pid ?
1023 netlink_insert(sk, nladdr->nl_pid) :
1024 netlink_autobind(sock);
1026 netlink_undo_bind(BITS_PER_TYPE(u32), groups, sk);
1031 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
1033 netlink_unlock_table();
1035 netlink_table_grab();
1036 netlink_update_subscriptions(sk, nlk->subscriptions +
1038 hweight32(nlk->groups[0]));
1039 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
1040 netlink_update_listeners(sk);
1041 netlink_table_ungrab();
1046 netlink_unlock_table();
1050 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1051 int alen, int flags)
1054 struct sock *sk = sock->sk;
1055 struct netlink_sock *nlk = nlk_sk(sk);
1056 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1058 if (alen < sizeof(addr->sa_family))
1061 if (addr->sa_family == AF_UNSPEC) {
1062 sk->sk_state = NETLINK_UNCONNECTED;
1063 nlk->dst_portid = 0;
1067 if (addr->sa_family != AF_NETLINK)
1070 if (alen < sizeof(struct sockaddr_nl))
1073 if ((nladdr->nl_groups || nladdr->nl_pid) &&
1074 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
1077 /* No need for barriers here as we return to user-space without
1078 * using any of the bound attributes.
1079 * Paired with WRITE_ONCE() in netlink_insert().
1081 if (!READ_ONCE(nlk->bound))
1082 err = netlink_autobind(sock);
1085 sk->sk_state = NETLINK_CONNECTED;
1086 nlk->dst_portid = nladdr->nl_pid;
1087 nlk->dst_group = ffs(nladdr->nl_groups);
1093 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1094 int *addr_len, int peer)
1096 struct sock *sk = sock->sk;
1097 struct netlink_sock *nlk = nlk_sk(sk);
1098 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
1100 nladdr->nl_family = AF_NETLINK;
1102 *addr_len = sizeof(*nladdr);
1105 nladdr->nl_pid = nlk->dst_portid;
1106 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
1108 nladdr->nl_pid = nlk->portid;
1109 netlink_lock_table();
1110 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
1111 netlink_unlock_table();
1116 static int netlink_ioctl(struct socket *sock, unsigned int cmd,
1119 /* try to hand this ioctl down to the NIC drivers.
1121 return -ENOIOCTLCMD;
1124 static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
1127 struct netlink_sock *nlk;
1129 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
1131 return ERR_PTR(-ECONNREFUSED);
1133 /* Don't bother queuing skb if kernel socket has no input function */
1135 if (sock->sk_state == NETLINK_CONNECTED &&
1136 nlk->dst_portid != nlk_sk(ssk)->portid) {
1138 return ERR_PTR(-ECONNREFUSED);
1143 struct sock *netlink_getsockbyfilp(struct file *filp)
1145 struct inode *inode = file_inode(filp);
1148 if (!S_ISSOCK(inode->i_mode))
1149 return ERR_PTR(-ENOTSOCK);
1151 sock = SOCKET_I(inode)->sk;
1152 if (sock->sk_family != AF_NETLINK)
1153 return ERR_PTR(-EINVAL);
1159 static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1162 struct sk_buff *skb;
1165 if (size <= NLMSG_GOODSIZE || broadcast)
1166 return alloc_skb(size, GFP_KERNEL);
1168 size = SKB_DATA_ALIGN(size) +
1169 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1171 data = vmalloc(size);
1175 skb = __build_skb(data, size);
1179 skb->destructor = netlink_skb_destructor;
1185 * Attach a skb to a netlink socket.
1186 * The caller must hold a reference to the destination socket. On error, the
1187 * reference is dropped. The skb is not send to the destination, just all
1188 * all error checks are performed and memory in the queue is reserved.
1190 * < 0: error. skb freed, reference to sock dropped.
1192 * 1: repeat lookup - reference dropped while waiting for socket memory.
1194 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
1195 long *timeo, struct sock *ssk)
1197 struct netlink_sock *nlk;
1201 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1202 test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
1203 DECLARE_WAITQUEUE(wait, current);
1205 if (!ssk || netlink_is_kernel(ssk))
1206 netlink_overrun(sk);
1212 __set_current_state(TASK_INTERRUPTIBLE);
1213 add_wait_queue(&nlk->wait, &wait);
1215 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1216 test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
1217 !sock_flag(sk, SOCK_DEAD))
1218 *timeo = schedule_timeout(*timeo);
1220 __set_current_state(TASK_RUNNING);
1221 remove_wait_queue(&nlk->wait, &wait);
1224 if (signal_pending(current)) {
1226 return sock_intr_errno(*timeo);
1230 netlink_skb_set_owner_r(skb, sk);
1234 static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1238 netlink_deliver_tap(skb);
1240 skb_queue_tail(&sk->sk_receive_queue, skb);
1241 sk->sk_data_ready(sk);
1245 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1247 int len = __netlink_sendskb(sk, skb);
1253 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1259 static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
1263 WARN_ON(skb->sk != NULL);
1264 delta = skb->end - skb->tail;
1265 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
1268 if (skb_shared(skb)) {
1269 struct sk_buff *nskb = skb_clone(skb, allocation);
1276 pskb_expand_head(skb, 0, -delta,
1277 (allocation & ~__GFP_DIRECT_RECLAIM) |
1278 __GFP_NOWARN | __GFP_NORETRY);
1282 static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1286 struct netlink_sock *nlk = nlk_sk(sk);
1288 ret = -ECONNREFUSED;
1289 if (nlk->netlink_rcv != NULL) {
1291 netlink_skb_set_owner_r(skb, sk);
1292 NETLINK_CB(skb).sk = ssk;
1293 netlink_deliver_tap_kernel(sk, ssk, skb);
1294 nlk->netlink_rcv(skb);
1303 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
1304 u32 portid, int nonblock)
1310 skb = netlink_trim(skb, gfp_any());
1312 timeo = sock_sndtimeo(ssk, nonblock);
1314 sk = netlink_getsockbyportid(ssk, portid);
1319 if (netlink_is_kernel(sk))
1320 return netlink_unicast_kernel(sk, skb, ssk);
1322 if (sk_filter(sk, skb)) {
1329 err = netlink_attachskb(sk, skb, &timeo, ssk);
1335 return netlink_sendskb(sk, skb);
1337 EXPORT_SYMBOL(netlink_unicast);
1339 int netlink_has_listeners(struct sock *sk, unsigned int group)
1342 struct listeners *listeners;
1344 BUG_ON(!netlink_is_kernel(sk));
1347 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1349 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
1350 res = test_bit(group - 1, listeners->masks);
1356 EXPORT_SYMBOL_GPL(netlink_has_listeners);
1358 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
1360 struct netlink_sock *nlk = nlk_sk(sk);
1362 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
1363 !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
1364 netlink_skb_set_owner_r(skb, sk);
1365 __netlink_sendskb(sk, skb);
1366 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1371 struct netlink_broadcast_data {
1372 struct sock *exclude_sk;
1377 int delivery_failure;
1381 struct sk_buff *skb, *skb2;
1382 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1386 static void do_one_broadcast(struct sock *sk,
1387 struct netlink_broadcast_data *p)
1389 struct netlink_sock *nlk = nlk_sk(sk);
1392 if (p->exclude_sk == sk)
1395 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1396 !test_bit(p->group - 1, nlk->groups))
1399 if (!net_eq(sock_net(sk), p->net)) {
1400 if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
1403 if (!peernet_has_id(sock_net(sk), p->net))
1406 if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
1412 netlink_overrun(sk);
1417 if (p->skb2 == NULL) {
1418 if (skb_shared(p->skb)) {
1419 p->skb2 = skb_clone(p->skb, p->allocation);
1421 p->skb2 = skb_get(p->skb);
1423 * skb ownership may have been set when
1424 * delivered to a previous socket.
1426 skb_orphan(p->skb2);
1429 if (p->skb2 == NULL) {
1430 netlink_overrun(sk);
1431 /* Clone failed. Notify ALL listeners. */
1433 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
1434 p->delivery_failure = 1;
1437 if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1442 if (sk_filter(sk, p->skb2)) {
1447 NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
1448 if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED)
1449 NETLINK_CB(p->skb2).nsid_is_set = true;
1450 val = netlink_broadcast_deliver(sk, p->skb2);
1452 netlink_overrun(sk);
1453 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
1454 p->delivery_failure = 1;
1456 p->congested |= val;
1464 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
1465 u32 group, gfp_t allocation,
1466 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1469 struct net *net = sock_net(ssk);
1470 struct netlink_broadcast_data info;
1473 skb = netlink_trim(skb, allocation);
1475 info.exclude_sk = ssk;
1477 info.portid = portid;
1480 info.delivery_failure = 0;
1483 info.allocation = allocation;
1486 info.tx_filter = filter;
1487 info.tx_data = filter_data;
1489 /* While we sleep in clone, do not allow to change socket list */
1491 netlink_lock_table();
1493 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1494 do_one_broadcast(sk, &info);
1498 netlink_unlock_table();
1500 if (info.delivery_failure) {
1501 kfree_skb(info.skb2);
1504 consume_skb(info.skb2);
1506 if (info.delivered) {
1507 if (info.congested && gfpflags_allow_blocking(allocation))
1513 EXPORT_SYMBOL(netlink_broadcast_filtered);
1515 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
1516 u32 group, gfp_t allocation)
1518 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
1521 EXPORT_SYMBOL(netlink_broadcast);
1523 struct netlink_set_err_data {
1524 struct sock *exclude_sk;
1530 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1532 struct netlink_sock *nlk = nlk_sk(sk);
1535 if (sk == p->exclude_sk)
1538 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
1541 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1542 !test_bit(p->group - 1, nlk->groups))
1545 if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
1550 sk->sk_err = p->code;
1551 sk->sk_error_report(sk);
1557 * netlink_set_err - report error to broadcast listeners
1558 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1559 * @portid: the PORTID of a process that we want to skip (if any)
1560 * @group: the broadcast group that will notice the error
1561 * @code: error code, must be negative (as usual in kernelspace)
1563 * This function returns the number of broadcast listeners that have set the
1564 * NETLINK_NO_ENOBUFS socket option.
1566 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1568 struct netlink_set_err_data info;
1572 info.exclude_sk = ssk;
1573 info.portid = portid;
1575 /* sk->sk_err wants a positive error value */
1578 read_lock(&nl_table_lock);
1580 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1581 ret += do_one_set_err(sk, &info);
1583 read_unlock(&nl_table_lock);
1586 EXPORT_SYMBOL(netlink_set_err);
1588 /* must be called with netlink table grabbed */
1589 static void netlink_update_socket_mc(struct netlink_sock *nlk,
1593 int old, new = !!is_new, subscriptions;
1595 old = test_bit(group - 1, nlk->groups);
1596 subscriptions = nlk->subscriptions - old + new;
1598 __set_bit(group - 1, nlk->groups);
1600 __clear_bit(group - 1, nlk->groups);
1601 netlink_update_subscriptions(&nlk->sk, subscriptions);
1602 netlink_update_listeners(&nlk->sk);
1605 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1606 char __user *optval, unsigned int optlen)
1608 struct sock *sk = sock->sk;
1609 struct netlink_sock *nlk = nlk_sk(sk);
1610 unsigned int val = 0;
1613 if (level != SOL_NETLINK)
1614 return -ENOPROTOOPT;
1616 if (optlen >= sizeof(int) &&
1617 get_user(val, (unsigned int __user *)optval))
1621 case NETLINK_PKTINFO:
1623 nlk->flags |= NETLINK_F_RECV_PKTINFO;
1625 nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
1628 case NETLINK_ADD_MEMBERSHIP:
1629 case NETLINK_DROP_MEMBERSHIP: {
1630 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
1632 err = netlink_realloc_groups(sk);
1635 if (!val || val - 1 >= nlk->ngroups)
1637 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
1638 err = nlk->netlink_bind(sock_net(sk), val);
1642 netlink_table_grab();
1643 netlink_update_socket_mc(nlk, val,
1644 optname == NETLINK_ADD_MEMBERSHIP);
1645 netlink_table_ungrab();
1646 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
1647 nlk->netlink_unbind(sock_net(sk), val);
1652 case NETLINK_BROADCAST_ERROR:
1654 nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
1656 nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
1659 case NETLINK_NO_ENOBUFS:
1661 nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
1662 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
1663 wake_up_interruptible(&nlk->wait);
1665 nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
1669 case NETLINK_LISTEN_ALL_NSID:
1670 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
1674 nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
1676 nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
1679 case NETLINK_CAP_ACK:
1681 nlk->flags |= NETLINK_F_CAP_ACK;
1683 nlk->flags &= ~NETLINK_F_CAP_ACK;
1686 case NETLINK_EXT_ACK:
1688 nlk->flags |= NETLINK_F_EXT_ACK;
1690 nlk->flags &= ~NETLINK_F_EXT_ACK;
1699 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1700 char __user *optval, int __user *optlen)
1702 struct sock *sk = sock->sk;
1703 struct netlink_sock *nlk = nlk_sk(sk);
1706 if (level != SOL_NETLINK)
1707 return -ENOPROTOOPT;
1709 if (get_user(len, optlen))
1715 case NETLINK_PKTINFO:
1716 if (len < sizeof(int))
1719 val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
1720 if (put_user(len, optlen) ||
1721 put_user(val, optval))
1725 case NETLINK_BROADCAST_ERROR:
1726 if (len < sizeof(int))
1729 val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
1730 if (put_user(len, optlen) ||
1731 put_user(val, optval))
1735 case NETLINK_NO_ENOBUFS:
1736 if (len < sizeof(int))
1739 val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
1740 if (put_user(len, optlen) ||
1741 put_user(val, optval))
1745 case NETLINK_LIST_MEMBERSHIPS: {
1746 int pos, idx, shift;
1749 netlink_lock_table();
1750 for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
1751 if (len - pos < sizeof(u32))
1754 idx = pos / sizeof(unsigned long);
1755 shift = (pos % sizeof(unsigned long)) * 8;
1756 if (put_user((u32)(nlk->groups[idx] >> shift),
1757 (u32 __user *)(optval + pos))) {
1762 if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
1764 netlink_unlock_table();
1767 case NETLINK_CAP_ACK:
1768 if (len < sizeof(int))
1771 val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
1772 if (put_user(len, optlen) ||
1773 put_user(val, optval))
1777 case NETLINK_EXT_ACK:
1778 if (len < sizeof(int))
1781 val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
1782 if (put_user(len, optlen) || put_user(val, optval))
1792 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1794 struct nl_pktinfo info;
1796 info.group = NETLINK_CB(skb).dst_group;
1797 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1800 static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
1801 struct sk_buff *skb)
1803 if (!NETLINK_CB(skb).nsid_is_set)
1806 put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
1807 &NETLINK_CB(skb).nsid);
1810 static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1812 struct sock *sk = sock->sk;
1813 struct netlink_sock *nlk = nlk_sk(sk);
1814 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
1817 struct sk_buff *skb;
1819 struct scm_cookie scm;
1820 u32 netlink_skb_flags = 0;
1822 if (msg->msg_flags&MSG_OOB)
1826 pr_warn_once("Zero length message leads to an empty skb\n");
1830 err = scm_send(sock, msg, &scm, true);
1834 if (msg->msg_namelen) {
1836 if (msg->msg_namelen < sizeof(struct sockaddr_nl))
1838 if (addr->nl_family != AF_NETLINK)
1840 dst_portid = addr->nl_pid;
1841 dst_group = ffs(addr->nl_groups);
1843 if ((dst_group || dst_portid) &&
1844 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
1846 netlink_skb_flags |= NETLINK_SKB_DST;
1848 dst_portid = nlk->dst_portid;
1849 dst_group = nlk->dst_group;
1852 /* Paired with WRITE_ONCE() in netlink_insert() */
1853 if (!READ_ONCE(nlk->bound)) {
1854 err = netlink_autobind(sock);
1858 /* Ensure nlk is hashed and visible. */
1863 if (len > sk->sk_sndbuf - 32)
1866 skb = netlink_alloc_large_skb(len, dst_group);
1870 NETLINK_CB(skb).portid = nlk->portid;
1871 NETLINK_CB(skb).dst_group = dst_group;
1872 NETLINK_CB(skb).creds = scm.creds;
1873 NETLINK_CB(skb).flags = netlink_skb_flags;
1876 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
1881 err = security_netlink_send(sk, skb);
1888 refcount_inc(&skb->users);
1889 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1891 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1898 static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1901 struct scm_cookie scm;
1902 struct sock *sk = sock->sk;
1903 struct netlink_sock *nlk = nlk_sk(sk);
1904 int noblock = flags&MSG_DONTWAIT;
1906 struct sk_buff *skb, *data_skb;
1914 skb = skb_recv_datagram(sk, flags, noblock, &err);
1920 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1921 if (unlikely(skb_shinfo(skb)->frag_list)) {
1923 * If this skb has a frag_list, then here that means that we
1924 * will have to use the frag_list skb's data for compat tasks
1925 * and the regular skb's data for normal (non-compat) tasks.
1927 * If we need to send the compat skb, assign it to the
1928 * 'data_skb' variable so that it will be used below for data
1929 * copying. We keep 'skb' for everything else, including
1930 * freeing both later.
1932 if (flags & MSG_CMSG_COMPAT)
1933 data_skb = skb_shinfo(skb)->frag_list;
1937 /* Record the max length of recvmsg() calls for future allocations */
1938 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
1939 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
1940 SKB_WITH_OVERHEAD(32768));
1942 copied = data_skb->len;
1944 msg->msg_flags |= MSG_TRUNC;
1948 skb_reset_transport_header(data_skb);
1949 err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
1951 if (msg->msg_name) {
1952 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
1953 addr->nl_family = AF_NETLINK;
1955 addr->nl_pid = NETLINK_CB(skb).portid;
1956 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1957 msg->msg_namelen = sizeof(*addr);
1960 if (nlk->flags & NETLINK_F_RECV_PKTINFO)
1961 netlink_cmsg_recv_pktinfo(msg, skb);
1962 if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
1963 netlink_cmsg_listen_all_nsid(sk, msg, skb);
1965 memset(&scm, 0, sizeof(scm));
1966 scm.creds = *NETLINK_CREDS(skb);
1967 if (flags & MSG_TRUNC)
1968 copied = data_skb->len;
1970 skb_free_datagram(sk, skb);
1972 if (nlk->cb_running &&
1973 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1974 ret = netlink_dump(sk);
1977 sk->sk_error_report(sk);
1981 scm_recv(sock, msg, &scm, flags);
1983 netlink_rcv_wake(sk);
1984 return err ? : copied;
1987 static void netlink_data_ready(struct sock *sk)
1993 * We export these functions to other modules. They provide a
1994 * complete set of kernel non-blocking support for message
1999 __netlink_kernel_create(struct net *net, int unit, struct module *module,
2000 struct netlink_kernel_cfg *cfg)
2002 struct socket *sock;
2004 struct netlink_sock *nlk;
2005 struct listeners *listeners = NULL;
2006 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2007 unsigned int groups;
2011 if (unit < 0 || unit >= MAX_LINKS)
2014 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2017 if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
2018 goto out_sock_release_nosk;
2022 if (!cfg || cfg->groups < 32)
2025 groups = cfg->groups;
2027 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2029 goto out_sock_release;
2031 sk->sk_data_ready = netlink_data_ready;
2032 if (cfg && cfg->input)
2033 nlk_sk(sk)->netlink_rcv = cfg->input;
2035 if (netlink_insert(sk, 0))
2036 goto out_sock_release;
2039 nlk->flags |= NETLINK_F_KERNEL_SOCKET;
2041 netlink_table_grab();
2042 if (!nl_table[unit].registered) {
2043 nl_table[unit].groups = groups;
2044 rcu_assign_pointer(nl_table[unit].listeners, listeners);
2045 nl_table[unit].cb_mutex = cb_mutex;
2046 nl_table[unit].module = module;
2048 nl_table[unit].bind = cfg->bind;
2049 nl_table[unit].unbind = cfg->unbind;
2050 nl_table[unit].flags = cfg->flags;
2052 nl_table[unit].compare = cfg->compare;
2054 nl_table[unit].registered = 1;
2057 nl_table[unit].registered++;
2059 netlink_table_ungrab();
2064 netlink_kernel_release(sk);
2067 out_sock_release_nosk:
2071 EXPORT_SYMBOL(__netlink_kernel_create);
2074 netlink_kernel_release(struct sock *sk)
2076 if (sk == NULL || sk->sk_socket == NULL)
2079 sock_release(sk->sk_socket);
2081 EXPORT_SYMBOL(netlink_kernel_release);
2083 int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
2085 struct listeners *new, *old;
2086 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
2091 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
2092 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2095 old = nl_deref_protected(tbl->listeners);
2096 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2097 rcu_assign_pointer(tbl->listeners, new);
2099 kfree_rcu(old, rcu);
2101 tbl->groups = groups;
2107 * netlink_change_ngroups - change number of multicast groups
2109 * This changes the number of multicast groups that are available
2110 * on a certain netlink family. Note that it is not possible to
2111 * change the number of groups to below 32. Also note that it does
2112 * not implicitly call netlink_clear_multicast_users() when the
2113 * number of groups is reduced.
2115 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2116 * @groups: The new number of groups.
2118 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2122 netlink_table_grab();
2123 err = __netlink_change_ngroups(sk, groups);
2124 netlink_table_ungrab();
2129 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2132 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2134 sk_for_each_bound(sk, &tbl->mc_list)
2135 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2139 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
2141 struct nlmsghdr *nlh;
2142 int size = nlmsg_msg_size(len);
2144 nlh = skb_put(skb, NLMSG_ALIGN(size));
2145 nlh->nlmsg_type = type;
2146 nlh->nlmsg_len = size;
2147 nlh->nlmsg_flags = flags;
2148 nlh->nlmsg_pid = portid;
2149 nlh->nlmsg_seq = seq;
2150 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
2151 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
2154 EXPORT_SYMBOL(__nlmsg_put);
2157 * It looks a bit ugly.
2158 * It would be better to create kernel thread.
2161 static int netlink_dump(struct sock *sk)
2163 struct netlink_sock *nlk = nlk_sk(sk);
2164 struct netlink_callback *cb;
2165 struct sk_buff *skb = NULL;
2166 struct nlmsghdr *nlh;
2167 struct module *module;
2172 mutex_lock(nlk->cb_mutex);
2173 if (!nlk->cb_running) {
2178 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2181 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2182 * required, but it makes sense to _attempt_ a 16K bytes allocation
2183 * to reduce number of system calls on dump operations, if user
2184 * ever provided a big enough buffer.
2187 alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2189 if (alloc_min_size < nlk->max_recvmsg_len) {
2190 alloc_size = nlk->max_recvmsg_len;
2191 skb = alloc_skb(alloc_size,
2192 (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
2193 __GFP_NOWARN | __GFP_NORETRY);
2196 alloc_size = alloc_min_size;
2197 skb = alloc_skb(alloc_size, GFP_KERNEL);
2202 /* Trim skb to allocated size. User is expected to provide buffer as
2203 * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
2204 * netlink_recvmsg())). dump will pack as many smaller messages as
2205 * could fit within the allocated skb. skb is typically allocated
2206 * with larger space than required (could be as much as near 2x the
2207 * requested size with align to next power of 2 approach). Allowing
2208 * dump to use the excess space makes it difficult for a user to have a
2209 * reasonable static buffer based on the expected largest dump of a
2210 * single netdev. The outcome is MSG_TRUNC error.
2212 skb_reserve(skb, skb_tailroom(skb) - alloc_size);
2213 netlink_skb_set_owner_r(skb, sk);
2215 if (nlk->dump_done_errno > 0)
2216 nlk->dump_done_errno = cb->dump(skb, cb);
2218 if (nlk->dump_done_errno > 0 ||
2219 skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) {
2220 mutex_unlock(nlk->cb_mutex);
2222 if (sk_filter(sk, skb))
2225 __netlink_sendskb(sk, skb);
2229 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE,
2230 sizeof(nlk->dump_done_errno), NLM_F_MULTI);
2234 nl_dump_check_consistent(cb, nlh);
2236 memcpy(nlmsg_data(nlh), &nlk->dump_done_errno,
2237 sizeof(nlk->dump_done_errno));
2239 if (sk_filter(sk, skb))
2242 __netlink_sendskb(sk, skb);
2247 nlk->cb_running = false;
2248 module = cb->module;
2250 mutex_unlock(nlk->cb_mutex);
2256 mutex_unlock(nlk->cb_mutex);
2261 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2262 const struct nlmsghdr *nlh,
2263 struct netlink_dump_control *control)
2265 struct netlink_callback *cb;
2267 struct netlink_sock *nlk;
2270 refcount_inc(&skb->users);
2272 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
2274 ret = -ECONNREFUSED;
2279 mutex_lock(nlk->cb_mutex);
2280 /* A dump is in progress... */
2281 if (nlk->cb_running) {
2285 /* add reference of module which cb->dump belongs to */
2286 if (!try_module_get(control->module)) {
2287 ret = -EPROTONOSUPPORT;
2292 memset(cb, 0, sizeof(*cb));
2293 cb->start = control->start;
2294 cb->dump = control->dump;
2295 cb->done = control->done;
2297 cb->data = control->data;
2298 cb->module = control->module;
2299 cb->min_dump_alloc = control->min_dump_alloc;
2303 ret = cb->start(cb);
2308 nlk->cb_running = true;
2309 nlk->dump_done_errno = INT_MAX;
2311 mutex_unlock(nlk->cb_mutex);
2313 ret = netlink_dump(sk);
2320 /* We successfully started a dump, by returning -EINTR we
2321 * signal not to send ACK even if it was requested.
2326 module_put(control->module);
2329 mutex_unlock(nlk->cb_mutex);
2334 EXPORT_SYMBOL(__netlink_dump_start);
2336 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
2337 const struct netlink_ext_ack *extack)
2339 struct sk_buff *skb;
2340 struct nlmsghdr *rep;
2341 struct nlmsgerr *errmsg;
2342 size_t payload = sizeof(*errmsg);
2344 struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
2345 unsigned int flags = 0;
2346 bool nlk_has_extack = nlk->flags & NETLINK_F_EXT_ACK;
2348 /* Error messages get the original request appened, unless the user
2349 * requests to cap the error message, and get extra error data if
2353 if (!(nlk->flags & NETLINK_F_CAP_ACK))
2354 payload += nlmsg_len(nlh);
2356 flags |= NLM_F_CAPPED;
2357 if (nlk_has_extack && extack) {
2359 tlvlen += nla_total_size(strlen(extack->_msg) + 1);
2360 if (extack->bad_attr)
2361 tlvlen += nla_total_size(sizeof(u32));
2364 flags |= NLM_F_CAPPED;
2366 if (nlk_has_extack && extack && extack->cookie_len)
2367 tlvlen += nla_total_size(extack->cookie_len);
2371 flags |= NLM_F_ACK_TLVS;
2373 skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
2377 sk = netlink_lookup(sock_net(in_skb->sk),
2378 in_skb->sk->sk_protocol,
2379 NETLINK_CB(in_skb).portid);
2381 sk->sk_err = ENOBUFS;
2382 sk->sk_error_report(sk);
2388 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
2389 NLMSG_ERROR, payload, flags);
2390 errmsg = nlmsg_data(rep);
2391 errmsg->error = err;
2392 memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
2394 if (nlk_has_extack && extack) {
2397 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
2399 if (extack->bad_attr &&
2400 !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
2401 (u8 *)extack->bad_attr >= in_skb->data +
2403 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
2404 (u8 *)extack->bad_attr -
2407 if (extack->cookie_len)
2408 WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
2414 nlmsg_end(skb, rep);
2416 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
2418 EXPORT_SYMBOL(netlink_ack);
2420 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
2422 struct netlink_ext_ack *))
2424 struct netlink_ext_ack extack;
2425 struct nlmsghdr *nlh;
2428 while (skb->len >= nlmsg_total_size(0)) {
2431 memset(&extack, 0, sizeof(extack));
2432 nlh = nlmsg_hdr(skb);
2435 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
2438 /* Only requests are handled by the kernel */
2439 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
2442 /* Skip control messages */
2443 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
2446 err = cb(skb, nlh, &extack);
2451 if (nlh->nlmsg_flags & NLM_F_ACK || err)
2452 netlink_ack(skb, nlh, err, &extack);
2455 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
2456 if (msglen > skb->len)
2458 skb_pull(skb, msglen);
2463 EXPORT_SYMBOL(netlink_rcv_skb);
2466 * nlmsg_notify - send a notification netlink message
2467 * @sk: netlink socket to use
2468 * @skb: notification message
2469 * @portid: destination netlink portid for reports or 0
2470 * @group: destination multicast group or 0
2471 * @report: 1 to report back, 0 to disable
2472 * @flags: allocation flags
2474 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
2475 unsigned int group, int report, gfp_t flags)
2480 int exclude_portid = 0;
2483 refcount_inc(&skb->users);
2484 exclude_portid = portid;
2487 /* errors reported via destination sk->sk_err, but propagate
2488 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
2489 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
2497 err2 = nlmsg_unicast(sk, skb, portid);
2504 EXPORT_SYMBOL(nlmsg_notify);
2506 #ifdef CONFIG_PROC_FS
2507 struct nl_seq_iter {
2508 struct seq_net_private p;
2509 struct rhashtable_iter hti;
2513 static int netlink_walk_start(struct nl_seq_iter *iter)
2517 err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
2520 iter->link = MAX_LINKS;
2524 err = rhashtable_walk_start(&iter->hti);
2525 return err == -EAGAIN ? 0 : err;
2528 static void netlink_walk_stop(struct nl_seq_iter *iter)
2530 rhashtable_walk_stop(&iter->hti);
2531 rhashtable_walk_exit(&iter->hti);
2534 static void *__netlink_seq_next(struct seq_file *seq)
2536 struct nl_seq_iter *iter = seq->private;
2537 struct netlink_sock *nlk;
2543 nlk = rhashtable_walk_next(&iter->hti);
2546 if (PTR_ERR(nlk) == -EAGAIN)
2555 netlink_walk_stop(iter);
2556 if (++iter->link >= MAX_LINKS)
2559 err = netlink_walk_start(iter);
2561 return ERR_PTR(err);
2563 } while (sock_net(&nlk->sk) != seq_file_net(seq));
2568 static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
2570 struct nl_seq_iter *iter = seq->private;
2571 void *obj = SEQ_START_TOKEN;
2577 err = netlink_walk_start(iter);
2579 return ERR_PTR(err);
2581 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
2582 obj = __netlink_seq_next(seq);
2587 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2590 return __netlink_seq_next(seq);
2593 static void netlink_seq_stop(struct seq_file *seq, void *v)
2595 struct nl_seq_iter *iter = seq->private;
2597 if (iter->link >= MAX_LINKS)
2600 netlink_walk_stop(iter);
2604 static int netlink_seq_show(struct seq_file *seq, void *v)
2606 if (v == SEQ_START_TOKEN) {
2608 "sk Eth Pid Groups "
2609 "Rmem Wmem Dump Locks Drops Inode\n");
2612 struct netlink_sock *nlk = nlk_sk(s);
2614 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
2618 nlk->groups ? (u32)nlk->groups[0] : 0,
2619 sk_rmem_alloc_get(s),
2620 sk_wmem_alloc_get(s),
2622 refcount_read(&s->sk_refcnt),
2623 atomic_read(&s->sk_drops),
2631 static const struct seq_operations netlink_seq_ops = {
2632 .start = netlink_seq_start,
2633 .next = netlink_seq_next,
2634 .stop = netlink_seq_stop,
2635 .show = netlink_seq_show,
2639 static int netlink_seq_open(struct inode *inode, struct file *file)
2641 return seq_open_net(inode, file, &netlink_seq_ops,
2642 sizeof(struct nl_seq_iter));
2645 static const struct file_operations netlink_seq_fops = {
2646 .owner = THIS_MODULE,
2647 .open = netlink_seq_open,
2649 .llseek = seq_lseek,
2650 .release = seq_release_net,
2655 int netlink_register_notifier(struct notifier_block *nb)
2657 return blocking_notifier_chain_register(&netlink_chain, nb);
2659 EXPORT_SYMBOL(netlink_register_notifier);
2661 int netlink_unregister_notifier(struct notifier_block *nb)
2663 return blocking_notifier_chain_unregister(&netlink_chain, nb);
2665 EXPORT_SYMBOL(netlink_unregister_notifier);
2667 static const struct proto_ops netlink_ops = {
2668 .family = PF_NETLINK,
2669 .owner = THIS_MODULE,
2670 .release = netlink_release,
2671 .bind = netlink_bind,
2672 .connect = netlink_connect,
2673 .socketpair = sock_no_socketpair,
2674 .accept = sock_no_accept,
2675 .getname = netlink_getname,
2676 .poll = datagram_poll,
2677 .ioctl = netlink_ioctl,
2678 .listen = sock_no_listen,
2679 .shutdown = sock_no_shutdown,
2680 .setsockopt = netlink_setsockopt,
2681 .getsockopt = netlink_getsockopt,
2682 .sendmsg = netlink_sendmsg,
2683 .recvmsg = netlink_recvmsg,
2684 .mmap = sock_no_mmap,
2685 .sendpage = sock_no_sendpage,
2688 static const struct net_proto_family netlink_family_ops = {
2689 .family = PF_NETLINK,
2690 .create = netlink_create,
2691 .owner = THIS_MODULE, /* for consistency 8) */
2694 static int __net_init netlink_net_init(struct net *net)
2696 #ifdef CONFIG_PROC_FS
2697 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
2703 static void __net_exit netlink_net_exit(struct net *net)
2705 #ifdef CONFIG_PROC_FS
2706 remove_proc_entry("netlink", net->proc_net);
2710 static void __init netlink_add_usersock_entry(void)
2712 struct listeners *listeners;
2715 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2717 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2719 netlink_table_grab();
2721 nl_table[NETLINK_USERSOCK].groups = groups;
2722 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
2723 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2724 nl_table[NETLINK_USERSOCK].registered = 1;
2725 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
2727 netlink_table_ungrab();
2730 static struct pernet_operations __net_initdata netlink_net_ops = {
2731 .init = netlink_net_init,
2732 .exit = netlink_net_exit,
2735 static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
2737 const struct netlink_sock *nlk = data;
2738 struct netlink_compare_arg arg;
2740 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
2741 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
2744 static const struct rhashtable_params netlink_rhashtable_params = {
2745 .head_offset = offsetof(struct netlink_sock, node),
2746 .key_len = netlink_compare_arg_len,
2747 .obj_hashfn = netlink_hash,
2748 .obj_cmpfn = netlink_compare,
2749 .automatic_shrinking = true,
2752 static int __init netlink_proto_init(void)
2755 int err = proto_register(&netlink_proto, 0);
2760 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
2762 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
2766 for (i = 0; i < MAX_LINKS; i++) {
2767 if (rhashtable_init(&nl_table[i].hash,
2768 &netlink_rhashtable_params) < 0) {
2770 rhashtable_destroy(&nl_table[i].hash);
2776 INIT_LIST_HEAD(&netlink_tap_all);
2778 netlink_add_usersock_entry();
2780 sock_register(&netlink_family_ops);
2781 register_pernet_subsys(&netlink_net_ops);
2782 /* The netlink device handler may be needed early. */
2787 panic("netlink_init: Cannot allocate nl_table\n");
2790 core_initcall(netlink_proto_init);