blob: 406a493300d8ea0578f748103aa70a8d8a59efc8 [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
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700387static int __netlink_create(struct net *net, struct socket *sock,
388 struct mutex *cb_mutex, 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
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700395 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
Patrick McHardyab33a172005-08-14 19:31:36 -0700396 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
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700415static int netlink_create(struct net *net, struct socket *sock, int protocol)
Patrick McHardyab33a172005-08-14 19:31:36 -0700416{
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
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700422 if (net != &init_net)
423 return -EAFNOSUPPORT;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 sock->state = SS_UNCONNECTED;
426
427 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
428 return -ESOCKTNOSUPPORT;
429
430 if (protocol<0 || protocol >= MAX_LINKS)
431 return -EPROTONOSUPPORT;
432
Patrick McHardy77247bb2005-08-14 19:27:13 -0700433 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700434#ifdef CONFIG_KMOD
Patrick McHardyab33a172005-08-14 19:31:36 -0700435 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700436 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700437 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700438 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700439 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700440#endif
441 if (nl_table[protocol].registered &&
442 try_module_get(nl_table[protocol].module))
443 module = nl_table[protocol].module;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700444 cb_mutex = nl_table[protocol].cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700445 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700446
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700447 if ((err = __netlink_create(net, sock, cb_mutex, protocol)) < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700448 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700450 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700451 nlk->module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700452out:
453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Patrick McHardyab33a172005-08-14 19:31:36 -0700455out_module:
456 module_put(module);
457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460static int netlink_release(struct socket *sock)
461{
462 struct sock *sk = sock->sk;
463 struct netlink_sock *nlk;
464
465 if (!sk)
466 return 0;
467
468 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700469 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 nlk = nlk_sk(sk);
471
Herbert Xu3f660d62007-05-03 03:17:14 -0700472 /*
473 * OK. Socket is unlinked, any packets that arrive now
474 * will be purged.
475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 sock->sk = NULL;
478 wake_up_interruptible_all(&nlk->wait);
479
480 skb_queue_purge(&sk->sk_write_queue);
481
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700482 if (nlk->pid && !nlk->subscriptions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct netlink_notify n = {
484 .protocol = sk->sk_protocol,
485 .pid = nlk->pid,
486 };
Alan Sterne041c682006-03-27 01:16:30 -0800487 atomic_notifier_call_chain(&netlink_chain,
488 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900489 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700490
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800491 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700492
Patrick McHardy4277a082006-03-20 18:52:01 -0800493 netlink_table_grab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700494 if (nlk->flags & NETLINK_KERNEL_SOCKET) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800495 kfree(nl_table[sk->sk_protocol].listeners);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700496 nl_table[sk->sk_protocol].module = NULL;
Patrick McHardyab33a172005-08-14 19:31:36 -0700497 nl_table[sk->sk_protocol].registered = 0;
Patrick McHardy4277a082006-03-20 18:52:01 -0800498 } else if (nlk->subscriptions)
499 netlink_update_listeners(sk);
500 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700501
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700502 kfree(nlk->groups);
503 nlk->groups = NULL;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 sock_put(sk);
506 return 0;
507}
508
509static int netlink_autobind(struct socket *sock)
510{
511 struct sock *sk = sock->sk;
512 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
513 struct hlist_head *head;
514 struct sock *osk;
515 struct hlist_node *node;
Herbert Xuc27bd492005-11-22 14:41:50 -0800516 s32 pid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int err;
518 static s32 rover = -4097;
519
520retry:
521 cond_resched();
522 netlink_table_grab();
523 head = nl_pid_hashfn(hash, pid);
524 sk_for_each(osk, node, head) {
525 if (nlk_sk(osk)->pid == pid) {
526 /* Bind collision, search negative pid values. */
527 pid = rover--;
528 if (rover > -4097)
529 rover = -4097;
530 netlink_table_ungrab();
531 goto retry;
532 }
533 }
534 netlink_table_ungrab();
535
536 err = netlink_insert(sk, pid);
537 if (err == -EADDRINUSE)
538 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700539
540 /* If 2 threads race to autobind, that is fine. */
541 if (err == -EBUSY)
542 err = 0;
543
544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900547static inline int netlink_capable(struct socket *sock, unsigned int flag)
548{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
550 capable(CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900551}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700553static void
554netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
555{
556 struct netlink_sock *nlk = nlk_sk(sk);
557
558 if (nlk->subscriptions && !subscriptions)
559 __sk_del_bind_node(sk);
560 else if (!nlk->subscriptions && subscriptions)
561 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
562 nlk->subscriptions = subscriptions;
563}
564
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700565static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700566{
567 struct netlink_sock *nlk = nlk_sk(sk);
568 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700569 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700570 int err = 0;
571
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700572 netlink_table_grab();
573
Patrick McHardy513c2502005-09-06 15:43:59 -0700574 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700575 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700576 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700577 goto out_unlock;
578 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700579
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700580 if (nlk->ngroups >= groups)
581 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700582
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700583 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
584 if (new_groups == NULL) {
585 err = -ENOMEM;
586 goto out_unlock;
587 }
588 memset((char*)new_groups + NLGRPSZ(nlk->ngroups), 0,
589 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
590
591 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700592 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700593 out_unlock:
594 netlink_table_ungrab();
595 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
599{
600 struct sock *sk = sock->sk;
601 struct netlink_sock *nlk = nlk_sk(sk);
602 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
603 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (nladdr->nl_family != AF_NETLINK)
606 return -EINVAL;
607
608 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700609 if (nladdr->nl_groups) {
610 if (!netlink_capable(sock, NL_NONROOT_RECV))
611 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700612 err = netlink_realloc_groups(sk);
613 if (err)
614 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (nlk->pid) {
618 if (nladdr->nl_pid != nlk->pid)
619 return -EINVAL;
620 } else {
621 err = nladdr->nl_pid ?
622 netlink_insert(sk, nladdr->nl_pid) :
623 netlink_autobind(sock);
624 if (err)
625 return err;
626 }
627
Patrick McHardy513c2502005-09-06 15:43:59 -0700628 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630
631 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700632 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900633 hweight32(nladdr->nl_groups) -
634 hweight32(nlk->groups[0]));
635 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800636 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 netlink_table_ungrab();
638
639 return 0;
640}
641
642static int netlink_connect(struct socket *sock, struct sockaddr *addr,
643 int alen, int flags)
644{
645 int err = 0;
646 struct sock *sk = sock->sk;
647 struct netlink_sock *nlk = nlk_sk(sk);
648 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
649
650 if (addr->sa_family == AF_UNSPEC) {
651 sk->sk_state = NETLINK_UNCONNECTED;
652 nlk->dst_pid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700653 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655 }
656 if (addr->sa_family != AF_NETLINK)
657 return -EINVAL;
658
659 /* Only superuser is allowed to send multicasts */
660 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
661 return -EPERM;
662
663 if (!nlk->pid)
664 err = netlink_autobind(sock);
665
666 if (err == 0) {
667 sk->sk_state = NETLINK_CONNECTED;
668 nlk->dst_pid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700669 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 return err;
673}
674
675static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
676{
677 struct sock *sk = sock->sk;
678 struct netlink_sock *nlk = nlk_sk(sk);
679 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 nladdr->nl_family = AF_NETLINK;
682 nladdr->nl_pad = 0;
683 *addr_len = sizeof(*nladdr);
684
685 if (peer) {
686 nladdr->nl_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700687 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 } else {
689 nladdr->nl_pid = nlk->pid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700690 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 return 0;
693}
694
695static void netlink_overrun(struct sock *sk)
696{
697 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
698 sk->sk_err = ENOBUFS;
699 sk->sk_error_report(sk);
700 }
701}
702
703static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
704{
705 int protocol = ssk->sk_protocol;
706 struct sock *sock;
707 struct netlink_sock *nlk;
708
709 sock = netlink_lookup(protocol, pid);
710 if (!sock)
711 return ERR_PTR(-ECONNREFUSED);
712
713 /* Don't bother queuing skb if kernel socket has no input function */
714 nlk = nlk_sk(sock);
715 if ((nlk->pid == 0 && !nlk->data_ready) ||
716 (sock->sk_state == NETLINK_CONNECTED &&
717 nlk->dst_pid != nlk_sk(ssk)->pid)) {
718 sock_put(sock);
719 return ERR_PTR(-ECONNREFUSED);
720 }
721 return sock;
722}
723
724struct sock *netlink_getsockbyfilp(struct file *filp)
725{
Josef Sipek6db5fc52006-12-08 02:37:25 -0800726 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct sock *sock;
728
729 if (!S_ISSOCK(inode->i_mode))
730 return ERR_PTR(-ENOTSOCK);
731
732 sock = SOCKET_I(inode)->sk;
733 if (sock->sk_family != AF_NETLINK)
734 return ERR_PTR(-EINVAL);
735
736 sock_hold(sock);
737 return sock;
738}
739
740/*
741 * Attach a skb to a netlink socket.
742 * The caller must hold a reference to the destination socket. On error, the
743 * reference is dropped. The skb is not send to the destination, just all
744 * all error checks are performed and memory in the queue is reserved.
745 * Return values:
746 * < 0: error. skb freed, reference to sock dropped.
747 * 0: continue
748 * 1: repeat lookup - reference dropped while waiting for socket memory.
749 */
Alexey Kuznetsova70ea992006-02-09 16:40:11 -0800750int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock,
751 long timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct netlink_sock *nlk;
754
755 nlk = nlk_sk(sk);
756
757 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
758 test_bit(0, &nlk->state)) {
759 DECLARE_WAITQUEUE(wait, current);
760 if (!timeo) {
Alexey Kuznetsova70ea992006-02-09 16:40:11 -0800761 if (!ssk || nlk_sk(ssk)->pid == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 netlink_overrun(sk);
763 sock_put(sk);
764 kfree_skb(skb);
765 return -EAGAIN;
766 }
767
768 __set_current_state(TASK_INTERRUPTIBLE);
769 add_wait_queue(&nlk->wait, &wait);
770
771 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
772 test_bit(0, &nlk->state)) &&
773 !sock_flag(sk, SOCK_DEAD))
774 timeo = schedule_timeout(timeo);
775
776 __set_current_state(TASK_RUNNING);
777 remove_wait_queue(&nlk->wait, &wait);
778 sock_put(sk);
779
780 if (signal_pending(current)) {
781 kfree_skb(skb);
782 return sock_intr_errno(timeo);
783 }
784 return 1;
785 }
786 skb_set_owner_r(skb, sk);
787 return 0;
788}
789
790int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int len = skb->len;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 skb_queue_tail(&sk->sk_receive_queue, skb);
795 sk->sk_data_ready(sk, len);
796 sock_put(sk);
797 return len;
798}
799
800void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
801{
802 kfree_skb(skb);
803 sock_put(sk);
804}
805
Victor Fusco37da6472005-07-18 13:35:43 -0700806static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +0100807 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 int delta;
810
811 skb_orphan(skb);
812
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700813 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (delta * 2 < skb->truesize)
815 return skb;
816
817 if (skb_shared(skb)) {
818 struct sk_buff *nskb = skb_clone(skb, allocation);
819 if (!nskb)
820 return skb;
821 kfree_skb(skb);
822 skb = nskb;
823 }
824
825 if (!pskb_expand_head(skb, 0, -delta, allocation))
826 skb->truesize -= delta;
827
828 return skb;
829}
830
831int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
832{
833 struct sock *sk;
834 int err;
835 long timeo;
836
837 skb = netlink_trim(skb, gfp_any());
838
839 timeo = sock_sndtimeo(ssk, nonblock);
840retry:
841 sk = netlink_getsockbypid(ssk, pid);
842 if (IS_ERR(sk)) {
843 kfree_skb(skb);
844 return PTR_ERR(sk);
845 }
Alexey Kuznetsova70ea992006-02-09 16:40:11 -0800846 err = netlink_attachskb(sk, skb, nonblock, timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (err == 1)
848 goto retry;
849 if (err)
850 return err;
851
852 return netlink_sendskb(sk, skb, ssk->sk_protocol);
853}
854
Patrick McHardy4277a082006-03-20 18:52:01 -0800855int netlink_has_listeners(struct sock *sk, unsigned int group)
856{
857 int res = 0;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700858 unsigned long *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -0800859
860 BUG_ON(!(nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET));
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700861
862 rcu_read_lock();
863 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
864
Patrick McHardy4277a082006-03-20 18:52:01 -0800865 if (group - 1 < nl_table[sk->sk_protocol].groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700866 res = test_bit(group - 1, listeners);
867
868 rcu_read_unlock();
869
Patrick McHardy4277a082006-03-20 18:52:01 -0800870 return res;
871}
872EXPORT_SYMBOL_GPL(netlink_has_listeners);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
875{
876 struct netlink_sock *nlk = nlk_sk(sk);
877
878 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
879 !test_bit(0, &nlk->state)) {
880 skb_set_owner_r(skb, sk);
881 skb_queue_tail(&sk->sk_receive_queue, skb);
882 sk->sk_data_ready(sk, skb->len);
883 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
884 }
885 return -1;
886}
887
888struct netlink_broadcast_data {
889 struct sock *exclude_sk;
890 u32 pid;
891 u32 group;
892 int failure;
893 int congested;
894 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -0400895 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 struct sk_buff *skb, *skb2;
897};
898
899static inline int do_one_broadcast(struct sock *sk,
900 struct netlink_broadcast_data *p)
901{
902 struct netlink_sock *nlk = nlk_sk(sk);
903 int val;
904
905 if (p->exclude_sk == sk)
906 goto out;
907
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700908 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
909 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto out;
911
912 if (p->failure) {
913 netlink_overrun(sk);
914 goto out;
915 }
916
917 sock_hold(sk);
918 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700919 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 p->skb2 = skb_clone(p->skb, p->allocation);
921 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700922 p->skb2 = skb_get(p->skb);
923 /*
924 * skb ownership may have been set when
925 * delivered to a previous socket.
926 */
927 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 }
930 if (p->skb2 == NULL) {
931 netlink_overrun(sk);
932 /* Clone failed. Notify ALL listeners. */
933 p->failure = 1;
934 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
935 netlink_overrun(sk);
936 } else {
937 p->congested |= val;
938 p->delivered = 1;
939 p->skb2 = NULL;
940 }
941 sock_put(sk);
942
943out:
944 return 0;
945}
946
947int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
Al Virodd0fc662005-10-07 07:46:04 +0100948 u32 group, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct netlink_broadcast_data info;
951 struct hlist_node *node;
952 struct sock *sk;
953
954 skb = netlink_trim(skb, allocation);
955
956 info.exclude_sk = ssk;
957 info.pid = pid;
958 info.group = group;
959 info.failure = 0;
960 info.congested = 0;
961 info.delivered = 0;
962 info.allocation = allocation;
963 info.skb = skb;
964 info.skb2 = NULL;
965
966 /* While we sleep in clone, do not allow to change socket list */
967
968 netlink_lock_table();
969
970 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
971 do_one_broadcast(sk, &info);
972
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -0700973 kfree_skb(skb);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 netlink_unlock_table();
976
977 if (info.skb2)
978 kfree_skb(info.skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (info.delivered) {
981 if (info.congested && (allocation & __GFP_WAIT))
982 yield();
983 return 0;
984 }
985 if (info.failure)
986 return -ENOBUFS;
987 return -ESRCH;
988}
989
990struct netlink_set_err_data {
991 struct sock *exclude_sk;
992 u32 pid;
993 u32 group;
994 int code;
995};
996
997static inline int do_one_set_err(struct sock *sk,
998 struct netlink_set_err_data *p)
999{
1000 struct netlink_sock *nlk = nlk_sk(sk);
1001
1002 if (sk == p->exclude_sk)
1003 goto out;
1004
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001005 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1006 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto out;
1008
1009 sk->sk_err = p->code;
1010 sk->sk_error_report(sk);
1011out:
1012 return 0;
1013}
1014
1015void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
1016{
1017 struct netlink_set_err_data info;
1018 struct hlist_node *node;
1019 struct sock *sk;
1020
1021 info.exclude_sk = ssk;
1022 info.pid = pid;
1023 info.group = group;
1024 info.code = code;
1025
1026 read_lock(&nl_table_lock);
1027
1028 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1029 do_one_set_err(sk, &info);
1030
1031 read_unlock(&nl_table_lock);
1032}
1033
Johannes Berg84659eb2007-07-18 15:47:05 -07001034/* must be called with netlink table grabbed */
1035static void netlink_update_socket_mc(struct netlink_sock *nlk,
1036 unsigned int group,
1037 int is_new)
1038{
1039 int old, new = !!is_new, subscriptions;
1040
1041 old = test_bit(group - 1, nlk->groups);
1042 subscriptions = nlk->subscriptions - old + new;
1043 if (new)
1044 __set_bit(group - 1, nlk->groups);
1045 else
1046 __clear_bit(group - 1, nlk->groups);
1047 netlink_update_subscriptions(&nlk->sk, subscriptions);
1048 netlink_update_listeners(&nlk->sk);
1049}
1050
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001051static int netlink_setsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001052 char __user *optval, int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001053{
1054 struct sock *sk = sock->sk;
1055 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001056 unsigned int val = 0;
1057 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001058
1059 if (level != SOL_NETLINK)
1060 return -ENOPROTOOPT;
1061
1062 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001063 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001064 return -EFAULT;
1065
1066 switch (optname) {
1067 case NETLINK_PKTINFO:
1068 if (val)
1069 nlk->flags |= NETLINK_RECV_PKTINFO;
1070 else
1071 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1072 err = 0;
1073 break;
1074 case NETLINK_ADD_MEMBERSHIP:
1075 case NETLINK_DROP_MEMBERSHIP: {
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001076 if (!netlink_capable(sock, NL_NONROOT_RECV))
1077 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001078 err = netlink_realloc_groups(sk);
1079 if (err)
1080 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001081 if (!val || val - 1 >= nlk->ngroups)
1082 return -EINVAL;
1083 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001084 netlink_update_socket_mc(nlk, val,
1085 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001086 netlink_table_ungrab();
1087 err = 0;
1088 break;
1089 }
1090 default:
1091 err = -ENOPROTOOPT;
1092 }
1093 return err;
1094}
1095
1096static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001097 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001098{
1099 struct sock *sk = sock->sk;
1100 struct netlink_sock *nlk = nlk_sk(sk);
1101 int len, val, err;
1102
1103 if (level != SOL_NETLINK)
1104 return -ENOPROTOOPT;
1105
1106 if (get_user(len, optlen))
1107 return -EFAULT;
1108 if (len < 0)
1109 return -EINVAL;
1110
1111 switch (optname) {
1112 case NETLINK_PKTINFO:
1113 if (len < sizeof(int))
1114 return -EINVAL;
1115 len = sizeof(int);
1116 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001117 if (put_user(len, optlen) ||
1118 put_user(val, optval))
1119 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001120 err = 0;
1121 break;
1122 default:
1123 err = -ENOPROTOOPT;
1124 }
1125 return err;
1126}
1127
1128static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1129{
1130 struct nl_pktinfo info;
1131
1132 info.group = NETLINK_CB(skb).dst_group;
1133 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136static inline void netlink_rcv_wake(struct sock *sk)
1137{
1138 struct netlink_sock *nlk = nlk_sk(sk);
1139
David S. Millerb03efcf2005-07-08 14:57:23 -07001140 if (skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 clear_bit(0, &nlk->state);
1142 if (!test_bit(0, &nlk->state))
1143 wake_up_interruptible(&nlk->wait);
1144}
1145
1146static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1147 struct msghdr *msg, size_t len)
1148{
1149 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1150 struct sock *sk = sock->sk;
1151 struct netlink_sock *nlk = nlk_sk(sk);
1152 struct sockaddr_nl *addr=msg->msg_name;
1153 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001154 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 struct sk_buff *skb;
1156 int err;
1157 struct scm_cookie scm;
1158
1159 if (msg->msg_flags&MSG_OOB)
1160 return -EOPNOTSUPP;
1161
1162 if (NULL == siocb->scm)
1163 siocb->scm = &scm;
1164 err = scm_send(sock, msg, siocb->scm);
1165 if (err < 0)
1166 return err;
1167
1168 if (msg->msg_namelen) {
1169 if (addr->nl_family != AF_NETLINK)
1170 return -EINVAL;
1171 dst_pid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001172 dst_group = ffs(addr->nl_groups);
1173 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return -EPERM;
1175 } else {
1176 dst_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001177 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179
1180 if (!nlk->pid) {
1181 err = netlink_autobind(sock);
1182 if (err)
1183 goto out;
1184 }
1185
1186 err = -EMSGSIZE;
1187 if (len > sk->sk_sndbuf - 32)
1188 goto out;
1189 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001190 skb = alloc_skb(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (skb==NULL)
1192 goto out;
1193
1194 NETLINK_CB(skb).pid = nlk->pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001195 NETLINK_CB(skb).dst_group = dst_group;
Serge Hallync94c2572005-04-29 16:27:17 +01001196 NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
Steve Grubbe7c34972006-04-03 09:08:13 -04001197 selinux_get_task_sid(current, &(NETLINK_CB(skb).sid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1199
1200 /* What can I do? Netlink is asynchronous, so that
1201 we will have to save current capabilities to
1202 check them, when this message will be delivered
1203 to corresponding kernel module. --ANK (980802)
1204 */
1205
1206 err = -EFAULT;
1207 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
1208 kfree_skb(skb);
1209 goto out;
1210 }
1211
1212 err = security_netlink_send(sk, skb);
1213 if (err) {
1214 kfree_skb(skb);
1215 goto out;
1216 }
1217
Patrick McHardyd629b832005-08-14 19:27:50 -07001218 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 atomic_inc(&skb->users);
Patrick McHardyd629b832005-08-14 19:27:50 -07001220 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1223
1224out:
1225 return err;
1226}
1227
1228static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1229 struct msghdr *msg, size_t len,
1230 int flags)
1231{
1232 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1233 struct scm_cookie scm;
1234 struct sock *sk = sock->sk;
1235 struct netlink_sock *nlk = nlk_sk(sk);
1236 int noblock = flags&MSG_DONTWAIT;
1237 size_t copied;
1238 struct sk_buff *skb;
1239 int err;
1240
1241 if (flags&MSG_OOB)
1242 return -EOPNOTSUPP;
1243
1244 copied = 0;
1245
1246 skb = skb_recv_datagram(sk,flags,noblock,&err);
1247 if (skb==NULL)
1248 goto out;
1249
1250 msg->msg_namelen = 0;
1251
1252 copied = skb->len;
1253 if (len < copied) {
1254 msg->msg_flags |= MSG_TRUNC;
1255 copied = len;
1256 }
1257
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001258 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1260
1261 if (msg->msg_name) {
1262 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
1263 addr->nl_family = AF_NETLINK;
1264 addr->nl_pad = 0;
1265 addr->nl_pid = NETLINK_CB(skb).pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001266 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 msg->msg_namelen = sizeof(*addr);
1268 }
1269
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001270 if (nlk->flags & NETLINK_RECV_PKTINFO)
1271 netlink_cmsg_recv_pktinfo(msg, skb);
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (NULL == siocb->scm) {
1274 memset(&scm, 0, sizeof(scm));
1275 siocb->scm = &scm;
1276 }
1277 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001278 if (flags & MSG_TRUNC)
1279 copied = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 skb_free_datagram(sk, skb);
1281
1282 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1283 netlink_dump(sk);
1284
1285 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286out:
1287 netlink_rcv_wake(sk);
1288 return err ? : copied;
1289}
1290
1291static void netlink_data_ready(struct sock *sk, int len)
1292{
1293 struct netlink_sock *nlk = nlk_sk(sk);
1294
1295 if (nlk->data_ready)
1296 nlk->data_ready(sk, len);
1297 netlink_rcv_wake(sk);
1298}
1299
1300/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001301 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 * complete set of kernel non-blocking support for message
1303 * queueing.
1304 */
1305
1306struct sock *
Patrick McHardy06628602005-08-15 12:33:26 -07001307netlink_kernel_create(int unit, unsigned int groups,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001308 void (*input)(struct sock *sk, int len),
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001309 struct mutex *cb_mutex, struct module *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct socket *sock;
1312 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001313 struct netlink_sock *nlk;
Patrick McHardy4277a082006-03-20 18:52:01 -08001314 unsigned long *listeners = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001316 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (unit<0 || unit>=MAX_LINKS)
1319 return NULL;
1320
1321 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1322 return NULL;
1323
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001324 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
Patrick McHardy77247bb2005-08-14 19:27:13 -07001325 goto out_sock_release;
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001326
Patrick McHardy4277a082006-03-20 18:52:01 -08001327 if (groups < 32)
1328 groups = 32;
1329
1330 listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
1331 if (!listeners)
1332 goto out_sock_release;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 sk = sock->sk;
1335 sk->sk_data_ready = netlink_data_ready;
1336 if (input)
1337 nlk_sk(sk)->data_ready = input;
1338
Patrick McHardy77247bb2005-08-14 19:27:13 -07001339 if (netlink_insert(sk, 0))
1340 goto out_sock_release;
1341
1342 nlk = nlk_sk(sk);
1343 nlk->flags |= NETLINK_KERNEL_SOCKET;
1344
1345 netlink_table_grab();
Patrick McHardy4277a082006-03-20 18:52:01 -08001346 nl_table[unit].groups = groups;
1347 nl_table[unit].listeners = listeners;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001348 nl_table[unit].cb_mutex = cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001349 nl_table[unit].module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -07001350 nl_table[unit].registered = 1;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001351 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001352
1353 return sk;
1354
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001355out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001356 kfree(listeners);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001357 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001358 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001361/**
1362 * netlink_change_ngroups - change number of multicast groups
1363 *
1364 * This changes the number of multicast groups that are available
1365 * on a certain netlink family. Note that it is not possible to
Johannes Berg84659eb2007-07-18 15:47:05 -07001366 * change the number of groups to below 32. Also note that it does
1367 * not implicitly call netlink_clear_multicast_users() when the
1368 * number of groups is reduced.
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001369 *
1370 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1371 * @groups: The new number of groups.
1372 */
1373int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1374{
1375 unsigned long *listeners, *old = NULL;
1376 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1377 int err = 0;
1378
1379 if (groups < 32)
1380 groups = 32;
1381
1382 netlink_table_grab();
1383 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1384 listeners = kzalloc(NLGRPSZ(groups), GFP_ATOMIC);
1385 if (!listeners) {
1386 err = -ENOMEM;
1387 goto out_ungrab;
1388 }
1389 old = tbl->listeners;
1390 memcpy(listeners, old, NLGRPSZ(tbl->groups));
1391 rcu_assign_pointer(tbl->listeners, listeners);
1392 }
1393 tbl->groups = groups;
1394
1395 out_ungrab:
1396 netlink_table_ungrab();
1397 synchronize_rcu();
1398 kfree(old);
1399 return err;
1400}
1401EXPORT_SYMBOL(netlink_change_ngroups);
1402
Johannes Berg84659eb2007-07-18 15:47:05 -07001403/**
1404 * netlink_clear_multicast_users - kick off multicast listeners
1405 *
1406 * This function removes all listeners from the given group.
1407 * @ksk: The kernel netlink socket, as returned by
1408 * netlink_kernel_create().
1409 * @group: The multicast group to clear.
1410 */
1411void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1412{
1413 struct sock *sk;
1414 struct hlist_node *node;
1415 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1416
1417 netlink_table_grab();
1418
1419 sk_for_each_bound(sk, node, &tbl->mc_list)
1420 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1421
1422 netlink_table_ungrab();
1423}
1424EXPORT_SYMBOL(netlink_clear_multicast_users);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426void netlink_set_nonroot(int protocol, unsigned int flags)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001427{
1428 if ((unsigned int)protocol < MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 nl_table[protocol].nl_nonroot = flags;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432static void netlink_destroy_callback(struct netlink_callback *cb)
1433{
1434 if (cb->skb)
1435 kfree_skb(cb->skb);
1436 kfree(cb);
1437}
1438
1439/*
1440 * It looks a bit ugly.
1441 * It would be better to create kernel thread.
1442 */
1443
1444static int netlink_dump(struct sock *sk)
1445{
1446 struct netlink_sock *nlk = nlk_sk(sk);
1447 struct netlink_callback *cb;
1448 struct sk_buff *skb;
1449 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001450 int len, err = -ENOBUFS;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1453 if (!skb)
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001454 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001456 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 cb = nlk->cb;
1459 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001460 err = -EINVAL;
1461 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
1464 len = cb->dump(skb, cb);
1465
1466 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001467 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 skb_queue_tail(&sk->sk_receive_queue, skb);
1469 sk->sk_data_ready(sk, len);
1470 return 0;
1471 }
1472
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001473 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1474 if (!nlh)
1475 goto errout_skb;
1476
1477 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 skb_queue_tail(&sk->sk_receive_queue, skb);
1480 sk->sk_data_ready(sk, skb->len);
1481
Thomas Grafa8f74b22005-11-10 02:25:52 +01001482 if (cb->done)
1483 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001485 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 netlink_destroy_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001489
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001490errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001491 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001492 kfree_skb(skb);
1493errout:
1494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1498 struct nlmsghdr *nlh,
1499 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1500 int (*done)(struct netlink_callback*))
1501{
1502 struct netlink_callback *cb;
1503 struct sock *sk;
1504 struct netlink_sock *nlk;
1505
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001506 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (cb == NULL)
1508 return -ENOBUFS;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 cb->dump = dump;
1511 cb->done = done;
1512 cb->nlh = nlh;
1513 atomic_inc(&skb->users);
1514 cb->skb = skb;
1515
1516 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1517 if (sk == NULL) {
1518 netlink_destroy_callback(cb);
1519 return -ECONNREFUSED;
1520 }
1521 nlk = nlk_sk(sk);
Herbert Xu3f660d62007-05-03 03:17:14 -07001522 /* A dump is in progress... */
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001523 mutex_lock(nlk->cb_mutex);
Herbert Xu3f660d62007-05-03 03:17:14 -07001524 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001525 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 netlink_destroy_callback(cb);
1527 sock_put(sk);
1528 return -EBUSY;
1529 }
1530 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001531 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 netlink_dump(sk);
1534 sock_put(sk);
Thomas Grafc702e802007-03-22 23:30:55 -07001535
1536 /* We successfully started a dump, by returning -EINTR we
1537 * signal the queue mangement to interrupt processing of
1538 * any netlink messages so userspace gets a chance to read
1539 * the results. */
1540 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1544{
1545 struct sk_buff *skb;
1546 struct nlmsghdr *rep;
1547 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08001548 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Thomas Graf339bf982006-11-10 14:10:15 -08001550 /* error messages get the original request appened */
1551 if (err)
1552 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Thomas Graf339bf982006-11-10 14:10:15 -08001554 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (!skb) {
1556 struct sock *sk;
1557
1558 sk = netlink_lookup(in_skb->sk->sk_protocol,
1559 NETLINK_CB(in_skb).pid);
1560 if (sk) {
1561 sk->sk_err = ENOBUFS;
1562 sk->sk_error_report(sk);
1563 sock_put(sk);
1564 }
1565 return;
1566 }
1567
1568 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
Thomas Graf17977542005-06-18 22:53:48 -07001569 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001570 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001572 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1574}
1575
Thomas Graf82ace472005-11-10 02:25:53 +01001576static int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001577 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001578{
Thomas Graf82ace472005-11-10 02:25:53 +01001579 struct nlmsghdr *nlh;
1580 int err;
1581
1582 while (skb->len >= nlmsg_total_size(0)) {
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001583 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07001584 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01001585
Martin Murrayad8e4b72006-01-10 13:02:29 -08001586 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01001587 return 0;
1588
Thomas Grafd35b6852007-03-22 23:28:46 -07001589 /* Only requests are handled by the kernel */
1590 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1591 goto skip;
1592
Thomas Graf45e7ae72007-03-22 23:29:10 -07001593 /* Skip control messages */
1594 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
1595 goto skip;
1596
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001597 err = cb(skb, nlh);
1598 if (err == -EINTR) {
1599 /* Not an error, but we interrupt processing */
1600 netlink_queue_skip(nlh, skb);
1601 return err;
Thomas Grafd35b6852007-03-22 23:28:46 -07001602 }
1603skip:
1604 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01001605 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01001606
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001607 netlink_queue_skip(nlh, skb);
Thomas Graf82ace472005-11-10 02:25:53 +01001608 }
1609
1610 return 0;
1611}
1612
1613/**
1614 * nelink_run_queue - Process netlink receive queue.
1615 * @sk: Netlink socket containing the queue
1616 * @qlen: Place to store queue length upon entry
1617 * @cb: Callback function invoked for each netlink message found
1618 *
1619 * Processes as much as there was in the queue upon entry and invokes
1620 * a callback function for each netlink message found. The callback
1621 * function may refuse a message by returning a negative error code
1622 * but setting the error pointer to 0 in which case this function
1623 * returns with a qlen != 0.
1624 *
1625 * qlen must be initialized to 0 before the initial entry, afterwards
1626 * the function may be called repeatedly until qlen reaches 0.
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001627 *
1628 * The callback function may return -EINTR to signal that processing
1629 * of netlink messages shall be interrupted. In this case the message
1630 * currently being processed will NOT be requeued onto the receive
1631 * queue.
Thomas Graf82ace472005-11-10 02:25:53 +01001632 */
1633void netlink_run_queue(struct sock *sk, unsigned int *qlen,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001634 int (*cb)(struct sk_buff *, struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001635{
1636 struct sk_buff *skb;
1637
1638 if (!*qlen || *qlen > skb_queue_len(&sk->sk_receive_queue))
1639 *qlen = skb_queue_len(&sk->sk_receive_queue);
1640
1641 for (; *qlen; (*qlen)--) {
1642 skb = skb_dequeue(&sk->sk_receive_queue);
1643 if (netlink_rcv_skb(skb, cb)) {
1644 if (skb->len)
1645 skb_queue_head(&sk->sk_receive_queue, skb);
1646 else {
1647 kfree_skb(skb);
1648 (*qlen)--;
1649 }
1650 break;
1651 }
1652
1653 kfree_skb(skb);
1654 }
1655}
1656
1657/**
1658 * netlink_queue_skip - Skip netlink message while processing queue.
1659 * @nlh: Netlink message to be skipped
1660 * @skb: Socket buffer containing the netlink messages.
1661 *
1662 * Pulls the given netlink message off the socket buffer so the next
1663 * call to netlink_queue_run() will not reconsider the message.
1664 */
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001665static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb)
Thomas Graf82ace472005-11-10 02:25:53 +01001666{
1667 int msglen = NLMSG_ALIGN(nlh->nlmsg_len);
1668
1669 if (msglen > skb->len)
1670 msglen = skb->len;
1671
1672 skb_pull(skb, msglen);
1673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Thomas Grafd387f6a2006-08-15 00:31:06 -07001675/**
1676 * nlmsg_notify - send a notification netlink message
1677 * @sk: netlink socket to use
1678 * @skb: notification message
1679 * @pid: destination netlink pid for reports or 0
1680 * @group: destination multicast group or 0
1681 * @report: 1 to report back, 0 to disable
1682 * @flags: allocation flags
1683 */
1684int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
1685 unsigned int group, int report, gfp_t flags)
1686{
1687 int err = 0;
1688
1689 if (group) {
1690 int exclude_pid = 0;
1691
1692 if (report) {
1693 atomic_inc(&skb->users);
1694 exclude_pid = pid;
1695 }
1696
1697 /* errors reported via destination sk->sk_err */
1698 nlmsg_multicast(sk, skb, exclude_pid, group, flags);
1699 }
1700
1701 if (report)
1702 err = nlmsg_unicast(sk, skb, pid);
1703
1704 return err;
1705}
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707#ifdef CONFIG_PROC_FS
1708struct nl_seq_iter {
1709 int link;
1710 int hash_idx;
1711};
1712
1713static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1714{
1715 struct nl_seq_iter *iter = seq->private;
1716 int i, j;
1717 struct sock *s;
1718 struct hlist_node *node;
1719 loff_t off = 0;
1720
1721 for (i=0; i<MAX_LINKS; i++) {
1722 struct nl_pid_hash *hash = &nl_table[i].hash;
1723
1724 for (j = 0; j <= hash->mask; j++) {
1725 sk_for_each(s, node, &hash->table[j]) {
1726 if (off == pos) {
1727 iter->link = i;
1728 iter->hash_idx = j;
1729 return s;
1730 }
1731 ++off;
1732 }
1733 }
1734 }
1735 return NULL;
1736}
1737
1738static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1739{
1740 read_lock(&nl_table_lock);
1741 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1742}
1743
1744static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1745{
1746 struct sock *s;
1747 struct nl_seq_iter *iter;
1748 int i, j;
1749
1750 ++*pos;
1751
1752 if (v == SEQ_START_TOKEN)
1753 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 s = sk_next(v);
1756 if (s)
1757 return s;
1758
1759 iter = seq->private;
1760 i = iter->link;
1761 j = iter->hash_idx + 1;
1762
1763 do {
1764 struct nl_pid_hash *hash = &nl_table[i].hash;
1765
1766 for (; j <= hash->mask; j++) {
1767 s = sk_head(&hash->table[j]);
1768 if (s) {
1769 iter->link = i;
1770 iter->hash_idx = j;
1771 return s;
1772 }
1773 }
1774
1775 j = 0;
1776 } while (++i < MAX_LINKS);
1777
1778 return NULL;
1779}
1780
1781static void netlink_seq_stop(struct seq_file *seq, void *v)
1782{
1783 read_unlock(&nl_table_lock);
1784}
1785
1786
1787static int netlink_seq_show(struct seq_file *seq, void *v)
1788{
1789 if (v == SEQ_START_TOKEN)
1790 seq_puts(seq,
1791 "sk Eth Pid Groups "
1792 "Rmem Wmem Dump Locks\n");
1793 else {
1794 struct sock *s = v;
1795 struct netlink_sock *nlk = nlk_sk(s);
1796
1797 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1798 s,
1799 s->sk_protocol,
1800 nlk->pid,
Patrick McHardy513c2502005-09-06 15:43:59 -07001801 nlk->groups ? (u32)nlk->groups[0] : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 atomic_read(&s->sk_rmem_alloc),
1803 atomic_read(&s->sk_wmem_alloc),
1804 nlk->cb,
1805 atomic_read(&s->sk_refcnt)
1806 );
1807
1808 }
1809 return 0;
1810}
1811
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001812static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 .start = netlink_seq_start,
1814 .next = netlink_seq_next,
1815 .stop = netlink_seq_stop,
1816 .show = netlink_seq_show,
1817};
1818
1819
1820static int netlink_seq_open(struct inode *inode, struct file *file)
1821{
1822 struct seq_file *seq;
1823 struct nl_seq_iter *iter;
1824 int err;
1825
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001826 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (!iter)
1828 return -ENOMEM;
1829
1830 err = seq_open(file, &netlink_seq_ops);
1831 if (err) {
1832 kfree(iter);
1833 return err;
1834 }
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 seq = file->private_data;
1837 seq->private = iter;
1838 return 0;
1839}
1840
Arjan van de Venda7071d2007-02-12 00:55:36 -08001841static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 .owner = THIS_MODULE,
1843 .open = netlink_seq_open,
1844 .read = seq_read,
1845 .llseek = seq_lseek,
1846 .release = seq_release_private,
1847};
1848
1849#endif
1850
1851int netlink_register_notifier(struct notifier_block *nb)
1852{
Alan Sterne041c682006-03-27 01:16:30 -08001853 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856int netlink_unregister_notifier(struct notifier_block *nb)
1857{
Alan Sterne041c682006-03-27 01:16:30 -08001858 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001860
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001861static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 .family = PF_NETLINK,
1863 .owner = THIS_MODULE,
1864 .release = netlink_release,
1865 .bind = netlink_bind,
1866 .connect = netlink_connect,
1867 .socketpair = sock_no_socketpair,
1868 .accept = sock_no_accept,
1869 .getname = netlink_getname,
1870 .poll = datagram_poll,
1871 .ioctl = sock_no_ioctl,
1872 .listen = sock_no_listen,
1873 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001874 .setsockopt = netlink_setsockopt,
1875 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 .sendmsg = netlink_sendmsg,
1877 .recvmsg = netlink_recvmsg,
1878 .mmap = sock_no_mmap,
1879 .sendpage = sock_no_sendpage,
1880};
1881
1882static struct net_proto_family netlink_family_ops = {
1883 .family = PF_NETLINK,
1884 .create = netlink_create,
1885 .owner = THIS_MODULE, /* for consistency 8) */
1886};
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888static int __init netlink_proto_init(void)
1889{
1890 struct sk_buff *dummy_skb;
1891 int i;
1892 unsigned long max;
1893 unsigned int order;
1894 int err = proto_register(&netlink_proto, 0);
1895
1896 if (err != 0)
1897 goto out;
1898
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07001899 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001901 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001902 if (!nl_table)
1903 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (num_physpages >= (128 * 1024))
1906 max = num_physpages >> (21 - PAGE_SHIFT);
1907 else
1908 max = num_physpages >> (23 - PAGE_SHIFT);
1909
1910 order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1911 max = (1UL << order) / sizeof(struct hlist_head);
1912 order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1913
1914 for (i = 0; i < MAX_LINKS; i++) {
1915 struct nl_pid_hash *hash = &nl_table[i].hash;
1916
1917 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1918 if (!hash->table) {
1919 while (i-- > 0)
1920 nl_pid_hash_free(nl_table[i].hash.table,
1921 1 * sizeof(*hash->table));
1922 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001923 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925 memset(hash->table, 0, 1 * sizeof(*hash->table));
1926 hash->max_shift = order;
1927 hash->shift = 0;
1928 hash->mask = 0;
1929 hash->rehash_time = jiffies;
1930 }
1931
1932 sock_register(&netlink_family_ops);
1933#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001934 proc_net_fops_create(&init_net, "netlink", 0, &netlink_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935#endif
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001936 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 rtnetlink_init();
1938out:
1939 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001940panic:
1941 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944core_initcall(netlink_proto_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946EXPORT_SYMBOL(netlink_ack);
Thomas Graf82ace472005-11-10 02:25:53 +01001947EXPORT_SYMBOL(netlink_run_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948EXPORT_SYMBOL(netlink_broadcast);
1949EXPORT_SYMBOL(netlink_dump_start);
1950EXPORT_SYMBOL(netlink_kernel_create);
1951EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952EXPORT_SYMBOL(netlink_set_nonroot);
1953EXPORT_SYMBOL(netlink_unicast);
1954EXPORT_SYMBOL(netlink_unregister_notifier);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001955EXPORT_SYMBOL(nlmsg_notify);