]>
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> | |
7c0f6ba6 | 43 | #include <linux/uaccess.h> |
1da177e4 LT |
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> |
bcbde0d4 | 60 | #include <linux/if_arp.h> |
e341694e | 61 | #include <linux/rhashtable.h> |
9652e931 | 62 | #include <asm/cacheflush.h> |
e341694e | 63 | #include <linux/hash.h> |
ee1c2442 | 64 | #include <linux/genetlink.h> |
7212462f | 65 | #include <linux/net_namespace.h> |
9cf38886 | 66 | #include <linux/nospec.h> |
54e0f520 | 67 | |
457c4cbc | 68 | #include <net/net_namespace.h> |
1da177e4 LT |
69 | #include <net/sock.h> |
70 | #include <net/scm.h> | |
82ace47a | 71 | #include <net/netlink.h> |
1da177e4 | 72 | |
0f29c768 | 73 | #include "af_netlink.h" |
1da177e4 | 74 | |
5c398dc8 ED |
75 | struct listeners { |
76 | struct rcu_head rcu; | |
77 | unsigned long masks[0]; | |
6c04bb18 JB |
78 | }; |
79 | ||
cd967e05 | 80 | /* state bits */ |
cc3a572f | 81 | #define NETLINK_S_CONGESTED 0x0 |
cd967e05 | 82 | |
035c4c16 | 83 | static inline int netlink_is_kernel(struct sock *sk) |
aed81560 | 84 | { |
cc3a572f | 85 | return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET; |
aed81560 DL |
86 | } |
87 | ||
91dd93f9 | 88 | struct netlink_table *nl_table __read_mostly; |
0f29c768 | 89 | EXPORT_SYMBOL_GPL(nl_table); |
1da177e4 LT |
90 | |
91 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); | |
92 | ||
8a0f5ccf HX |
93 | static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS]; |
94 | ||
95 | static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = { | |
96 | "nlk_cb_mutex-ROUTE", | |
97 | "nlk_cb_mutex-1", | |
98 | "nlk_cb_mutex-USERSOCK", | |
99 | "nlk_cb_mutex-FIREWALL", | |
100 | "nlk_cb_mutex-SOCK_DIAG", | |
101 | "nlk_cb_mutex-NFLOG", | |
102 | "nlk_cb_mutex-XFRM", | |
103 | "nlk_cb_mutex-SELINUX", | |
104 | "nlk_cb_mutex-ISCSI", | |
105 | "nlk_cb_mutex-AUDIT", | |
106 | "nlk_cb_mutex-FIB_LOOKUP", | |
107 | "nlk_cb_mutex-CONNECTOR", | |
108 | "nlk_cb_mutex-NETFILTER", | |
109 | "nlk_cb_mutex-IP6_FW", | |
110 | "nlk_cb_mutex-DNRTMSG", | |
111 | "nlk_cb_mutex-KOBJECT_UEVENT", | |
112 | "nlk_cb_mutex-GENERIC", | |
113 | "nlk_cb_mutex-17", | |
114 | "nlk_cb_mutex-SCSITRANSPORT", | |
115 | "nlk_cb_mutex-ECRYPTFS", | |
116 | "nlk_cb_mutex-RDMA", | |
117 | "nlk_cb_mutex-CRYPTO", | |
118 | "nlk_cb_mutex-SMC", | |
119 | "nlk_cb_mutex-23", | |
120 | "nlk_cb_mutex-24", | |
121 | "nlk_cb_mutex-25", | |
122 | "nlk_cb_mutex-26", | |
123 | "nlk_cb_mutex-27", | |
124 | "nlk_cb_mutex-28", | |
125 | "nlk_cb_mutex-29", | |
126 | "nlk_cb_mutex-30", | |
127 | "nlk_cb_mutex-31", | |
128 | "nlk_cb_mutex-MAX_LINKS" | |
129 | }; | |
130 | ||
1da177e4 | 131 | static int netlink_dump(struct sock *sk); |
1da177e4 | 132 | |
78fd1d0a | 133 | /* nl_table locking explained: |
21e4902a | 134 | * Lookup and traversal are protected with an RCU read-side lock. Insertion |
c5adde94 | 135 | * and removal are protected with per bucket lock while using RCU list |
21e4902a TG |
136 | * modification primitives and may run in parallel to RCU protected lookups. |
137 | * Destruction of the Netlink socket may only occur *after* nl_table_lock has | |
138 | * been acquired * either during or after the socket has been removed from | |
139 | * the list and after an RCU grace period. | |
78fd1d0a | 140 | */ |
0f29c768 AV |
141 | DEFINE_RWLOCK(nl_table_lock); |
142 | EXPORT_SYMBOL_GPL(nl_table_lock); | |
1da177e4 LT |
143 | static atomic_t nl_table_users = ATOMIC_INIT(0); |
144 | ||
6d772ac5 ED |
145 | #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock)); |
146 | ||
efa172f4 | 147 | static BLOCKING_NOTIFIER_HEAD(netlink_chain); |
1da177e4 | 148 | |
bcbde0d4 DB |
149 | static DEFINE_SPINLOCK(netlink_tap_lock); |
150 | static struct list_head netlink_tap_all __read_mostly; | |
151 | ||
c428ecd1 HX |
152 | static const struct rhashtable_params netlink_rhashtable_params; |
153 | ||
b57ef81f | 154 | static inline u32 netlink_group_mask(u32 group) |
d629b836 PM |
155 | { |
156 | return group ? 1 << (group - 1) : 0; | |
157 | } | |
158 | ||
1853c949 DB |
159 | static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb, |
160 | gfp_t gfp_mask) | |
161 | { | |
162 | unsigned int len = skb_end_offset(skb); | |
163 | struct sk_buff *new; | |
164 | ||
165 | new = alloc_skb(len, gfp_mask); | |
166 | if (new == NULL) | |
167 | return NULL; | |
168 | ||
169 | NETLINK_CB(new).portid = NETLINK_CB(skb).portid; | |
170 | NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group; | |
171 | NETLINK_CB(new).creds = NETLINK_CB(skb).creds; | |
172 | ||
59ae1d12 | 173 | skb_put_data(new, skb->data, len); |
1853c949 DB |
174 | return new; |
175 | } | |
176 | ||
bcbde0d4 DB |
177 | int netlink_add_tap(struct netlink_tap *nt) |
178 | { | |
179 | if (unlikely(nt->dev->type != ARPHRD_NETLINK)) | |
180 | return -EINVAL; | |
181 | ||
182 | spin_lock(&netlink_tap_lock); | |
183 | list_add_rcu(&nt->list, &netlink_tap_all); | |
184 | spin_unlock(&netlink_tap_lock); | |
185 | ||
fcd4d35e | 186 | __module_get(nt->module); |
bcbde0d4 DB |
187 | |
188 | return 0; | |
189 | } | |
190 | EXPORT_SYMBOL_GPL(netlink_add_tap); | |
191 | ||
2173f8d9 | 192 | static int __netlink_remove_tap(struct netlink_tap *nt) |
bcbde0d4 DB |
193 | { |
194 | bool found = false; | |
195 | struct netlink_tap *tmp; | |
196 | ||
197 | spin_lock(&netlink_tap_lock); | |
198 | ||
199 | list_for_each_entry(tmp, &netlink_tap_all, list) { | |
200 | if (nt == tmp) { | |
201 | list_del_rcu(&nt->list); | |
202 | found = true; | |
203 | goto out; | |
204 | } | |
205 | } | |
206 | ||
207 | pr_warn("__netlink_remove_tap: %p not found\n", nt); | |
208 | out: | |
209 | spin_unlock(&netlink_tap_lock); | |
210 | ||
92b80eb3 | 211 | if (found) |
bcbde0d4 DB |
212 | module_put(nt->module); |
213 | ||
214 | return found ? 0 : -ENODEV; | |
215 | } | |
bcbde0d4 DB |
216 | |
217 | int netlink_remove_tap(struct netlink_tap *nt) | |
218 | { | |
219 | int ret; | |
220 | ||
221 | ret = __netlink_remove_tap(nt); | |
222 | synchronize_net(); | |
223 | ||
224 | return ret; | |
225 | } | |
226 | EXPORT_SYMBOL_GPL(netlink_remove_tap); | |
227 | ||
5ffd5cdd DB |
228 | static bool netlink_filter_tap(const struct sk_buff *skb) |
229 | { | |
230 | struct sock *sk = skb->sk; | |
5ffd5cdd DB |
231 | |
232 | /* We take the more conservative approach and | |
233 | * whitelist socket protocols that may pass. | |
234 | */ | |
235 | switch (sk->sk_protocol) { | |
236 | case NETLINK_ROUTE: | |
237 | case NETLINK_USERSOCK: | |
238 | case NETLINK_SOCK_DIAG: | |
239 | case NETLINK_NFLOG: | |
240 | case NETLINK_XFRM: | |
241 | case NETLINK_FIB_LOOKUP: | |
242 | case NETLINK_NETFILTER: | |
243 | case NETLINK_GENERIC: | |
498044bb | 244 | return true; |
5ffd5cdd DB |
245 | } |
246 | ||
498044bb | 247 | return false; |
5ffd5cdd DB |
248 | } |
249 | ||
bcbde0d4 DB |
250 | static int __netlink_deliver_tap_skb(struct sk_buff *skb, |
251 | struct net_device *dev) | |
252 | { | |
253 | struct sk_buff *nskb; | |
5ffd5cdd | 254 | struct sock *sk = skb->sk; |
bcbde0d4 DB |
255 | int ret = -ENOMEM; |
256 | ||
93c64764 KC |
257 | if (!net_eq(dev_net(dev), sock_net(sk))) |
258 | return 0; | |
259 | ||
bcbde0d4 | 260 | dev_hold(dev); |
1853c949 | 261 | |
d1b4c689 | 262 | if (is_vmalloc_addr(skb->head)) |
1853c949 DB |
263 | nskb = netlink_to_full_skb(skb, GFP_ATOMIC); |
264 | else | |
265 | nskb = skb_clone(skb, GFP_ATOMIC); | |
bcbde0d4 DB |
266 | if (nskb) { |
267 | nskb->dev = dev; | |
5ffd5cdd | 268 | nskb->protocol = htons((u16) sk->sk_protocol); |
604d13c9 DB |
269 | nskb->pkt_type = netlink_is_kernel(sk) ? |
270 | PACKET_KERNEL : PACKET_USER; | |
4e48ed88 | 271 | skb_reset_network_header(nskb); |
bcbde0d4 DB |
272 | ret = dev_queue_xmit(nskb); |
273 | if (unlikely(ret > 0)) | |
274 | ret = net_xmit_errno(ret); | |
275 | } | |
276 | ||
277 | dev_put(dev); | |
278 | return ret; | |
279 | } | |
280 | ||
281 | static void __netlink_deliver_tap(struct sk_buff *skb) | |
282 | { | |
283 | int ret; | |
284 | struct netlink_tap *tmp; | |
285 | ||
5ffd5cdd DB |
286 | if (!netlink_filter_tap(skb)) |
287 | return; | |
288 | ||
bcbde0d4 DB |
289 | list_for_each_entry_rcu(tmp, &netlink_tap_all, list) { |
290 | ret = __netlink_deliver_tap_skb(skb, tmp->dev); | |
291 | if (unlikely(ret)) | |
292 | break; | |
293 | } | |
294 | } | |
295 | ||
296 | static void netlink_deliver_tap(struct sk_buff *skb) | |
297 | { | |
298 | rcu_read_lock(); | |
299 | ||
300 | if (unlikely(!list_empty(&netlink_tap_all))) | |
301 | __netlink_deliver_tap(skb); | |
302 | ||
303 | rcu_read_unlock(); | |
304 | } | |
305 | ||
73bfd370 DB |
306 | static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src, |
307 | struct sk_buff *skb) | |
308 | { | |
309 | if (!(netlink_is_kernel(dst) && netlink_is_kernel(src))) | |
310 | netlink_deliver_tap(skb); | |
311 | } | |
312 | ||
cd1df525 PM |
313 | static void netlink_overrun(struct sock *sk) |
314 | { | |
315 | struct netlink_sock *nlk = nlk_sk(sk); | |
316 | ||
cc3a572f ND |
317 | if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) { |
318 | if (!test_and_set_bit(NETLINK_S_CONGESTED, | |
319 | &nlk_sk(sk)->state)) { | |
cd1df525 PM |
320 | sk->sk_err = ENOBUFS; |
321 | sk->sk_error_report(sk); | |
322 | } | |
323 | } | |
324 | atomic_inc(&sk->sk_drops); | |
325 | } | |
326 | ||
327 | static void netlink_rcv_wake(struct sock *sk) | |
328 | { | |
329 | struct netlink_sock *nlk = nlk_sk(sk); | |
330 | ||
331 | if (skb_queue_empty(&sk->sk_receive_queue)) | |
cc3a572f ND |
332 | clear_bit(NETLINK_S_CONGESTED, &nlk->state); |
333 | if (!test_bit(NETLINK_S_CONGESTED, &nlk->state)) | |
cd1df525 PM |
334 | wake_up_interruptible(&nlk->wait); |
335 | } | |
336 | ||
cf0a018a PM |
337 | static void netlink_skb_destructor(struct sk_buff *skb) |
338 | { | |
c05cdb1b | 339 | if (is_vmalloc_addr(skb->head)) { |
3a36515f PN |
340 | if (!skb->cloned || |
341 | !atomic_dec_return(&(skb_shinfo(skb)->dataref))) | |
342 | vfree(skb->head); | |
343 | ||
c05cdb1b PNA |
344 | skb->head = NULL; |
345 | } | |
9652e931 PM |
346 | if (skb->sk != NULL) |
347 | sock_rfree(skb); | |
cf0a018a PM |
348 | } |
349 | ||
350 | static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk) | |
351 | { | |
352 | WARN_ON(skb->sk != NULL); | |
353 | skb->sk = sk; | |
354 | skb->destructor = netlink_skb_destructor; | |
355 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); | |
356 | sk_mem_charge(sk, skb->truesize); | |
357 | } | |
358 | ||
ed5d7788 | 359 | static void netlink_sock_destruct(struct sock *sk) |
1da177e4 | 360 | { |
3f660d66 HX |
361 | struct netlink_sock *nlk = nlk_sk(sk); |
362 | ||
16b304f3 | 363 | if (nlk->cb_running) { |
ed5d7788 HX |
364 | if (nlk->cb.done) |
365 | nlk->cb.done(&nlk->cb); | |
16b304f3 PS |
366 | module_put(nlk->cb.module); |
367 | kfree_skb(nlk->cb.skb); | |
3f660d66 HX |
368 | } |
369 | ||
1da177e4 LT |
370 | skb_queue_purge(&sk->sk_receive_queue); |
371 | ||
372 | if (!sock_flag(sk, SOCK_DEAD)) { | |
6ac552fd | 373 | printk(KERN_ERR "Freeing alive netlink socket %p\n", sk); |
1da177e4 LT |
374 | return; |
375 | } | |
547b792c IJ |
376 | |
377 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); | |
14afee4b | 378 | WARN_ON(refcount_read(&sk->sk_wmem_alloc)); |
547b792c | 379 | WARN_ON(nlk_sk(sk)->groups); |
1da177e4 LT |
380 | } |
381 | ||
707693c8 HX |
382 | static void netlink_sock_destruct_work(struct work_struct *work) |
383 | { | |
384 | struct netlink_sock *nlk = container_of(work, struct netlink_sock, | |
385 | work); | |
386 | ||
ed5d7788 | 387 | sk_free(&nlk->sk); |
707693c8 HX |
388 | } |
389 | ||
6ac552fd PM |
390 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on |
391 | * SMP. Look, when several writers sleep and reader wakes them up, all but one | |
1da177e4 LT |
392 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves |
393 | * this, _but_ remember, it adds useless work on UP machines. | |
394 | */ | |
395 | ||
d136f1bd | 396 | void netlink_table_grab(void) |
9a429c49 | 397 | __acquires(nl_table_lock) |
1da177e4 | 398 | { |
d136f1bd JB |
399 | might_sleep(); |
400 | ||
6abd219c | 401 | write_lock_irq(&nl_table_lock); |
1da177e4 LT |
402 | |
403 | if (atomic_read(&nl_table_users)) { | |
404 | DECLARE_WAITQUEUE(wait, current); | |
405 | ||
406 | add_wait_queue_exclusive(&nl_table_wait, &wait); | |
6ac552fd | 407 | for (;;) { |
1da177e4 LT |
408 | set_current_state(TASK_UNINTERRUPTIBLE); |
409 | if (atomic_read(&nl_table_users) == 0) | |
410 | break; | |
6abd219c | 411 | write_unlock_irq(&nl_table_lock); |
1da177e4 | 412 | schedule(); |
6abd219c | 413 | write_lock_irq(&nl_table_lock); |
1da177e4 LT |
414 | } |
415 | ||
416 | __set_current_state(TASK_RUNNING); | |
417 | remove_wait_queue(&nl_table_wait, &wait); | |
418 | } | |
419 | } | |
420 | ||
d136f1bd | 421 | void netlink_table_ungrab(void) |
9a429c49 | 422 | __releases(nl_table_lock) |
1da177e4 | 423 | { |
6abd219c | 424 | write_unlock_irq(&nl_table_lock); |
1da177e4 LT |
425 | wake_up(&nl_table_wait); |
426 | } | |
427 | ||
6ac552fd | 428 | static inline void |
1da177e4 LT |
429 | netlink_lock_table(void) |
430 | { | |
431 | /* read_lock() synchronizes us to netlink_table_grab */ | |
432 | ||
433 | read_lock(&nl_table_lock); | |
434 | atomic_inc(&nl_table_users); | |
435 | read_unlock(&nl_table_lock); | |
436 | } | |
437 | ||
6ac552fd | 438 | static inline void |
1da177e4 LT |
439 | netlink_unlock_table(void) |
440 | { | |
441 | if (atomic_dec_and_test(&nl_table_users)) | |
442 | wake_up(&nl_table_wait); | |
443 | } | |
444 | ||
e341694e | 445 | struct netlink_compare_arg |
1da177e4 | 446 | { |
c428ecd1 | 447 | possible_net_t pnet; |
e341694e TG |
448 | u32 portid; |
449 | }; | |
1da177e4 | 450 | |
8f2ddaac HX |
451 | /* Doing sizeof directly may yield 4 extra bytes on 64-bit. */ |
452 | #define netlink_compare_arg_len \ | |
453 | (offsetof(struct netlink_compare_arg, portid) + sizeof(u32)) | |
c428ecd1 HX |
454 | |
455 | static inline int netlink_compare(struct rhashtable_compare_arg *arg, | |
456 | const void *ptr) | |
1da177e4 | 457 | { |
c428ecd1 HX |
458 | const struct netlink_compare_arg *x = arg->key; |
459 | const struct netlink_sock *nlk = ptr; | |
1da177e4 | 460 | |
da314c99 | 461 | return nlk->portid != x->portid || |
c428ecd1 HX |
462 | !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet)); |
463 | } | |
464 | ||
465 | static void netlink_compare_arg_init(struct netlink_compare_arg *arg, | |
466 | struct net *net, u32 portid) | |
467 | { | |
468 | memset(arg, 0, sizeof(*arg)); | |
469 | write_pnet(&arg->pnet, net); | |
470 | arg->portid = portid; | |
1da177e4 LT |
471 | } |
472 | ||
e341694e TG |
473 | static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid, |
474 | struct net *net) | |
1da177e4 | 475 | { |
c428ecd1 | 476 | struct netlink_compare_arg arg; |
1da177e4 | 477 | |
c428ecd1 HX |
478 | netlink_compare_arg_init(&arg, net, portid); |
479 | return rhashtable_lookup_fast(&table->hash, &arg, | |
480 | netlink_rhashtable_params); | |
1da177e4 LT |
481 | } |
482 | ||
c428ecd1 | 483 | static int __netlink_insert(struct netlink_table *table, struct sock *sk) |
c5adde94 | 484 | { |
c428ecd1 | 485 | struct netlink_compare_arg arg; |
c5adde94 | 486 | |
da314c99 | 487 | netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid); |
c428ecd1 HX |
488 | return rhashtable_lookup_insert_key(&table->hash, &arg, |
489 | &nlk_sk(sk)->node, | |
490 | netlink_rhashtable_params); | |
c5adde94 YX |
491 | } |
492 | ||
e341694e | 493 | static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid) |
1da177e4 | 494 | { |
e341694e TG |
495 | struct netlink_table *table = &nl_table[protocol]; |
496 | struct sock *sk; | |
1da177e4 | 497 | |
e341694e TG |
498 | rcu_read_lock(); |
499 | sk = __netlink_lookup(table, portid, net); | |
500 | if (sk) | |
501 | sock_hold(sk); | |
502 | rcu_read_unlock(); | |
1da177e4 | 503 | |
e341694e | 504 | return sk; |
1da177e4 LT |
505 | } |
506 | ||
90ddc4f0 | 507 | static const struct proto_ops netlink_ops; |
1da177e4 | 508 | |
4277a083 PM |
509 | static void |
510 | netlink_update_listeners(struct sock *sk) | |
511 | { | |
512 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; | |
4277a083 PM |
513 | unsigned long mask; |
514 | unsigned int i; | |
6d772ac5 ED |
515 | struct listeners *listeners; |
516 | ||
517 | listeners = nl_deref_protected(tbl->listeners); | |
518 | if (!listeners) | |
519 | return; | |
4277a083 | 520 | |
b4ff4f04 | 521 | for (i = 0; i < NLGRPLONGS(tbl->groups); i++) { |
4277a083 | 522 | mask = 0; |
b67bfe0d | 523 | sk_for_each_bound(sk, &tbl->mc_list) { |
b4ff4f04 JB |
524 | if (i < NLGRPLONGS(nlk_sk(sk)->ngroups)) |
525 | mask |= nlk_sk(sk)->groups[i]; | |
526 | } | |
6d772ac5 | 527 | listeners->masks[i] = mask; |
4277a083 PM |
528 | } |
529 | /* this function is only called with the netlink table "grabbed", which | |
530 | * makes sure updates are visible before bind or setsockopt return. */ | |
531 | } | |
532 | ||
8ea65f4a | 533 | static int netlink_insert(struct sock *sk, u32 portid) |
1da177e4 | 534 | { |
da12c90e | 535 | struct netlink_table *table = &nl_table[sk->sk_protocol]; |
919d9db9 | 536 | int err; |
1da177e4 | 537 | |
c5adde94 | 538 | lock_sock(sk); |
1da177e4 | 539 | |
da314c99 HX |
540 | err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY; |
541 | if (nlk_sk(sk)->bound) | |
1da177e4 LT |
542 | goto err; |
543 | ||
544 | err = -ENOMEM; | |
97defe1e TG |
545 | if (BITS_PER_LONG > 32 && |
546 | unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX)) | |
1da177e4 LT |
547 | goto err; |
548 | ||
da314c99 | 549 | nlk_sk(sk)->portid = portid; |
e341694e | 550 | sock_hold(sk); |
919d9db9 | 551 | |
c428ecd1 HX |
552 | err = __netlink_insert(table, sk); |
553 | if (err) { | |
4e7c1330 DB |
554 | /* In case the hashtable backend returns with -EBUSY |
555 | * from here, it must not escape to the caller. | |
556 | */ | |
557 | if (unlikely(err == -EBUSY)) | |
558 | err = -EOVERFLOW; | |
c428ecd1 HX |
559 | if (err == -EEXIST) |
560 | err = -EADDRINUSE; | |
c5adde94 | 561 | sock_put(sk); |
1f770c0a | 562 | goto err; |
919d9db9 HX |
563 | } |
564 | ||
da314c99 HX |
565 | /* We need to ensure that the socket is hashed and visible. */ |
566 | smp_wmb(); | |
567 | nlk_sk(sk)->bound = portid; | |
1f770c0a | 568 | |
1da177e4 | 569 | err: |
c5adde94 | 570 | release_sock(sk); |
1da177e4 LT |
571 | return err; |
572 | } | |
573 | ||
574 | static void netlink_remove(struct sock *sk) | |
575 | { | |
e341694e TG |
576 | struct netlink_table *table; |
577 | ||
e341694e | 578 | table = &nl_table[sk->sk_protocol]; |
c428ecd1 HX |
579 | if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node, |
580 | netlink_rhashtable_params)) { | |
41c6d650 | 581 | WARN_ON(refcount_read(&sk->sk_refcnt) == 1); |
e341694e TG |
582 | __sock_put(sk); |
583 | } | |
e341694e | 584 | |
1da177e4 | 585 | netlink_table_grab(); |
b10dcb3b | 586 | if (nlk_sk(sk)->subscriptions) { |
1da177e4 | 587 | __sk_del_bind_node(sk); |
b10dcb3b JB |
588 | netlink_update_listeners(sk); |
589 | } | |
ee1c2442 JB |
590 | if (sk->sk_protocol == NETLINK_GENERIC) |
591 | atomic_inc(&genl_sk_destructing_cnt); | |
1da177e4 LT |
592 | netlink_table_ungrab(); |
593 | } | |
594 | ||
595 | static struct proto netlink_proto = { | |
596 | .name = "NETLINK", | |
597 | .owner = THIS_MODULE, | |
598 | .obj_size = sizeof(struct netlink_sock), | |
599 | }; | |
600 | ||
1b8d7ae4 | 601 | static int __netlink_create(struct net *net, struct socket *sock, |
11aa9c28 EB |
602 | struct mutex *cb_mutex, int protocol, |
603 | int kern) | |
1da177e4 LT |
604 | { |
605 | struct sock *sk; | |
606 | struct netlink_sock *nlk; | |
ab33a171 PM |
607 | |
608 | sock->ops = &netlink_ops; | |
609 | ||
11aa9c28 | 610 | sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern); |
ab33a171 PM |
611 | if (!sk) |
612 | return -ENOMEM; | |
613 | ||
614 | sock_init_data(sock, sk); | |
615 | ||
616 | nlk = nlk_sk(sk); | |
658cb354 | 617 | if (cb_mutex) { |
ffa4d721 | 618 | nlk->cb_mutex = cb_mutex; |
658cb354 | 619 | } else { |
ffa4d721 PM |
620 | nlk->cb_mutex = &nlk->cb_def_mutex; |
621 | mutex_init(nlk->cb_mutex); | |
8a0f5ccf HX |
622 | lockdep_set_class_and_name(nlk->cb_mutex, |
623 | nlk_cb_mutex_keys + protocol, | |
624 | nlk_cb_mutex_key_strings[protocol]); | |
ffa4d721 | 625 | } |
ab33a171 PM |
626 | init_waitqueue_head(&nlk->wait); |
627 | ||
628 | sk->sk_destruct = netlink_sock_destruct; | |
629 | sk->sk_protocol = protocol; | |
630 | return 0; | |
631 | } | |
632 | ||
3f378b68 EP |
633 | static int netlink_create(struct net *net, struct socket *sock, int protocol, |
634 | int kern) | |
ab33a171 PM |
635 | { |
636 | struct module *module = NULL; | |
af65bdfc | 637 | struct mutex *cb_mutex; |
f7fa9b10 | 638 | struct netlink_sock *nlk; |
023e2cfa JB |
639 | int (*bind)(struct net *net, int group); |
640 | void (*unbind)(struct net *net, int group); | |
ab33a171 | 641 | int err = 0; |
1da177e4 LT |
642 | |
643 | sock->state = SS_UNCONNECTED; | |
644 | ||
645 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) | |
646 | return -ESOCKTNOSUPPORT; | |
647 | ||
6ac552fd | 648 | if (protocol < 0 || protocol >= MAX_LINKS) |
1da177e4 | 649 | return -EPROTONOSUPPORT; |
9cf38886 | 650 | protocol = array_index_nospec(protocol, MAX_LINKS); |
1da177e4 | 651 | |
77247bbb | 652 | netlink_lock_table(); |
95a5afca | 653 | #ifdef CONFIG_MODULES |
ab33a171 | 654 | if (!nl_table[protocol].registered) { |
77247bbb | 655 | netlink_unlock_table(); |
4fdb3bb7 | 656 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
77247bbb | 657 | netlink_lock_table(); |
4fdb3bb7 | 658 | } |
ab33a171 PM |
659 | #endif |
660 | if (nl_table[protocol].registered && | |
661 | try_module_get(nl_table[protocol].module)) | |
662 | module = nl_table[protocol].module; | |
974c37e9 AD |
663 | else |
664 | err = -EPROTONOSUPPORT; | |
af65bdfc | 665 | cb_mutex = nl_table[protocol].cb_mutex; |
03292745 | 666 | bind = nl_table[protocol].bind; |
4f520900 | 667 | unbind = nl_table[protocol].unbind; |
77247bbb | 668 | netlink_unlock_table(); |
4fdb3bb7 | 669 | |
974c37e9 AD |
670 | if (err < 0) |
671 | goto out; | |
672 | ||
11aa9c28 | 673 | err = __netlink_create(net, sock, cb_mutex, protocol, kern); |
6ac552fd | 674 | if (err < 0) |
f7fa9b10 PM |
675 | goto out_module; |
676 | ||
6f756a8c | 677 | local_bh_disable(); |
c1fd3b94 | 678 | sock_prot_inuse_add(net, &netlink_proto, 1); |
6f756a8c DM |
679 | local_bh_enable(); |
680 | ||
f7fa9b10 | 681 | nlk = nlk_sk(sock->sk); |
f7fa9b10 | 682 | nlk->module = module; |
03292745 | 683 | nlk->netlink_bind = bind; |
4f520900 | 684 | nlk->netlink_unbind = unbind; |
ab33a171 PM |
685 | out: |
686 | return err; | |
1da177e4 | 687 | |
ab33a171 PM |
688 | out_module: |
689 | module_put(module); | |
690 | goto out; | |
1da177e4 LT |
691 | } |
692 | ||
21e4902a TG |
693 | static void deferred_put_nlk_sk(struct rcu_head *head) |
694 | { | |
695 | struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu); | |
ed5d7788 HX |
696 | struct sock *sk = &nlk->sk; |
697 | ||
be82485f XL |
698 | kfree(nlk->groups); |
699 | nlk->groups = NULL; | |
700 | ||
41c6d650 | 701 | if (!refcount_dec_and_test(&sk->sk_refcnt)) |
ed5d7788 HX |
702 | return; |
703 | ||
704 | if (nlk->cb_running && nlk->cb.done) { | |
705 | INIT_WORK(&nlk->work, netlink_sock_destruct_work); | |
706 | schedule_work(&nlk->work); | |
707 | return; | |
708 | } | |
21e4902a | 709 | |
ed5d7788 | 710 | sk_free(sk); |
21e4902a TG |
711 | } |
712 | ||
1da177e4 LT |
713 | static int netlink_release(struct socket *sock) |
714 | { | |
715 | struct sock *sk = sock->sk; | |
716 | struct netlink_sock *nlk; | |
717 | ||
718 | if (!sk) | |
719 | return 0; | |
720 | ||
721 | netlink_remove(sk); | |
ac57b3a9 | 722 | sock_orphan(sk); |
1da177e4 LT |
723 | nlk = nlk_sk(sk); |
724 | ||
3f660d66 HX |
725 | /* |
726 | * OK. Socket is unlinked, any packets that arrive now | |
727 | * will be purged. | |
728 | */ | |
1da177e4 | 729 | |
ee1c2442 JB |
730 | /* must not acquire netlink_table_lock in any way again before unbind |
731 | * and notifying genetlink is done as otherwise it might deadlock | |
732 | */ | |
733 | if (nlk->netlink_unbind) { | |
734 | int i; | |
735 | ||
736 | for (i = 0; i < nlk->ngroups; i++) | |
737 | if (test_bit(i, nlk->groups)) | |
738 | nlk->netlink_unbind(sock_net(sk), i + 1); | |
739 | } | |
740 | if (sk->sk_protocol == NETLINK_GENERIC && | |
741 | atomic_dec_return(&genl_sk_destructing_cnt) == 0) | |
742 | wake_up(&genl_sk_destructing_waitq); | |
743 | ||
1da177e4 LT |
744 | sock->sk = NULL; |
745 | wake_up_interruptible_all(&nlk->wait); | |
746 | ||
747 | skb_queue_purge(&sk->sk_write_queue); | |
748 | ||
e2726020 | 749 | if (nlk->portid && nlk->bound) { |
1da177e4 | 750 | struct netlink_notify n = { |
3b1e0a65 | 751 | .net = sock_net(sk), |
1da177e4 | 752 | .protocol = sk->sk_protocol, |
15e47304 | 753 | .portid = nlk->portid, |
1da177e4 | 754 | }; |
efa172f4 | 755 | blocking_notifier_call_chain(&netlink_chain, |
e041c683 | 756 | NETLINK_URELEASE, &n); |
746fac4d | 757 | } |
4fdb3bb7 | 758 | |
5e7c001c | 759 | module_put(nlk->module); |
4fdb3bb7 | 760 | |
aed81560 | 761 | if (netlink_is_kernel(sk)) { |
b10dcb3b | 762 | netlink_table_grab(); |
869e58f8 DL |
763 | BUG_ON(nl_table[sk->sk_protocol].registered == 0); |
764 | if (--nl_table[sk->sk_protocol].registered == 0) { | |
6d772ac5 ED |
765 | struct listeners *old; |
766 | ||
767 | old = nl_deref_protected(nl_table[sk->sk_protocol].listeners); | |
768 | RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL); | |
769 | kfree_rcu(old, rcu); | |
869e58f8 | 770 | nl_table[sk->sk_protocol].module = NULL; |
9785e10a | 771 | nl_table[sk->sk_protocol].bind = NULL; |
4f520900 | 772 | nl_table[sk->sk_protocol].unbind = NULL; |
9785e10a | 773 | nl_table[sk->sk_protocol].flags = 0; |
869e58f8 DL |
774 | nl_table[sk->sk_protocol].registered = 0; |
775 | } | |
b10dcb3b | 776 | netlink_table_ungrab(); |
658cb354 | 777 | } |
77247bbb | 778 | |
3755810c | 779 | local_bh_disable(); |
c1fd3b94 | 780 | sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1); |
3755810c | 781 | local_bh_enable(); |
21e4902a | 782 | call_rcu(&nlk->rcu, deferred_put_nlk_sk); |
1da177e4 LT |
783 | return 0; |
784 | } | |
785 | ||
786 | static int netlink_autobind(struct socket *sock) | |
787 | { | |
788 | struct sock *sk = sock->sk; | |
3b1e0a65 | 789 | struct net *net = sock_net(sk); |
da12c90e | 790 | struct netlink_table *table = &nl_table[sk->sk_protocol]; |
15e47304 | 791 | s32 portid = task_tgid_vnr(current); |
1da177e4 | 792 | int err; |
b9fbe709 HX |
793 | s32 rover = -4096; |
794 | bool ok; | |
1da177e4 LT |
795 | |
796 | retry: | |
797 | cond_resched(); | |
e341694e | 798 | rcu_read_lock(); |
b9fbe709 HX |
799 | ok = !__netlink_lookup(table, portid, net); |
800 | rcu_read_unlock(); | |
801 | if (!ok) { | |
e341694e | 802 | /* Bind collision, search negative portid values. */ |
b9fbe709 HX |
803 | if (rover == -4096) |
804 | /* rover will be in range [S32_MIN, -4097] */ | |
805 | rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN); | |
806 | else if (rover >= -4096) | |
e341694e | 807 | rover = -4097; |
b9fbe709 | 808 | portid = rover--; |
e341694e | 809 | goto retry; |
1da177e4 | 810 | } |
1da177e4 | 811 | |
8ea65f4a | 812 | err = netlink_insert(sk, portid); |
1da177e4 LT |
813 | if (err == -EADDRINUSE) |
814 | goto retry; | |
d470e3b4 DM |
815 | |
816 | /* If 2 threads race to autobind, that is fine. */ | |
817 | if (err == -EBUSY) | |
818 | err = 0; | |
819 | ||
820 | return err; | |
1da177e4 LT |
821 | } |
822 | ||
aa4cf945 EB |
823 | /** |
824 | * __netlink_ns_capable - General netlink message capability test | |
825 | * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace. | |
826 | * @user_ns: The user namespace of the capability to use | |
827 | * @cap: The capability to use | |
828 | * | |
829 | * Test to see if the opener of the socket we received the message | |
830 | * from had when the netlink socket was created and the sender of the | |
831 | * message has has the capability @cap in the user namespace @user_ns. | |
832 | */ | |
833 | bool __netlink_ns_capable(const struct netlink_skb_parms *nsp, | |
834 | struct user_namespace *user_ns, int cap) | |
835 | { | |
2d7a85f4 EB |
836 | return ((nsp->flags & NETLINK_SKB_DST) || |
837 | file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) && | |
838 | ns_capable(user_ns, cap); | |
aa4cf945 EB |
839 | } |
840 | EXPORT_SYMBOL(__netlink_ns_capable); | |
841 | ||
842 | /** | |
843 | * netlink_ns_capable - General netlink message capability test | |
844 | * @skb: socket buffer holding a netlink command from userspace | |
845 | * @user_ns: The user namespace of the capability to use | |
846 | * @cap: The capability to use | |
847 | * | |
848 | * Test to see if the opener of the socket we received the message | |
849 | * from had when the netlink socket was created and the sender of the | |
850 | * message has has the capability @cap in the user namespace @user_ns. | |
851 | */ | |
852 | bool netlink_ns_capable(const struct sk_buff *skb, | |
853 | struct user_namespace *user_ns, int cap) | |
854 | { | |
855 | return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap); | |
856 | } | |
857 | EXPORT_SYMBOL(netlink_ns_capable); | |
858 | ||
859 | /** | |
860 | * netlink_capable - Netlink global message capability test | |
861 | * @skb: socket buffer holding a netlink command from userspace | |
862 | * @cap: The capability to use | |
863 | * | |
864 | * Test to see if the opener of the socket we received the message | |
865 | * from had when the netlink socket was created and the sender of the | |
866 | * message has has the capability @cap in all user namespaces. | |
867 | */ | |
868 | bool netlink_capable(const struct sk_buff *skb, int cap) | |
869 | { | |
870 | return netlink_ns_capable(skb, &init_user_ns, cap); | |
871 | } | |
872 | EXPORT_SYMBOL(netlink_capable); | |
873 | ||
874 | /** | |
875 | * netlink_net_capable - Netlink network namespace message capability test | |
876 | * @skb: socket buffer holding a netlink command from userspace | |
877 | * @cap: The capability to use | |
878 | * | |
879 | * Test to see if the opener of the socket we received the message | |
880 | * from had when the netlink socket was created and the sender of the | |
881 | * message has has the capability @cap over the network namespace of | |
882 | * the socket we received the message from. | |
883 | */ | |
884 | bool netlink_net_capable(const struct sk_buff *skb, int cap) | |
885 | { | |
886 | return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap); | |
887 | } | |
888 | EXPORT_SYMBOL(netlink_net_capable); | |
889 | ||
5187cd05 | 890 | static inline int netlink_allowed(const struct socket *sock, unsigned int flag) |
746fac4d | 891 | { |
9785e10a | 892 | return (nl_table[sock->sk->sk_protocol].flags & flag) || |
df008c91 | 893 | ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN); |
746fac4d | 894 | } |
1da177e4 | 895 | |
f7fa9b10 PM |
896 | static void |
897 | netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) | |
898 | { | |
899 | struct netlink_sock *nlk = nlk_sk(sk); | |
900 | ||
901 | if (nlk->subscriptions && !subscriptions) | |
902 | __sk_del_bind_node(sk); | |
903 | else if (!nlk->subscriptions && subscriptions) | |
904 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); | |
905 | nlk->subscriptions = subscriptions; | |
906 | } | |
907 | ||
b4ff4f04 | 908 | static int netlink_realloc_groups(struct sock *sk) |
513c2500 PM |
909 | { |
910 | struct netlink_sock *nlk = nlk_sk(sk); | |
911 | unsigned int groups; | |
b4ff4f04 | 912 | unsigned long *new_groups; |
513c2500 PM |
913 | int err = 0; |
914 | ||
b4ff4f04 JB |
915 | netlink_table_grab(); |
916 | ||
513c2500 | 917 | groups = nl_table[sk->sk_protocol].groups; |
b4ff4f04 | 918 | if (!nl_table[sk->sk_protocol].registered) { |
513c2500 | 919 | err = -ENOENT; |
b4ff4f04 JB |
920 | goto out_unlock; |
921 | } | |
513c2500 | 922 | |
b4ff4f04 JB |
923 | if (nlk->ngroups >= groups) |
924 | goto out_unlock; | |
513c2500 | 925 | |
b4ff4f04 JB |
926 | new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC); |
927 | if (new_groups == NULL) { | |
928 | err = -ENOMEM; | |
929 | goto out_unlock; | |
930 | } | |
6ac552fd | 931 | memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0, |
b4ff4f04 JB |
932 | NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups)); |
933 | ||
934 | nlk->groups = new_groups; | |
513c2500 | 935 | nlk->ngroups = groups; |
b4ff4f04 JB |
936 | out_unlock: |
937 | netlink_table_ungrab(); | |
938 | return err; | |
513c2500 PM |
939 | } |
940 | ||
02c81ab9 | 941 | static void netlink_undo_bind(int group, long unsigned int groups, |
023e2cfa | 942 | struct sock *sk) |
4f520900 | 943 | { |
023e2cfa | 944 | struct netlink_sock *nlk = nlk_sk(sk); |
4f520900 RGB |
945 | int undo; |
946 | ||
947 | if (!nlk->netlink_unbind) | |
948 | return; | |
949 | ||
950 | for (undo = 0; undo < group; undo++) | |
6251edd9 | 951 | if (test_bit(undo, &groups)) |
8b7c36d8 | 952 | nlk->netlink_unbind(sock_net(sk), undo + 1); |
4f520900 RGB |
953 | } |
954 | ||
6ac552fd PM |
955 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, |
956 | int addr_len) | |
1da177e4 LT |
957 | { |
958 | struct sock *sk = sock->sk; | |
3b1e0a65 | 959 | struct net *net = sock_net(sk); |
1da177e4 LT |
960 | struct netlink_sock *nlk = nlk_sk(sk); |
961 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; | |
f7736080 | 962 | int err = 0; |
4f520900 | 963 | long unsigned int groups = nladdr->nl_groups; |
da314c99 | 964 | bool bound; |
746fac4d | 965 | |
4e4b5376 HFS |
966 | if (addr_len < sizeof(struct sockaddr_nl)) |
967 | return -EINVAL; | |
968 | ||
1da177e4 LT |
969 | if (nladdr->nl_family != AF_NETLINK) |
970 | return -EINVAL; | |
971 | ||
972 | /* Only superuser is allowed to listen multicasts */ | |
4f520900 | 973 | if (groups) { |
5187cd05 | 974 | if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV)) |
513c2500 | 975 | return -EPERM; |
b4ff4f04 JB |
976 | err = netlink_realloc_groups(sk); |
977 | if (err) | |
978 | return err; | |
513c2500 | 979 | } |
ab69e057 DS |
980 | |
981 | if (nlk->ngroups == 0) | |
982 | groups = 0; | |
983 | else | |
984 | groups &= (1ULL << nlk->ngroups) - 1; | |
1da177e4 | 985 | |
da314c99 HX |
986 | bound = nlk->bound; |
987 | if (bound) { | |
988 | /* Ensure nlk->portid is up-to-date. */ | |
989 | smp_rmb(); | |
990 | ||
15e47304 | 991 | if (nladdr->nl_pid != nlk->portid) |
1da177e4 | 992 | return -EINVAL; |
da314c99 | 993 | } |
4f520900 | 994 | |
f7736080 | 995 | netlink_lock_table(); |
4f520900 RGB |
996 | if (nlk->netlink_bind && groups) { |
997 | int group; | |
998 | ||
999 | for (group = 0; group < nlk->ngroups; group++) { | |
1000 | if (!test_bit(group, &groups)) | |
1001 | continue; | |
8b7c36d8 | 1002 | err = nlk->netlink_bind(net, group + 1); |
4f520900 RGB |
1003 | if (!err) |
1004 | continue; | |
023e2cfa | 1005 | netlink_undo_bind(group, groups, sk); |
f7736080 | 1006 | goto unlock; |
4f520900 RGB |
1007 | } |
1008 | } | |
1009 | ||
da314c99 HX |
1010 | /* No need for barriers here as we return to user-space without |
1011 | * using any of the bound attributes. | |
1012 | */ | |
1013 | if (!bound) { | |
1da177e4 | 1014 | err = nladdr->nl_pid ? |
8ea65f4a | 1015 | netlink_insert(sk, nladdr->nl_pid) : |
1da177e4 | 1016 | netlink_autobind(sock); |
4f520900 | 1017 | if (err) { |
023e2cfa | 1018 | netlink_undo_bind(nlk->ngroups, groups, sk); |
f7736080 | 1019 | goto unlock; |
4f520900 | 1020 | } |
1da177e4 LT |
1021 | } |
1022 | ||
4f520900 | 1023 | if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0])) |
f7736080 XL |
1024 | goto unlock; |
1025 | netlink_unlock_table(); | |
1da177e4 LT |
1026 | |
1027 | netlink_table_grab(); | |
f7fa9b10 | 1028 | netlink_update_subscriptions(sk, nlk->subscriptions + |
4f520900 | 1029 | hweight32(groups) - |
746fac4d | 1030 | hweight32(nlk->groups[0])); |
4f520900 | 1031 | nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups; |
4277a083 | 1032 | netlink_update_listeners(sk); |
1da177e4 LT |
1033 | netlink_table_ungrab(); |
1034 | ||
1035 | return 0; | |
f7736080 XL |
1036 | |
1037 | unlock: | |
1038 | netlink_unlock_table(); | |
1039 | return err; | |
1da177e4 LT |
1040 | } |
1041 | ||
1042 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, | |
1043 | int alen, int flags) | |
1044 | { | |
1045 | int err = 0; | |
1046 | struct sock *sk = sock->sk; | |
1047 | struct netlink_sock *nlk = nlk_sk(sk); | |
6ac552fd | 1048 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
1da177e4 | 1049 | |
6503d961 CG |
1050 | if (alen < sizeof(addr->sa_family)) |
1051 | return -EINVAL; | |
1052 | ||
1da177e4 LT |
1053 | if (addr->sa_family == AF_UNSPEC) { |
1054 | sk->sk_state = NETLINK_UNCONNECTED; | |
15e47304 | 1055 | nlk->dst_portid = 0; |
d629b836 | 1056 | nlk->dst_group = 0; |
1da177e4 LT |
1057 | return 0; |
1058 | } | |
1059 | if (addr->sa_family != AF_NETLINK) | |
1060 | return -EINVAL; | |
1061 | ||
13fdc55c AP |
1062 | if (alen < sizeof(struct sockaddr_nl)) |
1063 | return -EINVAL; | |
1064 | ||
46833a86 | 1065 | if ((nladdr->nl_groups || nladdr->nl_pid) && |
5187cd05 | 1066 | !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND)) |
1da177e4 LT |
1067 | return -EPERM; |
1068 | ||
da314c99 HX |
1069 | /* No need for barriers here as we return to user-space without |
1070 | * using any of the bound attributes. | |
1071 | */ | |
1072 | if (!nlk->bound) | |
1da177e4 LT |
1073 | err = netlink_autobind(sock); |
1074 | ||
1075 | if (err == 0) { | |
1076 | sk->sk_state = NETLINK_CONNECTED; | |
15e47304 | 1077 | nlk->dst_portid = nladdr->nl_pid; |
d629b836 | 1078 | nlk->dst_group = ffs(nladdr->nl_groups); |
1da177e4 LT |
1079 | } |
1080 | ||
1081 | return err; | |
1082 | } | |
1083 | ||
6ac552fd PM |
1084 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, |
1085 | int *addr_len, int peer) | |
1da177e4 LT |
1086 | { |
1087 | struct sock *sk = sock->sk; | |
1088 | struct netlink_sock *nlk = nlk_sk(sk); | |
13cfa97b | 1089 | DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr); |
746fac4d | 1090 | |
1da177e4 LT |
1091 | nladdr->nl_family = AF_NETLINK; |
1092 | nladdr->nl_pad = 0; | |
1093 | *addr_len = sizeof(*nladdr); | |
1094 | ||
1095 | if (peer) { | |
15e47304 | 1096 | nladdr->nl_pid = nlk->dst_portid; |
d629b836 | 1097 | nladdr->nl_groups = netlink_group_mask(nlk->dst_group); |
1da177e4 | 1098 | } else { |
15e47304 | 1099 | nladdr->nl_pid = nlk->portid; |
f7736080 | 1100 | netlink_lock_table(); |
513c2500 | 1101 | nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; |
f7736080 | 1102 | netlink_unlock_table(); |
1da177e4 LT |
1103 | } |
1104 | return 0; | |
1105 | } | |
1106 | ||
025c6818 DD |
1107 | static int netlink_ioctl(struct socket *sock, unsigned int cmd, |
1108 | unsigned long arg) | |
1109 | { | |
1110 | /* try to hand this ioctl down to the NIC drivers. | |
1111 | */ | |
1112 | return -ENOIOCTLCMD; | |
1113 | } | |
1114 | ||
15e47304 | 1115 | static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid) |
1da177e4 | 1116 | { |
1da177e4 LT |
1117 | struct sock *sock; |
1118 | struct netlink_sock *nlk; | |
1119 | ||
15e47304 | 1120 | sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid); |
1da177e4 LT |
1121 | if (!sock) |
1122 | return ERR_PTR(-ECONNREFUSED); | |
1123 | ||
1124 | /* Don't bother queuing skb if kernel socket has no input function */ | |
1125 | nlk = nlk_sk(sock); | |
cd40b7d3 | 1126 | if (sock->sk_state == NETLINK_CONNECTED && |
15e47304 | 1127 | nlk->dst_portid != nlk_sk(ssk)->portid) { |
1da177e4 LT |
1128 | sock_put(sock); |
1129 | return ERR_PTR(-ECONNREFUSED); | |
1130 | } | |
1131 | return sock; | |
1132 | } | |
1133 | ||
1134 | struct sock *netlink_getsockbyfilp(struct file *filp) | |
1135 | { | |
496ad9aa | 1136 | struct inode *inode = file_inode(filp); |
1da177e4 LT |
1137 | struct sock *sock; |
1138 | ||
1139 | if (!S_ISSOCK(inode->i_mode)) | |
1140 | return ERR_PTR(-ENOTSOCK); | |
1141 | ||
1142 | sock = SOCKET_I(inode)->sk; | |
1143 | if (sock->sk_family != AF_NETLINK) | |
1144 | return ERR_PTR(-EINVAL); | |
1145 | ||
1146 | sock_hold(sock); | |
1147 | return sock; | |
1148 | } | |
1149 | ||
3a36515f PN |
1150 | static struct sk_buff *netlink_alloc_large_skb(unsigned int size, |
1151 | int broadcast) | |
c05cdb1b PNA |
1152 | { |
1153 | struct sk_buff *skb; | |
1154 | void *data; | |
1155 | ||
3a36515f | 1156 | if (size <= NLMSG_GOODSIZE || broadcast) |
c05cdb1b PNA |
1157 | return alloc_skb(size, GFP_KERNEL); |
1158 | ||
3a36515f PN |
1159 | size = SKB_DATA_ALIGN(size) + |
1160 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
c05cdb1b PNA |
1161 | |
1162 | data = vmalloc(size); | |
1163 | if (data == NULL) | |
3a36515f | 1164 | return NULL; |
c05cdb1b | 1165 | |
2ea2f62c | 1166 | skb = __build_skb(data, size); |
3a36515f PN |
1167 | if (skb == NULL) |
1168 | vfree(data); | |
2ea2f62c | 1169 | else |
3a36515f | 1170 | skb->destructor = netlink_skb_destructor; |
c05cdb1b PNA |
1171 | |
1172 | return skb; | |
c05cdb1b PNA |
1173 | } |
1174 | ||
1da177e4 LT |
1175 | /* |
1176 | * Attach a skb to a netlink socket. | |
1177 | * The caller must hold a reference to the destination socket. On error, the | |
1178 | * reference is dropped. The skb is not send to the destination, just all | |
1179 | * all error checks are performed and memory in the queue is reserved. | |
1180 | * Return values: | |
1181 | * < 0: error. skb freed, reference to sock dropped. | |
1182 | * 0: continue | |
1183 | * 1: repeat lookup - reference dropped while waiting for socket memory. | |
1184 | */ | |
9457afee | 1185 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, |
c3d8d1e3 | 1186 | long *timeo, struct sock *ssk) |
1da177e4 LT |
1187 | { |
1188 | struct netlink_sock *nlk; | |
1189 | ||
1190 | nlk = nlk_sk(sk); | |
1191 | ||
5fd96123 | 1192 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
d1b4c689 | 1193 | test_bit(NETLINK_S_CONGESTED, &nlk->state))) { |
1da177e4 | 1194 | DECLARE_WAITQUEUE(wait, current); |
c3d8d1e3 | 1195 | if (!*timeo) { |
aed81560 | 1196 | if (!ssk || netlink_is_kernel(ssk)) |
1da177e4 LT |
1197 | netlink_overrun(sk); |
1198 | sock_put(sk); | |
1199 | kfree_skb(skb); | |
1200 | return -EAGAIN; | |
1201 | } | |
1202 | ||
1203 | __set_current_state(TASK_INTERRUPTIBLE); | |
1204 | add_wait_queue(&nlk->wait, &wait); | |
1205 | ||
1206 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || | |
cc3a572f | 1207 | test_bit(NETLINK_S_CONGESTED, &nlk->state)) && |
1da177e4 | 1208 | !sock_flag(sk, SOCK_DEAD)) |
c3d8d1e3 | 1209 | *timeo = schedule_timeout(*timeo); |
1da177e4 LT |
1210 | |
1211 | __set_current_state(TASK_RUNNING); | |
1212 | remove_wait_queue(&nlk->wait, &wait); | |
1213 | sock_put(sk); | |
1214 | ||
1215 | if (signal_pending(current)) { | |
1216 | kfree_skb(skb); | |
c3d8d1e3 | 1217 | return sock_intr_errno(*timeo); |
1da177e4 LT |
1218 | } |
1219 | return 1; | |
1220 | } | |
cf0a018a | 1221 | netlink_skb_set_owner_r(skb, sk); |
1da177e4 LT |
1222 | return 0; |
1223 | } | |
1224 | ||
4a7e7c2a | 1225 | static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
1da177e4 | 1226 | { |
1da177e4 LT |
1227 | int len = skb->len; |
1228 | ||
bcbde0d4 DB |
1229 | netlink_deliver_tap(skb); |
1230 | ||
d1b4c689 | 1231 | skb_queue_tail(&sk->sk_receive_queue, skb); |
676d2369 | 1232 | sk->sk_data_ready(sk); |
4a7e7c2a ED |
1233 | return len; |
1234 | } | |
1235 | ||
1236 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb) | |
1237 | { | |
1238 | int len = __netlink_sendskb(sk, skb); | |
1239 | ||
1da177e4 LT |
1240 | sock_put(sk); |
1241 | return len; | |
1242 | } | |
1243 | ||
1244 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) | |
1245 | { | |
1246 | kfree_skb(skb); | |
1247 | sock_put(sk); | |
1248 | } | |
1249 | ||
b57ef81f | 1250 | static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation) |
1da177e4 LT |
1251 | { |
1252 | int delta; | |
1253 | ||
1298ca46 | 1254 | WARN_ON(skb->sk != NULL); |
4305b541 | 1255 | delta = skb->end - skb->tail; |
c05cdb1b | 1256 | if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize) |
1da177e4 LT |
1257 | return skb; |
1258 | ||
1259 | if (skb_shared(skb)) { | |
1260 | struct sk_buff *nskb = skb_clone(skb, allocation); | |
1261 | if (!nskb) | |
1262 | return skb; | |
8460c00f | 1263 | consume_skb(skb); |
1da177e4 LT |
1264 | skb = nskb; |
1265 | } | |
1266 | ||
158f323b ED |
1267 | pskb_expand_head(skb, 0, -delta, |
1268 | (allocation & ~__GFP_DIRECT_RECLAIM) | | |
1269 | __GFP_NOWARN | __GFP_NORETRY); | |
1da177e4 LT |
1270 | return skb; |
1271 | } | |
1272 | ||
3fbc2905 EB |
1273 | static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb, |
1274 | struct sock *ssk) | |
cd40b7d3 DL |
1275 | { |
1276 | int ret; | |
1277 | struct netlink_sock *nlk = nlk_sk(sk); | |
1278 | ||
1279 | ret = -ECONNREFUSED; | |
1280 | if (nlk->netlink_rcv != NULL) { | |
1281 | ret = skb->len; | |
cf0a018a | 1282 | netlink_skb_set_owner_r(skb, sk); |
e32123e5 | 1283 | NETLINK_CB(skb).sk = ssk; |
73bfd370 | 1284 | netlink_deliver_tap_kernel(sk, ssk, skb); |
cd40b7d3 | 1285 | nlk->netlink_rcv(skb); |
bfb253c9 ED |
1286 | consume_skb(skb); |
1287 | } else { | |
1288 | kfree_skb(skb); | |
cd40b7d3 | 1289 | } |
cd40b7d3 DL |
1290 | sock_put(sk); |
1291 | return ret; | |
1292 | } | |
1293 | ||
1294 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, | |
15e47304 | 1295 | u32 portid, int nonblock) |
1da177e4 LT |
1296 | { |
1297 | struct sock *sk; | |
1298 | int err; | |
1299 | long timeo; | |
1300 | ||
1301 | skb = netlink_trim(skb, gfp_any()); | |
1302 | ||
1303 | timeo = sock_sndtimeo(ssk, nonblock); | |
1304 | retry: | |
15e47304 | 1305 | sk = netlink_getsockbyportid(ssk, portid); |
1da177e4 LT |
1306 | if (IS_ERR(sk)) { |
1307 | kfree_skb(skb); | |
1308 | return PTR_ERR(sk); | |
1309 | } | |
cd40b7d3 | 1310 | if (netlink_is_kernel(sk)) |
3fbc2905 | 1311 | return netlink_unicast_kernel(sk, skb, ssk); |
cd40b7d3 | 1312 | |
b1153f29 | 1313 | if (sk_filter(sk, skb)) { |
84874607 | 1314 | err = skb->len; |
b1153f29 SH |
1315 | kfree_skb(skb); |
1316 | sock_put(sk); | |
1317 | return err; | |
1318 | } | |
1319 | ||
9457afee | 1320 | err = netlink_attachskb(sk, skb, &timeo, ssk); |
1da177e4 LT |
1321 | if (err == 1) |
1322 | goto retry; | |
1323 | if (err) | |
1324 | return err; | |
1325 | ||
7ee015e0 | 1326 | return netlink_sendskb(sk, skb); |
1da177e4 | 1327 | } |
6ac552fd | 1328 | EXPORT_SYMBOL(netlink_unicast); |
1da177e4 | 1329 | |
4277a083 PM |
1330 | int netlink_has_listeners(struct sock *sk, unsigned int group) |
1331 | { | |
1332 | int res = 0; | |
5c398dc8 | 1333 | struct listeners *listeners; |
4277a083 | 1334 | |
aed81560 | 1335 | BUG_ON(!netlink_is_kernel(sk)); |
b4ff4f04 JB |
1336 | |
1337 | rcu_read_lock(); | |
1338 | listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners); | |
1339 | ||
6d772ac5 | 1340 | if (listeners && group - 1 < nl_table[sk->sk_protocol].groups) |
5c398dc8 | 1341 | res = test_bit(group - 1, listeners->masks); |
b4ff4f04 JB |
1342 | |
1343 | rcu_read_unlock(); | |
1344 | ||
4277a083 PM |
1345 | return res; |
1346 | } | |
1347 | EXPORT_SYMBOL_GPL(netlink_has_listeners); | |
1348 | ||
b57ef81f | 1349 | static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) |
1da177e4 LT |
1350 | { |
1351 | struct netlink_sock *nlk = nlk_sk(sk); | |
1352 | ||
1353 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && | |
cc3a572f | 1354 | !test_bit(NETLINK_S_CONGESTED, &nlk->state)) { |
cf0a018a | 1355 | netlink_skb_set_owner_r(skb, sk); |
4a7e7c2a | 1356 | __netlink_sendskb(sk, skb); |
2c645800 | 1357 | return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1); |
1da177e4 LT |
1358 | } |
1359 | return -1; | |
1360 | } | |
1361 | ||
1362 | struct netlink_broadcast_data { | |
1363 | struct sock *exclude_sk; | |
b4b51029 | 1364 | struct net *net; |
15e47304 | 1365 | u32 portid; |
1da177e4 LT |
1366 | u32 group; |
1367 | int failure; | |
ff491a73 | 1368 | int delivery_failure; |
1da177e4 LT |
1369 | int congested; |
1370 | int delivered; | |
7d877f3b | 1371 | gfp_t allocation; |
1da177e4 | 1372 | struct sk_buff *skb, *skb2; |
910a7e90 EB |
1373 | int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data); |
1374 | void *tx_data; | |
1da177e4 LT |
1375 | }; |
1376 | ||
46c9521f RR |
1377 | static void do_one_broadcast(struct sock *sk, |
1378 | struct netlink_broadcast_data *p) | |
1da177e4 LT |
1379 | { |
1380 | struct netlink_sock *nlk = nlk_sk(sk); | |
1381 | int val; | |
1382 | ||
1383 | if (p->exclude_sk == sk) | |
46c9521f | 1384 | return; |
1da177e4 | 1385 | |
15e47304 | 1386 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
f7fa9b10 | 1387 | !test_bit(p->group - 1, nlk->groups)) |
46c9521f | 1388 | return; |
1da177e4 | 1389 | |
59324cf3 ND |
1390 | if (!net_eq(sock_net(sk), p->net)) { |
1391 | if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID)) | |
1392 | return; | |
1393 | ||
1394 | if (!peernet_has_id(sock_net(sk), p->net)) | |
1395 | return; | |
1396 | ||
1397 | if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns, | |
1398 | CAP_NET_BROADCAST)) | |
1399 | return; | |
1400 | } | |
b4b51029 | 1401 | |
1da177e4 LT |
1402 | if (p->failure) { |
1403 | netlink_overrun(sk); | |
46c9521f | 1404 | return; |
1da177e4 LT |
1405 | } |
1406 | ||
1407 | sock_hold(sk); | |
1408 | if (p->skb2 == NULL) { | |
68acc024 | 1409 | if (skb_shared(p->skb)) { |
1da177e4 LT |
1410 | p->skb2 = skb_clone(p->skb, p->allocation); |
1411 | } else { | |
68acc024 TC |
1412 | p->skb2 = skb_get(p->skb); |
1413 | /* | |
1414 | * skb ownership may have been set when | |
1415 | * delivered to a previous socket. | |
1416 | */ | |
1417 | skb_orphan(p->skb2); | |
1da177e4 LT |
1418 | } |
1419 | } | |
1420 | if (p->skb2 == NULL) { | |
1421 | netlink_overrun(sk); | |
1422 | /* Clone failed. Notify ALL listeners. */ | |
1423 | p->failure = 1; | |
cc3a572f | 1424 | if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR) |
be0c22a4 | 1425 | p->delivery_failure = 1; |
59324cf3 ND |
1426 | goto out; |
1427 | } | |
1428 | if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) { | |
910a7e90 EB |
1429 | kfree_skb(p->skb2); |
1430 | p->skb2 = NULL; | |
59324cf3 ND |
1431 | goto out; |
1432 | } | |
1433 | if (sk_filter(sk, p->skb2)) { | |
b1153f29 SH |
1434 | kfree_skb(p->skb2); |
1435 | p->skb2 = NULL; | |
59324cf3 ND |
1436 | goto out; |
1437 | } | |
1438 | NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net); | |
7212462f ND |
1439 | if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED) |
1440 | NETLINK_CB(p->skb2).nsid_is_set = true; | |
59324cf3 ND |
1441 | val = netlink_broadcast_deliver(sk, p->skb2); |
1442 | if (val < 0) { | |
1da177e4 | 1443 | netlink_overrun(sk); |
cc3a572f | 1444 | if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR) |
be0c22a4 | 1445 | p->delivery_failure = 1; |
1da177e4 LT |
1446 | } else { |
1447 | p->congested |= val; | |
1448 | p->delivered = 1; | |
1449 | p->skb2 = NULL; | |
1450 | } | |
59324cf3 | 1451 | out: |
1da177e4 | 1452 | sock_put(sk); |
1da177e4 LT |
1453 | } |
1454 | ||
15e47304 | 1455 | int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid, |
910a7e90 EB |
1456 | u32 group, gfp_t allocation, |
1457 | int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data), | |
1458 | void *filter_data) | |
1da177e4 | 1459 | { |
3b1e0a65 | 1460 | struct net *net = sock_net(ssk); |
1da177e4 | 1461 | struct netlink_broadcast_data info; |
1da177e4 LT |
1462 | struct sock *sk; |
1463 | ||
1464 | skb = netlink_trim(skb, allocation); | |
1465 | ||
1466 | info.exclude_sk = ssk; | |
b4b51029 | 1467 | info.net = net; |
15e47304 | 1468 | info.portid = portid; |
1da177e4 LT |
1469 | info.group = group; |
1470 | info.failure = 0; | |
ff491a73 | 1471 | info.delivery_failure = 0; |
1da177e4 LT |
1472 | info.congested = 0; |
1473 | info.delivered = 0; | |
1474 | info.allocation = allocation; | |
1475 | info.skb = skb; | |
1476 | info.skb2 = NULL; | |
910a7e90 EB |
1477 | info.tx_filter = filter; |
1478 | info.tx_data = filter_data; | |
1da177e4 LT |
1479 | |
1480 | /* While we sleep in clone, do not allow to change socket list */ | |
1481 | ||
1482 | netlink_lock_table(); | |
1483 | ||
b67bfe0d | 1484 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
1da177e4 LT |
1485 | do_one_broadcast(sk, &info); |
1486 | ||
70d4bf6d | 1487 | consume_skb(skb); |
aa1c6a6f | 1488 | |
1da177e4 LT |
1489 | netlink_unlock_table(); |
1490 | ||
70d4bf6d NH |
1491 | if (info.delivery_failure) { |
1492 | kfree_skb(info.skb2); | |
ff491a73 | 1493 | return -ENOBUFS; |
658cb354 ED |
1494 | } |
1495 | consume_skb(info.skb2); | |
ff491a73 | 1496 | |
1da177e4 | 1497 | if (info.delivered) { |
d0164adc | 1498 | if (info.congested && gfpflags_allow_blocking(allocation)) |
1da177e4 LT |
1499 | yield(); |
1500 | return 0; | |
1501 | } | |
1da177e4 LT |
1502 | return -ESRCH; |
1503 | } | |
910a7e90 EB |
1504 | EXPORT_SYMBOL(netlink_broadcast_filtered); |
1505 | ||
15e47304 | 1506 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid, |
910a7e90 EB |
1507 | u32 group, gfp_t allocation) |
1508 | { | |
15e47304 | 1509 | return netlink_broadcast_filtered(ssk, skb, portid, group, allocation, |
910a7e90 EB |
1510 | NULL, NULL); |
1511 | } | |
6ac552fd | 1512 | EXPORT_SYMBOL(netlink_broadcast); |
1da177e4 LT |
1513 | |
1514 | struct netlink_set_err_data { | |
1515 | struct sock *exclude_sk; | |
15e47304 | 1516 | u32 portid; |
1da177e4 LT |
1517 | u32 group; |
1518 | int code; | |
1519 | }; | |
1520 | ||
b57ef81f | 1521 | static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p) |
1da177e4 LT |
1522 | { |
1523 | struct netlink_sock *nlk = nlk_sk(sk); | |
1a50307b | 1524 | int ret = 0; |
1da177e4 LT |
1525 | |
1526 | if (sk == p->exclude_sk) | |
1527 | goto out; | |
1528 | ||
09ad9bc7 | 1529 | if (!net_eq(sock_net(sk), sock_net(p->exclude_sk))) |
b4b51029 EB |
1530 | goto out; |
1531 | ||
15e47304 | 1532 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
f7fa9b10 | 1533 | !test_bit(p->group - 1, nlk->groups)) |
1da177e4 LT |
1534 | goto out; |
1535 | ||
cc3a572f | 1536 | if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) { |
1a50307b PNA |
1537 | ret = 1; |
1538 | goto out; | |
1539 | } | |
1540 | ||
1da177e4 LT |
1541 | sk->sk_err = p->code; |
1542 | sk->sk_error_report(sk); | |
1543 | out: | |
1a50307b | 1544 | return ret; |
1da177e4 LT |
1545 | } |
1546 | ||
4843b93c PNA |
1547 | /** |
1548 | * netlink_set_err - report error to broadcast listeners | |
1549 | * @ssk: the kernel netlink socket, as returned by netlink_kernel_create() | |
15e47304 | 1550 | * @portid: the PORTID of a process that we want to skip (if any) |
840e93f2 | 1551 | * @group: the broadcast group that will notice the error |
4843b93c | 1552 | * @code: error code, must be negative (as usual in kernelspace) |
1a50307b PNA |
1553 | * |
1554 | * This function returns the number of broadcast listeners that have set the | |
cc3a572f | 1555 | * NETLINK_NO_ENOBUFS socket option. |
4843b93c | 1556 | */ |
15e47304 | 1557 | int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code) |
1da177e4 LT |
1558 | { |
1559 | struct netlink_set_err_data info; | |
1da177e4 | 1560 | struct sock *sk; |
1a50307b | 1561 | int ret = 0; |
1da177e4 LT |
1562 | |
1563 | info.exclude_sk = ssk; | |
15e47304 | 1564 | info.portid = portid; |
1da177e4 | 1565 | info.group = group; |
4843b93c PNA |
1566 | /* sk->sk_err wants a positive error value */ |
1567 | info.code = -code; | |
1da177e4 LT |
1568 | |
1569 | read_lock(&nl_table_lock); | |
1570 | ||
b67bfe0d | 1571 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
1a50307b | 1572 | ret += do_one_set_err(sk, &info); |
1da177e4 LT |
1573 | |
1574 | read_unlock(&nl_table_lock); | |
1a50307b | 1575 | return ret; |
1da177e4 | 1576 | } |
dd5b6ce6 | 1577 | EXPORT_SYMBOL(netlink_set_err); |
1da177e4 | 1578 | |
84659eb5 JB |
1579 | /* must be called with netlink table grabbed */ |
1580 | static void netlink_update_socket_mc(struct netlink_sock *nlk, | |
1581 | unsigned int group, | |
1582 | int is_new) | |
1583 | { | |
1584 | int old, new = !!is_new, subscriptions; | |
1585 | ||
1586 | old = test_bit(group - 1, nlk->groups); | |
1587 | subscriptions = nlk->subscriptions - old + new; | |
1588 | if (new) | |
1589 | __set_bit(group - 1, nlk->groups); | |
1590 | else | |
1591 | __clear_bit(group - 1, nlk->groups); | |
1592 | netlink_update_subscriptions(&nlk->sk, subscriptions); | |
1593 | netlink_update_listeners(&nlk->sk); | |
1594 | } | |
1595 | ||
9a4595bc | 1596 | static int netlink_setsockopt(struct socket *sock, int level, int optname, |
b7058842 | 1597 | char __user *optval, unsigned int optlen) |
9a4595bc PM |
1598 | { |
1599 | struct sock *sk = sock->sk; | |
1600 | struct netlink_sock *nlk = nlk_sk(sk); | |
eb496534 JB |
1601 | unsigned int val = 0; |
1602 | int err; | |
9a4595bc PM |
1603 | |
1604 | if (level != SOL_NETLINK) | |
1605 | return -ENOPROTOOPT; | |
1606 | ||
d1b4c689 | 1607 | if (optlen >= sizeof(int) && |
eb496534 | 1608 | get_user(val, (unsigned int __user *)optval)) |
9a4595bc PM |
1609 | return -EFAULT; |
1610 | ||
1611 | switch (optname) { | |
1612 | case NETLINK_PKTINFO: | |
1613 | if (val) | |
cc3a572f | 1614 | nlk->flags |= NETLINK_F_RECV_PKTINFO; |
9a4595bc | 1615 | else |
cc3a572f | 1616 | nlk->flags &= ~NETLINK_F_RECV_PKTINFO; |
9a4595bc PM |
1617 | err = 0; |
1618 | break; | |
1619 | case NETLINK_ADD_MEMBERSHIP: | |
1620 | case NETLINK_DROP_MEMBERSHIP: { | |
5187cd05 | 1621 | if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV)) |
9a4595bc | 1622 | return -EPERM; |
b4ff4f04 JB |
1623 | err = netlink_realloc_groups(sk); |
1624 | if (err) | |
1625 | return err; | |
9a4595bc PM |
1626 | if (!val || val - 1 >= nlk->ngroups) |
1627 | return -EINVAL; | |
7774d5e0 | 1628 | if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) { |
023e2cfa | 1629 | err = nlk->netlink_bind(sock_net(sk), val); |
4f520900 RGB |
1630 | if (err) |
1631 | return err; | |
1632 | } | |
9a4595bc | 1633 | netlink_table_grab(); |
84659eb5 JB |
1634 | netlink_update_socket_mc(nlk, val, |
1635 | optname == NETLINK_ADD_MEMBERSHIP); | |
9a4595bc | 1636 | netlink_table_ungrab(); |
7774d5e0 | 1637 | if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind) |
023e2cfa | 1638 | nlk->netlink_unbind(sock_net(sk), val); |
03292745 | 1639 | |
9a4595bc PM |
1640 | err = 0; |
1641 | break; | |
1642 | } | |
be0c22a4 PNA |
1643 | case NETLINK_BROADCAST_ERROR: |
1644 | if (val) | |
cc3a572f | 1645 | nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR; |
be0c22a4 | 1646 | else |
cc3a572f | 1647 | nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR; |
be0c22a4 PNA |
1648 | err = 0; |
1649 | break; | |
38938bfe PNA |
1650 | case NETLINK_NO_ENOBUFS: |
1651 | if (val) { | |
cc3a572f ND |
1652 | nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS; |
1653 | clear_bit(NETLINK_S_CONGESTED, &nlk->state); | |
38938bfe | 1654 | wake_up_interruptible(&nlk->wait); |
658cb354 | 1655 | } else { |
cc3a572f | 1656 | nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS; |
658cb354 | 1657 | } |
38938bfe PNA |
1658 | err = 0; |
1659 | break; | |
59324cf3 ND |
1660 | case NETLINK_LISTEN_ALL_NSID: |
1661 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST)) | |
1662 | return -EPERM; | |
1663 | ||
1664 | if (val) | |
1665 | nlk->flags |= NETLINK_F_LISTEN_ALL_NSID; | |
1666 | else | |
1667 | nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID; | |
1668 | err = 0; | |
1669 | break; | |
0a6a3a23 CR |
1670 | case NETLINK_CAP_ACK: |
1671 | if (val) | |
1672 | nlk->flags |= NETLINK_F_CAP_ACK; | |
1673 | else | |
1674 | nlk->flags &= ~NETLINK_F_CAP_ACK; | |
1675 | err = 0; | |
1676 | break; | |
2d4bc933 JB |
1677 | case NETLINK_EXT_ACK: |
1678 | if (val) | |
1679 | nlk->flags |= NETLINK_F_EXT_ACK; | |
1680 | else | |
1681 | nlk->flags &= ~NETLINK_F_EXT_ACK; | |
1682 | err = 0; | |
1683 | break; | |
9a4595bc PM |
1684 | default: |
1685 | err = -ENOPROTOOPT; | |
1686 | } | |
1687 | return err; | |
1688 | } | |
1689 | ||
1690 | static int netlink_getsockopt(struct socket *sock, int level, int optname, | |
746fac4d | 1691 | char __user *optval, int __user *optlen) |
9a4595bc PM |
1692 | { |
1693 | struct sock *sk = sock->sk; | |
1694 | struct netlink_sock *nlk = nlk_sk(sk); | |
1695 | int len, val, err; | |
1696 | ||
1697 | if (level != SOL_NETLINK) | |
1698 | return -ENOPROTOOPT; | |
1699 | ||
1700 | if (get_user(len, optlen)) | |
1701 | return -EFAULT; | |
1702 | if (len < 0) | |
1703 | return -EINVAL; | |
1704 | ||
1705 | switch (optname) { | |
1706 | case NETLINK_PKTINFO: | |
1707 | if (len < sizeof(int)) | |
1708 | return -EINVAL; | |
1709 | len = sizeof(int); | |
cc3a572f | 1710 | val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0; |
a27b58fe HC |
1711 | if (put_user(len, optlen) || |
1712 | put_user(val, optval)) | |
1713 | return -EFAULT; | |
9a4595bc PM |
1714 | err = 0; |
1715 | break; | |
be0c22a4 PNA |
1716 | case NETLINK_BROADCAST_ERROR: |
1717 | if (len < sizeof(int)) | |
1718 | return -EINVAL; | |
1719 | len = sizeof(int); | |
cc3a572f | 1720 | val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0; |
be0c22a4 PNA |
1721 | if (put_user(len, optlen) || |
1722 | put_user(val, optval)) | |
1723 | return -EFAULT; | |
1724 | err = 0; | |
1725 | break; | |
38938bfe PNA |
1726 | case NETLINK_NO_ENOBUFS: |
1727 | if (len < sizeof(int)) | |
1728 | return -EINVAL; | |
1729 | len = sizeof(int); | |
cc3a572f | 1730 | val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0; |
38938bfe PNA |
1731 | if (put_user(len, optlen) || |
1732 | put_user(val, optval)) | |
1733 | return -EFAULT; | |
1734 | err = 0; | |
1735 | break; | |
b42be38b DH |
1736 | case NETLINK_LIST_MEMBERSHIPS: { |
1737 | int pos, idx, shift; | |
1738 | ||
1739 | err = 0; | |
47191d65 | 1740 | netlink_lock_table(); |
b42be38b DH |
1741 | for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) { |
1742 | if (len - pos < sizeof(u32)) | |
1743 | break; | |
1744 | ||
1745 | idx = pos / sizeof(unsigned long); | |
1746 | shift = (pos % sizeof(unsigned long)) * 8; | |
1747 | if (put_user((u32)(nlk->groups[idx] >> shift), | |
1748 | (u32 __user *)(optval + pos))) { | |
1749 | err = -EFAULT; | |
1750 | break; | |
1751 | } | |
1752 | } | |
1753 | if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen)) | |
1754 | err = -EFAULT; | |
47191d65 | 1755 | netlink_unlock_table(); |
b42be38b DH |
1756 | break; |
1757 | } | |
0a6a3a23 CR |
1758 | case NETLINK_CAP_ACK: |
1759 | if (len < sizeof(int)) | |
1760 | return -EINVAL; | |
1761 | len = sizeof(int); | |
1762 | val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0; | |
1763 | if (put_user(len, optlen) || | |
1764 | put_user(val, optval)) | |
1765 | return -EFAULT; | |
1766 | err = 0; | |
1767 | break; | |
2d4bc933 JB |
1768 | case NETLINK_EXT_ACK: |
1769 | if (len < sizeof(int)) | |
1770 | return -EINVAL; | |
1771 | len = sizeof(int); | |
1772 | val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0; | |
1773 | if (put_user(len, optlen) || put_user(val, optval)) | |
1774 | return -EFAULT; | |
1775 | err = 0; | |
1776 | break; | |
9a4595bc PM |
1777 | default: |
1778 | err = -ENOPROTOOPT; | |
1779 | } | |
1780 | return err; | |
1781 | } | |
1782 | ||
1783 | static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
1784 | { | |
1785 | struct nl_pktinfo info; | |
1786 | ||
1787 | info.group = NETLINK_CB(skb).dst_group; | |
1788 | put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); | |
1789 | } | |
1790 | ||
59324cf3 ND |
1791 | static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg, |
1792 | struct sk_buff *skb) | |
1793 | { | |
1794 | if (!NETLINK_CB(skb).nsid_is_set) | |
1795 | return; | |
1796 | ||
1797 | put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int), | |
1798 | &NETLINK_CB(skb).nsid); | |
1799 | } | |
1800 | ||
1b784140 | 1801 | static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
1da177e4 | 1802 | { |
1da177e4 LT |
1803 | struct sock *sk = sock->sk; |
1804 | struct netlink_sock *nlk = nlk_sk(sk); | |
342dfc30 | 1805 | DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name); |
15e47304 | 1806 | u32 dst_portid; |
d629b836 | 1807 | u32 dst_group; |
1da177e4 LT |
1808 | struct sk_buff *skb; |
1809 | int err; | |
1810 | struct scm_cookie scm; | |
2d7a85f4 | 1811 | u32 netlink_skb_flags = 0; |
1da177e4 LT |
1812 | |
1813 | if (msg->msg_flags&MSG_OOB) | |
1814 | return -EOPNOTSUPP; | |
1815 | ||
7cc05662 | 1816 | err = scm_send(sock, msg, &scm, true); |
1da177e4 LT |
1817 | if (err < 0) |
1818 | return err; | |
1819 | ||
1820 | if (msg->msg_namelen) { | |
b47030c7 | 1821 | err = -EINVAL; |
9d0b5dfc ED |
1822 | if (msg->msg_namelen < sizeof(struct sockaddr_nl)) |
1823 | goto out; | |
1da177e4 | 1824 | if (addr->nl_family != AF_NETLINK) |
b47030c7 | 1825 | goto out; |
15e47304 | 1826 | dst_portid = addr->nl_pid; |
d629b836 | 1827 | dst_group = ffs(addr->nl_groups); |
b47030c7 | 1828 | err = -EPERM; |
15e47304 | 1829 | if ((dst_group || dst_portid) && |
5187cd05 | 1830 | !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND)) |
b47030c7 | 1831 | goto out; |
2d7a85f4 | 1832 | netlink_skb_flags |= NETLINK_SKB_DST; |
1da177e4 | 1833 | } else { |
15e47304 | 1834 | dst_portid = nlk->dst_portid; |
d629b836 | 1835 | dst_group = nlk->dst_group; |
1da177e4 LT |
1836 | } |
1837 | ||
da314c99 | 1838 | if (!nlk->bound) { |
1da177e4 LT |
1839 | err = netlink_autobind(sock); |
1840 | if (err) | |
1841 | goto out; | |
da314c99 HX |
1842 | } else { |
1843 | /* Ensure nlk is hashed and visible. */ | |
1844 | smp_rmb(); | |
1da177e4 LT |
1845 | } |
1846 | ||
1847 | err = -EMSGSIZE; | |
1848 | if (len > sk->sk_sndbuf - 32) | |
1849 | goto out; | |
1850 | err = -ENOBUFS; | |
3a36515f | 1851 | skb = netlink_alloc_large_skb(len, dst_group); |
6ac552fd | 1852 | if (skb == NULL) |
1da177e4 LT |
1853 | goto out; |
1854 | ||
15e47304 | 1855 | NETLINK_CB(skb).portid = nlk->portid; |
d629b836 | 1856 | NETLINK_CB(skb).dst_group = dst_group; |
7cc05662 | 1857 | NETLINK_CB(skb).creds = scm.creds; |
2d7a85f4 | 1858 | NETLINK_CB(skb).flags = netlink_skb_flags; |
1da177e4 | 1859 | |
1da177e4 | 1860 | err = -EFAULT; |
6ce8e9ce | 1861 | if (memcpy_from_msg(skb_put(skb, len), msg, len)) { |
1da177e4 LT |
1862 | kfree_skb(skb); |
1863 | goto out; | |
1864 | } | |
1865 | ||
1866 | err = security_netlink_send(sk, skb); | |
1867 | if (err) { | |
1868 | kfree_skb(skb); | |
1869 | goto out; | |
1870 | } | |
1871 | ||
d629b836 | 1872 | if (dst_group) { |
63354797 | 1873 | refcount_inc(&skb->users); |
15e47304 | 1874 | netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL); |
1da177e4 | 1875 | } |
15e47304 | 1876 | err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT); |
1da177e4 LT |
1877 | |
1878 | out: | |
7cc05662 | 1879 | scm_destroy(&scm); |
1da177e4 LT |
1880 | return err; |
1881 | } | |
1882 | ||
1b784140 | 1883 | static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
1da177e4 LT |
1884 | int flags) |
1885 | { | |
1da177e4 LT |
1886 | struct scm_cookie scm; |
1887 | struct sock *sk = sock->sk; | |
1888 | struct netlink_sock *nlk = nlk_sk(sk); | |
1889 | int noblock = flags&MSG_DONTWAIT; | |
1890 | size_t copied; | |
68d6ac6d | 1891 | struct sk_buff *skb, *data_skb; |
b44d211e | 1892 | int err, ret; |
1da177e4 LT |
1893 | |
1894 | if (flags&MSG_OOB) | |
1895 | return -EOPNOTSUPP; | |
1896 | ||
1897 | copied = 0; | |
1898 | ||
6ac552fd PM |
1899 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
1900 | if (skb == NULL) | |
1da177e4 LT |
1901 | goto out; |
1902 | ||
68d6ac6d JB |
1903 | data_skb = skb; |
1904 | ||
1dacc76d JB |
1905 | #ifdef CONFIG_COMPAT_NETLINK_MESSAGES |
1906 | if (unlikely(skb_shinfo(skb)->frag_list)) { | |
1dacc76d | 1907 | /* |
68d6ac6d JB |
1908 | * If this skb has a frag_list, then here that means that we |
1909 | * will have to use the frag_list skb's data for compat tasks | |
1910 | * and the regular skb's data for normal (non-compat) tasks. | |
1dacc76d | 1911 | * |
68d6ac6d JB |
1912 | * If we need to send the compat skb, assign it to the |
1913 | * 'data_skb' variable so that it will be used below for data | |
1914 | * copying. We keep 'skb' for everything else, including | |
1915 | * freeing both later. | |
1dacc76d | 1916 | */ |
68d6ac6d JB |
1917 | if (flags & MSG_CMSG_COMPAT) |
1918 | data_skb = skb_shinfo(skb)->frag_list; | |
1dacc76d JB |
1919 | } |
1920 | #endif | |
1921 | ||
9063e21f ED |
1922 | /* Record the max length of recvmsg() calls for future allocations */ |
1923 | nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len); | |
1924 | nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len, | |
d35c99ff | 1925 | SKB_WITH_OVERHEAD(32768)); |
9063e21f | 1926 | |
68d6ac6d | 1927 | copied = data_skb->len; |
1da177e4 LT |
1928 | if (len < copied) { |
1929 | msg->msg_flags |= MSG_TRUNC; | |
1930 | copied = len; | |
1931 | } | |
1932 | ||
68d6ac6d | 1933 | skb_reset_transport_header(data_skb); |
51f3d02b | 1934 | err = skb_copy_datagram_msg(data_skb, 0, msg, copied); |
1da177e4 LT |
1935 | |
1936 | if (msg->msg_name) { | |
342dfc30 | 1937 | DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name); |
1da177e4 LT |
1938 | addr->nl_family = AF_NETLINK; |
1939 | addr->nl_pad = 0; | |
15e47304 | 1940 | addr->nl_pid = NETLINK_CB(skb).portid; |
d629b836 | 1941 | addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); |
1da177e4 LT |
1942 | msg->msg_namelen = sizeof(*addr); |
1943 | } | |
1944 | ||
cc3a572f | 1945 | if (nlk->flags & NETLINK_F_RECV_PKTINFO) |
cc9a06cd | 1946 | netlink_cmsg_recv_pktinfo(msg, skb); |
59324cf3 ND |
1947 | if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID) |
1948 | netlink_cmsg_listen_all_nsid(sk, msg, skb); | |
cc9a06cd | 1949 | |
7cc05662 CH |
1950 | memset(&scm, 0, sizeof(scm)); |
1951 | scm.creds = *NETLINK_CREDS(skb); | |
188ccb55 | 1952 | if (flags & MSG_TRUNC) |
68d6ac6d | 1953 | copied = data_skb->len; |
daa3766e | 1954 | |
1da177e4 LT |
1955 | skb_free_datagram(sk, skb); |
1956 | ||
16b304f3 PS |
1957 | if (nlk->cb_running && |
1958 | atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) { | |
b44d211e AV |
1959 | ret = netlink_dump(sk); |
1960 | if (ret) { | |
ac30ef83 | 1961 | sk->sk_err = -ret; |
b44d211e AV |
1962 | sk->sk_error_report(sk); |
1963 | } | |
1964 | } | |
1da177e4 | 1965 | |
7cc05662 | 1966 | scm_recv(sock, msg, &scm, flags); |
1da177e4 LT |
1967 | out: |
1968 | netlink_rcv_wake(sk); | |
1969 | return err ? : copied; | |
1970 | } | |
1971 | ||
676d2369 | 1972 | static void netlink_data_ready(struct sock *sk) |
1da177e4 | 1973 | { |
cd40b7d3 | 1974 | BUG(); |
1da177e4 LT |
1975 | } |
1976 | ||
1977 | /* | |
746fac4d | 1978 | * We export these functions to other modules. They provide a |
1da177e4 LT |
1979 | * complete set of kernel non-blocking support for message |
1980 | * queueing. | |
1981 | */ | |
1982 | ||
1983 | struct sock * | |
9f00d977 PNA |
1984 | __netlink_kernel_create(struct net *net, int unit, struct module *module, |
1985 | struct netlink_kernel_cfg *cfg) | |
1da177e4 LT |
1986 | { |
1987 | struct socket *sock; | |
1988 | struct sock *sk; | |
77247bbb | 1989 | struct netlink_sock *nlk; |
5c398dc8 | 1990 | struct listeners *listeners = NULL; |
a31f2d17 PNA |
1991 | struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL; |
1992 | unsigned int groups; | |
1da177e4 | 1993 | |
fab2caf6 | 1994 | BUG_ON(!nl_table); |
1da177e4 | 1995 | |
6ac552fd | 1996 | if (unit < 0 || unit >= MAX_LINKS) |
1da177e4 LT |
1997 | return NULL; |
1998 | ||
1999 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) | |
2000 | return NULL; | |
13d3078e EB |
2001 | |
2002 | if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0) | |
23fe1866 PE |
2003 | goto out_sock_release_nosk; |
2004 | ||
2005 | sk = sock->sk; | |
4fdb3bb7 | 2006 | |
a31f2d17 | 2007 | if (!cfg || cfg->groups < 32) |
4277a083 | 2008 | groups = 32; |
a31f2d17 PNA |
2009 | else |
2010 | groups = cfg->groups; | |
4277a083 | 2011 | |
5c398dc8 | 2012 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
4277a083 PM |
2013 | if (!listeners) |
2014 | goto out_sock_release; | |
2015 | ||
1da177e4 | 2016 | sk->sk_data_ready = netlink_data_ready; |
a31f2d17 PNA |
2017 | if (cfg && cfg->input) |
2018 | nlk_sk(sk)->netlink_rcv = cfg->input; | |
1da177e4 | 2019 | |
8ea65f4a | 2020 | if (netlink_insert(sk, 0)) |
77247bbb | 2021 | goto out_sock_release; |
4fdb3bb7 | 2022 | |
77247bbb | 2023 | nlk = nlk_sk(sk); |
cc3a572f | 2024 | nlk->flags |= NETLINK_F_KERNEL_SOCKET; |
4fdb3bb7 | 2025 | |
4fdb3bb7 | 2026 | netlink_table_grab(); |
b4b51029 EB |
2027 | if (!nl_table[unit].registered) { |
2028 | nl_table[unit].groups = groups; | |
5c398dc8 | 2029 | rcu_assign_pointer(nl_table[unit].listeners, listeners); |
b4b51029 EB |
2030 | nl_table[unit].cb_mutex = cb_mutex; |
2031 | nl_table[unit].module = module; | |
9785e10a PNA |
2032 | if (cfg) { |
2033 | nl_table[unit].bind = cfg->bind; | |
6251edd9 | 2034 | nl_table[unit].unbind = cfg->unbind; |
9785e10a | 2035 | nl_table[unit].flags = cfg->flags; |
da12c90e G |
2036 | if (cfg->compare) |
2037 | nl_table[unit].compare = cfg->compare; | |
9785e10a | 2038 | } |
b4b51029 | 2039 | nl_table[unit].registered = 1; |
f937f1f4 JJ |
2040 | } else { |
2041 | kfree(listeners); | |
869e58f8 | 2042 | nl_table[unit].registered++; |
b4b51029 | 2043 | } |
4fdb3bb7 | 2044 | netlink_table_ungrab(); |
77247bbb PM |
2045 | return sk; |
2046 | ||
4fdb3bb7 | 2047 | out_sock_release: |
4277a083 | 2048 | kfree(listeners); |
9dfbec1f | 2049 | netlink_kernel_release(sk); |
23fe1866 PE |
2050 | return NULL; |
2051 | ||
2052 | out_sock_release_nosk: | |
4fdb3bb7 | 2053 | sock_release(sock); |
77247bbb | 2054 | return NULL; |
1da177e4 | 2055 | } |
9f00d977 | 2056 | EXPORT_SYMBOL(__netlink_kernel_create); |
b7c6ba6e DL |
2057 | |
2058 | void | |
2059 | netlink_kernel_release(struct sock *sk) | |
2060 | { | |
13d3078e EB |
2061 | if (sk == NULL || sk->sk_socket == NULL) |
2062 | return; | |
2063 | ||
2064 | sock_release(sk->sk_socket); | |
b7c6ba6e DL |
2065 | } |
2066 | EXPORT_SYMBOL(netlink_kernel_release); | |
2067 | ||
d136f1bd | 2068 | int __netlink_change_ngroups(struct sock *sk, unsigned int groups) |
b4ff4f04 | 2069 | { |
5c398dc8 | 2070 | struct listeners *new, *old; |
b4ff4f04 | 2071 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
b4ff4f04 JB |
2072 | |
2073 | if (groups < 32) | |
2074 | groups = 32; | |
2075 | ||
b4ff4f04 | 2076 | if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) { |
5c398dc8 ED |
2077 | new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC); |
2078 | if (!new) | |
d136f1bd | 2079 | return -ENOMEM; |
6d772ac5 | 2080 | old = nl_deref_protected(tbl->listeners); |
5c398dc8 ED |
2081 | memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups)); |
2082 | rcu_assign_pointer(tbl->listeners, new); | |
2083 | ||
37b6b935 | 2084 | kfree_rcu(old, rcu); |
b4ff4f04 JB |
2085 | } |
2086 | tbl->groups = groups; | |
2087 | ||
d136f1bd JB |
2088 | return 0; |
2089 | } | |
2090 | ||
2091 | /** | |
2092 | * netlink_change_ngroups - change number of multicast groups | |
2093 | * | |
2094 | * This changes the number of multicast groups that are available | |
2095 | * on a certain netlink family. Note that it is not possible to | |
2096 | * change the number of groups to below 32. Also note that it does | |
2097 | * not implicitly call netlink_clear_multicast_users() when the | |
2098 | * number of groups is reduced. | |
2099 | * | |
2100 | * @sk: The kernel netlink socket, as returned by netlink_kernel_create(). | |
2101 | * @groups: The new number of groups. | |
2102 | */ | |
2103 | int netlink_change_ngroups(struct sock *sk, unsigned int groups) | |
2104 | { | |
2105 | int err; | |
2106 | ||
2107 | netlink_table_grab(); | |
2108 | err = __netlink_change_ngroups(sk, groups); | |
b4ff4f04 | 2109 | netlink_table_ungrab(); |
d136f1bd | 2110 | |
b4ff4f04 JB |
2111 | return err; |
2112 | } | |
b4ff4f04 | 2113 | |
b8273570 JB |
2114 | void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group) |
2115 | { | |
2116 | struct sock *sk; | |
b8273570 JB |
2117 | struct netlink_table *tbl = &nl_table[ksk->sk_protocol]; |
2118 | ||
b67bfe0d | 2119 | sk_for_each_bound(sk, &tbl->mc_list) |
b8273570 JB |
2120 | netlink_update_socket_mc(nlk_sk(sk), group, 0); |
2121 | } | |
2122 | ||
a46621a3 | 2123 | struct nlmsghdr * |
15e47304 | 2124 | __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags) |
a46621a3 DV |
2125 | { |
2126 | struct nlmsghdr *nlh; | |
573ce260 | 2127 | int size = nlmsg_msg_size(len); |
a46621a3 | 2128 | |
4df864c1 | 2129 | nlh = skb_put(skb, NLMSG_ALIGN(size)); |
a46621a3 DV |
2130 | nlh->nlmsg_type = type; |
2131 | nlh->nlmsg_len = size; | |
2132 | nlh->nlmsg_flags = flags; | |
15e47304 | 2133 | nlh->nlmsg_pid = portid; |
a46621a3 DV |
2134 | nlh->nlmsg_seq = seq; |
2135 | if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0) | |
573ce260 | 2136 | memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size); |
a46621a3 DV |
2137 | return nlh; |
2138 | } | |
2139 | EXPORT_SYMBOL(__nlmsg_put); | |
2140 | ||
1da177e4 LT |
2141 | /* |
2142 | * It looks a bit ugly. | |
2143 | * It would be better to create kernel thread. | |
2144 | */ | |
2145 | ||
2146 | static int netlink_dump(struct sock *sk) | |
2147 | { | |
2148 | struct netlink_sock *nlk = nlk_sk(sk); | |
2149 | struct netlink_callback *cb; | |
c7ac8679 | 2150 | struct sk_buff *skb = NULL; |
1da177e4 | 2151 | struct nlmsghdr *nlh; |
92964c79 | 2152 | struct module *module; |
0642840b | 2153 | int err = -ENOBUFS; |
db65a3aa | 2154 | int alloc_min_size; |
c7ac8679 | 2155 | int alloc_size; |
1da177e4 | 2156 | |
af65bdfc | 2157 | mutex_lock(nlk->cb_mutex); |
16b304f3 | 2158 | if (!nlk->cb_running) { |
bf8b79e4 TG |
2159 | err = -EINVAL; |
2160 | goto errout_skb; | |
1da177e4 LT |
2161 | } |
2162 | ||
d1b4c689 | 2163 | if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) |
f9c22888 | 2164 | goto errout_skb; |
9063e21f ED |
2165 | |
2166 | /* NLMSG_GOODSIZE is small to avoid high order allocations being | |
2167 | * required, but it makes sense to _attempt_ a 16K bytes allocation | |
2168 | * to reduce number of system calls on dump operations, if user | |
2169 | * ever provided a big enough buffer. | |
2170 | */ | |
db65a3aa AR |
2171 | cb = &nlk->cb; |
2172 | alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); | |
2173 | ||
2174 | if (alloc_min_size < nlk->max_recvmsg_len) { | |
2175 | alloc_size = nlk->max_recvmsg_len; | |
d35c99ff ED |
2176 | skb = alloc_skb(alloc_size, |
2177 | (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) | | |
2178 | __GFP_NOWARN | __GFP_NORETRY); | |
9063e21f | 2179 | } |
db65a3aa AR |
2180 | if (!skb) { |
2181 | alloc_size = alloc_min_size; | |
c5b0db32 | 2182 | skb = alloc_skb(alloc_size, GFP_KERNEL); |
db65a3aa | 2183 | } |
c7ac8679 | 2184 | if (!skb) |
c63d6ea3 | 2185 | goto errout_skb; |
db65a3aa AR |
2186 | |
2187 | /* Trim skb to allocated size. User is expected to provide buffer as | |
2188 | * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at | |
2189 | * netlink_recvmsg())). dump will pack as many smaller messages as | |
2190 | * could fit within the allocated skb. skb is typically allocated | |
2191 | * with larger space than required (could be as much as near 2x the | |
2192 | * requested size with align to next power of 2 approach). Allowing | |
2193 | * dump to use the excess space makes it difficult for a user to have a | |
2194 | * reasonable static buffer based on the expected largest dump of a | |
2195 | * single netdev. The outcome is MSG_TRUNC error. | |
2196 | */ | |
d1b4c689 | 2197 | skb_reserve(skb, skb_tailroom(skb) - alloc_size); |
f9c22888 | 2198 | netlink_skb_set_owner_r(skb, sk); |
c7ac8679 | 2199 | |
0642840b JD |
2200 | if (nlk->dump_done_errno > 0) |
2201 | nlk->dump_done_errno = cb->dump(skb, cb); | |
1da177e4 | 2202 | |
0642840b JD |
2203 | if (nlk->dump_done_errno > 0 || |
2204 | skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) { | |
af65bdfc | 2205 | mutex_unlock(nlk->cb_mutex); |
b1153f29 SH |
2206 | |
2207 | if (sk_filter(sk, skb)) | |
2208 | kfree_skb(skb); | |
4a7e7c2a ED |
2209 | else |
2210 | __netlink_sendskb(sk, skb); | |
1da177e4 LT |
2211 | return 0; |
2212 | } | |
2213 | ||
0642840b JD |
2214 | nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, |
2215 | sizeof(nlk->dump_done_errno), NLM_F_MULTI); | |
2216 | if (WARN_ON(!nlh)) | |
bf8b79e4 TG |
2217 | goto errout_skb; |
2218 | ||
670dc283 JB |
2219 | nl_dump_check_consistent(cb, nlh); |
2220 | ||
0642840b JD |
2221 | memcpy(nlmsg_data(nlh), &nlk->dump_done_errno, |
2222 | sizeof(nlk->dump_done_errno)); | |
bf8b79e4 | 2223 | |
b1153f29 SH |
2224 | if (sk_filter(sk, skb)) |
2225 | kfree_skb(skb); | |
4a7e7c2a ED |
2226 | else |
2227 | __netlink_sendskb(sk, skb); | |
1da177e4 | 2228 | |
a8f74b22 TG |
2229 | if (cb->done) |
2230 | cb->done(cb); | |
1da177e4 | 2231 | |
16b304f3 | 2232 | nlk->cb_running = false; |
92964c79 HX |
2233 | module = cb->module; |
2234 | skb = cb->skb; | |
16b304f3 | 2235 | mutex_unlock(nlk->cb_mutex); |
92964c79 HX |
2236 | module_put(module); |
2237 | consume_skb(skb); | |
1da177e4 | 2238 | return 0; |
1797754e | 2239 | |
bf8b79e4 | 2240 | errout_skb: |
af65bdfc | 2241 | mutex_unlock(nlk->cb_mutex); |
bf8b79e4 | 2242 | kfree_skb(skb); |
bf8b79e4 | 2243 | return err; |
1da177e4 LT |
2244 | } |
2245 | ||
6dc878a8 G |
2246 | int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb, |
2247 | const struct nlmsghdr *nlh, | |
2248 | struct netlink_dump_control *control) | |
1da177e4 LT |
2249 | { |
2250 | struct netlink_callback *cb; | |
2251 | struct sock *sk; | |
2252 | struct netlink_sock *nlk; | |
b44d211e | 2253 | int ret; |
1da177e4 | 2254 | |
63354797 | 2255 | refcount_inc(&skb->users); |
f9c22888 | 2256 | |
15e47304 | 2257 | sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid); |
1da177e4 | 2258 | if (sk == NULL) { |
16b304f3 PS |
2259 | ret = -ECONNREFUSED; |
2260 | goto error_free; | |
1da177e4 | 2261 | } |
6dc878a8 | 2262 | |
16b304f3 | 2263 | nlk = nlk_sk(sk); |
af65bdfc | 2264 | mutex_lock(nlk->cb_mutex); |
6dc878a8 | 2265 | /* A dump is in progress... */ |
16b304f3 | 2266 | if (nlk->cb_running) { |
6dc878a8 | 2267 | ret = -EBUSY; |
16b304f3 | 2268 | goto error_unlock; |
1da177e4 | 2269 | } |
6dc878a8 | 2270 | /* add reference of module which cb->dump belongs to */ |
16b304f3 | 2271 | if (!try_module_get(control->module)) { |
6dc878a8 | 2272 | ret = -EPROTONOSUPPORT; |
16b304f3 | 2273 | goto error_unlock; |
6dc878a8 G |
2274 | } |
2275 | ||
16b304f3 PS |
2276 | cb = &nlk->cb; |
2277 | memset(cb, 0, sizeof(*cb)); | |
fc9e50f5 | 2278 | cb->start = control->start; |
16b304f3 PS |
2279 | cb->dump = control->dump; |
2280 | cb->done = control->done; | |
2281 | cb->nlh = nlh; | |
2282 | cb->data = control->data; | |
2283 | cb->module = control->module; | |
2284 | cb->min_dump_alloc = control->min_dump_alloc; | |
2285 | cb->skb = skb; | |
2286 | ||
41c87425 JD |
2287 | if (cb->start) { |
2288 | ret = cb->start(cb); | |
2289 | if (ret) | |
29fc657e | 2290 | goto error_put; |
41c87425 JD |
2291 | } |
2292 | ||
16b304f3 | 2293 | nlk->cb_running = true; |
0642840b | 2294 | nlk->dump_done_errno = INT_MAX; |
16b304f3 | 2295 | |
af65bdfc | 2296 | mutex_unlock(nlk->cb_mutex); |
1da177e4 | 2297 | |
41c87425 | 2298 | ret = netlink_dump(sk); |
fc9e50f5 | 2299 | |
1da177e4 | 2300 | sock_put(sk); |
5c58298c | 2301 | |
b44d211e AV |
2302 | if (ret) |
2303 | return ret; | |
2304 | ||
5c58298c DL |
2305 | /* We successfully started a dump, by returning -EINTR we |
2306 | * signal not to send ACK even if it was requested. | |
2307 | */ | |
2308 | return -EINTR; | |
16b304f3 | 2309 | |
29fc657e JD |
2310 | error_put: |
2311 | module_put(control->module); | |
16b304f3 PS |
2312 | error_unlock: |
2313 | sock_put(sk); | |
2314 | mutex_unlock(nlk->cb_mutex); | |
2315 | error_free: | |
2316 | kfree_skb(skb); | |
2317 | return ret; | |
1da177e4 | 2318 | } |
6dc878a8 | 2319 | EXPORT_SYMBOL(__netlink_dump_start); |
1da177e4 | 2320 | |
2d4bc933 JB |
2321 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err, |
2322 | const struct netlink_ext_ack *extack) | |
1da177e4 LT |
2323 | { |
2324 | struct sk_buff *skb; | |
2325 | struct nlmsghdr *rep; | |
2326 | struct nlmsgerr *errmsg; | |
339bf98f | 2327 | size_t payload = sizeof(*errmsg); |
2d4bc933 | 2328 | size_t tlvlen = 0; |
0a6a3a23 | 2329 | struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk); |
2d4bc933 | 2330 | unsigned int flags = 0; |
48044eb4 | 2331 | bool nlk_has_extack = nlk->flags & NETLINK_F_EXT_ACK; |
1da177e4 | 2332 | |
0a6a3a23 | 2333 | /* Error messages get the original request appened, unless the user |
2d4bc933 JB |
2334 | * requests to cap the error message, and get extra error data if |
2335 | * requested. | |
0a6a3a23 | 2336 | */ |
4f6265d4 DA |
2337 | if (nlk_has_extack && extack && extack->_msg) |
2338 | tlvlen += nla_total_size(strlen(extack->_msg) + 1); | |
2339 | ||
2d4bc933 JB |
2340 | if (err) { |
2341 | if (!(nlk->flags & NETLINK_F_CAP_ACK)) | |
2342 | payload += nlmsg_len(nlh); | |
2343 | else | |
2344 | flags |= NLM_F_CAPPED; | |
4f6265d4 DA |
2345 | if (nlk_has_extack && extack && extack->bad_attr) |
2346 | tlvlen += nla_total_size(sizeof(u32)); | |
2d4bc933 JB |
2347 | } else { |
2348 | flags |= NLM_F_CAPPED; | |
ba0dc5f6 | 2349 | |
48044eb4 | 2350 | if (nlk_has_extack && extack && extack->cookie_len) |
ba0dc5f6 | 2351 | tlvlen += nla_total_size(extack->cookie_len); |
2d4bc933 | 2352 | } |
1da177e4 | 2353 | |
2d4bc933 JB |
2354 | if (tlvlen) |
2355 | flags |= NLM_F_ACK_TLVS; | |
2356 | ||
2357 | skb = nlmsg_new(payload + tlvlen, GFP_KERNEL); | |
1da177e4 | 2358 | if (!skb) { |
a2084f56 JB |
2359 | NETLINK_CB(in_skb).sk->sk_err = ENOBUFS; |
2360 | NETLINK_CB(in_skb).sk->sk_error_report(NETLINK_CB(in_skb).sk); | |
1da177e4 LT |
2361 | return; |
2362 | } | |
2363 | ||
15e47304 | 2364 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, |
2d4bc933 | 2365 | NLMSG_ERROR, payload, flags); |
bf8b79e4 | 2366 | errmsg = nlmsg_data(rep); |
1da177e4 | 2367 | errmsg->error = err; |
0a6a3a23 | 2368 | memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh)); |
2d4bc933 | 2369 | |
48044eb4 | 2370 | if (nlk_has_extack && extack) { |
4f6265d4 DA |
2371 | if (extack->_msg) { |
2372 | WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG, | |
2373 | extack->_msg)); | |
2374 | } | |
ba0dc5f6 | 2375 | if (err) { |
ba0dc5f6 JB |
2376 | if (extack->bad_attr && |
2377 | !WARN_ON((u8 *)extack->bad_attr < in_skb->data || | |
2378 | (u8 *)extack->bad_attr >= in_skb->data + | |
2379 | in_skb->len)) | |
2380 | WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, | |
2381 | (u8 *)extack->bad_attr - | |
2382 | in_skb->data)); | |
2383 | } else { | |
2384 | if (extack->cookie_len) | |
2385 | WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE, | |
2386 | extack->cookie_len, | |
2387 | extack->cookie)); | |
2388 | } | |
2d4bc933 JB |
2389 | } |
2390 | ||
2391 | nlmsg_end(skb, rep); | |
2392 | ||
15e47304 | 2393 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT); |
1da177e4 | 2394 | } |
6ac552fd | 2395 | EXPORT_SYMBOL(netlink_ack); |
1da177e4 | 2396 | |
cd40b7d3 | 2397 | int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, |
2d4bc933 JB |
2398 | struct nlmsghdr *, |
2399 | struct netlink_ext_ack *)) | |
82ace47a | 2400 | { |
cbbdf843 | 2401 | struct netlink_ext_ack extack; |
82ace47a TG |
2402 | struct nlmsghdr *nlh; |
2403 | int err; | |
2404 | ||
2405 | while (skb->len >= nlmsg_total_size(0)) { | |
cd40b7d3 DL |
2406 | int msglen; |
2407 | ||
cd443f1e | 2408 | memset(&extack, 0, sizeof(extack)); |
b529ccf2 | 2409 | nlh = nlmsg_hdr(skb); |
d35b6856 | 2410 | err = 0; |
82ace47a | 2411 | |
ad8e4b75 | 2412 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) |
82ace47a TG |
2413 | return 0; |
2414 | ||
d35b6856 TG |
2415 | /* Only requests are handled by the kernel */ |
2416 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) | |
5c58298c | 2417 | goto ack; |
45e7ae7f TG |
2418 | |
2419 | /* Skip control messages */ | |
2420 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) | |
5c58298c | 2421 | goto ack; |
d35b6856 | 2422 | |
2d4bc933 | 2423 | err = cb(skb, nlh, &extack); |
5c58298c DL |
2424 | if (err == -EINTR) |
2425 | goto skip; | |
2426 | ||
2427 | ack: | |
d35b6856 | 2428 | if (nlh->nlmsg_flags & NLM_F_ACK || err) |
2d4bc933 | 2429 | netlink_ack(skb, nlh, err, &extack); |
82ace47a | 2430 | |
5c58298c | 2431 | skip: |
6ac552fd | 2432 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
cd40b7d3 DL |
2433 | if (msglen > skb->len) |
2434 | msglen = skb->len; | |
2435 | skb_pull(skb, msglen); | |
82ace47a TG |
2436 | } |
2437 | ||
2438 | return 0; | |
2439 | } | |
6ac552fd | 2440 | EXPORT_SYMBOL(netlink_rcv_skb); |
82ace47a | 2441 | |
d387f6ad TG |
2442 | /** |
2443 | * nlmsg_notify - send a notification netlink message | |
2444 | * @sk: netlink socket to use | |
2445 | * @skb: notification message | |
15e47304 | 2446 | * @portid: destination netlink portid for reports or 0 |
d387f6ad TG |
2447 | * @group: destination multicast group or 0 |
2448 | * @report: 1 to report back, 0 to disable | |
2449 | * @flags: allocation flags | |
2450 | */ | |
15e47304 | 2451 | int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, |
d387f6ad TG |
2452 | unsigned int group, int report, gfp_t flags) |
2453 | { | |
2454 | int err = 0; | |
2455 | ||
2456 | if (group) { | |
15e47304 | 2457 | int exclude_portid = 0; |
d387f6ad TG |
2458 | |
2459 | if (report) { | |
63354797 | 2460 | refcount_inc(&skb->users); |
15e47304 | 2461 | exclude_portid = portid; |
d387f6ad TG |
2462 | } |
2463 | ||
1ce85fe4 PNA |
2464 | /* errors reported via destination sk->sk_err, but propagate |
2465 | * delivery errors if NETLINK_BROADCAST_ERROR flag is set */ | |
15e47304 | 2466 | err = nlmsg_multicast(sk, skb, exclude_portid, group, flags); |
d387f6ad TG |
2467 | } |
2468 | ||
1ce85fe4 PNA |
2469 | if (report) { |
2470 | int err2; | |
2471 | ||
15e47304 | 2472 | err2 = nlmsg_unicast(sk, skb, portid); |
1ce85fe4 PNA |
2473 | if (!err || err == -ESRCH) |
2474 | err = err2; | |
2475 | } | |
d387f6ad TG |
2476 | |
2477 | return err; | |
2478 | } | |
6ac552fd | 2479 | EXPORT_SYMBOL(nlmsg_notify); |
d387f6ad | 2480 | |
1da177e4 LT |
2481 | #ifdef CONFIG_PROC_FS |
2482 | struct nl_seq_iter { | |
e372c414 | 2483 | struct seq_net_private p; |
56d28b1e | 2484 | struct rhashtable_iter hti; |
1da177e4 | 2485 | int link; |
1da177e4 LT |
2486 | }; |
2487 | ||
56d28b1e | 2488 | static int netlink_walk_start(struct nl_seq_iter *iter) |
1da177e4 | 2489 | { |
56d28b1e | 2490 | int err; |
1da177e4 | 2491 | |
8f6fd83c BC |
2492 | err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti, |
2493 | GFP_KERNEL); | |
56d28b1e HX |
2494 | if (err) { |
2495 | iter->link = MAX_LINKS; | |
2496 | return err; | |
1da177e4 | 2497 | } |
56d28b1e HX |
2498 | |
2499 | err = rhashtable_walk_start(&iter->hti); | |
2500 | return err == -EAGAIN ? 0 : err; | |
1da177e4 LT |
2501 | } |
2502 | ||
56d28b1e | 2503 | static void netlink_walk_stop(struct nl_seq_iter *iter) |
1da177e4 | 2504 | { |
56d28b1e HX |
2505 | rhashtable_walk_stop(&iter->hti); |
2506 | rhashtable_walk_exit(&iter->hti); | |
1da177e4 LT |
2507 | } |
2508 | ||
56d28b1e | 2509 | static void *__netlink_seq_next(struct seq_file *seq) |
1da177e4 | 2510 | { |
56d28b1e | 2511 | struct nl_seq_iter *iter = seq->private; |
e341694e | 2512 | struct netlink_sock *nlk; |
1da177e4 | 2513 | |
56d28b1e HX |
2514 | do { |
2515 | for (;;) { | |
2516 | int err; | |
1da177e4 | 2517 | |
56d28b1e | 2518 | nlk = rhashtable_walk_next(&iter->hti); |
746fac4d | 2519 | |
56d28b1e HX |
2520 | if (IS_ERR(nlk)) { |
2521 | if (PTR_ERR(nlk) == -EAGAIN) | |
2522 | continue; | |
e341694e | 2523 | |
56d28b1e HX |
2524 | return nlk; |
2525 | } | |
1da177e4 | 2526 | |
56d28b1e HX |
2527 | if (nlk) |
2528 | break; | |
1da177e4 | 2529 | |
56d28b1e HX |
2530 | netlink_walk_stop(iter); |
2531 | if (++iter->link >= MAX_LINKS) | |
2532 | return NULL; | |
da12c90e | 2533 | |
56d28b1e HX |
2534 | err = netlink_walk_start(iter); |
2535 | if (err) | |
2536 | return ERR_PTR(err); | |
1da177e4 | 2537 | } |
56d28b1e | 2538 | } while (sock_net(&nlk->sk) != seq_file_net(seq)); |
1da177e4 | 2539 | |
56d28b1e HX |
2540 | return nlk; |
2541 | } | |
1da177e4 | 2542 | |
56d28b1e HX |
2543 | static void *netlink_seq_start(struct seq_file *seq, loff_t *posp) |
2544 | { | |
2545 | struct nl_seq_iter *iter = seq->private; | |
2546 | void *obj = SEQ_START_TOKEN; | |
2547 | loff_t pos; | |
2548 | int err; | |
2549 | ||
2550 | iter->link = 0; | |
2551 | ||
2552 | err = netlink_walk_start(iter); | |
2553 | if (err) | |
2554 | return ERR_PTR(err); | |
2555 | ||
2556 | for (pos = *posp; pos && obj && !IS_ERR(obj); pos--) | |
2557 | obj = __netlink_seq_next(seq); | |
2558 | ||
2559 | return obj; | |
2560 | } | |
2561 | ||
2562 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
2563 | { | |
2564 | ++*pos; | |
2565 | return __netlink_seq_next(seq); | |
1da177e4 LT |
2566 | } |
2567 | ||
2568 | static void netlink_seq_stop(struct seq_file *seq, void *v) | |
2569 | { | |
56d28b1e HX |
2570 | struct nl_seq_iter *iter = seq->private; |
2571 | ||
2572 | if (iter->link >= MAX_LINKS) | |
2573 | return; | |
2574 | ||
2575 | netlink_walk_stop(iter); | |
1da177e4 LT |
2576 | } |
2577 | ||
2578 | ||
2579 | static int netlink_seq_show(struct seq_file *seq, void *v) | |
2580 | { | |
658cb354 | 2581 | if (v == SEQ_START_TOKEN) { |
1da177e4 LT |
2582 | seq_puts(seq, |
2583 | "sk Eth Pid Groups " | |
cf0aa4e0 | 2584 | "Rmem Wmem Dump Locks Drops Inode\n"); |
658cb354 | 2585 | } else { |
1da177e4 LT |
2586 | struct sock *s = v; |
2587 | struct netlink_sock *nlk = nlk_sk(s); | |
2588 | ||
16b304f3 | 2589 | seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n", |
1da177e4 LT |
2590 | s, |
2591 | s->sk_protocol, | |
15e47304 | 2592 | nlk->portid, |
513c2500 | 2593 | nlk->groups ? (u32)nlk->groups[0] : 0, |
31e6d363 ED |
2594 | sk_rmem_alloc_get(s), |
2595 | sk_wmem_alloc_get(s), | |
16b304f3 | 2596 | nlk->cb_running, |
41c6d650 | 2597 | refcount_read(&s->sk_refcnt), |
cf0aa4e0 MY |
2598 | atomic_read(&s->sk_drops), |
2599 | sock_i_ino(s) | |
1da177e4 LT |
2600 | ); |
2601 | ||
2602 | } | |
2603 | return 0; | |
2604 | } | |
2605 | ||
56b3d975 | 2606 | static const struct seq_operations netlink_seq_ops = { |
1da177e4 LT |
2607 | .start = netlink_seq_start, |
2608 | .next = netlink_seq_next, | |
2609 | .stop = netlink_seq_stop, | |
2610 | .show = netlink_seq_show, | |
2611 | }; | |
2612 | ||
2613 | ||
2614 | static int netlink_seq_open(struct inode *inode, struct file *file) | |
2615 | { | |
e372c414 DL |
2616 | return seq_open_net(inode, file, &netlink_seq_ops, |
2617 | sizeof(struct nl_seq_iter)); | |
b4b51029 EB |
2618 | } |
2619 | ||
da7071d7 | 2620 | static const struct file_operations netlink_seq_fops = { |
1da177e4 LT |
2621 | .owner = THIS_MODULE, |
2622 | .open = netlink_seq_open, | |
2623 | .read = seq_read, | |
2624 | .llseek = seq_lseek, | |
e372c414 | 2625 | .release = seq_release_net, |
1da177e4 LT |
2626 | }; |
2627 | ||
2628 | #endif | |
2629 | ||
2630 | int netlink_register_notifier(struct notifier_block *nb) | |
2631 | { | |
efa172f4 | 2632 | return blocking_notifier_chain_register(&netlink_chain, nb); |
1da177e4 | 2633 | } |
6ac552fd | 2634 | EXPORT_SYMBOL(netlink_register_notifier); |
1da177e4 LT |
2635 | |
2636 | int netlink_unregister_notifier(struct notifier_block *nb) | |
2637 | { | |
efa172f4 | 2638 | return blocking_notifier_chain_unregister(&netlink_chain, nb); |
1da177e4 | 2639 | } |
6ac552fd | 2640 | EXPORT_SYMBOL(netlink_unregister_notifier); |
746fac4d | 2641 | |
90ddc4f0 | 2642 | static const struct proto_ops netlink_ops = { |
1da177e4 LT |
2643 | .family = PF_NETLINK, |
2644 | .owner = THIS_MODULE, | |
2645 | .release = netlink_release, | |
2646 | .bind = netlink_bind, | |
2647 | .connect = netlink_connect, | |
2648 | .socketpair = sock_no_socketpair, | |
2649 | .accept = sock_no_accept, | |
2650 | .getname = netlink_getname, | |
d1b4c689 | 2651 | .poll = datagram_poll, |
025c6818 | 2652 | .ioctl = netlink_ioctl, |
1da177e4 LT |
2653 | .listen = sock_no_listen, |
2654 | .shutdown = sock_no_shutdown, | |
9a4595bc PM |
2655 | .setsockopt = netlink_setsockopt, |
2656 | .getsockopt = netlink_getsockopt, | |
1da177e4 LT |
2657 | .sendmsg = netlink_sendmsg, |
2658 | .recvmsg = netlink_recvmsg, | |
d1b4c689 | 2659 | .mmap = sock_no_mmap, |
1da177e4 LT |
2660 | .sendpage = sock_no_sendpage, |
2661 | }; | |
2662 | ||
ec1b4cf7 | 2663 | static const struct net_proto_family netlink_family_ops = { |
1da177e4 LT |
2664 | .family = PF_NETLINK, |
2665 | .create = netlink_create, | |
2666 | .owner = THIS_MODULE, /* for consistency 8) */ | |
2667 | }; | |
2668 | ||
4665079c | 2669 | static int __net_init netlink_net_init(struct net *net) |
b4b51029 EB |
2670 | { |
2671 | #ifdef CONFIG_PROC_FS | |
d4beaa66 | 2672 | if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops)) |
b4b51029 EB |
2673 | return -ENOMEM; |
2674 | #endif | |
2675 | return 0; | |
2676 | } | |
2677 | ||
4665079c | 2678 | static void __net_exit netlink_net_exit(struct net *net) |
b4b51029 EB |
2679 | { |
2680 | #ifdef CONFIG_PROC_FS | |
ece31ffd | 2681 | remove_proc_entry("netlink", net->proc_net); |
b4b51029 EB |
2682 | #endif |
2683 | } | |
2684 | ||
b963ea89 DM |
2685 | static void __init netlink_add_usersock_entry(void) |
2686 | { | |
5c398dc8 | 2687 | struct listeners *listeners; |
b963ea89 DM |
2688 | int groups = 32; |
2689 | ||
5c398dc8 | 2690 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
b963ea89 | 2691 | if (!listeners) |
5c398dc8 | 2692 | panic("netlink_add_usersock_entry: Cannot allocate listeners\n"); |
b963ea89 DM |
2693 | |
2694 | netlink_table_grab(); | |
2695 | ||
2696 | nl_table[NETLINK_USERSOCK].groups = groups; | |
5c398dc8 | 2697 | rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners); |
b963ea89 DM |
2698 | nl_table[NETLINK_USERSOCK].module = THIS_MODULE; |
2699 | nl_table[NETLINK_USERSOCK].registered = 1; | |
9785e10a | 2700 | nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND; |
b963ea89 DM |
2701 | |
2702 | netlink_table_ungrab(); | |
2703 | } | |
2704 | ||
022cbae6 | 2705 | static struct pernet_operations __net_initdata netlink_net_ops = { |
b4b51029 EB |
2706 | .init = netlink_net_init, |
2707 | .exit = netlink_net_exit, | |
2708 | }; | |
2709 | ||
49f7b33e | 2710 | static inline u32 netlink_hash(const void *data, u32 len, u32 seed) |
c428ecd1 HX |
2711 | { |
2712 | const struct netlink_sock *nlk = data; | |
2713 | struct netlink_compare_arg arg; | |
2714 | ||
da314c99 | 2715 | netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid); |
11b58ba1 | 2716 | return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed); |
c428ecd1 HX |
2717 | } |
2718 | ||
2719 | static const struct rhashtable_params netlink_rhashtable_params = { | |
2720 | .head_offset = offsetof(struct netlink_sock, node), | |
2721 | .key_len = netlink_compare_arg_len, | |
c428ecd1 HX |
2722 | .obj_hashfn = netlink_hash, |
2723 | .obj_cmpfn = netlink_compare, | |
b5e2c150 | 2724 | .automatic_shrinking = true, |
c428ecd1 HX |
2725 | }; |
2726 | ||
1da177e4 LT |
2727 | static int __init netlink_proto_init(void) |
2728 | { | |
1da177e4 | 2729 | int i; |
1da177e4 LT |
2730 | int err = proto_register(&netlink_proto, 0); |
2731 | ||
2732 | if (err != 0) | |
2733 | goto out; | |
2734 | ||
fab25745 | 2735 | BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb)); |
1da177e4 | 2736 | |
0da974f4 | 2737 | nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL); |
fab2caf6 AM |
2738 | if (!nl_table) |
2739 | goto panic; | |
1da177e4 | 2740 | |
1da177e4 | 2741 | for (i = 0; i < MAX_LINKS; i++) { |
c428ecd1 HX |
2742 | if (rhashtable_init(&nl_table[i].hash, |
2743 | &netlink_rhashtable_params) < 0) { | |
e341694e TG |
2744 | while (--i > 0) |
2745 | rhashtable_destroy(&nl_table[i].hash); | |
1da177e4 | 2746 | kfree(nl_table); |
fab2caf6 | 2747 | goto panic; |
1da177e4 | 2748 | } |
1da177e4 LT |
2749 | } |
2750 | ||
bcbde0d4 DB |
2751 | INIT_LIST_HEAD(&netlink_tap_all); |
2752 | ||
b963ea89 DM |
2753 | netlink_add_usersock_entry(); |
2754 | ||
1da177e4 | 2755 | sock_register(&netlink_family_ops); |
b4b51029 | 2756 | register_pernet_subsys(&netlink_net_ops); |
746fac4d | 2757 | /* The netlink device handler may be needed early. */ |
1da177e4 LT |
2758 | rtnetlink_init(); |
2759 | out: | |
2760 | return err; | |
fab2caf6 AM |
2761 | panic: |
2762 | panic("netlink_init: Cannot allocate nl_table\n"); | |
1da177e4 LT |
2763 | } |
2764 | ||
1da177e4 | 2765 | core_initcall(netlink_proto_init); |