Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * NETLINK Kernel-user communication protocol. | |
3 | * | |
4 | * Authors: Alan Cox <alan@redhat.com> | |
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. | |
11 | * | |
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 | ||
24 | #include <linux/config.h> | |
25 | #include <linux/module.h> | |
26 | ||
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> | |
48 | #include <linux/smp_lock.h> | |
49 | #include <linux/notifier.h> | |
50 | #include <linux/security.h> | |
51 | #include <linux/jhash.h> | |
52 | #include <linux/jiffies.h> | |
53 | #include <linux/random.h> | |
54 | #include <linux/bitops.h> | |
55 | #include <linux/mm.h> | |
56 | #include <linux/types.h> | |
54e0f520 AM |
57 | #include <linux/audit.h> |
58 | ||
1da177e4 LT |
59 | #include <net/sock.h> |
60 | #include <net/scm.h> | |
61 | ||
62 | #define Nprintk(a...) | |
f7fa9b10 | 63 | #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8) |
1da177e4 LT |
64 | |
65 | struct netlink_sock { | |
66 | /* struct sock has to be the first member of netlink_sock */ | |
67 | struct sock sk; | |
68 | u32 pid; | |
1da177e4 | 69 | u32 dst_pid; |
d629b836 | 70 | u32 dst_group; |
f7fa9b10 PM |
71 | u32 flags; |
72 | u32 subscriptions; | |
73 | u32 ngroups; | |
74 | unsigned long *groups; | |
1da177e4 LT |
75 | unsigned long state; |
76 | wait_queue_head_t wait; | |
77 | struct netlink_callback *cb; | |
78 | spinlock_t cb_lock; | |
79 | void (*data_ready)(struct sock *sk, int bytes); | |
77247bbb | 80 | struct module *module; |
1da177e4 LT |
81 | }; |
82 | ||
77247bbb | 83 | #define NETLINK_KERNEL_SOCKET 0x1 |
9a4595bc | 84 | #define NETLINK_RECV_PKTINFO 0x2 |
77247bbb | 85 | |
1da177e4 LT |
86 | static inline struct netlink_sock *nlk_sk(struct sock *sk) |
87 | { | |
88 | return (struct netlink_sock *)sk; | |
89 | } | |
90 | ||
91 | struct nl_pid_hash { | |
92 | struct hlist_head *table; | |
93 | unsigned long rehash_time; | |
94 | ||
95 | unsigned int mask; | |
96 | unsigned int shift; | |
97 | ||
98 | unsigned int entries; | |
99 | unsigned int max_shift; | |
100 | ||
101 | u32 rnd; | |
102 | }; | |
103 | ||
104 | struct netlink_table { | |
105 | struct nl_pid_hash hash; | |
106 | struct hlist_head mc_list; | |
107 | unsigned int nl_nonroot; | |
f7fa9b10 | 108 | unsigned int groups; |
77247bbb | 109 | struct module *module; |
ab33a171 | 110 | int registered; |
1da177e4 LT |
111 | }; |
112 | ||
113 | static struct netlink_table *nl_table; | |
114 | ||
115 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); | |
116 | ||
117 | static int netlink_dump(struct sock *sk); | |
118 | static void netlink_destroy_callback(struct netlink_callback *cb); | |
119 | ||
120 | static DEFINE_RWLOCK(nl_table_lock); | |
121 | static atomic_t nl_table_users = ATOMIC_INIT(0); | |
122 | ||
123 | static struct notifier_block *netlink_chain; | |
124 | ||
d629b836 PM |
125 | static u32 netlink_group_mask(u32 group) |
126 | { | |
127 | return group ? 1 << (group - 1) : 0; | |
128 | } | |
129 | ||
1da177e4 LT |
130 | static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid) |
131 | { | |
132 | return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask]; | |
133 | } | |
134 | ||
135 | static void netlink_sock_destruct(struct sock *sk) | |
136 | { | |
137 | skb_queue_purge(&sk->sk_receive_queue); | |
138 | ||
139 | if (!sock_flag(sk, SOCK_DEAD)) { | |
140 | printk("Freeing alive netlink socket %p\n", sk); | |
141 | return; | |
142 | } | |
143 | BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc)); | |
144 | BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc)); | |
145 | BUG_TRAP(!nlk_sk(sk)->cb); | |
f7fa9b10 | 146 | BUG_TRAP(!nlk_sk(sk)->groups); |
1da177e4 LT |
147 | } |
148 | ||
149 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP. | |
150 | * Look, when several writers sleep and reader wakes them up, all but one | |
151 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves | |
152 | * this, _but_ remember, it adds useless work on UP machines. | |
153 | */ | |
154 | ||
155 | static void netlink_table_grab(void) | |
156 | { | |
157 | write_lock_bh(&nl_table_lock); | |
158 | ||
159 | if (atomic_read(&nl_table_users)) { | |
160 | DECLARE_WAITQUEUE(wait, current); | |
161 | ||
162 | add_wait_queue_exclusive(&nl_table_wait, &wait); | |
163 | for(;;) { | |
164 | set_current_state(TASK_UNINTERRUPTIBLE); | |
165 | if (atomic_read(&nl_table_users) == 0) | |
166 | break; | |
167 | write_unlock_bh(&nl_table_lock); | |
168 | schedule(); | |
169 | write_lock_bh(&nl_table_lock); | |
170 | } | |
171 | ||
172 | __set_current_state(TASK_RUNNING); | |
173 | remove_wait_queue(&nl_table_wait, &wait); | |
174 | } | |
175 | } | |
176 | ||
177 | static __inline__ void netlink_table_ungrab(void) | |
178 | { | |
179 | write_unlock_bh(&nl_table_lock); | |
180 | wake_up(&nl_table_wait); | |
181 | } | |
182 | ||
183 | static __inline__ void | |
184 | netlink_lock_table(void) | |
185 | { | |
186 | /* read_lock() synchronizes us to netlink_table_grab */ | |
187 | ||
188 | read_lock(&nl_table_lock); | |
189 | atomic_inc(&nl_table_users); | |
190 | read_unlock(&nl_table_lock); | |
191 | } | |
192 | ||
193 | static __inline__ void | |
194 | netlink_unlock_table(void) | |
195 | { | |
196 | if (atomic_dec_and_test(&nl_table_users)) | |
197 | wake_up(&nl_table_wait); | |
198 | } | |
199 | ||
200 | static __inline__ struct sock *netlink_lookup(int protocol, u32 pid) | |
201 | { | |
202 | struct nl_pid_hash *hash = &nl_table[protocol].hash; | |
203 | struct hlist_head *head; | |
204 | struct sock *sk; | |
205 | struct hlist_node *node; | |
206 | ||
207 | read_lock(&nl_table_lock); | |
208 | head = nl_pid_hashfn(hash, pid); | |
209 | sk_for_each(sk, node, head) { | |
210 | if (nlk_sk(sk)->pid == pid) { | |
211 | sock_hold(sk); | |
212 | goto found; | |
213 | } | |
214 | } | |
215 | sk = NULL; | |
216 | found: | |
217 | read_unlock(&nl_table_lock); | |
218 | return sk; | |
219 | } | |
220 | ||
221 | static inline struct hlist_head *nl_pid_hash_alloc(size_t size) | |
222 | { | |
223 | if (size <= PAGE_SIZE) | |
224 | return kmalloc(size, GFP_ATOMIC); | |
225 | else | |
226 | return (struct hlist_head *) | |
227 | __get_free_pages(GFP_ATOMIC, get_order(size)); | |
228 | } | |
229 | ||
230 | static inline void nl_pid_hash_free(struct hlist_head *table, size_t size) | |
231 | { | |
232 | if (size <= PAGE_SIZE) | |
233 | kfree(table); | |
234 | else | |
235 | free_pages((unsigned long)table, get_order(size)); | |
236 | } | |
237 | ||
238 | static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow) | |
239 | { | |
240 | unsigned int omask, mask, shift; | |
241 | size_t osize, size; | |
242 | struct hlist_head *otable, *table; | |
243 | int i; | |
244 | ||
245 | omask = mask = hash->mask; | |
246 | osize = size = (mask + 1) * sizeof(*table); | |
247 | shift = hash->shift; | |
248 | ||
249 | if (grow) { | |
250 | if (++shift > hash->max_shift) | |
251 | return 0; | |
252 | mask = mask * 2 + 1; | |
253 | size *= 2; | |
254 | } | |
255 | ||
256 | table = nl_pid_hash_alloc(size); | |
257 | if (!table) | |
258 | return 0; | |
259 | ||
260 | memset(table, 0, size); | |
261 | otable = hash->table; | |
262 | hash->table = table; | |
263 | hash->mask = mask; | |
264 | hash->shift = shift; | |
265 | get_random_bytes(&hash->rnd, sizeof(hash->rnd)); | |
266 | ||
267 | for (i = 0; i <= omask; i++) { | |
268 | struct sock *sk; | |
269 | struct hlist_node *node, *tmp; | |
270 | ||
271 | sk_for_each_safe(sk, node, tmp, &otable[i]) | |
272 | __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid)); | |
273 | } | |
274 | ||
275 | nl_pid_hash_free(otable, osize); | |
276 | hash->rehash_time = jiffies + 10 * 60 * HZ; | |
277 | return 1; | |
278 | } | |
279 | ||
280 | static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len) | |
281 | { | |
282 | int avg = hash->entries >> hash->shift; | |
283 | ||
284 | if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1)) | |
285 | return 1; | |
286 | ||
287 | if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) { | |
288 | nl_pid_hash_rehash(hash, 0); | |
289 | return 1; | |
290 | } | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
295 | static struct proto_ops netlink_ops; | |
296 | ||
297 | static int netlink_insert(struct sock *sk, u32 pid) | |
298 | { | |
299 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; | |
300 | struct hlist_head *head; | |
301 | int err = -EADDRINUSE; | |
302 | struct sock *osk; | |
303 | struct hlist_node *node; | |
304 | int len; | |
305 | ||
306 | netlink_table_grab(); | |
307 | head = nl_pid_hashfn(hash, pid); | |
308 | len = 0; | |
309 | sk_for_each(osk, node, head) { | |
310 | if (nlk_sk(osk)->pid == pid) | |
311 | break; | |
312 | len++; | |
313 | } | |
314 | if (node) | |
315 | goto err; | |
316 | ||
317 | err = -EBUSY; | |
318 | if (nlk_sk(sk)->pid) | |
319 | goto err; | |
320 | ||
321 | err = -ENOMEM; | |
322 | if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX)) | |
323 | goto err; | |
324 | ||
325 | if (len && nl_pid_hash_dilute(hash, len)) | |
326 | head = nl_pid_hashfn(hash, pid); | |
327 | hash->entries++; | |
328 | nlk_sk(sk)->pid = pid; | |
329 | sk_add_node(sk, head); | |
330 | err = 0; | |
331 | ||
332 | err: | |
333 | netlink_table_ungrab(); | |
334 | return err; | |
335 | } | |
336 | ||
337 | static void netlink_remove(struct sock *sk) | |
338 | { | |
339 | netlink_table_grab(); | |
d470e3b4 DM |
340 | if (sk_del_node_init(sk)) |
341 | nl_table[sk->sk_protocol].hash.entries--; | |
f7fa9b10 | 342 | if (nlk_sk(sk)->subscriptions) |
1da177e4 LT |
343 | __sk_del_bind_node(sk); |
344 | netlink_table_ungrab(); | |
345 | } | |
346 | ||
347 | static struct proto netlink_proto = { | |
348 | .name = "NETLINK", | |
349 | .owner = THIS_MODULE, | |
350 | .obj_size = sizeof(struct netlink_sock), | |
351 | }; | |
352 | ||
ab33a171 | 353 | static int __netlink_create(struct socket *sock, int protocol) |
1da177e4 LT |
354 | { |
355 | struct sock *sk; | |
356 | struct netlink_sock *nlk; | |
ab33a171 PM |
357 | |
358 | sock->ops = &netlink_ops; | |
359 | ||
360 | sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1); | |
361 | if (!sk) | |
362 | return -ENOMEM; | |
363 | ||
364 | sock_init_data(sock, sk); | |
365 | ||
366 | nlk = nlk_sk(sk); | |
367 | spin_lock_init(&nlk->cb_lock); | |
368 | init_waitqueue_head(&nlk->wait); | |
369 | ||
370 | sk->sk_destruct = netlink_sock_destruct; | |
371 | sk->sk_protocol = protocol; | |
372 | return 0; | |
373 | } | |
374 | ||
375 | static int netlink_create(struct socket *sock, int protocol) | |
376 | { | |
377 | struct module *module = NULL; | |
f7fa9b10 PM |
378 | struct netlink_sock *nlk; |
379 | unsigned int groups; | |
ab33a171 | 380 | int err = 0; |
1da177e4 LT |
381 | |
382 | sock->state = SS_UNCONNECTED; | |
383 | ||
384 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) | |
385 | return -ESOCKTNOSUPPORT; | |
386 | ||
387 | if (protocol<0 || protocol >= MAX_LINKS) | |
388 | return -EPROTONOSUPPORT; | |
389 | ||
77247bbb | 390 | netlink_lock_table(); |
4fdb3bb7 | 391 | #ifdef CONFIG_KMOD |
ab33a171 | 392 | if (!nl_table[protocol].registered) { |
77247bbb | 393 | netlink_unlock_table(); |
4fdb3bb7 | 394 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
77247bbb | 395 | netlink_lock_table(); |
4fdb3bb7 | 396 | } |
ab33a171 PM |
397 | #endif |
398 | if (nl_table[protocol].registered && | |
399 | try_module_get(nl_table[protocol].module)) | |
400 | module = nl_table[protocol].module; | |
f7fa9b10 | 401 | groups = nl_table[protocol].groups; |
77247bbb | 402 | netlink_unlock_table(); |
4fdb3bb7 | 403 | |
513c2500 | 404 | if ((err = __netlink_create(sock, protocol) < 0)) |
f7fa9b10 PM |
405 | goto out_module; |
406 | ||
407 | nlk = nlk_sk(sock->sk); | |
f7fa9b10 | 408 | nlk->module = module; |
ab33a171 PM |
409 | out: |
410 | return err; | |
1da177e4 | 411 | |
ab33a171 PM |
412 | out_module: |
413 | module_put(module); | |
414 | goto out; | |
1da177e4 LT |
415 | } |
416 | ||
417 | static int netlink_release(struct socket *sock) | |
418 | { | |
419 | struct sock *sk = sock->sk; | |
420 | struct netlink_sock *nlk; | |
421 | ||
422 | if (!sk) | |
423 | return 0; | |
424 | ||
425 | netlink_remove(sk); | |
426 | nlk = nlk_sk(sk); | |
427 | ||
428 | spin_lock(&nlk->cb_lock); | |
429 | if (nlk->cb) { | |
a8f74b22 TG |
430 | if (nlk->cb->done) |
431 | nlk->cb->done(nlk->cb); | |
1da177e4 LT |
432 | netlink_destroy_callback(nlk->cb); |
433 | nlk->cb = NULL; | |
1da177e4 LT |
434 | } |
435 | spin_unlock(&nlk->cb_lock); | |
436 | ||
437 | /* OK. Socket is unlinked, and, therefore, | |
438 | no new packets will arrive */ | |
439 | ||
440 | sock_orphan(sk); | |
441 | sock->sk = NULL; | |
442 | wake_up_interruptible_all(&nlk->wait); | |
443 | ||
444 | skb_queue_purge(&sk->sk_write_queue); | |
445 | ||
f7fa9b10 | 446 | if (nlk->pid && !nlk->subscriptions) { |
1da177e4 LT |
447 | struct netlink_notify n = { |
448 | .protocol = sk->sk_protocol, | |
449 | .pid = nlk->pid, | |
450 | }; | |
451 | notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n); | |
452 | } | |
4fdb3bb7 | 453 | |
77247bbb PM |
454 | if (nlk->module) |
455 | module_put(nlk->module); | |
4fdb3bb7 | 456 | |
77247bbb | 457 | if (nlk->flags & NETLINK_KERNEL_SOCKET) { |
4fdb3bb7 | 458 | netlink_table_grab(); |
77247bbb | 459 | nl_table[sk->sk_protocol].module = NULL; |
ab33a171 | 460 | nl_table[sk->sk_protocol].registered = 0; |
4fdb3bb7 HW |
461 | netlink_table_ungrab(); |
462 | } | |
77247bbb | 463 | |
f7fa9b10 PM |
464 | kfree(nlk->groups); |
465 | nlk->groups = NULL; | |
466 | ||
1da177e4 LT |
467 | sock_put(sk); |
468 | return 0; | |
469 | } | |
470 | ||
471 | static int netlink_autobind(struct socket *sock) | |
472 | { | |
473 | struct sock *sk = sock->sk; | |
474 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; | |
475 | struct hlist_head *head; | |
476 | struct sock *osk; | |
477 | struct hlist_node *node; | |
478 | s32 pid = current->pid; | |
479 | int err; | |
480 | static s32 rover = -4097; | |
481 | ||
482 | retry: | |
483 | cond_resched(); | |
484 | netlink_table_grab(); | |
485 | head = nl_pid_hashfn(hash, pid); | |
486 | sk_for_each(osk, node, head) { | |
487 | if (nlk_sk(osk)->pid == pid) { | |
488 | /* Bind collision, search negative pid values. */ | |
489 | pid = rover--; | |
490 | if (rover > -4097) | |
491 | rover = -4097; | |
492 | netlink_table_ungrab(); | |
493 | goto retry; | |
494 | } | |
495 | } | |
496 | netlink_table_ungrab(); | |
497 | ||
498 | err = netlink_insert(sk, pid); | |
499 | if (err == -EADDRINUSE) | |
500 | goto retry; | |
d470e3b4 DM |
501 | |
502 | /* If 2 threads race to autobind, that is fine. */ | |
503 | if (err == -EBUSY) | |
504 | err = 0; | |
505 | ||
506 | return err; | |
1da177e4 LT |
507 | } |
508 | ||
509 | static inline int netlink_capable(struct socket *sock, unsigned int flag) | |
510 | { | |
511 | return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) || | |
512 | capable(CAP_NET_ADMIN); | |
513 | } | |
514 | ||
f7fa9b10 PM |
515 | static void |
516 | netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) | |
517 | { | |
518 | struct netlink_sock *nlk = nlk_sk(sk); | |
519 | ||
520 | if (nlk->subscriptions && !subscriptions) | |
521 | __sk_del_bind_node(sk); | |
522 | else if (!nlk->subscriptions && subscriptions) | |
523 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); | |
524 | nlk->subscriptions = subscriptions; | |
525 | } | |
526 | ||
513c2500 PM |
527 | static int netlink_alloc_groups(struct sock *sk) |
528 | { | |
529 | struct netlink_sock *nlk = nlk_sk(sk); | |
530 | unsigned int groups; | |
531 | int err = 0; | |
532 | ||
533 | netlink_lock_table(); | |
534 | groups = nl_table[sk->sk_protocol].groups; | |
535 | if (!nl_table[sk->sk_protocol].registered) | |
536 | err = -ENOENT; | |
537 | netlink_unlock_table(); | |
538 | ||
539 | if (err) | |
540 | return err; | |
541 | ||
542 | nlk->groups = kmalloc(NLGRPSZ(groups), GFP_KERNEL); | |
543 | if (nlk->groups == NULL) | |
544 | return -ENOMEM; | |
545 | memset(nlk->groups, 0, NLGRPSZ(groups)); | |
546 | nlk->ngroups = groups; | |
547 | return 0; | |
548 | } | |
549 | ||
1da177e4 LT |
550 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
551 | { | |
552 | struct sock *sk = sock->sk; | |
553 | struct netlink_sock *nlk = nlk_sk(sk); | |
554 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; | |
555 | int err; | |
556 | ||
557 | if (nladdr->nl_family != AF_NETLINK) | |
558 | return -EINVAL; | |
559 | ||
560 | /* Only superuser is allowed to listen multicasts */ | |
513c2500 PM |
561 | if (nladdr->nl_groups) { |
562 | if (!netlink_capable(sock, NL_NONROOT_RECV)) | |
563 | return -EPERM; | |
564 | if (nlk->groups == NULL) { | |
565 | err = netlink_alloc_groups(sk); | |
566 | if (err) | |
567 | return err; | |
568 | } | |
569 | } | |
1da177e4 LT |
570 | |
571 | if (nlk->pid) { | |
572 | if (nladdr->nl_pid != nlk->pid) | |
573 | return -EINVAL; | |
574 | } else { | |
575 | err = nladdr->nl_pid ? | |
576 | netlink_insert(sk, nladdr->nl_pid) : | |
577 | netlink_autobind(sock); | |
578 | if (err) | |
579 | return err; | |
580 | } | |
581 | ||
513c2500 | 582 | if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0])) |
1da177e4 LT |
583 | return 0; |
584 | ||
585 | netlink_table_grab(); | |
f7fa9b10 PM |
586 | netlink_update_subscriptions(sk, nlk->subscriptions + |
587 | hweight32(nladdr->nl_groups) - | |
588 | hweight32(nlk->groups[0])); | |
589 | nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups; | |
1da177e4 LT |
590 | netlink_table_ungrab(); |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
595 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, | |
596 | int alen, int flags) | |
597 | { | |
598 | int err = 0; | |
599 | struct sock *sk = sock->sk; | |
600 | struct netlink_sock *nlk = nlk_sk(sk); | |
601 | struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr; | |
602 | ||
603 | if (addr->sa_family == AF_UNSPEC) { | |
604 | sk->sk_state = NETLINK_UNCONNECTED; | |
605 | nlk->dst_pid = 0; | |
d629b836 | 606 | nlk->dst_group = 0; |
1da177e4 LT |
607 | return 0; |
608 | } | |
609 | if (addr->sa_family != AF_NETLINK) | |
610 | return -EINVAL; | |
611 | ||
612 | /* Only superuser is allowed to send multicasts */ | |
613 | if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND)) | |
614 | return -EPERM; | |
615 | ||
616 | if (!nlk->pid) | |
617 | err = netlink_autobind(sock); | |
618 | ||
619 | if (err == 0) { | |
620 | sk->sk_state = NETLINK_CONNECTED; | |
621 | nlk->dst_pid = nladdr->nl_pid; | |
d629b836 | 622 | nlk->dst_group = ffs(nladdr->nl_groups); |
1da177e4 LT |
623 | } |
624 | ||
625 | return err; | |
626 | } | |
627 | ||
628 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer) | |
629 | { | |
630 | struct sock *sk = sock->sk; | |
631 | struct netlink_sock *nlk = nlk_sk(sk); | |
632 | struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr; | |
633 | ||
634 | nladdr->nl_family = AF_NETLINK; | |
635 | nladdr->nl_pad = 0; | |
636 | *addr_len = sizeof(*nladdr); | |
637 | ||
638 | if (peer) { | |
639 | nladdr->nl_pid = nlk->dst_pid; | |
d629b836 | 640 | nladdr->nl_groups = netlink_group_mask(nlk->dst_group); |
1da177e4 LT |
641 | } else { |
642 | nladdr->nl_pid = nlk->pid; | |
513c2500 | 643 | nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; |
1da177e4 LT |
644 | } |
645 | return 0; | |
646 | } | |
647 | ||
648 | static void netlink_overrun(struct sock *sk) | |
649 | { | |
650 | if (!test_and_set_bit(0, &nlk_sk(sk)->state)) { | |
651 | sk->sk_err = ENOBUFS; | |
652 | sk->sk_error_report(sk); | |
653 | } | |
654 | } | |
655 | ||
656 | static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid) | |
657 | { | |
658 | int protocol = ssk->sk_protocol; | |
659 | struct sock *sock; | |
660 | struct netlink_sock *nlk; | |
661 | ||
662 | sock = netlink_lookup(protocol, pid); | |
663 | if (!sock) | |
664 | return ERR_PTR(-ECONNREFUSED); | |
665 | ||
666 | /* Don't bother queuing skb if kernel socket has no input function */ | |
667 | nlk = nlk_sk(sock); | |
668 | if ((nlk->pid == 0 && !nlk->data_ready) || | |
669 | (sock->sk_state == NETLINK_CONNECTED && | |
670 | nlk->dst_pid != nlk_sk(ssk)->pid)) { | |
671 | sock_put(sock); | |
672 | return ERR_PTR(-ECONNREFUSED); | |
673 | } | |
674 | return sock; | |
675 | } | |
676 | ||
677 | struct sock *netlink_getsockbyfilp(struct file *filp) | |
678 | { | |
679 | struct inode *inode = filp->f_dentry->d_inode; | |
680 | struct sock *sock; | |
681 | ||
682 | if (!S_ISSOCK(inode->i_mode)) | |
683 | return ERR_PTR(-ENOTSOCK); | |
684 | ||
685 | sock = SOCKET_I(inode)->sk; | |
686 | if (sock->sk_family != AF_NETLINK) | |
687 | return ERR_PTR(-EINVAL); | |
688 | ||
689 | sock_hold(sock); | |
690 | return sock; | |
691 | } | |
692 | ||
693 | /* | |
694 | * Attach a skb to a netlink socket. | |
695 | * The caller must hold a reference to the destination socket. On error, the | |
696 | * reference is dropped. The skb is not send to the destination, just all | |
697 | * all error checks are performed and memory in the queue is reserved. | |
698 | * Return values: | |
699 | * < 0: error. skb freed, reference to sock dropped. | |
700 | * 0: continue | |
701 | * 1: repeat lookup - reference dropped while waiting for socket memory. | |
702 | */ | |
703 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo) | |
704 | { | |
705 | struct netlink_sock *nlk; | |
706 | ||
707 | nlk = nlk_sk(sk); | |
708 | ||
709 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || | |
710 | test_bit(0, &nlk->state)) { | |
711 | DECLARE_WAITQUEUE(wait, current); | |
712 | if (!timeo) { | |
713 | if (!nlk->pid) | |
714 | netlink_overrun(sk); | |
715 | sock_put(sk); | |
716 | kfree_skb(skb); | |
717 | return -EAGAIN; | |
718 | } | |
719 | ||
720 | __set_current_state(TASK_INTERRUPTIBLE); | |
721 | add_wait_queue(&nlk->wait, &wait); | |
722 | ||
723 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || | |
724 | test_bit(0, &nlk->state)) && | |
725 | !sock_flag(sk, SOCK_DEAD)) | |
726 | timeo = schedule_timeout(timeo); | |
727 | ||
728 | __set_current_state(TASK_RUNNING); | |
729 | remove_wait_queue(&nlk->wait, &wait); | |
730 | sock_put(sk); | |
731 | ||
732 | if (signal_pending(current)) { | |
733 | kfree_skb(skb); | |
734 | return sock_intr_errno(timeo); | |
735 | } | |
736 | return 1; | |
737 | } | |
738 | skb_set_owner_r(skb, sk); | |
739 | return 0; | |
740 | } | |
741 | ||
742 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol) | |
743 | { | |
1da177e4 LT |
744 | int len = skb->len; |
745 | ||
1da177e4 LT |
746 | skb_queue_tail(&sk->sk_receive_queue, skb); |
747 | sk->sk_data_ready(sk, len); | |
748 | sock_put(sk); | |
749 | return len; | |
750 | } | |
751 | ||
752 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) | |
753 | { | |
754 | kfree_skb(skb); | |
755 | sock_put(sk); | |
756 | } | |
757 | ||
37da647d | 758 | static inline struct sk_buff *netlink_trim(struct sk_buff *skb, |
dd0fc66f | 759 | gfp_t allocation) |
1da177e4 LT |
760 | { |
761 | int delta; | |
762 | ||
763 | skb_orphan(skb); | |
764 | ||
765 | delta = skb->end - skb->tail; | |
766 | if (delta * 2 < skb->truesize) | |
767 | return skb; | |
768 | ||
769 | if (skb_shared(skb)) { | |
770 | struct sk_buff *nskb = skb_clone(skb, allocation); | |
771 | if (!nskb) | |
772 | return skb; | |
773 | kfree_skb(skb); | |
774 | skb = nskb; | |
775 | } | |
776 | ||
777 | if (!pskb_expand_head(skb, 0, -delta, allocation)) | |
778 | skb->truesize -= delta; | |
779 | ||
780 | return skb; | |
781 | } | |
782 | ||
783 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock) | |
784 | { | |
785 | struct sock *sk; | |
786 | int err; | |
787 | long timeo; | |
788 | ||
789 | skb = netlink_trim(skb, gfp_any()); | |
790 | ||
791 | timeo = sock_sndtimeo(ssk, nonblock); | |
792 | retry: | |
793 | sk = netlink_getsockbypid(ssk, pid); | |
794 | if (IS_ERR(sk)) { | |
795 | kfree_skb(skb); | |
796 | return PTR_ERR(sk); | |
797 | } | |
798 | err = netlink_attachskb(sk, skb, nonblock, timeo); | |
799 | if (err == 1) | |
800 | goto retry; | |
801 | if (err) | |
802 | return err; | |
803 | ||
804 | return netlink_sendskb(sk, skb, ssk->sk_protocol); | |
805 | } | |
806 | ||
807 | static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) | |
808 | { | |
809 | struct netlink_sock *nlk = nlk_sk(sk); | |
810 | ||
811 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && | |
812 | !test_bit(0, &nlk->state)) { | |
813 | skb_set_owner_r(skb, sk); | |
814 | skb_queue_tail(&sk->sk_receive_queue, skb); | |
815 | sk->sk_data_ready(sk, skb->len); | |
816 | return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf; | |
817 | } | |
818 | return -1; | |
819 | } | |
820 | ||
821 | struct netlink_broadcast_data { | |
822 | struct sock *exclude_sk; | |
823 | u32 pid; | |
824 | u32 group; | |
825 | int failure; | |
826 | int congested; | |
827 | int delivered; | |
7d877f3b | 828 | gfp_t allocation; |
1da177e4 LT |
829 | struct sk_buff *skb, *skb2; |
830 | }; | |
831 | ||
832 | static inline int do_one_broadcast(struct sock *sk, | |
833 | struct netlink_broadcast_data *p) | |
834 | { | |
835 | struct netlink_sock *nlk = nlk_sk(sk); | |
836 | int val; | |
837 | ||
838 | if (p->exclude_sk == sk) | |
839 | goto out; | |
840 | ||
f7fa9b10 PM |
841 | if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups || |
842 | !test_bit(p->group - 1, nlk->groups)) | |
1da177e4 LT |
843 | goto out; |
844 | ||
845 | if (p->failure) { | |
846 | netlink_overrun(sk); | |
847 | goto out; | |
848 | } | |
849 | ||
850 | sock_hold(sk); | |
851 | if (p->skb2 == NULL) { | |
68acc024 | 852 | if (skb_shared(p->skb)) { |
1da177e4 LT |
853 | p->skb2 = skb_clone(p->skb, p->allocation); |
854 | } else { | |
68acc024 TC |
855 | p->skb2 = skb_get(p->skb); |
856 | /* | |
857 | * skb ownership may have been set when | |
858 | * delivered to a previous socket. | |
859 | */ | |
860 | skb_orphan(p->skb2); | |
1da177e4 LT |
861 | } |
862 | } | |
863 | if (p->skb2 == NULL) { | |
864 | netlink_overrun(sk); | |
865 | /* Clone failed. Notify ALL listeners. */ | |
866 | p->failure = 1; | |
867 | } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) { | |
868 | netlink_overrun(sk); | |
869 | } else { | |
870 | p->congested |= val; | |
871 | p->delivered = 1; | |
872 | p->skb2 = NULL; | |
873 | } | |
874 | sock_put(sk); | |
875 | ||
876 | out: | |
877 | return 0; | |
878 | } | |
879 | ||
880 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid, | |
dd0fc66f | 881 | u32 group, gfp_t allocation) |
1da177e4 LT |
882 | { |
883 | struct netlink_broadcast_data info; | |
884 | struct hlist_node *node; | |
885 | struct sock *sk; | |
886 | ||
887 | skb = netlink_trim(skb, allocation); | |
888 | ||
889 | info.exclude_sk = ssk; | |
890 | info.pid = pid; | |
891 | info.group = group; | |
892 | info.failure = 0; | |
893 | info.congested = 0; | |
894 | info.delivered = 0; | |
895 | info.allocation = allocation; | |
896 | info.skb = skb; | |
897 | info.skb2 = NULL; | |
898 | ||
899 | /* While we sleep in clone, do not allow to change socket list */ | |
900 | ||
901 | netlink_lock_table(); | |
902 | ||
903 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) | |
904 | do_one_broadcast(sk, &info); | |
905 | ||
aa1c6a6f TC |
906 | kfree_skb(skb); |
907 | ||
1da177e4 LT |
908 | netlink_unlock_table(); |
909 | ||
910 | if (info.skb2) | |
911 | kfree_skb(info.skb2); | |
1da177e4 LT |
912 | |
913 | if (info.delivered) { | |
914 | if (info.congested && (allocation & __GFP_WAIT)) | |
915 | yield(); | |
916 | return 0; | |
917 | } | |
918 | if (info.failure) | |
919 | return -ENOBUFS; | |
920 | return -ESRCH; | |
921 | } | |
922 | ||
923 | struct netlink_set_err_data { | |
924 | struct sock *exclude_sk; | |
925 | u32 pid; | |
926 | u32 group; | |
927 | int code; | |
928 | }; | |
929 | ||
930 | static inline int do_one_set_err(struct sock *sk, | |
931 | struct netlink_set_err_data *p) | |
932 | { | |
933 | struct netlink_sock *nlk = nlk_sk(sk); | |
934 | ||
935 | if (sk == p->exclude_sk) | |
936 | goto out; | |
937 | ||
f7fa9b10 PM |
938 | if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups || |
939 | !test_bit(p->group - 1, nlk->groups)) | |
1da177e4 LT |
940 | goto out; |
941 | ||
942 | sk->sk_err = p->code; | |
943 | sk->sk_error_report(sk); | |
944 | out: | |
945 | return 0; | |
946 | } | |
947 | ||
948 | void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code) | |
949 | { | |
950 | struct netlink_set_err_data info; | |
951 | struct hlist_node *node; | |
952 | struct sock *sk; | |
953 | ||
954 | info.exclude_sk = ssk; | |
955 | info.pid = pid; | |
956 | info.group = group; | |
957 | info.code = code; | |
958 | ||
959 | read_lock(&nl_table_lock); | |
960 | ||
961 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) | |
962 | do_one_set_err(sk, &info); | |
963 | ||
964 | read_unlock(&nl_table_lock); | |
965 | } | |
966 | ||
9a4595bc PM |
967 | static int netlink_setsockopt(struct socket *sock, int level, int optname, |
968 | char __user *optval, int optlen) | |
969 | { | |
970 | struct sock *sk = sock->sk; | |
971 | struct netlink_sock *nlk = nlk_sk(sk); | |
972 | int val = 0, err; | |
973 | ||
974 | if (level != SOL_NETLINK) | |
975 | return -ENOPROTOOPT; | |
976 | ||
977 | if (optlen >= sizeof(int) && | |
978 | get_user(val, (int __user *)optval)) | |
979 | return -EFAULT; | |
980 | ||
981 | switch (optname) { | |
982 | case NETLINK_PKTINFO: | |
983 | if (val) | |
984 | nlk->flags |= NETLINK_RECV_PKTINFO; | |
985 | else | |
986 | nlk->flags &= ~NETLINK_RECV_PKTINFO; | |
987 | err = 0; | |
988 | break; | |
989 | case NETLINK_ADD_MEMBERSHIP: | |
990 | case NETLINK_DROP_MEMBERSHIP: { | |
991 | unsigned int subscriptions; | |
992 | int old, new = optname == NETLINK_ADD_MEMBERSHIP ? 1 : 0; | |
993 | ||
994 | if (!netlink_capable(sock, NL_NONROOT_RECV)) | |
995 | return -EPERM; | |
513c2500 PM |
996 | if (nlk->groups == NULL) { |
997 | err = netlink_alloc_groups(sk); | |
998 | if (err) | |
999 | return err; | |
1000 | } | |
9a4595bc PM |
1001 | if (!val || val - 1 >= nlk->ngroups) |
1002 | return -EINVAL; | |
1003 | netlink_table_grab(); | |
1004 | old = test_bit(val - 1, nlk->groups); | |
1005 | subscriptions = nlk->subscriptions - old + new; | |
1006 | if (new) | |
1007 | __set_bit(val - 1, nlk->groups); | |
1008 | else | |
1009 | __clear_bit(val - 1, nlk->groups); | |
1010 | netlink_update_subscriptions(sk, subscriptions); | |
1011 | netlink_table_ungrab(); | |
1012 | err = 0; | |
1013 | break; | |
1014 | } | |
1015 | default: | |
1016 | err = -ENOPROTOOPT; | |
1017 | } | |
1018 | return err; | |
1019 | } | |
1020 | ||
1021 | static int netlink_getsockopt(struct socket *sock, int level, int optname, | |
1022 | char __user *optval, int __user *optlen) | |
1023 | { | |
1024 | struct sock *sk = sock->sk; | |
1025 | struct netlink_sock *nlk = nlk_sk(sk); | |
1026 | int len, val, err; | |
1027 | ||
1028 | if (level != SOL_NETLINK) | |
1029 | return -ENOPROTOOPT; | |
1030 | ||
1031 | if (get_user(len, optlen)) | |
1032 | return -EFAULT; | |
1033 | if (len < 0) | |
1034 | return -EINVAL; | |
1035 | ||
1036 | switch (optname) { | |
1037 | case NETLINK_PKTINFO: | |
1038 | if (len < sizeof(int)) | |
1039 | return -EINVAL; | |
1040 | len = sizeof(int); | |
1041 | val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0; | |
1042 | put_user(len, optlen); | |
1043 | put_user(val, optval); | |
1044 | err = 0; | |
1045 | break; | |
1046 | default: | |
1047 | err = -ENOPROTOOPT; | |
1048 | } | |
1049 | return err; | |
1050 | } | |
1051 | ||
1052 | static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
1053 | { | |
1054 | struct nl_pktinfo info; | |
1055 | ||
1056 | info.group = NETLINK_CB(skb).dst_group; | |
1057 | put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); | |
1058 | } | |
1059 | ||
1da177e4 LT |
1060 | static inline void netlink_rcv_wake(struct sock *sk) |
1061 | { | |
1062 | struct netlink_sock *nlk = nlk_sk(sk); | |
1063 | ||
b03efcfb | 1064 | if (skb_queue_empty(&sk->sk_receive_queue)) |
1da177e4 LT |
1065 | clear_bit(0, &nlk->state); |
1066 | if (!test_bit(0, &nlk->state)) | |
1067 | wake_up_interruptible(&nlk->wait); | |
1068 | } | |
1069 | ||
1070 | static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, | |
1071 | struct msghdr *msg, size_t len) | |
1072 | { | |
1073 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | |
1074 | struct sock *sk = sock->sk; | |
1075 | struct netlink_sock *nlk = nlk_sk(sk); | |
1076 | struct sockaddr_nl *addr=msg->msg_name; | |
1077 | u32 dst_pid; | |
d629b836 | 1078 | u32 dst_group; |
1da177e4 LT |
1079 | struct sk_buff *skb; |
1080 | int err; | |
1081 | struct scm_cookie scm; | |
1082 | ||
1083 | if (msg->msg_flags&MSG_OOB) | |
1084 | return -EOPNOTSUPP; | |
1085 | ||
1086 | if (NULL == siocb->scm) | |
1087 | siocb->scm = &scm; | |
1088 | err = scm_send(sock, msg, siocb->scm); | |
1089 | if (err < 0) | |
1090 | return err; | |
1091 | ||
1092 | if (msg->msg_namelen) { | |
1093 | if (addr->nl_family != AF_NETLINK) | |
1094 | return -EINVAL; | |
1095 | dst_pid = addr->nl_pid; | |
d629b836 PM |
1096 | dst_group = ffs(addr->nl_groups); |
1097 | if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND)) | |
1da177e4 LT |
1098 | return -EPERM; |
1099 | } else { | |
1100 | dst_pid = nlk->dst_pid; | |
d629b836 | 1101 | dst_group = nlk->dst_group; |
1da177e4 LT |
1102 | } |
1103 | ||
1104 | if (!nlk->pid) { | |
1105 | err = netlink_autobind(sock); | |
1106 | if (err) | |
1107 | goto out; | |
1108 | } | |
1109 | ||
1110 | err = -EMSGSIZE; | |
1111 | if (len > sk->sk_sndbuf - 32) | |
1112 | goto out; | |
1113 | err = -ENOBUFS; | |
1114 | skb = alloc_skb(len, GFP_KERNEL); | |
1115 | if (skb==NULL) | |
1116 | goto out; | |
1117 | ||
1118 | NETLINK_CB(skb).pid = nlk->pid; | |
1da177e4 | 1119 | NETLINK_CB(skb).dst_pid = dst_pid; |
d629b836 | 1120 | NETLINK_CB(skb).dst_group = dst_group; |
c94c257c | 1121 | NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context); |
1da177e4 LT |
1122 | memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred)); |
1123 | ||
1124 | /* What can I do? Netlink is asynchronous, so that | |
1125 | we will have to save current capabilities to | |
1126 | check them, when this message will be delivered | |
1127 | to corresponding kernel module. --ANK (980802) | |
1128 | */ | |
1129 | ||
1130 | err = -EFAULT; | |
1131 | if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) { | |
1132 | kfree_skb(skb); | |
1133 | goto out; | |
1134 | } | |
1135 | ||
1136 | err = security_netlink_send(sk, skb); | |
1137 | if (err) { | |
1138 | kfree_skb(skb); | |
1139 | goto out; | |
1140 | } | |
1141 | ||
d629b836 | 1142 | if (dst_group) { |
1da177e4 | 1143 | atomic_inc(&skb->users); |
d629b836 | 1144 | netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL); |
1da177e4 LT |
1145 | } |
1146 | err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT); | |
1147 | ||
1148 | out: | |
1149 | return err; | |
1150 | } | |
1151 | ||
1152 | static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, | |
1153 | struct msghdr *msg, size_t len, | |
1154 | int flags) | |
1155 | { | |
1156 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | |
1157 | struct scm_cookie scm; | |
1158 | struct sock *sk = sock->sk; | |
1159 | struct netlink_sock *nlk = nlk_sk(sk); | |
1160 | int noblock = flags&MSG_DONTWAIT; | |
1161 | size_t copied; | |
1162 | struct sk_buff *skb; | |
1163 | int err; | |
1164 | ||
1165 | if (flags&MSG_OOB) | |
1166 | return -EOPNOTSUPP; | |
1167 | ||
1168 | copied = 0; | |
1169 | ||
1170 | skb = skb_recv_datagram(sk,flags,noblock,&err); | |
1171 | if (skb==NULL) | |
1172 | goto out; | |
1173 | ||
1174 | msg->msg_namelen = 0; | |
1175 | ||
1176 | copied = skb->len; | |
1177 | if (len < copied) { | |
1178 | msg->msg_flags |= MSG_TRUNC; | |
1179 | copied = len; | |
1180 | } | |
1181 | ||
1182 | skb->h.raw = skb->data; | |
1183 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | |
1184 | ||
1185 | if (msg->msg_name) { | |
1186 | struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name; | |
1187 | addr->nl_family = AF_NETLINK; | |
1188 | addr->nl_pad = 0; | |
1189 | addr->nl_pid = NETLINK_CB(skb).pid; | |
d629b836 | 1190 | addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); |
1da177e4 LT |
1191 | msg->msg_namelen = sizeof(*addr); |
1192 | } | |
1193 | ||
1194 | if (NULL == siocb->scm) { | |
1195 | memset(&scm, 0, sizeof(scm)); | |
1196 | siocb->scm = &scm; | |
1197 | } | |
1198 | siocb->scm->creds = *NETLINK_CREDS(skb); | |
1199 | skb_free_datagram(sk, skb); | |
1200 | ||
1201 | if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) | |
1202 | netlink_dump(sk); | |
1203 | ||
1204 | scm_recv(sock, msg, siocb->scm, flags); | |
9a4595bc PM |
1205 | if (nlk->flags & NETLINK_RECV_PKTINFO) |
1206 | netlink_cmsg_recv_pktinfo(msg, skb); | |
1da177e4 LT |
1207 | |
1208 | out: | |
1209 | netlink_rcv_wake(sk); | |
1210 | return err ? : copied; | |
1211 | } | |
1212 | ||
1213 | static void netlink_data_ready(struct sock *sk, int len) | |
1214 | { | |
1215 | struct netlink_sock *nlk = nlk_sk(sk); | |
1216 | ||
1217 | if (nlk->data_ready) | |
1218 | nlk->data_ready(sk, len); | |
1219 | netlink_rcv_wake(sk); | |
1220 | } | |
1221 | ||
1222 | /* | |
1223 | * We export these functions to other modules. They provide a | |
1224 | * complete set of kernel non-blocking support for message | |
1225 | * queueing. | |
1226 | */ | |
1227 | ||
1228 | struct sock * | |
06628607 PM |
1229 | netlink_kernel_create(int unit, unsigned int groups, |
1230 | void (*input)(struct sock *sk, int len), | |
1231 | struct module *module) | |
1da177e4 LT |
1232 | { |
1233 | struct socket *sock; | |
1234 | struct sock *sk; | |
77247bbb | 1235 | struct netlink_sock *nlk; |
1da177e4 LT |
1236 | |
1237 | if (!nl_table) | |
1238 | return NULL; | |
1239 | ||
1240 | if (unit<0 || unit>=MAX_LINKS) | |
1241 | return NULL; | |
1242 | ||
1243 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) | |
1244 | return NULL; | |
1245 | ||
ab33a171 | 1246 | if (__netlink_create(sock, unit) < 0) |
77247bbb | 1247 | goto out_sock_release; |
4fdb3bb7 | 1248 | |
1da177e4 LT |
1249 | sk = sock->sk; |
1250 | sk->sk_data_ready = netlink_data_ready; | |
1251 | if (input) | |
1252 | nlk_sk(sk)->data_ready = input; | |
1253 | ||
77247bbb PM |
1254 | if (netlink_insert(sk, 0)) |
1255 | goto out_sock_release; | |
4fdb3bb7 | 1256 | |
77247bbb PM |
1257 | nlk = nlk_sk(sk); |
1258 | nlk->flags |= NETLINK_KERNEL_SOCKET; | |
4fdb3bb7 | 1259 | |
4fdb3bb7 | 1260 | netlink_table_grab(); |
06628607 | 1261 | nl_table[unit].groups = groups < 32 ? 32 : groups; |
77247bbb | 1262 | nl_table[unit].module = module; |
ab33a171 | 1263 | nl_table[unit].registered = 1; |
4fdb3bb7 | 1264 | netlink_table_ungrab(); |
77247bbb PM |
1265 | |
1266 | return sk; | |
1267 | ||
4fdb3bb7 HW |
1268 | out_sock_release: |
1269 | sock_release(sock); | |
77247bbb | 1270 | return NULL; |
1da177e4 LT |
1271 | } |
1272 | ||
1273 | void netlink_set_nonroot(int protocol, unsigned int flags) | |
1274 | { | |
1275 | if ((unsigned int)protocol < MAX_LINKS) | |
1276 | nl_table[protocol].nl_nonroot = flags; | |
1277 | } | |
1278 | ||
1279 | static void netlink_destroy_callback(struct netlink_callback *cb) | |
1280 | { | |
1281 | if (cb->skb) | |
1282 | kfree_skb(cb->skb); | |
1283 | kfree(cb); | |
1284 | } | |
1285 | ||
1286 | /* | |
1287 | * It looks a bit ugly. | |
1288 | * It would be better to create kernel thread. | |
1289 | */ | |
1290 | ||
1291 | static int netlink_dump(struct sock *sk) | |
1292 | { | |
1293 | struct netlink_sock *nlk = nlk_sk(sk); | |
1294 | struct netlink_callback *cb; | |
1295 | struct sk_buff *skb; | |
1296 | struct nlmsghdr *nlh; | |
1297 | int len; | |
1298 | ||
1299 | skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL); | |
1300 | if (!skb) | |
1301 | return -ENOBUFS; | |
1302 | ||
1303 | spin_lock(&nlk->cb_lock); | |
1304 | ||
1305 | cb = nlk->cb; | |
1306 | if (cb == NULL) { | |
1307 | spin_unlock(&nlk->cb_lock); | |
1308 | kfree_skb(skb); | |
1309 | return -EINVAL; | |
1310 | } | |
1311 | ||
1312 | len = cb->dump(skb, cb); | |
1313 | ||
1314 | if (len > 0) { | |
1315 | spin_unlock(&nlk->cb_lock); | |
1316 | skb_queue_tail(&sk->sk_receive_queue, skb); | |
1317 | sk->sk_data_ready(sk, len); | |
1318 | return 0; | |
1319 | } | |
1320 | ||
1797754e | 1321 | nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI); |
1da177e4 LT |
1322 | memcpy(NLMSG_DATA(nlh), &len, sizeof(len)); |
1323 | skb_queue_tail(&sk->sk_receive_queue, skb); | |
1324 | sk->sk_data_ready(sk, skb->len); | |
1325 | ||
a8f74b22 TG |
1326 | if (cb->done) |
1327 | cb->done(cb); | |
1da177e4 LT |
1328 | nlk->cb = NULL; |
1329 | spin_unlock(&nlk->cb_lock); | |
1330 | ||
1331 | netlink_destroy_callback(cb); | |
1da177e4 | 1332 | return 0; |
1797754e TG |
1333 | |
1334 | nlmsg_failure: | |
1335 | return -ENOBUFS; | |
1da177e4 LT |
1336 | } |
1337 | ||
1338 | int netlink_dump_start(struct sock *ssk, struct sk_buff *skb, | |
1339 | struct nlmsghdr *nlh, | |
1340 | int (*dump)(struct sk_buff *skb, struct netlink_callback*), | |
1341 | int (*done)(struct netlink_callback*)) | |
1342 | { | |
1343 | struct netlink_callback *cb; | |
1344 | struct sock *sk; | |
1345 | struct netlink_sock *nlk; | |
1346 | ||
1347 | cb = kmalloc(sizeof(*cb), GFP_KERNEL); | |
1348 | if (cb == NULL) | |
1349 | return -ENOBUFS; | |
1350 | ||
1351 | memset(cb, 0, sizeof(*cb)); | |
1352 | cb->dump = dump; | |
1353 | cb->done = done; | |
1354 | cb->nlh = nlh; | |
1355 | atomic_inc(&skb->users); | |
1356 | cb->skb = skb; | |
1357 | ||
1358 | sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid); | |
1359 | if (sk == NULL) { | |
1360 | netlink_destroy_callback(cb); | |
1361 | return -ECONNREFUSED; | |
1362 | } | |
1363 | nlk = nlk_sk(sk); | |
1364 | /* A dump is in progress... */ | |
1365 | spin_lock(&nlk->cb_lock); | |
1366 | if (nlk->cb) { | |
1367 | spin_unlock(&nlk->cb_lock); | |
1368 | netlink_destroy_callback(cb); | |
1369 | sock_put(sk); | |
1370 | return -EBUSY; | |
1371 | } | |
1372 | nlk->cb = cb; | |
1da177e4 LT |
1373 | spin_unlock(&nlk->cb_lock); |
1374 | ||
1375 | netlink_dump(sk); | |
1376 | sock_put(sk); | |
1377 | return 0; | |
1378 | } | |
1379 | ||
1380 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err) | |
1381 | { | |
1382 | struct sk_buff *skb; | |
1383 | struct nlmsghdr *rep; | |
1384 | struct nlmsgerr *errmsg; | |
1385 | int size; | |
1386 | ||
1387 | if (err == 0) | |
1388 | size = NLMSG_SPACE(sizeof(struct nlmsgerr)); | |
1389 | else | |
1390 | size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len)); | |
1391 | ||
1392 | skb = alloc_skb(size, GFP_KERNEL); | |
1393 | if (!skb) { | |
1394 | struct sock *sk; | |
1395 | ||
1396 | sk = netlink_lookup(in_skb->sk->sk_protocol, | |
1397 | NETLINK_CB(in_skb).pid); | |
1398 | if (sk) { | |
1399 | sk->sk_err = ENOBUFS; | |
1400 | sk->sk_error_report(sk); | |
1401 | sock_put(sk); | |
1402 | } | |
1403 | return; | |
1404 | } | |
1405 | ||
1406 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, | |
1797754e | 1407 | NLMSG_ERROR, sizeof(struct nlmsgerr), 0); |
1da177e4 LT |
1408 | errmsg = NLMSG_DATA(rep); |
1409 | errmsg->error = err; | |
1410 | memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr)); | |
1411 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); | |
1412 | } | |
1413 | ||
1414 | ||
1415 | #ifdef CONFIG_PROC_FS | |
1416 | struct nl_seq_iter { | |
1417 | int link; | |
1418 | int hash_idx; | |
1419 | }; | |
1420 | ||
1421 | static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos) | |
1422 | { | |
1423 | struct nl_seq_iter *iter = seq->private; | |
1424 | int i, j; | |
1425 | struct sock *s; | |
1426 | struct hlist_node *node; | |
1427 | loff_t off = 0; | |
1428 | ||
1429 | for (i=0; i<MAX_LINKS; i++) { | |
1430 | struct nl_pid_hash *hash = &nl_table[i].hash; | |
1431 | ||
1432 | for (j = 0; j <= hash->mask; j++) { | |
1433 | sk_for_each(s, node, &hash->table[j]) { | |
1434 | if (off == pos) { | |
1435 | iter->link = i; | |
1436 | iter->hash_idx = j; | |
1437 | return s; | |
1438 | } | |
1439 | ++off; | |
1440 | } | |
1441 | } | |
1442 | } | |
1443 | return NULL; | |
1444 | } | |
1445 | ||
1446 | static void *netlink_seq_start(struct seq_file *seq, loff_t *pos) | |
1447 | { | |
1448 | read_lock(&nl_table_lock); | |
1449 | return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN; | |
1450 | } | |
1451 | ||
1452 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
1453 | { | |
1454 | struct sock *s; | |
1455 | struct nl_seq_iter *iter; | |
1456 | int i, j; | |
1457 | ||
1458 | ++*pos; | |
1459 | ||
1460 | if (v == SEQ_START_TOKEN) | |
1461 | return netlink_seq_socket_idx(seq, 0); | |
1462 | ||
1463 | s = sk_next(v); | |
1464 | if (s) | |
1465 | return s; | |
1466 | ||
1467 | iter = seq->private; | |
1468 | i = iter->link; | |
1469 | j = iter->hash_idx + 1; | |
1470 | ||
1471 | do { | |
1472 | struct nl_pid_hash *hash = &nl_table[i].hash; | |
1473 | ||
1474 | for (; j <= hash->mask; j++) { | |
1475 | s = sk_head(&hash->table[j]); | |
1476 | if (s) { | |
1477 | iter->link = i; | |
1478 | iter->hash_idx = j; | |
1479 | return s; | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | j = 0; | |
1484 | } while (++i < MAX_LINKS); | |
1485 | ||
1486 | return NULL; | |
1487 | } | |
1488 | ||
1489 | static void netlink_seq_stop(struct seq_file *seq, void *v) | |
1490 | { | |
1491 | read_unlock(&nl_table_lock); | |
1492 | } | |
1493 | ||
1494 | ||
1495 | static int netlink_seq_show(struct seq_file *seq, void *v) | |
1496 | { | |
1497 | if (v == SEQ_START_TOKEN) | |
1498 | seq_puts(seq, | |
1499 | "sk Eth Pid Groups " | |
1500 | "Rmem Wmem Dump Locks\n"); | |
1501 | else { | |
1502 | struct sock *s = v; | |
1503 | struct netlink_sock *nlk = nlk_sk(s); | |
1504 | ||
1505 | seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n", | |
1506 | s, | |
1507 | s->sk_protocol, | |
1508 | nlk->pid, | |
513c2500 | 1509 | nlk->groups ? (u32)nlk->groups[0] : 0, |
1da177e4 LT |
1510 | atomic_read(&s->sk_rmem_alloc), |
1511 | atomic_read(&s->sk_wmem_alloc), | |
1512 | nlk->cb, | |
1513 | atomic_read(&s->sk_refcnt) | |
1514 | ); | |
1515 | ||
1516 | } | |
1517 | return 0; | |
1518 | } | |
1519 | ||
1520 | static struct seq_operations netlink_seq_ops = { | |
1521 | .start = netlink_seq_start, | |
1522 | .next = netlink_seq_next, | |
1523 | .stop = netlink_seq_stop, | |
1524 | .show = netlink_seq_show, | |
1525 | }; | |
1526 | ||
1527 | ||
1528 | static int netlink_seq_open(struct inode *inode, struct file *file) | |
1529 | { | |
1530 | struct seq_file *seq; | |
1531 | struct nl_seq_iter *iter; | |
1532 | int err; | |
1533 | ||
1534 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); | |
1535 | if (!iter) | |
1536 | return -ENOMEM; | |
1537 | ||
1538 | err = seq_open(file, &netlink_seq_ops); | |
1539 | if (err) { | |
1540 | kfree(iter); | |
1541 | return err; | |
1542 | } | |
1543 | ||
1544 | memset(iter, 0, sizeof(*iter)); | |
1545 | seq = file->private_data; | |
1546 | seq->private = iter; | |
1547 | return 0; | |
1548 | } | |
1549 | ||
1550 | static struct file_operations netlink_seq_fops = { | |
1551 | .owner = THIS_MODULE, | |
1552 | .open = netlink_seq_open, | |
1553 | .read = seq_read, | |
1554 | .llseek = seq_lseek, | |
1555 | .release = seq_release_private, | |
1556 | }; | |
1557 | ||
1558 | #endif | |
1559 | ||
1560 | int netlink_register_notifier(struct notifier_block *nb) | |
1561 | { | |
1562 | return notifier_chain_register(&netlink_chain, nb); | |
1563 | } | |
1564 | ||
1565 | int netlink_unregister_notifier(struct notifier_block *nb) | |
1566 | { | |
1567 | return notifier_chain_unregister(&netlink_chain, nb); | |
1568 | } | |
1569 | ||
1570 | static struct proto_ops netlink_ops = { | |
1571 | .family = PF_NETLINK, | |
1572 | .owner = THIS_MODULE, | |
1573 | .release = netlink_release, | |
1574 | .bind = netlink_bind, | |
1575 | .connect = netlink_connect, | |
1576 | .socketpair = sock_no_socketpair, | |
1577 | .accept = sock_no_accept, | |
1578 | .getname = netlink_getname, | |
1579 | .poll = datagram_poll, | |
1580 | .ioctl = sock_no_ioctl, | |
1581 | .listen = sock_no_listen, | |
1582 | .shutdown = sock_no_shutdown, | |
9a4595bc PM |
1583 | .setsockopt = netlink_setsockopt, |
1584 | .getsockopt = netlink_getsockopt, | |
1da177e4 LT |
1585 | .sendmsg = netlink_sendmsg, |
1586 | .recvmsg = netlink_recvmsg, | |
1587 | .mmap = sock_no_mmap, | |
1588 | .sendpage = sock_no_sendpage, | |
1589 | }; | |
1590 | ||
1591 | static struct net_proto_family netlink_family_ops = { | |
1592 | .family = PF_NETLINK, | |
1593 | .create = netlink_create, | |
1594 | .owner = THIS_MODULE, /* for consistency 8) */ | |
1595 | }; | |
1596 | ||
1597 | extern void netlink_skb_parms_too_large(void); | |
1598 | ||
1599 | static int __init netlink_proto_init(void) | |
1600 | { | |
1601 | struct sk_buff *dummy_skb; | |
1602 | int i; | |
1603 | unsigned long max; | |
1604 | unsigned int order; | |
1605 | int err = proto_register(&netlink_proto, 0); | |
1606 | ||
1607 | if (err != 0) | |
1608 | goto out; | |
1609 | ||
1610 | if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)) | |
1611 | netlink_skb_parms_too_large(); | |
1612 | ||
1613 | nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL); | |
1614 | if (!nl_table) { | |
1615 | enomem: | |
1616 | printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n"); | |
1617 | return -ENOMEM; | |
1618 | } | |
1619 | ||
1620 | memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS); | |
1621 | ||
1622 | if (num_physpages >= (128 * 1024)) | |
1623 | max = num_physpages >> (21 - PAGE_SHIFT); | |
1624 | else | |
1625 | max = num_physpages >> (23 - PAGE_SHIFT); | |
1626 | ||
1627 | order = get_bitmask_order(max) - 1 + PAGE_SHIFT; | |
1628 | max = (1UL << order) / sizeof(struct hlist_head); | |
1629 | order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1; | |
1630 | ||
1631 | for (i = 0; i < MAX_LINKS; i++) { | |
1632 | struct nl_pid_hash *hash = &nl_table[i].hash; | |
1633 | ||
1634 | hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table)); | |
1635 | if (!hash->table) { | |
1636 | while (i-- > 0) | |
1637 | nl_pid_hash_free(nl_table[i].hash.table, | |
1638 | 1 * sizeof(*hash->table)); | |
1639 | kfree(nl_table); | |
1640 | goto enomem; | |
1641 | } | |
1642 | memset(hash->table, 0, 1 * sizeof(*hash->table)); | |
1643 | hash->max_shift = order; | |
1644 | hash->shift = 0; | |
1645 | hash->mask = 0; | |
1646 | hash->rehash_time = jiffies; | |
1647 | } | |
1648 | ||
1649 | sock_register(&netlink_family_ops); | |
1650 | #ifdef CONFIG_PROC_FS | |
1651 | proc_net_fops_create("netlink", 0, &netlink_seq_fops); | |
1652 | #endif | |
1653 | /* The netlink device handler may be needed early. */ | |
1654 | rtnetlink_init(); | |
1655 | out: | |
1656 | return err; | |
1657 | } | |
1658 | ||
1da177e4 | 1659 | core_initcall(netlink_proto_init); |
1da177e4 LT |
1660 | |
1661 | EXPORT_SYMBOL(netlink_ack); | |
1662 | EXPORT_SYMBOL(netlink_broadcast); | |
1663 | EXPORT_SYMBOL(netlink_dump_start); | |
1664 | EXPORT_SYMBOL(netlink_kernel_create); | |
1665 | EXPORT_SYMBOL(netlink_register_notifier); | |
1666 | EXPORT_SYMBOL(netlink_set_err); | |
1667 | EXPORT_SYMBOL(netlink_set_nonroot); | |
1668 | EXPORT_SYMBOL(netlink_unicast); | |
1669 | EXPORT_SYMBOL(netlink_unregister_notifier); | |
1670 |