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