blob: 1e3fd5bfcd86a995fcbfd4277452738a7a823727 [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
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000141#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
142
Alan Sterne041c682006-03-27 01:16:30 -0800143static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000145static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700146{
147 return group ? 1 << (group - 1) : 0;
148}
149
Eric W. Biederman15e47302012-09-07 20:12:54 +0000150static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000152 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Eric Dumazet658cb352012-04-22 21:30:21 +0000155static void netlink_destroy_callback(struct netlink_callback *cb)
156{
157 kfree_skb(cb->skb);
158 kfree(cb);
159}
160
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000161static void netlink_consume_callback(struct netlink_callback *cb)
162{
163 consume_skb(cb->skb);
164 kfree(cb);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void netlink_sock_destruct(struct sock *sk)
168{
Herbert Xu3f660d62007-05-03 03:17:14 -0700169 struct netlink_sock *nlk = nlk_sk(sk);
170
Herbert Xu3f660d62007-05-03 03:17:14 -0700171 if (nlk->cb) {
172 if (nlk->cb->done)
173 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000174
175 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700176 netlink_destroy_callback(nlk->cb);
177 }
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 skb_queue_purge(&sk->sk_receive_queue);
180
181 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800182 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return;
184 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700185
186 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
187 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
188 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800191/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
192 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
194 * this, _but_ remember, it adds useless work on UP machines.
195 */
196
Johannes Bergd136f1b2009-09-12 03:03:15 +0000197void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800198 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000200 might_sleep();
201
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700202 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (atomic_read(&nl_table_users)) {
205 DECLARE_WAITQUEUE(wait, current);
206
207 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800208 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 set_current_state(TASK_UNINTERRUPTIBLE);
210 if (atomic_read(&nl_table_users) == 0)
211 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700212 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700214 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 __set_current_state(TASK_RUNNING);
218 remove_wait_queue(&nl_table_wait, &wait);
219 }
220}
221
Johannes Bergd136f1b2009-09-12 03:03:15 +0000222void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800223 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700225 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 wake_up(&nl_table_wait);
227}
228
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800229static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230netlink_lock_table(void)
231{
232 /* read_lock() synchronizes us to netlink_table_grab */
233
234 read_lock(&nl_table_lock);
235 atomic_inc(&nl_table_users);
236 read_unlock(&nl_table_lock);
237}
238
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800239static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240netlink_unlock_table(void)
241{
242 if (atomic_dec_and_test(&nl_table_users))
243 wake_up(&nl_table_wait);
244}
245
Eric W. Biederman15e47302012-09-07 20:12:54 +0000246static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000248 struct nl_portid_hash *hash = &nl_table[protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct hlist_head *head;
250 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000253 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800254 sk_for_each(sk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000255 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 sock_hold(sk);
257 goto found;
258 }
259 }
260 sk = NULL;
261found:
262 read_unlock(&nl_table_lock);
263 return sk;
264}
265
Eric W. Biederman15e47302012-09-07 20:12:54 +0000266static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800269 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 else
271 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800272 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
273 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Eric W. Biederman15e47302012-09-07 20:12:54 +0000276static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 if (size <= PAGE_SIZE)
279 kfree(table);
280 else
281 free_pages((unsigned long)table, get_order(size));
282}
283
Eric W. Biederman15e47302012-09-07 20:12:54 +0000284static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 unsigned int omask, mask, shift;
287 size_t osize, size;
288 struct hlist_head *otable, *table;
289 int i;
290
291 omask = mask = hash->mask;
292 osize = size = (mask + 1) * sizeof(*table);
293 shift = hash->shift;
294
295 if (grow) {
296 if (++shift > hash->max_shift)
297 return 0;
298 mask = mask * 2 + 1;
299 size *= 2;
300 }
301
Eric W. Biederman15e47302012-09-07 20:12:54 +0000302 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!table)
304 return 0;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 otable = hash->table;
307 hash->table = table;
308 hash->mask = mask;
309 hash->shift = shift;
310 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
311
312 for (i = 0; i <= omask; i++) {
313 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800314 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Sasha Levinb67bfe02013-02-27 17:06:00 -0800316 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000317 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Eric W. Biederman15e47302012-09-07 20:12:54 +0000320 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 hash->rehash_time = jiffies + 10 * 60 * HZ;
322 return 1;
323}
324
Eric W. Biederman15e47302012-09-07 20:12:54 +0000325static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int avg = hash->entries >> hash->shift;
328
Eric W. Biederman15e47302012-09-07 20:12:54 +0000329 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 1;
331
332 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000333 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 1;
335 }
336
337 return 0;
338}
339
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800340static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Patrick McHardy4277a082006-03-20 18:52:01 -0800342static void
343netlink_update_listeners(struct sock *sk)
344{
345 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800346 unsigned long mask;
347 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000348 struct listeners *listeners;
349
350 listeners = nl_deref_protected(tbl->listeners);
351 if (!listeners)
352 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800353
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700354 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800355 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800356 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700357 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
358 mask |= nlk_sk(sk)->groups[i];
359 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000360 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800361 }
362 /* this function is only called with the netlink table "grabbed", which
363 * makes sure updates are visible before bind or setsockopt return. */
364}
365
Eric W. Biederman15e47302012-09-07 20:12:54 +0000366static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000368 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct hlist_head *head;
370 int err = -EADDRINUSE;
371 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int len;
373
374 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000375 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800377 sk_for_each(osk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000378 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380 len++;
381 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800382 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto err;
384
385 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000386 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 goto err;
388
389 err = -ENOMEM;
390 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
391 goto err;
392
Eric W. Biederman15e47302012-09-07 20:12:54 +0000393 if (len && nl_portid_hash_dilute(hash, len))
394 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000396 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sk_add_node(sk, head);
398 err = 0;
399
400err:
401 netlink_table_ungrab();
402 return err;
403}
404
405static void netlink_remove(struct sock *sk)
406{
407 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700408 if (sk_del_node_init(sk))
409 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700410 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 __sk_del_bind_node(sk);
412 netlink_table_ungrab();
413}
414
415static struct proto netlink_proto = {
416 .name = "NETLINK",
417 .owner = THIS_MODULE,
418 .obj_size = sizeof(struct netlink_sock),
419};
420
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700421static int __netlink_create(struct net *net, struct socket *sock,
422 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct sock *sk;
425 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700426
427 sock->ops = &netlink_ops;
428
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700429 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700430 if (!sk)
431 return -ENOMEM;
432
433 sock_init_data(sock, sk);
434
435 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000436 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700437 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000438 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700439 nlk->cb_mutex = &nlk->cb_def_mutex;
440 mutex_init(nlk->cb_mutex);
441 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700442 init_waitqueue_head(&nlk->wait);
443
444 sk->sk_destruct = netlink_sock_destruct;
445 sk->sk_protocol = protocol;
446 return 0;
447}
448
Eric Paris3f378b62009-11-05 22:18:14 -0800449static int netlink_create(struct net *net, struct socket *sock, int protocol,
450 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700451{
452 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700453 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700454 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000455 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -0700456 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 sock->state = SS_UNCONNECTED;
459
460 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
461 return -ESOCKTNOSUPPORT;
462
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800463 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EPROTONOSUPPORT;
465
Patrick McHardy77247bb2005-08-14 19:27:13 -0700466 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700467#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700468 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700469 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700470 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700471 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700472 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700473#endif
474 if (nl_table[protocol].registered &&
475 try_module_get(nl_table[protocol].module))
476 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000477 else
478 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700479 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000480 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700481 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700482
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000483 if (err < 0)
484 goto out;
485
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800486 err = __netlink_create(net, sock, cb_mutex, protocol);
487 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700488 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
David S. Miller6f756a82008-11-23 17:34:03 -0800490 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800491 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800492 local_bh_enable();
493
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700494 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700495 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000496 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -0700497out:
498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Patrick McHardyab33a172005-08-14 19:31:36 -0700500out_module:
501 module_put(module);
502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static int netlink_release(struct socket *sock)
506{
507 struct sock *sk = sock->sk;
508 struct netlink_sock *nlk;
509
510 if (!sk)
511 return 0;
512
513 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700514 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 nlk = nlk_sk(sk);
516
Herbert Xu3f660d62007-05-03 03:17:14 -0700517 /*
518 * OK. Socket is unlinked, any packets that arrive now
519 * will be purged.
520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 sock->sk = NULL;
523 wake_up_interruptible_all(&nlk->wait);
524
525 skb_queue_purge(&sk->sk_write_queue);
526
Eric W. Biederman15e47302012-09-07 20:12:54 +0000527 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900529 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000531 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 };
Alan Sterne041c682006-03-27 01:16:30 -0800533 atomic_notifier_call_chain(&netlink_chain,
534 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900535 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700536
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800537 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700538
Patrick McHardy4277a082006-03-20 18:52:01 -0800539 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -0700540 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800541 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
542 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000543 struct listeners *old;
544
545 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
546 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
547 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800548 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000549 nl_table[sk->sk_protocol].bind = NULL;
550 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800551 nl_table[sk->sk_protocol].registered = 0;
552 }
Eric Dumazet658cb352012-04-22 21:30:21 +0000553 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800554 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000555 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800556 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700557
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700558 kfree(nlk->groups);
559 nlk->groups = NULL;
560
Eric Dumazet37558102008-11-24 14:05:22 -0800561 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800562 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800563 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 sock_put(sk);
565 return 0;
566}
567
568static int netlink_autobind(struct socket *sock)
569{
570 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900571 struct net *net = sock_net(sk);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000572 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct hlist_head *head;
574 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000575 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int err;
577 static s32 rover = -4097;
578
579retry:
580 cond_resched();
581 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000582 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800583 sk_for_each(osk, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900584 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200585 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000586 if (nlk_sk(osk)->portid == portid) {
587 /* Bind collision, search negative portid values. */
588 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (rover > -4097)
590 rover = -4097;
591 netlink_table_ungrab();
592 goto retry;
593 }
594 }
595 netlink_table_ungrab();
596
Eric W. Biederman15e47302012-09-07 20:12:54 +0000597 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (err == -EADDRINUSE)
599 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700600
601 /* If 2 threads race to autobind, that is fine. */
602 if (err == -EBUSY)
603 err = 0;
604
605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000608static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900609{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000610 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000611 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700614static void
615netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
616{
617 struct netlink_sock *nlk = nlk_sk(sk);
618
619 if (nlk->subscriptions && !subscriptions)
620 __sk_del_bind_node(sk);
621 else if (!nlk->subscriptions && subscriptions)
622 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
623 nlk->subscriptions = subscriptions;
624}
625
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700626static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700627{
628 struct netlink_sock *nlk = nlk_sk(sk);
629 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700630 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700631 int err = 0;
632
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700633 netlink_table_grab();
634
Patrick McHardy513c2502005-09-06 15:43:59 -0700635 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700636 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700637 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700638 goto out_unlock;
639 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700640
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700641 if (nlk->ngroups >= groups)
642 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700643
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700644 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
645 if (new_groups == NULL) {
646 err = -ENOMEM;
647 goto out_unlock;
648 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800649 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700650 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
651
652 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700653 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700654 out_unlock:
655 netlink_table_ungrab();
656 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700657}
658
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800659static int netlink_bind(struct socket *sock, struct sockaddr *addr,
660 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900663 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct netlink_sock *nlk = nlk_sk(sk);
665 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
666 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900667
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +0000668 if (addr_len < sizeof(struct sockaddr_nl))
669 return -EINVAL;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (nladdr->nl_family != AF_NETLINK)
672 return -EINVAL;
673
674 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700675 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000676 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -0700677 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700678 err = netlink_realloc_groups(sk);
679 if (err)
680 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Eric W. Biederman15e47302012-09-07 20:12:54 +0000683 if (nlk->portid) {
684 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -EINVAL;
686 } else {
687 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200688 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 netlink_autobind(sock);
690 if (err)
691 return err;
692 }
693
Patrick McHardy513c2502005-09-06 15:43:59 -0700694 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
696
697 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700698 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900699 hweight32(nladdr->nl_groups) -
700 hweight32(nlk->groups[0]));
701 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800702 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 netlink_table_ungrab();
704
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000705 if (nlk->netlink_bind && nlk->groups[0]) {
706 int i;
707
708 for (i=0; i<nlk->ngroups; i++) {
709 if (test_bit(i, nlk->groups))
710 nlk->netlink_bind(i);
711 }
712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
715}
716
717static int netlink_connect(struct socket *sock, struct sockaddr *addr,
718 int alen, int flags)
719{
720 int err = 0;
721 struct sock *sk = sock->sk;
722 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800723 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Changli Gao6503d962010-03-31 22:58:26 +0000725 if (alen < sizeof(addr->sa_family))
726 return -EINVAL;
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (addr->sa_family == AF_UNSPEC) {
729 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000730 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700731 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
733 }
734 if (addr->sa_family != AF_NETLINK)
735 return -EINVAL;
736
737 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000738 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EPERM;
740
Eric W. Biederman15e47302012-09-07 20:12:54 +0000741 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 err = netlink_autobind(sock);
743
744 if (err == 0) {
745 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000746 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700747 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 return err;
751}
752
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800753static int netlink_getname(struct socket *sock, struct sockaddr *addr,
754 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct sock *sk = sock->sk;
757 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +0000758 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 nladdr->nl_family = AF_NETLINK;
761 nladdr->nl_pad = 0;
762 *addr_len = sizeof(*nladdr);
763
764 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000765 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700766 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000768 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700769 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 return 0;
772}
773
774static void netlink_overrun(struct sock *sk)
775{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700776 struct netlink_sock *nlk = nlk_sk(sk);
777
778 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
779 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
780 sk->sk_err = ENOBUFS;
781 sk->sk_error_report(sk);
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700784 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Eric W. Biederman15e47302012-09-07 20:12:54 +0000787static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct sock *sock;
790 struct netlink_sock *nlk;
791
Eric W. Biederman15e47302012-09-07 20:12:54 +0000792 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!sock)
794 return ERR_PTR(-ECONNREFUSED);
795
796 /* Don't bother queuing skb if kernel socket has no input function */
797 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700798 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +0000799 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 sock_put(sock);
801 return ERR_PTR(-ECONNREFUSED);
802 }
803 return sock;
804}
805
806struct sock *netlink_getsockbyfilp(struct file *filp)
807{
Al Viro496ad9a2013-01-23 17:07:38 -0500808 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct sock *sock;
810
811 if (!S_ISSOCK(inode->i_mode))
812 return ERR_PTR(-ENOTSOCK);
813
814 sock = SOCKET_I(inode)->sk;
815 if (sock->sk_family != AF_NETLINK)
816 return ERR_PTR(-EINVAL);
817
818 sock_hold(sock);
819 return sock;
820}
821
822/*
823 * Attach a skb to a netlink socket.
824 * The caller must hold a reference to the destination socket. On error, the
825 * reference is dropped. The skb is not send to the destination, just all
826 * all error checks are performed and memory in the queue is reserved.
827 * Return values:
828 * < 0: error. skb freed, reference to sock dropped.
829 * 0: continue
830 * 1: repeat lookup - reference dropped while waiting for socket memory.
831 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700832int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800833 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct netlink_sock *nlk;
836
837 nlk = nlk_sk(sk);
838
839 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
840 test_bit(0, &nlk->state)) {
841 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800842 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -0700843 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 netlink_overrun(sk);
845 sock_put(sk);
846 kfree_skb(skb);
847 return -EAGAIN;
848 }
849
850 __set_current_state(TASK_INTERRUPTIBLE);
851 add_wait_queue(&nlk->wait, &wait);
852
853 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
854 test_bit(0, &nlk->state)) &&
855 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800856 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 __set_current_state(TASK_RUNNING);
859 remove_wait_queue(&nlk->wait, &wait);
860 sock_put(sk);
861
862 if (signal_pending(current)) {
863 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800864 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 return 1;
867 }
868 skb_set_owner_r(skb, sk);
869 return 0;
870}
871
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000872static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int len = skb->len;
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 skb_queue_tail(&sk->sk_receive_queue, skb);
877 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +0000878 return len;
879}
880
881int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
882{
883 int len = __netlink_sendskb(sk, skb);
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 sock_put(sk);
886 return len;
887}
888
889void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
890{
891 kfree_skb(skb);
892 sock_put(sk);
893}
894
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000895static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 int delta;
898
899 skb_orphan(skb);
900
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700901 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (delta * 2 < skb->truesize)
903 return skb;
904
905 if (skb_shared(skb)) {
906 struct sk_buff *nskb = skb_clone(skb, allocation);
907 if (!nskb)
908 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +0000909 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 skb = nskb;
911 }
912
913 if (!pskb_expand_head(skb, 0, -delta, allocation))
914 skb->truesize -= delta;
915
916 return skb;
917}
918
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000919static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700920{
921 struct netlink_sock *nlk = nlk_sk(sk);
922
923 if (skb_queue_empty(&sk->sk_receive_queue))
924 clear_bit(0, &nlk->state);
925 if (!test_bit(0, &nlk->state))
926 wake_up_interruptible(&nlk->wait);
927}
928
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600929static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
930 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700931{
932 int ret;
933 struct netlink_sock *nlk = nlk_sk(sk);
934
935 ret = -ECONNREFUSED;
936 if (nlk->netlink_rcv != NULL) {
937 ret = skb->len;
938 skb_set_owner_r(skb, sk);
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600939 NETLINK_CB(skb).ssk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700940 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000941 consume_skb(skb);
942 } else {
943 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700944 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700945 sock_put(sk);
946 return ret;
947}
948
949int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000950 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 struct sock *sk;
953 int err;
954 long timeo;
955
956 skb = netlink_trim(skb, gfp_any());
957
958 timeo = sock_sndtimeo(ssk, nonblock);
959retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000960 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (IS_ERR(sk)) {
962 kfree_skb(skb);
963 return PTR_ERR(sk);
964 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700965 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -0600966 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700967
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700968 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -0700969 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -0700970 kfree_skb(skb);
971 sock_put(sk);
972 return err;
973 }
974
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700975 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (err == 1)
977 goto retry;
978 if (err)
979 return err;
980
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700981 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800983EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Patrick McHardy4277a082006-03-20 18:52:01 -0800985int netlink_has_listeners(struct sock *sk, unsigned int group)
986{
987 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000988 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -0800989
Denis V. Lunevaed81562007-10-10 21:14:32 -0700990 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700991
992 rcu_read_lock();
993 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
994
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000995 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +0000996 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700997
998 rcu_read_unlock();
999
Patrick McHardy4277a082006-03-20 18:52:01 -08001000 return res;
1001}
1002EXPORT_SYMBOL_GPL(netlink_has_listeners);
1003
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001004static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct netlink_sock *nlk = nlk_sk(sk);
1007
1008 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
1009 !test_bit(0, &nlk->state)) {
1010 skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001011 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001012 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014 return -1;
1015}
1016
1017struct netlink_broadcast_data {
1018 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001019 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001020 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 u32 group;
1022 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001023 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int congested;
1025 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001026 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001028 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1029 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030};
1031
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001032static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct netlink_broadcast_data *p)
1034{
1035 struct netlink_sock *nlk = nlk_sk(sk);
1036 int val;
1037
1038 if (p->exclude_sk == sk)
1039 goto out;
1040
Eric W. Biederman15e47302012-09-07 20:12:54 +00001041 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001042 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto out;
1044
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001045 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001046 goto out;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (p->failure) {
1049 netlink_overrun(sk);
1050 goto out;
1051 }
1052
1053 sock_hold(sk);
1054 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001055 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 p->skb2 = skb_clone(p->skb, p->allocation);
1057 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001058 p->skb2 = skb_get(p->skb);
1059 /*
1060 * skb ownership may have been set when
1061 * delivered to a previous socket.
1062 */
1063 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 }
1066 if (p->skb2 == NULL) {
1067 netlink_overrun(sk);
1068 /* Clone failed. Notify ALL listeners. */
1069 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001070 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1071 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001072 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1073 kfree_skb(p->skb2);
1074 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001075 } else if (sk_filter(sk, p->skb2)) {
1076 kfree_skb(p->skb2);
1077 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1079 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001080 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1081 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 } else {
1083 p->congested |= val;
1084 p->delivered = 1;
1085 p->skb2 = NULL;
1086 }
1087 sock_put(sk);
1088
1089out:
1090 return 0;
1091}
1092
Eric W. Biederman15e47302012-09-07 20:12:54 +00001093int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001094 u32 group, gfp_t allocation,
1095 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1096 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001098 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct sock *sk;
1101
1102 skb = netlink_trim(skb, allocation);
1103
1104 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001105 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001106 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 info.group = group;
1108 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001109 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 info.congested = 0;
1111 info.delivered = 0;
1112 info.allocation = allocation;
1113 info.skb = skb;
1114 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001115 info.tx_filter = filter;
1116 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /* While we sleep in clone, do not allow to change socket list */
1119
1120 netlink_lock_table();
1121
Sasha Levinb67bfe02013-02-27 17:06:00 -08001122 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 do_one_broadcast(sk, &info);
1124
Neil Horman70d4bf62010-07-20 06:45:56 +00001125 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 netlink_unlock_table();
1128
Neil Horman70d4bf62010-07-20 06:45:56 +00001129 if (info.delivery_failure) {
1130 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001131 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001132 }
1133 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (info.delivered) {
1136 if (info.congested && (allocation & __GFP_WAIT))
1137 yield();
1138 return 0;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return -ESRCH;
1141}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001142EXPORT_SYMBOL(netlink_broadcast_filtered);
1143
Eric W. Biederman15e47302012-09-07 20:12:54 +00001144int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001145 u32 group, gfp_t allocation)
1146{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001147 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001148 NULL, NULL);
1149}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001150EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152struct netlink_set_err_data {
1153 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001154 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 u32 group;
1156 int code;
1157};
1158
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001159static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001162 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (sk == p->exclude_sk)
1165 goto out;
1166
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001167 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001168 goto out;
1169
Eric W. Biederman15e47302012-09-07 20:12:54 +00001170 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001171 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto out;
1173
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001174 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1175 ret = 1;
1176 goto out;
1177 }
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 sk->sk_err = p->code;
1180 sk->sk_error_report(sk);
1181out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001185/**
1186 * netlink_set_err - report error to broadcast listeners
1187 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001188 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001189 * @groups: the broadcast group that will notice the error
1190 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001191 *
1192 * This function returns the number of broadcast listeners that have set the
1193 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001194 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001195int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001199 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001202 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001204 /* sk->sk_err wants a positive error value */
1205 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 read_lock(&nl_table_lock);
1208
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001210 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001215EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Johannes Berg84659eb2007-07-18 15:47:05 -07001217/* must be called with netlink table grabbed */
1218static void netlink_update_socket_mc(struct netlink_sock *nlk,
1219 unsigned int group,
1220 int is_new)
1221{
1222 int old, new = !!is_new, subscriptions;
1223
1224 old = test_bit(group - 1, nlk->groups);
1225 subscriptions = nlk->subscriptions - old + new;
1226 if (new)
1227 __set_bit(group - 1, nlk->groups);
1228 else
1229 __clear_bit(group - 1, nlk->groups);
1230 netlink_update_subscriptions(&nlk->sk, subscriptions);
1231 netlink_update_listeners(&nlk->sk);
1232}
1233
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001234static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001235 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001236{
1237 struct sock *sk = sock->sk;
1238 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001239 unsigned int val = 0;
1240 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001241
1242 if (level != SOL_NETLINK)
1243 return -ENOPROTOOPT;
1244
1245 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001246 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001247 return -EFAULT;
1248
1249 switch (optname) {
1250 case NETLINK_PKTINFO:
1251 if (val)
1252 nlk->flags |= NETLINK_RECV_PKTINFO;
1253 else
1254 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1255 err = 0;
1256 break;
1257 case NETLINK_ADD_MEMBERSHIP:
1258 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001259 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001260 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001261 err = netlink_realloc_groups(sk);
1262 if (err)
1263 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001264 if (!val || val - 1 >= nlk->ngroups)
1265 return -EINVAL;
1266 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001267 netlink_update_socket_mc(nlk, val,
1268 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001269 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001270
1271 if (nlk->netlink_bind)
1272 nlk->netlink_bind(val);
1273
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001274 err = 0;
1275 break;
1276 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001277 case NETLINK_BROADCAST_ERROR:
1278 if (val)
1279 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1280 else
1281 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1282 err = 0;
1283 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001284 case NETLINK_NO_ENOBUFS:
1285 if (val) {
1286 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1287 clear_bit(0, &nlk->state);
1288 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001289 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001290 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001291 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001292 err = 0;
1293 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001294 default:
1295 err = -ENOPROTOOPT;
1296 }
1297 return err;
1298}
1299
1300static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001301 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001302{
1303 struct sock *sk = sock->sk;
1304 struct netlink_sock *nlk = nlk_sk(sk);
1305 int len, val, err;
1306
1307 if (level != SOL_NETLINK)
1308 return -ENOPROTOOPT;
1309
1310 if (get_user(len, optlen))
1311 return -EFAULT;
1312 if (len < 0)
1313 return -EINVAL;
1314
1315 switch (optname) {
1316 case NETLINK_PKTINFO:
1317 if (len < sizeof(int))
1318 return -EINVAL;
1319 len = sizeof(int);
1320 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001321 if (put_user(len, optlen) ||
1322 put_user(val, optval))
1323 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001324 err = 0;
1325 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001326 case NETLINK_BROADCAST_ERROR:
1327 if (len < sizeof(int))
1328 return -EINVAL;
1329 len = sizeof(int);
1330 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1331 if (put_user(len, optlen) ||
1332 put_user(val, optval))
1333 return -EFAULT;
1334 err = 0;
1335 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001336 case NETLINK_NO_ENOBUFS:
1337 if (len < sizeof(int))
1338 return -EINVAL;
1339 len = sizeof(int);
1340 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1341 if (put_user(len, optlen) ||
1342 put_user(val, optval))
1343 return -EFAULT;
1344 err = 0;
1345 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001346 default:
1347 err = -ENOPROTOOPT;
1348 }
1349 return err;
1350}
1351
1352static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1353{
1354 struct nl_pktinfo info;
1355
1356 info.group = NETLINK_CB(skb).dst_group;
1357 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1361 struct msghdr *msg, size_t len)
1362{
1363 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1364 struct sock *sk = sock->sk;
1365 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001366 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001367 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001368 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct sk_buff *skb;
1370 int err;
1371 struct scm_cookie scm;
1372
1373 if (msg->msg_flags&MSG_OOB)
1374 return -EOPNOTSUPP;
1375
Eric Dumazet16e57262011-09-19 05:52:27 +00001376 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00001378
Eric Dumazete0e3cea2012-08-21 06:21:17 +00001379 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (err < 0)
1381 return err;
1382
1383 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001384 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001386 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001387 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001388 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001389 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001390 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001391 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001394 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001395 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
Eric W. Biederman15e47302012-09-07 20:12:54 +00001398 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 err = netlink_autobind(sock);
1400 if (err)
1401 goto out;
1402 }
1403
1404 err = -EMSGSIZE;
1405 if (len > sk->sk_sndbuf - 32)
1406 goto out;
1407 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001408 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001409 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 goto out;
1411
Eric W. Biederman15e47302012-09-07 20:12:54 +00001412 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001413 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00001414 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001417 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 kfree_skb(skb);
1419 goto out;
1420 }
1421
1422 err = security_netlink_send(sk, skb);
1423 if (err) {
1424 kfree_skb(skb);
1425 goto out;
1426 }
1427
Patrick McHardyd629b832005-08-14 19:27:50 -07001428 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001430 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001432 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001435 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return err;
1437}
1438
1439static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1440 struct msghdr *msg, size_t len,
1441 int flags)
1442{
1443 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1444 struct scm_cookie scm;
1445 struct sock *sk = sock->sk;
1446 struct netlink_sock *nlk = nlk_sk(sk);
1447 int noblock = flags&MSG_DONTWAIT;
1448 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001449 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001450 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (flags&MSG_OOB)
1453 return -EOPNOTSUPP;
1454
1455 copied = 0;
1456
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001457 skb = skb_recv_datagram(sk, flags, noblock, &err);
1458 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 goto out;
1460
Johannes Berg68d6ac62010-08-15 21:20:44 +00001461 data_skb = skb;
1462
Johannes Berg1dacc762009-07-01 11:26:02 +00001463#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1464 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00001465 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00001466 * If this skb has a frag_list, then here that means that we
1467 * will have to use the frag_list skb's data for compat tasks
1468 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00001469 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00001470 * If we need to send the compat skb, assign it to the
1471 * 'data_skb' variable so that it will be used below for data
1472 * copying. We keep 'skb' for everything else, including
1473 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00001474 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00001475 if (flags & MSG_CMSG_COMPAT)
1476 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00001477 }
1478#endif
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 msg->msg_namelen = 0;
1481
Johannes Berg68d6ac62010-08-15 21:20:44 +00001482 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (len < copied) {
1484 msg->msg_flags |= MSG_TRUNC;
1485 copied = len;
1486 }
1487
Johannes Berg68d6ac62010-08-15 21:20:44 +00001488 skb_reset_transport_header(data_skb);
1489 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001492 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 addr->nl_family = AF_NETLINK;
1494 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001495 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001496 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 msg->msg_namelen = sizeof(*addr);
1498 }
1499
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001500 if (nlk->flags & NETLINK_RECV_PKTINFO)
1501 netlink_cmsg_recv_pktinfo(msg, skb);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (NULL == siocb->scm) {
1504 memset(&scm, 0, sizeof(scm));
1505 siocb->scm = &scm;
1506 }
1507 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001508 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00001509 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 skb_free_datagram(sk, skb);
1512
Andrey Vaginb44d2112011-02-21 02:40:47 +00001513 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1514 ret = netlink_dump(sk);
1515 if (ret) {
1516 sk->sk_err = ret;
1517 sk->sk_error_report(sk);
1518 }
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522out:
1523 netlink_rcv_wake(sk);
1524 return err ? : copied;
1525}
1526
1527static void netlink_data_ready(struct sock *sk, int len)
1528{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001529 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001533 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * complete set of kernel non-blocking support for message
1535 * queueing.
1536 */
1537
1538struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001539__netlink_kernel_create(struct net *net, int unit, struct module *module,
1540 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct socket *sock;
1543 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001544 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001545 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001546 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1547 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001549 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001551 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return NULL;
1553
1554 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1555 return NULL;
1556
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001557 /*
1558 * We have to just have a reference on the net from sk, but don't
1559 * get_net it. Besides, we cannot get and then put the net here.
1560 * So we create one inside init_net and the move it to net.
1561 */
1562
1563 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1564 goto out_sock_release_nosk;
1565
1566 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08001567 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001568
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001569 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08001570 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001571 else
1572 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001573
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001574 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08001575 if (!listeners)
1576 goto out_sock_release;
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001579 if (cfg && cfg->input)
1580 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001582 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001583 goto out_sock_release;
1584
1585 nlk = nlk_sk(sk);
1586 nlk->flags |= NETLINK_KERNEL_SOCKET;
1587
1588 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001589 if (!nl_table[unit].registered) {
1590 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001591 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001592 nl_table[unit].cb_mutex = cb_mutex;
1593 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001594 if (cfg) {
1595 nl_table[unit].bind = cfg->bind;
1596 nl_table[unit].flags = cfg->flags;
1597 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001598 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07001599 } else {
1600 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001601 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001602 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07001603 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001604 return sk;
1605
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001606out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001607 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08001608 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001609 return NULL;
1610
1611out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001612 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001613 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001615EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001616
1617void
1618netlink_kernel_release(struct sock *sk)
1619{
Denis V. Lunevedf02082008-02-29 11:18:32 -08001620 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001621}
1622EXPORT_SYMBOL(netlink_kernel_release);
1623
Johannes Bergd136f1b2009-09-12 03:03:15 +00001624int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001625{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001626 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001627 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001628
1629 if (groups < 32)
1630 groups = 32;
1631
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001632 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001633 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1634 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00001635 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001636 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001637 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1638 rcu_assign_pointer(tbl->listeners, new);
1639
Lai Jiangshan37b6b932011-03-15 18:01:42 +08001640 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001641 }
1642 tbl->groups = groups;
1643
Johannes Bergd136f1b2009-09-12 03:03:15 +00001644 return 0;
1645}
1646
1647/**
1648 * netlink_change_ngroups - change number of multicast groups
1649 *
1650 * This changes the number of multicast groups that are available
1651 * on a certain netlink family. Note that it is not possible to
1652 * change the number of groups to below 32. Also note that it does
1653 * not implicitly call netlink_clear_multicast_users() when the
1654 * number of groups is reduced.
1655 *
1656 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1657 * @groups: The new number of groups.
1658 */
1659int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1660{
1661 int err;
1662
1663 netlink_table_grab();
1664 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001665 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00001666
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001667 return err;
1668}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001669
Johannes Bergb8273572009-09-24 15:44:05 -07001670void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1671{
1672 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07001673 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1674
Sasha Levinb67bfe02013-02-27 17:06:00 -08001675 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07001676 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1677}
1678
Johannes Berg84659eb2007-07-18 15:47:05 -07001679/**
1680 * netlink_clear_multicast_users - kick off multicast listeners
1681 *
1682 * This function removes all listeners from the given group.
1683 * @ksk: The kernel netlink socket, as returned by
1684 * netlink_kernel_create().
1685 * @group: The multicast group to clear.
1686 */
1687void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1688{
Johannes Berg84659eb2007-07-18 15:47:05 -07001689 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07001690 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07001691 netlink_table_ungrab();
1692}
Johannes Berg84659eb2007-07-18 15:47:05 -07001693
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001694struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00001695__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001696{
1697 struct nlmsghdr *nlh;
1698 int size = NLMSG_LENGTH(len);
1699
1700 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1701 nlh->nlmsg_type = type;
1702 nlh->nlmsg_len = size;
1703 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001704 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001705 nlh->nlmsg_seq = seq;
1706 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
1707 memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size);
1708 return nlh;
1709}
1710EXPORT_SYMBOL(__nlmsg_put);
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712/*
1713 * It looks a bit ugly.
1714 * It would be better to create kernel thread.
1715 */
1716
1717static int netlink_dump(struct sock *sk)
1718{
1719 struct netlink_sock *nlk = nlk_sk(sk);
1720 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001721 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001723 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001724 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001726 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 cb = nlk->cb;
1729 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001730 err = -EINVAL;
1731 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Greg Rosec7ac8672011-06-10 01:27:09 +00001734 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1735
1736 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1737 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00001738 goto errout_skb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 len = cb->dump(skb, cb);
1741
1742 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001743 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001744
1745 if (sk_filter(sk, skb))
1746 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001747 else
1748 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return 0;
1750 }
1751
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001752 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1753 if (!nlh)
1754 goto errout_skb;
1755
Johannes Berg670dc282011-06-20 13:40:46 +02001756 nl_dump_check_consistent(cb, nlh);
1757
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001758 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1759
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001760 if (sk_filter(sk, skb))
1761 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001762 else
1763 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Thomas Grafa8f74b22005-11-10 02:25:52 +01001765 if (cb->done)
1766 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001768 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Gao feng6dc878a2012-10-04 20:15:48 +00001770 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001771 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07001773
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001774errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001775 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001776 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Gao feng6dc878a2012-10-04 20:15:48 +00001780int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1781 const struct nlmsghdr *nlh,
1782 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct netlink_callback *cb;
1785 struct sock *sk;
1786 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001787 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001789 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (cb == NULL)
1791 return -ENOBUFS;
1792
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001793 cb->dump = control->dump;
1794 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00001796 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00001797 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001798 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 atomic_inc(&skb->users);
1800 cb->skb = skb;
1801
Eric W. Biederman15e47302012-09-07 20:12:54 +00001802 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (sk == NULL) {
1804 netlink_destroy_callback(cb);
1805 return -ECONNREFUSED;
1806 }
1807 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00001808
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001809 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00001810 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07001811 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001812 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00001814 ret = -EBUSY;
1815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Gao feng6dc878a2012-10-04 20:15:48 +00001817 /* add reference of module which cb->dump belongs to */
1818 if (!try_module_get(cb->module)) {
1819 mutex_unlock(nlk->cb_mutex);
1820 netlink_destroy_callback(cb);
1821 ret = -EPROTONOSUPPORT;
1822 goto out;
1823 }
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001826 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Andrey Vaginb44d2112011-02-21 02:40:47 +00001828 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00001829out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001831
Andrey Vaginb44d2112011-02-21 02:40:47 +00001832 if (ret)
1833 return ret;
1834
Denis V. Lunev5c582982007-10-23 20:29:25 -07001835 /* We successfully started a dump, by returning -EINTR we
1836 * signal not to send ACK even if it was requested.
1837 */
1838 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
Gao feng6dc878a2012-10-04 20:15:48 +00001840EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1843{
1844 struct sk_buff *skb;
1845 struct nlmsghdr *rep;
1846 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08001847 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Thomas Graf339bf982006-11-10 14:10:15 -08001849 /* error messages get the original request appened */
1850 if (err)
1851 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Thomas Graf339bf982006-11-10 14:10:15 -08001853 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (!skb) {
1855 struct sock *sk;
1856
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001857 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001858 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001859 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (sk) {
1861 sk->sk_err = ENOBUFS;
1862 sk->sk_error_report(sk);
1863 sock_put(sk);
1864 }
1865 return;
1866 }
1867
Eric W. Biederman15e47302012-09-07 20:12:54 +00001868 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00001869 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001870 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001872 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00001873 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001875EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001877int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001878 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01001879{
Thomas Graf82ace472005-11-10 02:25:53 +01001880 struct nlmsghdr *nlh;
1881 int err;
1882
1883 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001884 int msglen;
1885
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001886 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07001887 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01001888
Martin Murrayad8e4b72006-01-10 13:02:29 -08001889 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01001890 return 0;
1891
Thomas Grafd35b6852007-03-22 23:28:46 -07001892 /* Only requests are handled by the kernel */
1893 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07001894 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07001895
Thomas Graf45e7ae72007-03-22 23:29:10 -07001896 /* Skip control messages */
1897 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07001898 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07001899
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001900 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07001901 if (err == -EINTR)
1902 goto skip;
1903
1904ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07001905 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01001906 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01001907
Denis V. Lunev5c582982007-10-23 20:29:25 -07001908skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001909 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001910 if (msglen > skb->len)
1911 msglen = skb->len;
1912 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01001913 }
1914
1915 return 0;
1916}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001917EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01001918
1919/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07001920 * nlmsg_notify - send a notification netlink message
1921 * @sk: netlink socket to use
1922 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00001923 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07001924 * @group: destination multicast group or 0
1925 * @report: 1 to report back, 0 to disable
1926 * @flags: allocation flags
1927 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001928int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07001929 unsigned int group, int report, gfp_t flags)
1930{
1931 int err = 0;
1932
1933 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001934 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07001935
1936 if (report) {
1937 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001938 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07001939 }
1940
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001941 /* errors reported via destination sk->sk_err, but propagate
1942 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001943 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001944 }
1945
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001946 if (report) {
1947 int err2;
1948
Eric W. Biederman15e47302012-09-07 20:12:54 +00001949 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001950 if (!err || err == -ESRCH)
1951 err = err2;
1952 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07001953
1954 return err;
1955}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001956EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958#ifdef CONFIG_PROC_FS
1959struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08001960 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 int link;
1962 int hash_idx;
1963};
1964
1965static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1966{
1967 struct nl_seq_iter *iter = seq->private;
1968 int i, j;
1969 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 loff_t off = 0;
1971
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001972 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001973 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001976 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001977 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001978 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (off == pos) {
1980 iter->link = i;
1981 iter->hash_idx = j;
1982 return s;
1983 }
1984 ++off;
1985 }
1986 }
1987 }
1988 return NULL;
1989}
1990
1991static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001992 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 read_lock(&nl_table_lock);
1995 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1996}
1997
1998static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1999{
2000 struct sock *s;
2001 struct nl_seq_iter *iter;
2002 int i, j;
2003
2004 ++*pos;
2005
2006 if (v == SEQ_START_TOKEN)
2007 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002008
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002009 iter = seq->private;
2010 s = v;
2011 do {
2012 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002013 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (s)
2015 return s;
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 i = iter->link;
2018 j = iter->hash_idx + 1;
2019
2020 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002021 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 for (; j <= hash->mask; j++) {
2024 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002025 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002026 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (s) {
2028 iter->link = i;
2029 iter->hash_idx = j;
2030 return s;
2031 }
2032 }
2033
2034 j = 0;
2035 } while (++i < MAX_LINKS);
2036
2037 return NULL;
2038}
2039
2040static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002041 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 read_unlock(&nl_table_lock);
2044}
2045
2046
2047static int netlink_seq_show(struct seq_file *seq, void *v)
2048{
Eric Dumazet658cb352012-04-22 21:30:21 +00002049 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 seq_puts(seq,
2051 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002052 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002053 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct sock *s = v;
2055 struct netlink_sock *nlk = nlk_sk(s);
2056
Hannes Frederic Sowa9f1e0ad2012-12-15 15:09:19 +00002057 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 s,
2059 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002060 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002061 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002062 sk_rmem_alloc_get(s),
2063 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002065 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002066 atomic_read(&s->sk_drops),
2067 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 );
2069
2070 }
2071 return 0;
2072}
2073
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002074static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 .start = netlink_seq_start,
2076 .next = netlink_seq_next,
2077 .stop = netlink_seq_stop,
2078 .show = netlink_seq_show,
2079};
2080
2081
2082static int netlink_seq_open(struct inode *inode, struct file *file)
2083{
Denis V. Luneve372c412007-11-19 22:31:54 -08002084 return seq_open_net(inode, file, &netlink_seq_ops,
2085 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002086}
2087
Arjan van de Venda7071d2007-02-12 00:55:36 -08002088static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 .owner = THIS_MODULE,
2090 .open = netlink_seq_open,
2091 .read = seq_read,
2092 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002093 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094};
2095
2096#endif
2097
2098int netlink_register_notifier(struct notifier_block *nb)
2099{
Alan Sterne041c682006-03-27 01:16:30 -08002100 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002102EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104int netlink_unregister_notifier(struct notifier_block *nb)
2105{
Alan Sterne041c682006-03-27 01:16:30 -08002106 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002108EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002109
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002110static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 .family = PF_NETLINK,
2112 .owner = THIS_MODULE,
2113 .release = netlink_release,
2114 .bind = netlink_bind,
2115 .connect = netlink_connect,
2116 .socketpair = sock_no_socketpair,
2117 .accept = sock_no_accept,
2118 .getname = netlink_getname,
2119 .poll = datagram_poll,
2120 .ioctl = sock_no_ioctl,
2121 .listen = sock_no_listen,
2122 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002123 .setsockopt = netlink_setsockopt,
2124 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 .sendmsg = netlink_sendmsg,
2126 .recvmsg = netlink_recvmsg,
2127 .mmap = sock_no_mmap,
2128 .sendpage = sock_no_sendpage,
2129};
2130
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002131static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 .family = PF_NETLINK,
2133 .create = netlink_create,
2134 .owner = THIS_MODULE, /* for consistency 8) */
2135};
2136
Pavel Emelyanov46650792007-10-08 20:38:39 -07002137static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002138{
2139#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002140 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002141 return -ENOMEM;
2142#endif
2143 return 0;
2144}
2145
Pavel Emelyanov46650792007-10-08 20:38:39 -07002146static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002147{
2148#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002149 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002150#endif
2151}
2152
David S. Millerb963ea82010-08-30 19:08:01 -07002153static void __init netlink_add_usersock_entry(void)
2154{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002155 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002156 int groups = 32;
2157
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002158 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002159 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002160 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002161
2162 netlink_table_grab();
2163
2164 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002165 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002166 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2167 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002168 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002169
2170 netlink_table_ungrab();
2171}
2172
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002173static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002174 .init = netlink_net_init,
2175 .exit = netlink_net_exit,
2176};
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178static int __init netlink_proto_init(void)
2179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002181 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 unsigned int order;
2183 int err = proto_register(&netlink_proto, 0);
2184
2185 if (err != 0)
2186 goto out;
2187
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002188 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002190 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002191 if (!nl_table)
2192 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Jan Beulich44813742009-09-21 17:03:05 -07002194 if (totalram_pages >= (128 * 1024))
2195 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 else
Jan Beulich44813742009-09-21 17:03:05 -07002197 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002199 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2200 limit = (1UL << order) / sizeof(struct hlist_head);
2201 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002204 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Eric W. Biederman15e47302012-09-07 20:12:54 +00002206 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (!hash->table) {
2208 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002209 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 1 * sizeof(*hash->table));
2211 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002212 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 hash->max_shift = order;
2215 hash->shift = 0;
2216 hash->mask = 0;
2217 hash->rehash_time = jiffies;
2218 }
2219
David S. Millerb963ea82010-08-30 19:08:01 -07002220 netlink_add_usersock_entry();
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002223 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002224 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 rtnetlink_init();
2226out:
2227 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002228panic:
2229 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232core_initcall(netlink_proto_init);