linux/drivers/isdn/mISDN/socket.c
<<
>>
Prefs
   1/*
   2 *
   3 * Author       Karsten Keil <kkeil@novell.com>
   4 *
   5 * Copyright 2008  by Karsten Keil <kkeil@novell.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 as
   9 * published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 *
  16 */
  17
  18#include <linux/mISDNif.h>
  19#include "core.h"
  20
  21static u_int    *debug;
  22
  23static struct proto mISDN_proto = {
  24        .name           = "misdn",
  25        .owner          = THIS_MODULE,
  26        .obj_size       = sizeof(struct mISDN_sock)
  27};
  28
  29#define _pms(sk)        ((struct mISDN_sock *)sk)
  30
  31static struct mISDN_sock_list   data_sockets = {
  32        .lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
  33};
  34
  35static struct mISDN_sock_list   base_sockets = {
  36        .lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
  37};
  38
  39#define L2_HEADER_LEN   4
  40
  41static inline struct sk_buff *
  42_l2_alloc_skb(unsigned int len, gfp_t gfp_mask)
  43{
  44        struct sk_buff  *skb;
  45
  46        skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
  47        if (likely(skb))
  48                skb_reserve(skb, L2_HEADER_LEN);
  49        return skb;
  50}
  51
  52static void
  53mISDN_sock_link(struct mISDN_sock_list *l, struct sock *sk)
  54{
  55        write_lock_bh(&l->lock);
  56        sk_add_node(sk, &l->head);
  57        write_unlock_bh(&l->lock);
  58}
  59
  60static void mISDN_sock_unlink(struct mISDN_sock_list *l, struct sock *sk)
  61{
  62        write_lock_bh(&l->lock);
  63        sk_del_node_init(sk);
  64        write_unlock_bh(&l->lock);
  65}
  66
  67static int
  68mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
  69{
  70        struct mISDN_sock *msk;
  71        int     err;
  72
  73        msk = container_of(ch, struct mISDN_sock, ch);
  74        if (*debug & DEBUG_SOCKET)
  75                printk(KERN_DEBUG "%s len %d %p\n", __func__, skb->len, skb);
  76        if (msk->sk.sk_state == MISDN_CLOSED)
  77                return -EUNATCH;
  78        __net_timestamp(skb);
  79        err = sock_queue_rcv_skb(&msk->sk, skb);
  80        if (err)
  81                printk(KERN_WARNING "%s: error %d\n", __func__, err);
  82        return err;
  83}
  84
  85static int
  86mISDN_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  87{
  88        struct mISDN_sock *msk;
  89
  90        msk = container_of(ch, struct mISDN_sock, ch);
  91        if (*debug & DEBUG_SOCKET)
  92                printk(KERN_DEBUG "%s(%p, %x, %p)\n", __func__, ch, cmd, arg);
  93        switch (cmd) {
  94        case CLOSE_CHANNEL:
  95                msk->sk.sk_state = MISDN_CLOSED;
  96                break;
  97        }
  98        return 0;
  99}
 100
 101static inline void
 102mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
 103{
 104        struct timeval  tv;
 105
 106        if (_pms(sk)->cmask & MISDN_TIME_STAMP) {
 107                skb_get_timestamp(skb, &tv);
 108                put_cmsg(msg, SOL_MISDN, MISDN_TIME_STAMP, sizeof(tv), &tv);
 109        }
 110}
 111
 112static int
 113mISDN_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
 114    struct msghdr *msg, size_t len, int flags)
 115{
 116        struct sk_buff          *skb;
 117        struct sock             *sk = sock->sk;
 118        struct sockaddr_mISDN   *maddr;
 119
 120        int             copied, err;
 121
 122        if (*debug & DEBUG_SOCKET)
 123                printk(KERN_DEBUG "%s: len %d, flags %x ch.nr %d, proto %x\n",
 124                        __func__, (int)len, flags, _pms(sk)->ch.nr,
 125                        sk->sk_protocol);
 126        if (flags & (MSG_OOB))
 127                return -EOPNOTSUPP;
 128
 129        if (sk->sk_state == MISDN_CLOSED)
 130                return 0;
 131
 132        skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
 133        if (!skb)
 134                return err;
 135
 136        if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
 137                msg->msg_namelen = sizeof(struct sockaddr_mISDN);
 138                maddr = (struct sockaddr_mISDN *)msg->msg_name;
 139                maddr->family = AF_ISDN;
 140                maddr->dev = _pms(sk)->dev->id;
 141                if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
 142                    (sk->sk_protocol == ISDN_P_LAPD_NT)) {
 143                        maddr->channel = (mISDN_HEAD_ID(skb) >> 16) & 0xff;
 144                        maddr->tei =  (mISDN_HEAD_ID(skb) >> 8) & 0xff;
 145                        maddr->sapi = mISDN_HEAD_ID(skb) & 0xff;
 146                } else {
 147                        maddr->channel = _pms(sk)->ch.nr;
 148                        maddr->sapi = _pms(sk)->ch.addr & 0xFF;
 149                        maddr->tei =  (_pms(sk)->ch.addr >> 8) & 0xFF;
 150                }
 151        } else {
 152                if (msg->msg_namelen)
 153                        printk(KERN_WARNING "%s: too small namelen %d\n",
 154                            __func__, msg->msg_namelen);
 155                msg->msg_namelen = 0;
 156        }
 157
 158        copied = skb->len + MISDN_HEADER_LEN;
 159        if (len < copied) {
 160                if (flags & MSG_PEEK)
 161                        atomic_dec(&skb->users);
 162                else
 163                        skb_queue_head(&sk->sk_receive_queue, skb);
 164                return -ENOSPC;
 165        }
 166        memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
 167            MISDN_HEADER_LEN);
 168
 169        err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
 170
 171        mISDN_sock_cmsg(sk, msg, skb);
 172
 173        skb_free_datagram(sk, skb);
 174
 175        return err ? : copied;
 176}
 177
 178static int
 179mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
 180    struct msghdr *msg, size_t len)
 181{
 182        struct sock             *sk = sock->sk;
 183        struct sk_buff          *skb;
 184        int                     err = -ENOMEM;
 185        struct sockaddr_mISDN   *maddr;
 186
 187        if (*debug & DEBUG_SOCKET)
 188                printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
 189                     __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
 190                     sk->sk_protocol);
 191
 192        if (msg->msg_flags & MSG_OOB)
 193                return -EOPNOTSUPP;
 194
 195        if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
 196                return -EINVAL;
 197
 198        if (len < MISDN_HEADER_LEN)
 199                return -EINVAL;
 200
 201        if (sk->sk_state != MISDN_BOUND)
 202                return -EBADFD;
 203
 204        lock_sock(sk);
 205
 206        skb = _l2_alloc_skb(len, GFP_KERNEL);
 207        if (!skb)
 208                goto done;
 209
 210        if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
 211                err = -EFAULT;
 212                goto done;
 213        }
 214
 215        memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
 216        skb_pull(skb, MISDN_HEADER_LEN);
 217
 218        if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
 219                /* if we have a address, we use it */
 220                maddr = (struct sockaddr_mISDN *)msg->msg_name;
 221                mISDN_HEAD_ID(skb) = maddr->channel;
 222        } else { /* use default for L2 messages */
 223                if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
 224                    (sk->sk_protocol == ISDN_P_LAPD_NT))
 225                        mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
 226        }
 227
 228        if (*debug & DEBUG_SOCKET)
 229                printk(KERN_DEBUG "%s: ID:%x\n",
 230                     __func__, mISDN_HEAD_ID(skb));
 231
 232        err = -ENODEV;
 233        if (!_pms(sk)->ch.peer)
 234                goto done;
 235        err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
 236        if (err)
 237                goto done;
 238        else {
 239                skb = NULL;
 240                err = len;
 241        }
 242
 243done:
 244        if (skb)
 245                kfree_skb(skb);
 246        release_sock(sk);
 247        return err;
 248}
 249
 250static int
 251data_sock_release(struct socket *sock)
 252{
 253        struct sock *sk = sock->sk;
 254
 255        if (*debug & DEBUG_SOCKET)
 256                printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
 257        if (!sk)
 258                return 0;
 259        switch (sk->sk_protocol) {
 260        case ISDN_P_TE_S0:
 261        case ISDN_P_NT_S0:
 262        case ISDN_P_TE_E1:
 263        case ISDN_P_NT_E1:
 264                if (sk->sk_state == MISDN_BOUND)
 265                        delete_channel(&_pms(sk)->ch);
 266                else
 267                        mISDN_sock_unlink(&data_sockets, sk);
 268                break;
 269        case ISDN_P_LAPD_TE:
 270        case ISDN_P_LAPD_NT:
 271        case ISDN_P_B_RAW:
 272        case ISDN_P_B_HDLC:
 273        case ISDN_P_B_X75SLP:
 274        case ISDN_P_B_L2DTMF:
 275        case ISDN_P_B_L2DSP:
 276        case ISDN_P_B_L2DSPHDLC:
 277                delete_channel(&_pms(sk)->ch);
 278                mISDN_sock_unlink(&data_sockets, sk);
 279                break;
 280        }
 281
 282        lock_sock(sk);
 283
 284        sock_orphan(sk);
 285        skb_queue_purge(&sk->sk_receive_queue);
 286
 287        release_sock(sk);
 288        sock_put(sk);
 289
 290        return 0;
 291}
 292
 293static int
 294data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
 295{
 296        struct mISDN_ctrl_req   cq;
 297        int                     err = -EINVAL, val[2];
 298        struct mISDNchannel     *bchan, *next;
 299
 300        lock_sock(sk);
 301        if (!_pms(sk)->dev) {
 302                err = -ENODEV;
 303                goto done;
 304        }
 305        switch (cmd) {
 306        case IMCTRLREQ:
 307                if (copy_from_user(&cq, p, sizeof(cq))) {
 308                        err = -EFAULT;
 309                        break;
 310                }
 311                if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
 312                        list_for_each_entry_safe(bchan, next,
 313                                &_pms(sk)->dev->bchannels, list) {
 314                                if (bchan->nr == cq.channel) {
 315                                        err = bchan->ctrl(bchan,
 316                                                CONTROL_CHANNEL, &cq);
 317                                        break;
 318                                }
 319                        }
 320                } else
 321                        err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
 322                                CONTROL_CHANNEL, &cq);
 323                if (err)
 324                        break;
 325                if (copy_to_user(p, &cq, sizeof(cq)))
 326                        err = -EFAULT;
 327                break;
 328        case IMCLEAR_L2:
 329                if (sk->sk_protocol != ISDN_P_LAPD_NT) {
 330                        err = -EINVAL;
 331                        break;
 332                }
 333                val[0] = cmd;
 334                if (get_user(val[1], (int __user *)p)) {
 335                        err = -EFAULT;
 336                        break;
 337                }
 338                err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
 339                    CONTROL_CHANNEL, val);
 340                break;
 341        case IMHOLD_L1:
 342                if (sk->sk_protocol != ISDN_P_LAPD_NT
 343                 && sk->sk_protocol != ISDN_P_LAPD_TE) {
 344                        err = -EINVAL;
 345                        break;
 346                }
 347                val[0] = cmd;
 348                if (get_user(val[1], (int __user *)p)) {
 349                        err = -EFAULT;
 350                        break;
 351                }
 352                err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
 353                    CONTROL_CHANNEL, val);
 354                break;
 355        default:
 356                err = -EINVAL;
 357                break;
 358        }
 359done:
 360        release_sock(sk);
 361        return err;
 362}
 363
 364static int
 365data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 366{
 367        int                     err = 0, id;
 368        struct sock             *sk = sock->sk;
 369        struct mISDNdevice      *dev;
 370        struct mISDNversion     ver;
 371
 372        switch (cmd) {
 373        case IMGETVERSION:
 374                ver.major = MISDN_MAJOR_VERSION;
 375                ver.minor = MISDN_MINOR_VERSION;
 376                ver.release = MISDN_RELEASE;
 377                if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
 378                        err = -EFAULT;
 379                break;
 380        case IMGETCOUNT:
 381                id = get_mdevice_count();
 382                if (put_user(id, (int __user *)arg))
 383                        err = -EFAULT;
 384                break;
 385        case IMGETDEVINFO:
 386                if (get_user(id, (int __user *)arg)) {
 387                        err = -EFAULT;
 388                        break;
 389                }
 390                dev = get_mdevice(id);
 391                if (dev) {
 392                        struct mISDN_devinfo di;
 393
 394                        di.id = dev->id;
 395                        di.Dprotocols = dev->Dprotocols;
 396                        di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
 397                        di.protocol = dev->D.protocol;
 398                        memcpy(di.channelmap, dev->channelmap,
 399                                sizeof(di.channelmap));
 400                        di.nrbchan = dev->nrbchan;
 401                        strcpy(di.name, dev_name(&dev->dev));
 402                        if (copy_to_user((void __user *)arg, &di, sizeof(di)))
 403                                err = -EFAULT;
 404                } else
 405                        err = -ENODEV;
 406                break;
 407        default:
 408                if (sk->sk_state == MISDN_BOUND)
 409                        err = data_sock_ioctl_bound(sk, cmd,
 410                                (void __user *)arg);
 411                else
 412                        err = -ENOTCONN;
 413        }
 414        return err;
 415}
 416
 417static int data_sock_setsockopt(struct socket *sock, int level, int optname,
 418        char __user *optval, unsigned int len)
 419{
 420        struct sock *sk = sock->sk;
 421        int err = 0, opt = 0;
 422
 423        if (*debug & DEBUG_SOCKET)
 424                printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
 425                    level, optname, optval, len);
 426
 427        lock_sock(sk);
 428
 429        switch (optname) {
 430        case MISDN_TIME_STAMP:
 431                if (get_user(opt, (int __user *)optval)) {
 432                        err = -EFAULT;
 433                        break;
 434                }
 435
 436                if (opt)
 437                        _pms(sk)->cmask |= MISDN_TIME_STAMP;
 438                else
 439                        _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
 440                break;
 441        default:
 442                err = -ENOPROTOOPT;
 443                break;
 444        }
 445        release_sock(sk);
 446        return err;
 447}
 448
 449static int data_sock_getsockopt(struct socket *sock, int level, int optname,
 450        char __user *optval, int __user *optlen)
 451{
 452        struct sock *sk = sock->sk;
 453        int len, opt;
 454
 455        if (get_user(len, optlen))
 456                return -EFAULT;
 457
 458        switch (optname) {
 459        case MISDN_TIME_STAMP:
 460                if (_pms(sk)->cmask & MISDN_TIME_STAMP)
 461                        opt = 1;
 462                else
 463                        opt = 0;
 464
 465                if (put_user(opt, optval))
 466                        return -EFAULT;
 467                break;
 468        default:
 469                return -ENOPROTOOPT;
 470        }
 471
 472        return 0;
 473}
 474
 475static int
 476data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
 477{
 478        struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
 479        struct sock *sk = sock->sk;
 480        struct hlist_node *node;
 481        struct sock *csk;
 482        int err = 0;
 483
 484        if (*debug & DEBUG_SOCKET)
 485                printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
 486        if (addr_len != sizeof(struct sockaddr_mISDN))
 487                return -EINVAL;
 488        if (!maddr || maddr->family != AF_ISDN)
 489                return -EINVAL;
 490
 491        lock_sock(sk);
 492
 493        if (_pms(sk)->dev) {
 494                err = -EALREADY;
 495                goto done;
 496        }
 497        _pms(sk)->dev = get_mdevice(maddr->dev);
 498        if (!_pms(sk)->dev) {
 499                err = -ENODEV;
 500                goto done;
 501        }
 502
 503        if (sk->sk_protocol < ISDN_P_B_START) {
 504                read_lock_bh(&data_sockets.lock);
 505                sk_for_each(csk, node, &data_sockets.head) {
 506                        if (sk == csk)
 507                                continue;
 508                        if (_pms(csk)->dev != _pms(sk)->dev)
 509                                continue;
 510                        if (csk->sk_protocol >= ISDN_P_B_START)
 511                                continue;
 512                        if (IS_ISDN_P_TE(csk->sk_protocol)
 513                                        == IS_ISDN_P_TE(sk->sk_protocol))
 514                                continue;
 515                        read_unlock_bh(&data_sockets.lock);
 516                        err = -EBUSY;
 517                        goto done;
 518                }
 519                read_unlock_bh(&data_sockets.lock);
 520        }
 521
 522        _pms(sk)->ch.send = mISDN_send;
 523        _pms(sk)->ch.ctrl = mISDN_ctrl;
 524
 525        switch (sk->sk_protocol) {
 526        case ISDN_P_TE_S0:
 527        case ISDN_P_NT_S0:
 528        case ISDN_P_TE_E1:
 529        case ISDN_P_NT_E1:
 530                mISDN_sock_unlink(&data_sockets, sk);
 531                err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
 532                    sk->sk_protocol, maddr);
 533                if (err)
 534                        mISDN_sock_link(&data_sockets, sk);
 535                break;
 536        case ISDN_P_LAPD_TE:
 537        case ISDN_P_LAPD_NT:
 538                err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
 539                    sk->sk_protocol, maddr);
 540                break;
 541        case ISDN_P_B_RAW:
 542        case ISDN_P_B_HDLC:
 543        case ISDN_P_B_X75SLP:
 544        case ISDN_P_B_L2DTMF:
 545        case ISDN_P_B_L2DSP:
 546        case ISDN_P_B_L2DSPHDLC:
 547                err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
 548                    sk->sk_protocol, maddr);
 549                break;
 550        default:
 551                err = -EPROTONOSUPPORT;
 552        }
 553        if (err)
 554                goto done;
 555        sk->sk_state = MISDN_BOUND;
 556        _pms(sk)->ch.protocol = sk->sk_protocol;
 557
 558done:
 559        release_sock(sk);
 560        return err;
 561}
 562
 563static int
 564data_sock_getname(struct socket *sock, struct sockaddr *addr,
 565    int *addr_len, int peer)
 566{
 567        struct sockaddr_mISDN   *maddr = (struct sockaddr_mISDN *) addr;
 568        struct sock             *sk = sock->sk;
 569
 570        if (!_pms(sk)->dev)
 571                return -EBADFD;
 572
 573        lock_sock(sk);
 574
 575        *addr_len = sizeof(*maddr);
 576        maddr->dev = _pms(sk)->dev->id;
 577        maddr->channel = _pms(sk)->ch.nr;
 578        maddr->sapi = _pms(sk)->ch.addr & 0xff;
 579        maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
 580        release_sock(sk);
 581        return 0;
 582}
 583
 584static const struct proto_ops data_sock_ops = {
 585        .family         = PF_ISDN,
 586        .owner          = THIS_MODULE,
 587        .release        = data_sock_release,
 588        .ioctl          = data_sock_ioctl,
 589        .bind           = data_sock_bind,
 590        .getname        = data_sock_getname,
 591        .sendmsg        = mISDN_sock_sendmsg,
 592        .recvmsg        = mISDN_sock_recvmsg,
 593        .poll           = datagram_poll,
 594        .listen         = sock_no_listen,
 595        .shutdown       = sock_no_shutdown,
 596        .setsockopt     = data_sock_setsockopt,
 597        .getsockopt     = data_sock_getsockopt,
 598        .connect        = sock_no_connect,
 599        .socketpair     = sock_no_socketpair,
 600        .accept         = sock_no_accept,
 601        .mmap           = sock_no_mmap
 602};
 603
 604static int
 605data_sock_create(struct net *net, struct socket *sock, int protocol)
 606{
 607        struct sock *sk;
 608
 609        if (sock->type != SOCK_DGRAM)
 610                return -ESOCKTNOSUPPORT;
 611
 612        sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
 613        if (!sk)
 614                return -ENOMEM;
 615
 616        sock_init_data(sock, sk);
 617
 618        sock->ops = &data_sock_ops;
 619        sock->state = SS_UNCONNECTED;
 620        sock_reset_flag(sk, SOCK_ZAPPED);
 621
 622        sk->sk_protocol = protocol;
 623        sk->sk_state    = MISDN_OPEN;
 624        mISDN_sock_link(&data_sockets, sk);
 625
 626        return 0;
 627}
 628
 629static int
 630base_sock_release(struct socket *sock)
 631{
 632        struct sock *sk = sock->sk;
 633
 634        printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
 635        if (!sk)
 636                return 0;
 637
 638        mISDN_sock_unlink(&base_sockets, sk);
 639        sock_orphan(sk);
 640        sock_put(sk);
 641
 642        return 0;
 643}
 644
 645static int
 646base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 647{
 648        int                     err = 0, id;
 649        struct mISDNdevice      *dev;
 650        struct mISDNversion     ver;
 651
 652        switch (cmd) {
 653        case IMGETVERSION:
 654                ver.major = MISDN_MAJOR_VERSION;
 655                ver.minor = MISDN_MINOR_VERSION;
 656                ver.release = MISDN_RELEASE;
 657                if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
 658                        err = -EFAULT;
 659                break;
 660        case IMGETCOUNT:
 661                id = get_mdevice_count();
 662                if (put_user(id, (int __user *)arg))
 663                        err = -EFAULT;
 664                break;
 665        case IMGETDEVINFO:
 666                if (get_user(id, (int __user *)arg)) {
 667                        err = -EFAULT;
 668                        break;
 669                }
 670                dev = get_mdevice(id);
 671                if (dev) {
 672                        struct mISDN_devinfo di;
 673
 674                        di.id = dev->id;
 675                        di.Dprotocols = dev->Dprotocols;
 676                        di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
 677                        di.protocol = dev->D.protocol;
 678                        memcpy(di.channelmap, dev->channelmap,
 679                                sizeof(di.channelmap));
 680                        di.nrbchan = dev->nrbchan;
 681                        strcpy(di.name, dev_name(&dev->dev));
 682                        if (copy_to_user((void __user *)arg, &di, sizeof(di)))
 683                                err = -EFAULT;
 684                } else
 685                        err = -ENODEV;
 686                break;
 687        case IMSETDEVNAME:
 688                {
 689                        struct mISDN_devrename dn;
 690                        if (copy_from_user(&dn, (void __user *)arg,
 691                            sizeof(dn))) {
 692                                err = -EFAULT;
 693                                break;
 694                        }
 695                        dev = get_mdevice(dn.id);
 696                        if (dev)
 697                                err = device_rename(&dev->dev, dn.name);
 698                        else
 699                                err = -ENODEV;
 700                }
 701                break;
 702        default:
 703                err = -EINVAL;
 704        }
 705        return err;
 706}
 707
 708static int
 709base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
 710{
 711        struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
 712        struct sock *sk = sock->sk;
 713        int err = 0;
 714
 715        if (!maddr || maddr->family != AF_ISDN)
 716                return -EINVAL;
 717
 718        lock_sock(sk);
 719
 720        if (_pms(sk)->dev) {
 721                err = -EALREADY;
 722                goto done;
 723        }
 724
 725        _pms(sk)->dev = get_mdevice(maddr->dev);
 726        if (!_pms(sk)->dev) {
 727                err = -ENODEV;
 728                goto done;
 729        }
 730        sk->sk_state = MISDN_BOUND;
 731
 732done:
 733        release_sock(sk);
 734        return err;
 735}
 736
 737static const struct proto_ops base_sock_ops = {
 738        .family         = PF_ISDN,
 739        .owner          = THIS_MODULE,
 740        .release        = base_sock_release,
 741        .ioctl          = base_sock_ioctl,
 742        .bind           = base_sock_bind,
 743        .getname        = sock_no_getname,
 744        .sendmsg        = sock_no_sendmsg,
 745        .recvmsg        = sock_no_recvmsg,
 746        .poll           = sock_no_poll,
 747        .listen         = sock_no_listen,
 748        .shutdown       = sock_no_shutdown,
 749        .setsockopt     = sock_no_setsockopt,
 750        .getsockopt     = sock_no_getsockopt,
 751        .connect        = sock_no_connect,
 752        .socketpair     = sock_no_socketpair,
 753        .accept         = sock_no_accept,
 754        .mmap           = sock_no_mmap
 755};
 756
 757
 758static int
 759base_sock_create(struct net *net, struct socket *sock, int protocol)
 760{
 761        struct sock *sk;
 762
 763        if (sock->type != SOCK_RAW)
 764                return -ESOCKTNOSUPPORT;
 765
 766        sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
 767        if (!sk)
 768                return -ENOMEM;
 769
 770        sock_init_data(sock, sk);
 771        sock->ops = &base_sock_ops;
 772        sock->state = SS_UNCONNECTED;
 773        sock_reset_flag(sk, SOCK_ZAPPED);
 774        sk->sk_protocol = protocol;
 775        sk->sk_state    = MISDN_OPEN;
 776        mISDN_sock_link(&base_sockets, sk);
 777
 778        return 0;
 779}
 780
 781static int
 782mISDN_sock_create(struct net *net, struct socket *sock, int proto)
 783{
 784        int err = -EPROTONOSUPPORT;
 785
 786        switch  (proto) {
 787        case ISDN_P_BASE:
 788                err = base_sock_create(net, sock, proto);
 789                break;
 790        case ISDN_P_TE_S0:
 791        case ISDN_P_NT_S0:
 792        case ISDN_P_TE_E1:
 793        case ISDN_P_NT_E1:
 794        case ISDN_P_LAPD_TE:
 795        case ISDN_P_LAPD_NT:
 796        case ISDN_P_B_RAW:
 797        case ISDN_P_B_HDLC:
 798        case ISDN_P_B_X75SLP:
 799        case ISDN_P_B_L2DTMF:
 800        case ISDN_P_B_L2DSP:
 801        case ISDN_P_B_L2DSPHDLC:
 802                err = data_sock_create(net, sock, proto);
 803                break;
 804        default:
 805                return err;
 806        }
 807
 808        return err;
 809}
 810
 811static struct
 812net_proto_family mISDN_sock_family_ops = {
 813        .owner  = THIS_MODULE,
 814        .family = PF_ISDN,
 815        .create = mISDN_sock_create,
 816};
 817
 818int
 819misdn_sock_init(u_int *deb)
 820{
 821        int err;
 822
 823        debug = deb;
 824        err = sock_register(&mISDN_sock_family_ops);
 825        if (err)
 826                printk(KERN_ERR "%s: error(%d)\n", __func__, err);
 827        return err;
 828}
 829
 830void
 831misdn_sock_cleanup(void)
 832{
 833        sock_unregister(PF_ISDN);
 834}
 835
 836