[NET]: kill gratitious includes of major.h
[linux-2.6/zen-sources.git] / net / netlink / af_netlink.c
blob29a5fd231eac9a97bb247162e8a8fc20a8163cea
1 /*
2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@redhat.com>
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)
19 #include <linux/config.h>
20 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/errno.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/socket.h>
30 #include <linux/un.h>
31 #include <linux/fcntl.h>
32 #include <linux/termios.h>
33 #include <linux/sockios.h>
34 #include <linux/net.h>
35 #include <linux/fs.h>
36 #include <linux/slab.h>
37 #include <asm/uaccess.h>
38 #include <linux/skbuff.h>
39 #include <linux/netdevice.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/smp_lock.h>
44 #include <linux/notifier.h>
45 #include <linux/security.h>
46 #include <linux/jhash.h>
47 #include <linux/jiffies.h>
48 #include <linux/random.h>
49 #include <linux/bitops.h>
50 #include <linux/mm.h>
51 #include <linux/types.h>
52 #include <net/sock.h>
53 #include <net/scm.h>
55 #define Nprintk(a...)
57 struct netlink_sock {
58 /* struct sock has to be the first member of netlink_sock */
59 struct sock sk;
60 u32 pid;
61 unsigned int groups;
62 u32 dst_pid;
63 unsigned int dst_groups;
64 unsigned long state;
65 wait_queue_head_t wait;
66 struct netlink_callback *cb;
67 spinlock_t cb_lock;
68 void (*data_ready)(struct sock *sk, int bytes);
71 static inline struct netlink_sock *nlk_sk(struct sock *sk)
73 return (struct netlink_sock *)sk;
76 struct nl_pid_hash {
77 struct hlist_head *table;
78 unsigned long rehash_time;
80 unsigned int mask;
81 unsigned int shift;
83 unsigned int entries;
84 unsigned int max_shift;
86 u32 rnd;
89 struct netlink_table {
90 struct nl_pid_hash hash;
91 struct hlist_head mc_list;
92 unsigned int nl_nonroot;
95 static struct netlink_table *nl_table;
97 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
99 static int netlink_dump(struct sock *sk);
100 static void netlink_destroy_callback(struct netlink_callback *cb);
102 static DEFINE_RWLOCK(nl_table_lock);
103 static atomic_t nl_table_users = ATOMIC_INIT(0);
105 static struct notifier_block *netlink_chain;
107 static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
109 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
112 static void netlink_sock_destruct(struct sock *sk)
114 skb_queue_purge(&sk->sk_receive_queue);
116 if (!sock_flag(sk, SOCK_DEAD)) {
117 printk("Freeing alive netlink socket %p\n", sk);
118 return;
120 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
121 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
122 BUG_TRAP(!nlk_sk(sk)->cb);
125 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
126 * Look, when several writers sleep and reader wakes them up, all but one
127 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
128 * this, _but_ remember, it adds useless work on UP machines.
131 static void netlink_table_grab(void)
133 write_lock_bh(&nl_table_lock);
135 if (atomic_read(&nl_table_users)) {
136 DECLARE_WAITQUEUE(wait, current);
138 add_wait_queue_exclusive(&nl_table_wait, &wait);
139 for(;;) {
140 set_current_state(TASK_UNINTERRUPTIBLE);
141 if (atomic_read(&nl_table_users) == 0)
142 break;
143 write_unlock_bh(&nl_table_lock);
144 schedule();
145 write_lock_bh(&nl_table_lock);
148 __set_current_state(TASK_RUNNING);
149 remove_wait_queue(&nl_table_wait, &wait);
153 static __inline__ void netlink_table_ungrab(void)
155 write_unlock_bh(&nl_table_lock);
156 wake_up(&nl_table_wait);
159 static __inline__ void
160 netlink_lock_table(void)
162 /* read_lock() synchronizes us to netlink_table_grab */
164 read_lock(&nl_table_lock);
165 atomic_inc(&nl_table_users);
166 read_unlock(&nl_table_lock);
169 static __inline__ void
170 netlink_unlock_table(void)
172 if (atomic_dec_and_test(&nl_table_users))
173 wake_up(&nl_table_wait);
176 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
178 struct nl_pid_hash *hash = &nl_table[protocol].hash;
179 struct hlist_head *head;
180 struct sock *sk;
181 struct hlist_node *node;
183 read_lock(&nl_table_lock);
184 head = nl_pid_hashfn(hash, pid);
185 sk_for_each(sk, node, head) {
186 if (nlk_sk(sk)->pid == pid) {
187 sock_hold(sk);
188 goto found;
191 sk = NULL;
192 found:
193 read_unlock(&nl_table_lock);
194 return sk;
197 static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
199 if (size <= PAGE_SIZE)
200 return kmalloc(size, GFP_ATOMIC);
201 else
202 return (struct hlist_head *)
203 __get_free_pages(GFP_ATOMIC, get_order(size));
206 static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
208 if (size <= PAGE_SIZE)
209 kfree(table);
210 else
211 free_pages((unsigned long)table, get_order(size));
214 static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
216 unsigned int omask, mask, shift;
217 size_t osize, size;
218 struct hlist_head *otable, *table;
219 int i;
221 omask = mask = hash->mask;
222 osize = size = (mask + 1) * sizeof(*table);
223 shift = hash->shift;
225 if (grow) {
226 if (++shift > hash->max_shift)
227 return 0;
228 mask = mask * 2 + 1;
229 size *= 2;
232 table = nl_pid_hash_alloc(size);
233 if (!table)
234 return 0;
236 memset(table, 0, size);
237 otable = hash->table;
238 hash->table = table;
239 hash->mask = mask;
240 hash->shift = shift;
241 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
243 for (i = 0; i <= omask; i++) {
244 struct sock *sk;
245 struct hlist_node *node, *tmp;
247 sk_for_each_safe(sk, node, tmp, &otable[i])
248 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
251 nl_pid_hash_free(otable, osize);
252 hash->rehash_time = jiffies + 10 * 60 * HZ;
253 return 1;
256 static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
258 int avg = hash->entries >> hash->shift;
260 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
261 return 1;
263 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
264 nl_pid_hash_rehash(hash, 0);
265 return 1;
268 return 0;
271 static struct proto_ops netlink_ops;
273 static int netlink_insert(struct sock *sk, u32 pid)
275 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
276 struct hlist_head *head;
277 int err = -EADDRINUSE;
278 struct sock *osk;
279 struct hlist_node *node;
280 int len;
282 netlink_table_grab();
283 head = nl_pid_hashfn(hash, pid);
284 len = 0;
285 sk_for_each(osk, node, head) {
286 if (nlk_sk(osk)->pid == pid)
287 break;
288 len++;
290 if (node)
291 goto err;
293 err = -EBUSY;
294 if (nlk_sk(sk)->pid)
295 goto err;
297 err = -ENOMEM;
298 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
299 goto err;
301 if (len && nl_pid_hash_dilute(hash, len))
302 head = nl_pid_hashfn(hash, pid);
303 hash->entries++;
304 nlk_sk(sk)->pid = pid;
305 sk_add_node(sk, head);
306 err = 0;
308 err:
309 netlink_table_ungrab();
310 return err;
313 static void netlink_remove(struct sock *sk)
315 netlink_table_grab();
316 nl_table[sk->sk_protocol].hash.entries--;
317 sk_del_node_init(sk);
318 if (nlk_sk(sk)->groups)
319 __sk_del_bind_node(sk);
320 netlink_table_ungrab();
323 static struct proto netlink_proto = {
324 .name = "NETLINK",
325 .owner = THIS_MODULE,
326 .obj_size = sizeof(struct netlink_sock),
329 static int netlink_create(struct socket *sock, int protocol)
331 struct sock *sk;
332 struct netlink_sock *nlk;
334 sock->state = SS_UNCONNECTED;
336 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
337 return -ESOCKTNOSUPPORT;
339 if (protocol<0 || protocol >= MAX_LINKS)
340 return -EPROTONOSUPPORT;
342 sock->ops = &netlink_ops;
344 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
345 if (!sk)
346 return -ENOMEM;
348 sock_init_data(sock, sk);
350 nlk = nlk_sk(sk);
352 spin_lock_init(&nlk->cb_lock);
353 init_waitqueue_head(&nlk->wait);
354 sk->sk_destruct = netlink_sock_destruct;
356 sk->sk_protocol = protocol;
357 return 0;
360 static int netlink_release(struct socket *sock)
362 struct sock *sk = sock->sk;
363 struct netlink_sock *nlk;
365 if (!sk)
366 return 0;
368 netlink_remove(sk);
369 nlk = nlk_sk(sk);
371 spin_lock(&nlk->cb_lock);
372 if (nlk->cb) {
373 nlk->cb->done(nlk->cb);
374 netlink_destroy_callback(nlk->cb);
375 nlk->cb = NULL;
376 __sock_put(sk);
378 spin_unlock(&nlk->cb_lock);
380 /* OK. Socket is unlinked, and, therefore,
381 no new packets will arrive */
383 sock_orphan(sk);
384 sock->sk = NULL;
385 wake_up_interruptible_all(&nlk->wait);
387 skb_queue_purge(&sk->sk_write_queue);
389 if (nlk->pid && !nlk->groups) {
390 struct netlink_notify n = {
391 .protocol = sk->sk_protocol,
392 .pid = nlk->pid,
394 notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
397 sock_put(sk);
398 return 0;
401 static int netlink_autobind(struct socket *sock)
403 struct sock *sk = sock->sk;
404 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
405 struct hlist_head *head;
406 struct sock *osk;
407 struct hlist_node *node;
408 s32 pid = current->pid;
409 int err;
410 static s32 rover = -4097;
412 retry:
413 cond_resched();
414 netlink_table_grab();
415 head = nl_pid_hashfn(hash, pid);
416 sk_for_each(osk, node, head) {
417 if (nlk_sk(osk)->pid == pid) {
418 /* Bind collision, search negative pid values. */
419 pid = rover--;
420 if (rover > -4097)
421 rover = -4097;
422 netlink_table_ungrab();
423 goto retry;
426 netlink_table_ungrab();
428 err = netlink_insert(sk, pid);
429 if (err == -EADDRINUSE)
430 goto retry;
431 return 0;
434 static inline int netlink_capable(struct socket *sock, unsigned int flag)
436 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
437 capable(CAP_NET_ADMIN);
440 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
442 struct sock *sk = sock->sk;
443 struct netlink_sock *nlk = nlk_sk(sk);
444 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
445 int err;
447 if (nladdr->nl_family != AF_NETLINK)
448 return -EINVAL;
450 /* Only superuser is allowed to listen multicasts */
451 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
452 return -EPERM;
454 if (nlk->pid) {
455 if (nladdr->nl_pid != nlk->pid)
456 return -EINVAL;
457 } else {
458 err = nladdr->nl_pid ?
459 netlink_insert(sk, nladdr->nl_pid) :
460 netlink_autobind(sock);
461 if (err)
462 return err;
465 if (!nladdr->nl_groups && !nlk->groups)
466 return 0;
468 netlink_table_grab();
469 if (nlk->groups && !nladdr->nl_groups)
470 __sk_del_bind_node(sk);
471 else if (!nlk->groups && nladdr->nl_groups)
472 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
473 nlk->groups = nladdr->nl_groups;
474 netlink_table_ungrab();
476 return 0;
479 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
480 int alen, int flags)
482 int err = 0;
483 struct sock *sk = sock->sk;
484 struct netlink_sock *nlk = nlk_sk(sk);
485 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
487 if (addr->sa_family == AF_UNSPEC) {
488 sk->sk_state = NETLINK_UNCONNECTED;
489 nlk->dst_pid = 0;
490 nlk->dst_groups = 0;
491 return 0;
493 if (addr->sa_family != AF_NETLINK)
494 return -EINVAL;
496 /* Only superuser is allowed to send multicasts */
497 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
498 return -EPERM;
500 if (!nlk->pid)
501 err = netlink_autobind(sock);
503 if (err == 0) {
504 sk->sk_state = NETLINK_CONNECTED;
505 nlk->dst_pid = nladdr->nl_pid;
506 nlk->dst_groups = nladdr->nl_groups;
509 return err;
512 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
514 struct sock *sk = sock->sk;
515 struct netlink_sock *nlk = nlk_sk(sk);
516 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
518 nladdr->nl_family = AF_NETLINK;
519 nladdr->nl_pad = 0;
520 *addr_len = sizeof(*nladdr);
522 if (peer) {
523 nladdr->nl_pid = nlk->dst_pid;
524 nladdr->nl_groups = nlk->dst_groups;
525 } else {
526 nladdr->nl_pid = nlk->pid;
527 nladdr->nl_groups = nlk->groups;
529 return 0;
532 static void netlink_overrun(struct sock *sk)
534 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
535 sk->sk_err = ENOBUFS;
536 sk->sk_error_report(sk);
540 static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
542 int protocol = ssk->sk_protocol;
543 struct sock *sock;
544 struct netlink_sock *nlk;
546 sock = netlink_lookup(protocol, pid);
547 if (!sock)
548 return ERR_PTR(-ECONNREFUSED);
550 /* Don't bother queuing skb if kernel socket has no input function */
551 nlk = nlk_sk(sock);
552 if ((nlk->pid == 0 && !nlk->data_ready) ||
553 (sock->sk_state == NETLINK_CONNECTED &&
554 nlk->dst_pid != nlk_sk(ssk)->pid)) {
555 sock_put(sock);
556 return ERR_PTR(-ECONNREFUSED);
558 return sock;
561 struct sock *netlink_getsockbyfilp(struct file *filp)
563 struct inode *inode = filp->f_dentry->d_inode;
564 struct sock *sock;
566 if (!S_ISSOCK(inode->i_mode))
567 return ERR_PTR(-ENOTSOCK);
569 sock = SOCKET_I(inode)->sk;
570 if (sock->sk_family != AF_NETLINK)
571 return ERR_PTR(-EINVAL);
573 sock_hold(sock);
574 return sock;
578 * Attach a skb to a netlink socket.
579 * The caller must hold a reference to the destination socket. On error, the
580 * reference is dropped. The skb is not send to the destination, just all
581 * all error checks are performed and memory in the queue is reserved.
582 * Return values:
583 * < 0: error. skb freed, reference to sock dropped.
584 * 0: continue
585 * 1: repeat lookup - reference dropped while waiting for socket memory.
587 int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
589 struct netlink_sock *nlk;
591 nlk = nlk_sk(sk);
593 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
594 test_bit(0, &nlk->state)) {
595 DECLARE_WAITQUEUE(wait, current);
596 if (!timeo) {
597 if (!nlk->pid)
598 netlink_overrun(sk);
599 sock_put(sk);
600 kfree_skb(skb);
601 return -EAGAIN;
604 __set_current_state(TASK_INTERRUPTIBLE);
605 add_wait_queue(&nlk->wait, &wait);
607 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
608 test_bit(0, &nlk->state)) &&
609 !sock_flag(sk, SOCK_DEAD))
610 timeo = schedule_timeout(timeo);
612 __set_current_state(TASK_RUNNING);
613 remove_wait_queue(&nlk->wait, &wait);
614 sock_put(sk);
616 if (signal_pending(current)) {
617 kfree_skb(skb);
618 return sock_intr_errno(timeo);
620 return 1;
622 skb_set_owner_r(skb, sk);
623 return 0;
626 int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
628 struct netlink_sock *nlk;
629 int len = skb->len;
631 nlk = nlk_sk(sk);
633 skb_queue_tail(&sk->sk_receive_queue, skb);
634 sk->sk_data_ready(sk, len);
635 sock_put(sk);
636 return len;
639 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
641 kfree_skb(skb);
642 sock_put(sk);
645 static inline struct sk_buff *netlink_trim(struct sk_buff *skb, int allocation)
647 int delta;
649 skb_orphan(skb);
651 delta = skb->end - skb->tail;
652 if (delta * 2 < skb->truesize)
653 return skb;
655 if (skb_shared(skb)) {
656 struct sk_buff *nskb = skb_clone(skb, allocation);
657 if (!nskb)
658 return skb;
659 kfree_skb(skb);
660 skb = nskb;
663 if (!pskb_expand_head(skb, 0, -delta, allocation))
664 skb->truesize -= delta;
666 return skb;
669 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
671 struct sock *sk;
672 int err;
673 long timeo;
675 skb = netlink_trim(skb, gfp_any());
677 timeo = sock_sndtimeo(ssk, nonblock);
678 retry:
679 sk = netlink_getsockbypid(ssk, pid);
680 if (IS_ERR(sk)) {
681 kfree_skb(skb);
682 return PTR_ERR(sk);
684 err = netlink_attachskb(sk, skb, nonblock, timeo);
685 if (err == 1)
686 goto retry;
687 if (err)
688 return err;
690 return netlink_sendskb(sk, skb, ssk->sk_protocol);
693 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
695 struct netlink_sock *nlk = nlk_sk(sk);
697 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
698 !test_bit(0, &nlk->state)) {
699 skb_set_owner_r(skb, sk);
700 skb_queue_tail(&sk->sk_receive_queue, skb);
701 sk->sk_data_ready(sk, skb->len);
702 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
704 return -1;
707 struct netlink_broadcast_data {
708 struct sock *exclude_sk;
709 u32 pid;
710 u32 group;
711 int failure;
712 int congested;
713 int delivered;
714 int allocation;
715 struct sk_buff *skb, *skb2;
718 static inline int do_one_broadcast(struct sock *sk,
719 struct netlink_broadcast_data *p)
721 struct netlink_sock *nlk = nlk_sk(sk);
722 int val;
724 if (p->exclude_sk == sk)
725 goto out;
727 if (nlk->pid == p->pid || !(nlk->groups & p->group))
728 goto out;
730 if (p->failure) {
731 netlink_overrun(sk);
732 goto out;
735 sock_hold(sk);
736 if (p->skb2 == NULL) {
737 if (atomic_read(&p->skb->users) != 1) {
738 p->skb2 = skb_clone(p->skb, p->allocation);
739 } else {
740 p->skb2 = p->skb;
741 atomic_inc(&p->skb->users);
744 if (p->skb2 == NULL) {
745 netlink_overrun(sk);
746 /* Clone failed. Notify ALL listeners. */
747 p->failure = 1;
748 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
749 netlink_overrun(sk);
750 } else {
751 p->congested |= val;
752 p->delivered = 1;
753 p->skb2 = NULL;
755 sock_put(sk);
757 out:
758 return 0;
761 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
762 u32 group, int allocation)
764 struct netlink_broadcast_data info;
765 struct hlist_node *node;
766 struct sock *sk;
768 skb = netlink_trim(skb, allocation);
770 info.exclude_sk = ssk;
771 info.pid = pid;
772 info.group = group;
773 info.failure = 0;
774 info.congested = 0;
775 info.delivered = 0;
776 info.allocation = allocation;
777 info.skb = skb;
778 info.skb2 = NULL;
780 /* While we sleep in clone, do not allow to change socket list */
782 netlink_lock_table();
784 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
785 do_one_broadcast(sk, &info);
787 netlink_unlock_table();
789 if (info.skb2)
790 kfree_skb(info.skb2);
791 kfree_skb(skb);
793 if (info.delivered) {
794 if (info.congested && (allocation & __GFP_WAIT))
795 yield();
796 return 0;
798 if (info.failure)
799 return -ENOBUFS;
800 return -ESRCH;
803 struct netlink_set_err_data {
804 struct sock *exclude_sk;
805 u32 pid;
806 u32 group;
807 int code;
810 static inline int do_one_set_err(struct sock *sk,
811 struct netlink_set_err_data *p)
813 struct netlink_sock *nlk = nlk_sk(sk);
815 if (sk == p->exclude_sk)
816 goto out;
818 if (nlk->pid == p->pid || !(nlk->groups & p->group))
819 goto out;
821 sk->sk_err = p->code;
822 sk->sk_error_report(sk);
823 out:
824 return 0;
827 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
829 struct netlink_set_err_data info;
830 struct hlist_node *node;
831 struct sock *sk;
833 info.exclude_sk = ssk;
834 info.pid = pid;
835 info.group = group;
836 info.code = code;
838 read_lock(&nl_table_lock);
840 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
841 do_one_set_err(sk, &info);
843 read_unlock(&nl_table_lock);
846 static inline void netlink_rcv_wake(struct sock *sk)
848 struct netlink_sock *nlk = nlk_sk(sk);
850 if (!skb_queue_len(&sk->sk_receive_queue))
851 clear_bit(0, &nlk->state);
852 if (!test_bit(0, &nlk->state))
853 wake_up_interruptible(&nlk->wait);
856 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
857 struct msghdr *msg, size_t len)
859 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
860 struct sock *sk = sock->sk;
861 struct netlink_sock *nlk = nlk_sk(sk);
862 struct sockaddr_nl *addr=msg->msg_name;
863 u32 dst_pid;
864 u32 dst_groups;
865 struct sk_buff *skb;
866 int err;
867 struct scm_cookie scm;
869 if (msg->msg_flags&MSG_OOB)
870 return -EOPNOTSUPP;
872 if (NULL == siocb->scm)
873 siocb->scm = &scm;
874 err = scm_send(sock, msg, siocb->scm);
875 if (err < 0)
876 return err;
878 if (msg->msg_namelen) {
879 if (addr->nl_family != AF_NETLINK)
880 return -EINVAL;
881 dst_pid = addr->nl_pid;
882 dst_groups = addr->nl_groups;
883 if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
884 return -EPERM;
885 } else {
886 dst_pid = nlk->dst_pid;
887 dst_groups = nlk->dst_groups;
890 if (!nlk->pid) {
891 err = netlink_autobind(sock);
892 if (err)
893 goto out;
896 err = -EMSGSIZE;
897 if (len > sk->sk_sndbuf - 32)
898 goto out;
899 err = -ENOBUFS;
900 skb = alloc_skb(len, GFP_KERNEL);
901 if (skb==NULL)
902 goto out;
904 NETLINK_CB(skb).pid = nlk->pid;
905 NETLINK_CB(skb).groups = nlk->groups;
906 NETLINK_CB(skb).dst_pid = dst_pid;
907 NETLINK_CB(skb).dst_groups = dst_groups;
908 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
910 /* What can I do? Netlink is asynchronous, so that
911 we will have to save current capabilities to
912 check them, when this message will be delivered
913 to corresponding kernel module. --ANK (980802)
916 err = -EFAULT;
917 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
918 kfree_skb(skb);
919 goto out;
922 err = security_netlink_send(sk, skb);
923 if (err) {
924 kfree_skb(skb);
925 goto out;
928 if (dst_groups) {
929 atomic_inc(&skb->users);
930 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
932 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
934 out:
935 return err;
938 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
939 struct msghdr *msg, size_t len,
940 int flags)
942 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
943 struct scm_cookie scm;
944 struct sock *sk = sock->sk;
945 struct netlink_sock *nlk = nlk_sk(sk);
946 int noblock = flags&MSG_DONTWAIT;
947 size_t copied;
948 struct sk_buff *skb;
949 int err;
951 if (flags&MSG_OOB)
952 return -EOPNOTSUPP;
954 copied = 0;
956 skb = skb_recv_datagram(sk,flags,noblock,&err);
957 if (skb==NULL)
958 goto out;
960 msg->msg_namelen = 0;
962 copied = skb->len;
963 if (len < copied) {
964 msg->msg_flags |= MSG_TRUNC;
965 copied = len;
968 skb->h.raw = skb->data;
969 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
971 if (msg->msg_name) {
972 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
973 addr->nl_family = AF_NETLINK;
974 addr->nl_pad = 0;
975 addr->nl_pid = NETLINK_CB(skb).pid;
976 addr->nl_groups = NETLINK_CB(skb).dst_groups;
977 msg->msg_namelen = sizeof(*addr);
980 if (NULL == siocb->scm) {
981 memset(&scm, 0, sizeof(scm));
982 siocb->scm = &scm;
984 siocb->scm->creds = *NETLINK_CREDS(skb);
985 skb_free_datagram(sk, skb);
987 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
988 netlink_dump(sk);
990 scm_recv(sock, msg, siocb->scm, flags);
992 out:
993 netlink_rcv_wake(sk);
994 return err ? : copied;
997 static void netlink_data_ready(struct sock *sk, int len)
999 struct netlink_sock *nlk = nlk_sk(sk);
1001 if (nlk->data_ready)
1002 nlk->data_ready(sk, len);
1003 netlink_rcv_wake(sk);
1007 * We export these functions to other modules. They provide a
1008 * complete set of kernel non-blocking support for message
1009 * queueing.
1012 struct sock *
1013 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
1015 struct socket *sock;
1016 struct sock *sk;
1018 if (!nl_table)
1019 return NULL;
1021 if (unit<0 || unit>=MAX_LINKS)
1022 return NULL;
1024 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1025 return NULL;
1027 if (netlink_create(sock, unit) < 0) {
1028 sock_release(sock);
1029 return NULL;
1031 sk = sock->sk;
1032 sk->sk_data_ready = netlink_data_ready;
1033 if (input)
1034 nlk_sk(sk)->data_ready = input;
1036 if (netlink_insert(sk, 0)) {
1037 sock_release(sock);
1038 return NULL;
1040 return sk;
1043 void netlink_set_nonroot(int protocol, unsigned int flags)
1045 if ((unsigned int)protocol < MAX_LINKS)
1046 nl_table[protocol].nl_nonroot = flags;
1049 static void netlink_destroy_callback(struct netlink_callback *cb)
1051 if (cb->skb)
1052 kfree_skb(cb->skb);
1053 kfree(cb);
1057 * It looks a bit ugly.
1058 * It would be better to create kernel thread.
1061 static int netlink_dump(struct sock *sk)
1063 struct netlink_sock *nlk = nlk_sk(sk);
1064 struct netlink_callback *cb;
1065 struct sk_buff *skb;
1066 struct nlmsghdr *nlh;
1067 int len;
1069 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1070 if (!skb)
1071 return -ENOBUFS;
1073 spin_lock(&nlk->cb_lock);
1075 cb = nlk->cb;
1076 if (cb == NULL) {
1077 spin_unlock(&nlk->cb_lock);
1078 kfree_skb(skb);
1079 return -EINVAL;
1082 len = cb->dump(skb, cb);
1084 if (len > 0) {
1085 spin_unlock(&nlk->cb_lock);
1086 skb_queue_tail(&sk->sk_receive_queue, skb);
1087 sk->sk_data_ready(sk, len);
1088 return 0;
1091 nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
1092 nlh->nlmsg_flags |= NLM_F_MULTI;
1093 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
1094 skb_queue_tail(&sk->sk_receive_queue, skb);
1095 sk->sk_data_ready(sk, skb->len);
1097 cb->done(cb);
1098 nlk->cb = NULL;
1099 spin_unlock(&nlk->cb_lock);
1101 netlink_destroy_callback(cb);
1102 __sock_put(sk);
1103 return 0;
1106 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1107 struct nlmsghdr *nlh,
1108 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1109 int (*done)(struct netlink_callback*))
1111 struct netlink_callback *cb;
1112 struct sock *sk;
1113 struct netlink_sock *nlk;
1115 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1116 if (cb == NULL)
1117 return -ENOBUFS;
1119 memset(cb, 0, sizeof(*cb));
1120 cb->dump = dump;
1121 cb->done = done;
1122 cb->nlh = nlh;
1123 atomic_inc(&skb->users);
1124 cb->skb = skb;
1126 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1127 if (sk == NULL) {
1128 netlink_destroy_callback(cb);
1129 return -ECONNREFUSED;
1131 nlk = nlk_sk(sk);
1132 /* A dump is in progress... */
1133 spin_lock(&nlk->cb_lock);
1134 if (nlk->cb) {
1135 spin_unlock(&nlk->cb_lock);
1136 netlink_destroy_callback(cb);
1137 sock_put(sk);
1138 return -EBUSY;
1140 nlk->cb = cb;
1141 sock_hold(sk);
1142 spin_unlock(&nlk->cb_lock);
1144 netlink_dump(sk);
1145 sock_put(sk);
1146 return 0;
1149 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1151 struct sk_buff *skb;
1152 struct nlmsghdr *rep;
1153 struct nlmsgerr *errmsg;
1154 int size;
1156 if (err == 0)
1157 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
1158 else
1159 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
1161 skb = alloc_skb(size, GFP_KERNEL);
1162 if (!skb) {
1163 struct sock *sk;
1165 sk = netlink_lookup(in_skb->sk->sk_protocol,
1166 NETLINK_CB(in_skb).pid);
1167 if (sk) {
1168 sk->sk_err = ENOBUFS;
1169 sk->sk_error_report(sk);
1170 sock_put(sk);
1172 return;
1175 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
1176 NLMSG_ERROR, sizeof(struct nlmsgerr));
1177 errmsg = NLMSG_DATA(rep);
1178 errmsg->error = err;
1179 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
1180 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1184 #ifdef CONFIG_PROC_FS
1185 struct nl_seq_iter {
1186 int link;
1187 int hash_idx;
1190 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1192 struct nl_seq_iter *iter = seq->private;
1193 int i, j;
1194 struct sock *s;
1195 struct hlist_node *node;
1196 loff_t off = 0;
1198 for (i=0; i<MAX_LINKS; i++) {
1199 struct nl_pid_hash *hash = &nl_table[i].hash;
1201 for (j = 0; j <= hash->mask; j++) {
1202 sk_for_each(s, node, &hash->table[j]) {
1203 if (off == pos) {
1204 iter->link = i;
1205 iter->hash_idx = j;
1206 return s;
1208 ++off;
1212 return NULL;
1215 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1217 read_lock(&nl_table_lock);
1218 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1221 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1223 struct sock *s;
1224 struct nl_seq_iter *iter;
1225 int i, j;
1227 ++*pos;
1229 if (v == SEQ_START_TOKEN)
1230 return netlink_seq_socket_idx(seq, 0);
1232 s = sk_next(v);
1233 if (s)
1234 return s;
1236 iter = seq->private;
1237 i = iter->link;
1238 j = iter->hash_idx + 1;
1240 do {
1241 struct nl_pid_hash *hash = &nl_table[i].hash;
1243 for (; j <= hash->mask; j++) {
1244 s = sk_head(&hash->table[j]);
1245 if (s) {
1246 iter->link = i;
1247 iter->hash_idx = j;
1248 return s;
1252 j = 0;
1253 } while (++i < MAX_LINKS);
1255 return NULL;
1258 static void netlink_seq_stop(struct seq_file *seq, void *v)
1260 read_unlock(&nl_table_lock);
1264 static int netlink_seq_show(struct seq_file *seq, void *v)
1266 if (v == SEQ_START_TOKEN)
1267 seq_puts(seq,
1268 "sk Eth Pid Groups "
1269 "Rmem Wmem Dump Locks\n");
1270 else {
1271 struct sock *s = v;
1272 struct netlink_sock *nlk = nlk_sk(s);
1274 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1276 s->sk_protocol,
1277 nlk->pid,
1278 nlk->groups,
1279 atomic_read(&s->sk_rmem_alloc),
1280 atomic_read(&s->sk_wmem_alloc),
1281 nlk->cb,
1282 atomic_read(&s->sk_refcnt)
1286 return 0;
1289 static struct seq_operations netlink_seq_ops = {
1290 .start = netlink_seq_start,
1291 .next = netlink_seq_next,
1292 .stop = netlink_seq_stop,
1293 .show = netlink_seq_show,
1297 static int netlink_seq_open(struct inode *inode, struct file *file)
1299 struct seq_file *seq;
1300 struct nl_seq_iter *iter;
1301 int err;
1303 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1304 if (!iter)
1305 return -ENOMEM;
1307 err = seq_open(file, &netlink_seq_ops);
1308 if (err) {
1309 kfree(iter);
1310 return err;
1313 memset(iter, 0, sizeof(*iter));
1314 seq = file->private_data;
1315 seq->private = iter;
1316 return 0;
1319 static struct file_operations netlink_seq_fops = {
1320 .owner = THIS_MODULE,
1321 .open = netlink_seq_open,
1322 .read = seq_read,
1323 .llseek = seq_lseek,
1324 .release = seq_release_private,
1327 #endif
1329 int netlink_register_notifier(struct notifier_block *nb)
1331 return notifier_chain_register(&netlink_chain, nb);
1334 int netlink_unregister_notifier(struct notifier_block *nb)
1336 return notifier_chain_unregister(&netlink_chain, nb);
1339 static struct proto_ops netlink_ops = {
1340 .family = PF_NETLINK,
1341 .owner = THIS_MODULE,
1342 .release = netlink_release,
1343 .bind = netlink_bind,
1344 .connect = netlink_connect,
1345 .socketpair = sock_no_socketpair,
1346 .accept = sock_no_accept,
1347 .getname = netlink_getname,
1348 .poll = datagram_poll,
1349 .ioctl = sock_no_ioctl,
1350 .listen = sock_no_listen,
1351 .shutdown = sock_no_shutdown,
1352 .setsockopt = sock_no_setsockopt,
1353 .getsockopt = sock_no_getsockopt,
1354 .sendmsg = netlink_sendmsg,
1355 .recvmsg = netlink_recvmsg,
1356 .mmap = sock_no_mmap,
1357 .sendpage = sock_no_sendpage,
1360 static struct net_proto_family netlink_family_ops = {
1361 .family = PF_NETLINK,
1362 .create = netlink_create,
1363 .owner = THIS_MODULE, /* for consistency 8) */
1366 extern void netlink_skb_parms_too_large(void);
1368 static int __init netlink_proto_init(void)
1370 struct sk_buff *dummy_skb;
1371 int i;
1372 unsigned long max;
1373 unsigned int order;
1374 int err = proto_register(&netlink_proto, 0);
1376 if (err != 0)
1377 goto out;
1379 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
1380 netlink_skb_parms_too_large();
1382 nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
1383 if (!nl_table) {
1384 enomem:
1385 printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
1386 return -ENOMEM;
1389 memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
1391 if (num_physpages >= (128 * 1024))
1392 max = num_physpages >> (21 - PAGE_SHIFT);
1393 else
1394 max = num_physpages >> (23 - PAGE_SHIFT);
1396 order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1397 max = (1UL << order) / sizeof(struct hlist_head);
1398 order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1400 for (i = 0; i < MAX_LINKS; i++) {
1401 struct nl_pid_hash *hash = &nl_table[i].hash;
1403 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1404 if (!hash->table) {
1405 while (i-- > 0)
1406 nl_pid_hash_free(nl_table[i].hash.table,
1407 1 * sizeof(*hash->table));
1408 kfree(nl_table);
1409 goto enomem;
1411 memset(hash->table, 0, 1 * sizeof(*hash->table));
1412 hash->max_shift = order;
1413 hash->shift = 0;
1414 hash->mask = 0;
1415 hash->rehash_time = jiffies;
1418 sock_register(&netlink_family_ops);
1419 #ifdef CONFIG_PROC_FS
1420 proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1421 #endif
1422 /* The netlink device handler may be needed early. */
1423 rtnetlink_init();
1424 out:
1425 return err;
1428 static void __exit netlink_proto_exit(void)
1430 sock_unregister(PF_NETLINK);
1431 proc_net_remove("netlink");
1432 kfree(nl_table);
1433 nl_table = NULL;
1434 proto_unregister(&netlink_proto);
1437 core_initcall(netlink_proto_init);
1438 module_exit(netlink_proto_exit);
1440 MODULE_LICENSE("GPL");
1442 MODULE_ALIAS_NETPROTO(PF_NETLINK);
1444 EXPORT_SYMBOL(netlink_ack);
1445 EXPORT_SYMBOL(netlink_broadcast);
1446 EXPORT_SYMBOL(netlink_dump_start);
1447 EXPORT_SYMBOL(netlink_kernel_create);
1448 EXPORT_SYMBOL(netlink_register_notifier);
1449 EXPORT_SYMBOL(netlink_set_err);
1450 EXPORT_SYMBOL(netlink_set_nonroot);
1451 EXPORT_SYMBOL(netlink_unicast);
1452 EXPORT_SYMBOL(netlink_unregister_notifier);