Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * NETLINK Kernel-user communication protocol. | |
3 | * | |
113aa838 | 4 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 | 5 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
cd1df525 | 6 | * Patrick McHardy <kaber@trash.net> |
1da177e4 LT |
7 | * |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
746fac4d | 12 | * |
1da177e4 LT |
13 | * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith |
14 | * added netlink_proto_exit | |
15 | * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br> | |
16 | * use nlk_sk, as sk->protinfo is on a diet 8) | |
4fdb3bb7 HW |
17 | * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org> |
18 | * - inc module use count of module that owns | |
19 | * the kernel socket in case userspace opens | |
20 | * socket of same protocol | |
21 | * - remove all module support, since netlink is | |
22 | * mandatory if CONFIG_NET=y these days | |
1da177e4 LT |
23 | */ |
24 | ||
1da177e4 LT |
25 | #include <linux/module.h> |
26 | ||
4fc268d2 | 27 | #include <linux/capability.h> |
1da177e4 LT |
28 | #include <linux/kernel.h> |
29 | #include <linux/init.h> | |
1da177e4 LT |
30 | #include <linux/signal.h> |
31 | #include <linux/sched.h> | |
32 | #include <linux/errno.h> | |
33 | #include <linux/string.h> | |
34 | #include <linux/stat.h> | |
35 | #include <linux/socket.h> | |
36 | #include <linux/un.h> | |
37 | #include <linux/fcntl.h> | |
38 | #include <linux/termios.h> | |
39 | #include <linux/sockios.h> | |
40 | #include <linux/net.h> | |
41 | #include <linux/fs.h> | |
42 | #include <linux/slab.h> | |
43 | #include <asm/uaccess.h> | |
44 | #include <linux/skbuff.h> | |
45 | #include <linux/netdevice.h> | |
46 | #include <linux/rtnetlink.h> | |
47 | #include <linux/proc_fs.h> | |
48 | #include <linux/seq_file.h> | |
1da177e4 LT |
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 | 57 | #include <linux/audit.h> |
af65bdfc | 58 | #include <linux/mutex.h> |
ccdfcc39 | 59 | #include <linux/vmalloc.h> |
9652e931 | 60 | #include <asm/cacheflush.h> |
54e0f520 | 61 | |
457c4cbc | 62 | #include <net/net_namespace.h> |
1da177e4 LT |
63 | #include <net/sock.h> |
64 | #include <net/scm.h> | |
82ace47a | 65 | #include <net/netlink.h> |
1da177e4 | 66 | |
0f29c768 | 67 | #include "af_netlink.h" |
1da177e4 | 68 | |
5c398dc8 ED |
69 | struct listeners { |
70 | struct rcu_head rcu; | |
71 | unsigned long masks[0]; | |
6c04bb18 JB |
72 | }; |
73 | ||
cd967e05 PM |
74 | /* state bits */ |
75 | #define NETLINK_CONGESTED 0x0 | |
76 | ||
77 | /* flags */ | |
77247bbb | 78 | #define NETLINK_KERNEL_SOCKET 0x1 |
9a4595bc | 79 | #define NETLINK_RECV_PKTINFO 0x2 |
be0c22a4 | 80 | #define NETLINK_BROADCAST_SEND_ERROR 0x4 |
38938bfe | 81 | #define NETLINK_RECV_NO_ENOBUFS 0x8 |
77247bbb | 82 | |
035c4c16 | 83 | static inline int netlink_is_kernel(struct sock *sk) |
aed81560 DL |
84 | { |
85 | return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET; | |
86 | } | |
87 | ||
0f29c768 AV |
88 | struct netlink_table *nl_table; |
89 | EXPORT_SYMBOL_GPL(nl_table); | |
1da177e4 LT |
90 | |
91 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); | |
92 | ||
93 | static int netlink_dump(struct sock *sk); | |
9652e931 | 94 | static void netlink_skb_destructor(struct sk_buff *skb); |
1da177e4 | 95 | |
0f29c768 AV |
96 | DEFINE_RWLOCK(nl_table_lock); |
97 | EXPORT_SYMBOL_GPL(nl_table_lock); | |
1da177e4 LT |
98 | static atomic_t nl_table_users = ATOMIC_INIT(0); |
99 | ||
6d772ac5 ED |
100 | #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock)); |
101 | ||
e041c683 | 102 | static ATOMIC_NOTIFIER_HEAD(netlink_chain); |
1da177e4 | 103 | |
b57ef81f | 104 | static inline u32 netlink_group_mask(u32 group) |
d629b836 PM |
105 | { |
106 | return group ? 1 << (group - 1) : 0; | |
107 | } | |
108 | ||
15e47304 | 109 | static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid) |
1da177e4 | 110 | { |
15e47304 | 111 | return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask]; |
1da177e4 LT |
112 | } |
113 | ||
cd1df525 PM |
114 | static void netlink_overrun(struct sock *sk) |
115 | { | |
116 | struct netlink_sock *nlk = nlk_sk(sk); | |
117 | ||
118 | if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) { | |
119 | if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) { | |
120 | sk->sk_err = ENOBUFS; | |
121 | sk->sk_error_report(sk); | |
122 | } | |
123 | } | |
124 | atomic_inc(&sk->sk_drops); | |
125 | } | |
126 | ||
127 | static void netlink_rcv_wake(struct sock *sk) | |
128 | { | |
129 | struct netlink_sock *nlk = nlk_sk(sk); | |
130 | ||
131 | if (skb_queue_empty(&sk->sk_receive_queue)) | |
132 | clear_bit(NETLINK_CONGESTED, &nlk->state); | |
133 | if (!test_bit(NETLINK_CONGESTED, &nlk->state)) | |
134 | wake_up_interruptible(&nlk->wait); | |
135 | } | |
136 | ||
ccdfcc39 | 137 | #ifdef CONFIG_NETLINK_MMAP |
9652e931 PM |
138 | static bool netlink_skb_is_mmaped(const struct sk_buff *skb) |
139 | { | |
140 | return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED; | |
141 | } | |
142 | ||
f9c22888 PM |
143 | static bool netlink_rx_is_mmaped(struct sock *sk) |
144 | { | |
145 | return nlk_sk(sk)->rx_ring.pg_vec != NULL; | |
146 | } | |
147 | ||
5fd96123 PM |
148 | static bool netlink_tx_is_mmaped(struct sock *sk) |
149 | { | |
150 | return nlk_sk(sk)->tx_ring.pg_vec != NULL; | |
151 | } | |
152 | ||
ccdfcc39 PM |
153 | static __pure struct page *pgvec_to_page(const void *addr) |
154 | { | |
155 | if (is_vmalloc_addr(addr)) | |
156 | return vmalloc_to_page(addr); | |
157 | else | |
158 | return virt_to_page(addr); | |
159 | } | |
160 | ||
161 | static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len) | |
162 | { | |
163 | unsigned int i; | |
164 | ||
165 | for (i = 0; i < len; i++) { | |
166 | if (pg_vec[i] != NULL) { | |
167 | if (is_vmalloc_addr(pg_vec[i])) | |
168 | vfree(pg_vec[i]); | |
169 | else | |
170 | free_pages((unsigned long)pg_vec[i], order); | |
171 | } | |
172 | } | |
173 | kfree(pg_vec); | |
174 | } | |
175 | ||
176 | static void *alloc_one_pg_vec_page(unsigned long order) | |
177 | { | |
178 | void *buffer; | |
179 | gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO | | |
180 | __GFP_NOWARN | __GFP_NORETRY; | |
181 | ||
182 | buffer = (void *)__get_free_pages(gfp_flags, order); | |
183 | if (buffer != NULL) | |
184 | return buffer; | |
185 | ||
186 | buffer = vzalloc((1 << order) * PAGE_SIZE); | |
187 | if (buffer != NULL) | |
188 | return buffer; | |
189 | ||
190 | gfp_flags &= ~__GFP_NORETRY; | |
191 | return (void *)__get_free_pages(gfp_flags, order); | |
192 | } | |
193 | ||
194 | static void **alloc_pg_vec(struct netlink_sock *nlk, | |
195 | struct nl_mmap_req *req, unsigned int order) | |
196 | { | |
197 | unsigned int block_nr = req->nm_block_nr; | |
198 | unsigned int i; | |
199 | void **pg_vec, *ptr; | |
200 | ||
201 | pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL); | |
202 | if (pg_vec == NULL) | |
203 | return NULL; | |
204 | ||
205 | for (i = 0; i < block_nr; i++) { | |
206 | pg_vec[i] = ptr = alloc_one_pg_vec_page(order); | |
207 | if (pg_vec[i] == NULL) | |
208 | goto err1; | |
209 | } | |
210 | ||
211 | return pg_vec; | |
212 | err1: | |
213 | free_pg_vec(pg_vec, order, block_nr); | |
214 | return NULL; | |
215 | } | |
216 | ||
3ebe377b FW |
217 | |
218 | static void | |
219 | __netlink_set_ring(struct sock *sk, struct nl_mmap_req *req, bool tx_ring, void **pg_vec, | |
220 | unsigned int order) | |
221 | { | |
222 | struct netlink_sock *nlk = nlk_sk(sk); | |
223 | struct sk_buff_head *queue; | |
224 | struct netlink_ring *ring; | |
225 | ||
226 | queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue; | |
227 | ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring; | |
228 | ||
229 | spin_lock_bh(&queue->lock); | |
230 | ||
231 | ring->frame_max = req->nm_frame_nr - 1; | |
232 | ring->head = 0; | |
233 | ring->frame_size = req->nm_frame_size; | |
234 | ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE; | |
235 | ||
236 | swap(ring->pg_vec_len, req->nm_block_nr); | |
237 | swap(ring->pg_vec_order, order); | |
238 | swap(ring->pg_vec, pg_vec); | |
239 | ||
240 | __skb_queue_purge(queue); | |
241 | spin_unlock_bh(&queue->lock); | |
242 | ||
243 | WARN_ON(atomic_read(&nlk->mapped)); | |
244 | ||
245 | if (pg_vec) | |
246 | free_pg_vec(pg_vec, order, req->nm_block_nr); | |
247 | } | |
248 | ||
ccdfcc39 | 249 | static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req, |
3ebe377b | 250 | bool tx_ring) |
ccdfcc39 PM |
251 | { |
252 | struct netlink_sock *nlk = nlk_sk(sk); | |
253 | struct netlink_ring *ring; | |
ccdfcc39 PM |
254 | void **pg_vec = NULL; |
255 | unsigned int order = 0; | |
ccdfcc39 PM |
256 | |
257 | ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring; | |
ccdfcc39 | 258 | |
3ebe377b FW |
259 | if (atomic_read(&nlk->mapped)) |
260 | return -EBUSY; | |
261 | if (atomic_read(&ring->pending)) | |
262 | return -EBUSY; | |
ccdfcc39 PM |
263 | |
264 | if (req->nm_block_nr) { | |
265 | if (ring->pg_vec != NULL) | |
266 | return -EBUSY; | |
267 | ||
268 | if ((int)req->nm_block_size <= 0) | |
269 | return -EINVAL; | |
270 | if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE)) | |
271 | return -EINVAL; | |
272 | if (req->nm_frame_size < NL_MMAP_HDRLEN) | |
273 | return -EINVAL; | |
274 | if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT)) | |
275 | return -EINVAL; | |
276 | ||
277 | ring->frames_per_block = req->nm_block_size / | |
278 | req->nm_frame_size; | |
279 | if (ring->frames_per_block == 0) | |
280 | return -EINVAL; | |
281 | if (ring->frames_per_block * req->nm_block_nr != | |
282 | req->nm_frame_nr) | |
283 | return -EINVAL; | |
284 | ||
285 | order = get_order(req->nm_block_size); | |
286 | pg_vec = alloc_pg_vec(nlk, req, order); | |
287 | if (pg_vec == NULL) | |
288 | return -ENOMEM; | |
289 | } else { | |
290 | if (req->nm_frame_nr) | |
291 | return -EINVAL; | |
292 | } | |
293 | ||
ccdfcc39 | 294 | mutex_lock(&nlk->pg_vec_lock); |
3ebe377b FW |
295 | if (atomic_read(&nlk->mapped) == 0) { |
296 | __netlink_set_ring(sk, req, tx_ring, pg_vec, order); | |
297 | mutex_unlock(&nlk->pg_vec_lock); | |
298 | return 0; | |
ccdfcc39 | 299 | } |
3ebe377b | 300 | |
ccdfcc39 PM |
301 | mutex_unlock(&nlk->pg_vec_lock); |
302 | ||
303 | if (pg_vec) | |
304 | free_pg_vec(pg_vec, order, req->nm_block_nr); | |
3ebe377b FW |
305 | |
306 | return -EBUSY; | |
ccdfcc39 PM |
307 | } |
308 | ||
309 | static void netlink_mm_open(struct vm_area_struct *vma) | |
310 | { | |
311 | struct file *file = vma->vm_file; | |
312 | struct socket *sock = file->private_data; | |
313 | struct sock *sk = sock->sk; | |
314 | ||
315 | if (sk) | |
316 | atomic_inc(&nlk_sk(sk)->mapped); | |
317 | } | |
318 | ||
319 | static void netlink_mm_close(struct vm_area_struct *vma) | |
320 | { | |
321 | struct file *file = vma->vm_file; | |
322 | struct socket *sock = file->private_data; | |
323 | struct sock *sk = sock->sk; | |
324 | ||
325 | if (sk) | |
326 | atomic_dec(&nlk_sk(sk)->mapped); | |
327 | } | |
328 | ||
329 | static const struct vm_operations_struct netlink_mmap_ops = { | |
330 | .open = netlink_mm_open, | |
331 | .close = netlink_mm_close, | |
332 | }; | |
333 | ||
334 | static int netlink_mmap(struct file *file, struct socket *sock, | |
335 | struct vm_area_struct *vma) | |
336 | { | |
337 | struct sock *sk = sock->sk; | |
338 | struct netlink_sock *nlk = nlk_sk(sk); | |
339 | struct netlink_ring *ring; | |
340 | unsigned long start, size, expected; | |
341 | unsigned int i; | |
342 | int err = -EINVAL; | |
343 | ||
344 | if (vma->vm_pgoff) | |
345 | return -EINVAL; | |
346 | ||
347 | mutex_lock(&nlk->pg_vec_lock); | |
348 | ||
349 | expected = 0; | |
350 | for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) { | |
351 | if (ring->pg_vec == NULL) | |
352 | continue; | |
353 | expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE; | |
354 | } | |
355 | ||
356 | if (expected == 0) | |
357 | goto out; | |
358 | ||
359 | size = vma->vm_end - vma->vm_start; | |
360 | if (size != expected) | |
361 | goto out; | |
362 | ||
363 | start = vma->vm_start; | |
364 | for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) { | |
365 | if (ring->pg_vec == NULL) | |
366 | continue; | |
367 | ||
368 | for (i = 0; i < ring->pg_vec_len; i++) { | |
369 | struct page *page; | |
370 | void *kaddr = ring->pg_vec[i]; | |
371 | unsigned int pg_num; | |
372 | ||
373 | for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) { | |
374 | page = pgvec_to_page(kaddr); | |
375 | err = vm_insert_page(vma, start, page); | |
376 | if (err < 0) | |
377 | goto out; | |
378 | start += PAGE_SIZE; | |
379 | kaddr += PAGE_SIZE; | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | atomic_inc(&nlk->mapped); | |
385 | vma->vm_ops = &netlink_mmap_ops; | |
386 | err = 0; | |
387 | out: | |
388 | mutex_unlock(&nlk->pg_vec_lock); | |
7cdbac71 | 389 | return err; |
ccdfcc39 | 390 | } |
9652e931 | 391 | |
5035c9a9 | 392 | static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr, unsigned int nm_len) |
9652e931 PM |
393 | { |
394 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1 | |
395 | struct page *p_start, *p_end; | |
396 | ||
397 | /* First page is flushed through netlink_{get,set}_status */ | |
398 | p_start = pgvec_to_page(hdr + PAGE_SIZE); | |
5035c9a9 | 399 | p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + nm_len - 1); |
9652e931 PM |
400 | while (p_start <= p_end) { |
401 | flush_dcache_page(p_start); | |
402 | p_start++; | |
403 | } | |
404 | #endif | |
405 | } | |
406 | ||
407 | static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr) | |
408 | { | |
409 | smp_rmb(); | |
410 | flush_dcache_page(pgvec_to_page(hdr)); | |
411 | return hdr->nm_status; | |
412 | } | |
413 | ||
414 | static void netlink_set_status(struct nl_mmap_hdr *hdr, | |
415 | enum nl_mmap_status status) | |
416 | { | |
57c374d3 | 417 | smp_mb(); |
9652e931 PM |
418 | hdr->nm_status = status; |
419 | flush_dcache_page(pgvec_to_page(hdr)); | |
9652e931 PM |
420 | } |
421 | ||
422 | static struct nl_mmap_hdr * | |
423 | __netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos) | |
424 | { | |
425 | unsigned int pg_vec_pos, frame_off; | |
426 | ||
427 | pg_vec_pos = pos / ring->frames_per_block; | |
428 | frame_off = pos % ring->frames_per_block; | |
429 | ||
430 | return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size); | |
431 | } | |
432 | ||
433 | static struct nl_mmap_hdr * | |
434 | netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos, | |
435 | enum nl_mmap_status status) | |
436 | { | |
437 | struct nl_mmap_hdr *hdr; | |
438 | ||
439 | hdr = __netlink_lookup_frame(ring, pos); | |
440 | if (netlink_get_status(hdr) != status) | |
441 | return NULL; | |
442 | ||
443 | return hdr; | |
444 | } | |
445 | ||
446 | static struct nl_mmap_hdr * | |
447 | netlink_current_frame(const struct netlink_ring *ring, | |
448 | enum nl_mmap_status status) | |
449 | { | |
450 | return netlink_lookup_frame(ring, ring->head, status); | |
451 | } | |
452 | ||
453 | static struct nl_mmap_hdr * | |
454 | netlink_previous_frame(const struct netlink_ring *ring, | |
455 | enum nl_mmap_status status) | |
456 | { | |
457 | unsigned int prev; | |
458 | ||
459 | prev = ring->head ? ring->head - 1 : ring->frame_max; | |
460 | return netlink_lookup_frame(ring, prev, status); | |
461 | } | |
462 | ||
463 | static void netlink_increment_head(struct netlink_ring *ring) | |
464 | { | |
465 | ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0; | |
466 | } | |
467 | ||
468 | static void netlink_forward_ring(struct netlink_ring *ring) | |
469 | { | |
470 | unsigned int head = ring->head, pos = head; | |
471 | const struct nl_mmap_hdr *hdr; | |
472 | ||
473 | do { | |
474 | hdr = __netlink_lookup_frame(ring, pos); | |
475 | if (hdr->nm_status == NL_MMAP_STATUS_UNUSED) | |
476 | break; | |
477 | if (hdr->nm_status != NL_MMAP_STATUS_SKIP) | |
478 | break; | |
479 | netlink_increment_head(ring); | |
480 | } while (ring->head != head); | |
481 | } | |
482 | ||
cd1df525 PM |
483 | static bool netlink_dump_space(struct netlink_sock *nlk) |
484 | { | |
485 | struct netlink_ring *ring = &nlk->rx_ring; | |
486 | struct nl_mmap_hdr *hdr; | |
487 | unsigned int n; | |
488 | ||
489 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); | |
490 | if (hdr == NULL) | |
491 | return false; | |
492 | ||
493 | n = ring->head + ring->frame_max / 2; | |
494 | if (n > ring->frame_max) | |
495 | n -= ring->frame_max; | |
496 | ||
497 | hdr = __netlink_lookup_frame(ring, n); | |
498 | ||
499 | return hdr->nm_status == NL_MMAP_STATUS_UNUSED; | |
500 | } | |
501 | ||
9652e931 PM |
502 | static unsigned int netlink_poll(struct file *file, struct socket *sock, |
503 | poll_table *wait) | |
504 | { | |
505 | struct sock *sk = sock->sk; | |
506 | struct netlink_sock *nlk = nlk_sk(sk); | |
507 | unsigned int mask; | |
cd1df525 | 508 | int err; |
9652e931 | 509 | |
cd1df525 PM |
510 | if (nlk->rx_ring.pg_vec != NULL) { |
511 | /* Memory mapped sockets don't call recvmsg(), so flow control | |
512 | * for dumps is performed here. A dump is allowed to continue | |
513 | * if at least half the ring is unused. | |
514 | */ | |
515 | while (nlk->cb != NULL && netlink_dump_space(nlk)) { | |
516 | err = netlink_dump(sk); | |
517 | if (err < 0) { | |
c29503e9 | 518 | sk->sk_err = -err; |
cd1df525 PM |
519 | sk->sk_error_report(sk); |
520 | break; | |
521 | } | |
522 | } | |
523 | netlink_rcv_wake(sk); | |
524 | } | |
5fd96123 | 525 | |
9652e931 PM |
526 | mask = datagram_poll(file, sock, wait); |
527 | ||
528 | spin_lock_bh(&sk->sk_receive_queue.lock); | |
529 | if (nlk->rx_ring.pg_vec) { | |
530 | netlink_forward_ring(&nlk->rx_ring); | |
531 | if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED)) | |
532 | mask |= POLLIN | POLLRDNORM; | |
533 | } | |
534 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
535 | ||
536 | spin_lock_bh(&sk->sk_write_queue.lock); | |
537 | if (nlk->tx_ring.pg_vec) { | |
538 | if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED)) | |
539 | mask |= POLLOUT | POLLWRNORM; | |
540 | } | |
541 | spin_unlock_bh(&sk->sk_write_queue.lock); | |
542 | ||
543 | return mask; | |
544 | } | |
545 | ||
546 | static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb) | |
547 | { | |
548 | return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN); | |
549 | } | |
550 | ||
551 | static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk, | |
552 | struct netlink_ring *ring, | |
553 | struct nl_mmap_hdr *hdr) | |
554 | { | |
555 | unsigned int size; | |
556 | void *data; | |
557 | ||
558 | size = ring->frame_size - NL_MMAP_HDRLEN; | |
559 | data = (void *)hdr + NL_MMAP_HDRLEN; | |
560 | ||
561 | skb->head = data; | |
562 | skb->data = data; | |
563 | skb_reset_tail_pointer(skb); | |
564 | skb->end = skb->tail + size; | |
565 | skb->len = 0; | |
566 | ||
567 | skb->destructor = netlink_skb_destructor; | |
568 | NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED; | |
569 | NETLINK_CB(skb).sk = sk; | |
570 | } | |
5fd96123 PM |
571 | |
572 | static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg, | |
573 | u32 dst_portid, u32 dst_group, | |
574 | struct sock_iocb *siocb) | |
575 | { | |
576 | struct netlink_sock *nlk = nlk_sk(sk); | |
577 | struct netlink_ring *ring; | |
578 | struct nl_mmap_hdr *hdr; | |
579 | struct sk_buff *skb; | |
580 | unsigned int maxlen; | |
5fd96123 PM |
581 | int err = 0, len = 0; |
582 | ||
5fd96123 PM |
583 | mutex_lock(&nlk->pg_vec_lock); |
584 | ||
585 | ring = &nlk->tx_ring; | |
586 | maxlen = ring->frame_size - NL_MMAP_HDRLEN; | |
587 | ||
588 | do { | |
5035c9a9 DM |
589 | unsigned int nm_len; |
590 | ||
5fd96123 PM |
591 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID); |
592 | if (hdr == NULL) { | |
593 | if (!(msg->msg_flags & MSG_DONTWAIT) && | |
594 | atomic_read(&nlk->tx_ring.pending)) | |
595 | schedule(); | |
596 | continue; | |
597 | } | |
5035c9a9 DM |
598 | |
599 | nm_len = ACCESS_ONCE(hdr->nm_len); | |
600 | if (nm_len > maxlen) { | |
5fd96123 PM |
601 | err = -EINVAL; |
602 | goto out; | |
603 | } | |
604 | ||
5035c9a9 | 605 | netlink_frame_flush_dcache(hdr, nm_len); |
5fd96123 | 606 | |
5035c9a9 DM |
607 | skb = alloc_skb(nm_len, GFP_KERNEL); |
608 | if (skb == NULL) { | |
609 | err = -ENOBUFS; | |
610 | goto out; | |
5fd96123 | 611 | } |
5035c9a9 DM |
612 | __skb_put(skb, nm_len); |
613 | memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, nm_len); | |
614 | netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED); | |
5fd96123 PM |
615 | |
616 | netlink_increment_head(ring); | |
617 | ||
618 | NETLINK_CB(skb).portid = nlk->portid; | |
619 | NETLINK_CB(skb).dst_group = dst_group; | |
620 | NETLINK_CB(skb).creds = siocb->scm->creds; | |
621 | ||
622 | err = security_netlink_send(sk, skb); | |
623 | if (err) { | |
624 | kfree_skb(skb); | |
625 | goto out; | |
626 | } | |
627 | ||
628 | if (unlikely(dst_group)) { | |
629 | atomic_inc(&skb->users); | |
630 | netlink_broadcast(sk, skb, dst_portid, dst_group, | |
631 | GFP_KERNEL); | |
632 | } | |
633 | err = netlink_unicast(sk, skb, dst_portid, | |
634 | msg->msg_flags & MSG_DONTWAIT); | |
635 | if (err < 0) | |
636 | goto out; | |
637 | len += err; | |
638 | ||
639 | } while (hdr != NULL || | |
640 | (!(msg->msg_flags & MSG_DONTWAIT) && | |
641 | atomic_read(&nlk->tx_ring.pending))); | |
642 | ||
643 | if (len > 0) | |
644 | err = len; | |
645 | out: | |
646 | mutex_unlock(&nlk->pg_vec_lock); | |
647 | return err; | |
648 | } | |
f9c22888 PM |
649 | |
650 | static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb) | |
651 | { | |
652 | struct nl_mmap_hdr *hdr; | |
653 | ||
654 | hdr = netlink_mmap_hdr(skb); | |
655 | hdr->nm_len = skb->len; | |
656 | hdr->nm_group = NETLINK_CB(skb).dst_group; | |
657 | hdr->nm_pid = NETLINK_CB(skb).creds.pid; | |
1bf9310a ND |
658 | hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid); |
659 | hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid); | |
5035c9a9 | 660 | netlink_frame_flush_dcache(hdr, hdr->nm_len); |
f9c22888 PM |
661 | netlink_set_status(hdr, NL_MMAP_STATUS_VALID); |
662 | ||
663 | NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED; | |
664 | kfree_skb(skb); | |
665 | } | |
666 | ||
667 | static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb) | |
668 | { | |
669 | struct netlink_sock *nlk = nlk_sk(sk); | |
670 | struct netlink_ring *ring = &nlk->rx_ring; | |
671 | struct nl_mmap_hdr *hdr; | |
672 | ||
673 | spin_lock_bh(&sk->sk_receive_queue.lock); | |
674 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); | |
675 | if (hdr == NULL) { | |
676 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
677 | kfree_skb(skb); | |
cd1df525 | 678 | netlink_overrun(sk); |
f9c22888 PM |
679 | return; |
680 | } | |
681 | netlink_increment_head(ring); | |
682 | __skb_queue_tail(&sk->sk_receive_queue, skb); | |
683 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
684 | ||
685 | hdr->nm_len = skb->len; | |
686 | hdr->nm_group = NETLINK_CB(skb).dst_group; | |
687 | hdr->nm_pid = NETLINK_CB(skb).creds.pid; | |
1bf9310a ND |
688 | hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid); |
689 | hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid); | |
f9c22888 PM |
690 | netlink_set_status(hdr, NL_MMAP_STATUS_COPY); |
691 | } | |
692 | ||
ccdfcc39 | 693 | #else /* CONFIG_NETLINK_MMAP */ |
9652e931 | 694 | #define netlink_skb_is_mmaped(skb) false |
f9c22888 | 695 | #define netlink_rx_is_mmaped(sk) false |
5fd96123 | 696 | #define netlink_tx_is_mmaped(sk) false |
ccdfcc39 | 697 | #define netlink_mmap sock_no_mmap |
9652e931 | 698 | #define netlink_poll datagram_poll |
5fd96123 | 699 | #define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0 |
ccdfcc39 PM |
700 | #endif /* CONFIG_NETLINK_MMAP */ |
701 | ||
658cb354 ED |
702 | static void netlink_destroy_callback(struct netlink_callback *cb) |
703 | { | |
704 | kfree_skb(cb->skb); | |
705 | kfree(cb); | |
706 | } | |
707 | ||
bfb253c9 ED |
708 | static void netlink_consume_callback(struct netlink_callback *cb) |
709 | { | |
710 | consume_skb(cb->skb); | |
711 | kfree(cb); | |
712 | } | |
713 | ||
cf0a018a PM |
714 | static void netlink_skb_destructor(struct sk_buff *skb) |
715 | { | |
9652e931 PM |
716 | #ifdef CONFIG_NETLINK_MMAP |
717 | struct nl_mmap_hdr *hdr; | |
718 | struct netlink_ring *ring; | |
719 | struct sock *sk; | |
720 | ||
721 | /* If a packet from the kernel to userspace was freed because of an | |
722 | * error without being delivered to userspace, the kernel must reset | |
723 | * the status. In the direction userspace to kernel, the status is | |
724 | * always reset here after the packet was processed and freed. | |
725 | */ | |
726 | if (netlink_skb_is_mmaped(skb)) { | |
727 | hdr = netlink_mmap_hdr(skb); | |
728 | sk = NETLINK_CB(skb).sk; | |
729 | ||
5fd96123 PM |
730 | if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) { |
731 | netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED); | |
732 | ring = &nlk_sk(sk)->tx_ring; | |
733 | } else { | |
734 | if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) { | |
735 | hdr->nm_len = 0; | |
736 | netlink_set_status(hdr, NL_MMAP_STATUS_VALID); | |
737 | } | |
738 | ring = &nlk_sk(sk)->rx_ring; | |
9652e931 | 739 | } |
9652e931 PM |
740 | |
741 | WARN_ON(atomic_read(&ring->pending) == 0); | |
742 | atomic_dec(&ring->pending); | |
743 | sock_put(sk); | |
744 | ||
5e71d9d7 | 745 | skb->head = NULL; |
9652e931 PM |
746 | } |
747 | #endif | |
748 | if (skb->sk != NULL) | |
749 | sock_rfree(skb); | |
cf0a018a PM |
750 | } |
751 | ||
752 | static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk) | |
753 | { | |
754 | WARN_ON(skb->sk != NULL); | |
755 | skb->sk = sk; | |
756 | skb->destructor = netlink_skb_destructor; | |
757 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); | |
758 | sk_mem_charge(sk, skb->truesize); | |
759 | } | |
760 | ||
1da177e4 LT |
761 | static void netlink_sock_destruct(struct sock *sk) |
762 | { | |
3f660d66 HX |
763 | struct netlink_sock *nlk = nlk_sk(sk); |
764 | ||
3f660d66 HX |
765 | if (nlk->cb) { |
766 | if (nlk->cb->done) | |
767 | nlk->cb->done(nlk->cb); | |
6dc878a8 G |
768 | |
769 | module_put(nlk->cb->module); | |
3f660d66 HX |
770 | netlink_destroy_callback(nlk->cb); |
771 | } | |
772 | ||
1da177e4 | 773 | skb_queue_purge(&sk->sk_receive_queue); |
ccdfcc39 PM |
774 | #ifdef CONFIG_NETLINK_MMAP |
775 | if (1) { | |
776 | struct nl_mmap_req req; | |
777 | ||
778 | memset(&req, 0, sizeof(req)); | |
779 | if (nlk->rx_ring.pg_vec) | |
3ebe377b | 780 | __netlink_set_ring(sk, &req, false, NULL, 0); |
ccdfcc39 PM |
781 | memset(&req, 0, sizeof(req)); |
782 | if (nlk->tx_ring.pg_vec) | |
3ebe377b | 783 | __netlink_set_ring(sk, &req, true, NULL, 0); |
ccdfcc39 PM |
784 | } |
785 | #endif /* CONFIG_NETLINK_MMAP */ | |
1da177e4 LT |
786 | |
787 | if (!sock_flag(sk, SOCK_DEAD)) { | |
6ac552fd | 788 | printk(KERN_ERR "Freeing alive netlink socket %p\n", sk); |
1da177e4 LT |
789 | return; |
790 | } | |
547b792c IJ |
791 | |
792 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); | |
793 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); | |
794 | WARN_ON(nlk_sk(sk)->groups); | |
1da177e4 LT |
795 | } |
796 | ||
6ac552fd PM |
797 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on |
798 | * SMP. Look, when several writers sleep and reader wakes them up, all but one | |
1da177e4 LT |
799 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves |
800 | * this, _but_ remember, it adds useless work on UP machines. | |
801 | */ | |
802 | ||
d136f1bd | 803 | void netlink_table_grab(void) |
9a429c49 | 804 | __acquires(nl_table_lock) |
1da177e4 | 805 | { |
d136f1bd JB |
806 | might_sleep(); |
807 | ||
6abd219c | 808 | write_lock_irq(&nl_table_lock); |
1da177e4 LT |
809 | |
810 | if (atomic_read(&nl_table_users)) { | |
811 | DECLARE_WAITQUEUE(wait, current); | |
812 | ||
813 | add_wait_queue_exclusive(&nl_table_wait, &wait); | |
6ac552fd | 814 | for (;;) { |
1da177e4 LT |
815 | set_current_state(TASK_UNINTERRUPTIBLE); |
816 | if (atomic_read(&nl_table_users) == 0) | |
817 | break; | |
6abd219c | 818 | write_unlock_irq(&nl_table_lock); |
1da177e4 | 819 | schedule(); |
6abd219c | 820 | write_lock_irq(&nl_table_lock); |
1da177e4 LT |
821 | } |
822 | ||
823 | __set_current_state(TASK_RUNNING); | |
824 | remove_wait_queue(&nl_table_wait, &wait); | |
825 | } | |
826 | } | |
827 | ||
d136f1bd | 828 | void netlink_table_ungrab(void) |
9a429c49 | 829 | __releases(nl_table_lock) |
1da177e4 | 830 | { |
6abd219c | 831 | write_unlock_irq(&nl_table_lock); |
1da177e4 LT |
832 | wake_up(&nl_table_wait); |
833 | } | |
834 | ||
6ac552fd | 835 | static inline void |
1da177e4 LT |
836 | netlink_lock_table(void) |
837 | { | |
838 | /* read_lock() synchronizes us to netlink_table_grab */ | |
839 | ||
840 | read_lock(&nl_table_lock); | |
841 | atomic_inc(&nl_table_users); | |
842 | read_unlock(&nl_table_lock); | |
843 | } | |
844 | ||
6ac552fd | 845 | static inline void |
1da177e4 LT |
846 | netlink_unlock_table(void) |
847 | { | |
848 | if (atomic_dec_and_test(&nl_table_users)) | |
849 | wake_up(&nl_table_wait); | |
850 | } | |
851 | ||
15e47304 | 852 | static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid) |
1da177e4 | 853 | { |
15e47304 | 854 | struct nl_portid_hash *hash = &nl_table[protocol].hash; |
1da177e4 LT |
855 | struct hlist_head *head; |
856 | struct sock *sk; | |
1da177e4 LT |
857 | |
858 | read_lock(&nl_table_lock); | |
15e47304 | 859 | head = nl_portid_hashfn(hash, portid); |
b67bfe0d | 860 | sk_for_each(sk, head) { |
15e47304 | 861 | if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) { |
1da177e4 LT |
862 | sock_hold(sk); |
863 | goto found; | |
864 | } | |
865 | } | |
866 | sk = NULL; | |
867 | found: | |
868 | read_unlock(&nl_table_lock); | |
869 | return sk; | |
870 | } | |
871 | ||
15e47304 | 872 | static struct hlist_head *nl_portid_hash_zalloc(size_t size) |
1da177e4 LT |
873 | { |
874 | if (size <= PAGE_SIZE) | |
ea72912c | 875 | return kzalloc(size, GFP_ATOMIC); |
1da177e4 LT |
876 | else |
877 | return (struct hlist_head *) | |
ea72912c ED |
878 | __get_free_pages(GFP_ATOMIC | __GFP_ZERO, |
879 | get_order(size)); | |
1da177e4 LT |
880 | } |
881 | ||
15e47304 | 882 | static void nl_portid_hash_free(struct hlist_head *table, size_t size) |
1da177e4 LT |
883 | { |
884 | if (size <= PAGE_SIZE) | |
885 | kfree(table); | |
886 | else | |
887 | free_pages((unsigned long)table, get_order(size)); | |
888 | } | |
889 | ||
15e47304 | 890 | static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow) |
1da177e4 LT |
891 | { |
892 | unsigned int omask, mask, shift; | |
893 | size_t osize, size; | |
894 | struct hlist_head *otable, *table; | |
895 | int i; | |
896 | ||
897 | omask = mask = hash->mask; | |
898 | osize = size = (mask + 1) * sizeof(*table); | |
899 | shift = hash->shift; | |
900 | ||
901 | if (grow) { | |
902 | if (++shift > hash->max_shift) | |
903 | return 0; | |
904 | mask = mask * 2 + 1; | |
905 | size *= 2; | |
906 | } | |
907 | ||
15e47304 | 908 | table = nl_portid_hash_zalloc(size); |
1da177e4 LT |
909 | if (!table) |
910 | return 0; | |
911 | ||
1da177e4 LT |
912 | otable = hash->table; |
913 | hash->table = table; | |
914 | hash->mask = mask; | |
915 | hash->shift = shift; | |
916 | get_random_bytes(&hash->rnd, sizeof(hash->rnd)); | |
917 | ||
918 | for (i = 0; i <= omask; i++) { | |
919 | struct sock *sk; | |
b67bfe0d | 920 | struct hlist_node *tmp; |
1da177e4 | 921 | |
b67bfe0d | 922 | sk_for_each_safe(sk, tmp, &otable[i]) |
15e47304 | 923 | __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid)); |
1da177e4 LT |
924 | } |
925 | ||
15e47304 | 926 | nl_portid_hash_free(otable, osize); |
1da177e4 LT |
927 | hash->rehash_time = jiffies + 10 * 60 * HZ; |
928 | return 1; | |
929 | } | |
930 | ||
15e47304 | 931 | static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len) |
1da177e4 LT |
932 | { |
933 | int avg = hash->entries >> hash->shift; | |
934 | ||
15e47304 | 935 | if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1)) |
1da177e4 LT |
936 | return 1; |
937 | ||
938 | if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) { | |
15e47304 | 939 | nl_portid_hash_rehash(hash, 0); |
1da177e4 LT |
940 | return 1; |
941 | } | |
942 | ||
943 | return 0; | |
944 | } | |
945 | ||
90ddc4f0 | 946 | static const struct proto_ops netlink_ops; |
1da177e4 | 947 | |
4277a083 PM |
948 | static void |
949 | netlink_update_listeners(struct sock *sk) | |
950 | { | |
951 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; | |
4277a083 PM |
952 | unsigned long mask; |
953 | unsigned int i; | |
6d772ac5 ED |
954 | struct listeners *listeners; |
955 | ||
956 | listeners = nl_deref_protected(tbl->listeners); | |
957 | if (!listeners) | |
958 | return; | |
4277a083 | 959 | |
b4ff4f04 | 960 | for (i = 0; i < NLGRPLONGS(tbl->groups); i++) { |
4277a083 | 961 | mask = 0; |
b67bfe0d | 962 | sk_for_each_bound(sk, &tbl->mc_list) { |
b4ff4f04 JB |
963 | if (i < NLGRPLONGS(nlk_sk(sk)->ngroups)) |
964 | mask |= nlk_sk(sk)->groups[i]; | |
965 | } | |
6d772ac5 | 966 | listeners->masks[i] = mask; |
4277a083 PM |
967 | } |
968 | /* this function is only called with the netlink table "grabbed", which | |
969 | * makes sure updates are visible before bind or setsockopt return. */ | |
970 | } | |
971 | ||
15e47304 | 972 | static int netlink_insert(struct sock *sk, struct net *net, u32 portid) |
1da177e4 | 973 | { |
15e47304 | 974 | struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash; |
1da177e4 LT |
975 | struct hlist_head *head; |
976 | int err = -EADDRINUSE; | |
977 | struct sock *osk; | |
1da177e4 LT |
978 | int len; |
979 | ||
980 | netlink_table_grab(); | |
15e47304 | 981 | head = nl_portid_hashfn(hash, portid); |
1da177e4 | 982 | len = 0; |
b67bfe0d | 983 | sk_for_each(osk, head) { |
15e47304 | 984 | if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid)) |
1da177e4 LT |
985 | break; |
986 | len++; | |
987 | } | |
b67bfe0d | 988 | if (osk) |
1da177e4 LT |
989 | goto err; |
990 | ||
991 | err = -EBUSY; | |
15e47304 | 992 | if (nlk_sk(sk)->portid) |
1da177e4 LT |
993 | goto err; |
994 | ||
995 | err = -ENOMEM; | |
996 | if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX)) | |
997 | goto err; | |
998 | ||
15e47304 EB |
999 | if (len && nl_portid_hash_dilute(hash, len)) |
1000 | head = nl_portid_hashfn(hash, portid); | |
1da177e4 | 1001 | hash->entries++; |
15e47304 | 1002 | nlk_sk(sk)->portid = portid; |
1da177e4 LT |
1003 | sk_add_node(sk, head); |
1004 | err = 0; | |
1005 | ||
1006 | err: | |
1007 | netlink_table_ungrab(); | |
1008 | return err; | |
1009 | } | |
1010 | ||
1011 | static void netlink_remove(struct sock *sk) | |
1012 | { | |
1013 | netlink_table_grab(); | |
d470e3b4 DM |
1014 | if (sk_del_node_init(sk)) |
1015 | nl_table[sk->sk_protocol].hash.entries--; | |
f7fa9b10 | 1016 | if (nlk_sk(sk)->subscriptions) |
1da177e4 LT |
1017 | __sk_del_bind_node(sk); |
1018 | netlink_table_ungrab(); | |
1019 | } | |
1020 | ||
1021 | static struct proto netlink_proto = { | |
1022 | .name = "NETLINK", | |
1023 | .owner = THIS_MODULE, | |
1024 | .obj_size = sizeof(struct netlink_sock), | |
1025 | }; | |
1026 | ||
1b8d7ae4 EB |
1027 | static int __netlink_create(struct net *net, struct socket *sock, |
1028 | struct mutex *cb_mutex, int protocol) | |
1da177e4 LT |
1029 | { |
1030 | struct sock *sk; | |
1031 | struct netlink_sock *nlk; | |
ab33a171 PM |
1032 | |
1033 | sock->ops = &netlink_ops; | |
1034 | ||
6257ff21 | 1035 | sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto); |
ab33a171 PM |
1036 | if (!sk) |
1037 | return -ENOMEM; | |
1038 | ||
1039 | sock_init_data(sock, sk); | |
1040 | ||
1041 | nlk = nlk_sk(sk); | |
658cb354 | 1042 | if (cb_mutex) { |
ffa4d721 | 1043 | nlk->cb_mutex = cb_mutex; |
658cb354 | 1044 | } else { |
ffa4d721 PM |
1045 | nlk->cb_mutex = &nlk->cb_def_mutex; |
1046 | mutex_init(nlk->cb_mutex); | |
1047 | } | |
ab33a171 | 1048 | init_waitqueue_head(&nlk->wait); |
ccdfcc39 PM |
1049 | #ifdef CONFIG_NETLINK_MMAP |
1050 | mutex_init(&nlk->pg_vec_lock); | |
1051 | #endif | |
ab33a171 PM |
1052 | |
1053 | sk->sk_destruct = netlink_sock_destruct; | |
1054 | sk->sk_protocol = protocol; | |
1055 | return 0; | |
1056 | } | |
1057 | ||
3f378b68 EP |
1058 | static int netlink_create(struct net *net, struct socket *sock, int protocol, |
1059 | int kern) | |
ab33a171 PM |
1060 | { |
1061 | struct module *module = NULL; | |
af65bdfc | 1062 | struct mutex *cb_mutex; |
f7fa9b10 | 1063 | struct netlink_sock *nlk; |
03292745 | 1064 | void (*bind)(int group); |
ab33a171 | 1065 | int err = 0; |
1da177e4 LT |
1066 | |
1067 | sock->state = SS_UNCONNECTED; | |
1068 | ||
1069 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) | |
1070 | return -ESOCKTNOSUPPORT; | |
1071 | ||
6ac552fd | 1072 | if (protocol < 0 || protocol >= MAX_LINKS) |
1da177e4 LT |
1073 | return -EPROTONOSUPPORT; |
1074 | ||
77247bbb | 1075 | netlink_lock_table(); |
95a5afca | 1076 | #ifdef CONFIG_MODULES |
ab33a171 | 1077 | if (!nl_table[protocol].registered) { |
77247bbb | 1078 | netlink_unlock_table(); |
4fdb3bb7 | 1079 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
77247bbb | 1080 | netlink_lock_table(); |
4fdb3bb7 | 1081 | } |
ab33a171 PM |
1082 | #endif |
1083 | if (nl_table[protocol].registered && | |
1084 | try_module_get(nl_table[protocol].module)) | |
1085 | module = nl_table[protocol].module; | |
974c37e9 AD |
1086 | else |
1087 | err = -EPROTONOSUPPORT; | |
af65bdfc | 1088 | cb_mutex = nl_table[protocol].cb_mutex; |
03292745 | 1089 | bind = nl_table[protocol].bind; |
77247bbb | 1090 | netlink_unlock_table(); |
4fdb3bb7 | 1091 | |
974c37e9 AD |
1092 | if (err < 0) |
1093 | goto out; | |
1094 | ||
6ac552fd PM |
1095 | err = __netlink_create(net, sock, cb_mutex, protocol); |
1096 | if (err < 0) | |
f7fa9b10 PM |
1097 | goto out_module; |
1098 | ||
6f756a8c | 1099 | local_bh_disable(); |
c1fd3b94 | 1100 | sock_prot_inuse_add(net, &netlink_proto, 1); |
6f756a8c DM |
1101 | local_bh_enable(); |
1102 | ||
f7fa9b10 | 1103 | nlk = nlk_sk(sock->sk); |
f7fa9b10 | 1104 | nlk->module = module; |
03292745 | 1105 | nlk->netlink_bind = bind; |
ab33a171 PM |
1106 | out: |
1107 | return err; | |
1da177e4 | 1108 | |
ab33a171 PM |
1109 | out_module: |
1110 | module_put(module); | |
1111 | goto out; | |
1da177e4 LT |
1112 | } |
1113 | ||
1114 | static int netlink_release(struct socket *sock) | |
1115 | { | |
1116 | struct sock *sk = sock->sk; | |
1117 | struct netlink_sock *nlk; | |
1118 | ||
1119 | if (!sk) | |
1120 | return 0; | |
1121 | ||
1122 | netlink_remove(sk); | |
ac57b3a9 | 1123 | sock_orphan(sk); |
1da177e4 LT |
1124 | nlk = nlk_sk(sk); |
1125 | ||
3f660d66 HX |
1126 | /* |
1127 | * OK. Socket is unlinked, any packets that arrive now | |
1128 | * will be purged. | |
1129 | */ | |
1da177e4 | 1130 | |
1da177e4 LT |
1131 | sock->sk = NULL; |
1132 | wake_up_interruptible_all(&nlk->wait); | |
1133 | ||
1134 | skb_queue_purge(&sk->sk_write_queue); | |
1135 | ||
15e47304 | 1136 | if (nlk->portid) { |
1da177e4 | 1137 | struct netlink_notify n = { |
3b1e0a65 | 1138 | .net = sock_net(sk), |
1da177e4 | 1139 | .protocol = sk->sk_protocol, |
15e47304 | 1140 | .portid = nlk->portid, |
1da177e4 | 1141 | }; |
e041c683 AS |
1142 | atomic_notifier_call_chain(&netlink_chain, |
1143 | NETLINK_URELEASE, &n); | |
746fac4d | 1144 | } |
4fdb3bb7 | 1145 | |
5e7c001c | 1146 | module_put(nlk->module); |
4fdb3bb7 | 1147 | |
4277a083 | 1148 | netlink_table_grab(); |
aed81560 | 1149 | if (netlink_is_kernel(sk)) { |
869e58f8 DL |
1150 | BUG_ON(nl_table[sk->sk_protocol].registered == 0); |
1151 | if (--nl_table[sk->sk_protocol].registered == 0) { | |
6d772ac5 ED |
1152 | struct listeners *old; |
1153 | ||
1154 | old = nl_deref_protected(nl_table[sk->sk_protocol].listeners); | |
1155 | RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL); | |
1156 | kfree_rcu(old, rcu); | |
869e58f8 | 1157 | nl_table[sk->sk_protocol].module = NULL; |
9785e10a PNA |
1158 | nl_table[sk->sk_protocol].bind = NULL; |
1159 | nl_table[sk->sk_protocol].flags = 0; | |
869e58f8 DL |
1160 | nl_table[sk->sk_protocol].registered = 0; |
1161 | } | |
658cb354 | 1162 | } else if (nlk->subscriptions) { |
4277a083 | 1163 | netlink_update_listeners(sk); |
658cb354 | 1164 | } |
4277a083 | 1165 | netlink_table_ungrab(); |
77247bbb | 1166 | |
f7fa9b10 PM |
1167 | kfree(nlk->groups); |
1168 | nlk->groups = NULL; | |
1169 | ||
3755810c | 1170 | local_bh_disable(); |
c1fd3b94 | 1171 | sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1); |
3755810c | 1172 | local_bh_enable(); |
1da177e4 LT |
1173 | sock_put(sk); |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | static int netlink_autobind(struct socket *sock) | |
1178 | { | |
1179 | struct sock *sk = sock->sk; | |
3b1e0a65 | 1180 | struct net *net = sock_net(sk); |
15e47304 | 1181 | struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash; |
1da177e4 LT |
1182 | struct hlist_head *head; |
1183 | struct sock *osk; | |
15e47304 | 1184 | s32 portid = task_tgid_vnr(current); |
1da177e4 LT |
1185 | int err; |
1186 | static s32 rover = -4097; | |
1187 | ||
1188 | retry: | |
1189 | cond_resched(); | |
1190 | netlink_table_grab(); | |
15e47304 | 1191 | head = nl_portid_hashfn(hash, portid); |
b67bfe0d | 1192 | sk_for_each(osk, head) { |
878628fb | 1193 | if (!net_eq(sock_net(osk), net)) |
b4b51029 | 1194 | continue; |
15e47304 EB |
1195 | if (nlk_sk(osk)->portid == portid) { |
1196 | /* Bind collision, search negative portid values. */ | |
1197 | portid = rover--; | |
1da177e4 LT |
1198 | if (rover > -4097) |
1199 | rover = -4097; | |
1200 | netlink_table_ungrab(); | |
1201 | goto retry; | |
1202 | } | |
1203 | } | |
1204 | netlink_table_ungrab(); | |
1205 | ||
15e47304 | 1206 | err = netlink_insert(sk, net, portid); |
1da177e4 LT |
1207 | if (err == -EADDRINUSE) |
1208 | goto retry; | |
d470e3b4 DM |
1209 | |
1210 | /* If 2 threads race to autobind, that is fine. */ | |
1211 | if (err == -EBUSY) | |
1212 | err = 0; | |
1213 | ||
1214 | return err; | |
1da177e4 LT |
1215 | } |
1216 | ||
738f378d EB |
1217 | /** |
1218 | * __netlink_ns_capable - General netlink message capability test | |
1219 | * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace. | |
1220 | * @user_ns: The user namespace of the capability to use | |
1221 | * @cap: The capability to use | |
1222 | * | |
1223 | * Test to see if the opener of the socket we received the message | |
1224 | * from had when the netlink socket was created and the sender of the | |
1225 | * message has has the capability @cap in the user namespace @user_ns. | |
1226 | */ | |
1227 | bool __netlink_ns_capable(const struct netlink_skb_parms *nsp, | |
1228 | struct user_namespace *user_ns, int cap) | |
1229 | { | |
8ab02acd EB |
1230 | return ((nsp->flags & NETLINK_SKB_DST) || |
1231 | file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) && | |
1232 | ns_capable(user_ns, cap); | |
738f378d EB |
1233 | } |
1234 | EXPORT_SYMBOL(__netlink_ns_capable); | |
1235 | ||
1236 | /** | |
1237 | * netlink_ns_capable - General netlink message capability test | |
1238 | * @skb: socket buffer holding a netlink command from userspace | |
1239 | * @user_ns: The user namespace of the capability to use | |
1240 | * @cap: The capability to use | |
1241 | * | |
1242 | * Test to see if the opener of the socket we received the message | |
1243 | * from had when the netlink socket was created and the sender of the | |
1244 | * message has has the capability @cap in the user namespace @user_ns. | |
1245 | */ | |
1246 | bool netlink_ns_capable(const struct sk_buff *skb, | |
1247 | struct user_namespace *user_ns, int cap) | |
1248 | { | |
1249 | return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap); | |
1250 | } | |
1251 | EXPORT_SYMBOL(netlink_ns_capable); | |
1252 | ||
1253 | /** | |
1254 | * netlink_capable - Netlink global message capability test | |
1255 | * @skb: socket buffer holding a netlink command from userspace | |
1256 | * @cap: The capability to use | |
1257 | * | |
1258 | * Test to see if the opener of the socket we received the message | |
1259 | * from had when the netlink socket was created and the sender of the | |
1260 | * message has has the capability @cap in all user namespaces. | |
1261 | */ | |
1262 | bool netlink_capable(const struct sk_buff *skb, int cap) | |
1263 | { | |
1264 | return netlink_ns_capable(skb, &init_user_ns, cap); | |
1265 | } | |
1266 | EXPORT_SYMBOL(netlink_capable); | |
1267 | ||
1268 | /** | |
1269 | * netlink_net_capable - Netlink network namespace message capability test | |
1270 | * @skb: socket buffer holding a netlink command from userspace | |
1271 | * @cap: The capability to use | |
1272 | * | |
1273 | * Test to see if the opener of the socket we received the message | |
1274 | * from had when the netlink socket was created and the sender of the | |
1275 | * message has has the capability @cap over the network namespace of | |
1276 | * the socket we received the message from. | |
1277 | */ | |
1278 | bool netlink_net_capable(const struct sk_buff *skb, int cap) | |
1279 | { | |
1280 | return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap); | |
1281 | } | |
1282 | EXPORT_SYMBOL(netlink_net_capable); | |
1283 | ||
d806b731 | 1284 | static inline int netlink_allowed(const struct socket *sock, unsigned int flag) |
746fac4d | 1285 | { |
9785e10a | 1286 | return (nl_table[sock->sk->sk_protocol].flags & flag) || |
df008c91 | 1287 | ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN); |
746fac4d | 1288 | } |
1da177e4 | 1289 | |
f7fa9b10 PM |
1290 | static void |
1291 | netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) | |
1292 | { | |
1293 | struct netlink_sock *nlk = nlk_sk(sk); | |
1294 | ||
1295 | if (nlk->subscriptions && !subscriptions) | |
1296 | __sk_del_bind_node(sk); | |
1297 | else if (!nlk->subscriptions && subscriptions) | |
1298 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); | |
1299 | nlk->subscriptions = subscriptions; | |
1300 | } | |
1301 | ||
b4ff4f04 | 1302 | static int netlink_realloc_groups(struct sock *sk) |
513c2500 PM |
1303 | { |
1304 | struct netlink_sock *nlk = nlk_sk(sk); | |
1305 | unsigned int groups; | |
b4ff4f04 | 1306 | unsigned long *new_groups; |
513c2500 PM |
1307 | int err = 0; |
1308 | ||
b4ff4f04 JB |
1309 | netlink_table_grab(); |
1310 | ||
513c2500 | 1311 | groups = nl_table[sk->sk_protocol].groups; |
b4ff4f04 | 1312 | if (!nl_table[sk->sk_protocol].registered) { |
513c2500 | 1313 | err = -ENOENT; |
b4ff4f04 JB |
1314 | goto out_unlock; |
1315 | } | |
513c2500 | 1316 | |
b4ff4f04 JB |
1317 | if (nlk->ngroups >= groups) |
1318 | goto out_unlock; | |
513c2500 | 1319 | |
b4ff4f04 JB |
1320 | new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC); |
1321 | if (new_groups == NULL) { | |
1322 | err = -ENOMEM; | |
1323 | goto out_unlock; | |
1324 | } | |
6ac552fd | 1325 | memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0, |
b4ff4f04 JB |
1326 | NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups)); |
1327 | ||
1328 | nlk->groups = new_groups; | |
513c2500 | 1329 | nlk->ngroups = groups; |
b4ff4f04 JB |
1330 | out_unlock: |
1331 | netlink_table_ungrab(); | |
1332 | return err; | |
513c2500 PM |
1333 | } |
1334 | ||
6ac552fd PM |
1335 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, |
1336 | int addr_len) | |
1da177e4 LT |
1337 | { |
1338 | struct sock *sk = sock->sk; | |
3b1e0a65 | 1339 | struct net *net = sock_net(sk); |
1da177e4 LT |
1340 | struct netlink_sock *nlk = nlk_sk(sk); |
1341 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; | |
1342 | int err; | |
746fac4d | 1343 | |
4e4b5376 HFS |
1344 | if (addr_len < sizeof(struct sockaddr_nl)) |
1345 | return -EINVAL; | |
1346 | ||
1da177e4 LT |
1347 | if (nladdr->nl_family != AF_NETLINK) |
1348 | return -EINVAL; | |
1349 | ||
1350 | /* Only superuser is allowed to listen multicasts */ | |
513c2500 | 1351 | if (nladdr->nl_groups) { |
d806b731 | 1352 | if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV)) |
513c2500 | 1353 | return -EPERM; |
b4ff4f04 JB |
1354 | err = netlink_realloc_groups(sk); |
1355 | if (err) | |
1356 | return err; | |
513c2500 | 1357 | } |
1da177e4 | 1358 | |
15e47304 EB |
1359 | if (nlk->portid) { |
1360 | if (nladdr->nl_pid != nlk->portid) | |
1da177e4 LT |
1361 | return -EINVAL; |
1362 | } else { | |
1363 | err = nladdr->nl_pid ? | |
b4b51029 | 1364 | netlink_insert(sk, net, nladdr->nl_pid) : |
1da177e4 LT |
1365 | netlink_autobind(sock); |
1366 | if (err) | |
1367 | return err; | |
1368 | } | |
1369 | ||
513c2500 | 1370 | if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0])) |
1da177e4 LT |
1371 | return 0; |
1372 | ||
1373 | netlink_table_grab(); | |
f7fa9b10 | 1374 | netlink_update_subscriptions(sk, nlk->subscriptions + |
746fac4d YH |
1375 | hweight32(nladdr->nl_groups) - |
1376 | hweight32(nlk->groups[0])); | |
1377 | nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups; | |
4277a083 | 1378 | netlink_update_listeners(sk); |
1da177e4 LT |
1379 | netlink_table_ungrab(); |
1380 | ||
03292745 PNA |
1381 | if (nlk->netlink_bind && nlk->groups[0]) { |
1382 | int i; | |
1383 | ||
1384 | for (i=0; i<nlk->ngroups; i++) { | |
1385 | if (test_bit(i, nlk->groups)) | |
1386 | nlk->netlink_bind(i); | |
1387 | } | |
1388 | } | |
1389 | ||
1da177e4 LT |
1390 | return 0; |
1391 | } | |
1392 | ||
1393 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, | |
1394 | int alen, int flags) | |
1395 | { | |
1396 | int err = 0; | |
1397 | struct sock *sk = sock->sk; | |
1398 | struct netlink_sock *nlk = nlk_sk(sk); | |
6ac552fd | 1399 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
1da177e4 | 1400 | |
6503d961 CG |
1401 | if (alen < sizeof(addr->sa_family)) |
1402 | return -EINVAL; | |
1403 | ||
1da177e4 LT |
1404 | if (addr->sa_family == AF_UNSPEC) { |
1405 | sk->sk_state = NETLINK_UNCONNECTED; | |
15e47304 | 1406 | nlk->dst_portid = 0; |
d629b836 | 1407 | nlk->dst_group = 0; |
1da177e4 LT |
1408 | return 0; |
1409 | } | |
1410 | if (addr->sa_family != AF_NETLINK) | |
1411 | return -EINVAL; | |
1412 | ||
1413 | /* Only superuser is allowed to send multicasts */ | |
d806b731 | 1414 | if (nladdr->nl_groups && !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND)) |
1da177e4 LT |
1415 | return -EPERM; |
1416 | ||
15e47304 | 1417 | if (!nlk->portid) |
1da177e4 LT |
1418 | err = netlink_autobind(sock); |
1419 | ||
1420 | if (err == 0) { | |
1421 | sk->sk_state = NETLINK_CONNECTED; | |
15e47304 | 1422 | nlk->dst_portid = nladdr->nl_pid; |
d629b836 | 1423 | nlk->dst_group = ffs(nladdr->nl_groups); |
1da177e4 LT |
1424 | } |
1425 | ||
1426 | return err; | |
1427 | } | |
1428 | ||
6ac552fd PM |
1429 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, |
1430 | int *addr_len, int peer) | |
1da177e4 LT |
1431 | { |
1432 | struct sock *sk = sock->sk; | |
1433 | struct netlink_sock *nlk = nlk_sk(sk); | |
13cfa97b | 1434 | DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr); |
746fac4d | 1435 | |
1da177e4 LT |
1436 | nladdr->nl_family = AF_NETLINK; |
1437 | nladdr->nl_pad = 0; | |
1438 | *addr_len = sizeof(*nladdr); | |
1439 | ||
1440 | if (peer) { | |
15e47304 | 1441 | nladdr->nl_pid = nlk->dst_portid; |
d629b836 | 1442 | nladdr->nl_groups = netlink_group_mask(nlk->dst_group); |
1da177e4 | 1443 | } else { |
15e47304 | 1444 | nladdr->nl_pid = nlk->portid; |
513c2500 | 1445 | nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; |
1da177e4 LT |
1446 | } |
1447 | return 0; | |
1448 | } | |
1449 | ||
15e47304 | 1450 | static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid) |
1da177e4 | 1451 | { |
1da177e4 LT |
1452 | struct sock *sock; |
1453 | struct netlink_sock *nlk; | |
1454 | ||
15e47304 | 1455 | sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid); |
1da177e4 LT |
1456 | if (!sock) |
1457 | return ERR_PTR(-ECONNREFUSED); | |
1458 | ||
1459 | /* Don't bother queuing skb if kernel socket has no input function */ | |
1460 | nlk = nlk_sk(sock); | |
cd40b7d3 | 1461 | if (sock->sk_state == NETLINK_CONNECTED && |
15e47304 | 1462 | nlk->dst_portid != nlk_sk(ssk)->portid) { |
1da177e4 LT |
1463 | sock_put(sock); |
1464 | return ERR_PTR(-ECONNREFUSED); | |
1465 | } | |
1466 | return sock; | |
1467 | } | |
1468 | ||
1469 | struct sock *netlink_getsockbyfilp(struct file *filp) | |
1470 | { | |
496ad9aa | 1471 | struct inode *inode = file_inode(filp); |
1da177e4 LT |
1472 | struct sock *sock; |
1473 | ||
1474 | if (!S_ISSOCK(inode->i_mode)) | |
1475 | return ERR_PTR(-ENOTSOCK); | |
1476 | ||
1477 | sock = SOCKET_I(inode)->sk; | |
1478 | if (sock->sk_family != AF_NETLINK) | |
1479 | return ERR_PTR(-EINVAL); | |
1480 | ||
1481 | sock_hold(sock); | |
1482 | return sock; | |
1483 | } | |
1484 | ||
1485 | /* | |
1486 | * Attach a skb to a netlink socket. | |
1487 | * The caller must hold a reference to the destination socket. On error, the | |
1488 | * reference is dropped. The skb is not send to the destination, just all | |
1489 | * all error checks are performed and memory in the queue is reserved. | |
1490 | * Return values: | |
1491 | * < 0: error. skb freed, reference to sock dropped. | |
1492 | * 0: continue | |
1493 | * 1: repeat lookup - reference dropped while waiting for socket memory. | |
1494 | */ | |
9457afee | 1495 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, |
c3d8d1e3 | 1496 | long *timeo, struct sock *ssk) |
1da177e4 LT |
1497 | { |
1498 | struct netlink_sock *nlk; | |
1499 | ||
1500 | nlk = nlk_sk(sk); | |
1501 | ||
5fd96123 PM |
1502 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
1503 | test_bit(NETLINK_CONGESTED, &nlk->state)) && | |
1504 | !netlink_skb_is_mmaped(skb)) { | |
1da177e4 | 1505 | DECLARE_WAITQUEUE(wait, current); |
c3d8d1e3 | 1506 | if (!*timeo) { |
aed81560 | 1507 | if (!ssk || netlink_is_kernel(ssk)) |
1da177e4 LT |
1508 | netlink_overrun(sk); |
1509 | sock_put(sk); | |
1510 | kfree_skb(skb); | |
1511 | return -EAGAIN; | |
1512 | } | |
1513 | ||
1514 | __set_current_state(TASK_INTERRUPTIBLE); | |
1515 | add_wait_queue(&nlk->wait, &wait); | |
1516 | ||
1517 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || | |
cd967e05 | 1518 | test_bit(NETLINK_CONGESTED, &nlk->state)) && |
1da177e4 | 1519 | !sock_flag(sk, SOCK_DEAD)) |
c3d8d1e3 | 1520 | *timeo = schedule_timeout(*timeo); |
1da177e4 LT |
1521 | |
1522 | __set_current_state(TASK_RUNNING); | |
1523 | remove_wait_queue(&nlk->wait, &wait); | |
1524 | sock_put(sk); | |
1525 | ||
1526 | if (signal_pending(current)) { | |
1527 | kfree_skb(skb); | |
c3d8d1e3 | 1528 | return sock_intr_errno(*timeo); |
1da177e4 LT |
1529 | } |
1530 | return 1; | |
1531 | } | |
cf0a018a | 1532 | netlink_skb_set_owner_r(skb, sk); |
1da177e4 LT |
1533 | return 0; |
1534 | } | |
1535 | ||
4a7e7c2a | 1536 | static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
1da177e4 | 1537 | { |
1da177e4 LT |
1538 | int len = skb->len; |
1539 | ||
f9c22888 PM |
1540 | #ifdef CONFIG_NETLINK_MMAP |
1541 | if (netlink_skb_is_mmaped(skb)) | |
1542 | netlink_queue_mmaped_skb(sk, skb); | |
1543 | else if (netlink_rx_is_mmaped(sk)) | |
1544 | netlink_ring_set_copied(sk, skb); | |
1545 | else | |
1546 | #endif /* CONFIG_NETLINK_MMAP */ | |
1547 | skb_queue_tail(&sk->sk_receive_queue, skb); | |
1da177e4 | 1548 | sk->sk_data_ready(sk, len); |
4a7e7c2a ED |
1549 | return len; |
1550 | } | |
1551 | ||
1552 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb) | |
1553 | { | |
1554 | int len = __netlink_sendskb(sk, skb); | |
1555 | ||
1da177e4 LT |
1556 | sock_put(sk); |
1557 | return len; | |
1558 | } | |
1559 | ||
1560 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) | |
1561 | { | |
1562 | kfree_skb(skb); | |
1563 | sock_put(sk); | |
1564 | } | |
1565 | ||
b57ef81f | 1566 | static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation) |
1da177e4 LT |
1567 | { |
1568 | int delta; | |
1569 | ||
1298ca46 | 1570 | WARN_ON(skb->sk != NULL); |
5fd96123 PM |
1571 | if (netlink_skb_is_mmaped(skb)) |
1572 | return skb; | |
1da177e4 | 1573 | |
4305b541 | 1574 | delta = skb->end - skb->tail; |
1da177e4 LT |
1575 | if (delta * 2 < skb->truesize) |
1576 | return skb; | |
1577 | ||
1578 | if (skb_shared(skb)) { | |
1579 | struct sk_buff *nskb = skb_clone(skb, allocation); | |
1580 | if (!nskb) | |
1581 | return skb; | |
8460c00f | 1582 | consume_skb(skb); |
1da177e4 LT |
1583 | skb = nskb; |
1584 | } | |
1585 | ||
1586 | if (!pskb_expand_head(skb, 0, -delta, allocation)) | |
1587 | skb->truesize -= delta; | |
1588 | ||
1589 | return skb; | |
1590 | } | |
1591 | ||
3fbc2905 EB |
1592 | static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb, |
1593 | struct sock *ssk) | |
cd40b7d3 DL |
1594 | { |
1595 | int ret; | |
1596 | struct netlink_sock *nlk = nlk_sk(sk); | |
1597 | ||
1598 | ret = -ECONNREFUSED; | |
1599 | if (nlk->netlink_rcv != NULL) { | |
1600 | ret = skb->len; | |
cf0a018a | 1601 | netlink_skb_set_owner_r(skb, sk); |
e32123e5 | 1602 | NETLINK_CB(skb).sk = ssk; |
cd40b7d3 | 1603 | nlk->netlink_rcv(skb); |
bfb253c9 ED |
1604 | consume_skb(skb); |
1605 | } else { | |
1606 | kfree_skb(skb); | |
cd40b7d3 | 1607 | } |
cd40b7d3 DL |
1608 | sock_put(sk); |
1609 | return ret; | |
1610 | } | |
1611 | ||
1612 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, | |
15e47304 | 1613 | u32 portid, int nonblock) |
1da177e4 LT |
1614 | { |
1615 | struct sock *sk; | |
1616 | int err; | |
1617 | long timeo; | |
1618 | ||
1619 | skb = netlink_trim(skb, gfp_any()); | |
1620 | ||
1621 | timeo = sock_sndtimeo(ssk, nonblock); | |
1622 | retry: | |
15e47304 | 1623 | sk = netlink_getsockbyportid(ssk, portid); |
1da177e4 LT |
1624 | if (IS_ERR(sk)) { |
1625 | kfree_skb(skb); | |
1626 | return PTR_ERR(sk); | |
1627 | } | |
cd40b7d3 | 1628 | if (netlink_is_kernel(sk)) |
3fbc2905 | 1629 | return netlink_unicast_kernel(sk, skb, ssk); |
cd40b7d3 | 1630 | |
b1153f29 | 1631 | if (sk_filter(sk, skb)) { |
84874607 | 1632 | err = skb->len; |
b1153f29 SH |
1633 | kfree_skb(skb); |
1634 | sock_put(sk); | |
1635 | return err; | |
1636 | } | |
1637 | ||
9457afee | 1638 | err = netlink_attachskb(sk, skb, &timeo, ssk); |
1da177e4 LT |
1639 | if (err == 1) |
1640 | goto retry; | |
1641 | if (err) | |
1642 | return err; | |
1643 | ||
7ee015e0 | 1644 | return netlink_sendskb(sk, skb); |
1da177e4 | 1645 | } |
6ac552fd | 1646 | EXPORT_SYMBOL(netlink_unicast); |
1da177e4 | 1647 | |
f9c22888 PM |
1648 | struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size, |
1649 | u32 dst_portid, gfp_t gfp_mask) | |
1650 | { | |
1651 | #ifdef CONFIG_NETLINK_MMAP | |
1652 | struct sock *sk = NULL; | |
1653 | struct sk_buff *skb; | |
1654 | struct netlink_ring *ring; | |
1655 | struct nl_mmap_hdr *hdr; | |
1656 | unsigned int maxlen; | |
1657 | ||
1658 | sk = netlink_getsockbyportid(ssk, dst_portid); | |
1659 | if (IS_ERR(sk)) | |
1660 | goto out; | |
1661 | ||
1662 | ring = &nlk_sk(sk)->rx_ring; | |
1663 | /* fast-path without atomic ops for common case: non-mmaped receiver */ | |
1664 | if (ring->pg_vec == NULL) | |
1665 | goto out_put; | |
1666 | ||
1667 | skb = alloc_skb_head(gfp_mask); | |
1668 | if (skb == NULL) | |
1669 | goto err1; | |
1670 | ||
1671 | spin_lock_bh(&sk->sk_receive_queue.lock); | |
1672 | /* check again under lock */ | |
1673 | if (ring->pg_vec == NULL) | |
1674 | goto out_free; | |
1675 | ||
1676 | maxlen = ring->frame_size - NL_MMAP_HDRLEN; | |
1677 | if (maxlen < size) | |
1678 | goto out_free; | |
1679 | ||
1680 | netlink_forward_ring(ring); | |
1681 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); | |
1682 | if (hdr == NULL) | |
1683 | goto err2; | |
1684 | netlink_ring_setup_skb(skb, sk, ring, hdr); | |
1685 | netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED); | |
1686 | atomic_inc(&ring->pending); | |
1687 | netlink_increment_head(ring); | |
1688 | ||
1689 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
1690 | return skb; | |
1691 | ||
1692 | err2: | |
1693 | kfree_skb(skb); | |
1694 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
cd1df525 | 1695 | netlink_overrun(sk); |
f9c22888 PM |
1696 | err1: |
1697 | sock_put(sk); | |
1698 | return NULL; | |
1699 | ||
1700 | out_free: | |
1701 | kfree_skb(skb); | |
1702 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
1703 | out_put: | |
1704 | sock_put(sk); | |
1705 | out: | |
1706 | #endif | |
1707 | return alloc_skb(size, gfp_mask); | |
1708 | } | |
1709 | EXPORT_SYMBOL_GPL(netlink_alloc_skb); | |
1710 | ||
4277a083 PM |
1711 | int netlink_has_listeners(struct sock *sk, unsigned int group) |
1712 | { | |
1713 | int res = 0; | |
5c398dc8 | 1714 | struct listeners *listeners; |
4277a083 | 1715 | |
aed81560 | 1716 | BUG_ON(!netlink_is_kernel(sk)); |
b4ff4f04 JB |
1717 | |
1718 | rcu_read_lock(); | |
1719 | listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners); | |
1720 | ||
6d772ac5 | 1721 | if (listeners && group - 1 < nl_table[sk->sk_protocol].groups) |
5c398dc8 | 1722 | res = test_bit(group - 1, listeners->masks); |
b4ff4f04 JB |
1723 | |
1724 | rcu_read_unlock(); | |
1725 | ||
4277a083 PM |
1726 | return res; |
1727 | } | |
1728 | EXPORT_SYMBOL_GPL(netlink_has_listeners); | |
1729 | ||
b57ef81f | 1730 | static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) |
1da177e4 LT |
1731 | { |
1732 | struct netlink_sock *nlk = nlk_sk(sk); | |
1733 | ||
1734 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && | |
cd967e05 | 1735 | !test_bit(NETLINK_CONGESTED, &nlk->state)) { |
cf0a018a | 1736 | netlink_skb_set_owner_r(skb, sk); |
4a7e7c2a | 1737 | __netlink_sendskb(sk, skb); |
2c645800 | 1738 | return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1); |
1da177e4 LT |
1739 | } |
1740 | return -1; | |
1741 | } | |
1742 | ||
1743 | struct netlink_broadcast_data { | |
1744 | struct sock *exclude_sk; | |
b4b51029 | 1745 | struct net *net; |
15e47304 | 1746 | u32 portid; |
1da177e4 LT |
1747 | u32 group; |
1748 | int failure; | |
ff491a73 | 1749 | int delivery_failure; |
1da177e4 LT |
1750 | int congested; |
1751 | int delivered; | |
7d877f3b | 1752 | gfp_t allocation; |
1da177e4 | 1753 | struct sk_buff *skb, *skb2; |
910a7e90 EB |
1754 | int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data); |
1755 | void *tx_data; | |
1da177e4 LT |
1756 | }; |
1757 | ||
b57ef81f | 1758 | static int do_one_broadcast(struct sock *sk, |
1da177e4 LT |
1759 | struct netlink_broadcast_data *p) |
1760 | { | |
1761 | struct netlink_sock *nlk = nlk_sk(sk); | |
1762 | int val; | |
1763 | ||
1764 | if (p->exclude_sk == sk) | |
1765 | goto out; | |
1766 | ||
15e47304 | 1767 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
f7fa9b10 | 1768 | !test_bit(p->group - 1, nlk->groups)) |
1da177e4 LT |
1769 | goto out; |
1770 | ||
878628fb | 1771 | if (!net_eq(sock_net(sk), p->net)) |
b4b51029 EB |
1772 | goto out; |
1773 | ||
1da177e4 LT |
1774 | if (p->failure) { |
1775 | netlink_overrun(sk); | |
1776 | goto out; | |
1777 | } | |
1778 | ||
1779 | sock_hold(sk); | |
1780 | if (p->skb2 == NULL) { | |
68acc024 | 1781 | if (skb_shared(p->skb)) { |
1da177e4 LT |
1782 | p->skb2 = skb_clone(p->skb, p->allocation); |
1783 | } else { | |
68acc024 TC |
1784 | p->skb2 = skb_get(p->skb); |
1785 | /* | |
1786 | * skb ownership may have been set when | |
1787 | * delivered to a previous socket. | |
1788 | */ | |
1789 | skb_orphan(p->skb2); | |
1da177e4 LT |
1790 | } |
1791 | } | |
1792 | if (p->skb2 == NULL) { | |
1793 | netlink_overrun(sk); | |
1794 | /* Clone failed. Notify ALL listeners. */ | |
1795 | p->failure = 1; | |
be0c22a4 PNA |
1796 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
1797 | p->delivery_failure = 1; | |
910a7e90 EB |
1798 | } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) { |
1799 | kfree_skb(p->skb2); | |
1800 | p->skb2 = NULL; | |
b1153f29 SH |
1801 | } else if (sk_filter(sk, p->skb2)) { |
1802 | kfree_skb(p->skb2); | |
1803 | p->skb2 = NULL; | |
1da177e4 LT |
1804 | } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) { |
1805 | netlink_overrun(sk); | |
be0c22a4 PNA |
1806 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
1807 | p->delivery_failure = 1; | |
1da177e4 LT |
1808 | } else { |
1809 | p->congested |= val; | |
1810 | p->delivered = 1; | |
1811 | p->skb2 = NULL; | |
1812 | } | |
1813 | sock_put(sk); | |
1814 | ||
1815 | out: | |
1816 | return 0; | |
1817 | } | |
1818 | ||
15e47304 | 1819 | int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid, |
910a7e90 EB |
1820 | u32 group, gfp_t allocation, |
1821 | int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data), | |
1822 | void *filter_data) | |
1da177e4 | 1823 | { |
3b1e0a65 | 1824 | struct net *net = sock_net(ssk); |
1da177e4 | 1825 | struct netlink_broadcast_data info; |
1da177e4 LT |
1826 | struct sock *sk; |
1827 | ||
1828 | skb = netlink_trim(skb, allocation); | |
1829 | ||
1830 | info.exclude_sk = ssk; | |
b4b51029 | 1831 | info.net = net; |
15e47304 | 1832 | info.portid = portid; |
1da177e4 LT |
1833 | info.group = group; |
1834 | info.failure = 0; | |
ff491a73 | 1835 | info.delivery_failure = 0; |
1da177e4 LT |
1836 | info.congested = 0; |
1837 | info.delivered = 0; | |
1838 | info.allocation = allocation; | |
1839 | info.skb = skb; | |
1840 | info.skb2 = NULL; | |
910a7e90 EB |
1841 | info.tx_filter = filter; |
1842 | info.tx_data = filter_data; | |
1da177e4 LT |
1843 | |
1844 | /* While we sleep in clone, do not allow to change socket list */ | |
1845 | ||
1846 | netlink_lock_table(); | |
1847 | ||
b67bfe0d | 1848 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
1da177e4 LT |
1849 | do_one_broadcast(sk, &info); |
1850 | ||
70d4bf6d | 1851 | consume_skb(skb); |
aa1c6a6f | 1852 | |
1da177e4 LT |
1853 | netlink_unlock_table(); |
1854 | ||
70d4bf6d NH |
1855 | if (info.delivery_failure) { |
1856 | kfree_skb(info.skb2); | |
ff491a73 | 1857 | return -ENOBUFS; |
658cb354 ED |
1858 | } |
1859 | consume_skb(info.skb2); | |
ff491a73 | 1860 | |
1da177e4 LT |
1861 | if (info.delivered) { |
1862 | if (info.congested && (allocation & __GFP_WAIT)) | |
1863 | yield(); | |
1864 | return 0; | |
1865 | } | |
1da177e4 LT |
1866 | return -ESRCH; |
1867 | } | |
910a7e90 EB |
1868 | EXPORT_SYMBOL(netlink_broadcast_filtered); |
1869 | ||
15e47304 | 1870 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid, |
910a7e90 EB |
1871 | u32 group, gfp_t allocation) |
1872 | { | |
15e47304 | 1873 | return netlink_broadcast_filtered(ssk, skb, portid, group, allocation, |
910a7e90 EB |
1874 | NULL, NULL); |
1875 | } | |
6ac552fd | 1876 | EXPORT_SYMBOL(netlink_broadcast); |
1da177e4 LT |
1877 | |
1878 | struct netlink_set_err_data { | |
1879 | struct sock *exclude_sk; | |
15e47304 | 1880 | u32 portid; |
1da177e4 LT |
1881 | u32 group; |
1882 | int code; | |
1883 | }; | |
1884 | ||
b57ef81f | 1885 | static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p) |
1da177e4 LT |
1886 | { |
1887 | struct netlink_sock *nlk = nlk_sk(sk); | |
1a50307b | 1888 | int ret = 0; |
1da177e4 LT |
1889 | |
1890 | if (sk == p->exclude_sk) | |
1891 | goto out; | |
1892 | ||
09ad9bc7 | 1893 | if (!net_eq(sock_net(sk), sock_net(p->exclude_sk))) |
b4b51029 EB |
1894 | goto out; |
1895 | ||
15e47304 | 1896 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
f7fa9b10 | 1897 | !test_bit(p->group - 1, nlk->groups)) |
1da177e4 LT |
1898 | goto out; |
1899 | ||
1a50307b PNA |
1900 | if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) { |
1901 | ret = 1; | |
1902 | goto out; | |
1903 | } | |
1904 | ||
1da177e4 LT |
1905 | sk->sk_err = p->code; |
1906 | sk->sk_error_report(sk); | |
1907 | out: | |
1a50307b | 1908 | return ret; |
1da177e4 LT |
1909 | } |
1910 | ||
4843b93c PNA |
1911 | /** |
1912 | * netlink_set_err - report error to broadcast listeners | |
1913 | * @ssk: the kernel netlink socket, as returned by netlink_kernel_create() | |
15e47304 | 1914 | * @portid: the PORTID of a process that we want to skip (if any) |
4843b93c PNA |
1915 | * @groups: the broadcast group that will notice the error |
1916 | * @code: error code, must be negative (as usual in kernelspace) | |
1a50307b PNA |
1917 | * |
1918 | * This function returns the number of broadcast listeners that have set the | |
1919 | * NETLINK_RECV_NO_ENOBUFS socket option. | |
4843b93c | 1920 | */ |
15e47304 | 1921 | int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code) |
1da177e4 LT |
1922 | { |
1923 | struct netlink_set_err_data info; | |
1da177e4 | 1924 | struct sock *sk; |
1a50307b | 1925 | int ret = 0; |
1da177e4 LT |
1926 | |
1927 | info.exclude_sk = ssk; | |
15e47304 | 1928 | info.portid = portid; |
1da177e4 | 1929 | info.group = group; |
4843b93c PNA |
1930 | /* sk->sk_err wants a positive error value */ |
1931 | info.code = -code; | |
1da177e4 LT |
1932 | |
1933 | read_lock(&nl_table_lock); | |
1934 | ||
b67bfe0d | 1935 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
1a50307b | 1936 | ret += do_one_set_err(sk, &info); |
1da177e4 LT |
1937 | |
1938 | read_unlock(&nl_table_lock); | |
1a50307b | 1939 | return ret; |
1da177e4 | 1940 | } |
dd5b6ce6 | 1941 | EXPORT_SYMBOL(netlink_set_err); |
1da177e4 | 1942 | |
84659eb5 JB |
1943 | /* must be called with netlink table grabbed */ |
1944 | static void netlink_update_socket_mc(struct netlink_sock *nlk, | |
1945 | unsigned int group, | |
1946 | int is_new) | |
1947 | { | |
1948 | int old, new = !!is_new, subscriptions; | |
1949 | ||
1950 | old = test_bit(group - 1, nlk->groups); | |
1951 | subscriptions = nlk->subscriptions - old + new; | |
1952 | if (new) | |
1953 | __set_bit(group - 1, nlk->groups); | |
1954 | else | |
1955 | __clear_bit(group - 1, nlk->groups); | |
1956 | netlink_update_subscriptions(&nlk->sk, subscriptions); | |
1957 | netlink_update_listeners(&nlk->sk); | |
1958 | } | |
1959 | ||
9a4595bc | 1960 | static int netlink_setsockopt(struct socket *sock, int level, int optname, |
b7058842 | 1961 | char __user *optval, unsigned int optlen) |
9a4595bc PM |
1962 | { |
1963 | struct sock *sk = sock->sk; | |
1964 | struct netlink_sock *nlk = nlk_sk(sk); | |
eb496534 JB |
1965 | unsigned int val = 0; |
1966 | int err; | |
9a4595bc PM |
1967 | |
1968 | if (level != SOL_NETLINK) | |
1969 | return -ENOPROTOOPT; | |
1970 | ||
ccdfcc39 PM |
1971 | if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING && |
1972 | optlen >= sizeof(int) && | |
eb496534 | 1973 | get_user(val, (unsigned int __user *)optval)) |
9a4595bc PM |
1974 | return -EFAULT; |
1975 | ||
1976 | switch (optname) { | |
1977 | case NETLINK_PKTINFO: | |
1978 | if (val) | |
1979 | nlk->flags |= NETLINK_RECV_PKTINFO; | |
1980 | else | |
1981 | nlk->flags &= ~NETLINK_RECV_PKTINFO; | |
1982 | err = 0; | |
1983 | break; | |
1984 | case NETLINK_ADD_MEMBERSHIP: | |
1985 | case NETLINK_DROP_MEMBERSHIP: { | |
d806b731 | 1986 | if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV)) |
9a4595bc | 1987 | return -EPERM; |
b4ff4f04 JB |
1988 | err = netlink_realloc_groups(sk); |
1989 | if (err) | |
1990 | return err; | |
9a4595bc PM |
1991 | if (!val || val - 1 >= nlk->ngroups) |
1992 | return -EINVAL; | |
1993 | netlink_table_grab(); | |
84659eb5 JB |
1994 | netlink_update_socket_mc(nlk, val, |
1995 | optname == NETLINK_ADD_MEMBERSHIP); | |
9a4595bc | 1996 | netlink_table_ungrab(); |
03292745 PNA |
1997 | |
1998 | if (nlk->netlink_bind) | |
1999 | nlk->netlink_bind(val); | |
2000 | ||
9a4595bc PM |
2001 | err = 0; |
2002 | break; | |
2003 | } | |
be0c22a4 PNA |
2004 | case NETLINK_BROADCAST_ERROR: |
2005 | if (val) | |
2006 | nlk->flags |= NETLINK_BROADCAST_SEND_ERROR; | |
2007 | else | |
2008 | nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR; | |
2009 | err = 0; | |
2010 | break; | |
38938bfe PNA |
2011 | case NETLINK_NO_ENOBUFS: |
2012 | if (val) { | |
2013 | nlk->flags |= NETLINK_RECV_NO_ENOBUFS; | |
cd967e05 | 2014 | clear_bit(NETLINK_CONGESTED, &nlk->state); |
38938bfe | 2015 | wake_up_interruptible(&nlk->wait); |
658cb354 | 2016 | } else { |
38938bfe | 2017 | nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS; |
658cb354 | 2018 | } |
38938bfe PNA |
2019 | err = 0; |
2020 | break; | |
ccdfcc39 PM |
2021 | #ifdef CONFIG_NETLINK_MMAP |
2022 | case NETLINK_RX_RING: | |
2023 | case NETLINK_TX_RING: { | |
2024 | struct nl_mmap_req req; | |
2025 | ||
2026 | /* Rings might consume more memory than queue limits, require | |
2027 | * CAP_NET_ADMIN. | |
2028 | */ | |
2029 | if (!capable(CAP_NET_ADMIN)) | |
2030 | return -EPERM; | |
2031 | if (optlen < sizeof(req)) | |
2032 | return -EINVAL; | |
2033 | if (copy_from_user(&req, optval, sizeof(req))) | |
2034 | return -EFAULT; | |
3ebe377b | 2035 | err = netlink_set_ring(sk, &req, |
ccdfcc39 PM |
2036 | optname == NETLINK_TX_RING); |
2037 | break; | |
2038 | } | |
2039 | #endif /* CONFIG_NETLINK_MMAP */ | |
9a4595bc PM |
2040 | default: |
2041 | err = -ENOPROTOOPT; | |
2042 | } | |
2043 | return err; | |
2044 | } | |
2045 | ||
2046 | static int netlink_getsockopt(struct socket *sock, int level, int optname, | |
746fac4d | 2047 | char __user *optval, int __user *optlen) |
9a4595bc PM |
2048 | { |
2049 | struct sock *sk = sock->sk; | |
2050 | struct netlink_sock *nlk = nlk_sk(sk); | |
2051 | int len, val, err; | |
2052 | ||
2053 | if (level != SOL_NETLINK) | |
2054 | return -ENOPROTOOPT; | |
2055 | ||
2056 | if (get_user(len, optlen)) | |
2057 | return -EFAULT; | |
2058 | if (len < 0) | |
2059 | return -EINVAL; | |
2060 | ||
2061 | switch (optname) { | |
2062 | case NETLINK_PKTINFO: | |
2063 | if (len < sizeof(int)) | |
2064 | return -EINVAL; | |
2065 | len = sizeof(int); | |
2066 | val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0; | |
a27b58fe HC |
2067 | if (put_user(len, optlen) || |
2068 | put_user(val, optval)) | |
2069 | return -EFAULT; | |
9a4595bc PM |
2070 | err = 0; |
2071 | break; | |
be0c22a4 PNA |
2072 | case NETLINK_BROADCAST_ERROR: |
2073 | if (len < sizeof(int)) | |
2074 | return -EINVAL; | |
2075 | len = sizeof(int); | |
2076 | val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0; | |
2077 | if (put_user(len, optlen) || | |
2078 | put_user(val, optval)) | |
2079 | return -EFAULT; | |
2080 | err = 0; | |
2081 | break; | |
38938bfe PNA |
2082 | case NETLINK_NO_ENOBUFS: |
2083 | if (len < sizeof(int)) | |
2084 | return -EINVAL; | |
2085 | len = sizeof(int); | |
2086 | val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0; | |
2087 | if (put_user(len, optlen) || | |
2088 | put_user(val, optval)) | |
2089 | return -EFAULT; | |
2090 | err = 0; | |
2091 | break; | |
9a4595bc PM |
2092 | default: |
2093 | err = -ENOPROTOOPT; | |
2094 | } | |
2095 | return err; | |
2096 | } | |
2097 | ||
2098 | static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
2099 | { | |
2100 | struct nl_pktinfo info; | |
2101 | ||
2102 | info.group = NETLINK_CB(skb).dst_group; | |
2103 | put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); | |
2104 | } | |
2105 | ||
1da177e4 LT |
2106 | static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, |
2107 | struct msghdr *msg, size_t len) | |
2108 | { | |
2109 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | |
2110 | struct sock *sk = sock->sk; | |
2111 | struct netlink_sock *nlk = nlk_sk(sk); | |
6ac552fd | 2112 | struct sockaddr_nl *addr = msg->msg_name; |
15e47304 | 2113 | u32 dst_portid; |
d629b836 | 2114 | u32 dst_group; |
1da177e4 LT |
2115 | struct sk_buff *skb; |
2116 | int err; | |
2117 | struct scm_cookie scm; | |
8ab02acd | 2118 | u32 netlink_skb_flags = 0; |
1da177e4 LT |
2119 | |
2120 | if (msg->msg_flags&MSG_OOB) | |
2121 | return -EOPNOTSUPP; | |
2122 | ||
16e57262 | 2123 | if (NULL == siocb->scm) |
1da177e4 | 2124 | siocb->scm = &scm; |
16e57262 | 2125 | |
e0e3cea4 | 2126 | err = scm_send(sock, msg, siocb->scm, true); |
1da177e4 LT |
2127 | if (err < 0) |
2128 | return err; | |
2129 | ||
2130 | if (msg->msg_namelen) { | |
b47030c7 | 2131 | err = -EINVAL; |
1da177e4 | 2132 | if (addr->nl_family != AF_NETLINK) |
b47030c7 | 2133 | goto out; |
15e47304 | 2134 | dst_portid = addr->nl_pid; |
d629b836 | 2135 | dst_group = ffs(addr->nl_groups); |
b47030c7 | 2136 | err = -EPERM; |
15e47304 | 2137 | if ((dst_group || dst_portid) && |
d806b731 | 2138 | !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND)) |
b47030c7 | 2139 | goto out; |
8ab02acd | 2140 | netlink_skb_flags |= NETLINK_SKB_DST; |
1da177e4 | 2141 | } else { |
15e47304 | 2142 | dst_portid = nlk->dst_portid; |
d629b836 | 2143 | dst_group = nlk->dst_group; |
1da177e4 LT |
2144 | } |
2145 | ||
15e47304 | 2146 | if (!nlk->portid) { |
1da177e4 LT |
2147 | err = netlink_autobind(sock); |
2148 | if (err) | |
2149 | goto out; | |
2150 | } | |
2151 | ||
5fd96123 PM |
2152 | if (netlink_tx_is_mmaped(sk) && |
2153 | msg->msg_iov->iov_base == NULL) { | |
2154 | err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, | |
2155 | siocb); | |
2156 | goto out; | |
2157 | } | |
2158 | ||
1da177e4 LT |
2159 | err = -EMSGSIZE; |
2160 | if (len > sk->sk_sndbuf - 32) | |
2161 | goto out; | |
2162 | err = -ENOBUFS; | |
339bf98f | 2163 | skb = alloc_skb(len, GFP_KERNEL); |
6ac552fd | 2164 | if (skb == NULL) |
1da177e4 LT |
2165 | goto out; |
2166 | ||
15e47304 | 2167 | NETLINK_CB(skb).portid = nlk->portid; |
d629b836 | 2168 | NETLINK_CB(skb).dst_group = dst_group; |
dbe9a417 | 2169 | NETLINK_CB(skb).creds = siocb->scm->creds; |
8ab02acd | 2170 | NETLINK_CB(skb).flags = netlink_skb_flags; |
1da177e4 | 2171 | |
1da177e4 | 2172 | err = -EFAULT; |
6ac552fd | 2173 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
1da177e4 LT |
2174 | kfree_skb(skb); |
2175 | goto out; | |
2176 | } | |
2177 | ||
2178 | err = security_netlink_send(sk, skb); | |
2179 | if (err) { | |
2180 | kfree_skb(skb); | |
2181 | goto out; | |
2182 | } | |
2183 | ||
d629b836 | 2184 | if (dst_group) { |
1da177e4 | 2185 | atomic_inc(&skb->users); |
15e47304 | 2186 | netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL); |
1da177e4 | 2187 | } |
15e47304 | 2188 | err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT); |
1da177e4 LT |
2189 | |
2190 | out: | |
b47030c7 | 2191 | scm_destroy(siocb->scm); |
1da177e4 LT |
2192 | return err; |
2193 | } | |
2194 | ||
2195 | static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, | |
2196 | struct msghdr *msg, size_t len, | |
2197 | int flags) | |
2198 | { | |
2199 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | |
2200 | struct scm_cookie scm; | |
2201 | struct sock *sk = sock->sk; | |
2202 | struct netlink_sock *nlk = nlk_sk(sk); | |
2203 | int noblock = flags&MSG_DONTWAIT; | |
2204 | size_t copied; | |
68d6ac6d | 2205 | struct sk_buff *skb, *data_skb; |
b44d211e | 2206 | int err, ret; |
1da177e4 LT |
2207 | |
2208 | if (flags&MSG_OOB) | |
2209 | return -EOPNOTSUPP; | |
2210 | ||
2211 | copied = 0; | |
2212 | ||
6ac552fd PM |
2213 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
2214 | if (skb == NULL) | |
1da177e4 LT |
2215 | goto out; |
2216 | ||
68d6ac6d JB |
2217 | data_skb = skb; |
2218 | ||
1dacc76d JB |
2219 | #ifdef CONFIG_COMPAT_NETLINK_MESSAGES |
2220 | if (unlikely(skb_shinfo(skb)->frag_list)) { | |
1dacc76d | 2221 | /* |
68d6ac6d JB |
2222 | * If this skb has a frag_list, then here that means that we |
2223 | * will have to use the frag_list skb's data for compat tasks | |
2224 | * and the regular skb's data for normal (non-compat) tasks. | |
1dacc76d | 2225 | * |
68d6ac6d JB |
2226 | * If we need to send the compat skb, assign it to the |
2227 | * 'data_skb' variable so that it will be used below for data | |
2228 | * copying. We keep 'skb' for everything else, including | |
2229 | * freeing both later. | |
1dacc76d | 2230 | */ |
68d6ac6d JB |
2231 | if (flags & MSG_CMSG_COMPAT) |
2232 | data_skb = skb_shinfo(skb)->frag_list; | |
1dacc76d JB |
2233 | } |
2234 | #endif | |
2235 | ||
68d6ac6d | 2236 | copied = data_skb->len; |
1da177e4 LT |
2237 | if (len < copied) { |
2238 | msg->msg_flags |= MSG_TRUNC; | |
2239 | copied = len; | |
2240 | } | |
2241 | ||
68d6ac6d JB |
2242 | skb_reset_transport_header(data_skb); |
2243 | err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied); | |
1da177e4 LT |
2244 | |
2245 | if (msg->msg_name) { | |
6ac552fd | 2246 | struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name; |
1da177e4 LT |
2247 | addr->nl_family = AF_NETLINK; |
2248 | addr->nl_pad = 0; | |
15e47304 | 2249 | addr->nl_pid = NETLINK_CB(skb).portid; |
d629b836 | 2250 | addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); |
1da177e4 LT |
2251 | msg->msg_namelen = sizeof(*addr); |
2252 | } | |
2253 | ||
cc9a06cd PM |
2254 | if (nlk->flags & NETLINK_RECV_PKTINFO) |
2255 | netlink_cmsg_recv_pktinfo(msg, skb); | |
2256 | ||
1da177e4 LT |
2257 | if (NULL == siocb->scm) { |
2258 | memset(&scm, 0, sizeof(scm)); | |
2259 | siocb->scm = &scm; | |
2260 | } | |
2261 | siocb->scm->creds = *NETLINK_CREDS(skb); | |
188ccb55 | 2262 | if (flags & MSG_TRUNC) |
68d6ac6d | 2263 | copied = data_skb->len; |
daa3766e | 2264 | |
1da177e4 LT |
2265 | skb_free_datagram(sk, skb); |
2266 | ||
b44d211e AV |
2267 | if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) { |
2268 | ret = netlink_dump(sk); | |
2269 | if (ret) { | |
c29503e9 | 2270 | sk->sk_err = -ret; |
b44d211e AV |
2271 | sk->sk_error_report(sk); |
2272 | } | |
2273 | } | |
1da177e4 LT |
2274 | |
2275 | scm_recv(sock, msg, siocb->scm, flags); | |
1da177e4 LT |
2276 | out: |
2277 | netlink_rcv_wake(sk); | |
2278 | return err ? : copied; | |
2279 | } | |
2280 | ||
2281 | static void netlink_data_ready(struct sock *sk, int len) | |
2282 | { | |
cd40b7d3 | 2283 | BUG(); |
1da177e4 LT |
2284 | } |
2285 | ||
2286 | /* | |
746fac4d | 2287 | * We export these functions to other modules. They provide a |
1da177e4 LT |
2288 | * complete set of kernel non-blocking support for message |
2289 | * queueing. | |
2290 | */ | |
2291 | ||
2292 | struct sock * | |
9f00d977 PNA |
2293 | __netlink_kernel_create(struct net *net, int unit, struct module *module, |
2294 | struct netlink_kernel_cfg *cfg) | |
1da177e4 LT |
2295 | { |
2296 | struct socket *sock; | |
2297 | struct sock *sk; | |
77247bbb | 2298 | struct netlink_sock *nlk; |
5c398dc8 | 2299 | struct listeners *listeners = NULL; |
a31f2d17 PNA |
2300 | struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL; |
2301 | unsigned int groups; | |
1da177e4 | 2302 | |
fab2caf6 | 2303 | BUG_ON(!nl_table); |
1da177e4 | 2304 | |
6ac552fd | 2305 | if (unit < 0 || unit >= MAX_LINKS) |
1da177e4 LT |
2306 | return NULL; |
2307 | ||
2308 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) | |
2309 | return NULL; | |
2310 | ||
23fe1866 PE |
2311 | /* |
2312 | * We have to just have a reference on the net from sk, but don't | |
2313 | * get_net it. Besides, we cannot get and then put the net here. | |
2314 | * So we create one inside init_net and the move it to net. | |
2315 | */ | |
2316 | ||
2317 | if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0) | |
2318 | goto out_sock_release_nosk; | |
2319 | ||
2320 | sk = sock->sk; | |
edf02087 | 2321 | sk_change_net(sk, net); |
4fdb3bb7 | 2322 | |
a31f2d17 | 2323 | if (!cfg || cfg->groups < 32) |
4277a083 | 2324 | groups = 32; |
a31f2d17 PNA |
2325 | else |
2326 | groups = cfg->groups; | |
4277a083 | 2327 | |
5c398dc8 | 2328 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
4277a083 PM |
2329 | if (!listeners) |
2330 | goto out_sock_release; | |
2331 | ||
1da177e4 | 2332 | sk->sk_data_ready = netlink_data_ready; |
a31f2d17 PNA |
2333 | if (cfg && cfg->input) |
2334 | nlk_sk(sk)->netlink_rcv = cfg->input; | |
1da177e4 | 2335 | |
b4b51029 | 2336 | if (netlink_insert(sk, net, 0)) |
77247bbb | 2337 | goto out_sock_release; |
4fdb3bb7 | 2338 | |
77247bbb PM |
2339 | nlk = nlk_sk(sk); |
2340 | nlk->flags |= NETLINK_KERNEL_SOCKET; | |
4fdb3bb7 | 2341 | |
4fdb3bb7 | 2342 | netlink_table_grab(); |
b4b51029 EB |
2343 | if (!nl_table[unit].registered) { |
2344 | nl_table[unit].groups = groups; | |
5c398dc8 | 2345 | rcu_assign_pointer(nl_table[unit].listeners, listeners); |
b4b51029 EB |
2346 | nl_table[unit].cb_mutex = cb_mutex; |
2347 | nl_table[unit].module = module; | |
9785e10a PNA |
2348 | if (cfg) { |
2349 | nl_table[unit].bind = cfg->bind; | |
2350 | nl_table[unit].flags = cfg->flags; | |
2351 | } | |
b4b51029 | 2352 | nl_table[unit].registered = 1; |
f937f1f4 JJ |
2353 | } else { |
2354 | kfree(listeners); | |
869e58f8 | 2355 | nl_table[unit].registered++; |
b4b51029 | 2356 | } |
4fdb3bb7 | 2357 | netlink_table_ungrab(); |
77247bbb PM |
2358 | return sk; |
2359 | ||
4fdb3bb7 | 2360 | out_sock_release: |
4277a083 | 2361 | kfree(listeners); |
9dfbec1f | 2362 | netlink_kernel_release(sk); |
23fe1866 PE |
2363 | return NULL; |
2364 | ||
2365 | out_sock_release_nosk: | |
4fdb3bb7 | 2366 | sock_release(sock); |
77247bbb | 2367 | return NULL; |
1da177e4 | 2368 | } |
9f00d977 | 2369 | EXPORT_SYMBOL(__netlink_kernel_create); |
b7c6ba6e DL |
2370 | |
2371 | void | |
2372 | netlink_kernel_release(struct sock *sk) | |
2373 | { | |
edf02087 | 2374 | sk_release_kernel(sk); |
b7c6ba6e DL |
2375 | } |
2376 | EXPORT_SYMBOL(netlink_kernel_release); | |
2377 | ||
d136f1bd | 2378 | int __netlink_change_ngroups(struct sock *sk, unsigned int groups) |
b4ff4f04 | 2379 | { |
5c398dc8 | 2380 | struct listeners *new, *old; |
b4ff4f04 | 2381 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
b4ff4f04 JB |
2382 | |
2383 | if (groups < 32) | |
2384 | groups = 32; | |
2385 | ||
b4ff4f04 | 2386 | if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) { |
5c398dc8 ED |
2387 | new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC); |
2388 | if (!new) | |
d136f1bd | 2389 | return -ENOMEM; |
6d772ac5 | 2390 | old = nl_deref_protected(tbl->listeners); |
5c398dc8 ED |
2391 | memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups)); |
2392 | rcu_assign_pointer(tbl->listeners, new); | |
2393 | ||
37b6b935 | 2394 | kfree_rcu(old, rcu); |
b4ff4f04 JB |
2395 | } |
2396 | tbl->groups = groups; | |
2397 | ||
d136f1bd JB |
2398 | return 0; |
2399 | } | |
2400 | ||
2401 | /** | |
2402 | * netlink_change_ngroups - change number of multicast groups | |
2403 | * | |
2404 | * This changes the number of multicast groups that are available | |
2405 | * on a certain netlink family. Note that it is not possible to | |
2406 | * change the number of groups to below 32. Also note that it does | |
2407 | * not implicitly call netlink_clear_multicast_users() when the | |
2408 | * number of groups is reduced. | |
2409 | * | |
2410 | * @sk: The kernel netlink socket, as returned by netlink_kernel_create(). | |
2411 | * @groups: The new number of groups. | |
2412 | */ | |
2413 | int netlink_change_ngroups(struct sock *sk, unsigned int groups) | |
2414 | { | |
2415 | int err; | |
2416 | ||
2417 | netlink_table_grab(); | |
2418 | err = __netlink_change_ngroups(sk, groups); | |
b4ff4f04 | 2419 | netlink_table_ungrab(); |
d136f1bd | 2420 | |
b4ff4f04 JB |
2421 | return err; |
2422 | } | |
b4ff4f04 | 2423 | |
b8273570 JB |
2424 | void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group) |
2425 | { | |
2426 | struct sock *sk; | |
b8273570 JB |
2427 | struct netlink_table *tbl = &nl_table[ksk->sk_protocol]; |
2428 | ||
b67bfe0d | 2429 | sk_for_each_bound(sk, &tbl->mc_list) |
b8273570 JB |
2430 | netlink_update_socket_mc(nlk_sk(sk), group, 0); |
2431 | } | |
2432 | ||
84659eb5 JB |
2433 | /** |
2434 | * netlink_clear_multicast_users - kick off multicast listeners | |
2435 | * | |
2436 | * This function removes all listeners from the given group. | |
2437 | * @ksk: The kernel netlink socket, as returned by | |
2438 | * netlink_kernel_create(). | |
2439 | * @group: The multicast group to clear. | |
2440 | */ | |
2441 | void netlink_clear_multicast_users(struct sock *ksk, unsigned int group) | |
2442 | { | |
84659eb5 | 2443 | netlink_table_grab(); |
b8273570 | 2444 | __netlink_clear_multicast_users(ksk, group); |
84659eb5 JB |
2445 | netlink_table_ungrab(); |
2446 | } | |
84659eb5 | 2447 | |
a46621a3 | 2448 | struct nlmsghdr * |
15e47304 | 2449 | __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags) |
a46621a3 DV |
2450 | { |
2451 | struct nlmsghdr *nlh; | |
573ce260 | 2452 | int size = nlmsg_msg_size(len); |
a46621a3 DV |
2453 | |
2454 | nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size)); | |
2455 | nlh->nlmsg_type = type; | |
2456 | nlh->nlmsg_len = size; | |
2457 | nlh->nlmsg_flags = flags; | |
15e47304 | 2458 | nlh->nlmsg_pid = portid; |
a46621a3 DV |
2459 | nlh->nlmsg_seq = seq; |
2460 | if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0) | |
573ce260 | 2461 | memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size); |
a46621a3 DV |
2462 | return nlh; |
2463 | } | |
2464 | EXPORT_SYMBOL(__nlmsg_put); | |
2465 | ||
1da177e4 LT |
2466 | /* |
2467 | * It looks a bit ugly. | |
2468 | * It would be better to create kernel thread. | |
2469 | */ | |
2470 | ||
2471 | static int netlink_dump(struct sock *sk) | |
2472 | { | |
2473 | struct netlink_sock *nlk = nlk_sk(sk); | |
2474 | struct netlink_callback *cb; | |
c7ac8679 | 2475 | struct sk_buff *skb = NULL; |
1da177e4 | 2476 | struct nlmsghdr *nlh; |
bf8b79e4 | 2477 | int len, err = -ENOBUFS; |
c7ac8679 | 2478 | int alloc_size; |
1da177e4 | 2479 | |
af65bdfc | 2480 | mutex_lock(nlk->cb_mutex); |
1da177e4 LT |
2481 | |
2482 | cb = nlk->cb; | |
2483 | if (cb == NULL) { | |
bf8b79e4 TG |
2484 | err = -EINVAL; |
2485 | goto errout_skb; | |
1da177e4 LT |
2486 | } |
2487 | ||
c7ac8679 GR |
2488 | alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); |
2489 | ||
f9c22888 PM |
2490 | if (!netlink_rx_is_mmaped(sk) && |
2491 | atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) | |
2492 | goto errout_skb; | |
2493 | skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, GFP_KERNEL); | |
c7ac8679 | 2494 | if (!skb) |
c63d6ea3 | 2495 | goto errout_skb; |
f9c22888 | 2496 | netlink_skb_set_owner_r(skb, sk); |
c7ac8679 | 2497 | |
1da177e4 LT |
2498 | len = cb->dump(skb, cb); |
2499 | ||
2500 | if (len > 0) { | |
af65bdfc | 2501 | mutex_unlock(nlk->cb_mutex); |
b1153f29 SH |
2502 | |
2503 | if (sk_filter(sk, skb)) | |
2504 | kfree_skb(skb); | |
4a7e7c2a ED |
2505 | else |
2506 | __netlink_sendskb(sk, skb); | |
1da177e4 LT |
2507 | return 0; |
2508 | } | |
2509 | ||
bf8b79e4 TG |
2510 | nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI); |
2511 | if (!nlh) | |
2512 | goto errout_skb; | |
2513 | ||
670dc283 JB |
2514 | nl_dump_check_consistent(cb, nlh); |
2515 | ||
bf8b79e4 TG |
2516 | memcpy(nlmsg_data(nlh), &len, sizeof(len)); |
2517 | ||
b1153f29 SH |
2518 | if (sk_filter(sk, skb)) |
2519 | kfree_skb(skb); | |
4a7e7c2a ED |
2520 | else |
2521 | __netlink_sendskb(sk, skb); | |
1da177e4 | 2522 | |
a8f74b22 TG |
2523 | if (cb->done) |
2524 | cb->done(cb); | |
1da177e4 | 2525 | nlk->cb = NULL; |
af65bdfc | 2526 | mutex_unlock(nlk->cb_mutex); |
1da177e4 | 2527 | |
6dc878a8 | 2528 | module_put(cb->module); |
bfb253c9 | 2529 | netlink_consume_callback(cb); |
1da177e4 | 2530 | return 0; |
1797754e | 2531 | |
bf8b79e4 | 2532 | errout_skb: |
af65bdfc | 2533 | mutex_unlock(nlk->cb_mutex); |
bf8b79e4 | 2534 | kfree_skb(skb); |
bf8b79e4 | 2535 | return err; |
1da177e4 LT |
2536 | } |
2537 | ||
6dc878a8 G |
2538 | int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb, |
2539 | const struct nlmsghdr *nlh, | |
2540 | struct netlink_dump_control *control) | |
1da177e4 LT |
2541 | { |
2542 | struct netlink_callback *cb; | |
2543 | struct sock *sk; | |
2544 | struct netlink_sock *nlk; | |
b44d211e | 2545 | int ret; |
1da177e4 | 2546 | |
0da974f4 | 2547 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
1da177e4 LT |
2548 | if (cb == NULL) |
2549 | return -ENOBUFS; | |
2550 | ||
f9c22888 PM |
2551 | /* Memory mapped dump requests need to be copied to avoid looping |
2552 | * on the pending state in netlink_mmap_sendmsg() while the CB hold | |
2553 | * a reference to the skb. | |
2554 | */ | |
2555 | if (netlink_skb_is_mmaped(skb)) { | |
2556 | skb = skb_copy(skb, GFP_KERNEL); | |
2557 | if (skb == NULL) { | |
2558 | kfree(cb); | |
2559 | return -ENOBUFS; | |
2560 | } | |
2561 | } else | |
2562 | atomic_inc(&skb->users); | |
2563 | ||
80d326fa PNA |
2564 | cb->dump = control->dump; |
2565 | cb->done = control->done; | |
1da177e4 | 2566 | cb->nlh = nlh; |
7175c883 | 2567 | cb->data = control->data; |
6dc878a8 | 2568 | cb->module = control->module; |
80d326fa | 2569 | cb->min_dump_alloc = control->min_dump_alloc; |
1da177e4 LT |
2570 | cb->skb = skb; |
2571 | ||
15e47304 | 2572 | sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid); |
1da177e4 LT |
2573 | if (sk == NULL) { |
2574 | netlink_destroy_callback(cb); | |
2575 | return -ECONNREFUSED; | |
2576 | } | |
2577 | nlk = nlk_sk(sk); | |
6dc878a8 | 2578 | |
af65bdfc | 2579 | mutex_lock(nlk->cb_mutex); |
6dc878a8 | 2580 | /* A dump is in progress... */ |
3f660d66 | 2581 | if (nlk->cb) { |
af65bdfc | 2582 | mutex_unlock(nlk->cb_mutex); |
1da177e4 | 2583 | netlink_destroy_callback(cb); |
6dc878a8 G |
2584 | ret = -EBUSY; |
2585 | goto out; | |
1da177e4 | 2586 | } |
6dc878a8 G |
2587 | /* add reference of module which cb->dump belongs to */ |
2588 | if (!try_module_get(cb->module)) { | |
2589 | mutex_unlock(nlk->cb_mutex); | |
2590 | netlink_destroy_callback(cb); | |
2591 | ret = -EPROTONOSUPPORT; | |
2592 | goto out; | |
2593 | } | |
2594 | ||
1da177e4 | 2595 | nlk->cb = cb; |
af65bdfc | 2596 | mutex_unlock(nlk->cb_mutex); |
1da177e4 | 2597 | |
b44d211e | 2598 | ret = netlink_dump(sk); |
6dc878a8 | 2599 | out: |
1da177e4 | 2600 | sock_put(sk); |
5c58298c | 2601 | |
b44d211e AV |
2602 | if (ret) |
2603 | return ret; | |
2604 | ||
5c58298c DL |
2605 | /* We successfully started a dump, by returning -EINTR we |
2606 | * signal not to send ACK even if it was requested. | |
2607 | */ | |
2608 | return -EINTR; | |
1da177e4 | 2609 | } |
6dc878a8 | 2610 | EXPORT_SYMBOL(__netlink_dump_start); |
1da177e4 LT |
2611 | |
2612 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err) | |
2613 | { | |
2614 | struct sk_buff *skb; | |
2615 | struct nlmsghdr *rep; | |
2616 | struct nlmsgerr *errmsg; | |
339bf98f | 2617 | size_t payload = sizeof(*errmsg); |
1da177e4 | 2618 | |
339bf98f TG |
2619 | /* error messages get the original request appened */ |
2620 | if (err) | |
2621 | payload += nlmsg_len(nlh); | |
1da177e4 | 2622 | |
f9c22888 PM |
2623 | skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload), |
2624 | NETLINK_CB(in_skb).portid, GFP_KERNEL); | |
1da177e4 LT |
2625 | if (!skb) { |
2626 | struct sock *sk; | |
2627 | ||
3b1e0a65 | 2628 | sk = netlink_lookup(sock_net(in_skb->sk), |
b4b51029 | 2629 | in_skb->sk->sk_protocol, |
15e47304 | 2630 | NETLINK_CB(in_skb).portid); |
1da177e4 LT |
2631 | if (sk) { |
2632 | sk->sk_err = ENOBUFS; | |
2633 | sk->sk_error_report(sk); | |
2634 | sock_put(sk); | |
2635 | } | |
2636 | return; | |
2637 | } | |
2638 | ||
15e47304 | 2639 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, |
5dba93ae | 2640 | NLMSG_ERROR, payload, 0); |
bf8b79e4 | 2641 | errmsg = nlmsg_data(rep); |
1da177e4 | 2642 | errmsg->error = err; |
bf8b79e4 | 2643 | memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh)); |
15e47304 | 2644 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT); |
1da177e4 | 2645 | } |
6ac552fd | 2646 | EXPORT_SYMBOL(netlink_ack); |
1da177e4 | 2647 | |
cd40b7d3 | 2648 | int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, |
1d00a4eb | 2649 | struct nlmsghdr *)) |
82ace47a | 2650 | { |
82ace47a TG |
2651 | struct nlmsghdr *nlh; |
2652 | int err; | |
2653 | ||
2654 | while (skb->len >= nlmsg_total_size(0)) { | |
cd40b7d3 DL |
2655 | int msglen; |
2656 | ||
b529ccf2 | 2657 | nlh = nlmsg_hdr(skb); |
d35b6856 | 2658 | err = 0; |
82ace47a | 2659 | |
ad8e4b75 | 2660 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) |
82ace47a TG |
2661 | return 0; |
2662 | ||
d35b6856 TG |
2663 | /* Only requests are handled by the kernel */ |
2664 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) | |
5c58298c | 2665 | goto ack; |
45e7ae7f TG |
2666 | |
2667 | /* Skip control messages */ | |
2668 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) | |
5c58298c | 2669 | goto ack; |
d35b6856 | 2670 | |
1d00a4eb | 2671 | err = cb(skb, nlh); |
5c58298c DL |
2672 | if (err == -EINTR) |
2673 | goto skip; | |
2674 | ||
2675 | ack: | |
d35b6856 | 2676 | if (nlh->nlmsg_flags & NLM_F_ACK || err) |
82ace47a | 2677 | netlink_ack(skb, nlh, err); |
82ace47a | 2678 | |
5c58298c | 2679 | skip: |
6ac552fd | 2680 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
cd40b7d3 DL |
2681 | if (msglen > skb->len) |
2682 | msglen = skb->len; | |
2683 | skb_pull(skb, msglen); | |
82ace47a TG |
2684 | } |
2685 | ||
2686 | return 0; | |
2687 | } | |
6ac552fd | 2688 | EXPORT_SYMBOL(netlink_rcv_skb); |
82ace47a | 2689 | |
d387f6ad TG |
2690 | /** |
2691 | * nlmsg_notify - send a notification netlink message | |
2692 | * @sk: netlink socket to use | |
2693 | * @skb: notification message | |
15e47304 | 2694 | * @portid: destination netlink portid for reports or 0 |
d387f6ad TG |
2695 | * @group: destination multicast group or 0 |
2696 | * @report: 1 to report back, 0 to disable | |
2697 | * @flags: allocation flags | |
2698 | */ | |
15e47304 | 2699 | int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, |
d387f6ad TG |
2700 | unsigned int group, int report, gfp_t flags) |
2701 | { | |
2702 | int err = 0; | |
2703 | ||
2704 | if (group) { | |
15e47304 | 2705 | int exclude_portid = 0; |
d387f6ad TG |
2706 | |
2707 | if (report) { | |
2708 | atomic_inc(&skb->users); | |
15e47304 | 2709 | exclude_portid = portid; |
d387f6ad TG |
2710 | } |
2711 | ||
1ce85fe4 PNA |
2712 | /* errors reported via destination sk->sk_err, but propagate |
2713 | * delivery errors if NETLINK_BROADCAST_ERROR flag is set */ | |
15e47304 | 2714 | err = nlmsg_multicast(sk, skb, exclude_portid, group, flags); |
d387f6ad TG |
2715 | } |
2716 | ||
1ce85fe4 PNA |
2717 | if (report) { |
2718 | int err2; | |
2719 | ||
15e47304 | 2720 | err2 = nlmsg_unicast(sk, skb, portid); |
1ce85fe4 PNA |
2721 | if (!err || err == -ESRCH) |
2722 | err = err2; | |
2723 | } | |
d387f6ad TG |
2724 | |
2725 | return err; | |
2726 | } | |
6ac552fd | 2727 | EXPORT_SYMBOL(nlmsg_notify); |
d387f6ad | 2728 | |
1da177e4 LT |
2729 | #ifdef CONFIG_PROC_FS |
2730 | struct nl_seq_iter { | |
e372c414 | 2731 | struct seq_net_private p; |
1da177e4 LT |
2732 | int link; |
2733 | int hash_idx; | |
2734 | }; | |
2735 | ||
2736 | static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos) | |
2737 | { | |
2738 | struct nl_seq_iter *iter = seq->private; | |
2739 | int i, j; | |
2740 | struct sock *s; | |
1da177e4 LT |
2741 | loff_t off = 0; |
2742 | ||
6ac552fd | 2743 | for (i = 0; i < MAX_LINKS; i++) { |
15e47304 | 2744 | struct nl_portid_hash *hash = &nl_table[i].hash; |
1da177e4 LT |
2745 | |
2746 | for (j = 0; j <= hash->mask; j++) { | |
b67bfe0d | 2747 | sk_for_each(s, &hash->table[j]) { |
1218854a | 2748 | if (sock_net(s) != seq_file_net(seq)) |
b4b51029 | 2749 | continue; |
1da177e4 LT |
2750 | if (off == pos) { |
2751 | iter->link = i; | |
2752 | iter->hash_idx = j; | |
2753 | return s; | |
2754 | } | |
2755 | ++off; | |
2756 | } | |
2757 | } | |
2758 | } | |
2759 | return NULL; | |
2760 | } | |
2761 | ||
2762 | static void *netlink_seq_start(struct seq_file *seq, loff_t *pos) | |
9a429c49 | 2763 | __acquires(nl_table_lock) |
1da177e4 LT |
2764 | { |
2765 | read_lock(&nl_table_lock); | |
2766 | return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN; | |
2767 | } | |
2768 | ||
2769 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
2770 | { | |
2771 | struct sock *s; | |
2772 | struct nl_seq_iter *iter; | |
2773 | int i, j; | |
2774 | ||
2775 | ++*pos; | |
2776 | ||
2777 | if (v == SEQ_START_TOKEN) | |
2778 | return netlink_seq_socket_idx(seq, 0); | |
746fac4d | 2779 | |
b4b51029 EB |
2780 | iter = seq->private; |
2781 | s = v; | |
2782 | do { | |
2783 | s = sk_next(s); | |
1218854a | 2784 | } while (s && sock_net(s) != seq_file_net(seq)); |
1da177e4 LT |
2785 | if (s) |
2786 | return s; | |
2787 | ||
1da177e4 LT |
2788 | i = iter->link; |
2789 | j = iter->hash_idx + 1; | |
2790 | ||
2791 | do { | |
15e47304 | 2792 | struct nl_portid_hash *hash = &nl_table[i].hash; |
1da177e4 LT |
2793 | |
2794 | for (; j <= hash->mask; j++) { | |
2795 | s = sk_head(&hash->table[j]); | |
1218854a | 2796 | while (s && sock_net(s) != seq_file_net(seq)) |
b4b51029 | 2797 | s = sk_next(s); |
1da177e4 LT |
2798 | if (s) { |
2799 | iter->link = i; | |
2800 | iter->hash_idx = j; | |
2801 | return s; | |
2802 | } | |
2803 | } | |
2804 | ||
2805 | j = 0; | |
2806 | } while (++i < MAX_LINKS); | |
2807 | ||
2808 | return NULL; | |
2809 | } | |
2810 | ||
2811 | static void netlink_seq_stop(struct seq_file *seq, void *v) | |
9a429c49 | 2812 | __releases(nl_table_lock) |
1da177e4 LT |
2813 | { |
2814 | read_unlock(&nl_table_lock); | |
2815 | } | |
2816 | ||
2817 | ||
2818 | static int netlink_seq_show(struct seq_file *seq, void *v) | |
2819 | { | |
658cb354 | 2820 | if (v == SEQ_START_TOKEN) { |
1da177e4 LT |
2821 | seq_puts(seq, |
2822 | "sk Eth Pid Groups " | |
cf0aa4e0 | 2823 | "Rmem Wmem Dump Locks Drops Inode\n"); |
658cb354 | 2824 | } else { |
1da177e4 LT |
2825 | struct sock *s = v; |
2826 | struct netlink_sock *nlk = nlk_sk(s); | |
2827 | ||
6fa3eb70 | 2828 | seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n", |
1da177e4 LT |
2829 | s, |
2830 | s->sk_protocol, | |
6fa3eb70 | 2831 | (int)(nlk->portid), |
513c2500 | 2832 | nlk->groups ? (u32)nlk->groups[0] : 0, |
31e6d363 ED |
2833 | sk_rmem_alloc_get(s), |
2834 | sk_wmem_alloc_get(s), | |
1da177e4 | 2835 | nlk->cb, |
38938bfe | 2836 | atomic_read(&s->sk_refcnt), |
cf0aa4e0 MY |
2837 | atomic_read(&s->sk_drops), |
2838 | sock_i_ino(s) | |
1da177e4 LT |
2839 | ); |
2840 | ||
2841 | } | |
2842 | return 0; | |
2843 | } | |
2844 | ||
56b3d975 | 2845 | static const struct seq_operations netlink_seq_ops = { |
1da177e4 LT |
2846 | .start = netlink_seq_start, |
2847 | .next = netlink_seq_next, | |
2848 | .stop = netlink_seq_stop, | |
2849 | .show = netlink_seq_show, | |
2850 | }; | |
2851 | ||
2852 | ||
2853 | static int netlink_seq_open(struct inode *inode, struct file *file) | |
2854 | { | |
e372c414 DL |
2855 | return seq_open_net(inode, file, &netlink_seq_ops, |
2856 | sizeof(struct nl_seq_iter)); | |
b4b51029 EB |
2857 | } |
2858 | ||
da7071d7 | 2859 | static const struct file_operations netlink_seq_fops = { |
1da177e4 LT |
2860 | .owner = THIS_MODULE, |
2861 | .open = netlink_seq_open, | |
2862 | .read = seq_read, | |
2863 | .llseek = seq_lseek, | |
e372c414 | 2864 | .release = seq_release_net, |
1da177e4 LT |
2865 | }; |
2866 | ||
2867 | #endif | |
2868 | ||
2869 | int netlink_register_notifier(struct notifier_block *nb) | |
2870 | { | |
e041c683 | 2871 | return atomic_notifier_chain_register(&netlink_chain, nb); |
1da177e4 | 2872 | } |
6ac552fd | 2873 | EXPORT_SYMBOL(netlink_register_notifier); |
1da177e4 LT |
2874 | |
2875 | int netlink_unregister_notifier(struct notifier_block *nb) | |
2876 | { | |
e041c683 | 2877 | return atomic_notifier_chain_unregister(&netlink_chain, nb); |
1da177e4 | 2878 | } |
6ac552fd | 2879 | EXPORT_SYMBOL(netlink_unregister_notifier); |
746fac4d | 2880 | |
90ddc4f0 | 2881 | static const struct proto_ops netlink_ops = { |
1da177e4 LT |
2882 | .family = PF_NETLINK, |
2883 | .owner = THIS_MODULE, | |
2884 | .release = netlink_release, | |
2885 | .bind = netlink_bind, | |
2886 | .connect = netlink_connect, | |
2887 | .socketpair = sock_no_socketpair, | |
2888 | .accept = sock_no_accept, | |
2889 | .getname = netlink_getname, | |
9652e931 | 2890 | .poll = netlink_poll, |
1da177e4 LT |
2891 | .ioctl = sock_no_ioctl, |
2892 | .listen = sock_no_listen, | |
2893 | .shutdown = sock_no_shutdown, | |
9a4595bc PM |
2894 | .setsockopt = netlink_setsockopt, |
2895 | .getsockopt = netlink_getsockopt, | |
1da177e4 LT |
2896 | .sendmsg = netlink_sendmsg, |
2897 | .recvmsg = netlink_recvmsg, | |
ccdfcc39 | 2898 | .mmap = netlink_mmap, |
1da177e4 LT |
2899 | .sendpage = sock_no_sendpage, |
2900 | }; | |
2901 | ||
ec1b4cf7 | 2902 | static const struct net_proto_family netlink_family_ops = { |
1da177e4 LT |
2903 | .family = PF_NETLINK, |
2904 | .create = netlink_create, | |
2905 | .owner = THIS_MODULE, /* for consistency 8) */ | |
2906 | }; | |
2907 | ||
4665079c | 2908 | static int __net_init netlink_net_init(struct net *net) |
b4b51029 EB |
2909 | { |
2910 | #ifdef CONFIG_PROC_FS | |
d4beaa66 | 2911 | if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops)) |
b4b51029 EB |
2912 | return -ENOMEM; |
2913 | #endif | |
2914 | return 0; | |
2915 | } | |
2916 | ||
4665079c | 2917 | static void __net_exit netlink_net_exit(struct net *net) |
b4b51029 EB |
2918 | { |
2919 | #ifdef CONFIG_PROC_FS | |
ece31ffd | 2920 | remove_proc_entry("netlink", net->proc_net); |
b4b51029 EB |
2921 | #endif |
2922 | } | |
2923 | ||
b963ea89 DM |
2924 | static void __init netlink_add_usersock_entry(void) |
2925 | { | |
5c398dc8 | 2926 | struct listeners *listeners; |
b963ea89 DM |
2927 | int groups = 32; |
2928 | ||
5c398dc8 | 2929 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
b963ea89 | 2930 | if (!listeners) |
5c398dc8 | 2931 | panic("netlink_add_usersock_entry: Cannot allocate listeners\n"); |
b963ea89 DM |
2932 | |
2933 | netlink_table_grab(); | |
2934 | ||
2935 | nl_table[NETLINK_USERSOCK].groups = groups; | |
5c398dc8 | 2936 | rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners); |
b963ea89 DM |
2937 | nl_table[NETLINK_USERSOCK].module = THIS_MODULE; |
2938 | nl_table[NETLINK_USERSOCK].registered = 1; | |
9785e10a | 2939 | nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND; |
b963ea89 DM |
2940 | |
2941 | netlink_table_ungrab(); | |
2942 | } | |
2943 | ||
022cbae6 | 2944 | static struct pernet_operations __net_initdata netlink_net_ops = { |
b4b51029 EB |
2945 | .init = netlink_net_init, |
2946 | .exit = netlink_net_exit, | |
2947 | }; | |
2948 | ||
1da177e4 LT |
2949 | static int __init netlink_proto_init(void) |
2950 | { | |
1da177e4 | 2951 | int i; |
26ff5ddc | 2952 | unsigned long limit; |
1da177e4 LT |
2953 | unsigned int order; |
2954 | int err = proto_register(&netlink_proto, 0); | |
2955 | ||
2956 | if (err != 0) | |
2957 | goto out; | |
2958 | ||
fab25745 | 2959 | BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb)); |
1da177e4 | 2960 | |
0da974f4 | 2961 | nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL); |
fab2caf6 AM |
2962 | if (!nl_table) |
2963 | goto panic; | |
1da177e4 | 2964 | |
4481374c JB |
2965 | if (totalram_pages >= (128 * 1024)) |
2966 | limit = totalram_pages >> (21 - PAGE_SHIFT); | |
1da177e4 | 2967 | else |
4481374c | 2968 | limit = totalram_pages >> (23 - PAGE_SHIFT); |
1da177e4 | 2969 | |
26ff5ddc DC |
2970 | order = get_bitmask_order(limit) - 1 + PAGE_SHIFT; |
2971 | limit = (1UL << order) / sizeof(struct hlist_head); | |
2972 | order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1; | |
1da177e4 LT |
2973 | |
2974 | for (i = 0; i < MAX_LINKS; i++) { | |
15e47304 | 2975 | struct nl_portid_hash *hash = &nl_table[i].hash; |
1da177e4 | 2976 | |
15e47304 | 2977 | hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table)); |
1da177e4 LT |
2978 | if (!hash->table) { |
2979 | while (i-- > 0) | |
15e47304 | 2980 | nl_portid_hash_free(nl_table[i].hash.table, |
1da177e4 LT |
2981 | 1 * sizeof(*hash->table)); |
2982 | kfree(nl_table); | |
fab2caf6 | 2983 | goto panic; |
1da177e4 | 2984 | } |
1da177e4 LT |
2985 | hash->max_shift = order; |
2986 | hash->shift = 0; | |
2987 | hash->mask = 0; | |
2988 | hash->rehash_time = jiffies; | |
2989 | } | |
2990 | ||
b963ea89 DM |
2991 | netlink_add_usersock_entry(); |
2992 | ||
1da177e4 | 2993 | sock_register(&netlink_family_ops); |
b4b51029 | 2994 | register_pernet_subsys(&netlink_net_ops); |
746fac4d | 2995 | /* The netlink device handler may be needed early. */ |
1da177e4 LT |
2996 | rtnetlink_init(); |
2997 | out: | |
2998 | return err; | |
fab2caf6 AM |
2999 | panic: |
3000 | panic("netlink_init: Cannot allocate nl_table\n"); | |
1da177e4 LT |
3001 | } |
3002 | ||
1da177e4 | 3003 | core_initcall(netlink_proto_init); |