blob: e57d700bf6d9c6dddac4becdcfc8d5c9526d395a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070057#include <linux/mutex.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010058
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020059#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <net/sock.h>
61#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010062#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070064#define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
Johannes Bergb4ff4f02007-07-18 15:46:06 -070065#define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67struct netlink_sock {
68 /* struct sock has to be the first member of netlink_sock */
69 struct sock sk;
70 u32 pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -070072 u32 dst_group;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070073 u32 flags;
74 u32 subscriptions;
75 u32 ngroups;
76 unsigned long *groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned long state;
78 wait_queue_head_t wait;
79 struct netlink_callback *cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070080 struct mutex *cb_mutex;
81 struct mutex cb_def_mutex;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -070082 void (*netlink_rcv)(struct sk_buff *skb);
Patrick McHardy77247bb2005-08-14 19:27:13 -070083 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Patrick McHardy77247bb2005-08-14 19:27:13 -070086#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070087#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000088#define NETLINK_BROADCAST_SEND_ERROR 0x4
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
Denis V. Lunevaed815602007-10-10 21:14:32 -070095static inline int netlink_is_kernel(struct sock *sk)
96{
97 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100struct nl_pid_hash {
101 struct hlist_head *table;
102 unsigned long rehash_time;
103
104 unsigned int mask;
105 unsigned int shift;
106
107 unsigned int entries;
108 unsigned int max_shift;
109
110 u32 rnd;
111};
112
113struct netlink_table {
114 struct nl_pid_hash hash;
115 struct hlist_head mc_list;
Patrick McHardy4277a082006-03-20 18:52:01 -0800116 unsigned long *listeners;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned int nl_nonroot;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700118 unsigned int groups;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700119 struct mutex *cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700120 struct module *module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700121 int registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124static struct netlink_table *nl_table;
125
126static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
127
128static int netlink_dump(struct sock *sk);
129static void netlink_destroy_callback(struct netlink_callback *cb);
130
131static DEFINE_RWLOCK(nl_table_lock);
132static atomic_t nl_table_users = ATOMIC_INIT(0);
133
Alan Sterne041c682006-03-27 01:16:30 -0800134static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Patrick McHardyd629b832005-08-14 19:27:50 -0700136static u32 netlink_group_mask(u32 group)
137{
138 return group ? 1 << (group - 1) : 0;
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
142{
143 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
144}
145
146static void netlink_sock_destruct(struct sock *sk)
147{
Herbert Xu3f660d62007-05-03 03:17:14 -0700148 struct netlink_sock *nlk = nlk_sk(sk);
149
Herbert Xu3f660d62007-05-03 03:17:14 -0700150 if (nlk->cb) {
151 if (nlk->cb->done)
152 nlk->cb->done(nlk->cb);
153 netlink_destroy_callback(nlk->cb);
154 }
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 skb_queue_purge(&sk->sk_receive_queue);
157
158 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800159 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return;
161 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700162
163 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
164 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
165 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800168/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
169 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
171 * this, _but_ remember, it adds useless work on UP machines.
172 */
173
174static void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800175 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700177 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (atomic_read(&nl_table_users)) {
180 DECLARE_WAITQUEUE(wait, current);
181
182 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800183 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 set_current_state(TASK_UNINTERRUPTIBLE);
185 if (atomic_read(&nl_table_users) == 0)
186 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700187 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700189 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
192 __set_current_state(TASK_RUNNING);
193 remove_wait_queue(&nl_table_wait, &wait);
194 }
195}
196
Ilpo Järvinen3f252522008-01-12 03:21:50 -0800197static void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800198 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700200 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 wake_up(&nl_table_wait);
202}
203
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800204static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205netlink_lock_table(void)
206{
207 /* read_lock() synchronizes us to netlink_table_grab */
208
209 read_lock(&nl_table_lock);
210 atomic_inc(&nl_table_users);
211 read_unlock(&nl_table_lock);
212}
213
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800214static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215netlink_unlock_table(void)
216{
217 if (atomic_dec_and_test(&nl_table_users))
218 wake_up(&nl_table_wait);
219}
220
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800221static inline struct sock *netlink_lookup(struct net *net, int protocol,
222 u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct nl_pid_hash *hash = &nl_table[protocol].hash;
225 struct hlist_head *head;
226 struct sock *sk;
227 struct hlist_node *node;
228
229 read_lock(&nl_table_lock);
230 head = nl_pid_hashfn(hash, pid);
231 sk_for_each(sk, node, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900232 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 sock_hold(sk);
234 goto found;
235 }
236 }
237 sk = NULL;
238found:
239 read_unlock(&nl_table_lock);
240 return sk;
241}
242
Eric Dumazetea729122007-12-11 02:09:47 -0800243static inline struct hlist_head *nl_pid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800246 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 else
248 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800249 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
250 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
254{
255 if (size <= PAGE_SIZE)
256 kfree(table);
257 else
258 free_pages((unsigned long)table, get_order(size));
259}
260
261static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
262{
263 unsigned int omask, mask, shift;
264 size_t osize, size;
265 struct hlist_head *otable, *table;
266 int i;
267
268 omask = mask = hash->mask;
269 osize = size = (mask + 1) * sizeof(*table);
270 shift = hash->shift;
271
272 if (grow) {
273 if (++shift > hash->max_shift)
274 return 0;
275 mask = mask * 2 + 1;
276 size *= 2;
277 }
278
Eric Dumazetea729122007-12-11 02:09:47 -0800279 table = nl_pid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!table)
281 return 0;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 otable = hash->table;
284 hash->table = table;
285 hash->mask = mask;
286 hash->shift = shift;
287 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
288
289 for (i = 0; i <= omask; i++) {
290 struct sock *sk;
291 struct hlist_node *node, *tmp;
292
293 sk_for_each_safe(sk, node, tmp, &otable[i])
294 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
295 }
296
297 nl_pid_hash_free(otable, osize);
298 hash->rehash_time = jiffies + 10 * 60 * HZ;
299 return 1;
300}
301
302static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
303{
304 int avg = hash->entries >> hash->shift;
305
306 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
307 return 1;
308
309 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
310 nl_pid_hash_rehash(hash, 0);
311 return 1;
312 }
313
314 return 0;
315}
316
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800317static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Patrick McHardy4277a082006-03-20 18:52:01 -0800319static void
320netlink_update_listeners(struct sock *sk)
321{
322 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
323 struct hlist_node *node;
324 unsigned long mask;
325 unsigned int i;
326
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700327 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800328 mask = 0;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700329 sk_for_each_bound(sk, node, &tbl->mc_list) {
330 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
331 mask |= nlk_sk(sk)->groups[i];
332 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800333 tbl->listeners[i] = mask;
334 }
335 /* this function is only called with the netlink table "grabbed", which
336 * makes sure updates are visible before bind or setsockopt return. */
337}
338
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200339static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
342 struct hlist_head *head;
343 int err = -EADDRINUSE;
344 struct sock *osk;
345 struct hlist_node *node;
346 int len;
347
348 netlink_table_grab();
349 head = nl_pid_hashfn(hash, pid);
350 len = 0;
351 sk_for_each(osk, node, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900352 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 len++;
355 }
356 if (node)
357 goto err;
358
359 err = -EBUSY;
360 if (nlk_sk(sk)->pid)
361 goto err;
362
363 err = -ENOMEM;
364 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
365 goto err;
366
367 if (len && nl_pid_hash_dilute(hash, len))
368 head = nl_pid_hashfn(hash, pid);
369 hash->entries++;
370 nlk_sk(sk)->pid = pid;
371 sk_add_node(sk, head);
372 err = 0;
373
374err:
375 netlink_table_ungrab();
376 return err;
377}
378
379static void netlink_remove(struct sock *sk)
380{
381 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700382 if (sk_del_node_init(sk))
383 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700384 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 __sk_del_bind_node(sk);
386 netlink_table_ungrab();
387}
388
389static struct proto netlink_proto = {
390 .name = "NETLINK",
391 .owner = THIS_MODULE,
392 .obj_size = sizeof(struct netlink_sock),
393};
394
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700395static int __netlink_create(struct net *net, struct socket *sock,
396 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct sock *sk;
399 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700400
401 sock->ops = &netlink_ops;
402
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700403 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700404 if (!sk)
405 return -ENOMEM;
406
407 sock_init_data(sock, sk);
408
409 nlk = nlk_sk(sk);
Patrick McHardyffa4d722007-04-25 14:01:17 -0700410 if (cb_mutex)
411 nlk->cb_mutex = cb_mutex;
412 else {
413 nlk->cb_mutex = &nlk->cb_def_mutex;
414 mutex_init(nlk->cb_mutex);
415 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700416 init_waitqueue_head(&nlk->wait);
417
418 sk->sk_destruct = netlink_sock_destruct;
419 sk->sk_protocol = protocol;
420 return 0;
421}
422
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700423static int netlink_create(struct net *net, struct socket *sock, int protocol)
Patrick McHardyab33a172005-08-14 19:31:36 -0700424{
425 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700426 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700427 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700428 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 sock->state = SS_UNCONNECTED;
431
432 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
433 return -ESOCKTNOSUPPORT;
434
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800435 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EPROTONOSUPPORT;
437
Patrick McHardy77247bb2005-08-14 19:27:13 -0700438 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700439#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700440 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700441 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700442 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700443 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700444 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700445#endif
446 if (nl_table[protocol].registered &&
447 try_module_get(nl_table[protocol].module))
448 module = nl_table[protocol].module;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700449 cb_mutex = nl_table[protocol].cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700450 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700451
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800452 err = __netlink_create(net, sock, cb_mutex, protocol);
453 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700454 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
David S. Miller6f756a82008-11-23 17:34:03 -0800456 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800457 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800458 local_bh_enable();
459
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700460 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700461 nlk->module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700462out:
463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Patrick McHardyab33a172005-08-14 19:31:36 -0700465out_module:
466 module_put(module);
467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static int netlink_release(struct socket *sock)
471{
472 struct sock *sk = sock->sk;
473 struct netlink_sock *nlk;
474
475 if (!sk)
476 return 0;
477
478 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700479 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 nlk = nlk_sk(sk);
481
Herbert Xu3f660d62007-05-03 03:17:14 -0700482 /*
483 * OK. Socket is unlinked, any packets that arrive now
484 * will be purged.
485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 sock->sk = NULL;
488 wake_up_interruptible_all(&nlk->wait);
489
490 skb_queue_purge(&sk->sk_write_queue);
491
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700492 if (nlk->pid && !nlk->subscriptions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900494 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .protocol = sk->sk_protocol,
496 .pid = nlk->pid,
497 };
Alan Sterne041c682006-03-27 01:16:30 -0800498 atomic_notifier_call_chain(&netlink_chain,
499 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900500 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700501
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800502 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700503
Patrick McHardy4277a082006-03-20 18:52:01 -0800504 netlink_table_grab();
Denis V. Lunevaed815602007-10-10 21:14:32 -0700505 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800506 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
507 if (--nl_table[sk->sk_protocol].registered == 0) {
508 kfree(nl_table[sk->sk_protocol].listeners);
509 nl_table[sk->sk_protocol].module = NULL;
510 nl_table[sk->sk_protocol].registered = 0;
511 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800512 } else if (nlk->subscriptions)
513 netlink_update_listeners(sk);
514 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700515
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700516 kfree(nlk->groups);
517 nlk->groups = NULL;
518
Eric Dumazet37558102008-11-24 14:05:22 -0800519 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800520 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800521 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 sock_put(sk);
523 return 0;
524}
525
526static int netlink_autobind(struct socket *sock)
527{
528 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900529 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
531 struct hlist_head *head;
532 struct sock *osk;
533 struct hlist_node *node;
Herbert Xuc27bd492005-11-22 14:41:50 -0800534 s32 pid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int err;
536 static s32 rover = -4097;
537
538retry:
539 cond_resched();
540 netlink_table_grab();
541 head = nl_pid_hashfn(hash, pid);
542 sk_for_each(osk, node, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900543 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200544 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (nlk_sk(osk)->pid == pid) {
546 /* Bind collision, search negative pid values. */
547 pid = rover--;
548 if (rover > -4097)
549 rover = -4097;
550 netlink_table_ungrab();
551 goto retry;
552 }
553 }
554 netlink_table_ungrab();
555
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200556 err = netlink_insert(sk, net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (err == -EADDRINUSE)
558 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700559
560 /* If 2 threads race to autobind, that is fine. */
561 if (err == -EBUSY)
562 err = 0;
563
564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900567static inline int netlink_capable(struct socket *sock, unsigned int flag)
568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
570 capable(CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900571}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700573static void
574netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
575{
576 struct netlink_sock *nlk = nlk_sk(sk);
577
578 if (nlk->subscriptions && !subscriptions)
579 __sk_del_bind_node(sk);
580 else if (!nlk->subscriptions && subscriptions)
581 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
582 nlk->subscriptions = subscriptions;
583}
584
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700585static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700586{
587 struct netlink_sock *nlk = nlk_sk(sk);
588 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700589 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700590 int err = 0;
591
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700592 netlink_table_grab();
593
Patrick McHardy513c2502005-09-06 15:43:59 -0700594 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700595 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700596 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700597 goto out_unlock;
598 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700599
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700600 if (nlk->ngroups >= groups)
601 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700602
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700603 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
604 if (new_groups == NULL) {
605 err = -ENOMEM;
606 goto out_unlock;
607 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800608 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700609 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
610
611 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700612 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700613 out_unlock:
614 netlink_table_ungrab();
615 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700616}
617
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800618static int netlink_bind(struct socket *sock, struct sockaddr *addr,
619 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900622 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct netlink_sock *nlk = nlk_sk(sk);
624 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
625 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (nladdr->nl_family != AF_NETLINK)
628 return -EINVAL;
629
630 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700631 if (nladdr->nl_groups) {
632 if (!netlink_capable(sock, NL_NONROOT_RECV))
633 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700634 err = netlink_realloc_groups(sk);
635 if (err)
636 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (nlk->pid) {
640 if (nladdr->nl_pid != nlk->pid)
641 return -EINVAL;
642 } else {
643 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200644 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 netlink_autobind(sock);
646 if (err)
647 return err;
648 }
649
Patrick McHardy513c2502005-09-06 15:43:59 -0700650 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652
653 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700654 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900655 hweight32(nladdr->nl_groups) -
656 hweight32(nlk->groups[0]));
657 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800658 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 netlink_table_ungrab();
660
661 return 0;
662}
663
664static int netlink_connect(struct socket *sock, struct sockaddr *addr,
665 int alen, int flags)
666{
667 int err = 0;
668 struct sock *sk = sock->sk;
669 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800670 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (addr->sa_family == AF_UNSPEC) {
673 sk->sk_state = NETLINK_UNCONNECTED;
674 nlk->dst_pid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700675 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
677 }
678 if (addr->sa_family != AF_NETLINK)
679 return -EINVAL;
680
681 /* Only superuser is allowed to send multicasts */
682 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
683 return -EPERM;
684
685 if (!nlk->pid)
686 err = netlink_autobind(sock);
687
688 if (err == 0) {
689 sk->sk_state = NETLINK_CONNECTED;
690 nlk->dst_pid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700691 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
694 return err;
695}
696
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800697static int netlink_getname(struct socket *sock, struct sockaddr *addr,
698 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct sock *sk = sock->sk;
701 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800702 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 nladdr->nl_family = AF_NETLINK;
705 nladdr->nl_pad = 0;
706 *addr_len = sizeof(*nladdr);
707
708 if (peer) {
709 nladdr->nl_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700710 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } else {
712 nladdr->nl_pid = nlk->pid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700713 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 return 0;
716}
717
718static void netlink_overrun(struct sock *sk)
719{
720 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
721 sk->sk_err = ENOBUFS;
722 sk->sk_error_report(sk);
723 }
724}
725
726static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct sock *sock;
729 struct netlink_sock *nlk;
730
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900731 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!sock)
733 return ERR_PTR(-ECONNREFUSED);
734
735 /* Don't bother queuing skb if kernel socket has no input function */
736 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700737 if (sock->sk_state == NETLINK_CONNECTED &&
738 nlk->dst_pid != nlk_sk(ssk)->pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 sock_put(sock);
740 return ERR_PTR(-ECONNREFUSED);
741 }
742 return sock;
743}
744
745struct sock *netlink_getsockbyfilp(struct file *filp)
746{
Josef Sipek6db5fc52006-12-08 02:37:25 -0800747 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct sock *sock;
749
750 if (!S_ISSOCK(inode->i_mode))
751 return ERR_PTR(-ENOTSOCK);
752
753 sock = SOCKET_I(inode)->sk;
754 if (sock->sk_family != AF_NETLINK)
755 return ERR_PTR(-EINVAL);
756
757 sock_hold(sock);
758 return sock;
759}
760
761/*
762 * Attach a skb to a netlink socket.
763 * The caller must hold a reference to the destination socket. On error, the
764 * reference is dropped. The skb is not send to the destination, just all
765 * all error checks are performed and memory in the queue is reserved.
766 * Return values:
767 * < 0: error. skb freed, reference to sock dropped.
768 * 0: continue
769 * 1: repeat lookup - reference dropped while waiting for socket memory.
770 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700771int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800772 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct netlink_sock *nlk;
775
776 nlk = nlk_sk(sk);
777
778 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
779 test_bit(0, &nlk->state)) {
780 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800781 if (!*timeo) {
Denis V. Lunevaed815602007-10-10 21:14:32 -0700782 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 netlink_overrun(sk);
784 sock_put(sk);
785 kfree_skb(skb);
786 return -EAGAIN;
787 }
788
789 __set_current_state(TASK_INTERRUPTIBLE);
790 add_wait_queue(&nlk->wait, &wait);
791
792 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
793 test_bit(0, &nlk->state)) &&
794 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800795 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 __set_current_state(TASK_RUNNING);
798 remove_wait_queue(&nlk->wait, &wait);
799 sock_put(sk);
800
801 if (signal_pending(current)) {
802 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800803 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 return 1;
806 }
807 skb_set_owner_r(skb, sk);
808 return 0;
809}
810
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700811int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int len = skb->len;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 skb_queue_tail(&sk->sk_receive_queue, skb);
816 sk->sk_data_ready(sk, len);
817 sock_put(sk);
818 return len;
819}
820
821void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
822{
823 kfree_skb(skb);
824 sock_put(sk);
825}
826
Victor Fusco37da6472005-07-18 13:35:43 -0700827static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +0100828 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 int delta;
831
832 skb_orphan(skb);
833
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700834 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (delta * 2 < skb->truesize)
836 return skb;
837
838 if (skb_shared(skb)) {
839 struct sk_buff *nskb = skb_clone(skb, allocation);
840 if (!nskb)
841 return skb;
842 kfree_skb(skb);
843 skb = nskb;
844 }
845
846 if (!pskb_expand_head(skb, 0, -delta, allocation))
847 skb->truesize -= delta;
848
849 return skb;
850}
851
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700852static inline void netlink_rcv_wake(struct sock *sk)
853{
854 struct netlink_sock *nlk = nlk_sk(sk);
855
856 if (skb_queue_empty(&sk->sk_receive_queue))
857 clear_bit(0, &nlk->state);
858 if (!test_bit(0, &nlk->state))
859 wake_up_interruptible(&nlk->wait);
860}
861
862static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
863{
864 int ret;
865 struct netlink_sock *nlk = nlk_sk(sk);
866
867 ret = -ECONNREFUSED;
868 if (nlk->netlink_rcv != NULL) {
869 ret = skb->len;
870 skb_set_owner_r(skb, sk);
871 nlk->netlink_rcv(skb);
872 }
873 kfree_skb(skb);
874 sock_put(sk);
875 return ret;
876}
877
878int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
879 u32 pid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct sock *sk;
882 int err;
883 long timeo;
884
885 skb = netlink_trim(skb, gfp_any());
886
887 timeo = sock_sndtimeo(ssk, nonblock);
888retry:
889 sk = netlink_getsockbypid(ssk, pid);
890 if (IS_ERR(sk)) {
891 kfree_skb(skb);
892 return PTR_ERR(sk);
893 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700894 if (netlink_is_kernel(sk))
895 return netlink_unicast_kernel(sk, skb);
896
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700897 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -0700898 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700899 kfree_skb(skb);
900 sock_put(sk);
901 return err;
902 }
903
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700904 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (err == 1)
906 goto retry;
907 if (err)
908 return err;
909
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700910 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800912EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Patrick McHardy4277a082006-03-20 18:52:01 -0800914int netlink_has_listeners(struct sock *sk, unsigned int group)
915{
916 int res = 0;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700917 unsigned long *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -0800918
Denis V. Lunevaed815602007-10-10 21:14:32 -0700919 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700920
921 rcu_read_lock();
922 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
923
Patrick McHardy4277a082006-03-20 18:52:01 -0800924 if (group - 1 < nl_table[sk->sk_protocol].groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700925 res = test_bit(group - 1, listeners);
926
927 rcu_read_unlock();
928
Patrick McHardy4277a082006-03-20 18:52:01 -0800929 return res;
930}
931EXPORT_SYMBOL_GPL(netlink_has_listeners);
932
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800933static inline int netlink_broadcast_deliver(struct sock *sk,
934 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct netlink_sock *nlk = nlk_sk(sk);
937
938 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
939 !test_bit(0, &nlk->state)) {
940 skb_set_owner_r(skb, sk);
941 skb_queue_tail(&sk->sk_receive_queue, skb);
942 sk->sk_data_ready(sk, skb->len);
943 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
944 }
945 return -1;
946}
947
948struct netlink_broadcast_data {
949 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200950 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 u32 pid;
952 u32 group;
953 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800954 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int congested;
956 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -0400957 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct sk_buff *skb, *skb2;
959};
960
961static inline int do_one_broadcast(struct sock *sk,
962 struct netlink_broadcast_data *p)
963{
964 struct netlink_sock *nlk = nlk_sk(sk);
965 int val;
966
967 if (p->exclude_sk == sk)
968 goto out;
969
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700970 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
971 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 goto out;
973
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900974 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200975 goto out;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (p->failure) {
978 netlink_overrun(sk);
979 goto out;
980 }
981
982 sock_hold(sk);
983 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700984 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 p->skb2 = skb_clone(p->skb, p->allocation);
986 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -0700987 p->skb2 = skb_get(p->skb);
988 /*
989 * skb ownership may have been set when
990 * delivered to a previous socket.
991 */
992 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 }
995 if (p->skb2 == NULL) {
996 netlink_overrun(sk);
997 /* Clone failed. Notify ALL listeners. */
998 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +0000999 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1000 p->delivery_failure = 1;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001001 } else if (sk_filter(sk, p->skb2)) {
1002 kfree_skb(p->skb2);
1003 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1005 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001006 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1007 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else {
1009 p->congested |= val;
1010 p->delivered = 1;
1011 p->skb2 = NULL;
1012 }
1013 sock_put(sk);
1014
1015out:
1016 return 0;
1017}
1018
1019int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
Al Virodd0fc662005-10-07 07:46:04 +01001020 u32 group, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001022 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct netlink_broadcast_data info;
1024 struct hlist_node *node;
1025 struct sock *sk;
1026
1027 skb = netlink_trim(skb, allocation);
1028
1029 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001030 info.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 info.pid = pid;
1032 info.group = group;
1033 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001034 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 info.congested = 0;
1036 info.delivered = 0;
1037 info.allocation = allocation;
1038 info.skb = skb;
1039 info.skb2 = NULL;
1040
1041 /* While we sleep in clone, do not allow to change socket list */
1042
1043 netlink_lock_table();
1044
1045 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1046 do_one_broadcast(sk, &info);
1047
Tommy S. Christensenaa1c6a6f2005-05-19 13:07:32 -07001048 kfree_skb(skb);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 netlink_unlock_table();
1051
Wei Yongjun91744f62009-02-25 00:34:41 +00001052 kfree_skb(info.skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001054 if (info.delivery_failure)
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001055 return -ENOBUFS;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (info.delivered) {
1058 if (info.congested && (allocation & __GFP_WAIT))
1059 yield();
1060 return 0;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -ESRCH;
1063}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001064EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066struct netlink_set_err_data {
1067 struct sock *exclude_sk;
1068 u32 pid;
1069 u32 group;
1070 int code;
1071};
1072
1073static inline int do_one_set_err(struct sock *sk,
1074 struct netlink_set_err_data *p)
1075{
1076 struct netlink_sock *nlk = nlk_sk(sk);
1077
1078 if (sk == p->exclude_sk)
1079 goto out;
1080
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001081 if (sock_net(sk) != sock_net(p->exclude_sk))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001082 goto out;
1083
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001084 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1085 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto out;
1087
1088 sk->sk_err = p->code;
1089 sk->sk_error_report(sk);
1090out:
1091 return 0;
1092}
1093
1094void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
1095{
1096 struct netlink_set_err_data info;
1097 struct hlist_node *node;
1098 struct sock *sk;
1099
1100 info.exclude_sk = ssk;
1101 info.pid = pid;
1102 info.group = group;
1103 info.code = code;
1104
1105 read_lock(&nl_table_lock);
1106
1107 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1108 do_one_set_err(sk, &info);
1109
1110 read_unlock(&nl_table_lock);
1111}
1112
Johannes Berg84659eb2007-07-18 15:47:05 -07001113/* must be called with netlink table grabbed */
1114static void netlink_update_socket_mc(struct netlink_sock *nlk,
1115 unsigned int group,
1116 int is_new)
1117{
1118 int old, new = !!is_new, subscriptions;
1119
1120 old = test_bit(group - 1, nlk->groups);
1121 subscriptions = nlk->subscriptions - old + new;
1122 if (new)
1123 __set_bit(group - 1, nlk->groups);
1124 else
1125 __clear_bit(group - 1, nlk->groups);
1126 netlink_update_subscriptions(&nlk->sk, subscriptions);
1127 netlink_update_listeners(&nlk->sk);
1128}
1129
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001130static int netlink_setsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001131 char __user *optval, int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001132{
1133 struct sock *sk = sock->sk;
1134 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001135 unsigned int val = 0;
1136 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001137
1138 if (level != SOL_NETLINK)
1139 return -ENOPROTOOPT;
1140
1141 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001142 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001143 return -EFAULT;
1144
1145 switch (optname) {
1146 case NETLINK_PKTINFO:
1147 if (val)
1148 nlk->flags |= NETLINK_RECV_PKTINFO;
1149 else
1150 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1151 err = 0;
1152 break;
1153 case NETLINK_ADD_MEMBERSHIP:
1154 case NETLINK_DROP_MEMBERSHIP: {
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001155 if (!netlink_capable(sock, NL_NONROOT_RECV))
1156 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001157 err = netlink_realloc_groups(sk);
1158 if (err)
1159 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001160 if (!val || val - 1 >= nlk->ngroups)
1161 return -EINVAL;
1162 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001163 netlink_update_socket_mc(nlk, val,
1164 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001165 netlink_table_ungrab();
1166 err = 0;
1167 break;
1168 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001169 case NETLINK_BROADCAST_ERROR:
1170 if (val)
1171 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1172 else
1173 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1174 err = 0;
1175 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001176 default:
1177 err = -ENOPROTOOPT;
1178 }
1179 return err;
1180}
1181
1182static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001183 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001184{
1185 struct sock *sk = sock->sk;
1186 struct netlink_sock *nlk = nlk_sk(sk);
1187 int len, val, err;
1188
1189 if (level != SOL_NETLINK)
1190 return -ENOPROTOOPT;
1191
1192 if (get_user(len, optlen))
1193 return -EFAULT;
1194 if (len < 0)
1195 return -EINVAL;
1196
1197 switch (optname) {
1198 case NETLINK_PKTINFO:
1199 if (len < sizeof(int))
1200 return -EINVAL;
1201 len = sizeof(int);
1202 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001203 if (put_user(len, optlen) ||
1204 put_user(val, optval))
1205 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001206 err = 0;
1207 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001208 case NETLINK_BROADCAST_ERROR:
1209 if (len < sizeof(int))
1210 return -EINVAL;
1211 len = sizeof(int);
1212 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1213 if (put_user(len, optlen) ||
1214 put_user(val, optval))
1215 return -EFAULT;
1216 err = 0;
1217 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001218 default:
1219 err = -ENOPROTOOPT;
1220 }
1221 return err;
1222}
1223
1224static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1225{
1226 struct nl_pktinfo info;
1227
1228 info.group = NETLINK_CB(skb).dst_group;
1229 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1233 struct msghdr *msg, size_t len)
1234{
1235 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1236 struct sock *sk = sock->sk;
1237 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001238 struct sockaddr_nl *addr = msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001240 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct sk_buff *skb;
1242 int err;
1243 struct scm_cookie scm;
1244
1245 if (msg->msg_flags&MSG_OOB)
1246 return -EOPNOTSUPP;
1247
1248 if (NULL == siocb->scm)
1249 siocb->scm = &scm;
1250 err = scm_send(sock, msg, siocb->scm);
1251 if (err < 0)
1252 return err;
1253
1254 if (msg->msg_namelen) {
1255 if (addr->nl_family != AF_NETLINK)
1256 return -EINVAL;
1257 dst_pid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001258 dst_group = ffs(addr->nl_groups);
1259 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return -EPERM;
1261 } else {
1262 dst_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001263 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 if (!nlk->pid) {
1267 err = netlink_autobind(sock);
1268 if (err)
1269 goto out;
1270 }
1271
1272 err = -EMSGSIZE;
1273 if (len > sk->sk_sndbuf - 32)
1274 goto out;
1275 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001276 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001277 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 goto out;
1279
1280 NETLINK_CB(skb).pid = nlk->pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001281 NETLINK_CB(skb).dst_group = dst_group;
Al Viro0c11b942008-01-10 04:20:52 -05001282 NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001283 NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
Ahmed S. Darwish0ce784c2008-03-01 21:56:22 +02001284 security_task_getsecid(current, &(NETLINK_CB(skb).sid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1286
1287 /* What can I do? Netlink is asynchronous, so that
1288 we will have to save current capabilities to
1289 check them, when this message will be delivered
1290 to corresponding kernel module. --ANK (980802)
1291 */
1292
1293 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001294 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 kfree_skb(skb);
1296 goto out;
1297 }
1298
1299 err = security_netlink_send(sk, skb);
1300 if (err) {
1301 kfree_skb(skb);
1302 goto out;
1303 }
1304
Patrick McHardyd629b832005-08-14 19:27:50 -07001305 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 atomic_inc(&skb->users);
Patrick McHardyd629b832005-08-14 19:27:50 -07001307 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1310
1311out:
1312 return err;
1313}
1314
1315static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1316 struct msghdr *msg, size_t len,
1317 int flags)
1318{
1319 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1320 struct scm_cookie scm;
1321 struct sock *sk = sock->sk;
1322 struct netlink_sock *nlk = nlk_sk(sk);
1323 int noblock = flags&MSG_DONTWAIT;
1324 size_t copied;
1325 struct sk_buff *skb;
1326 int err;
1327
1328 if (flags&MSG_OOB)
1329 return -EOPNOTSUPP;
1330
1331 copied = 0;
1332
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001333 skb = skb_recv_datagram(sk, flags, noblock, &err);
1334 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto out;
1336
1337 msg->msg_namelen = 0;
1338
1339 copied = skb->len;
1340 if (len < copied) {
1341 msg->msg_flags |= MSG_TRUNC;
1342 copied = len;
1343 }
1344
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001345 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1347
1348 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001349 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 addr->nl_family = AF_NETLINK;
1351 addr->nl_pad = 0;
1352 addr->nl_pid = NETLINK_CB(skb).pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001353 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 msg->msg_namelen = sizeof(*addr);
1355 }
1356
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001357 if (nlk->flags & NETLINK_RECV_PKTINFO)
1358 netlink_cmsg_recv_pktinfo(msg, skb);
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (NULL == siocb->scm) {
1361 memset(&scm, 0, sizeof(scm));
1362 siocb->scm = &scm;
1363 }
1364 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001365 if (flags & MSG_TRUNC)
1366 copied = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 skb_free_datagram(sk, skb);
1368
1369 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1370 netlink_dump(sk);
1371
1372 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373out:
1374 netlink_rcv_wake(sk);
1375 return err ? : copied;
1376}
1377
1378static void netlink_data_ready(struct sock *sk, int len)
1379{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001380 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001384 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * complete set of kernel non-blocking support for message
1386 * queueing.
1387 */
1388
1389struct sock *
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001390netlink_kernel_create(struct net *net, int unit, unsigned int groups,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001391 void (*input)(struct sk_buff *skb),
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001392 struct mutex *cb_mutex, struct module *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct socket *sock;
1395 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001396 struct netlink_sock *nlk;
Patrick McHardy4277a082006-03-20 18:52:01 -08001397 unsigned long *listeners = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001399 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001401 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return NULL;
1403
1404 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1405 return NULL;
1406
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001407 /*
1408 * We have to just have a reference on the net from sk, but don't
1409 * get_net it. Besides, we cannot get and then put the net here.
1410 * So we create one inside init_net and the move it to net.
1411 */
1412
1413 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1414 goto out_sock_release_nosk;
1415
1416 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08001417 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001418
Patrick McHardy4277a082006-03-20 18:52:01 -08001419 if (groups < 32)
1420 groups = 32;
1421
1422 listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
1423 if (!listeners)
1424 goto out_sock_release;
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 sk->sk_data_ready = netlink_data_ready;
1427 if (input)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001428 nlk_sk(sk)->netlink_rcv = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001430 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001431 goto out_sock_release;
1432
1433 nlk = nlk_sk(sk);
1434 nlk->flags |= NETLINK_KERNEL_SOCKET;
1435
1436 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001437 if (!nl_table[unit].registered) {
1438 nl_table[unit].groups = groups;
1439 nl_table[unit].listeners = listeners;
1440 nl_table[unit].cb_mutex = cb_mutex;
1441 nl_table[unit].module = module;
1442 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07001443 } else {
1444 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001445 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001446 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07001447 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001448 return sk;
1449
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001450out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001451 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08001452 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001453 return NULL;
1454
1455out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001456 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001457 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001459EXPORT_SYMBOL(netlink_kernel_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001461
1462void
1463netlink_kernel_release(struct sock *sk)
1464{
Denis V. Lunevedf02082008-02-29 11:18:32 -08001465 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001466}
1467EXPORT_SYMBOL(netlink_kernel_release);
1468
1469
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001470/**
1471 * netlink_change_ngroups - change number of multicast groups
1472 *
1473 * This changes the number of multicast groups that are available
1474 * on a certain netlink family. Note that it is not possible to
Johannes Berg84659eb2007-07-18 15:47:05 -07001475 * change the number of groups to below 32. Also note that it does
1476 * not implicitly call netlink_clear_multicast_users() when the
1477 * number of groups is reduced.
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001478 *
1479 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1480 * @groups: The new number of groups.
1481 */
1482int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1483{
1484 unsigned long *listeners, *old = NULL;
1485 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1486 int err = 0;
1487
1488 if (groups < 32)
1489 groups = 32;
1490
1491 netlink_table_grab();
1492 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1493 listeners = kzalloc(NLGRPSZ(groups), GFP_ATOMIC);
1494 if (!listeners) {
1495 err = -ENOMEM;
1496 goto out_ungrab;
1497 }
1498 old = tbl->listeners;
1499 memcpy(listeners, old, NLGRPSZ(tbl->groups));
1500 rcu_assign_pointer(tbl->listeners, listeners);
1501 }
1502 tbl->groups = groups;
1503
1504 out_ungrab:
1505 netlink_table_ungrab();
1506 synchronize_rcu();
1507 kfree(old);
1508 return err;
1509}
1510EXPORT_SYMBOL(netlink_change_ngroups);
1511
Johannes Berg84659eb2007-07-18 15:47:05 -07001512/**
1513 * netlink_clear_multicast_users - kick off multicast listeners
1514 *
1515 * This function removes all listeners from the given group.
1516 * @ksk: The kernel netlink socket, as returned by
1517 * netlink_kernel_create().
1518 * @group: The multicast group to clear.
1519 */
1520void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1521{
1522 struct sock *sk;
1523 struct hlist_node *node;
1524 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1525
1526 netlink_table_grab();
1527
1528 sk_for_each_bound(sk, node, &tbl->mc_list)
1529 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1530
1531 netlink_table_ungrab();
1532}
1533EXPORT_SYMBOL(netlink_clear_multicast_users);
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535void netlink_set_nonroot(int protocol, unsigned int flags)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001536{
1537 if ((unsigned int)protocol < MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 nl_table[protocol].nl_nonroot = flags;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001539}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001540EXPORT_SYMBOL(netlink_set_nonroot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542static void netlink_destroy_callback(struct netlink_callback *cb)
1543{
Wei Yongjun91744f62009-02-25 00:34:41 +00001544 kfree_skb(cb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 kfree(cb);
1546}
1547
1548/*
1549 * It looks a bit ugly.
1550 * It would be better to create kernel thread.
1551 */
1552
1553static int netlink_dump(struct sock *sk)
1554{
1555 struct netlink_sock *nlk = nlk_sk(sk);
1556 struct netlink_callback *cb;
1557 struct sk_buff *skb;
1558 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001559 int len, err = -ENOBUFS;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1562 if (!skb)
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001563 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001565 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 cb = nlk->cb;
1568 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001569 err = -EINVAL;
1570 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
1573 len = cb->dump(skb, cb);
1574
1575 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001576 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001577
1578 if (sk_filter(sk, skb))
1579 kfree_skb(skb);
1580 else {
1581 skb_queue_tail(&sk->sk_receive_queue, skb);
1582 sk->sk_data_ready(sk, skb->len);
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return 0;
1585 }
1586
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001587 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1588 if (!nlh)
1589 goto errout_skb;
1590
1591 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1592
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001593 if (sk_filter(sk, skb))
1594 kfree_skb(skb);
1595 else {
1596 skb_queue_tail(&sk->sk_receive_queue, skb);
1597 sk->sk_data_ready(sk, skb->len);
1598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Thomas Grafa8f74b22005-11-10 02:25:52 +01001600 if (cb->done)
1601 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001603 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 netlink_destroy_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001607
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001608errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001609 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001610 kfree_skb(skb);
1611errout:
1612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
1615int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1616 struct nlmsghdr *nlh,
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001617 int (*dump)(struct sk_buff *skb,
1618 struct netlink_callback *),
1619 int (*done)(struct netlink_callback *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 struct netlink_callback *cb;
1622 struct sock *sk;
1623 struct netlink_sock *nlk;
1624
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001625 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (cb == NULL)
1627 return -ENOBUFS;
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 cb->dump = dump;
1630 cb->done = done;
1631 cb->nlh = nlh;
1632 atomic_inc(&skb->users);
1633 cb->skb = skb;
1634
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001635 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (sk == NULL) {
1637 netlink_destroy_callback(cb);
1638 return -ECONNREFUSED;
1639 }
1640 nlk = nlk_sk(sk);
Herbert Xu3f660d62007-05-03 03:17:14 -07001641 /* A dump is in progress... */
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001642 mutex_lock(nlk->cb_mutex);
Herbert Xu3f660d62007-05-03 03:17:14 -07001643 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001644 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 netlink_destroy_callback(cb);
1646 sock_put(sk);
1647 return -EBUSY;
1648 }
1649 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001650 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 netlink_dump(sk);
1653 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001654
1655 /* We successfully started a dump, by returning -EINTR we
1656 * signal not to send ACK even if it was requested.
1657 */
1658 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001660EXPORT_SYMBOL(netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1663{
1664 struct sk_buff *skb;
1665 struct nlmsghdr *rep;
1666 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08001667 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Thomas Graf339bf982006-11-10 14:10:15 -08001669 /* error messages get the original request appened */
1670 if (err)
1671 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Thomas Graf339bf982006-11-10 14:10:15 -08001673 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (!skb) {
1675 struct sock *sk;
1676
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001677 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001678 in_skb->sk->sk_protocol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 NETLINK_CB(in_skb).pid);
1680 if (sk) {
1681 sk->sk_err = ENOBUFS;
1682 sk->sk_error_report(sk);
1683 sock_put(sk);
1684 }
1685 return;
1686 }
1687
1688 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
Thomas Graf17977542005-06-18 22:53:48 -07001689 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001690 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001692 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1694}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001695EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001697int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001698 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001699{
Thomas Graf82ace472005-11-10 02:25:53 +01001700 struct nlmsghdr *nlh;
1701 int err;
1702
1703 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001704 int msglen;
1705
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001706 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07001707 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01001708
Martin Murrayad8e4b72006-01-10 13:02:29 -08001709 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01001710 return 0;
1711
Thomas Grafd35b6852007-03-22 23:28:46 -07001712 /* Only requests are handled by the kernel */
1713 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07001714 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07001715
Thomas Graf45e7ae72007-03-22 23:29:10 -07001716 /* Skip control messages */
1717 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07001718 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07001719
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001720 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001721 if (err == -EINTR)
1722 goto skip;
1723
1724ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07001725 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01001726 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01001727
Denis V. Lunev5c582982007-10-23 20:29:25 -07001728skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001729 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001730 if (msglen > skb->len)
1731 msglen = skb->len;
1732 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01001733 }
1734
1735 return 0;
1736}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001737EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01001738
1739/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07001740 * nlmsg_notify - send a notification netlink message
1741 * @sk: netlink socket to use
1742 * @skb: notification message
1743 * @pid: destination netlink pid for reports or 0
1744 * @group: destination multicast group or 0
1745 * @report: 1 to report back, 0 to disable
1746 * @flags: allocation flags
1747 */
1748int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
1749 unsigned int group, int report, gfp_t flags)
1750{
1751 int err = 0;
1752
1753 if (group) {
1754 int exclude_pid = 0;
1755
1756 if (report) {
1757 atomic_inc(&skb->users);
1758 exclude_pid = pid;
1759 }
1760
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001761 /* errors reported via destination sk->sk_err, but propagate
1762 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
1763 err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001764 }
1765
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001766 if (report) {
1767 int err2;
1768
1769 err2 = nlmsg_unicast(sk, skb, pid);
1770 if (!err || err == -ESRCH)
1771 err = err2;
1772 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07001773
1774 return err;
1775}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001776EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778#ifdef CONFIG_PROC_FS
1779struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08001780 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 int link;
1782 int hash_idx;
1783};
1784
1785static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1786{
1787 struct nl_seq_iter *iter = seq->private;
1788 int i, j;
1789 struct sock *s;
1790 struct hlist_node *node;
1791 loff_t off = 0;
1792
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001793 for (i = 0; i < MAX_LINKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 struct nl_pid_hash *hash = &nl_table[i].hash;
1795
1796 for (j = 0; j <= hash->mask; j++) {
1797 sk_for_each(s, node, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001798 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001799 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (off == pos) {
1801 iter->link = i;
1802 iter->hash_idx = j;
1803 return s;
1804 }
1805 ++off;
1806 }
1807 }
1808 }
1809 return NULL;
1810}
1811
1812static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001813 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 read_lock(&nl_table_lock);
1816 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1817}
1818
1819static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1820{
1821 struct sock *s;
1822 struct nl_seq_iter *iter;
1823 int i, j;
1824
1825 ++*pos;
1826
1827 if (v == SEQ_START_TOKEN)
1828 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001829
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001830 iter = seq->private;
1831 s = v;
1832 do {
1833 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001834 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (s)
1836 return s;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 i = iter->link;
1839 j = iter->hash_idx + 1;
1840
1841 do {
1842 struct nl_pid_hash *hash = &nl_table[i].hash;
1843
1844 for (; j <= hash->mask; j++) {
1845 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001846 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001847 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (s) {
1849 iter->link = i;
1850 iter->hash_idx = j;
1851 return s;
1852 }
1853 }
1854
1855 j = 0;
1856 } while (++i < MAX_LINKS);
1857
1858 return NULL;
1859}
1860
1861static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001862 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 read_unlock(&nl_table_lock);
1865}
1866
1867
1868static int netlink_seq_show(struct seq_file *seq, void *v)
1869{
1870 if (v == SEQ_START_TOKEN)
1871 seq_puts(seq,
1872 "sk Eth Pid Groups "
1873 "Rmem Wmem Dump Locks\n");
1874 else {
1875 struct sock *s = v;
1876 struct netlink_sock *nlk = nlk_sk(s);
1877
1878 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1879 s,
1880 s->sk_protocol,
1881 nlk->pid,
Patrick McHardy513c2502005-09-06 15:43:59 -07001882 nlk->groups ? (u32)nlk->groups[0] : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 atomic_read(&s->sk_rmem_alloc),
1884 atomic_read(&s->sk_wmem_alloc),
1885 nlk->cb,
1886 atomic_read(&s->sk_refcnt)
1887 );
1888
1889 }
1890 return 0;
1891}
1892
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001893static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 .start = netlink_seq_start,
1895 .next = netlink_seq_next,
1896 .stop = netlink_seq_stop,
1897 .show = netlink_seq_show,
1898};
1899
1900
1901static int netlink_seq_open(struct inode *inode, struct file *file)
1902{
Denis V. Luneve372c412007-11-19 22:31:54 -08001903 return seq_open_net(inode, file, &netlink_seq_ops,
1904 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001905}
1906
Arjan van de Venda7071d2007-02-12 00:55:36 -08001907static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 .owner = THIS_MODULE,
1909 .open = netlink_seq_open,
1910 .read = seq_read,
1911 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08001912 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913};
1914
1915#endif
1916
1917int netlink_register_notifier(struct notifier_block *nb)
1918{
Alan Sterne041c682006-03-27 01:16:30 -08001919 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001921EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923int netlink_unregister_notifier(struct notifier_block *nb)
1924{
Alan Sterne041c682006-03-27 01:16:30 -08001925 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001927EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001928
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001929static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 .family = PF_NETLINK,
1931 .owner = THIS_MODULE,
1932 .release = netlink_release,
1933 .bind = netlink_bind,
1934 .connect = netlink_connect,
1935 .socketpair = sock_no_socketpair,
1936 .accept = sock_no_accept,
1937 .getname = netlink_getname,
1938 .poll = datagram_poll,
1939 .ioctl = sock_no_ioctl,
1940 .listen = sock_no_listen,
1941 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001942 .setsockopt = netlink_setsockopt,
1943 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 .sendmsg = netlink_sendmsg,
1945 .recvmsg = netlink_recvmsg,
1946 .mmap = sock_no_mmap,
1947 .sendpage = sock_no_sendpage,
1948};
1949
1950static struct net_proto_family netlink_family_ops = {
1951 .family = PF_NETLINK,
1952 .create = netlink_create,
1953 .owner = THIS_MODULE, /* for consistency 8) */
1954};
1955
Pavel Emelyanov46650792007-10-08 20:38:39 -07001956static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001957{
1958#ifdef CONFIG_PROC_FS
1959 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
1960 return -ENOMEM;
1961#endif
1962 return 0;
1963}
1964
Pavel Emelyanov46650792007-10-08 20:38:39 -07001965static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001966{
1967#ifdef CONFIG_PROC_FS
1968 proc_net_remove(net, "netlink");
1969#endif
1970}
1971
Denis V. Lunev022cbae2007-11-13 03:23:50 -08001972static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001973 .init = netlink_net_init,
1974 .exit = netlink_net_exit,
1975};
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977static int __init netlink_proto_init(void)
1978{
1979 struct sk_buff *dummy_skb;
1980 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07001981 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 unsigned int order;
1983 int err = proto_register(&netlink_proto, 0);
1984
1985 if (err != 0)
1986 goto out;
1987
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07001988 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001990 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001991 if (!nl_table)
1992 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (num_physpages >= (128 * 1024))
Denis Cheng26ff5dd2007-09-16 16:36:02 -07001995 limit = num_physpages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 else
Denis Cheng26ff5dd2007-09-16 16:36:02 -07001997 limit = num_physpages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Denis Cheng26ff5dd2007-09-16 16:36:02 -07001999 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2000 limit = (1UL << order) / sizeof(struct hlist_head);
2001 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 for (i = 0; i < MAX_LINKS; i++) {
2004 struct nl_pid_hash *hash = &nl_table[i].hash;
2005
Eric Dumazetea729122007-12-11 02:09:47 -08002006 hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!hash->table) {
2008 while (i-- > 0)
2009 nl_pid_hash_free(nl_table[i].hash.table,
2010 1 * sizeof(*hash->table));
2011 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002012 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 hash->max_shift = order;
2015 hash->shift = 0;
2016 hash->mask = 0;
2017 hash->rehash_time = jiffies;
2018 }
2019
2020 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002021 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002022 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 rtnetlink_init();
2024out:
2025 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002026panic:
2027 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030core_initcall(netlink_proto_init);