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/module.h>
103 #include <linux/errno.h>
104 #include <linux/types.h>
105 #include <linux/slab.h>
106 #include <linux/socket.h>
107 #include <linux/in.h>
108 #include <linux/kernel.h>
109 #include <linux/sched.h>
110 #include <linux/timer.h>
111 #include <linux/string.h>
112 #include <linux/sockios.h>
113 #include <linux/net.h>
114 #include <linux/netdevice.h>
115 #include <linux/inet.h>
116 #include <linux/route.h>
117 #include <linux/netfilter.h>
118 #include <linux/seq_file.h>
119 #include <net/sock.h>
120 #include <net/tcp_states.h>
121 #include <net/flow.h>
122 #include <asm/system.h>
123 #include <asm/ioctls.h>
124 #include <linux/capability.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 const 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
;
156 static atomic_t decnet_memory_allocated
;
158 static int __dn_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int optlen
, int flags
);
159 static int __dn_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
, int flags
);
161 static struct hlist_head
*dn_find_list(struct sock
*sk
)
163 struct dn_scp
*scp
= DN_SK(sk
);
165 if (scp
->addr
.sdn_flags
& SDF_WILD
)
166 return hlist_empty(&dn_wild_sk
) ? &dn_wild_sk
: NULL
;
168 return &dn_sk_hash
[scp
->addrloc
& DN_SK_HASH_MASK
];
172 * Valid ports are those greater than zero and not already in use.
174 static int check_port(__le16 port
)
177 struct hlist_node
*node
;
182 sk_for_each(sk
, node
, &dn_sk_hash
[port
& DN_SK_HASH_MASK
]) {
183 struct dn_scp
*scp
= DN_SK(sk
);
184 if (scp
->addrloc
== port
)
190 static unsigned short port_alloc(struct sock
*sk
)
192 struct dn_scp
*scp
= DN_SK(sk
);
193 static unsigned short port
= 0x2000;
194 unsigned short i_port
= port
;
196 while(check_port(++port
) != 0) {
207 * Since this is only ever called from user
208 * level, we don't need a write_lock() version
211 static int dn_hash_sock(struct sock
*sk
)
213 struct dn_scp
*scp
= DN_SK(sk
);
214 struct hlist_head
*list
;
217 BUG_ON(sk_hashed(sk
));
219 write_lock_bh(&dn_hash_lock
);
221 if (!scp
->addrloc
&& !port_alloc(sk
))
225 if ((list
= dn_find_list(sk
)) == NULL
)
228 sk_add_node(sk
, list
);
231 write_unlock_bh(&dn_hash_lock
);
235 static void dn_unhash_sock(struct sock
*sk
)
237 write_lock(&dn_hash_lock
);
238 sk_del_node_init(sk
);
239 write_unlock(&dn_hash_lock
);
242 static void dn_unhash_sock_bh(struct sock
*sk
)
244 write_lock_bh(&dn_hash_lock
);
245 sk_del_node_init(sk
);
246 write_unlock_bh(&dn_hash_lock
);
249 static struct hlist_head
*listen_hash(struct sockaddr_dn
*addr
)
252 unsigned hash
= addr
->sdn_objnum
;
255 hash
= addr
->sdn_objnamel
;
256 for(i
= 0; i
< dn_ntohs(addr
->sdn_objnamel
); i
++) {
257 hash
^= addr
->sdn_objname
[i
];
262 return &dn_sk_hash
[hash
& DN_SK_HASH_MASK
];
266 * Called to transform a socket from bound (i.e. with a local address)
267 * into a listening socket (doesn't need a local port number) and rehashes
268 * based upon the object name/number.
270 static void dn_rehash_sock(struct sock
*sk
)
272 struct hlist_head
*list
;
273 struct dn_scp
*scp
= DN_SK(sk
);
275 if (scp
->addr
.sdn_flags
& SDF_WILD
)
278 write_lock_bh(&dn_hash_lock
);
279 sk_del_node_init(sk
);
280 DN_SK(sk
)->addrloc
= 0;
281 list
= listen_hash(&DN_SK(sk
)->addr
);
282 sk_add_node(sk
, list
);
283 write_unlock_bh(&dn_hash_lock
);
286 int dn_sockaddr2username(struct sockaddr_dn
*sdn
, unsigned char *buf
, unsigned char type
)
294 *buf
++ = sdn
->sdn_objnum
;
298 *buf
++ = dn_ntohs(sdn
->sdn_objnamel
);
299 memcpy(buf
, sdn
->sdn_objname
, dn_ntohs(sdn
->sdn_objnamel
));
300 len
= 3 + dn_ntohs(sdn
->sdn_objnamel
);
305 *buf
++ = dn_ntohs(sdn
->sdn_objnamel
);
306 memcpy(buf
, sdn
->sdn_objname
, dn_ntohs(sdn
->sdn_objnamel
));
307 len
= 7 + dn_ntohs(sdn
->sdn_objnamel
);
315 * On reception of usernames, we handle types 1 and 0 for destination
316 * addresses only. Types 2 and 4 are used for source addresses, but the
317 * UIC, GIC are ignored and they are both treated the same way. Type 3
318 * is never used as I've no idea what its purpose might be or what its
321 int dn_username2sockaddr(unsigned char *data
, int len
, struct sockaddr_dn
*sdn
, unsigned char *fmt
)
328 sdn
->sdn_objnamel
= dn_htons(0);
329 memset(sdn
->sdn_objname
, 0, DN_MAXOBJL
);
340 sdn
->sdn_objnum
= type
;
362 sdn
->sdn_objnamel
= dn_htons(*data
++);
363 len
-= dn_ntohs(sdn
->sdn_objnamel
);
365 if ((len
< 0) || (dn_ntohs(sdn
->sdn_objnamel
) > namel
))
368 memcpy(sdn
->sdn_objname
, data
, dn_ntohs(sdn
->sdn_objnamel
));
373 struct sock
*dn_sklist_find_listener(struct sockaddr_dn
*addr
)
375 struct hlist_head
*list
= listen_hash(addr
);
376 struct hlist_node
*node
;
379 read_lock(&dn_hash_lock
);
380 sk_for_each(sk
, node
, list
) {
381 struct dn_scp
*scp
= DN_SK(sk
);
382 if (sk
->sk_state
!= TCP_LISTEN
)
384 if (scp
->addr
.sdn_objnum
) {
385 if (scp
->addr
.sdn_objnum
!= addr
->sdn_objnum
)
388 if (addr
->sdn_objnum
)
390 if (scp
->addr
.sdn_objnamel
!= addr
->sdn_objnamel
)
392 if (memcmp(scp
->addr
.sdn_objname
, addr
->sdn_objname
, dn_ntohs(addr
->sdn_objnamel
)) != 0)
396 read_unlock(&dn_hash_lock
);
400 sk
= sk_head(&dn_wild_sk
);
402 if (sk
->sk_state
== TCP_LISTEN
)
408 read_unlock(&dn_hash_lock
);
412 struct sock
*dn_find_by_skb(struct sk_buff
*skb
)
414 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
416 struct hlist_node
*node
;
419 read_lock(&dn_hash_lock
);
420 sk_for_each(sk
, node
, &dn_sk_hash
[cb
->dst_port
& DN_SK_HASH_MASK
]) {
422 if (cb
->src
!= dn_saddr2dn(&scp
->peer
))
424 if (cb
->dst_port
!= scp
->addrloc
)
426 if (scp
->addrrem
&& (cb
->src_port
!= scp
->addrrem
))
433 read_unlock(&dn_hash_lock
);
439 static void dn_destruct(struct sock
*sk
)
441 struct dn_scp
*scp
= DN_SK(sk
);
443 skb_queue_purge(&scp
->data_xmit_queue
);
444 skb_queue_purge(&scp
->other_xmit_queue
);
445 skb_queue_purge(&scp
->other_receive_queue
);
447 dst_release(xchg(&sk
->sk_dst_cache
, NULL
));
450 static int dn_memory_pressure
;
452 static void dn_enter_memory_pressure(void)
454 if (!dn_memory_pressure
) {
455 dn_memory_pressure
= 1;
459 static struct proto dn_proto
= {
461 .owner
= THIS_MODULE
,
462 .enter_memory_pressure
= dn_enter_memory_pressure
,
463 .memory_pressure
= &dn_memory_pressure
,
464 .memory_allocated
= &decnet_memory_allocated
,
465 .sysctl_mem
= sysctl_decnet_mem
,
466 .sysctl_wmem
= sysctl_decnet_wmem
,
467 .sysctl_rmem
= sysctl_decnet_rmem
,
468 .max_header
= DN_MAX_NSP_DATA_HEADER
+ 64,
469 .obj_size
= sizeof(struct dn_sock
),
472 static struct sock
*dn_alloc_sock(struct socket
*sock
, gfp_t gfp
)
475 struct sock
*sk
= sk_alloc(PF_DECnet
, gfp
, &dn_proto
, 1);
481 sock
->ops
= &dn_proto_ops
;
482 sock_init_data(sock
, sk
);
484 sk
->sk_backlog_rcv
= dn_nsp_backlog_rcv
;
485 sk
->sk_destruct
= dn_destruct
;
487 sk
->sk_family
= PF_DECnet
;
489 sk
->sk_allocation
= gfp
;
490 sk
->sk_sndbuf
= sysctl_decnet_wmem
[1];
491 sk
->sk_rcvbuf
= sysctl_decnet_rmem
[1];
493 /* Initialization of DECnet Session Control Port */
495 scp
->state
= DN_O
; /* Open */
496 scp
->numdat
= 1; /* Next data seg to tx */
497 scp
->numoth
= 1; /* Next oth data to tx */
498 scp
->ackxmt_dat
= 0; /* Last data seg ack'ed */
499 scp
->ackxmt_oth
= 0; /* Last oth data ack'ed */
500 scp
->ackrcv_dat
= 0; /* Highest data ack recv*/
501 scp
->ackrcv_oth
= 0; /* Last oth data ack rec*/
502 scp
->flowrem_sw
= DN_SEND
;
503 scp
->flowloc_sw
= DN_SEND
;
504 scp
->flowrem_dat
= 0;
505 scp
->flowrem_oth
= 1;
506 scp
->flowloc_dat
= 0;
507 scp
->flowloc_oth
= 1;
508 scp
->services_rem
= 0;
509 scp
->services_loc
= 1 | NSP_FC_NONE
;
511 scp
->info_loc
= 0x03; /* NSP version 4.1 */
512 scp
->segsize_rem
= 230 - DN_MAX_NSP_DATA_HEADER
; /* Default: Updated by remote segsize */
515 scp
->accept_mode
= ACC_IMMED
;
516 scp
->addr
.sdn_family
= AF_DECnet
;
517 scp
->peer
.sdn_family
= AF_DECnet
;
518 scp
->accessdata
.acc_accl
= 5;
519 memcpy(scp
->accessdata
.acc_acc
, "LINUX", 5);
521 scp
->max_window
= NSP_MAX_WINDOW
;
522 scp
->snd_window
= NSP_MIN_WINDOW
;
523 scp
->nsp_srtt
= NSP_INITIAL_SRTT
;
524 scp
->nsp_rttvar
= NSP_INITIAL_RTTVAR
;
525 scp
->nsp_rxtshift
= 0;
527 skb_queue_head_init(&scp
->data_xmit_queue
);
528 skb_queue_head_init(&scp
->other_xmit_queue
);
529 skb_queue_head_init(&scp
->other_receive_queue
);
532 scp
->persist_fxn
= NULL
;
533 scp
->keepalive
= 10 * HZ
;
534 scp
->keepalive_fxn
= dn_keepalive
;
536 init_timer(&scp
->delack_timer
);
537 scp
->delack_pending
= 0;
538 scp
->delack_fxn
= dn_nsp_delayed_ack
;
540 dn_start_slow_timer(sk
);
547 * FIXME: Should respond to SO_KEEPALIVE etc.
549 static void dn_keepalive(struct sock
*sk
)
551 struct dn_scp
*scp
= DN_SK(sk
);
554 * By checking the other_data transmit queue is empty
555 * we are double checking that we are not sending too
556 * many of these keepalive frames.
558 if (skb_queue_empty(&scp
->other_xmit_queue
))
559 dn_nsp_send_link(sk
, DN_NOCHANGE
, 0);
564 * Timer for shutdown/destroyed sockets.
565 * When socket is dead & no packets have been sent for a
566 * certain amount of time, they are removed by this
567 * routine. Also takes care of sending out DI & DC
568 * frames at correct times.
570 int dn_destroy_timer(struct sock
*sk
)
572 struct dn_scp
*scp
= DN_SK(sk
);
574 scp
->persist
= dn_nsp_persist(sk
);
578 dn_nsp_send_disc(sk
, NSP_DISCINIT
, 0, GFP_ATOMIC
);
579 if (scp
->nsp_rxtshift
>= decnet_di_count
)
584 dn_nsp_send_disc(sk
, NSP_DISCINIT
, 0, GFP_ATOMIC
);
585 if (scp
->nsp_rxtshift
>= decnet_dr_count
)
590 if (scp
->nsp_rxtshift
< decnet_dn_count
) {
591 /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
592 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
, GFP_ATOMIC
);
597 scp
->persist
= (HZ
* decnet_time_wait
);
602 if ((jiffies
- scp
->stamp
) >= (HZ
* decnet_time_wait
)) {
611 static void dn_destroy_sock(struct sock
*sk
)
613 struct dn_scp
*scp
= DN_SK(sk
);
615 scp
->nsp_rxtshift
= 0; /* reset back off */
618 if (sk
->sk_socket
->state
!= SS_UNCONNECTED
)
619 sk
->sk_socket
->state
= SS_DISCONNECTING
;
622 sk
->sk_state
= TCP_CLOSE
;
626 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
,
628 scp
->persist_fxn
= dn_destroy_timer
;
629 scp
->persist
= dn_nsp_persist(sk
);
639 dn_nsp_send_disc(sk
, NSP_DISCINIT
, 0, sk
->sk_allocation
);
648 scp
->persist_fxn
= dn_destroy_timer
;
649 scp
->persist
= dn_nsp_persist(sk
);
652 printk(KERN_DEBUG
"DECnet: dn_destroy_sock passed socket in invalid state\n");
654 dn_stop_slow_timer(sk
);
656 dn_unhash_sock_bh(sk
);
663 char *dn_addr2asc(__u16 addr
, char *buf
)
665 unsigned short node
, area
;
667 node
= addr
& 0x03ff;
669 sprintf(buf
, "%hd.%hd", area
, node
);
676 static int dn_create(struct socket
*sock
, int protocol
)
682 if (protocol
!= DNPROTO_NSP
)
683 return -EPROTONOSUPPORT
;
688 return -ESOCKTNOSUPPORT
;
692 if ((sk
= dn_alloc_sock(sock
, GFP_KERNEL
)) == NULL
)
695 sk
->sk_protocol
= protocol
;
702 dn_release(struct socket
*sock
)
704 struct sock
*sk
= sock
->sk
;
718 static int dn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
720 struct sock
*sk
= sock
->sk
;
721 struct dn_scp
*scp
= DN_SK(sk
);
722 struct sockaddr_dn
*saddr
= (struct sockaddr_dn
*)uaddr
;
723 struct net_device
*dev
;
726 if (addr_len
!= sizeof(struct sockaddr_dn
))
729 if (saddr
->sdn_family
!= AF_DECnet
)
732 if (dn_ntohs(saddr
->sdn_nodeaddrl
) && (dn_ntohs(saddr
->sdn_nodeaddrl
) != 2))
735 if (dn_ntohs(saddr
->sdn_objnamel
) > DN_MAXOBJL
)
738 if (saddr
->sdn_flags
& ~SDF_WILD
)
741 if (!capable(CAP_NET_BIND_SERVICE
) && (saddr
->sdn_objnum
||
742 (saddr
->sdn_flags
& SDF_WILD
)))
745 if (!(saddr
->sdn_flags
& SDF_WILD
)) {
746 if (dn_ntohs(saddr
->sdn_nodeaddrl
)) {
747 read_lock(&dev_base_lock
);
748 for(dev
= dev_base
; dev
; dev
= dev
->next
) {
751 if (dn_dev_islocal(dev
, dn_saddr2dn(saddr
)))
754 read_unlock(&dev_base_lock
);
756 return -EADDRNOTAVAIL
;
762 if (sock_flag(sk
, SOCK_ZAPPED
)) {
763 memcpy(&scp
->addr
, saddr
, addr_len
);
764 sock_reset_flag(sk
, SOCK_ZAPPED
);
766 rv
= dn_hash_sock(sk
);
768 sock_set_flag(sk
, SOCK_ZAPPED
);
776 static int dn_auto_bind(struct socket
*sock
)
778 struct sock
*sk
= sock
->sk
;
779 struct dn_scp
*scp
= DN_SK(sk
);
782 sock_reset_flag(sk
, SOCK_ZAPPED
);
784 scp
->addr
.sdn_flags
= 0;
785 scp
->addr
.sdn_objnum
= 0;
788 * This stuff is to keep compatibility with Eduardo's
789 * patch. I hope I can dispense with it shortly...
791 if ((scp
->accessdata
.acc_accl
!= 0) &&
792 (scp
->accessdata
.acc_accl
<= 12)) {
794 scp
->addr
.sdn_objnamel
= dn_htons(scp
->accessdata
.acc_accl
);
795 memcpy(scp
->addr
.sdn_objname
, scp
->accessdata
.acc_acc
, dn_ntohs(scp
->addr
.sdn_objnamel
));
797 scp
->accessdata
.acc_accl
= 0;
798 memset(scp
->accessdata
.acc_acc
, 0, 40);
800 /* End of compatibility stuff */
802 scp
->addr
.sdn_add
.a_len
= dn_htons(2);
803 rv
= dn_dev_bind_default((__le16
*)scp
->addr
.sdn_add
.a_addr
);
805 rv
= dn_hash_sock(sk
);
807 sock_set_flag(sk
, SOCK_ZAPPED
);
813 static int dn_confirm_accept(struct sock
*sk
, long *timeo
, gfp_t allocation
)
815 struct dn_scp
*scp
= DN_SK(sk
);
819 if (scp
->state
!= DN_CR
)
823 scp
->segsize_loc
= dst_metric(__sk_dst_get(sk
), RTAX_ADVMSS
);
824 dn_send_conn_conf(sk
, allocation
);
826 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
829 if (scp
->state
== DN_CC
)
830 *timeo
= schedule_timeout(*timeo
);
833 if (scp
->state
== DN_RUN
)
835 err
= sock_error(sk
);
838 err
= sock_intr_errno(*timeo
);
839 if (signal_pending(current
))
844 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
846 finish_wait(sk
->sk_sleep
, &wait
);
848 sk
->sk_socket
->state
= SS_CONNECTED
;
849 } else if (scp
->state
!= DN_CC
) {
850 sk
->sk_socket
->state
= SS_UNCONNECTED
;
855 static int dn_wait_run(struct sock
*sk
, long *timeo
)
857 struct dn_scp
*scp
= DN_SK(sk
);
861 if (scp
->state
== DN_RUN
)
867 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
870 if (scp
->state
== DN_CI
|| scp
->state
== DN_CC
)
871 *timeo
= schedule_timeout(*timeo
);
874 if (scp
->state
== DN_RUN
)
876 err
= sock_error(sk
);
879 err
= sock_intr_errno(*timeo
);
880 if (signal_pending(current
))
885 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
887 finish_wait(sk
->sk_sleep
, &wait
);
890 sk
->sk_socket
->state
= SS_CONNECTED
;
891 } else if (scp
->state
!= DN_CI
&& scp
->state
!= DN_CC
) {
892 sk
->sk_socket
->state
= SS_UNCONNECTED
;
897 static int __dn_connect(struct sock
*sk
, struct sockaddr_dn
*addr
, int addrlen
, long *timeo
, int flags
)
899 struct socket
*sock
= sk
->sk_socket
;
900 struct dn_scp
*scp
= DN_SK(sk
);
904 if (sock
->state
== SS_CONNECTED
)
907 if (sock
->state
== SS_CONNECTING
) {
909 if (scp
->state
== DN_RUN
) {
910 sock
->state
= SS_CONNECTED
;
914 if (scp
->state
!= DN_CI
&& scp
->state
!= DN_CC
) {
915 sock
->state
= SS_UNCONNECTED
;
918 return dn_wait_run(sk
, timeo
);
922 if (scp
->state
!= DN_O
)
925 if (addr
== NULL
|| addrlen
!= sizeof(struct sockaddr_dn
))
927 if (addr
->sdn_family
!= AF_DECnet
)
929 if (addr
->sdn_flags
& SDF_WILD
)
932 if (sock_flag(sk
, SOCK_ZAPPED
)) {
933 err
= dn_auto_bind(sk
->sk_socket
);
938 memcpy(&scp
->peer
, addr
, sizeof(struct sockaddr_dn
));
941 memset(&fl
, 0, sizeof(fl
));
942 fl
.oif
= sk
->sk_bound_dev_if
;
943 fl
.fld_dst
= dn_saddr2dn(&scp
->peer
);
944 fl
.fld_src
= dn_saddr2dn(&scp
->addr
);
945 dn_sk_ports_copy(&fl
, scp
);
946 fl
.proto
= DNPROTO_NSP
;
947 if (dn_route_output_sock(&sk
->sk_dst_cache
, &fl
, sk
, flags
) < 0)
949 sk
->sk_route_caps
= sk
->sk_dst_cache
->dev
->features
;
950 sock
->state
= SS_CONNECTING
;
952 scp
->segsize_loc
= dst_metric(sk
->sk_dst_cache
, RTAX_ADVMSS
);
954 dn_nsp_send_conninit(sk
, NSP_CI
);
957 err
= dn_wait_run(sk
, timeo
);
963 static int dn_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addrlen
, int flags
)
965 struct sockaddr_dn
*addr
= (struct sockaddr_dn
*)uaddr
;
966 struct sock
*sk
= sock
->sk
;
968 long timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
971 err
= __dn_connect(sk
, addr
, addrlen
, &timeo
, 0);
977 static inline int dn_check_state(struct sock
*sk
, struct sockaddr_dn
*addr
, int addrlen
, long *timeo
, int flags
)
979 struct dn_scp
*scp
= DN_SK(sk
);
985 return dn_confirm_accept(sk
, timeo
, sk
->sk_allocation
);
988 return dn_wait_run(sk
, timeo
);
990 return __dn_connect(sk
, addr
, addrlen
, timeo
, flags
);
997 static void dn_access_copy(struct sk_buff
*skb
, struct accessdata_dn
*acc
)
999 unsigned char *ptr
= skb
->data
;
1001 acc
->acc_userl
= *ptr
++;
1002 memcpy(&acc
->acc_user
, ptr
, acc
->acc_userl
);
1003 ptr
+= acc
->acc_userl
;
1005 acc
->acc_passl
= *ptr
++;
1006 memcpy(&acc
->acc_pass
, ptr
, acc
->acc_passl
);
1007 ptr
+= acc
->acc_passl
;
1009 acc
->acc_accl
= *ptr
++;
1010 memcpy(&acc
->acc_acc
, ptr
, acc
->acc_accl
);
1012 skb_pull(skb
, acc
->acc_accl
+ acc
->acc_passl
+ acc
->acc_userl
+ 3);
1016 static void dn_user_copy(struct sk_buff
*skb
, struct optdata_dn
*opt
)
1018 unsigned char *ptr
= skb
->data
;
1020 opt
->opt_optl
= *ptr
++;
1021 opt
->opt_status
= 0;
1022 memcpy(opt
->opt_data
, ptr
, opt
->opt_optl
);
1023 skb_pull(skb
, dn_ntohs(opt
->opt_optl
) + 1);
1027 static struct sk_buff
*dn_wait_for_connect(struct sock
*sk
, long *timeo
)
1030 struct sk_buff
*skb
= NULL
;
1033 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
1036 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1038 *timeo
= schedule_timeout(*timeo
);
1039 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1045 if (sk
->sk_state
!= TCP_LISTEN
)
1047 err
= sock_intr_errno(*timeo
);
1048 if (signal_pending(current
))
1053 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
1055 finish_wait(sk
->sk_sleep
, &wait
);
1057 return skb
== NULL
? ERR_PTR(err
) : skb
;
1060 static int dn_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1062 struct sock
*sk
= sock
->sk
, *newsk
;
1063 struct sk_buff
*skb
= NULL
;
1064 struct dn_skb_cb
*cb
;
1065 unsigned char menuver
;
1068 long timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1072 if (sk
->sk_state
!= TCP_LISTEN
|| DN_SK(sk
)->state
!= DN_O
) {
1077 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1079 skb
= dn_wait_for_connect(sk
, &timeo
);
1082 return PTR_ERR(skb
);
1086 cb
= DN_SKB_CB(skb
);
1087 sk
->sk_ack_backlog
--;
1088 newsk
= dn_alloc_sock(newsock
, sk
->sk_allocation
);
1089 if (newsk
== NULL
) {
1096 dst_release(xchg(&newsk
->sk_dst_cache
, skb
->dst
));
1099 DN_SK(newsk
)->state
= DN_CR
;
1100 DN_SK(newsk
)->addrrem
= cb
->src_port
;
1101 DN_SK(newsk
)->services_rem
= cb
->services
;
1102 DN_SK(newsk
)->info_rem
= cb
->info
;
1103 DN_SK(newsk
)->segsize_rem
= cb
->segsize
;
1104 DN_SK(newsk
)->accept_mode
= DN_SK(sk
)->accept_mode
;
1106 if (DN_SK(newsk
)->segsize_rem
< 230)
1107 DN_SK(newsk
)->segsize_rem
= 230;
1109 if ((DN_SK(newsk
)->services_rem
& NSP_FC_MASK
) == NSP_FC_NONE
)
1110 DN_SK(newsk
)->max_window
= decnet_no_fc_max_cwnd
;
1112 newsk
->sk_state
= TCP_LISTEN
;
1113 memcpy(&(DN_SK(newsk
)->addr
), &(DN_SK(sk
)->addr
), sizeof(struct sockaddr_dn
));
1116 * If we are listening on a wild socket, we don't want
1117 * the newly created socket on the wrong hash queue.
1119 DN_SK(newsk
)->addr
.sdn_flags
&= ~SDF_WILD
;
1121 skb_pull(skb
, dn_username2sockaddr(skb
->data
, skb
->len
, &(DN_SK(newsk
)->addr
), &type
));
1122 skb_pull(skb
, dn_username2sockaddr(skb
->data
, skb
->len
, &(DN_SK(newsk
)->peer
), &type
));
1123 *(__le16
*)(DN_SK(newsk
)->peer
.sdn_add
.a_addr
) = cb
->src
;
1124 *(__le16
*)(DN_SK(newsk
)->addr
.sdn_add
.a_addr
) = cb
->dst
;
1126 menuver
= *skb
->data
;
1129 if (menuver
& DN_MENUVER_ACC
)
1130 dn_access_copy(skb
, &(DN_SK(newsk
)->accessdata
));
1132 if (menuver
& DN_MENUVER_USR
)
1133 dn_user_copy(skb
, &(DN_SK(newsk
)->conndata_in
));
1135 if (menuver
& DN_MENUVER_PRX
)
1136 DN_SK(newsk
)->peer
.sdn_flags
|= SDF_PROXY
;
1138 if (menuver
& DN_MENUVER_UIC
)
1139 DN_SK(newsk
)->peer
.sdn_flags
|= SDF_UICPROXY
;
1143 memcpy(&(DN_SK(newsk
)->conndata_out
), &(DN_SK(sk
)->conndata_out
),
1144 sizeof(struct optdata_dn
));
1145 memcpy(&(DN_SK(newsk
)->discdata_out
), &(DN_SK(sk
)->discdata_out
),
1146 sizeof(struct optdata_dn
));
1149 err
= dn_hash_sock(newsk
);
1151 sock_reset_flag(newsk
, SOCK_ZAPPED
);
1152 dn_send_conn_ack(newsk
);
1155 * Here we use sk->sk_allocation since although the conn conf is
1156 * for the newsk, the context is the old socket.
1158 if (DN_SK(newsk
)->accept_mode
== ACC_IMMED
)
1159 err
= dn_confirm_accept(newsk
, &timeo
,
1162 release_sock(newsk
);
1167 static int dn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,int *uaddr_len
,int peer
)
1169 struct sockaddr_dn
*sa
= (struct sockaddr_dn
*)uaddr
;
1170 struct sock
*sk
= sock
->sk
;
1171 struct dn_scp
*scp
= DN_SK(sk
);
1173 *uaddr_len
= sizeof(struct sockaddr_dn
);
1178 if ((sock
->state
!= SS_CONNECTED
&&
1179 sock
->state
!= SS_CONNECTING
) &&
1180 scp
->accept_mode
== ACC_IMMED
)
1183 memcpy(sa
, &scp
->peer
, sizeof(struct sockaddr_dn
));
1185 memcpy(sa
, &scp
->addr
, sizeof(struct sockaddr_dn
));
1194 static unsigned int dn_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
1196 struct sock
*sk
= sock
->sk
;
1197 struct dn_scp
*scp
= DN_SK(sk
);
1198 int mask
= datagram_poll(file
, sock
, wait
);
1200 if (!skb_queue_empty(&scp
->other_receive_queue
))
1206 static int dn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1208 struct sock
*sk
= sock
->sk
;
1209 struct dn_scp
*scp
= DN_SK(sk
);
1210 int err
= -EOPNOTSUPP
;
1212 struct sk_buff
*skb
;
1219 return dn_dev_ioctl(cmd
, (void __user
*)arg
);
1223 val
= !skb_queue_empty(&scp
->other_receive_queue
);
1224 if (scp
->state
!= DN_RUN
)
1230 amount
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
1233 err
= put_user(amount
, (int __user
*)arg
);
1238 if ((skb
= skb_peek(&scp
->other_receive_queue
)) != NULL
) {
1241 struct sk_buff
*skb
= sk
->sk_receive_queue
.next
;
1244 (struct sk_buff
*)&sk
->sk_receive_queue
)
1251 err
= put_user(amount
, (int __user
*)arg
);
1262 static int dn_listen(struct socket
*sock
, int backlog
)
1264 struct sock
*sk
= sock
->sk
;
1269 if (sock_flag(sk
, SOCK_ZAPPED
))
1272 if ((DN_SK(sk
)->state
!= DN_O
) || (sk
->sk_state
== TCP_LISTEN
))
1275 sk
->sk_max_ack_backlog
= backlog
;
1276 sk
->sk_ack_backlog
= 0;
1277 sk
->sk_state
= TCP_LISTEN
;
1288 static int dn_shutdown(struct socket
*sock
, int how
)
1290 struct sock
*sk
= sock
->sk
;
1291 struct dn_scp
*scp
= DN_SK(sk
);
1292 int err
= -ENOTCONN
;
1296 if (sock
->state
== SS_UNCONNECTED
)
1300 if (sock
->state
== SS_DISCONNECTING
)
1304 if (scp
->state
== DN_O
)
1307 if (how
!= SHUTDOWN_MASK
)
1310 sk
->sk_shutdown
= how
;
1311 dn_destroy_sock(sk
);
1320 static int dn_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int optlen
)
1322 struct sock
*sk
= sock
->sk
;
1326 err
= __dn_setsockopt(sock
, level
, optname
, optval
, optlen
, 0);
1332 static int __dn_setsockopt(struct socket
*sock
, int level
,int optname
, char __user
*optval
, int optlen
, int flags
)
1334 struct sock
*sk
= sock
->sk
;
1335 struct dn_scp
*scp
= DN_SK(sk
);
1338 struct optdata_dn opt
;
1339 struct accessdata_dn acc
;
1343 unsigned char services
;
1348 if (optlen
&& !optval
)
1351 if (optlen
> sizeof(u
))
1354 if (copy_from_user(&u
, optval
, optlen
))
1359 if (sock
->state
== SS_CONNECTED
)
1361 if ((scp
->state
!= DN_O
) && (scp
->state
!= DN_CR
))
1364 if (optlen
!= sizeof(struct optdata_dn
))
1367 if (dn_ntohs(u
.opt
.opt_optl
) > 16)
1370 memcpy(&scp
->conndata_out
, &u
.opt
, optlen
);
1374 if (sock
->state
!= SS_CONNECTED
&& scp
->accept_mode
== ACC_IMMED
)
1377 if (optlen
!= sizeof(struct optdata_dn
))
1380 if (dn_ntohs(u
.opt
.opt_optl
) > 16)
1383 memcpy(&scp
->discdata_out
, &u
.opt
, optlen
);
1387 if (sock
->state
== SS_CONNECTED
)
1389 if (scp
->state
!= DN_O
)
1392 if (optlen
!= sizeof(struct accessdata_dn
))
1395 if ((u
.acc
.acc_accl
> DN_MAXACCL
) ||
1396 (u
.acc
.acc_passl
> DN_MAXACCL
) ||
1397 (u
.acc
.acc_userl
> DN_MAXACCL
))
1400 memcpy(&scp
->accessdata
, &u
.acc
, optlen
);
1403 case DSO_ACCEPTMODE
:
1404 if (sock
->state
== SS_CONNECTED
)
1406 if (scp
->state
!= DN_O
)
1409 if (optlen
!= sizeof(int))
1412 if ((u
.mode
!= ACC_IMMED
) && (u
.mode
!= ACC_DEFER
))
1415 scp
->accept_mode
= (unsigned char)u
.mode
;
1420 if (scp
->state
!= DN_CR
)
1422 timeo
= sock_rcvtimeo(sk
, 0);
1423 err
= dn_confirm_accept(sk
, &timeo
, sk
->sk_allocation
);
1428 if (scp
->state
!= DN_CR
)
1432 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1433 dn_nsp_send_disc(sk
, 0x38, 0, sk
->sk_allocation
);
1437 #ifdef CONFIG_NETFILTER
1438 return nf_setsockopt(sk
, PF_DECnet
, optname
, optval
, optlen
);
1443 return -ENOPROTOOPT
;
1446 if (optlen
!= sizeof(unsigned long))
1448 if (u
.win
> NSP_MAX_WINDOW
)
1449 u
.win
= NSP_MAX_WINDOW
;
1452 scp
->max_window
= u
.win
;
1453 if (scp
->snd_window
> u
.win
)
1454 scp
->snd_window
= u
.win
;
1458 if (optlen
!= sizeof(int))
1460 if (scp
->nonagle
== 2)
1462 scp
->nonagle
= (u
.val
== 0) ? 0 : 1;
1463 /* if (scp->nonagle == 1) { Push pending frames } */
1467 if (optlen
!= sizeof(int))
1469 if (scp
->nonagle
== 1)
1471 scp
->nonagle
= (u
.val
== 0) ? 0 : 2;
1472 /* if (scp->nonagle == 0) { Push pending frames } */
1476 if (optlen
!= sizeof(unsigned char))
1478 if ((u
.services
& ~NSP_FC_MASK
) != 0x01)
1480 if ((u
.services
& NSP_FC_MASK
) == NSP_FC_MASK
)
1482 scp
->services_loc
= u
.services
;
1486 if (optlen
!= sizeof(unsigned char))
1490 scp
->info_loc
= u
.info
;
1497 static int dn_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
1499 struct sock
*sk
= sock
->sk
;
1503 err
= __dn_getsockopt(sock
, level
, optname
, optval
, optlen
, 0);
1509 static int __dn_getsockopt(struct socket
*sock
, int level
,int optname
, char __user
*optval
,int __user
*optlen
, int flags
)
1511 struct sock
*sk
= sock
->sk
;
1512 struct dn_scp
*scp
= DN_SK(sk
);
1513 struct linkinfo_dn link
;
1515 void *r_data
= NULL
;
1518 if(get_user(r_len
, optlen
))
1523 if (r_len
> sizeof(struct optdata_dn
))
1524 r_len
= sizeof(struct optdata_dn
);
1525 r_data
= &scp
->conndata_in
;
1529 if (r_len
> sizeof(struct optdata_dn
))
1530 r_len
= sizeof(struct optdata_dn
);
1531 r_data
= &scp
->discdata_in
;
1535 if (r_len
> sizeof(struct accessdata_dn
))
1536 r_len
= sizeof(struct accessdata_dn
);
1537 r_data
= &scp
->accessdata
;
1540 case DSO_ACCEPTMODE
:
1541 if (r_len
> sizeof(unsigned char))
1542 r_len
= sizeof(unsigned char);
1543 r_data
= &scp
->accept_mode
;
1547 if (r_len
> sizeof(struct linkinfo_dn
))
1548 r_len
= sizeof(struct linkinfo_dn
);
1550 switch(sock
->state
) {
1552 link
.idn_linkstate
= LL_CONNECTING
;
1554 case SS_DISCONNECTING
:
1555 link
.idn_linkstate
= LL_DISCONNECTING
;
1558 link
.idn_linkstate
= LL_RUNNING
;
1561 link
.idn_linkstate
= LL_INACTIVE
;
1564 link
.idn_segsize
= scp
->segsize_rem
;
1569 #ifdef CONFIG_NETFILTER
1573 if(get_user(len
, optlen
))
1576 val
= nf_getsockopt(sk
, PF_DECnet
, optname
,
1579 val
= put_user(len
, optlen
);
1587 return -ENOPROTOOPT
;
1590 if (r_len
> sizeof(unsigned long))
1591 r_len
= sizeof(unsigned long);
1592 r_data
= &scp
->max_window
;
1596 if (r_len
> sizeof(int))
1597 r_len
= sizeof(int);
1598 val
= (scp
->nonagle
== 1);
1603 if (r_len
> sizeof(int))
1604 r_len
= sizeof(int);
1605 val
= (scp
->nonagle
== 2);
1610 if (r_len
> sizeof(unsigned char))
1611 r_len
= sizeof(unsigned char);
1612 r_data
= &scp
->services_rem
;
1616 if (r_len
> sizeof(unsigned char))
1617 r_len
= sizeof(unsigned char);
1618 r_data
= &scp
->info_rem
;
1623 if (copy_to_user(optval
, r_data
, r_len
))
1625 if (put_user(r_len
, optlen
))
1633 static int dn_data_ready(struct sock
*sk
, struct sk_buff_head
*q
, int flags
, int target
)
1635 struct sk_buff
*skb
= q
->next
;
1638 if (flags
& MSG_OOB
)
1639 return !skb_queue_empty(q
) ? 1 : 0;
1641 while(skb
!= (struct sk_buff
*)q
) {
1642 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
1645 if (cb
->nsp_flags
& 0x40) {
1646 /* SOCK_SEQPACKET reads to EOM */
1647 if (sk
->sk_type
== SOCK_SEQPACKET
)
1649 /* so does SOCK_STREAM unless WAITALL is specified */
1650 if (!(flags
& MSG_WAITALL
))
1654 /* minimum data length for read exceeded */
1665 static int dn_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1666 struct msghdr
*msg
, size_t size
, int flags
)
1668 struct sock
*sk
= sock
->sk
;
1669 struct dn_scp
*scp
= DN_SK(sk
);
1670 struct sk_buff_head
*queue
= &sk
->sk_receive_queue
;
1671 size_t target
= size
> 1 ? 1 : 0;
1674 struct sk_buff
*skb
, *nskb
;
1675 struct dn_skb_cb
*cb
= NULL
;
1676 unsigned char eor
= 0;
1677 long timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1681 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1682 rv
= -EADDRNOTAVAIL
;
1686 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
1691 rv
= dn_check_state(sk
, NULL
, 0, &timeo
, flags
);
1695 if (flags
& ~(MSG_CMSG_COMPAT
|MSG_PEEK
|MSG_OOB
|MSG_WAITALL
|MSG_DONTWAIT
|MSG_NOSIGNAL
)) {
1700 if (flags
& MSG_OOB
)
1701 queue
= &scp
->other_receive_queue
;
1703 if (flags
& MSG_WAITALL
)
1708 * See if there is data ready to read, sleep if there isn't
1714 if (!skb_queue_empty(&scp
->other_receive_queue
)) {
1715 if (!(flags
& MSG_OOB
)) {
1716 msg
->msg_flags
|= MSG_OOB
;
1717 if (!scp
->other_report
) {
1718 scp
->other_report
= 1;
1724 if (scp
->state
!= DN_RUN
)
1727 if (signal_pending(current
)) {
1728 rv
= sock_intr_errno(timeo
);
1732 if (dn_data_ready(sk
, queue
, flags
, target
))
1735 if (flags
& MSG_DONTWAIT
) {
1740 set_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
);
1743 if (!dn_data_ready(sk
, queue
, flags
, target
))
1747 clear_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
);
1750 for(skb
= queue
->next
; skb
!= (struct sk_buff
*)queue
; skb
= nskb
) {
1751 unsigned int chunk
= skb
->len
;
1752 cb
= DN_SKB_CB(skb
);
1754 if ((chunk
+ copied
) > size
)
1755 chunk
= size
- copied
;
1757 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
1763 if (!(flags
& MSG_PEEK
))
1764 skb_pull(skb
, chunk
);
1766 eor
= cb
->nsp_flags
& 0x40;
1769 if (skb
->len
== 0) {
1770 skb_unlink(skb
, queue
);
1773 * N.B. Don't refer to skb or cb after this point
1776 if ((scp
->flowloc_sw
== DN_DONTSEND
) && !dn_congested(sk
)) {
1777 scp
->flowloc_sw
= DN_SEND
;
1778 dn_nsp_send_link(sk
, DN_SEND
, 0);
1783 if (sk
->sk_type
== SOCK_SEQPACKET
)
1785 if (!(flags
& MSG_WAITALL
))
1789 if (flags
& MSG_OOB
)
1792 if (copied
>= target
)
1799 if (eor
&& (sk
->sk_type
== SOCK_SEQPACKET
))
1800 msg
->msg_flags
|= MSG_EOR
;
1804 rv
= (flags
& MSG_PEEK
) ? -sk
->sk_err
: sock_error(sk
);
1806 if ((rv
>= 0) && msg
->msg_name
) {
1807 memcpy(msg
->msg_name
, &scp
->peer
, sizeof(struct sockaddr_dn
));
1808 msg
->msg_namelen
= sizeof(struct sockaddr_dn
);
1817 static inline int dn_queue_too_long(struct dn_scp
*scp
, struct sk_buff_head
*queue
, int flags
)
1819 unsigned char fctype
= scp
->services_rem
& NSP_FC_MASK
;
1820 if (skb_queue_len(queue
) >= scp
->snd_window
)
1822 if (fctype
!= NSP_FC_NONE
) {
1823 if (flags
& MSG_OOB
) {
1824 if (scp
->flowrem_oth
== 0)
1827 if (scp
->flowrem_dat
== 0)
1835 * The DECnet spec requires the the "routing layer" accepts packets which
1836 * are at least 230 bytes in size. This excludes any headers which the NSP
1837 * layer might add, so we always assume that we'll be using the maximal
1838 * length header on data packets. The variation in length is due to the
1839 * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
1840 * make much practical difference.
1842 unsigned dn_mss_from_pmtu(struct net_device
*dev
, int mtu
)
1844 unsigned mss
= 230 - DN_MAX_NSP_DATA_HEADER
;
1846 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1847 mtu
-= LL_RESERVED_SPACE(dev
);
1848 if (dn_db
->use_long
)
1852 mtu
-= DN_MAX_NSP_DATA_HEADER
;
1855 * 21 = long header, 16 = guess at MAC header length
1857 mtu
-= (21 + DN_MAX_NSP_DATA_HEADER
+ 16);
1864 static inline unsigned int dn_current_mss(struct sock
*sk
, int flags
)
1866 struct dst_entry
*dst
= __sk_dst_get(sk
);
1867 struct dn_scp
*scp
= DN_SK(sk
);
1868 int mss_now
= min_t(int, scp
->segsize_loc
, scp
->segsize_rem
);
1870 /* Other data messages are limited to 16 bytes per packet */
1871 if (flags
& MSG_OOB
)
1874 /* This works out the maximum size of segment we can send out */
1876 u32 mtu
= dst_mtu(dst
);
1877 mss_now
= min_t(int, dn_mss_from_pmtu(dst
->dev
, mtu
), mss_now
);
1884 * N.B. We get the timeout wrong here, but then we always did get it
1885 * wrong before and this is another step along the road to correcting
1886 * it. It ought to get updated each time we pass through the routine,
1887 * but in practise it probably doesn't matter too much for now.
1889 static inline struct sk_buff
*dn_alloc_send_pskb(struct sock
*sk
,
1890 unsigned long datalen
, int noblock
,
1893 struct sk_buff
*skb
= sock_alloc_send_skb(sk
, datalen
,
1896 skb
->protocol
= __constant_htons(ETH_P_DNA_RT
);
1897 skb
->pkt_type
= PACKET_OUTGOING
;
1902 static int dn_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1903 struct msghdr
*msg
, size_t size
)
1905 struct sock
*sk
= sock
->sk
;
1906 struct dn_scp
*scp
= DN_SK(sk
);
1908 struct sk_buff_head
*queue
= &scp
->data_xmit_queue
;
1909 int flags
= msg
->msg_flags
;
1912 int addr_len
= msg
->msg_namelen
;
1913 struct sockaddr_dn
*addr
= (struct sockaddr_dn
*)msg
->msg_name
;
1914 struct sk_buff
*skb
= NULL
;
1915 struct dn_skb_cb
*cb
;
1917 unsigned char fctype
;
1920 if (flags
& ~(MSG_TRYHARD
|MSG_OOB
|MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|MSG_MORE
|MSG_CMSG_COMPAT
))
1923 if (addr_len
&& (addr_len
!= sizeof(struct sockaddr_dn
)))
1927 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
1929 * The only difference between stream sockets and sequenced packet
1930 * sockets is that the stream sockets always behave as if MSG_EOR
1933 if (sock
->type
== SOCK_STREAM
) {
1934 if (flags
& MSG_EOR
) {
1942 err
= dn_check_state(sk
, addr
, addr_len
, &timeo
, flags
);
1946 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1948 if (!(flags
& MSG_NOSIGNAL
))
1949 send_sig(SIGPIPE
, current
, 0);
1953 if ((flags
& MSG_TRYHARD
) && sk
->sk_dst_cache
)
1954 dst_negative_advice(&sk
->sk_dst_cache
);
1956 mss
= scp
->segsize_rem
;
1957 fctype
= scp
->services_rem
& NSP_FC_MASK
;
1959 mss
= dn_current_mss(sk
, flags
);
1961 if (flags
& MSG_OOB
) {
1962 queue
= &scp
->other_xmit_queue
;
1969 scp
->persist_fxn
= dn_nsp_xmit_timeout
;
1971 while(sent
< size
) {
1972 err
= sock_error(sk
);
1976 if (signal_pending(current
)) {
1977 err
= sock_intr_errno(timeo
);
1982 * Calculate size that we wish to send.
1990 * Wait for queue size to go down below the window
1993 if (dn_queue_too_long(scp
, queue
, flags
)) {
1994 if (flags
& MSG_DONTWAIT
) {
2001 if (dn_queue_too_long(scp
, queue
, flags
))
2010 * Get a suitably sized skb.
2011 * 64 is a bit of a hack really, but its larger than any
2012 * link-layer headers and has served us well as a good
2013 * guess as to their real length.
2015 skb
= dn_alloc_send_pskb(sk
, len
+ 64 + DN_MAX_NSP_DATA_HEADER
,
2016 flags
& MSG_DONTWAIT
, &err
);
2024 cb
= DN_SKB_CB(skb
);
2026 skb_reserve(skb
, 64 + DN_MAX_NSP_DATA_HEADER
);
2028 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
2033 if (flags
& MSG_OOB
) {
2034 cb
->nsp_flags
= 0x30;
2035 if (fctype
!= NSP_FC_NONE
)
2038 cb
->nsp_flags
= 0x00;
2039 if (scp
->seg_total
== 0)
2040 cb
->nsp_flags
|= 0x20;
2042 scp
->seg_total
+= len
;
2044 if (((sent
+ len
) == size
) && (flags
& MSG_EOR
)) {
2045 cb
->nsp_flags
|= 0x40;
2047 if (fctype
== NSP_FC_SCMC
)
2050 if (fctype
== NSP_FC_SRC
)
2055 dn_nsp_queue_xmit(sk
, skb
, sk
->sk_allocation
, flags
& MSG_OOB
);
2058 scp
->persist
= dn_nsp_persist(sk
);
2068 return sent
? sent
: err
;
2071 err
= sk_stream_error(sk
, flags
, err
);
2076 static int dn_device_event(struct notifier_block
*this, unsigned long event
,
2079 struct net_device
*dev
= (struct net_device
*)ptr
;
2095 static struct notifier_block dn_dev_notifier
= {
2096 .notifier_call
= dn_device_event
,
2099 extern int dn_route_rcv(struct sk_buff
*, struct net_device
*, struct packet_type
*, struct net_device
*);
2101 static struct packet_type dn_dix_packet_type
= {
2102 .type
= __constant_htons(ETH_P_DNA_RT
),
2103 .dev
= NULL
, /* All devices */
2104 .func
= dn_route_rcv
,
2107 #ifdef CONFIG_PROC_FS
2108 struct dn_iter_state
{
2112 static struct sock
*dn_socket_get_first(struct seq_file
*seq
)
2114 struct dn_iter_state
*state
= seq
->private;
2115 struct sock
*n
= NULL
;
2117 for(state
->bucket
= 0;
2118 state
->bucket
< DN_SK_HASH_SIZE
;
2120 n
= sk_head(&dn_sk_hash
[state
->bucket
]);
2128 static struct sock
*dn_socket_get_next(struct seq_file
*seq
,
2131 struct dn_iter_state
*state
= seq
->private;
2137 if (++state
->bucket
>= DN_SK_HASH_SIZE
)
2139 n
= sk_head(&dn_sk_hash
[state
->bucket
]);
2145 static struct sock
*socket_get_idx(struct seq_file
*seq
, loff_t
*pos
)
2147 struct sock
*sk
= dn_socket_get_first(seq
);
2150 while(*pos
&& (sk
= dn_socket_get_next(seq
, sk
)))
2153 return *pos
? NULL
: sk
;
2156 static void *dn_socket_get_idx(struct seq_file
*seq
, loff_t pos
)
2159 read_lock_bh(&dn_hash_lock
);
2160 rc
= socket_get_idx(seq
, &pos
);
2162 read_unlock_bh(&dn_hash_lock
);
2167 static void *dn_socket_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2169 return *pos
? dn_socket_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2172 static void *dn_socket_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2176 if (v
== SEQ_START_TOKEN
) {
2177 rc
= dn_socket_get_idx(seq
, 0);
2181 rc
= dn_socket_get_next(seq
, v
);
2184 read_unlock_bh(&dn_hash_lock
);
2190 static void dn_socket_seq_stop(struct seq_file
*seq
, void *v
)
2192 if (v
&& v
!= SEQ_START_TOKEN
)
2193 read_unlock_bh(&dn_hash_lock
);
2196 #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
2198 static void dn_printable_object(struct sockaddr_dn
*dn
, unsigned char *buf
)
2202 switch (dn_ntohs(dn
->sdn_objnamel
)) {
2204 sprintf(buf
, "%d", dn
->sdn_objnum
);
2207 for (i
= 0; i
< dn_ntohs(dn
->sdn_objnamel
); i
++) {
2208 buf
[i
] = dn
->sdn_objname
[i
];
2209 if (IS_NOT_PRINTABLE(buf
[i
]))
2216 static char *dn_state2asc(unsigned char state
)
2256 static inline void dn_socket_format_entry(struct seq_file
*seq
, struct sock
*sk
)
2258 struct dn_scp
*scp
= DN_SK(sk
);
2259 char buf1
[DN_ASCBUF_LEN
];
2260 char buf2
[DN_ASCBUF_LEN
];
2261 char local_object
[DN_MAXOBJL
+3];
2262 char remote_object
[DN_MAXOBJL
+3];
2264 dn_printable_object(&scp
->addr
, local_object
);
2265 dn_printable_object(&scp
->peer
, remote_object
);
2268 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
2269 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
2270 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp
->addr
)), buf1
),
2278 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp
->peer
)), buf2
),
2286 dn_state2asc(scp
->state
),
2287 ((scp
->accept_mode
== ACC_IMMED
) ? "IMMED" : "DEFER"));
2290 static int dn_socket_seq_show(struct seq_file
*seq
, void *v
)
2292 if (v
== SEQ_START_TOKEN
) {
2293 seq_puts(seq
, "Local Remote\n");
2295 dn_socket_format_entry(seq
, v
);
2300 static struct seq_operations dn_socket_seq_ops
= {
2301 .start
= dn_socket_seq_start
,
2302 .next
= dn_socket_seq_next
,
2303 .stop
= dn_socket_seq_stop
,
2304 .show
= dn_socket_seq_show
,
2307 static int dn_socket_seq_open(struct inode
*inode
, struct file
*file
)
2309 struct seq_file
*seq
;
2311 struct dn_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2316 rc
= seq_open(file
, &dn_socket_seq_ops
);
2320 seq
= file
->private_data
;
2322 memset(s
, 0, sizeof(*s
));
2330 static struct file_operations dn_socket_seq_fops
= {
2331 .owner
= THIS_MODULE
,
2332 .open
= dn_socket_seq_open
,
2334 .llseek
= seq_lseek
,
2335 .release
= seq_release_private
,
2339 static struct net_proto_family dn_family_ops
= {
2340 .family
= AF_DECnet
,
2341 .create
= dn_create
,
2342 .owner
= THIS_MODULE
,
2345 static const struct proto_ops dn_proto_ops
= {
2346 .family
= AF_DECnet
,
2347 .owner
= THIS_MODULE
,
2348 .release
= dn_release
,
2350 .connect
= dn_connect
,
2351 .socketpair
= sock_no_socketpair
,
2352 .accept
= dn_accept
,
2353 .getname
= dn_getname
,
2356 .listen
= dn_listen
,
2357 .shutdown
= dn_shutdown
,
2358 .setsockopt
= dn_setsockopt
,
2359 .getsockopt
= dn_getsockopt
,
2360 .sendmsg
= dn_sendmsg
,
2361 .recvmsg
= dn_recvmsg
,
2362 .mmap
= sock_no_mmap
,
2363 .sendpage
= sock_no_sendpage
,
2366 void dn_register_sysctl(void);
2367 void dn_unregister_sysctl(void);
2369 MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
2370 MODULE_AUTHOR("Linux DECnet Project Team");
2371 MODULE_LICENSE("GPL");
2372 MODULE_ALIAS_NETPROTO(PF_DECnet
);
2374 static char banner
[] __initdata
= KERN_INFO
"NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
2376 static int __init
decnet_init(void)
2382 rc
= proto_register(&dn_proto
, 1);
2391 sock_register(&dn_family_ops
);
2392 dev_add_pack(&dn_dix_packet_type
);
2393 register_netdevice_notifier(&dn_dev_notifier
);
2395 proc_net_fops_create("decnet", S_IRUGO
, &dn_socket_seq_fops
);
2396 dn_register_sysctl();
2401 module_init(decnet_init
);
2404 * Prevent DECnet module unloading until its fixed properly.
2405 * Requires an audit of the code to check for memory leaks and
2406 * initialisation problems etc.
2409 static void __exit
decnet_exit(void)
2411 sock_unregister(AF_DECnet
);
2412 dev_remove_pack(&dn_dix_packet_type
);
2414 dn_unregister_sysctl();
2416 unregister_netdevice_notifier(&dn_dev_notifier
);
2423 proc_net_remove("decnet");
2425 proto_unregister(&dn_proto
);
2427 module_exit(decnet_exit
);