* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / net / decnet / af_decnet.c
blobaa7759dedbd65a0ee26b263ec849cd92cb3b6db5
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.
7 * DECnet Socket Layer Interface
9 * Authors: Eduardo Marcelo Serrat <emserrat@geocities.com>
10 * Patrick Caulfield <patrick@pandh.demon.co.uk>
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
38 /******************************************************************************
39 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
41 This program is free software; you can redistribute it and/or modify
42 it under the terms of the GNU General Public License as published by
43 the Free Software Foundation; either version 2 of the License, or
44 any later version.
46 This program is distributed in the hope that it will be useful,
47 but WITHOUT ANY WARRANTY; without even the implied warranty of
48 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 GNU General Public License for more details.
51 HISTORY:
53 Version Kernel Date Author/Comments
54 ------- ------ ---- ---------------
55 Version 0.0.1 2.0.30 01-dic-97 Eduardo Marcelo Serrat
56 (emserrat@geocities.com)
58 First Development of DECnet Socket La-
59 yer for Linux. Only supports outgoing
60 connections.
62 Version 0.0.2 2.1.105 20-jun-98 Patrick J. Caulfield
63 (patrick@pandh.demon.co.uk)
65 Port to new kernel development version.
67 Version 0.0.3 2.1.106 25-jun-98 Eduardo Marcelo Serrat
68 (emserrat@geocities.com)
70 Added support for incoming connections
71 so we can start developing server apps
72 on Linux.
74 Module Support
75 Version 0.0.4 2.1.109 21-jul-98 Eduardo Marcelo Serrat
76 (emserrat@geocities.com)
78 Added support for X11R6.4. Now we can
79 use DECnet transport for X on Linux!!!
81 Version 0.0.5 2.1.110 01-aug-98 Eduardo Marcelo Serrat
82 (emserrat@geocities.com)
83 Removed bugs on flow control
84 Removed bugs on incoming accessdata
85 order
87 Version 0.0.6 2.1.110 07-aug-98 Eduardo Marcelo Serrat
88 dn_recvmsg fixes
90 Patrick J. Caulfield
91 dn_bind fixes
92 *******************************************************************************/
94 #include <linux/config.h>
95 #include <linux/module.h>
96 #include <linux/errno.h>
97 #include <linux/types.h>
98 #include <linux/socket.h>
99 #include <linux/in.h>
100 #include <linux/kernel.h>
101 #include <linux/sched.h>
102 #include <linux/timer.h>
103 #include <linux/string.h>
104 #include <linux/sockios.h>
105 #include <linux/net.h>
106 #include <linux/netdevice.h>
107 #include <linux/inet.h>
108 #include <linux/route.h>
109 #include <net/sock.h>
110 #include <asm/segment.h>
111 #include <asm/system.h>
112 #include <linux/mm.h>
113 #include <linux/interrupt.h>
114 #include <linux/proc_fs.h>
115 #include <linux/stat.h>
116 #include <linux/init.h>
117 #include <linux/poll.h>
118 #include <net/neighbour.h>
119 #include <net/dst.h>
120 #include <net/dn.h>
121 #include <net/dn_nsp.h>
122 #include <net/dn_dev.h>
123 #include <net/dn_route.h>
124 #include <net/dn_fib.h>
125 #include <net/dn_raw.h>
126 #include <net/dn_neigh.h>
128 #define MAX(a,b) ((a)>(b)?(a):(b))
130 static void dn_keepalive(struct sock *sk);
133 * decnet_address is kept in network order, decnet_ether_address is kept
134 * as a string of bytes.
136 dn_address decnet_address = 0;
137 unsigned char decnet_ether_address[ETH_ALEN] = { 0xAA, 0x00, 0x04, 0x00, 0x00, 0x00 };
138 int decnet_node_type = DN_RT_INFO_ENDN;
140 static struct proto_ops dn_proto_ops;
141 rwlock_t dn_hash_lock = RW_LOCK_UNLOCKED;
142 static struct sock *dn_sklist = NULL;
143 static struct sock *dn_wild_sk = NULL;
145 static int __dn_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen, int flags);
146 static int __dn_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen, int flags);
148 static struct sock **dn_find_list(struct sock *sk)
150 struct dn_scp *scp = &sk->protinfo.dn;
152 if (scp->addr.sdn_flags & SDF_WILD)
153 return dn_wild_sk ? NULL : &dn_wild_sk;
155 return &dn_sklist;
158 static unsigned short port_alloc(struct sock *sk)
160 struct dn_scp *scp = &sk->protinfo.dn;
161 static unsigned short port = 0x2000;
163 if (port == 0)
164 port++;
166 scp->addrloc = port++;
168 return 1;
172 * Since this is only ever called from user
173 * level, we don't need a write_lock() version
174 * of this.
176 static int dn_hash_sock(struct sock *sk)
178 struct dn_scp *scp = &sk->protinfo.dn;
179 struct sock **skp;
180 int rv = -EUSERS;
182 write_lock_bh(&dn_hash_lock);
184 if (!scp->addrloc && !port_alloc(sk))
185 goto out;
187 rv = -EADDRINUSE;
188 if ((skp = dn_find_list(sk)) == NULL)
189 goto out;
191 sk->next = *skp;
192 sk->pprev = skp;
193 *skp = sk;
194 rv = 0;
195 out:
196 write_unlock_bh(&dn_hash_lock);
197 return rv;
200 static void dn_unhash_sock(struct sock *sk)
202 struct sock **skp = sk->pprev;
204 if (skp == NULL)
205 return;
207 write_lock(&dn_hash_lock);
208 while(*skp != sk)
209 skp = &((*skp)->next);
210 *skp = sk->next;
211 write_unlock(&dn_hash_lock);
213 sk->next = NULL;
214 sk->pprev = NULL;
217 static void dn_unhash_sock_bh(struct sock *sk)
219 struct sock **skp = sk->pprev;
221 if (skp == NULL)
222 return;
224 write_lock_bh(&dn_hash_lock);
225 while(*skp != sk)
226 skp = &((*skp)->next);
227 *skp = sk->next;
228 write_unlock_bh(&dn_hash_lock);
230 sk->next = NULL;
231 sk->pprev = NULL;
235 int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
237 int len = 2;
239 *buf++ = type;
241 switch(type) {
242 case 0:
243 *buf++ = sdn->sdn_objnum;
244 break;
245 case 1:
246 *buf++ = 0;
247 *buf++ = dn_ntohs(sdn->sdn_objnamel);
248 memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
249 len = 3 + dn_ntohs(sdn->sdn_objnamel);
250 break;
251 case 2:
252 memset(buf, 0, 5);
253 buf += 5;
254 *buf++ = dn_ntohs(sdn->sdn_objnamel);
255 memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
256 len = 7 + dn_ntohs(sdn->sdn_objnamel);
257 break;
260 return len;
264 * On reception of usernames, we handle types 1 and 0 for destination
265 * addresses only. Types 2 and 4 are used for source addresses, but the
266 * UIC, GIC are ignored and they are both treated the same way. Type 3
267 * is never used as I've no idea what its purpose might be or what its
268 * format is.
270 int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
272 unsigned char type;
273 int size = len;
274 int namel = 12;
276 sdn->sdn_objnum = 0;
277 sdn->sdn_objnamel = dn_htons(0);
278 memset(sdn->sdn_objname, 0, DN_MAXOBJL);
280 if (len < 2)
281 return -1;
283 len -= 2;
284 *fmt = *data++;
285 type = *data++;
287 switch(*fmt) {
288 case 0:
289 sdn->sdn_objnum = dn_htons(type);
290 return 2;
291 case 1:
292 namel = 16;
293 break;
294 case 2:
295 len -= 4;
296 data += 4;
297 break;
298 case 4:
299 len -= 8;
300 data += 8;
301 break;
302 default:
303 return -1;
306 len -= 1;
308 if (len < 0)
309 return -1;
311 sdn->sdn_objnamel = dn_htons(*data++);
312 len -= dn_ntohs(sdn->sdn_objnamel);
314 if ((len < 0) || (dn_ntohs(sdn->sdn_objnamel) > namel))
315 return -1;
317 memcpy(sdn->sdn_objname, data, dn_ntohs(sdn->sdn_objnamel));
319 return size - len;
322 struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
324 struct sock *sk;
326 read_lock(&dn_hash_lock);
327 for(sk = dn_sklist; sk != NULL; sk = sk->next) {
328 struct dn_scp *scp = &sk->protinfo.dn;
329 if (sk->state != TCP_LISTEN)
330 continue;
331 if (scp->addr.sdn_objnum) {
332 if (scp->addr.sdn_objnum != addr->sdn_objnum)
333 continue;
334 } else {
335 if (addr->sdn_objnum)
336 continue;
337 if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
338 continue;
339 if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, dn_ntohs(addr->sdn_objnamel)) != 0)
340 continue;
342 sock_hold(sk);
343 read_unlock(&dn_hash_lock);
344 return sk;
347 if (dn_wild_sk && (dn_wild_sk->state == TCP_LISTEN))
348 sock_hold((sk = dn_wild_sk));
350 read_unlock(&dn_hash_lock);
351 return sk;
354 struct sock *dn_find_by_skb(struct sk_buff *skb)
356 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
357 struct sock *sk;
358 struct dn_scp *scp;
360 read_lock(&dn_hash_lock);
361 for(sk = dn_sklist; sk != NULL; sk = sk->next) {
362 scp = &sk->protinfo.dn;
363 if (cb->src != dn_saddr2dn(&scp->peer))
364 continue;
365 if (cb->dst_port != scp->addrloc)
366 continue;
367 if (scp->addrrem && (cb->src_port != scp->addrrem))
368 continue;
369 break;
372 if (sk)
373 sock_hold(sk);
375 read_unlock(&dn_hash_lock);
377 return sk;
382 static void dn_destruct(struct sock *sk)
384 struct dn_scp *scp = &sk->protinfo.dn;
386 skb_queue_purge(&scp->data_xmit_queue);
387 skb_queue_purge(&scp->other_xmit_queue);
388 skb_queue_purge(&scp->other_receive_queue);
390 dst_release(xchg(&sk->dst_cache, NULL));
392 MOD_DEC_USE_COUNT;
395 struct sock *dn_alloc_sock(struct socket *sock, int flags)
397 struct sock *sk;
398 struct dn_scp *scp;
400 if ((sk = sk_alloc(PF_DECnet, flags, 1)) == NULL)
401 goto no_sock;
403 if (sock) {
404 #ifdef CONFIG_DECNET_RAW
405 if (sock->type == SOCK_RAW)
406 sock->ops = &dn_raw_proto_ops;
407 else
408 #endif /* CONFIG_DECNET_RAW */
409 sock->ops = &dn_proto_ops;
411 sock_init_data(sock,sk);
412 scp = &sk->protinfo.dn;
414 sk->backlog_rcv = dn_nsp_backlog_rcv;
415 sk->destruct = dn_destruct;
416 sk->no_check = 1;
417 sk->family = PF_DECnet;
418 sk->protocol = 0;
420 /* Initialization of DECnet Session Control Port */
421 scp->state = DN_O; /* Open */
422 scp->numdat = 1; /* Next data seg to tx */
423 scp->numoth = 1; /* Next oth data to tx */
424 scp->ackxmt_dat = 0; /* Last data seg ack'ed */
425 scp->ackxmt_oth = 0; /* Last oth data ack'ed */
426 scp->ackrcv_dat = 0; /* Highest data ack recv*/
427 scp->ackrcv_oth = 0; /* Last oth data ack rec*/
428 scp->flowrem_sw = DN_SEND;
429 scp->flowloc_sw = DN_SEND;
430 scp->accept_mode = ACC_IMMED;
431 scp->addr.sdn_family = AF_DECnet;
432 scp->peer.sdn_family = AF_DECnet;
433 scp->accessdata.acc_accl = 5;
434 memcpy(scp->accessdata.acc_acc, "LINUX", 5);
435 scp->mss = 1460;
437 scp->snd_window = NSP_MIN_WINDOW;
438 scp->nsp_srtt = NSP_INITIAL_SRTT;
439 scp->nsp_rttvar = NSP_INITIAL_RTTVAR;
440 scp->nsp_rxtshift = 0;
442 skb_queue_head_init(&scp->data_xmit_queue);
443 skb_queue_head_init(&scp->other_xmit_queue);
444 skb_queue_head_init(&scp->other_receive_queue);
446 scp->persist = 0;
447 scp->persist_fxn = NULL;
448 scp->keepalive = 10 * HZ;
449 scp->keepalive_fxn = dn_keepalive;
451 init_timer(&scp->delack_timer);
452 scp->delack_pending = 0;
453 scp->delack_fxn = dn_nsp_delayed_ack;
455 dn_start_slow_timer(sk);
457 MOD_INC_USE_COUNT;
459 return sk;
460 no_sock:
461 return NULL;
465 * Keepalive timer.
466 * FIXME: Should respond to SO_KEEPALIVE etc.
468 static void dn_keepalive(struct sock *sk)
470 struct dn_scp *scp = &sk->protinfo.dn;
473 * By checking the other_data transmit queue is empty
474 * we are double checking that we are not sending too
475 * many of these keepalive frames.
477 if (skb_queue_len(&scp->other_xmit_queue) == 0)
478 dn_nsp_send_lnk(sk, DN_NOCHANGE);
483 * Timer for shutdown/destroyed sockets.
484 * When socket is dead & no packets have been sent for a
485 * certain amount of time, they are removed by this
486 * routine. Also takes care of sending out DI & DC
487 * frames at correct times. This is called by both
488 * socket level and interrupt driven code.
490 static int dn_destroy_timer(struct sock *sk)
492 struct dn_scp *scp = &sk->protinfo.dn;
494 scp->persist = dn_nsp_persist(sk);
496 switch(scp->state) {
497 case DN_DI:
498 dn_send_disc(sk, NSP_DISCINIT, 0);
499 if (scp->nsp_rxtshift >= decnet_di_count)
500 scp->state = DN_CN;
501 return 0;
503 case DN_DR:
504 dn_send_disc(sk, NSP_DISCINIT, 0);
505 if (scp->nsp_rxtshift >= decnet_dr_count)
506 scp->state = DN_DRC;
507 return 0;
509 case DN_DN:
510 if (scp->nsp_rxtshift < decnet_dn_count) {
511 /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
512 dn_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC);
513 return 0;
517 scp->persist = (HZ * decnet_time_wait);
519 if (sk->socket)
520 return 0;
522 dn_stop_fast_timer(sk); /* unlikely, but possible that this is runninng */
523 if ((jiffies - scp->stamp) >= (HZ * decnet_time_wait)) {
524 dn_unhash_sock(sk);
525 sock_put(sk);
526 return 1;
529 return 0;
532 void dn_destroy_sock(struct sock *sk)
534 struct dn_scp *scp = &sk->protinfo.dn;
536 if (sk->dead)
537 return;
539 sk->dead = 1;
540 scp->nsp_rxtshift = 0; /* reset back off */
542 if (sk->socket) {
543 if (sk->socket->state != SS_UNCONNECTED)
544 sk->socket->state = SS_DISCONNECTING;
547 sk->state = TCP_CLOSE;
549 switch(scp->state) {
550 case DN_DN:
551 dn_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC);
552 scp->persist_fxn = dn_destroy_timer;
553 scp->persist = dn_nsp_persist(sk);
554 break;
555 case DN_CD:
556 case DN_CR:
557 scp->state = DN_DR;
558 goto disc_reject;
559 case DN_RUN:
560 scp->state = DN_DI;
561 case DN_DI:
562 case DN_DR:
563 disc_reject:
564 dn_send_disc(sk, NSP_DISCINIT, 0);
565 case DN_NC:
566 case DN_NR:
567 case DN_RJ:
568 case DN_DIC:
569 case DN_CN:
570 case DN_DRC:
571 case DN_CI:
572 scp->persist_fxn = dn_destroy_timer;
573 scp->persist = dn_nsp_persist(sk);
574 break;
575 default:
576 printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
577 case DN_O:
578 dn_stop_fast_timer(sk);
579 dn_stop_slow_timer(sk);
581 dn_unhash_sock_bh(sk);
582 sock_put(sk);
584 break;
588 char *dn_addr2asc(dn_address addr, char *buf)
590 unsigned short node, area;
592 node = addr & 0x03ff;
593 area = addr >> 10;
594 sprintf(buf, "%hd.%hd", area, node);
596 return buf;
600 static char *dn_state2asc(unsigned char state)
602 switch(state) {
603 case DN_O:
604 return "OPEN";
605 case DN_CR:
606 return " CR";
607 case DN_DR:
608 return " DR";
609 case DN_DRC:
610 return " DRC";
611 case DN_CC:
612 return " CC";
613 case DN_CI:
614 return " CI";
615 case DN_NR:
616 return " NR";
617 case DN_NC:
618 return " NC";
619 case DN_CD:
620 return " CD";
621 case DN_RJ:
622 return " RJ";
623 case DN_RUN:
624 return " RUN";
625 case DN_DI:
626 return " DI";
627 case DN_DIC:
628 return " DIC";
629 case DN_DN:
630 return " DN";
631 case DN_CL:
632 return " CL";
633 case DN_CN:
634 return " CN";
637 return "????";
640 static int dn_create(struct socket *sock, int protocol)
642 struct sock *sk;
644 switch(sock->type) {
645 case SOCK_SEQPACKET:
646 if (protocol != DNPROTO_NSP)
647 return -EPROTONOSUPPORT;
648 break;
649 case SOCK_STREAM:
650 break;
651 #ifdef CONFIG_DECNET_RAW
652 case SOCK_RAW:
653 if ((protocol != DNPROTO_NSP) &&
654 (protocol != DNPROTO_ROU))
655 return -EPROTONOSUPPORT;
656 break;
657 #endif /* CONFIG_DECNET_RAW */
658 default:
659 return -ESOCKTNOSUPPORT;
663 if ((sk = dn_alloc_sock(sock, GFP_KERNEL)) == NULL)
664 return -ENOBUFS;
666 sk->protocol = protocol;
668 return 0;
672 static int
673 dn_release(struct socket *sock)
675 struct sock *sk = sock->sk;
677 if (sk) {
678 lock_sock(sk);
679 sock->sk = NULL;
680 sk->socket = NULL;
681 dn_destroy_sock(sk);
682 release_sock(sk);
685 return 0;
688 static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
690 struct sock *sk = sock->sk;
691 struct dn_scp *scp = &sk->protinfo.dn;
692 struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
693 struct net_device *dev;
694 int rv;
696 if (sk->zapped == 0)
697 return -EINVAL;
699 if (addr_len != sizeof(struct sockaddr_dn))
700 return -EINVAL;
702 if (saddr->sdn_family != AF_DECnet)
703 return -EINVAL;
705 if (dn_ntohs(saddr->sdn_nodeaddrl) && (dn_ntohs(saddr->sdn_nodeaddrl) != 2))
706 return -EINVAL;
708 if (saddr->sdn_objnum && !suser())
709 return -EPERM;
711 if (dn_ntohs(saddr->sdn_objnamel) > DN_MAXOBJL)
712 return -EINVAL;
714 if (saddr->sdn_flags & ~SDF_WILD)
715 return -EINVAL;
717 if (saddr->sdn_flags & SDF_WILD) {
718 if (!suser())
719 return -EPERM;
720 } else {
721 if (dn_ntohs(saddr->sdn_nodeaddrl)) {
722 read_lock(&dev_base_lock);
723 for(dev = dev_base; dev; dev = dev->next) {
724 if (!dev->dn_ptr)
725 continue;
726 if (dn_dev_islocal(dev, dn_saddr2dn(saddr)))
727 break;
729 read_unlock(&dev_base_lock);
730 if (dev == NULL)
731 return -EADDRNOTAVAIL;
736 memcpy(&scp->addr, saddr, addr_len);
737 sk->zapped = 0;
739 if ((rv = dn_hash_sock(sk)) == 0)
740 goto out;
742 sk->zapped = 1;
743 out:
745 return rv;
749 static int dn_auto_bind(struct socket *sock)
751 struct sock *sk = sock->sk;
752 struct dn_scp *scp = &sk->protinfo.dn;
754 sk->zapped = 0;
756 scp->addr.sdn_flags = 0;
757 scp->addr.sdn_objnum = 0;
760 * This stuff is to keep compatibility with Eduardo's
761 * patch. I hope I can dispense with it shortly...
763 if ((scp->accessdata.acc_accl != 0) &&
764 (scp->accessdata.acc_accl <= 12)) {
766 scp->addr.sdn_objnamel = dn_htons(scp->accessdata.acc_accl);
767 memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, dn_ntohs(scp->addr.sdn_objnamel));
769 scp->accessdata.acc_accl = 0;
770 memset(scp->accessdata.acc_acc, 0, 40);
773 scp->addr.sdn_add.a_len = dn_htons(2);
774 *(dn_address *)scp->addr.sdn_add.a_addr = decnet_address;
776 dn_hash_sock(sk);
778 return 0;
782 static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
784 struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
785 struct sock *sk = sock->sk;
786 struct dn_scp *scp = &sk->protinfo.dn;
787 int err = -EISCONN;
789 lock_sock(sk);
791 if (sock->state == SS_CONNECTED)
792 goto out;
794 if (sock->state == SS_CONNECTING) {
795 err = 0;
796 if (sk->state == TCP_ESTABLISHED)
797 goto out;
799 err = -ECONNREFUSED;
800 if (sk->state == TCP_CLOSE)
801 goto out;
804 err = -EINVAL;
805 if (sk->protinfo.dn.state != DN_O)
806 goto out;
808 if (addr_len != sizeof(struct sockaddr_dn))
809 goto out;
811 if (addr->sdn_family != AF_DECnet)
812 goto out;
814 if (addr->sdn_flags & SDF_WILD)
815 goto out;
817 err = -EADDRNOTAVAIL;
818 if (sk->zapped && (err = dn_auto_bind(sock)))
819 goto out;
821 memcpy(&scp->peer, addr, addr_len);
823 err = -EHOSTUNREACH;
824 if (dn_route_output(&sk->dst_cache, dn_saddr2dn(&scp->peer), dn_saddr2dn(&scp->addr), 0) < 0)
825 goto out;
827 sk->state = TCP_SYN_SENT;
828 sock->state = SS_CONNECTING;
829 sk->protinfo.dn.state = DN_CI;
831 dn_nsp_send_conninit(sk, NSP_CI);
833 err = -EINPROGRESS;
834 if ((sk->state == TCP_SYN_SENT) && (flags & O_NONBLOCK))
835 goto out;
837 while(sk->state == TCP_SYN_SENT) {
839 err = -ERESTARTSYS;
840 if (signal_pending(current))
841 goto out;
843 if ((err = sock_error(sk)) != 0) {
844 sock->state = SS_UNCONNECTED;
845 goto out;
848 SOCK_SLEEP_PRE(sk);
850 if (sk->state == TCP_SYN_SENT)
851 schedule();
853 SOCK_SLEEP_POST(sk);
856 if (sk->state != TCP_ESTABLISHED) {
857 sock->state = SS_UNCONNECTED;
858 err = sock_error(sk);
859 goto out;
862 err = 0;
863 sock->state = SS_CONNECTED;
864 out:
865 release_sock(sk);
867 return err;
870 static int dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
872 unsigned char *ptr = skb->data;
874 acc->acc_userl = *ptr++;
875 memcpy(&acc->acc_user, ptr, acc->acc_userl);
876 ptr += acc->acc_userl;
878 acc->acc_passl = *ptr++;
879 memcpy(&acc->acc_pass, ptr, acc->acc_passl);
880 ptr += acc->acc_passl;
882 acc->acc_accl = *ptr++;
883 memcpy(&acc->acc_acc, ptr, acc->acc_accl);
885 skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
887 return 0;
890 static int dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
892 unsigned char *ptr = skb->data;
894 opt->opt_optl = *ptr++;
895 opt->opt_status = 0;
896 memcpy(opt->opt_data, ptr, opt->opt_optl);
897 skb_pull(skb, opt->opt_optl + 1);
899 return 0;
904 * This is here for use in the sockopt() call as well as
905 * in accept(). Must be called with a locked socket.
907 static int dn_wait_accept(struct socket *sock, int flags)
909 struct sock *sk = sock->sk;
911 while(sk->state == TCP_LISTEN) {
912 if (flags & O_NONBLOCK) {
913 return -EAGAIN;
916 SOCK_SLEEP_PRE(sk)
918 if (sk->state == TCP_LISTEN)
919 schedule();
921 SOCK_SLEEP_POST(sk)
923 if (signal_pending(current))
924 return -ERESTARTSYS; /* But of course you don't! */
927 if ((sk->protinfo.dn.state != DN_RUN) && (sk->protinfo.dn.state != DN_DRC)) {
928 sock->state = SS_UNCONNECTED;
929 return sock_error(sk);
932 sock->state = SS_CONNECTED;
934 return 0;
938 static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
940 struct sock *sk = sock->sk, *newsk;
941 struct sk_buff *skb = NULL;
942 struct dn_skb_cb *cb;
943 unsigned char menuver;
944 int err = 0;
945 unsigned char type;
947 lock_sock(sk);
949 if (sk->state != TCP_LISTEN) {
950 release_sock(sk);
951 return -EINVAL;
954 if (sk->protinfo.dn.state != DN_O) {
955 release_sock(sk);
956 return -EINVAL;
961 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL)
963 if (flags & O_NONBLOCK)
965 release_sock(sk);
966 return -EAGAIN;
969 SOCK_SLEEP_PRE(sk);
971 if (!skb_peek(&sk->receive_queue))
972 schedule();
974 SOCK_SLEEP_POST(sk);
976 if (signal_pending(current))
978 release_sock(sk);
979 return -ERESTARTSYS;
982 } while (skb == NULL);
984 cb = (struct dn_skb_cb *)skb->cb;
986 if ((newsk = dn_alloc_sock(newsock, GFP_KERNEL)) == NULL) {
987 release_sock(sk);
988 kfree_skb(skb);
989 return -ENOBUFS;
991 sk->ack_backlog--;
992 release_sock(sk);
994 dst_release(xchg(&newsk->dst_cache, skb->dst));
995 skb->dst = NULL;
997 newsk->protinfo.dn.state = DN_CR;
998 newsk->protinfo.dn.addrrem = cb->src_port;
999 newsk->protinfo.dn.mss = cb->segsize;
1000 newsk->protinfo.dn.accept_mode = sk->protinfo.dn.accept_mode;
1002 if (newsk->protinfo.dn.mss < 230)
1003 newsk->protinfo.dn.mss = 230;
1005 newsk->state = TCP_LISTEN;
1006 newsk->zapped = 0;
1008 memcpy(&newsk->protinfo.dn.addr, &sk->protinfo.dn.addr, sizeof(struct sockaddr_dn));
1010 skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &newsk->protinfo.dn.addr, &type));
1011 skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &newsk->protinfo.dn.peer, &type));
1012 *(dn_address *)newsk->protinfo.dn.peer.sdn_add.a_addr = cb->src;
1013 *(dn_address *)newsk->protinfo.dn.addr.sdn_add.a_addr = cb->dst;
1015 menuver = *skb->data;
1016 skb_pull(skb, 1);
1018 if (menuver & DN_MENUVER_ACC)
1019 dn_access_copy(skb, &newsk->protinfo.dn.accessdata);
1021 if (menuver & DN_MENUVER_USR)
1022 dn_user_copy(skb, &newsk->protinfo.dn.conndata_in);
1024 if (menuver & DN_MENUVER_PRX)
1025 newsk->protinfo.dn.peer.sdn_flags |= SDF_PROXY;
1027 if (menuver & DN_MENUVER_UIC)
1028 newsk->protinfo.dn.peer.sdn_flags |= SDF_UICPROXY;
1030 kfree_skb(skb);
1032 memcpy(&newsk->protinfo.dn.conndata_out, &sk->protinfo.dn.conndata_out,
1033 sizeof(struct optdata_dn));
1034 memcpy(&newsk->protinfo.dn.discdata_out, &sk->protinfo.dn.discdata_out,
1035 sizeof(struct optdata_dn));
1037 lock_sock(newsk);
1038 dn_hash_sock(newsk);
1040 dn_send_conn_ack(newsk);
1042 if (newsk->protinfo.dn.accept_mode == ACC_IMMED) {
1043 newsk->protinfo.dn.state = DN_CC;
1044 dn_send_conn_conf(newsk);
1045 err = dn_wait_accept(newsock, flags);
1048 release_sock(newsk);
1049 return err;
1053 static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int *uaddr_len,int peer)
1055 struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
1056 struct sock *sk = sock->sk;
1057 struct dn_scp *scp = &sk->protinfo.dn;
1059 *uaddr_len = sizeof(struct sockaddr_dn);
1061 lock_sock(sk);
1063 if (peer) {
1064 if (sock->state != SS_CONNECTED && scp->accept_mode == ACC_IMMED)
1065 return -ENOTCONN;
1067 memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
1068 } else {
1069 memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
1072 release_sock(sk);
1074 return 0;
1078 static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table *wait)
1080 struct sock *sk = sock->sk;
1081 struct dn_scp *scp = &sk->protinfo.dn;
1082 int mask = datagram_poll(file, sock, wait);
1084 if (skb_queue_len(&scp->other_receive_queue))
1085 mask |= POLLRDBAND;
1087 return mask;
1090 static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1092 struct sock *sk = sock->sk;
1093 int err = -EOPNOTSUPP;
1094 unsigned long amount = 0;
1095 struct sk_buff *skb;
1097 #if 0
1098 struct dn_naddr dnaddr;
1099 #endif
1100 switch(cmd)
1102 case SIOCGIFADDR:
1103 case SIOCSIFADDR:
1104 return dn_dev_ioctl(cmd, (void *)arg);
1106 #ifdef CONFIG_DECNET_ROUTER
1107 case SIOCADDRT:
1108 case SIOCDELRT:
1109 return dn_fib_ioctl(sock, cmd, arg);
1110 #endif /* CONFIG_DECNET_ROUTER */
1112 #if 0
1113 case SIOCSIFADDR:
1114 if (!suser()) return -EPERM;
1116 if ((err = copy_from_user(devname, ioarg->devname, 5)) != 0)
1117 break;
1118 if ((err = copy_from_user(addr, ioarg->exec_addr, 6)) != 0)
1119 break;
1120 if ((dev = dev_get(devname)) == NULL) {
1121 err = -ENODEV;
1122 break;
1124 if (dev->dn_ptr == NULL) {
1125 err = -ENODEV;
1126 break;
1129 dn_dev_devices_off();
1131 decnet_default_device = dev;
1132 memcpy(decnet_ether_address, addr, ETH_ALEN);
1133 decnet_address = dn_htons(dn_eth2dn(decnet_ether_address));
1135 dn_dev_devices_on();
1137 break;
1139 case SIOCGIFADDR:
1140 if (decnet_default_device)
1141 strcpy(devname, decnet_default_device->name);
1142 else
1143 memset(devname, 0, 6);
1145 if ((err = copy_to_user(ioarg->devname, devname, 5)) != 0)
1146 break;
1148 if ((err = copy_to_user(ioarg->exec_addr, decnet_ether_address, 6)) != 0)
1149 break;
1151 break;
1152 #endif
1154 #if 0
1155 case SIOCSNETADDR:
1156 if (!suser()) {
1157 err = -EPERM;
1158 break;
1161 if ((err = copy_from_user(&dnaddr, (void *)arg, sizeof(struct dn_naddr))) != 0)
1162 break;
1164 if (dnaddr.a_len != ETH_ALEN) {
1165 err = -EINVAL;
1166 break;
1169 dn_dev_devices_off();
1171 memcpy(decnet_ether_address, dnaddr.a_addr, ETH_ALEN);
1172 decnet_address = dn_htons(dn_eth2dn(decnet_ether_address));
1174 dn_dev_devices_on();
1175 break;
1177 case SIOCGNETADDR:
1178 dnaddr.a_len = ETH_ALEN;
1179 memcpy(dnaddr.a_addr, decnet_ether_address, ETH_ALEN);
1181 if ((err = copy_to_user((void *)arg, &dnaddr, sizeof(struct dn_naddr))) != 0)
1182 break;
1184 break;
1185 #endif
1186 case OSIOCSNETADDR:
1187 if (!suser()) {
1188 err = -EPERM;
1189 break;
1192 dn_dev_devices_off();
1194 decnet_address = (unsigned short)arg;
1195 dn_dn2eth(decnet_ether_address, dn_ntohs(decnet_address));
1197 dn_dev_devices_on();
1198 err = 0;
1199 break;
1201 case OSIOCGNETADDR:
1202 if ((err = put_user(decnet_address, (unsigned short *)arg)) != 0)
1203 break;
1204 break;
1205 case SIOCGIFCONF:
1206 case SIOCGIFFLAGS:
1207 case SIOCGIFBRDADDR:
1208 return dev_ioctl(cmd,(void *)arg);
1210 case TIOCOUTQ:
1211 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1212 if (amount < 0)
1213 amount = 0;
1214 err = put_user(amount, (int *)arg);
1215 break;
1217 case TIOCINQ:
1218 lock_sock(sk);
1219 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1220 amount = skb->len;
1221 release_sock(sk);
1222 err = put_user(amount, (int *)arg);
1223 break;
1226 return err;
1229 static int dn_listen(struct socket *sock, int backlog)
1231 struct sock *sk = sock->sk;
1232 int err = -EINVAL;
1234 lock_sock(sk);
1236 if (sk->zapped)
1237 goto out;
1239 if ((sk->protinfo.dn.state != DN_O) || (sk->state == TCP_LISTEN))
1240 goto out;
1242 sk->max_ack_backlog = backlog;
1243 sk->ack_backlog = 0;
1244 sk->state = TCP_LISTEN;
1245 err = 0;
1247 out:
1248 release_sock(sk);
1250 return err;
1254 static int dn_shutdown(struct socket *sock, int how)
1256 struct sock *sk = sock->sk;
1257 struct dn_scp *scp = &sk->protinfo.dn;
1258 int err = -ENOTCONN;
1260 lock_sock(sk);
1262 if (sock->state == SS_UNCONNECTED)
1263 goto out;
1265 err = 0;
1266 if (sock->state == SS_DISCONNECTING)
1267 goto out;
1269 err = -EINVAL;
1270 if (scp->state == DN_O)
1271 goto out;
1273 if (how != SHUTDOWN_MASK)
1274 goto out;
1277 sk->shutdown = how;
1278 dn_destroy_sock(sk);
1279 err = 0;
1281 out:
1282 release_sock(sk);
1284 return err;
1287 static int dn_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
1289 struct sock *sk = sock->sk;
1290 int err;
1292 lock_sock(sk);
1293 err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
1294 release_sock(sk);
1296 return err;
1299 static int __dn_setsockopt(struct socket *sock, int level,int optname, char *optval, int optlen, int flags)
1301 struct sock *sk = sock->sk;
1302 struct dn_scp *scp = &sk->protinfo.dn;
1303 struct optdata_dn opt;
1304 struct accessdata_dn acc;
1305 #ifdef CONFIG_DECNET_FW
1306 char tmp_fw[MAX(sizeof(struct dn_fwtest),sizeof(struct dn_fwnew))];
1307 #endif
1308 int err;
1310 if (optlen && !optval)
1311 return -EINVAL;
1313 switch(optname) {
1314 case DSO_CONDATA:
1315 if (sock->state == SS_CONNECTED)
1316 return -EISCONN;
1317 if ((scp->state != DN_O) && (scp->state != DN_CR))
1318 return -EINVAL;
1320 if (optlen != sizeof(struct optdata_dn))
1321 return -EINVAL;
1323 if (copy_from_user(&opt, optval, optlen))
1324 return -EFAULT;
1326 if (opt.opt_optl > 16)
1327 return -EINVAL;
1329 memcpy(&scp->conndata_out, &opt, sizeof(struct optdata_dn));
1330 break;
1332 case DSO_DISDATA:
1333 if (sock->state != SS_CONNECTED && sk->protinfo.dn.accept_mode == ACC_IMMED)
1334 return -ENOTCONN;
1336 if (optlen != sizeof(struct optdata_dn))
1337 return -EINVAL;
1339 if (copy_from_user(&opt, optval, sizeof(struct optdata_dn)))
1340 return -EFAULT;
1342 if (opt.opt_optl > 16)
1343 return -EINVAL;
1345 memcpy(&scp->discdata_out, &opt, sizeof(struct optdata_dn));
1346 break;
1348 case DSO_CONACCESS:
1349 if (sock->state == SS_CONNECTED)
1350 return -EISCONN;
1351 if (scp->state != DN_O)
1352 return -EINVAL;
1354 if (optlen != sizeof(struct accessdata_dn))
1355 return -EINVAL;
1357 if (copy_from_user(&acc, optval, sizeof(struct accessdata_dn)))
1358 return -EFAULT;
1360 if ((acc.acc_accl > DN_MAXACCL) ||
1361 (acc.acc_passl > DN_MAXACCL) ||
1362 (acc.acc_userl > DN_MAXACCL))
1363 return -EINVAL;
1365 memcpy(&scp->accessdata, &acc, sizeof(struct accessdata_dn));
1366 break;
1368 case DSO_ACCEPTMODE:
1369 if (sock->state == SS_CONNECTED)
1370 return -EISCONN;
1371 if (scp->state != DN_O)
1372 return -EINVAL;
1374 if (optlen != sizeof(int))
1375 return -EINVAL;
1378 int mode;
1380 if (get_user(mode, optval))
1381 return -EFAULT;
1382 if ((mode != ACC_IMMED) && (mode != ACC_DEFER))
1383 return -EINVAL;
1385 scp->accept_mode = (unsigned char)mode;
1387 break;
1389 case DSO_CONACCEPT:
1391 if (scp->state != DN_CR)
1392 return -EINVAL;
1394 scp->state = DN_CC;
1395 dn_send_conn_conf(sk);
1396 err = dn_wait_accept(sock, sock->file->f_flags);
1397 return err;
1399 case DSO_CONREJECT:
1401 if (scp->state != DN_CR)
1402 return -EINVAL;
1404 scp->state = DN_DR;
1405 sk->shutdown = SHUTDOWN_MASK;
1406 dn_send_disc(sk, 0x38, 0);
1407 break;
1409 #ifdef CONFIG_DECNET_FW
1410 case DN_FW_APPEND:
1411 case DN_FW_REPLACE:
1412 case DN_FW_DELETE:
1413 case DN_FW_DELETE_NUM:
1414 case DN_FW_INSERT:
1415 case DN_FW_FLUSH:
1416 case DN_FW_ZERO:
1417 case DN_FW_CHECK:
1418 case DN_FW_CREATECHAIN:
1419 case DN_FW_DELETECHAIN:
1420 case DN_FW_POLICY:
1422 if (!capable(CAP_NET_ADMIN))
1423 return -EACCES;
1424 if ((optlen > sizeof(tmp_fw)) || (optlen < 1))
1425 return -EINVAL;
1426 if (copy_from_user(&tmp_fw, optval, optlen))
1427 return -EFAULT;
1428 err = dn_fw_ctl(optname, &tmp_fw, optlen);
1429 return -err; /* -0 is 0 after all */
1430 #endif
1431 default:
1432 case DSO_LINKINFO:
1433 case DSO_STREAM:
1434 case DSO_SEQPACKET:
1436 return -EOPNOTSUPP;
1439 return 0;
1442 static int dn_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
1444 struct sock *sk = sock->sk;
1445 int err;
1447 lock_sock(sk);
1448 err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
1449 release_sock(sk);
1451 return err;
1454 static int __dn_getsockopt(struct socket *sock, int level,int optname, char *optval,int *optlen, int flags)
1456 struct sock *sk = sock->sk;
1457 struct dn_scp *scp = &sk->protinfo.dn;
1458 struct linkinfo_dn link;
1459 int mode = scp->accept_mode;
1461 switch(optname) {
1462 case DSO_CONDATA:
1463 if (*optlen != sizeof(struct optdata_dn))
1464 return -EINVAL;
1466 if (copy_to_user(optval, &scp->conndata_in, sizeof(struct optdata_dn)))
1467 return -EFAULT;
1468 break;
1470 case DSO_DISDATA:
1471 if (*optlen != sizeof(struct optdata_dn))
1472 return -EINVAL;
1474 if (copy_to_user(optval, &scp->discdata_in, sizeof(struct optdata_dn)))
1475 return -EFAULT;
1477 break;
1479 case DSO_CONACCESS:
1480 if (*optlen != sizeof(struct accessdata_dn))
1481 return -EINVAL;
1483 if (copy_to_user(optval, &scp->accessdata, sizeof(struct accessdata_dn)))
1484 return -EFAULT;
1485 break;
1487 case DSO_ACCEPTMODE:
1488 if (put_user(mode, optval))
1489 return -EFAULT;
1490 break;
1492 case DSO_LINKINFO:
1493 if (*optlen != sizeof(struct linkinfo_dn))
1494 return -EINVAL;
1496 switch(sock->state) {
1497 case SS_CONNECTING:
1498 link.idn_linkstate = LL_CONNECTING;
1499 break;
1500 case SS_DISCONNECTING:
1501 link.idn_linkstate = LL_DISCONNECTING;
1502 break;
1503 case SS_CONNECTED:
1504 link.idn_linkstate = LL_RUNNING;
1505 break;
1506 default:
1507 link.idn_linkstate = LL_INACTIVE;
1510 link.idn_segsize = scp->mss;
1512 if (copy_to_user(optval, &link, sizeof(struct linkinfo_dn)))
1513 return -EFAULT;
1514 break;
1516 case DSO_STREAM:
1517 case DSO_SEQPACKET:
1518 case DSO_CONACCEPT:
1519 case DSO_CONREJECT:
1520 default:
1521 return -EOPNOTSUPP;
1524 return 0;
1529 * Used by send/recvmsg to wait until the socket is connected
1530 * before passing data.
1532 static int dn_wait_run(struct sock *sk, int flags)
1534 struct dn_scp *scp = &sk->protinfo.dn;
1535 int err = 0;
1537 switch(scp->state) {
1538 case DN_RUN:
1539 return 0;
1541 case DN_CR:
1542 scp->state = DN_CC;
1543 dn_send_conn_conf(sk);
1544 return dn_wait_accept(sk->socket, (flags & MSG_DONTWAIT) ? O_NONBLOCK : 0);
1545 case DN_CI:
1546 case DN_CC:
1547 break;
1548 default:
1549 return -ENOTCONN;
1552 if (flags & MSG_DONTWAIT)
1553 return -EWOULDBLOCK;
1555 do {
1556 if ((err = sock_error(sk)) != 0)
1557 break;
1559 if (signal_pending(current)) {
1560 err = -ERESTARTSYS;
1561 break;
1564 SOCK_SLEEP_PRE(sk)
1566 if (scp->state != DN_RUN)
1567 schedule();
1569 SOCK_SLEEP_POST(sk)
1571 } while(scp->state != DN_RUN);
1573 return 0;
1577 static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
1579 struct sk_buff *skb = q->next;
1580 int len = 0;
1582 if (flags & MSG_OOB)
1583 return skb_queue_len(q) ? 1 : 0;
1585 while(skb != (struct sk_buff *)q) {
1586 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
1587 len += skb->len;
1589 if (cb->nsp_flags & 0x40) {
1590 /* SOCK_SEQPACKET reads to EOM */
1591 if (sk->type == SOCK_SEQPACKET)
1592 return 1;
1593 /* so does SOCK_STREAM unless WAITALL is specified */
1594 if (!(flags & MSG_WAITALL))
1595 return 1;
1598 /* minimum data length for read exceeded */
1599 if (len >= target)
1600 return 1;
1602 skb = skb->next;
1605 return 0;
1609 static int dn_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1610 int flags, struct scm_cookie *scm)
1612 struct sock *sk = sock->sk;
1613 struct dn_scp *scp = &sk->protinfo.dn;
1614 struct sk_buff_head *queue = &sk->receive_queue;
1615 int target = size > 1 ? 1 : 0;
1616 int copied = 0;
1617 int rv = 0;
1618 struct sk_buff *skb, *nskb;
1619 struct dn_skb_cb *cb = NULL;
1620 unsigned char eor = 0;
1622 lock_sock(sk);
1624 if (sk->zapped) {
1625 rv = -EADDRNOTAVAIL;
1626 goto out;
1629 if ((rv = dn_wait_run(sk, flags)) != 0)
1630 goto out;
1632 if (sk->shutdown & RCV_SHUTDOWN) {
1633 send_sig(SIGPIPE, current, 0);
1634 rv = -EPIPE;
1635 goto out;
1638 if (flags & ~(MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT)) {
1639 rv = -EOPNOTSUPP;
1640 goto out;
1643 if (flags & MSG_OOB)
1644 queue = &scp->other_receive_queue;
1646 if (flags & MSG_WAITALL)
1647 target = size;
1651 * See if there is data ready to read, sleep if there isn't
1653 for(;;) {
1654 if (sk->err)
1655 goto out;
1657 if (skb_queue_len(&scp->other_receive_queue)) {
1658 if (!(flags & MSG_OOB)) {
1659 msg->msg_flags |= MSG_OOB;
1660 if (!scp->other_report) {
1661 scp->other_report = 1;
1662 goto out;
1667 if (scp->state != DN_RUN)
1668 goto out;
1670 if (signal_pending(current)) {
1671 rv = -ERESTARTSYS;
1672 goto out;
1675 if (dn_data_ready(sk, queue, flags, target))
1676 break;
1678 if (flags & MSG_DONTWAIT) {
1679 rv = -EWOULDBLOCK;
1680 goto out;
1683 sock->flags |= SO_WAITDATA;
1684 SOCK_SLEEP_PRE(sk)
1686 if (!dn_data_ready(sk, queue, flags, target))
1687 schedule();
1689 SOCK_SLEEP_POST(sk)
1690 sock->flags &= ~SO_WAITDATA;
1693 for(skb = queue->next; skb != (struct sk_buff *)queue; skb = nskb) {
1694 int chunk = skb->len;
1695 cb = (struct dn_skb_cb *)skb->cb;
1697 if ((chunk + copied) > size)
1698 chunk = size - copied;
1700 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1701 rv = -EFAULT;
1702 break;
1704 copied += chunk;
1706 if (!(flags & MSG_PEEK))
1707 skb->len -= chunk;
1709 eor = cb->nsp_flags & 0x40;
1710 nskb = skb->next;
1712 if (skb->len == 0) {
1713 skb_unlink(skb);
1714 kfree_skb(skb);
1716 * N.B. Don't refer to skb or cb after this point
1717 * in loop.
1719 if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
1720 scp->flowloc_sw = DN_SEND;
1721 dn_nsp_send_lnk(sk, DN_SEND);
1725 if (eor) {
1726 if (sk->type == SOCK_SEQPACKET)
1727 break;
1728 if (!(flags & MSG_WAITALL))
1729 break;
1732 if (flags & MSG_OOB)
1733 break;
1735 if (copied >= target)
1736 break;
1739 rv = copied;
1741 if (eor && (sk->type == SOCK_SEQPACKET))
1742 msg->msg_flags |= MSG_EOR;
1744 out:
1745 if (rv == 0)
1746 rv = (flags & MSG_PEEK) ? -sk->err : sock_error(sk);
1748 if ((rv >= 0) && msg->msg_name) {
1749 memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
1750 msg->msg_namelen = sizeof(struct sockaddr_dn);
1753 release_sock(sk);
1755 return rv;
1759 static int dn_sendmsg(struct socket *sock, struct msghdr *msg, int size,
1760 struct scm_cookie *scm)
1762 struct sock *sk = sock->sk;
1763 struct dn_scp *scp = &sk->protinfo.dn;
1764 int mss = scp->mss;
1765 int mtu = 230 - 11; /* maximum value thats always safe */
1766 struct sk_buff_head *queue = &scp->data_xmit_queue;
1767 int flags = msg->msg_flags;
1768 unsigned short numseg = 0;
1769 int err = 0;
1770 int sent = 0;
1771 int addr_len = msg->msg_namelen;
1772 struct sockaddr_dn *addr = (struct sockaddr_dn *)msg->msg_name;
1773 struct sk_buff *skb = NULL;
1774 struct dn_skb_cb *cb;
1775 unsigned char msgflg;
1776 unsigned char *ptr;
1777 unsigned short ack;
1778 int len;
1780 if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR))
1781 return -EOPNOTSUPP;
1783 if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
1784 return -EINVAL;
1786 if (sk->zapped && dn_auto_bind(sock)) {
1787 err = -EADDRNOTAVAIL;
1788 goto out;
1791 if (scp->state == DN_O) {
1792 if (!addr_len || !addr) {
1793 err = -ENOTCONN;
1794 goto out;
1797 if ((err = dn_connect(sock, (struct sockaddr *)addr, addr_len, (flags & MSG_DONTWAIT) ? O_NONBLOCK : 0)) < 0)
1798 goto out;
1801 lock_sock(sk);
1803 if ((err = dn_wait_run(sk, flags)) < 0)
1804 goto out;
1806 if (sk->shutdown & SEND_SHUTDOWN) {
1807 send_sig(SIGPIPE, current, 0);
1808 err = -EPIPE;
1809 goto out;
1812 if ((flags & MSG_TRYHARD) && sk->dst_cache)
1813 dst_negative_advice(&sk->dst_cache);
1815 if (sk->dst_cache && sk->dst_cache->neighbour) {
1816 struct dn_neigh *dn = (struct dn_neigh *)sk->dst_cache->neighbour;
1817 if (dn->blksize > 230)
1818 mtu = dn->blksize - 11;
1822 * The only difference between SEQPACKET & STREAM sockets under DECnet
1823 * AFAIK is that SEQPACKET sockets set the MSG_EOR flag for the last
1824 * session control message segment.
1827 if (flags & MSG_OOB) {
1828 mss = 16;
1829 queue = &scp->other_xmit_queue;
1830 if (size > mss) {
1831 err = -EMSGSIZE;
1832 goto out;
1836 if (mss < mtu)
1837 mtu = mss;
1839 scp->persist_fxn = dn_nsp_xmit_timeout;
1841 while(sent < size) {
1842 if ((err = sock_error(sk) != 0))
1843 goto out;
1845 if (signal_pending(current)) {
1846 err = -ERESTARTSYS;
1847 goto out;
1851 * Calculate size that we wish to send.
1853 len = size - sent;
1855 if (len > mtu)
1856 len = mtu;
1859 * Wait for queue size to go down below the window
1860 * size.
1862 if (skb_queue_len(queue) >= scp->snd_window) {
1863 if (flags & MSG_DONTWAIT) {
1864 err = -EWOULDBLOCK;
1865 goto out;
1868 SOCK_SLEEP_PRE(sk)
1870 if (skb_queue_len(queue) >= scp->snd_window)
1871 schedule();
1873 SOCK_SLEEP_POST(sk)
1875 continue;
1879 * Get a suitably sized skb.
1881 skb = dn_alloc_send_skb(sk, &len, flags & MSG_DONTWAIT, &err);
1883 if (err)
1884 break;
1886 if (!skb)
1887 continue;
1889 cb = (struct dn_skb_cb *)skb->cb;
1891 ptr = skb_put(skb, 9);
1893 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1894 err = -EFAULT;
1895 goto out;
1898 if (flags & MSG_OOB) {
1899 cb->segnum = scp->numoth++;
1900 scp->numoth &= 0x0fff;
1901 msgflg = 0x30;
1902 ack = scp->ackxmt_oth | 0x8000;
1903 } else {
1904 cb->segnum = scp->numdat++;
1905 scp->numdat &= 0x0fff;
1906 msgflg = 0x00;
1907 if (sock->type == SOCK_STREAM)
1908 msgflg = 0x60;
1909 if (scp->seg_size == 0)
1910 msgflg |= 0x20;
1912 scp->seg_size += len;
1914 if (((sent + len) == size) && (flags & MSG_EOR)) {
1915 msgflg |= 0x40;
1916 scp->seg_size = 0;
1918 ack = scp->ackxmt_dat | 0x8000;
1921 *ptr++ = msgflg;
1922 *(__u16 *)ptr = scp->addrrem;
1923 ptr += 2;
1924 *(__u16 *)ptr = scp->addrloc;
1925 ptr += 2;
1926 *(__u16 *)ptr = dn_htons(ack);
1927 ptr += 2;
1928 *(__u16 *)ptr = dn_htons(cb->segnum);
1930 sent += len;
1931 dn_nsp_queue_xmit(sk, skb, flags & MSG_OOB);
1932 numseg++;
1933 skb = NULL;
1935 scp->persist = dn_nsp_persist(sk);
1938 out:
1940 if (skb)
1941 kfree_skb(skb);
1943 release_sock(sk);
1945 return sent ? sent : err;
1948 static int dn_device_event(struct notifier_block *this, unsigned long event,
1949 void *ptr)
1951 struct net_device *dev = (struct net_device *)ptr;
1953 switch(event) {
1954 case NETDEV_UP:
1955 dn_dev_up(dev);
1956 break;
1957 case NETDEV_DOWN:
1958 dn_dev_down(dev);
1959 break;
1960 default:
1961 break;
1964 return NOTIFY_DONE;
1967 static struct notifier_block dn_dev_notifier = {
1968 dn_device_event,
1972 extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *);
1974 static struct packet_type dn_dix_packet_type =
1976 __constant_htons(ETH_P_DNA_RT),
1977 NULL, /* All devices */
1978 dn_route_rcv,
1979 NULL,
1980 NULL,
1983 #ifdef CONFIG_PROC_FS
1985 static int dn_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1987 struct sock *sk;
1988 struct dn_scp *scp;
1989 int len = 0;
1990 off_t pos = 0;
1991 off_t begin = 0;
1992 char buf1[DN_ASCBUF_LEN];
1993 char buf2[DN_ASCBUF_LEN];
1995 len += sprintf(buffer + len, "Local Remote\n");
1997 read_lock(&dn_hash_lock);
1998 for(sk = dn_sklist; sk != NULL; sk = sk->next) {
1999 scp = &sk->protinfo.dn;
2001 len += sprintf(buffer + len,
2002 "%6s/%04X %04d:%04d %04d:%04d %01d %6s/%04X %04d:%04d %04d:%04d %01d %4s %s\n",
2003 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp->addr)), buf1),
2004 scp->addrloc,
2005 scp->numdat,
2006 scp->numoth,
2007 scp->ackxmt_dat,
2008 scp->ackxmt_oth,
2009 scp->flowloc_sw,
2010 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp->peer)), buf2),
2011 scp->addrrem,
2012 scp->numdat_rcv,
2013 scp->numoth_rcv,
2014 scp->ackrcv_dat,
2015 scp->ackrcv_oth,
2016 scp->flowrem_sw,
2017 dn_state2asc(scp->state),
2018 ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER"));
2020 pos = begin + len;
2021 if (pos < offset) {
2022 len = 0;
2023 begin = pos;
2025 if (pos > (offset + length))
2026 break;
2028 read_unlock(&dn_hash_lock);
2030 *start = buffer + (offset - begin);
2031 len -= (offset - begin);
2033 if (len > length)
2034 len = length;
2036 return len;
2039 struct proc_dir_entry decnet_linkinfo = {
2040 PROC_NET_DN_SKT, 6, "decnet", S_IFREG | S_IRUGO,
2041 1, 0, 0, 0, &proc_net_inode_operations, dn_get_info
2044 #ifdef CONFIG_DECNET_RAW
2046 extern int dn_raw_get_info(char *, char **, off_t, int, int);
2048 struct proc_dir_entry decnet_rawinfo = {
2049 PROC_NET_DN_RAW, 10, "decnet_raw", S_IFREG | S_IRUGO,
2050 1, 0, 0, 0, &proc_net_inode_operations, dn_raw_get_info
2053 #endif /* CONFIG_DECNET_RAW */
2054 #endif /* CONFIG_PROC_FS */
2055 static struct net_proto_family dn_family_ops = {
2056 AF_DECnet,
2057 dn_create
2060 static struct proto_ops dn_proto_ops = {
2061 AF_DECnet,
2063 dn_release,
2064 dn_bind,
2065 dn_connect,
2066 sock_no_socketpair,
2067 dn_accept,
2068 dn_getname,
2069 dn_poll,
2070 dn_ioctl,
2071 dn_listen,
2072 dn_shutdown,
2073 dn_setsockopt,
2074 dn_getsockopt,
2075 sock_no_fcntl,
2076 dn_sendmsg,
2077 dn_recvmsg,
2078 sock_no_mmap
2081 #ifdef CONFIG_SYSCTL
2082 void dn_register_sysctl(void);
2083 void dn_unregister_sysctl(void);
2084 #endif
2086 void __init decnet_proto_init(struct net_proto *pro)
2088 sock_register(&dn_family_ops);
2089 dev_add_pack(&dn_dix_packet_type);
2090 register_netdevice_notifier(&dn_dev_notifier);
2092 #ifdef CONFIG_PROC_FS
2093 proc_net_register(&decnet_linkinfo);
2094 #ifdef CONFIG_DECNET_RAW
2095 proc_net_register(&decnet_rawinfo);
2096 #endif
2097 #endif
2098 dn_dev_init();
2099 dn_neigh_init();
2100 dn_route_init();
2102 #ifdef CONFIG_DECNET_FW
2103 dn_fw_init();
2104 #endif /* CONFIG_DECNET_FW */
2106 #ifdef CONFIG_DECNET_ROUTER
2107 dn_fib_init();
2108 #endif /* CONFIG_DECNET_ROUTER */
2110 #ifdef CONFIG_SYSCTL
2111 dn_register_sysctl();
2112 #endif /* CONFIG_SYSCTL */
2113 printk(KERN_INFO "NET4: DECnet for Linux: V.2.3.15s (C) 1995-1999 Linux DECnet Project Team\n");
2117 #ifndef MODULE
2118 static int __init decnet_setup(char *str)
2120 unsigned short area = simple_strtoul(str, &str, 0);
2121 unsigned short node = simple_strtoul(*str > 0 ? ++str : str, &str, 0);
2122 unsigned short type = simple_strtoul(*str > 0 ? ++str : str, &str, 0);
2124 decnet_address = dn_htons(area << 10 | node);
2125 dn_dn2eth(decnet_ether_address, dn_ntohs(decnet_address));
2127 switch(type) {
2128 default:
2129 printk(KERN_INFO "Invalid DECnet node type, switching to EndNode\n");
2130 case 0:
2131 decnet_node_type = DN_RT_INFO_ENDN;
2132 break;
2133 #ifdef CONFIG_DECNET_ROUTER
2134 case 1:
2135 decnet_node_type = DN_RT_INFO_L1RT;
2136 break;
2137 case 2:
2138 decnet_node_type = DN_RT_INFO_L2RT;
2139 break;
2140 #endif /* CONFIG_DECNET_ROUTER */
2143 return 0;
2146 __setup("decnet=", decnet_setup);
2147 #endif
2149 #ifdef MODULE
2150 EXPORT_NO_SYMBOLS;
2151 MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
2152 MODULE_AUTHOR("Linux DECnet Project Team");
2154 static int addr[2] = {0, 0};
2155 #ifdef CONFIG_DECNET_ROUTER
2156 static int type = 0;
2157 #endif
2159 MODULE_PARM(addr, "2i");
2160 MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
2161 #ifdef CONFIG_DECNET_ROUTER
2162 MODULE_PARM(type, "i");
2163 MODULE_PARM_DESC(type, "The type of this DECnet node: 0=EndNode, 1,2=Router");
2164 #endif
2166 int init_module(void)
2168 if (addr[0] > 63 || addr[0] < 0) {
2169 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
2170 return 1;
2173 if (addr[1] > 1023 || addr[1] < 0) {
2174 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
2175 return 1;
2178 decnet_address = dn_htons((addr[0] << 10) | addr[1]);
2179 dn_dn2eth(decnet_ether_address, dn_ntohs(decnet_address));
2181 #ifdef CONFIG_DECNET_ROUTER
2182 switch(type) {
2183 case 0:
2184 decnet_node_type = DN_RT_INFO_ENDN;
2185 break;
2186 case 1:
2187 decnet_node_type = DN_RT_INFO_L1RT;
2188 break;
2189 case 2:
2190 decnet_node_type = DN_RT_INFO_L2RT;
2191 break;
2192 default:
2193 printk(KERN_ERR "DECnet: Node type must be between 0 and 2 inclusive\n");
2194 return 1;
2196 #else
2197 decnet_node_type = DN_RT_INFO_ENDN;
2198 #endif
2200 decnet_proto_init(NULL);
2202 return 0;
2205 void cleanup_module(void)
2207 #ifdef CONFIG_SYSCTL
2208 dn_unregister_sysctl();
2209 #endif /* CONFIG_SYSCTL */
2211 unregister_netdevice_notifier(&dn_dev_notifier);
2213 dn_route_cleanup();
2214 dn_neigh_cleanup();
2215 dn_dev_cleanup();
2217 #ifdef CONFIG_DECNET_FW
2218 /* dn_fw_cleanup(); */
2219 #endif /* CONFIG_DECNET_FW */
2221 #ifdef CONFIG_DECNET_ROUTER
2222 dn_fib_cleanup();
2223 #endif /* CONFIG_DECNET_ROUTER */
2225 #ifdef CONFIG_PROC_FS
2226 proc_net_unregister(PROC_NET_DN_SKT);
2227 #ifdef CONFIG_DECNET_RAW
2228 proc_net_unregister(PROC_NET_DN_RAW);
2229 #endif
2230 #endif
2232 dev_remove_pack(&dn_dix_packet_type);
2233 sock_unregister(AF_DECnet);
2236 #endif