linux/net/phonet/socket.c
<<
>>
Prefs
   1/*
   2 * File: socket.c
   3 *
   4 * Phonet sockets
   5 *
   6 * Copyright (C) 2008 Nokia Corporation.
   7 *
   8 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
   9 *          RĂ©mi Denis-Courmont
  10 *
  11 * This program is free software; you can redistribute it and/or
  12 * modify it under the terms of the GNU General Public License
  13 * version 2 as published by the Free Software Foundation.
  14 *
  15 * This program is distributed in the hope that it will be useful, but
  16 * WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18 * General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23 * 02110-1301 USA
  24 */
  25
  26#include <linux/gfp.h>
  27#include <linux/kernel.h>
  28#include <linux/net.h>
  29#include <linux/poll.h>
  30#include <linux/sched/signal.h>
  31
  32#include <net/sock.h>
  33#include <net/tcp_states.h>
  34
  35#include <linux/phonet.h>
  36#include <linux/export.h>
  37#include <net/phonet/phonet.h>
  38#include <net/phonet/pep.h>
  39#include <net/phonet/pn_dev.h>
  40
  41static int pn_socket_release(struct socket *sock)
  42{
  43        struct sock *sk = sock->sk;
  44
  45        if (sk) {
  46                sock->sk = NULL;
  47                sk->sk_prot->close(sk, 0);
  48        }
  49        return 0;
  50}
  51
  52#define PN_HASHSIZE     16
  53#define PN_HASHMASK     (PN_HASHSIZE-1)
  54
  55
  56static struct  {
  57        struct hlist_head hlist[PN_HASHSIZE];
  58        struct mutex lock;
  59} pnsocks;
  60
  61void __init pn_sock_init(void)
  62{
  63        unsigned int i;
  64
  65        for (i = 0; i < PN_HASHSIZE; i++)
  66                INIT_HLIST_HEAD(pnsocks.hlist + i);
  67        mutex_init(&pnsocks.lock);
  68}
  69
  70static struct hlist_head *pn_hash_list(u16 obj)
  71{
  72        return pnsocks.hlist + (obj & PN_HASHMASK);
  73}
  74
  75/*
  76 * Find address based on socket address, match only certain fields.
  77 * Also grab sock if it was found. Remember to sock_put it later.
  78 */
  79struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
  80{
  81        struct sock *sknode;
  82        struct sock *rval = NULL;
  83        u16 obj = pn_sockaddr_get_object(spn);
  84        u8 res = spn->spn_resource;
  85        struct hlist_head *hlist = pn_hash_list(obj);
  86
  87        rcu_read_lock();
  88        sk_for_each_rcu(sknode, hlist) {
  89                struct pn_sock *pn = pn_sk(sknode);
  90                BUG_ON(!pn->sobject); /* unbound socket */
  91
  92                if (!net_eq(sock_net(sknode), net))
  93                        continue;
  94                if (pn_port(obj)) {
  95                        /* Look up socket by port */
  96                        if (pn_port(pn->sobject) != pn_port(obj))
  97                                continue;
  98                } else {
  99                        /* If port is zero, look up by resource */
 100                        if (pn->resource != res)
 101                                continue;
 102                }
 103                if (pn_addr(pn->sobject) &&
 104                    pn_addr(pn->sobject) != pn_addr(obj))
 105                        continue;
 106
 107                rval = sknode;
 108                sock_hold(sknode);
 109                break;
 110        }
 111        rcu_read_unlock();
 112
 113        return rval;
 114}
 115
 116/* Deliver a broadcast packet (only in bottom-half) */
 117void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
 118{
 119        struct hlist_head *hlist = pnsocks.hlist;
 120        unsigned int h;
 121
 122        rcu_read_lock();
 123        for (h = 0; h < PN_HASHSIZE; h++) {
 124                struct sock *sknode;
 125
 126                sk_for_each(sknode, hlist) {
 127                        struct sk_buff *clone;
 128
 129                        if (!net_eq(sock_net(sknode), net))
 130                                continue;
 131                        if (!sock_flag(sknode, SOCK_BROADCAST))
 132                                continue;
 133
 134                        clone = skb_clone(skb, GFP_ATOMIC);
 135                        if (clone) {
 136                                sock_hold(sknode);
 137                                sk_receive_skb(sknode, clone, 0);
 138                        }
 139                }
 140                hlist++;
 141        }
 142        rcu_read_unlock();
 143}
 144
 145int pn_sock_hash(struct sock *sk)
 146{
 147        struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject);
 148
 149        mutex_lock(&pnsocks.lock);
 150        sk_add_node_rcu(sk, hlist);
 151        mutex_unlock(&pnsocks.lock);
 152
 153        return 0;
 154}
 155EXPORT_SYMBOL(pn_sock_hash);
 156
 157void pn_sock_unhash(struct sock *sk)
 158{
 159        mutex_lock(&pnsocks.lock);
 160        sk_del_node_init_rcu(sk);
 161        mutex_unlock(&pnsocks.lock);
 162        pn_sock_unbind_all_res(sk);
 163        synchronize_rcu();
 164}
 165EXPORT_SYMBOL(pn_sock_unhash);
 166
 167static DEFINE_MUTEX(port_mutex);
 168
 169static int pn_socket_bind(struct socket *sock, struct sockaddr *addr, int len)
 170{
 171        struct sock *sk = sock->sk;
 172        struct pn_sock *pn = pn_sk(sk);
 173        struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
 174        int err;
 175        u16 handle;
 176        u8 saddr;
 177
 178        if (sk->sk_prot->bind)
 179                return sk->sk_prot->bind(sk, addr, len);
 180
 181        if (len < sizeof(struct sockaddr_pn))
 182                return -EINVAL;
 183        if (spn->spn_family != AF_PHONET)
 184                return -EAFNOSUPPORT;
 185
 186        handle = pn_sockaddr_get_object((struct sockaddr_pn *)addr);
 187        saddr = pn_addr(handle);
 188        if (saddr && phonet_address_lookup(sock_net(sk), saddr))
 189                return -EADDRNOTAVAIL;
 190
 191        lock_sock(sk);
 192        if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) {
 193                err = -EINVAL; /* attempt to rebind */
 194                goto out;
 195        }
 196        WARN_ON(sk_hashed(sk));
 197        mutex_lock(&port_mutex);
 198        err = sk->sk_prot->get_port(sk, pn_port(handle));
 199        if (err)
 200                goto out_port;
 201
 202        /* get_port() sets the port, bind() sets the address if applicable */
 203        pn->sobject = pn_object(saddr, pn_port(pn->sobject));
 204        pn->resource = spn->spn_resource;
 205
 206        /* Enable RX on the socket */
 207        err = sk->sk_prot->hash(sk);
 208out_port:
 209        mutex_unlock(&port_mutex);
 210out:
 211        release_sock(sk);
 212        return err;
 213}
 214
 215static int pn_socket_autobind(struct socket *sock)
 216{
 217        struct sockaddr_pn sa;
 218        int err;
 219
 220        memset(&sa, 0, sizeof(sa));
 221        sa.spn_family = AF_PHONET;
 222        err = pn_socket_bind(sock, (struct sockaddr *)&sa,
 223                                sizeof(struct sockaddr_pn));
 224        if (err != -EINVAL)
 225                return err;
 226        BUG_ON(!pn_port(pn_sk(sock->sk)->sobject));
 227        return 0; /* socket was already bound */
 228}
 229
 230static int pn_socket_connect(struct socket *sock, struct sockaddr *addr,
 231                int len, int flags)
 232{
 233        struct sock *sk = sock->sk;
 234        struct pn_sock *pn = pn_sk(sk);
 235        struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
 236        struct task_struct *tsk = current;
 237        long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
 238        int err;
 239
 240        if (pn_socket_autobind(sock))
 241                return -ENOBUFS;
 242        if (len < sizeof(struct sockaddr_pn))
 243                return -EINVAL;
 244        if (spn->spn_family != AF_PHONET)
 245                return -EAFNOSUPPORT;
 246
 247        lock_sock(sk);
 248
 249        switch (sock->state) {
 250        case SS_UNCONNECTED:
 251                if (sk->sk_state != TCP_CLOSE) {
 252                        err = -EISCONN;
 253                        goto out;
 254                }
 255                break;
 256        case SS_CONNECTING:
 257                err = -EALREADY;
 258                goto out;
 259        default:
 260                err = -EISCONN;
 261                goto out;
 262        }
 263
 264        pn->dobject = pn_sockaddr_get_object(spn);
 265        pn->resource = pn_sockaddr_get_resource(spn);
 266        sock->state = SS_CONNECTING;
 267
 268        err = sk->sk_prot->connect(sk, addr, len);
 269        if (err) {
 270                sock->state = SS_UNCONNECTED;
 271                pn->dobject = 0;
 272                goto out;
 273        }
 274
 275        while (sk->sk_state == TCP_SYN_SENT) {
 276                DEFINE_WAIT(wait);
 277
 278                if (!timeo) {
 279                        err = -EINPROGRESS;
 280                        goto out;
 281                }
 282                if (signal_pending(tsk)) {
 283                        err = sock_intr_errno(timeo);
 284                        goto out;
 285                }
 286
 287                prepare_to_wait_exclusive(sk_sleep(sk), &wait,
 288                                                TASK_INTERRUPTIBLE);
 289                release_sock(sk);
 290                timeo = schedule_timeout(timeo);
 291                lock_sock(sk);
 292                finish_wait(sk_sleep(sk), &wait);
 293        }
 294
 295        if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED))
 296                err = 0;
 297        else if (sk->sk_state == TCP_CLOSE_WAIT)
 298                err = -ECONNRESET;
 299        else
 300                err = -ECONNREFUSED;
 301        sock->state = err ? SS_UNCONNECTED : SS_CONNECTED;
 302out:
 303        release_sock(sk);
 304        return err;
 305}
 306
 307static int pn_socket_accept(struct socket *sock, struct socket *newsock,
 308                            int flags, bool kern)
 309{
 310        struct sock *sk = sock->sk;
 311        struct sock *newsk;
 312        int err;
 313
 314        if (unlikely(sk->sk_state != TCP_LISTEN))
 315                return -EINVAL;
 316
 317        newsk = sk->sk_prot->accept(sk, flags, &err, kern);
 318        if (!newsk)
 319                return err;
 320
 321        lock_sock(newsk);
 322        sock_graft(newsk, newsock);
 323        newsock->state = SS_CONNECTED;
 324        release_sock(newsk);
 325        return 0;
 326}
 327
 328static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
 329                                int *sockaddr_len, int peer)
 330{
 331        struct sock *sk = sock->sk;
 332        struct pn_sock *pn = pn_sk(sk);
 333
 334        memset(addr, 0, sizeof(struct sockaddr_pn));
 335        addr->sa_family = AF_PHONET;
 336        if (!peer) /* Race with bind() here is userland's problem. */
 337                pn_sockaddr_set_object((struct sockaddr_pn *)addr,
 338                                        pn->sobject);
 339
 340        *sockaddr_len = sizeof(struct sockaddr_pn);
 341        return 0;
 342}
 343
 344static __poll_t pn_socket_poll(struct file *file, struct socket *sock,
 345                                        poll_table *wait)
 346{
 347        struct sock *sk = sock->sk;
 348        struct pep_sock *pn = pep_sk(sk);
 349        __poll_t mask = 0;
 350
 351        poll_wait(file, sk_sleep(sk), wait);
 352
 353        if (sk->sk_state == TCP_CLOSE)
 354                return EPOLLERR;
 355        if (!skb_queue_empty(&sk->sk_receive_queue))
 356                mask |= EPOLLIN | EPOLLRDNORM;
 357        if (!skb_queue_empty(&pn->ctrlreq_queue))
 358                mask |= EPOLLPRI;
 359        if (!mask && sk->sk_state == TCP_CLOSE_WAIT)
 360                return EPOLLHUP;
 361
 362        if (sk->sk_state == TCP_ESTABLISHED &&
 363                refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf &&
 364                atomic_read(&pn->tx_credits))
 365                mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
 366
 367        return mask;
 368}
 369
 370static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
 371                                unsigned long arg)
 372{
 373        struct sock *sk = sock->sk;
 374        struct pn_sock *pn = pn_sk(sk);
 375
 376        if (cmd == SIOCPNGETOBJECT) {
 377                struct net_device *dev;
 378                u16 handle;
 379                u8 saddr;
 380
 381                if (get_user(handle, (__u16 __user *)arg))
 382                        return -EFAULT;
 383
 384                lock_sock(sk);
 385                if (sk->sk_bound_dev_if)
 386                        dev = dev_get_by_index(sock_net(sk),
 387                                                sk->sk_bound_dev_if);
 388                else
 389                        dev = phonet_device_get(sock_net(sk));
 390                if (dev && (dev->flags & IFF_UP))
 391                        saddr = phonet_address_get(dev, pn_addr(handle));
 392                else
 393                        saddr = PN_NO_ADDR;
 394                release_sock(sk);
 395
 396                if (dev)
 397                        dev_put(dev);
 398                if (saddr == PN_NO_ADDR)
 399                        return -EHOSTUNREACH;
 400
 401                handle = pn_object(saddr, pn_port(pn->sobject));
 402                return put_user(handle, (__u16 __user *)arg);
 403        }
 404
 405        return sk->sk_prot->ioctl(sk, cmd, arg);
 406}
 407
 408static int pn_socket_listen(struct socket *sock, int backlog)
 409{
 410        struct sock *sk = sock->sk;
 411        int err = 0;
 412
 413        if (pn_socket_autobind(sock))
 414                return -ENOBUFS;
 415
 416        lock_sock(sk);
 417        if (sock->state != SS_UNCONNECTED) {
 418                err = -EINVAL;
 419                goto out;
 420        }
 421
 422        if (sk->sk_state != TCP_LISTEN) {
 423                sk->sk_state = TCP_LISTEN;
 424                sk->sk_ack_backlog = 0;
 425        }
 426        sk->sk_max_ack_backlog = backlog;
 427out:
 428        release_sock(sk);
 429        return err;
 430}
 431
 432static int pn_socket_sendmsg(struct socket *sock, struct msghdr *m,
 433                             size_t total_len)
 434{
 435        struct sock *sk = sock->sk;
 436
 437        if (pn_socket_autobind(sock))
 438                return -EAGAIN;
 439
 440        return sk->sk_prot->sendmsg(sk, m, total_len);
 441}
 442
 443const struct proto_ops phonet_dgram_ops = {
 444        .family         = AF_PHONET,
 445        .owner          = THIS_MODULE,
 446        .release        = pn_socket_release,
 447        .bind           = pn_socket_bind,
 448        .connect        = sock_no_connect,
 449        .socketpair     = sock_no_socketpair,
 450        .accept         = sock_no_accept,
 451        .getname        = pn_socket_getname,
 452        .poll           = datagram_poll,
 453        .ioctl          = pn_socket_ioctl,
 454        .listen         = sock_no_listen,
 455        .shutdown       = sock_no_shutdown,
 456        .setsockopt     = sock_no_setsockopt,
 457        .getsockopt     = sock_no_getsockopt,
 458#ifdef CONFIG_COMPAT
 459        .compat_setsockopt = sock_no_setsockopt,
 460        .compat_getsockopt = sock_no_getsockopt,
 461#endif
 462        .sendmsg        = pn_socket_sendmsg,
 463        .recvmsg        = sock_common_recvmsg,
 464        .mmap           = sock_no_mmap,
 465        .sendpage       = sock_no_sendpage,
 466};
 467
 468const struct proto_ops phonet_stream_ops = {
 469        .family         = AF_PHONET,
 470        .owner          = THIS_MODULE,
 471        .release        = pn_socket_release,
 472        .bind           = pn_socket_bind,
 473        .connect        = pn_socket_connect,
 474        .socketpair     = sock_no_socketpair,
 475        .accept         = pn_socket_accept,
 476        .getname        = pn_socket_getname,
 477        .poll           = pn_socket_poll,
 478        .ioctl          = pn_socket_ioctl,
 479        .listen         = pn_socket_listen,
 480        .shutdown       = sock_no_shutdown,
 481        .setsockopt     = sock_common_setsockopt,
 482        .getsockopt     = sock_common_getsockopt,
 483#ifdef CONFIG_COMPAT
 484        .compat_setsockopt = compat_sock_common_setsockopt,
 485        .compat_getsockopt = compat_sock_common_getsockopt,
 486#endif
 487        .sendmsg        = pn_socket_sendmsg,
 488        .recvmsg        = sock_common_recvmsg,
 489        .mmap           = sock_no_mmap,
 490        .sendpage       = sock_no_sendpage,
 491};
 492EXPORT_SYMBOL(phonet_stream_ops);
 493
 494/* allocate port for a socket */
 495int pn_sock_get_port(struct sock *sk, unsigned short sport)
 496{
 497        static int port_cur;
 498        struct net *net = sock_net(sk);
 499        struct pn_sock *pn = pn_sk(sk);
 500        struct sockaddr_pn try_sa;
 501        struct sock *tmpsk;
 502
 503        memset(&try_sa, 0, sizeof(struct sockaddr_pn));
 504        try_sa.spn_family = AF_PHONET;
 505        WARN_ON(!mutex_is_locked(&port_mutex));
 506        if (!sport) {
 507                /* search free port */
 508                int port, pmin, pmax;
 509
 510                phonet_get_local_port_range(&pmin, &pmax);
 511                for (port = pmin; port <= pmax; port++) {
 512                        port_cur++;
 513                        if (port_cur < pmin || port_cur > pmax)
 514                                port_cur = pmin;
 515
 516                        pn_sockaddr_set_port(&try_sa, port_cur);
 517                        tmpsk = pn_find_sock_by_sa(net, &try_sa);
 518                        if (tmpsk == NULL) {
 519                                sport = port_cur;
 520                                goto found;
 521                        } else
 522                                sock_put(tmpsk);
 523                }
 524        } else {
 525                /* try to find specific port */
 526                pn_sockaddr_set_port(&try_sa, sport);
 527                tmpsk = pn_find_sock_by_sa(net, &try_sa);
 528                if (tmpsk == NULL)
 529                        /* No sock there! We can use that port... */
 530                        goto found;
 531                else
 532                        sock_put(tmpsk);
 533        }
 534        /* the port must be in use already */
 535        return -EADDRINUSE;
 536
 537found:
 538        pn->sobject = pn_object(pn_addr(pn->sobject), sport);
 539        return 0;
 540}
 541EXPORT_SYMBOL(pn_sock_get_port);
 542
 543#ifdef CONFIG_PROC_FS
 544static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
 545{
 546        struct net *net = seq_file_net(seq);
 547        struct hlist_head *hlist = pnsocks.hlist;
 548        struct sock *sknode;
 549        unsigned int h;
 550
 551        for (h = 0; h < PN_HASHSIZE; h++) {
 552                sk_for_each_rcu(sknode, hlist) {
 553                        if (!net_eq(net, sock_net(sknode)))
 554                                continue;
 555                        if (!pos)
 556                                return sknode;
 557                        pos--;
 558                }
 559                hlist++;
 560        }
 561        return NULL;
 562}
 563
 564static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk)
 565{
 566        struct net *net = seq_file_net(seq);
 567
 568        do
 569                sk = sk_next(sk);
 570        while (sk && !net_eq(net, sock_net(sk)));
 571
 572        return sk;
 573}
 574
 575static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos)
 576        __acquires(rcu)
 577{
 578        rcu_read_lock();
 579        return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
 580}
 581
 582static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 583{
 584        struct sock *sk;
 585
 586        if (v == SEQ_START_TOKEN)
 587                sk = pn_sock_get_idx(seq, 0);
 588        else
 589                sk = pn_sock_get_next(seq, v);
 590        (*pos)++;
 591        return sk;
 592}
 593
 594static void pn_sock_seq_stop(struct seq_file *seq, void *v)
 595        __releases(rcu)
 596{
 597        rcu_read_unlock();
 598}
 599
 600static int pn_sock_seq_show(struct seq_file *seq, void *v)
 601{
 602        seq_setwidth(seq, 127);
 603        if (v == SEQ_START_TOKEN)
 604                seq_puts(seq, "pt  loc  rem rs st tx_queue rx_queue "
 605                        "  uid inode ref pointer drops");
 606        else {
 607                struct sock *sk = v;
 608                struct pn_sock *pn = pn_sk(sk);
 609
 610                seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
 611                        "%d %pK %d",
 612                        sk->sk_protocol, pn->sobject, pn->dobject,
 613                        pn->resource, sk->sk_state,
 614                        sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
 615                        from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
 616                        sock_i_ino(sk),
 617                        refcount_read(&sk->sk_refcnt), sk,
 618                        atomic_read(&sk->sk_drops));
 619        }
 620        seq_pad(seq, '\n');
 621        return 0;
 622}
 623
 624static const struct seq_operations pn_sock_seq_ops = {
 625        .start = pn_sock_seq_start,
 626        .next = pn_sock_seq_next,
 627        .stop = pn_sock_seq_stop,
 628        .show = pn_sock_seq_show,
 629};
 630
 631static int pn_sock_open(struct inode *inode, struct file *file)
 632{
 633        return seq_open_net(inode, file, &pn_sock_seq_ops,
 634                                sizeof(struct seq_net_private));
 635}
 636
 637const struct file_operations pn_sock_seq_fops = {
 638        .open = pn_sock_open,
 639        .read = seq_read,
 640        .llseek = seq_lseek,
 641        .release = seq_release_net,
 642};
 643#endif
 644
 645static struct  {
 646        struct sock *sk[256];
 647} pnres;
 648
 649/*
 650 * Find and hold socket based on resource.
 651 */
 652struct sock *pn_find_sock_by_res(struct net *net, u8 res)
 653{
 654        struct sock *sk;
 655
 656        if (!net_eq(net, &init_net))
 657                return NULL;
 658
 659        rcu_read_lock();
 660        sk = rcu_dereference(pnres.sk[res]);
 661        if (sk)
 662                sock_hold(sk);
 663        rcu_read_unlock();
 664        return sk;
 665}
 666
 667static DEFINE_MUTEX(resource_mutex);
 668
 669int pn_sock_bind_res(struct sock *sk, u8 res)
 670{
 671        int ret = -EADDRINUSE;
 672
 673        if (!net_eq(sock_net(sk), &init_net))
 674                return -ENOIOCTLCMD;
 675        if (!capable(CAP_SYS_ADMIN))
 676                return -EPERM;
 677        if (pn_socket_autobind(sk->sk_socket))
 678                return -EAGAIN;
 679
 680        mutex_lock(&resource_mutex);
 681        if (pnres.sk[res] == NULL) {
 682                sock_hold(sk);
 683                rcu_assign_pointer(pnres.sk[res], sk);
 684                ret = 0;
 685        }
 686        mutex_unlock(&resource_mutex);
 687        return ret;
 688}
 689
 690int pn_sock_unbind_res(struct sock *sk, u8 res)
 691{
 692        int ret = -ENOENT;
 693
 694        if (!capable(CAP_SYS_ADMIN))
 695                return -EPERM;
 696
 697        mutex_lock(&resource_mutex);
 698        if (pnres.sk[res] == sk) {
 699                RCU_INIT_POINTER(pnres.sk[res], NULL);
 700                ret = 0;
 701        }
 702        mutex_unlock(&resource_mutex);
 703
 704        if (ret == 0) {
 705                synchronize_rcu();
 706                sock_put(sk);
 707        }
 708        return ret;
 709}
 710
 711void pn_sock_unbind_all_res(struct sock *sk)
 712{
 713        unsigned int res, match = 0;
 714
 715        mutex_lock(&resource_mutex);
 716        for (res = 0; res < 256; res++) {
 717                if (pnres.sk[res] == sk) {
 718                        RCU_INIT_POINTER(pnres.sk[res], NULL);
 719                        match++;
 720                }
 721        }
 722        mutex_unlock(&resource_mutex);
 723
 724        while (match > 0) {
 725                __sock_put(sk);
 726                match--;
 727        }
 728        /* Caller is responsible for RCU sync before final sock_put() */
 729}
 730
 731#ifdef CONFIG_PROC_FS
 732static struct sock **pn_res_get_idx(struct seq_file *seq, loff_t pos)
 733{
 734        struct net *net = seq_file_net(seq);
 735        unsigned int i;
 736
 737        if (!net_eq(net, &init_net))
 738                return NULL;
 739
 740        for (i = 0; i < 256; i++) {
 741                if (pnres.sk[i] == NULL)
 742                        continue;
 743                if (!pos)
 744                        return pnres.sk + i;
 745                pos--;
 746        }
 747        return NULL;
 748}
 749
 750static struct sock **pn_res_get_next(struct seq_file *seq, struct sock **sk)
 751{
 752        struct net *net = seq_file_net(seq);
 753        unsigned int i;
 754
 755        BUG_ON(!net_eq(net, &init_net));
 756
 757        for (i = (sk - pnres.sk) + 1; i < 256; i++)
 758                if (pnres.sk[i])
 759                        return pnres.sk + i;
 760        return NULL;
 761}
 762
 763static void *pn_res_seq_start(struct seq_file *seq, loff_t *pos)
 764        __acquires(resource_mutex)
 765{
 766        mutex_lock(&resource_mutex);
 767        return *pos ? pn_res_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
 768}
 769
 770static void *pn_res_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 771{
 772        struct sock **sk;
 773
 774        if (v == SEQ_START_TOKEN)
 775                sk = pn_res_get_idx(seq, 0);
 776        else
 777                sk = pn_res_get_next(seq, v);
 778        (*pos)++;
 779        return sk;
 780}
 781
 782static void pn_res_seq_stop(struct seq_file *seq, void *v)
 783        __releases(resource_mutex)
 784{
 785        mutex_unlock(&resource_mutex);
 786}
 787
 788static int pn_res_seq_show(struct seq_file *seq, void *v)
 789{
 790        seq_setwidth(seq, 63);
 791        if (v == SEQ_START_TOKEN)
 792                seq_puts(seq, "rs   uid inode");
 793        else {
 794                struct sock **psk = v;
 795                struct sock *sk = *psk;
 796
 797                seq_printf(seq, "%02X %5u %lu",
 798                           (int) (psk - pnres.sk),
 799                           from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
 800                           sock_i_ino(sk));
 801        }
 802        seq_pad(seq, '\n');
 803        return 0;
 804}
 805
 806static const struct seq_operations pn_res_seq_ops = {
 807        .start = pn_res_seq_start,
 808        .next = pn_res_seq_next,
 809        .stop = pn_res_seq_stop,
 810        .show = pn_res_seq_show,
 811};
 812
 813static int pn_res_open(struct inode *inode, struct file *file)
 814{
 815        return seq_open_net(inode, file, &pn_res_seq_ops,
 816                                sizeof(struct seq_net_private));
 817}
 818
 819const struct file_operations pn_res_seq_fops = {
 820        .open = pn_res_open,
 821        .read = seq_read,
 822        .llseek = seq_lseek,
 823        .release = seq_release_net,
 824};
 825#endif
 826