blob: 43a124feaad88908948a19d2d7a07cb9b69f34de [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
Eric Dumazet5c398dc2010-10-24 04:27:10 +000086struct listeners {
87 struct rcu_head rcu;
88 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000089};
90
Patrick McHardy77247bb2005-08-14 19:27:13 -070091#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070092#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000093#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070094#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static inline struct netlink_sock *nlk_sk(struct sock *sk)
97{
Denis Cheng32b21e02007-08-28 15:41:11 -070098 return container_of(sk, struct netlink_sock, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
David S. Miller035c4c12011-12-23 17:33:03 -0500101static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -0700102{
103 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct nl_pid_hash {
Eric Dumazet658cb352012-04-22 21:30:21 +0000107 struct hlist_head *table;
108 unsigned long rehash_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Eric Dumazet658cb352012-04-22 21:30:21 +0000110 unsigned int mask;
111 unsigned int shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Eric Dumazet658cb352012-04-22 21:30:21 +0000113 unsigned int entries;
114 unsigned int max_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Eric Dumazet658cb352012-04-22 21:30:21 +0000116 u32 rnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119struct netlink_table {
Eric Dumazet658cb352012-04-22 21:30:21 +0000120 struct nl_pid_hash hash;
121 struct hlist_head mc_list;
122 struct listeners __rcu *listeners;
123 unsigned int nl_nonroot;
124 unsigned int groups;
125 struct mutex *cb_mutex;
126 struct module *module;
127 int registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static struct netlink_table *nl_table;
131
132static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
133
134static int netlink_dump(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136static DEFINE_RWLOCK(nl_table_lock);
137static atomic_t nl_table_users = ATOMIC_INIT(0);
138
Alan Sterne041c682006-03-27 01:16:30 -0800139static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000141static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700142{
143 return group ? 1 << (group - 1) : 0;
144}
145
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000146static inline struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
149}
150
Eric Dumazet658cb352012-04-22 21:30:21 +0000151static void netlink_destroy_callback(struct netlink_callback *cb)
152{
153 kfree_skb(cb->skb);
154 kfree(cb);
155}
156
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000157static void netlink_consume_callback(struct netlink_callback *cb)
158{
159 consume_skb(cb->skb);
160 kfree(cb);
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static void netlink_sock_destruct(struct sock *sk)
164{
Herbert Xu3f660d62007-05-03 03:17:14 -0700165 struct netlink_sock *nlk = nlk_sk(sk);
166
Herbert Xu3f660d62007-05-03 03:17:14 -0700167 if (nlk->cb) {
168 if (nlk->cb->done)
169 nlk->cb->done(nlk->cb);
170 netlink_destroy_callback(nlk->cb);
171 }
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 skb_queue_purge(&sk->sk_receive_queue);
174
175 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800176 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return;
178 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700179
180 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
181 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
182 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800185/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
186 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
188 * this, _but_ remember, it adds useless work on UP machines.
189 */
190
Johannes Bergd136f1b2009-09-12 03:03:15 +0000191void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800192 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000194 might_sleep();
195
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700196 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (atomic_read(&nl_table_users)) {
199 DECLARE_WAITQUEUE(wait, current);
200
201 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800202 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 set_current_state(TASK_UNINTERRUPTIBLE);
204 if (atomic_read(&nl_table_users) == 0)
205 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700206 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700208 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 __set_current_state(TASK_RUNNING);
212 remove_wait_queue(&nl_table_wait, &wait);
213 }
214}
215
Johannes Bergd136f1b2009-09-12 03:03:15 +0000216void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800217 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700219 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 wake_up(&nl_table_wait);
221}
222
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800223static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224netlink_lock_table(void)
225{
226 /* read_lock() synchronizes us to netlink_table_grab */
227
228 read_lock(&nl_table_lock);
229 atomic_inc(&nl_table_users);
230 read_unlock(&nl_table_lock);
231}
232
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800233static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234netlink_unlock_table(void)
235{
236 if (atomic_dec_and_test(&nl_table_users))
237 wake_up(&nl_table_wait);
238}
239
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000240static struct sock *netlink_lookup(struct net *net, int protocol, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct nl_pid_hash *hash = &nl_table[protocol].hash;
243 struct hlist_head *head;
244 struct sock *sk;
245 struct hlist_node *node;
246
247 read_lock(&nl_table_lock);
248 head = nl_pid_hashfn(hash, pid);
249 sk_for_each(sk, node, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900250 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 sock_hold(sk);
252 goto found;
253 }
254 }
255 sk = NULL;
256found:
257 read_unlock(&nl_table_lock);
258 return sk;
259}
260
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000261static struct hlist_head *nl_pid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800264 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 else
266 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800267 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
268 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000271static void nl_pid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 if (size <= PAGE_SIZE)
274 kfree(table);
275 else
276 free_pages((unsigned long)table, get_order(size));
277}
278
279static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
280{
281 unsigned int omask, mask, shift;
282 size_t osize, size;
283 struct hlist_head *otable, *table;
284 int i;
285
286 omask = mask = hash->mask;
287 osize = size = (mask + 1) * sizeof(*table);
288 shift = hash->shift;
289
290 if (grow) {
291 if (++shift > hash->max_shift)
292 return 0;
293 mask = mask * 2 + 1;
294 size *= 2;
295 }
296
Eric Dumazetea729122007-12-11 02:09:47 -0800297 table = nl_pid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!table)
299 return 0;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 otable = hash->table;
302 hash->table = table;
303 hash->mask = mask;
304 hash->shift = shift;
305 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
306
307 for (i = 0; i <= omask; i++) {
308 struct sock *sk;
309 struct hlist_node *node, *tmp;
310
311 sk_for_each_safe(sk, node, tmp, &otable[i])
312 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
313 }
314
315 nl_pid_hash_free(otable, osize);
316 hash->rehash_time = jiffies + 10 * 60 * HZ;
317 return 1;
318}
319
320static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
321{
322 int avg = hash->entries >> hash->shift;
323
324 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
325 return 1;
326
327 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
328 nl_pid_hash_rehash(hash, 0);
329 return 1;
330 }
331
332 return 0;
333}
334
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800335static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Patrick McHardy4277a082006-03-20 18:52:01 -0800337static void
338netlink_update_listeners(struct sock *sk)
339{
340 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
341 struct hlist_node *node;
342 unsigned long mask;
343 unsigned int i;
344
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700345 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800346 mask = 0;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700347 sk_for_each_bound(sk, node, &tbl->mc_list) {
348 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
349 mask |= nlk_sk(sk)->groups[i];
350 }
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000351 tbl->listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800352 }
353 /* this function is only called with the netlink table "grabbed", which
354 * makes sure updates are visible before bind or setsockopt return. */
355}
356
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200357static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
360 struct hlist_head *head;
361 int err = -EADDRINUSE;
362 struct sock *osk;
363 struct hlist_node *node;
364 int len;
365
366 netlink_table_grab();
367 head = nl_pid_hashfn(hash, pid);
368 len = 0;
369 sk_for_each(osk, node, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900370 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 break;
372 len++;
373 }
374 if (node)
375 goto err;
376
377 err = -EBUSY;
378 if (nlk_sk(sk)->pid)
379 goto err;
380
381 err = -ENOMEM;
382 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
383 goto err;
384
385 if (len && nl_pid_hash_dilute(hash, len))
386 head = nl_pid_hashfn(hash, pid);
387 hash->entries++;
388 nlk_sk(sk)->pid = pid;
389 sk_add_node(sk, head);
390 err = 0;
391
392err:
393 netlink_table_ungrab();
394 return err;
395}
396
397static void netlink_remove(struct sock *sk)
398{
399 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700400 if (sk_del_node_init(sk))
401 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700402 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 __sk_del_bind_node(sk);
404 netlink_table_ungrab();
405}
406
407static struct proto netlink_proto = {
408 .name = "NETLINK",
409 .owner = THIS_MODULE,
410 .obj_size = sizeof(struct netlink_sock),
411};
412
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700413static int __netlink_create(struct net *net, struct socket *sock,
414 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct sock *sk;
417 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700418
419 sock->ops = &netlink_ops;
420
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700421 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700422 if (!sk)
423 return -ENOMEM;
424
425 sock_init_data(sock, sk);
426
427 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000428 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700429 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000430 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700431 nlk->cb_mutex = &nlk->cb_def_mutex;
432 mutex_init(nlk->cb_mutex);
433 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700434 init_waitqueue_head(&nlk->wait);
435
436 sk->sk_destruct = netlink_sock_destruct;
437 sk->sk_protocol = protocol;
438 return 0;
439}
440
Eric Paris3f378b62009-11-05 22:18:14 -0800441static int netlink_create(struct net *net, struct socket *sock, int protocol,
442 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700443{
444 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700445 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700446 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700447 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 sock->state = SS_UNCONNECTED;
450
451 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
452 return -ESOCKTNOSUPPORT;
453
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800454 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -EPROTONOSUPPORT;
456
Patrick McHardy77247bb2005-08-14 19:27:13 -0700457 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700458#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700459 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700460 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700461 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700462 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700463 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700464#endif
465 if (nl_table[protocol].registered &&
466 try_module_get(nl_table[protocol].module))
467 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000468 else
469 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700470 cb_mutex = nl_table[protocol].cb_mutex;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700471 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700472
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000473 if (err < 0)
474 goto out;
475
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800476 err = __netlink_create(net, sock, cb_mutex, protocol);
477 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700478 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David S. Miller6f756a82008-11-23 17:34:03 -0800480 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800481 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800482 local_bh_enable();
483
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700484 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700485 nlk->module = module;
Patrick McHardyab33a172005-08-14 19:31:36 -0700486out:
487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Patrick McHardyab33a172005-08-14 19:31:36 -0700489out_module:
490 module_put(module);
491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494static int netlink_release(struct socket *sock)
495{
496 struct sock *sk = sock->sk;
497 struct netlink_sock *nlk;
498
499 if (!sk)
500 return 0;
501
502 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700503 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 nlk = nlk_sk(sk);
505
Herbert Xu3f660d62007-05-03 03:17:14 -0700506 /*
507 * OK. Socket is unlinked, any packets that arrive now
508 * will be purged.
509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 sock->sk = NULL;
512 wake_up_interruptible_all(&nlk->wait);
513
514 skb_queue_purge(&sk->sk_write_queue);
515
Johannes Berg649300b2009-11-16 12:05:34 +0000516 if (nlk->pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900518 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 .protocol = sk->sk_protocol,
520 .pid = nlk->pid,
521 };
Alan Sterne041c682006-03-27 01:16:30 -0800522 atomic_notifier_call_chain(&netlink_chain,
523 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900524 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700525
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800526 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700527
Patrick McHardy4277a082006-03-20 18:52:01 -0800528 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -0700529 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800530 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
531 if (--nl_table[sk->sk_protocol].registered == 0) {
532 kfree(nl_table[sk->sk_protocol].listeners);
533 nl_table[sk->sk_protocol].module = NULL;
534 nl_table[sk->sk_protocol].registered = 0;
535 }
Eric Dumazet658cb352012-04-22 21:30:21 +0000536 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800537 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000538 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800539 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700540
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700541 kfree(nlk->groups);
542 nlk->groups = NULL;
543
Eric Dumazet37558102008-11-24 14:05:22 -0800544 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800545 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800546 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 sock_put(sk);
548 return 0;
549}
550
551static int netlink_autobind(struct socket *sock)
552{
553 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900554 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
556 struct hlist_head *head;
557 struct sock *osk;
558 struct hlist_node *node;
Tom Goff66aa4a52010-03-19 15:38:50 +0000559 s32 pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int err;
561 static s32 rover = -4097;
562
563retry:
564 cond_resched();
565 netlink_table_grab();
566 head = nl_pid_hashfn(hash, pid);
567 sk_for_each(osk, node, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900568 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200569 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (nlk_sk(osk)->pid == pid) {
571 /* Bind collision, search negative pid values. */
572 pid = rover--;
573 if (rover > -4097)
574 rover = -4097;
575 netlink_table_ungrab();
576 goto retry;
577 }
578 }
579 netlink_table_ungrab();
580
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200581 err = netlink_insert(sk, net, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (err == -EADDRINUSE)
583 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700584
585 /* If 2 threads race to autobind, that is fine. */
586 if (err == -EBUSY)
587 err = 0;
588
589 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000592static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900593{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
595 capable(CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700598static void
599netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
600{
601 struct netlink_sock *nlk = nlk_sk(sk);
602
603 if (nlk->subscriptions && !subscriptions)
604 __sk_del_bind_node(sk);
605 else if (!nlk->subscriptions && subscriptions)
606 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
607 nlk->subscriptions = subscriptions;
608}
609
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700610static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700611{
612 struct netlink_sock *nlk = nlk_sk(sk);
613 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700614 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700615 int err = 0;
616
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700617 netlink_table_grab();
618
Patrick McHardy513c2502005-09-06 15:43:59 -0700619 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700620 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700621 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700622 goto out_unlock;
623 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700624
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700625 if (nlk->ngroups >= groups)
626 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700627
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700628 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
629 if (new_groups == NULL) {
630 err = -ENOMEM;
631 goto out_unlock;
632 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800633 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700634 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
635
636 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700637 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700638 out_unlock:
639 netlink_table_ungrab();
640 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700641}
642
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800643static int netlink_bind(struct socket *sock, struct sockaddr *addr,
644 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900647 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct netlink_sock *nlk = nlk_sk(sk);
649 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
650 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (nladdr->nl_family != AF_NETLINK)
653 return -EINVAL;
654
655 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700656 if (nladdr->nl_groups) {
657 if (!netlink_capable(sock, NL_NONROOT_RECV))
658 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700659 err = netlink_realloc_groups(sk);
660 if (err)
661 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (nlk->pid) {
665 if (nladdr->nl_pid != nlk->pid)
666 return -EINVAL;
667 } else {
668 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200669 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 netlink_autobind(sock);
671 if (err)
672 return err;
673 }
674
Patrick McHardy513c2502005-09-06 15:43:59 -0700675 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
677
678 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700679 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900680 hweight32(nladdr->nl_groups) -
681 hweight32(nlk->groups[0]));
682 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800683 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 netlink_table_ungrab();
685
686 return 0;
687}
688
689static int netlink_connect(struct socket *sock, struct sockaddr *addr,
690 int alen, int flags)
691{
692 int err = 0;
693 struct sock *sk = sock->sk;
694 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800695 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Changli Gao6503d962010-03-31 22:58:26 +0000697 if (alen < sizeof(addr->sa_family))
698 return -EINVAL;
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (addr->sa_family == AF_UNSPEC) {
701 sk->sk_state = NETLINK_UNCONNECTED;
702 nlk->dst_pid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700703 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705 }
706 if (addr->sa_family != AF_NETLINK)
707 return -EINVAL;
708
709 /* Only superuser is allowed to send multicasts */
710 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
711 return -EPERM;
712
713 if (!nlk->pid)
714 err = netlink_autobind(sock);
715
716 if (err == 0) {
717 sk->sk_state = NETLINK_CONNECTED;
718 nlk->dst_pid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700719 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
722 return err;
723}
724
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800725static int netlink_getname(struct socket *sock, struct sockaddr *addr,
726 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct sock *sk = sock->sk;
729 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +0000730 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 nladdr->nl_family = AF_NETLINK;
733 nladdr->nl_pad = 0;
734 *addr_len = sizeof(*nladdr);
735
736 if (peer) {
737 nladdr->nl_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700738 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 } else {
740 nladdr->nl_pid = nlk->pid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700741 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 return 0;
744}
745
746static void netlink_overrun(struct sock *sk)
747{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700748 struct netlink_sock *nlk = nlk_sk(sk);
749
750 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
751 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
752 sk->sk_err = ENOBUFS;
753 sk->sk_error_report(sk);
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700756 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct sock *sock;
762 struct netlink_sock *nlk;
763
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900764 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (!sock)
766 return ERR_PTR(-ECONNREFUSED);
767
768 /* Don't bother queuing skb if kernel socket has no input function */
769 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700770 if (sock->sk_state == NETLINK_CONNECTED &&
771 nlk->dst_pid != nlk_sk(ssk)->pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 sock_put(sock);
773 return ERR_PTR(-ECONNREFUSED);
774 }
775 return sock;
776}
777
778struct sock *netlink_getsockbyfilp(struct file *filp)
779{
Josef Sipek6db5fc52006-12-08 02:37:25 -0800780 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct sock *sock;
782
783 if (!S_ISSOCK(inode->i_mode))
784 return ERR_PTR(-ENOTSOCK);
785
786 sock = SOCKET_I(inode)->sk;
787 if (sock->sk_family != AF_NETLINK)
788 return ERR_PTR(-EINVAL);
789
790 sock_hold(sock);
791 return sock;
792}
793
794/*
795 * Attach a skb to a netlink socket.
796 * The caller must hold a reference to the destination socket. On error, the
797 * reference is dropped. The skb is not send to the destination, just all
798 * all error checks are performed and memory in the queue is reserved.
799 * Return values:
800 * < 0: error. skb freed, reference to sock dropped.
801 * 0: continue
802 * 1: repeat lookup - reference dropped while waiting for socket memory.
803 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700804int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800805 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct netlink_sock *nlk;
808
809 nlk = nlk_sk(sk);
810
811 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
812 test_bit(0, &nlk->state)) {
813 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800814 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -0700815 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 netlink_overrun(sk);
817 sock_put(sk);
818 kfree_skb(skb);
819 return -EAGAIN;
820 }
821
822 __set_current_state(TASK_INTERRUPTIBLE);
823 add_wait_queue(&nlk->wait, &wait);
824
825 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
826 test_bit(0, &nlk->state)) &&
827 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800828 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 __set_current_state(TASK_RUNNING);
831 remove_wait_queue(&nlk->wait, &wait);
832 sock_put(sk);
833
834 if (signal_pending(current)) {
835 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800836 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 return 1;
839 }
840 skb_set_owner_r(skb, sk);
841 return 0;
842}
843
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000844static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int len = skb->len;
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 skb_queue_tail(&sk->sk_receive_queue, skb);
849 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000850 return len;
851}
852
853int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
854{
855 int len = __netlink_sendskb(sk, skb);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 sock_put(sk);
858 return len;
859}
860
861void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
862{
863 kfree_skb(skb);
864 sock_put(sk);
865}
866
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000867static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 int delta;
870
871 skb_orphan(skb);
872
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700873 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (delta * 2 < skb->truesize)
875 return skb;
876
877 if (skb_shared(skb)) {
878 struct sk_buff *nskb = skb_clone(skb, allocation);
879 if (!nskb)
880 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +0000881 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 skb = nskb;
883 }
884
885 if (!pskb_expand_head(skb, 0, -delta, allocation))
886 skb->truesize -= delta;
887
888 return skb;
889}
890
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000891static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700892{
893 struct netlink_sock *nlk = nlk_sk(sk);
894
895 if (skb_queue_empty(&sk->sk_receive_queue))
896 clear_bit(0, &nlk->state);
897 if (!test_bit(0, &nlk->state))
898 wake_up_interruptible(&nlk->wait);
899}
900
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000901static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700902{
903 int ret;
904 struct netlink_sock *nlk = nlk_sk(sk);
905
906 ret = -ECONNREFUSED;
907 if (nlk->netlink_rcv != NULL) {
908 ret = skb->len;
909 skb_set_owner_r(skb, sk);
910 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000911 consume_skb(skb);
912 } else {
913 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700914 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700915 sock_put(sk);
916 return ret;
917}
918
919int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
920 u32 pid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct sock *sk;
923 int err;
924 long timeo;
925
926 skb = netlink_trim(skb, gfp_any());
927
928 timeo = sock_sndtimeo(ssk, nonblock);
929retry:
930 sk = netlink_getsockbypid(ssk, pid);
931 if (IS_ERR(sk)) {
932 kfree_skb(skb);
933 return PTR_ERR(sk);
934 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700935 if (netlink_is_kernel(sk))
936 return netlink_unicast_kernel(sk, skb);
937
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700938 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -0700939 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700940 kfree_skb(skb);
941 sock_put(sk);
942 return err;
943 }
944
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700945 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (err == 1)
947 goto retry;
948 if (err)
949 return err;
950
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700951 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800953EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Patrick McHardy4277a082006-03-20 18:52:01 -0800955int netlink_has_listeners(struct sock *sk, unsigned int group)
956{
957 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000958 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -0800959
Denis V. Lunevaed81562007-10-10 21:14:32 -0700960 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700961
962 rcu_read_lock();
963 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
964
Patrick McHardy4277a082006-03-20 18:52:01 -0800965 if (group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000966 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700967
968 rcu_read_unlock();
969
Patrick McHardy4277a082006-03-20 18:52:01 -0800970 return res;
971}
972EXPORT_SYMBOL_GPL(netlink_has_listeners);
973
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000974static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct netlink_sock *nlk = nlk_sk(sk);
977
978 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
979 !test_bit(0, &nlk->state)) {
980 skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000981 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +0000982 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 return -1;
985}
986
987struct netlink_broadcast_data {
988 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200989 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 u32 pid;
991 u32 group;
992 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800993 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 int congested;
995 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -0400996 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -0700998 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
999 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000};
1001
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001002static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct netlink_broadcast_data *p)
1004{
1005 struct netlink_sock *nlk = nlk_sk(sk);
1006 int val;
1007
1008 if (p->exclude_sk == sk)
1009 goto out;
1010
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001011 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1012 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto out;
1014
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001015 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001016 goto out;
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (p->failure) {
1019 netlink_overrun(sk);
1020 goto out;
1021 }
1022
1023 sock_hold(sk);
1024 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001025 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 p->skb2 = skb_clone(p->skb, p->allocation);
1027 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001028 p->skb2 = skb_get(p->skb);
1029 /*
1030 * skb ownership may have been set when
1031 * delivered to a previous socket.
1032 */
1033 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 }
1036 if (p->skb2 == NULL) {
1037 netlink_overrun(sk);
1038 /* Clone failed. Notify ALL listeners. */
1039 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001040 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1041 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001042 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1043 kfree_skb(p->skb2);
1044 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001045 } else if (sk_filter(sk, p->skb2)) {
1046 kfree_skb(p->skb2);
1047 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1049 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001050 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1051 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 } else {
1053 p->congested |= val;
1054 p->delivered = 1;
1055 p->skb2 = NULL;
1056 }
1057 sock_put(sk);
1058
1059out:
1060 return 0;
1061}
1062
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001063int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 pid,
1064 u32 group, gfp_t allocation,
1065 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1066 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001068 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct netlink_broadcast_data info;
1070 struct hlist_node *node;
1071 struct sock *sk;
1072
1073 skb = netlink_trim(skb, allocation);
1074
1075 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001076 info.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 info.pid = pid;
1078 info.group = group;
1079 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001080 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 info.congested = 0;
1082 info.delivered = 0;
1083 info.allocation = allocation;
1084 info.skb = skb;
1085 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001086 info.tx_filter = filter;
1087 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /* While we sleep in clone, do not allow to change socket list */
1090
1091 netlink_lock_table();
1092
1093 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1094 do_one_broadcast(sk, &info);
1095
Neil Horman70d4bf62010-07-20 06:45:56 +00001096 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 netlink_unlock_table();
1099
Neil Horman70d4bf62010-07-20 06:45:56 +00001100 if (info.delivery_failure) {
1101 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001102 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001103 }
1104 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (info.delivered) {
1107 if (info.congested && (allocation & __GFP_WAIT))
1108 yield();
1109 return 0;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -ESRCH;
1112}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001113EXPORT_SYMBOL(netlink_broadcast_filtered);
1114
1115int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
1116 u32 group, gfp_t allocation)
1117{
1118 return netlink_broadcast_filtered(ssk, skb, pid, group, allocation,
1119 NULL, NULL);
1120}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001121EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123struct netlink_set_err_data {
1124 struct sock *exclude_sk;
1125 u32 pid;
1126 u32 group;
1127 int code;
1128};
1129
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001130static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001133 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 if (sk == p->exclude_sk)
1136 goto out;
1137
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001138 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001139 goto out;
1140
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001141 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1142 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto out;
1144
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001145 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1146 ret = 1;
1147 goto out;
1148 }
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 sk->sk_err = p->code;
1151 sk->sk_error_report(sk);
1152out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001153 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001156/**
1157 * netlink_set_err - report error to broadcast listeners
1158 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1159 * @pid: the PID of a process that we want to skip (if any)
1160 * @groups: the broadcast group that will notice the error
1161 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001162 *
1163 * This function returns the number of broadcast listeners that have set the
1164 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001165 */
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001166int netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct netlink_set_err_data info;
1169 struct hlist_node *node;
1170 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001171 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 info.exclude_sk = ssk;
1174 info.pid = pid;
1175 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001176 /* sk->sk_err wants a positive error value */
1177 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 read_lock(&nl_table_lock);
1180
1181 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001182 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001185 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001187EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Johannes Berg84659eb2007-07-18 15:47:05 -07001189/* must be called with netlink table grabbed */
1190static void netlink_update_socket_mc(struct netlink_sock *nlk,
1191 unsigned int group,
1192 int is_new)
1193{
1194 int old, new = !!is_new, subscriptions;
1195
1196 old = test_bit(group - 1, nlk->groups);
1197 subscriptions = nlk->subscriptions - old + new;
1198 if (new)
1199 __set_bit(group - 1, nlk->groups);
1200 else
1201 __clear_bit(group - 1, nlk->groups);
1202 netlink_update_subscriptions(&nlk->sk, subscriptions);
1203 netlink_update_listeners(&nlk->sk);
1204}
1205
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001206static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001207 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001208{
1209 struct sock *sk = sock->sk;
1210 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001211 unsigned int val = 0;
1212 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001213
1214 if (level != SOL_NETLINK)
1215 return -ENOPROTOOPT;
1216
1217 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001218 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001219 return -EFAULT;
1220
1221 switch (optname) {
1222 case NETLINK_PKTINFO:
1223 if (val)
1224 nlk->flags |= NETLINK_RECV_PKTINFO;
1225 else
1226 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1227 err = 0;
1228 break;
1229 case NETLINK_ADD_MEMBERSHIP:
1230 case NETLINK_DROP_MEMBERSHIP: {
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001231 if (!netlink_capable(sock, NL_NONROOT_RECV))
1232 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001233 err = netlink_realloc_groups(sk);
1234 if (err)
1235 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001236 if (!val || val - 1 >= nlk->ngroups)
1237 return -EINVAL;
1238 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001239 netlink_update_socket_mc(nlk, val,
1240 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001241 netlink_table_ungrab();
1242 err = 0;
1243 break;
1244 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001245 case NETLINK_BROADCAST_ERROR:
1246 if (val)
1247 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1248 else
1249 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1250 err = 0;
1251 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001252 case NETLINK_NO_ENOBUFS:
1253 if (val) {
1254 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1255 clear_bit(0, &nlk->state);
1256 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001257 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001258 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001259 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001260 err = 0;
1261 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001262 default:
1263 err = -ENOPROTOOPT;
1264 }
1265 return err;
1266}
1267
1268static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001269 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001270{
1271 struct sock *sk = sock->sk;
1272 struct netlink_sock *nlk = nlk_sk(sk);
1273 int len, val, err;
1274
1275 if (level != SOL_NETLINK)
1276 return -ENOPROTOOPT;
1277
1278 if (get_user(len, optlen))
1279 return -EFAULT;
1280 if (len < 0)
1281 return -EINVAL;
1282
1283 switch (optname) {
1284 case NETLINK_PKTINFO:
1285 if (len < sizeof(int))
1286 return -EINVAL;
1287 len = sizeof(int);
1288 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001289 if (put_user(len, optlen) ||
1290 put_user(val, optval))
1291 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001292 err = 0;
1293 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001294 case NETLINK_BROADCAST_ERROR:
1295 if (len < sizeof(int))
1296 return -EINVAL;
1297 len = sizeof(int);
1298 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1299 if (put_user(len, optlen) ||
1300 put_user(val, optval))
1301 return -EFAULT;
1302 err = 0;
1303 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001304 case NETLINK_NO_ENOBUFS:
1305 if (len < sizeof(int))
1306 return -EINVAL;
1307 len = sizeof(int);
1308 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1309 if (put_user(len, optlen) ||
1310 put_user(val, optval))
1311 return -EFAULT;
1312 err = 0;
1313 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001314 default:
1315 err = -ENOPROTOOPT;
1316 }
1317 return err;
1318}
1319
1320static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1321{
1322 struct nl_pktinfo info;
1323
1324 info.group = NETLINK_CB(skb).dst_group;
1325 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1329 struct msghdr *msg, size_t len)
1330{
1331 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1332 struct sock *sk = sock->sk;
1333 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001334 struct sockaddr_nl *addr = msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 u32 dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001336 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct sk_buff *skb;
1338 int err;
1339 struct scm_cookie scm;
1340
1341 if (msg->msg_flags&MSG_OOB)
1342 return -EOPNOTSUPP;
1343
Eric Dumazet16e57262011-09-19 05:52:27 +00001344 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 err = scm_send(sock, msg, siocb->scm);
1348 if (err < 0)
1349 return err;
1350
1351 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001352 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 dst_pid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001356 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001357 err = -EPERM;
Patrick McHardyd629b832005-08-14 19:27:50 -07001358 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 } else {
1361 dst_pid = nlk->dst_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001362 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
1365 if (!nlk->pid) {
1366 err = netlink_autobind(sock);
1367 if (err)
1368 goto out;
1369 }
1370
1371 err = -EMSGSIZE;
1372 if (len > sk->sk_sndbuf - 32)
1373 goto out;
1374 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001375 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001376 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto out;
1378
1379 NETLINK_CB(skb).pid = nlk->pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001380 NETLINK_CB(skb).dst_group = dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001384 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 kfree_skb(skb);
1386 goto out;
1387 }
1388
1389 err = security_netlink_send(sk, skb);
1390 if (err) {
1391 kfree_skb(skb);
1392 goto out;
1393 }
1394
Patrick McHardyd629b832005-08-14 19:27:50 -07001395 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 atomic_inc(&skb->users);
Patrick McHardyd629b832005-08-14 19:27:50 -07001397 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1400
1401out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001402 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return err;
1404}
1405
1406static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1407 struct msghdr *msg, size_t len,
1408 int flags)
1409{
1410 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1411 struct scm_cookie scm;
1412 struct sock *sk = sock->sk;
1413 struct netlink_sock *nlk = nlk_sk(sk);
1414 int noblock = flags&MSG_DONTWAIT;
1415 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001416 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001417 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (flags&MSG_OOB)
1420 return -EOPNOTSUPP;
1421
1422 copied = 0;
1423
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001424 skb = skb_recv_datagram(sk, flags, noblock, &err);
1425 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 goto out;
1427
Johannes Berg68d6ac62010-08-15 21:20:44 +00001428 data_skb = skb;
1429
Johannes Berg1dacc762009-07-01 11:26:02 +00001430#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1431 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00001432 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00001433 * If this skb has a frag_list, then here that means that we
1434 * will have to use the frag_list skb's data for compat tasks
1435 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00001436 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00001437 * If we need to send the compat skb, assign it to the
1438 * 'data_skb' variable so that it will be used below for data
1439 * copying. We keep 'skb' for everything else, including
1440 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00001441 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00001442 if (flags & MSG_CMSG_COMPAT)
1443 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00001444 }
1445#endif
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 msg->msg_namelen = 0;
1448
Johannes Berg68d6ac62010-08-15 21:20:44 +00001449 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (len < copied) {
1451 msg->msg_flags |= MSG_TRUNC;
1452 copied = len;
1453 }
1454
Johannes Berg68d6ac62010-08-15 21:20:44 +00001455 skb_reset_transport_header(data_skb);
1456 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001459 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 addr->nl_family = AF_NETLINK;
1461 addr->nl_pad = 0;
1462 addr->nl_pid = NETLINK_CB(skb).pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001463 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 msg->msg_namelen = sizeof(*addr);
1465 }
1466
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001467 if (nlk->flags & NETLINK_RECV_PKTINFO)
1468 netlink_cmsg_recv_pktinfo(msg, skb);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (NULL == siocb->scm) {
1471 memset(&scm, 0, sizeof(scm));
1472 siocb->scm = &scm;
1473 }
1474 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001475 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00001476 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 skb_free_datagram(sk, skb);
1479
Andrey Vaginb44d2112011-02-21 02:40:47 +00001480 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1481 ret = netlink_dump(sk);
1482 if (ret) {
1483 sk->sk_err = ret;
1484 sk->sk_error_report(sk);
1485 }
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489out:
1490 netlink_rcv_wake(sk);
1491 return err ? : copied;
1492}
1493
1494static void netlink_data_ready(struct sock *sk, int len)
1495{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001496 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001500 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 * complete set of kernel non-blocking support for message
1502 * queueing.
1503 */
1504
1505struct sock *
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001506netlink_kernel_create(struct net *net, int unit,
1507 struct module *module,
1508 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct socket *sock;
1511 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001512 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001513 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001514 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1515 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001517 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001519 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return NULL;
1521
1522 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1523 return NULL;
1524
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001525 /*
1526 * We have to just have a reference on the net from sk, but don't
1527 * get_net it. Besides, we cannot get and then put the net here.
1528 * So we create one inside init_net and the move it to net.
1529 */
1530
1531 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1532 goto out_sock_release_nosk;
1533
1534 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08001535 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001536
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001537 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08001538 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001539 else
1540 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001541
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001542 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08001543 if (!listeners)
1544 goto out_sock_release;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001547 if (cfg && cfg->input)
1548 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001550 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001551 goto out_sock_release;
1552
1553 nlk = nlk_sk(sk);
1554 nlk->flags |= NETLINK_KERNEL_SOCKET;
1555
1556 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001557 if (!nl_table[unit].registered) {
1558 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001559 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001560 nl_table[unit].cb_mutex = cb_mutex;
1561 nl_table[unit].module = module;
1562 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07001563 } else {
1564 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001565 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001566 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07001567 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001568 return sk;
1569
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001570out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001571 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08001572 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001573 return NULL;
1574
1575out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001576 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001577 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001579EXPORT_SYMBOL(netlink_kernel_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001581
1582void
1583netlink_kernel_release(struct sock *sk)
1584{
Denis V. Lunevedf02082008-02-29 11:18:32 -08001585 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001586}
1587EXPORT_SYMBOL(netlink_kernel_release);
1588
Johannes Bergd136f1b2009-09-12 03:03:15 +00001589int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001590{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001591 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001592 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001593
1594 if (groups < 32)
1595 groups = 32;
1596
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001597 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001598 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1599 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00001600 return -ENOMEM;
Eric Dumazet33d480c2011-08-11 19:30:52 +00001601 old = rcu_dereference_protected(tbl->listeners, 1);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001602 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1603 rcu_assign_pointer(tbl->listeners, new);
1604
Lai Jiangshan37b6b932011-03-15 18:01:42 +08001605 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001606 }
1607 tbl->groups = groups;
1608
Johannes Bergd136f1b2009-09-12 03:03:15 +00001609 return 0;
1610}
1611
1612/**
1613 * netlink_change_ngroups - change number of multicast groups
1614 *
1615 * This changes the number of multicast groups that are available
1616 * on a certain netlink family. Note that it is not possible to
1617 * change the number of groups to below 32. Also note that it does
1618 * not implicitly call netlink_clear_multicast_users() when the
1619 * number of groups is reduced.
1620 *
1621 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1622 * @groups: The new number of groups.
1623 */
1624int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1625{
1626 int err;
1627
1628 netlink_table_grab();
1629 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001630 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00001631
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001632 return err;
1633}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001634
Johannes Bergb8273572009-09-24 15:44:05 -07001635void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1636{
1637 struct sock *sk;
1638 struct hlist_node *node;
1639 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1640
1641 sk_for_each_bound(sk, node, &tbl->mc_list)
1642 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1643}
1644
Johannes Berg84659eb2007-07-18 15:47:05 -07001645/**
1646 * netlink_clear_multicast_users - kick off multicast listeners
1647 *
1648 * This function removes all listeners from the given group.
1649 * @ksk: The kernel netlink socket, as returned by
1650 * netlink_kernel_create().
1651 * @group: The multicast group to clear.
1652 */
1653void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1654{
Johannes Berg84659eb2007-07-18 15:47:05 -07001655 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07001656 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07001657 netlink_table_ungrab();
1658}
Johannes Berg84659eb2007-07-18 15:47:05 -07001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660void netlink_set_nonroot(int protocol, unsigned int flags)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001661{
1662 if ((unsigned int)protocol < MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 nl_table[protocol].nl_nonroot = flags;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001664}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001665EXPORT_SYMBOL(netlink_set_nonroot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001667struct nlmsghdr *
1668__nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, int type, int len, int flags)
1669{
1670 struct nlmsghdr *nlh;
1671 int size = NLMSG_LENGTH(len);
1672
1673 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1674 nlh->nlmsg_type = type;
1675 nlh->nlmsg_len = size;
1676 nlh->nlmsg_flags = flags;
1677 nlh->nlmsg_pid = pid;
1678 nlh->nlmsg_seq = seq;
1679 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
1680 memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size);
1681 return nlh;
1682}
1683EXPORT_SYMBOL(__nlmsg_put);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685/*
1686 * It looks a bit ugly.
1687 * It would be better to create kernel thread.
1688 */
1689
1690static int netlink_dump(struct sock *sk)
1691{
1692 struct netlink_sock *nlk = nlk_sk(sk);
1693 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001694 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001696 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001697 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001699 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 cb = nlk->cb;
1702 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001703 err = -EINVAL;
1704 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706
Greg Rosec7ac8672011-06-10 01:27:09 +00001707 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1708
1709 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1710 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00001711 goto errout_skb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 len = cb->dump(skb, cb);
1714
1715 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001716 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001717
1718 if (sk_filter(sk, skb))
1719 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001720 else
1721 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return 0;
1723 }
1724
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001725 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1726 if (!nlh)
1727 goto errout_skb;
1728
Johannes Berg670dc282011-06-20 13:40:46 +02001729 nl_dump_check_consistent(cb, nlh);
1730
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001731 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1732
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001733 if (sk_filter(sk, skb))
1734 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001735 else
1736 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Thomas Grafa8f74b22005-11-10 02:25:52 +01001738 if (cb->done)
1739 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001741 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001743 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001745
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001746errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001747 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001748 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001749 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
Patrick McHardy3a6c2b42009-08-25 16:07:40 +02001753 const struct nlmsghdr *nlh,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001754 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 struct netlink_callback *cb;
1757 struct sock *sk;
1758 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001759 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001761 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (cb == NULL)
1763 return -ENOBUFS;
1764
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001765 cb->dump = control->dump;
1766 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00001768 cb->data = control->data;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001769 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 atomic_inc(&skb->users);
1771 cb->skb = skb;
1772
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001773 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (sk == NULL) {
1775 netlink_destroy_callback(cb);
1776 return -ECONNREFUSED;
1777 }
1778 nlk = nlk_sk(sk);
Herbert Xu3f660d62007-05-03 03:17:14 -07001779 /* A dump is in progress... */
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001780 mutex_lock(nlk->cb_mutex);
Herbert Xu3f660d62007-05-03 03:17:14 -07001781 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001782 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 netlink_destroy_callback(cb);
1784 sock_put(sk);
1785 return -EBUSY;
1786 }
1787 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001788 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Andrey Vaginb44d2112011-02-21 02:40:47 +00001790 ret = netlink_dump(sk);
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001793
Andrey Vaginb44d2112011-02-21 02:40:47 +00001794 if (ret)
1795 return ret;
1796
Denis V. Lunev5c582982007-10-23 20:29:25 -07001797 /* We successfully started a dump, by returning -EINTR we
1798 * signal not to send ACK even if it was requested.
1799 */
1800 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001802EXPORT_SYMBOL(netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1805{
1806 struct sk_buff *skb;
1807 struct nlmsghdr *rep;
1808 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08001809 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Thomas Graf339bf982006-11-10 14:10:15 -08001811 /* error messages get the original request appened */
1812 if (err)
1813 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Thomas Graf339bf982006-11-10 14:10:15 -08001815 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (!skb) {
1817 struct sock *sk;
1818
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001819 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001820 in_skb->sk->sk_protocol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 NETLINK_CB(in_skb).pid);
1822 if (sk) {
1823 sk->sk_err = ENOBUFS;
1824 sk->sk_error_report(sk);
1825 sock_put(sk);
1826 }
1827 return;
1828 }
1829
1830 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00001831 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001832 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001834 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1836}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001837EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001839int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001840 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001841{
Thomas Graf82ace472005-11-10 02:25:53 +01001842 struct nlmsghdr *nlh;
1843 int err;
1844
1845 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001846 int msglen;
1847
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001848 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07001849 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01001850
Martin Murrayad8e4b72006-01-10 13:02:29 -08001851 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01001852 return 0;
1853
Thomas Grafd35b6852007-03-22 23:28:46 -07001854 /* Only requests are handled by the kernel */
1855 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07001856 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07001857
Thomas Graf45e7ae72007-03-22 23:29:10 -07001858 /* Skip control messages */
1859 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07001860 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07001861
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001862 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001863 if (err == -EINTR)
1864 goto skip;
1865
1866ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07001867 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01001868 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01001869
Denis V. Lunev5c582982007-10-23 20:29:25 -07001870skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001871 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001872 if (msglen > skb->len)
1873 msglen = skb->len;
1874 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01001875 }
1876
1877 return 0;
1878}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001879EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01001880
1881/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07001882 * nlmsg_notify - send a notification netlink message
1883 * @sk: netlink socket to use
1884 * @skb: notification message
1885 * @pid: destination netlink pid for reports or 0
1886 * @group: destination multicast group or 0
1887 * @report: 1 to report back, 0 to disable
1888 * @flags: allocation flags
1889 */
1890int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
1891 unsigned int group, int report, gfp_t flags)
1892{
1893 int err = 0;
1894
1895 if (group) {
1896 int exclude_pid = 0;
1897
1898 if (report) {
1899 atomic_inc(&skb->users);
1900 exclude_pid = pid;
1901 }
1902
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001903 /* errors reported via destination sk->sk_err, but propagate
1904 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
1905 err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001906 }
1907
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001908 if (report) {
1909 int err2;
1910
1911 err2 = nlmsg_unicast(sk, skb, pid);
1912 if (!err || err == -ESRCH)
1913 err = err2;
1914 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07001915
1916 return err;
1917}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001918EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920#ifdef CONFIG_PROC_FS
1921struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08001922 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 int link;
1924 int hash_idx;
1925};
1926
1927static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1928{
1929 struct nl_seq_iter *iter = seq->private;
1930 int i, j;
1931 struct sock *s;
1932 struct hlist_node *node;
1933 loff_t off = 0;
1934
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001935 for (i = 0; i < MAX_LINKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct nl_pid_hash *hash = &nl_table[i].hash;
1937
1938 for (j = 0; j <= hash->mask; j++) {
1939 sk_for_each(s, node, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001940 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001941 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (off == pos) {
1943 iter->link = i;
1944 iter->hash_idx = j;
1945 return s;
1946 }
1947 ++off;
1948 }
1949 }
1950 }
1951 return NULL;
1952}
1953
1954static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001955 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 read_lock(&nl_table_lock);
1958 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1959}
1960
1961static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1962{
1963 struct sock *s;
1964 struct nl_seq_iter *iter;
1965 int i, j;
1966
1967 ++*pos;
1968
1969 if (v == SEQ_START_TOKEN)
1970 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001971
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001972 iter = seq->private;
1973 s = v;
1974 do {
1975 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001976 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (s)
1978 return s;
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 i = iter->link;
1981 j = iter->hash_idx + 1;
1982
1983 do {
1984 struct nl_pid_hash *hash = &nl_table[i].hash;
1985
1986 for (; j <= hash->mask; j++) {
1987 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001988 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001989 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (s) {
1991 iter->link = i;
1992 iter->hash_idx = j;
1993 return s;
1994 }
1995 }
1996
1997 j = 0;
1998 } while (++i < MAX_LINKS);
1999
2000 return NULL;
2001}
2002
2003static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002004 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 read_unlock(&nl_table_lock);
2007}
2008
2009
2010static int netlink_seq_show(struct seq_file *seq, void *v)
2011{
Eric Dumazet658cb352012-04-22 21:30:21 +00002012 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 seq_puts(seq,
2014 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002015 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002016 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct sock *s = v;
2018 struct netlink_sock *nlk = nlk_sk(s);
2019
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002020 seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 s,
2022 s->sk_protocol,
2023 nlk->pid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002024 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002025 sk_rmem_alloc_get(s),
2026 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002028 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002029 atomic_read(&s->sk_drops),
2030 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 );
2032
2033 }
2034 return 0;
2035}
2036
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002037static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 .start = netlink_seq_start,
2039 .next = netlink_seq_next,
2040 .stop = netlink_seq_stop,
2041 .show = netlink_seq_show,
2042};
2043
2044
2045static int netlink_seq_open(struct inode *inode, struct file *file)
2046{
Denis V. Luneve372c412007-11-19 22:31:54 -08002047 return seq_open_net(inode, file, &netlink_seq_ops,
2048 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002049}
2050
Arjan van de Venda7071d2007-02-12 00:55:36 -08002051static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 .owner = THIS_MODULE,
2053 .open = netlink_seq_open,
2054 .read = seq_read,
2055 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002056 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057};
2058
2059#endif
2060
2061int netlink_register_notifier(struct notifier_block *nb)
2062{
Alan Sterne041c682006-03-27 01:16:30 -08002063 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002065EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067int netlink_unregister_notifier(struct notifier_block *nb)
2068{
Alan Sterne041c682006-03-27 01:16:30 -08002069 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002071EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002072
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002073static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 .family = PF_NETLINK,
2075 .owner = THIS_MODULE,
2076 .release = netlink_release,
2077 .bind = netlink_bind,
2078 .connect = netlink_connect,
2079 .socketpair = sock_no_socketpair,
2080 .accept = sock_no_accept,
2081 .getname = netlink_getname,
2082 .poll = datagram_poll,
2083 .ioctl = sock_no_ioctl,
2084 .listen = sock_no_listen,
2085 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002086 .setsockopt = netlink_setsockopt,
2087 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 .sendmsg = netlink_sendmsg,
2089 .recvmsg = netlink_recvmsg,
2090 .mmap = sock_no_mmap,
2091 .sendpage = sock_no_sendpage,
2092};
2093
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002094static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 .family = PF_NETLINK,
2096 .create = netlink_create,
2097 .owner = THIS_MODULE, /* for consistency 8) */
2098};
2099
Pavel Emelyanov46650792007-10-08 20:38:39 -07002100static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002101{
2102#ifdef CONFIG_PROC_FS
2103 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
2104 return -ENOMEM;
2105#endif
2106 return 0;
2107}
2108
Pavel Emelyanov46650792007-10-08 20:38:39 -07002109static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002110{
2111#ifdef CONFIG_PROC_FS
2112 proc_net_remove(net, "netlink");
2113#endif
2114}
2115
David S. Millerb963ea82010-08-30 19:08:01 -07002116static void __init netlink_add_usersock_entry(void)
2117{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002118 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002119 int groups = 32;
2120
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002121 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002122 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002123 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002124
2125 netlink_table_grab();
2126
2127 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002128 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002129 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2130 nl_table[NETLINK_USERSOCK].registered = 1;
2131
2132 netlink_table_ungrab();
2133}
2134
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002135static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002136 .init = netlink_net_init,
2137 .exit = netlink_net_exit,
2138};
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140static int __init netlink_proto_init(void)
2141{
2142 struct sk_buff *dummy_skb;
2143 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002144 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 unsigned int order;
2146 int err = proto_register(&netlink_proto, 0);
2147
2148 if (err != 0)
2149 goto out;
2150
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07002151 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002153 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002154 if (!nl_table)
2155 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Jan Beulich44813742009-09-21 17:03:05 -07002157 if (totalram_pages >= (128 * 1024))
2158 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 else
Jan Beulich44813742009-09-21 17:03:05 -07002160 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002162 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2163 limit = (1UL << order) / sizeof(struct hlist_head);
2164 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 for (i = 0; i < MAX_LINKS; i++) {
2167 struct nl_pid_hash *hash = &nl_table[i].hash;
2168
Eric Dumazetea729122007-12-11 02:09:47 -08002169 hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (!hash->table) {
2171 while (i-- > 0)
2172 nl_pid_hash_free(nl_table[i].hash.table,
2173 1 * sizeof(*hash->table));
2174 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002175 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 hash->max_shift = order;
2178 hash->shift = 0;
2179 hash->mask = 0;
2180 hash->rehash_time = jiffies;
2181 }
2182
David S. Millerb963ea82010-08-30 19:08:01 -07002183 netlink_add_usersock_entry();
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002186 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002187 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rtnetlink_init();
2189out:
2190 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002191panic:
2192 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195core_initcall(netlink_proto_init);