blob: 01e944a017a4db6be96c3d4a8e7adc8d7890b34e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
15 * use nlk_sk, as sk->protinfo is on a diet 8)
Harald Welte4fdb3bb2005-08-09 19:40:55 -070016 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17 * - inc module use count of module that owns
18 * the kernel socket in case userspace opens
19 * socket of same protocol
20 * - remove all module support, since netlink is
21 * mandatory if CONFIG_NET=y these days
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25
Randy Dunlap4fc268d2006-01-11 12:17:47 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/signal.h>
30#include <linux/sched.h>
31#include <linux/errno.h>
32#include <linux/string.h>
33#include <linux/stat.h>
34#include <linux/socket.h>
35#include <linux/un.h>
36#include <linux/fcntl.h>
37#include <linux/termios.h>
38#include <linux/sockios.h>
39#include <linux/net.h>
40#include <linux/fs.h>
41#include <linux/slab.h>
42#include <asm/uaccess.h>
43#include <linux/skbuff.h>
44#include <linux/netdevice.h>
45#include <linux/rtnetlink.h>
46#include <linux/proc_fs.h>
47#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/notifier.h>
49#include <linux/security.h>
50#include <linux/jhash.h>
51#include <linux/jiffies.h>
52#include <linux/random.h>
53#include <linux/bitops.h>
54#include <linux/mm.h>
55#include <linux/types.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010056#include <linux/audit.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070057#include <linux/mutex.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010058
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020059#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <net/sock.h>
61#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010062#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070064#define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
Johannes Bergb4ff4f02007-07-18 15:46:06 -070065#define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67struct netlink_sock {
68 /* struct sock has to be the first member of netlink_sock */
69 struct sock sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +000070 u32 portid;
71 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -070072 u32 dst_group;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -070073 u32 flags;
74 u32 subscriptions;
75 u32 ngroups;
76 unsigned long *groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned long state;
78 wait_queue_head_t wait;
79 struct netlink_callback *cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070080 struct mutex *cb_mutex;
81 struct mutex cb_def_mutex;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -070082 void (*netlink_rcv)(struct sk_buff *skb);
Pablo Neira Ayuso03292742012-06-29 06:15:22 +000083 void (*netlink_bind)(int group);
Patrick McHardy77247bb2005-08-14 19:27:13 -070084 struct module *module;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Eric Dumazet5c398dc2010-10-24 04:27:10 +000087struct listeners {
88 struct rcu_head rcu;
89 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000090};
91
Patrick McHardy77247bb2005-08-14 19:27:13 -070092#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070093#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000094#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070095#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static inline struct netlink_sock *nlk_sk(struct sock *sk)
98{
Denis Cheng32b21e02007-08-28 15:41:11 -070099 return container_of(sk, struct netlink_sock, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
David S. Miller035c4c12011-12-23 17:33:03 -0500102static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -0700103{
104 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
105}
106
Eric W. Biederman15e47302012-09-07 20:12:54 +0000107struct nl_portid_hash {
Eric Dumazet658cb352012-04-22 21:30:21 +0000108 struct hlist_head *table;
109 unsigned long rehash_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Eric Dumazet658cb352012-04-22 21:30:21 +0000111 unsigned int mask;
112 unsigned int shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Eric Dumazet658cb352012-04-22 21:30:21 +0000114 unsigned int entries;
115 unsigned int max_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Eric Dumazet658cb352012-04-22 21:30:21 +0000117 u32 rnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct netlink_table {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000121 struct nl_portid_hash hash;
Eric Dumazet658cb352012-04-22 21:30:21 +0000122 struct hlist_head mc_list;
123 struct listeners __rcu *listeners;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000124 unsigned int flags;
Eric Dumazet658cb352012-04-22 21:30:21 +0000125 unsigned int groups;
126 struct mutex *cb_mutex;
127 struct module *module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000128 void (*bind)(int group);
Eric Dumazet658cb352012-04-22 21:30:21 +0000129 int registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132static struct netlink_table *nl_table;
133
134static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
135
136static int netlink_dump(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138static DEFINE_RWLOCK(nl_table_lock);
139static atomic_t nl_table_users = ATOMIC_INIT(0);
140
Alan Sterne041c682006-03-27 01:16:30 -0800141static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000143static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700144{
145 return group ? 1 << (group - 1) : 0;
146}
147
Eric W. Biederman15e47302012-09-07 20:12:54 +0000148static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000150 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Eric Dumazet658cb352012-04-22 21:30:21 +0000153static void netlink_destroy_callback(struct netlink_callback *cb)
154{
155 kfree_skb(cb->skb);
156 kfree(cb);
157}
158
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000159static void netlink_consume_callback(struct netlink_callback *cb)
160{
161 consume_skb(cb->skb);
162 kfree(cb);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void netlink_sock_destruct(struct sock *sk)
166{
Herbert Xu3f660d62007-05-03 03:17:14 -0700167 struct netlink_sock *nlk = nlk_sk(sk);
168
Herbert Xu3f660d62007-05-03 03:17:14 -0700169 if (nlk->cb) {
170 if (nlk->cb->done)
171 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000172
173 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700174 netlink_destroy_callback(nlk->cb);
175 }
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 skb_queue_purge(&sk->sk_receive_queue);
178
179 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800180 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return;
182 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700183
184 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
185 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
186 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800189/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
190 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
192 * this, _but_ remember, it adds useless work on UP machines.
193 */
194
Johannes Bergd136f1b2009-09-12 03:03:15 +0000195void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800196 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000198 might_sleep();
199
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700200 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (atomic_read(&nl_table_users)) {
203 DECLARE_WAITQUEUE(wait, current);
204
205 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800206 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 set_current_state(TASK_UNINTERRUPTIBLE);
208 if (atomic_read(&nl_table_users) == 0)
209 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700210 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700212 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215 __set_current_state(TASK_RUNNING);
216 remove_wait_queue(&nl_table_wait, &wait);
217 }
218}
219
Johannes Bergd136f1b2009-09-12 03:03:15 +0000220void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800221 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700223 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 wake_up(&nl_table_wait);
225}
226
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800227static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228netlink_lock_table(void)
229{
230 /* read_lock() synchronizes us to netlink_table_grab */
231
232 read_lock(&nl_table_lock);
233 atomic_inc(&nl_table_users);
234 read_unlock(&nl_table_lock);
235}
236
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800237static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238netlink_unlock_table(void)
239{
240 if (atomic_dec_and_test(&nl_table_users))
241 wake_up(&nl_table_wait);
242}
243
Eric W. Biederman15e47302012-09-07 20:12:54 +0000244static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000246 struct nl_portid_hash *hash = &nl_table[protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 struct hlist_head *head;
248 struct sock *sk;
249 struct hlist_node *node;
250
251 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000252 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 sk_for_each(sk, node, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000254 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 sock_hold(sk);
256 goto found;
257 }
258 }
259 sk = NULL;
260found:
261 read_unlock(&nl_table_lock);
262 return sk;
263}
264
Eric W. Biederman15e47302012-09-07 20:12:54 +0000265static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800268 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 else
270 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800271 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
272 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Eric W. Biederman15e47302012-09-07 20:12:54 +0000275static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 if (size <= PAGE_SIZE)
278 kfree(table);
279 else
280 free_pages((unsigned long)table, get_order(size));
281}
282
Eric W. Biederman15e47302012-09-07 20:12:54 +0000283static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 unsigned int omask, mask, shift;
286 size_t osize, size;
287 struct hlist_head *otable, *table;
288 int i;
289
290 omask = mask = hash->mask;
291 osize = size = (mask + 1) * sizeof(*table);
292 shift = hash->shift;
293
294 if (grow) {
295 if (++shift > hash->max_shift)
296 return 0;
297 mask = mask * 2 + 1;
298 size *= 2;
299 }
300
Eric W. Biederman15e47302012-09-07 20:12:54 +0000301 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!table)
303 return 0;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 otable = hash->table;
306 hash->table = table;
307 hash->mask = mask;
308 hash->shift = shift;
309 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
310
311 for (i = 0; i <= omask; i++) {
312 struct sock *sk;
313 struct hlist_node *node, *tmp;
314
315 sk_for_each_safe(sk, node, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000316 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
Eric W. Biederman15e47302012-09-07 20:12:54 +0000319 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 hash->rehash_time = jiffies + 10 * 60 * HZ;
321 return 1;
322}
323
Eric W. Biederman15e47302012-09-07 20:12:54 +0000324static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int avg = hash->entries >> hash->shift;
327
Eric W. Biederman15e47302012-09-07 20:12:54 +0000328 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 1;
330
331 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000332 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 1;
334 }
335
336 return 0;
337}
338
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800339static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Patrick McHardy4277a082006-03-20 18:52:01 -0800341static void
342netlink_update_listeners(struct sock *sk)
343{
344 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
345 struct hlist_node *node;
346 unsigned long mask;
347 unsigned int i;
348
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700349 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800350 mask = 0;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700351 sk_for_each_bound(sk, node, &tbl->mc_list) {
352 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
353 mask |= nlk_sk(sk)->groups[i];
354 }
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000355 tbl->listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800356 }
357 /* this function is only called with the netlink table "grabbed", which
358 * makes sure updates are visible before bind or setsockopt return. */
359}
360
Eric W. Biederman15e47302012-09-07 20:12:54 +0000361static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000363 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct hlist_head *head;
365 int err = -EADDRINUSE;
366 struct sock *osk;
367 struct hlist_node *node;
368 int len;
369
370 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000371 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 len = 0;
373 sk_for_each(osk, node, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000374 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
376 len++;
377 }
378 if (node)
379 goto err;
380
381 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000382 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto err;
384
385 err = -ENOMEM;
386 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
387 goto err;
388
Eric W. Biederman15e47302012-09-07 20:12:54 +0000389 if (len && nl_portid_hash_dilute(hash, len))
390 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000392 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sk_add_node(sk, head);
394 err = 0;
395
396err:
397 netlink_table_ungrab();
398 return err;
399}
400
401static void netlink_remove(struct sock *sk)
402{
403 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700404 if (sk_del_node_init(sk))
405 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700406 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 __sk_del_bind_node(sk);
408 netlink_table_ungrab();
409}
410
411static struct proto netlink_proto = {
412 .name = "NETLINK",
413 .owner = THIS_MODULE,
414 .obj_size = sizeof(struct netlink_sock),
415};
416
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700417static int __netlink_create(struct net *net, struct socket *sock,
418 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct sock *sk;
421 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700422
423 sock->ops = &netlink_ops;
424
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700425 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700426 if (!sk)
427 return -ENOMEM;
428
429 sock_init_data(sock, sk);
430
431 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000432 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700433 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000434 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700435 nlk->cb_mutex = &nlk->cb_def_mutex;
436 mutex_init(nlk->cb_mutex);
437 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700438 init_waitqueue_head(&nlk->wait);
439
440 sk->sk_destruct = netlink_sock_destruct;
441 sk->sk_protocol = protocol;
442 return 0;
443}
444
Eric Paris3f378b62009-11-05 22:18:14 -0800445static int netlink_create(struct net *net, struct socket *sock, int protocol,
446 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700447{
448 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700449 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700450 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000451 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -0700452 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 sock->state = SS_UNCONNECTED;
455
456 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
457 return -ESOCKTNOSUPPORT;
458
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800459 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return -EPROTONOSUPPORT;
461
Patrick McHardy77247bb2005-08-14 19:27:13 -0700462 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700463#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700464 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700465 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700466 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700467 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700468 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700469#endif
470 if (nl_table[protocol].registered &&
471 try_module_get(nl_table[protocol].module))
472 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000473 else
474 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700475 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000476 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700477 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700478
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000479 if (err < 0)
480 goto out;
481
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800482 err = __netlink_create(net, sock, cb_mutex, protocol);
483 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700484 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David S. Miller6f756a82008-11-23 17:34:03 -0800486 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800487 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800488 local_bh_enable();
489
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700490 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700491 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000492 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -0700493out:
494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Patrick McHardyab33a172005-08-14 19:31:36 -0700496out_module:
497 module_put(module);
498 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501static int netlink_release(struct socket *sock)
502{
503 struct sock *sk = sock->sk;
504 struct netlink_sock *nlk;
505
506 if (!sk)
507 return 0;
508
509 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700510 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 nlk = nlk_sk(sk);
512
Herbert Xu3f660d62007-05-03 03:17:14 -0700513 /*
514 * OK. Socket is unlinked, any packets that arrive now
515 * will be purged.
516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 sock->sk = NULL;
519 wake_up_interruptible_all(&nlk->wait);
520
521 skb_queue_purge(&sk->sk_write_queue);
522
Eric W. Biederman15e47302012-09-07 20:12:54 +0000523 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900525 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000527 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 };
Alan Sterne041c682006-03-27 01:16:30 -0800529 atomic_notifier_call_chain(&netlink_chain,
530 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900531 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700532
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800533 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700534
Patrick McHardy4277a082006-03-20 18:52:01 -0800535 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -0700536 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800537 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
538 if (--nl_table[sk->sk_protocol].registered == 0) {
539 kfree(nl_table[sk->sk_protocol].listeners);
540 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000541 nl_table[sk->sk_protocol].bind = NULL;
542 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800543 nl_table[sk->sk_protocol].registered = 0;
544 }
Eric Dumazet658cb352012-04-22 21:30:21 +0000545 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800546 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000547 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800548 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700549
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700550 kfree(nlk->groups);
551 nlk->groups = NULL;
552
Eric Dumazet37558102008-11-24 14:05:22 -0800553 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800554 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800555 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 sock_put(sk);
557 return 0;
558}
559
560static int netlink_autobind(struct socket *sock)
561{
562 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900563 struct net *net = sock_net(sk);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000564 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct hlist_head *head;
566 struct sock *osk;
567 struct hlist_node *node;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000568 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int err;
570 static s32 rover = -4097;
571
572retry:
573 cond_resched();
574 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000575 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 sk_for_each(osk, node, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900577 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200578 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000579 if (nlk_sk(osk)->portid == portid) {
580 /* Bind collision, search negative portid values. */
581 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (rover > -4097)
583 rover = -4097;
584 netlink_table_ungrab();
585 goto retry;
586 }
587 }
588 netlink_table_ungrab();
589
Eric W. Biederman15e47302012-09-07 20:12:54 +0000590 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (err == -EADDRINUSE)
592 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700593
594 /* If 2 threads race to autobind, that is fine. */
595 if (err == -EBUSY)
596 err = 0;
597
598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000601static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900602{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000603 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 capable(CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700607static void
608netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
609{
610 struct netlink_sock *nlk = nlk_sk(sk);
611
612 if (nlk->subscriptions && !subscriptions)
613 __sk_del_bind_node(sk);
614 else if (!nlk->subscriptions && subscriptions)
615 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
616 nlk->subscriptions = subscriptions;
617}
618
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700619static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700620{
621 struct netlink_sock *nlk = nlk_sk(sk);
622 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700623 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700624 int err = 0;
625
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700626 netlink_table_grab();
627
Patrick McHardy513c2502005-09-06 15:43:59 -0700628 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700629 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700630 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700631 goto out_unlock;
632 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700633
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700634 if (nlk->ngroups >= groups)
635 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700636
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700637 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
638 if (new_groups == NULL) {
639 err = -ENOMEM;
640 goto out_unlock;
641 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800642 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700643 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
644
645 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700646 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700647 out_unlock:
648 netlink_table_ungrab();
649 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700650}
651
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800652static int netlink_bind(struct socket *sock, struct sockaddr *addr,
653 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900656 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct netlink_sock *nlk = nlk_sk(sk);
658 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
659 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (nladdr->nl_family != AF_NETLINK)
662 return -EINVAL;
663
664 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700665 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000666 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -0700667 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700668 err = netlink_realloc_groups(sk);
669 if (err)
670 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Eric W. Biederman15e47302012-09-07 20:12:54 +0000673 if (nlk->portid) {
674 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -EINVAL;
676 } else {
677 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200678 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 netlink_autobind(sock);
680 if (err)
681 return err;
682 }
683
Patrick McHardy513c2502005-09-06 15:43:59 -0700684 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
686
687 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700688 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900689 hweight32(nladdr->nl_groups) -
690 hweight32(nlk->groups[0]));
691 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800692 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 netlink_table_ungrab();
694
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000695 if (nlk->netlink_bind && nlk->groups[0]) {
696 int i;
697
698 for (i=0; i<nlk->ngroups; i++) {
699 if (test_bit(i, nlk->groups))
700 nlk->netlink_bind(i);
701 }
702 }
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705}
706
707static int netlink_connect(struct socket *sock, struct sockaddr *addr,
708 int alen, int flags)
709{
710 int err = 0;
711 struct sock *sk = sock->sk;
712 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800713 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Changli Gao6503d962010-03-31 22:58:26 +0000715 if (alen < sizeof(addr->sa_family))
716 return -EINVAL;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (addr->sa_family == AF_UNSPEC) {
719 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000720 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700721 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723 }
724 if (addr->sa_family != AF_NETLINK)
725 return -EINVAL;
726
727 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000728 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EPERM;
730
Eric W. Biederman15e47302012-09-07 20:12:54 +0000731 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 err = netlink_autobind(sock);
733
734 if (err == 0) {
735 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000736 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700737 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
740 return err;
741}
742
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800743static int netlink_getname(struct socket *sock, struct sockaddr *addr,
744 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct sock *sk = sock->sk;
747 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +0000748 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 nladdr->nl_family = AF_NETLINK;
751 nladdr->nl_pad = 0;
752 *addr_len = sizeof(*nladdr);
753
754 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000755 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700756 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000758 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700759 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 return 0;
762}
763
764static void netlink_overrun(struct sock *sk)
765{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700766 struct netlink_sock *nlk = nlk_sk(sk);
767
768 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
769 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
770 sk->sk_err = ENOBUFS;
771 sk->sk_error_report(sk);
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700774 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Eric W. Biederman15e47302012-09-07 20:12:54 +0000777static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct sock *sock;
780 struct netlink_sock *nlk;
781
Eric W. Biederman15e47302012-09-07 20:12:54 +0000782 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!sock)
784 return ERR_PTR(-ECONNREFUSED);
785
786 /* Don't bother queuing skb if kernel socket has no input function */
787 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700788 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +0000789 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 sock_put(sock);
791 return ERR_PTR(-ECONNREFUSED);
792 }
793 return sock;
794}
795
796struct sock *netlink_getsockbyfilp(struct file *filp)
797{
Josef Sipek6db5fc52006-12-08 02:37:25 -0800798 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct sock *sock;
800
801 if (!S_ISSOCK(inode->i_mode))
802 return ERR_PTR(-ENOTSOCK);
803
804 sock = SOCKET_I(inode)->sk;
805 if (sock->sk_family != AF_NETLINK)
806 return ERR_PTR(-EINVAL);
807
808 sock_hold(sock);
809 return sock;
810}
811
812/*
813 * Attach a skb to a netlink socket.
814 * The caller must hold a reference to the destination socket. On error, the
815 * reference is dropped. The skb is not send to the destination, just all
816 * all error checks are performed and memory in the queue is reserved.
817 * Return values:
818 * < 0: error. skb freed, reference to sock dropped.
819 * 0: continue
820 * 1: repeat lookup - reference dropped while waiting for socket memory.
821 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700822int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800823 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct netlink_sock *nlk;
826
827 nlk = nlk_sk(sk);
828
829 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
830 test_bit(0, &nlk->state)) {
831 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800832 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -0700833 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 netlink_overrun(sk);
835 sock_put(sk);
836 kfree_skb(skb);
837 return -EAGAIN;
838 }
839
840 __set_current_state(TASK_INTERRUPTIBLE);
841 add_wait_queue(&nlk->wait, &wait);
842
843 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
844 test_bit(0, &nlk->state)) &&
845 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800846 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 __set_current_state(TASK_RUNNING);
849 remove_wait_queue(&nlk->wait, &wait);
850 sock_put(sk);
851
852 if (signal_pending(current)) {
853 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800854 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 return 1;
857 }
858 skb_set_owner_r(skb, sk);
859 return 0;
860}
861
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000862static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int len = skb->len;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 skb_queue_tail(&sk->sk_receive_queue, skb);
867 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000868 return len;
869}
870
871int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
872{
873 int len = __netlink_sendskb(sk, skb);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 sock_put(sk);
876 return len;
877}
878
879void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
880{
881 kfree_skb(skb);
882 sock_put(sk);
883}
884
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000885static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 int delta;
888
889 skb_orphan(skb);
890
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700891 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (delta * 2 < skb->truesize)
893 return skb;
894
895 if (skb_shared(skb)) {
896 struct sk_buff *nskb = skb_clone(skb, allocation);
897 if (!nskb)
898 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +0000899 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 skb = nskb;
901 }
902
903 if (!pskb_expand_head(skb, 0, -delta, allocation))
904 skb->truesize -= delta;
905
906 return skb;
907}
908
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000909static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700910{
911 struct netlink_sock *nlk = nlk_sk(sk);
912
913 if (skb_queue_empty(&sk->sk_receive_queue))
914 clear_bit(0, &nlk->state);
915 if (!test_bit(0, &nlk->state))
916 wake_up_interruptible(&nlk->wait);
917}
918
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600919static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
920 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700921{
922 int ret;
923 struct netlink_sock *nlk = nlk_sk(sk);
924
925 ret = -ECONNREFUSED;
926 if (nlk->netlink_rcv != NULL) {
927 ret = skb->len;
928 skb_set_owner_r(skb, sk);
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600929 NETLINK_CB(skb).ssk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700930 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000931 consume_skb(skb);
932 } else {
933 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700934 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700935 sock_put(sk);
936 return ret;
937}
938
939int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000940 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 struct sock *sk;
943 int err;
944 long timeo;
945
946 skb = netlink_trim(skb, gfp_any());
947
948 timeo = sock_sndtimeo(ssk, nonblock);
949retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000950 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (IS_ERR(sk)) {
952 kfree_skb(skb);
953 return PTR_ERR(sk);
954 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700955 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600956 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700957
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700958 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -0700959 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700960 kfree_skb(skb);
961 sock_put(sk);
962 return err;
963 }
964
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700965 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (err == 1)
967 goto retry;
968 if (err)
969 return err;
970
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700971 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800973EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Patrick McHardy4277a082006-03-20 18:52:01 -0800975int netlink_has_listeners(struct sock *sk, unsigned int group)
976{
977 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000978 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -0800979
Denis V. Lunevaed81562007-10-10 21:14:32 -0700980 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700981
982 rcu_read_lock();
983 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
984
Patrick McHardy4277a082006-03-20 18:52:01 -0800985 if (group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000986 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700987
988 rcu_read_unlock();
989
Patrick McHardy4277a082006-03-20 18:52:01 -0800990 return res;
991}
992EXPORT_SYMBOL_GPL(netlink_has_listeners);
993
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000994static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct netlink_sock *nlk = nlk_sk(sk);
997
998 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
999 !test_bit(0, &nlk->state)) {
1000 skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001001 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001002 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004 return -1;
1005}
1006
1007struct netlink_broadcast_data {
1008 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001009 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001010 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 u32 group;
1012 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001013 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int congested;
1015 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001016 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001018 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1019 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020};
1021
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001022static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct netlink_broadcast_data *p)
1024{
1025 struct netlink_sock *nlk = nlk_sk(sk);
1026 int val;
1027
1028 if (p->exclude_sk == sk)
1029 goto out;
1030
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001032 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 goto out;
1034
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001035 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001036 goto out;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (p->failure) {
1039 netlink_overrun(sk);
1040 goto out;
1041 }
1042
1043 sock_hold(sk);
1044 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001045 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 p->skb2 = skb_clone(p->skb, p->allocation);
1047 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001048 p->skb2 = skb_get(p->skb);
1049 /*
1050 * skb ownership may have been set when
1051 * delivered to a previous socket.
1052 */
1053 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055 }
1056 if (p->skb2 == NULL) {
1057 netlink_overrun(sk);
1058 /* Clone failed. Notify ALL listeners. */
1059 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001060 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1061 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001062 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1063 kfree_skb(p->skb2);
1064 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001065 } else if (sk_filter(sk, p->skb2)) {
1066 kfree_skb(p->skb2);
1067 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1069 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001070 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1071 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 } else {
1073 p->congested |= val;
1074 p->delivered = 1;
1075 p->skb2 = NULL;
1076 }
1077 sock_put(sk);
1078
1079out:
1080 return 0;
1081}
1082
Eric W. Biederman15e47302012-09-07 20:12:54 +00001083int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001084 u32 group, gfp_t allocation,
1085 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1086 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001088 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct netlink_broadcast_data info;
1090 struct hlist_node *node;
1091 struct sock *sk;
1092
1093 skb = netlink_trim(skb, allocation);
1094
1095 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001096 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001097 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 info.group = group;
1099 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001100 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 info.congested = 0;
1102 info.delivered = 0;
1103 info.allocation = allocation;
1104 info.skb = skb;
1105 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001106 info.tx_filter = filter;
1107 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* While we sleep in clone, do not allow to change socket list */
1110
1111 netlink_lock_table();
1112
1113 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1114 do_one_broadcast(sk, &info);
1115
Neil Horman70d4bf62010-07-20 06:45:56 +00001116 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 netlink_unlock_table();
1119
Neil Horman70d4bf62010-07-20 06:45:56 +00001120 if (info.delivery_failure) {
1121 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001122 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001123 }
1124 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (info.delivered) {
1127 if (info.congested && (allocation & __GFP_WAIT))
1128 yield();
1129 return 0;
1130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -ESRCH;
1132}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001133EXPORT_SYMBOL(netlink_broadcast_filtered);
1134
Eric W. Biederman15e47302012-09-07 20:12:54 +00001135int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001136 u32 group, gfp_t allocation)
1137{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001138 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001139 NULL, NULL);
1140}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001141EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143struct netlink_set_err_data {
1144 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001145 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 u32 group;
1147 int code;
1148};
1149
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001150static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001153 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 if (sk == p->exclude_sk)
1156 goto out;
1157
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001158 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001159 goto out;
1160
Eric W. Biederman15e47302012-09-07 20:12:54 +00001161 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001162 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto out;
1164
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001165 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1166 ret = 1;
1167 goto out;
1168 }
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 sk->sk_err = p->code;
1171 sk->sk_error_report(sk);
1172out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001176/**
1177 * netlink_set_err - report error to broadcast listeners
1178 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001179 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001180 * @groups: the broadcast group that will notice the error
1181 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001182 *
1183 * This function returns the number of broadcast listeners that have set the
1184 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001185 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001186int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct netlink_set_err_data info;
1189 struct hlist_node *node;
1190 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001191 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001194 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001196 /* sk->sk_err wants a positive error value */
1197 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 read_lock(&nl_table_lock);
1200
1201 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001202 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001205 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001207EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Johannes Berg84659eb2007-07-18 15:47:05 -07001209/* must be called with netlink table grabbed */
1210static void netlink_update_socket_mc(struct netlink_sock *nlk,
1211 unsigned int group,
1212 int is_new)
1213{
1214 int old, new = !!is_new, subscriptions;
1215
1216 old = test_bit(group - 1, nlk->groups);
1217 subscriptions = nlk->subscriptions - old + new;
1218 if (new)
1219 __set_bit(group - 1, nlk->groups);
1220 else
1221 __clear_bit(group - 1, nlk->groups);
1222 netlink_update_subscriptions(&nlk->sk, subscriptions);
1223 netlink_update_listeners(&nlk->sk);
1224}
1225
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001226static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001227 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001228{
1229 struct sock *sk = sock->sk;
1230 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001231 unsigned int val = 0;
1232 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001233
1234 if (level != SOL_NETLINK)
1235 return -ENOPROTOOPT;
1236
1237 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001238 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001239 return -EFAULT;
1240
1241 switch (optname) {
1242 case NETLINK_PKTINFO:
1243 if (val)
1244 nlk->flags |= NETLINK_RECV_PKTINFO;
1245 else
1246 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1247 err = 0;
1248 break;
1249 case NETLINK_ADD_MEMBERSHIP:
1250 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001251 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001252 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001253 err = netlink_realloc_groups(sk);
1254 if (err)
1255 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001256 if (!val || val - 1 >= nlk->ngroups)
1257 return -EINVAL;
1258 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001259 netlink_update_socket_mc(nlk, val,
1260 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001261 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001262
1263 if (nlk->netlink_bind)
1264 nlk->netlink_bind(val);
1265
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001266 err = 0;
1267 break;
1268 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001269 case NETLINK_BROADCAST_ERROR:
1270 if (val)
1271 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1272 else
1273 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1274 err = 0;
1275 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001276 case NETLINK_NO_ENOBUFS:
1277 if (val) {
1278 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1279 clear_bit(0, &nlk->state);
1280 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001281 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001282 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001283 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001284 err = 0;
1285 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001286 default:
1287 err = -ENOPROTOOPT;
1288 }
1289 return err;
1290}
1291
1292static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001293 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001294{
1295 struct sock *sk = sock->sk;
1296 struct netlink_sock *nlk = nlk_sk(sk);
1297 int len, val, err;
1298
1299 if (level != SOL_NETLINK)
1300 return -ENOPROTOOPT;
1301
1302 if (get_user(len, optlen))
1303 return -EFAULT;
1304 if (len < 0)
1305 return -EINVAL;
1306
1307 switch (optname) {
1308 case NETLINK_PKTINFO:
1309 if (len < sizeof(int))
1310 return -EINVAL;
1311 len = sizeof(int);
1312 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001313 if (put_user(len, optlen) ||
1314 put_user(val, optval))
1315 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001316 err = 0;
1317 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001318 case NETLINK_BROADCAST_ERROR:
1319 if (len < sizeof(int))
1320 return -EINVAL;
1321 len = sizeof(int);
1322 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1323 if (put_user(len, optlen) ||
1324 put_user(val, optval))
1325 return -EFAULT;
1326 err = 0;
1327 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001328 case NETLINK_NO_ENOBUFS:
1329 if (len < sizeof(int))
1330 return -EINVAL;
1331 len = sizeof(int);
1332 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1333 if (put_user(len, optlen) ||
1334 put_user(val, optval))
1335 return -EFAULT;
1336 err = 0;
1337 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001338 default:
1339 err = -ENOPROTOOPT;
1340 }
1341 return err;
1342}
1343
1344static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1345{
1346 struct nl_pktinfo info;
1347
1348 info.group = NETLINK_CB(skb).dst_group;
1349 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1350}
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1353 struct msghdr *msg, size_t len)
1354{
1355 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1356 struct sock *sk = sock->sk;
1357 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001358 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001359 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001360 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 struct sk_buff *skb;
1362 int err;
1363 struct scm_cookie scm;
1364
1365 if (msg->msg_flags&MSG_OOB)
1366 return -EOPNOTSUPP;
1367
Eric Dumazet16e57262011-09-19 05:52:27 +00001368 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00001370
Eric Dumazete0e3cea2012-08-21 06:21:17 +00001371 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (err < 0)
1373 return err;
1374
1375 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001376 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001378 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001379 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001380 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001381 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001382 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001383 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001386 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001387 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Eric W. Biederman15e47302012-09-07 20:12:54 +00001390 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 err = netlink_autobind(sock);
1392 if (err)
1393 goto out;
1394 }
1395
1396 err = -EMSGSIZE;
1397 if (len > sk->sk_sndbuf - 32)
1398 goto out;
1399 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001400 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001401 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto out;
1403
Eric W. Biederman15e47302012-09-07 20:12:54 +00001404 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001405 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00001406 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001409 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 kfree_skb(skb);
1411 goto out;
1412 }
1413
1414 err = security_netlink_send(sk, skb);
1415 if (err) {
1416 kfree_skb(skb);
1417 goto out;
1418 }
1419
Patrick McHardyd629b832005-08-14 19:27:50 -07001420 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001422 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001424 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001427 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return err;
1429}
1430
1431static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1432 struct msghdr *msg, size_t len,
1433 int flags)
1434{
1435 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1436 struct scm_cookie scm;
1437 struct sock *sk = sock->sk;
1438 struct netlink_sock *nlk = nlk_sk(sk);
1439 int noblock = flags&MSG_DONTWAIT;
1440 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001441 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001442 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 if (flags&MSG_OOB)
1445 return -EOPNOTSUPP;
1446
1447 copied = 0;
1448
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001449 skb = skb_recv_datagram(sk, flags, noblock, &err);
1450 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto out;
1452
Johannes Berg68d6ac62010-08-15 21:20:44 +00001453 data_skb = skb;
1454
Johannes Berg1dacc762009-07-01 11:26:02 +00001455#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1456 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00001457 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00001458 * If this skb has a frag_list, then here that means that we
1459 * will have to use the frag_list skb's data for compat tasks
1460 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00001461 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00001462 * If we need to send the compat skb, assign it to the
1463 * 'data_skb' variable so that it will be used below for data
1464 * copying. We keep 'skb' for everything else, including
1465 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00001466 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00001467 if (flags & MSG_CMSG_COMPAT)
1468 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00001469 }
1470#endif
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 msg->msg_namelen = 0;
1473
Johannes Berg68d6ac62010-08-15 21:20:44 +00001474 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (len < copied) {
1476 msg->msg_flags |= MSG_TRUNC;
1477 copied = len;
1478 }
1479
Johannes Berg68d6ac62010-08-15 21:20:44 +00001480 skb_reset_transport_header(data_skb);
1481 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001484 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 addr->nl_family = AF_NETLINK;
1486 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001487 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001488 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 msg->msg_namelen = sizeof(*addr);
1490 }
1491
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001492 if (nlk->flags & NETLINK_RECV_PKTINFO)
1493 netlink_cmsg_recv_pktinfo(msg, skb);
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (NULL == siocb->scm) {
1496 memset(&scm, 0, sizeof(scm));
1497 siocb->scm = &scm;
1498 }
1499 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001500 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00001501 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 skb_free_datagram(sk, skb);
1504
Andrey Vaginb44d2112011-02-21 02:40:47 +00001505 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1506 ret = netlink_dump(sk);
1507 if (ret) {
1508 sk->sk_err = ret;
1509 sk->sk_error_report(sk);
1510 }
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514out:
1515 netlink_rcv_wake(sk);
1516 return err ? : copied;
1517}
1518
1519static void netlink_data_ready(struct sock *sk, int len)
1520{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001521 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001525 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 * complete set of kernel non-blocking support for message
1527 * queueing.
1528 */
1529
1530struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001531__netlink_kernel_create(struct net *net, int unit, struct module *module,
1532 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct socket *sock;
1535 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001536 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001537 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001538 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1539 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001541 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001543 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return NULL;
1545
1546 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1547 return NULL;
1548
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001549 /*
1550 * We have to just have a reference on the net from sk, but don't
1551 * get_net it. Besides, we cannot get and then put the net here.
1552 * So we create one inside init_net and the move it to net.
1553 */
1554
1555 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1556 goto out_sock_release_nosk;
1557
1558 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08001559 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001560
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001561 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08001562 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001563 else
1564 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001565
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001566 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08001567 if (!listeners)
1568 goto out_sock_release;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001571 if (cfg && cfg->input)
1572 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001574 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001575 goto out_sock_release;
1576
1577 nlk = nlk_sk(sk);
1578 nlk->flags |= NETLINK_KERNEL_SOCKET;
1579
1580 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001581 if (!nl_table[unit].registered) {
1582 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001583 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001584 nl_table[unit].cb_mutex = cb_mutex;
1585 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001586 if (cfg) {
1587 nl_table[unit].bind = cfg->bind;
1588 nl_table[unit].flags = cfg->flags;
1589 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001590 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07001591 } else {
1592 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001593 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001594 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07001595 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001596 return sk;
1597
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001598out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001599 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08001600 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001601 return NULL;
1602
1603out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001604 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001605 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001607EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001608
1609void
1610netlink_kernel_release(struct sock *sk)
1611{
Denis V. Lunevedf02082008-02-29 11:18:32 -08001612 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001613}
1614EXPORT_SYMBOL(netlink_kernel_release);
1615
Johannes Bergd136f1b2009-09-12 03:03:15 +00001616int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001617{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001618 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001619 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001620
1621 if (groups < 32)
1622 groups = 32;
1623
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001624 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001625 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1626 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00001627 return -ENOMEM;
Eric Dumazet33d480c2011-08-11 19:30:52 +00001628 old = rcu_dereference_protected(tbl->listeners, 1);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001629 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1630 rcu_assign_pointer(tbl->listeners, new);
1631
Lai Jiangshan37b6b932011-03-15 18:01:42 +08001632 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001633 }
1634 tbl->groups = groups;
1635
Johannes Bergd136f1b2009-09-12 03:03:15 +00001636 return 0;
1637}
1638
1639/**
1640 * netlink_change_ngroups - change number of multicast groups
1641 *
1642 * This changes the number of multicast groups that are available
1643 * on a certain netlink family. Note that it is not possible to
1644 * change the number of groups to below 32. Also note that it does
1645 * not implicitly call netlink_clear_multicast_users() when the
1646 * number of groups is reduced.
1647 *
1648 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1649 * @groups: The new number of groups.
1650 */
1651int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1652{
1653 int err;
1654
1655 netlink_table_grab();
1656 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001657 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00001658
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001659 return err;
1660}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001661
Johannes Bergb8273572009-09-24 15:44:05 -07001662void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1663{
1664 struct sock *sk;
1665 struct hlist_node *node;
1666 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1667
1668 sk_for_each_bound(sk, node, &tbl->mc_list)
1669 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1670}
1671
Johannes Berg84659eb2007-07-18 15:47:05 -07001672/**
1673 * netlink_clear_multicast_users - kick off multicast listeners
1674 *
1675 * This function removes all listeners from the given group.
1676 * @ksk: The kernel netlink socket, as returned by
1677 * netlink_kernel_create().
1678 * @group: The multicast group to clear.
1679 */
1680void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1681{
Johannes Berg84659eb2007-07-18 15:47:05 -07001682 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07001683 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07001684 netlink_table_ungrab();
1685}
Johannes Berg84659eb2007-07-18 15:47:05 -07001686
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001687struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00001688__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001689{
1690 struct nlmsghdr *nlh;
1691 int size = NLMSG_LENGTH(len);
1692
1693 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1694 nlh->nlmsg_type = type;
1695 nlh->nlmsg_len = size;
1696 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001697 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001698 nlh->nlmsg_seq = seq;
1699 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
1700 memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size);
1701 return nlh;
1702}
1703EXPORT_SYMBOL(__nlmsg_put);
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705/*
1706 * It looks a bit ugly.
1707 * It would be better to create kernel thread.
1708 */
1709
1710static int netlink_dump(struct sock *sk)
1711{
1712 struct netlink_sock *nlk = nlk_sk(sk);
1713 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001714 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001716 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001717 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001719 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 cb = nlk->cb;
1722 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001723 err = -EINVAL;
1724 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
Greg Rosec7ac8672011-06-10 01:27:09 +00001727 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1728
1729 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1730 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00001731 goto errout_skb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 len = cb->dump(skb, cb);
1734
1735 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001736 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001737
1738 if (sk_filter(sk, skb))
1739 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001740 else
1741 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 0;
1743 }
1744
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001745 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1746 if (!nlh)
1747 goto errout_skb;
1748
Johannes Berg670dc282011-06-20 13:40:46 +02001749 nl_dump_check_consistent(cb, nlh);
1750
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001751 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1752
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001753 if (sk_filter(sk, skb))
1754 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001755 else
1756 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Thomas Grafa8f74b22005-11-10 02:25:52 +01001758 if (cb->done)
1759 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001761 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Gao feng6dc878a2012-10-04 20:15:48 +00001763 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001764 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001766
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001767errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001768 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001769 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001770 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Gao feng6dc878a2012-10-04 20:15:48 +00001773int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1774 const struct nlmsghdr *nlh,
1775 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct netlink_callback *cb;
1778 struct sock *sk;
1779 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001780 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001782 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (cb == NULL)
1784 return -ENOBUFS;
1785
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001786 cb->dump = control->dump;
1787 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00001789 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00001790 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001791 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 atomic_inc(&skb->users);
1793 cb->skb = skb;
1794
Eric W. Biederman15e47302012-09-07 20:12:54 +00001795 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (sk == NULL) {
1797 netlink_destroy_callback(cb);
1798 return -ECONNREFUSED;
1799 }
1800 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00001801
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001802 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00001803 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07001804 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001805 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00001807 ret = -EBUSY;
1808 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Gao feng6dc878a2012-10-04 20:15:48 +00001810 /* add reference of module which cb->dump belongs to */
1811 if (!try_module_get(cb->module)) {
1812 mutex_unlock(nlk->cb_mutex);
1813 netlink_destroy_callback(cb);
1814 ret = -EPROTONOSUPPORT;
1815 goto out;
1816 }
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001819 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Andrey Vaginb44d2112011-02-21 02:40:47 +00001821 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00001822out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001824
Andrey Vaginb44d2112011-02-21 02:40:47 +00001825 if (ret)
1826 return ret;
1827
Denis V. Lunev5c582982007-10-23 20:29:25 -07001828 /* We successfully started a dump, by returning -EINTR we
1829 * signal not to send ACK even if it was requested.
1830 */
1831 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
Gao feng6dc878a2012-10-04 20:15:48 +00001833EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1836{
1837 struct sk_buff *skb;
1838 struct nlmsghdr *rep;
1839 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08001840 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Thomas Graf339bf982006-11-10 14:10:15 -08001842 /* error messages get the original request appened */
1843 if (err)
1844 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Thomas Graf339bf982006-11-10 14:10:15 -08001846 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (!skb) {
1848 struct sock *sk;
1849
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001850 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001851 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001852 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (sk) {
1854 sk->sk_err = ENOBUFS;
1855 sk->sk_error_report(sk);
1856 sock_put(sk);
1857 }
1858 return;
1859 }
1860
Eric W. Biederman15e47302012-09-07 20:12:54 +00001861 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00001862 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001863 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001865 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00001866 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001868EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001870int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001871 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001872{
Thomas Graf82ace472005-11-10 02:25:53 +01001873 struct nlmsghdr *nlh;
1874 int err;
1875
1876 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001877 int msglen;
1878
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001879 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07001880 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01001881
Martin Murrayad8e4b72006-01-10 13:02:29 -08001882 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01001883 return 0;
1884
Thomas Grafd35b6852007-03-22 23:28:46 -07001885 /* Only requests are handled by the kernel */
1886 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07001887 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07001888
Thomas Graf45e7ae72007-03-22 23:29:10 -07001889 /* Skip control messages */
1890 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07001891 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07001892
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001893 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001894 if (err == -EINTR)
1895 goto skip;
1896
1897ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07001898 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01001899 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01001900
Denis V. Lunev5c582982007-10-23 20:29:25 -07001901skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001902 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001903 if (msglen > skb->len)
1904 msglen = skb->len;
1905 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01001906 }
1907
1908 return 0;
1909}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001910EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01001911
1912/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07001913 * nlmsg_notify - send a notification netlink message
1914 * @sk: netlink socket to use
1915 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00001916 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07001917 * @group: destination multicast group or 0
1918 * @report: 1 to report back, 0 to disable
1919 * @flags: allocation flags
1920 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001921int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07001922 unsigned int group, int report, gfp_t flags)
1923{
1924 int err = 0;
1925
1926 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001927 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07001928
1929 if (report) {
1930 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001931 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07001932 }
1933
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001934 /* errors reported via destination sk->sk_err, but propagate
1935 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001936 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001937 }
1938
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001939 if (report) {
1940 int err2;
1941
Eric W. Biederman15e47302012-09-07 20:12:54 +00001942 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001943 if (!err || err == -ESRCH)
1944 err = err2;
1945 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07001946
1947 return err;
1948}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001949EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951#ifdef CONFIG_PROC_FS
1952struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08001953 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int link;
1955 int hash_idx;
1956};
1957
1958static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1959{
1960 struct nl_seq_iter *iter = seq->private;
1961 int i, j;
1962 struct sock *s;
1963 struct hlist_node *node;
1964 loff_t off = 0;
1965
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001966 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001967 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 for (j = 0; j <= hash->mask; j++) {
1970 sk_for_each(s, node, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001971 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001972 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (off == pos) {
1974 iter->link = i;
1975 iter->hash_idx = j;
1976 return s;
1977 }
1978 ++off;
1979 }
1980 }
1981 }
1982 return NULL;
1983}
1984
1985static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001986 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 read_lock(&nl_table_lock);
1989 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1990}
1991
1992static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1993{
1994 struct sock *s;
1995 struct nl_seq_iter *iter;
1996 int i, j;
1997
1998 ++*pos;
1999
2000 if (v == SEQ_START_TOKEN)
2001 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002002
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002003 iter = seq->private;
2004 s = v;
2005 do {
2006 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002007 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (s)
2009 return s;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 i = iter->link;
2012 j = iter->hash_idx + 1;
2013
2014 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002015 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 for (; j <= hash->mask; j++) {
2018 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002019 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002020 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (s) {
2022 iter->link = i;
2023 iter->hash_idx = j;
2024 return s;
2025 }
2026 }
2027
2028 j = 0;
2029 } while (++i < MAX_LINKS);
2030
2031 return NULL;
2032}
2033
2034static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002035 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 read_unlock(&nl_table_lock);
2038}
2039
2040
2041static int netlink_seq_show(struct seq_file *seq, void *v)
2042{
Eric Dumazet658cb352012-04-22 21:30:21 +00002043 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 seq_puts(seq,
2045 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002046 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002047 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct sock *s = v;
2049 struct netlink_sock *nlk = nlk_sk(s);
2050
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002051 seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 s,
2053 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002054 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002055 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002056 sk_rmem_alloc_get(s),
2057 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002059 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002060 atomic_read(&s->sk_drops),
2061 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 );
2063
2064 }
2065 return 0;
2066}
2067
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002068static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 .start = netlink_seq_start,
2070 .next = netlink_seq_next,
2071 .stop = netlink_seq_stop,
2072 .show = netlink_seq_show,
2073};
2074
2075
2076static int netlink_seq_open(struct inode *inode, struct file *file)
2077{
Denis V. Luneve372c412007-11-19 22:31:54 -08002078 return seq_open_net(inode, file, &netlink_seq_ops,
2079 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002080}
2081
Arjan van de Venda7071d2007-02-12 00:55:36 -08002082static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .owner = THIS_MODULE,
2084 .open = netlink_seq_open,
2085 .read = seq_read,
2086 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002087 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088};
2089
2090#endif
2091
2092int netlink_register_notifier(struct notifier_block *nb)
2093{
Alan Sterne041c682006-03-27 01:16:30 -08002094 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002096EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098int netlink_unregister_notifier(struct notifier_block *nb)
2099{
Alan Sterne041c682006-03-27 01:16:30 -08002100 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002102EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002103
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002104static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 .family = PF_NETLINK,
2106 .owner = THIS_MODULE,
2107 .release = netlink_release,
2108 .bind = netlink_bind,
2109 .connect = netlink_connect,
2110 .socketpair = sock_no_socketpair,
2111 .accept = sock_no_accept,
2112 .getname = netlink_getname,
2113 .poll = datagram_poll,
2114 .ioctl = sock_no_ioctl,
2115 .listen = sock_no_listen,
2116 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002117 .setsockopt = netlink_setsockopt,
2118 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 .sendmsg = netlink_sendmsg,
2120 .recvmsg = netlink_recvmsg,
2121 .mmap = sock_no_mmap,
2122 .sendpage = sock_no_sendpage,
2123};
2124
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002125static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 .family = PF_NETLINK,
2127 .create = netlink_create,
2128 .owner = THIS_MODULE, /* for consistency 8) */
2129};
2130
Pavel Emelyanov46650792007-10-08 20:38:39 -07002131static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002132{
2133#ifdef CONFIG_PROC_FS
2134 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
2135 return -ENOMEM;
2136#endif
2137 return 0;
2138}
2139
Pavel Emelyanov46650792007-10-08 20:38:39 -07002140static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002141{
2142#ifdef CONFIG_PROC_FS
2143 proc_net_remove(net, "netlink");
2144#endif
2145}
2146
David S. Millerb963ea82010-08-30 19:08:01 -07002147static void __init netlink_add_usersock_entry(void)
2148{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002149 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002150 int groups = 32;
2151
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002152 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002153 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002154 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002155
2156 netlink_table_grab();
2157
2158 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002159 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002160 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2161 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002162 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002163
2164 netlink_table_ungrab();
2165}
2166
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002167static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002168 .init = netlink_net_init,
2169 .exit = netlink_net_exit,
2170};
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172static int __init netlink_proto_init(void)
2173{
2174 struct sk_buff *dummy_skb;
2175 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002176 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 unsigned int order;
2178 int err = proto_register(&netlink_proto, 0);
2179
2180 if (err != 0)
2181 goto out;
2182
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07002183 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002185 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002186 if (!nl_table)
2187 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Jan Beulich44813742009-09-21 17:03:05 -07002189 if (totalram_pages >= (128 * 1024))
2190 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 else
Jan Beulich44813742009-09-21 17:03:05 -07002192 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002194 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2195 limit = (1UL << order) / sizeof(struct hlist_head);
2196 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002199 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Eric W. Biederman15e47302012-09-07 20:12:54 +00002201 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (!hash->table) {
2203 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002204 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 1 * sizeof(*hash->table));
2206 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002207 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 hash->max_shift = order;
2210 hash->shift = 0;
2211 hash->mask = 0;
2212 hash->rehash_time = jiffies;
2213 }
2214
David S. Millerb963ea82010-08-30 19:08:01 -07002215 netlink_add_usersock_entry();
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002218 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002219 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 rtnetlink_init();
2221out:
2222 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002223panic:
2224 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227core_initcall(netlink_proto_init);