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