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