blob: 47e791738014ad5d97177740ab51cc5eabf49b50 [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...)
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070063#define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65struct netlink_sock {
66 /* struct sock has to be the first member of netlink_sock */
67 struct sock sk;
68 u32 pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -070070 u32 dst_group;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070071 u32 flags;
72 u32 subscriptions;
73 u32 ngroups;
74 unsigned long *groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned long state;
76 wait_queue_head_t wait;
77 struct netlink_callback *cb;
78 spinlock_t cb_lock;
79 void (*data_ready)(struct sock *sk, int bytes);
Patrick McHardy77247bb2005-08-14 19:27:13 -070080 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
Patrick McHardy77247bb2005-08-14 19:27:13 -070083#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070084#define NETLINK_RECV_PKTINFO 0x2
Patrick McHardy77247bb2005-08-14 19:27:13 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static inline struct netlink_sock *nlk_sk(struct sock *sk)
87{
88 return (struct netlink_sock *)sk;
89}
90
91struct nl_pid_hash {
92 struct hlist_head *table;
93 unsigned long rehash_time;
94
95 unsigned int mask;
96 unsigned int shift;
97
98 unsigned int entries;
99 unsigned int max_shift;
100
101 u32 rnd;
102};
103
104struct netlink_table {
105 struct nl_pid_hash hash;
106 struct hlist_head mc_list;
107 unsigned int nl_nonroot;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700108 unsigned int groups;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700109 struct module *module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700110 int registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113static struct netlink_table *nl_table;
114
115static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
116
117static int netlink_dump(struct sock *sk);
118static void netlink_destroy_callback(struct netlink_callback *cb);
119
120static DEFINE_RWLOCK(nl_table_lock);
121static atomic_t nl_table_users = ATOMIC_INIT(0);
122
123static struct notifier_block *netlink_chain;
124
Patrick McHardyd629b832005-08-14 19:27:50 -0700125static u32 netlink_group_mask(u32 group)
126{
127 return group ? 1 << (group - 1) : 0;
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
131{
132 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
133}
134
135static void netlink_sock_destruct(struct sock *sk)
136{
137 skb_queue_purge(&sk->sk_receive_queue);
138
139 if (!sock_flag(sk, SOCK_DEAD)) {
140 printk("Freeing alive netlink socket %p\n", sk);
141 return;
142 }
143 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
144 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
145 BUG_TRAP(!nlk_sk(sk)->cb);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700146 BUG_TRAP(!nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
150 * Look, when several writers sleep and reader wakes them up, all but one
151 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
152 * this, _but_ remember, it adds useless work on UP machines.
153 */
154
155static void netlink_table_grab(void)
156{
157 write_lock_bh(&nl_table_lock);
158
159 if (atomic_read(&nl_table_users)) {
160 DECLARE_WAITQUEUE(wait, current);
161
162 add_wait_queue_exclusive(&nl_table_wait, &wait);
163 for(;;) {
164 set_current_state(TASK_UNINTERRUPTIBLE);
165 if (atomic_read(&nl_table_users) == 0)
166 break;
167 write_unlock_bh(&nl_table_lock);
168 schedule();
169 write_lock_bh(&nl_table_lock);
170 }
171
172 __set_current_state(TASK_RUNNING);
173 remove_wait_queue(&nl_table_wait, &wait);
174 }
175}
176
177static __inline__ void netlink_table_ungrab(void)
178{
179 write_unlock_bh(&nl_table_lock);
180 wake_up(&nl_table_wait);
181}
182
183static __inline__ void
184netlink_lock_table(void)
185{
186 /* read_lock() synchronizes us to netlink_table_grab */
187
188 read_lock(&nl_table_lock);
189 atomic_inc(&nl_table_users);
190 read_unlock(&nl_table_lock);
191}
192
193static __inline__ void
194netlink_unlock_table(void)
195{
196 if (atomic_dec_and_test(&nl_table_users))
197 wake_up(&nl_table_wait);
198}
199
200static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
201{
202 struct nl_pid_hash *hash = &nl_table[protocol].hash;
203 struct hlist_head *head;
204 struct sock *sk;
205 struct hlist_node *node;
206
207 read_lock(&nl_table_lock);
208 head = nl_pid_hashfn(hash, pid);
209 sk_for_each(sk, node, head) {
210 if (nlk_sk(sk)->pid == pid) {
211 sock_hold(sk);
212 goto found;
213 }
214 }
215 sk = NULL;
216found:
217 read_unlock(&nl_table_lock);
218 return sk;
219}
220
221static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
222{
223 if (size <= PAGE_SIZE)
224 return kmalloc(size, GFP_ATOMIC);
225 else
226 return (struct hlist_head *)
227 __get_free_pages(GFP_ATOMIC, get_order(size));
228}
229
230static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
231{
232 if (size <= PAGE_SIZE)
233 kfree(table);
234 else
235 free_pages((unsigned long)table, get_order(size));
236}
237
238static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
239{
240 unsigned int omask, mask, shift;
241 size_t osize, size;
242 struct hlist_head *otable, *table;
243 int i;
244
245 omask = mask = hash->mask;
246 osize = size = (mask + 1) * sizeof(*table);
247 shift = hash->shift;
248
249 if (grow) {
250 if (++shift > hash->max_shift)
251 return 0;
252 mask = mask * 2 + 1;
253 size *= 2;
254 }
255
256 table = nl_pid_hash_alloc(size);
257 if (!table)
258 return 0;
259
260 memset(table, 0, size);
261 otable = hash->table;
262 hash->table = table;
263 hash->mask = mask;
264 hash->shift = shift;
265 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
266
267 for (i = 0; i <= omask; i++) {
268 struct sock *sk;
269 struct hlist_node *node, *tmp;
270
271 sk_for_each_safe(sk, node, tmp, &otable[i])
272 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
273 }
274
275 nl_pid_hash_free(otable, osize);
276 hash->rehash_time = jiffies + 10 * 60 * HZ;
277 return 1;
278}
279
280static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
281{
282 int avg = hash->entries >> hash->shift;
283
284 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
285 return 1;
286
287 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
288 nl_pid_hash_rehash(hash, 0);
289 return 1;
290 }
291
292 return 0;
293}
294
295static struct proto_ops netlink_ops;
296
297static int netlink_insert(struct sock *sk, u32 pid)
298{
299 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
300 struct hlist_head *head;
301 int err = -EADDRINUSE;
302 struct sock *osk;
303 struct hlist_node *node;
304 int len;
305
306 netlink_table_grab();
307 head = nl_pid_hashfn(hash, pid);
308 len = 0;
309 sk_for_each(osk, node, head) {
310 if (nlk_sk(osk)->pid == pid)
311 break;
312 len++;
313 }
314 if (node)
315 goto err;
316
317 err = -EBUSY;
318 if (nlk_sk(sk)->pid)
319 goto err;
320
321 err = -ENOMEM;
322 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
323 goto err;
324
325 if (len && nl_pid_hash_dilute(hash, len))
326 head = nl_pid_hashfn(hash, pid);
327 hash->entries++;
328 nlk_sk(sk)->pid = pid;
329 sk_add_node(sk, head);
330 err = 0;
331
332err:
333 netlink_table_ungrab();
334 return err;
335}
336
337static void netlink_remove(struct sock *sk)
338{
339 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700340 if (sk_del_node_init(sk))
341 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700342 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 __sk_del_bind_node(sk);
344 netlink_table_ungrab();
345}
346
347static struct proto netlink_proto = {
348 .name = "NETLINK",
349 .owner = THIS_MODULE,
350 .obj_size = sizeof(struct netlink_sock),
351};
352
Patrick McHardyab33a172005-08-14 19:31:36 -0700353static int __netlink_create(struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sock *sk;
356 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700357
358 sock->ops = &netlink_ops;
359
360 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
361 if (!sk)
362 return -ENOMEM;
363
364 sock_init_data(sock, sk);
365
366 nlk = nlk_sk(sk);
367 spin_lock_init(&nlk->cb_lock);
368 init_waitqueue_head(&nlk->wait);
369
370 sk->sk_destruct = netlink_sock_destruct;
371 sk->sk_protocol = protocol;
372 return 0;
373}
374
375static int netlink_create(struct socket *sock, int protocol)
376{
377 struct module *module = NULL;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700378 struct netlink_sock *nlk;
379 unsigned int groups;
Patrick McHardyab33a172005-08-14 19:31:36 -0700380 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 sock->state = SS_UNCONNECTED;
383
384 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
385 return -ESOCKTNOSUPPORT;
386
387 if (protocol<0 || protocol >= MAX_LINKS)
388 return -EPROTONOSUPPORT;
389
Patrick McHardy77247bb2005-08-14 19:27:13 -0700390 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700391#ifdef CONFIG_KMOD
Patrick McHardyab33a172005-08-14 19:31:36 -0700392 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700393 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700394 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700395 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700396 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700397#endif
398 if (nl_table[protocol].registered &&
399 try_module_get(nl_table[protocol].module))
400 module = nl_table[protocol].module;
401 else
402 err = -EPROTONOSUPPORT;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700403 groups = nl_table[protocol].groups;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700404 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700405
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700406 if (err || (err = __netlink_create(sock, protocol) < 0))
Patrick McHardyab33a172005-08-14 19:31:36 -0700407 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700409 nlk = nlk_sk(sock->sk);
410
411 nlk->groups = kmalloc(NLGRPSZ(groups), GFP_KERNEL);
412 if (nlk->groups == NULL) {
413 err = -ENOMEM;
414 goto out_module;
415 }
416 memset(nlk->groups, 0, NLGRPSZ(groups));
417 nlk->ngroups = groups;
418
419 nlk->module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700420out:
421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Patrick McHardyab33a172005-08-14 19:31:36 -0700423out_module:
424 module_put(module);
425 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428static int netlink_release(struct socket *sock)
429{
430 struct sock *sk = sock->sk;
431 struct netlink_sock *nlk;
432
433 if (!sk)
434 return 0;
435
436 netlink_remove(sk);
437 nlk = nlk_sk(sk);
438
439 spin_lock(&nlk->cb_lock);
440 if (nlk->cb) {
441 nlk->cb->done(nlk->cb);
442 netlink_destroy_callback(nlk->cb);
443 nlk->cb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 spin_unlock(&nlk->cb_lock);
446
447 /* OK. Socket is unlinked, and, therefore,
448 no new packets will arrive */
449
450 sock_orphan(sk);
451 sock->sk = NULL;
452 wake_up_interruptible_all(&nlk->wait);
453
454 skb_queue_purge(&sk->sk_write_queue);
455
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700456 if (nlk->pid && !nlk->subscriptions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct netlink_notify n = {
458 .protocol = sk->sk_protocol,
459 .pid = nlk->pid,
460 };
461 notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
462 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700463
Patrick McHardy77247bb2005-08-14 19:27:13 -0700464 if (nlk->module)
465 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700466
Patrick McHardy77247bb2005-08-14 19:27:13 -0700467 if (nlk->flags & NETLINK_KERNEL_SOCKET) {
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700468 netlink_table_grab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700469 nl_table[sk->sk_protocol].module = NULL;
Patrick McHardyab33a172005-08-14 19:31:36 -0700470 nl_table[sk->sk_protocol].registered = 0;
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700471 netlink_table_ungrab();
472 }
Patrick McHardy77247bb2005-08-14 19:27:13 -0700473
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700474 kfree(nlk->groups);
475 nlk->groups = NULL;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 sock_put(sk);
478 return 0;
479}
480
481static int netlink_autobind(struct socket *sock)
482{
483 struct sock *sk = sock->sk;
484 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
485 struct hlist_head *head;
486 struct sock *osk;
487 struct hlist_node *node;
488 s32 pid = current->pid;
489 int err;
490 static s32 rover = -4097;
491
492retry:
493 cond_resched();
494 netlink_table_grab();
495 head = nl_pid_hashfn(hash, pid);
496 sk_for_each(osk, node, head) {
497 if (nlk_sk(osk)->pid == pid) {
498 /* Bind collision, search negative pid values. */
499 pid = rover--;
500 if (rover > -4097)
501 rover = -4097;
502 netlink_table_ungrab();
503 goto retry;
504 }
505 }
506 netlink_table_ungrab();
507
508 err = netlink_insert(sk, pid);
509 if (err == -EADDRINUSE)
510 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700511
512 /* If 2 threads race to autobind, that is fine. */
513 if (err == -EBUSY)
514 err = 0;
515
516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519static inline int netlink_capable(struct socket *sock, unsigned int flag)
520{
521 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
522 capable(CAP_NET_ADMIN);
523}
524
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700525static void
526netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
527{
528 struct netlink_sock *nlk = nlk_sk(sk);
529
530 if (nlk->subscriptions && !subscriptions)
531 __sk_del_bind_node(sk);
532 else if (!nlk->subscriptions && subscriptions)
533 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
534 nlk->subscriptions = subscriptions;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
538{
539 struct sock *sk = sock->sk;
540 struct netlink_sock *nlk = nlk_sk(sk);
541 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
542 int err;
543
544 if (nladdr->nl_family != AF_NETLINK)
545 return -EINVAL;
546
547 /* Only superuser is allowed to listen multicasts */
548 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
549 return -EPERM;
550
551 if (nlk->pid) {
552 if (nladdr->nl_pid != nlk->pid)
553 return -EINVAL;
554 } else {
555 err = nladdr->nl_pid ?
556 netlink_insert(sk, nladdr->nl_pid) :
557 netlink_autobind(sock);
558 if (err)
559 return err;
560 }
561
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700562 if (!nladdr->nl_groups && !(u32)nlk->groups[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564
565 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700566 netlink_update_subscriptions(sk, nlk->subscriptions +
567 hweight32(nladdr->nl_groups) -
568 hweight32(nlk->groups[0]));
569 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 netlink_table_ungrab();
571
572 return 0;
573}
574
575static int netlink_connect(struct socket *sock, struct sockaddr *addr,
576 int alen, int flags)
577{
578 int err = 0;
579 struct sock *sk = sock->sk;
580 struct netlink_sock *nlk = nlk_sk(sk);
581 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
582
583 if (addr->sa_family == AF_UNSPEC) {
584 sk->sk_state = NETLINK_UNCONNECTED;
585 nlk->dst_pid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700586 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
588 }
589 if (addr->sa_family != AF_NETLINK)
590 return -EINVAL;
591
592 /* Only superuser is allowed to send multicasts */
593 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
594 return -EPERM;
595
596 if (!nlk->pid)
597 err = netlink_autobind(sock);
598
599 if (err == 0) {
600 sk->sk_state = NETLINK_CONNECTED;
601 nlk->dst_pid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700602 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
605 return err;
606}
607
608static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
609{
610 struct sock *sk = sock->sk;
611 struct netlink_sock *nlk = nlk_sk(sk);
612 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
613
614 nladdr->nl_family = AF_NETLINK;
615 nladdr->nl_pad = 0;
616 *addr_len = sizeof(*nladdr);
617
618 if (peer) {
619 nladdr->nl_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700620 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 } else {
622 nladdr->nl_pid = nlk->pid;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700623 nladdr->nl_groups = nlk->groups[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 return 0;
626}
627
628static void netlink_overrun(struct sock *sk)
629{
630 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
631 sk->sk_err = ENOBUFS;
632 sk->sk_error_report(sk);
633 }
634}
635
636static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
637{
638 int protocol = ssk->sk_protocol;
639 struct sock *sock;
640 struct netlink_sock *nlk;
641
642 sock = netlink_lookup(protocol, pid);
643 if (!sock)
644 return ERR_PTR(-ECONNREFUSED);
645
646 /* Don't bother queuing skb if kernel socket has no input function */
647 nlk = nlk_sk(sock);
648 if ((nlk->pid == 0 && !nlk->data_ready) ||
649 (sock->sk_state == NETLINK_CONNECTED &&
650 nlk->dst_pid != nlk_sk(ssk)->pid)) {
651 sock_put(sock);
652 return ERR_PTR(-ECONNREFUSED);
653 }
654 return sock;
655}
656
657struct sock *netlink_getsockbyfilp(struct file *filp)
658{
659 struct inode *inode = filp->f_dentry->d_inode;
660 struct sock *sock;
661
662 if (!S_ISSOCK(inode->i_mode))
663 return ERR_PTR(-ENOTSOCK);
664
665 sock = SOCKET_I(inode)->sk;
666 if (sock->sk_family != AF_NETLINK)
667 return ERR_PTR(-EINVAL);
668
669 sock_hold(sock);
670 return sock;
671}
672
673/*
674 * Attach a skb to a netlink socket.
675 * The caller must hold a reference to the destination socket. On error, the
676 * reference is dropped. The skb is not send to the destination, just all
677 * all error checks are performed and memory in the queue is reserved.
678 * Return values:
679 * < 0: error. skb freed, reference to sock dropped.
680 * 0: continue
681 * 1: repeat lookup - reference dropped while waiting for socket memory.
682 */
683int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
684{
685 struct netlink_sock *nlk;
686
687 nlk = nlk_sk(sk);
688
689 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
690 test_bit(0, &nlk->state)) {
691 DECLARE_WAITQUEUE(wait, current);
692 if (!timeo) {
693 if (!nlk->pid)
694 netlink_overrun(sk);
695 sock_put(sk);
696 kfree_skb(skb);
697 return -EAGAIN;
698 }
699
700 __set_current_state(TASK_INTERRUPTIBLE);
701 add_wait_queue(&nlk->wait, &wait);
702
703 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
704 test_bit(0, &nlk->state)) &&
705 !sock_flag(sk, SOCK_DEAD))
706 timeo = schedule_timeout(timeo);
707
708 __set_current_state(TASK_RUNNING);
709 remove_wait_queue(&nlk->wait, &wait);
710 sock_put(sk);
711
712 if (signal_pending(current)) {
713 kfree_skb(skb);
714 return sock_intr_errno(timeo);
715 }
716 return 1;
717 }
718 skb_set_owner_r(skb, sk);
719 return 0;
720}
721
722int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
723{
724 struct netlink_sock *nlk;
725 int len = skb->len;
726
727 nlk = nlk_sk(sk);
728
729 skb_queue_tail(&sk->sk_receive_queue, skb);
730 sk->sk_data_ready(sk, len);
731 sock_put(sk);
732 return len;
733}
734
735void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
736{
737 kfree_skb(skb);
738 sock_put(sk);
739}
740
Victor Fusco37da6472005-07-18 13:35:43 -0700741static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
742 unsigned int __nocast allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 int delta;
745
746 skb_orphan(skb);
747
748 delta = skb->end - skb->tail;
749 if (delta * 2 < skb->truesize)
750 return skb;
751
752 if (skb_shared(skb)) {
753 struct sk_buff *nskb = skb_clone(skb, allocation);
754 if (!nskb)
755 return skb;
756 kfree_skb(skb);
757 skb = nskb;
758 }
759
760 if (!pskb_expand_head(skb, 0, -delta, allocation))
761 skb->truesize -= delta;
762
763 return skb;
764}
765
766int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
767{
768 struct sock *sk;
769 int err;
770 long timeo;
771
772 skb = netlink_trim(skb, gfp_any());
773
774 timeo = sock_sndtimeo(ssk, nonblock);
775retry:
776 sk = netlink_getsockbypid(ssk, pid);
777 if (IS_ERR(sk)) {
778 kfree_skb(skb);
779 return PTR_ERR(sk);
780 }
781 err = netlink_attachskb(sk, skb, nonblock, timeo);
782 if (err == 1)
783 goto retry;
784 if (err)
785 return err;
786
787 return netlink_sendskb(sk, skb, ssk->sk_protocol);
788}
789
790static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
791{
792 struct netlink_sock *nlk = nlk_sk(sk);
793
794 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
795 !test_bit(0, &nlk->state)) {
796 skb_set_owner_r(skb, sk);
797 skb_queue_tail(&sk->sk_receive_queue, skb);
798 sk->sk_data_ready(sk, skb->len);
799 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
800 }
801 return -1;
802}
803
804struct netlink_broadcast_data {
805 struct sock *exclude_sk;
806 u32 pid;
807 u32 group;
808 int failure;
809 int congested;
810 int delivered;
Victor Fusco37da6472005-07-18 13:35:43 -0700811 unsigned int allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct sk_buff *skb, *skb2;
813};
814
815static inline int do_one_broadcast(struct sock *sk,
816 struct netlink_broadcast_data *p)
817{
818 struct netlink_sock *nlk = nlk_sk(sk);
819 int val;
820
821 if (p->exclude_sk == sk)
822 goto out;
823
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700824 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
825 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto out;
827
828 if (p->failure) {
829 netlink_overrun(sk);
830 goto out;
831 }
832
833 sock_hold(sk);
834 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700835 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 p->skb2 = skb_clone(p->skb, p->allocation);
837 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700838 p->skb2 = skb_get(p->skb);
839 /*
840 * skb ownership may have been set when
841 * delivered to a previous socket.
842 */
843 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 }
846 if (p->skb2 == NULL) {
847 netlink_overrun(sk);
848 /* Clone failed. Notify ALL listeners. */
849 p->failure = 1;
850 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
851 netlink_overrun(sk);
852 } else {
853 p->congested |= val;
854 p->delivered = 1;
855 p->skb2 = NULL;
856 }
857 sock_put(sk);
858
859out:
860 return 0;
861}
862
863int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
864 u32 group, int allocation)
865{
866 struct netlink_broadcast_data info;
867 struct hlist_node *node;
868 struct sock *sk;
869
870 skb = netlink_trim(skb, allocation);
871
872 info.exclude_sk = ssk;
873 info.pid = pid;
874 info.group = group;
875 info.failure = 0;
876 info.congested = 0;
877 info.delivered = 0;
878 info.allocation = allocation;
879 info.skb = skb;
880 info.skb2 = NULL;
881
882 /* While we sleep in clone, do not allow to change socket list */
883
884 netlink_lock_table();
885
886 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
887 do_one_broadcast(sk, &info);
888
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -0700889 kfree_skb(skb);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 netlink_unlock_table();
892
893 if (info.skb2)
894 kfree_skb(info.skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (info.delivered) {
897 if (info.congested && (allocation & __GFP_WAIT))
898 yield();
899 return 0;
900 }
901 if (info.failure)
902 return -ENOBUFS;
903 return -ESRCH;
904}
905
906struct netlink_set_err_data {
907 struct sock *exclude_sk;
908 u32 pid;
909 u32 group;
910 int code;
911};
912
913static inline int do_one_set_err(struct sock *sk,
914 struct netlink_set_err_data *p)
915{
916 struct netlink_sock *nlk = nlk_sk(sk);
917
918 if (sk == p->exclude_sk)
919 goto out;
920
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700921 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
922 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto out;
924
925 sk->sk_err = p->code;
926 sk->sk_error_report(sk);
927out:
928 return 0;
929}
930
931void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
932{
933 struct netlink_set_err_data info;
934 struct hlist_node *node;
935 struct sock *sk;
936
937 info.exclude_sk = ssk;
938 info.pid = pid;
939 info.group = group;
940 info.code = code;
941
942 read_lock(&nl_table_lock);
943
944 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
945 do_one_set_err(sk, &info);
946
947 read_unlock(&nl_table_lock);
948}
949
Patrick McHardy9a4595b2005-08-15 12:32:15 -0700950static int netlink_setsockopt(struct socket *sock, int level, int optname,
951 char __user *optval, int optlen)
952{
953 struct sock *sk = sock->sk;
954 struct netlink_sock *nlk = nlk_sk(sk);
955 int val = 0, err;
956
957 if (level != SOL_NETLINK)
958 return -ENOPROTOOPT;
959
960 if (optlen >= sizeof(int) &&
961 get_user(val, (int __user *)optval))
962 return -EFAULT;
963
964 switch (optname) {
965 case NETLINK_PKTINFO:
966 if (val)
967 nlk->flags |= NETLINK_RECV_PKTINFO;
968 else
969 nlk->flags &= ~NETLINK_RECV_PKTINFO;
970 err = 0;
971 break;
972 case NETLINK_ADD_MEMBERSHIP:
973 case NETLINK_DROP_MEMBERSHIP: {
974 unsigned int subscriptions;
975 int old, new = optname == NETLINK_ADD_MEMBERSHIP ? 1 : 0;
976
977 if (!netlink_capable(sock, NL_NONROOT_RECV))
978 return -EPERM;
979 if (!val || val - 1 >= nlk->ngroups)
980 return -EINVAL;
981 netlink_table_grab();
982 old = test_bit(val - 1, nlk->groups);
983 subscriptions = nlk->subscriptions - old + new;
984 if (new)
985 __set_bit(val - 1, nlk->groups);
986 else
987 __clear_bit(val - 1, nlk->groups);
988 netlink_update_subscriptions(sk, subscriptions);
989 netlink_table_ungrab();
990 err = 0;
991 break;
992 }
993 default:
994 err = -ENOPROTOOPT;
995 }
996 return err;
997}
998
999static int netlink_getsockopt(struct socket *sock, int level, int optname,
1000 char __user *optval, int __user *optlen)
1001{
1002 struct sock *sk = sock->sk;
1003 struct netlink_sock *nlk = nlk_sk(sk);
1004 int len, val, err;
1005
1006 if (level != SOL_NETLINK)
1007 return -ENOPROTOOPT;
1008
1009 if (get_user(len, optlen))
1010 return -EFAULT;
1011 if (len < 0)
1012 return -EINVAL;
1013
1014 switch (optname) {
1015 case NETLINK_PKTINFO:
1016 if (len < sizeof(int))
1017 return -EINVAL;
1018 len = sizeof(int);
1019 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
1020 put_user(len, optlen);
1021 put_user(val, optval);
1022 err = 0;
1023 break;
1024 default:
1025 err = -ENOPROTOOPT;
1026 }
1027 return err;
1028}
1029
1030static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1031{
1032 struct nl_pktinfo info;
1033
1034 info.group = NETLINK_CB(skb).dst_group;
1035 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static inline void netlink_rcv_wake(struct sock *sk)
1039{
1040 struct netlink_sock *nlk = nlk_sk(sk);
1041
David S. Millerb03efcf2005-07-08 14:57:23 -07001042 if (skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 clear_bit(0, &nlk->state);
1044 if (!test_bit(0, &nlk->state))
1045 wake_up_interruptible(&nlk->wait);
1046}
1047
1048static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1049 struct msghdr *msg, size_t len)
1050{
1051 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1052 struct sock *sk = sock->sk;
1053 struct netlink_sock *nlk = nlk_sk(sk);
1054 struct sockaddr_nl *addr=msg->msg_name;
1055 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001056 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct sk_buff *skb;
1058 int err;
1059 struct scm_cookie scm;
1060
1061 if (msg->msg_flags&MSG_OOB)
1062 return -EOPNOTSUPP;
1063
1064 if (NULL == siocb->scm)
1065 siocb->scm = &scm;
1066 err = scm_send(sock, msg, siocb->scm);
1067 if (err < 0)
1068 return err;
1069
1070 if (msg->msg_namelen) {
1071 if (addr->nl_family != AF_NETLINK)
1072 return -EINVAL;
1073 dst_pid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001074 dst_group = ffs(addr->nl_groups);
1075 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return -EPERM;
1077 } else {
1078 dst_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001079 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082 if (!nlk->pid) {
1083 err = netlink_autobind(sock);
1084 if (err)
1085 goto out;
1086 }
1087
1088 err = -EMSGSIZE;
1089 if (len > sk->sk_sndbuf - 32)
1090 goto out;
1091 err = -ENOBUFS;
1092 skb = alloc_skb(len, GFP_KERNEL);
1093 if (skb==NULL)
1094 goto out;
1095
1096 NETLINK_CB(skb).pid = nlk->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 NETLINK_CB(skb).dst_pid = dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001098 NETLINK_CB(skb).dst_group = dst_group;
Serge Hallync94c2572005-04-29 16:27:17 +01001099 NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1101
1102 /* What can I do? Netlink is asynchronous, so that
1103 we will have to save current capabilities to
1104 check them, when this message will be delivered
1105 to corresponding kernel module. --ANK (980802)
1106 */
1107
1108 err = -EFAULT;
1109 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
1110 kfree_skb(skb);
1111 goto out;
1112 }
1113
1114 err = security_netlink_send(sk, skb);
1115 if (err) {
1116 kfree_skb(skb);
1117 goto out;
1118 }
1119
Patrick McHardyd629b832005-08-14 19:27:50 -07001120 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 atomic_inc(&skb->users);
Patrick McHardyd629b832005-08-14 19:27:50 -07001122 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1125
1126out:
1127 return err;
1128}
1129
1130static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1131 struct msghdr *msg, size_t len,
1132 int flags)
1133{
1134 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1135 struct scm_cookie scm;
1136 struct sock *sk = sock->sk;
1137 struct netlink_sock *nlk = nlk_sk(sk);
1138 int noblock = flags&MSG_DONTWAIT;
1139 size_t copied;
1140 struct sk_buff *skb;
1141 int err;
1142
1143 if (flags&MSG_OOB)
1144 return -EOPNOTSUPP;
1145
1146 copied = 0;
1147
1148 skb = skb_recv_datagram(sk,flags,noblock,&err);
1149 if (skb==NULL)
1150 goto out;
1151
1152 msg->msg_namelen = 0;
1153
1154 copied = skb->len;
1155 if (len < copied) {
1156 msg->msg_flags |= MSG_TRUNC;
1157 copied = len;
1158 }
1159
1160 skb->h.raw = skb->data;
1161 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1162
1163 if (msg->msg_name) {
1164 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
1165 addr->nl_family = AF_NETLINK;
1166 addr->nl_pad = 0;
1167 addr->nl_pid = NETLINK_CB(skb).pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001168 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 msg->msg_namelen = sizeof(*addr);
1170 }
1171
1172 if (NULL == siocb->scm) {
1173 memset(&scm, 0, sizeof(scm));
1174 siocb->scm = &scm;
1175 }
1176 siocb->scm->creds = *NETLINK_CREDS(skb);
1177 skb_free_datagram(sk, skb);
1178
1179 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1180 netlink_dump(sk);
1181
1182 scm_recv(sock, msg, siocb->scm, flags);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001183 if (nlk->flags & NETLINK_RECV_PKTINFO)
1184 netlink_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186out:
1187 netlink_rcv_wake(sk);
1188 return err ? : copied;
1189}
1190
1191static void netlink_data_ready(struct sock *sk, int len)
1192{
1193 struct netlink_sock *nlk = nlk_sk(sk);
1194
1195 if (nlk->data_ready)
1196 nlk->data_ready(sk, len);
1197 netlink_rcv_wake(sk);
1198}
1199
1200/*
1201 * We export these functions to other modules. They provide a
1202 * complete set of kernel non-blocking support for message
1203 * queueing.
1204 */
1205
1206struct sock *
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001207netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len), struct module *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct socket *sock;
1210 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001211 struct netlink_sock *nlk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (!nl_table)
1214 return NULL;
1215
1216 if (unit<0 || unit>=MAX_LINKS)
1217 return NULL;
1218
1219 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1220 return NULL;
1221
Patrick McHardyab33a172005-08-14 19:31:36 -07001222 if (__netlink_create(sock, unit) < 0)
Patrick McHardy77247bb2005-08-14 19:27:13 -07001223 goto out_sock_release;
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 sk = sock->sk;
1226 sk->sk_data_ready = netlink_data_ready;
1227 if (input)
1228 nlk_sk(sk)->data_ready = input;
1229
Patrick McHardy77247bb2005-08-14 19:27:13 -07001230 if (netlink_insert(sk, 0))
1231 goto out_sock_release;
1232
1233 nlk = nlk_sk(sk);
1234 nlk->flags |= NETLINK_KERNEL_SOCKET;
1235
1236 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001237 nl_table[unit].groups = 32;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001238 nl_table[unit].module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -07001239 nl_table[unit].registered = 1;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001240 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001241
1242 return sk;
1243
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001244out_sock_release:
1245 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001246 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249void netlink_set_nonroot(int protocol, unsigned int flags)
1250{
1251 if ((unsigned int)protocol < MAX_LINKS)
1252 nl_table[protocol].nl_nonroot = flags;
1253}
1254
1255static void netlink_destroy_callback(struct netlink_callback *cb)
1256{
1257 if (cb->skb)
1258 kfree_skb(cb->skb);
1259 kfree(cb);
1260}
1261
1262/*
1263 * It looks a bit ugly.
1264 * It would be better to create kernel thread.
1265 */
1266
1267static int netlink_dump(struct sock *sk)
1268{
1269 struct netlink_sock *nlk = nlk_sk(sk);
1270 struct netlink_callback *cb;
1271 struct sk_buff *skb;
1272 struct nlmsghdr *nlh;
1273 int len;
1274
1275 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1276 if (!skb)
1277 return -ENOBUFS;
1278
1279 spin_lock(&nlk->cb_lock);
1280
1281 cb = nlk->cb;
1282 if (cb == NULL) {
1283 spin_unlock(&nlk->cb_lock);
1284 kfree_skb(skb);
1285 return -EINVAL;
1286 }
1287
1288 len = cb->dump(skb, cb);
1289
1290 if (len > 0) {
1291 spin_unlock(&nlk->cb_lock);
1292 skb_queue_tail(&sk->sk_receive_queue, skb);
1293 sk->sk_data_ready(sk, len);
1294 return 0;
1295 }
1296
Thomas Graf17977542005-06-18 22:53:48 -07001297 nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
1299 skb_queue_tail(&sk->sk_receive_queue, skb);
1300 sk->sk_data_ready(sk, skb->len);
1301
1302 cb->done(cb);
1303 nlk->cb = NULL;
1304 spin_unlock(&nlk->cb_lock);
1305
1306 netlink_destroy_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001308
1309nlmsg_failure:
1310 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1314 struct nlmsghdr *nlh,
1315 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1316 int (*done)(struct netlink_callback*))
1317{
1318 struct netlink_callback *cb;
1319 struct sock *sk;
1320 struct netlink_sock *nlk;
1321
1322 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1323 if (cb == NULL)
1324 return -ENOBUFS;
1325
1326 memset(cb, 0, sizeof(*cb));
1327 cb->dump = dump;
1328 cb->done = done;
1329 cb->nlh = nlh;
1330 atomic_inc(&skb->users);
1331 cb->skb = skb;
1332
1333 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1334 if (sk == NULL) {
1335 netlink_destroy_callback(cb);
1336 return -ECONNREFUSED;
1337 }
1338 nlk = nlk_sk(sk);
1339 /* A dump is in progress... */
1340 spin_lock(&nlk->cb_lock);
1341 if (nlk->cb) {
1342 spin_unlock(&nlk->cb_lock);
1343 netlink_destroy_callback(cb);
1344 sock_put(sk);
1345 return -EBUSY;
1346 }
1347 nlk->cb = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_unlock(&nlk->cb_lock);
1349
1350 netlink_dump(sk);
1351 sock_put(sk);
1352 return 0;
1353}
1354
1355void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1356{
1357 struct sk_buff *skb;
1358 struct nlmsghdr *rep;
1359 struct nlmsgerr *errmsg;
1360 int size;
1361
1362 if (err == 0)
1363 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
1364 else
1365 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
1366
1367 skb = alloc_skb(size, GFP_KERNEL);
1368 if (!skb) {
1369 struct sock *sk;
1370
1371 sk = netlink_lookup(in_skb->sk->sk_protocol,
1372 NETLINK_CB(in_skb).pid);
1373 if (sk) {
1374 sk->sk_err = ENOBUFS;
1375 sk->sk_error_report(sk);
1376 sock_put(sk);
1377 }
1378 return;
1379 }
1380
1381 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
Thomas Graf17977542005-06-18 22:53:48 -07001382 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 errmsg = NLMSG_DATA(rep);
1384 errmsg->error = err;
1385 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
1386 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1387}
1388
1389
1390#ifdef CONFIG_PROC_FS
1391struct nl_seq_iter {
1392 int link;
1393 int hash_idx;
1394};
1395
1396static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1397{
1398 struct nl_seq_iter *iter = seq->private;
1399 int i, j;
1400 struct sock *s;
1401 struct hlist_node *node;
1402 loff_t off = 0;
1403
1404 for (i=0; i<MAX_LINKS; i++) {
1405 struct nl_pid_hash *hash = &nl_table[i].hash;
1406
1407 for (j = 0; j <= hash->mask; j++) {
1408 sk_for_each(s, node, &hash->table[j]) {
1409 if (off == pos) {
1410 iter->link = i;
1411 iter->hash_idx = j;
1412 return s;
1413 }
1414 ++off;
1415 }
1416 }
1417 }
1418 return NULL;
1419}
1420
1421static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1422{
1423 read_lock(&nl_table_lock);
1424 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1425}
1426
1427static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1428{
1429 struct sock *s;
1430 struct nl_seq_iter *iter;
1431 int i, j;
1432
1433 ++*pos;
1434
1435 if (v == SEQ_START_TOKEN)
1436 return netlink_seq_socket_idx(seq, 0);
1437
1438 s = sk_next(v);
1439 if (s)
1440 return s;
1441
1442 iter = seq->private;
1443 i = iter->link;
1444 j = iter->hash_idx + 1;
1445
1446 do {
1447 struct nl_pid_hash *hash = &nl_table[i].hash;
1448
1449 for (; j <= hash->mask; j++) {
1450 s = sk_head(&hash->table[j]);
1451 if (s) {
1452 iter->link = i;
1453 iter->hash_idx = j;
1454 return s;
1455 }
1456 }
1457
1458 j = 0;
1459 } while (++i < MAX_LINKS);
1460
1461 return NULL;
1462}
1463
1464static void netlink_seq_stop(struct seq_file *seq, void *v)
1465{
1466 read_unlock(&nl_table_lock);
1467}
1468
1469
1470static int netlink_seq_show(struct seq_file *seq, void *v)
1471{
1472 if (v == SEQ_START_TOKEN)
1473 seq_puts(seq,
1474 "sk Eth Pid Groups "
1475 "Rmem Wmem Dump Locks\n");
1476 else {
1477 struct sock *s = v;
1478 struct netlink_sock *nlk = nlk_sk(s);
1479
1480 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1481 s,
1482 s->sk_protocol,
1483 nlk->pid,
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001484 nlk->flags & NETLINK_KERNEL_SOCKET ?
1485 0 : (unsigned int)nlk->groups[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 atomic_read(&s->sk_rmem_alloc),
1487 atomic_read(&s->sk_wmem_alloc),
1488 nlk->cb,
1489 atomic_read(&s->sk_refcnt)
1490 );
1491
1492 }
1493 return 0;
1494}
1495
1496static struct seq_operations netlink_seq_ops = {
1497 .start = netlink_seq_start,
1498 .next = netlink_seq_next,
1499 .stop = netlink_seq_stop,
1500 .show = netlink_seq_show,
1501};
1502
1503
1504static int netlink_seq_open(struct inode *inode, struct file *file)
1505{
1506 struct seq_file *seq;
1507 struct nl_seq_iter *iter;
1508 int err;
1509
1510 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1511 if (!iter)
1512 return -ENOMEM;
1513
1514 err = seq_open(file, &netlink_seq_ops);
1515 if (err) {
1516 kfree(iter);
1517 return err;
1518 }
1519
1520 memset(iter, 0, sizeof(*iter));
1521 seq = file->private_data;
1522 seq->private = iter;
1523 return 0;
1524}
1525
1526static struct file_operations netlink_seq_fops = {
1527 .owner = THIS_MODULE,
1528 .open = netlink_seq_open,
1529 .read = seq_read,
1530 .llseek = seq_lseek,
1531 .release = seq_release_private,
1532};
1533
1534#endif
1535
1536int netlink_register_notifier(struct notifier_block *nb)
1537{
1538 return notifier_chain_register(&netlink_chain, nb);
1539}
1540
1541int netlink_unregister_notifier(struct notifier_block *nb)
1542{
1543 return notifier_chain_unregister(&netlink_chain, nb);
1544}
1545
1546static struct proto_ops netlink_ops = {
1547 .family = PF_NETLINK,
1548 .owner = THIS_MODULE,
1549 .release = netlink_release,
1550 .bind = netlink_bind,
1551 .connect = netlink_connect,
1552 .socketpair = sock_no_socketpair,
1553 .accept = sock_no_accept,
1554 .getname = netlink_getname,
1555 .poll = datagram_poll,
1556 .ioctl = sock_no_ioctl,
1557 .listen = sock_no_listen,
1558 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001559 .setsockopt = netlink_setsockopt,
1560 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 .sendmsg = netlink_sendmsg,
1562 .recvmsg = netlink_recvmsg,
1563 .mmap = sock_no_mmap,
1564 .sendpage = sock_no_sendpage,
1565};
1566
1567static struct net_proto_family netlink_family_ops = {
1568 .family = PF_NETLINK,
1569 .create = netlink_create,
1570 .owner = THIS_MODULE, /* for consistency 8) */
1571};
1572
1573extern void netlink_skb_parms_too_large(void);
1574
1575static int __init netlink_proto_init(void)
1576{
1577 struct sk_buff *dummy_skb;
1578 int i;
1579 unsigned long max;
1580 unsigned int order;
1581 int err = proto_register(&netlink_proto, 0);
1582
1583 if (err != 0)
1584 goto out;
1585
1586 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
1587 netlink_skb_parms_too_large();
1588
1589 nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
1590 if (!nl_table) {
1591enomem:
1592 printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
1593 return -ENOMEM;
1594 }
1595
1596 memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
1597
1598 if (num_physpages >= (128 * 1024))
1599 max = num_physpages >> (21 - PAGE_SHIFT);
1600 else
1601 max = num_physpages >> (23 - PAGE_SHIFT);
1602
1603 order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1604 max = (1UL << order) / sizeof(struct hlist_head);
1605 order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1606
1607 for (i = 0; i < MAX_LINKS; i++) {
1608 struct nl_pid_hash *hash = &nl_table[i].hash;
1609
1610 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1611 if (!hash->table) {
1612 while (i-- > 0)
1613 nl_pid_hash_free(nl_table[i].hash.table,
1614 1 * sizeof(*hash->table));
1615 kfree(nl_table);
1616 goto enomem;
1617 }
1618 memset(hash->table, 0, 1 * sizeof(*hash->table));
1619 hash->max_shift = order;
1620 hash->shift = 0;
1621 hash->mask = 0;
1622 hash->rehash_time = jiffies;
1623 }
1624
1625 sock_register(&netlink_family_ops);
1626#ifdef CONFIG_PROC_FS
1627 proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1628#endif
1629 /* The netlink device handler may be needed early. */
1630 rtnetlink_init();
1631out:
1632 return err;
1633}
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635core_initcall(netlink_proto_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637EXPORT_SYMBOL(netlink_ack);
1638EXPORT_SYMBOL(netlink_broadcast);
1639EXPORT_SYMBOL(netlink_dump_start);
1640EXPORT_SYMBOL(netlink_kernel_create);
1641EXPORT_SYMBOL(netlink_register_notifier);
1642EXPORT_SYMBOL(netlink_set_err);
1643EXPORT_SYMBOL(netlink_set_nonroot);
1644EXPORT_SYMBOL(netlink_unicast);
1645EXPORT_SYMBOL(netlink_unregister_notifier);
1646