linux/net/atm/common.c
<<
>>
Prefs
   1/* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
   2
   3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
   4
   5#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
   6
   7#include <linux/module.h>
   8#include <linux/kmod.h>
   9#include <linux/net.h>          /* struct socket, struct proto_ops */
  10#include <linux/atm.h>          /* ATM stuff */
  11#include <linux/atmdev.h>
  12#include <linux/socket.h>       /* SOL_SOCKET */
  13#include <linux/errno.h>        /* error codes */
  14#include <linux/capability.h>
  15#include <linux/mm.h>
  16#include <linux/sched.h>
  17#include <linux/time.h>         /* struct timeval */
  18#include <linux/skbuff.h>
  19#include <linux/bitops.h>
  20#include <linux/init.h>
  21#include <linux/slab.h>
  22#include <net/sock.h>           /* struct sock */
  23#include <linux/uaccess.h>
  24#include <linux/poll.h>
  25
  26#include <asm/atomic.h>
  27
  28#include "resources.h"          /* atm_find_dev */
  29#include "common.h"             /* prototypes */
  30#include "protocols.h"          /* atm_init_<transport> */
  31#include "addr.h"               /* address registry */
  32#include "signaling.h"          /* for WAITING and sigd_attach */
  33
  34struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
  35EXPORT_SYMBOL(vcc_hash);
  36
  37DEFINE_RWLOCK(vcc_sklist_lock);
  38EXPORT_SYMBOL(vcc_sklist_lock);
  39
  40static void __vcc_insert_socket(struct sock *sk)
  41{
  42        struct atm_vcc *vcc = atm_sk(sk);
  43        struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
  44        sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
  45        sk_add_node(sk, head);
  46}
  47
  48void vcc_insert_socket(struct sock *sk)
  49{
  50        write_lock_irq(&vcc_sklist_lock);
  51        __vcc_insert_socket(sk);
  52        write_unlock_irq(&vcc_sklist_lock);
  53}
  54EXPORT_SYMBOL(vcc_insert_socket);
  55
  56static void vcc_remove_socket(struct sock *sk)
  57{
  58        write_lock_irq(&vcc_sklist_lock);
  59        sk_del_node_init(sk);
  60        write_unlock_irq(&vcc_sklist_lock);
  61}
  62
  63static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
  64{
  65        struct sk_buff *skb;
  66        struct sock *sk = sk_atm(vcc);
  67
  68        if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
  69                pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
  70                         sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
  71                return NULL;
  72        }
  73        while (!(skb = alloc_skb(size, GFP_KERNEL)))
  74                schedule();
  75        pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
  76        atomic_add(skb->truesize, &sk->sk_wmem_alloc);
  77        return skb;
  78}
  79
  80static void vcc_sock_destruct(struct sock *sk)
  81{
  82        if (atomic_read(&sk->sk_rmem_alloc))
  83                printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
  84                       __func__, atomic_read(&sk->sk_rmem_alloc));
  85
  86        if (atomic_read(&sk->sk_wmem_alloc))
  87                printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
  88                       __func__, atomic_read(&sk->sk_wmem_alloc));
  89}
  90
  91static void vcc_def_wakeup(struct sock *sk)
  92{
  93        struct socket_wq *wq;
  94
  95        rcu_read_lock();
  96        wq = rcu_dereference(sk->sk_wq);
  97        if (wq_has_sleeper(wq))
  98                wake_up(&wq->wait);
  99        rcu_read_unlock();
 100}
 101
 102static inline int vcc_writable(struct sock *sk)
 103{
 104        struct atm_vcc *vcc = atm_sk(sk);
 105
 106        return (vcc->qos.txtp.max_sdu +
 107                atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
 108}
 109
 110static void vcc_write_space(struct sock *sk)
 111{
 112        struct socket_wq *wq;
 113
 114        rcu_read_lock();
 115
 116        if (vcc_writable(sk)) {
 117                wq = rcu_dereference(sk->sk_wq);
 118                if (wq_has_sleeper(wq))
 119                        wake_up_interruptible(&wq->wait);
 120
 121                sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
 122        }
 123
 124        rcu_read_unlock();
 125}
 126
 127static struct proto vcc_proto = {
 128        .name     = "VCC",
 129        .owner    = THIS_MODULE,
 130        .obj_size = sizeof(struct atm_vcc),
 131};
 132
 133int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
 134{
 135        struct sock *sk;
 136        struct atm_vcc *vcc;
 137
 138        sock->sk = NULL;
 139        if (sock->type == SOCK_STREAM)
 140                return -EINVAL;
 141        sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
 142        if (!sk)
 143                return -ENOMEM;
 144        sock_init_data(sock, sk);
 145        sk->sk_state_change = vcc_def_wakeup;
 146        sk->sk_write_space = vcc_write_space;
 147
 148        vcc = atm_sk(sk);
 149        vcc->dev = NULL;
 150        memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
 151        memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
 152        vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
 153        atomic_set(&sk->sk_wmem_alloc, 1);
 154        atomic_set(&sk->sk_rmem_alloc, 0);
 155        vcc->push = NULL;
 156        vcc->pop = NULL;
 157        vcc->push_oam = NULL;
 158        vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
 159        vcc->atm_options = vcc->aal_options = 0;
 160        sk->sk_destruct = vcc_sock_destruct;
 161        return 0;
 162}
 163
 164static void vcc_destroy_socket(struct sock *sk)
 165{
 166        struct atm_vcc *vcc = atm_sk(sk);
 167        struct sk_buff *skb;
 168
 169        set_bit(ATM_VF_CLOSE, &vcc->flags);
 170        clear_bit(ATM_VF_READY, &vcc->flags);
 171        if (vcc->dev) {
 172                if (vcc->dev->ops->close)
 173                        vcc->dev->ops->close(vcc);
 174                if (vcc->push)
 175                        vcc->push(vcc, NULL); /* atmarpd has no push */
 176
 177                while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
 178                        atm_return(vcc, skb->truesize);
 179                        kfree_skb(skb);
 180                }
 181
 182                module_put(vcc->dev->ops->owner);
 183                atm_dev_put(vcc->dev);
 184        }
 185
 186        vcc_remove_socket(sk);
 187}
 188
 189int vcc_release(struct socket *sock)
 190{
 191        struct sock *sk = sock->sk;
 192
 193        if (sk) {
 194                lock_sock(sk);
 195                vcc_destroy_socket(sock->sk);
 196                release_sock(sk);
 197                sock_put(sk);
 198        }
 199
 200        return 0;
 201}
 202
 203void vcc_release_async(struct atm_vcc *vcc, int reply)
 204{
 205        struct sock *sk = sk_atm(vcc);
 206
 207        set_bit(ATM_VF_CLOSE, &vcc->flags);
 208        sk->sk_shutdown |= RCV_SHUTDOWN;
 209        sk->sk_err = -reply;
 210        clear_bit(ATM_VF_WAITING, &vcc->flags);
 211        sk->sk_state_change(sk);
 212}
 213EXPORT_SYMBOL(vcc_release_async);
 214
 215
 216void atm_dev_release_vccs(struct atm_dev *dev)
 217{
 218        int i;
 219
 220        write_lock_irq(&vcc_sklist_lock);
 221        for (i = 0; i < VCC_HTABLE_SIZE; i++) {
 222                struct hlist_head *head = &vcc_hash[i];
 223                struct hlist_node *node, *tmp;
 224                struct sock *s;
 225                struct atm_vcc *vcc;
 226
 227                sk_for_each_safe(s, node, tmp, head) {
 228                        vcc = atm_sk(s);
 229                        if (vcc->dev == dev) {
 230                                vcc_release_async(vcc, -EPIPE);
 231                                sk_del_node_init(s);
 232                        }
 233                }
 234        }
 235        write_unlock_irq(&vcc_sklist_lock);
 236}
 237
 238static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
 239{
 240        int max_sdu;
 241
 242        if (!tp->traffic_class)
 243                return 0;
 244        switch (aal) {
 245        case ATM_AAL0:
 246                max_sdu = ATM_CELL_SIZE-1;
 247                break;
 248        case ATM_AAL34:
 249                max_sdu = ATM_MAX_AAL34_PDU;
 250                break;
 251        default:
 252                pr_warning("AAL problems ... (%d)\n", aal);
 253                /* fall through */
 254        case ATM_AAL5:
 255                max_sdu = ATM_MAX_AAL5_PDU;
 256        }
 257        if (!tp->max_sdu)
 258                tp->max_sdu = max_sdu;
 259        else if (tp->max_sdu > max_sdu)
 260                return -EINVAL;
 261        if (!tp->max_cdv)
 262                tp->max_cdv = ATM_MAX_CDV;
 263        return 0;
 264}
 265
 266static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
 267{
 268        struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
 269        struct hlist_node *node;
 270        struct sock *s;
 271        struct atm_vcc *walk;
 272
 273        sk_for_each(s, node, head) {
 274                walk = atm_sk(s);
 275                if (walk->dev != vcc->dev)
 276                        continue;
 277                if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
 278                    walk->vci == vci && ((walk->qos.txtp.traffic_class !=
 279                    ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
 280                    (walk->qos.rxtp.traffic_class != ATM_NONE &&
 281                    vcc->qos.rxtp.traffic_class != ATM_NONE)))
 282                        return -EADDRINUSE;
 283        }
 284
 285        /* allow VCCs with same VPI/VCI iff they don't collide on
 286           TX/RX (but we may refuse such sharing for other reasons,
 287           e.g. if protocol requires to have both channels) */
 288
 289        return 0;
 290}
 291
 292static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
 293{
 294        static short p;        /* poor man's per-device cache */
 295        static int c;
 296        short old_p;
 297        int old_c;
 298        int err;
 299
 300        if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
 301                err = check_ci(vcc, *vpi, *vci);
 302                return err;
 303        }
 304        /* last scan may have left values out of bounds for current device */
 305        if (*vpi != ATM_VPI_ANY)
 306                p = *vpi;
 307        else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
 308                p = 0;
 309        if (*vci != ATM_VCI_ANY)
 310                c = *vci;
 311        else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
 312                        c = ATM_NOT_RSV_VCI;
 313        old_p = p;
 314        old_c = c;
 315        do {
 316                if (!check_ci(vcc, p, c)) {
 317                        *vpi = p;
 318                        *vci = c;
 319                        return 0;
 320                }
 321                if (*vci == ATM_VCI_ANY) {
 322                        c++;
 323                        if (c >= 1 << vcc->dev->ci_range.vci_bits)
 324                                c = ATM_NOT_RSV_VCI;
 325                }
 326                if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
 327                    *vpi == ATM_VPI_ANY) {
 328                        p++;
 329                        if (p >= 1 << vcc->dev->ci_range.vpi_bits)
 330                                p = 0;
 331                }
 332        } while (old_p != p || old_c != c);
 333        return -EADDRINUSE;
 334}
 335
 336static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
 337                         int vci)
 338{
 339        struct sock *sk = sk_atm(vcc);
 340        int error;
 341
 342        if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
 343            vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
 344            vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
 345                return -EINVAL;
 346        if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
 347                return -EPERM;
 348        error = -ENODEV;
 349        if (!try_module_get(dev->ops->owner))
 350                return error;
 351        vcc->dev = dev;
 352        write_lock_irq(&vcc_sklist_lock);
 353        if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
 354            (error = find_ci(vcc, &vpi, &vci))) {
 355                write_unlock_irq(&vcc_sklist_lock);
 356                goto fail_module_put;
 357        }
 358        vcc->vpi = vpi;
 359        vcc->vci = vci;
 360        __vcc_insert_socket(sk);
 361        write_unlock_irq(&vcc_sklist_lock);
 362        switch (vcc->qos.aal) {
 363        case ATM_AAL0:
 364                error = atm_init_aal0(vcc);
 365                vcc->stats = &dev->stats.aal0;
 366                break;
 367        case ATM_AAL34:
 368                error = atm_init_aal34(vcc);
 369                vcc->stats = &dev->stats.aal34;
 370                break;
 371        case ATM_NO_AAL:
 372                /* ATM_AAL5 is also used in the "0 for default" case */
 373                vcc->qos.aal = ATM_AAL5;
 374                /* fall through */
 375        case ATM_AAL5:
 376                error = atm_init_aal5(vcc);
 377                vcc->stats = &dev->stats.aal5;
 378                break;
 379        default:
 380                error = -EPROTOTYPE;
 381        }
 382        if (!error)
 383                error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
 384        if (!error)
 385                error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
 386        if (error)
 387                goto fail;
 388        pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
 389        pr_debug("  TX: %d, PCR %d..%d, SDU %d\n",
 390                 vcc->qos.txtp.traffic_class,
 391                 vcc->qos.txtp.min_pcr,
 392                 vcc->qos.txtp.max_pcr,
 393                 vcc->qos.txtp.max_sdu);
 394        pr_debug("  RX: %d, PCR %d..%d, SDU %d\n",
 395                 vcc->qos.rxtp.traffic_class,
 396                 vcc->qos.rxtp.min_pcr,
 397                 vcc->qos.rxtp.max_pcr,
 398                 vcc->qos.rxtp.max_sdu);
 399
 400        if (dev->ops->open) {
 401                error = dev->ops->open(vcc);
 402                if (error)
 403                        goto fail;
 404        }
 405        return 0;
 406
 407fail:
 408        vcc_remove_socket(sk);
 409fail_module_put:
 410        module_put(dev->ops->owner);
 411        /* ensure we get dev module ref count correct */
 412        vcc->dev = NULL;
 413        return error;
 414}
 415
 416int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
 417{
 418        struct atm_dev *dev;
 419        struct atm_vcc *vcc = ATM_SD(sock);
 420        int error;
 421
 422        pr_debug("(vpi %d, vci %d)\n", vpi, vci);
 423        if (sock->state == SS_CONNECTED)
 424                return -EISCONN;
 425        if (sock->state != SS_UNCONNECTED)
 426                return -EINVAL;
 427        if (!(vpi || vci))
 428                return -EINVAL;
 429
 430        if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
 431                clear_bit(ATM_VF_PARTIAL, &vcc->flags);
 432        else
 433                if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
 434                        return -EINVAL;
 435        pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
 436                 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
 437                 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
 438                 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
 439                 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
 440                 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
 441                 vcc->qos.aal == ATM_AAL5 ? "" :
 442                 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
 443                 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
 444        if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
 445                return -EBADFD;
 446        if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
 447            vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
 448                return -EINVAL;
 449        if (likely(itf != ATM_ITF_ANY)) {
 450                dev = try_then_request_module(atm_dev_lookup(itf),
 451                                              "atm-device-%d", itf);
 452        } else {
 453                dev = NULL;
 454                mutex_lock(&atm_dev_mutex);
 455                if (!list_empty(&atm_devs)) {
 456                        dev = list_entry(atm_devs.next,
 457                                         struct atm_dev, dev_list);
 458                        atm_dev_hold(dev);
 459                }
 460                mutex_unlock(&atm_dev_mutex);
 461        }
 462        if (!dev)
 463                return -ENODEV;
 464        error = __vcc_connect(vcc, dev, vpi, vci);
 465        if (error) {
 466                atm_dev_put(dev);
 467                return error;
 468        }
 469        if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
 470                set_bit(ATM_VF_PARTIAL, &vcc->flags);
 471        if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
 472                sock->state = SS_CONNECTED;
 473        return 0;
 474}
 475
 476int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
 477                size_t size, int flags)
 478{
 479        struct sock *sk = sock->sk;
 480        struct atm_vcc *vcc;
 481        struct sk_buff *skb;
 482        int copied, error = -EINVAL;
 483
 484        if (sock->state != SS_CONNECTED)
 485                return -ENOTCONN;
 486        if (flags & ~MSG_DONTWAIT)              /* only handle MSG_DONTWAIT */
 487                return -EOPNOTSUPP;
 488        vcc = ATM_SD(sock);
 489        if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
 490            test_bit(ATM_VF_CLOSE, &vcc->flags) ||
 491            !test_bit(ATM_VF_READY, &vcc->flags))
 492                return 0;
 493
 494        skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
 495        if (!skb)
 496                return error;
 497
 498        copied = skb->len;
 499        if (copied > size) {
 500                copied = size;
 501                msg->msg_flags |= MSG_TRUNC;
 502        }
 503
 504        error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
 505        if (error)
 506                return error;
 507        sock_recv_ts_and_drops(msg, sk, skb);
 508        pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
 509        atm_return(vcc, skb->truesize);
 510        skb_free_datagram(sk, skb);
 511        return copied;
 512}
 513
 514int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
 515                size_t total_len)
 516{
 517        struct sock *sk = sock->sk;
 518        DEFINE_WAIT(wait);
 519        struct atm_vcc *vcc;
 520        struct sk_buff *skb;
 521        int eff, error;
 522        const void __user *buff;
 523        int size;
 524
 525        lock_sock(sk);
 526        if (sock->state != SS_CONNECTED) {
 527                error = -ENOTCONN;
 528                goto out;
 529        }
 530        if (m->msg_name) {
 531                error = -EISCONN;
 532                goto out;
 533        }
 534        if (m->msg_iovlen != 1) {
 535                error = -ENOSYS; /* fix this later @@@ */
 536                goto out;
 537        }
 538        buff = m->msg_iov->iov_base;
 539        size = m->msg_iov->iov_len;
 540        vcc = ATM_SD(sock);
 541        if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
 542            test_bit(ATM_VF_CLOSE, &vcc->flags) ||
 543            !test_bit(ATM_VF_READY, &vcc->flags)) {
 544                error = -EPIPE;
 545                send_sig(SIGPIPE, current, 0);
 546                goto out;
 547        }
 548        if (!size) {
 549                error = 0;
 550                goto out;
 551        }
 552        if (size < 0 || size > vcc->qos.txtp.max_sdu) {
 553                error = -EMSGSIZE;
 554                goto out;
 555        }
 556
 557        eff = (size+3) & ~3; /* align to word boundary */
 558        prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
 559        error = 0;
 560        while (!(skb = alloc_tx(vcc, eff))) {
 561                if (m->msg_flags & MSG_DONTWAIT) {
 562                        error = -EAGAIN;
 563                        break;
 564                }
 565                schedule();
 566                if (signal_pending(current)) {
 567                        error = -ERESTARTSYS;
 568                        break;
 569                }
 570                if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
 571                    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
 572                    !test_bit(ATM_VF_READY, &vcc->flags)) {
 573                        error = -EPIPE;
 574                        send_sig(SIGPIPE, current, 0);
 575                        break;
 576                }
 577                prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
 578        }
 579        finish_wait(sk_sleep(sk), &wait);
 580        if (error)
 581                goto out;
 582        skb->dev = NULL; /* for paths shared with net_device interfaces */
 583        ATM_SKB(skb)->atm_options = vcc->atm_options;
 584        if (copy_from_user(skb_put(skb, size), buff, size)) {
 585                kfree_skb(skb);
 586                error = -EFAULT;
 587                goto out;
 588        }
 589        if (eff != size)
 590                memset(skb->data + size, 0, eff-size);
 591        error = vcc->dev->ops->send(vcc, skb);
 592        error = error ? error : size;
 593out:
 594        release_sock(sk);
 595        return error;
 596}
 597
 598unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
 599{
 600        struct sock *sk = sock->sk;
 601        struct atm_vcc *vcc;
 602        unsigned int mask;
 603
 604        sock_poll_wait(file, sk_sleep(sk), wait);
 605        mask = 0;
 606
 607        vcc = ATM_SD(sock);
 608
 609        /* exceptional events */
 610        if (sk->sk_err)
 611                mask = POLLERR;
 612
 613        if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
 614            test_bit(ATM_VF_CLOSE, &vcc->flags))
 615                mask |= POLLHUP;
 616
 617        /* readable? */
 618        if (!skb_queue_empty(&sk->sk_receive_queue))
 619                mask |= POLLIN | POLLRDNORM;
 620
 621        /* writable? */
 622        if (sock->state == SS_CONNECTING &&
 623            test_bit(ATM_VF_WAITING, &vcc->flags))
 624                return mask;
 625
 626        if (vcc->qos.txtp.traffic_class != ATM_NONE &&
 627            vcc_writable(sk))
 628                mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
 629
 630        return mask;
 631}
 632
 633static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
 634{
 635        int error;
 636
 637        /*
 638         * Don't let the QoS change the already connected AAL type nor the
 639         * traffic class.
 640         */
 641        if (qos->aal != vcc->qos.aal ||
 642            qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
 643            qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
 644                return -EINVAL;
 645        error = adjust_tp(&qos->txtp, qos->aal);
 646        if (!error)
 647                error = adjust_tp(&qos->rxtp, qos->aal);
 648        if (error)
 649                return error;
 650        if (!vcc->dev->ops->change_qos)
 651                return -EOPNOTSUPP;
 652        if (sk_atm(vcc)->sk_family == AF_ATMPVC)
 653                return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
 654        return svc_change_qos(vcc, qos);
 655}
 656
 657static int check_tp(const struct atm_trafprm *tp)
 658{
 659        /* @@@ Should be merged with adjust_tp */
 660        if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
 661                return 0;
 662        if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
 663            !tp->max_pcr)
 664                return -EINVAL;
 665        if (tp->min_pcr == ATM_MAX_PCR)
 666                return -EINVAL;
 667        if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
 668            tp->min_pcr > tp->max_pcr)
 669                return -EINVAL;
 670        /*
 671         * We allow pcr to be outside [min_pcr,max_pcr], because later
 672         * adjustment may still push it in the valid range.
 673         */
 674        return 0;
 675}
 676
 677static int check_qos(const struct atm_qos *qos)
 678{
 679        int error;
 680
 681        if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
 682                return -EINVAL;
 683        if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
 684            qos->txtp.traffic_class && qos->rxtp.traffic_class &&
 685            qos->txtp.traffic_class != ATM_ANYCLASS &&
 686            qos->rxtp.traffic_class != ATM_ANYCLASS)
 687                return -EINVAL;
 688        error = check_tp(&qos->txtp);
 689        if (error)
 690                return error;
 691        return check_tp(&qos->rxtp);
 692}
 693
 694int vcc_setsockopt(struct socket *sock, int level, int optname,
 695                   char __user *optval, unsigned int optlen)
 696{
 697        struct atm_vcc *vcc;
 698        unsigned long value;
 699        int error;
 700
 701        if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
 702                return -EINVAL;
 703
 704        vcc = ATM_SD(sock);
 705        switch (optname) {
 706        case SO_ATMQOS:
 707        {
 708                struct atm_qos qos;
 709
 710                if (copy_from_user(&qos, optval, sizeof(qos)))
 711                        return -EFAULT;
 712                error = check_qos(&qos);
 713                if (error)
 714                        return error;
 715                if (sock->state == SS_CONNECTED)
 716                        return atm_change_qos(vcc, &qos);
 717                if (sock->state != SS_UNCONNECTED)
 718                        return -EBADFD;
 719                vcc->qos = qos;
 720                set_bit(ATM_VF_HASQOS, &vcc->flags);
 721                return 0;
 722        }
 723        case SO_SETCLP:
 724                if (get_user(value, (unsigned long __user *)optval))
 725                        return -EFAULT;
 726                if (value)
 727                        vcc->atm_options |= ATM_ATMOPT_CLP;
 728                else
 729                        vcc->atm_options &= ~ATM_ATMOPT_CLP;
 730                return 0;
 731        default:
 732                if (level == SOL_SOCKET)
 733                        return -EINVAL;
 734                break;
 735        }
 736        if (!vcc->dev || !vcc->dev->ops->setsockopt)
 737                return -EINVAL;
 738        return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
 739}
 740
 741int vcc_getsockopt(struct socket *sock, int level, int optname,
 742                   char __user *optval, int __user *optlen)
 743{
 744        struct atm_vcc *vcc;
 745        int len;
 746
 747        if (get_user(len, optlen))
 748                return -EFAULT;
 749        if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
 750                return -EINVAL;
 751
 752        vcc = ATM_SD(sock);
 753        switch (optname) {
 754        case SO_ATMQOS:
 755                if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
 756                        return -EINVAL;
 757                return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
 758                        ? -EFAULT : 0;
 759        case SO_SETCLP:
 760                return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
 761                                (unsigned long __user *)optval) ? -EFAULT : 0;
 762        case SO_ATMPVC:
 763        {
 764                struct sockaddr_atmpvc pvc;
 765
 766                if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
 767                        return -ENOTCONN;
 768                pvc.sap_family = AF_ATMPVC;
 769                pvc.sap_addr.itf = vcc->dev->number;
 770                pvc.sap_addr.vpi = vcc->vpi;
 771                pvc.sap_addr.vci = vcc->vci;
 772                return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
 773        }
 774        default:
 775                if (level == SOL_SOCKET)
 776                        return -EINVAL;
 777                        break;
 778        }
 779        if (!vcc->dev || !vcc->dev->ops->getsockopt)
 780                return -EINVAL;
 781        return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
 782}
 783
 784static int __init atm_init(void)
 785{
 786        int error;
 787
 788        error = proto_register(&vcc_proto, 0);
 789        if (error < 0)
 790                goto out;
 791        error = atmpvc_init();
 792        if (error < 0) {
 793                pr_err("atmpvc_init() failed with %d\n", error);
 794                goto out_unregister_vcc_proto;
 795        }
 796        error = atmsvc_init();
 797        if (error < 0) {
 798                pr_err("atmsvc_init() failed with %d\n", error);
 799                goto out_atmpvc_exit;
 800        }
 801        error = atm_proc_init();
 802        if (error < 0) {
 803                pr_err("atm_proc_init() failed with %d\n", error);
 804                goto out_atmsvc_exit;
 805        }
 806        error = atm_sysfs_init();
 807        if (error < 0) {
 808                pr_err("atm_sysfs_init() failed with %d\n", error);
 809                goto out_atmproc_exit;
 810        }
 811out:
 812        return error;
 813out_atmproc_exit:
 814        atm_proc_exit();
 815out_atmsvc_exit:
 816        atmsvc_exit();
 817out_atmpvc_exit:
 818        atmsvc_exit();
 819out_unregister_vcc_proto:
 820        proto_unregister(&vcc_proto);
 821        goto out;
 822}
 823
 824static void __exit atm_exit(void)
 825{
 826        atm_proc_exit();
 827        atm_sysfs_exit();
 828        atmsvc_exit();
 829        atmpvc_exit();
 830        proto_unregister(&vcc_proto);
 831}
 832
 833subsys_initcall(atm_init);
 834
 835module_exit(atm_exit);
 836
 837MODULE_LICENSE("GPL");
 838MODULE_ALIAS_NETPROTO(PF_ATMPVC);
 839MODULE_ALIAS_NETPROTO(PF_ATMSVC);
 840