3456cd331835c738286d6a2cf6c67a5de8aee16b
[linux-2.6.git] / 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/module.h>
103 #include <linux/errno.h>
104 #include <linux/types.h>
105 #include <linux/slab.h>
106 #include <linux/socket.h>
107 #include <linux/in.h>
108 #include <linux/kernel.h>
109 #include <linux/sched.h>
110 #include <linux/timer.h>
111 #include <linux/string.h>
112 #include <linux/sockios.h>
113 #include <linux/net.h>
114 #include <linux/netdevice.h>
115 #include <linux/inet.h>
116 #include <linux/route.h>
117 #include <linux/netfilter.h>
118 #include <linux/seq_file.h>
119 #include <net/sock.h>
120 #include <net/tcp_states.h>
121 #include <net/flow.h>
122 #include <asm/system.h>
123 #include <asm/ioctls.h>
124 #include <linux/capability.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/fib_rules.h>
134 #include <net/dn.h>
135 #include <net/dn_nsp.h>
136 #include <net/dn_dev.h>
137 #include <net/dn_route.h>
138 #include <net/dn_fib.h>
139 #include <net/dn_neigh.h>
140
141 struct dn_sock {
142         struct sock sk;
143         struct dn_scp scp;
144 };
145
146 static void dn_keepalive(struct sock *sk);
147
148 #define DN_SK_HASH_SHIFT 8
149 #define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
150 #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
151
152
153 static const struct proto_ops dn_proto_ops;
154 static DEFINE_RWLOCK(dn_hash_lock);
155 static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
156 static struct hlist_head dn_wild_sk;
157 static atomic_t decnet_memory_allocated;
158
159 static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen, int flags);
160 static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);
161
162 static struct hlist_head *dn_find_list(struct sock *sk)
163 {
164         struct dn_scp *scp = DN_SK(sk);
165
166         if (scp->addr.sdn_flags & SDF_WILD)
167                 return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL;
168
169         return &dn_sk_hash[scp->addrloc & DN_SK_HASH_MASK];
170 }
171
172 /* 
173  * Valid ports are those greater than zero and not already in use.
174  */
175 static int check_port(__le16 port)
176 {
177         struct sock *sk;
178         struct hlist_node *node;
179
180         if (port == 0)
181                 return -1;
182
183         sk_for_each(sk, node, &dn_sk_hash[port & DN_SK_HASH_MASK]) {
184                 struct dn_scp *scp = DN_SK(sk);
185                 if (scp->addrloc == port)
186                         return -1;
187         }
188         return 0;
189 }
190
191 static unsigned short port_alloc(struct sock *sk)
192 {
193         struct dn_scp *scp = DN_SK(sk);
194 static unsigned short port = 0x2000;
195         unsigned short i_port = port;
196
197         while(check_port(++port) != 0) {
198                 if (port == i_port)
199                         return 0;
200         }
201
202         scp->addrloc = port;
203
204         return 1;
205 }
206
207 /*
208  * Since this is only ever called from user
209  * level, we don't need a write_lock() version
210  * of this.
211  */
212 static int dn_hash_sock(struct sock *sk)
213 {
214         struct dn_scp *scp = DN_SK(sk);
215         struct hlist_head *list;
216         int rv = -EUSERS;
217
218         BUG_ON(sk_hashed(sk));
219
220         write_lock_bh(&dn_hash_lock);
221         
222         if (!scp->addrloc && !port_alloc(sk))
223                 goto out;
224
225         rv = -EADDRINUSE;
226         if ((list = dn_find_list(sk)) == NULL)
227                 goto out;
228
229         sk_add_node(sk, list);
230         rv = 0;
231 out:
232         write_unlock_bh(&dn_hash_lock);
233         return rv;
234 }
235
236 static void dn_unhash_sock(struct sock *sk)
237 {
238         write_lock(&dn_hash_lock);
239         sk_del_node_init(sk);
240         write_unlock(&dn_hash_lock);
241 }
242
243 static void dn_unhash_sock_bh(struct sock *sk)
244 {
245         write_lock_bh(&dn_hash_lock);
246         sk_del_node_init(sk);
247         write_unlock_bh(&dn_hash_lock);
248 }
249
250 static struct hlist_head *listen_hash(struct sockaddr_dn *addr)
251 {
252         int i;
253         unsigned hash = addr->sdn_objnum;
254
255         if (hash == 0) {
256                 hash = addr->sdn_objnamel;
257                 for(i = 0; i < dn_ntohs(addr->sdn_objnamel); i++) {
258                         hash ^= addr->sdn_objname[i];
259                         hash ^= (hash << 3);
260                 }
261         }
262
263         return &dn_sk_hash[hash & DN_SK_HASH_MASK];
264 }
265
266 /*
267  * Called to transform a socket from bound (i.e. with a local address)
268  * into a listening socket (doesn't need a local port number) and rehashes
269  * based upon the object name/number.
270  */
271 static void dn_rehash_sock(struct sock *sk)
272 {
273         struct hlist_head *list;
274         struct dn_scp *scp = DN_SK(sk);
275
276         if (scp->addr.sdn_flags & SDF_WILD)
277                 return;
278
279         write_lock_bh(&dn_hash_lock);
280         sk_del_node_init(sk);
281         DN_SK(sk)->addrloc = 0;
282         list = listen_hash(&DN_SK(sk)->addr);
283         sk_add_node(sk, list);
284         write_unlock_bh(&dn_hash_lock);
285 }
286
287 int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
288 {
289         int len = 2;
290
291         *buf++ = type;
292
293         switch(type) {
294                 case 0:
295                         *buf++ = sdn->sdn_objnum;
296                         break;
297                 case 1:
298                         *buf++ = 0;
299                         *buf++ = dn_ntohs(sdn->sdn_objnamel);
300                         memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
301                         len = 3 + dn_ntohs(sdn->sdn_objnamel);
302                         break;
303                 case 2:
304                         memset(buf, 0, 5);
305                         buf += 5;
306                         *buf++ = dn_ntohs(sdn->sdn_objnamel);
307                         memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
308                         len = 7 + dn_ntohs(sdn->sdn_objnamel);
309                         break;
310         }
311
312         return len;
313 }
314
315 /*
316  * On reception of usernames, we handle types 1 and 0 for destination
317  * addresses only. Types 2 and 4 are used for source addresses, but the
318  * UIC, GIC are ignored and they are both treated the same way. Type 3
319  * is never used as I've no idea what its purpose might be or what its
320  * format is.
321  */
322 int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
323 {
324         unsigned char type;
325         int size = len;
326         int namel = 12;
327
328         sdn->sdn_objnum = 0;
329         sdn->sdn_objnamel = dn_htons(0);
330         memset(sdn->sdn_objname, 0, DN_MAXOBJL);
331
332         if (len < 2)
333                 return -1;
334
335         len -= 2;
336         *fmt = *data++;
337         type = *data++;
338
339         switch(*fmt) {
340                 case 0:
341                         sdn->sdn_objnum = type;
342                         return 2;
343                 case 1:
344                         namel = 16;
345                         break;
346                 case 2:
347                         len  -= 4;
348                         data += 4;
349                         break;
350                 case 4:
351                         len  -= 8;
352                         data += 8;
353                         break;
354                 default:
355                         return -1;
356         }
357
358         len -= 1;
359
360         if (len < 0)
361                 return -1;
362
363         sdn->sdn_objnamel = dn_htons(*data++);
364         len -= dn_ntohs(sdn->sdn_objnamel);
365
366         if ((len < 0) || (dn_ntohs(sdn->sdn_objnamel) > namel))
367                 return -1;
368
369         memcpy(sdn->sdn_objname, data, dn_ntohs(sdn->sdn_objnamel));
370
371         return size - len;
372 }
373
374 struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
375 {
376         struct hlist_head *list = listen_hash(addr);
377         struct hlist_node *node;
378         struct sock *sk;
379
380         read_lock(&dn_hash_lock);
381         sk_for_each(sk, node, list) {
382                 struct dn_scp *scp = DN_SK(sk);
383                 if (sk->sk_state != TCP_LISTEN)
384                         continue;
385                 if (scp->addr.sdn_objnum) {
386                         if (scp->addr.sdn_objnum != addr->sdn_objnum)
387                                 continue;
388                 } else {
389                         if (addr->sdn_objnum)
390                                 continue;
391                         if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
392                                 continue;
393                         if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, dn_ntohs(addr->sdn_objnamel)) != 0)
394                                 continue;
395                 }
396                 sock_hold(sk);
397                 read_unlock(&dn_hash_lock);
398                 return sk;
399         }
400
401         sk = sk_head(&dn_wild_sk);
402         if (sk) {
403                 if (sk->sk_state == TCP_LISTEN)
404                         sock_hold(sk);
405                 else
406                         sk = NULL;
407         }
408
409         read_unlock(&dn_hash_lock);
410         return sk;
411 }
412
413 struct sock *dn_find_by_skb(struct sk_buff *skb)
414 {
415         struct dn_skb_cb *cb = DN_SKB_CB(skb);
416         struct sock *sk;
417         struct hlist_node *node;
418         struct dn_scp *scp;
419
420         read_lock(&dn_hash_lock);
421         sk_for_each(sk, node, &dn_sk_hash[cb->dst_port & DN_SK_HASH_MASK]) {
422                 scp = DN_SK(sk);
423                 if (cb->src != dn_saddr2dn(&scp->peer))
424                         continue;
425                 if (cb->dst_port != scp->addrloc)
426                         continue;
427                 if (scp->addrrem && (cb->src_port != scp->addrrem))
428                         continue;
429                 sock_hold(sk);
430                 goto found;
431         }
432         sk = NULL;
433 found:
434         read_unlock(&dn_hash_lock);
435         return sk;
436 }
437
438
439
440 static void dn_destruct(struct sock *sk)
441 {
442         struct dn_scp *scp = DN_SK(sk);
443
444         skb_queue_purge(&scp->data_xmit_queue);
445         skb_queue_purge(&scp->other_xmit_queue);
446         skb_queue_purge(&scp->other_receive_queue);
447
448         dst_release(xchg(&sk->sk_dst_cache, NULL));
449 }
450
451 static int dn_memory_pressure;
452
453 static void dn_enter_memory_pressure(void)
454 {
455         if (!dn_memory_pressure) {
456                 dn_memory_pressure = 1;
457         }
458 }
459
460 static struct proto dn_proto = {
461         .name                   = "NSP",
462         .owner                  = THIS_MODULE,
463         .enter_memory_pressure  = dn_enter_memory_pressure,
464         .memory_pressure        = &dn_memory_pressure,
465         .memory_allocated       = &decnet_memory_allocated,
466         .sysctl_mem             = sysctl_decnet_mem,
467         .sysctl_wmem            = sysctl_decnet_wmem,
468         .sysctl_rmem            = sysctl_decnet_rmem,
469         .max_header             = DN_MAX_NSP_DATA_HEADER + 64,
470         .obj_size               = sizeof(struct dn_sock),
471 };
472
473 static struct sock *dn_alloc_sock(struct socket *sock, gfp_t gfp)
474 {
475         struct dn_scp *scp;
476         struct sock *sk = sk_alloc(PF_DECnet, gfp, &dn_proto, 1);
477
478         if  (!sk)
479                 goto out;
480
481         if (sock)
482                 sock->ops = &dn_proto_ops;
483         sock_init_data(sock, sk);
484
485         sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
486         sk->sk_destruct    = dn_destruct;
487         sk->sk_no_check    = 1;
488         sk->sk_family      = PF_DECnet;
489         sk->sk_protocol    = 0;
490         sk->sk_allocation  = gfp;
491         sk->sk_sndbuf      = sysctl_decnet_wmem[1];
492         sk->sk_rcvbuf      = sysctl_decnet_rmem[1];
493
494         /* Initialization of DECnet Session Control Port                */
495         scp = DN_SK(sk);
496         scp->state      = DN_O;         /* Open                 */
497         scp->numdat     = 1;            /* Next data seg to tx  */
498         scp->numoth     = 1;            /* Next oth data to tx  */
499         scp->ackxmt_dat = 0;            /* Last data seg ack'ed */
500         scp->ackxmt_oth = 0;            /* Last oth data ack'ed */
501         scp->ackrcv_dat = 0;            /* Highest data ack recv*/
502         scp->ackrcv_oth = 0;            /* Last oth data ack rec*/
503         scp->flowrem_sw = DN_SEND;
504         scp->flowloc_sw = DN_SEND;
505         scp->flowrem_dat = 0;
506         scp->flowrem_oth = 1;
507         scp->flowloc_dat = 0;
508         scp->flowloc_oth = 1;
509         scp->services_rem = 0;
510         scp->services_loc = 1 | NSP_FC_NONE;
511         scp->info_rem = 0;
512         scp->info_loc = 0x03; /* NSP version 4.1 */
513         scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
514         scp->nonagle = 0;
515         scp->multi_ireq = 1;
516         scp->accept_mode = ACC_IMMED;
517         scp->addr.sdn_family    = AF_DECnet;
518         scp->peer.sdn_family    = AF_DECnet;
519         scp->accessdata.acc_accl = 5;
520         memcpy(scp->accessdata.acc_acc, "LINUX", 5);
521
522         scp->max_window   = NSP_MAX_WINDOW;
523         scp->snd_window   = NSP_MIN_WINDOW;
524         scp->nsp_srtt     = NSP_INITIAL_SRTT;
525         scp->nsp_rttvar   = NSP_INITIAL_RTTVAR;
526         scp->nsp_rxtshift = 0;
527
528         skb_queue_head_init(&scp->data_xmit_queue);
529         skb_queue_head_init(&scp->other_xmit_queue);
530         skb_queue_head_init(&scp->other_receive_queue);
531
532         scp->persist = 0;
533         scp->persist_fxn = NULL;
534         scp->keepalive = 10 * HZ;
535         scp->keepalive_fxn = dn_keepalive;
536
537         init_timer(&scp->delack_timer);
538         scp->delack_pending = 0;
539         scp->delack_fxn = dn_nsp_delayed_ack;
540
541         dn_start_slow_timer(sk);
542 out:
543         return sk;
544 }
545
546 /*
547  * Keepalive timer.
548  * FIXME: Should respond to SO_KEEPALIVE etc.
549  */
550 static void dn_keepalive(struct sock *sk)
551 {
552         struct dn_scp *scp = DN_SK(sk);
553
554         /*
555          * By checking the other_data transmit queue is empty
556          * we are double checking that we are not sending too
557          * many of these keepalive frames.
558          */
559         if (skb_queue_empty(&scp->other_xmit_queue))
560                 dn_nsp_send_link(sk, DN_NOCHANGE, 0);
561 }
562
563
564 /*
565  * Timer for shutdown/destroyed sockets.
566  * When socket is dead & no packets have been sent for a
567  * certain amount of time, they are removed by this
568  * routine. Also takes care of sending out DI & DC
569  * frames at correct times.
570  */
571 int dn_destroy_timer(struct sock *sk)
572 {
573         struct dn_scp *scp = DN_SK(sk);
574
575         scp->persist = dn_nsp_persist(sk);
576
577         switch(scp->state) {
578                 case DN_DI:
579                         dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
580                         if (scp->nsp_rxtshift >= decnet_di_count)
581                                 scp->state = DN_CN;
582                         return 0;
583
584                 case DN_DR:
585                         dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
586                         if (scp->nsp_rxtshift >= decnet_dr_count)
587                                 scp->state = DN_DRC;
588                         return 0;
589
590                 case DN_DN:
591                         if (scp->nsp_rxtshift < decnet_dn_count) {
592                                 /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
593                                 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
594                                 return 0;
595                         }
596         }
597
598         scp->persist = (HZ * decnet_time_wait);
599
600         if (sk->sk_socket)
601                 return 0;
602
603         if ((jiffies - scp->stamp) >= (HZ * decnet_time_wait)) {
604                 dn_unhash_sock(sk);
605                 sock_put(sk);
606                 return 1;
607         }
608
609         return 0;
610 }
611
612 static void dn_destroy_sock(struct sock *sk)
613 {
614         struct dn_scp *scp = DN_SK(sk);
615
616         scp->nsp_rxtshift = 0; /* reset back off */
617
618         if (sk->sk_socket) {
619                 if (sk->sk_socket->state != SS_UNCONNECTED)
620                         sk->sk_socket->state = SS_DISCONNECTING;
621         }
622
623         sk->sk_state = TCP_CLOSE;
624
625         switch(scp->state) {
626                 case DN_DN:
627                         dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
628                                          sk->sk_allocation);
629                         scp->persist_fxn = dn_destroy_timer;
630                         scp->persist = dn_nsp_persist(sk);
631                         break;
632                 case DN_CR:
633                         scp->state = DN_DR;
634                         goto disc_reject;
635                 case DN_RUN:
636                         scp->state = DN_DI;
637                 case DN_DI:
638                 case DN_DR:
639 disc_reject:
640                         dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation);
641                 case DN_NC:
642                 case DN_NR:
643                 case DN_RJ:
644                 case DN_DIC:
645                 case DN_CN:
646                 case DN_DRC:
647                 case DN_CI:
648                 case DN_CD:
649                         scp->persist_fxn = dn_destroy_timer;
650                         scp->persist = dn_nsp_persist(sk);
651                         break;
652                 default:
653                         printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
654                 case DN_O:
655                         dn_stop_slow_timer(sk);
656
657                         dn_unhash_sock_bh(sk);
658                         sock_put(sk);
659
660                         break;
661         }
662 }
663
664 char *dn_addr2asc(__u16 addr, char *buf)
665 {
666         unsigned short node, area;
667
668         node = addr & 0x03ff;
669         area = addr >> 10;
670         sprintf(buf, "%hd.%hd", area, node);
671
672         return buf;
673 }
674
675
676
677 static int dn_create(struct socket *sock, int protocol)
678 {
679         struct sock *sk;
680
681         switch(sock->type) {
682                 case SOCK_SEQPACKET:
683                         if (protocol != DNPROTO_NSP)
684                                 return -EPROTONOSUPPORT;
685                         break;
686                 case SOCK_STREAM:
687                         break;
688                 default:
689                         return -ESOCKTNOSUPPORT;
690         }
691
692
693         if ((sk = dn_alloc_sock(sock, GFP_KERNEL)) == NULL) 
694                 return -ENOBUFS;
695
696         sk->sk_protocol = protocol;
697
698         return 0;
699 }
700
701
702 static int
703 dn_release(struct socket *sock)
704 {
705         struct sock *sk = sock->sk;
706
707         if (sk) {
708                 sock_orphan(sk);
709                 sock_hold(sk);
710                 lock_sock(sk);
711                 dn_destroy_sock(sk);
712                 release_sock(sk);
713                 sock_put(sk);
714         }
715
716         return 0;
717 }
718
719 static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
720 {
721         struct sock *sk = sock->sk;
722         struct dn_scp *scp = DN_SK(sk);
723         struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
724         struct net_device *dev;
725         int rv;
726
727         if (addr_len != sizeof(struct sockaddr_dn))
728                 return -EINVAL;
729
730         if (saddr->sdn_family != AF_DECnet)
731                 return -EINVAL;
732
733         if (dn_ntohs(saddr->sdn_nodeaddrl) && (dn_ntohs(saddr->sdn_nodeaddrl) != 2))
734                 return -EINVAL;
735
736         if (dn_ntohs(saddr->sdn_objnamel) > DN_MAXOBJL)
737                 return -EINVAL;
738
739         if (saddr->sdn_flags & ~SDF_WILD)
740                 return -EINVAL;
741
742         if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
743             (saddr->sdn_flags & SDF_WILD)))
744                 return -EACCES;
745
746         if (!(saddr->sdn_flags & SDF_WILD)) {
747                 if (dn_ntohs(saddr->sdn_nodeaddrl)) {
748                         read_lock(&dev_base_lock);
749                         for(dev = dev_base; dev; dev = dev->next) {
750                                 if (!dev->dn_ptr)
751                                         continue;
752                                 if (dn_dev_islocal(dev, dn_saddr2dn(saddr)))
753                                         break;
754                         }
755                         read_unlock(&dev_base_lock);
756                         if (dev == NULL)
757                                 return -EADDRNOTAVAIL;
758                 }
759         }
760
761         rv = -EINVAL;
762         lock_sock(sk);
763         if (sock_flag(sk, SOCK_ZAPPED)) {
764                 memcpy(&scp->addr, saddr, addr_len);
765                 sock_reset_flag(sk, SOCK_ZAPPED);
766
767                 rv = dn_hash_sock(sk);
768                 if (rv)
769                         sock_set_flag(sk, SOCK_ZAPPED);
770         }
771         release_sock(sk);
772
773         return rv;
774 }
775
776
777 static int dn_auto_bind(struct socket *sock)
778 {
779         struct sock *sk = sock->sk;
780         struct dn_scp *scp = DN_SK(sk);
781         int rv;
782
783         sock_reset_flag(sk, SOCK_ZAPPED);
784
785         scp->addr.sdn_flags  = 0;
786         scp->addr.sdn_objnum = 0;
787
788         /*
789          * This stuff is to keep compatibility with Eduardo's
790          * patch. I hope I can dispense with it shortly...
791          */
792         if ((scp->accessdata.acc_accl != 0) &&
793                 (scp->accessdata.acc_accl <= 12)) {
794         
795                 scp->addr.sdn_objnamel = dn_htons(scp->accessdata.acc_accl);
796                 memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, dn_ntohs(scp->addr.sdn_objnamel));
797
798                 scp->accessdata.acc_accl = 0;
799                 memset(scp->accessdata.acc_acc, 0, 40);
800         }
801         /* End of compatibility stuff */
802
803         scp->addr.sdn_add.a_len = dn_htons(2);
804         rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr);
805         if (rv == 0) {
806                 rv = dn_hash_sock(sk);
807                 if (rv)
808                         sock_set_flag(sk, SOCK_ZAPPED);
809         }
810
811         return rv;
812 }
813
814 static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation)
815 {
816         struct dn_scp *scp = DN_SK(sk);
817         DEFINE_WAIT(wait);
818         int err;
819
820         if (scp->state != DN_CR)
821                 return -EINVAL;
822
823         scp->state = DN_CC;
824         scp->segsize_loc = dst_metric(__sk_dst_get(sk), RTAX_ADVMSS);
825         dn_send_conn_conf(sk, allocation);
826
827         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
828         for(;;) {
829                 release_sock(sk);
830                 if (scp->state == DN_CC)
831                         *timeo = schedule_timeout(*timeo);
832                 lock_sock(sk);
833                 err = 0;
834                 if (scp->state == DN_RUN)
835                         break;
836                 err = sock_error(sk);
837                 if (err)
838                         break;
839                 err = sock_intr_errno(*timeo);
840                 if (signal_pending(current))
841                         break;
842                 err = -EAGAIN;
843                 if (!*timeo)
844                         break;
845                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
846         }
847         finish_wait(sk->sk_sleep, &wait);
848         if (err == 0) {
849                 sk->sk_socket->state = SS_CONNECTED;
850         } else if (scp->state != DN_CC) {
851                 sk->sk_socket->state = SS_UNCONNECTED;
852         }
853         return err;
854 }
855
856 static int dn_wait_run(struct sock *sk, long *timeo)
857 {
858         struct dn_scp *scp = DN_SK(sk);
859         DEFINE_WAIT(wait);
860         int err = 0;
861
862         if (scp->state == DN_RUN)
863                 goto out;
864
865         if (!*timeo)
866                 return -EALREADY;
867
868         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
869         for(;;) {
870                 release_sock(sk);
871                 if (scp->state == DN_CI || scp->state == DN_CC)
872                         *timeo = schedule_timeout(*timeo);
873                 lock_sock(sk);
874                 err = 0;
875                 if (scp->state == DN_RUN)
876                         break;
877                 err = sock_error(sk);
878                 if (err)
879                         break;
880                 err = sock_intr_errno(*timeo);
881                 if (signal_pending(current))
882                         break;
883                 err = -ETIMEDOUT;
884                 if (!*timeo)
885                         break;
886                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
887         }
888         finish_wait(sk->sk_sleep, &wait);
889 out:
890         if (err == 0) {
891                 sk->sk_socket->state = SS_CONNECTED;
892         } else if (scp->state != DN_CI && scp->state != DN_CC) {
893                 sk->sk_socket->state = SS_UNCONNECTED;
894         }
895         return err;
896 }
897
898 static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
899 {
900         struct socket *sock = sk->sk_socket;
901         struct dn_scp *scp = DN_SK(sk);
902         int err = -EISCONN;
903         struct flowi fl;
904
905         if (sock->state == SS_CONNECTED)
906                 goto out;
907
908         if (sock->state == SS_CONNECTING) {
909                 err = 0;
910                 if (scp->state == DN_RUN) {
911                         sock->state = SS_CONNECTED;
912                         goto out;
913                 }
914                 err = -ECONNREFUSED;
915                 if (scp->state != DN_CI && scp->state != DN_CC) {
916                         sock->state = SS_UNCONNECTED;
917                         goto out;
918                 }
919                 return dn_wait_run(sk, timeo);
920         }
921
922         err = -EINVAL;
923         if (scp->state != DN_O)
924                 goto out;
925
926         if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
927                 goto out;
928         if (addr->sdn_family != AF_DECnet)
929                 goto out;
930         if (addr->sdn_flags & SDF_WILD)
931                 goto out;
932
933         if (sock_flag(sk, SOCK_ZAPPED)) {
934                 err = dn_auto_bind(sk->sk_socket);
935                 if (err)
936                         goto out;
937         }
938
939         memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));
940
941         err = -EHOSTUNREACH;
942         memset(&fl, 0, sizeof(fl));
943         fl.oif = sk->sk_bound_dev_if;
944         fl.fld_dst = dn_saddr2dn(&scp->peer);
945         fl.fld_src = dn_saddr2dn(&scp->addr);
946         dn_sk_ports_copy(&fl, scp);
947         fl.proto = DNPROTO_NSP;
948         if (dn_route_output_sock(&sk->sk_dst_cache, &fl, sk, flags) < 0)
949                 goto out;
950         sk->sk_route_caps = sk->sk_dst_cache->dev->features;
951         sock->state = SS_CONNECTING;
952         scp->state = DN_CI;
953         scp->segsize_loc = dst_metric(sk->sk_dst_cache, RTAX_ADVMSS);
954
955         dn_nsp_send_conninit(sk, NSP_CI);
956         err = -EINPROGRESS;
957         if (*timeo) {
958                 err = dn_wait_run(sk, timeo);
959         }
960 out:
961         return err;
962 }
963
964 static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags)
965 {
966         struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
967         struct sock *sk = sock->sk;
968         int err;
969         long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
970
971         lock_sock(sk);
972         err = __dn_connect(sk, addr, addrlen, &timeo, 0);
973         release_sock(sk);
974
975         return err;
976 }
977
978 static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
979 {
980         struct dn_scp *scp = DN_SK(sk);
981
982         switch(scp->state) {
983                 case DN_RUN:
984                         return 0;
985                 case DN_CR:
986                         return dn_confirm_accept(sk, timeo, sk->sk_allocation);
987                 case DN_CI:
988                 case DN_CC:
989                         return dn_wait_run(sk, timeo);
990                 case DN_O:
991                         return __dn_connect(sk, addr, addrlen, timeo, flags);
992         }
993
994         return -EINVAL;
995 }
996
997
998 static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
999 {
1000         unsigned char *ptr = skb->data;
1001
1002         acc->acc_userl = *ptr++;
1003         memcpy(&acc->acc_user, ptr, acc->acc_userl);
1004         ptr += acc->acc_userl;
1005
1006         acc->acc_passl = *ptr++;
1007         memcpy(&acc->acc_pass, ptr, acc->acc_passl);
1008         ptr += acc->acc_passl;
1009
1010         acc->acc_accl = *ptr++;
1011         memcpy(&acc->acc_acc, ptr, acc->acc_accl);
1012
1013         skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
1014
1015 }
1016
1017 static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
1018 {
1019         unsigned char *ptr = skb->data;
1020         
1021         opt->opt_optl   = *ptr++;
1022         opt->opt_status = 0;
1023         memcpy(opt->opt_data, ptr, opt->opt_optl);
1024         skb_pull(skb, dn_ntohs(opt->opt_optl) + 1);
1025
1026 }
1027
1028 static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo)
1029 {
1030         DEFINE_WAIT(wait);
1031         struct sk_buff *skb = NULL;
1032         int err = 0;
1033
1034         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1035         for(;;) {
1036                 release_sock(sk);
1037                 skb = skb_dequeue(&sk->sk_receive_queue);
1038                 if (skb == NULL) {
1039                         *timeo = schedule_timeout(*timeo);
1040                         skb = skb_dequeue(&sk->sk_receive_queue);
1041                 }
1042                 lock_sock(sk);
1043                 if (skb != NULL)
1044                         break;
1045                 err = -EINVAL;
1046                 if (sk->sk_state != TCP_LISTEN)
1047                         break;
1048                 err = sock_intr_errno(*timeo);
1049                 if (signal_pending(current))
1050                         break;
1051                 err = -EAGAIN;
1052                 if (!*timeo)
1053                         break;
1054                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1055         }
1056         finish_wait(sk->sk_sleep, &wait);
1057
1058         return skb == NULL ? ERR_PTR(err) : skb;
1059 }
1060
1061 static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
1062 {
1063         struct sock *sk = sock->sk, *newsk;
1064         struct sk_buff *skb = NULL;
1065         struct dn_skb_cb *cb;
1066         unsigned char menuver;
1067         int err = 0;
1068         unsigned char type;
1069         long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1070
1071         lock_sock(sk);
1072
1073         if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) {
1074                 release_sock(sk);
1075                 return -EINVAL;
1076         }
1077
1078         skb = skb_dequeue(&sk->sk_receive_queue);
1079         if (skb == NULL) {
1080                 skb = dn_wait_for_connect(sk, &timeo);
1081                 if (IS_ERR(skb)) {
1082                         release_sock(sk);
1083                         return PTR_ERR(skb);
1084                 }
1085         }
1086
1087         cb = DN_SKB_CB(skb);
1088         sk->sk_ack_backlog--;
1089         newsk = dn_alloc_sock(newsock, sk->sk_allocation);
1090         if (newsk == NULL) {
1091                 release_sock(sk);
1092                 kfree_skb(skb);
1093                 return -ENOBUFS;
1094         }
1095         release_sock(sk);
1096
1097         dst_release(xchg(&newsk->sk_dst_cache, skb->dst));
1098         skb->dst = NULL;
1099
1100         DN_SK(newsk)->state        = DN_CR;
1101         DN_SK(newsk)->addrrem      = cb->src_port;
1102         DN_SK(newsk)->services_rem = cb->services;
1103         DN_SK(newsk)->info_rem     = cb->info;
1104         DN_SK(newsk)->segsize_rem  = cb->segsize;
1105         DN_SK(newsk)->accept_mode  = DN_SK(sk)->accept_mode;
1106         
1107         if (DN_SK(newsk)->segsize_rem < 230)
1108                 DN_SK(newsk)->segsize_rem = 230;
1109
1110         if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
1111                 DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd;
1112
1113         newsk->sk_state  = TCP_LISTEN;
1114         memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn));
1115
1116         /*
1117          * If we are listening on a wild socket, we don't want
1118          * the newly created socket on the wrong hash queue.
1119          */
1120         DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD;
1121
1122         skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
1123         skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
1124         *(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
1125         *(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
1126
1127         menuver = *skb->data;
1128         skb_pull(skb, 1);
1129
1130         if (menuver & DN_MENUVER_ACC)
1131                 dn_access_copy(skb, &(DN_SK(newsk)->accessdata));
1132
1133         if (menuver & DN_MENUVER_USR)
1134                 dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));
1135
1136         if (menuver & DN_MENUVER_PRX)
1137                 DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY;
1138
1139         if (menuver & DN_MENUVER_UIC)
1140                 DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY;
1141
1142         kfree_skb(skb);
1143
1144         memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
1145                 sizeof(struct optdata_dn));
1146         memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
1147                 sizeof(struct optdata_dn));
1148
1149         lock_sock(newsk);
1150         err = dn_hash_sock(newsk);
1151         if (err == 0) {
1152                 sock_reset_flag(newsk, SOCK_ZAPPED);
1153                 dn_send_conn_ack(newsk);
1154
1155                 /*
1156                  * Here we use sk->sk_allocation since although the conn conf is
1157                  * for the newsk, the context is the old socket.
1158                  */
1159                 if (DN_SK(newsk)->accept_mode == ACC_IMMED)
1160                         err = dn_confirm_accept(newsk, &timeo,
1161                                                 sk->sk_allocation);
1162         }
1163         release_sock(newsk);
1164         return err;
1165 }
1166
1167
1168 static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int *uaddr_len,int peer)
1169 {
1170         struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
1171         struct sock *sk = sock->sk;
1172         struct dn_scp *scp = DN_SK(sk);
1173
1174         *uaddr_len = sizeof(struct sockaddr_dn);
1175
1176         lock_sock(sk);
1177
1178         if (peer) {
1179                 if ((sock->state != SS_CONNECTED && 
1180                      sock->state != SS_CONNECTING) && 
1181                     scp->accept_mode == ACC_IMMED) {
1182                         release_sock(sk);
1183                         return -ENOTCONN;
1184                 }
1185
1186                 memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
1187         } else {
1188                 memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
1189         }
1190
1191         release_sock(sk);
1192
1193         return 0;
1194 }
1195
1196
1197 static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table  *wait)
1198 {
1199         struct sock *sk = sock->sk;
1200         struct dn_scp *scp = DN_SK(sk);
1201         int mask = datagram_poll(file, sock, wait);
1202
1203         if (!skb_queue_empty(&scp->other_receive_queue))
1204                 mask |= POLLRDBAND;
1205
1206         return mask;
1207 }
1208
1209 static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1210 {
1211         struct sock *sk = sock->sk;
1212         struct dn_scp *scp = DN_SK(sk);
1213         int err = -EOPNOTSUPP;
1214         long amount = 0;
1215         struct sk_buff *skb;
1216         int val;
1217
1218         switch(cmd)
1219         {
1220         case SIOCGIFADDR:
1221         case SIOCSIFADDR:
1222                 return dn_dev_ioctl(cmd, (void __user *)arg);
1223
1224         case SIOCATMARK:
1225                 lock_sock(sk);
1226                 val = !skb_queue_empty(&scp->other_receive_queue);
1227                 if (scp->state != DN_RUN)
1228                         val = -ENOTCONN;
1229                 release_sock(sk);
1230                 return val;
1231
1232         case TIOCOUTQ:
1233                 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1234                 if (amount < 0)
1235                         amount = 0;
1236                 err = put_user(amount, (int __user *)arg);
1237                 break;
1238
1239         case TIOCINQ:
1240                 lock_sock(sk);
1241                 if ((skb = skb_peek(&scp->other_receive_queue)) != NULL) {
1242                         amount = skb->len;
1243                 } else {
1244                         struct sk_buff *skb = sk->sk_receive_queue.next;
1245                         for(;;) {
1246                                 if (skb ==
1247                                     (struct sk_buff *)&sk->sk_receive_queue)
1248                                         break;
1249                                 amount += skb->len;
1250                                 skb = skb->next;
1251                         }
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
1265 static 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
1284 out:
1285         release_sock(sk);
1286
1287         return err;
1288 }
1289
1290
1291 static 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 != SHUTDOWN_MASK)
1311                 goto out;
1312
1313         sk->sk_shutdown = how;
1314         dn_destroy_sock(sk);
1315         err = 0;
1316
1317 out:
1318         release_sock(sk);
1319
1320         return err;
1321 }
1322
1323 static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, 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
1332         return err;
1333 }
1334
1335 static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, int optlen, int flags) 
1336 {
1337         struct  sock *sk = sock->sk;
1338         struct dn_scp *scp = DN_SK(sk);
1339         long timeo;
1340         union {
1341                 struct optdata_dn opt;
1342                 struct accessdata_dn acc;
1343                 int mode;
1344                 unsigned long win;
1345                 int val;
1346                 unsigned char services;
1347                 unsigned char info;
1348         } u;
1349         int err;
1350
1351         if (optlen && !optval)
1352                 return -EINVAL;
1353
1354         if (optlen > sizeof(u))
1355                 return -EINVAL;
1356
1357         if (copy_from_user(&u, optval, optlen))
1358                 return -EFAULT;
1359
1360         switch(optname) {
1361                 case DSO_CONDATA:
1362                         if (sock->state == SS_CONNECTED) 
1363                                 return -EISCONN;
1364                         if ((scp->state != DN_O) && (scp->state != DN_CR))
1365                                 return -EINVAL;
1366
1367                         if (optlen != sizeof(struct optdata_dn))
1368                                 return -EINVAL;
1369
1370                         if (dn_ntohs(u.opt.opt_optl) > 16)
1371                                 return -EINVAL;
1372
1373                         memcpy(&scp->conndata_out, &u.opt, optlen);
1374                         break;
1375
1376                 case DSO_DISDATA:
1377                         if (sock->state != SS_CONNECTED && scp->accept_mode == ACC_IMMED)
1378                                 return -ENOTCONN;
1379
1380                         if (optlen != sizeof(struct optdata_dn))
1381                                 return -EINVAL;
1382
1383                         if (dn_ntohs(u.opt.opt_optl) > 16)
1384                                 return -EINVAL;
1385
1386                         memcpy(&scp->discdata_out, &u.opt, optlen);
1387                         break;
1388
1389                 case DSO_CONACCESS:
1390                         if (sock->state == SS_CONNECTED) 
1391                                 return -EISCONN;
1392                         if (scp->state != DN_O)
1393                                 return -EINVAL;
1394
1395                         if (optlen != sizeof(struct accessdata_dn))
1396                                 return -EINVAL;
1397
1398                         if ((u.acc.acc_accl > DN_MAXACCL) ||
1399                                         (u.acc.acc_passl > DN_MAXACCL) ||
1400                                         (u.acc.acc_userl > DN_MAXACCL))
1401                                 return -EINVAL;
1402
1403                         memcpy(&scp->accessdata, &u.acc, optlen);
1404                         break;
1405
1406                 case DSO_ACCEPTMODE:
1407                         if (sock->state == SS_CONNECTED)
1408                                 return -EISCONN;
1409                         if (scp->state != DN_O)
1410                                 return -EINVAL;
1411
1412                         if (optlen != sizeof(int))
1413                                 return -EINVAL;
1414
1415                         if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
1416                                 return -EINVAL;
1417
1418                         scp->accept_mode = (unsigned char)u.mode;
1419                         break;
1420
1421                 case DSO_CONACCEPT:
1422
1423                         if (scp->state != DN_CR)
1424                                 return -EINVAL;
1425                         timeo = sock_rcvtimeo(sk, 0);
1426                         err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
1427                         return err;
1428
1429                 case DSO_CONREJECT:
1430
1431                         if (scp->state != DN_CR)
1432                                 return -EINVAL;
1433
1434                         scp->state = DN_DR;
1435                         sk->sk_shutdown = SHUTDOWN_MASK;
1436                         dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
1437                         break;
1438
1439                 default:
1440 #ifdef CONFIG_NETFILTER
1441                 return nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
1442 #endif
1443                 case DSO_LINKINFO:
1444                 case DSO_STREAM:
1445                 case DSO_SEQPACKET:
1446                         return -ENOPROTOOPT;
1447
1448                 case DSO_MAXWINDOW:
1449                         if (optlen != sizeof(unsigned long))
1450                                 return -EINVAL;
1451                         if (u.win > NSP_MAX_WINDOW)
1452                                 u.win = NSP_MAX_WINDOW;
1453                         if (u.win == 0)
1454                                 return -EINVAL;
1455                         scp->max_window = u.win;
1456                         if (scp->snd_window > u.win)
1457                                 scp->snd_window = u.win;
1458                         break;
1459
1460                 case DSO_NODELAY:
1461                         if (optlen != sizeof(int))
1462                                 return -EINVAL;
1463                         if (scp->nonagle == 2)
1464                                 return -EINVAL;
1465                         scp->nonagle = (u.val == 0) ? 0 : 1;
1466                         /* if (scp->nonagle == 1) { Push pending frames } */
1467                         break;
1468
1469                 case DSO_CORK:
1470                         if (optlen != sizeof(int))
1471                                 return -EINVAL;
1472                         if (scp->nonagle == 1)
1473                                 return -EINVAL;
1474                         scp->nonagle = (u.val == 0) ? 0 : 2;
1475                         /* if (scp->nonagle == 0) { Push pending frames } */
1476                         break;
1477
1478                 case DSO_SERVICES:
1479                         if (optlen != sizeof(unsigned char))
1480                                 return -EINVAL;
1481                         if ((u.services & ~NSP_FC_MASK) != 0x01)
1482                                 return -EINVAL;
1483                         if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
1484                                 return -EINVAL;
1485                         scp->services_loc = u.services;
1486                         break;
1487
1488                 case DSO_INFO:
1489                         if (optlen != sizeof(unsigned char))
1490                                 return -EINVAL;
1491                         if (u.info & 0xfc)
1492                                 return -EINVAL;
1493                         scp->info_loc = u.info;
1494                         break;
1495         }
1496
1497         return 0;
1498 }
1499
1500 static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
1501 {
1502         struct sock *sk = sock->sk;
1503         int err;
1504
1505         lock_sock(sk);
1506         err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
1507         release_sock(sk);
1508
1509         return err;
1510 }
1511
1512 static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
1513 {
1514         struct  sock *sk = sock->sk;
1515         struct dn_scp *scp = DN_SK(sk);
1516         struct linkinfo_dn link;
1517         unsigned int r_len;
1518         void *r_data = NULL;
1519         unsigned int val;
1520
1521         if(get_user(r_len , optlen))
1522                 return -EFAULT;
1523                 
1524         switch(optname) {
1525                 case DSO_CONDATA:
1526                         if (r_len > sizeof(struct optdata_dn))
1527                                 r_len = sizeof(struct optdata_dn);
1528                         r_data = &scp->conndata_in;
1529                         break;
1530
1531                 case DSO_DISDATA:
1532                         if (r_len > sizeof(struct optdata_dn))
1533                                 r_len = sizeof(struct optdata_dn);
1534                         r_data = &scp->discdata_in;
1535                         break;
1536
1537                 case DSO_CONACCESS:
1538                         if (r_len > sizeof(struct accessdata_dn))
1539                                 r_len = sizeof(struct accessdata_dn);
1540                         r_data = &scp->accessdata;
1541                         break;
1542
1543                 case DSO_ACCEPTMODE:
1544                         if (r_len > sizeof(unsigned char))
1545                                 r_len = sizeof(unsigned char);
1546                         r_data = &scp->accept_mode;
1547                         break;
1548
1549                 case DSO_LINKINFO:
1550                         if (r_len > sizeof(struct linkinfo_dn))
1551                                 r_len = sizeof(struct linkinfo_dn);
1552
1553                         switch(sock->state) {
1554                                 case SS_CONNECTING:
1555                                         link.idn_linkstate = LL_CONNECTING;
1556                                         break;
1557                                 case SS_DISCONNECTING:
1558                                         link.idn_linkstate = LL_DISCONNECTING;
1559                                         break;
1560                                 case SS_CONNECTED:
1561                                         link.idn_linkstate = LL_RUNNING;
1562                                         break;
1563                                 default:
1564                                         link.idn_linkstate = LL_INACTIVE;
1565                         }
1566
1567                         link.idn_segsize = scp->segsize_rem;
1568                         r_data = &link;
1569                         break;
1570
1571                 default:
1572 #ifdef CONFIG_NETFILTER
1573                 {
1574                         int val, len;
1575                         
1576                         if(get_user(len, optlen))
1577                                 return -EFAULT;
1578                         
1579                         val = nf_getsockopt(sk, PF_DECnet, optname, 
1580                                                         optval, &len);
1581                         if (val >= 0)
1582                                 val = put_user(len, optlen);
1583                         return val;
1584                 }
1585 #endif
1586                 case DSO_STREAM:
1587                 case DSO_SEQPACKET:
1588                 case DSO_CONACCEPT:
1589                 case DSO_CONREJECT:
1590                         return -ENOPROTOOPT;
1591
1592                 case DSO_MAXWINDOW:
1593                         if (r_len > sizeof(unsigned long))
1594                                 r_len = sizeof(unsigned long);
1595                         r_data = &scp->max_window;
1596                         break;
1597
1598                 case DSO_NODELAY:
1599                         if (r_len > sizeof(int))
1600                                 r_len = sizeof(int);
1601                         val = (scp->nonagle == 1);
1602                         r_data = &val;
1603                         break;
1604
1605                 case DSO_CORK:
1606                         if (r_len > sizeof(int))
1607                                 r_len = sizeof(int);
1608                         val = (scp->nonagle == 2);
1609                         r_data = &val;
1610                         break;
1611
1612                 case DSO_SERVICES:
1613                         if (r_len > sizeof(unsigned char))
1614                                 r_len = sizeof(unsigned char);
1615                         r_data = &scp->services_rem;
1616                         break;
1617
1618                 case DSO_INFO:
1619                         if (r_len > sizeof(unsigned char))
1620                                 r_len = sizeof(unsigned char);
1621                         r_data = &scp->info_rem;
1622                         break;
1623         }
1624
1625         if (r_data) {
1626                 if (copy_to_user(optval, r_data, r_len))
1627                         return -EFAULT;
1628                 if (put_user(r_len, optlen))
1629                         return -EFAULT;
1630         }
1631
1632         return 0;
1633 }
1634
1635
1636 static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
1637 {
1638         struct sk_buff *skb = q->next;
1639         int len = 0;
1640
1641         if (flags & MSG_OOB)
1642                 return !skb_queue_empty(q) ? 1 : 0;
1643
1644         while(skb != (struct sk_buff *)q) {
1645                 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1646                 len += skb->len;
1647
1648                 if (cb->nsp_flags & 0x40) {
1649                         /* SOCK_SEQPACKET reads to EOM */
1650                         if (sk->sk_type == SOCK_SEQPACKET)
1651                                 return 1;
1652                         /* so does SOCK_STREAM unless WAITALL is specified */
1653                         if (!(flags & MSG_WAITALL))
1654                                 return 1;
1655                 }
1656
1657                 /* minimum data length for read exceeded */
1658                 if (len >= target)
1659                         return 1;
1660
1661                 skb = skb->next;
1662         }
1663
1664         return 0;
1665 }
1666
1667
1668 static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
1669         struct msghdr *msg, size_t size, int flags)
1670 {
1671         struct sock *sk = sock->sk;
1672         struct dn_scp *scp = DN_SK(sk);
1673         struct sk_buff_head *queue = &sk->sk_receive_queue;
1674         size_t target = size > 1 ? 1 : 0;
1675         size_t copied = 0;
1676         int rv = 0;
1677         struct sk_buff *skb, *nskb;
1678         struct dn_skb_cb *cb = NULL;
1679         unsigned char eor = 0;
1680         long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1681
1682         lock_sock(sk);
1683
1684         if (sock_flag(sk, SOCK_ZAPPED)) {
1685                 rv = -EADDRNOTAVAIL;
1686                 goto out;
1687         }
1688
1689         if (sk->sk_shutdown & RCV_SHUTDOWN) {
1690                 rv = 0;
1691                 goto out;
1692         }
1693
1694         rv = dn_check_state(sk, NULL, 0, &timeo, flags);
1695         if (rv)
1696                 goto out;
1697
1698         if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
1699                 rv = -EOPNOTSUPP;
1700                 goto out;
1701         }
1702
1703         if (flags & MSG_OOB)
1704                 queue = &scp->other_receive_queue;
1705
1706         if (flags & MSG_WAITALL)
1707                 target = size;
1708
1709
1710         /*
1711          * See if there is data ready to read, sleep if there isn't
1712          */
1713         for(;;) {
1714                 if (sk->sk_err)
1715                         goto out;
1716
1717                 if (!skb_queue_empty(&scp->other_receive_queue)) {
1718                         if (!(flags & MSG_OOB)) {
1719                                 msg->msg_flags |= MSG_OOB;
1720                                 if (!scp->other_report) {
1721                                         scp->other_report = 1;
1722                                         goto out;
1723                                 }
1724                         }
1725                 }
1726                 
1727                 if (scp->state != DN_RUN)
1728                         goto out;
1729
1730                 if (signal_pending(current)) {
1731                         rv = sock_intr_errno(timeo);
1732                         goto out;
1733                 }
1734
1735                 if (dn_data_ready(sk, queue, flags, target))
1736                         break;
1737
1738                 if (flags & MSG_DONTWAIT) {
1739                         rv = -EWOULDBLOCK;
1740                         goto out;
1741                 }
1742
1743                 set_bit(SOCK_ASYNC_WAITDATA, &sock->flags);
1744                 SOCK_SLEEP_PRE(sk)
1745
1746                 if (!dn_data_ready(sk, queue, flags, target))
1747                         schedule();
1748
1749                 SOCK_SLEEP_POST(sk)
1750                 clear_bit(SOCK_ASYNC_WAITDATA, &sock->flags);
1751         }
1752
1753         for(skb = queue->next; skb != (struct sk_buff *)queue; skb = nskb) {
1754                 unsigned int chunk = skb->len;
1755                 cb = DN_SKB_CB(skb);
1756
1757                 if ((chunk + copied) > size)
1758                         chunk = size - copied;
1759
1760                 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1761                         rv = -EFAULT;
1762                         break;
1763                 }
1764                 copied += chunk;
1765
1766                 if (!(flags & MSG_PEEK))
1767                         skb_pull(skb, chunk);
1768
1769                 eor = cb->nsp_flags & 0x40;
1770                 nskb = skb->next;
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
1805 out:
1806         if (rv == 0)
1807                 rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk);
1808
1809         if ((rv >= 0) && msg->msg_name) {
1810                 memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
1811                 msg->msg_namelen = sizeof(struct sockaddr_dn);
1812         }
1813
1814         release_sock(sk);
1815
1816         return rv;
1817 }
1818
1819
1820 static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags)
1821 {
1822         unsigned char fctype = scp->services_rem & NSP_FC_MASK;
1823         if (skb_queue_len(queue) >= scp->snd_window)
1824                 return 1;
1825         if (fctype != NSP_FC_NONE) {
1826                 if (flags & MSG_OOB) {
1827                         if (scp->flowrem_oth == 0)
1828                                 return 1;
1829                 } else {
1830                         if (scp->flowrem_dat == 0)
1831                                 return 1;
1832                 }
1833         }
1834         return 0;
1835 }
1836
1837 /*
1838  * The DECnet spec requires the the "routing layer" accepts packets which
1839  * are at least 230 bytes in size. This excludes any headers which the NSP
1840  * layer might add, so we always assume that we'll be using the maximal
1841  * length header on data packets. The variation in length is due to the
1842  * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
1843  * make much practical difference.
1844  */
1845 unsigned dn_mss_from_pmtu(struct net_device *dev, int mtu)
1846 {
1847         unsigned mss = 230 - DN_MAX_NSP_DATA_HEADER;
1848         if (dev) {
1849                 struct dn_dev *dn_db = dev->dn_ptr;
1850                 mtu -= LL_RESERVED_SPACE(dev);
1851                 if (dn_db->use_long)
1852                         mtu -= 21;
1853                 else
1854                         mtu -= 6;
1855                 mtu -= DN_MAX_NSP_DATA_HEADER;
1856         } else {
1857                 /*
1858                  * 21 = long header, 16 = guess at MAC header length
1859                  */
1860                 mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16);
1861         }
1862         if (mtu > mss)
1863                 mss = mtu;
1864         return mss;
1865 }
1866
1867 static inline unsigned int dn_current_mss(struct sock *sk, int flags)
1868 {
1869         struct dst_entry *dst = __sk_dst_get(sk);
1870         struct dn_scp *scp = DN_SK(sk);
1871         int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem);
1872
1873         /* Other data messages are limited to 16 bytes per packet */
1874         if (flags & MSG_OOB)
1875                 return 16;
1876
1877         /* This works out the maximum size of segment we can send out */
1878         if (dst) {
1879                 u32 mtu = dst_mtu(dst);
1880                 mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now);
1881         }
1882
1883         return mss_now;
1884 }
1885
1886 /* 
1887  * N.B. We get the timeout wrong here, but then we always did get it
1888  * wrong before and this is another step along the road to correcting
1889  * it. It ought to get updated each time we pass through the routine,
1890  * but in practise it probably doesn't matter too much for now.
1891  */
1892 static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
1893                               unsigned long datalen, int noblock,
1894                               int *errcode)
1895 {
1896         struct sk_buff *skb = sock_alloc_send_skb(sk, datalen,
1897                                                    noblock, errcode);
1898         if (skb) {
1899                 skb->protocol = __constant_htons(ETH_P_DNA_RT);
1900                 skb->pkt_type = PACKET_OUTGOING;
1901         }
1902         return skb;
1903 }
1904
1905 static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
1906                       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         struct sockaddr_dn *addr = (struct sockaddr_dn *)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->sk_dst_cache);
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                         if (flags & MSG_DONTWAIT) {
1998                                 err = -EWOULDBLOCK;
1999                                 goto out;
2000                         }
2001
2002                         SOCK_SLEEP_PRE(sk)
2003
2004                         if (dn_queue_too_long(scp, queue, flags))
2005                                 schedule();
2006
2007                         SOCK_SLEEP_POST(sk)
2008
2009                         continue;
2010                 }
2011
2012                 /*
2013                  * Get a suitably sized skb.
2014                  * 64 is a bit of a hack really, but its larger than any
2015                  * link-layer headers and has served us well as a good
2016                  * guess as to their real length.
2017                  */
2018                 skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER,
2019                                          flags & MSG_DONTWAIT, &err);
2020
2021                 if (err)
2022                         break;
2023
2024                 if (!skb)
2025                         continue;
2026
2027                 cb = DN_SKB_CB(skb);
2028
2029                 skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER);
2030
2031                 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
2032                         err = -EFAULT;
2033                         goto out;
2034                 }
2035
2036                 if (flags & MSG_OOB) {
2037                         cb->nsp_flags = 0x30;
2038                         if (fctype != NSP_FC_NONE)
2039                                 scp->flowrem_oth--;
2040                 } else {
2041                         cb->nsp_flags = 0x00;
2042                         if (scp->seg_total == 0)
2043                                 cb->nsp_flags |= 0x20;
2044
2045                         scp->seg_total += len;
2046                 
2047                         if (((sent + len) == size) && (flags & MSG_EOR)) {
2048                                 cb->nsp_flags |= 0x40;
2049                                 scp->seg_total = 0;
2050                                 if (fctype == NSP_FC_SCMC)
2051                                         scp->flowrem_dat--;
2052                         }
2053                         if (fctype == NSP_FC_SRC)
2054                                 scp->flowrem_dat--;
2055                 }
2056
2057                 sent += len;
2058                 dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB);
2059                 skb = NULL;
2060
2061                 scp->persist = dn_nsp_persist(sk);
2062
2063         }
2064 out:
2065
2066         if (skb)
2067                 kfree_skb(skb);
2068
2069         release_sock(sk);
2070
2071         return sent ? sent : err;
2072
2073 out_err:
2074         err = sk_stream_error(sk, flags, err);
2075         release_sock(sk);
2076         return err;
2077 }
2078
2079 static int dn_device_event(struct notifier_block *this, unsigned long event,
2080                         void *ptr)
2081 {
2082         struct net_device *dev = (struct net_device *)ptr;
2083
2084         switch(event) {
2085                 case NETDEV_UP:
2086                         dn_dev_up(dev);
2087                         break;
2088                 case NETDEV_DOWN:
2089                         dn_dev_down(dev);
2090                         break;
2091                 default:
2092                         break;
2093         }
2094
2095         return NOTIFY_DONE;
2096 }
2097
2098 static struct notifier_block dn_dev_notifier = {
2099         .notifier_call = dn_device_event,
2100 };
2101
2102 extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
2103
2104 static struct packet_type dn_dix_packet_type = {
2105         .type =         __constant_htons(ETH_P_DNA_RT),
2106         .dev =          NULL,           /* All devices */
2107         .func =         dn_route_rcv,
2108 };
2109
2110 #ifdef CONFIG_PROC_FS
2111 struct dn_iter_state {
2112         int bucket;
2113 };
2114
2115 static 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
2131 static 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);
2137 try_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;
2144 out:
2145         return n;
2146 }
2147
2148 static 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
2159 static 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
2170 static 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
2175 static 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);
2188 out:
2189         ++*pos;
2190         return rc;
2191 }
2192
2193 static 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
2201 static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf)
2202 {
2203         int i;
2204     
2205         switch (dn_ntohs(dn->sdn_objnamel)) {
2206                 case 0:
2207                         sprintf(buf, "%d", dn->sdn_objnum);
2208                         break;
2209                 default:
2210                         for (i = 0; i < dn_ntohs(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
2219 static 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
2259 static 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(dn_ntohs(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(dn_ntohs(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
2293 static 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
2303 static 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
2310 static int dn_socket_seq_open(struct inode *inode, struct file *file)
2311 {
2312         struct seq_file *seq;
2313         int rc = -ENOMEM;
2314         struct dn_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2315
2316         if (!s)
2317                 goto out;
2318
2319         rc = seq_open(file, &dn_socket_seq_ops);
2320         if (rc)
2321                 goto out_kfree;
2322
2323         seq             = file->private_data;
2324         seq->private    = s;
2325         memset(s, 0, sizeof(*s));
2326 out:
2327         return rc;
2328 out_kfree:
2329         kfree(s);
2330         goto out;
2331 }
2332
2333 static struct file_operations dn_socket_seq_fops = {
2334         .owner          = THIS_MODULE,
2335         .open           = dn_socket_seq_open,
2336         .read           = seq_read,
2337         .llseek         = seq_lseek,
2338         .release        = seq_release_private,
2339 };
2340 #endif
2341
2342 static struct net_proto_family  dn_family_ops = {
2343         .family =       AF_DECnet,
2344         .create =       dn_create,
2345         .owner  =       THIS_MODULE,
2346 };
2347
2348 static const struct proto_ops dn_proto_ops = {
2349         .family =       AF_DECnet,
2350         .owner =        THIS_MODULE,
2351         .release =      dn_release,
2352         .bind =         dn_bind,
2353         .connect =      dn_connect,
2354         .socketpair =   sock_no_socketpair,
2355         .accept =       dn_accept,
2356         .getname =      dn_getname,
2357         .poll =         dn_poll,
2358         .ioctl =        dn_ioctl,
2359         .listen =       dn_listen,
2360         .shutdown =     dn_shutdown,
2361         .setsockopt =   dn_setsockopt,
2362         .getsockopt =   dn_getsockopt,
2363         .sendmsg =      dn_sendmsg,
2364         .recvmsg =      dn_recvmsg,
2365         .mmap =         sock_no_mmap,
2366         .sendpage =     sock_no_sendpage,
2367 };
2368
2369 void dn_register_sysctl(void);
2370 void dn_unregister_sysctl(void);
2371
2372 MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
2373 MODULE_AUTHOR("Linux DECnet Project Team");
2374 MODULE_LICENSE("GPL");
2375 MODULE_ALIAS_NETPROTO(PF_DECnet);
2376
2377 static char banner[] __initdata = KERN_INFO "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
2378
2379 static int __init decnet_init(void)
2380 {
2381         int rc;
2382
2383         printk(banner);
2384
2385         rc = proto_register(&dn_proto, 1);
2386         if (rc != 0)
2387                 goto out;
2388
2389         dn_neigh_init();
2390         dn_dev_init();
2391         dn_route_init();
2392         dn_fib_init();
2393
2394         sock_register(&dn_family_ops);
2395         dev_add_pack(&dn_dix_packet_type);
2396         register_netdevice_notifier(&dn_dev_notifier);
2397
2398         proc_net_fops_create("decnet", S_IRUGO, &dn_socket_seq_fops);
2399         dn_register_sysctl();
2400 out:
2401         return rc;
2402
2403 }
2404 module_init(decnet_init);
2405
2406 /*
2407  * Prevent DECnet module unloading until its fixed properly.
2408  * Requires an audit of the code to check for memory leaks and
2409  * initialisation problems etc.
2410  */
2411 #if 0
2412 static void __exit decnet_exit(void)
2413 {
2414         sock_unregister(AF_DECnet);
2415         dev_remove_pack(&dn_dix_packet_type);
2416
2417         dn_unregister_sysctl();
2418
2419         unregister_netdevice_notifier(&dn_dev_notifier);
2420
2421         dn_route_cleanup();
2422         dn_dev_cleanup();
2423         dn_neigh_cleanup();
2424         dn_fib_cleanup();
2425
2426         proc_net_remove("decnet");
2427
2428         proto_unregister(&dn_proto);
2429 }
2430 module_exit(decnet_exit);
2431 #endif