linux/net/decnet/af_decnet.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2
   3/*
   4 * DECnet       An implementation of the DECnet protocol suite for the LINUX
   5 *              operating system.  DECnet is implemented using the  BSD Socket
   6 *              interface as the means of communication with the user level.
   7 *
   8 *              DECnet Socket Layer Interface
   9 *
  10 * Authors:     Eduardo Marcelo Serrat <emserrat@geocities.com>
  11 *              Patrick Caulfield <patrick@pandh.demon.co.uk>
  12 *
  13 * Changes:
  14 *        Steve Whitehouse: Copied from Eduardo Serrat and Patrick Caulfield's
  15 *                          version of the code. Original copyright preserved
  16 *                          below.
  17 *        Steve Whitehouse: Some bug fixes, cleaning up some code to make it
  18 *                          compatible with my routing layer.
  19 *        Steve Whitehouse: Merging changes from Eduardo Serrat and Patrick
  20 *                          Caulfield.
  21 *        Steve Whitehouse: Further bug fixes, checking module code still works
  22 *                          with new routing layer.
  23 *        Steve Whitehouse: Additional set/get_sockopt() calls.
  24 *        Steve Whitehouse: Fixed TIOCINQ ioctl to be same as Eduardo's new
  25 *                          code.
  26 *        Steve Whitehouse: recvmsg() changed to try and behave in a POSIX like
  27 *                          way. Didn't manage it entirely, but its better.
  28 *        Steve Whitehouse: ditto for sendmsg().
  29 *        Steve Whitehouse: A selection of bug fixes to various things.
  30 *        Steve Whitehouse: Added TIOCOUTQ ioctl.
  31 *        Steve Whitehouse: Fixes to username2sockaddr & sockaddr2username.
  32 *        Steve Whitehouse: Fixes to connect() error returns.
  33 *       Patrick Caulfield: Fixes to delayed acceptance logic.
  34 *         David S. Miller: New socket locking
  35 *        Steve Whitehouse: Socket list hashing/locking
  36 *         Arnaldo C. Melo: use capable, not suser
  37 *        Steve Whitehouse: Removed unused code. Fix to use sk->allocation
  38 *                          when required.
  39 *       Patrick Caulfield: /proc/net/decnet now has object name/number
  40 *        Steve Whitehouse: Fixed local port allocation, hashed sk list
  41 *          Matthew Wilcox: Fixes for dn_ioctl()
  42 *        Steve Whitehouse: New connect/accept logic to allow timeouts and
  43 *                          prepare for sendpage etc.
  44 */
  45
  46
  47/******************************************************************************
  48    (c) 1995-1998 E.M. Serrat           emserrat@geocities.com
  49
  50
  51HISTORY:
  52
  53Version           Kernel     Date       Author/Comments
  54-------           ------     ----       ---------------
  55Version 0.0.1     2.0.30    01-dic-97   Eduardo Marcelo Serrat
  56                                        (emserrat@geocities.com)
  57
  58                                        First Development of DECnet Socket La-
  59                                        yer for Linux. Only supports outgoing
  60                                        connections.
  61
  62Version 0.0.2     2.1.105   20-jun-98   Patrick J. Caulfield
  63                                        (patrick@pandh.demon.co.uk)
  64
  65                                        Port to new kernel development version.
  66
  67Version 0.0.3     2.1.106   25-jun-98   Eduardo Marcelo Serrat
  68                                        (emserrat@geocities.com)
  69                                        _
  70                                        Added support for incoming connections
  71                                        so we can start developing server apps
  72                                        on Linux.
  73                                        -
  74                                        Module Support
  75Version 0.0.4     2.1.109   21-jul-98   Eduardo Marcelo Serrat
  76                                       (emserrat@geocities.com)
  77                                       _
  78                                        Added support for X11R6.4. Now we can
  79                                        use DECnet transport for X on Linux!!!
  80                                       -
  81Version 0.0.5    2.1.110   01-aug-98   Eduardo Marcelo Serrat
  82                                       (emserrat@geocities.com)
  83                                       Removed bugs on flow control
  84                                       Removed bugs on incoming accessdata
  85                                       order
  86                                       -
  87Version 0.0.6    2.1.110   07-aug-98   Eduardo Marcelo Serrat
  88                                       dn_recvmsg fixes
  89
  90                                        Patrick J. Caulfield
  91                                       dn_bind fixes
  92*******************************************************************************/
  93
  94#include <linux/module.h>
  95#include <linux/errno.h>
  96#include <linux/types.h>
  97#include <linux/slab.h>
  98#include <linux/socket.h>
  99#include <linux/in.h>
 100#include <linux/kernel.h>
 101#include <linux/sched/signal.h>
 102#include <linux/timer.h>
 103#include <linux/string.h>
 104#include <linux/sockios.h>
 105#include <linux/net.h>
 106#include <linux/netdevice.h>
 107#include <linux/inet.h>
 108#include <linux/route.h>
 109#include <linux/netfilter.h>
 110#include <linux/seq_file.h>
 111#include <net/sock.h>
 112#include <net/tcp_states.h>
 113#include <net/flow.h>
 114#include <asm/ioctls.h>
 115#include <linux/capability.h>
 116#include <linux/mm.h>
 117#include <linux/interrupt.h>
 118#include <linux/proc_fs.h>
 119#include <linux/stat.h>
 120#include <linux/init.h>
 121#include <linux/poll.h>
 122#include <linux/jiffies.h>
 123#include <net/net_namespace.h>
 124#include <net/neighbour.h>
 125#include <net/dst.h>
 126#include <net/fib_rules.h>
 127#include <net/tcp.h>
 128#include <net/dn.h>
 129#include <net/dn_nsp.h>
 130#include <net/dn_dev.h>
 131#include <net/dn_route.h>
 132#include <net/dn_fib.h>
 133#include <net/dn_neigh.h>
 134
 135struct dn_sock {
 136        struct sock sk;
 137        struct dn_scp scp;
 138};
 139
 140static void dn_keepalive(struct sock *sk);
 141
 142#define DN_SK_HASH_SHIFT 8
 143#define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
 144#define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
 145
 146
 147static const struct proto_ops dn_proto_ops;
 148static DEFINE_RWLOCK(dn_hash_lock);
 149static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
 150static struct hlist_head dn_wild_sk;
 151static atomic_long_t decnet_memory_allocated;
 152
 153static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen, int flags);
 154static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);
 155
 156static struct hlist_head *dn_find_list(struct sock *sk)
 157{
 158        struct dn_scp *scp = DN_SK(sk);
 159
 160        if (scp->addr.sdn_flags & SDF_WILD)
 161                return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL;
 162
 163        return &dn_sk_hash[le16_to_cpu(scp->addrloc) & DN_SK_HASH_MASK];
 164}
 165
 166/*
 167 * Valid ports are those greater than zero and not already in use.
 168 */
 169static int check_port(__le16 port)
 170{
 171        struct sock *sk;
 172
 173        if (port == 0)
 174                return -1;
 175
 176        sk_for_each(sk, &dn_sk_hash[le16_to_cpu(port) & DN_SK_HASH_MASK]) {
 177                struct dn_scp *scp = DN_SK(sk);
 178                if (scp->addrloc == port)
 179                        return -1;
 180        }
 181        return 0;
 182}
 183
 184static unsigned short port_alloc(struct sock *sk)
 185{
 186        struct dn_scp *scp = DN_SK(sk);
 187        static unsigned short port = 0x2000;
 188        unsigned short i_port = port;
 189
 190        while(check_port(cpu_to_le16(++port)) != 0) {
 191                if (port == i_port)
 192                        return 0;
 193        }
 194
 195        scp->addrloc = cpu_to_le16(port);
 196
 197        return 1;
 198}
 199
 200/*
 201 * Since this is only ever called from user
 202 * level, we don't need a write_lock() version
 203 * of this.
 204 */
 205static int dn_hash_sock(struct sock *sk)
 206{
 207        struct dn_scp *scp = DN_SK(sk);
 208        struct hlist_head *list;
 209        int rv = -EUSERS;
 210
 211        BUG_ON(sk_hashed(sk));
 212
 213        write_lock_bh(&dn_hash_lock);
 214
 215        if (!scp->addrloc && !port_alloc(sk))
 216                goto out;
 217
 218        rv = -EADDRINUSE;
 219        if ((list = dn_find_list(sk)) == NULL)
 220                goto out;
 221
 222        sk_add_node(sk, list);
 223        rv = 0;
 224out:
 225        write_unlock_bh(&dn_hash_lock);
 226        return rv;
 227}
 228
 229static void dn_unhash_sock(struct sock *sk)
 230{
 231        write_lock(&dn_hash_lock);
 232        sk_del_node_init(sk);
 233        write_unlock(&dn_hash_lock);
 234}
 235
 236static void dn_unhash_sock_bh(struct sock *sk)
 237{
 238        write_lock_bh(&dn_hash_lock);
 239        sk_del_node_init(sk);
 240        write_unlock_bh(&dn_hash_lock);
 241}
 242
 243static struct hlist_head *listen_hash(struct sockaddr_dn *addr)
 244{
 245        int i;
 246        unsigned int hash = addr->sdn_objnum;
 247
 248        if (hash == 0) {
 249                hash = addr->sdn_objnamel;
 250                for(i = 0; i < le16_to_cpu(addr->sdn_objnamel); i++) {
 251                        hash ^= addr->sdn_objname[i];
 252                        hash ^= (hash << 3);
 253                }
 254        }
 255
 256        return &dn_sk_hash[hash & DN_SK_HASH_MASK];
 257}
 258
 259/*
 260 * Called to transform a socket from bound (i.e. with a local address)
 261 * into a listening socket (doesn't need a local port number) and rehashes
 262 * based upon the object name/number.
 263 */
 264static void dn_rehash_sock(struct sock *sk)
 265{
 266        struct hlist_head *list;
 267        struct dn_scp *scp = DN_SK(sk);
 268
 269        if (scp->addr.sdn_flags & SDF_WILD)
 270                return;
 271
 272        write_lock_bh(&dn_hash_lock);
 273        sk_del_node_init(sk);
 274        DN_SK(sk)->addrloc = 0;
 275        list = listen_hash(&DN_SK(sk)->addr);
 276        sk_add_node(sk, list);
 277        write_unlock_bh(&dn_hash_lock);
 278}
 279
 280int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
 281{
 282        int len = 2;
 283
 284        *buf++ = type;
 285
 286        switch (type) {
 287        case 0:
 288                *buf++ = sdn->sdn_objnum;
 289                break;
 290        case 1:
 291                *buf++ = 0;
 292                *buf++ = le16_to_cpu(sdn->sdn_objnamel);
 293                memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
 294                len = 3 + le16_to_cpu(sdn->sdn_objnamel);
 295                break;
 296        case 2:
 297                memset(buf, 0, 5);
 298                buf += 5;
 299                *buf++ = le16_to_cpu(sdn->sdn_objnamel);
 300                memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
 301                len = 7 + le16_to_cpu(sdn->sdn_objnamel);
 302                break;
 303        }
 304
 305        return len;
 306}
 307
 308/*
 309 * On reception of usernames, we handle types 1 and 0 for destination
 310 * addresses only. Types 2 and 4 are used for source addresses, but the
 311 * UIC, GIC are ignored and they are both treated the same way. Type 3
 312 * is never used as I've no idea what its purpose might be or what its
 313 * format is.
 314 */
 315int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
 316{
 317        unsigned char type;
 318        int size = len;
 319        int namel = 12;
 320
 321        sdn->sdn_objnum = 0;
 322        sdn->sdn_objnamel = cpu_to_le16(0);
 323        memset(sdn->sdn_objname, 0, DN_MAXOBJL);
 324
 325        if (len < 2)
 326                return -1;
 327
 328        len -= 2;
 329        *fmt = *data++;
 330        type = *data++;
 331
 332        switch (*fmt) {
 333        case 0:
 334                sdn->sdn_objnum = type;
 335                return 2;
 336        case 1:
 337                namel = 16;
 338                break;
 339        case 2:
 340                len  -= 4;
 341                data += 4;
 342                break;
 343        case 4:
 344                len  -= 8;
 345                data += 8;
 346                break;
 347        default:
 348                return -1;
 349        }
 350
 351        len -= 1;
 352
 353        if (len < 0)
 354                return -1;
 355
 356        sdn->sdn_objnamel = cpu_to_le16(*data++);
 357        len -= le16_to_cpu(sdn->sdn_objnamel);
 358
 359        if ((len < 0) || (le16_to_cpu(sdn->sdn_objnamel) > namel))
 360                return -1;
 361
 362        memcpy(sdn->sdn_objname, data, le16_to_cpu(sdn->sdn_objnamel));
 363
 364        return size - len;
 365}
 366
 367struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
 368{
 369        struct hlist_head *list = listen_hash(addr);
 370        struct sock *sk;
 371
 372        read_lock(&dn_hash_lock);
 373        sk_for_each(sk, list) {
 374                struct dn_scp *scp = DN_SK(sk);
 375                if (sk->sk_state != TCP_LISTEN)
 376                        continue;
 377                if (scp->addr.sdn_objnum) {
 378                        if (scp->addr.sdn_objnum != addr->sdn_objnum)
 379                                continue;
 380                } else {
 381                        if (addr->sdn_objnum)
 382                                continue;
 383                        if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
 384                                continue;
 385                        if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, le16_to_cpu(addr->sdn_objnamel)) != 0)
 386                                continue;
 387                }
 388                sock_hold(sk);
 389                read_unlock(&dn_hash_lock);
 390                return sk;
 391        }
 392
 393        sk = sk_head(&dn_wild_sk);
 394        if (sk) {
 395                if (sk->sk_state == TCP_LISTEN)
 396                        sock_hold(sk);
 397                else
 398                        sk = NULL;
 399        }
 400
 401        read_unlock(&dn_hash_lock);
 402        return sk;
 403}
 404
 405struct sock *dn_find_by_skb(struct sk_buff *skb)
 406{
 407        struct dn_skb_cb *cb = DN_SKB_CB(skb);
 408        struct sock *sk;
 409        struct dn_scp *scp;
 410
 411        read_lock(&dn_hash_lock);
 412        sk_for_each(sk, &dn_sk_hash[le16_to_cpu(cb->dst_port) & DN_SK_HASH_MASK]) {
 413                scp = DN_SK(sk);
 414                if (cb->src != dn_saddr2dn(&scp->peer))
 415                        continue;
 416                if (cb->dst_port != scp->addrloc)
 417                        continue;
 418                if (scp->addrrem && (cb->src_port != scp->addrrem))
 419                        continue;
 420                sock_hold(sk);
 421                goto found;
 422        }
 423        sk = NULL;
 424found:
 425        read_unlock(&dn_hash_lock);
 426        return sk;
 427}
 428
 429
 430
 431static void dn_destruct(struct sock *sk)
 432{
 433        struct dn_scp *scp = DN_SK(sk);
 434
 435        skb_queue_purge(&scp->data_xmit_queue);
 436        skb_queue_purge(&scp->other_xmit_queue);
 437        skb_queue_purge(&scp->other_receive_queue);
 438
 439        dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1));
 440}
 441
 442static unsigned long dn_memory_pressure;
 443
 444static void dn_enter_memory_pressure(struct sock *sk)
 445{
 446        if (!dn_memory_pressure) {
 447                dn_memory_pressure = 1;
 448        }
 449}
 450
 451static struct proto dn_proto = {
 452        .name                   = "NSP",
 453        .owner                  = THIS_MODULE,
 454        .enter_memory_pressure  = dn_enter_memory_pressure,
 455        .memory_pressure        = &dn_memory_pressure,
 456        .memory_allocated       = &decnet_memory_allocated,
 457        .sysctl_mem             = sysctl_decnet_mem,
 458        .sysctl_wmem            = sysctl_decnet_wmem,
 459        .sysctl_rmem            = sysctl_decnet_rmem,
 460        .max_header             = DN_MAX_NSP_DATA_HEADER + 64,
 461        .obj_size               = sizeof(struct dn_sock),
 462};
 463
 464static struct sock *dn_alloc_sock(struct net *net, struct socket *sock, gfp_t gfp, int kern)
 465{
 466        struct dn_scp *scp;
 467        struct sock *sk = sk_alloc(net, PF_DECnet, gfp, &dn_proto, kern);
 468
 469        if  (!sk)
 470                goto out;
 471
 472        if (sock)
 473                sock->ops = &dn_proto_ops;
 474        sock_init_data(sock, sk);
 475
 476        sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
 477        sk->sk_destruct    = dn_destruct;
 478        sk->sk_no_check_tx = 1;
 479        sk->sk_family      = PF_DECnet;
 480        sk->sk_protocol    = 0;
 481        sk->sk_allocation  = gfp;
 482        sk->sk_sndbuf      = sysctl_decnet_wmem[1];
 483        sk->sk_rcvbuf      = sysctl_decnet_rmem[1];
 484
 485        /* Initialization of DECnet Session Control Port                */
 486        scp = DN_SK(sk);
 487        scp->state      = DN_O;         /* Open                 */
 488        scp->numdat     = 1;            /* Next data seg to tx  */
 489        scp->numoth     = 1;            /* Next oth data to tx  */
 490        scp->ackxmt_dat = 0;            /* Last data seg ack'ed */
 491        scp->ackxmt_oth = 0;            /* Last oth data ack'ed */
 492        scp->ackrcv_dat = 0;            /* Highest data ack recv*/
 493        scp->ackrcv_oth = 0;            /* Last oth data ack rec*/
 494        scp->flowrem_sw = DN_SEND;
 495        scp->flowloc_sw = DN_SEND;
 496        scp->flowrem_dat = 0;
 497        scp->flowrem_oth = 1;
 498        scp->flowloc_dat = 0;
 499        scp->flowloc_oth = 1;
 500        scp->services_rem = 0;
 501        scp->services_loc = 1 | NSP_FC_NONE;
 502        scp->info_rem = 0;
 503        scp->info_loc = 0x03; /* NSP version 4.1 */
 504        scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
 505        scp->nonagle = 0;
 506        scp->multi_ireq = 1;
 507        scp->accept_mode = ACC_IMMED;
 508        scp->addr.sdn_family    = AF_DECnet;
 509        scp->peer.sdn_family    = AF_DECnet;
 510        scp->accessdata.acc_accl = 5;
 511        memcpy(scp->accessdata.acc_acc, "LINUX", 5);
 512
 513        scp->max_window   = NSP_MAX_WINDOW;
 514        scp->snd_window   = NSP_MIN_WINDOW;
 515        scp->nsp_srtt     = NSP_INITIAL_SRTT;
 516        scp->nsp_rttvar   = NSP_INITIAL_RTTVAR;
 517        scp->nsp_rxtshift = 0;
 518
 519        skb_queue_head_init(&scp->data_xmit_queue);
 520        skb_queue_head_init(&scp->other_xmit_queue);
 521        skb_queue_head_init(&scp->other_receive_queue);
 522
 523        scp->persist = 0;
 524        scp->persist_fxn = NULL;
 525        scp->keepalive = 10 * HZ;
 526        scp->keepalive_fxn = dn_keepalive;
 527
 528        dn_start_slow_timer(sk);
 529out:
 530        return sk;
 531}
 532
 533/*
 534 * Keepalive timer.
 535 * FIXME: Should respond to SO_KEEPALIVE etc.
 536 */
 537static void dn_keepalive(struct sock *sk)
 538{
 539        struct dn_scp *scp = DN_SK(sk);
 540
 541        /*
 542         * By checking the other_data transmit queue is empty
 543         * we are double checking that we are not sending too
 544         * many of these keepalive frames.
 545         */
 546        if (skb_queue_empty(&scp->other_xmit_queue))
 547                dn_nsp_send_link(sk, DN_NOCHANGE, 0);
 548}
 549
 550
 551/*
 552 * Timer for shutdown/destroyed sockets.
 553 * When socket is dead & no packets have been sent for a
 554 * certain amount of time, they are removed by this
 555 * routine. Also takes care of sending out DI & DC
 556 * frames at correct times.
 557 */
 558int dn_destroy_timer(struct sock *sk)
 559{
 560        struct dn_scp *scp = DN_SK(sk);
 561
 562        scp->persist = dn_nsp_persist(sk);
 563
 564        switch (scp->state) {
 565        case DN_DI:
 566                dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
 567                if (scp->nsp_rxtshift >= decnet_di_count)
 568                        scp->state = DN_CN;
 569                return 0;
 570
 571        case DN_DR:
 572                dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
 573                if (scp->nsp_rxtshift >= decnet_dr_count)
 574                        scp->state = DN_DRC;
 575                return 0;
 576
 577        case DN_DN:
 578                if (scp->nsp_rxtshift < decnet_dn_count) {
 579                        /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
 580                        dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
 581                                         GFP_ATOMIC);
 582                        return 0;
 583                }
 584        }
 585
 586        scp->persist = (HZ * decnet_time_wait);
 587
 588        if (sk->sk_socket)
 589                return 0;
 590
 591        if (time_after_eq(jiffies, scp->stamp + HZ * decnet_time_wait)) {
 592                dn_unhash_sock(sk);
 593                sock_put(sk);
 594                return 1;
 595        }
 596
 597        return 0;
 598}
 599
 600static void dn_destroy_sock(struct sock *sk)
 601{
 602        struct dn_scp *scp = DN_SK(sk);
 603
 604        scp->nsp_rxtshift = 0; /* reset back off */
 605
 606        if (sk->sk_socket) {
 607                if (sk->sk_socket->state != SS_UNCONNECTED)
 608                        sk->sk_socket->state = SS_DISCONNECTING;
 609        }
 610
 611        sk->sk_state = TCP_CLOSE;
 612
 613        switch (scp->state) {
 614        case DN_DN:
 615                dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
 616                                 sk->sk_allocation);
 617                scp->persist_fxn = dn_destroy_timer;
 618                scp->persist = dn_nsp_persist(sk);
 619                break;
 620        case DN_CR:
 621                scp->state = DN_DR;
 622                goto disc_reject;
 623        case DN_RUN:
 624                scp->state = DN_DI;
 625                /* fall through */
 626        case DN_DI:
 627        case DN_DR:
 628disc_reject:
 629                dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation);
 630                /* fall through */
 631        case DN_NC:
 632        case DN_NR:
 633        case DN_RJ:
 634        case DN_DIC:
 635        case DN_CN:
 636        case DN_DRC:
 637        case DN_CI:
 638        case DN_CD:
 639                scp->persist_fxn = dn_destroy_timer;
 640                scp->persist = dn_nsp_persist(sk);
 641                break;
 642        default:
 643                printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
 644                /* fall through */
 645        case DN_O:
 646                dn_stop_slow_timer(sk);
 647
 648                dn_unhash_sock_bh(sk);
 649                sock_put(sk);
 650
 651                break;
 652        }
 653}
 654
 655char *dn_addr2asc(__u16 addr, char *buf)
 656{
 657        unsigned short node, area;
 658
 659        node = addr & 0x03ff;
 660        area = addr >> 10;
 661        sprintf(buf, "%hd.%hd", area, node);
 662
 663        return buf;
 664}
 665
 666
 667
 668static int dn_create(struct net *net, struct socket *sock, int protocol,
 669                     int kern)
 670{
 671        struct sock *sk;
 672
 673        if (protocol < 0 || protocol > SK_PROTOCOL_MAX)
 674                return -EINVAL;
 675
 676        if (!net_eq(net, &init_net))
 677                return -EAFNOSUPPORT;
 678
 679        switch (sock->type) {
 680        case SOCK_SEQPACKET:
 681                if (protocol != DNPROTO_NSP)
 682                        return -EPROTONOSUPPORT;
 683                break;
 684        case SOCK_STREAM:
 685                break;
 686        default:
 687                return -ESOCKTNOSUPPORT;
 688        }
 689
 690
 691        if ((sk = dn_alloc_sock(net, sock, GFP_KERNEL, kern)) == NULL)
 692                return -ENOBUFS;
 693
 694        sk->sk_protocol = protocol;
 695
 696        return 0;
 697}
 698
 699
 700static int
 701dn_release(struct socket *sock)
 702{
 703        struct sock *sk = sock->sk;
 704
 705        if (sk) {
 706                sock_orphan(sk);
 707                sock_hold(sk);
 708                lock_sock(sk);
 709                dn_destroy_sock(sk);
 710                release_sock(sk);
 711                sock_put(sk);
 712        }
 713
 714        return 0;
 715}
 716
 717static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 718{
 719        struct sock *sk = sock->sk;
 720        struct dn_scp *scp = DN_SK(sk);
 721        struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
 722        struct net_device *dev, *ldev;
 723        int rv;
 724
 725        if (addr_len != sizeof(struct sockaddr_dn))
 726                return -EINVAL;
 727
 728        if (saddr->sdn_family != AF_DECnet)
 729                return -EINVAL;
 730
 731        if (le16_to_cpu(saddr->sdn_nodeaddrl) && (le16_to_cpu(saddr->sdn_nodeaddrl) != 2))
 732                return -EINVAL;
 733
 734        if (le16_to_cpu(saddr->sdn_objnamel) > DN_MAXOBJL)
 735                return -EINVAL;
 736
 737        if (saddr->sdn_flags & ~SDF_WILD)
 738                return -EINVAL;
 739
 740        if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
 741            (saddr->sdn_flags & SDF_WILD)))
 742                return -EACCES;
 743
 744        if (!(saddr->sdn_flags & SDF_WILD)) {
 745                if (le16_to_cpu(saddr->sdn_nodeaddrl)) {
 746                        rcu_read_lock();
 747                        ldev = NULL;
 748                        for_each_netdev_rcu(&init_net, dev) {
 749                                if (!dev->dn_ptr)
 750                                        continue;
 751                                if (dn_dev_islocal(dev, dn_saddr2dn(saddr))) {
 752                                        ldev = dev;
 753                                        break;
 754                                }
 755                        }
 756                        rcu_read_unlock();
 757                        if (ldev == NULL)
 758                                return -EADDRNOTAVAIL;
 759                }
 760        }
 761
 762        rv = -EINVAL;
 763        lock_sock(sk);
 764        if (sock_flag(sk, SOCK_ZAPPED)) {
 765                memcpy(&scp->addr, saddr, addr_len);
 766                sock_reset_flag(sk, SOCK_ZAPPED);
 767
 768                rv = dn_hash_sock(sk);
 769                if (rv)
 770                        sock_set_flag(sk, SOCK_ZAPPED);
 771        }
 772        release_sock(sk);
 773
 774        return rv;
 775}
 776
 777
 778static int dn_auto_bind(struct socket *sock)
 779{
 780        struct sock *sk = sock->sk;
 781        struct dn_scp *scp = DN_SK(sk);
 782        int rv;
 783
 784        sock_reset_flag(sk, SOCK_ZAPPED);
 785
 786        scp->addr.sdn_flags  = 0;
 787        scp->addr.sdn_objnum = 0;
 788
 789        /*
 790         * This stuff is to keep compatibility with Eduardo's
 791         * patch. I hope I can dispense with it shortly...
 792         */
 793        if ((scp->accessdata.acc_accl != 0) &&
 794                (scp->accessdata.acc_accl <= 12)) {
 795
 796                scp->addr.sdn_objnamel = cpu_to_le16(scp->accessdata.acc_accl);
 797                memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, le16_to_cpu(scp->addr.sdn_objnamel));
 798
 799                scp->accessdata.acc_accl = 0;
 800                memset(scp->accessdata.acc_acc, 0, 40);
 801        }
 802        /* End of compatibility stuff */
 803
 804        scp->addr.sdn_add.a_len = cpu_to_le16(2);
 805        rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr);
 806        if (rv == 0) {
 807                rv = dn_hash_sock(sk);
 808                if (rv)
 809                        sock_set_flag(sk, SOCK_ZAPPED);
 810        }
 811
 812        return rv;
 813}
 814
 815static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation)
 816{
 817        struct dn_scp *scp = DN_SK(sk);
 818        DEFINE_WAIT(wait);
 819        int err;
 820
 821        if (scp->state != DN_CR)
 822                return -EINVAL;
 823
 824        scp->state = DN_CC;
 825        scp->segsize_loc = dst_metric_advmss(__sk_dst_get(sk));
 826        dn_send_conn_conf(sk, allocation);
 827
 828        prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
 829        for(;;) {
 830                release_sock(sk);
 831                if (scp->state == DN_CC)
 832                        *timeo = schedule_timeout(*timeo);
 833                lock_sock(sk);
 834                err = 0;
 835                if (scp->state == DN_RUN)
 836                        break;
 837                err = sock_error(sk);
 838                if (err)
 839                        break;
 840                err = sock_intr_errno(*timeo);
 841                if (signal_pending(current))
 842                        break;
 843                err = -EAGAIN;
 844                if (!*timeo)
 845                        break;
 846                prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
 847        }
 848        finish_wait(sk_sleep(sk), &wait);
 849        if (err == 0) {
 850                sk->sk_socket->state = SS_CONNECTED;
 851        } else if (scp->state != DN_CC) {
 852                sk->sk_socket->state = SS_UNCONNECTED;
 853        }
 854        return err;
 855}
 856
 857static int dn_wait_run(struct sock *sk, long *timeo)
 858{
 859        struct dn_scp *scp = DN_SK(sk);
 860        DEFINE_WAIT(wait);
 861        int err = 0;
 862
 863        if (scp->state == DN_RUN)
 864                goto out;
 865
 866        if (!*timeo)
 867                return -EALREADY;
 868
 869        prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
 870        for(;;) {
 871                release_sock(sk);
 872                if (scp->state == DN_CI || scp->state == DN_CC)
 873                        *timeo = schedule_timeout(*timeo);
 874                lock_sock(sk);
 875                err = 0;
 876                if (scp->state == DN_RUN)
 877                        break;
 878                err = sock_error(sk);
 879                if (err)
 880                        break;
 881                err = sock_intr_errno(*timeo);
 882                if (signal_pending(current))
 883                        break;
 884                err = -ETIMEDOUT;
 885                if (!*timeo)
 886                        break;
 887                prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
 888        }
 889        finish_wait(sk_sleep(sk), &wait);
 890out:
 891        if (err == 0) {
 892                sk->sk_socket->state = SS_CONNECTED;
 893        } else if (scp->state != DN_CI && scp->state != DN_CC) {
 894                sk->sk_socket->state = SS_UNCONNECTED;
 895        }
 896        return err;
 897}
 898
 899static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
 900{
 901        struct socket *sock = sk->sk_socket;
 902        struct dn_scp *scp = DN_SK(sk);
 903        int err = -EISCONN;
 904        struct flowidn fld;
 905        struct dst_entry *dst;
 906
 907        if (sock->state == SS_CONNECTED)
 908                goto out;
 909
 910        if (sock->state == SS_CONNECTING) {
 911                err = 0;
 912                if (scp->state == DN_RUN) {
 913                        sock->state = SS_CONNECTED;
 914                        goto out;
 915                }
 916                err = -ECONNREFUSED;
 917                if (scp->state != DN_CI && scp->state != DN_CC) {
 918                        sock->state = SS_UNCONNECTED;
 919                        goto out;
 920                }
 921                return dn_wait_run(sk, timeo);
 922        }
 923
 924        err = -EINVAL;
 925        if (scp->state != DN_O)
 926                goto out;
 927
 928        if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
 929                goto out;
 930        if (addr->sdn_family != AF_DECnet)
 931                goto out;
 932        if (addr->sdn_flags & SDF_WILD)
 933                goto out;
 934
 935        if (sock_flag(sk, SOCK_ZAPPED)) {
 936                err = dn_auto_bind(sk->sk_socket);
 937                if (err)
 938                        goto out;
 939        }
 940
 941        memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));
 942
 943        err = -EHOSTUNREACH;
 944        memset(&fld, 0, sizeof(fld));
 945        fld.flowidn_oif = sk->sk_bound_dev_if;
 946        fld.daddr = dn_saddr2dn(&scp->peer);
 947        fld.saddr = dn_saddr2dn(&scp->addr);
 948        dn_sk_ports_copy(&fld, scp);
 949        fld.flowidn_proto = DNPROTO_NSP;
 950        if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, flags) < 0)
 951                goto out;
 952        dst = __sk_dst_get(sk);
 953        sk->sk_route_caps = dst->dev->features;
 954        sock->state = SS_CONNECTING;
 955        scp->state = DN_CI;
 956        scp->segsize_loc = dst_metric_advmss(dst);
 957
 958        dn_nsp_send_conninit(sk, NSP_CI);
 959        err = -EINPROGRESS;
 960        if (*timeo) {
 961                err = dn_wait_run(sk, timeo);
 962        }
 963out:
 964        return err;
 965}
 966
 967static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags)
 968{
 969        struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
 970        struct sock *sk = sock->sk;
 971        int err;
 972        long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
 973
 974        lock_sock(sk);
 975        err = __dn_connect(sk, addr, addrlen, &timeo, 0);
 976        release_sock(sk);
 977
 978        return err;
 979}
 980
 981static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
 982{
 983        struct dn_scp *scp = DN_SK(sk);
 984
 985        switch (scp->state) {
 986        case DN_RUN:
 987                return 0;
 988        case DN_CR:
 989                return dn_confirm_accept(sk, timeo, sk->sk_allocation);
 990        case DN_CI:
 991        case DN_CC:
 992                return dn_wait_run(sk, timeo);
 993        case DN_O:
 994                return __dn_connect(sk, addr, addrlen, timeo, flags);
 995        }
 996
 997        return -EINVAL;
 998}
 999
1000
1001static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
1002{
1003        unsigned char *ptr = skb->data;
1004
1005        acc->acc_userl = *ptr++;
1006        memcpy(&acc->acc_user, ptr, acc->acc_userl);
1007        ptr += acc->acc_userl;
1008
1009        acc->acc_passl = *ptr++;
1010        memcpy(&acc->acc_pass, ptr, acc->acc_passl);
1011        ptr += acc->acc_passl;
1012
1013        acc->acc_accl = *ptr++;
1014        memcpy(&acc->acc_acc, ptr, acc->acc_accl);
1015
1016        skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
1017
1018}
1019
1020static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
1021{
1022        unsigned char *ptr = skb->data;
1023        u16 len = *ptr++; /* yes, it's 8bit on the wire */
1024
1025        BUG_ON(len > 16); /* we've checked the contents earlier */
1026        opt->opt_optl   = cpu_to_le16(len);
1027        opt->opt_status = 0;
1028        memcpy(opt->opt_data, ptr, len);
1029        skb_pull(skb, len + 1);
1030}
1031
1032static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo)
1033{
1034        DEFINE_WAIT(wait);
1035        struct sk_buff *skb = NULL;
1036        int err = 0;
1037
1038        prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1039        for(;;) {
1040                release_sock(sk);
1041                skb = skb_dequeue(&sk->sk_receive_queue);
1042                if (skb == NULL) {
1043                        *timeo = schedule_timeout(*timeo);
1044                        skb = skb_dequeue(&sk->sk_receive_queue);
1045                }
1046                lock_sock(sk);
1047                if (skb != NULL)
1048                        break;
1049                err = -EINVAL;
1050                if (sk->sk_state != TCP_LISTEN)
1051                        break;
1052                err = sock_intr_errno(*timeo);
1053                if (signal_pending(current))
1054                        break;
1055                err = -EAGAIN;
1056                if (!*timeo)
1057                        break;
1058                prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1059        }
1060        finish_wait(sk_sleep(sk), &wait);
1061
1062        return skb == NULL ? ERR_PTR(err) : skb;
1063}
1064
1065static int dn_accept(struct socket *sock, struct socket *newsock, int flags,
1066                     bool kern)
1067{
1068        struct sock *sk = sock->sk, *newsk;
1069        struct sk_buff *skb = NULL;
1070        struct dn_skb_cb *cb;
1071        unsigned char menuver;
1072        int err = 0;
1073        unsigned char type;
1074        long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1075        struct dst_entry *dst;
1076
1077        lock_sock(sk);
1078
1079        if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) {
1080                release_sock(sk);
1081                return -EINVAL;
1082        }
1083
1084        skb = skb_dequeue(&sk->sk_receive_queue);
1085        if (skb == NULL) {
1086                skb = dn_wait_for_connect(sk, &timeo);
1087                if (IS_ERR(skb)) {
1088                        release_sock(sk);
1089                        return PTR_ERR(skb);
1090                }
1091        }
1092
1093        cb = DN_SKB_CB(skb);
1094        sk->sk_ack_backlog--;
1095        newsk = dn_alloc_sock(sock_net(sk), newsock, sk->sk_allocation, kern);
1096        if (newsk == NULL) {
1097                release_sock(sk);
1098                kfree_skb(skb);
1099                return -ENOBUFS;
1100        }
1101        release_sock(sk);
1102
1103        dst = skb_dst(skb);
1104        sk_dst_set(newsk, dst);
1105        skb_dst_set(skb, NULL);
1106
1107        DN_SK(newsk)->state        = DN_CR;
1108        DN_SK(newsk)->addrrem      = cb->src_port;
1109        DN_SK(newsk)->services_rem = cb->services;
1110        DN_SK(newsk)->info_rem     = cb->info;
1111        DN_SK(newsk)->segsize_rem  = cb->segsize;
1112        DN_SK(newsk)->accept_mode  = DN_SK(sk)->accept_mode;
1113
1114        if (DN_SK(newsk)->segsize_rem < 230)
1115                DN_SK(newsk)->segsize_rem = 230;
1116
1117        if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
1118                DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd;
1119
1120        newsk->sk_state  = TCP_LISTEN;
1121        memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn));
1122
1123        /*
1124         * If we are listening on a wild socket, we don't want
1125         * the newly created socket on the wrong hash queue.
1126         */
1127        DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD;
1128
1129        skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
1130        skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
1131        *(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
1132        *(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
1133
1134        menuver = *skb->data;
1135        skb_pull(skb, 1);
1136
1137        if (menuver & DN_MENUVER_ACC)
1138                dn_access_copy(skb, &(DN_SK(newsk)->accessdata));
1139
1140        if (menuver & DN_MENUVER_USR)
1141                dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));
1142
1143        if (menuver & DN_MENUVER_PRX)
1144                DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY;
1145
1146        if (menuver & DN_MENUVER_UIC)
1147                DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY;
1148
1149        kfree_skb(skb);
1150
1151        memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
1152                sizeof(struct optdata_dn));
1153        memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
1154                sizeof(struct optdata_dn));
1155
1156        lock_sock(newsk);
1157        err = dn_hash_sock(newsk);
1158        if (err == 0) {
1159                sock_reset_flag(newsk, SOCK_ZAPPED);
1160                dn_send_conn_ack(newsk);
1161
1162                /*
1163                 * Here we use sk->sk_allocation since although the conn conf is
1164                 * for the newsk, the context is the old socket.
1165                 */
1166                if (DN_SK(newsk)->accept_mode == ACC_IMMED)
1167                        err = dn_confirm_accept(newsk, &timeo,
1168                                                sk->sk_allocation);
1169        }
1170        release_sock(newsk);
1171        return err;
1172}
1173
1174
1175static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int peer)
1176{
1177        struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
1178        struct sock *sk = sock->sk;
1179        struct dn_scp *scp = DN_SK(sk);
1180
1181        lock_sock(sk);
1182
1183        if (peer) {
1184                if ((sock->state != SS_CONNECTED &&
1185                     sock->state != SS_CONNECTING) &&
1186                    scp->accept_mode == ACC_IMMED) {
1187                        release_sock(sk);
1188                        return -ENOTCONN;
1189                }
1190
1191                memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
1192        } else {
1193                memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
1194        }
1195
1196        release_sock(sk);
1197
1198        return sizeof(struct sockaddr_dn);
1199}
1200
1201
1202static __poll_t dn_poll(struct file *file, struct socket *sock, poll_table  *wait)
1203{
1204        struct sock *sk = sock->sk;
1205        struct dn_scp *scp = DN_SK(sk);
1206        __poll_t mask = datagram_poll(file, sock, wait);
1207
1208        if (!skb_queue_empty_lockless(&scp->other_receive_queue))
1209                mask |= EPOLLRDBAND;
1210
1211        return mask;
1212}
1213
1214static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1215{
1216        struct sock *sk = sock->sk;
1217        struct dn_scp *scp = DN_SK(sk);
1218        int err = -EOPNOTSUPP;
1219        long amount = 0;
1220        struct sk_buff *skb;
1221        int val;
1222
1223        switch(cmd)
1224        {
1225        case SIOCGIFADDR:
1226        case SIOCSIFADDR:
1227                return dn_dev_ioctl(cmd, (void __user *)arg);
1228
1229        case SIOCATMARK:
1230                lock_sock(sk);
1231                val = !skb_queue_empty(&scp->other_receive_queue);
1232                if (scp->state != DN_RUN)
1233                        val = -ENOTCONN;
1234                release_sock(sk);
1235                return val;
1236
1237        case TIOCOUTQ:
1238                amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1239                if (amount < 0)
1240                        amount = 0;
1241                err = put_user(amount, (int __user *)arg);
1242                break;
1243
1244        case TIOCINQ:
1245                lock_sock(sk);
1246                skb = skb_peek(&scp->other_receive_queue);
1247                if (skb) {
1248                        amount = skb->len;
1249                } else {
1250                        skb_queue_walk(&sk->sk_receive_queue, skb)
1251                                amount += skb->len;
1252                }
1253                release_sock(sk);
1254                err = put_user(amount, (int __user *)arg);
1255                break;
1256
1257        default:
1258                err = -ENOIOCTLCMD;
1259                break;
1260        }
1261
1262        return err;
1263}
1264
1265static int dn_listen(struct socket *sock, int backlog)
1266{
1267        struct sock *sk = sock->sk;
1268        int err = -EINVAL;
1269
1270        lock_sock(sk);
1271
1272        if (sock_flag(sk, SOCK_ZAPPED))
1273                goto out;
1274
1275        if ((DN_SK(sk)->state != DN_O) || (sk->sk_state == TCP_LISTEN))
1276                goto out;
1277
1278        sk->sk_max_ack_backlog = backlog;
1279        sk->sk_ack_backlog     = 0;
1280        sk->sk_state           = TCP_LISTEN;
1281        err                 = 0;
1282        dn_rehash_sock(sk);
1283
1284out:
1285        release_sock(sk);
1286
1287        return err;
1288}
1289
1290
1291static int dn_shutdown(struct socket *sock, int how)
1292{
1293        struct sock *sk = sock->sk;
1294        struct dn_scp *scp = DN_SK(sk);
1295        int err = -ENOTCONN;
1296
1297        lock_sock(sk);
1298
1299        if (sock->state == SS_UNCONNECTED)
1300                goto out;
1301
1302        err = 0;
1303        if (sock->state == SS_DISCONNECTING)
1304                goto out;
1305
1306        err = -EINVAL;
1307        if (scp->state == DN_O)
1308                goto out;
1309
1310        if (how != SHUT_RDWR)
1311                goto out;
1312
1313        sk->sk_shutdown = SHUTDOWN_MASK;
1314        dn_destroy_sock(sk);
1315        err = 0;
1316
1317out:
1318        release_sock(sk);
1319
1320        return err;
1321}
1322
1323static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
1324{
1325        struct sock *sk = sock->sk;
1326        int err;
1327
1328        lock_sock(sk);
1329        err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
1330        release_sock(sk);
1331#ifdef CONFIG_NETFILTER
1332        /* we need to exclude all possible ENOPROTOOPTs except default case */
1333        if (err == -ENOPROTOOPT && optname != DSO_LINKINFO &&
1334            optname != DSO_STREAM && optname != DSO_SEQPACKET)
1335                err = nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
1336#endif
1337
1338        return err;
1339}
1340
1341static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, unsigned int optlen, int flags)
1342{
1343        struct  sock *sk = sock->sk;
1344        struct dn_scp *scp = DN_SK(sk);
1345        long timeo;
1346        union {
1347                struct optdata_dn opt;
1348                struct accessdata_dn acc;
1349                int mode;
1350                unsigned long win;
1351                int val;
1352                unsigned char services;
1353                unsigned char info;
1354        } u;
1355        int err;
1356
1357        if (optlen && !optval)
1358                return -EINVAL;
1359
1360        if (optlen > sizeof(u))
1361                return -EINVAL;
1362
1363        if (copy_from_user(&u, optval, optlen))
1364                return -EFAULT;
1365
1366        switch (optname) {
1367        case DSO_CONDATA:
1368                if (sock->state == SS_CONNECTED)
1369                        return -EISCONN;
1370                if ((scp->state != DN_O) && (scp->state != DN_CR))
1371                        return -EINVAL;
1372
1373                if (optlen != sizeof(struct optdata_dn))
1374                        return -EINVAL;
1375
1376                if (le16_to_cpu(u.opt.opt_optl) > 16)
1377                        return -EINVAL;
1378
1379                memcpy(&scp->conndata_out, &u.opt, optlen);
1380                break;
1381
1382        case DSO_DISDATA:
1383                if (sock->state != SS_CONNECTED &&
1384                    scp->accept_mode == ACC_IMMED)
1385                        return -ENOTCONN;
1386
1387                if (optlen != sizeof(struct optdata_dn))
1388                        return -EINVAL;
1389
1390                if (le16_to_cpu(u.opt.opt_optl) > 16)
1391                        return -EINVAL;
1392
1393                memcpy(&scp->discdata_out, &u.opt, optlen);
1394                break;
1395
1396        case DSO_CONACCESS:
1397                if (sock->state == SS_CONNECTED)
1398                        return -EISCONN;
1399                if (scp->state != DN_O)
1400                        return -EINVAL;
1401
1402                if (optlen != sizeof(struct accessdata_dn))
1403                        return -EINVAL;
1404
1405                if ((u.acc.acc_accl > DN_MAXACCL) ||
1406                    (u.acc.acc_passl > DN_MAXACCL) ||
1407                    (u.acc.acc_userl > DN_MAXACCL))
1408                        return -EINVAL;
1409
1410                memcpy(&scp->accessdata, &u.acc, optlen);
1411                break;
1412
1413        case DSO_ACCEPTMODE:
1414                if (sock->state == SS_CONNECTED)
1415                        return -EISCONN;
1416                if (scp->state != DN_O)
1417                        return -EINVAL;
1418
1419                if (optlen != sizeof(int))
1420                        return -EINVAL;
1421
1422                if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
1423                        return -EINVAL;
1424
1425                scp->accept_mode = (unsigned char)u.mode;
1426                break;
1427
1428        case DSO_CONACCEPT:
1429                if (scp->state != DN_CR)
1430                        return -EINVAL;
1431                timeo = sock_rcvtimeo(sk, 0);
1432                err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
1433                return err;
1434
1435        case DSO_CONREJECT:
1436                if (scp->state != DN_CR)
1437                        return -EINVAL;
1438
1439                scp->state = DN_DR;
1440                sk->sk_shutdown = SHUTDOWN_MASK;
1441                dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
1442                break;
1443
1444        case DSO_MAXWINDOW:
1445                if (optlen != sizeof(unsigned long))
1446                        return -EINVAL;
1447                if (u.win > NSP_MAX_WINDOW)
1448                        u.win = NSP_MAX_WINDOW;
1449                if (u.win == 0)
1450                        return -EINVAL;
1451                scp->max_window = u.win;
1452                if (scp->snd_window > u.win)
1453                        scp->snd_window = u.win;
1454                break;
1455
1456        case DSO_NODELAY:
1457                if (optlen != sizeof(int))
1458                        return -EINVAL;
1459                if (scp->nonagle == TCP_NAGLE_CORK)
1460                        return -EINVAL;
1461                scp->nonagle = (u.val == 0) ? 0 : TCP_NAGLE_OFF;
1462                /* if (scp->nonagle == 1) { Push pending frames } */
1463                break;
1464
1465        case DSO_CORK:
1466                if (optlen != sizeof(int))
1467                        return -EINVAL;
1468                if (scp->nonagle == TCP_NAGLE_OFF)
1469                        return -EINVAL;
1470                scp->nonagle = (u.val == 0) ? 0 : TCP_NAGLE_CORK;
1471                /* if (scp->nonagle == 0) { Push pending frames } */
1472                break;
1473
1474        case DSO_SERVICES:
1475                if (optlen != sizeof(unsigned char))
1476                        return -EINVAL;
1477                if ((u.services & ~NSP_FC_MASK) != 0x01)
1478                        return -EINVAL;
1479                if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
1480                        return -EINVAL;
1481                scp->services_loc = u.services;
1482                break;
1483
1484        case DSO_INFO:
1485                if (optlen != sizeof(unsigned char))
1486                        return -EINVAL;
1487                if (u.info & 0xfc)
1488                        return -EINVAL;
1489                scp->info_loc = u.info;
1490                break;
1491
1492        case DSO_LINKINFO:
1493        case DSO_STREAM:
1494        case DSO_SEQPACKET:
1495        default:
1496                return -ENOPROTOOPT;
1497        }
1498
1499        return 0;
1500}
1501
1502static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
1503{
1504        struct sock *sk = sock->sk;
1505        int err;
1506
1507        lock_sock(sk);
1508        err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
1509        release_sock(sk);
1510#ifdef CONFIG_NETFILTER
1511        if (err == -ENOPROTOOPT && optname != DSO_STREAM &&
1512            optname != DSO_SEQPACKET && optname != DSO_CONACCEPT &&
1513            optname != DSO_CONREJECT) {
1514                int len;
1515
1516                if (get_user(len, optlen))
1517                        return -EFAULT;
1518
1519                err = nf_getsockopt(sk, PF_DECnet, optname, optval, &len);
1520                if (err >= 0)
1521                        err = put_user(len, optlen);
1522        }
1523#endif
1524
1525        return err;
1526}
1527
1528static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
1529{
1530        struct  sock *sk = sock->sk;
1531        struct dn_scp *scp = DN_SK(sk);
1532        struct linkinfo_dn link;
1533        unsigned int r_len;
1534        void *r_data = NULL;
1535        unsigned int val;
1536
1537        if(get_user(r_len , optlen))
1538                return -EFAULT;
1539
1540        switch (optname) {
1541        case DSO_CONDATA:
1542                if (r_len > sizeof(struct optdata_dn))
1543                        r_len = sizeof(struct optdata_dn);
1544                r_data = &scp->conndata_in;
1545                break;
1546
1547        case DSO_DISDATA:
1548                if (r_len > sizeof(struct optdata_dn))
1549                        r_len = sizeof(struct optdata_dn);
1550                r_data = &scp->discdata_in;
1551                break;
1552
1553        case DSO_CONACCESS:
1554                if (r_len > sizeof(struct accessdata_dn))
1555                        r_len = sizeof(struct accessdata_dn);
1556                r_data = &scp->accessdata;
1557                break;
1558
1559        case DSO_ACCEPTMODE:
1560                if (r_len > sizeof(unsigned char))
1561                        r_len = sizeof(unsigned char);
1562                r_data = &scp->accept_mode;
1563                break;
1564
1565        case DSO_LINKINFO:
1566                if (r_len > sizeof(struct linkinfo_dn))
1567                        r_len = sizeof(struct linkinfo_dn);
1568
1569                memset(&link, 0, sizeof(link));
1570
1571                switch (sock->state) {
1572                case SS_CONNECTING:
1573                        link.idn_linkstate = LL_CONNECTING;
1574                        break;
1575                case SS_DISCONNECTING:
1576                        link.idn_linkstate = LL_DISCONNECTING;
1577                        break;
1578                case SS_CONNECTED:
1579                        link.idn_linkstate = LL_RUNNING;
1580                        break;
1581                default:
1582                        link.idn_linkstate = LL_INACTIVE;
1583                }
1584
1585                link.idn_segsize = scp->segsize_rem;
1586                r_data = &link;
1587                break;
1588
1589        case DSO_MAXWINDOW:
1590                if (r_len > sizeof(unsigned long))
1591                        r_len = sizeof(unsigned long);
1592                r_data = &scp->max_window;
1593                break;
1594
1595        case DSO_NODELAY:
1596                if (r_len > sizeof(int))
1597                        r_len = sizeof(int);
1598                val = (scp->nonagle == TCP_NAGLE_OFF);
1599                r_data = &val;
1600                break;
1601
1602        case DSO_CORK:
1603                if (r_len > sizeof(int))
1604                        r_len = sizeof(int);
1605                val = (scp->nonagle == TCP_NAGLE_CORK);
1606                r_data = &val;
1607                break;
1608
1609        case DSO_SERVICES:
1610                if (r_len > sizeof(unsigned char))
1611                        r_len = sizeof(unsigned char);
1612                r_data = &scp->services_rem;
1613                break;
1614
1615        case DSO_INFO:
1616                if (r_len > sizeof(unsigned char))
1617                        r_len = sizeof(unsigned char);
1618                r_data = &scp->info_rem;
1619                break;
1620
1621        case DSO_STREAM:
1622        case DSO_SEQPACKET:
1623        case DSO_CONACCEPT:
1624        case DSO_CONREJECT:
1625        default:
1626                return -ENOPROTOOPT;
1627        }
1628
1629        if (r_data) {
1630                if (copy_to_user(optval, r_data, r_len))
1631                        return -EFAULT;
1632                if (put_user(r_len, optlen))
1633                        return -EFAULT;
1634        }
1635
1636        return 0;
1637}
1638
1639
1640static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
1641{
1642        struct sk_buff *skb;
1643        int len = 0;
1644
1645        if (flags & MSG_OOB)
1646                return !skb_queue_empty(q) ? 1 : 0;
1647
1648        skb_queue_walk(q, skb) {
1649                struct dn_skb_cb *cb = DN_SKB_CB(skb);
1650                len += skb->len;
1651
1652                if (cb->nsp_flags & 0x40) {
1653                        /* SOCK_SEQPACKET reads to EOM */
1654                        if (sk->sk_type == SOCK_SEQPACKET)
1655                                return 1;
1656                        /* so does SOCK_STREAM unless WAITALL is specified */
1657                        if (!(flags & MSG_WAITALL))
1658                                return 1;
1659                }
1660
1661                /* minimum data length for read exceeded */
1662                if (len >= target)
1663                        return 1;
1664        }
1665
1666        return 0;
1667}
1668
1669
1670static int dn_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1671                      int flags)
1672{
1673        struct sock *sk = sock->sk;
1674        struct dn_scp *scp = DN_SK(sk);
1675        struct sk_buff_head *queue = &sk->sk_receive_queue;
1676        size_t target = size > 1 ? 1 : 0;
1677        size_t copied = 0;
1678        int rv = 0;
1679        struct sk_buff *skb, *n;
1680        struct dn_skb_cb *cb = NULL;
1681        unsigned char eor = 0;
1682        long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1683
1684        lock_sock(sk);
1685
1686        if (sock_flag(sk, SOCK_ZAPPED)) {
1687                rv = -EADDRNOTAVAIL;
1688                goto out;
1689        }
1690
1691        if (sk->sk_shutdown & RCV_SHUTDOWN) {
1692                rv = 0;
1693                goto out;
1694        }
1695
1696        rv = dn_check_state(sk, NULL, 0, &timeo, flags);
1697        if (rv)
1698                goto out;
1699
1700        if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
1701                rv = -EOPNOTSUPP;
1702                goto out;
1703        }
1704
1705        if (flags & MSG_OOB)
1706                queue = &scp->other_receive_queue;
1707
1708        if (flags & MSG_WAITALL)
1709                target = size;
1710
1711
1712        /*
1713         * See if there is data ready to read, sleep if there isn't
1714         */
1715        for(;;) {
1716                DEFINE_WAIT_FUNC(wait, woken_wake_function);
1717
1718                if (sk->sk_err)
1719                        goto out;
1720
1721                if (!skb_queue_empty(&scp->other_receive_queue)) {
1722                        if (!(flags & MSG_OOB)) {
1723                                msg->msg_flags |= MSG_OOB;
1724                                if (!scp->other_report) {
1725                                        scp->other_report = 1;
1726                                        goto out;
1727                                }
1728                        }
1729                }
1730
1731                if (scp->state != DN_RUN)
1732                        goto out;
1733
1734                if (signal_pending(current)) {
1735                        rv = sock_intr_errno(timeo);
1736                        goto out;
1737                }
1738
1739                if (dn_data_ready(sk, queue, flags, target))
1740                        break;
1741
1742                if (flags & MSG_DONTWAIT) {
1743                        rv = -EWOULDBLOCK;
1744                        goto out;
1745                }
1746
1747                add_wait_queue(sk_sleep(sk), &wait);
1748                sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1749                sk_wait_event(sk, &timeo, dn_data_ready(sk, queue, flags, target), &wait);
1750                sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1751                remove_wait_queue(sk_sleep(sk), &wait);
1752        }
1753
1754        skb_queue_walk_safe(queue, skb, n) {
1755                unsigned int chunk = skb->len;
1756                cb = DN_SKB_CB(skb);
1757
1758                if ((chunk + copied) > size)
1759                        chunk = size - copied;
1760
1761                if (memcpy_to_msg(msg, skb->data, chunk)) {
1762                        rv = -EFAULT;
1763                        break;
1764                }
1765                copied += chunk;
1766
1767                if (!(flags & MSG_PEEK))
1768                        skb_pull(skb, chunk);
1769
1770                eor = cb->nsp_flags & 0x40;
1771
1772                if (skb->len == 0) {
1773                        skb_unlink(skb, queue);
1774                        kfree_skb(skb);
1775                        /*
1776                         * N.B. Don't refer to skb or cb after this point
1777                         * in loop.
1778                         */
1779                        if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
1780                                scp->flowloc_sw = DN_SEND;
1781                                dn_nsp_send_link(sk, DN_SEND, 0);
1782                        }
1783                }
1784
1785                if (eor) {
1786                        if (sk->sk_type == SOCK_SEQPACKET)
1787                                break;
1788                        if (!(flags & MSG_WAITALL))
1789                                break;
1790                }
1791
1792                if (flags & MSG_OOB)
1793                        break;
1794
1795                if (copied >= target)
1796                        break;
1797        }
1798
1799        rv = copied;
1800
1801
1802        if (eor && (sk->sk_type == SOCK_SEQPACKET))
1803                msg->msg_flags |= MSG_EOR;
1804
1805out:
1806        if (rv == 0)
1807                rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk);
1808
1809        if ((rv >= 0) && msg->msg_name) {
1810                __sockaddr_check_size(sizeof(struct sockaddr_dn));
1811                memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
1812                msg->msg_namelen = sizeof(struct sockaddr_dn);
1813        }
1814
1815        release_sock(sk);
1816
1817        return rv;
1818}
1819
1820
1821static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags)
1822{
1823        unsigned char fctype = scp->services_rem & NSP_FC_MASK;
1824        if (skb_queue_len(queue) >= scp->snd_window)
1825                return 1;
1826        if (fctype != NSP_FC_NONE) {
1827                if (flags & MSG_OOB) {
1828                        if (scp->flowrem_oth == 0)
1829                                return 1;
1830                } else {
1831                        if (scp->flowrem_dat == 0)
1832                                return 1;
1833                }
1834        }
1835        return 0;
1836}
1837
1838/*
1839 * The DECnet spec requires that the "routing layer" accepts packets which
1840 * are at least 230 bytes in size. This excludes any headers which the NSP
1841 * layer might add, so we always assume that we'll be using the maximal
1842 * length header on data packets. The variation in length is due to the
1843 * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
1844 * make much practical difference.
1845 */
1846unsigned int dn_mss_from_pmtu(struct net_device *dev, int mtu)
1847{
1848        unsigned int mss = 230 - DN_MAX_NSP_DATA_HEADER;
1849        if (dev) {
1850                struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
1851                mtu -= LL_RESERVED_SPACE(dev);
1852                if (dn_db->use_long)
1853                        mtu -= 21;
1854                else
1855                        mtu -= 6;
1856                mtu -= DN_MAX_NSP_DATA_HEADER;
1857        } else {
1858                /*
1859                 * 21 = long header, 16 = guess at MAC header length
1860                 */
1861                mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16);
1862        }
1863        if (mtu > mss)
1864                mss = mtu;
1865        return mss;
1866}
1867
1868static inline unsigned int dn_current_mss(struct sock *sk, int flags)
1869{
1870        struct dst_entry *dst = __sk_dst_get(sk);
1871        struct dn_scp *scp = DN_SK(sk);
1872        int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem);
1873
1874        /* Other data messages are limited to 16 bytes per packet */
1875        if (flags & MSG_OOB)
1876                return 16;
1877
1878        /* This works out the maximum size of segment we can send out */
1879        if (dst) {
1880                u32 mtu = dst_mtu(dst);
1881                mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now);
1882        }
1883
1884        return mss_now;
1885}
1886
1887/*
1888 * N.B. We get the timeout wrong here, but then we always did get it
1889 * wrong before and this is another step along the road to correcting
1890 * it. It ought to get updated each time we pass through the routine,
1891 * but in practise it probably doesn't matter too much for now.
1892 */
1893static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
1894                              unsigned long datalen, int noblock,
1895                              int *errcode)
1896{
1897        struct sk_buff *skb = sock_alloc_send_skb(sk, datalen,
1898                                                   noblock, errcode);
1899        if (skb) {
1900                skb->protocol = htons(ETH_P_DNA_RT);
1901                skb->pkt_type = PACKET_OUTGOING;
1902        }
1903        return skb;
1904}
1905
1906static int dn_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1907{
1908        struct sock *sk = sock->sk;
1909        struct dn_scp *scp = DN_SK(sk);
1910        size_t mss;
1911        struct sk_buff_head *queue = &scp->data_xmit_queue;
1912        int flags = msg->msg_flags;
1913        int err = 0;
1914        size_t sent = 0;
1915        int addr_len = msg->msg_namelen;
1916        DECLARE_SOCKADDR(struct sockaddr_dn *, addr, msg->msg_name);
1917        struct sk_buff *skb = NULL;
1918        struct dn_skb_cb *cb;
1919        size_t len;
1920        unsigned char fctype;
1921        long timeo;
1922
1923        if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT))
1924                return -EOPNOTSUPP;
1925
1926        if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
1927                return -EINVAL;
1928
1929        lock_sock(sk);
1930        timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1931        /*
1932         * The only difference between stream sockets and sequenced packet
1933         * sockets is that the stream sockets always behave as if MSG_EOR
1934         * has been set.
1935         */
1936        if (sock->type == SOCK_STREAM) {
1937                if (flags & MSG_EOR) {
1938                        err = -EINVAL;
1939                        goto out;
1940                }
1941                flags |= MSG_EOR;
1942        }
1943
1944
1945        err = dn_check_state(sk, addr, addr_len, &timeo, flags);
1946        if (err)
1947                goto out_err;
1948
1949        if (sk->sk_shutdown & SEND_SHUTDOWN) {
1950                err = -EPIPE;
1951                if (!(flags & MSG_NOSIGNAL))
1952                        send_sig(SIGPIPE, current, 0);
1953                goto out_err;
1954        }
1955
1956        if ((flags & MSG_TRYHARD) && sk->sk_dst_cache)
1957                dst_negative_advice(sk);
1958
1959        mss = scp->segsize_rem;
1960        fctype = scp->services_rem & NSP_FC_MASK;
1961
1962        mss = dn_current_mss(sk, flags);
1963
1964        if (flags & MSG_OOB) {
1965                queue = &scp->other_xmit_queue;
1966                if (size > mss) {
1967                        err = -EMSGSIZE;
1968                        goto out;
1969                }
1970        }
1971
1972        scp->persist_fxn = dn_nsp_xmit_timeout;
1973
1974        while(sent < size) {
1975                err = sock_error(sk);
1976                if (err)
1977                        goto out;
1978
1979                if (signal_pending(current)) {
1980                        err = sock_intr_errno(timeo);
1981                        goto out;
1982                }
1983
1984                /*
1985                 * Calculate size that we wish to send.
1986                 */
1987                len = size - sent;
1988
1989                if (len > mss)
1990                        len = mss;
1991
1992                /*
1993                 * Wait for queue size to go down below the window
1994                 * size.
1995                 */
1996                if (dn_queue_too_long(scp, queue, flags)) {
1997                        DEFINE_WAIT_FUNC(wait, woken_wake_function);
1998
1999                        if (flags & MSG_DONTWAIT) {
2000                                err = -EWOULDBLOCK;
2001                                goto out;
2002                        }
2003
2004                        add_wait_queue(sk_sleep(sk), &wait);
2005                        sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
2006                        sk_wait_event(sk, &timeo,
2007                                      !dn_queue_too_long(scp, queue, flags), &wait);
2008                        sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
2009                        remove_wait_queue(sk_sleep(sk), &wait);
2010                        continue;
2011                }
2012
2013                /*
2014                 * Get a suitably sized skb.
2015                 * 64 is a bit of a hack really, but its larger than any
2016                 * link-layer headers and has served us well as a good
2017                 * guess as to their real length.
2018                 */
2019                skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER,
2020                                         flags & MSG_DONTWAIT, &err);
2021
2022                if (err)
2023                        break;
2024
2025                if (!skb)
2026                        continue;
2027
2028                cb = DN_SKB_CB(skb);
2029
2030                skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER);
2031
2032                if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
2033                        err = -EFAULT;
2034                        goto out;
2035                }
2036
2037                if (flags & MSG_OOB) {
2038                        cb->nsp_flags = 0x30;
2039                        if (fctype != NSP_FC_NONE)
2040                                scp->flowrem_oth--;
2041                } else {
2042                        cb->nsp_flags = 0x00;
2043                        if (scp->seg_total == 0)
2044                                cb->nsp_flags |= 0x20;
2045
2046                        scp->seg_total += len;
2047
2048                        if (((sent + len) == size) && (flags & MSG_EOR)) {
2049                                cb->nsp_flags |= 0x40;
2050                                scp->seg_total = 0;
2051                                if (fctype == NSP_FC_SCMC)
2052                                        scp->flowrem_dat--;
2053                        }
2054                        if (fctype == NSP_FC_SRC)
2055                                scp->flowrem_dat--;
2056                }
2057
2058                sent += len;
2059                dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB);
2060                skb = NULL;
2061
2062                scp->persist = dn_nsp_persist(sk);
2063
2064        }
2065out:
2066
2067        kfree_skb(skb);
2068
2069        release_sock(sk);
2070
2071        return sent ? sent : err;
2072
2073out_err:
2074        err = sk_stream_error(sk, flags, err);
2075        release_sock(sk);
2076        return err;
2077}
2078
2079static int dn_device_event(struct notifier_block *this, unsigned long event,
2080                           void *ptr)
2081{
2082        struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2083
2084        if (!net_eq(dev_net(dev), &init_net))
2085                return NOTIFY_DONE;
2086
2087        switch (event) {
2088        case NETDEV_UP:
2089                dn_dev_up(dev);
2090                break;
2091        case NETDEV_DOWN:
2092                dn_dev_down(dev);
2093                break;
2094        default:
2095                break;
2096        }
2097
2098        return NOTIFY_DONE;
2099}
2100
2101static struct notifier_block dn_dev_notifier = {
2102        .notifier_call = dn_device_event,
2103};
2104
2105static struct packet_type dn_dix_packet_type __read_mostly = {
2106        .type =         cpu_to_be16(ETH_P_DNA_RT),
2107        .func =         dn_route_rcv,
2108};
2109
2110#ifdef CONFIG_PROC_FS
2111struct dn_iter_state {
2112        int bucket;
2113};
2114
2115static struct sock *dn_socket_get_first(struct seq_file *seq)
2116{
2117        struct dn_iter_state *state = seq->private;
2118        struct sock *n = NULL;
2119
2120        for(state->bucket = 0;
2121            state->bucket < DN_SK_HASH_SIZE;
2122            ++state->bucket) {
2123                n = sk_head(&dn_sk_hash[state->bucket]);
2124                if (n)
2125                        break;
2126        }
2127
2128        return n;
2129}
2130
2131static struct sock *dn_socket_get_next(struct seq_file *seq,
2132                                       struct sock *n)
2133{
2134        struct dn_iter_state *state = seq->private;
2135
2136        n = sk_next(n);
2137try_again:
2138        if (n)
2139                goto out;
2140        if (++state->bucket >= DN_SK_HASH_SIZE)
2141                goto out;
2142        n = sk_head(&dn_sk_hash[state->bucket]);
2143        goto try_again;
2144out:
2145        return n;
2146}
2147
2148static struct sock *socket_get_idx(struct seq_file *seq, loff_t *pos)
2149{
2150        struct sock *sk = dn_socket_get_first(seq);
2151
2152        if (sk) {
2153                while(*pos && (sk = dn_socket_get_next(seq, sk)))
2154                        --*pos;
2155        }
2156        return *pos ? NULL : sk;
2157}
2158
2159static void *dn_socket_get_idx(struct seq_file *seq, loff_t pos)
2160{
2161        void *rc;
2162        read_lock_bh(&dn_hash_lock);
2163        rc = socket_get_idx(seq, &pos);
2164        if (!rc) {
2165                read_unlock_bh(&dn_hash_lock);
2166        }
2167        return rc;
2168}
2169
2170static void *dn_socket_seq_start(struct seq_file *seq, loff_t *pos)
2171{
2172        return *pos ? dn_socket_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2173}
2174
2175static void *dn_socket_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2176{
2177        void *rc;
2178
2179        if (v == SEQ_START_TOKEN) {
2180                rc = dn_socket_get_idx(seq, 0);
2181                goto out;
2182        }
2183
2184        rc = dn_socket_get_next(seq, v);
2185        if (rc)
2186                goto out;
2187        read_unlock_bh(&dn_hash_lock);
2188out:
2189        ++*pos;
2190        return rc;
2191}
2192
2193static void dn_socket_seq_stop(struct seq_file *seq, void *v)
2194{
2195        if (v && v != SEQ_START_TOKEN)
2196                read_unlock_bh(&dn_hash_lock);
2197}
2198
2199#define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
2200
2201static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf)
2202{
2203        int i;
2204
2205        switch (le16_to_cpu(dn->sdn_objnamel)) {
2206        case 0:
2207                sprintf(buf, "%d", dn->sdn_objnum);
2208                break;
2209        default:
2210                for (i = 0; i < le16_to_cpu(dn->sdn_objnamel); i++) {
2211                        buf[i] = dn->sdn_objname[i];
2212                        if (IS_NOT_PRINTABLE(buf[i]))
2213                                buf[i] = '.';
2214                }
2215                buf[i] = 0;
2216        }
2217}
2218
2219static char *dn_state2asc(unsigned char state)
2220{
2221        switch (state) {
2222        case DN_O:
2223                return "OPEN";
2224        case DN_CR:
2225                return "  CR";
2226        case DN_DR:
2227                return "  DR";
2228        case DN_DRC:
2229                return " DRC";
2230        case DN_CC:
2231                return "  CC";
2232        case DN_CI:
2233                return "  CI";
2234        case DN_NR:
2235                return "  NR";
2236        case DN_NC:
2237                return "  NC";
2238        case DN_CD:
2239                return "  CD";
2240        case DN_RJ:
2241                return "  RJ";
2242        case DN_RUN:
2243                return " RUN";
2244        case DN_DI:
2245                return "  DI";
2246        case DN_DIC:
2247                return " DIC";
2248        case DN_DN:
2249                return "  DN";
2250        case DN_CL:
2251                return "  CL";
2252        case DN_CN:
2253                return "  CN";
2254        }
2255
2256        return "????";
2257}
2258
2259static inline void dn_socket_format_entry(struct seq_file *seq, struct sock *sk)
2260{
2261        struct dn_scp *scp = DN_SK(sk);
2262        char buf1[DN_ASCBUF_LEN];
2263        char buf2[DN_ASCBUF_LEN];
2264        char local_object[DN_MAXOBJL+3];
2265        char remote_object[DN_MAXOBJL+3];
2266
2267        dn_printable_object(&scp->addr, local_object);
2268        dn_printable_object(&scp->peer, remote_object);
2269
2270        seq_printf(seq,
2271                   "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
2272                   "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
2273                   dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->addr)), buf1),
2274                   scp->addrloc,
2275                   scp->numdat,
2276                   scp->numoth,
2277                   scp->ackxmt_dat,
2278                   scp->ackxmt_oth,
2279                   scp->flowloc_sw,
2280                   local_object,
2281                   dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->peer)), buf2),
2282                   scp->addrrem,
2283                   scp->numdat_rcv,
2284                   scp->numoth_rcv,
2285                   scp->ackrcv_dat,
2286                   scp->ackrcv_oth,
2287                   scp->flowrem_sw,
2288                   remote_object,
2289                   dn_state2asc(scp->state),
2290                   ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER"));
2291}
2292
2293static int dn_socket_seq_show(struct seq_file *seq, void *v)
2294{
2295        if (v == SEQ_START_TOKEN) {
2296                seq_puts(seq, "Local                                              Remote\n");
2297        } else {
2298                dn_socket_format_entry(seq, v);
2299        }
2300        return 0;
2301}
2302
2303static const struct seq_operations dn_socket_seq_ops = {
2304        .start  = dn_socket_seq_start,
2305        .next   = dn_socket_seq_next,
2306        .stop   = dn_socket_seq_stop,
2307        .show   = dn_socket_seq_show,
2308};
2309#endif
2310
2311static const struct net_proto_family    dn_family_ops = {
2312        .family =       AF_DECnet,
2313        .create =       dn_create,
2314        .owner  =       THIS_MODULE,
2315};
2316
2317static const struct proto_ops dn_proto_ops = {
2318        .family =       AF_DECnet,
2319        .owner =        THIS_MODULE,
2320        .release =      dn_release,
2321        .bind =         dn_bind,
2322        .connect =      dn_connect,
2323        .socketpair =   sock_no_socketpair,
2324        .accept =       dn_accept,
2325        .getname =      dn_getname,
2326        .poll =         dn_poll,
2327        .ioctl =        dn_ioctl,
2328        .listen =       dn_listen,
2329        .shutdown =     dn_shutdown,
2330        .setsockopt =   dn_setsockopt,
2331        .getsockopt =   dn_getsockopt,
2332        .sendmsg =      dn_sendmsg,
2333        .recvmsg =      dn_recvmsg,
2334        .mmap =         sock_no_mmap,
2335        .sendpage =     sock_no_sendpage,
2336};
2337
2338MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
2339MODULE_AUTHOR("Linux DECnet Project Team");
2340MODULE_LICENSE("GPL");
2341MODULE_ALIAS_NETPROTO(PF_DECnet);
2342
2343static const char banner[] __initconst = KERN_INFO
2344"NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
2345
2346static int __init decnet_init(void)
2347{
2348        int rc;
2349
2350        printk(banner);
2351
2352        rc = proto_register(&dn_proto, 1);
2353        if (rc != 0)
2354                goto out;
2355
2356        dn_neigh_init();
2357        dn_dev_init();
2358        dn_route_init();
2359        dn_fib_init();
2360
2361        sock_register(&dn_family_ops);
2362        dev_add_pack(&dn_dix_packet_type);
2363        register_netdevice_notifier(&dn_dev_notifier);
2364
2365        proc_create_seq_private("decnet", 0444, init_net.proc_net,
2366                        &dn_socket_seq_ops, sizeof(struct dn_iter_state),
2367                        NULL);
2368        dn_register_sysctl();
2369out:
2370        return rc;
2371
2372}
2373module_init(decnet_init);
2374
2375/*
2376 * Prevent DECnet module unloading until its fixed properly.
2377 * Requires an audit of the code to check for memory leaks and
2378 * initialisation problems etc.
2379 */
2380#if 0
2381static void __exit decnet_exit(void)
2382{
2383        sock_unregister(AF_DECnet);
2384        rtnl_unregister_all(PF_DECnet);
2385        dev_remove_pack(&dn_dix_packet_type);
2386
2387        dn_unregister_sysctl();
2388
2389        unregister_netdevice_notifier(&dn_dev_notifier);
2390
2391        dn_route_cleanup();
2392        dn_dev_cleanup();
2393        dn_neigh_cleanup();
2394        dn_fib_cleanup();
2395
2396        remove_proc_entry("decnet", init_net.proc_net);
2397
2398        proto_unregister(&dn_proto);
2399
2400        rcu_barrier(); /* Wait for completion of call_rcu()'s */
2401}
2402module_exit(decnet_exit);
2403#endif
2404