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