00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <linux/module.h>
00025
00026 #include <linux/capability.h>
00027 #include <linux/kernel.h>
00028 #include <linux/init.h>
00029 #include <linux/signal.h>
00030 #include <linux/sched.h>
00031 #include <linux/errno.h>
00032 #include <linux/string.h>
00033 #include <linux/stat.h>
00034 #include <linux/socket.h>
00035 #include <linux/un.h>
00036 #include <linux/fcntl.h>
00037 #include <linux/termios.h>
00038 #include <linux/sockios.h>
00039 #include <linux/net.h>
00040 #include <linux/fs.h>
00041 #include <linux/slab.h>
00042 #include <asm/uaccess.h>
00043 #include <linux/skbuff.h>
00044 #include <linux/netdevice.h>
00045 #include <linux/rtnetlink.h>
00046 #include <linux/proc_fs.h>
00047 #include <linux/seq_file.h>
00048 #include <linux/notifier.h>
00049 #include <linux/security.h>
00050 #include <linux/jhash.h>
00051 #include <linux/jiffies.h>
00052 #include <linux/random.h>
00053 #include <linux/bitops.h>
00054 #include <linux/mm.h>
00055 #include <linux/types.h>
00056 #include <linux/audit.h>
00057 #include <linux/mutex.h>
00058
00059 #include <net/net_namespace.h>
00060 #include <net/sock.h>
00061 #include <net/scm.h>
00062 #include <net/netlink.h>
00063
00064 #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
00065 #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
00066
00067 struct netlink_sock {
00068
00069 struct sock sk;
00070 u32 pid;
00071 u32 dst_pid;
00072 u32 dst_group;
00073 u32 flags;
00074 u32 subscriptions;
00075 u32 ngroups;
00076 unsigned long *groups;
00077 unsigned long state;
00078 wait_queue_head_t wait;
00079 struct netlink_callback *cb;
00080 struct mutex *cb_mutex;
00081 struct mutex cb_def_mutex;
00082 void (*netlink_rcv)(struct sk_buff *skb);
00083 struct module *module;
00084 };
00085
00086 #define NETLINK_KERNEL_SOCKET 0x1
00087 #define NETLINK_RECV_PKTINFO 0x2
00088
00089 static inline struct netlink_sock *nlk_sk(struct sock *sk)
00090 {
00091 return container_of(sk, struct netlink_sock, sk);
00092 }
00093
00094 static inline int netlink_is_kernel(struct sock *sk)
00095 {
00096 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
00097 }
00098
00099 struct nl_pid_hash {
00100 struct hlist_head *table;
00101 unsigned long rehash_time;
00102
00103 unsigned int mask;
00104 unsigned int shift;
00105
00106 unsigned int entries;
00107 unsigned int max_shift;
00108
00109 u32 rnd;
00110 };
00111
00112 struct netlink_table {
00113 struct nl_pid_hash hash;
00114 struct hlist_head mc_list;
00115 unsigned long *listeners;
00116 unsigned int nl_nonroot;
00117 unsigned int groups;
00118 struct mutex *cb_mutex;
00119 struct module *module;
00120 int registered;
00121 };
00122
00123 static struct netlink_table *nl_table;
00124
00125 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
00126
00127 static int netlink_dump(struct sock *sk);
00128 static void netlink_destroy_callback(struct netlink_callback *cb);
00129
00130 static DEFINE_RWLOCK(nl_table_lock);
00131 static atomic_t nl_table_users = ATOMIC_INIT(0);
00132
00133 static ATOMIC_NOTIFIER_HEAD(netlink_chain);
00134
00135 static u32 netlink_group_mask(u32 group)
00136 {
00137 return group ? 1 << (group - 1) : 0;
00138 }
00139
00140 static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
00141 {
00142 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
00143 }
00144
00145 static void netlink_sock_destruct(struct sock *sk)
00146 {
00147 struct netlink_sock *nlk = nlk_sk(sk);
00148
00149 if (nlk->cb) {
00150 if (nlk->cb->done)
00151 nlk->cb->done(nlk->cb);
00152 netlink_destroy_callback(nlk->cb);
00153 }
00154
00155 skb_queue_purge(&sk->sk_receive_queue);
00156
00157 if (!sock_flag(sk, SOCK_DEAD)) {
00158 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
00159 return;
00160 }
00161
00162 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
00163 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
00164 WARN_ON(nlk_sk(sk)->groups);
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 static void netlink_table_grab(void)
00174 __acquires(nl_table_lock)
00175 {
00176 write_lock_irq(&nl_table_lock);
00177
00178 if (atomic_read(&nl_table_users)) {
00179 DECLARE_WAITQUEUE(wait, current);
00180
00181 add_wait_queue_exclusive(&nl_table_wait, &wait);
00182 for (;;) {
00183 set_current_state(TASK_UNINTERRUPTIBLE);
00184 if (atomic_read(&nl_table_users) == 0)
00185 break;
00186 write_unlock_irq(&nl_table_lock);
00187 schedule();
00188 write_lock_irq(&nl_table_lock);
00189 }
00190
00191 __set_current_state(TASK_RUNNING);
00192 remove_wait_queue(&nl_table_wait, &wait);
00193 }
00194 }
00195
00196 static void netlink_table_ungrab(void)
00197 __releases(nl_table_lock)
00198 {
00199 write_unlock_irq(&nl_table_lock);
00200 wake_up(&nl_table_wait);
00201 }
00202
00203 static inline void
00204 netlink_lock_table(void)
00205 {
00206
00207
00208 read_lock(&nl_table_lock);
00209 atomic_inc(&nl_table_users);
00210 read_unlock(&nl_table_lock);
00211 }
00212
00213 static inline void
00214 netlink_unlock_table(void)
00215 {
00216 if (atomic_dec_and_test(&nl_table_users))
00217 wake_up(&nl_table_wait);
00218 }
00219
00220 static inline struct sock *netlink_lookup(struct net *net, int protocol,
00221 u32 pid)
00222 {
00223 struct nl_pid_hash *hash = &nl_table[protocol].hash;
00224 struct hlist_head *head;
00225 struct sock *sk;
00226 struct hlist_node *node;
00227
00228 read_lock(&nl_table_lock);
00229 head = nl_pid_hashfn(hash, pid);
00230 sk_for_each(sk, node, head) {
00231 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
00232 sock_hold(sk);
00233 goto found;
00234 }
00235 }
00236 sk = NULL;
00237 found:
00238 read_unlock(&nl_table_lock);
00239 return sk;
00240 }
00241
00242 static inline struct hlist_head *nl_pid_hash_zalloc(size_t size)
00243 {
00244 if (size <= PAGE_SIZE)
00245 return kzalloc(size, GFP_ATOMIC);
00246 else
00247 return (struct hlist_head *)
00248 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
00249 get_order(size));
00250 }
00251
00252 static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
00253 {
00254 if (size <= PAGE_SIZE)
00255 kfree(table);
00256 else
00257 free_pages((unsigned long)table, get_order(size));
00258 }
00259
00260 static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
00261 {
00262 unsigned int omask, mask, shift;
00263 size_t osize, size;
00264 struct hlist_head *otable, *table;
00265 int i;
00266
00267 omask = mask = hash->mask;
00268 osize = size = (mask + 1) * sizeof(*table);
00269 shift = hash->shift;
00270
00271 if (grow) {
00272 if (++shift > hash->max_shift)
00273 return 0;
00274 mask = mask * 2 + 1;
00275 size *= 2;
00276 }
00277
00278 table = nl_pid_hash_zalloc(size);
00279 if (!table)
00280 return 0;
00281
00282 otable = hash->table;
00283 hash->table = table;
00284 hash->mask = mask;
00285 hash->shift = shift;
00286 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
00287
00288 for (i = 0; i <= omask; i++) {
00289 struct sock *sk;
00290 struct hlist_node *node, *tmp;
00291
00292 sk_for_each_safe(sk, node, tmp, &otable[i])
00293 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
00294 }
00295
00296 nl_pid_hash_free(otable, osize);
00297 hash->rehash_time = jiffies + 10 * 60 * HZ;
00298 return 1;
00299 }
00300
00301 static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
00302 {
00303 int avg = hash->entries >> hash->shift;
00304
00305 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
00306 return 1;
00307
00308 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
00309 nl_pid_hash_rehash(hash, 0);
00310 return 1;
00311 }
00312
00313 return 0;
00314 }
00315
00316 static const struct proto_ops netlink_ops;
00317
00318 static void
00319 netlink_update_listeners(struct sock *sk)
00320 {
00321 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
00322 struct hlist_node *node;
00323 unsigned long mask;
00324 unsigned int i;
00325
00326 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
00327 mask = 0;
00328 sk_for_each_bound(sk, node, &tbl->mc_list) {
00329 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
00330 mask |= nlk_sk(sk)->groups[i];
00331 }
00332 tbl->listeners[i] = mask;
00333 }
00334
00335
00336 }
00337
00338 static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
00339 {
00340 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
00341 struct hlist_head *head;
00342 int err = -EADDRINUSE;
00343 struct sock *osk;
00344 struct hlist_node *node;
00345 int len;
00346
00347 netlink_table_grab();
00348 head = nl_pid_hashfn(hash, pid);
00349 len = 0;
00350 sk_for_each(osk, node, head) {
00351 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
00352 break;
00353 len++;
00354 }
00355 if (node)
00356 goto err;
00357
00358 err = -EBUSY;
00359 if (nlk_sk(sk)->pid)
00360 goto err;
00361
00362 err = -ENOMEM;
00363 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
00364 goto err;
00365
00366 if (len && nl_pid_hash_dilute(hash, len))
00367 head = nl_pid_hashfn(hash, pid);
00368 hash->entries++;
00369 nlk_sk(sk)->pid = pid;
00370 sk_add_node(sk, head);
00371 err = 0;
00372
00373 err:
00374 netlink_table_ungrab();
00375 return err;
00376 }
00377
00378 static void netlink_remove(struct sock *sk)
00379 {
00380 netlink_table_grab();
00381 if (sk_del_node_init(sk))
00382 nl_table[sk->sk_protocol].hash.entries--;
00383 if (nlk_sk(sk)->subscriptions)
00384 __sk_del_bind_node(sk);
00385 netlink_table_ungrab();
00386 }
00387
00388 static struct proto netlink_proto = {
00389 .name = "NETLINK",
00390 .owner = THIS_MODULE,
00391 .obj_size = sizeof(struct netlink_sock),
00392 };
00393
00394 static int __netlink_create(struct net *net, struct socket *sock,
00395 struct mutex *cb_mutex, int protocol)
00396 {
00397 struct sock *sk;
00398 struct netlink_sock *nlk;
00399
00400 sock->ops = &netlink_ops;
00401
00402 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
00403 if (!sk)
00404 return -ENOMEM;
00405
00406 sock_init_data(sock, sk);
00407
00408 nlk = nlk_sk(sk);
00409 if (cb_mutex)
00410 nlk->cb_mutex = cb_mutex;
00411 else {
00412 nlk->cb_mutex = &nlk->cb_def_mutex;
00413 mutex_init(nlk->cb_mutex);
00414 }
00415 init_waitqueue_head(&nlk->wait);
00416
00417 sk->sk_destruct = netlink_sock_destruct;
00418 sk->sk_protocol = protocol;
00419 return 0;
00420 }
00421
00422 static int netlink_create(struct net *net, struct socket *sock, int protocol)
00423 {
00424 struct module *module = NULL;
00425 struct mutex *cb_mutex;
00426 struct netlink_sock *nlk;
00427 int err = 0;
00428
00429 sock->state = SS_UNCONNECTED;
00430
00431 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
00432 return -ESOCKTNOSUPPORT;
00433
00434 if (protocol < 0 || protocol >= MAX_LINKS)
00435 return -EPROTONOSUPPORT;
00436
00437 netlink_lock_table();
00438 #ifdef CONFIG_MODULES
00439 if (!nl_table[protocol].registered) {
00440 netlink_unlock_table();
00441 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
00442 netlink_lock_table();
00443 }
00444 #endif
00445 if (nl_table[protocol].registered &&
00446 try_module_get(nl_table[protocol].module))
00447 module = nl_table[protocol].module;
00448 cb_mutex = nl_table[protocol].cb_mutex;
00449 netlink_unlock_table();
00450
00451 err = __netlink_create(net, sock, cb_mutex, protocol);
00452 if (err < 0)
00453 goto out_module;
00454
00455 local_bh_disable();
00456 sock_prot_inuse_add(net, &netlink_proto, 1);
00457 local_bh_enable();
00458
00459 nlk = nlk_sk(sock->sk);
00460 nlk->module = module;
00461 out:
00462 return err;
00463
00464 out_module:
00465 module_put(module);
00466 goto out;
00467 }
00468
00469 static int netlink_release(struct socket *sock)
00470 {
00471 struct sock *sk = sock->sk;
00472 struct netlink_sock *nlk;
00473
00474 if (!sk)
00475 return 0;
00476
00477 netlink_remove(sk);
00478 sock_orphan(sk);
00479 nlk = nlk_sk(sk);
00480
00481
00482
00483
00484
00485
00486 sock->sk = NULL;
00487 wake_up_interruptible_all(&nlk->wait);
00488
00489 skb_queue_purge(&sk->sk_write_queue);
00490
00491 if (nlk->pid && !nlk->subscriptions) {
00492 struct netlink_notify n = {
00493 .net = sock_net(sk),
00494 .protocol = sk->sk_protocol,
00495 .pid = nlk->pid,
00496 };
00497 atomic_notifier_call_chain(&netlink_chain,
00498 NETLINK_URELEASE, &n);
00499 }
00500
00501 module_put(nlk->module);
00502
00503 netlink_table_grab();
00504 if (netlink_is_kernel(sk)) {
00505 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
00506 if (--nl_table[sk->sk_protocol].registered == 0) {
00507 kfree(nl_table[sk->sk_protocol].listeners);
00508 nl_table[sk->sk_protocol].module = NULL;
00509 nl_table[sk->sk_protocol].registered = 0;
00510 }
00511 } else if (nlk->subscriptions)
00512 netlink_update_listeners(sk);
00513 netlink_table_ungrab();
00514
00515 kfree(nlk->groups);
00516 nlk->groups = NULL;
00517
00518 local_bh_disable();
00519 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
00520 local_bh_enable();
00521 sock_put(sk);
00522 return 0;
00523 }
00524
00525 static int netlink_autobind(struct socket *sock)
00526 {
00527 struct sock *sk = sock->sk;
00528 struct net *net = sock_net(sk);
00529 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
00530 struct hlist_head *head;
00531 struct sock *osk;
00532 struct hlist_node *node;
00533 s32 pid = current->tgid;
00534 int err;
00535 static s32 rover = -4097;
00536
00537 retry:
00538 cond_resched();
00539 netlink_table_grab();
00540 head = nl_pid_hashfn(hash, pid);
00541 sk_for_each(osk, node, head) {
00542 if (!net_eq(sock_net(osk), net))
00543 continue;
00544 if (nlk_sk(osk)->pid == pid) {
00545
00546 pid = rover--;
00547 if (rover > -4097)
00548 rover = -4097;
00549 netlink_table_ungrab();
00550 goto retry;
00551 }
00552 }
00553 netlink_table_ungrab();
00554
00555 err = netlink_insert(sk, net, pid);
00556 if (err == -EADDRINUSE)
00557 goto retry;
00558
00559
00560 if (err == -EBUSY)
00561 err = 0;
00562
00563 return err;
00564 }
00565
00566 static inline int netlink_capable(struct socket *sock, unsigned int flag)
00567 {
00568 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
00569 capable(CAP_NET_ADMIN);
00570 }
00571
00572 static void
00573 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
00574 {
00575 struct netlink_sock *nlk = nlk_sk(sk);
00576
00577 if (nlk->subscriptions && !subscriptions)
00578 __sk_del_bind_node(sk);
00579 else if (!nlk->subscriptions && subscriptions)
00580 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
00581 nlk->subscriptions = subscriptions;
00582 }
00583
00584 static int netlink_realloc_groups(struct sock *sk)
00585 {
00586 struct netlink_sock *nlk = nlk_sk(sk);
00587 unsigned int groups;
00588 unsigned long *new_groups;
00589 int err = 0;
00590
00591 netlink_table_grab();
00592
00593 groups = nl_table[sk->sk_protocol].groups;
00594 if (!nl_table[sk->sk_protocol].registered) {
00595 err = -ENOENT;
00596 goto out_unlock;
00597 }
00598
00599 if (nlk->ngroups >= groups)
00600 goto out_unlock;
00601
00602 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
00603 if (new_groups == NULL) {
00604 err = -ENOMEM;
00605 goto out_unlock;
00606 }
00607 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
00608 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
00609
00610 nlk->groups = new_groups;
00611 nlk->ngroups = groups;
00612 out_unlock:
00613 netlink_table_ungrab();
00614 return err;
00615 }
00616
00617 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
00618 int addr_len)
00619 {
00620 struct sock *sk = sock->sk;
00621 struct net *net = sock_net(sk);
00622 struct netlink_sock *nlk = nlk_sk(sk);
00623 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
00624 int err;
00625
00626 if (nladdr->nl_family != AF_NETLINK)
00627 return -EINVAL;
00628
00629
00630 if (nladdr->nl_groups) {
00631 if (!netlink_capable(sock, NL_NONROOT_RECV))
00632 return -EPERM;
00633 err = netlink_realloc_groups(sk);
00634 if (err)
00635 return err;
00636 }
00637
00638 if (nlk->pid) {
00639 if (nladdr->nl_pid != nlk->pid)
00640 return -EINVAL;
00641 } else {
00642 err = nladdr->nl_pid ?
00643 netlink_insert(sk, net, nladdr->nl_pid) :
00644 netlink_autobind(sock);
00645 if (err)
00646 return err;
00647 }
00648
00649 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
00650 return 0;
00651
00652 netlink_table_grab();
00653 netlink_update_subscriptions(sk, nlk->subscriptions +
00654 hweight32(nladdr->nl_groups) -
00655 hweight32(nlk->groups[0]));
00656 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
00657 netlink_update_listeners(sk);
00658 netlink_table_ungrab();
00659
00660 return 0;
00661 }
00662
00663 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
00664 int alen, int flags)
00665 {
00666 int err = 0;
00667 struct sock *sk = sock->sk;
00668 struct netlink_sock *nlk = nlk_sk(sk);
00669 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
00670
00671 if (addr->sa_family == AF_UNSPEC) {
00672 sk->sk_state = NETLINK_UNCONNECTED;
00673 nlk->dst_pid = 0;
00674 nlk->dst_group = 0;
00675 return 0;
00676 }
00677 if (addr->sa_family != AF_NETLINK)
00678 return -EINVAL;
00679
00680
00681 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
00682 return -EPERM;
00683
00684 if (!nlk->pid)
00685 err = netlink_autobind(sock);
00686
00687 if (err == 0) {
00688 sk->sk_state = NETLINK_CONNECTED;
00689 nlk->dst_pid = nladdr->nl_pid;
00690 nlk->dst_group = ffs(nladdr->nl_groups);
00691 }
00692
00693 return err;
00694 }
00695
00696 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
00697 int *addr_len, int peer)
00698 {
00699 struct sock *sk = sock->sk;
00700 struct netlink_sock *nlk = nlk_sk(sk);
00701 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
00702
00703 nladdr->nl_family = AF_NETLINK;
00704 nladdr->nl_pad = 0;
00705 *addr_len = sizeof(*nladdr);
00706
00707 if (peer) {
00708 nladdr->nl_pid = nlk->dst_pid;
00709 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
00710 } else {
00711 nladdr->nl_pid = nlk->pid;
00712 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
00713 }
00714 return 0;
00715 }
00716
00717 static void netlink_overrun(struct sock *sk)
00718 {
00719 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
00720 sk->sk_err = ENOBUFS;
00721 sk->sk_error_report(sk);
00722 }
00723 }
00724
00725 static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
00726 {
00727 struct sock *sock;
00728 struct netlink_sock *nlk;
00729
00730 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
00731 if (!sock)
00732 return ERR_PTR(-ECONNREFUSED);
00733
00734
00735 nlk = nlk_sk(sock);
00736 if (sock->sk_state == NETLINK_CONNECTED &&
00737 nlk->dst_pid != nlk_sk(ssk)->pid) {
00738 sock_put(sock);
00739 return ERR_PTR(-ECONNREFUSED);
00740 }
00741 return sock;
00742 }
00743
00744 struct sock *netlink_getsockbyfilp(struct file *filp)
00745 {
00746 struct inode *inode = filp->f_path.dentry->d_inode;
00747 struct sock *sock;
00748
00749 if (!S_ISSOCK(inode->i_mode))
00750 return ERR_PTR(-ENOTSOCK);
00751
00752 sock = SOCKET_I(inode)->sk;
00753 if (sock->sk_family != AF_NETLINK)
00754 return ERR_PTR(-EINVAL);
00755
00756 sock_hold(sock);
00757 return sock;
00758 }
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
00771 long *timeo, struct sock *ssk)
00772 {
00773 struct netlink_sock *nlk;
00774
00775 nlk = nlk_sk(sk);
00776
00777 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
00778 test_bit(0, &nlk->state)) {
00779 DECLARE_WAITQUEUE(wait, current);
00780 if (!*timeo) {
00781 if (!ssk || netlink_is_kernel(ssk))
00782 netlink_overrun(sk);
00783 sock_put(sk);
00784 kfree_skb(skb);
00785 return -EAGAIN;
00786 }
00787
00788 __set_current_state(TASK_INTERRUPTIBLE);
00789 add_wait_queue(&nlk->wait, &wait);
00790
00791 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
00792 test_bit(0, &nlk->state)) &&
00793 !sock_flag(sk, SOCK_DEAD))
00794 *timeo = schedule_timeout(*timeo);
00795
00796 __set_current_state(TASK_RUNNING);
00797 remove_wait_queue(&nlk->wait, &wait);
00798 sock_put(sk);
00799
00800 if (signal_pending(current)) {
00801 kfree_skb(skb);
00802 return sock_intr_errno(*timeo);
00803 }
00804 return 1;
00805 }
00806 skb_set_owner_r(skb, sk);
00807 return 0;
00808 }
00809
00810 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
00811 {
00812 int len = skb->len;
00813
00814 skb_queue_tail(&sk->sk_receive_queue, skb);
00815 sk->sk_data_ready(sk, len);
00816 sock_put(sk);
00817 return len;
00818 }
00819
00820 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
00821 {
00822 kfree_skb(skb);
00823 sock_put(sk);
00824 }
00825
00826 static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
00827 gfp_t allocation)
00828 {
00829 int delta;
00830
00831 skb_orphan(skb);
00832
00833 delta = skb->end - skb->tail;
00834 if (delta * 2 < skb->truesize)
00835 return skb;
00836
00837 if (skb_shared(skb)) {
00838 struct sk_buff *nskb = skb_clone(skb, allocation);
00839 if (!nskb)
00840 return skb;
00841 kfree_skb(skb);
00842 skb = nskb;
00843 }
00844
00845 if (!pskb_expand_head(skb, 0, -delta, allocation))
00846 skb->truesize -= delta;
00847
00848 return skb;
00849 }
00850
00851 static inline void netlink_rcv_wake(struct sock *sk)
00852 {
00853 struct netlink_sock *nlk = nlk_sk(sk);
00854
00855 if (skb_queue_empty(&sk->sk_receive_queue))
00856 clear_bit(0, &nlk->state);
00857 if (!test_bit(0, &nlk->state))
00858 wake_up_interruptible(&nlk->wait);
00859 }
00860
00861 static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
00862 {
00863 int ret;
00864 struct netlink_sock *nlk = nlk_sk(sk);
00865
00866 ret = -ECONNREFUSED;
00867 if (nlk->netlink_rcv != NULL) {
00868 ret = skb->len;
00869 skb_set_owner_r(skb, sk);
00870 nlk->netlink_rcv(skb);
00871 }
00872 kfree_skb(skb);
00873 sock_put(sk);
00874 return ret;
00875 }
00876
00877 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
00878 u32 pid, int nonblock)
00879 {
00880 struct sock *sk;
00881 int err;
00882 long timeo;
00883
00884 skb = netlink_trim(skb, gfp_any());
00885
00886 timeo = sock_sndtimeo(ssk, nonblock);
00887 retry:
00888 sk = netlink_getsockbypid(ssk, pid);
00889 if (IS_ERR(sk)) {
00890 kfree_skb(skb);
00891 return PTR_ERR(sk);
00892 }
00893 if (netlink_is_kernel(sk))
00894 return netlink_unicast_kernel(sk, skb);
00895
00896 if (sk_filter(sk, skb)) {
00897 err = skb->len;
00898 kfree_skb(skb);
00899 sock_put(sk);
00900 return err;
00901 }
00902
00903 err = netlink_attachskb(sk, skb, &timeo, ssk);
00904 if (err == 1)
00905 goto retry;
00906 if (err)
00907 return err;
00908
00909 return netlink_sendskb(sk, skb);
00910 }
00911 EXPORT_SYMBOL(netlink_unicast);
00912
00913 int netlink_has_listeners(struct sock *sk, unsigned int group)
00914 {
00915 int res = 0;
00916 unsigned long *listeners;
00917
00918 BUG_ON(!netlink_is_kernel(sk));
00919
00920 rcu_read_lock();
00921 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
00922
00923 if (group - 1 < nl_table[sk->sk_protocol].groups)
00924 res = test_bit(group - 1, listeners);
00925
00926 rcu_read_unlock();
00927
00928 return res;
00929 }
00930 EXPORT_SYMBOL_GPL(netlink_has_listeners);
00931
00932 static inline int netlink_broadcast_deliver(struct sock *sk,
00933 struct sk_buff *skb)
00934 {
00935 struct netlink_sock *nlk = nlk_sk(sk);
00936
00937 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
00938 !test_bit(0, &nlk->state)) {
00939 skb_set_owner_r(skb, sk);
00940 skb_queue_tail(&sk->sk_receive_queue, skb);
00941 sk->sk_data_ready(sk, skb->len);
00942 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
00943 }
00944 return -1;
00945 }
00946
00947 struct netlink_broadcast_data {
00948 struct sock *exclude_sk;
00949 struct net *net;
00950 u32 pid;
00951 u32 group;
00952 int failure;
00953 int congested;
00954 int delivered;
00955 gfp_t allocation;
00956 struct sk_buff *skb, *skb2;
00957 };
00958
00959 static inline int do_one_broadcast(struct sock *sk,
00960 struct netlink_broadcast_data *p)
00961 {
00962 struct netlink_sock *nlk = nlk_sk(sk);
00963 int val;
00964
00965 if (p->exclude_sk == sk)
00966 goto out;
00967
00968 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
00969 !test_bit(p->group - 1, nlk->groups))
00970 goto out;
00971
00972 if (!net_eq(sock_net(sk), p->net))
00973 goto out;
00974
00975 if (p->failure) {
00976 netlink_overrun(sk);
00977 goto out;
00978 }
00979
00980 sock_hold(sk);
00981 if (p->skb2 == NULL) {
00982 if (skb_shared(p->skb)) {
00983 p->skb2 = skb_clone(p->skb, p->allocation);
00984 } else {
00985 p->skb2 = skb_get(p->skb);
00986
00987
00988
00989
00990 skb_orphan(p->skb2);
00991 }
00992 }
00993 if (p->skb2 == NULL) {
00994 netlink_overrun(sk);
00995
00996 p->failure = 1;
00997 } else if (sk_filter(sk, p->skb2)) {
00998 kfree_skb(p->skb2);
00999 p->skb2 = NULL;
01000 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
01001 netlink_overrun(sk);
01002 } else {
01003 p->congested |= val;
01004 p->delivered = 1;
01005 p->skb2 = NULL;
01006 }
01007 sock_put(sk);
01008
01009 out:
01010 return 0;
01011 }
01012
01013 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
01014 u32 group, gfp_t allocation)
01015 {
01016 struct net *net = sock_net(ssk);
01017 struct netlink_broadcast_data info;
01018 struct hlist_node *node;
01019 struct sock *sk;
01020
01021 skb = netlink_trim(skb, allocation);
01022
01023 info.exclude_sk = ssk;
01024 info.net = net;
01025 info.pid = pid;
01026 info.group = group;
01027 info.failure = 0;
01028 info.congested = 0;
01029 info.delivered = 0;
01030 info.allocation = allocation;
01031 info.skb = skb;
01032 info.skb2 = NULL;
01033
01034
01035
01036 netlink_lock_table();
01037
01038 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
01039 do_one_broadcast(sk, &info);
01040
01041 kfree_skb(skb);
01042
01043 netlink_unlock_table();
01044
01045 if (info.skb2)
01046 kfree_skb(info.skb2);
01047
01048 if (info.delivered) {
01049 if (info.congested && (allocation & __GFP_WAIT))
01050 yield();
01051 return 0;
01052 }
01053 if (info.failure)
01054 return -ENOBUFS;
01055 return -ESRCH;
01056 }
01057 EXPORT_SYMBOL(netlink_broadcast);
01058
01059 struct netlink_set_err_data {
01060 struct sock *exclude_sk;
01061 u32 pid;
01062 u32 group;
01063 int code;
01064 };
01065
01066 static inline int do_one_set_err(struct sock *sk,
01067 struct netlink_set_err_data *p)
01068 {
01069 struct netlink_sock *nlk = nlk_sk(sk);
01070
01071 if (sk == p->exclude_sk)
01072 goto out;
01073
01074 if (sock_net(sk) != sock_net(p->exclude_sk))
01075 goto out;
01076
01077 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
01078 !test_bit(p->group - 1, nlk->groups))
01079 goto out;
01080
01081 sk->sk_err = p->code;
01082 sk->sk_error_report(sk);
01083 out:
01084 return 0;
01085 }
01086
01087
01088
01089
01090
01091
01092
01093
01094 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
01095 {
01096 struct netlink_set_err_data info;
01097 struct hlist_node *node;
01098 struct sock *sk;
01099
01100 info.exclude_sk = ssk;
01101 info.pid = pid;
01102 info.group = group;
01103
01104 info.code = -code;
01105
01106 read_lock(&nl_table_lock);
01107
01108 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
01109 do_one_set_err(sk, &info);
01110
01111 read_unlock(&nl_table_lock);
01112 }
01113
01114
01115 static void netlink_update_socket_mc(struct netlink_sock *nlk,
01116 unsigned int group,
01117 int is_new)
01118 {
01119 int old, new = !!is_new, subscriptions;
01120
01121 old = test_bit(group - 1, nlk->groups);
01122 subscriptions = nlk->subscriptions - old + new;
01123 if (new)
01124 __set_bit(group - 1, nlk->groups);
01125 else
01126 __clear_bit(group - 1, nlk->groups);
01127 netlink_update_subscriptions(&nlk->sk, subscriptions);
01128 netlink_update_listeners(&nlk->sk);
01129 }
01130
01131 static int netlink_setsockopt(struct socket *sock, int level, int optname,
01132 char __user *optval, int optlen)
01133 {
01134 struct sock *sk = sock->sk;
01135 struct netlink_sock *nlk = nlk_sk(sk);
01136 unsigned int val = 0;
01137 int err;
01138
01139 if (level != SOL_NETLINK)
01140 return -ENOPROTOOPT;
01141
01142 if (optlen >= sizeof(int) &&
01143 get_user(val, (unsigned int __user *)optval))
01144 return -EFAULT;
01145
01146 switch (optname) {
01147 case NETLINK_PKTINFO:
01148 if (val)
01149 nlk->flags |= NETLINK_RECV_PKTINFO;
01150 else
01151 nlk->flags &= ~NETLINK_RECV_PKTINFO;
01152 err = 0;
01153 break;
01154 case NETLINK_ADD_MEMBERSHIP:
01155 case NETLINK_DROP_MEMBERSHIP: {
01156 if (!netlink_capable(sock, NL_NONROOT_RECV))
01157 return -EPERM;
01158 err = netlink_realloc_groups(sk);
01159 if (err)
01160 return err;
01161 if (!val || val - 1 >= nlk->ngroups)
01162 return -EINVAL;
01163 netlink_table_grab();
01164 netlink_update_socket_mc(nlk, val,
01165 optname == NETLINK_ADD_MEMBERSHIP);
01166 netlink_table_ungrab();
01167 err = 0;
01168 break;
01169 }
01170 default:
01171 err = -ENOPROTOOPT;
01172 }
01173 return err;
01174 }
01175
01176 static int netlink_getsockopt(struct socket *sock, int level, int optname,
01177 char __user *optval, int __user *optlen)
01178 {
01179 struct sock *sk = sock->sk;
01180 struct netlink_sock *nlk = nlk_sk(sk);
01181 int len, val, err;
01182
01183 if (level != SOL_NETLINK)
01184 return -ENOPROTOOPT;
01185
01186 if (get_user(len, optlen))
01187 return -EFAULT;
01188 if (len < 0)
01189 return -EINVAL;
01190
01191 switch (optname) {
01192 case NETLINK_PKTINFO:
01193 if (len < sizeof(int))
01194 return -EINVAL;
01195 len = sizeof(int);
01196 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
01197 if (put_user(len, optlen) ||
01198 put_user(val, optval))
01199 return -EFAULT;
01200 err = 0;
01201 break;
01202 default:
01203 err = -ENOPROTOOPT;
01204 }
01205 return err;
01206 }
01207
01208 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
01209 {
01210 struct nl_pktinfo info;
01211
01212 info.group = NETLINK_CB(skb).dst_group;
01213 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
01214 }
01215
01216 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
01217 struct msghdr *msg, size_t len)
01218 {
01219 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
01220 struct sock *sk = sock->sk;
01221 struct netlink_sock *nlk = nlk_sk(sk);
01222 struct sockaddr_nl *addr = msg->msg_name;
01223 u32 dst_pid;
01224 u32 dst_group;
01225 struct sk_buff *skb;
01226 int err;
01227 struct scm_cookie scm;
01228
01229 if (msg->msg_flags&MSG_OOB)
01230 return -EOPNOTSUPP;
01231
01232 if (NULL == siocb->scm)
01233 siocb->scm = &scm;
01234 err = scm_send(sock, msg, siocb->scm);
01235 if (err < 0)
01236 return err;
01237
01238 if (msg->msg_namelen) {
01239 if (addr->nl_family != AF_NETLINK)
01240 return -EINVAL;
01241 dst_pid = addr->nl_pid;
01242 dst_group = ffs(addr->nl_groups);
01243 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
01244 return -EPERM;
01245 } else {
01246 dst_pid = nlk->dst_pid;
01247 dst_group = nlk->dst_group;
01248 }
01249
01250 if (!nlk->pid) {
01251 err = netlink_autobind(sock);
01252 if (err)
01253 goto out;
01254 }
01255
01256 err = -EMSGSIZE;
01257 if (len > sk->sk_sndbuf - 32)
01258 goto out;
01259 err = -ENOBUFS;
01260 skb = alloc_skb(len, GFP_KERNEL);
01261 if (skb == NULL)
01262 goto out;
01263
01264 NETLINK_CB(skb).pid = nlk->pid;
01265 NETLINK_CB(skb).dst_group = dst_group;
01266 NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
01267 NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
01268 security_task_getsecid(current, &(NETLINK_CB(skb).sid));
01269 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
01270
01271
01272
01273
01274
01275
01276
01277 err = -EFAULT;
01278 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
01279 kfree_skb(skb);
01280 goto out;
01281 }
01282
01283 err = security_netlink_send(sk, skb);
01284 if (err) {
01285 kfree_skb(skb);
01286 goto out;
01287 }
01288
01289 if (dst_group) {
01290 atomic_inc(&skb->users);
01291 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
01292 }
01293 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
01294
01295 out:
01296 return err;
01297 }
01298
01299 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
01300 struct msghdr *msg, size_t len,
01301 int flags)
01302 {
01303 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
01304 struct scm_cookie scm;
01305 struct sock *sk = sock->sk;
01306 struct netlink_sock *nlk = nlk_sk(sk);
01307 int noblock = flags&MSG_DONTWAIT;
01308 size_t copied;
01309 struct sk_buff *skb;
01310 int err;
01311
01312 if (flags&MSG_OOB)
01313 return -EOPNOTSUPP;
01314
01315 copied = 0;
01316
01317 skb = skb_recv_datagram(sk, flags, noblock, &err);
01318 if (skb == NULL)
01319 goto out;
01320
01321 msg->msg_namelen = 0;
01322
01323 copied = skb->len;
01324 if (len < copied) {
01325 msg->msg_flags |= MSG_TRUNC;
01326 copied = len;
01327 }
01328
01329 skb_reset_transport_header(skb);
01330 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
01331
01332 if (msg->msg_name) {
01333 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
01334 addr->nl_family = AF_NETLINK;
01335 addr->nl_pad = 0;
01336 addr->nl_pid = NETLINK_CB(skb).pid;
01337 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
01338 msg->msg_namelen = sizeof(*addr);
01339 }
01340
01341 if (nlk->flags & NETLINK_RECV_PKTINFO)
01342 netlink_cmsg_recv_pktinfo(msg, skb);
01343
01344 if (NULL == siocb->scm) {
01345 memset(&scm, 0, sizeof(scm));
01346 siocb->scm = &scm;
01347 }
01348 siocb->scm->creds = *NETLINK_CREDS(skb);
01349 if (flags & MSG_TRUNC)
01350 copied = skb->len;
01351 skb_free_datagram(sk, skb);
01352
01353 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
01354 netlink_dump(sk);
01355
01356 scm_recv(sock, msg, siocb->scm, flags);
01357 out:
01358 netlink_rcv_wake(sk);
01359 return err ? : copied;
01360 }
01361
01362 static void netlink_data_ready(struct sock *sk, int len)
01363 {
01364 BUG();
01365 }
01366
01367
01368
01369
01370
01371
01372
01373 struct sock *
01374 netlink_kernel_create(struct net *net, int unit, unsigned int groups,
01375 void (*input)(struct sk_buff *skb),
01376 struct mutex *cb_mutex, struct module *module)
01377 {
01378 struct socket *sock;
01379 struct sock *sk;
01380 struct netlink_sock *nlk;
01381 unsigned long *listeners = NULL;
01382
01383 BUG_ON(!nl_table);
01384
01385 if (unit < 0 || unit >= MAX_LINKS)
01386 return NULL;
01387
01388 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
01389 return NULL;
01390
01391
01392
01393
01394
01395
01396
01397 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
01398 goto out_sock_release_nosk;
01399
01400 sk = sock->sk;
01401 sk_change_net(sk, net);
01402
01403 if (groups < 32)
01404 groups = 32;
01405
01406 listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
01407 if (!listeners)
01408 goto out_sock_release;
01409
01410 sk->sk_data_ready = netlink_data_ready;
01411 if (input)
01412 nlk_sk(sk)->netlink_rcv = input;
01413
01414 if (netlink_insert(sk, net, 0))
01415 goto out_sock_release;
01416
01417 nlk = nlk_sk(sk);
01418 nlk->flags |= NETLINK_KERNEL_SOCKET;
01419
01420 netlink_table_grab();
01421 if (!nl_table[unit].registered) {
01422 nl_table[unit].groups = groups;
01423 nl_table[unit].listeners = listeners;
01424 nl_table[unit].cb_mutex = cb_mutex;
01425 nl_table[unit].module = module;
01426 nl_table[unit].registered = 1;
01427 } else {
01428 kfree(listeners);
01429 nl_table[unit].registered++;
01430 }
01431 netlink_table_ungrab();
01432 return sk;
01433
01434 out_sock_release:
01435 kfree(listeners);
01436 netlink_kernel_release(sk);
01437 return NULL;
01438
01439 out_sock_release_nosk:
01440 sock_release(sock);
01441 return NULL;
01442 }
01443 EXPORT_SYMBOL(netlink_kernel_create);
01444
01445
01446 void
01447 netlink_kernel_release(struct sock *sk)
01448 {
01449 sk_release_kernel(sk);
01450 }
01451 EXPORT_SYMBOL(netlink_kernel_release);
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
01467 {
01468 unsigned long *listeners, *old = NULL;
01469 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
01470 int err = 0;
01471
01472 if (groups < 32)
01473 groups = 32;
01474
01475 netlink_table_grab();
01476 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
01477 listeners = kzalloc(NLGRPSZ(groups), GFP_ATOMIC);
01478 if (!listeners) {
01479 err = -ENOMEM;
01480 goto out_ungrab;
01481 }
01482 old = tbl->listeners;
01483 memcpy(listeners, old, NLGRPSZ(tbl->groups));
01484 rcu_assign_pointer(tbl->listeners, listeners);
01485 }
01486 tbl->groups = groups;
01487
01488 out_ungrab:
01489 netlink_table_ungrab();
01490 synchronize_rcu();
01491 kfree(old);
01492 return err;
01493 }
01494 EXPORT_SYMBOL(netlink_change_ngroups);
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504 void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
01505 {
01506 struct sock *sk;
01507 struct hlist_node *node;
01508 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
01509
01510 netlink_table_grab();
01511
01512 sk_for_each_bound(sk, node, &tbl->mc_list)
01513 netlink_update_socket_mc(nlk_sk(sk), group, 0);
01514
01515 netlink_table_ungrab();
01516 }
01517 EXPORT_SYMBOL(netlink_clear_multicast_users);
01518
01519 void netlink_set_nonroot(int protocol, unsigned int flags)
01520 {
01521 if ((unsigned int)protocol < MAX_LINKS)
01522 nl_table[protocol].nl_nonroot = flags;
01523 }
01524 EXPORT_SYMBOL(netlink_set_nonroot);
01525
01526 static void netlink_destroy_callback(struct netlink_callback *cb)
01527 {
01528 if (cb->skb)
01529 kfree_skb(cb->skb);
01530 kfree(cb);
01531 }
01532
01533
01534
01535
01536
01537
01538 static int netlink_dump(struct sock *sk)
01539 {
01540 struct netlink_sock *nlk = nlk_sk(sk);
01541 struct netlink_callback *cb;
01542 struct sk_buff *skb;
01543 struct nlmsghdr *nlh;
01544 int len, err = -ENOBUFS;
01545
01546 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
01547 if (!skb)
01548 goto errout;
01549
01550 mutex_lock(nlk->cb_mutex);
01551
01552 cb = nlk->cb;
01553 if (cb == NULL) {
01554 err = -EINVAL;
01555 goto errout_skb;
01556 }
01557
01558 len = cb->dump(skb, cb);
01559
01560 if (len > 0) {
01561 mutex_unlock(nlk->cb_mutex);
01562
01563 if (sk_filter(sk, skb))
01564 kfree_skb(skb);
01565 else {
01566 skb_queue_tail(&sk->sk_receive_queue, skb);
01567 sk->sk_data_ready(sk, skb->len);
01568 }
01569 return 0;
01570 }
01571
01572 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
01573 if (!nlh)
01574 goto errout_skb;
01575
01576 memcpy(nlmsg_data(nlh), &len, sizeof(len));
01577
01578 if (sk_filter(sk, skb))
01579 kfree_skb(skb);
01580 else {
01581 skb_queue_tail(&sk->sk_receive_queue, skb);
01582 sk->sk_data_ready(sk, skb->len);
01583 }
01584
01585 if (cb->done)
01586 cb->done(cb);
01587 nlk->cb = NULL;
01588 mutex_unlock(nlk->cb_mutex);
01589
01590 netlink_destroy_callback(cb);
01591 return 0;
01592
01593 errout_skb:
01594 mutex_unlock(nlk->cb_mutex);
01595 kfree_skb(skb);
01596 errout:
01597 return err;
01598 }
01599
01600 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
01601 struct nlmsghdr *nlh,
01602 int (*dump)(struct sk_buff *skb,
01603 struct netlink_callback *),
01604 int (*done)(struct netlink_callback *))
01605 {
01606 #ifdef DDE_LINUX
01607 return -ENOBUFS;
01608 #else
01609 struct netlink_callback *cb;
01610 struct sock *sk;
01611 struct netlink_sock *nlk;
01612
01613 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
01614 if (cb == NULL)
01615 return -ENOBUFS;
01616
01617 cb->dump = dump;
01618 cb->done = done;
01619 cb->nlh = nlh;
01620 atomic_inc(&skb->users);
01621 cb->skb = skb;
01622
01623 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
01624 if (sk == NULL) {
01625 netlink_destroy_callback(cb);
01626 return -ECONNREFUSED;
01627 }
01628 nlk = nlk_sk(sk);
01629
01630 mutex_lock(nlk->cb_mutex);
01631 if (nlk->cb) {
01632 mutex_unlock(nlk->cb_mutex);
01633 netlink_destroy_callback(cb);
01634 sock_put(sk);
01635 return -EBUSY;
01636 }
01637 nlk->cb = cb;
01638 mutex_unlock(nlk->cb_mutex);
01639
01640 netlink_dump(sk);
01641 sock_put(sk);
01642
01643
01644
01645
01646 return -EINTR;
01647 #endif
01648 }
01649 EXPORT_SYMBOL(netlink_dump_start);
01650
01651 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
01652 {
01653 struct sk_buff *skb;
01654 struct nlmsghdr *rep;
01655 struct nlmsgerr *errmsg;
01656 size_t payload = sizeof(*errmsg);
01657
01658
01659 if (err)
01660 payload += nlmsg_len(nlh);
01661
01662 skb = nlmsg_new(payload, GFP_KERNEL);
01663 if (!skb) {
01664 struct sock *sk;
01665
01666 sk = netlink_lookup(sock_net(in_skb->sk),
01667 in_skb->sk->sk_protocol,
01668 NETLINK_CB(in_skb).pid);
01669 if (sk) {
01670 sk->sk_err = ENOBUFS;
01671 sk->sk_error_report(sk);
01672 sock_put(sk);
01673 }
01674 return;
01675 }
01676
01677 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
01678 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
01679 errmsg = nlmsg_data(rep);
01680 errmsg->error = err;
01681 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
01682 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
01683 }
01684 EXPORT_SYMBOL(netlink_ack);
01685
01686 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
01687 struct nlmsghdr *))
01688 {
01689 struct nlmsghdr *nlh;
01690 int err;
01691
01692 while (skb->len >= nlmsg_total_size(0)) {
01693 int msglen;
01694
01695 nlh = nlmsg_hdr(skb);
01696 err = 0;
01697
01698 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
01699 return 0;
01700
01701
01702 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
01703 goto ack;
01704
01705
01706 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
01707 goto ack;
01708
01709 err = cb(skb, nlh);
01710 if (err == -EINTR)
01711 goto skip;
01712
01713 ack:
01714 if (nlh->nlmsg_flags & NLM_F_ACK || err)
01715 netlink_ack(skb, nlh, err);
01716
01717 skip:
01718 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
01719 if (msglen > skb->len)
01720 msglen = skb->len;
01721 skb_pull(skb, msglen);
01722 }
01723
01724 return 0;
01725 }
01726 EXPORT_SYMBOL(netlink_rcv_skb);
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
01738 unsigned int group, int report, gfp_t flags)
01739 {
01740 int err = 0;
01741
01742 if (group) {
01743 int exclude_pid = 0;
01744
01745 if (report) {
01746 atomic_inc(&skb->users);
01747 exclude_pid = pid;
01748 }
01749
01750
01751 nlmsg_multicast(sk, skb, exclude_pid, group, flags);
01752 }
01753
01754 if (report)
01755 err = nlmsg_unicast(sk, skb, pid);
01756
01757 return err;
01758 }
01759 EXPORT_SYMBOL(nlmsg_notify);
01760
01761 #ifdef CONFIG_PROC_FS
01762 struct nl_seq_iter {
01763 struct seq_net_private p;
01764 int link;
01765 int hash_idx;
01766 };
01767
01768 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
01769 {
01770 struct nl_seq_iter *iter = seq->private;
01771 int i, j;
01772 struct sock *s;
01773 struct hlist_node *node;
01774 loff_t off = 0;
01775
01776 for (i = 0; i < MAX_LINKS; i++) {
01777 struct nl_pid_hash *hash = &nl_table[i].hash;
01778
01779 for (j = 0; j <= hash->mask; j++) {
01780 sk_for_each(s, node, &hash->table[j]) {
01781 if (sock_net(s) != seq_file_net(seq))
01782 continue;
01783 if (off == pos) {
01784 iter->link = i;
01785 iter->hash_idx = j;
01786 return s;
01787 }
01788 ++off;
01789 }
01790 }
01791 }
01792 return NULL;
01793 }
01794
01795 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
01796 __acquires(nl_table_lock)
01797 {
01798 read_lock(&nl_table_lock);
01799 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
01800 }
01801
01802 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
01803 {
01804 struct sock *s;
01805 struct nl_seq_iter *iter;
01806 int i, j;
01807
01808 ++*pos;
01809
01810 if (v == SEQ_START_TOKEN)
01811 return netlink_seq_socket_idx(seq, 0);
01812
01813 iter = seq->private;
01814 s = v;
01815 do {
01816 s = sk_next(s);
01817 } while (s && sock_net(s) != seq_file_net(seq));
01818 if (s)
01819 return s;
01820
01821 i = iter->link;
01822 j = iter->hash_idx + 1;
01823
01824 do {
01825 struct nl_pid_hash *hash = &nl_table[i].hash;
01826
01827 for (; j <= hash->mask; j++) {
01828 s = sk_head(&hash->table[j]);
01829 while (s && sock_net(s) != seq_file_net(seq))
01830 s = sk_next(s);
01831 if (s) {
01832 iter->link = i;
01833 iter->hash_idx = j;
01834 return s;
01835 }
01836 }
01837
01838 j = 0;
01839 } while (++i < MAX_LINKS);
01840
01841 return NULL;
01842 }
01843
01844 static void netlink_seq_stop(struct seq_file *seq, void *v)
01845 __releases(nl_table_lock)
01846 {
01847 read_unlock(&nl_table_lock);
01848 }
01849
01850
01851 static int netlink_seq_show(struct seq_file *seq, void *v)
01852 {
01853 if (v == SEQ_START_TOKEN)
01854 seq_puts(seq,
01855 "sk Eth Pid Groups "
01856 "Rmem Wmem Dump Locks\n");
01857 else {
01858 struct sock *s = v;
01859 struct netlink_sock *nlk = nlk_sk(s);
01860
01861 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
01862 s,
01863 s->sk_protocol,
01864 nlk->pid,
01865 nlk->groups ? (u32)nlk->groups[0] : 0,
01866 atomic_read(&s->sk_rmem_alloc),
01867 atomic_read(&s->sk_wmem_alloc),
01868 nlk->cb,
01869 atomic_read(&s->sk_refcnt)
01870 );
01871
01872 }
01873 return 0;
01874 }
01875
01876 static const struct seq_operations netlink_seq_ops = {
01877 .start = netlink_seq_start,
01878 .next = netlink_seq_next,
01879 .stop = netlink_seq_stop,
01880 .show = netlink_seq_show,
01881 };
01882
01883
01884 static int netlink_seq_open(struct inode *inode, struct file *file)
01885 {
01886 return seq_open_net(inode, file, &netlink_seq_ops,
01887 sizeof(struct nl_seq_iter));
01888 }
01889
01890 static const struct file_operations netlink_seq_fops = {
01891 .owner = THIS_MODULE,
01892 .open = netlink_seq_open,
01893 .read = seq_read,
01894 .llseek = seq_lseek,
01895 .release = seq_release_net,
01896 };
01897
01898 #endif
01899
01900 int netlink_register_notifier(struct notifier_block *nb)
01901 {
01902 return atomic_notifier_chain_register(&netlink_chain, nb);
01903 }
01904 EXPORT_SYMBOL(netlink_register_notifier);
01905
01906 int netlink_unregister_notifier(struct notifier_block *nb)
01907 {
01908 return atomic_notifier_chain_unregister(&netlink_chain, nb);
01909 }
01910 EXPORT_SYMBOL(netlink_unregister_notifier);
01911
01912 static const struct proto_ops netlink_ops = {
01913 .family = PF_NETLINK,
01914 .owner = THIS_MODULE,
01915 .release = netlink_release,
01916 .bind = netlink_bind,
01917 .connect = netlink_connect,
01918 .socketpair = sock_no_socketpair,
01919 .accept = sock_no_accept,
01920 .getname = netlink_getname,
01921 .poll = datagram_poll,
01922 .ioctl = sock_no_ioctl,
01923 .listen = sock_no_listen,
01924 .shutdown = sock_no_shutdown,
01925 .setsockopt = netlink_setsockopt,
01926 .getsockopt = netlink_getsockopt,
01927 .sendmsg = netlink_sendmsg,
01928 .recvmsg = netlink_recvmsg,
01929 .mmap = sock_no_mmap,
01930 .sendpage = sock_no_sendpage,
01931 };
01932
01933 static struct net_proto_family netlink_family_ops = {
01934 .family = PF_NETLINK,
01935 .create = netlink_create,
01936 .owner = THIS_MODULE,
01937 };
01938
01939 static int __net_init netlink_net_init(struct net *net)
01940 {
01941 #ifdef CONFIG_PROC_FS
01942 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
01943 return -ENOMEM;
01944 #endif
01945 return 0;
01946 }
01947
01948 static void __net_exit netlink_net_exit(struct net *net)
01949 {
01950 #ifdef CONFIG_PROC_FS
01951 proc_net_remove(net, "netlink");
01952 #endif
01953 }
01954
01955 static struct pernet_operations __net_initdata netlink_net_ops = {
01956 .init = netlink_net_init,
01957 .exit = netlink_net_exit,
01958 };
01959
01960 static int __init netlink_proto_init(void)
01961 {
01962 struct sk_buff *dummy_skb;
01963 int i;
01964 unsigned long limit;
01965 unsigned int order;
01966 int err = proto_register(&netlink_proto, 0);
01967
01968 if (err != 0)
01969 goto out;
01970
01971 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
01972
01973 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
01974 if (!nl_table)
01975 goto panic;
01976
01977 if (num_physpages >= (128 * 1024))
01978 limit = num_physpages >> (21 - PAGE_SHIFT);
01979 else
01980 limit = num_physpages >> (23 - PAGE_SHIFT);
01981
01982 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
01983 limit = (1UL << order) / sizeof(struct hlist_head);
01984 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
01985
01986 for (i = 0; i < MAX_LINKS; i++) {
01987 struct nl_pid_hash *hash = &nl_table[i].hash;
01988
01989 hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
01990 if (!hash->table) {
01991 while (i-- > 0)
01992 nl_pid_hash_free(nl_table[i].hash.table,
01993 1 * sizeof(*hash->table));
01994 kfree(nl_table);
01995 goto panic;
01996 }
01997 hash->max_shift = order;
01998 hash->shift = 0;
01999 hash->mask = 0;
02000 hash->rehash_time = jiffies;
02001 }
02002
02003 sock_register(&netlink_family_ops);
02004 register_pernet_subsys(&netlink_net_ops);
02005
02006 rtnetlink_init();
02007 out:
02008 return err;
02009 panic:
02010 panic("netlink_init: Cannot allocate nl_table\n");
02011 }
02012
02013 core_initcall(netlink_proto_init);