]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * NETLINK Kernel-user communication protocol. | |
3 | * | |
113aa838 | 4 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
5 | * 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. | |
746fac4d | 11 | * |
1da177e4 LT |
12 | * 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) | |
4fdb3bb7 HW |
16 | * 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 | |
1da177e4 LT |
22 | */ |
23 | ||
1da177e4 LT |
24 | #include <linux/module.h> |
25 | ||
4fc268d2 | 26 | #include <linux/capability.h> |
1da177e4 LT |
27 | #include <linux/kernel.h> |
28 | #include <linux/init.h> | |
1da177e4 LT |
29 | #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> | |
1da177e4 LT |
48 | #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> | |
54e0f520 | 56 | #include <linux/audit.h> |
af65bdfc | 57 | #include <linux/mutex.h> |
54e0f520 | 58 | |
457c4cbc | 59 | #include <net/net_namespace.h> |
1da177e4 LT |
60 | #include <net/sock.h> |
61 | #include <net/scm.h> | |
82ace47a | 62 | #include <net/netlink.h> |
1da177e4 | 63 | |
f7fa9b10 | 64 | #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8) |
b4ff4f04 | 65 | #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long)) |
1da177e4 LT |
66 | |
67 | struct netlink_sock { | |
68 | /* struct sock has to be the first member of netlink_sock */ | |
69 | struct sock sk; | |
70 | u32 pid; | |
1da177e4 | 71 | u32 dst_pid; |
d629b836 | 72 | u32 dst_group; |
f7fa9b10 PM |
73 | u32 flags; |
74 | u32 subscriptions; | |
75 | u32 ngroups; | |
76 | unsigned long *groups; | |
1da177e4 LT |
77 | unsigned long state; |
78 | wait_queue_head_t wait; | |
79 | struct netlink_callback *cb; | |
af65bdfc PM |
80 | struct mutex *cb_mutex; |
81 | struct mutex cb_def_mutex; | |
cd40b7d3 | 82 | void (*netlink_rcv)(struct sk_buff *skb); |
77247bbb | 83 | struct module *module; |
1da177e4 LT |
84 | }; |
85 | ||
5c398dc8 ED |
86 | struct listeners { |
87 | struct rcu_head rcu; | |
88 | unsigned long masks[0]; | |
6c04bb18 JB |
89 | }; |
90 | ||
77247bbb | 91 | #define NETLINK_KERNEL_SOCKET 0x1 |
9a4595bc | 92 | #define NETLINK_RECV_PKTINFO 0x2 |
be0c22a4 | 93 | #define NETLINK_BROADCAST_SEND_ERROR 0x4 |
38938bfe | 94 | #define NETLINK_RECV_NO_ENOBUFS 0x8 |
77247bbb | 95 | |
1da177e4 LT |
96 | static inline struct netlink_sock *nlk_sk(struct sock *sk) |
97 | { | |
32b21e03 | 98 | return container_of(sk, struct netlink_sock, sk); |
1da177e4 LT |
99 | } |
100 | ||
035c4c16 | 101 | static inline int netlink_is_kernel(struct sock *sk) |
aed81560 DL |
102 | { |
103 | return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET; | |
104 | } | |
105 | ||
1da177e4 | 106 | struct nl_pid_hash { |
658cb354 ED |
107 | struct hlist_head *table; |
108 | unsigned long rehash_time; | |
1da177e4 | 109 | |
658cb354 ED |
110 | unsigned int mask; |
111 | unsigned int shift; | |
1da177e4 | 112 | |
658cb354 ED |
113 | unsigned int entries; |
114 | unsigned int max_shift; | |
1da177e4 | 115 | |
658cb354 | 116 | u32 rnd; |
1da177e4 LT |
117 | }; |
118 | ||
119 | struct netlink_table { | |
658cb354 ED |
120 | struct nl_pid_hash hash; |
121 | struct hlist_head mc_list; | |
122 | struct listeners __rcu *listeners; | |
123 | unsigned int nl_nonroot; | |
124 | unsigned int groups; | |
125 | struct mutex *cb_mutex; | |
126 | struct module *module; | |
127 | int registered; | |
1da177e4 LT |
128 | }; |
129 | ||
130 | static struct netlink_table *nl_table; | |
131 | ||
132 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); | |
133 | ||
134 | static int netlink_dump(struct sock *sk); | |
1da177e4 LT |
135 | |
136 | static DEFINE_RWLOCK(nl_table_lock); | |
137 | static atomic_t nl_table_users = ATOMIC_INIT(0); | |
138 | ||
e041c683 | 139 | static ATOMIC_NOTIFIER_HEAD(netlink_chain); |
1da177e4 | 140 | |
b57ef81f | 141 | static inline u32 netlink_group_mask(u32 group) |
d629b836 PM |
142 | { |
143 | return group ? 1 << (group - 1) : 0; | |
144 | } | |
145 | ||
b57ef81f | 146 | static inline struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid) |
1da177e4 LT |
147 | { |
148 | return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask]; | |
149 | } | |
150 | ||
658cb354 ED |
151 | static void netlink_destroy_callback(struct netlink_callback *cb) |
152 | { | |
153 | kfree_skb(cb->skb); | |
154 | kfree(cb); | |
155 | } | |
156 | ||
1da177e4 LT |
157 | static void netlink_sock_destruct(struct sock *sk) |
158 | { | |
3f660d66 HX |
159 | struct netlink_sock *nlk = nlk_sk(sk); |
160 | ||
3f660d66 HX |
161 | if (nlk->cb) { |
162 | if (nlk->cb->done) | |
163 | nlk->cb->done(nlk->cb); | |
164 | netlink_destroy_callback(nlk->cb); | |
165 | } | |
166 | ||
1da177e4 LT |
167 | skb_queue_purge(&sk->sk_receive_queue); |
168 | ||
169 | if (!sock_flag(sk, SOCK_DEAD)) { | |
6ac552fd | 170 | printk(KERN_ERR "Freeing alive netlink socket %p\n", sk); |
1da177e4 LT |
171 | return; |
172 | } | |
547b792c IJ |
173 | |
174 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); | |
175 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); | |
176 | WARN_ON(nlk_sk(sk)->groups); | |
1da177e4 LT |
177 | } |
178 | ||
6ac552fd PM |
179 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on |
180 | * SMP. Look, when several writers sleep and reader wakes them up, all but one | |
1da177e4 LT |
181 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves |
182 | * this, _but_ remember, it adds useless work on UP machines. | |
183 | */ | |
184 | ||
d136f1bd | 185 | void netlink_table_grab(void) |
9a429c49 | 186 | __acquires(nl_table_lock) |
1da177e4 | 187 | { |
d136f1bd JB |
188 | might_sleep(); |
189 | ||
6abd219c | 190 | write_lock_irq(&nl_table_lock); |
1da177e4 LT |
191 | |
192 | if (atomic_read(&nl_table_users)) { | |
193 | DECLARE_WAITQUEUE(wait, current); | |
194 | ||
195 | add_wait_queue_exclusive(&nl_table_wait, &wait); | |
6ac552fd | 196 | for (;;) { |
1da177e4 LT |
197 | set_current_state(TASK_UNINTERRUPTIBLE); |
198 | if (atomic_read(&nl_table_users) == 0) | |
199 | break; | |
6abd219c | 200 | write_unlock_irq(&nl_table_lock); |
1da177e4 | 201 | schedule(); |
6abd219c | 202 | write_lock_irq(&nl_table_lock); |
1da177e4 LT |
203 | } |
204 | ||
205 | __set_current_state(TASK_RUNNING); | |
206 | remove_wait_queue(&nl_table_wait, &wait); | |
207 | } | |
208 | } | |
209 | ||
d136f1bd | 210 | void netlink_table_ungrab(void) |
9a429c49 | 211 | __releases(nl_table_lock) |
1da177e4 | 212 | { |
6abd219c | 213 | write_unlock_irq(&nl_table_lock); |
1da177e4 LT |
214 | wake_up(&nl_table_wait); |
215 | } | |
216 | ||
6ac552fd | 217 | static inline void |
1da177e4 LT |
218 | netlink_lock_table(void) |
219 | { | |
220 | /* read_lock() synchronizes us to netlink_table_grab */ | |
221 | ||
222 | read_lock(&nl_table_lock); | |
223 | atomic_inc(&nl_table_users); | |
224 | read_unlock(&nl_table_lock); | |
225 | } | |
226 | ||
6ac552fd | 227 | static inline void |
1da177e4 LT |
228 | netlink_unlock_table(void) |
229 | { | |
230 | if (atomic_dec_and_test(&nl_table_users)) | |
231 | wake_up(&nl_table_wait); | |
232 | } | |
233 | ||
b57ef81f | 234 | static struct sock *netlink_lookup(struct net *net, int protocol, u32 pid) |
1da177e4 LT |
235 | { |
236 | struct nl_pid_hash *hash = &nl_table[protocol].hash; | |
237 | struct hlist_head *head; | |
238 | struct sock *sk; | |
239 | struct hlist_node *node; | |
240 | ||
241 | read_lock(&nl_table_lock); | |
242 | head = nl_pid_hashfn(hash, pid); | |
243 | sk_for_each(sk, node, head) { | |
878628fb | 244 | if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) { |
1da177e4 LT |
245 | sock_hold(sk); |
246 | goto found; | |
247 | } | |
248 | } | |
249 | sk = NULL; | |
250 | found: | |
251 | read_unlock(&nl_table_lock); | |
252 | return sk; | |
253 | } | |
254 | ||
b57ef81f | 255 | static struct hlist_head *nl_pid_hash_zalloc(size_t size) |
1da177e4 LT |
256 | { |
257 | if (size <= PAGE_SIZE) | |
ea72912c | 258 | return kzalloc(size, GFP_ATOMIC); |
1da177e4 LT |
259 | else |
260 | return (struct hlist_head *) | |
ea72912c ED |
261 | __get_free_pages(GFP_ATOMIC | __GFP_ZERO, |
262 | get_order(size)); | |
1da177e4 LT |
263 | } |
264 | ||
b57ef81f | 265 | static void nl_pid_hash_free(struct hlist_head *table, size_t size) |
1da177e4 LT |
266 | { |
267 | if (size <= PAGE_SIZE) | |
268 | kfree(table); | |
269 | else | |
270 | free_pages((unsigned long)table, get_order(size)); | |
271 | } | |
272 | ||
273 | static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow) | |
274 | { | |
275 | unsigned int omask, mask, shift; | |
276 | size_t osize, size; | |
277 | struct hlist_head *otable, *table; | |
278 | int i; | |
279 | ||
280 | omask = mask = hash->mask; | |
281 | osize = size = (mask + 1) * sizeof(*table); | |
282 | shift = hash->shift; | |
283 | ||
284 | if (grow) { | |
285 | if (++shift > hash->max_shift) | |
286 | return 0; | |
287 | mask = mask * 2 + 1; | |
288 | size *= 2; | |
289 | } | |
290 | ||
ea72912c | 291 | table = nl_pid_hash_zalloc(size); |
1da177e4 LT |
292 | if (!table) |
293 | return 0; | |
294 | ||
1da177e4 LT |
295 | otable = hash->table; |
296 | hash->table = table; | |
297 | hash->mask = mask; | |
298 | hash->shift = shift; | |
299 | get_random_bytes(&hash->rnd, sizeof(hash->rnd)); | |
300 | ||
301 | for (i = 0; i <= omask; i++) { | |
302 | struct sock *sk; | |
303 | struct hlist_node *node, *tmp; | |
304 | ||
305 | sk_for_each_safe(sk, node, tmp, &otable[i]) | |
306 | __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid)); | |
307 | } | |
308 | ||
309 | nl_pid_hash_free(otable, osize); | |
310 | hash->rehash_time = jiffies + 10 * 60 * HZ; | |
311 | return 1; | |
312 | } | |
313 | ||
314 | static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len) | |
315 | { | |
316 | int avg = hash->entries >> hash->shift; | |
317 | ||
318 | if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1)) | |
319 | return 1; | |
320 | ||
321 | if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) { | |
322 | nl_pid_hash_rehash(hash, 0); | |
323 | return 1; | |
324 | } | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
90ddc4f0 | 329 | static const struct proto_ops netlink_ops; |
1da177e4 | 330 | |
4277a083 PM |
331 | static void |
332 | netlink_update_listeners(struct sock *sk) | |
333 | { | |
334 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; | |
335 | struct hlist_node *node; | |
336 | unsigned long mask; | |
337 | unsigned int i; | |
338 | ||
b4ff4f04 | 339 | for (i = 0; i < NLGRPLONGS(tbl->groups); i++) { |
4277a083 | 340 | mask = 0; |
b4ff4f04 JB |
341 | sk_for_each_bound(sk, node, &tbl->mc_list) { |
342 | if (i < NLGRPLONGS(nlk_sk(sk)->ngroups)) | |
343 | mask |= nlk_sk(sk)->groups[i]; | |
344 | } | |
5c398dc8 | 345 | tbl->listeners->masks[i] = mask; |
4277a083 PM |
346 | } |
347 | /* this function is only called with the netlink table "grabbed", which | |
348 | * makes sure updates are visible before bind or setsockopt return. */ | |
349 | } | |
350 | ||
b4b51029 | 351 | static int netlink_insert(struct sock *sk, struct net *net, u32 pid) |
1da177e4 LT |
352 | { |
353 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; | |
354 | struct hlist_head *head; | |
355 | int err = -EADDRINUSE; | |
356 | struct sock *osk; | |
357 | struct hlist_node *node; | |
358 | int len; | |
359 | ||
360 | netlink_table_grab(); | |
361 | head = nl_pid_hashfn(hash, pid); | |
362 | len = 0; | |
363 | sk_for_each(osk, node, head) { | |
878628fb | 364 | if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid)) |
1da177e4 LT |
365 | break; |
366 | len++; | |
367 | } | |
368 | if (node) | |
369 | goto err; | |
370 | ||
371 | err = -EBUSY; | |
372 | if (nlk_sk(sk)->pid) | |
373 | goto err; | |
374 | ||
375 | err = -ENOMEM; | |
376 | if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX)) | |
377 | goto err; | |
378 | ||
379 | if (len && nl_pid_hash_dilute(hash, len)) | |
380 | head = nl_pid_hashfn(hash, pid); | |
381 | hash->entries++; | |
382 | nlk_sk(sk)->pid = pid; | |
383 | sk_add_node(sk, head); | |
384 | err = 0; | |
385 | ||
386 | err: | |
387 | netlink_table_ungrab(); | |
388 | return err; | |
389 | } | |
390 | ||
391 | static void netlink_remove(struct sock *sk) | |
392 | { | |
393 | netlink_table_grab(); | |
d470e3b4 DM |
394 | if (sk_del_node_init(sk)) |
395 | nl_table[sk->sk_protocol].hash.entries--; | |
f7fa9b10 | 396 | if (nlk_sk(sk)->subscriptions) |
1da177e4 LT |
397 | __sk_del_bind_node(sk); |
398 | netlink_table_ungrab(); | |
399 | } | |
400 | ||
401 | static struct proto netlink_proto = { | |
402 | .name = "NETLINK", | |
403 | .owner = THIS_MODULE, | |
404 | .obj_size = sizeof(struct netlink_sock), | |
405 | }; | |
406 | ||
1b8d7ae4 EB |
407 | static int __netlink_create(struct net *net, struct socket *sock, |
408 | struct mutex *cb_mutex, int protocol) | |
1da177e4 LT |
409 | { |
410 | struct sock *sk; | |
411 | struct netlink_sock *nlk; | |
ab33a171 PM |
412 | |
413 | sock->ops = &netlink_ops; | |
414 | ||
6257ff21 | 415 | sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto); |
ab33a171 PM |
416 | if (!sk) |
417 | return -ENOMEM; | |
418 | ||
419 | sock_init_data(sock, sk); | |
420 | ||
421 | nlk = nlk_sk(sk); | |
658cb354 | 422 | if (cb_mutex) { |
ffa4d721 | 423 | nlk->cb_mutex = cb_mutex; |
658cb354 | 424 | } else { |
ffa4d721 PM |
425 | nlk->cb_mutex = &nlk->cb_def_mutex; |
426 | mutex_init(nlk->cb_mutex); | |
427 | } | |
ab33a171 PM |
428 | init_waitqueue_head(&nlk->wait); |
429 | ||
430 | sk->sk_destruct = netlink_sock_destruct; | |
431 | sk->sk_protocol = protocol; | |
432 | return 0; | |
433 | } | |
434 | ||
3f378b68 EP |
435 | static int netlink_create(struct net *net, struct socket *sock, int protocol, |
436 | int kern) | |
ab33a171 PM |
437 | { |
438 | struct module *module = NULL; | |
af65bdfc | 439 | struct mutex *cb_mutex; |
f7fa9b10 | 440 | struct netlink_sock *nlk; |
ab33a171 | 441 | int err = 0; |
1da177e4 LT |
442 | |
443 | sock->state = SS_UNCONNECTED; | |
444 | ||
445 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) | |
446 | return -ESOCKTNOSUPPORT; | |
447 | ||
6ac552fd | 448 | if (protocol < 0 || protocol >= MAX_LINKS) |
1da177e4 LT |
449 | return -EPROTONOSUPPORT; |
450 | ||
77247bbb | 451 | netlink_lock_table(); |
95a5afca | 452 | #ifdef CONFIG_MODULES |
ab33a171 | 453 | if (!nl_table[protocol].registered) { |
77247bbb | 454 | netlink_unlock_table(); |
4fdb3bb7 | 455 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
77247bbb | 456 | netlink_lock_table(); |
4fdb3bb7 | 457 | } |
ab33a171 PM |
458 | #endif |
459 | if (nl_table[protocol].registered && | |
460 | try_module_get(nl_table[protocol].module)) | |
461 | module = nl_table[protocol].module; | |
974c37e9 AD |
462 | else |
463 | err = -EPROTONOSUPPORT; | |
af65bdfc | 464 | cb_mutex = nl_table[protocol].cb_mutex; |
77247bbb | 465 | netlink_unlock_table(); |
4fdb3bb7 | 466 | |
974c37e9 AD |
467 | if (err < 0) |
468 | goto out; | |
469 | ||
6ac552fd PM |
470 | err = __netlink_create(net, sock, cb_mutex, protocol); |
471 | if (err < 0) | |
f7fa9b10 PM |
472 | goto out_module; |
473 | ||
6f756a8c | 474 | local_bh_disable(); |
c1fd3b94 | 475 | sock_prot_inuse_add(net, &netlink_proto, 1); |
6f756a8c DM |
476 | local_bh_enable(); |
477 | ||
f7fa9b10 | 478 | nlk = nlk_sk(sock->sk); |
f7fa9b10 | 479 | nlk->module = module; |
ab33a171 PM |
480 | out: |
481 | return err; | |
1da177e4 | 482 | |
ab33a171 PM |
483 | out_module: |
484 | module_put(module); | |
485 | goto out; | |
1da177e4 LT |
486 | } |
487 | ||
488 | static int netlink_release(struct socket *sock) | |
489 | { | |
490 | struct sock *sk = sock->sk; | |
491 | struct netlink_sock *nlk; | |
492 | ||
493 | if (!sk) | |
494 | return 0; | |
495 | ||
496 | netlink_remove(sk); | |
ac57b3a9 | 497 | sock_orphan(sk); |
1da177e4 LT |
498 | nlk = nlk_sk(sk); |
499 | ||
3f660d66 HX |
500 | /* |
501 | * OK. Socket is unlinked, any packets that arrive now | |
502 | * will be purged. | |
503 | */ | |
1da177e4 | 504 | |
1da177e4 LT |
505 | sock->sk = NULL; |
506 | wake_up_interruptible_all(&nlk->wait); | |
507 | ||
508 | skb_queue_purge(&sk->sk_write_queue); | |
509 | ||
649300b9 | 510 | if (nlk->pid) { |
1da177e4 | 511 | struct netlink_notify n = { |
3b1e0a65 | 512 | .net = sock_net(sk), |
1da177e4 LT |
513 | .protocol = sk->sk_protocol, |
514 | .pid = nlk->pid, | |
515 | }; | |
e041c683 AS |
516 | atomic_notifier_call_chain(&netlink_chain, |
517 | NETLINK_URELEASE, &n); | |
746fac4d | 518 | } |
4fdb3bb7 | 519 | |
5e7c001c | 520 | module_put(nlk->module); |
4fdb3bb7 | 521 | |
4277a083 | 522 | netlink_table_grab(); |
aed81560 | 523 | if (netlink_is_kernel(sk)) { |
869e58f8 DL |
524 | BUG_ON(nl_table[sk->sk_protocol].registered == 0); |
525 | if (--nl_table[sk->sk_protocol].registered == 0) { | |
526 | kfree(nl_table[sk->sk_protocol].listeners); | |
527 | nl_table[sk->sk_protocol].module = NULL; | |
528 | nl_table[sk->sk_protocol].registered = 0; | |
529 | } | |
658cb354 | 530 | } else if (nlk->subscriptions) { |
4277a083 | 531 | netlink_update_listeners(sk); |
658cb354 | 532 | } |
4277a083 | 533 | netlink_table_ungrab(); |
77247bbb | 534 | |
f7fa9b10 PM |
535 | kfree(nlk->groups); |
536 | nlk->groups = NULL; | |
537 | ||
3755810c | 538 | local_bh_disable(); |
c1fd3b94 | 539 | sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1); |
3755810c | 540 | local_bh_enable(); |
1da177e4 LT |
541 | sock_put(sk); |
542 | return 0; | |
543 | } | |
544 | ||
545 | static int netlink_autobind(struct socket *sock) | |
546 | { | |
547 | struct sock *sk = sock->sk; | |
3b1e0a65 | 548 | struct net *net = sock_net(sk); |
1da177e4 LT |
549 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; |
550 | struct hlist_head *head; | |
551 | struct sock *osk; | |
552 | struct hlist_node *node; | |
66aa4a55 | 553 | s32 pid = task_tgid_vnr(current); |
1da177e4 LT |
554 | int err; |
555 | static s32 rover = -4097; | |
556 | ||
557 | retry: | |
558 | cond_resched(); | |
559 | netlink_table_grab(); | |
560 | head = nl_pid_hashfn(hash, pid); | |
561 | sk_for_each(osk, node, head) { | |
878628fb | 562 | if (!net_eq(sock_net(osk), net)) |
b4b51029 | 563 | continue; |
1da177e4 LT |
564 | if (nlk_sk(osk)->pid == pid) { |
565 | /* Bind collision, search negative pid values. */ | |
566 | pid = rover--; | |
567 | if (rover > -4097) | |
568 | rover = -4097; | |
569 | netlink_table_ungrab(); | |
570 | goto retry; | |
571 | } | |
572 | } | |
573 | netlink_table_ungrab(); | |
574 | ||
b4b51029 | 575 | err = netlink_insert(sk, net, pid); |
1da177e4 LT |
576 | if (err == -EADDRINUSE) |
577 | goto retry; | |
d470e3b4 DM |
578 | |
579 | /* If 2 threads race to autobind, that is fine. */ | |
580 | if (err == -EBUSY) | |
581 | err = 0; | |
582 | ||
583 | return err; | |
1da177e4 LT |
584 | } |
585 | ||
b57ef81f | 586 | static inline int netlink_capable(const struct socket *sock, unsigned int flag) |
746fac4d | 587 | { |
1da177e4 LT |
588 | return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) || |
589 | capable(CAP_NET_ADMIN); | |
746fac4d | 590 | } |
1da177e4 | 591 | |
f7fa9b10 PM |
592 | static void |
593 | netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) | |
594 | { | |
595 | struct netlink_sock *nlk = nlk_sk(sk); | |
596 | ||
597 | if (nlk->subscriptions && !subscriptions) | |
598 | __sk_del_bind_node(sk); | |
599 | else if (!nlk->subscriptions && subscriptions) | |
600 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); | |
601 | nlk->subscriptions = subscriptions; | |
602 | } | |
603 | ||
b4ff4f04 | 604 | static int netlink_realloc_groups(struct sock *sk) |
513c2500 PM |
605 | { |
606 | struct netlink_sock *nlk = nlk_sk(sk); | |
607 | unsigned int groups; | |
b4ff4f04 | 608 | unsigned long *new_groups; |
513c2500 PM |
609 | int err = 0; |
610 | ||
b4ff4f04 JB |
611 | netlink_table_grab(); |
612 | ||
513c2500 | 613 | groups = nl_table[sk->sk_protocol].groups; |
b4ff4f04 | 614 | if (!nl_table[sk->sk_protocol].registered) { |
513c2500 | 615 | err = -ENOENT; |
b4ff4f04 JB |
616 | goto out_unlock; |
617 | } | |
513c2500 | 618 | |
b4ff4f04 JB |
619 | if (nlk->ngroups >= groups) |
620 | goto out_unlock; | |
513c2500 | 621 | |
b4ff4f04 JB |
622 | new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC); |
623 | if (new_groups == NULL) { | |
624 | err = -ENOMEM; | |
625 | goto out_unlock; | |
626 | } | |
6ac552fd | 627 | memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0, |
b4ff4f04 JB |
628 | NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups)); |
629 | ||
630 | nlk->groups = new_groups; | |
513c2500 | 631 | nlk->ngroups = groups; |
b4ff4f04 JB |
632 | out_unlock: |
633 | netlink_table_ungrab(); | |
634 | return err; | |
513c2500 PM |
635 | } |
636 | ||
6ac552fd PM |
637 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, |
638 | int addr_len) | |
1da177e4 LT |
639 | { |
640 | struct sock *sk = sock->sk; | |
3b1e0a65 | 641 | struct net *net = sock_net(sk); |
1da177e4 LT |
642 | struct netlink_sock *nlk = nlk_sk(sk); |
643 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; | |
644 | int err; | |
746fac4d | 645 | |
1da177e4 LT |
646 | if (nladdr->nl_family != AF_NETLINK) |
647 | return -EINVAL; | |
648 | ||
649 | /* Only superuser is allowed to listen multicasts */ | |
513c2500 PM |
650 | if (nladdr->nl_groups) { |
651 | if (!netlink_capable(sock, NL_NONROOT_RECV)) | |
652 | return -EPERM; | |
b4ff4f04 JB |
653 | err = netlink_realloc_groups(sk); |
654 | if (err) | |
655 | return err; | |
513c2500 | 656 | } |
1da177e4 LT |
657 | |
658 | if (nlk->pid) { | |
659 | if (nladdr->nl_pid != nlk->pid) | |
660 | return -EINVAL; | |
661 | } else { | |
662 | err = nladdr->nl_pid ? | |
b4b51029 | 663 | netlink_insert(sk, net, nladdr->nl_pid) : |
1da177e4 LT |
664 | netlink_autobind(sock); |
665 | if (err) | |
666 | return err; | |
667 | } | |
668 | ||
513c2500 | 669 | if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0])) |
1da177e4 LT |
670 | return 0; |
671 | ||
672 | netlink_table_grab(); | |
f7fa9b10 | 673 | netlink_update_subscriptions(sk, nlk->subscriptions + |
746fac4d YH |
674 | hweight32(nladdr->nl_groups) - |
675 | hweight32(nlk->groups[0])); | |
676 | nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups; | |
4277a083 | 677 | netlink_update_listeners(sk); |
1da177e4 LT |
678 | netlink_table_ungrab(); |
679 | ||
680 | return 0; | |
681 | } | |
682 | ||
683 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, | |
684 | int alen, int flags) | |
685 | { | |
686 | int err = 0; | |
687 | struct sock *sk = sock->sk; | |
688 | struct netlink_sock *nlk = nlk_sk(sk); | |
6ac552fd | 689 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
1da177e4 | 690 | |
6503d961 CG |
691 | if (alen < sizeof(addr->sa_family)) |
692 | return -EINVAL; | |
693 | ||
1da177e4 LT |
694 | if (addr->sa_family == AF_UNSPEC) { |
695 | sk->sk_state = NETLINK_UNCONNECTED; | |
696 | nlk->dst_pid = 0; | |
d629b836 | 697 | nlk->dst_group = 0; |
1da177e4 LT |
698 | return 0; |
699 | } | |
700 | if (addr->sa_family != AF_NETLINK) | |
701 | return -EINVAL; | |
702 | ||
703 | /* Only superuser is allowed to send multicasts */ | |
704 | if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND)) | |
705 | return -EPERM; | |
706 | ||
707 | if (!nlk->pid) | |
708 | err = netlink_autobind(sock); | |
709 | ||
710 | if (err == 0) { | |
711 | sk->sk_state = NETLINK_CONNECTED; | |
712 | nlk->dst_pid = nladdr->nl_pid; | |
d629b836 | 713 | nlk->dst_group = ffs(nladdr->nl_groups); |
1da177e4 LT |
714 | } |
715 | ||
716 | return err; | |
717 | } | |
718 | ||
6ac552fd PM |
719 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, |
720 | int *addr_len, int peer) | |
1da177e4 LT |
721 | { |
722 | struct sock *sk = sock->sk; | |
723 | struct netlink_sock *nlk = nlk_sk(sk); | |
13cfa97b | 724 | DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr); |
746fac4d | 725 | |
1da177e4 LT |
726 | nladdr->nl_family = AF_NETLINK; |
727 | nladdr->nl_pad = 0; | |
728 | *addr_len = sizeof(*nladdr); | |
729 | ||
730 | if (peer) { | |
731 | nladdr->nl_pid = nlk->dst_pid; | |
d629b836 | 732 | nladdr->nl_groups = netlink_group_mask(nlk->dst_group); |
1da177e4 LT |
733 | } else { |
734 | nladdr->nl_pid = nlk->pid; | |
513c2500 | 735 | nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; |
1da177e4 LT |
736 | } |
737 | return 0; | |
738 | } | |
739 | ||
740 | static void netlink_overrun(struct sock *sk) | |
741 | { | |
38938bfe PNA |
742 | struct netlink_sock *nlk = nlk_sk(sk); |
743 | ||
744 | if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) { | |
745 | if (!test_and_set_bit(0, &nlk_sk(sk)->state)) { | |
746 | sk->sk_err = ENOBUFS; | |
747 | sk->sk_error_report(sk); | |
748 | } | |
1da177e4 | 749 | } |
38938bfe | 750 | atomic_inc(&sk->sk_drops); |
1da177e4 LT |
751 | } |
752 | ||
753 | static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid) | |
754 | { | |
1da177e4 LT |
755 | struct sock *sock; |
756 | struct netlink_sock *nlk; | |
757 | ||
3b1e0a65 | 758 | sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid); |
1da177e4 LT |
759 | if (!sock) |
760 | return ERR_PTR(-ECONNREFUSED); | |
761 | ||
762 | /* Don't bother queuing skb if kernel socket has no input function */ | |
763 | nlk = nlk_sk(sock); | |
cd40b7d3 DL |
764 | if (sock->sk_state == NETLINK_CONNECTED && |
765 | nlk->dst_pid != nlk_sk(ssk)->pid) { | |
1da177e4 LT |
766 | sock_put(sock); |
767 | return ERR_PTR(-ECONNREFUSED); | |
768 | } | |
769 | return sock; | |
770 | } | |
771 | ||
772 | struct sock *netlink_getsockbyfilp(struct file *filp) | |
773 | { | |
6db5fc5d | 774 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
775 | struct sock *sock; |
776 | ||
777 | if (!S_ISSOCK(inode->i_mode)) | |
778 | return ERR_PTR(-ENOTSOCK); | |
779 | ||
780 | sock = SOCKET_I(inode)->sk; | |
781 | if (sock->sk_family != AF_NETLINK) | |
782 | return ERR_PTR(-EINVAL); | |
783 | ||
784 | sock_hold(sock); | |
785 | return sock; | |
786 | } | |
787 | ||
788 | /* | |
789 | * Attach a skb to a netlink socket. | |
790 | * The caller must hold a reference to the destination socket. On error, the | |
791 | * reference is dropped. The skb is not send to the destination, just all | |
792 | * all error checks are performed and memory in the queue is reserved. | |
793 | * Return values: | |
794 | * < 0: error. skb freed, reference to sock dropped. | |
795 | * 0: continue | |
796 | * 1: repeat lookup - reference dropped while waiting for socket memory. | |
797 | */ | |
9457afee | 798 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, |
c3d8d1e3 | 799 | long *timeo, struct sock *ssk) |
1da177e4 LT |
800 | { |
801 | struct netlink_sock *nlk; | |
802 | ||
803 | nlk = nlk_sk(sk); | |
804 | ||
805 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || | |
806 | test_bit(0, &nlk->state)) { | |
807 | DECLARE_WAITQUEUE(wait, current); | |
c3d8d1e3 | 808 | if (!*timeo) { |
aed81560 | 809 | if (!ssk || netlink_is_kernel(ssk)) |
1da177e4 LT |
810 | netlink_overrun(sk); |
811 | sock_put(sk); | |
812 | kfree_skb(skb); | |
813 | return -EAGAIN; | |
814 | } | |
815 | ||
816 | __set_current_state(TASK_INTERRUPTIBLE); | |
817 | add_wait_queue(&nlk->wait, &wait); | |
818 | ||
819 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || | |
820 | test_bit(0, &nlk->state)) && | |
821 | !sock_flag(sk, SOCK_DEAD)) | |
c3d8d1e3 | 822 | *timeo = schedule_timeout(*timeo); |
1da177e4 LT |
823 | |
824 | __set_current_state(TASK_RUNNING); | |
825 | remove_wait_queue(&nlk->wait, &wait); | |
826 | sock_put(sk); | |
827 | ||
828 | if (signal_pending(current)) { | |
829 | kfree_skb(skb); | |
c3d8d1e3 | 830 | return sock_intr_errno(*timeo); |
1da177e4 LT |
831 | } |
832 | return 1; | |
833 | } | |
834 | skb_set_owner_r(skb, sk); | |
835 | return 0; | |
836 | } | |
837 | ||
4a7e7c2a | 838 | static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
1da177e4 | 839 | { |
1da177e4 LT |
840 | int len = skb->len; |
841 | ||
1da177e4 LT |
842 | skb_queue_tail(&sk->sk_receive_queue, skb); |
843 | sk->sk_data_ready(sk, len); | |
4a7e7c2a ED |
844 | return len; |
845 | } | |
846 | ||
847 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb) | |
848 | { | |
849 | int len = __netlink_sendskb(sk, skb); | |
850 | ||
1da177e4 LT |
851 | sock_put(sk); |
852 | return len; | |
853 | } | |
854 | ||
855 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) | |
856 | { | |
857 | kfree_skb(skb); | |
858 | sock_put(sk); | |
859 | } | |
860 | ||
b57ef81f | 861 | static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation) |
1da177e4 LT |
862 | { |
863 | int delta; | |
864 | ||
865 | skb_orphan(skb); | |
866 | ||
4305b541 | 867 | delta = skb->end - skb->tail; |
1da177e4 LT |
868 | if (delta * 2 < skb->truesize) |
869 | return skb; | |
870 | ||
871 | if (skb_shared(skb)) { | |
872 | struct sk_buff *nskb = skb_clone(skb, allocation); | |
873 | if (!nskb) | |
874 | return skb; | |
8460c00f | 875 | consume_skb(skb); |
1da177e4 LT |
876 | skb = nskb; |
877 | } | |
878 | ||
879 | if (!pskb_expand_head(skb, 0, -delta, allocation)) | |
880 | skb->truesize -= delta; | |
881 | ||
882 | return skb; | |
883 | } | |
884 | ||
b57ef81f | 885 | static void netlink_rcv_wake(struct sock *sk) |
cd40b7d3 DL |
886 | { |
887 | struct netlink_sock *nlk = nlk_sk(sk); | |
888 | ||
889 | if (skb_queue_empty(&sk->sk_receive_queue)) | |
890 | clear_bit(0, &nlk->state); | |
891 | if (!test_bit(0, &nlk->state)) | |
892 | wake_up_interruptible(&nlk->wait); | |
893 | } | |
894 | ||
b57ef81f | 895 | static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb) |
cd40b7d3 DL |
896 | { |
897 | int ret; | |
898 | struct netlink_sock *nlk = nlk_sk(sk); | |
899 | ||
900 | ret = -ECONNREFUSED; | |
901 | if (nlk->netlink_rcv != NULL) { | |
902 | ret = skb->len; | |
903 | skb_set_owner_r(skb, sk); | |
904 | nlk->netlink_rcv(skb); | |
905 | } | |
906 | kfree_skb(skb); | |
907 | sock_put(sk); | |
908 | return ret; | |
909 | } | |
910 | ||
911 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, | |
912 | u32 pid, int nonblock) | |
1da177e4 LT |
913 | { |
914 | struct sock *sk; | |
915 | int err; | |
916 | long timeo; | |
917 | ||
918 | skb = netlink_trim(skb, gfp_any()); | |
919 | ||
920 | timeo = sock_sndtimeo(ssk, nonblock); | |
921 | retry: | |
922 | sk = netlink_getsockbypid(ssk, pid); | |
923 | if (IS_ERR(sk)) { | |
924 | kfree_skb(skb); | |
925 | return PTR_ERR(sk); | |
926 | } | |
cd40b7d3 DL |
927 | if (netlink_is_kernel(sk)) |
928 | return netlink_unicast_kernel(sk, skb); | |
929 | ||
b1153f29 | 930 | if (sk_filter(sk, skb)) { |
84874607 | 931 | err = skb->len; |
b1153f29 SH |
932 | kfree_skb(skb); |
933 | sock_put(sk); | |
934 | return err; | |
935 | } | |
936 | ||
9457afee | 937 | err = netlink_attachskb(sk, skb, &timeo, ssk); |
1da177e4 LT |
938 | if (err == 1) |
939 | goto retry; | |
940 | if (err) | |
941 | return err; | |
942 | ||
7ee015e0 | 943 | return netlink_sendskb(sk, skb); |
1da177e4 | 944 | } |
6ac552fd | 945 | EXPORT_SYMBOL(netlink_unicast); |
1da177e4 | 946 | |
4277a083 PM |
947 | int netlink_has_listeners(struct sock *sk, unsigned int group) |
948 | { | |
949 | int res = 0; | |
5c398dc8 | 950 | struct listeners *listeners; |
4277a083 | 951 | |
aed81560 | 952 | BUG_ON(!netlink_is_kernel(sk)); |
b4ff4f04 JB |
953 | |
954 | rcu_read_lock(); | |
955 | listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners); | |
956 | ||
4277a083 | 957 | if (group - 1 < nl_table[sk->sk_protocol].groups) |
5c398dc8 | 958 | res = test_bit(group - 1, listeners->masks); |
b4ff4f04 JB |
959 | |
960 | rcu_read_unlock(); | |
961 | ||
4277a083 PM |
962 | return res; |
963 | } | |
964 | EXPORT_SYMBOL_GPL(netlink_has_listeners); | |
965 | ||
b57ef81f | 966 | static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) |
1da177e4 LT |
967 | { |
968 | struct netlink_sock *nlk = nlk_sk(sk); | |
969 | ||
970 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && | |
971 | !test_bit(0, &nlk->state)) { | |
972 | skb_set_owner_r(skb, sk); | |
4a7e7c2a | 973 | __netlink_sendskb(sk, skb); |
2c645800 | 974 | return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1); |
1da177e4 LT |
975 | } |
976 | return -1; | |
977 | } | |
978 | ||
979 | struct netlink_broadcast_data { | |
980 | struct sock *exclude_sk; | |
b4b51029 | 981 | struct net *net; |
1da177e4 LT |
982 | u32 pid; |
983 | u32 group; | |
984 | int failure; | |
ff491a73 | 985 | int delivery_failure; |
1da177e4 LT |
986 | int congested; |
987 | int delivered; | |
7d877f3b | 988 | gfp_t allocation; |
1da177e4 | 989 | struct sk_buff *skb, *skb2; |
910a7e90 EB |
990 | int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data); |
991 | void *tx_data; | |
1da177e4 LT |
992 | }; |
993 | ||
b57ef81f | 994 | static int do_one_broadcast(struct sock *sk, |
1da177e4 LT |
995 | struct netlink_broadcast_data *p) |
996 | { | |
997 | struct netlink_sock *nlk = nlk_sk(sk); | |
998 | int val; | |
999 | ||
1000 | if (p->exclude_sk == sk) | |
1001 | goto out; | |
1002 | ||
f7fa9b10 PM |
1003 | if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups || |
1004 | !test_bit(p->group - 1, nlk->groups)) | |
1da177e4 LT |
1005 | goto out; |
1006 | ||
878628fb | 1007 | if (!net_eq(sock_net(sk), p->net)) |
b4b51029 EB |
1008 | goto out; |
1009 | ||
1da177e4 LT |
1010 | if (p->failure) { |
1011 | netlink_overrun(sk); | |
1012 | goto out; | |
1013 | } | |
1014 | ||
1015 | sock_hold(sk); | |
1016 | if (p->skb2 == NULL) { | |
68acc024 | 1017 | if (skb_shared(p->skb)) { |
1da177e4 LT |
1018 | p->skb2 = skb_clone(p->skb, p->allocation); |
1019 | } else { | |
68acc024 TC |
1020 | p->skb2 = skb_get(p->skb); |
1021 | /* | |
1022 | * skb ownership may have been set when | |
1023 | * delivered to a previous socket. | |
1024 | */ | |
1025 | skb_orphan(p->skb2); | |
1da177e4 LT |
1026 | } |
1027 | } | |
1028 | if (p->skb2 == NULL) { | |
1029 | netlink_overrun(sk); | |
1030 | /* Clone failed. Notify ALL listeners. */ | |
1031 | p->failure = 1; | |
be0c22a4 PNA |
1032 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
1033 | p->delivery_failure = 1; | |
910a7e90 EB |
1034 | } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) { |
1035 | kfree_skb(p->skb2); | |
1036 | p->skb2 = NULL; | |
b1153f29 SH |
1037 | } else if (sk_filter(sk, p->skb2)) { |
1038 | kfree_skb(p->skb2); | |
1039 | p->skb2 = NULL; | |
1da177e4 LT |
1040 | } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) { |
1041 | netlink_overrun(sk); | |
be0c22a4 PNA |
1042 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
1043 | p->delivery_failure = 1; | |
1da177e4 LT |
1044 | } else { |
1045 | p->congested |= val; | |
1046 | p->delivered = 1; | |
1047 | p->skb2 = NULL; | |
1048 | } | |
1049 | sock_put(sk); | |
1050 | ||
1051 | out: | |
1052 | return 0; | |
1053 | } | |
1054 | ||
910a7e90 EB |
1055 | int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 pid, |
1056 | u32 group, gfp_t allocation, | |
1057 | int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data), | |
1058 | void *filter_data) | |
1da177e4 | 1059 | { |
3b1e0a65 | 1060 | struct net *net = sock_net(ssk); |
1da177e4 LT |
1061 | struct netlink_broadcast_data info; |
1062 | struct hlist_node *node; | |
1063 | struct sock *sk; | |
1064 | ||
1065 | skb = netlink_trim(skb, allocation); | |
1066 | ||
1067 | info.exclude_sk = ssk; | |
b4b51029 | 1068 | info.net = net; |
1da177e4 LT |
1069 | info.pid = pid; |
1070 | info.group = group; | |
1071 | info.failure = 0; | |
ff491a73 | 1072 | info.delivery_failure = 0; |
1da177e4 LT |
1073 | info.congested = 0; |
1074 | info.delivered = 0; | |
1075 | info.allocation = allocation; | |
1076 | info.skb = skb; | |
1077 | info.skb2 = NULL; | |
910a7e90 EB |
1078 | info.tx_filter = filter; |
1079 | info.tx_data = filter_data; | |
1da177e4 LT |
1080 | |
1081 | /* While we sleep in clone, do not allow to change socket list */ | |
1082 | ||
1083 | netlink_lock_table(); | |
1084 | ||
1085 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) | |
1086 | do_one_broadcast(sk, &info); | |
1087 | ||
70d4bf6d | 1088 | consume_skb(skb); |
aa1c6a6f | 1089 | |
1da177e4 LT |
1090 | netlink_unlock_table(); |
1091 | ||
70d4bf6d NH |
1092 | if (info.delivery_failure) { |
1093 | kfree_skb(info.skb2); | |
ff491a73 | 1094 | return -ENOBUFS; |
658cb354 ED |
1095 | } |
1096 | consume_skb(info.skb2); | |
ff491a73 | 1097 | |
1da177e4 LT |
1098 | if (info.delivered) { |
1099 | if (info.congested && (allocation & __GFP_WAIT)) | |
1100 | yield(); | |
1101 | return 0; | |
1102 | } | |
1da177e4 LT |
1103 | return -ESRCH; |
1104 | } | |
910a7e90 EB |
1105 | EXPORT_SYMBOL(netlink_broadcast_filtered); |
1106 | ||
1107 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid, | |
1108 | u32 group, gfp_t allocation) | |
1109 | { | |
1110 | return netlink_broadcast_filtered(ssk, skb, pid, group, allocation, | |
1111 | NULL, NULL); | |
1112 | } | |
6ac552fd | 1113 | EXPORT_SYMBOL(netlink_broadcast); |
1da177e4 LT |
1114 | |
1115 | struct netlink_set_err_data { | |
1116 | struct sock *exclude_sk; | |
1117 | u32 pid; | |
1118 | u32 group; | |
1119 | int code; | |
1120 | }; | |
1121 | ||
b57ef81f | 1122 | static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p) |
1da177e4 LT |
1123 | { |
1124 | struct netlink_sock *nlk = nlk_sk(sk); | |
1a50307b | 1125 | int ret = 0; |
1da177e4 LT |
1126 | |
1127 | if (sk == p->exclude_sk) | |
1128 | goto out; | |
1129 | ||
09ad9bc7 | 1130 | if (!net_eq(sock_net(sk), sock_net(p->exclude_sk))) |
b4b51029 EB |
1131 | goto out; |
1132 | ||
f7fa9b10 PM |
1133 | if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups || |
1134 | !test_bit(p->group - 1, nlk->groups)) | |
1da177e4 LT |
1135 | goto out; |
1136 | ||
1a50307b PNA |
1137 | if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) { |
1138 | ret = 1; | |
1139 | goto out; | |
1140 | } | |
1141 | ||
1da177e4 LT |
1142 | sk->sk_err = p->code; |
1143 | sk->sk_error_report(sk); | |
1144 | out: | |
1a50307b | 1145 | return ret; |
1da177e4 LT |
1146 | } |
1147 | ||
4843b93c PNA |
1148 | /** |
1149 | * netlink_set_err - report error to broadcast listeners | |
1150 | * @ssk: the kernel netlink socket, as returned by netlink_kernel_create() | |
1151 | * @pid: the PID of a process that we want to skip (if any) | |
1152 | * @groups: the broadcast group that will notice the error | |
1153 | * @code: error code, must be negative (as usual in kernelspace) | |
1a50307b PNA |
1154 | * |
1155 | * This function returns the number of broadcast listeners that have set the | |
1156 | * NETLINK_RECV_NO_ENOBUFS socket option. | |
4843b93c | 1157 | */ |
1a50307b | 1158 | int netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code) |
1da177e4 LT |
1159 | { |
1160 | struct netlink_set_err_data info; | |
1161 | struct hlist_node *node; | |
1162 | struct sock *sk; | |
1a50307b | 1163 | int ret = 0; |
1da177e4 LT |
1164 | |
1165 | info.exclude_sk = ssk; | |
1166 | info.pid = pid; | |
1167 | info.group = group; | |
4843b93c PNA |
1168 | /* sk->sk_err wants a positive error value */ |
1169 | info.code = -code; | |
1da177e4 LT |
1170 | |
1171 | read_lock(&nl_table_lock); | |
1172 | ||
1173 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) | |
1a50307b | 1174 | ret += do_one_set_err(sk, &info); |
1da177e4 LT |
1175 | |
1176 | read_unlock(&nl_table_lock); | |
1a50307b | 1177 | return ret; |
1da177e4 | 1178 | } |
dd5b6ce6 | 1179 | EXPORT_SYMBOL(netlink_set_err); |
1da177e4 | 1180 | |
84659eb5 JB |
1181 | /* must be called with netlink table grabbed */ |
1182 | static void netlink_update_socket_mc(struct netlink_sock *nlk, | |
1183 | unsigned int group, | |
1184 | int is_new) | |
1185 | { | |
1186 | int old, new = !!is_new, subscriptions; | |
1187 | ||
1188 | old = test_bit(group - 1, nlk->groups); | |
1189 | subscriptions = nlk->subscriptions - old + new; | |
1190 | if (new) | |
1191 | __set_bit(group - 1, nlk->groups); | |
1192 | else | |
1193 | __clear_bit(group - 1, nlk->groups); | |
1194 | netlink_update_subscriptions(&nlk->sk, subscriptions); | |
1195 | netlink_update_listeners(&nlk->sk); | |
1196 | } | |
1197 | ||
9a4595bc | 1198 | static int netlink_setsockopt(struct socket *sock, int level, int optname, |
b7058842 | 1199 | char __user *optval, unsigned int optlen) |
9a4595bc PM |
1200 | { |
1201 | struct sock *sk = sock->sk; | |
1202 | struct netlink_sock *nlk = nlk_sk(sk); | |
eb496534 JB |
1203 | unsigned int val = 0; |
1204 | int err; | |
9a4595bc PM |
1205 | |
1206 | if (level != SOL_NETLINK) | |
1207 | return -ENOPROTOOPT; | |
1208 | ||
1209 | if (optlen >= sizeof(int) && | |
eb496534 | 1210 | get_user(val, (unsigned int __user *)optval)) |
9a4595bc PM |
1211 | return -EFAULT; |
1212 | ||
1213 | switch (optname) { | |
1214 | case NETLINK_PKTINFO: | |
1215 | if (val) | |
1216 | nlk->flags |= NETLINK_RECV_PKTINFO; | |
1217 | else | |
1218 | nlk->flags &= ~NETLINK_RECV_PKTINFO; | |
1219 | err = 0; | |
1220 | break; | |
1221 | case NETLINK_ADD_MEMBERSHIP: | |
1222 | case NETLINK_DROP_MEMBERSHIP: { | |
9a4595bc PM |
1223 | if (!netlink_capable(sock, NL_NONROOT_RECV)) |
1224 | return -EPERM; | |
b4ff4f04 JB |
1225 | err = netlink_realloc_groups(sk); |
1226 | if (err) | |
1227 | return err; | |
9a4595bc PM |
1228 | if (!val || val - 1 >= nlk->ngroups) |
1229 | return -EINVAL; | |
1230 | netlink_table_grab(); | |
84659eb5 JB |
1231 | netlink_update_socket_mc(nlk, val, |
1232 | optname == NETLINK_ADD_MEMBERSHIP); | |
9a4595bc PM |
1233 | netlink_table_ungrab(); |
1234 | err = 0; | |
1235 | break; | |
1236 | } | |
be0c22a4 PNA |
1237 | case NETLINK_BROADCAST_ERROR: |
1238 | if (val) | |
1239 | nlk->flags |= NETLINK_BROADCAST_SEND_ERROR; | |
1240 | else | |
1241 | nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR; | |
1242 | err = 0; | |
1243 | break; | |
38938bfe PNA |
1244 | case NETLINK_NO_ENOBUFS: |
1245 | if (val) { | |
1246 | nlk->flags |= NETLINK_RECV_NO_ENOBUFS; | |
1247 | clear_bit(0, &nlk->state); | |
1248 | wake_up_interruptible(&nlk->wait); | |
658cb354 | 1249 | } else { |
38938bfe | 1250 | nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS; |
658cb354 | 1251 | } |
38938bfe PNA |
1252 | err = 0; |
1253 | break; | |
9a4595bc PM |
1254 | default: |
1255 | err = -ENOPROTOOPT; | |
1256 | } | |
1257 | return err; | |
1258 | } | |
1259 | ||
1260 | static int netlink_getsockopt(struct socket *sock, int level, int optname, | |
746fac4d | 1261 | char __user *optval, int __user *optlen) |
9a4595bc PM |
1262 | { |
1263 | struct sock *sk = sock->sk; | |
1264 | struct netlink_sock *nlk = nlk_sk(sk); | |
1265 | int len, val, err; | |
1266 | ||
1267 | if (level != SOL_NETLINK) | |
1268 | return -ENOPROTOOPT; | |
1269 | ||
1270 | if (get_user(len, optlen)) | |
1271 | return -EFAULT; | |
1272 | if (len < 0) | |
1273 | return -EINVAL; | |
1274 | ||
1275 | switch (optname) { | |
1276 | case NETLINK_PKTINFO: | |
1277 | if (len < sizeof(int)) | |
1278 | return -EINVAL; | |
1279 | len = sizeof(int); | |
1280 | val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0; | |
a27b58fe HC |
1281 | if (put_user(len, optlen) || |
1282 | put_user(val, optval)) | |
1283 | return -EFAULT; | |
9a4595bc PM |
1284 | err = 0; |
1285 | break; | |
be0c22a4 PNA |
1286 | case NETLINK_BROADCAST_ERROR: |
1287 | if (len < sizeof(int)) | |
1288 | return -EINVAL; | |
1289 | len = sizeof(int); | |
1290 | val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0; | |
1291 | if (put_user(len, optlen) || | |
1292 | put_user(val, optval)) | |
1293 | return -EFAULT; | |
1294 | err = 0; | |
1295 | break; | |
38938bfe PNA |
1296 | case NETLINK_NO_ENOBUFS: |
1297 | if (len < sizeof(int)) | |
1298 | return -EINVAL; | |
1299 | len = sizeof(int); | |
1300 | val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0; | |
1301 | if (put_user(len, optlen) || | |
1302 | put_user(val, optval)) | |
1303 | return -EFAULT; | |
1304 | err = 0; | |
1305 | break; | |
9a4595bc PM |
1306 | default: |
1307 | err = -ENOPROTOOPT; | |
1308 | } | |
1309 | return err; | |
1310 | } | |
1311 | ||
1312 | static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
1313 | { | |
1314 | struct nl_pktinfo info; | |
1315 | ||
1316 | info.group = NETLINK_CB(skb).dst_group; | |
1317 | put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); | |
1318 | } | |
1319 | ||
1da177e4 LT |
1320 | static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, |
1321 | struct msghdr *msg, size_t len) | |
1322 | { | |
1323 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | |
1324 | struct sock *sk = sock->sk; | |
1325 | struct netlink_sock *nlk = nlk_sk(sk); | |
6ac552fd | 1326 | struct sockaddr_nl *addr = msg->msg_name; |
1da177e4 | 1327 | u32 dst_pid; |
d629b836 | 1328 | u32 dst_group; |
1da177e4 LT |
1329 | struct sk_buff *skb; |
1330 | int err; | |
1331 | struct scm_cookie scm; | |
1332 | ||
1333 | if (msg->msg_flags&MSG_OOB) | |
1334 | return -EOPNOTSUPP; | |
1335 | ||
16e57262 | 1336 | if (NULL == siocb->scm) |
1da177e4 | 1337 | siocb->scm = &scm; |
16e57262 | 1338 | |
1da177e4 LT |
1339 | err = scm_send(sock, msg, siocb->scm); |
1340 | if (err < 0) | |
1341 | return err; | |
1342 | ||
1343 | if (msg->msg_namelen) { | |
b47030c7 | 1344 | err = -EINVAL; |
1da177e4 | 1345 | if (addr->nl_family != AF_NETLINK) |
b47030c7 | 1346 | goto out; |
1da177e4 | 1347 | dst_pid = addr->nl_pid; |
d629b836 | 1348 | dst_group = ffs(addr->nl_groups); |
b47030c7 | 1349 | err = -EPERM; |
d629b836 | 1350 | if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND)) |
b47030c7 | 1351 | goto out; |
1da177e4 LT |
1352 | } else { |
1353 | dst_pid = nlk->dst_pid; | |
d629b836 | 1354 | dst_group = nlk->dst_group; |
1da177e4 LT |
1355 | } |
1356 | ||
1357 | if (!nlk->pid) { | |
1358 | err = netlink_autobind(sock); | |
1359 | if (err) | |
1360 | goto out; | |
1361 | } | |
1362 | ||
1363 | err = -EMSGSIZE; | |
1364 | if (len > sk->sk_sndbuf - 32) | |
1365 | goto out; | |
1366 | err = -ENOBUFS; | |
339bf98f | 1367 | skb = alloc_skb(len, GFP_KERNEL); |
6ac552fd | 1368 | if (skb == NULL) |
1da177e4 LT |
1369 | goto out; |
1370 | ||
1371 | NETLINK_CB(skb).pid = nlk->pid; | |
d629b836 | 1372 | NETLINK_CB(skb).dst_group = dst_group; |
1da177e4 LT |
1373 | memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred)); |
1374 | ||
1da177e4 | 1375 | err = -EFAULT; |
6ac552fd | 1376 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
1da177e4 LT |
1377 | kfree_skb(skb); |
1378 | goto out; | |
1379 | } | |
1380 | ||
1381 | err = security_netlink_send(sk, skb); | |
1382 | if (err) { | |
1383 | kfree_skb(skb); | |
1384 | goto out; | |
1385 | } | |
1386 | ||
d629b836 | 1387 | if (dst_group) { |
1da177e4 | 1388 | atomic_inc(&skb->users); |
d629b836 | 1389 | netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL); |
1da177e4 LT |
1390 | } |
1391 | err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT); | |
1392 | ||
1393 | out: | |
b47030c7 | 1394 | scm_destroy(siocb->scm); |
1da177e4 LT |
1395 | return err; |
1396 | } | |
1397 | ||
1398 | static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, | |
1399 | struct msghdr *msg, size_t len, | |
1400 | int flags) | |
1401 | { | |
1402 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | |
1403 | struct scm_cookie scm; | |
1404 | struct sock *sk = sock->sk; | |
1405 | struct netlink_sock *nlk = nlk_sk(sk); | |
1406 | int noblock = flags&MSG_DONTWAIT; | |
1407 | size_t copied; | |
68d6ac6d | 1408 | struct sk_buff *skb, *data_skb; |
b44d211e | 1409 | int err, ret; |
1da177e4 LT |
1410 | |
1411 | if (flags&MSG_OOB) | |
1412 | return -EOPNOTSUPP; | |
1413 | ||
1414 | copied = 0; | |
1415 | ||
6ac552fd PM |
1416 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
1417 | if (skb == NULL) | |
1da177e4 LT |
1418 | goto out; |
1419 | ||
68d6ac6d JB |
1420 | data_skb = skb; |
1421 | ||
1dacc76d JB |
1422 | #ifdef CONFIG_COMPAT_NETLINK_MESSAGES |
1423 | if (unlikely(skb_shinfo(skb)->frag_list)) { | |
1dacc76d | 1424 | /* |
68d6ac6d JB |
1425 | * If this skb has a frag_list, then here that means that we |
1426 | * will have to use the frag_list skb's data for compat tasks | |
1427 | * and the regular skb's data for normal (non-compat) tasks. | |
1dacc76d | 1428 | * |
68d6ac6d JB |
1429 | * If we need to send the compat skb, assign it to the |
1430 | * 'data_skb' variable so that it will be used below for data | |
1431 | * copying. We keep 'skb' for everything else, including | |
1432 | * freeing both later. | |
1dacc76d | 1433 | */ |
68d6ac6d JB |
1434 | if (flags & MSG_CMSG_COMPAT) |
1435 | data_skb = skb_shinfo(skb)->frag_list; | |
1dacc76d JB |
1436 | } |
1437 | #endif | |
1438 | ||
1da177e4 LT |
1439 | msg->msg_namelen = 0; |
1440 | ||
68d6ac6d | 1441 | copied = data_skb->len; |
1da177e4 LT |
1442 | if (len < copied) { |
1443 | msg->msg_flags |= MSG_TRUNC; | |
1444 | copied = len; | |
1445 | } | |
1446 | ||
68d6ac6d JB |
1447 | skb_reset_transport_header(data_skb); |
1448 | err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied); | |
1da177e4 LT |
1449 | |
1450 | if (msg->msg_name) { | |
6ac552fd | 1451 | struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name; |
1da177e4 LT |
1452 | addr->nl_family = AF_NETLINK; |
1453 | addr->nl_pad = 0; | |
1454 | addr->nl_pid = NETLINK_CB(skb).pid; | |
d629b836 | 1455 | addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); |
1da177e4 LT |
1456 | msg->msg_namelen = sizeof(*addr); |
1457 | } | |
1458 | ||
cc9a06cd PM |
1459 | if (nlk->flags & NETLINK_RECV_PKTINFO) |
1460 | netlink_cmsg_recv_pktinfo(msg, skb); | |
1461 | ||
1da177e4 LT |
1462 | if (NULL == siocb->scm) { |
1463 | memset(&scm, 0, sizeof(scm)); | |
1464 | siocb->scm = &scm; | |
1465 | } | |
1466 | siocb->scm->creds = *NETLINK_CREDS(skb); | |
188ccb55 | 1467 | if (flags & MSG_TRUNC) |
68d6ac6d | 1468 | copied = data_skb->len; |
daa3766e | 1469 | |
1da177e4 LT |
1470 | skb_free_datagram(sk, skb); |
1471 | ||
b44d211e AV |
1472 | if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) { |
1473 | ret = netlink_dump(sk); | |
1474 | if (ret) { | |
1475 | sk->sk_err = ret; | |
1476 | sk->sk_error_report(sk); | |
1477 | } | |
1478 | } | |
1da177e4 LT |
1479 | |
1480 | scm_recv(sock, msg, siocb->scm, flags); | |
1da177e4 LT |
1481 | out: |
1482 | netlink_rcv_wake(sk); | |
1483 | return err ? : copied; | |
1484 | } | |
1485 | ||
1486 | static void netlink_data_ready(struct sock *sk, int len) | |
1487 | { | |
cd40b7d3 | 1488 | BUG(); |
1da177e4 LT |
1489 | } |
1490 | ||
1491 | /* | |
746fac4d | 1492 | * We export these functions to other modules. They provide a |
1da177e4 LT |
1493 | * complete set of kernel non-blocking support for message |
1494 | * queueing. | |
1495 | */ | |
1496 | ||
1497 | struct sock * | |
b4b51029 | 1498 | netlink_kernel_create(struct net *net, int unit, unsigned int groups, |
cd40b7d3 | 1499 | void (*input)(struct sk_buff *skb), |
af65bdfc | 1500 | struct mutex *cb_mutex, struct module *module) |
1da177e4 LT |
1501 | { |
1502 | struct socket *sock; | |
1503 | struct sock *sk; | |
77247bbb | 1504 | struct netlink_sock *nlk; |
5c398dc8 | 1505 | struct listeners *listeners = NULL; |
1da177e4 | 1506 | |
fab2caf6 | 1507 | BUG_ON(!nl_table); |
1da177e4 | 1508 | |
6ac552fd | 1509 | if (unit < 0 || unit >= MAX_LINKS) |
1da177e4 LT |
1510 | return NULL; |
1511 | ||
1512 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) | |
1513 | return NULL; | |
1514 | ||
23fe1866 PE |
1515 | /* |
1516 | * We have to just have a reference on the net from sk, but don't | |
1517 | * get_net it. Besides, we cannot get and then put the net here. | |
1518 | * So we create one inside init_net and the move it to net. | |
1519 | */ | |
1520 | ||
1521 | if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0) | |
1522 | goto out_sock_release_nosk; | |
1523 | ||
1524 | sk = sock->sk; | |
edf02087 | 1525 | sk_change_net(sk, net); |
4fdb3bb7 | 1526 | |
4277a083 PM |
1527 | if (groups < 32) |
1528 | groups = 32; | |
1529 | ||
5c398dc8 | 1530 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
4277a083 PM |
1531 | if (!listeners) |
1532 | goto out_sock_release; | |
1533 | ||
1da177e4 LT |
1534 | sk->sk_data_ready = netlink_data_ready; |
1535 | if (input) | |
cd40b7d3 | 1536 | nlk_sk(sk)->netlink_rcv = input; |
1da177e4 | 1537 | |
b4b51029 | 1538 | if (netlink_insert(sk, net, 0)) |
77247bbb | 1539 | goto out_sock_release; |
4fdb3bb7 | 1540 | |
77247bbb PM |
1541 | nlk = nlk_sk(sk); |
1542 | nlk->flags |= NETLINK_KERNEL_SOCKET; | |
4fdb3bb7 | 1543 | |
4fdb3bb7 | 1544 | netlink_table_grab(); |
b4b51029 EB |
1545 | if (!nl_table[unit].registered) { |
1546 | nl_table[unit].groups = groups; | |
5c398dc8 | 1547 | rcu_assign_pointer(nl_table[unit].listeners, listeners); |
b4b51029 EB |
1548 | nl_table[unit].cb_mutex = cb_mutex; |
1549 | nl_table[unit].module = module; | |
1550 | nl_table[unit].registered = 1; | |
f937f1f4 JJ |
1551 | } else { |
1552 | kfree(listeners); | |
869e58f8 | 1553 | nl_table[unit].registered++; |
b4b51029 | 1554 | } |
4fdb3bb7 | 1555 | netlink_table_ungrab(); |
77247bbb PM |
1556 | return sk; |
1557 | ||
4fdb3bb7 | 1558 | out_sock_release: |
4277a083 | 1559 | kfree(listeners); |
9dfbec1f | 1560 | netlink_kernel_release(sk); |
23fe1866 PE |
1561 | return NULL; |
1562 | ||
1563 | out_sock_release_nosk: | |
4fdb3bb7 | 1564 | sock_release(sock); |
77247bbb | 1565 | return NULL; |
1da177e4 | 1566 | } |
6ac552fd | 1567 | EXPORT_SYMBOL(netlink_kernel_create); |
1da177e4 | 1568 | |
b7c6ba6e DL |
1569 | |
1570 | void | |
1571 | netlink_kernel_release(struct sock *sk) | |
1572 | { | |
edf02087 | 1573 | sk_release_kernel(sk); |
b7c6ba6e DL |
1574 | } |
1575 | EXPORT_SYMBOL(netlink_kernel_release); | |
1576 | ||
d136f1bd | 1577 | int __netlink_change_ngroups(struct sock *sk, unsigned int groups) |
b4ff4f04 | 1578 | { |
5c398dc8 | 1579 | struct listeners *new, *old; |
b4ff4f04 | 1580 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
b4ff4f04 JB |
1581 | |
1582 | if (groups < 32) | |
1583 | groups = 32; | |
1584 | ||
b4ff4f04 | 1585 | if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) { |
5c398dc8 ED |
1586 | new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC); |
1587 | if (!new) | |
d136f1bd | 1588 | return -ENOMEM; |
33d480ce | 1589 | old = rcu_dereference_protected(tbl->listeners, 1); |
5c398dc8 ED |
1590 | memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups)); |
1591 | rcu_assign_pointer(tbl->listeners, new); | |
1592 | ||
37b6b935 | 1593 | kfree_rcu(old, rcu); |
b4ff4f04 JB |
1594 | } |
1595 | tbl->groups = groups; | |
1596 | ||
d136f1bd JB |
1597 | return 0; |
1598 | } | |
1599 | ||
1600 | /** | |
1601 | * netlink_change_ngroups - change number of multicast groups | |
1602 | * | |
1603 | * This changes the number of multicast groups that are available | |
1604 | * on a certain netlink family. Note that it is not possible to | |
1605 | * change the number of groups to below 32. Also note that it does | |
1606 | * not implicitly call netlink_clear_multicast_users() when the | |
1607 | * number of groups is reduced. | |
1608 | * | |
1609 | * @sk: The kernel netlink socket, as returned by netlink_kernel_create(). | |
1610 | * @groups: The new number of groups. | |
1611 | */ | |
1612 | int netlink_change_ngroups(struct sock *sk, unsigned int groups) | |
1613 | { | |
1614 | int err; | |
1615 | ||
1616 | netlink_table_grab(); | |
1617 | err = __netlink_change_ngroups(sk, groups); | |
b4ff4f04 | 1618 | netlink_table_ungrab(); |
d136f1bd | 1619 | |
b4ff4f04 JB |
1620 | return err; |
1621 | } | |
b4ff4f04 | 1622 | |
b8273570 JB |
1623 | void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group) |
1624 | { | |
1625 | struct sock *sk; | |
1626 | struct hlist_node *node; | |
1627 | struct netlink_table *tbl = &nl_table[ksk->sk_protocol]; | |
1628 | ||
1629 | sk_for_each_bound(sk, node, &tbl->mc_list) | |
1630 | netlink_update_socket_mc(nlk_sk(sk), group, 0); | |
1631 | } | |
1632 | ||
84659eb5 JB |
1633 | /** |
1634 | * netlink_clear_multicast_users - kick off multicast listeners | |
1635 | * | |
1636 | * This function removes all listeners from the given group. | |
1637 | * @ksk: The kernel netlink socket, as returned by | |
1638 | * netlink_kernel_create(). | |
1639 | * @group: The multicast group to clear. | |
1640 | */ | |
1641 | void netlink_clear_multicast_users(struct sock *ksk, unsigned int group) | |
1642 | { | |
84659eb5 | 1643 | netlink_table_grab(); |
b8273570 | 1644 | __netlink_clear_multicast_users(ksk, group); |
84659eb5 JB |
1645 | netlink_table_ungrab(); |
1646 | } | |
84659eb5 | 1647 | |
1da177e4 | 1648 | void netlink_set_nonroot(int protocol, unsigned int flags) |
746fac4d YH |
1649 | { |
1650 | if ((unsigned int)protocol < MAX_LINKS) | |
1da177e4 | 1651 | nl_table[protocol].nl_nonroot = flags; |
746fac4d | 1652 | } |
6ac552fd | 1653 | EXPORT_SYMBOL(netlink_set_nonroot); |
1da177e4 | 1654 | |
a46621a3 DV |
1655 | struct nlmsghdr * |
1656 | __nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, int type, int len, int flags) | |
1657 | { | |
1658 | struct nlmsghdr *nlh; | |
1659 | int size = NLMSG_LENGTH(len); | |
1660 | ||
1661 | nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size)); | |
1662 | nlh->nlmsg_type = type; | |
1663 | nlh->nlmsg_len = size; | |
1664 | nlh->nlmsg_flags = flags; | |
1665 | nlh->nlmsg_pid = pid; | |
1666 | nlh->nlmsg_seq = seq; | |
1667 | if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0) | |
1668 | memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size); | |
1669 | return nlh; | |
1670 | } | |
1671 | EXPORT_SYMBOL(__nlmsg_put); | |
1672 | ||
1da177e4 LT |
1673 | /* |
1674 | * It looks a bit ugly. | |
1675 | * It would be better to create kernel thread. | |
1676 | */ | |
1677 | ||
1678 | static int netlink_dump(struct sock *sk) | |
1679 | { | |
1680 | struct netlink_sock *nlk = nlk_sk(sk); | |
1681 | struct netlink_callback *cb; | |
c7ac8679 | 1682 | struct sk_buff *skb = NULL; |
1da177e4 | 1683 | struct nlmsghdr *nlh; |
bf8b79e4 | 1684 | int len, err = -ENOBUFS; |
c7ac8679 | 1685 | int alloc_size; |
1da177e4 | 1686 | |
af65bdfc | 1687 | mutex_lock(nlk->cb_mutex); |
1da177e4 LT |
1688 | |
1689 | cb = nlk->cb; | |
1690 | if (cb == NULL) { | |
bf8b79e4 TG |
1691 | err = -EINVAL; |
1692 | goto errout_skb; | |
1da177e4 LT |
1693 | } |
1694 | ||
c7ac8679 GR |
1695 | alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); |
1696 | ||
1697 | skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL); | |
1698 | if (!skb) | |
c63d6ea3 | 1699 | goto errout_skb; |
c7ac8679 | 1700 | |
1da177e4 LT |
1701 | len = cb->dump(skb, cb); |
1702 | ||
1703 | if (len > 0) { | |
af65bdfc | 1704 | mutex_unlock(nlk->cb_mutex); |
b1153f29 SH |
1705 | |
1706 | if (sk_filter(sk, skb)) | |
1707 | kfree_skb(skb); | |
4a7e7c2a ED |
1708 | else |
1709 | __netlink_sendskb(sk, skb); | |
1da177e4 LT |
1710 | return 0; |
1711 | } | |
1712 | ||
bf8b79e4 TG |
1713 | nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI); |
1714 | if (!nlh) | |
1715 | goto errout_skb; | |
1716 | ||
670dc283 JB |
1717 | nl_dump_check_consistent(cb, nlh); |
1718 | ||
bf8b79e4 TG |
1719 | memcpy(nlmsg_data(nlh), &len, sizeof(len)); |
1720 | ||
b1153f29 SH |
1721 | if (sk_filter(sk, skb)) |
1722 | kfree_skb(skb); | |
4a7e7c2a ED |
1723 | else |
1724 | __netlink_sendskb(sk, skb); | |
1da177e4 | 1725 | |
a8f74b22 TG |
1726 | if (cb->done) |
1727 | cb->done(cb); | |
1da177e4 | 1728 | nlk->cb = NULL; |
af65bdfc | 1729 | mutex_unlock(nlk->cb_mutex); |
1da177e4 LT |
1730 | |
1731 | netlink_destroy_callback(cb); | |
1da177e4 | 1732 | return 0; |
1797754e | 1733 | |
bf8b79e4 | 1734 | errout_skb: |
af65bdfc | 1735 | mutex_unlock(nlk->cb_mutex); |
bf8b79e4 | 1736 | kfree_skb(skb); |
bf8b79e4 | 1737 | return err; |
1da177e4 LT |
1738 | } |
1739 | ||
1740 | int netlink_dump_start(struct sock *ssk, struct sk_buff *skb, | |
3a6c2b41 | 1741 | const struct nlmsghdr *nlh, |
80d326fa | 1742 | struct netlink_dump_control *control) |
1da177e4 LT |
1743 | { |
1744 | struct netlink_callback *cb; | |
1745 | struct sock *sk; | |
1746 | struct netlink_sock *nlk; | |
b44d211e | 1747 | int ret; |
1da177e4 | 1748 | |
0da974f4 | 1749 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
1da177e4 LT |
1750 | if (cb == NULL) |
1751 | return -ENOBUFS; | |
1752 | ||
80d326fa PNA |
1753 | cb->dump = control->dump; |
1754 | cb->done = control->done; | |
1da177e4 | 1755 | cb->nlh = nlh; |
7175c883 | 1756 | cb->data = control->data; |
80d326fa | 1757 | cb->min_dump_alloc = control->min_dump_alloc; |
1da177e4 LT |
1758 | atomic_inc(&skb->users); |
1759 | cb->skb = skb; | |
1760 | ||
3b1e0a65 | 1761 | sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid); |
1da177e4 LT |
1762 | if (sk == NULL) { |
1763 | netlink_destroy_callback(cb); | |
1764 | return -ECONNREFUSED; | |
1765 | } | |
1766 | nlk = nlk_sk(sk); | |
3f660d66 | 1767 | /* A dump is in progress... */ |
af65bdfc | 1768 | mutex_lock(nlk->cb_mutex); |
3f660d66 | 1769 | if (nlk->cb) { |
af65bdfc | 1770 | mutex_unlock(nlk->cb_mutex); |
1da177e4 LT |
1771 | netlink_destroy_callback(cb); |
1772 | sock_put(sk); | |
1773 | return -EBUSY; | |
1774 | } | |
1775 | nlk->cb = cb; | |
af65bdfc | 1776 | mutex_unlock(nlk->cb_mutex); |
1da177e4 | 1777 | |
b44d211e AV |
1778 | ret = netlink_dump(sk); |
1779 | ||
1da177e4 | 1780 | sock_put(sk); |
5c58298c | 1781 | |
b44d211e AV |
1782 | if (ret) |
1783 | return ret; | |
1784 | ||
5c58298c DL |
1785 | /* We successfully started a dump, by returning -EINTR we |
1786 | * signal not to send ACK even if it was requested. | |
1787 | */ | |
1788 | return -EINTR; | |
1da177e4 | 1789 | } |
6ac552fd | 1790 | EXPORT_SYMBOL(netlink_dump_start); |
1da177e4 LT |
1791 | |
1792 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err) | |
1793 | { | |
1794 | struct sk_buff *skb; | |
1795 | struct nlmsghdr *rep; | |
1796 | struct nlmsgerr *errmsg; | |
339bf98f | 1797 | size_t payload = sizeof(*errmsg); |
1da177e4 | 1798 | |
339bf98f TG |
1799 | /* error messages get the original request appened */ |
1800 | if (err) | |
1801 | payload += nlmsg_len(nlh); | |
1da177e4 | 1802 | |
339bf98f | 1803 | skb = nlmsg_new(payload, GFP_KERNEL); |
1da177e4 LT |
1804 | if (!skb) { |
1805 | struct sock *sk; | |
1806 | ||
3b1e0a65 | 1807 | sk = netlink_lookup(sock_net(in_skb->sk), |
b4b51029 | 1808 | in_skb->sk->sk_protocol, |
1da177e4 LT |
1809 | NETLINK_CB(in_skb).pid); |
1810 | if (sk) { | |
1811 | sk->sk_err = ENOBUFS; | |
1812 | sk->sk_error_report(sk); | |
1813 | sock_put(sk); | |
1814 | } | |
1815 | return; | |
1816 | } | |
1817 | ||
1818 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, | |
5dba93ae | 1819 | NLMSG_ERROR, payload, 0); |
bf8b79e4 | 1820 | errmsg = nlmsg_data(rep); |
1da177e4 | 1821 | errmsg->error = err; |
bf8b79e4 | 1822 | memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh)); |
1da177e4 LT |
1823 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); |
1824 | } | |
6ac552fd | 1825 | EXPORT_SYMBOL(netlink_ack); |
1da177e4 | 1826 | |
cd40b7d3 | 1827 | int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, |
1d00a4eb | 1828 | struct nlmsghdr *)) |
82ace47a | 1829 | { |
82ace47a TG |
1830 | struct nlmsghdr *nlh; |
1831 | int err; | |
1832 | ||
1833 | while (skb->len >= nlmsg_total_size(0)) { | |
cd40b7d3 DL |
1834 | int msglen; |
1835 | ||
b529ccf2 | 1836 | nlh = nlmsg_hdr(skb); |
d35b6856 | 1837 | err = 0; |
82ace47a | 1838 | |
ad8e4b75 | 1839 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) |
82ace47a TG |
1840 | return 0; |
1841 | ||
d35b6856 TG |
1842 | /* Only requests are handled by the kernel */ |
1843 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) | |
5c58298c | 1844 | goto ack; |
45e7ae7f TG |
1845 | |
1846 | /* Skip control messages */ | |
1847 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) | |
5c58298c | 1848 | goto ack; |
d35b6856 | 1849 | |
1d00a4eb | 1850 | err = cb(skb, nlh); |
5c58298c DL |
1851 | if (err == -EINTR) |
1852 | goto skip; | |
1853 | ||
1854 | ack: | |
d35b6856 | 1855 | if (nlh->nlmsg_flags & NLM_F_ACK || err) |
82ace47a | 1856 | netlink_ack(skb, nlh, err); |
82ace47a | 1857 | |
5c58298c | 1858 | skip: |
6ac552fd | 1859 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
cd40b7d3 DL |
1860 | if (msglen > skb->len) |
1861 | msglen = skb->len; | |
1862 | skb_pull(skb, msglen); | |
82ace47a TG |
1863 | } |
1864 | ||
1865 | return 0; | |
1866 | } | |
6ac552fd | 1867 | EXPORT_SYMBOL(netlink_rcv_skb); |
82ace47a | 1868 | |
d387f6ad TG |
1869 | /** |
1870 | * nlmsg_notify - send a notification netlink message | |
1871 | * @sk: netlink socket to use | |
1872 | * @skb: notification message | |
1873 | * @pid: destination netlink pid for reports or 0 | |
1874 | * @group: destination multicast group or 0 | |
1875 | * @report: 1 to report back, 0 to disable | |
1876 | * @flags: allocation flags | |
1877 | */ | |
1878 | int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid, | |
1879 | unsigned int group, int report, gfp_t flags) | |
1880 | { | |
1881 | int err = 0; | |
1882 | ||
1883 | if (group) { | |
1884 | int exclude_pid = 0; | |
1885 | ||
1886 | if (report) { | |
1887 | atomic_inc(&skb->users); | |
1888 | exclude_pid = pid; | |
1889 | } | |
1890 | ||
1ce85fe4 PNA |
1891 | /* errors reported via destination sk->sk_err, but propagate |
1892 | * delivery errors if NETLINK_BROADCAST_ERROR flag is set */ | |
1893 | err = nlmsg_multicast(sk, skb, exclude_pid, group, flags); | |
d387f6ad TG |
1894 | } |
1895 | ||
1ce85fe4 PNA |
1896 | if (report) { |
1897 | int err2; | |
1898 | ||
1899 | err2 = nlmsg_unicast(sk, skb, pid); | |
1900 | if (!err || err == -ESRCH) | |
1901 | err = err2; | |
1902 | } | |
d387f6ad TG |
1903 | |
1904 | return err; | |
1905 | } | |
6ac552fd | 1906 | EXPORT_SYMBOL(nlmsg_notify); |
d387f6ad | 1907 | |
1da177e4 LT |
1908 | #ifdef CONFIG_PROC_FS |
1909 | struct nl_seq_iter { | |
e372c414 | 1910 | struct seq_net_private p; |
1da177e4 LT |
1911 | int link; |
1912 | int hash_idx; | |
1913 | }; | |
1914 | ||
1915 | static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos) | |
1916 | { | |
1917 | struct nl_seq_iter *iter = seq->private; | |
1918 | int i, j; | |
1919 | struct sock *s; | |
1920 | struct hlist_node *node; | |
1921 | loff_t off = 0; | |
1922 | ||
6ac552fd | 1923 | for (i = 0; i < MAX_LINKS; i++) { |
1da177e4 LT |
1924 | struct nl_pid_hash *hash = &nl_table[i].hash; |
1925 | ||
1926 | for (j = 0; j <= hash->mask; j++) { | |
1927 | sk_for_each(s, node, &hash->table[j]) { | |
1218854a | 1928 | if (sock_net(s) != seq_file_net(seq)) |
b4b51029 | 1929 | continue; |
1da177e4 LT |
1930 | if (off == pos) { |
1931 | iter->link = i; | |
1932 | iter->hash_idx = j; | |
1933 | return s; | |
1934 | } | |
1935 | ++off; | |
1936 | } | |
1937 | } | |
1938 | } | |
1939 | return NULL; | |
1940 | } | |
1941 | ||
1942 | static void *netlink_seq_start(struct seq_file *seq, loff_t *pos) | |
9a429c49 | 1943 | __acquires(nl_table_lock) |
1da177e4 LT |
1944 | { |
1945 | read_lock(&nl_table_lock); | |
1946 | return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN; | |
1947 | } | |
1948 | ||
1949 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
1950 | { | |
1951 | struct sock *s; | |
1952 | struct nl_seq_iter *iter; | |
1953 | int i, j; | |
1954 | ||
1955 | ++*pos; | |
1956 | ||
1957 | if (v == SEQ_START_TOKEN) | |
1958 | return netlink_seq_socket_idx(seq, 0); | |
746fac4d | 1959 | |
b4b51029 EB |
1960 | iter = seq->private; |
1961 | s = v; | |
1962 | do { | |
1963 | s = sk_next(s); | |
1218854a | 1964 | } while (s && sock_net(s) != seq_file_net(seq)); |
1da177e4 LT |
1965 | if (s) |
1966 | return s; | |
1967 | ||
1da177e4 LT |
1968 | i = iter->link; |
1969 | j = iter->hash_idx + 1; | |
1970 | ||
1971 | do { | |
1972 | struct nl_pid_hash *hash = &nl_table[i].hash; | |
1973 | ||
1974 | for (; j <= hash->mask; j++) { | |
1975 | s = sk_head(&hash->table[j]); | |
1218854a | 1976 | while (s && sock_net(s) != seq_file_net(seq)) |
b4b51029 | 1977 | s = sk_next(s); |
1da177e4 LT |
1978 | if (s) { |
1979 | iter->link = i; | |
1980 | iter->hash_idx = j; | |
1981 | return s; | |
1982 | } | |
1983 | } | |
1984 | ||
1985 | j = 0; | |
1986 | } while (++i < MAX_LINKS); | |
1987 | ||
1988 | return NULL; | |
1989 | } | |
1990 | ||
1991 | static void netlink_seq_stop(struct seq_file *seq, void *v) | |
9a429c49 | 1992 | __releases(nl_table_lock) |
1da177e4 LT |
1993 | { |
1994 | read_unlock(&nl_table_lock); | |
1995 | } | |
1996 | ||
1997 | ||
1998 | static int netlink_seq_show(struct seq_file *seq, void *v) | |
1999 | { | |
658cb354 | 2000 | if (v == SEQ_START_TOKEN) { |
1da177e4 LT |
2001 | seq_puts(seq, |
2002 | "sk Eth Pid Groups " | |
cf0aa4e0 | 2003 | "Rmem Wmem Dump Locks Drops Inode\n"); |
658cb354 | 2004 | } else { |
1da177e4 LT |
2005 | struct sock *s = v; |
2006 | struct netlink_sock *nlk = nlk_sk(s); | |
2007 | ||
71338aa7 | 2008 | seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n", |
1da177e4 LT |
2009 | s, |
2010 | s->sk_protocol, | |
2011 | nlk->pid, | |
513c2500 | 2012 | nlk->groups ? (u32)nlk->groups[0] : 0, |
31e6d363 ED |
2013 | sk_rmem_alloc_get(s), |
2014 | sk_wmem_alloc_get(s), | |
1da177e4 | 2015 | nlk->cb, |
38938bfe | 2016 | atomic_read(&s->sk_refcnt), |
cf0aa4e0 MY |
2017 | atomic_read(&s->sk_drops), |
2018 | sock_i_ino(s) | |
1da177e4 LT |
2019 | ); |
2020 | ||
2021 | } | |
2022 | return 0; | |
2023 | } | |
2024 | ||
56b3d975 | 2025 | static const struct seq_operations netlink_seq_ops = { |
1da177e4 LT |
2026 | .start = netlink_seq_start, |
2027 | .next = netlink_seq_next, | |
2028 | .stop = netlink_seq_stop, | |
2029 | .show = netlink_seq_show, | |
2030 | }; | |
2031 | ||
2032 | ||
2033 | static int netlink_seq_open(struct inode *inode, struct file *file) | |
2034 | { | |
e372c414 DL |
2035 | return seq_open_net(inode, file, &netlink_seq_ops, |
2036 | sizeof(struct nl_seq_iter)); | |
b4b51029 EB |
2037 | } |
2038 | ||
da7071d7 | 2039 | static const struct file_operations netlink_seq_fops = { |
1da177e4 LT |
2040 | .owner = THIS_MODULE, |
2041 | .open = netlink_seq_open, | |
2042 | .read = seq_read, | |
2043 | .llseek = seq_lseek, | |
e372c414 | 2044 | .release = seq_release_net, |
1da177e4 LT |
2045 | }; |
2046 | ||
2047 | #endif | |
2048 | ||
2049 | int netlink_register_notifier(struct notifier_block *nb) | |
2050 | { | |
e041c683 | 2051 | return atomic_notifier_chain_register(&netlink_chain, nb); |
1da177e4 | 2052 | } |
6ac552fd | 2053 | EXPORT_SYMBOL(netlink_register_notifier); |
1da177e4 LT |
2054 | |
2055 | int netlink_unregister_notifier(struct notifier_block *nb) | |
2056 | { | |
e041c683 | 2057 | return atomic_notifier_chain_unregister(&netlink_chain, nb); |
1da177e4 | 2058 | } |
6ac552fd | 2059 | EXPORT_SYMBOL(netlink_unregister_notifier); |
746fac4d | 2060 | |
90ddc4f0 | 2061 | static const struct proto_ops netlink_ops = { |
1da177e4 LT |
2062 | .family = PF_NETLINK, |
2063 | .owner = THIS_MODULE, | |
2064 | .release = netlink_release, | |
2065 | .bind = netlink_bind, | |
2066 | .connect = netlink_connect, | |
2067 | .socketpair = sock_no_socketpair, | |
2068 | .accept = sock_no_accept, | |
2069 | .getname = netlink_getname, | |
2070 | .poll = datagram_poll, | |
2071 | .ioctl = sock_no_ioctl, | |
2072 | .listen = sock_no_listen, | |
2073 | .shutdown = sock_no_shutdown, | |
9a4595bc PM |
2074 | .setsockopt = netlink_setsockopt, |
2075 | .getsockopt = netlink_getsockopt, | |
1da177e4 LT |
2076 | .sendmsg = netlink_sendmsg, |
2077 | .recvmsg = netlink_recvmsg, | |
2078 | .mmap = sock_no_mmap, | |
2079 | .sendpage = sock_no_sendpage, | |
2080 | }; | |
2081 | ||
ec1b4cf7 | 2082 | static const struct net_proto_family netlink_family_ops = { |
1da177e4 LT |
2083 | .family = PF_NETLINK, |
2084 | .create = netlink_create, | |
2085 | .owner = THIS_MODULE, /* for consistency 8) */ | |
2086 | }; | |
2087 | ||
4665079c | 2088 | static int __net_init netlink_net_init(struct net *net) |
b4b51029 EB |
2089 | { |
2090 | #ifdef CONFIG_PROC_FS | |
2091 | if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops)) | |
2092 | return -ENOMEM; | |
2093 | #endif | |
2094 | return 0; | |
2095 | } | |
2096 | ||
4665079c | 2097 | static void __net_exit netlink_net_exit(struct net *net) |
b4b51029 EB |
2098 | { |
2099 | #ifdef CONFIG_PROC_FS | |
2100 | proc_net_remove(net, "netlink"); | |
2101 | #endif | |
2102 | } | |
2103 | ||
b963ea89 DM |
2104 | static void __init netlink_add_usersock_entry(void) |
2105 | { | |
5c398dc8 | 2106 | struct listeners *listeners; |
b963ea89 DM |
2107 | int groups = 32; |
2108 | ||
5c398dc8 | 2109 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
b963ea89 | 2110 | if (!listeners) |
5c398dc8 | 2111 | panic("netlink_add_usersock_entry: Cannot allocate listeners\n"); |
b963ea89 DM |
2112 | |
2113 | netlink_table_grab(); | |
2114 | ||
2115 | nl_table[NETLINK_USERSOCK].groups = groups; | |
5c398dc8 | 2116 | rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners); |
b963ea89 DM |
2117 | nl_table[NETLINK_USERSOCK].module = THIS_MODULE; |
2118 | nl_table[NETLINK_USERSOCK].registered = 1; | |
2119 | ||
2120 | netlink_table_ungrab(); | |
2121 | } | |
2122 | ||
022cbae6 | 2123 | static struct pernet_operations __net_initdata netlink_net_ops = { |
b4b51029 EB |
2124 | .init = netlink_net_init, |
2125 | .exit = netlink_net_exit, | |
2126 | }; | |
2127 | ||
1da177e4 LT |
2128 | static int __init netlink_proto_init(void) |
2129 | { | |
2130 | struct sk_buff *dummy_skb; | |
2131 | int i; | |
26ff5ddc | 2132 | unsigned long limit; |
1da177e4 LT |
2133 | unsigned int order; |
2134 | int err = proto_register(&netlink_proto, 0); | |
2135 | ||
2136 | if (err != 0) | |
2137 | goto out; | |
2138 | ||
ef047f5e | 2139 | BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)); |
1da177e4 | 2140 | |
0da974f4 | 2141 | nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL); |
fab2caf6 AM |
2142 | if (!nl_table) |
2143 | goto panic; | |
1da177e4 | 2144 | |
4481374c JB |
2145 | if (totalram_pages >= (128 * 1024)) |
2146 | limit = totalram_pages >> (21 - PAGE_SHIFT); | |
1da177e4 | 2147 | else |
4481374c | 2148 | limit = totalram_pages >> (23 - PAGE_SHIFT); |
1da177e4 | 2149 | |
26ff5ddc DC |
2150 | order = get_bitmask_order(limit) - 1 + PAGE_SHIFT; |
2151 | limit = (1UL << order) / sizeof(struct hlist_head); | |
2152 | order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1; | |
1da177e4 LT |
2153 | |
2154 | for (i = 0; i < MAX_LINKS; i++) { | |
2155 | struct nl_pid_hash *hash = &nl_table[i].hash; | |
2156 | ||
ea72912c | 2157 | hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table)); |
1da177e4 LT |
2158 | if (!hash->table) { |
2159 | while (i-- > 0) | |
2160 | nl_pid_hash_free(nl_table[i].hash.table, | |
2161 | 1 * sizeof(*hash->table)); | |
2162 | kfree(nl_table); | |
fab2caf6 | 2163 | goto panic; |
1da177e4 | 2164 | } |
1da177e4 LT |
2165 | hash->max_shift = order; |
2166 | hash->shift = 0; | |
2167 | hash->mask = 0; | |
2168 | hash->rehash_time = jiffies; | |
2169 | } | |
2170 | ||
b963ea89 DM |
2171 | netlink_add_usersock_entry(); |
2172 | ||
1da177e4 | 2173 | sock_register(&netlink_family_ops); |
b4b51029 | 2174 | register_pernet_subsys(&netlink_net_ops); |
746fac4d | 2175 | /* The netlink device handler may be needed early. */ |
1da177e4 LT |
2176 | rtnetlink_init(); |
2177 | out: | |
2178 | return err; | |
fab2caf6 AM |
2179 | panic: |
2180 | panic("netlink_init: Cannot allocate nl_table\n"); | |
1da177e4 LT |
2181 | } |
2182 | ||
1da177e4 | 2183 | core_initcall(netlink_proto_init); |