blob: 5d487cd69c8c4918e1e3b19489f04a892316cff6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
4 * Authors: Alan Cox <alan@redhat.com>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 *
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.
11 *
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)
Harald Welte4fdb3bb2005-08-09 19:40:55 -070016 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
24#include <linux/config.h>
25#include <linux/module.h>
26
27#include <linux/kernel.h>
28#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
35#include <linux/un.h>
36#include <linux/fcntl.h>
37#include <linux/termios.h>
38#include <linux/sockios.h>
39#include <linux/net.h>
40#include <linux/fs.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/smp_lock.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>
55#include <linux/mm.h>
56#include <linux/types.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010057#include <linux/audit.h>
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/sock.h>
60#include <net/scm.h>
61
62#define Nprintk(a...)
63
64struct netlink_sock {
65 /* struct sock has to be the first member of netlink_sock */
66 struct sock sk;
67 u32 pid;
68 unsigned int groups;
69 u32 dst_pid;
70 unsigned int dst_groups;
71 unsigned long state;
72 wait_queue_head_t wait;
73 struct netlink_callback *cb;
74 spinlock_t cb_lock;
75 void (*data_ready)(struct sock *sk, int bytes);
76};
77
78static inline struct netlink_sock *nlk_sk(struct sock *sk)
79{
80 return (struct netlink_sock *)sk;
81}
82
83struct nl_pid_hash {
84 struct hlist_head *table;
85 unsigned long rehash_time;
86
87 unsigned int mask;
88 unsigned int shift;
89
90 unsigned int entries;
91 unsigned int max_shift;
92
93 u32 rnd;
94};
95
96struct netlink_table {
97 struct nl_pid_hash hash;
98 struct hlist_head mc_list;
99 unsigned int nl_nonroot;
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700100 struct proto_ops *p_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103static struct netlink_table *nl_table;
104
105static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
106
107static int netlink_dump(struct sock *sk);
108static void netlink_destroy_callback(struct netlink_callback *cb);
109
110static DEFINE_RWLOCK(nl_table_lock);
111static atomic_t nl_table_users = ATOMIC_INIT(0);
112
113static struct notifier_block *netlink_chain;
114
115static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
116{
117 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
118}
119
120static void netlink_sock_destruct(struct sock *sk)
121{
122 skb_queue_purge(&sk->sk_receive_queue);
123
124 if (!sock_flag(sk, SOCK_DEAD)) {
125 printk("Freeing alive netlink socket %p\n", sk);
126 return;
127 }
128 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
129 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
130 BUG_TRAP(!nlk_sk(sk)->cb);
131}
132
133/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
134 * Look, when several writers sleep and reader wakes them up, all but one
135 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
136 * this, _but_ remember, it adds useless work on UP machines.
137 */
138
139static void netlink_table_grab(void)
140{
141 write_lock_bh(&nl_table_lock);
142
143 if (atomic_read(&nl_table_users)) {
144 DECLARE_WAITQUEUE(wait, current);
145
146 add_wait_queue_exclusive(&nl_table_wait, &wait);
147 for(;;) {
148 set_current_state(TASK_UNINTERRUPTIBLE);
149 if (atomic_read(&nl_table_users) == 0)
150 break;
151 write_unlock_bh(&nl_table_lock);
152 schedule();
153 write_lock_bh(&nl_table_lock);
154 }
155
156 __set_current_state(TASK_RUNNING);
157 remove_wait_queue(&nl_table_wait, &wait);
158 }
159}
160
161static __inline__ void netlink_table_ungrab(void)
162{
163 write_unlock_bh(&nl_table_lock);
164 wake_up(&nl_table_wait);
165}
166
167static __inline__ void
168netlink_lock_table(void)
169{
170 /* read_lock() synchronizes us to netlink_table_grab */
171
172 read_lock(&nl_table_lock);
173 atomic_inc(&nl_table_users);
174 read_unlock(&nl_table_lock);
175}
176
177static __inline__ void
178netlink_unlock_table(void)
179{
180 if (atomic_dec_and_test(&nl_table_users))
181 wake_up(&nl_table_wait);
182}
183
184static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
185{
186 struct nl_pid_hash *hash = &nl_table[protocol].hash;
187 struct hlist_head *head;
188 struct sock *sk;
189 struct hlist_node *node;
190
191 read_lock(&nl_table_lock);
192 head = nl_pid_hashfn(hash, pid);
193 sk_for_each(sk, node, head) {
194 if (nlk_sk(sk)->pid == pid) {
195 sock_hold(sk);
196 goto found;
197 }
198 }
199 sk = NULL;
200found:
201 read_unlock(&nl_table_lock);
202 return sk;
203}
204
205static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
206{
207 if (size <= PAGE_SIZE)
208 return kmalloc(size, GFP_ATOMIC);
209 else
210 return (struct hlist_head *)
211 __get_free_pages(GFP_ATOMIC, get_order(size));
212}
213
214static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
215{
216 if (size <= PAGE_SIZE)
217 kfree(table);
218 else
219 free_pages((unsigned long)table, get_order(size));
220}
221
222static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
223{
224 unsigned int omask, mask, shift;
225 size_t osize, size;
226 struct hlist_head *otable, *table;
227 int i;
228
229 omask = mask = hash->mask;
230 osize = size = (mask + 1) * sizeof(*table);
231 shift = hash->shift;
232
233 if (grow) {
234 if (++shift > hash->max_shift)
235 return 0;
236 mask = mask * 2 + 1;
237 size *= 2;
238 }
239
240 table = nl_pid_hash_alloc(size);
241 if (!table)
242 return 0;
243
244 memset(table, 0, size);
245 otable = hash->table;
246 hash->table = table;
247 hash->mask = mask;
248 hash->shift = shift;
249 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
250
251 for (i = 0; i <= omask; i++) {
252 struct sock *sk;
253 struct hlist_node *node, *tmp;
254
255 sk_for_each_safe(sk, node, tmp, &otable[i])
256 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
257 }
258
259 nl_pid_hash_free(otable, osize);
260 hash->rehash_time = jiffies + 10 * 60 * HZ;
261 return 1;
262}
263
264static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
265{
266 int avg = hash->entries >> hash->shift;
267
268 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
269 return 1;
270
271 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
272 nl_pid_hash_rehash(hash, 0);
273 return 1;
274 }
275
276 return 0;
277}
278
279static struct proto_ops netlink_ops;
280
281static int netlink_insert(struct sock *sk, u32 pid)
282{
283 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
284 struct hlist_head *head;
285 int err = -EADDRINUSE;
286 struct sock *osk;
287 struct hlist_node *node;
288 int len;
289
290 netlink_table_grab();
291 head = nl_pid_hashfn(hash, pid);
292 len = 0;
293 sk_for_each(osk, node, head) {
294 if (nlk_sk(osk)->pid == pid)
295 break;
296 len++;
297 }
298 if (node)
299 goto err;
300
301 err = -EBUSY;
302 if (nlk_sk(sk)->pid)
303 goto err;
304
305 err = -ENOMEM;
306 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
307 goto err;
308
309 if (len && nl_pid_hash_dilute(hash, len))
310 head = nl_pid_hashfn(hash, pid);
311 hash->entries++;
312 nlk_sk(sk)->pid = pid;
313 sk_add_node(sk, head);
314 err = 0;
315
316err:
317 netlink_table_ungrab();
318 return err;
319}
320
321static void netlink_remove(struct sock *sk)
322{
323 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700324 if (sk_del_node_init(sk))
325 nl_table[sk->sk_protocol].hash.entries--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (nlk_sk(sk)->groups)
327 __sk_del_bind_node(sk);
328 netlink_table_ungrab();
329}
330
331static struct proto netlink_proto = {
332 .name = "NETLINK",
333 .owner = THIS_MODULE,
334 .obj_size = sizeof(struct netlink_sock),
335};
336
337static int netlink_create(struct socket *sock, int protocol)
338{
339 struct sock *sk;
340 struct netlink_sock *nlk;
341
342 sock->state = SS_UNCONNECTED;
343
344 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
345 return -ESOCKTNOSUPPORT;
346
347 if (protocol<0 || protocol >= MAX_LINKS)
348 return -EPROTONOSUPPORT;
349
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700350 netlink_table_grab();
351 if (!nl_table[protocol].hash.entries) {
352#ifdef CONFIG_KMOD
353 /* We do 'best effort'. If we find a matching module,
354 * it is loaded. If not, we don't return an error to
355 * allow pure userspace<->userspace communication. -HW
356 */
357 netlink_table_ungrab();
358 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
359 netlink_table_grab();
360#endif
361 }
362 netlink_table_ungrab();
363
364 sock->ops = nl_table[protocol].p_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
367 if (!sk)
368 return -ENOMEM;
369
370 sock_init_data(sock, sk);
371
372 nlk = nlk_sk(sk);
373
374 spin_lock_init(&nlk->cb_lock);
375 init_waitqueue_head(&nlk->wait);
376 sk->sk_destruct = netlink_sock_destruct;
377
378 sk->sk_protocol = protocol;
379 return 0;
380}
381
382static int netlink_release(struct socket *sock)
383{
384 struct sock *sk = sock->sk;
385 struct netlink_sock *nlk;
386
387 if (!sk)
388 return 0;
389
390 netlink_remove(sk);
391 nlk = nlk_sk(sk);
392
393 spin_lock(&nlk->cb_lock);
394 if (nlk->cb) {
395 nlk->cb->done(nlk->cb);
396 netlink_destroy_callback(nlk->cb);
397 nlk->cb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 spin_unlock(&nlk->cb_lock);
400
401 /* OK. Socket is unlinked, and, therefore,
402 no new packets will arrive */
403
404 sock_orphan(sk);
405 sock->sk = NULL;
406 wake_up_interruptible_all(&nlk->wait);
407
408 skb_queue_purge(&sk->sk_write_queue);
409
410 if (nlk->pid && !nlk->groups) {
411 struct netlink_notify n = {
412 .protocol = sk->sk_protocol,
413 .pid = nlk->pid,
414 };
415 notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
416 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700417
418 /* When this is a kernel socket, we need to remove the owner pointer,
419 * since we don't know whether the module will be dying at any given
420 * point - HW
421 */
422 if (!nlk->pid) {
423 struct proto_ops *p_tmp;
424
425 netlink_table_grab();
426 p_tmp = nl_table[sk->sk_protocol].p_ops;
427 if (p_tmp != &netlink_ops) {
428 nl_table[sk->sk_protocol].p_ops = &netlink_ops;
429 kfree(p_tmp);
430 }
431 netlink_table_ungrab();
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 sock_put(sk);
435 return 0;
436}
437
438static int netlink_autobind(struct socket *sock)
439{
440 struct sock *sk = sock->sk;
441 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
442 struct hlist_head *head;
443 struct sock *osk;
444 struct hlist_node *node;
445 s32 pid = current->pid;
446 int err;
447 static s32 rover = -4097;
448
449retry:
450 cond_resched();
451 netlink_table_grab();
452 head = nl_pid_hashfn(hash, pid);
453 sk_for_each(osk, node, head) {
454 if (nlk_sk(osk)->pid == pid) {
455 /* Bind collision, search negative pid values. */
456 pid = rover--;
457 if (rover > -4097)
458 rover = -4097;
459 netlink_table_ungrab();
460 goto retry;
461 }
462 }
463 netlink_table_ungrab();
464
465 err = netlink_insert(sk, pid);
466 if (err == -EADDRINUSE)
467 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700468
469 /* If 2 threads race to autobind, that is fine. */
470 if (err == -EBUSY)
471 err = 0;
472
473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static inline int netlink_capable(struct socket *sock, unsigned int flag)
477{
478 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
479 capable(CAP_NET_ADMIN);
480}
481
482static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
483{
484 struct sock *sk = sock->sk;
485 struct netlink_sock *nlk = nlk_sk(sk);
486 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
487 int err;
488
489 if (nladdr->nl_family != AF_NETLINK)
490 return -EINVAL;
491
492 /* Only superuser is allowed to listen multicasts */
493 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
494 return -EPERM;
495
496 if (nlk->pid) {
497 if (nladdr->nl_pid != nlk->pid)
498 return -EINVAL;
499 } else {
500 err = nladdr->nl_pid ?
501 netlink_insert(sk, nladdr->nl_pid) :
502 netlink_autobind(sock);
503 if (err)
504 return err;
505 }
506
507 if (!nladdr->nl_groups && !nlk->groups)
508 return 0;
509
510 netlink_table_grab();
511 if (nlk->groups && !nladdr->nl_groups)
512 __sk_del_bind_node(sk);
513 else if (!nlk->groups && nladdr->nl_groups)
514 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
515 nlk->groups = nladdr->nl_groups;
516 netlink_table_ungrab();
517
518 return 0;
519}
520
521static int netlink_connect(struct socket *sock, struct sockaddr *addr,
522 int alen, int flags)
523{
524 int err = 0;
525 struct sock *sk = sock->sk;
526 struct netlink_sock *nlk = nlk_sk(sk);
527 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
528
529 if (addr->sa_family == AF_UNSPEC) {
530 sk->sk_state = NETLINK_UNCONNECTED;
531 nlk->dst_pid = 0;
532 nlk->dst_groups = 0;
533 return 0;
534 }
535 if (addr->sa_family != AF_NETLINK)
536 return -EINVAL;
537
538 /* Only superuser is allowed to send multicasts */
539 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
540 return -EPERM;
541
542 if (!nlk->pid)
543 err = netlink_autobind(sock);
544
545 if (err == 0) {
546 sk->sk_state = NETLINK_CONNECTED;
547 nlk->dst_pid = nladdr->nl_pid;
548 nlk->dst_groups = nladdr->nl_groups;
549 }
550
551 return err;
552}
553
554static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
555{
556 struct sock *sk = sock->sk;
557 struct netlink_sock *nlk = nlk_sk(sk);
558 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
559
560 nladdr->nl_family = AF_NETLINK;
561 nladdr->nl_pad = 0;
562 *addr_len = sizeof(*nladdr);
563
564 if (peer) {
565 nladdr->nl_pid = nlk->dst_pid;
566 nladdr->nl_groups = nlk->dst_groups;
567 } else {
568 nladdr->nl_pid = nlk->pid;
569 nladdr->nl_groups = nlk->groups;
570 }
571 return 0;
572}
573
574static void netlink_overrun(struct sock *sk)
575{
576 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
577 sk->sk_err = ENOBUFS;
578 sk->sk_error_report(sk);
579 }
580}
581
582static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
583{
584 int protocol = ssk->sk_protocol;
585 struct sock *sock;
586 struct netlink_sock *nlk;
587
588 sock = netlink_lookup(protocol, pid);
589 if (!sock)
590 return ERR_PTR(-ECONNREFUSED);
591
592 /* Don't bother queuing skb if kernel socket has no input function */
593 nlk = nlk_sk(sock);
594 if ((nlk->pid == 0 && !nlk->data_ready) ||
595 (sock->sk_state == NETLINK_CONNECTED &&
596 nlk->dst_pid != nlk_sk(ssk)->pid)) {
597 sock_put(sock);
598 return ERR_PTR(-ECONNREFUSED);
599 }
600 return sock;
601}
602
603struct sock *netlink_getsockbyfilp(struct file *filp)
604{
605 struct inode *inode = filp->f_dentry->d_inode;
606 struct sock *sock;
607
608 if (!S_ISSOCK(inode->i_mode))
609 return ERR_PTR(-ENOTSOCK);
610
611 sock = SOCKET_I(inode)->sk;
612 if (sock->sk_family != AF_NETLINK)
613 return ERR_PTR(-EINVAL);
614
615 sock_hold(sock);
616 return sock;
617}
618
619/*
620 * Attach a skb to a netlink socket.
621 * The caller must hold a reference to the destination socket. On error, the
622 * reference is dropped. The skb is not send to the destination, just all
623 * all error checks are performed and memory in the queue is reserved.
624 * Return values:
625 * < 0: error. skb freed, reference to sock dropped.
626 * 0: continue
627 * 1: repeat lookup - reference dropped while waiting for socket memory.
628 */
629int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
630{
631 struct netlink_sock *nlk;
632
633 nlk = nlk_sk(sk);
634
635 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
636 test_bit(0, &nlk->state)) {
637 DECLARE_WAITQUEUE(wait, current);
638 if (!timeo) {
639 if (!nlk->pid)
640 netlink_overrun(sk);
641 sock_put(sk);
642 kfree_skb(skb);
643 return -EAGAIN;
644 }
645
646 __set_current_state(TASK_INTERRUPTIBLE);
647 add_wait_queue(&nlk->wait, &wait);
648
649 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
650 test_bit(0, &nlk->state)) &&
651 !sock_flag(sk, SOCK_DEAD))
652 timeo = schedule_timeout(timeo);
653
654 __set_current_state(TASK_RUNNING);
655 remove_wait_queue(&nlk->wait, &wait);
656 sock_put(sk);
657
658 if (signal_pending(current)) {
659 kfree_skb(skb);
660 return sock_intr_errno(timeo);
661 }
662 return 1;
663 }
664 skb_set_owner_r(skb, sk);
665 return 0;
666}
667
668int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
669{
670 struct netlink_sock *nlk;
671 int len = skb->len;
672
673 nlk = nlk_sk(sk);
674
675 skb_queue_tail(&sk->sk_receive_queue, skb);
676 sk->sk_data_ready(sk, len);
677 sock_put(sk);
678 return len;
679}
680
681void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
682{
683 kfree_skb(skb);
684 sock_put(sk);
685}
686
Victor Fusco37da6472005-07-18 13:35:43 -0700687static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
688 unsigned int __nocast allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 int delta;
691
692 skb_orphan(skb);
693
694 delta = skb->end - skb->tail;
695 if (delta * 2 < skb->truesize)
696 return skb;
697
698 if (skb_shared(skb)) {
699 struct sk_buff *nskb = skb_clone(skb, allocation);
700 if (!nskb)
701 return skb;
702 kfree_skb(skb);
703 skb = nskb;
704 }
705
706 if (!pskb_expand_head(skb, 0, -delta, allocation))
707 skb->truesize -= delta;
708
709 return skb;
710}
711
712int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
713{
714 struct sock *sk;
715 int err;
716 long timeo;
717
718 skb = netlink_trim(skb, gfp_any());
719
720 timeo = sock_sndtimeo(ssk, nonblock);
721retry:
722 sk = netlink_getsockbypid(ssk, pid);
723 if (IS_ERR(sk)) {
724 kfree_skb(skb);
725 return PTR_ERR(sk);
726 }
727 err = netlink_attachskb(sk, skb, nonblock, timeo);
728 if (err == 1)
729 goto retry;
730 if (err)
731 return err;
732
733 return netlink_sendskb(sk, skb, ssk->sk_protocol);
734}
735
736static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
737{
738 struct netlink_sock *nlk = nlk_sk(sk);
739
740 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
741 !test_bit(0, &nlk->state)) {
742 skb_set_owner_r(skb, sk);
743 skb_queue_tail(&sk->sk_receive_queue, skb);
744 sk->sk_data_ready(sk, skb->len);
745 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
746 }
747 return -1;
748}
749
750struct netlink_broadcast_data {
751 struct sock *exclude_sk;
752 u32 pid;
753 u32 group;
754 int failure;
755 int congested;
756 int delivered;
Victor Fusco37da6472005-07-18 13:35:43 -0700757 unsigned int allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct sk_buff *skb, *skb2;
759};
760
761static inline int do_one_broadcast(struct sock *sk,
762 struct netlink_broadcast_data *p)
763{
764 struct netlink_sock *nlk = nlk_sk(sk);
765 int val;
766
767 if (p->exclude_sk == sk)
768 goto out;
769
770 if (nlk->pid == p->pid || !(nlk->groups & p->group))
771 goto out;
772
773 if (p->failure) {
774 netlink_overrun(sk);
775 goto out;
776 }
777
778 sock_hold(sk);
779 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700780 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 p->skb2 = skb_clone(p->skb, p->allocation);
782 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700783 p->skb2 = skb_get(p->skb);
784 /*
785 * skb ownership may have been set when
786 * delivered to a previous socket.
787 */
788 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 }
791 if (p->skb2 == NULL) {
792 netlink_overrun(sk);
793 /* Clone failed. Notify ALL listeners. */
794 p->failure = 1;
795 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
796 netlink_overrun(sk);
797 } else {
798 p->congested |= val;
799 p->delivered = 1;
800 p->skb2 = NULL;
801 }
802 sock_put(sk);
803
804out:
805 return 0;
806}
807
808int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
809 u32 group, int allocation)
810{
811 struct netlink_broadcast_data info;
812 struct hlist_node *node;
813 struct sock *sk;
814
815 skb = netlink_trim(skb, allocation);
816
817 info.exclude_sk = ssk;
818 info.pid = pid;
819 info.group = group;
820 info.failure = 0;
821 info.congested = 0;
822 info.delivered = 0;
823 info.allocation = allocation;
824 info.skb = skb;
825 info.skb2 = NULL;
826
827 /* While we sleep in clone, do not allow to change socket list */
828
829 netlink_lock_table();
830
831 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
832 do_one_broadcast(sk, &info);
833
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -0700834 kfree_skb(skb);
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 netlink_unlock_table();
837
838 if (info.skb2)
839 kfree_skb(info.skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (info.delivered) {
842 if (info.congested && (allocation & __GFP_WAIT))
843 yield();
844 return 0;
845 }
846 if (info.failure)
847 return -ENOBUFS;
848 return -ESRCH;
849}
850
851struct netlink_set_err_data {
852 struct sock *exclude_sk;
853 u32 pid;
854 u32 group;
855 int code;
856};
857
858static inline int do_one_set_err(struct sock *sk,
859 struct netlink_set_err_data *p)
860{
861 struct netlink_sock *nlk = nlk_sk(sk);
862
863 if (sk == p->exclude_sk)
864 goto out;
865
866 if (nlk->pid == p->pid || !(nlk->groups & p->group))
867 goto out;
868
869 sk->sk_err = p->code;
870 sk->sk_error_report(sk);
871out:
872 return 0;
873}
874
875void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
876{
877 struct netlink_set_err_data info;
878 struct hlist_node *node;
879 struct sock *sk;
880
881 info.exclude_sk = ssk;
882 info.pid = pid;
883 info.group = group;
884 info.code = code;
885
886 read_lock(&nl_table_lock);
887
888 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
889 do_one_set_err(sk, &info);
890
891 read_unlock(&nl_table_lock);
892}
893
894static inline void netlink_rcv_wake(struct sock *sk)
895{
896 struct netlink_sock *nlk = nlk_sk(sk);
897
David S. Millerb03efcf2005-07-08 14:57:23 -0700898 if (skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 clear_bit(0, &nlk->state);
900 if (!test_bit(0, &nlk->state))
901 wake_up_interruptible(&nlk->wait);
902}
903
904static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
905 struct msghdr *msg, size_t len)
906{
907 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
908 struct sock *sk = sock->sk;
909 struct netlink_sock *nlk = nlk_sk(sk);
910 struct sockaddr_nl *addr=msg->msg_name;
911 u32 dst_pid;
912 u32 dst_groups;
913 struct sk_buff *skb;
914 int err;
915 struct scm_cookie scm;
916
917 if (msg->msg_flags&MSG_OOB)
918 return -EOPNOTSUPP;
919
920 if (NULL == siocb->scm)
921 siocb->scm = &scm;
922 err = scm_send(sock, msg, siocb->scm);
923 if (err < 0)
924 return err;
925
926 if (msg->msg_namelen) {
927 if (addr->nl_family != AF_NETLINK)
928 return -EINVAL;
929 dst_pid = addr->nl_pid;
930 dst_groups = addr->nl_groups;
931 if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
932 return -EPERM;
933 } else {
934 dst_pid = nlk->dst_pid;
935 dst_groups = nlk->dst_groups;
936 }
937
938 if (!nlk->pid) {
939 err = netlink_autobind(sock);
940 if (err)
941 goto out;
942 }
943
944 err = -EMSGSIZE;
945 if (len > sk->sk_sndbuf - 32)
946 goto out;
947 err = -ENOBUFS;
948 skb = alloc_skb(len, GFP_KERNEL);
949 if (skb==NULL)
950 goto out;
951
952 NETLINK_CB(skb).pid = nlk->pid;
953 NETLINK_CB(skb).groups = nlk->groups;
954 NETLINK_CB(skb).dst_pid = dst_pid;
955 NETLINK_CB(skb).dst_groups = dst_groups;
Serge Hallync94c2572005-04-29 16:27:17 +0100956 NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
958
959 /* What can I do? Netlink is asynchronous, so that
960 we will have to save current capabilities to
961 check them, when this message will be delivered
962 to corresponding kernel module. --ANK (980802)
963 */
964
965 err = -EFAULT;
966 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
967 kfree_skb(skb);
968 goto out;
969 }
970
971 err = security_netlink_send(sk, skb);
972 if (err) {
973 kfree_skb(skb);
974 goto out;
975 }
976
977 if (dst_groups) {
978 atomic_inc(&skb->users);
979 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
980 }
981 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
982
983out:
984 return err;
985}
986
987static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
988 struct msghdr *msg, size_t len,
989 int flags)
990{
991 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
992 struct scm_cookie scm;
993 struct sock *sk = sock->sk;
994 struct netlink_sock *nlk = nlk_sk(sk);
995 int noblock = flags&MSG_DONTWAIT;
996 size_t copied;
997 struct sk_buff *skb;
998 int err;
999
1000 if (flags&MSG_OOB)
1001 return -EOPNOTSUPP;
1002
1003 copied = 0;
1004
1005 skb = skb_recv_datagram(sk,flags,noblock,&err);
1006 if (skb==NULL)
1007 goto out;
1008
1009 msg->msg_namelen = 0;
1010
1011 copied = skb->len;
1012 if (len < copied) {
1013 msg->msg_flags |= MSG_TRUNC;
1014 copied = len;
1015 }
1016
1017 skb->h.raw = skb->data;
1018 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1019
1020 if (msg->msg_name) {
1021 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
1022 addr->nl_family = AF_NETLINK;
1023 addr->nl_pad = 0;
1024 addr->nl_pid = NETLINK_CB(skb).pid;
1025 addr->nl_groups = NETLINK_CB(skb).dst_groups;
1026 msg->msg_namelen = sizeof(*addr);
1027 }
1028
1029 if (NULL == siocb->scm) {
1030 memset(&scm, 0, sizeof(scm));
1031 siocb->scm = &scm;
1032 }
1033 siocb->scm->creds = *NETLINK_CREDS(skb);
1034 skb_free_datagram(sk, skb);
1035
1036 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1037 netlink_dump(sk);
1038
1039 scm_recv(sock, msg, siocb->scm, flags);
1040
1041out:
1042 netlink_rcv_wake(sk);
1043 return err ? : copied;
1044}
1045
1046static void netlink_data_ready(struct sock *sk, int len)
1047{
1048 struct netlink_sock *nlk = nlk_sk(sk);
1049
1050 if (nlk->data_ready)
1051 nlk->data_ready(sk, len);
1052 netlink_rcv_wake(sk);
1053}
1054
1055/*
1056 * We export these functions to other modules. They provide a
1057 * complete set of kernel non-blocking support for message
1058 * queueing.
1059 */
1060
1061struct sock *
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001062netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len), struct module *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001064 struct proto_ops *p_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct socket *sock;
1066 struct sock *sk;
1067
1068 if (!nl_table)
1069 return NULL;
1070
1071 if (unit<0 || unit>=MAX_LINKS)
1072 return NULL;
1073
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001074 /* Do a quick check, to make us not go down to netlink_insert()
1075 * if protocol already has kernel socket.
1076 */
1077 sk = netlink_lookup(unit, 0);
1078 if (unlikely(sk)) {
1079 sock_put(sk);
1080 return NULL;
1081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1084 return NULL;
1085
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001086 sk = NULL;
1087 if (module) {
1088 /* Every registering protocol implemented in a module needs
1089 * it's own p_ops, since the socket code cannot deal with
1090 * module refcounting otherwise. -HW
1091 */
1092 p_ops = kmalloc(sizeof(*p_ops), GFP_KERNEL);
1093 if (!p_ops)
1094 goto out_sock_release;
1095
1096 memcpy(p_ops, &netlink_ops, sizeof(*p_ops));
1097 p_ops->owner = module;
1098 } else
1099 p_ops = &netlink_ops;
1100
1101 netlink_table_grab();
1102 nl_table[unit].p_ops = p_ops;
1103 netlink_table_ungrab();
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (netlink_create(sock, unit) < 0) {
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001106 sk = NULL;
1107 goto out_kfree_p_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 sk = sock->sk;
1111 sk->sk_data_ready = netlink_data_ready;
1112 if (input)
1113 nlk_sk(sk)->data_ready = input;
1114
1115 if (netlink_insert(sk, 0)) {
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001116 sk = NULL;
1117 goto out_kfree_p_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001119
1120 return sk;
1121
1122out_kfree_p_ops:
1123 netlink_table_grab();
1124 if (nl_table[unit].p_ops != &netlink_ops) {
1125 kfree(nl_table[unit].p_ops);
1126 nl_table[unit].p_ops = &netlink_ops;
1127 }
1128 netlink_table_ungrab();
1129out_sock_release:
1130 sock_release(sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return sk;
1132}
1133
1134void netlink_set_nonroot(int protocol, unsigned int flags)
1135{
1136 if ((unsigned int)protocol < MAX_LINKS)
1137 nl_table[protocol].nl_nonroot = flags;
1138}
1139
1140static void netlink_destroy_callback(struct netlink_callback *cb)
1141{
1142 if (cb->skb)
1143 kfree_skb(cb->skb);
1144 kfree(cb);
1145}
1146
1147/*
1148 * It looks a bit ugly.
1149 * It would be better to create kernel thread.
1150 */
1151
1152static int netlink_dump(struct sock *sk)
1153{
1154 struct netlink_sock *nlk = nlk_sk(sk);
1155 struct netlink_callback *cb;
1156 struct sk_buff *skb;
1157 struct nlmsghdr *nlh;
1158 int len;
1159
1160 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1161 if (!skb)
1162 return -ENOBUFS;
1163
1164 spin_lock(&nlk->cb_lock);
1165
1166 cb = nlk->cb;
1167 if (cb == NULL) {
1168 spin_unlock(&nlk->cb_lock);
1169 kfree_skb(skb);
1170 return -EINVAL;
1171 }
1172
1173 len = cb->dump(skb, cb);
1174
1175 if (len > 0) {
1176 spin_unlock(&nlk->cb_lock);
1177 skb_queue_tail(&sk->sk_receive_queue, skb);
1178 sk->sk_data_ready(sk, len);
1179 return 0;
1180 }
1181
Thomas Graf17977542005-06-18 22:53:48 -07001182 nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
1184 skb_queue_tail(&sk->sk_receive_queue, skb);
1185 sk->sk_data_ready(sk, skb->len);
1186
1187 cb->done(cb);
1188 nlk->cb = NULL;
1189 spin_unlock(&nlk->cb_lock);
1190
1191 netlink_destroy_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001193
1194nlmsg_failure:
1195 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1199 struct nlmsghdr *nlh,
1200 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1201 int (*done)(struct netlink_callback*))
1202{
1203 struct netlink_callback *cb;
1204 struct sock *sk;
1205 struct netlink_sock *nlk;
1206
1207 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1208 if (cb == NULL)
1209 return -ENOBUFS;
1210
1211 memset(cb, 0, sizeof(*cb));
1212 cb->dump = dump;
1213 cb->done = done;
1214 cb->nlh = nlh;
1215 atomic_inc(&skb->users);
1216 cb->skb = skb;
1217
1218 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1219 if (sk == NULL) {
1220 netlink_destroy_callback(cb);
1221 return -ECONNREFUSED;
1222 }
1223 nlk = nlk_sk(sk);
1224 /* A dump is in progress... */
1225 spin_lock(&nlk->cb_lock);
1226 if (nlk->cb) {
1227 spin_unlock(&nlk->cb_lock);
1228 netlink_destroy_callback(cb);
1229 sock_put(sk);
1230 return -EBUSY;
1231 }
1232 nlk->cb = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 spin_unlock(&nlk->cb_lock);
1234
1235 netlink_dump(sk);
1236 sock_put(sk);
1237 return 0;
1238}
1239
1240void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1241{
1242 struct sk_buff *skb;
1243 struct nlmsghdr *rep;
1244 struct nlmsgerr *errmsg;
1245 int size;
1246
1247 if (err == 0)
1248 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
1249 else
1250 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
1251
1252 skb = alloc_skb(size, GFP_KERNEL);
1253 if (!skb) {
1254 struct sock *sk;
1255
1256 sk = netlink_lookup(in_skb->sk->sk_protocol,
1257 NETLINK_CB(in_skb).pid);
1258 if (sk) {
1259 sk->sk_err = ENOBUFS;
1260 sk->sk_error_report(sk);
1261 sock_put(sk);
1262 }
1263 return;
1264 }
1265
1266 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
Thomas Graf17977542005-06-18 22:53:48 -07001267 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 errmsg = NLMSG_DATA(rep);
1269 errmsg->error = err;
1270 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
1271 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1272}
1273
1274
1275#ifdef CONFIG_PROC_FS
1276struct nl_seq_iter {
1277 int link;
1278 int hash_idx;
1279};
1280
1281static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1282{
1283 struct nl_seq_iter *iter = seq->private;
1284 int i, j;
1285 struct sock *s;
1286 struct hlist_node *node;
1287 loff_t off = 0;
1288
1289 for (i=0; i<MAX_LINKS; i++) {
1290 struct nl_pid_hash *hash = &nl_table[i].hash;
1291
1292 for (j = 0; j <= hash->mask; j++) {
1293 sk_for_each(s, node, &hash->table[j]) {
1294 if (off == pos) {
1295 iter->link = i;
1296 iter->hash_idx = j;
1297 return s;
1298 }
1299 ++off;
1300 }
1301 }
1302 }
1303 return NULL;
1304}
1305
1306static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1307{
1308 read_lock(&nl_table_lock);
1309 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1310}
1311
1312static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1313{
1314 struct sock *s;
1315 struct nl_seq_iter *iter;
1316 int i, j;
1317
1318 ++*pos;
1319
1320 if (v == SEQ_START_TOKEN)
1321 return netlink_seq_socket_idx(seq, 0);
1322
1323 s = sk_next(v);
1324 if (s)
1325 return s;
1326
1327 iter = seq->private;
1328 i = iter->link;
1329 j = iter->hash_idx + 1;
1330
1331 do {
1332 struct nl_pid_hash *hash = &nl_table[i].hash;
1333
1334 for (; j <= hash->mask; j++) {
1335 s = sk_head(&hash->table[j]);
1336 if (s) {
1337 iter->link = i;
1338 iter->hash_idx = j;
1339 return s;
1340 }
1341 }
1342
1343 j = 0;
1344 } while (++i < MAX_LINKS);
1345
1346 return NULL;
1347}
1348
1349static void netlink_seq_stop(struct seq_file *seq, void *v)
1350{
1351 read_unlock(&nl_table_lock);
1352}
1353
1354
1355static int netlink_seq_show(struct seq_file *seq, void *v)
1356{
1357 if (v == SEQ_START_TOKEN)
1358 seq_puts(seq,
1359 "sk Eth Pid Groups "
1360 "Rmem Wmem Dump Locks\n");
1361 else {
1362 struct sock *s = v;
1363 struct netlink_sock *nlk = nlk_sk(s);
1364
1365 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1366 s,
1367 s->sk_protocol,
1368 nlk->pid,
1369 nlk->groups,
1370 atomic_read(&s->sk_rmem_alloc),
1371 atomic_read(&s->sk_wmem_alloc),
1372 nlk->cb,
1373 atomic_read(&s->sk_refcnt)
1374 );
1375
1376 }
1377 return 0;
1378}
1379
1380static struct seq_operations netlink_seq_ops = {
1381 .start = netlink_seq_start,
1382 .next = netlink_seq_next,
1383 .stop = netlink_seq_stop,
1384 .show = netlink_seq_show,
1385};
1386
1387
1388static int netlink_seq_open(struct inode *inode, struct file *file)
1389{
1390 struct seq_file *seq;
1391 struct nl_seq_iter *iter;
1392 int err;
1393
1394 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1395 if (!iter)
1396 return -ENOMEM;
1397
1398 err = seq_open(file, &netlink_seq_ops);
1399 if (err) {
1400 kfree(iter);
1401 return err;
1402 }
1403
1404 memset(iter, 0, sizeof(*iter));
1405 seq = file->private_data;
1406 seq->private = iter;
1407 return 0;
1408}
1409
1410static struct file_operations netlink_seq_fops = {
1411 .owner = THIS_MODULE,
1412 .open = netlink_seq_open,
1413 .read = seq_read,
1414 .llseek = seq_lseek,
1415 .release = seq_release_private,
1416};
1417
1418#endif
1419
1420int netlink_register_notifier(struct notifier_block *nb)
1421{
1422 return notifier_chain_register(&netlink_chain, nb);
1423}
1424
1425int netlink_unregister_notifier(struct notifier_block *nb)
1426{
1427 return notifier_chain_unregister(&netlink_chain, nb);
1428}
1429
1430static struct proto_ops netlink_ops = {
1431 .family = PF_NETLINK,
1432 .owner = THIS_MODULE,
1433 .release = netlink_release,
1434 .bind = netlink_bind,
1435 .connect = netlink_connect,
1436 .socketpair = sock_no_socketpair,
1437 .accept = sock_no_accept,
1438 .getname = netlink_getname,
1439 .poll = datagram_poll,
1440 .ioctl = sock_no_ioctl,
1441 .listen = sock_no_listen,
1442 .shutdown = sock_no_shutdown,
1443 .setsockopt = sock_no_setsockopt,
1444 .getsockopt = sock_no_getsockopt,
1445 .sendmsg = netlink_sendmsg,
1446 .recvmsg = netlink_recvmsg,
1447 .mmap = sock_no_mmap,
1448 .sendpage = sock_no_sendpage,
1449};
1450
1451static struct net_proto_family netlink_family_ops = {
1452 .family = PF_NETLINK,
1453 .create = netlink_create,
1454 .owner = THIS_MODULE, /* for consistency 8) */
1455};
1456
1457extern void netlink_skb_parms_too_large(void);
1458
1459static int __init netlink_proto_init(void)
1460{
1461 struct sk_buff *dummy_skb;
1462 int i;
1463 unsigned long max;
1464 unsigned int order;
1465 int err = proto_register(&netlink_proto, 0);
1466
1467 if (err != 0)
1468 goto out;
1469
1470 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
1471 netlink_skb_parms_too_large();
1472
1473 nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
1474 if (!nl_table) {
1475enomem:
1476 printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
1477 return -ENOMEM;
1478 }
1479
1480 memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
1481
1482 if (num_physpages >= (128 * 1024))
1483 max = num_physpages >> (21 - PAGE_SHIFT);
1484 else
1485 max = num_physpages >> (23 - PAGE_SHIFT);
1486
1487 order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1488 max = (1UL << order) / sizeof(struct hlist_head);
1489 order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1490
1491 for (i = 0; i < MAX_LINKS; i++) {
1492 struct nl_pid_hash *hash = &nl_table[i].hash;
1493
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001494 nl_table[i].p_ops = &netlink_ops;
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1497 if (!hash->table) {
1498 while (i-- > 0)
1499 nl_pid_hash_free(nl_table[i].hash.table,
1500 1 * sizeof(*hash->table));
1501 kfree(nl_table);
1502 goto enomem;
1503 }
1504 memset(hash->table, 0, 1 * sizeof(*hash->table));
1505 hash->max_shift = order;
1506 hash->shift = 0;
1507 hash->mask = 0;
1508 hash->rehash_time = jiffies;
1509 }
1510
1511 sock_register(&netlink_family_ops);
1512#ifdef CONFIG_PROC_FS
1513 proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1514#endif
1515 /* The netlink device handler may be needed early. */
1516 rtnetlink_init();
1517out:
1518 return err;
1519}
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521core_initcall(netlink_proto_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523EXPORT_SYMBOL(netlink_ack);
1524EXPORT_SYMBOL(netlink_broadcast);
1525EXPORT_SYMBOL(netlink_dump_start);
1526EXPORT_SYMBOL(netlink_kernel_create);
1527EXPORT_SYMBOL(netlink_register_notifier);
1528EXPORT_SYMBOL(netlink_set_err);
1529EXPORT_SYMBOL(netlink_set_nonroot);
1530EXPORT_SYMBOL(netlink_unicast);
1531EXPORT_SYMBOL(netlink_unregister_notifier);
1532