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>
13 * Steve Whitehouse: Copied from Eduardo Serrat and Patrick Caulfield's
14 * version of the code. Original copyright preserved
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
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
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
38 * Patrick Caulfield: /proc/net/decnet now has object name/number
39 * Steve Whitehouse: Fixed local port allocation, hashed sk list
40 * Matthew Wilcox: Fixes for dn_ioctl()
41 * Steve Whitehouse: New connect/accept logic to allow timeouts and
42 * prepare for sendpage etc.
46 /******************************************************************************
47 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
49 This program is free software; you can redistribute it and/or modify
50 it under the terms of the GNU General Public License as published by
51 the Free Software Foundation; either version 2 of the License, or
54 This program is distributed in the hope that it will be useful,
55 but WITHOUT ANY WARRANTY; without even the implied warranty of
56 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 GNU General Public License for more details.
61 Version Kernel Date Author/Comments
62 ------- ------ ---- ---------------
63 Version 0.0.1 2.0.30 01-dic-97 Eduardo Marcelo Serrat
64 (emserrat@geocities.com)
66 First Development of DECnet Socket La-
67 yer for Linux. Only supports outgoing
70 Version 0.0.2 2.1.105 20-jun-98 Patrick J. Caulfield
71 (patrick@pandh.demon.co.uk)
73 Port to new kernel development version.
75 Version 0.0.3 2.1.106 25-jun-98 Eduardo Marcelo Serrat
76 (emserrat@geocities.com)
78 Added support for incoming connections
79 so we can start developing server apps
83 Version 0.0.4 2.1.109 21-jul-98 Eduardo Marcelo Serrat
84 (emserrat@geocities.com)
86 Added support for X11R6.4. Now we can
87 use DECnet transport for X on Linux!!!
89 Version 0.0.5 2.1.110 01-aug-98 Eduardo Marcelo Serrat
90 (emserrat@geocities.com)
91 Removed bugs on flow control
92 Removed bugs on incoming accessdata
95 Version 0.0.6 2.1.110 07-aug-98 Eduardo Marcelo Serrat
100 *******************************************************************************/
102 #include <linux/config.h>
103 #include <linux/module.h>
104 #include <linux/errno.h>
105 #include <linux/types.h>
106 #include <linux/slab.h>
107 #include <linux/socket.h>
108 #include <linux/in.h>
109 #include <linux/kernel.h>
110 #include <linux/sched.h>
111 #include <linux/timer.h>
112 #include <linux/string.h>
113 #include <linux/sockios.h>
114 #include <linux/net.h>
115 #include <linux/netdevice.h>
116 #include <linux/inet.h>
117 #include <linux/route.h>
118 #include <linux/netfilter.h>
119 #include <linux/seq_file.h>
120 #include <net/sock.h>
122 #include <net/flow.h>
123 #include <asm/system.h>
124 #include <asm/ioctls.h>
125 #include <linux/mm.h>
126 #include <linux/interrupt.h>
127 #include <linux/proc_fs.h>
128 #include <linux/stat.h>
129 #include <linux/init.h>
130 #include <linux/poll.h>
131 #include <net/neighbour.h>
134 #include <net/dn_nsp.h>
135 #include <net/dn_dev.h>
136 #include <net/dn_route.h>
137 #include <net/dn_fib.h>
138 #include <net/dn_neigh.h>
145 static void dn_keepalive(struct sock *sk);
147 #define DN_SK_HASH_SHIFT 8
148 #define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
149 #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
152 static struct proto_ops dn_proto_ops;
153 static DEFINE_RWLOCK(dn_hash_lock);
154 static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
155 static struct hlist_head dn_wild_sk;
157 static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen, int flags);
158 static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);
160 static struct hlist_head *dn_find_list(struct sock *sk)
162 struct dn_scp *scp = DN_SK(sk);
164 if (scp->addr.sdn_flags & SDF_WILD)
165 return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL;
167 return &dn_sk_hash[scp->addrloc & DN_SK_HASH_MASK];
171 * Valid ports are those greater than zero and not already in use.
173 static int check_port(unsigned short port)
176 struct hlist_node *node;
181 sk_for_each(sk, node, &dn_sk_hash[port & DN_SK_HASH_MASK]) {
182 struct dn_scp *scp = DN_SK(sk);
183 if (scp->addrloc == port)
189 static unsigned short port_alloc(struct sock *sk)
191 struct dn_scp *scp = DN_SK(sk);
192 static unsigned short port = 0x2000;
193 unsigned short i_port = port;
195 while(check_port(++port) != 0) {
206 * Since this is only ever called from user
207 * level, we don't need a write_lock() version
210 static int dn_hash_sock(struct sock *sk)
212 struct dn_scp *scp = DN_SK(sk);
213 struct hlist_head *list;
216 BUG_ON(sk_hashed(sk));
218 write_lock_bh(&dn_hash_lock);
220 if (!scp->addrloc && !port_alloc(sk))
224 if ((list = dn_find_list(sk)) == NULL)
227 sk_add_node(sk, list);
230 write_unlock_bh(&dn_hash_lock);
234 static void dn_unhash_sock(struct sock *sk)
236 write_lock(&dn_hash_lock);
237 sk_del_node_init(sk);
238 write_unlock(&dn_hash_lock);
241 static void dn_unhash_sock_bh(struct sock *sk)
243 write_lock_bh(&dn_hash_lock);
244 sk_del_node_init(sk);
245 write_unlock_bh(&dn_hash_lock);
248 static struct hlist_head *listen_hash(struct sockaddr_dn *addr)
251 unsigned hash = addr->sdn_objnum;
254 hash = addr->sdn_objnamel;
255 for(i = 0; i < dn_ntohs(addr->sdn_objnamel); i++) {
256 hash ^= addr->sdn_objname[i];
261 return &dn_sk_hash[hash & DN_SK_HASH_MASK];
265 * Called to transform a socket from bound (i.e. with a local address)
266 * into a listening socket (doesn't need a local port number) and rehashes
267 * based upon the object name/number.
269 static void dn_rehash_sock(struct sock *sk)
271 struct hlist_head *list;
272 struct dn_scp *scp = DN_SK(sk);
274 if (scp->addr.sdn_flags & SDF_WILD)
277 write_lock_bh(&dn_hash_lock);
278 sk_del_node_init(sk);
279 DN_SK(sk)->addrloc = 0;
280 list = listen_hash(&DN_SK(sk)->addr);
281 sk_add_node(sk, list);
282 write_unlock_bh(&dn_hash_lock);
285 int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
293 *buf++ = sdn->sdn_objnum;
297 *buf++ = dn_ntohs(sdn->sdn_objnamel);
298 memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
299 len = 3 + dn_ntohs(sdn->sdn_objnamel);
304 *buf++ = dn_ntohs(sdn->sdn_objnamel);
305 memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
306 len = 7 + dn_ntohs(sdn->sdn_objnamel);
314 * On reception of usernames, we handle types 1 and 0 for destination
315 * addresses only. Types 2 and 4 are used for source addresses, but the
316 * UIC, GIC are ignored and they are both treated the same way. Type 3
317 * is never used as I've no idea what its purpose might be or what its
320 int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
327 sdn->sdn_objnamel = dn_htons(0);
328 memset(sdn->sdn_objname, 0, DN_MAXOBJL);
339 sdn->sdn_objnum = type;
361 sdn->sdn_objnamel = dn_htons(*data++);
362 len -= dn_ntohs(sdn->sdn_objnamel);
364 if ((len < 0) || (dn_ntohs(sdn->sdn_objnamel) > namel))
367 memcpy(sdn->sdn_objname, data, dn_ntohs(sdn->sdn_objnamel));
372 struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
374 struct hlist_head *list = listen_hash(addr);
375 struct hlist_node *node;
378 read_lock(&dn_hash_lock);
379 sk_for_each(sk, node, list) {
380 struct dn_scp *scp = DN_SK(sk);
381 if (sk->sk_state != TCP_LISTEN)
383 if (scp->addr.sdn_objnum) {
384 if (scp->addr.sdn_objnum != addr->sdn_objnum)
387 if (addr->sdn_objnum)
389 if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
391 if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, dn_ntohs(addr->sdn_objnamel)) != 0)
395 read_unlock(&dn_hash_lock);
399 sk = sk_head(&dn_wild_sk);
401 if (sk->sk_state == TCP_LISTEN)
407 read_unlock(&dn_hash_lock);
411 struct sock *dn_find_by_skb(struct sk_buff *skb)
413 struct dn_skb_cb *cb = DN_SKB_CB(skb);
415 struct hlist_node *node;
418 read_lock(&dn_hash_lock);
419 sk_for_each(sk, node, &dn_sk_hash[cb->dst_port & DN_SK_HASH_MASK]) {
421 if (cb->src != dn_saddr2dn(&scp->peer))
423 if (cb->dst_port != scp->addrloc)
425 if (scp->addrrem && (cb->src_port != scp->addrrem))
432 read_unlock(&dn_hash_lock);
438 static void dn_destruct(struct sock *sk)
440 struct dn_scp *scp = DN_SK(sk);
442 skb_queue_purge(&scp->data_xmit_queue);
443 skb_queue_purge(&scp->other_xmit_queue);
444 skb_queue_purge(&scp->other_receive_queue);
446 dst_release(xchg(&sk->sk_dst_cache, NULL));
449 static struct proto dn_proto = {
451 .owner = THIS_MODULE,
452 .obj_size = sizeof(struct dn_sock),
455 static struct sock *dn_alloc_sock(struct socket *sock, int gfp)
458 struct sock *sk = sk_alloc(PF_DECnet, gfp, &dn_proto, 1);
464 sock->ops = &dn_proto_ops;
465 sock_init_data(sock, sk);
467 sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
468 sk->sk_destruct = dn_destruct;
470 sk->sk_family = PF_DECnet;
472 sk->sk_allocation = gfp;
474 /* Initialization of DECnet Session Control Port */
476 scp->state = DN_O; /* Open */
477 scp->numdat = 1; /* Next data seg to tx */
478 scp->numoth = 1; /* Next oth data to tx */
479 scp->ackxmt_dat = 0; /* Last data seg ack'ed */
480 scp->ackxmt_oth = 0; /* Last oth data ack'ed */
481 scp->ackrcv_dat = 0; /* Highest data ack recv*/
482 scp->ackrcv_oth = 0; /* Last oth data ack rec*/
483 scp->flowrem_sw = DN_SEND;
484 scp->flowloc_sw = DN_SEND;
485 scp->flowrem_dat = 0;
486 scp->flowrem_oth = 1;
487 scp->flowloc_dat = 0;
488 scp->flowloc_oth = 1;
489 scp->services_rem = 0;
490 scp->services_loc = 1 | NSP_FC_NONE;
492 scp->info_loc = 0x03; /* NSP version 4.1 */
493 scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
496 scp->accept_mode = ACC_IMMED;
497 scp->addr.sdn_family = AF_DECnet;
498 scp->peer.sdn_family = AF_DECnet;
499 scp->accessdata.acc_accl = 5;
500 memcpy(scp->accessdata.acc_acc, "LINUX", 5);
502 scp->max_window = NSP_MAX_WINDOW;
503 scp->snd_window = NSP_MIN_WINDOW;
504 scp->nsp_srtt = NSP_INITIAL_SRTT;
505 scp->nsp_rttvar = NSP_INITIAL_RTTVAR;
506 scp->nsp_rxtshift = 0;
508 skb_queue_head_init(&scp->data_xmit_queue);
509 skb_queue_head_init(&scp->other_xmit_queue);
510 skb_queue_head_init(&scp->other_receive_queue);
513 scp->persist_fxn = NULL;
514 scp->keepalive = 10 * HZ;
515 scp->keepalive_fxn = dn_keepalive;
517 init_timer(&scp->delack_timer);
518 scp->delack_pending = 0;
519 scp->delack_fxn = dn_nsp_delayed_ack;
521 dn_start_slow_timer(sk);
528 * FIXME: Should respond to SO_KEEPALIVE etc.
530 static void dn_keepalive(struct sock *sk)
532 struct dn_scp *scp = DN_SK(sk);
535 * By checking the other_data transmit queue is empty
536 * we are double checking that we are not sending too
537 * many of these keepalive frames.
539 if (skb_queue_empty(&scp->other_xmit_queue))
540 dn_nsp_send_link(sk, DN_NOCHANGE, 0);
545 * Timer for shutdown/destroyed sockets.
546 * When socket is dead & no packets have been sent for a
547 * certain amount of time, they are removed by this
548 * routine. Also takes care of sending out DI & DC
549 * frames at correct times.
551 int dn_destroy_timer(struct sock *sk)
553 struct dn_scp *scp = DN_SK(sk);
555 scp->persist = dn_nsp_persist(sk);
559 dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
560 if (scp->nsp_rxtshift >= decnet_di_count)
565 dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
566 if (scp->nsp_rxtshift >= decnet_dr_count)
571 if (scp->nsp_rxtshift < decnet_dn_count) {
572 /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
573 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
578 scp->persist = (HZ * decnet_time_wait);
583 if ((jiffies - scp->stamp) >= (HZ * decnet_time_wait)) {
592 static void dn_destroy_sock(struct sock *sk)
594 struct dn_scp *scp = DN_SK(sk);
596 scp->nsp_rxtshift = 0; /* reset back off */
599 if (sk->sk_socket->state != SS_UNCONNECTED)
600 sk->sk_socket->state = SS_DISCONNECTING;
603 sk->sk_state = TCP_CLOSE;
607 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
609 scp->persist_fxn = dn_destroy_timer;
610 scp->persist = dn_nsp_persist(sk);
620 dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation);
629 scp->persist_fxn = dn_destroy_timer;
630 scp->persist = dn_nsp_persist(sk);
633 printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
635 dn_stop_slow_timer(sk);
637 dn_unhash_sock_bh(sk);
644 char *dn_addr2asc(dn_address addr, char *buf)
646 unsigned short node, area;
648 node = addr & 0x03ff;
650 sprintf(buf, "%hd.%hd", area, node);
657 static int dn_create(struct socket *sock, int protocol)
663 if (protocol != DNPROTO_NSP)
664 return -EPROTONOSUPPORT;
669 return -ESOCKTNOSUPPORT;
673 if ((sk = dn_alloc_sock(sock, GFP_KERNEL)) == NULL)
676 sk->sk_protocol = protocol;
683 dn_release(struct socket *sock)
685 struct sock *sk = sock->sk;
699 static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
701 struct sock *sk = sock->sk;
702 struct dn_scp *scp = DN_SK(sk);
703 struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
704 struct net_device *dev;
707 if (addr_len != sizeof(struct sockaddr_dn))
710 if (saddr->sdn_family != AF_DECnet)
713 if (dn_ntohs(saddr->sdn_nodeaddrl) && (dn_ntohs(saddr->sdn_nodeaddrl) != 2))
716 if (dn_ntohs(saddr->sdn_objnamel) > DN_MAXOBJL)
719 if (saddr->sdn_flags & ~SDF_WILD)
723 if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
724 (saddr->sdn_flags & SDF_WILD)))
728 * Maybe put the default actions in the default security ops for
729 * dn_prot_sock ? Would be nice if the capable call would go there
732 if (security_dn_prot_sock(saddr) &&
733 !capable(CAP_NET_BIND_SERVICE) ||
734 saddr->sdn_objnum || (saddr->sdn_flags & SDF_WILD))
739 if (!(saddr->sdn_flags & SDF_WILD)) {
740 if (dn_ntohs(saddr->sdn_nodeaddrl)) {
741 read_lock(&dev_base_lock);
742 for(dev = dev_base; dev; dev = dev->next) {
745 if (dn_dev_islocal(dev, dn_saddr2dn(saddr)))
748 read_unlock(&dev_base_lock);
750 return -EADDRNOTAVAIL;
756 if (sock_flag(sk, SOCK_ZAPPED)) {
757 memcpy(&scp->addr, saddr, addr_len);
758 sock_reset_flag(sk, SOCK_ZAPPED);
760 rv = dn_hash_sock(sk);
762 sock_set_flag(sk, SOCK_ZAPPED);
770 static int dn_auto_bind(struct socket *sock)
772 struct sock *sk = sock->sk;
773 struct dn_scp *scp = DN_SK(sk);
776 sock_reset_flag(sk, SOCK_ZAPPED);
778 scp->addr.sdn_flags = 0;
779 scp->addr.sdn_objnum = 0;
782 * This stuff is to keep compatibility with Eduardo's
783 * patch. I hope I can dispense with it shortly...
785 if ((scp->accessdata.acc_accl != 0) &&
786 (scp->accessdata.acc_accl <= 12)) {
788 scp->addr.sdn_objnamel = dn_htons(scp->accessdata.acc_accl);
789 memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, dn_ntohs(scp->addr.sdn_objnamel));
791 scp->accessdata.acc_accl = 0;
792 memset(scp->accessdata.acc_acc, 0, 40);
794 /* End of compatibility stuff */
796 scp->addr.sdn_add.a_len = dn_htons(2);
797 rv = dn_dev_bind_default((dn_address *)scp->addr.sdn_add.a_addr);
799 rv = dn_hash_sock(sk);
801 sock_set_flag(sk, SOCK_ZAPPED);
807 static int dn_confirm_accept(struct sock *sk, long *timeo, int allocation)
809 struct dn_scp *scp = DN_SK(sk);
813 if (scp->state != DN_CR)
817 scp->segsize_loc = dst_metric(__sk_dst_get(sk), RTAX_ADVMSS);
818 dn_send_conn_conf(sk, allocation);
820 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
823 if (scp->state == DN_CC)
824 *timeo = schedule_timeout(*timeo);
827 if (scp->state == DN_RUN)
829 err = sock_error(sk);
832 err = sock_intr_errno(*timeo);
833 if (signal_pending(current))
838 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
840 finish_wait(sk->sk_sleep, &wait);
842 sk->sk_socket->state = SS_CONNECTED;
843 } else if (scp->state != DN_CC) {
844 sk->sk_socket->state = SS_UNCONNECTED;
849 static int dn_wait_run(struct sock *sk, long *timeo)
851 struct dn_scp *scp = DN_SK(sk);
855 if (scp->state == DN_RUN)
861 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
864 if (scp->state == DN_CI || scp->state == DN_CC)
865 *timeo = schedule_timeout(*timeo);
868 if (scp->state == DN_RUN)
870 err = sock_error(sk);
873 err = sock_intr_errno(*timeo);
874 if (signal_pending(current))
879 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
881 finish_wait(sk->sk_sleep, &wait);
884 sk->sk_socket->state = SS_CONNECTED;
885 } else if (scp->state != DN_CI && scp->state != DN_CC) {
886 sk->sk_socket->state = SS_UNCONNECTED;
891 static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
893 struct socket *sock = sk->sk_socket;
894 struct dn_scp *scp = DN_SK(sk);
898 if (sock->state == SS_CONNECTED)
901 if (sock->state == SS_CONNECTING) {
903 if (scp->state == DN_RUN) {
904 sock->state = SS_CONNECTED;
908 if (scp->state != DN_CI && scp->state != DN_CC) {
909 sock->state = SS_UNCONNECTED;
912 return dn_wait_run(sk, timeo);
916 if (scp->state != DN_O)
919 if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
921 if (addr->sdn_family != AF_DECnet)
923 if (addr->sdn_flags & SDF_WILD)
926 if (sock_flag(sk, SOCK_ZAPPED)) {
927 err = dn_auto_bind(sk->sk_socket);
932 memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));
935 memset(&fl, 0, sizeof(fl));
936 fl.oif = sk->sk_bound_dev_if;
937 fl.fld_dst = dn_saddr2dn(&scp->peer);
938 fl.fld_src = dn_saddr2dn(&scp->addr);
939 dn_sk_ports_copy(&fl, scp);
940 fl.proto = DNPROTO_NSP;
941 if (dn_route_output_sock(&sk->sk_dst_cache, &fl, sk, flags) < 0)
943 sk->sk_route_caps = sk->sk_dst_cache->dev->features;
944 sock->state = SS_CONNECTING;
946 scp->segsize_loc = dst_metric(sk->sk_dst_cache, RTAX_ADVMSS);
948 dn_nsp_send_conninit(sk, NSP_CI);
951 err = dn_wait_run(sk, timeo);
957 static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags)
959 struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
960 struct sock *sk = sock->sk;
962 long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
965 err = __dn_connect(sk, addr, addrlen, &timeo, 0);
971 static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
973 struct dn_scp *scp = DN_SK(sk);
979 return dn_confirm_accept(sk, timeo, sk->sk_allocation);
982 return dn_wait_run(sk, timeo);
984 return __dn_connect(sk, addr, addrlen, timeo, flags);
991 static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
993 unsigned char *ptr = skb->data;
995 acc->acc_userl = *ptr++;
996 memcpy(&acc->acc_user, ptr, acc->acc_userl);
997 ptr += acc->acc_userl;
999 acc->acc_passl = *ptr++;
1000 memcpy(&acc->acc_pass, ptr, acc->acc_passl);
1001 ptr += acc->acc_passl;
1003 acc->acc_accl = *ptr++;
1004 memcpy(&acc->acc_acc, ptr, acc->acc_accl);
1006 skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
1010 static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
1012 unsigned char *ptr = skb->data;
1014 opt->opt_optl = *ptr++;
1015 opt->opt_status = 0;
1016 memcpy(opt->opt_data, ptr, opt->opt_optl);
1017 skb_pull(skb, opt->opt_optl + 1);
1021 static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo)
1024 struct sk_buff *skb = NULL;
1027 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1030 skb = skb_dequeue(&sk->sk_receive_queue);
1032 *timeo = schedule_timeout(*timeo);
1033 skb = skb_dequeue(&sk->sk_receive_queue);
1039 if (sk->sk_state != TCP_LISTEN)
1041 err = sock_intr_errno(*timeo);
1042 if (signal_pending(current))
1047 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1049 finish_wait(sk->sk_sleep, &wait);
1051 return skb == NULL ? ERR_PTR(err) : skb;
1054 static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
1056 struct sock *sk = sock->sk, *newsk;
1057 struct sk_buff *skb = NULL;
1058 struct dn_skb_cb *cb;
1059 unsigned char menuver;
1062 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1066 if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) {
1071 skb = skb_dequeue(&sk->sk_receive_queue);
1073 skb = dn_wait_for_connect(sk, &timeo);
1076 return PTR_ERR(skb);
1080 cb = DN_SKB_CB(skb);
1081 sk->sk_ack_backlog--;
1082 newsk = dn_alloc_sock(newsock, sk->sk_allocation);
1083 if (newsk == NULL) {
1090 dst_release(xchg(&newsk->sk_dst_cache, skb->dst));
1093 DN_SK(newsk)->state = DN_CR;
1094 DN_SK(newsk)->addrrem = cb->src_port;
1095 DN_SK(newsk)->services_rem = cb->services;
1096 DN_SK(newsk)->info_rem = cb->info;
1097 DN_SK(newsk)->segsize_rem = cb->segsize;
1098 DN_SK(newsk)->accept_mode = DN_SK(sk)->accept_mode;
1100 if (DN_SK(newsk)->segsize_rem < 230)
1101 DN_SK(newsk)->segsize_rem = 230;
1103 if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
1104 DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd;
1106 newsk->sk_state = TCP_LISTEN;
1107 memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn));
1110 * If we are listening on a wild socket, we don't want
1111 * the newly created socket on the wrong hash queue.
1113 DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD;
1115 skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
1116 skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
1117 *(dn_address *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
1118 *(dn_address *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
1120 menuver = *skb->data;
1123 if (menuver & DN_MENUVER_ACC)
1124 dn_access_copy(skb, &(DN_SK(newsk)->accessdata));
1126 if (menuver & DN_MENUVER_USR)
1127 dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));
1129 if (menuver & DN_MENUVER_PRX)
1130 DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY;
1132 if (menuver & DN_MENUVER_UIC)
1133 DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY;
1137 memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
1138 sizeof(struct optdata_dn));
1139 memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
1140 sizeof(struct optdata_dn));
1143 err = dn_hash_sock(newsk);
1145 sock_reset_flag(newsk, SOCK_ZAPPED);
1146 dn_send_conn_ack(newsk);
1149 * Here we use sk->sk_allocation since although the conn conf is
1150 * for the newsk, the context is the old socket.
1152 if (DN_SK(newsk)->accept_mode == ACC_IMMED)
1153 err = dn_confirm_accept(newsk, &timeo,
1156 release_sock(newsk);
1161 static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int *uaddr_len,int peer)
1163 struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
1164 struct sock *sk = sock->sk;
1165 struct dn_scp *scp = DN_SK(sk);
1167 *uaddr_len = sizeof(struct sockaddr_dn);
1172 if ((sock->state != SS_CONNECTED &&
1173 sock->state != SS_CONNECTING) &&
1174 scp->accept_mode == ACC_IMMED)
1177 memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
1179 memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
1188 static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table *wait)
1190 struct sock *sk = sock->sk;
1191 struct dn_scp *scp = DN_SK(sk);
1192 int mask = datagram_poll(file, sock, wait);
1194 if (!skb_queue_empty(&scp->other_receive_queue))
1200 static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1202 struct sock *sk = sock->sk;
1203 struct dn_scp *scp = DN_SK(sk);
1204 int err = -EOPNOTSUPP;
1206 struct sk_buff *skb;
1213 return dn_dev_ioctl(cmd, (void __user *)arg);
1217 val = !skb_queue_empty(&scp->other_receive_queue);
1218 if (scp->state != DN_RUN)
1224 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1227 err = put_user(amount, (int __user *)arg);
1232 if ((skb = skb_peek(&scp->other_receive_queue)) != NULL) {
1235 struct sk_buff *skb = sk->sk_receive_queue.next;
1238 (struct sk_buff *)&sk->sk_receive_queue)
1245 err = put_user(amount, (int __user *)arg);
1249 err = dev_ioctl(cmd, (void __user *)arg);
1256 static int dn_listen(struct socket *sock, int backlog)
1258 struct sock *sk = sock->sk;
1263 if (sock_flag(sk, SOCK_ZAPPED))
1266 if ((DN_SK(sk)->state != DN_O) || (sk->sk_state == TCP_LISTEN))
1269 sk->sk_max_ack_backlog = backlog;
1270 sk->sk_ack_backlog = 0;
1271 sk->sk_state = TCP_LISTEN;
1282 static int dn_shutdown(struct socket *sock, int how)
1284 struct sock *sk = sock->sk;
1285 struct dn_scp *scp = DN_SK(sk);
1286 int err = -ENOTCONN;
1290 if (sock->state == SS_UNCONNECTED)
1294 if (sock->state == SS_DISCONNECTING)
1298 if (scp->state == DN_O)
1301 if (how != SHUTDOWN_MASK)
1304 sk->sk_shutdown = how;
1305 dn_destroy_sock(sk);
1314 static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1316 struct sock *sk = sock->sk;
1320 err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
1326 static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, int optlen, int flags)
1328 struct sock *sk = sock->sk;
1329 struct dn_scp *scp = DN_SK(sk);
1332 struct optdata_dn opt;
1333 struct accessdata_dn acc;
1337 unsigned char services;
1342 if (optlen && !optval)
1345 if (optlen > sizeof(u))
1348 if (copy_from_user(&u, optval, optlen))
1353 if (sock->state == SS_CONNECTED)
1355 if ((scp->state != DN_O) && (scp->state != DN_CR))
1358 if (optlen != sizeof(struct optdata_dn))
1361 if (u.opt.opt_optl > 16)
1364 memcpy(&scp->conndata_out, &u.opt, optlen);
1368 if (sock->state != SS_CONNECTED && scp->accept_mode == ACC_IMMED)
1371 if (optlen != sizeof(struct optdata_dn))
1374 if (u.opt.opt_optl > 16)
1377 memcpy(&scp->discdata_out, &u.opt, optlen);
1381 if (sock->state == SS_CONNECTED)
1383 if (scp->state != DN_O)
1386 if (optlen != sizeof(struct accessdata_dn))
1389 if ((u.acc.acc_accl > DN_MAXACCL) ||
1390 (u.acc.acc_passl > DN_MAXACCL) ||
1391 (u.acc.acc_userl > DN_MAXACCL))
1394 memcpy(&scp->accessdata, &u.acc, optlen);
1397 case DSO_ACCEPTMODE:
1398 if (sock->state == SS_CONNECTED)
1400 if (scp->state != DN_O)
1403 if (optlen != sizeof(int))
1406 if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
1409 scp->accept_mode = (unsigned char)u.mode;
1414 if (scp->state != DN_CR)
1416 timeo = sock_rcvtimeo(sk, 0);
1417 err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
1422 if (scp->state != DN_CR)
1426 sk->sk_shutdown = SHUTDOWN_MASK;
1427 dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
1431 #ifdef CONFIG_NETFILTER
1432 return nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
1437 return -ENOPROTOOPT;
1440 if (optlen != sizeof(unsigned long))
1442 if (u.win > NSP_MAX_WINDOW)
1443 u.win = NSP_MAX_WINDOW;
1446 scp->max_window = u.win;
1447 if (scp->snd_window > u.win)
1448 scp->snd_window = u.win;
1452 if (optlen != sizeof(int))
1454 if (scp->nonagle == 2)
1456 scp->nonagle = (u.val == 0) ? 0 : 1;
1457 /* if (scp->nonagle == 1) { Push pending frames } */
1461 if (optlen != sizeof(int))
1463 if (scp->nonagle == 1)
1465 scp->nonagle = (u.val == 0) ? 0 : 2;
1466 /* if (scp->nonagle == 0) { Push pending frames } */
1470 if (optlen != sizeof(unsigned char))
1472 if ((u.services & ~NSP_FC_MASK) != 0x01)
1474 if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
1476 scp->services_loc = u.services;
1480 if (optlen != sizeof(unsigned char))
1484 scp->info_loc = u.info;
1491 static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
1493 struct sock *sk = sock->sk;
1497 err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
1503 static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
1505 struct sock *sk = sock->sk;
1506 struct dn_scp *scp = DN_SK(sk);
1507 struct linkinfo_dn link;
1509 void *r_data = NULL;
1512 if(get_user(r_len , optlen))
1517 if (r_len > sizeof(struct optdata_dn))
1518 r_len = sizeof(struct optdata_dn);
1519 r_data = &scp->conndata_in;
1523 if (r_len > sizeof(struct optdata_dn))
1524 r_len = sizeof(struct optdata_dn);
1525 r_data = &scp->discdata_in;
1529 if (r_len > sizeof(struct accessdata_dn))
1530 r_len = sizeof(struct accessdata_dn);
1531 r_data = &scp->accessdata;
1534 case DSO_ACCEPTMODE:
1535 if (r_len > sizeof(unsigned char))
1536 r_len = sizeof(unsigned char);
1537 r_data = &scp->accept_mode;
1541 if (r_len > sizeof(struct linkinfo_dn))
1542 r_len = sizeof(struct linkinfo_dn);
1544 switch(sock->state) {
1546 link.idn_linkstate = LL_CONNECTING;
1548 case SS_DISCONNECTING:
1549 link.idn_linkstate = LL_DISCONNECTING;
1552 link.idn_linkstate = LL_RUNNING;
1555 link.idn_linkstate = LL_INACTIVE;
1558 link.idn_segsize = scp->segsize_rem;
1563 #ifdef CONFIG_NETFILTER
1567 if(get_user(len, optlen))
1570 val = nf_getsockopt(sk, PF_DECnet, optname,
1573 val = put_user(len, optlen);
1581 return -ENOPROTOOPT;
1584 if (r_len > sizeof(unsigned long))
1585 r_len = sizeof(unsigned long);
1586 r_data = &scp->max_window;
1590 if (r_len > sizeof(int))
1591 r_len = sizeof(int);
1592 val = (scp->nonagle == 1);
1597 if (r_len > sizeof(int))
1598 r_len = sizeof(int);
1599 val = (scp->nonagle == 2);
1604 if (r_len > sizeof(unsigned char))
1605 r_len = sizeof(unsigned char);
1606 r_data = &scp->services_rem;
1610 if (r_len > sizeof(unsigned char))
1611 r_len = sizeof(unsigned char);
1612 r_data = &scp->info_rem;
1617 if (copy_to_user(optval, r_data, r_len))
1619 if (put_user(r_len, optlen))
1627 static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
1629 struct sk_buff *skb = q->next;
1632 if (flags & MSG_OOB)
1633 return !skb_queue_empty(q) ? 1 : 0;
1635 while(skb != (struct sk_buff *)q) {
1636 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1639 if (cb->nsp_flags & 0x40) {
1640 /* SOCK_SEQPACKET reads to EOM */
1641 if (sk->sk_type == SOCK_SEQPACKET)
1643 /* so does SOCK_STREAM unless WAITALL is specified */
1644 if (!(flags & MSG_WAITALL))
1648 /* minimum data length for read exceeded */
1659 static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
1660 struct msghdr *msg, size_t size, int flags)
1662 struct sock *sk = sock->sk;
1663 struct dn_scp *scp = DN_SK(sk);
1664 struct sk_buff_head *queue = &sk->sk_receive_queue;
1665 size_t target = size > 1 ? 1 : 0;
1668 struct sk_buff *skb, *nskb;
1669 struct dn_skb_cb *cb = NULL;
1670 unsigned char eor = 0;
1671 long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1675 if (sock_flag(sk, SOCK_ZAPPED)) {
1676 rv = -EADDRNOTAVAIL;
1680 rv = dn_check_state(sk, NULL, 0, &timeo, flags);
1684 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1685 if (!(flags & MSG_NOSIGNAL))
1686 send_sig(SIGPIPE, current, 0);
1691 if (flags & ~(MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
1696 if (flags & MSG_OOB)
1697 queue = &scp->other_receive_queue;
1699 if (flags & MSG_WAITALL)
1704 * See if there is data ready to read, sleep if there isn't
1710 if (!skb_queue_empty(&scp->other_receive_queue)) {
1711 if (!(flags & MSG_OOB)) {
1712 msg->msg_flags |= MSG_OOB;
1713 if (!scp->other_report) {
1714 scp->other_report = 1;
1720 if (scp->state != DN_RUN)
1723 if (signal_pending(current)) {
1724 rv = sock_intr_errno(timeo);
1728 if (dn_data_ready(sk, queue, flags, target))
1731 if (flags & MSG_DONTWAIT) {
1736 set_bit(SOCK_ASYNC_WAITDATA, &sock->flags);
1739 if (!dn_data_ready(sk, queue, flags, target))
1743 clear_bit(SOCK_ASYNC_WAITDATA, &sock->flags);
1746 for(skb = queue->next; skb != (struct sk_buff *)queue; skb = nskb) {
1747 unsigned int chunk = skb->len;
1748 cb = DN_SKB_CB(skb);
1750 if ((chunk + copied) > size)
1751 chunk = size - copied;
1753 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1759 if (!(flags & MSG_PEEK))
1760 skb_pull(skb, chunk);
1762 eor = cb->nsp_flags & 0x40;
1765 if (skb->len == 0) {
1769 * N.B. Don't refer to skb or cb after this point
1772 if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
1773 scp->flowloc_sw = DN_SEND;
1774 dn_nsp_send_link(sk, DN_SEND, 0);
1779 if (sk->sk_type == SOCK_SEQPACKET)
1781 if (!(flags & MSG_WAITALL))
1785 if (flags & MSG_OOB)
1788 if (copied >= target)
1795 if (eor && (sk->sk_type == SOCK_SEQPACKET))
1796 msg->msg_flags |= MSG_EOR;
1800 rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk);
1802 if ((rv >= 0) && msg->msg_name) {
1803 memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
1804 msg->msg_namelen = sizeof(struct sockaddr_dn);
1813 static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags)
1815 unsigned char fctype = scp->services_rem & NSP_FC_MASK;
1816 if (skb_queue_len(queue) >= scp->snd_window)
1818 if (fctype != NSP_FC_NONE) {
1819 if (flags & MSG_OOB) {
1820 if (scp->flowrem_oth == 0)
1823 if (scp->flowrem_dat == 0)
1831 * The DECnet spec requires the the "routing layer" accepts packets which
1832 * are at least 230 bytes in size. This excludes any headers which the NSP
1833 * layer might add, so we always assume that we'll be using the maximal
1834 * length header on data packets. The variation in length is due to the
1835 * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
1836 * make much practical difference.
1838 unsigned dn_mss_from_pmtu(struct net_device *dev, int mtu)
1840 unsigned mss = 230 - DN_MAX_NSP_DATA_HEADER;
1842 struct dn_dev *dn_db = dev->dn_ptr;
1843 mtu -= LL_RESERVED_SPACE(dev);
1844 if (dn_db->use_long)
1848 mtu -= DN_MAX_NSP_DATA_HEADER;
1851 * 21 = long header, 16 = guess at MAC header length
1853 mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16);
1860 static inline unsigned int dn_current_mss(struct sock *sk, int flags)
1862 struct dst_entry *dst = __sk_dst_get(sk);
1863 struct dn_scp *scp = DN_SK(sk);
1864 int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem);
1866 /* Other data messages are limited to 16 bytes per packet */
1867 if (flags & MSG_OOB)
1870 /* This works out the maximum size of segment we can send out */
1872 u32 mtu = dst_mtu(dst);
1873 mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now);
1879 static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
1880 struct msghdr *msg, size_t size)
1882 struct sock *sk = sock->sk;
1883 struct dn_scp *scp = DN_SK(sk);
1885 struct sk_buff_head *queue = &scp->data_xmit_queue;
1886 int flags = msg->msg_flags;
1889 int addr_len = msg->msg_namelen;
1890 struct sockaddr_dn *addr = (struct sockaddr_dn *)msg->msg_name;
1891 struct sk_buff *skb = NULL;
1892 struct dn_skb_cb *cb;
1894 unsigned char fctype;
1895 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1897 if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT))
1900 if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
1904 * The only difference between stream sockets and sequenced packet
1905 * sockets is that the stream sockets always behave as if MSG_EOR
1908 if (sock->type == SOCK_STREAM) {
1909 if (flags & MSG_EOR)
1916 err = dn_check_state(sk, addr, addr_len, &timeo, flags);
1920 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1925 if ((flags & MSG_TRYHARD) && sk->sk_dst_cache)
1926 dst_negative_advice(&sk->sk_dst_cache);
1928 mss = scp->segsize_rem;
1929 fctype = scp->services_rem & NSP_FC_MASK;
1931 mss = dn_current_mss(sk, flags);
1933 if (flags & MSG_OOB) {
1934 queue = &scp->other_xmit_queue;
1941 scp->persist_fxn = dn_nsp_xmit_timeout;
1943 while(sent < size) {
1944 err = sock_error(sk);
1948 if (signal_pending(current)) {
1949 err = sock_intr_errno(timeo);
1954 * Calculate size that we wish to send.
1962 * Wait for queue size to go down below the window
1965 if (dn_queue_too_long(scp, queue, flags)) {
1966 if (flags & MSG_DONTWAIT) {
1973 if (dn_queue_too_long(scp, queue, flags))
1982 * Get a suitably sized skb.
1984 skb = dn_alloc_send_skb(sk, &len, flags & MSG_DONTWAIT, timeo, &err);
1992 cb = DN_SKB_CB(skb);
1994 skb_reserve(skb, DN_MAX_NSP_DATA_HEADER);
1996 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
2001 if (flags & MSG_OOB) {
2002 cb->nsp_flags = 0x30;
2003 if (fctype != NSP_FC_NONE)
2006 cb->nsp_flags = 0x00;
2007 if (scp->seg_total == 0)
2008 cb->nsp_flags |= 0x20;
2010 scp->seg_total += len;
2012 if (((sent + len) == size) && (flags & MSG_EOR)) {
2013 cb->nsp_flags |= 0x40;
2015 if (fctype == NSP_FC_SCMC)
2018 if (fctype == NSP_FC_SRC)
2023 dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB);
2026 scp->persist = dn_nsp_persist(sk);
2036 return sent ? sent : err;
2039 err = sk_stream_error(sk, flags, err);
2044 static int dn_device_event(struct notifier_block *this, unsigned long event,
2047 struct net_device *dev = (struct net_device *)ptr;
2063 static struct notifier_block dn_dev_notifier = {
2064 .notifier_call = dn_device_event,
2067 extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *);
2069 static struct packet_type dn_dix_packet_type = {
2070 .type = __constant_htons(ETH_P_DNA_RT),
2071 .dev = NULL, /* All devices */
2072 .func = dn_route_rcv,
2075 #ifdef CONFIG_PROC_FS
2076 struct dn_iter_state {
2080 static struct sock *dn_socket_get_first(struct seq_file *seq)
2082 struct dn_iter_state *state = seq->private;
2083 struct sock *n = NULL;
2085 for(state->bucket = 0;
2086 state->bucket < DN_SK_HASH_SIZE;
2088 n = sk_head(&dn_sk_hash[state->bucket]);
2096 static struct sock *dn_socket_get_next(struct seq_file *seq,
2099 struct dn_iter_state *state = seq->private;
2105 if (++state->bucket >= DN_SK_HASH_SIZE)
2107 n = sk_head(&dn_sk_hash[state->bucket]);
2113 static struct sock *socket_get_idx(struct seq_file *seq, loff_t *pos)
2115 struct sock *sk = dn_socket_get_first(seq);
2118 while(*pos && (sk = dn_socket_get_next(seq, sk)))
2121 return *pos ? NULL : sk;
2124 static void *dn_socket_get_idx(struct seq_file *seq, loff_t pos)
2127 read_lock_bh(&dn_hash_lock);
2128 rc = socket_get_idx(seq, &pos);
2130 read_unlock_bh(&dn_hash_lock);
2135 static void *dn_socket_seq_start(struct seq_file *seq, loff_t *pos)
2137 return *pos ? dn_socket_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2140 static void *dn_socket_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2144 if (v == SEQ_START_TOKEN) {
2145 rc = dn_socket_get_idx(seq, 0);
2149 rc = dn_socket_get_next(seq, v);
2152 read_unlock_bh(&dn_hash_lock);
2158 static void dn_socket_seq_stop(struct seq_file *seq, void *v)
2160 if (v && v != SEQ_START_TOKEN)
2161 read_unlock_bh(&dn_hash_lock);
2164 #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
2166 static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf)
2170 switch (dn_ntohs(dn->sdn_objnamel)) {
2172 sprintf(buf, "%d", dn->sdn_objnum);
2175 for (i = 0; i < dn_ntohs(dn->sdn_objnamel); i++) {
2176 buf[i] = dn->sdn_objname[i];
2177 if (IS_NOT_PRINTABLE(buf[i]))
2184 static char *dn_state2asc(unsigned char state)
2224 static inline void dn_socket_format_entry(struct seq_file *seq, struct sock *sk)
2226 struct dn_scp *scp = DN_SK(sk);
2227 char buf1[DN_ASCBUF_LEN];
2228 char buf2[DN_ASCBUF_LEN];
2229 char local_object[DN_MAXOBJL+3];
2230 char remote_object[DN_MAXOBJL+3];
2232 dn_printable_object(&scp->addr, local_object);
2233 dn_printable_object(&scp->peer, remote_object);
2236 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
2237 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
2238 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp->addr)), buf1),
2246 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp->peer)), buf2),
2254 dn_state2asc(scp->state),
2255 ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER"));
2258 static int dn_socket_seq_show(struct seq_file *seq, void *v)
2260 if (v == SEQ_START_TOKEN) {
2261 seq_puts(seq, "Local Remote\n");
2263 dn_socket_format_entry(seq, v);
2268 static struct seq_operations dn_socket_seq_ops = {
2269 .start = dn_socket_seq_start,
2270 .next = dn_socket_seq_next,
2271 .stop = dn_socket_seq_stop,
2272 .show = dn_socket_seq_show,
2275 static int dn_socket_seq_open(struct inode *inode, struct file *file)
2277 struct seq_file *seq;
2279 struct dn_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2284 rc = seq_open(file, &dn_socket_seq_ops);
2288 seq = file->private_data;
2290 memset(s, 0, sizeof(*s));
2298 static struct file_operations dn_socket_seq_fops = {
2299 .owner = THIS_MODULE,
2300 .open = dn_socket_seq_open,
2302 .llseek = seq_lseek,
2303 .release = seq_release_private,
2307 static struct net_proto_family dn_family_ops = {
2308 .family = AF_DECnet,
2309 .create = dn_create,
2310 .owner = THIS_MODULE,
2313 static struct proto_ops dn_proto_ops = {
2314 .family = AF_DECnet,
2315 .owner = THIS_MODULE,
2316 .release = dn_release,
2318 .connect = dn_connect,
2319 .socketpair = sock_no_socketpair,
2320 .accept = dn_accept,
2321 .getname = dn_getname,
2324 .listen = dn_listen,
2325 .shutdown = dn_shutdown,
2326 .setsockopt = dn_setsockopt,
2327 .getsockopt = dn_getsockopt,
2328 .sendmsg = dn_sendmsg,
2329 .recvmsg = dn_recvmsg,
2330 .mmap = sock_no_mmap,
2331 .sendpage = sock_no_sendpage,
2334 void dn_register_sysctl(void);
2335 void dn_unregister_sysctl(void);
2337 MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
2338 MODULE_AUTHOR("Linux DECnet Project Team");
2339 MODULE_LICENSE("GPL");
2340 MODULE_ALIAS_NETPROTO(PF_DECnet);
2342 static char banner[] __initdata = KERN_INFO "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
2344 static int __init decnet_init(void)
2350 rc = proto_register(&dn_proto, 1);
2359 sock_register(&dn_family_ops);
2360 dev_add_pack(&dn_dix_packet_type);
2361 register_netdevice_notifier(&dn_dev_notifier);
2363 proc_net_fops_create("decnet", S_IRUGO, &dn_socket_seq_fops);
2364 dn_register_sysctl();
2369 module_init(decnet_init);
2372 * Prevent DECnet module unloading until its fixed properly.
2373 * Requires an audit of the code to check for memory leaks and
2374 * initialisation problems etc.
2377 static void __exit decnet_exit(void)
2379 sock_unregister(AF_DECnet);
2380 dev_remove_pack(&dn_dix_packet_type);
2382 dn_unregister_sysctl();
2384 unregister_netdevice_notifier(&dn_dev_notifier);
2391 proc_net_remove("decnet");
2393 proto_unregister(&dn_proto);
2395 module_exit(decnet_exit);