2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
15 * use nlk_sk, as sk->protinfo is on a diet 8)
16 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17 * - inc module use count of module that owns
18 * the kernel socket in case userspace opens
19 * socket of same protocol
20 * - remove all module support, since netlink is
21 * mandatory if CONFIG_NET=y these days
24 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/socket.h>
36 #include <linux/fcntl.h>
37 #include <linux/termios.h>
38 #include <linux/sockios.h>
39 #include <linux/net.h>
41 #include <linux/slab.h>
42 #include <asm/uaccess.h>
43 #include <linux/skbuff.h>
44 #include <linux/netdevice.h>
45 #include <linux/rtnetlink.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
48 #include <linux/notifier.h>
49 #include <linux/security.h>
50 #include <linux/jhash.h>
51 #include <linux/jiffies.h>
52 #include <linux/random.h>
53 #include <linux/bitops.h>
55 #include <linux/types.h>
56 #include <linux/audit.h>
57 #include <linux/mutex.h>
59 #include <net/net_namespace.h>
62 #include <net/netlink.h>
64 #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
65 #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
68 /* struct sock has to be the first member of netlink_sock */
76 unsigned long *groups;
78 wait_queue_head_t wait;
79 struct netlink_callback *cb;
80 struct mutex *cb_mutex;
81 struct mutex cb_def_mutex;
82 void (*netlink_rcv)(struct sk_buff *skb);
83 struct module *module;
86 #define NETLINK_KERNEL_SOCKET 0x1
87 #define NETLINK_RECV_PKTINFO 0x2
88 #define NETLINK_BROADCAST_SEND_ERROR 0x4
90 static inline struct netlink_sock *nlk_sk(struct sock *sk)
92 return container_of(sk, struct netlink_sock, sk);
95 static inline int netlink_is_kernel(struct sock *sk)
97 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
101 struct hlist_head *table;
102 unsigned long rehash_time;
107 unsigned int entries;
108 unsigned int max_shift;
113 struct netlink_table {
114 struct nl_pid_hash hash;
115 struct hlist_head mc_list;
116 unsigned long *listeners;
117 unsigned int nl_nonroot;
119 struct mutex *cb_mutex;
120 struct module *module;
124 static struct netlink_table *nl_table;
126 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
128 static int netlink_dump(struct sock *sk);
129 static void netlink_destroy_callback(struct netlink_callback *cb);
131 static DEFINE_RWLOCK(nl_table_lock);
132 static atomic_t nl_table_users = ATOMIC_INIT(0);
134 static ATOMIC_NOTIFIER_HEAD(netlink_chain);
136 static u32 netlink_group_mask(u32 group)
138 return group ? 1 << (group - 1) : 0;
141 static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
143 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
146 static void netlink_sock_destruct(struct sock *sk)
148 struct netlink_sock *nlk = nlk_sk(sk);
152 nlk->cb->done(nlk->cb);
153 netlink_destroy_callback(nlk->cb);
156 skb_queue_purge(&sk->sk_receive_queue);
158 if (!sock_flag(sk, SOCK_DEAD)) {
159 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
163 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
164 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
165 WARN_ON(nlk_sk(sk)->groups);
168 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
169 * SMP. Look, when several writers sleep and reader wakes them up, all but one
170 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
171 * this, _but_ remember, it adds useless work on UP machines.
174 static void netlink_table_grab(void)
175 __acquires(nl_table_lock)
177 write_lock_irq(&nl_table_lock);
179 if (atomic_read(&nl_table_users)) {
180 DECLARE_WAITQUEUE(wait, current);
182 add_wait_queue_exclusive(&nl_table_wait, &wait);
184 set_current_state(TASK_UNINTERRUPTIBLE);
185 if (atomic_read(&nl_table_users) == 0)
187 write_unlock_irq(&nl_table_lock);
189 write_lock_irq(&nl_table_lock);
192 __set_current_state(TASK_RUNNING);
193 remove_wait_queue(&nl_table_wait, &wait);
197 static void netlink_table_ungrab(void)
198 __releases(nl_table_lock)
200 write_unlock_irq(&nl_table_lock);
201 wake_up(&nl_table_wait);
205 netlink_lock_table(void)
207 /* read_lock() synchronizes us to netlink_table_grab */
209 read_lock(&nl_table_lock);
210 atomic_inc(&nl_table_users);
211 read_unlock(&nl_table_lock);
215 netlink_unlock_table(void)
217 if (atomic_dec_and_test(&nl_table_users))
218 wake_up(&nl_table_wait);
221 static inline struct sock *netlink_lookup(struct net *net, int protocol,
224 struct nl_pid_hash *hash = &nl_table[protocol].hash;
225 struct hlist_head *head;
227 struct hlist_node *node;
229 read_lock(&nl_table_lock);
230 head = nl_pid_hashfn(hash, pid);
231 sk_for_each(sk, node, head) {
232 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
239 read_unlock(&nl_table_lock);
243 static inline struct hlist_head *nl_pid_hash_zalloc(size_t size)
245 if (size <= PAGE_SIZE)
246 return kzalloc(size, GFP_ATOMIC);
248 return (struct hlist_head *)
249 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
253 static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
255 if (size <= PAGE_SIZE)
258 free_pages((unsigned long)table, get_order(size));
261 static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
263 unsigned int omask, mask, shift;
265 struct hlist_head *otable, *table;
268 omask = mask = hash->mask;
269 osize = size = (mask + 1) * sizeof(*table);
273 if (++shift > hash->max_shift)
279 table = nl_pid_hash_zalloc(size);
283 otable = hash->table;
287 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
289 for (i = 0; i <= omask; i++) {
291 struct hlist_node *node, *tmp;
293 sk_for_each_safe(sk, node, tmp, &otable[i])
294 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
297 nl_pid_hash_free(otable, osize);
298 hash->rehash_time = jiffies + 10 * 60 * HZ;
302 static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
304 int avg = hash->entries >> hash->shift;
306 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
309 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
310 nl_pid_hash_rehash(hash, 0);
317 static const struct proto_ops netlink_ops;
320 netlink_update_listeners(struct sock *sk)
322 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
323 struct hlist_node *node;
327 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
329 sk_for_each_bound(sk, node, &tbl->mc_list) {
330 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
331 mask |= nlk_sk(sk)->groups[i];
333 tbl->listeners[i] = mask;
335 /* this function is only called with the netlink table "grabbed", which
336 * makes sure updates are visible before bind or setsockopt return. */
339 static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
341 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
342 struct hlist_head *head;
343 int err = -EADDRINUSE;
345 struct hlist_node *node;
348 netlink_table_grab();
349 head = nl_pid_hashfn(hash, pid);
351 sk_for_each(osk, node, head) {
352 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
364 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
367 if (len && nl_pid_hash_dilute(hash, len))
368 head = nl_pid_hashfn(hash, pid);
370 nlk_sk(sk)->pid = pid;
371 sk_add_node(sk, head);
375 netlink_table_ungrab();
379 static void netlink_remove(struct sock *sk)
381 netlink_table_grab();
382 if (sk_del_node_init(sk))
383 nl_table[sk->sk_protocol].hash.entries--;
384 if (nlk_sk(sk)->subscriptions)
385 __sk_del_bind_node(sk);
386 netlink_table_ungrab();
389 static struct proto netlink_proto = {
391 .owner = THIS_MODULE,
392 .obj_size = sizeof(struct netlink_sock),
395 static int __netlink_create(struct net *net, struct socket *sock,
396 struct mutex *cb_mutex, int protocol)
399 struct netlink_sock *nlk;
401 sock->ops = &netlink_ops;
403 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
407 sock_init_data(sock, sk);
411 nlk->cb_mutex = cb_mutex;
413 nlk->cb_mutex = &nlk->cb_def_mutex;
414 mutex_init(nlk->cb_mutex);
416 init_waitqueue_head(&nlk->wait);
418 sk->sk_destruct = netlink_sock_destruct;
419 sk->sk_protocol = protocol;
423 static int netlink_create(struct net *net, struct socket *sock, int protocol)
425 struct module *module = NULL;
426 struct mutex *cb_mutex;
427 struct netlink_sock *nlk;
430 sock->state = SS_UNCONNECTED;
432 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
433 return -ESOCKTNOSUPPORT;
435 if (protocol < 0 || protocol >= MAX_LINKS)
436 return -EPROTONOSUPPORT;
438 netlink_lock_table();
439 #ifdef CONFIG_MODULES
440 if (!nl_table[protocol].registered) {
441 netlink_unlock_table();
442 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
443 netlink_lock_table();
446 if (nl_table[protocol].registered &&
447 try_module_get(nl_table[protocol].module))
448 module = nl_table[protocol].module;
449 cb_mutex = nl_table[protocol].cb_mutex;
450 netlink_unlock_table();
452 err = __netlink_create(net, sock, cb_mutex, protocol);
457 sock_prot_inuse_add(net, &netlink_proto, 1);
460 nlk = nlk_sk(sock->sk);
461 nlk->module = module;
470 static int netlink_release(struct socket *sock)
472 struct sock *sk = sock->sk;
473 struct netlink_sock *nlk;
483 * OK. Socket is unlinked, any packets that arrive now
488 wake_up_interruptible_all(&nlk->wait);
490 skb_queue_purge(&sk->sk_write_queue);
492 if (nlk->pid && !nlk->subscriptions) {
493 struct netlink_notify n = {
495 .protocol = sk->sk_protocol,
498 atomic_notifier_call_chain(&netlink_chain,
499 NETLINK_URELEASE, &n);
502 module_put(nlk->module);
504 netlink_table_grab();
505 if (netlink_is_kernel(sk)) {
506 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
507 if (--nl_table[sk->sk_protocol].registered == 0) {
508 kfree(nl_table[sk->sk_protocol].listeners);
509 nl_table[sk->sk_protocol].module = NULL;
510 nl_table[sk->sk_protocol].registered = 0;
512 } else if (nlk->subscriptions)
513 netlink_update_listeners(sk);
514 netlink_table_ungrab();
520 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
526 static int netlink_autobind(struct socket *sock)
528 struct sock *sk = sock->sk;
529 struct net *net = sock_net(sk);
530 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
531 struct hlist_head *head;
533 struct hlist_node *node;
534 s32 pid = current->tgid;
536 static s32 rover = -4097;
540 netlink_table_grab();
541 head = nl_pid_hashfn(hash, pid);
542 sk_for_each(osk, node, head) {
543 if (!net_eq(sock_net(osk), net))
545 if (nlk_sk(osk)->pid == pid) {
546 /* Bind collision, search negative pid values. */
550 netlink_table_ungrab();
554 netlink_table_ungrab();
556 err = netlink_insert(sk, net, pid);
557 if (err == -EADDRINUSE)
560 /* If 2 threads race to autobind, that is fine. */
567 static inline int netlink_capable(struct socket *sock, unsigned int flag)
569 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
570 capable(CAP_NET_ADMIN);
574 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
576 struct netlink_sock *nlk = nlk_sk(sk);
578 if (nlk->subscriptions && !subscriptions)
579 __sk_del_bind_node(sk);
580 else if (!nlk->subscriptions && subscriptions)
581 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
582 nlk->subscriptions = subscriptions;
585 static int netlink_realloc_groups(struct sock *sk)
587 struct netlink_sock *nlk = nlk_sk(sk);
589 unsigned long *new_groups;
592 netlink_table_grab();
594 groups = nl_table[sk->sk_protocol].groups;
595 if (!nl_table[sk->sk_protocol].registered) {
600 if (nlk->ngroups >= groups)
603 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
604 if (new_groups == NULL) {
608 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
609 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
611 nlk->groups = new_groups;
612 nlk->ngroups = groups;
614 netlink_table_ungrab();
618 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
621 struct sock *sk = sock->sk;
622 struct net *net = sock_net(sk);
623 struct netlink_sock *nlk = nlk_sk(sk);
624 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
627 if (nladdr->nl_family != AF_NETLINK)
630 /* Only superuser is allowed to listen multicasts */
631 if (nladdr->nl_groups) {
632 if (!netlink_capable(sock, NL_NONROOT_RECV))
634 err = netlink_realloc_groups(sk);
640 if (nladdr->nl_pid != nlk->pid)
643 err = nladdr->nl_pid ?
644 netlink_insert(sk, net, nladdr->nl_pid) :
645 netlink_autobind(sock);
650 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
653 netlink_table_grab();
654 netlink_update_subscriptions(sk, nlk->subscriptions +
655 hweight32(nladdr->nl_groups) -
656 hweight32(nlk->groups[0]));
657 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
658 netlink_update_listeners(sk);
659 netlink_table_ungrab();
664 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
668 struct sock *sk = sock->sk;
669 struct netlink_sock *nlk = nlk_sk(sk);
670 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
672 if (addr->sa_family == AF_UNSPEC) {
673 sk->sk_state = NETLINK_UNCONNECTED;
678 if (addr->sa_family != AF_NETLINK)
681 /* Only superuser is allowed to send multicasts */
682 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
686 err = netlink_autobind(sock);
689 sk->sk_state = NETLINK_CONNECTED;
690 nlk->dst_pid = nladdr->nl_pid;
691 nlk->dst_group = ffs(nladdr->nl_groups);
697 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
698 int *addr_len, int peer)
700 struct sock *sk = sock->sk;
701 struct netlink_sock *nlk = nlk_sk(sk);
702 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
704 nladdr->nl_family = AF_NETLINK;
706 *addr_len = sizeof(*nladdr);
709 nladdr->nl_pid = nlk->dst_pid;
710 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
712 nladdr->nl_pid = nlk->pid;
713 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
718 static void netlink_overrun(struct sock *sk)
720 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
721 sk->sk_err = ENOBUFS;
722 sk->sk_error_report(sk);
726 static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
729 struct netlink_sock *nlk;
731 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
733 return ERR_PTR(-ECONNREFUSED);
735 /* Don't bother queuing skb if kernel socket has no input function */
737 if (sock->sk_state == NETLINK_CONNECTED &&
738 nlk->dst_pid != nlk_sk(ssk)->pid) {
740 return ERR_PTR(-ECONNREFUSED);
745 struct sock *netlink_getsockbyfilp(struct file *filp)
747 struct inode *inode = filp->f_path.dentry->d_inode;
750 if (!S_ISSOCK(inode->i_mode))
751 return ERR_PTR(-ENOTSOCK);
753 sock = SOCKET_I(inode)->sk;
754 if (sock->sk_family != AF_NETLINK)
755 return ERR_PTR(-EINVAL);
762 * Attach a skb to a netlink socket.
763 * The caller must hold a reference to the destination socket. On error, the
764 * reference is dropped. The skb is not send to the destination, just all
765 * all error checks are performed and memory in the queue is reserved.
767 * < 0: error. skb freed, reference to sock dropped.
769 * 1: repeat lookup - reference dropped while waiting for socket memory.
771 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
772 long *timeo, struct sock *ssk)
774 struct netlink_sock *nlk;
778 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
779 test_bit(0, &nlk->state)) {
780 DECLARE_WAITQUEUE(wait, current);
782 if (!ssk || netlink_is_kernel(ssk))
789 __set_current_state(TASK_INTERRUPTIBLE);
790 add_wait_queue(&nlk->wait, &wait);
792 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
793 test_bit(0, &nlk->state)) &&
794 !sock_flag(sk, SOCK_DEAD))
795 *timeo = schedule_timeout(*timeo);
797 __set_current_state(TASK_RUNNING);
798 remove_wait_queue(&nlk->wait, &wait);
801 if (signal_pending(current)) {
803 return sock_intr_errno(*timeo);
807 skb_set_owner_r(skb, sk);
811 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
815 skb_queue_tail(&sk->sk_receive_queue, skb);
816 sk->sk_data_ready(sk, len);
821 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
827 static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
834 delta = skb->end - skb->tail;
835 if (delta * 2 < skb->truesize)
838 if (skb_shared(skb)) {
839 struct sk_buff *nskb = skb_clone(skb, allocation);
846 if (!pskb_expand_head(skb, 0, -delta, allocation))
847 skb->truesize -= delta;
852 static inline void netlink_rcv_wake(struct sock *sk)
854 struct netlink_sock *nlk = nlk_sk(sk);
856 if (skb_queue_empty(&sk->sk_receive_queue))
857 clear_bit(0, &nlk->state);
858 if (!test_bit(0, &nlk->state))
859 wake_up_interruptible(&nlk->wait);
862 static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
865 struct netlink_sock *nlk = nlk_sk(sk);
868 if (nlk->netlink_rcv != NULL) {
870 skb_set_owner_r(skb, sk);
871 nlk->netlink_rcv(skb);
878 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
879 u32 pid, int nonblock)
885 skb = netlink_trim(skb, gfp_any());
887 timeo = sock_sndtimeo(ssk, nonblock);
889 sk = netlink_getsockbypid(ssk, pid);
894 if (netlink_is_kernel(sk))
895 return netlink_unicast_kernel(sk, skb);
897 if (sk_filter(sk, skb)) {
904 err = netlink_attachskb(sk, skb, &timeo, ssk);
910 return netlink_sendskb(sk, skb);
912 EXPORT_SYMBOL(netlink_unicast);
914 int netlink_has_listeners(struct sock *sk, unsigned int group)
917 unsigned long *listeners;
919 BUG_ON(!netlink_is_kernel(sk));
922 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
924 if (group - 1 < nl_table[sk->sk_protocol].groups)
925 res = test_bit(group - 1, listeners);
931 EXPORT_SYMBOL_GPL(netlink_has_listeners);
933 static inline int netlink_broadcast_deliver(struct sock *sk,
936 struct netlink_sock *nlk = nlk_sk(sk);
938 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
939 !test_bit(0, &nlk->state)) {
940 skb_set_owner_r(skb, sk);
941 skb_queue_tail(&sk->sk_receive_queue, skb);
942 sk->sk_data_ready(sk, skb->len);
943 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
948 struct netlink_broadcast_data {
949 struct sock *exclude_sk;
954 int delivery_failure;
958 struct sk_buff *skb, *skb2;
961 static inline int do_one_broadcast(struct sock *sk,
962 struct netlink_broadcast_data *p)
964 struct netlink_sock *nlk = nlk_sk(sk);
967 if (p->exclude_sk == sk)
970 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
971 !test_bit(p->group - 1, nlk->groups))
974 if (!net_eq(sock_net(sk), p->net))
983 if (p->skb2 == NULL) {
984 if (skb_shared(p->skb)) {
985 p->skb2 = skb_clone(p->skb, p->allocation);
987 p->skb2 = skb_get(p->skb);
989 * skb ownership may have been set when
990 * delivered to a previous socket.
995 if (p->skb2 == NULL) {
997 /* Clone failed. Notify ALL listeners. */
999 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1000 p->delivery_failure = 1;
1001 } else if (sk_filter(sk, p->skb2)) {
1004 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1005 netlink_overrun(sk);
1006 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1007 p->delivery_failure = 1;
1009 p->congested |= val;
1019 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
1020 u32 group, gfp_t allocation)
1022 struct net *net = sock_net(ssk);
1023 struct netlink_broadcast_data info;
1024 struct hlist_node *node;
1027 skb = netlink_trim(skb, allocation);
1029 info.exclude_sk = ssk;
1034 info.delivery_failure = 0;
1037 info.allocation = allocation;
1041 /* While we sleep in clone, do not allow to change socket list */
1043 netlink_lock_table();
1045 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1046 do_one_broadcast(sk, &info);
1050 netlink_unlock_table();
1052 kfree_skb(info.skb2);
1054 if (info.delivery_failure)
1057 if (info.delivered) {
1058 if (info.congested && (allocation & __GFP_WAIT))
1064 EXPORT_SYMBOL(netlink_broadcast);
1066 struct netlink_set_err_data {
1067 struct sock *exclude_sk;
1073 static inline int do_one_set_err(struct sock *sk,
1074 struct netlink_set_err_data *p)
1076 struct netlink_sock *nlk = nlk_sk(sk);
1078 if (sk == p->exclude_sk)
1081 if (sock_net(sk) != sock_net(p->exclude_sk))
1084 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1085 !test_bit(p->group - 1, nlk->groups))
1088 sk->sk_err = p->code;
1089 sk->sk_error_report(sk);
1094 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
1096 struct netlink_set_err_data info;
1097 struct hlist_node *node;
1100 info.exclude_sk = ssk;
1105 read_lock(&nl_table_lock);
1107 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1108 do_one_set_err(sk, &info);
1110 read_unlock(&nl_table_lock);
1113 /* must be called with netlink table grabbed */
1114 static void netlink_update_socket_mc(struct netlink_sock *nlk,
1118 int old, new = !!is_new, subscriptions;
1120 old = test_bit(group - 1, nlk->groups);
1121 subscriptions = nlk->subscriptions - old + new;
1123 __set_bit(group - 1, nlk->groups);
1125 __clear_bit(group - 1, nlk->groups);
1126 netlink_update_subscriptions(&nlk->sk, subscriptions);
1127 netlink_update_listeners(&nlk->sk);
1130 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1131 char __user *optval, int optlen)
1133 struct sock *sk = sock->sk;
1134 struct netlink_sock *nlk = nlk_sk(sk);
1135 unsigned int val = 0;
1138 if (level != SOL_NETLINK)
1139 return -ENOPROTOOPT;
1141 if (optlen >= sizeof(int) &&
1142 get_user(val, (unsigned int __user *)optval))
1146 case NETLINK_PKTINFO:
1148 nlk->flags |= NETLINK_RECV_PKTINFO;
1150 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1153 case NETLINK_ADD_MEMBERSHIP:
1154 case NETLINK_DROP_MEMBERSHIP: {
1155 if (!netlink_capable(sock, NL_NONROOT_RECV))
1157 err = netlink_realloc_groups(sk);
1160 if (!val || val - 1 >= nlk->ngroups)
1162 netlink_table_grab();
1163 netlink_update_socket_mc(nlk, val,
1164 optname == NETLINK_ADD_MEMBERSHIP);
1165 netlink_table_ungrab();
1169 case NETLINK_BROADCAST_ERROR:
1171 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1173 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1182 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1183 char __user *optval, int __user *optlen)
1185 struct sock *sk = sock->sk;
1186 struct netlink_sock *nlk = nlk_sk(sk);
1189 if (level != SOL_NETLINK)
1190 return -ENOPROTOOPT;
1192 if (get_user(len, optlen))
1198 case NETLINK_PKTINFO:
1199 if (len < sizeof(int))
1202 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
1203 if (put_user(len, optlen) ||
1204 put_user(val, optval))
1208 case NETLINK_BROADCAST_ERROR:
1209 if (len < sizeof(int))
1212 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1213 if (put_user(len, optlen) ||
1214 put_user(val, optval))
1224 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1226 struct nl_pktinfo info;
1228 info.group = NETLINK_CB(skb).dst_group;
1229 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1232 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1233 struct msghdr *msg, size_t len)
1235 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1236 struct sock *sk = sock->sk;
1237 struct netlink_sock *nlk = nlk_sk(sk);
1238 struct sockaddr_nl *addr = msg->msg_name;
1241 struct sk_buff *skb;
1243 struct scm_cookie scm;
1245 if (msg->msg_flags&MSG_OOB)
1248 if (NULL == siocb->scm)
1250 err = scm_send(sock, msg, siocb->scm);
1254 if (msg->msg_namelen) {
1255 if (addr->nl_family != AF_NETLINK)
1257 dst_pid = addr->nl_pid;
1258 dst_group = ffs(addr->nl_groups);
1259 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
1262 dst_pid = nlk->dst_pid;
1263 dst_group = nlk->dst_group;
1267 err = netlink_autobind(sock);
1273 if (len > sk->sk_sndbuf - 32)
1276 skb = alloc_skb(len, GFP_KERNEL);
1280 NETLINK_CB(skb).pid = nlk->pid;
1281 NETLINK_CB(skb).dst_group = dst_group;
1282 NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
1283 NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
1284 security_task_getsecid(current, &(NETLINK_CB(skb).sid));
1285 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1287 /* What can I do? Netlink is asynchronous, so that
1288 we will have to save current capabilities to
1289 check them, when this message will be delivered
1290 to corresponding kernel module. --ANK (980802)
1294 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1299 err = security_netlink_send(sk, skb);
1306 atomic_inc(&skb->users);
1307 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
1309 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1315 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1316 struct msghdr *msg, size_t len,
1319 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1320 struct scm_cookie scm;
1321 struct sock *sk = sock->sk;
1322 struct netlink_sock *nlk = nlk_sk(sk);
1323 int noblock = flags&MSG_DONTWAIT;
1325 struct sk_buff *skb;
1333 skb = skb_recv_datagram(sk, flags, noblock, &err);
1337 msg->msg_namelen = 0;
1341 msg->msg_flags |= MSG_TRUNC;
1345 skb_reset_transport_header(skb);
1346 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1348 if (msg->msg_name) {
1349 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
1350 addr->nl_family = AF_NETLINK;
1352 addr->nl_pid = NETLINK_CB(skb).pid;
1353 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1354 msg->msg_namelen = sizeof(*addr);
1357 if (nlk->flags & NETLINK_RECV_PKTINFO)
1358 netlink_cmsg_recv_pktinfo(msg, skb);
1360 if (NULL == siocb->scm) {
1361 memset(&scm, 0, sizeof(scm));
1364 siocb->scm->creds = *NETLINK_CREDS(skb);
1365 if (flags & MSG_TRUNC)
1367 skb_free_datagram(sk, skb);
1369 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1372 scm_recv(sock, msg, siocb->scm, flags);
1374 netlink_rcv_wake(sk);
1375 return err ? : copied;
1378 static void netlink_data_ready(struct sock *sk, int len)
1384 * We export these functions to other modules. They provide a
1385 * complete set of kernel non-blocking support for message
1390 netlink_kernel_create(struct net *net, int unit, unsigned int groups,
1391 void (*input)(struct sk_buff *skb),
1392 struct mutex *cb_mutex, struct module *module)
1394 struct socket *sock;
1396 struct netlink_sock *nlk;
1397 unsigned long *listeners = NULL;
1401 if (unit < 0 || unit >= MAX_LINKS)
1404 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1408 * We have to just have a reference on the net from sk, but don't
1409 * get_net it. Besides, we cannot get and then put the net here.
1410 * So we create one inside init_net and the move it to net.
1413 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1414 goto out_sock_release_nosk;
1417 sk_change_net(sk, net);
1422 listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
1424 goto out_sock_release;
1426 sk->sk_data_ready = netlink_data_ready;
1428 nlk_sk(sk)->netlink_rcv = input;
1430 if (netlink_insert(sk, net, 0))
1431 goto out_sock_release;
1434 nlk->flags |= NETLINK_KERNEL_SOCKET;
1436 netlink_table_grab();
1437 if (!nl_table[unit].registered) {
1438 nl_table[unit].groups = groups;
1439 nl_table[unit].listeners = listeners;
1440 nl_table[unit].cb_mutex = cb_mutex;
1441 nl_table[unit].module = module;
1442 nl_table[unit].registered = 1;
1445 nl_table[unit].registered++;
1447 netlink_table_ungrab();
1452 netlink_kernel_release(sk);
1455 out_sock_release_nosk:
1459 EXPORT_SYMBOL(netlink_kernel_create);
1463 netlink_kernel_release(struct sock *sk)
1465 sk_release_kernel(sk);
1467 EXPORT_SYMBOL(netlink_kernel_release);
1471 * netlink_change_ngroups - change number of multicast groups
1473 * This changes the number of multicast groups that are available
1474 * on a certain netlink family. Note that it is not possible to
1475 * change the number of groups to below 32. Also note that it does
1476 * not implicitly call netlink_clear_multicast_users() when the
1477 * number of groups is reduced.
1479 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1480 * @groups: The new number of groups.
1482 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1484 unsigned long *listeners, *old = NULL;
1485 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1491 netlink_table_grab();
1492 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1493 listeners = kzalloc(NLGRPSZ(groups), GFP_ATOMIC);
1498 old = tbl->listeners;
1499 memcpy(listeners, old, NLGRPSZ(tbl->groups));
1500 rcu_assign_pointer(tbl->listeners, listeners);
1502 tbl->groups = groups;
1505 netlink_table_ungrab();
1510 EXPORT_SYMBOL(netlink_change_ngroups);
1513 * netlink_clear_multicast_users - kick off multicast listeners
1515 * This function removes all listeners from the given group.
1516 * @ksk: The kernel netlink socket, as returned by
1517 * netlink_kernel_create().
1518 * @group: The multicast group to clear.
1520 void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1523 struct hlist_node *node;
1524 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1526 netlink_table_grab();
1528 sk_for_each_bound(sk, node, &tbl->mc_list)
1529 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1531 netlink_table_ungrab();
1533 EXPORT_SYMBOL(netlink_clear_multicast_users);
1535 void netlink_set_nonroot(int protocol, unsigned int flags)
1537 if ((unsigned int)protocol < MAX_LINKS)
1538 nl_table[protocol].nl_nonroot = flags;
1540 EXPORT_SYMBOL(netlink_set_nonroot);
1542 static void netlink_destroy_callback(struct netlink_callback *cb)
1549 * It looks a bit ugly.
1550 * It would be better to create kernel thread.
1553 static int netlink_dump(struct sock *sk)
1555 struct netlink_sock *nlk = nlk_sk(sk);
1556 struct netlink_callback *cb;
1557 struct sk_buff *skb;
1558 struct nlmsghdr *nlh;
1559 int len, err = -ENOBUFS;
1561 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1565 mutex_lock(nlk->cb_mutex);
1573 len = cb->dump(skb, cb);
1576 mutex_unlock(nlk->cb_mutex);
1578 if (sk_filter(sk, skb))
1581 skb_queue_tail(&sk->sk_receive_queue, skb);
1582 sk->sk_data_ready(sk, skb->len);
1587 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1591 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1593 if (sk_filter(sk, skb))
1596 skb_queue_tail(&sk->sk_receive_queue, skb);
1597 sk->sk_data_ready(sk, skb->len);
1603 mutex_unlock(nlk->cb_mutex);
1605 netlink_destroy_callback(cb);
1609 mutex_unlock(nlk->cb_mutex);
1615 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1616 struct nlmsghdr *nlh,
1617 int (*dump)(struct sk_buff *skb,
1618 struct netlink_callback *),
1619 int (*done)(struct netlink_callback *))
1621 struct netlink_callback *cb;
1623 struct netlink_sock *nlk;
1625 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
1632 atomic_inc(&skb->users);
1635 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
1637 netlink_destroy_callback(cb);
1638 return -ECONNREFUSED;
1641 /* A dump is in progress... */
1642 mutex_lock(nlk->cb_mutex);
1644 mutex_unlock(nlk->cb_mutex);
1645 netlink_destroy_callback(cb);
1650 mutex_unlock(nlk->cb_mutex);
1655 /* We successfully started a dump, by returning -EINTR we
1656 * signal not to send ACK even if it was requested.
1660 EXPORT_SYMBOL(netlink_dump_start);
1662 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1664 struct sk_buff *skb;
1665 struct nlmsghdr *rep;
1666 struct nlmsgerr *errmsg;
1667 size_t payload = sizeof(*errmsg);
1669 /* error messages get the original request appened */
1671 payload += nlmsg_len(nlh);
1673 skb = nlmsg_new(payload, GFP_KERNEL);
1677 sk = netlink_lookup(sock_net(in_skb->sk),
1678 in_skb->sk->sk_protocol,
1679 NETLINK_CB(in_skb).pid);
1681 sk->sk_err = ENOBUFS;
1682 sk->sk_error_report(sk);
1688 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
1689 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
1690 errmsg = nlmsg_data(rep);
1691 errmsg->error = err;
1692 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
1693 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1695 EXPORT_SYMBOL(netlink_ack);
1697 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
1700 struct nlmsghdr *nlh;
1703 while (skb->len >= nlmsg_total_size(0)) {
1706 nlh = nlmsg_hdr(skb);
1709 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
1712 /* Only requests are handled by the kernel */
1713 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1716 /* Skip control messages */
1717 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
1725 if (nlh->nlmsg_flags & NLM_F_ACK || err)
1726 netlink_ack(skb, nlh, err);
1729 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
1730 if (msglen > skb->len)
1732 skb_pull(skb, msglen);
1737 EXPORT_SYMBOL(netlink_rcv_skb);
1740 * nlmsg_notify - send a notification netlink message
1741 * @sk: netlink socket to use
1742 * @skb: notification message
1743 * @pid: destination netlink pid for reports or 0
1744 * @group: destination multicast group or 0
1745 * @report: 1 to report back, 0 to disable
1746 * @flags: allocation flags
1748 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
1749 unsigned int group, int report, gfp_t flags)
1754 int exclude_pid = 0;
1757 atomic_inc(&skb->users);
1761 /* errors reported via destination sk->sk_err, but propagate
1762 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
1763 err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
1769 err2 = nlmsg_unicast(sk, skb, pid);
1770 if (!err || err == -ESRCH)
1776 EXPORT_SYMBOL(nlmsg_notify);
1778 #ifdef CONFIG_PROC_FS
1779 struct nl_seq_iter {
1780 struct seq_net_private p;
1785 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1787 struct nl_seq_iter *iter = seq->private;
1790 struct hlist_node *node;
1793 for (i = 0; i < MAX_LINKS; i++) {
1794 struct nl_pid_hash *hash = &nl_table[i].hash;
1796 for (j = 0; j <= hash->mask; j++) {
1797 sk_for_each(s, node, &hash->table[j]) {
1798 if (sock_net(s) != seq_file_net(seq))
1812 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1813 __acquires(nl_table_lock)
1815 read_lock(&nl_table_lock);
1816 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1819 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1822 struct nl_seq_iter *iter;
1827 if (v == SEQ_START_TOKEN)
1828 return netlink_seq_socket_idx(seq, 0);
1830 iter = seq->private;
1834 } while (s && sock_net(s) != seq_file_net(seq));
1839 j = iter->hash_idx + 1;
1842 struct nl_pid_hash *hash = &nl_table[i].hash;
1844 for (; j <= hash->mask; j++) {
1845 s = sk_head(&hash->table[j]);
1846 while (s && sock_net(s) != seq_file_net(seq))
1856 } while (++i < MAX_LINKS);
1861 static void netlink_seq_stop(struct seq_file *seq, void *v)
1862 __releases(nl_table_lock)
1864 read_unlock(&nl_table_lock);
1868 static int netlink_seq_show(struct seq_file *seq, void *v)
1870 if (v == SEQ_START_TOKEN)
1872 "sk Eth Pid Groups "
1873 "Rmem Wmem Dump Locks\n");
1876 struct netlink_sock *nlk = nlk_sk(s);
1878 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1882 nlk->groups ? (u32)nlk->groups[0] : 0,
1883 atomic_read(&s->sk_rmem_alloc),
1884 atomic_read(&s->sk_wmem_alloc),
1886 atomic_read(&s->sk_refcnt)
1893 static const struct seq_operations netlink_seq_ops = {
1894 .start = netlink_seq_start,
1895 .next = netlink_seq_next,
1896 .stop = netlink_seq_stop,
1897 .show = netlink_seq_show,
1901 static int netlink_seq_open(struct inode *inode, struct file *file)
1903 return seq_open_net(inode, file, &netlink_seq_ops,
1904 sizeof(struct nl_seq_iter));
1907 static const struct file_operations netlink_seq_fops = {
1908 .owner = THIS_MODULE,
1909 .open = netlink_seq_open,
1911 .llseek = seq_lseek,
1912 .release = seq_release_net,
1917 int netlink_register_notifier(struct notifier_block *nb)
1919 return atomic_notifier_chain_register(&netlink_chain, nb);
1921 EXPORT_SYMBOL(netlink_register_notifier);
1923 int netlink_unregister_notifier(struct notifier_block *nb)
1925 return atomic_notifier_chain_unregister(&netlink_chain, nb);
1927 EXPORT_SYMBOL(netlink_unregister_notifier);
1929 static const struct proto_ops netlink_ops = {
1930 .family = PF_NETLINK,
1931 .owner = THIS_MODULE,
1932 .release = netlink_release,
1933 .bind = netlink_bind,
1934 .connect = netlink_connect,
1935 .socketpair = sock_no_socketpair,
1936 .accept = sock_no_accept,
1937 .getname = netlink_getname,
1938 .poll = datagram_poll,
1939 .ioctl = sock_no_ioctl,
1940 .listen = sock_no_listen,
1941 .shutdown = sock_no_shutdown,
1942 .setsockopt = netlink_setsockopt,
1943 .getsockopt = netlink_getsockopt,
1944 .sendmsg = netlink_sendmsg,
1945 .recvmsg = netlink_recvmsg,
1946 .mmap = sock_no_mmap,
1947 .sendpage = sock_no_sendpage,
1950 static struct net_proto_family netlink_family_ops = {
1951 .family = PF_NETLINK,
1952 .create = netlink_create,
1953 .owner = THIS_MODULE, /* for consistency 8) */
1956 static int __net_init netlink_net_init(struct net *net)
1958 #ifdef CONFIG_PROC_FS
1959 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
1965 static void __net_exit netlink_net_exit(struct net *net)
1967 #ifdef CONFIG_PROC_FS
1968 proc_net_remove(net, "netlink");
1972 static struct pernet_operations __net_initdata netlink_net_ops = {
1973 .init = netlink_net_init,
1974 .exit = netlink_net_exit,
1977 static int __init netlink_proto_init(void)
1979 struct sk_buff *dummy_skb;
1981 unsigned long limit;
1983 int err = proto_register(&netlink_proto, 0);
1988 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
1990 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
1994 if (num_physpages >= (128 * 1024))
1995 limit = num_physpages >> (21 - PAGE_SHIFT);
1997 limit = num_physpages >> (23 - PAGE_SHIFT);
1999 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2000 limit = (1UL << order) / sizeof(struct hlist_head);
2001 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
2003 for (i = 0; i < MAX_LINKS; i++) {
2004 struct nl_pid_hash *hash = &nl_table[i].hash;
2006 hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
2009 nl_pid_hash_free(nl_table[i].hash.table,
2010 1 * sizeof(*hash->table));
2014 hash->max_shift = order;
2017 hash->rehash_time = jiffies;
2020 sock_register(&netlink_family_ops);
2021 register_pernet_subsys(&netlink_net_ops);
2022 /* The netlink device handler may be needed early. */
2027 panic("netlink_init: Cannot allocate nl_table\n");
2030 core_initcall(netlink_proto_init);