blob: 58b9025978faea309c56c355ea704d0ac43145e4 [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
Andrey Vagin0f29c762013-03-21 20:33:47 +040064#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric Dumazet5c398dc2010-10-24 04:27:10 +000066struct listeners {
67 struct rcu_head rcu;
68 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000069};
70
Patrick McHardycd967e02013-04-17 06:46:56 +000071/* state bits */
72#define NETLINK_CONGESTED 0x0
73
74/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070075#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070076#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000077#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070078#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070079
David S. Miller035c4c12011-12-23 17:33:03 -050080static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070081{
82 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
83}
84
Andrey Vagin0f29c762013-03-21 20:33:47 +040085struct netlink_table *nl_table;
86EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
89
90static int netlink_dump(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Andrey Vagin0f29c762013-03-21 20:33:47 +040092DEFINE_RWLOCK(nl_table_lock);
93EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static atomic_t nl_table_users = ATOMIC_INIT(0);
95
Eric Dumazet6d772ac2012-10-18 03:21:55 +000096#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
97
Alan Sterne041c682006-03-27 01:16:30 -080098static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000100static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700101{
102 return group ? 1 << (group - 1) : 0;
103}
104
Eric W. Biederman15e47302012-09-07 20:12:54 +0000105static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000107 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Eric Dumazet658cb352012-04-22 21:30:21 +0000110static void netlink_destroy_callback(struct netlink_callback *cb)
111{
112 kfree_skb(cb->skb);
113 kfree(cb);
114}
115
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000116static void netlink_consume_callback(struct netlink_callback *cb)
117{
118 consume_skb(cb->skb);
119 kfree(cb);
120}
121
Patrick McHardycf0a0182013-04-17 06:47:00 +0000122static void netlink_skb_destructor(struct sk_buff *skb)
123{
124 sock_rfree(skb);
125}
126
127static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
128{
129 WARN_ON(skb->sk != NULL);
130 skb->sk = sk;
131 skb->destructor = netlink_skb_destructor;
132 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
133 sk_mem_charge(sk, skb->truesize);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void netlink_sock_destruct(struct sock *sk)
137{
Herbert Xu3f660d62007-05-03 03:17:14 -0700138 struct netlink_sock *nlk = nlk_sk(sk);
139
Herbert Xu3f660d62007-05-03 03:17:14 -0700140 if (nlk->cb) {
141 if (nlk->cb->done)
142 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000143
144 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700145 netlink_destroy_callback(nlk->cb);
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 skb_queue_purge(&sk->sk_receive_queue);
149
150 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800151 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return;
153 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700154
155 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
156 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
157 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800160/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
161 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
163 * this, _but_ remember, it adds useless work on UP machines.
164 */
165
Johannes Bergd136f1b2009-09-12 03:03:15 +0000166void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800167 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000169 might_sleep();
170
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700171 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (atomic_read(&nl_table_users)) {
174 DECLARE_WAITQUEUE(wait, current);
175
176 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800177 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 set_current_state(TASK_UNINTERRUPTIBLE);
179 if (atomic_read(&nl_table_users) == 0)
180 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700181 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700183 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185
186 __set_current_state(TASK_RUNNING);
187 remove_wait_queue(&nl_table_wait, &wait);
188 }
189}
190
Johannes Bergd136f1b2009-09-12 03:03:15 +0000191void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800192 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700194 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 wake_up(&nl_table_wait);
196}
197
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800198static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199netlink_lock_table(void)
200{
201 /* read_lock() synchronizes us to netlink_table_grab */
202
203 read_lock(&nl_table_lock);
204 atomic_inc(&nl_table_users);
205 read_unlock(&nl_table_lock);
206}
207
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800208static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209netlink_unlock_table(void)
210{
211 if (atomic_dec_and_test(&nl_table_users))
212 wake_up(&nl_table_wait);
213}
214
Eric W. Biederman15e47302012-09-07 20:12:54 +0000215static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000217 struct nl_portid_hash *hash = &nl_table[protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct hlist_head *head;
219 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000222 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800223 sk_for_each(sk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000224 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 sock_hold(sk);
226 goto found;
227 }
228 }
229 sk = NULL;
230found:
231 read_unlock(&nl_table_lock);
232 return sk;
233}
234
Eric W. Biederman15e47302012-09-07 20:12:54 +0000235static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800238 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 else
240 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800241 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
242 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Eric W. Biederman15e47302012-09-07 20:12:54 +0000245static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 if (size <= PAGE_SIZE)
248 kfree(table);
249 else
250 free_pages((unsigned long)table, get_order(size));
251}
252
Eric W. Biederman15e47302012-09-07 20:12:54 +0000253static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 unsigned int omask, mask, shift;
256 size_t osize, size;
257 struct hlist_head *otable, *table;
258 int i;
259
260 omask = mask = hash->mask;
261 osize = size = (mask + 1) * sizeof(*table);
262 shift = hash->shift;
263
264 if (grow) {
265 if (++shift > hash->max_shift)
266 return 0;
267 mask = mask * 2 + 1;
268 size *= 2;
269 }
270
Eric W. Biederman15e47302012-09-07 20:12:54 +0000271 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!table)
273 return 0;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 otable = hash->table;
276 hash->table = table;
277 hash->mask = mask;
278 hash->shift = shift;
279 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
280
281 for (i = 0; i <= omask; i++) {
282 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800283 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Sasha Levinb67bfe02013-02-27 17:06:00 -0800285 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000286 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Eric W. Biederman15e47302012-09-07 20:12:54 +0000289 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 hash->rehash_time = jiffies + 10 * 60 * HZ;
291 return 1;
292}
293
Eric W. Biederman15e47302012-09-07 20:12:54 +0000294static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 int avg = hash->entries >> hash->shift;
297
Eric W. Biederman15e47302012-09-07 20:12:54 +0000298 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 1;
300
301 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000302 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 1;
304 }
305
306 return 0;
307}
308
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800309static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Patrick McHardy4277a082006-03-20 18:52:01 -0800311static void
312netlink_update_listeners(struct sock *sk)
313{
314 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800315 unsigned long mask;
316 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000317 struct listeners *listeners;
318
319 listeners = nl_deref_protected(tbl->listeners);
320 if (!listeners)
321 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800322
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700323 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800324 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800325 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700326 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
327 mask |= nlk_sk(sk)->groups[i];
328 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000329 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800330 }
331 /* this function is only called with the netlink table "grabbed", which
332 * makes sure updates are visible before bind or setsockopt return. */
333}
334
Eric W. Biederman15e47302012-09-07 20:12:54 +0000335static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000337 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct hlist_head *head;
339 int err = -EADDRINUSE;
340 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int len;
342
343 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000344 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800346 sk_for_each(osk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000347 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 len++;
350 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800351 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto err;
353
354 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000355 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto err;
357
358 err = -ENOMEM;
359 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
360 goto err;
361
Eric W. Biederman15e47302012-09-07 20:12:54 +0000362 if (len && nl_portid_hash_dilute(hash, len))
363 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000365 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 sk_add_node(sk, head);
367 err = 0;
368
369err:
370 netlink_table_ungrab();
371 return err;
372}
373
374static void netlink_remove(struct sock *sk)
375{
376 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700377 if (sk_del_node_init(sk))
378 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700379 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 __sk_del_bind_node(sk);
381 netlink_table_ungrab();
382}
383
384static struct proto netlink_proto = {
385 .name = "NETLINK",
386 .owner = THIS_MODULE,
387 .obj_size = sizeof(struct netlink_sock),
388};
389
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700390static int __netlink_create(struct net *net, struct socket *sock,
391 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct sock *sk;
394 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700395
396 sock->ops = &netlink_ops;
397
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700398 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700399 if (!sk)
400 return -ENOMEM;
401
402 sock_init_data(sock, sk);
403
404 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000405 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700406 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000407 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700408 nlk->cb_mutex = &nlk->cb_def_mutex;
409 mutex_init(nlk->cb_mutex);
410 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700411 init_waitqueue_head(&nlk->wait);
412
413 sk->sk_destruct = netlink_sock_destruct;
414 sk->sk_protocol = protocol;
415 return 0;
416}
417
Eric Paris3f378b62009-11-05 22:18:14 -0800418static int netlink_create(struct net *net, struct socket *sock, int protocol,
419 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700420{
421 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700422 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700423 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000424 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -0700425 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 sock->state = SS_UNCONNECTED;
428
429 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
430 return -ESOCKTNOSUPPORT;
431
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800432 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -EPROTONOSUPPORT;
434
Patrick McHardy77247bb2005-08-14 19:27:13 -0700435 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700436#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700437 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700438 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700439 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700440 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700441 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700442#endif
443 if (nl_table[protocol].registered &&
444 try_module_get(nl_table[protocol].module))
445 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000446 else
447 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700448 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000449 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700450 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700451
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000452 if (err < 0)
453 goto out;
454
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800455 err = __netlink_create(net, sock, cb_mutex, protocol);
456 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700457 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
David S. Miller6f756a82008-11-23 17:34:03 -0800459 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800460 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800461 local_bh_enable();
462
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700463 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700464 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000465 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -0700466out:
467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Patrick McHardyab33a172005-08-14 19:31:36 -0700469out_module:
470 module_put(module);
471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474static int netlink_release(struct socket *sock)
475{
476 struct sock *sk = sock->sk;
477 struct netlink_sock *nlk;
478
479 if (!sk)
480 return 0;
481
482 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700483 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 nlk = nlk_sk(sk);
485
Herbert Xu3f660d62007-05-03 03:17:14 -0700486 /*
487 * OK. Socket is unlinked, any packets that arrive now
488 * will be purged.
489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 sock->sk = NULL;
492 wake_up_interruptible_all(&nlk->wait);
493
494 skb_queue_purge(&sk->sk_write_queue);
495
Eric W. Biederman15e47302012-09-07 20:12:54 +0000496 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900498 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000500 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 };
Alan Sterne041c682006-03-27 01:16:30 -0800502 atomic_notifier_call_chain(&netlink_chain,
503 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900504 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700505
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800506 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700507
Patrick McHardy4277a082006-03-20 18:52:01 -0800508 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -0700509 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800510 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
511 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000512 struct listeners *old;
513
514 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
515 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
516 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800517 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000518 nl_table[sk->sk_protocol].bind = NULL;
519 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800520 nl_table[sk->sk_protocol].registered = 0;
521 }
Eric Dumazet658cb352012-04-22 21:30:21 +0000522 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800523 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000524 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800525 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700526
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700527 kfree(nlk->groups);
528 nlk->groups = NULL;
529
Eric Dumazet37558102008-11-24 14:05:22 -0800530 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800531 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800532 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 sock_put(sk);
534 return 0;
535}
536
537static int netlink_autobind(struct socket *sock)
538{
539 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900540 struct net *net = sock_net(sk);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000541 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct hlist_head *head;
543 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000544 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int err;
546 static s32 rover = -4097;
547
548retry:
549 cond_resched();
550 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000551 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800552 sk_for_each(osk, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900553 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200554 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000555 if (nlk_sk(osk)->portid == portid) {
556 /* Bind collision, search negative portid values. */
557 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (rover > -4097)
559 rover = -4097;
560 netlink_table_ungrab();
561 goto retry;
562 }
563 }
564 netlink_table_ungrab();
565
Eric W. Biederman15e47302012-09-07 20:12:54 +0000566 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (err == -EADDRINUSE)
568 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700569
570 /* If 2 threads race to autobind, that is fine. */
571 if (err == -EBUSY)
572 err = 0;
573
574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000577static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900578{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000579 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000580 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900581}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700583static void
584netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
585{
586 struct netlink_sock *nlk = nlk_sk(sk);
587
588 if (nlk->subscriptions && !subscriptions)
589 __sk_del_bind_node(sk);
590 else if (!nlk->subscriptions && subscriptions)
591 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
592 nlk->subscriptions = subscriptions;
593}
594
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700595static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700596{
597 struct netlink_sock *nlk = nlk_sk(sk);
598 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700599 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700600 int err = 0;
601
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700602 netlink_table_grab();
603
Patrick McHardy513c2502005-09-06 15:43:59 -0700604 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700605 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700606 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700607 goto out_unlock;
608 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700609
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700610 if (nlk->ngroups >= groups)
611 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700612
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700613 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
614 if (new_groups == NULL) {
615 err = -ENOMEM;
616 goto out_unlock;
617 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800618 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700619 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
620
621 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700622 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700623 out_unlock:
624 netlink_table_ungrab();
625 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700626}
627
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800628static int netlink_bind(struct socket *sock, struct sockaddr *addr,
629 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900632 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct netlink_sock *nlk = nlk_sk(sk);
634 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
635 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900636
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +0000637 if (addr_len < sizeof(struct sockaddr_nl))
638 return -EINVAL;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (nladdr->nl_family != AF_NETLINK)
641 return -EINVAL;
642
643 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700644 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000645 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -0700646 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700647 err = netlink_realloc_groups(sk);
648 if (err)
649 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric W. Biederman15e47302012-09-07 20:12:54 +0000652 if (nlk->portid) {
653 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
655 } else {
656 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200657 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 netlink_autobind(sock);
659 if (err)
660 return err;
661 }
662
Patrick McHardy513c2502005-09-06 15:43:59 -0700663 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665
666 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700667 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900668 hweight32(nladdr->nl_groups) -
669 hweight32(nlk->groups[0]));
670 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800671 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 netlink_table_ungrab();
673
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000674 if (nlk->netlink_bind && nlk->groups[0]) {
675 int i;
676
677 for (i=0; i<nlk->ngroups; i++) {
678 if (test_bit(i, nlk->groups))
679 nlk->netlink_bind(i);
680 }
681 }
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
686static int netlink_connect(struct socket *sock, struct sockaddr *addr,
687 int alen, int flags)
688{
689 int err = 0;
690 struct sock *sk = sock->sk;
691 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800692 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Changli Gao6503d962010-03-31 22:58:26 +0000694 if (alen < sizeof(addr->sa_family))
695 return -EINVAL;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (addr->sa_family == AF_UNSPEC) {
698 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000699 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700700 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702 }
703 if (addr->sa_family != AF_NETLINK)
704 return -EINVAL;
705
706 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000707 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -EPERM;
709
Eric W. Biederman15e47302012-09-07 20:12:54 +0000710 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 err = netlink_autobind(sock);
712
713 if (err == 0) {
714 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000715 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700716 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 return err;
720}
721
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800722static int netlink_getname(struct socket *sock, struct sockaddr *addr,
723 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct sock *sk = sock->sk;
726 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +0000727 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 nladdr->nl_family = AF_NETLINK;
730 nladdr->nl_pad = 0;
731 *addr_len = sizeof(*nladdr);
732
733 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000734 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700735 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000737 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700738 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 return 0;
741}
742
743static void netlink_overrun(struct sock *sk)
744{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700745 struct netlink_sock *nlk = nlk_sk(sk);
746
747 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
Patrick McHardycd967e02013-04-17 06:46:56 +0000748 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700749 sk->sk_err = ENOBUFS;
750 sk->sk_error_report(sk);
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700753 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Eric W. Biederman15e47302012-09-07 20:12:54 +0000756static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct sock *sock;
759 struct netlink_sock *nlk;
760
Eric W. Biederman15e47302012-09-07 20:12:54 +0000761 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (!sock)
763 return ERR_PTR(-ECONNREFUSED);
764
765 /* Don't bother queuing skb if kernel socket has no input function */
766 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700767 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +0000768 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 sock_put(sock);
770 return ERR_PTR(-ECONNREFUSED);
771 }
772 return sock;
773}
774
775struct sock *netlink_getsockbyfilp(struct file *filp)
776{
Al Viro496ad9a2013-01-23 17:07:38 -0500777 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct sock *sock;
779
780 if (!S_ISSOCK(inode->i_mode))
781 return ERR_PTR(-ENOTSOCK);
782
783 sock = SOCKET_I(inode)->sk;
784 if (sock->sk_family != AF_NETLINK)
785 return ERR_PTR(-EINVAL);
786
787 sock_hold(sock);
788 return sock;
789}
790
791/*
792 * Attach a skb to a netlink socket.
793 * The caller must hold a reference to the destination socket. On error, the
794 * reference is dropped. The skb is not send to the destination, just all
795 * all error checks are performed and memory in the queue is reserved.
796 * Return values:
797 * < 0: error. skb freed, reference to sock dropped.
798 * 0: continue
799 * 1: repeat lookup - reference dropped while waiting for socket memory.
800 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700801int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800802 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct netlink_sock *nlk;
805
806 nlk = nlk_sk(sk);
807
808 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +0000809 test_bit(NETLINK_CONGESTED, &nlk->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800811 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -0700812 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 netlink_overrun(sk);
814 sock_put(sk);
815 kfree_skb(skb);
816 return -EAGAIN;
817 }
818
819 __set_current_state(TASK_INTERRUPTIBLE);
820 add_wait_queue(&nlk->wait, &wait);
821
822 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +0000823 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800825 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 __set_current_state(TASK_RUNNING);
828 remove_wait_queue(&nlk->wait, &wait);
829 sock_put(sk);
830
831 if (signal_pending(current)) {
832 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800833 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 return 1;
836 }
Patrick McHardycf0a0182013-04-17 06:47:00 +0000837 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
839}
840
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000841static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int len = skb->len;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 skb_queue_tail(&sk->sk_receive_queue, skb);
846 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000847 return len;
848}
849
850int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
851{
852 int len = __netlink_sendskb(sk, skb);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 sock_put(sk);
855 return len;
856}
857
858void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
859{
860 kfree_skb(skb);
861 sock_put(sk);
862}
863
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000864static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 int delta;
867
Patrick McHardy1298ca42013-04-17 06:46:59 +0000868 WARN_ON(skb->sk != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700870 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (delta * 2 < skb->truesize)
872 return skb;
873
874 if (skb_shared(skb)) {
875 struct sk_buff *nskb = skb_clone(skb, allocation);
876 if (!nskb)
877 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +0000878 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 skb = nskb;
880 }
881
882 if (!pskb_expand_head(skb, 0, -delta, allocation))
883 skb->truesize -= delta;
884
885 return skb;
886}
887
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000888static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700889{
890 struct netlink_sock *nlk = nlk_sk(sk);
891
892 if (skb_queue_empty(&sk->sk_receive_queue))
Patrick McHardycd967e02013-04-17 06:46:56 +0000893 clear_bit(NETLINK_CONGESTED, &nlk->state);
894 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700895 wake_up_interruptible(&nlk->wait);
896}
897
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600898static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
899 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700900{
901 int ret;
902 struct netlink_sock *nlk = nlk_sk(sk);
903
904 ret = -ECONNREFUSED;
905 if (nlk->netlink_rcv != NULL) {
906 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +0000907 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +0000908 NETLINK_CB(skb).sk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700909 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000910 consume_skb(skb);
911 } else {
912 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700913 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700914 sock_put(sk);
915 return ret;
916}
917
918int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000919 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct sock *sk;
922 int err;
923 long timeo;
924
925 skb = netlink_trim(skb, gfp_any());
926
927 timeo = sock_sndtimeo(ssk, nonblock);
928retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000929 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (IS_ERR(sk)) {
931 kfree_skb(skb);
932 return PTR_ERR(sk);
933 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700934 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600935 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700936
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700937 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -0700938 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700939 kfree_skb(skb);
940 sock_put(sk);
941 return err;
942 }
943
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700944 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (err == 1)
946 goto retry;
947 if (err)
948 return err;
949
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700950 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800952EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Patrick McHardy4277a082006-03-20 18:52:01 -0800954int netlink_has_listeners(struct sock *sk, unsigned int group)
955{
956 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000957 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -0800958
Denis V. Lunevaed81562007-10-10 21:14:32 -0700959 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700960
961 rcu_read_lock();
962 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
963
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000964 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000965 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700966
967 rcu_read_unlock();
968
Patrick McHardy4277a082006-03-20 18:52:01 -0800969 return res;
970}
971EXPORT_SYMBOL_GPL(netlink_has_listeners);
972
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000973static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct netlink_sock *nlk = nlk_sk(sk);
976
977 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +0000978 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +0000979 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000980 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +0000981 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 return -1;
984}
985
986struct netlink_broadcast_data {
987 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200988 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000989 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 u32 group;
991 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800992 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 int congested;
994 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -0400995 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -0700997 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
998 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999};
1000
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001001static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct netlink_broadcast_data *p)
1003{
1004 struct netlink_sock *nlk = nlk_sk(sk);
1005 int val;
1006
1007 if (p->exclude_sk == sk)
1008 goto out;
1009
Eric W. Biederman15e47302012-09-07 20:12:54 +00001010 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001011 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto out;
1013
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001014 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001015 goto out;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (p->failure) {
1018 netlink_overrun(sk);
1019 goto out;
1020 }
1021
1022 sock_hold(sk);
1023 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001024 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 p->skb2 = skb_clone(p->skb, p->allocation);
1026 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001027 p->skb2 = skb_get(p->skb);
1028 /*
1029 * skb ownership may have been set when
1030 * delivered to a previous socket.
1031 */
1032 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 }
1035 if (p->skb2 == NULL) {
1036 netlink_overrun(sk);
1037 /* Clone failed. Notify ALL listeners. */
1038 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001039 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1040 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001041 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1042 kfree_skb(p->skb2);
1043 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001044 } else if (sk_filter(sk, p->skb2)) {
1045 kfree_skb(p->skb2);
1046 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1048 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001049 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1050 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 } else {
1052 p->congested |= val;
1053 p->delivered = 1;
1054 p->skb2 = NULL;
1055 }
1056 sock_put(sk);
1057
1058out:
1059 return 0;
1060}
1061
Eric W. Biederman15e47302012-09-07 20:12:54 +00001062int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001063 u32 group, gfp_t allocation,
1064 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1065 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001067 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct sock *sk;
1070
1071 skb = netlink_trim(skb, allocation);
1072
1073 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001074 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001075 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 info.group = group;
1077 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001078 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 info.congested = 0;
1080 info.delivered = 0;
1081 info.allocation = allocation;
1082 info.skb = skb;
1083 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001084 info.tx_filter = filter;
1085 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /* While we sleep in clone, do not allow to change socket list */
1088
1089 netlink_lock_table();
1090
Sasha Levinb67bfe02013-02-27 17:06:00 -08001091 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 do_one_broadcast(sk, &info);
1093
Neil Horman70d4bf62010-07-20 06:45:56 +00001094 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 netlink_unlock_table();
1097
Neil Horman70d4bf62010-07-20 06:45:56 +00001098 if (info.delivery_failure) {
1099 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001100 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001101 }
1102 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (info.delivered) {
1105 if (info.congested && (allocation & __GFP_WAIT))
1106 yield();
1107 return 0;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -ESRCH;
1110}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001111EXPORT_SYMBOL(netlink_broadcast_filtered);
1112
Eric W. Biederman15e47302012-09-07 20:12:54 +00001113int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001114 u32 group, gfp_t allocation)
1115{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001116 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001117 NULL, NULL);
1118}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001119EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121struct netlink_set_err_data {
1122 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001123 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 u32 group;
1125 int code;
1126};
1127
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001128static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001131 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 if (sk == p->exclude_sk)
1134 goto out;
1135
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001136 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001137 goto out;
1138
Eric W. Biederman15e47302012-09-07 20:12:54 +00001139 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001140 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 goto out;
1142
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001143 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1144 ret = 1;
1145 goto out;
1146 }
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 sk->sk_err = p->code;
1149 sk->sk_error_report(sk);
1150out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001151 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001154/**
1155 * netlink_set_err - report error to broadcast listeners
1156 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001157 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001158 * @groups: the broadcast group that will notice the error
1159 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001160 *
1161 * This function returns the number of broadcast listeners that have set the
1162 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001163 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001164int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001168 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001171 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001173 /* sk->sk_err wants a positive error value */
1174 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 read_lock(&nl_table_lock);
1177
Sasha Levinb67bfe02013-02-27 17:06:00 -08001178 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001179 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001184EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Johannes Berg84659eb2007-07-18 15:47:05 -07001186/* must be called with netlink table grabbed */
1187static void netlink_update_socket_mc(struct netlink_sock *nlk,
1188 unsigned int group,
1189 int is_new)
1190{
1191 int old, new = !!is_new, subscriptions;
1192
1193 old = test_bit(group - 1, nlk->groups);
1194 subscriptions = nlk->subscriptions - old + new;
1195 if (new)
1196 __set_bit(group - 1, nlk->groups);
1197 else
1198 __clear_bit(group - 1, nlk->groups);
1199 netlink_update_subscriptions(&nlk->sk, subscriptions);
1200 netlink_update_listeners(&nlk->sk);
1201}
1202
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001203static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001204 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001205{
1206 struct sock *sk = sock->sk;
1207 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001208 unsigned int val = 0;
1209 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001210
1211 if (level != SOL_NETLINK)
1212 return -ENOPROTOOPT;
1213
1214 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001215 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001216 return -EFAULT;
1217
1218 switch (optname) {
1219 case NETLINK_PKTINFO:
1220 if (val)
1221 nlk->flags |= NETLINK_RECV_PKTINFO;
1222 else
1223 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1224 err = 0;
1225 break;
1226 case NETLINK_ADD_MEMBERSHIP:
1227 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001228 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001229 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001230 err = netlink_realloc_groups(sk);
1231 if (err)
1232 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001233 if (!val || val - 1 >= nlk->ngroups)
1234 return -EINVAL;
1235 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001236 netlink_update_socket_mc(nlk, val,
1237 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001238 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001239
1240 if (nlk->netlink_bind)
1241 nlk->netlink_bind(val);
1242
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001243 err = 0;
1244 break;
1245 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001246 case NETLINK_BROADCAST_ERROR:
1247 if (val)
1248 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1249 else
1250 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1251 err = 0;
1252 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001253 case NETLINK_NO_ENOBUFS:
1254 if (val) {
1255 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00001256 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001257 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001258 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001259 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001260 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001261 err = 0;
1262 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001263 default:
1264 err = -ENOPROTOOPT;
1265 }
1266 return err;
1267}
1268
1269static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001270 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001271{
1272 struct sock *sk = sock->sk;
1273 struct netlink_sock *nlk = nlk_sk(sk);
1274 int len, val, err;
1275
1276 if (level != SOL_NETLINK)
1277 return -ENOPROTOOPT;
1278
1279 if (get_user(len, optlen))
1280 return -EFAULT;
1281 if (len < 0)
1282 return -EINVAL;
1283
1284 switch (optname) {
1285 case NETLINK_PKTINFO:
1286 if (len < sizeof(int))
1287 return -EINVAL;
1288 len = sizeof(int);
1289 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001290 if (put_user(len, optlen) ||
1291 put_user(val, optval))
1292 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001293 err = 0;
1294 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001295 case NETLINK_BROADCAST_ERROR:
1296 if (len < sizeof(int))
1297 return -EINVAL;
1298 len = sizeof(int);
1299 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1300 if (put_user(len, optlen) ||
1301 put_user(val, optval))
1302 return -EFAULT;
1303 err = 0;
1304 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001305 case NETLINK_NO_ENOBUFS:
1306 if (len < sizeof(int))
1307 return -EINVAL;
1308 len = sizeof(int);
1309 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1310 if (put_user(len, optlen) ||
1311 put_user(val, optval))
1312 return -EFAULT;
1313 err = 0;
1314 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001315 default:
1316 err = -ENOPROTOOPT;
1317 }
1318 return err;
1319}
1320
1321static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1322{
1323 struct nl_pktinfo info;
1324
1325 info.group = NETLINK_CB(skb).dst_group;
1326 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1330 struct msghdr *msg, size_t len)
1331{
1332 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1333 struct sock *sk = sock->sk;
1334 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001335 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001336 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001337 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct sk_buff *skb;
1339 int err;
1340 struct scm_cookie scm;
1341
1342 if (msg->msg_flags&MSG_OOB)
1343 return -EOPNOTSUPP;
1344
Eric Dumazet16e57262011-09-19 05:52:27 +00001345 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00001347
Eric Dumazete0e3cea2012-08-21 06:21:17 +00001348 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (err < 0)
1350 return err;
1351
1352 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001353 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001355 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001356 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001357 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001358 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001359 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001360 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001363 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001364 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
Eric W. Biederman15e47302012-09-07 20:12:54 +00001367 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 err = netlink_autobind(sock);
1369 if (err)
1370 goto out;
1371 }
1372
1373 err = -EMSGSIZE;
1374 if (len > sk->sk_sndbuf - 32)
1375 goto out;
1376 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001377 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001378 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto out;
1380
Eric W. Biederman15e47302012-09-07 20:12:54 +00001381 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001382 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00001383 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001386 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 kfree_skb(skb);
1388 goto out;
1389 }
1390
1391 err = security_netlink_send(sk, skb);
1392 if (err) {
1393 kfree_skb(skb);
1394 goto out;
1395 }
1396
Patrick McHardyd629b832005-08-14 19:27:50 -07001397 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001399 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001401 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001404 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return err;
1406}
1407
1408static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1409 struct msghdr *msg, size_t len,
1410 int flags)
1411{
1412 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1413 struct scm_cookie scm;
1414 struct sock *sk = sock->sk;
1415 struct netlink_sock *nlk = nlk_sk(sk);
1416 int noblock = flags&MSG_DONTWAIT;
1417 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001418 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001419 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 if (flags&MSG_OOB)
1422 return -EOPNOTSUPP;
1423
1424 copied = 0;
1425
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001426 skb = skb_recv_datagram(sk, flags, noblock, &err);
1427 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 goto out;
1429
Johannes Berg68d6ac62010-08-15 21:20:44 +00001430 data_skb = skb;
1431
Johannes Berg1dacc762009-07-01 11:26:02 +00001432#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1433 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00001434 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00001435 * If this skb has a frag_list, then here that means that we
1436 * will have to use the frag_list skb's data for compat tasks
1437 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00001438 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00001439 * If we need to send the compat skb, assign it to the
1440 * 'data_skb' variable so that it will be used below for data
1441 * copying. We keep 'skb' for everything else, including
1442 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00001443 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00001444 if (flags & MSG_CMSG_COMPAT)
1445 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00001446 }
1447#endif
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 msg->msg_namelen = 0;
1450
Johannes Berg68d6ac62010-08-15 21:20:44 +00001451 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (len < copied) {
1453 msg->msg_flags |= MSG_TRUNC;
1454 copied = len;
1455 }
1456
Johannes Berg68d6ac62010-08-15 21:20:44 +00001457 skb_reset_transport_header(data_skb);
1458 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001461 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 addr->nl_family = AF_NETLINK;
1463 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001464 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001465 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 msg->msg_namelen = sizeof(*addr);
1467 }
1468
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001469 if (nlk->flags & NETLINK_RECV_PKTINFO)
1470 netlink_cmsg_recv_pktinfo(msg, skb);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (NULL == siocb->scm) {
1473 memset(&scm, 0, sizeof(scm));
1474 siocb->scm = &scm;
1475 }
1476 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001477 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00001478 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 skb_free_datagram(sk, skb);
1481
Andrey Vaginb44d2112011-02-21 02:40:47 +00001482 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1483 ret = netlink_dump(sk);
1484 if (ret) {
1485 sk->sk_err = ret;
1486 sk->sk_error_report(sk);
1487 }
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491out:
1492 netlink_rcv_wake(sk);
1493 return err ? : copied;
1494}
1495
1496static void netlink_data_ready(struct sock *sk, int len)
1497{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001498 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001502 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 * complete set of kernel non-blocking support for message
1504 * queueing.
1505 */
1506
1507struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001508__netlink_kernel_create(struct net *net, int unit, struct module *module,
1509 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct socket *sock;
1512 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001513 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001514 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001515 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1516 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001518 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001520 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return NULL;
1522
1523 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1524 return NULL;
1525
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001526 /*
1527 * We have to just have a reference on the net from sk, but don't
1528 * get_net it. Besides, we cannot get and then put the net here.
1529 * So we create one inside init_net and the move it to net.
1530 */
1531
1532 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1533 goto out_sock_release_nosk;
1534
1535 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08001536 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001537
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001538 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08001539 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001540 else
1541 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001542
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001543 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08001544 if (!listeners)
1545 goto out_sock_release;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001548 if (cfg && cfg->input)
1549 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001551 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001552 goto out_sock_release;
1553
1554 nlk = nlk_sk(sk);
1555 nlk->flags |= NETLINK_KERNEL_SOCKET;
1556
1557 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001558 if (!nl_table[unit].registered) {
1559 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001560 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001561 nl_table[unit].cb_mutex = cb_mutex;
1562 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001563 if (cfg) {
1564 nl_table[unit].bind = cfg->bind;
1565 nl_table[unit].flags = cfg->flags;
1566 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001567 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07001568 } else {
1569 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001570 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001571 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07001572 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001573 return sk;
1574
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001575out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001576 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08001577 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001578 return NULL;
1579
1580out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001581 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001582 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001584EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001585
1586void
1587netlink_kernel_release(struct sock *sk)
1588{
Denis V. Lunevedf02082008-02-29 11:18:32 -08001589 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001590}
1591EXPORT_SYMBOL(netlink_kernel_release);
1592
Johannes Bergd136f1b2009-09-12 03:03:15 +00001593int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001594{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001595 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001596 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001597
1598 if (groups < 32)
1599 groups = 32;
1600
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001601 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001602 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1603 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00001604 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001605 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001606 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1607 rcu_assign_pointer(tbl->listeners, new);
1608
Lai Jiangshan37b6b932011-03-15 18:01:42 +08001609 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001610 }
1611 tbl->groups = groups;
1612
Johannes Bergd136f1b2009-09-12 03:03:15 +00001613 return 0;
1614}
1615
1616/**
1617 * netlink_change_ngroups - change number of multicast groups
1618 *
1619 * This changes the number of multicast groups that are available
1620 * on a certain netlink family. Note that it is not possible to
1621 * change the number of groups to below 32. Also note that it does
1622 * not implicitly call netlink_clear_multicast_users() when the
1623 * number of groups is reduced.
1624 *
1625 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1626 * @groups: The new number of groups.
1627 */
1628int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1629{
1630 int err;
1631
1632 netlink_table_grab();
1633 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001634 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00001635
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001636 return err;
1637}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001638
Johannes Bergb8273572009-09-24 15:44:05 -07001639void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1640{
1641 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07001642 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1643
Sasha Levinb67bfe02013-02-27 17:06:00 -08001644 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07001645 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1646}
1647
Johannes Berg84659eb2007-07-18 15:47:05 -07001648/**
1649 * netlink_clear_multicast_users - kick off multicast listeners
1650 *
1651 * This function removes all listeners from the given group.
1652 * @ksk: The kernel netlink socket, as returned by
1653 * netlink_kernel_create().
1654 * @group: The multicast group to clear.
1655 */
1656void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1657{
Johannes Berg84659eb2007-07-18 15:47:05 -07001658 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07001659 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07001660 netlink_table_ungrab();
1661}
Johannes Berg84659eb2007-07-18 15:47:05 -07001662
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001663struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00001664__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001665{
1666 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00001667 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001668
1669 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1670 nlh->nlmsg_type = type;
1671 nlh->nlmsg_len = size;
1672 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001673 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001674 nlh->nlmsg_seq = seq;
1675 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00001676 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001677 return nlh;
1678}
1679EXPORT_SYMBOL(__nlmsg_put);
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/*
1682 * It looks a bit ugly.
1683 * It would be better to create kernel thread.
1684 */
1685
1686static int netlink_dump(struct sock *sk)
1687{
1688 struct netlink_sock *nlk = nlk_sk(sk);
1689 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001690 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001692 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001693 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001695 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 cb = nlk->cb;
1698 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001699 err = -EINVAL;
1700 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702
Greg Rosec7ac8672011-06-10 01:27:09 +00001703 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1704
1705 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1706 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00001707 goto errout_skb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 len = cb->dump(skb, cb);
1710
1711 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001712 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001713
1714 if (sk_filter(sk, skb))
1715 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001716 else
1717 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719 }
1720
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001721 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1722 if (!nlh)
1723 goto errout_skb;
1724
Johannes Berg670dc282011-06-20 13:40:46 +02001725 nl_dump_check_consistent(cb, nlh);
1726
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001727 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1728
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001729 if (sk_filter(sk, skb))
1730 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001731 else
1732 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Thomas Grafa8f74b22005-11-10 02:25:52 +01001734 if (cb->done)
1735 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001737 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Gao feng6dc878a2012-10-04 20:15:48 +00001739 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001740 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001742
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001743errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001744 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001745 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Gao feng6dc878a2012-10-04 20:15:48 +00001749int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1750 const struct nlmsghdr *nlh,
1751 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 struct netlink_callback *cb;
1754 struct sock *sk;
1755 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001756 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001758 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (cb == NULL)
1760 return -ENOBUFS;
1761
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001762 cb->dump = control->dump;
1763 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00001765 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00001766 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001767 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 atomic_inc(&skb->users);
1769 cb->skb = skb;
1770
Eric W. Biederman15e47302012-09-07 20:12:54 +00001771 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (sk == NULL) {
1773 netlink_destroy_callback(cb);
1774 return -ECONNREFUSED;
1775 }
1776 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00001777
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001778 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00001779 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07001780 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001781 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00001783 ret = -EBUSY;
1784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
Gao feng6dc878a2012-10-04 20:15:48 +00001786 /* add reference of module which cb->dump belongs to */
1787 if (!try_module_get(cb->module)) {
1788 mutex_unlock(nlk->cb_mutex);
1789 netlink_destroy_callback(cb);
1790 ret = -EPROTONOSUPPORT;
1791 goto out;
1792 }
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001795 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Andrey Vaginb44d2112011-02-21 02:40:47 +00001797 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00001798out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001800
Andrey Vaginb44d2112011-02-21 02:40:47 +00001801 if (ret)
1802 return ret;
1803
Denis V. Lunev5c582982007-10-23 20:29:25 -07001804 /* We successfully started a dump, by returning -EINTR we
1805 * signal not to send ACK even if it was requested.
1806 */
1807 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
Gao feng6dc878a2012-10-04 20:15:48 +00001809EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1812{
1813 struct sk_buff *skb;
1814 struct nlmsghdr *rep;
1815 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08001816 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Thomas Graf339bf982006-11-10 14:10:15 -08001818 /* error messages get the original request appened */
1819 if (err)
1820 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Thomas Graf339bf982006-11-10 14:10:15 -08001822 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (!skb) {
1824 struct sock *sk;
1825
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001826 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001827 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001828 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (sk) {
1830 sk->sk_err = ENOBUFS;
1831 sk->sk_error_report(sk);
1832 sock_put(sk);
1833 }
1834 return;
1835 }
1836
Eric W. Biederman15e47302012-09-07 20:12:54 +00001837 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00001838 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001839 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001841 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00001842 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001844EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001846int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001847 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001848{
Thomas Graf82ace472005-11-10 02:25:53 +01001849 struct nlmsghdr *nlh;
1850 int err;
1851
1852 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001853 int msglen;
1854
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001855 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07001856 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01001857
Martin Murrayad8e4b72006-01-10 13:02:29 -08001858 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01001859 return 0;
1860
Thomas Grafd35b6852007-03-22 23:28:46 -07001861 /* Only requests are handled by the kernel */
1862 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07001863 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07001864
Thomas Graf45e7ae72007-03-22 23:29:10 -07001865 /* Skip control messages */
1866 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07001867 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07001868
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001869 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001870 if (err == -EINTR)
1871 goto skip;
1872
1873ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07001874 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01001875 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01001876
Denis V. Lunev5c582982007-10-23 20:29:25 -07001877skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001878 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001879 if (msglen > skb->len)
1880 msglen = skb->len;
1881 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01001882 }
1883
1884 return 0;
1885}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001886EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01001887
1888/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07001889 * nlmsg_notify - send a notification netlink message
1890 * @sk: netlink socket to use
1891 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00001892 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07001893 * @group: destination multicast group or 0
1894 * @report: 1 to report back, 0 to disable
1895 * @flags: allocation flags
1896 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001897int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07001898 unsigned int group, int report, gfp_t flags)
1899{
1900 int err = 0;
1901
1902 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001903 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07001904
1905 if (report) {
1906 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001907 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07001908 }
1909
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001910 /* errors reported via destination sk->sk_err, but propagate
1911 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001912 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001913 }
1914
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001915 if (report) {
1916 int err2;
1917
Eric W. Biederman15e47302012-09-07 20:12:54 +00001918 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001919 if (!err || err == -ESRCH)
1920 err = err2;
1921 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07001922
1923 return err;
1924}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001925EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927#ifdef CONFIG_PROC_FS
1928struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08001929 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 int link;
1931 int hash_idx;
1932};
1933
1934static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1935{
1936 struct nl_seq_iter *iter = seq->private;
1937 int i, j;
1938 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 loff_t off = 0;
1940
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001941 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001942 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001945 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001946 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001947 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (off == pos) {
1949 iter->link = i;
1950 iter->hash_idx = j;
1951 return s;
1952 }
1953 ++off;
1954 }
1955 }
1956 }
1957 return NULL;
1958}
1959
1960static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001961 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 read_lock(&nl_table_lock);
1964 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1965}
1966
1967static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1968{
1969 struct sock *s;
1970 struct nl_seq_iter *iter;
1971 int i, j;
1972
1973 ++*pos;
1974
1975 if (v == SEQ_START_TOKEN)
1976 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001977
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001978 iter = seq->private;
1979 s = v;
1980 do {
1981 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001982 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (s)
1984 return s;
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 i = iter->link;
1987 j = iter->hash_idx + 1;
1988
1989 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001990 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 for (; j <= hash->mask; j++) {
1993 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001994 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001995 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (s) {
1997 iter->link = i;
1998 iter->hash_idx = j;
1999 return s;
2000 }
2001 }
2002
2003 j = 0;
2004 } while (++i < MAX_LINKS);
2005
2006 return NULL;
2007}
2008
2009static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002010 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 read_unlock(&nl_table_lock);
2013}
2014
2015
2016static int netlink_seq_show(struct seq_file *seq, void *v)
2017{
Eric Dumazet658cb352012-04-22 21:30:21 +00002018 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 seq_puts(seq,
2020 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002021 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002022 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 struct sock *s = v;
2024 struct netlink_sock *nlk = nlk_sk(s);
2025
Hannes Frederic Sowa9f1e0ad2012-12-15 15:09:19 +00002026 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 s,
2028 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002029 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002030 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002031 sk_rmem_alloc_get(s),
2032 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002034 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002035 atomic_read(&s->sk_drops),
2036 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 );
2038
2039 }
2040 return 0;
2041}
2042
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002043static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 .start = netlink_seq_start,
2045 .next = netlink_seq_next,
2046 .stop = netlink_seq_stop,
2047 .show = netlink_seq_show,
2048};
2049
2050
2051static int netlink_seq_open(struct inode *inode, struct file *file)
2052{
Denis V. Luneve372c412007-11-19 22:31:54 -08002053 return seq_open_net(inode, file, &netlink_seq_ops,
2054 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002055}
2056
Arjan van de Venda7071d2007-02-12 00:55:36 -08002057static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 .owner = THIS_MODULE,
2059 .open = netlink_seq_open,
2060 .read = seq_read,
2061 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002062 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063};
2064
2065#endif
2066
2067int netlink_register_notifier(struct notifier_block *nb)
2068{
Alan Sterne041c682006-03-27 01:16:30 -08002069 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002071EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073int netlink_unregister_notifier(struct notifier_block *nb)
2074{
Alan Sterne041c682006-03-27 01:16:30 -08002075 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002077EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002078
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002079static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 .family = PF_NETLINK,
2081 .owner = THIS_MODULE,
2082 .release = netlink_release,
2083 .bind = netlink_bind,
2084 .connect = netlink_connect,
2085 .socketpair = sock_no_socketpair,
2086 .accept = sock_no_accept,
2087 .getname = netlink_getname,
2088 .poll = datagram_poll,
2089 .ioctl = sock_no_ioctl,
2090 .listen = sock_no_listen,
2091 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002092 .setsockopt = netlink_setsockopt,
2093 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 .sendmsg = netlink_sendmsg,
2095 .recvmsg = netlink_recvmsg,
2096 .mmap = sock_no_mmap,
2097 .sendpage = sock_no_sendpage,
2098};
2099
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002100static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 .family = PF_NETLINK,
2102 .create = netlink_create,
2103 .owner = THIS_MODULE, /* for consistency 8) */
2104};
2105
Pavel Emelyanov46650792007-10-08 20:38:39 -07002106static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002107{
2108#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002109 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002110 return -ENOMEM;
2111#endif
2112 return 0;
2113}
2114
Pavel Emelyanov46650792007-10-08 20:38:39 -07002115static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002116{
2117#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002118 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002119#endif
2120}
2121
David S. Millerb963ea82010-08-30 19:08:01 -07002122static void __init netlink_add_usersock_entry(void)
2123{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002124 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002125 int groups = 32;
2126
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002127 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002128 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002129 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002130
2131 netlink_table_grab();
2132
2133 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002134 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002135 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2136 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002137 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002138
2139 netlink_table_ungrab();
2140}
2141
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002142static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002143 .init = netlink_net_init,
2144 .exit = netlink_net_exit,
2145};
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147static int __init netlink_proto_init(void)
2148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002150 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 unsigned int order;
2152 int err = proto_register(&netlink_proto, 0);
2153
2154 if (err != 0)
2155 goto out;
2156
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002157 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002159 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002160 if (!nl_table)
2161 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Jan Beulich44813742009-09-21 17:03:05 -07002163 if (totalram_pages >= (128 * 1024))
2164 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 else
Jan Beulich44813742009-09-21 17:03:05 -07002166 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002168 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2169 limit = (1UL << order) / sizeof(struct hlist_head);
2170 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002173 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Eric W. Biederman15e47302012-09-07 20:12:54 +00002175 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (!hash->table) {
2177 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002178 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 1 * sizeof(*hash->table));
2180 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002181 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 hash->max_shift = order;
2184 hash->shift = 0;
2185 hash->mask = 0;
2186 hash->rehash_time = jiffies;
2187 }
2188
David S. Millerb963ea82010-08-30 19:08:01 -07002189 netlink_add_usersock_entry();
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002192 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002193 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 rtnetlink_init();
2195out:
2196 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002197panic:
2198 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201core_initcall(netlink_proto_init);