blob: 3982f13dab17954db40368b37d6cf37ea19e2c96 [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.
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25
Randy Dunlap4fc268d2006-01-11 12:17:47 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/notifier.h>
49#include <linux/security.h>
50#include <linux/jhash.h>
51#include <linux/jiffies.h>
52#include <linux/random.h>
53#include <linux/bitops.h>
54#include <linux/mm.h>
55#include <linux/types.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010056#include <linux/audit.h>
Steve Grubbe7c34972006-04-03 09:08:13 -040057#include <linux/selinux.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070058#include <linux/mutex.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010059
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020060#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <net/sock.h>
62#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010063#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070065#define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
Johannes Bergb4ff4f02007-07-18 15:46:06 -070066#define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68struct netlink_sock {
69 /* struct sock has to be the first member of netlink_sock */
70 struct sock sk;
71 u32 pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -070073 u32 dst_group;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070074 u32 flags;
75 u32 subscriptions;
76 u32 ngroups;
77 unsigned long *groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned long state;
79 wait_queue_head_t wait;
80 struct netlink_callback *cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070081 struct mutex *cb_mutex;
82 struct mutex cb_def_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 void (*data_ready)(struct sock *sk, int bytes);
Patrick McHardy77247bb2005-08-14 19:27:13 -070084 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Patrick McHardy77247bb2005-08-14 19:27:13 -070087#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070088#define NETLINK_RECV_PKTINFO 0x2
Patrick McHardy77247bb2005-08-14 19:27:13 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static inline struct netlink_sock *nlk_sk(struct sock *sk)
91{
Denis Cheng32b21e02007-08-28 15:41:11 -070092 return container_of(sk, struct netlink_sock, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95struct nl_pid_hash {
96 struct hlist_head *table;
97 unsigned long rehash_time;
98
99 unsigned int mask;
100 unsigned int shift;
101
102 unsigned int entries;
103 unsigned int max_shift;
104
105 u32 rnd;
106};
107
108struct netlink_table {
109 struct nl_pid_hash hash;
110 struct hlist_head mc_list;
Patrick McHardy4277a082006-03-20 18:52:01 -0800111 unsigned long *listeners;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned int nl_nonroot;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700113 unsigned int groups;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700114 struct mutex *cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700115 struct module *module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700116 int registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119static struct netlink_table *nl_table;
120
121static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
122
123static int netlink_dump(struct sock *sk);
124static void netlink_destroy_callback(struct netlink_callback *cb);
Adrian Bunk42bad1d2007-04-26 00:57:41 -0700125static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127static DEFINE_RWLOCK(nl_table_lock);
128static atomic_t nl_table_users = ATOMIC_INIT(0);
129
Alan Sterne041c682006-03-27 01:16:30 -0800130static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Patrick McHardyd629b832005-08-14 19:27:50 -0700132static u32 netlink_group_mask(u32 group)
133{
134 return group ? 1 << (group - 1) : 0;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
138{
139 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
140}
141
142static void netlink_sock_destruct(struct sock *sk)
143{
Herbert Xu3f660d62007-05-03 03:17:14 -0700144 struct netlink_sock *nlk = nlk_sk(sk);
145
Herbert Xu3f660d62007-05-03 03:17:14 -0700146 if (nlk->cb) {
147 if (nlk->cb->done)
148 nlk->cb->done(nlk->cb);
149 netlink_destroy_callback(nlk->cb);
150 }
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 skb_queue_purge(&sk->sk_receive_queue);
153
154 if (!sock_flag(sk, SOCK_DEAD)) {
155 printk("Freeing alive netlink socket %p\n", sk);
156 return;
157 }
158 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
159 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700160 BUG_TRAP(!nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
164 * Look, when several writers sleep and reader wakes them up, all but one
165 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
166 * this, _but_ remember, it adds useless work on UP machines.
167 */
168
169static void netlink_table_grab(void)
170{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700171 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (atomic_read(&nl_table_users)) {
174 DECLARE_WAITQUEUE(wait, current);
175
176 add_wait_queue_exclusive(&nl_table_wait, &wait);
177 for(;;) {
178 set_current_state(TASK_UNINTERRUPTIBLE);
179 if (atomic_read(&nl_table_users) == 0)
180 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700181 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700183 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185
186 __set_current_state(TASK_RUNNING);
187 remove_wait_queue(&nl_table_wait, &wait);
188 }
189}
190
191static __inline__ void netlink_table_ungrab(void)
192{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700193 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 wake_up(&nl_table_wait);
195}
196
197static __inline__ void
198netlink_lock_table(void)
199{
200 /* read_lock() synchronizes us to netlink_table_grab */
201
202 read_lock(&nl_table_lock);
203 atomic_inc(&nl_table_users);
204 read_unlock(&nl_table_lock);
205}
206
207static __inline__ void
208netlink_unlock_table(void)
209{
210 if (atomic_dec_and_test(&nl_table_users))
211 wake_up(&nl_table_wait);
212}
213
214static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
215{
216 struct nl_pid_hash *hash = &nl_table[protocol].hash;
217 struct hlist_head *head;
218 struct sock *sk;
219 struct hlist_node *node;
220
221 read_lock(&nl_table_lock);
222 head = nl_pid_hashfn(hash, pid);
223 sk_for_each(sk, node, head) {
224 if (nlk_sk(sk)->pid == pid) {
225 sock_hold(sk);
226 goto found;
227 }
228 }
229 sk = NULL;
230found:
231 read_unlock(&nl_table_lock);
232 return sk;
233}
234
235static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
236{
237 if (size <= PAGE_SIZE)
238 return kmalloc(size, GFP_ATOMIC);
239 else
240 return (struct hlist_head *)
241 __get_free_pages(GFP_ATOMIC, get_order(size));
242}
243
244static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
245{
246 if (size <= PAGE_SIZE)
247 kfree(table);
248 else
249 free_pages((unsigned long)table, get_order(size));
250}
251
252static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
253{
254 unsigned int omask, mask, shift;
255 size_t osize, size;
256 struct hlist_head *otable, *table;
257 int i;
258
259 omask = mask = hash->mask;
260 osize = size = (mask + 1) * sizeof(*table);
261 shift = hash->shift;
262
263 if (grow) {
264 if (++shift > hash->max_shift)
265 return 0;
266 mask = mask * 2 + 1;
267 size *= 2;
268 }
269
270 table = nl_pid_hash_alloc(size);
271 if (!table)
272 return 0;
273
274 memset(table, 0, size);
275 otable = hash->table;
276 hash->table = table;
277 hash->mask = mask;
278 hash->shift = shift;
279 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
280
281 for (i = 0; i <= omask; i++) {
282 struct sock *sk;
283 struct hlist_node *node, *tmp;
284
285 sk_for_each_safe(sk, node, tmp, &otable[i])
286 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
287 }
288
289 nl_pid_hash_free(otable, osize);
290 hash->rehash_time = jiffies + 10 * 60 * HZ;
291 return 1;
292}
293
294static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
295{
296 int avg = hash->entries >> hash->shift;
297
298 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
299 return 1;
300
301 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
302 nl_pid_hash_rehash(hash, 0);
303 return 1;
304 }
305
306 return 0;
307}
308
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800309static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Patrick McHardy4277a082006-03-20 18:52:01 -0800311static void
312netlink_update_listeners(struct sock *sk)
313{
314 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
315 struct hlist_node *node;
316 unsigned long mask;
317 unsigned int i;
318
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700319 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800320 mask = 0;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700321 sk_for_each_bound(sk, node, &tbl->mc_list) {
322 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
323 mask |= nlk_sk(sk)->groups[i];
324 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800325 tbl->listeners[i] = mask;
326 }
327 /* this function is only called with the netlink table "grabbed", which
328 * makes sure updates are visible before bind or setsockopt return. */
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static int netlink_insert(struct sock *sk, u32 pid)
332{
333 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
334 struct hlist_head *head;
335 int err = -EADDRINUSE;
336 struct sock *osk;
337 struct hlist_node *node;
338 int len;
339
340 netlink_table_grab();
341 head = nl_pid_hashfn(hash, pid);
342 len = 0;
343 sk_for_each(osk, node, head) {
344 if (nlk_sk(osk)->pid == pid)
345 break;
346 len++;
347 }
348 if (node)
349 goto err;
350
351 err = -EBUSY;
352 if (nlk_sk(sk)->pid)
353 goto err;
354
355 err = -ENOMEM;
356 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
357 goto err;
358
359 if (len && nl_pid_hash_dilute(hash, len))
360 head = nl_pid_hashfn(hash, pid);
361 hash->entries++;
362 nlk_sk(sk)->pid = pid;
363 sk_add_node(sk, head);
364 err = 0;
365
366err:
367 netlink_table_ungrab();
368 return err;
369}
370
371static void netlink_remove(struct sock *sk)
372{
373 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700374 if (sk_del_node_init(sk))
375 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700376 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 __sk_del_bind_node(sk);
378 netlink_table_ungrab();
379}
380
381static struct proto netlink_proto = {
382 .name = "NETLINK",
383 .owner = THIS_MODULE,
384 .obj_size = sizeof(struct netlink_sock),
385};
386
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700387static int __netlink_create(struct socket *sock, struct mutex *cb_mutex,
388 int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct sock *sk;
391 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700392
393 sock->ops = &netlink_ops;
394
395 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
396 if (!sk)
397 return -ENOMEM;
398
399 sock_init_data(sock, sk);
400
401 nlk = nlk_sk(sk);
Patrick McHardyffa4d722007-04-25 14:01:17 -0700402 if (cb_mutex)
403 nlk->cb_mutex = cb_mutex;
404 else {
405 nlk->cb_mutex = &nlk->cb_def_mutex;
406 mutex_init(nlk->cb_mutex);
407 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700408 init_waitqueue_head(&nlk->wait);
409
410 sk->sk_destruct = netlink_sock_destruct;
411 sk->sk_protocol = protocol;
412 return 0;
413}
414
415static int netlink_create(struct socket *sock, int protocol)
416{
417 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700418 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700419 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700420 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 sock->state = SS_UNCONNECTED;
423
424 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
425 return -ESOCKTNOSUPPORT;
426
427 if (protocol<0 || protocol >= MAX_LINKS)
428 return -EPROTONOSUPPORT;
429
Patrick McHardy77247bb2005-08-14 19:27:13 -0700430 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700431#ifdef CONFIG_KMOD
Patrick McHardyab33a172005-08-14 19:31:36 -0700432 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700433 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700434 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700435 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700436 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700437#endif
438 if (nl_table[protocol].registered &&
439 try_module_get(nl_table[protocol].module))
440 module = nl_table[protocol].module;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700441 cb_mutex = nl_table[protocol].cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700442 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700443
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700444 if ((err = __netlink_create(sock, cb_mutex, protocol)) < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700445 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700447 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700448 nlk->module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700449out:
450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Patrick McHardyab33a172005-08-14 19:31:36 -0700452out_module:
453 module_put(module);
454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static int netlink_release(struct socket *sock)
458{
459 struct sock *sk = sock->sk;
460 struct netlink_sock *nlk;
461
462 if (!sk)
463 return 0;
464
465 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700466 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 nlk = nlk_sk(sk);
468
Herbert Xu3f660d62007-05-03 03:17:14 -0700469 /*
470 * OK. Socket is unlinked, any packets that arrive now
471 * will be purged.
472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 sock->sk = NULL;
475 wake_up_interruptible_all(&nlk->wait);
476
477 skb_queue_purge(&sk->sk_write_queue);
478
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700479 if (nlk->pid && !nlk->subscriptions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct netlink_notify n = {
481 .protocol = sk->sk_protocol,
482 .pid = nlk->pid,
483 };
Alan Sterne041c682006-03-27 01:16:30 -0800484 atomic_notifier_call_chain(&netlink_chain,
485 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900486 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700487
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800488 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700489
Patrick McHardy4277a082006-03-20 18:52:01 -0800490 netlink_table_grab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700491 if (nlk->flags & NETLINK_KERNEL_SOCKET) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800492 kfree(nl_table[sk->sk_protocol].listeners);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700493 nl_table[sk->sk_protocol].module = NULL;
Patrick McHardyab33a172005-08-14 19:31:36 -0700494 nl_table[sk->sk_protocol].registered = 0;
Patrick McHardy4277a082006-03-20 18:52:01 -0800495 } else if (nlk->subscriptions)
496 netlink_update_listeners(sk);
497 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700498
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700499 kfree(nlk->groups);
500 nlk->groups = NULL;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 sock_put(sk);
503 return 0;
504}
505
506static int netlink_autobind(struct socket *sock)
507{
508 struct sock *sk = sock->sk;
509 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
510 struct hlist_head *head;
511 struct sock *osk;
512 struct hlist_node *node;
Herbert Xuc27bd492005-11-22 14:41:50 -0800513 s32 pid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int err;
515 static s32 rover = -4097;
516
517retry:
518 cond_resched();
519 netlink_table_grab();
520 head = nl_pid_hashfn(hash, pid);
521 sk_for_each(osk, node, head) {
522 if (nlk_sk(osk)->pid == pid) {
523 /* Bind collision, search negative pid values. */
524 pid = rover--;
525 if (rover > -4097)
526 rover = -4097;
527 netlink_table_ungrab();
528 goto retry;
529 }
530 }
531 netlink_table_ungrab();
532
533 err = netlink_insert(sk, pid);
534 if (err == -EADDRINUSE)
535 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700536
537 /* If 2 threads race to autobind, that is fine. */
538 if (err == -EBUSY)
539 err = 0;
540
541 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900544static inline int netlink_capable(struct socket *sock, unsigned int flag)
545{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
547 capable(CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900548}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700550static void
551netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
552{
553 struct netlink_sock *nlk = nlk_sk(sk);
554
555 if (nlk->subscriptions && !subscriptions)
556 __sk_del_bind_node(sk);
557 else if (!nlk->subscriptions && subscriptions)
558 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
559 nlk->subscriptions = subscriptions;
560}
561
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700562static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700563{
564 struct netlink_sock *nlk = nlk_sk(sk);
565 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700566 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700567 int err = 0;
568
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700569 netlink_table_grab();
570
Patrick McHardy513c2502005-09-06 15:43:59 -0700571 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700572 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700573 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700574 goto out_unlock;
575 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700576
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700577 if (nlk->ngroups >= groups)
578 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700579
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700580 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
581 if (new_groups == NULL) {
582 err = -ENOMEM;
583 goto out_unlock;
584 }
585 memset((char*)new_groups + NLGRPSZ(nlk->ngroups), 0,
586 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
587
588 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700589 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700590 out_unlock:
591 netlink_table_ungrab();
592 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
596{
597 struct sock *sk = sock->sk;
598 struct netlink_sock *nlk = nlk_sk(sk);
599 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
600 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (nladdr->nl_family != AF_NETLINK)
603 return -EINVAL;
604
605 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700606 if (nladdr->nl_groups) {
607 if (!netlink_capable(sock, NL_NONROOT_RECV))
608 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700609 err = netlink_realloc_groups(sk);
610 if (err)
611 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (nlk->pid) {
615 if (nladdr->nl_pid != nlk->pid)
616 return -EINVAL;
617 } else {
618 err = nladdr->nl_pid ?
619 netlink_insert(sk, nladdr->nl_pid) :
620 netlink_autobind(sock);
621 if (err)
622 return err;
623 }
624
Patrick McHardy513c2502005-09-06 15:43:59 -0700625 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627
628 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700629 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900630 hweight32(nladdr->nl_groups) -
631 hweight32(nlk->groups[0]));
632 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800633 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 netlink_table_ungrab();
635
636 return 0;
637}
638
639static int netlink_connect(struct socket *sock, struct sockaddr *addr,
640 int alen, int flags)
641{
642 int err = 0;
643 struct sock *sk = sock->sk;
644 struct netlink_sock *nlk = nlk_sk(sk);
645 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
646
647 if (addr->sa_family == AF_UNSPEC) {
648 sk->sk_state = NETLINK_UNCONNECTED;
649 nlk->dst_pid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700650 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652 }
653 if (addr->sa_family != AF_NETLINK)
654 return -EINVAL;
655
656 /* Only superuser is allowed to send multicasts */
657 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
658 return -EPERM;
659
660 if (!nlk->pid)
661 err = netlink_autobind(sock);
662
663 if (err == 0) {
664 sk->sk_state = NETLINK_CONNECTED;
665 nlk->dst_pid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700666 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
669 return err;
670}
671
672static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
673{
674 struct sock *sk = sock->sk;
675 struct netlink_sock *nlk = nlk_sk(sk);
676 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 nladdr->nl_family = AF_NETLINK;
679 nladdr->nl_pad = 0;
680 *addr_len = sizeof(*nladdr);
681
682 if (peer) {
683 nladdr->nl_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700684 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 } else {
686 nladdr->nl_pid = nlk->pid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700687 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689 return 0;
690}
691
692static void netlink_overrun(struct sock *sk)
693{
694 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
695 sk->sk_err = ENOBUFS;
696 sk->sk_error_report(sk);
697 }
698}
699
700static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
701{
702 int protocol = ssk->sk_protocol;
703 struct sock *sock;
704 struct netlink_sock *nlk;
705
706 sock = netlink_lookup(protocol, pid);
707 if (!sock)
708 return ERR_PTR(-ECONNREFUSED);
709
710 /* Don't bother queuing skb if kernel socket has no input function */
711 nlk = nlk_sk(sock);
712 if ((nlk->pid == 0 && !nlk->data_ready) ||
713 (sock->sk_state == NETLINK_CONNECTED &&
714 nlk->dst_pid != nlk_sk(ssk)->pid)) {
715 sock_put(sock);
716 return ERR_PTR(-ECONNREFUSED);
717 }
718 return sock;
719}
720
721struct sock *netlink_getsockbyfilp(struct file *filp)
722{
Josef Sipek6db5fc52006-12-08 02:37:25 -0800723 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct sock *sock;
725
726 if (!S_ISSOCK(inode->i_mode))
727 return ERR_PTR(-ENOTSOCK);
728
729 sock = SOCKET_I(inode)->sk;
730 if (sock->sk_family != AF_NETLINK)
731 return ERR_PTR(-EINVAL);
732
733 sock_hold(sock);
734 return sock;
735}
736
737/*
738 * Attach a skb to a netlink socket.
739 * The caller must hold a reference to the destination socket. On error, the
740 * reference is dropped. The skb is not send to the destination, just all
741 * all error checks are performed and memory in the queue is reserved.
742 * Return values:
743 * < 0: error. skb freed, reference to sock dropped.
744 * 0: continue
745 * 1: repeat lookup - reference dropped while waiting for socket memory.
746 */
Alexey Kuznetsova70ea992006-02-09 16:40:11 -0800747int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock,
748 long timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct netlink_sock *nlk;
751
752 nlk = nlk_sk(sk);
753
754 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
755 test_bit(0, &nlk->state)) {
756 DECLARE_WAITQUEUE(wait, current);
757 if (!timeo) {
Alexey Kuznetsova70ea992006-02-09 16:40:11 -0800758 if (!ssk || nlk_sk(ssk)->pid == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 netlink_overrun(sk);
760 sock_put(sk);
761 kfree_skb(skb);
762 return -EAGAIN;
763 }
764
765 __set_current_state(TASK_INTERRUPTIBLE);
766 add_wait_queue(&nlk->wait, &wait);
767
768 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
769 test_bit(0, &nlk->state)) &&
770 !sock_flag(sk, SOCK_DEAD))
771 timeo = schedule_timeout(timeo);
772
773 __set_current_state(TASK_RUNNING);
774 remove_wait_queue(&nlk->wait, &wait);
775 sock_put(sk);
776
777 if (signal_pending(current)) {
778 kfree_skb(skb);
779 return sock_intr_errno(timeo);
780 }
781 return 1;
782 }
783 skb_set_owner_r(skb, sk);
784 return 0;
785}
786
787int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
788{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int len = skb->len;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 skb_queue_tail(&sk->sk_receive_queue, skb);
792 sk->sk_data_ready(sk, len);
793 sock_put(sk);
794 return len;
795}
796
797void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
798{
799 kfree_skb(skb);
800 sock_put(sk);
801}
802
Victor Fusco37da6472005-07-18 13:35:43 -0700803static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +0100804 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 int delta;
807
808 skb_orphan(skb);
809
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700810 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (delta * 2 < skb->truesize)
812 return skb;
813
814 if (skb_shared(skb)) {
815 struct sk_buff *nskb = skb_clone(skb, allocation);
816 if (!nskb)
817 return skb;
818 kfree_skb(skb);
819 skb = nskb;
820 }
821
822 if (!pskb_expand_head(skb, 0, -delta, allocation))
823 skb->truesize -= delta;
824
825 return skb;
826}
827
828int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
829{
830 struct sock *sk;
831 int err;
832 long timeo;
833
834 skb = netlink_trim(skb, gfp_any());
835
836 timeo = sock_sndtimeo(ssk, nonblock);
837retry:
838 sk = netlink_getsockbypid(ssk, pid);
839 if (IS_ERR(sk)) {
840 kfree_skb(skb);
841 return PTR_ERR(sk);
842 }
Alexey Kuznetsova70ea992006-02-09 16:40:11 -0800843 err = netlink_attachskb(sk, skb, nonblock, timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (err == 1)
845 goto retry;
846 if (err)
847 return err;
848
849 return netlink_sendskb(sk, skb, ssk->sk_protocol);
850}
851
Patrick McHardy4277a082006-03-20 18:52:01 -0800852int netlink_has_listeners(struct sock *sk, unsigned int group)
853{
854 int res = 0;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700855 unsigned long *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -0800856
857 BUG_ON(!(nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET));
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700858
859 rcu_read_lock();
860 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
861
Patrick McHardy4277a082006-03-20 18:52:01 -0800862 if (group - 1 < nl_table[sk->sk_protocol].groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700863 res = test_bit(group - 1, listeners);
864
865 rcu_read_unlock();
866
Patrick McHardy4277a082006-03-20 18:52:01 -0800867 return res;
868}
869EXPORT_SYMBOL_GPL(netlink_has_listeners);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
872{
873 struct netlink_sock *nlk = nlk_sk(sk);
874
875 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
876 !test_bit(0, &nlk->state)) {
877 skb_set_owner_r(skb, sk);
878 skb_queue_tail(&sk->sk_receive_queue, skb);
879 sk->sk_data_ready(sk, skb->len);
880 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
881 }
882 return -1;
883}
884
885struct netlink_broadcast_data {
886 struct sock *exclude_sk;
887 u32 pid;
888 u32 group;
889 int failure;
890 int congested;
891 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -0400892 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct sk_buff *skb, *skb2;
894};
895
896static inline int do_one_broadcast(struct sock *sk,
897 struct netlink_broadcast_data *p)
898{
899 struct netlink_sock *nlk = nlk_sk(sk);
900 int val;
901
902 if (p->exclude_sk == sk)
903 goto out;
904
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700905 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
906 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto out;
908
909 if (p->failure) {
910 netlink_overrun(sk);
911 goto out;
912 }
913
914 sock_hold(sk);
915 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700916 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 p->skb2 = skb_clone(p->skb, p->allocation);
918 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700919 p->skb2 = skb_get(p->skb);
920 /*
921 * skb ownership may have been set when
922 * delivered to a previous socket.
923 */
924 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 }
927 if (p->skb2 == NULL) {
928 netlink_overrun(sk);
929 /* Clone failed. Notify ALL listeners. */
930 p->failure = 1;
931 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
932 netlink_overrun(sk);
933 } else {
934 p->congested |= val;
935 p->delivered = 1;
936 p->skb2 = NULL;
937 }
938 sock_put(sk);
939
940out:
941 return 0;
942}
943
944int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
Al Virodd0fc662005-10-07 07:46:04 +0100945 u32 group, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct netlink_broadcast_data info;
948 struct hlist_node *node;
949 struct sock *sk;
950
951 skb = netlink_trim(skb, allocation);
952
953 info.exclude_sk = ssk;
954 info.pid = pid;
955 info.group = group;
956 info.failure = 0;
957 info.congested = 0;
958 info.delivered = 0;
959 info.allocation = allocation;
960 info.skb = skb;
961 info.skb2 = NULL;
962
963 /* While we sleep in clone, do not allow to change socket list */
964
965 netlink_lock_table();
966
967 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
968 do_one_broadcast(sk, &info);
969
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -0700970 kfree_skb(skb);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 netlink_unlock_table();
973
974 if (info.skb2)
975 kfree_skb(info.skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (info.delivered) {
978 if (info.congested && (allocation & __GFP_WAIT))
979 yield();
980 return 0;
981 }
982 if (info.failure)
983 return -ENOBUFS;
984 return -ESRCH;
985}
986
987struct netlink_set_err_data {
988 struct sock *exclude_sk;
989 u32 pid;
990 u32 group;
991 int code;
992};
993
994static inline int do_one_set_err(struct sock *sk,
995 struct netlink_set_err_data *p)
996{
997 struct netlink_sock *nlk = nlk_sk(sk);
998
999 if (sk == p->exclude_sk)
1000 goto out;
1001
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001002 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1003 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 goto out;
1005
1006 sk->sk_err = p->code;
1007 sk->sk_error_report(sk);
1008out:
1009 return 0;
1010}
1011
1012void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
1013{
1014 struct netlink_set_err_data info;
1015 struct hlist_node *node;
1016 struct sock *sk;
1017
1018 info.exclude_sk = ssk;
1019 info.pid = pid;
1020 info.group = group;
1021 info.code = code;
1022
1023 read_lock(&nl_table_lock);
1024
1025 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1026 do_one_set_err(sk, &info);
1027
1028 read_unlock(&nl_table_lock);
1029}
1030
Johannes Berg84659eb2007-07-18 15:47:05 -07001031/* must be called with netlink table grabbed */
1032static void netlink_update_socket_mc(struct netlink_sock *nlk,
1033 unsigned int group,
1034 int is_new)
1035{
1036 int old, new = !!is_new, subscriptions;
1037
1038 old = test_bit(group - 1, nlk->groups);
1039 subscriptions = nlk->subscriptions - old + new;
1040 if (new)
1041 __set_bit(group - 1, nlk->groups);
1042 else
1043 __clear_bit(group - 1, nlk->groups);
1044 netlink_update_subscriptions(&nlk->sk, subscriptions);
1045 netlink_update_listeners(&nlk->sk);
1046}
1047
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001048static int netlink_setsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001049 char __user *optval, int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001050{
1051 struct sock *sk = sock->sk;
1052 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001053 unsigned int val = 0;
1054 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001055
1056 if (level != SOL_NETLINK)
1057 return -ENOPROTOOPT;
1058
1059 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001060 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001061 return -EFAULT;
1062
1063 switch (optname) {
1064 case NETLINK_PKTINFO:
1065 if (val)
1066 nlk->flags |= NETLINK_RECV_PKTINFO;
1067 else
1068 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1069 err = 0;
1070 break;
1071 case NETLINK_ADD_MEMBERSHIP:
1072 case NETLINK_DROP_MEMBERSHIP: {
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001073 if (!netlink_capable(sock, NL_NONROOT_RECV))
1074 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001075 err = netlink_realloc_groups(sk);
1076 if (err)
1077 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001078 if (!val || val - 1 >= nlk->ngroups)
1079 return -EINVAL;
1080 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001081 netlink_update_socket_mc(nlk, val,
1082 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001083 netlink_table_ungrab();
1084 err = 0;
1085 break;
1086 }
1087 default:
1088 err = -ENOPROTOOPT;
1089 }
1090 return err;
1091}
1092
1093static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001094 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001095{
1096 struct sock *sk = sock->sk;
1097 struct netlink_sock *nlk = nlk_sk(sk);
1098 int len, val, err;
1099
1100 if (level != SOL_NETLINK)
1101 return -ENOPROTOOPT;
1102
1103 if (get_user(len, optlen))
1104 return -EFAULT;
1105 if (len < 0)
1106 return -EINVAL;
1107
1108 switch (optname) {
1109 case NETLINK_PKTINFO:
1110 if (len < sizeof(int))
1111 return -EINVAL;
1112 len = sizeof(int);
1113 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001114 if (put_user(len, optlen) ||
1115 put_user(val, optval))
1116 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001117 err = 0;
1118 break;
1119 default:
1120 err = -ENOPROTOOPT;
1121 }
1122 return err;
1123}
1124
1125static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1126{
1127 struct nl_pktinfo info;
1128
1129 info.group = NETLINK_CB(skb).dst_group;
1130 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133static inline void netlink_rcv_wake(struct sock *sk)
1134{
1135 struct netlink_sock *nlk = nlk_sk(sk);
1136
David S. Millerb03efcf2005-07-08 14:57:23 -07001137 if (skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 clear_bit(0, &nlk->state);
1139 if (!test_bit(0, &nlk->state))
1140 wake_up_interruptible(&nlk->wait);
1141}
1142
1143static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1144 struct msghdr *msg, size_t len)
1145{
1146 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1147 struct sock *sk = sock->sk;
1148 struct netlink_sock *nlk = nlk_sk(sk);
1149 struct sockaddr_nl *addr=msg->msg_name;
1150 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001151 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct sk_buff *skb;
1153 int err;
1154 struct scm_cookie scm;
1155
1156 if (msg->msg_flags&MSG_OOB)
1157 return -EOPNOTSUPP;
1158
1159 if (NULL == siocb->scm)
1160 siocb->scm = &scm;
1161 err = scm_send(sock, msg, siocb->scm);
1162 if (err < 0)
1163 return err;
1164
1165 if (msg->msg_namelen) {
1166 if (addr->nl_family != AF_NETLINK)
1167 return -EINVAL;
1168 dst_pid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001169 dst_group = ffs(addr->nl_groups);
1170 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return -EPERM;
1172 } else {
1173 dst_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001174 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
1177 if (!nlk->pid) {
1178 err = netlink_autobind(sock);
1179 if (err)
1180 goto out;
1181 }
1182
1183 err = -EMSGSIZE;
1184 if (len > sk->sk_sndbuf - 32)
1185 goto out;
1186 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001187 skb = alloc_skb(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (skb==NULL)
1189 goto out;
1190
1191 NETLINK_CB(skb).pid = nlk->pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001192 NETLINK_CB(skb).dst_group = dst_group;
Serge Hallync94c2572005-04-29 16:27:17 +01001193 NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
Steve Grubbe7c34972006-04-03 09:08:13 -04001194 selinux_get_task_sid(current, &(NETLINK_CB(skb).sid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1196
1197 /* What can I do? Netlink is asynchronous, so that
1198 we will have to save current capabilities to
1199 check them, when this message will be delivered
1200 to corresponding kernel module. --ANK (980802)
1201 */
1202
1203 err = -EFAULT;
1204 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
1205 kfree_skb(skb);
1206 goto out;
1207 }
1208
1209 err = security_netlink_send(sk, skb);
1210 if (err) {
1211 kfree_skb(skb);
1212 goto out;
1213 }
1214
Patrick McHardyd629b832005-08-14 19:27:50 -07001215 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 atomic_inc(&skb->users);
Patrick McHardyd629b832005-08-14 19:27:50 -07001217 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1220
1221out:
1222 return err;
1223}
1224
1225static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1226 struct msghdr *msg, size_t len,
1227 int flags)
1228{
1229 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1230 struct scm_cookie scm;
1231 struct sock *sk = sock->sk;
1232 struct netlink_sock *nlk = nlk_sk(sk);
1233 int noblock = flags&MSG_DONTWAIT;
1234 size_t copied;
1235 struct sk_buff *skb;
1236 int err;
1237
1238 if (flags&MSG_OOB)
1239 return -EOPNOTSUPP;
1240
1241 copied = 0;
1242
1243 skb = skb_recv_datagram(sk,flags,noblock,&err);
1244 if (skb==NULL)
1245 goto out;
1246
1247 msg->msg_namelen = 0;
1248
1249 copied = skb->len;
1250 if (len < copied) {
1251 msg->msg_flags |= MSG_TRUNC;
1252 copied = len;
1253 }
1254
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001255 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1257
1258 if (msg->msg_name) {
1259 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
1260 addr->nl_family = AF_NETLINK;
1261 addr->nl_pad = 0;
1262 addr->nl_pid = NETLINK_CB(skb).pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001263 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 msg->msg_namelen = sizeof(*addr);
1265 }
1266
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001267 if (nlk->flags & NETLINK_RECV_PKTINFO)
1268 netlink_cmsg_recv_pktinfo(msg, skb);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (NULL == siocb->scm) {
1271 memset(&scm, 0, sizeof(scm));
1272 siocb->scm = &scm;
1273 }
1274 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001275 if (flags & MSG_TRUNC)
1276 copied = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 skb_free_datagram(sk, skb);
1278
1279 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1280 netlink_dump(sk);
1281
1282 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283out:
1284 netlink_rcv_wake(sk);
1285 return err ? : copied;
1286}
1287
1288static void netlink_data_ready(struct sock *sk, int len)
1289{
1290 struct netlink_sock *nlk = nlk_sk(sk);
1291
1292 if (nlk->data_ready)
1293 nlk->data_ready(sk, len);
1294 netlink_rcv_wake(sk);
1295}
1296
1297/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001298 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 * complete set of kernel non-blocking support for message
1300 * queueing.
1301 */
1302
1303struct sock *
Patrick McHardy06628602005-08-15 12:33:26 -07001304netlink_kernel_create(int unit, unsigned int groups,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001305 void (*input)(struct sock *sk, int len),
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001306 struct mutex *cb_mutex, struct module *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct socket *sock;
1309 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001310 struct netlink_sock *nlk;
Patrick McHardy4277a082006-03-20 18:52:01 -08001311 unsigned long *listeners = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001313 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (unit<0 || unit>=MAX_LINKS)
1316 return NULL;
1317
1318 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1319 return NULL;
1320
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001321 if (__netlink_create(sock, cb_mutex, unit) < 0)
Patrick McHardy77247bb2005-08-14 19:27:13 -07001322 goto out_sock_release;
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001323
Patrick McHardy4277a082006-03-20 18:52:01 -08001324 if (groups < 32)
1325 groups = 32;
1326
1327 listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
1328 if (!listeners)
1329 goto out_sock_release;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 sk = sock->sk;
1332 sk->sk_data_ready = netlink_data_ready;
1333 if (input)
1334 nlk_sk(sk)->data_ready = input;
1335
Patrick McHardy77247bb2005-08-14 19:27:13 -07001336 if (netlink_insert(sk, 0))
1337 goto out_sock_release;
1338
1339 nlk = nlk_sk(sk);
1340 nlk->flags |= NETLINK_KERNEL_SOCKET;
1341
1342 netlink_table_grab();
Patrick McHardy4277a082006-03-20 18:52:01 -08001343 nl_table[unit].groups = groups;
1344 nl_table[unit].listeners = listeners;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001345 nl_table[unit].cb_mutex = cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001346 nl_table[unit].module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -07001347 nl_table[unit].registered = 1;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001348 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001349
1350 return sk;
1351
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001352out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001353 kfree(listeners);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001354 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001355 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001358/**
1359 * netlink_change_ngroups - change number of multicast groups
1360 *
1361 * This changes the number of multicast groups that are available
1362 * on a certain netlink family. Note that it is not possible to
Johannes Berg84659eb2007-07-18 15:47:05 -07001363 * change the number of groups to below 32. Also note that it does
1364 * not implicitly call netlink_clear_multicast_users() when the
1365 * number of groups is reduced.
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001366 *
1367 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1368 * @groups: The new number of groups.
1369 */
1370int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1371{
1372 unsigned long *listeners, *old = NULL;
1373 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1374 int err = 0;
1375
1376 if (groups < 32)
1377 groups = 32;
1378
1379 netlink_table_grab();
1380 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1381 listeners = kzalloc(NLGRPSZ(groups), GFP_ATOMIC);
1382 if (!listeners) {
1383 err = -ENOMEM;
1384 goto out_ungrab;
1385 }
1386 old = tbl->listeners;
1387 memcpy(listeners, old, NLGRPSZ(tbl->groups));
1388 rcu_assign_pointer(tbl->listeners, listeners);
1389 }
1390 tbl->groups = groups;
1391
1392 out_ungrab:
1393 netlink_table_ungrab();
1394 synchronize_rcu();
1395 kfree(old);
1396 return err;
1397}
1398EXPORT_SYMBOL(netlink_change_ngroups);
1399
Johannes Berg84659eb2007-07-18 15:47:05 -07001400/**
1401 * netlink_clear_multicast_users - kick off multicast listeners
1402 *
1403 * This function removes all listeners from the given group.
1404 * @ksk: The kernel netlink socket, as returned by
1405 * netlink_kernel_create().
1406 * @group: The multicast group to clear.
1407 */
1408void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1409{
1410 struct sock *sk;
1411 struct hlist_node *node;
1412 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1413
1414 netlink_table_grab();
1415
1416 sk_for_each_bound(sk, node, &tbl->mc_list)
1417 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1418
1419 netlink_table_ungrab();
1420}
1421EXPORT_SYMBOL(netlink_clear_multicast_users);
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423void netlink_set_nonroot(int protocol, unsigned int flags)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001424{
1425 if ((unsigned int)protocol < MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 nl_table[protocol].nl_nonroot = flags;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001427}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429static void netlink_destroy_callback(struct netlink_callback *cb)
1430{
1431 if (cb->skb)
1432 kfree_skb(cb->skb);
1433 kfree(cb);
1434}
1435
1436/*
1437 * It looks a bit ugly.
1438 * It would be better to create kernel thread.
1439 */
1440
1441static int netlink_dump(struct sock *sk)
1442{
1443 struct netlink_sock *nlk = nlk_sk(sk);
1444 struct netlink_callback *cb;
1445 struct sk_buff *skb;
1446 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001447 int len, err = -ENOBUFS;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1450 if (!skb)
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001451 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001453 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 cb = nlk->cb;
1456 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001457 err = -EINVAL;
1458 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
1461 len = cb->dump(skb, cb);
1462
1463 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001464 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 skb_queue_tail(&sk->sk_receive_queue, skb);
1466 sk->sk_data_ready(sk, len);
1467 return 0;
1468 }
1469
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001470 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1471 if (!nlh)
1472 goto errout_skb;
1473
1474 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 skb_queue_tail(&sk->sk_receive_queue, skb);
1477 sk->sk_data_ready(sk, skb->len);
1478
Thomas Grafa8f74b22005-11-10 02:25:52 +01001479 if (cb->done)
1480 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001482 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 netlink_destroy_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001486
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001487errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001488 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001489 kfree_skb(skb);
1490errout:
1491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
1494int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1495 struct nlmsghdr *nlh,
1496 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1497 int (*done)(struct netlink_callback*))
1498{
1499 struct netlink_callback *cb;
1500 struct sock *sk;
1501 struct netlink_sock *nlk;
1502
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001503 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (cb == NULL)
1505 return -ENOBUFS;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 cb->dump = dump;
1508 cb->done = done;
1509 cb->nlh = nlh;
1510 atomic_inc(&skb->users);
1511 cb->skb = skb;
1512
1513 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1514 if (sk == NULL) {
1515 netlink_destroy_callback(cb);
1516 return -ECONNREFUSED;
1517 }
1518 nlk = nlk_sk(sk);
Herbert Xu3f660d62007-05-03 03:17:14 -07001519 /* A dump is in progress... */
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001520 mutex_lock(nlk->cb_mutex);
Herbert Xu3f660d62007-05-03 03:17:14 -07001521 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001522 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 netlink_destroy_callback(cb);
1524 sock_put(sk);
1525 return -EBUSY;
1526 }
1527 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001528 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 netlink_dump(sk);
1531 sock_put(sk);
Thomas Grafc702e802007-03-22 23:30:55 -07001532
1533 /* We successfully started a dump, by returning -EINTR we
1534 * signal the queue mangement to interrupt processing of
1535 * any netlink messages so userspace gets a chance to read
1536 * the results. */
1537 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1541{
1542 struct sk_buff *skb;
1543 struct nlmsghdr *rep;
1544 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08001545 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Thomas Graf339bf982006-11-10 14:10:15 -08001547 /* error messages get the original request appened */
1548 if (err)
1549 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Graf339bf982006-11-10 14:10:15 -08001551 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (!skb) {
1553 struct sock *sk;
1554
1555 sk = netlink_lookup(in_skb->sk->sk_protocol,
1556 NETLINK_CB(in_skb).pid);
1557 if (sk) {
1558 sk->sk_err = ENOBUFS;
1559 sk->sk_error_report(sk);
1560 sock_put(sk);
1561 }
1562 return;
1563 }
1564
1565 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
Thomas Graf17977542005-06-18 22:53:48 -07001566 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001567 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001569 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1571}
1572
Thomas Graf82ace472005-11-10 02:25:53 +01001573static int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001574 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001575{
Thomas Graf82ace472005-11-10 02:25:53 +01001576 struct nlmsghdr *nlh;
1577 int err;
1578
1579 while (skb->len >= nlmsg_total_size(0)) {
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001580 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07001581 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01001582
Martin Murrayad8e4b72006-01-10 13:02:29 -08001583 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01001584 return 0;
1585
Thomas Grafd35b6852007-03-22 23:28:46 -07001586 /* Only requests are handled by the kernel */
1587 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1588 goto skip;
1589
Thomas Graf45e7ae72007-03-22 23:29:10 -07001590 /* Skip control messages */
1591 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
1592 goto skip;
1593
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001594 err = cb(skb, nlh);
1595 if (err == -EINTR) {
1596 /* Not an error, but we interrupt processing */
1597 netlink_queue_skip(nlh, skb);
1598 return err;
Thomas Grafd35b6852007-03-22 23:28:46 -07001599 }
1600skip:
1601 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01001602 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01001603
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001604 netlink_queue_skip(nlh, skb);
Thomas Graf82ace472005-11-10 02:25:53 +01001605 }
1606
1607 return 0;
1608}
1609
1610/**
1611 * nelink_run_queue - Process netlink receive queue.
1612 * @sk: Netlink socket containing the queue
1613 * @qlen: Place to store queue length upon entry
1614 * @cb: Callback function invoked for each netlink message found
1615 *
1616 * Processes as much as there was in the queue upon entry and invokes
1617 * a callback function for each netlink message found. The callback
1618 * function may refuse a message by returning a negative error code
1619 * but setting the error pointer to 0 in which case this function
1620 * returns with a qlen != 0.
1621 *
1622 * qlen must be initialized to 0 before the initial entry, afterwards
1623 * the function may be called repeatedly until qlen reaches 0.
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001624 *
1625 * The callback function may return -EINTR to signal that processing
1626 * of netlink messages shall be interrupted. In this case the message
1627 * currently being processed will NOT be requeued onto the receive
1628 * queue.
Thomas Graf82ace472005-11-10 02:25:53 +01001629 */
1630void netlink_run_queue(struct sock *sk, unsigned int *qlen,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001631 int (*cb)(struct sk_buff *, struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001632{
1633 struct sk_buff *skb;
1634
1635 if (!*qlen || *qlen > skb_queue_len(&sk->sk_receive_queue))
1636 *qlen = skb_queue_len(&sk->sk_receive_queue);
1637
1638 for (; *qlen; (*qlen)--) {
1639 skb = skb_dequeue(&sk->sk_receive_queue);
1640 if (netlink_rcv_skb(skb, cb)) {
1641 if (skb->len)
1642 skb_queue_head(&sk->sk_receive_queue, skb);
1643 else {
1644 kfree_skb(skb);
1645 (*qlen)--;
1646 }
1647 break;
1648 }
1649
1650 kfree_skb(skb);
1651 }
1652}
1653
1654/**
1655 * netlink_queue_skip - Skip netlink message while processing queue.
1656 * @nlh: Netlink message to be skipped
1657 * @skb: Socket buffer containing the netlink messages.
1658 *
1659 * Pulls the given netlink message off the socket buffer so the next
1660 * call to netlink_queue_run() will not reconsider the message.
1661 */
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001662static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb)
Thomas Graf82ace472005-11-10 02:25:53 +01001663{
1664 int msglen = NLMSG_ALIGN(nlh->nlmsg_len);
1665
1666 if (msglen > skb->len)
1667 msglen = skb->len;
1668
1669 skb_pull(skb, msglen);
1670}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Thomas Grafd387f6a2006-08-15 00:31:06 -07001672/**
1673 * nlmsg_notify - send a notification netlink message
1674 * @sk: netlink socket to use
1675 * @skb: notification message
1676 * @pid: destination netlink pid for reports or 0
1677 * @group: destination multicast group or 0
1678 * @report: 1 to report back, 0 to disable
1679 * @flags: allocation flags
1680 */
1681int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
1682 unsigned int group, int report, gfp_t flags)
1683{
1684 int err = 0;
1685
1686 if (group) {
1687 int exclude_pid = 0;
1688
1689 if (report) {
1690 atomic_inc(&skb->users);
1691 exclude_pid = pid;
1692 }
1693
1694 /* errors reported via destination sk->sk_err */
1695 nlmsg_multicast(sk, skb, exclude_pid, group, flags);
1696 }
1697
1698 if (report)
1699 err = nlmsg_unicast(sk, skb, pid);
1700
1701 return err;
1702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704#ifdef CONFIG_PROC_FS
1705struct nl_seq_iter {
1706 int link;
1707 int hash_idx;
1708};
1709
1710static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1711{
1712 struct nl_seq_iter *iter = seq->private;
1713 int i, j;
1714 struct sock *s;
1715 struct hlist_node *node;
1716 loff_t off = 0;
1717
1718 for (i=0; i<MAX_LINKS; i++) {
1719 struct nl_pid_hash *hash = &nl_table[i].hash;
1720
1721 for (j = 0; j <= hash->mask; j++) {
1722 sk_for_each(s, node, &hash->table[j]) {
1723 if (off == pos) {
1724 iter->link = i;
1725 iter->hash_idx = j;
1726 return s;
1727 }
1728 ++off;
1729 }
1730 }
1731 }
1732 return NULL;
1733}
1734
1735static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1736{
1737 read_lock(&nl_table_lock);
1738 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1739}
1740
1741static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1742{
1743 struct sock *s;
1744 struct nl_seq_iter *iter;
1745 int i, j;
1746
1747 ++*pos;
1748
1749 if (v == SEQ_START_TOKEN)
1750 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 s = sk_next(v);
1753 if (s)
1754 return s;
1755
1756 iter = seq->private;
1757 i = iter->link;
1758 j = iter->hash_idx + 1;
1759
1760 do {
1761 struct nl_pid_hash *hash = &nl_table[i].hash;
1762
1763 for (; j <= hash->mask; j++) {
1764 s = sk_head(&hash->table[j]);
1765 if (s) {
1766 iter->link = i;
1767 iter->hash_idx = j;
1768 return s;
1769 }
1770 }
1771
1772 j = 0;
1773 } while (++i < MAX_LINKS);
1774
1775 return NULL;
1776}
1777
1778static void netlink_seq_stop(struct seq_file *seq, void *v)
1779{
1780 read_unlock(&nl_table_lock);
1781}
1782
1783
1784static int netlink_seq_show(struct seq_file *seq, void *v)
1785{
1786 if (v == SEQ_START_TOKEN)
1787 seq_puts(seq,
1788 "sk Eth Pid Groups "
1789 "Rmem Wmem Dump Locks\n");
1790 else {
1791 struct sock *s = v;
1792 struct netlink_sock *nlk = nlk_sk(s);
1793
1794 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1795 s,
1796 s->sk_protocol,
1797 nlk->pid,
Patrick McHardy513c2502005-09-06 15:43:59 -07001798 nlk->groups ? (u32)nlk->groups[0] : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 atomic_read(&s->sk_rmem_alloc),
1800 atomic_read(&s->sk_wmem_alloc),
1801 nlk->cb,
1802 atomic_read(&s->sk_refcnt)
1803 );
1804
1805 }
1806 return 0;
1807}
1808
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001809static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 .start = netlink_seq_start,
1811 .next = netlink_seq_next,
1812 .stop = netlink_seq_stop,
1813 .show = netlink_seq_show,
1814};
1815
1816
1817static int netlink_seq_open(struct inode *inode, struct file *file)
1818{
1819 struct seq_file *seq;
1820 struct nl_seq_iter *iter;
1821 int err;
1822
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001823 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (!iter)
1825 return -ENOMEM;
1826
1827 err = seq_open(file, &netlink_seq_ops);
1828 if (err) {
1829 kfree(iter);
1830 return err;
1831 }
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 seq = file->private_data;
1834 seq->private = iter;
1835 return 0;
1836}
1837
Arjan van de Venda7071d2007-02-12 00:55:36 -08001838static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 .owner = THIS_MODULE,
1840 .open = netlink_seq_open,
1841 .read = seq_read,
1842 .llseek = seq_lseek,
1843 .release = seq_release_private,
1844};
1845
1846#endif
1847
1848int netlink_register_notifier(struct notifier_block *nb)
1849{
Alan Sterne041c682006-03-27 01:16:30 -08001850 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853int netlink_unregister_notifier(struct notifier_block *nb)
1854{
Alan Sterne041c682006-03-27 01:16:30 -08001855 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001857
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001858static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .family = PF_NETLINK,
1860 .owner = THIS_MODULE,
1861 .release = netlink_release,
1862 .bind = netlink_bind,
1863 .connect = netlink_connect,
1864 .socketpair = sock_no_socketpair,
1865 .accept = sock_no_accept,
1866 .getname = netlink_getname,
1867 .poll = datagram_poll,
1868 .ioctl = sock_no_ioctl,
1869 .listen = sock_no_listen,
1870 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001871 .setsockopt = netlink_setsockopt,
1872 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 .sendmsg = netlink_sendmsg,
1874 .recvmsg = netlink_recvmsg,
1875 .mmap = sock_no_mmap,
1876 .sendpage = sock_no_sendpage,
1877};
1878
1879static struct net_proto_family netlink_family_ops = {
1880 .family = PF_NETLINK,
1881 .create = netlink_create,
1882 .owner = THIS_MODULE, /* for consistency 8) */
1883};
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885static int __init netlink_proto_init(void)
1886{
1887 struct sk_buff *dummy_skb;
1888 int i;
1889 unsigned long max;
1890 unsigned int order;
1891 int err = proto_register(&netlink_proto, 0);
1892
1893 if (err != 0)
1894 goto out;
1895
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07001896 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001898 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001899 if (!nl_table)
1900 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (num_physpages >= (128 * 1024))
1903 max = num_physpages >> (21 - PAGE_SHIFT);
1904 else
1905 max = num_physpages >> (23 - PAGE_SHIFT);
1906
1907 order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1908 max = (1UL << order) / sizeof(struct hlist_head);
1909 order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1910
1911 for (i = 0; i < MAX_LINKS; i++) {
1912 struct nl_pid_hash *hash = &nl_table[i].hash;
1913
1914 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1915 if (!hash->table) {
1916 while (i-- > 0)
1917 nl_pid_hash_free(nl_table[i].hash.table,
1918 1 * sizeof(*hash->table));
1919 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001920 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922 memset(hash->table, 0, 1 * sizeof(*hash->table));
1923 hash->max_shift = order;
1924 hash->shift = 0;
1925 hash->mask = 0;
1926 hash->rehash_time = jiffies;
1927 }
1928
1929 sock_register(&netlink_family_ops);
1930#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001931 proc_net_fops_create(&init_net, "netlink", 0, &netlink_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932#endif
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001933 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 rtnetlink_init();
1935out:
1936 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001937panic:
1938 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941core_initcall(netlink_proto_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943EXPORT_SYMBOL(netlink_ack);
Thomas Graf82ace472005-11-10 02:25:53 +01001944EXPORT_SYMBOL(netlink_run_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945EXPORT_SYMBOL(netlink_broadcast);
1946EXPORT_SYMBOL(netlink_dump_start);
1947EXPORT_SYMBOL(netlink_kernel_create);
1948EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949EXPORT_SYMBOL(netlink_set_nonroot);
1950EXPORT_SYMBOL(netlink_unicast);
1951EXPORT_SYMBOL(netlink_unregister_notifier);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001952EXPORT_SYMBOL(nlmsg_notify);