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