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