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