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