1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * DECnet An implementation of the DECnet protocol suite for the LINUX
5 * operating system. DECnet is implemented using the BSD Socket
6 * interface as the means of communication with the user level.
8 * DECnet Socket Layer Interface
10 * Authors: Eduardo Marcelo Serrat <emserrat@geocities.com>
11 * Patrick Caulfield <patrick@pandh.demon.co.uk>
14 * Steve Whitehouse: Copied from Eduardo Serrat and Patrick Caulfield's
15 * version of the code. Original copyright preserved
17 * Steve Whitehouse: Some bug fixes, cleaning up some code to make it
18 * compatible with my routing layer.
19 * Steve Whitehouse: Merging changes from Eduardo Serrat and Patrick
21 * Steve Whitehouse: Further bug fixes, checking module code still works
22 * with new routing layer.
23 * Steve Whitehouse: Additional set/get_sockopt() calls.
24 * Steve Whitehouse: Fixed TIOCINQ ioctl to be same as Eduardo's new
26 * Steve Whitehouse: recvmsg() changed to try and behave in a POSIX like
27 * way. Didn't manage it entirely, but its better.
28 * Steve Whitehouse: ditto for sendmsg().
29 * Steve Whitehouse: A selection of bug fixes to various things.
30 * Steve Whitehouse: Added TIOCOUTQ ioctl.
31 * Steve Whitehouse: Fixes to username2sockaddr & sockaddr2username.
32 * Steve Whitehouse: Fixes to connect() error returns.
33 * Patrick Caulfield: Fixes to delayed acceptance logic.
34 * David S. Miller: New socket locking
35 * Steve Whitehouse: Socket list hashing/locking
36 * Arnaldo C. Melo: use capable, not suser
37 * Steve Whitehouse: Removed unused code. Fix to use sk->allocation
39 * Patrick Caulfield: /proc/net/decnet now has object name/number
40 * Steve Whitehouse: Fixed local port allocation, hashed sk list
41 * Matthew Wilcox: Fixes for dn_ioctl()
42 * Steve Whitehouse: New connect/accept logic to allow timeouts and
43 * prepare for sendpage etc.
47 /******************************************************************************
48 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
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
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
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
87 Version 0.0.6 2.1.110 07-aug-98 Eduardo Marcelo Serrat
92 *******************************************************************************/
94 #include <linux/module.h>
95 #include <linux/errno.h>
96 #include <linux/types.h>
97 #include <linux/slab.h>
98 #include <linux/socket.h>
100 #include <linux/kernel.h>
101 #include <linux/sched/signal.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 <linux/netfilter.h>
110 #include <linux/seq_file.h>
111 #include <net/sock.h>
112 #include <net/tcp_states.h>
113 #include <net/flow.h>
114 #include <asm/ioctls.h>
115 #include <linux/capability.h>
116 #include <linux/mm.h>
117 #include <linux/interrupt.h>
118 #include <linux/proc_fs.h>
119 #include <linux/stat.h>
120 #include <linux/init.h>
121 #include <linux/poll.h>
122 #include <linux/jiffies.h>
123 #include <net/net_namespace.h>
124 #include <net/neighbour.h>
126 #include <net/fib_rules.h>
129 #include <net/dn_nsp.h>
130 #include <net/dn_dev.h>
131 #include <net/dn_route.h>
132 #include <net/dn_fib.h>
133 #include <net/dn_neigh.h>
140 static void dn_keepalive(struct sock
*sk
);
142 #define DN_SK_HASH_SHIFT 8
143 #define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
144 #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
147 static const struct proto_ops dn_proto_ops
;
148 static DEFINE_RWLOCK(dn_hash_lock
);
149 static struct hlist_head dn_sk_hash
[DN_SK_HASH_SIZE
];
150 static struct hlist_head dn_wild_sk
;
151 static atomic_long_t decnet_memory_allocated
;
153 static int __dn_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
, int flags
);
154 static int __dn_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
, int flags
);
156 static struct hlist_head
*dn_find_list(struct sock
*sk
)
158 struct dn_scp
*scp
= DN_SK(sk
);
160 if (scp
->addr
.sdn_flags
& SDF_WILD
)
161 return hlist_empty(&dn_wild_sk
) ? &dn_wild_sk
: NULL
;
163 return &dn_sk_hash
[le16_to_cpu(scp
->addrloc
) & DN_SK_HASH_MASK
];
167 * Valid ports are those greater than zero and not already in use.
169 static int check_port(__le16 port
)
176 sk_for_each(sk
, &dn_sk_hash
[le16_to_cpu(port
) & DN_SK_HASH_MASK
]) {
177 struct dn_scp
*scp
= DN_SK(sk
);
178 if (scp
->addrloc
== port
)
184 static unsigned short port_alloc(struct sock
*sk
)
186 struct dn_scp
*scp
= DN_SK(sk
);
187 static unsigned short port
= 0x2000;
188 unsigned short i_port
= port
;
190 while(check_port(cpu_to_le16(++port
)) != 0) {
195 scp
->addrloc
= cpu_to_le16(port
);
201 * Since this is only ever called from user
202 * level, we don't need a write_lock() version
205 static int dn_hash_sock(struct sock
*sk
)
207 struct dn_scp
*scp
= DN_SK(sk
);
208 struct hlist_head
*list
;
211 BUG_ON(sk_hashed(sk
));
213 write_lock_bh(&dn_hash_lock
);
215 if (!scp
->addrloc
&& !port_alloc(sk
))
219 if ((list
= dn_find_list(sk
)) == NULL
)
222 sk_add_node(sk
, list
);
225 write_unlock_bh(&dn_hash_lock
);
229 static void dn_unhash_sock(struct sock
*sk
)
231 write_lock(&dn_hash_lock
);
232 sk_del_node_init(sk
);
233 write_unlock(&dn_hash_lock
);
236 static void dn_unhash_sock_bh(struct sock
*sk
)
238 write_lock_bh(&dn_hash_lock
);
239 sk_del_node_init(sk
);
240 write_unlock_bh(&dn_hash_lock
);
243 static struct hlist_head
*listen_hash(struct sockaddr_dn
*addr
)
246 unsigned int hash
= addr
->sdn_objnum
;
249 hash
= addr
->sdn_objnamel
;
250 for(i
= 0; i
< le16_to_cpu(addr
->sdn_objnamel
); i
++) {
251 hash
^= addr
->sdn_objname
[i
];
256 return &dn_sk_hash
[hash
& DN_SK_HASH_MASK
];
260 * Called to transform a socket from bound (i.e. with a local address)
261 * into a listening socket (doesn't need a local port number) and rehashes
262 * based upon the object name/number.
264 static void dn_rehash_sock(struct sock
*sk
)
266 struct hlist_head
*list
;
267 struct dn_scp
*scp
= DN_SK(sk
);
269 if (scp
->addr
.sdn_flags
& SDF_WILD
)
272 write_lock_bh(&dn_hash_lock
);
273 sk_del_node_init(sk
);
274 DN_SK(sk
)->addrloc
= 0;
275 list
= listen_hash(&DN_SK(sk
)->addr
);
276 sk_add_node(sk
, list
);
277 write_unlock_bh(&dn_hash_lock
);
280 int dn_sockaddr2username(struct sockaddr_dn
*sdn
, unsigned char *buf
, unsigned char type
)
288 *buf
++ = sdn
->sdn_objnum
;
292 *buf
++ = le16_to_cpu(sdn
->sdn_objnamel
);
293 memcpy(buf
, sdn
->sdn_objname
, le16_to_cpu(sdn
->sdn_objnamel
));
294 len
= 3 + le16_to_cpu(sdn
->sdn_objnamel
);
299 *buf
++ = le16_to_cpu(sdn
->sdn_objnamel
);
300 memcpy(buf
, sdn
->sdn_objname
, le16_to_cpu(sdn
->sdn_objnamel
));
301 len
= 7 + le16_to_cpu(sdn
->sdn_objnamel
);
309 * On reception of usernames, we handle types 1 and 0 for destination
310 * addresses only. Types 2 and 4 are used for source addresses, but the
311 * UIC, GIC are ignored and they are both treated the same way. Type 3
312 * is never used as I've no idea what its purpose might be or what its
315 int dn_username2sockaddr(unsigned char *data
, int len
, struct sockaddr_dn
*sdn
, unsigned char *fmt
)
322 sdn
->sdn_objnamel
= cpu_to_le16(0);
323 memset(sdn
->sdn_objname
, 0, DN_MAXOBJL
);
334 sdn
->sdn_objnum
= type
;
356 sdn
->sdn_objnamel
= cpu_to_le16(*data
++);
357 len
-= le16_to_cpu(sdn
->sdn_objnamel
);
359 if ((len
< 0) || (le16_to_cpu(sdn
->sdn_objnamel
) > namel
))
362 memcpy(sdn
->sdn_objname
, data
, le16_to_cpu(sdn
->sdn_objnamel
));
367 struct sock
*dn_sklist_find_listener(struct sockaddr_dn
*addr
)
369 struct hlist_head
*list
= listen_hash(addr
);
372 read_lock(&dn_hash_lock
);
373 sk_for_each(sk
, list
) {
374 struct dn_scp
*scp
= DN_SK(sk
);
375 if (sk
->sk_state
!= TCP_LISTEN
)
377 if (scp
->addr
.sdn_objnum
) {
378 if (scp
->addr
.sdn_objnum
!= addr
->sdn_objnum
)
381 if (addr
->sdn_objnum
)
383 if (scp
->addr
.sdn_objnamel
!= addr
->sdn_objnamel
)
385 if (memcmp(scp
->addr
.sdn_objname
, addr
->sdn_objname
, le16_to_cpu(addr
->sdn_objnamel
)) != 0)
389 read_unlock(&dn_hash_lock
);
393 sk
= sk_head(&dn_wild_sk
);
395 if (sk
->sk_state
== TCP_LISTEN
)
401 read_unlock(&dn_hash_lock
);
405 struct sock
*dn_find_by_skb(struct sk_buff
*skb
)
407 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
411 read_lock(&dn_hash_lock
);
412 sk_for_each(sk
, &dn_sk_hash
[le16_to_cpu(cb
->dst_port
) & DN_SK_HASH_MASK
]) {
414 if (cb
->src
!= dn_saddr2dn(&scp
->peer
))
416 if (cb
->dst_port
!= scp
->addrloc
)
418 if (scp
->addrrem
&& (cb
->src_port
!= scp
->addrrem
))
425 read_unlock(&dn_hash_lock
);
431 static void dn_destruct(struct sock
*sk
)
433 struct dn_scp
*scp
= DN_SK(sk
);
435 skb_queue_purge(&scp
->data_xmit_queue
);
436 skb_queue_purge(&scp
->other_xmit_queue
);
437 skb_queue_purge(&scp
->other_receive_queue
);
439 dst_release(rcu_dereference_protected(sk
->sk_dst_cache
, 1));
442 static unsigned long dn_memory_pressure
;
444 static void dn_enter_memory_pressure(struct sock
*sk
)
446 if (!dn_memory_pressure
) {
447 dn_memory_pressure
= 1;
451 static struct proto dn_proto
= {
453 .owner
= THIS_MODULE
,
454 .enter_memory_pressure
= dn_enter_memory_pressure
,
455 .memory_pressure
= &dn_memory_pressure
,
456 .memory_allocated
= &decnet_memory_allocated
,
457 .sysctl_mem
= sysctl_decnet_mem
,
458 .sysctl_wmem
= sysctl_decnet_wmem
,
459 .sysctl_rmem
= sysctl_decnet_rmem
,
460 .max_header
= DN_MAX_NSP_DATA_HEADER
+ 64,
461 .obj_size
= sizeof(struct dn_sock
),
464 static struct sock
*dn_alloc_sock(struct net
*net
, struct socket
*sock
, gfp_t gfp
, int kern
)
467 struct sock
*sk
= sk_alloc(net
, PF_DECnet
, gfp
, &dn_proto
, kern
);
473 sock
->ops
= &dn_proto_ops
;
474 sock_init_data(sock
, sk
);
476 sk
->sk_backlog_rcv
= dn_nsp_backlog_rcv
;
477 sk
->sk_destruct
= dn_destruct
;
478 sk
->sk_no_check_tx
= 1;
479 sk
->sk_family
= PF_DECnet
;
481 sk
->sk_allocation
= gfp
;
482 sk
->sk_sndbuf
= sysctl_decnet_wmem
[1];
483 sk
->sk_rcvbuf
= sysctl_decnet_rmem
[1];
485 /* Initialization of DECnet Session Control Port */
487 scp
->state
= DN_O
; /* Open */
488 scp
->numdat
= 1; /* Next data seg to tx */
489 scp
->numoth
= 1; /* Next oth data to tx */
490 scp
->ackxmt_dat
= 0; /* Last data seg ack'ed */
491 scp
->ackxmt_oth
= 0; /* Last oth data ack'ed */
492 scp
->ackrcv_dat
= 0; /* Highest data ack recv*/
493 scp
->ackrcv_oth
= 0; /* Last oth data ack rec*/
494 scp
->flowrem_sw
= DN_SEND
;
495 scp
->flowloc_sw
= DN_SEND
;
496 scp
->flowrem_dat
= 0;
497 scp
->flowrem_oth
= 1;
498 scp
->flowloc_dat
= 0;
499 scp
->flowloc_oth
= 1;
500 scp
->services_rem
= 0;
501 scp
->services_loc
= 1 | NSP_FC_NONE
;
503 scp
->info_loc
= 0x03; /* NSP version 4.1 */
504 scp
->segsize_rem
= 230 - DN_MAX_NSP_DATA_HEADER
; /* Default: Updated by remote segsize */
507 scp
->accept_mode
= ACC_IMMED
;
508 scp
->addr
.sdn_family
= AF_DECnet
;
509 scp
->peer
.sdn_family
= AF_DECnet
;
510 scp
->accessdata
.acc_accl
= 5;
511 memcpy(scp
->accessdata
.acc_acc
, "LINUX", 5);
513 scp
->max_window
= NSP_MAX_WINDOW
;
514 scp
->snd_window
= NSP_MIN_WINDOW
;
515 scp
->nsp_srtt
= NSP_INITIAL_SRTT
;
516 scp
->nsp_rttvar
= NSP_INITIAL_RTTVAR
;
517 scp
->nsp_rxtshift
= 0;
519 skb_queue_head_init(&scp
->data_xmit_queue
);
520 skb_queue_head_init(&scp
->other_xmit_queue
);
521 skb_queue_head_init(&scp
->other_receive_queue
);
524 scp
->persist_fxn
= NULL
;
525 scp
->keepalive
= 10 * HZ
;
526 scp
->keepalive_fxn
= dn_keepalive
;
528 dn_start_slow_timer(sk
);
535 * FIXME: Should respond to SO_KEEPALIVE etc.
537 static void dn_keepalive(struct sock
*sk
)
539 struct dn_scp
*scp
= DN_SK(sk
);
542 * By checking the other_data transmit queue is empty
543 * we are double checking that we are not sending too
544 * many of these keepalive frames.
546 if (skb_queue_empty(&scp
->other_xmit_queue
))
547 dn_nsp_send_link(sk
, DN_NOCHANGE
, 0);
552 * Timer for shutdown/destroyed sockets.
553 * When socket is dead & no packets have been sent for a
554 * certain amount of time, they are removed by this
555 * routine. Also takes care of sending out DI & DC
556 * frames at correct times.
558 int dn_destroy_timer(struct sock
*sk
)
560 struct dn_scp
*scp
= DN_SK(sk
);
562 scp
->persist
= dn_nsp_persist(sk
);
564 switch (scp
->state
) {
566 dn_nsp_send_disc(sk
, NSP_DISCINIT
, 0, GFP_ATOMIC
);
567 if (scp
->nsp_rxtshift
>= decnet_di_count
)
572 dn_nsp_send_disc(sk
, NSP_DISCINIT
, 0, GFP_ATOMIC
);
573 if (scp
->nsp_rxtshift
>= decnet_dr_count
)
578 if (scp
->nsp_rxtshift
< decnet_dn_count
) {
579 /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
580 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
,
586 scp
->persist
= (HZ
* decnet_time_wait
);
591 if (time_after_eq(jiffies
, scp
->stamp
+ HZ
* decnet_time_wait
)) {
600 static void dn_destroy_sock(struct sock
*sk
)
602 struct dn_scp
*scp
= DN_SK(sk
);
604 scp
->nsp_rxtshift
= 0; /* reset back off */
607 if (sk
->sk_socket
->state
!= SS_UNCONNECTED
)
608 sk
->sk_socket
->state
= SS_DISCONNECTING
;
611 sk
->sk_state
= TCP_CLOSE
;
613 switch (scp
->state
) {
615 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
,
617 scp
->persist_fxn
= dn_destroy_timer
;
618 scp
->persist
= dn_nsp_persist(sk
);
629 dn_nsp_send_disc(sk
, NSP_DISCINIT
, 0, sk
->sk_allocation
);
639 scp
->persist_fxn
= dn_destroy_timer
;
640 scp
->persist
= dn_nsp_persist(sk
);
643 printk(KERN_DEBUG
"DECnet: dn_destroy_sock passed socket in invalid state\n");
646 dn_stop_slow_timer(sk
);
648 dn_unhash_sock_bh(sk
);
655 char *dn_addr2asc(__u16 addr
, char *buf
)
657 unsigned short node
, area
;
659 node
= addr
& 0x03ff;
661 sprintf(buf
, "%hd.%hd", area
, node
);
668 static int dn_create(struct net
*net
, struct socket
*sock
, int protocol
,
673 if (protocol
< 0 || protocol
> SK_PROTOCOL_MAX
)
676 if (!net_eq(net
, &init_net
))
677 return -EAFNOSUPPORT
;
679 switch (sock
->type
) {
681 if (protocol
!= DNPROTO_NSP
)
682 return -EPROTONOSUPPORT
;
687 return -ESOCKTNOSUPPORT
;
691 if ((sk
= dn_alloc_sock(net
, sock
, GFP_KERNEL
, kern
)) == NULL
)
694 sk
->sk_protocol
= protocol
;
701 dn_release(struct socket
*sock
)
703 struct sock
*sk
= sock
->sk
;
717 static int dn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
719 struct sock
*sk
= sock
->sk
;
720 struct dn_scp
*scp
= DN_SK(sk
);
721 struct sockaddr_dn
*saddr
= (struct sockaddr_dn
*)uaddr
;
722 struct net_device
*dev
, *ldev
;
725 if (addr_len
!= sizeof(struct sockaddr_dn
))
728 if (saddr
->sdn_family
!= AF_DECnet
)
731 if (le16_to_cpu(saddr
->sdn_nodeaddrl
) && (le16_to_cpu(saddr
->sdn_nodeaddrl
) != 2))
734 if (le16_to_cpu(saddr
->sdn_objnamel
) > DN_MAXOBJL
)
737 if (saddr
->sdn_flags
& ~SDF_WILD
)
740 if (!capable(CAP_NET_BIND_SERVICE
) && (saddr
->sdn_objnum
||
741 (saddr
->sdn_flags
& SDF_WILD
)))
744 if (!(saddr
->sdn_flags
& SDF_WILD
)) {
745 if (le16_to_cpu(saddr
->sdn_nodeaddrl
)) {
748 for_each_netdev_rcu(&init_net
, dev
) {
751 if (dn_dev_islocal(dev
, dn_saddr2dn(saddr
))) {
758 return -EADDRNOTAVAIL
;
764 if (sock_flag(sk
, SOCK_ZAPPED
)) {
765 memcpy(&scp
->addr
, saddr
, addr_len
);
766 sock_reset_flag(sk
, SOCK_ZAPPED
);
768 rv
= dn_hash_sock(sk
);
770 sock_set_flag(sk
, SOCK_ZAPPED
);
778 static int dn_auto_bind(struct socket
*sock
)
780 struct sock
*sk
= sock
->sk
;
781 struct dn_scp
*scp
= DN_SK(sk
);
784 sock_reset_flag(sk
, SOCK_ZAPPED
);
786 scp
->addr
.sdn_flags
= 0;
787 scp
->addr
.sdn_objnum
= 0;
790 * This stuff is to keep compatibility with Eduardo's
791 * patch. I hope I can dispense with it shortly...
793 if ((scp
->accessdata
.acc_accl
!= 0) &&
794 (scp
->accessdata
.acc_accl
<= 12)) {
796 scp
->addr
.sdn_objnamel
= cpu_to_le16(scp
->accessdata
.acc_accl
);
797 memcpy(scp
->addr
.sdn_objname
, scp
->accessdata
.acc_acc
, le16_to_cpu(scp
->addr
.sdn_objnamel
));
799 scp
->accessdata
.acc_accl
= 0;
800 memset(scp
->accessdata
.acc_acc
, 0, 40);
802 /* End of compatibility stuff */
804 scp
->addr
.sdn_add
.a_len
= cpu_to_le16(2);
805 rv
= dn_dev_bind_default((__le16
*)scp
->addr
.sdn_add
.a_addr
);
807 rv
= dn_hash_sock(sk
);
809 sock_set_flag(sk
, SOCK_ZAPPED
);
815 static int dn_confirm_accept(struct sock
*sk
, long *timeo
, gfp_t allocation
)
817 struct dn_scp
*scp
= DN_SK(sk
);
821 if (scp
->state
!= DN_CR
)
825 scp
->segsize_loc
= dst_metric_advmss(__sk_dst_get(sk
));
826 dn_send_conn_conf(sk
, allocation
);
828 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
831 if (scp
->state
== DN_CC
)
832 *timeo
= schedule_timeout(*timeo
);
835 if (scp
->state
== DN_RUN
)
837 err
= sock_error(sk
);
840 err
= sock_intr_errno(*timeo
);
841 if (signal_pending(current
))
846 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
848 finish_wait(sk_sleep(sk
), &wait
);
850 sk
->sk_socket
->state
= SS_CONNECTED
;
851 } else if (scp
->state
!= DN_CC
) {
852 sk
->sk_socket
->state
= SS_UNCONNECTED
;
857 static int dn_wait_run(struct sock
*sk
, long *timeo
)
859 struct dn_scp
*scp
= DN_SK(sk
);
863 if (scp
->state
== DN_RUN
)
869 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
872 if (scp
->state
== DN_CI
|| scp
->state
== DN_CC
)
873 *timeo
= schedule_timeout(*timeo
);
876 if (scp
->state
== DN_RUN
)
878 err
= sock_error(sk
);
881 err
= sock_intr_errno(*timeo
);
882 if (signal_pending(current
))
887 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
889 finish_wait(sk_sleep(sk
), &wait
);
892 sk
->sk_socket
->state
= SS_CONNECTED
;
893 } else if (scp
->state
!= DN_CI
&& scp
->state
!= DN_CC
) {
894 sk
->sk_socket
->state
= SS_UNCONNECTED
;
899 static int __dn_connect(struct sock
*sk
, struct sockaddr_dn
*addr
, int addrlen
, long *timeo
, int flags
)
901 struct socket
*sock
= sk
->sk_socket
;
902 struct dn_scp
*scp
= DN_SK(sk
);
905 struct dst_entry
*dst
;
907 if (sock
->state
== SS_CONNECTED
)
910 if (sock
->state
== SS_CONNECTING
) {
912 if (scp
->state
== DN_RUN
) {
913 sock
->state
= SS_CONNECTED
;
917 if (scp
->state
!= DN_CI
&& scp
->state
!= DN_CC
) {
918 sock
->state
= SS_UNCONNECTED
;
921 return dn_wait_run(sk
, timeo
);
925 if (scp
->state
!= DN_O
)
928 if (addr
== NULL
|| addrlen
!= sizeof(struct sockaddr_dn
))
930 if (addr
->sdn_family
!= AF_DECnet
)
932 if (addr
->sdn_flags
& SDF_WILD
)
935 if (sock_flag(sk
, SOCK_ZAPPED
)) {
936 err
= dn_auto_bind(sk
->sk_socket
);
941 memcpy(&scp
->peer
, addr
, sizeof(struct sockaddr_dn
));
944 memset(&fld
, 0, sizeof(fld
));
945 fld
.flowidn_oif
= sk
->sk_bound_dev_if
;
946 fld
.daddr
= dn_saddr2dn(&scp
->peer
);
947 fld
.saddr
= dn_saddr2dn(&scp
->addr
);
948 dn_sk_ports_copy(&fld
, scp
);
949 fld
.flowidn_proto
= DNPROTO_NSP
;
950 if (dn_route_output_sock(&sk
->sk_dst_cache
, &fld
, sk
, flags
) < 0)
952 dst
= __sk_dst_get(sk
);
953 sk
->sk_route_caps
= dst
->dev
->features
;
954 sock
->state
= SS_CONNECTING
;
956 scp
->segsize_loc
= dst_metric_advmss(dst
);
958 dn_nsp_send_conninit(sk
, NSP_CI
);
961 err
= dn_wait_run(sk
, timeo
);
967 static int dn_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addrlen
, int flags
)
969 struct sockaddr_dn
*addr
= (struct sockaddr_dn
*)uaddr
;
970 struct sock
*sk
= sock
->sk
;
972 long timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
975 err
= __dn_connect(sk
, addr
, addrlen
, &timeo
, 0);
981 static inline int dn_check_state(struct sock
*sk
, struct sockaddr_dn
*addr
, int addrlen
, long *timeo
, int flags
)
983 struct dn_scp
*scp
= DN_SK(sk
);
985 switch (scp
->state
) {
989 return dn_confirm_accept(sk
, timeo
, sk
->sk_allocation
);
992 return dn_wait_run(sk
, timeo
);
994 return __dn_connect(sk
, addr
, addrlen
, timeo
, flags
);
1001 static void dn_access_copy(struct sk_buff
*skb
, struct accessdata_dn
*acc
)
1003 unsigned char *ptr
= skb
->data
;
1005 acc
->acc_userl
= *ptr
++;
1006 memcpy(&acc
->acc_user
, ptr
, acc
->acc_userl
);
1007 ptr
+= acc
->acc_userl
;
1009 acc
->acc_passl
= *ptr
++;
1010 memcpy(&acc
->acc_pass
, ptr
, acc
->acc_passl
);
1011 ptr
+= acc
->acc_passl
;
1013 acc
->acc_accl
= *ptr
++;
1014 memcpy(&acc
->acc_acc
, ptr
, acc
->acc_accl
);
1016 skb_pull(skb
, acc
->acc_accl
+ acc
->acc_passl
+ acc
->acc_userl
+ 3);
1020 static void dn_user_copy(struct sk_buff
*skb
, struct optdata_dn
*opt
)
1022 unsigned char *ptr
= skb
->data
;
1023 u16 len
= *ptr
++; /* yes, it's 8bit on the wire */
1025 BUG_ON(len
> 16); /* we've checked the contents earlier */
1026 opt
->opt_optl
= cpu_to_le16(len
);
1027 opt
->opt_status
= 0;
1028 memcpy(opt
->opt_data
, ptr
, len
);
1029 skb_pull(skb
, len
+ 1);
1032 static struct sk_buff
*dn_wait_for_connect(struct sock
*sk
, long *timeo
)
1035 struct sk_buff
*skb
= NULL
;
1038 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1041 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1043 *timeo
= schedule_timeout(*timeo
);
1044 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1050 if (sk
->sk_state
!= TCP_LISTEN
)
1052 err
= sock_intr_errno(*timeo
);
1053 if (signal_pending(current
))
1058 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1060 finish_wait(sk_sleep(sk
), &wait
);
1062 return skb
== NULL
? ERR_PTR(err
) : skb
;
1065 static int dn_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
1068 struct sock
*sk
= sock
->sk
, *newsk
;
1069 struct sk_buff
*skb
= NULL
;
1070 struct dn_skb_cb
*cb
;
1071 unsigned char menuver
;
1074 long timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1075 struct dst_entry
*dst
;
1079 if (sk
->sk_state
!= TCP_LISTEN
|| DN_SK(sk
)->state
!= DN_O
) {
1084 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1086 skb
= dn_wait_for_connect(sk
, &timeo
);
1089 return PTR_ERR(skb
);
1093 cb
= DN_SKB_CB(skb
);
1094 sk
->sk_ack_backlog
--;
1095 newsk
= dn_alloc_sock(sock_net(sk
), newsock
, sk
->sk_allocation
, kern
);
1096 if (newsk
== NULL
) {
1104 sk_dst_set(newsk
, dst
);
1105 skb_dst_set(skb
, NULL
);
1107 DN_SK(newsk
)->state
= DN_CR
;
1108 DN_SK(newsk
)->addrrem
= cb
->src_port
;
1109 DN_SK(newsk
)->services_rem
= cb
->services
;
1110 DN_SK(newsk
)->info_rem
= cb
->info
;
1111 DN_SK(newsk
)->segsize_rem
= cb
->segsize
;
1112 DN_SK(newsk
)->accept_mode
= DN_SK(sk
)->accept_mode
;
1114 if (DN_SK(newsk
)->segsize_rem
< 230)
1115 DN_SK(newsk
)->segsize_rem
= 230;
1117 if ((DN_SK(newsk
)->services_rem
& NSP_FC_MASK
) == NSP_FC_NONE
)
1118 DN_SK(newsk
)->max_window
= decnet_no_fc_max_cwnd
;
1120 newsk
->sk_state
= TCP_LISTEN
;
1121 memcpy(&(DN_SK(newsk
)->addr
), &(DN_SK(sk
)->addr
), sizeof(struct sockaddr_dn
));
1124 * If we are listening on a wild socket, we don't want
1125 * the newly created socket on the wrong hash queue.
1127 DN_SK(newsk
)->addr
.sdn_flags
&= ~SDF_WILD
;
1129 skb_pull(skb
, dn_username2sockaddr(skb
->data
, skb
->len
, &(DN_SK(newsk
)->addr
), &type
));
1130 skb_pull(skb
, dn_username2sockaddr(skb
->data
, skb
->len
, &(DN_SK(newsk
)->peer
), &type
));
1131 *(__le16
*)(DN_SK(newsk
)->peer
.sdn_add
.a_addr
) = cb
->src
;
1132 *(__le16
*)(DN_SK(newsk
)->addr
.sdn_add
.a_addr
) = cb
->dst
;
1134 menuver
= *skb
->data
;
1137 if (menuver
& DN_MENUVER_ACC
)
1138 dn_access_copy(skb
, &(DN_SK(newsk
)->accessdata
));
1140 if (menuver
& DN_MENUVER_USR
)
1141 dn_user_copy(skb
, &(DN_SK(newsk
)->conndata_in
));
1143 if (menuver
& DN_MENUVER_PRX
)
1144 DN_SK(newsk
)->peer
.sdn_flags
|= SDF_PROXY
;
1146 if (menuver
& DN_MENUVER_UIC
)
1147 DN_SK(newsk
)->peer
.sdn_flags
|= SDF_UICPROXY
;
1151 memcpy(&(DN_SK(newsk
)->conndata_out
), &(DN_SK(sk
)->conndata_out
),
1152 sizeof(struct optdata_dn
));
1153 memcpy(&(DN_SK(newsk
)->discdata_out
), &(DN_SK(sk
)->discdata_out
),
1154 sizeof(struct optdata_dn
));
1157 err
= dn_hash_sock(newsk
);
1159 sock_reset_flag(newsk
, SOCK_ZAPPED
);
1160 dn_send_conn_ack(newsk
);
1163 * Here we use sk->sk_allocation since although the conn conf is
1164 * for the newsk, the context is the old socket.
1166 if (DN_SK(newsk
)->accept_mode
== ACC_IMMED
)
1167 err
= dn_confirm_accept(newsk
, &timeo
,
1170 release_sock(newsk
);
1175 static int dn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,int peer
)
1177 struct sockaddr_dn
*sa
= (struct sockaddr_dn
*)uaddr
;
1178 struct sock
*sk
= sock
->sk
;
1179 struct dn_scp
*scp
= DN_SK(sk
);
1184 if ((sock
->state
!= SS_CONNECTED
&&
1185 sock
->state
!= SS_CONNECTING
) &&
1186 scp
->accept_mode
== ACC_IMMED
) {
1191 memcpy(sa
, &scp
->peer
, sizeof(struct sockaddr_dn
));
1193 memcpy(sa
, &scp
->addr
, sizeof(struct sockaddr_dn
));
1198 return sizeof(struct sockaddr_dn
);
1202 static __poll_t
dn_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
1204 struct sock
*sk
= sock
->sk
;
1205 struct dn_scp
*scp
= DN_SK(sk
);
1206 __poll_t mask
= datagram_poll(file
, sock
, wait
);
1208 if (!skb_queue_empty(&scp
->other_receive_queue
))
1209 mask
|= EPOLLRDBAND
;
1214 static int dn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1216 struct sock
*sk
= sock
->sk
;
1217 struct dn_scp
*scp
= DN_SK(sk
);
1218 int err
= -EOPNOTSUPP
;
1220 struct sk_buff
*skb
;
1227 return dn_dev_ioctl(cmd
, (void __user
*)arg
);
1231 val
= !skb_queue_empty(&scp
->other_receive_queue
);
1232 if (scp
->state
!= DN_RUN
)
1238 amount
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1241 err
= put_user(amount
, (int __user
*)arg
);
1246 skb
= skb_peek(&scp
->other_receive_queue
);
1250 skb_queue_walk(&sk
->sk_receive_queue
, skb
)
1254 err
= put_user(amount
, (int __user
*)arg
);
1265 static int dn_listen(struct socket
*sock
, int backlog
)
1267 struct sock
*sk
= sock
->sk
;
1272 if (sock_flag(sk
, SOCK_ZAPPED
))
1275 if ((DN_SK(sk
)->state
!= DN_O
) || (sk
->sk_state
== TCP_LISTEN
))
1278 sk
->sk_max_ack_backlog
= backlog
;
1279 sk
->sk_ack_backlog
= 0;
1280 sk
->sk_state
= TCP_LISTEN
;
1291 static int dn_shutdown(struct socket
*sock
, int how
)
1293 struct sock
*sk
= sock
->sk
;
1294 struct dn_scp
*scp
= DN_SK(sk
);
1295 int err
= -ENOTCONN
;
1299 if (sock
->state
== SS_UNCONNECTED
)
1303 if (sock
->state
== SS_DISCONNECTING
)
1307 if (scp
->state
== DN_O
)
1310 if (how
!= SHUT_RDWR
)
1313 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1314 dn_destroy_sock(sk
);
1323 static int dn_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
1325 struct sock
*sk
= sock
->sk
;
1329 err
= __dn_setsockopt(sock
, level
, optname
, optval
, optlen
, 0);
1331 #ifdef CONFIG_NETFILTER
1332 /* we need to exclude all possible ENOPROTOOPTs except default case */
1333 if (err
== -ENOPROTOOPT
&& optname
!= DSO_LINKINFO
&&
1334 optname
!= DSO_STREAM
&& optname
!= DSO_SEQPACKET
)
1335 err
= nf_setsockopt(sk
, PF_DECnet
, optname
, optval
, optlen
);
1341 static int __dn_setsockopt(struct socket
*sock
, int level
,int optname
, char __user
*optval
, unsigned int optlen
, int flags
)
1343 struct sock
*sk
= sock
->sk
;
1344 struct dn_scp
*scp
= DN_SK(sk
);
1347 struct optdata_dn opt
;
1348 struct accessdata_dn acc
;
1352 unsigned char services
;
1357 if (optlen
&& !optval
)
1360 if (optlen
> sizeof(u
))
1363 if (copy_from_user(&u
, optval
, optlen
))
1368 if (sock
->state
== SS_CONNECTED
)
1370 if ((scp
->state
!= DN_O
) && (scp
->state
!= DN_CR
))
1373 if (optlen
!= sizeof(struct optdata_dn
))
1376 if (le16_to_cpu(u
.opt
.opt_optl
) > 16)
1379 memcpy(&scp
->conndata_out
, &u
.opt
, optlen
);
1383 if (sock
->state
!= SS_CONNECTED
&&
1384 scp
->accept_mode
== ACC_IMMED
)
1387 if (optlen
!= sizeof(struct optdata_dn
))
1390 if (le16_to_cpu(u
.opt
.opt_optl
) > 16)
1393 memcpy(&scp
->discdata_out
, &u
.opt
, optlen
);
1397 if (sock
->state
== SS_CONNECTED
)
1399 if (scp
->state
!= DN_O
)
1402 if (optlen
!= sizeof(struct accessdata_dn
))
1405 if ((u
.acc
.acc_accl
> DN_MAXACCL
) ||
1406 (u
.acc
.acc_passl
> DN_MAXACCL
) ||
1407 (u
.acc
.acc_userl
> DN_MAXACCL
))
1410 memcpy(&scp
->accessdata
, &u
.acc
, optlen
);
1413 case DSO_ACCEPTMODE
:
1414 if (sock
->state
== SS_CONNECTED
)
1416 if (scp
->state
!= DN_O
)
1419 if (optlen
!= sizeof(int))
1422 if ((u
.mode
!= ACC_IMMED
) && (u
.mode
!= ACC_DEFER
))
1425 scp
->accept_mode
= (unsigned char)u
.mode
;
1429 if (scp
->state
!= DN_CR
)
1431 timeo
= sock_rcvtimeo(sk
, 0);
1432 err
= dn_confirm_accept(sk
, &timeo
, sk
->sk_allocation
);
1436 if (scp
->state
!= DN_CR
)
1440 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1441 dn_nsp_send_disc(sk
, 0x38, 0, sk
->sk_allocation
);
1445 if (optlen
!= sizeof(unsigned long))
1447 if (u
.win
> NSP_MAX_WINDOW
)
1448 u
.win
= NSP_MAX_WINDOW
;
1451 scp
->max_window
= u
.win
;
1452 if (scp
->snd_window
> u
.win
)
1453 scp
->snd_window
= u
.win
;
1457 if (optlen
!= sizeof(int))
1459 if (scp
->nonagle
== TCP_NAGLE_CORK
)
1461 scp
->nonagle
= (u
.val
== 0) ? 0 : TCP_NAGLE_OFF
;
1462 /* if (scp->nonagle == 1) { Push pending frames } */
1466 if (optlen
!= sizeof(int))
1468 if (scp
->nonagle
== TCP_NAGLE_OFF
)
1470 scp
->nonagle
= (u
.val
== 0) ? 0 : TCP_NAGLE_CORK
;
1471 /* if (scp->nonagle == 0) { Push pending frames } */
1475 if (optlen
!= sizeof(unsigned char))
1477 if ((u
.services
& ~NSP_FC_MASK
) != 0x01)
1479 if ((u
.services
& NSP_FC_MASK
) == NSP_FC_MASK
)
1481 scp
->services_loc
= u
.services
;
1485 if (optlen
!= sizeof(unsigned char))
1489 scp
->info_loc
= u
.info
;
1496 return -ENOPROTOOPT
;
1502 static int dn_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
1504 struct sock
*sk
= sock
->sk
;
1508 err
= __dn_getsockopt(sock
, level
, optname
, optval
, optlen
, 0);
1510 #ifdef CONFIG_NETFILTER
1511 if (err
== -ENOPROTOOPT
&& optname
!= DSO_STREAM
&&
1512 optname
!= DSO_SEQPACKET
&& optname
!= DSO_CONACCEPT
&&
1513 optname
!= DSO_CONREJECT
) {
1516 if (get_user(len
, optlen
))
1519 err
= nf_getsockopt(sk
, PF_DECnet
, optname
, optval
, &len
);
1521 err
= put_user(len
, optlen
);
1528 static int __dn_getsockopt(struct socket
*sock
, int level
,int optname
, char __user
*optval
,int __user
*optlen
, int flags
)
1530 struct sock
*sk
= sock
->sk
;
1531 struct dn_scp
*scp
= DN_SK(sk
);
1532 struct linkinfo_dn link
;
1534 void *r_data
= NULL
;
1537 if(get_user(r_len
, optlen
))
1542 if (r_len
> sizeof(struct optdata_dn
))
1543 r_len
= sizeof(struct optdata_dn
);
1544 r_data
= &scp
->conndata_in
;
1548 if (r_len
> sizeof(struct optdata_dn
))
1549 r_len
= sizeof(struct optdata_dn
);
1550 r_data
= &scp
->discdata_in
;
1554 if (r_len
> sizeof(struct accessdata_dn
))
1555 r_len
= sizeof(struct accessdata_dn
);
1556 r_data
= &scp
->accessdata
;
1559 case DSO_ACCEPTMODE
:
1560 if (r_len
> sizeof(unsigned char))
1561 r_len
= sizeof(unsigned char);
1562 r_data
= &scp
->accept_mode
;
1566 if (r_len
> sizeof(struct linkinfo_dn
))
1567 r_len
= sizeof(struct linkinfo_dn
);
1569 memset(&link
, 0, sizeof(link
));
1571 switch (sock
->state
) {
1573 link
.idn_linkstate
= LL_CONNECTING
;
1575 case SS_DISCONNECTING
:
1576 link
.idn_linkstate
= LL_DISCONNECTING
;
1579 link
.idn_linkstate
= LL_RUNNING
;
1582 link
.idn_linkstate
= LL_INACTIVE
;
1585 link
.idn_segsize
= scp
->segsize_rem
;
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
== TCP_NAGLE_OFF
);
1603 if (r_len
> sizeof(int))
1604 r_len
= sizeof(int);
1605 val
= (scp
->nonagle
== TCP_NAGLE_CORK
);
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
;
1626 return -ENOPROTOOPT
;
1630 if (copy_to_user(optval
, r_data
, r_len
))
1632 if (put_user(r_len
, optlen
))
1640 static int dn_data_ready(struct sock
*sk
, struct sk_buff_head
*q
, int flags
, int target
)
1642 struct sk_buff
*skb
;
1645 if (flags
& MSG_OOB
)
1646 return !skb_queue_empty(q
) ? 1 : 0;
1648 skb_queue_walk(q
, skb
) {
1649 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
1652 if (cb
->nsp_flags
& 0x40) {
1653 /* SOCK_SEQPACKET reads to EOM */
1654 if (sk
->sk_type
== SOCK_SEQPACKET
)
1656 /* so does SOCK_STREAM unless WAITALL is specified */
1657 if (!(flags
& MSG_WAITALL
))
1661 /* minimum data length for read exceeded */
1670 static int dn_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
,
1673 struct sock
*sk
= sock
->sk
;
1674 struct dn_scp
*scp
= DN_SK(sk
);
1675 struct sk_buff_head
*queue
= &sk
->sk_receive_queue
;
1676 size_t target
= size
> 1 ? 1 : 0;
1679 struct sk_buff
*skb
, *n
;
1680 struct dn_skb_cb
*cb
= NULL
;
1681 unsigned char eor
= 0;
1682 long timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1686 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1687 rv
= -EADDRNOTAVAIL
;
1691 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
1696 rv
= dn_check_state(sk
, NULL
, 0, &timeo
, flags
);
1700 if (flags
& ~(MSG_CMSG_COMPAT
|MSG_PEEK
|MSG_OOB
|MSG_WAITALL
|MSG_DONTWAIT
|MSG_NOSIGNAL
)) {
1705 if (flags
& MSG_OOB
)
1706 queue
= &scp
->other_receive_queue
;
1708 if (flags
& MSG_WAITALL
)
1713 * See if there is data ready to read, sleep if there isn't
1716 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
1721 if (!skb_queue_empty(&scp
->other_receive_queue
)) {
1722 if (!(flags
& MSG_OOB
)) {
1723 msg
->msg_flags
|= MSG_OOB
;
1724 if (!scp
->other_report
) {
1725 scp
->other_report
= 1;
1731 if (scp
->state
!= DN_RUN
)
1734 if (signal_pending(current
)) {
1735 rv
= sock_intr_errno(timeo
);
1739 if (dn_data_ready(sk
, queue
, flags
, target
))
1742 if (flags
& MSG_DONTWAIT
) {
1747 add_wait_queue(sk_sleep(sk
), &wait
);
1748 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
1749 sk_wait_event(sk
, &timeo
, dn_data_ready(sk
, queue
, flags
, target
), &wait
);
1750 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
1751 remove_wait_queue(sk_sleep(sk
), &wait
);
1754 skb_queue_walk_safe(queue
, skb
, n
) {
1755 unsigned int chunk
= skb
->len
;
1756 cb
= DN_SKB_CB(skb
);
1758 if ((chunk
+ copied
) > size
)
1759 chunk
= size
- copied
;
1761 if (memcpy_to_msg(msg
, skb
->data
, chunk
)) {
1767 if (!(flags
& MSG_PEEK
))
1768 skb_pull(skb
, chunk
);
1770 eor
= cb
->nsp_flags
& 0x40;
1772 if (skb
->len
== 0) {
1773 skb_unlink(skb
, queue
);
1776 * N.B. Don't refer to skb or cb after this point
1779 if ((scp
->flowloc_sw
== DN_DONTSEND
) && !dn_congested(sk
)) {
1780 scp
->flowloc_sw
= DN_SEND
;
1781 dn_nsp_send_link(sk
, DN_SEND
, 0);
1786 if (sk
->sk_type
== SOCK_SEQPACKET
)
1788 if (!(flags
& MSG_WAITALL
))
1792 if (flags
& MSG_OOB
)
1795 if (copied
>= target
)
1802 if (eor
&& (sk
->sk_type
== SOCK_SEQPACKET
))
1803 msg
->msg_flags
|= MSG_EOR
;
1807 rv
= (flags
& MSG_PEEK
) ? -sk
->sk_err
: sock_error(sk
);
1809 if ((rv
>= 0) && msg
->msg_name
) {
1810 __sockaddr_check_size(sizeof(struct sockaddr_dn
));
1811 memcpy(msg
->msg_name
, &scp
->peer
, sizeof(struct sockaddr_dn
));
1812 msg
->msg_namelen
= sizeof(struct sockaddr_dn
);
1821 static inline int dn_queue_too_long(struct dn_scp
*scp
, struct sk_buff_head
*queue
, int flags
)
1823 unsigned char fctype
= scp
->services_rem
& NSP_FC_MASK
;
1824 if (skb_queue_len(queue
) >= scp
->snd_window
)
1826 if (fctype
!= NSP_FC_NONE
) {
1827 if (flags
& MSG_OOB
) {
1828 if (scp
->flowrem_oth
== 0)
1831 if (scp
->flowrem_dat
== 0)
1839 * The DECnet spec requires that the "routing layer" accepts packets which
1840 * are at least 230 bytes in size. This excludes any headers which the NSP
1841 * layer might add, so we always assume that we'll be using the maximal
1842 * length header on data packets. The variation in length is due to the
1843 * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
1844 * make much practical difference.
1846 unsigned int dn_mss_from_pmtu(struct net_device
*dev
, int mtu
)
1848 unsigned int mss
= 230 - DN_MAX_NSP_DATA_HEADER
;
1850 struct dn_dev
*dn_db
= rcu_dereference_raw(dev
->dn_ptr
);
1851 mtu
-= LL_RESERVED_SPACE(dev
);
1852 if (dn_db
->use_long
)
1856 mtu
-= DN_MAX_NSP_DATA_HEADER
;
1859 * 21 = long header, 16 = guess at MAC header length
1861 mtu
-= (21 + DN_MAX_NSP_DATA_HEADER
+ 16);
1868 static inline unsigned int dn_current_mss(struct sock
*sk
, int flags
)
1870 struct dst_entry
*dst
= __sk_dst_get(sk
);
1871 struct dn_scp
*scp
= DN_SK(sk
);
1872 int mss_now
= min_t(int, scp
->segsize_loc
, scp
->segsize_rem
);
1874 /* Other data messages are limited to 16 bytes per packet */
1875 if (flags
& MSG_OOB
)
1878 /* This works out the maximum size of segment we can send out */
1880 u32 mtu
= dst_mtu(dst
);
1881 mss_now
= min_t(int, dn_mss_from_pmtu(dst
->dev
, mtu
), mss_now
);
1888 * N.B. We get the timeout wrong here, but then we always did get it
1889 * wrong before and this is another step along the road to correcting
1890 * it. It ought to get updated each time we pass through the routine,
1891 * but in practise it probably doesn't matter too much for now.
1893 static inline struct sk_buff
*dn_alloc_send_pskb(struct sock
*sk
,
1894 unsigned long datalen
, int noblock
,
1897 struct sk_buff
*skb
= sock_alloc_send_skb(sk
, datalen
,
1900 skb
->protocol
= htons(ETH_P_DNA_RT
);
1901 skb
->pkt_type
= PACKET_OUTGOING
;
1906 static int dn_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
1908 struct sock
*sk
= sock
->sk
;
1909 struct dn_scp
*scp
= DN_SK(sk
);
1911 struct sk_buff_head
*queue
= &scp
->data_xmit_queue
;
1912 int flags
= msg
->msg_flags
;
1915 int addr_len
= msg
->msg_namelen
;
1916 DECLARE_SOCKADDR(struct sockaddr_dn
*, addr
, msg
->msg_name
);
1917 struct sk_buff
*skb
= NULL
;
1918 struct dn_skb_cb
*cb
;
1920 unsigned char fctype
;
1923 if (flags
& ~(MSG_TRYHARD
|MSG_OOB
|MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|MSG_MORE
|MSG_CMSG_COMPAT
))
1926 if (addr_len
&& (addr_len
!= sizeof(struct sockaddr_dn
)))
1930 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
1932 * The only difference between stream sockets and sequenced packet
1933 * sockets is that the stream sockets always behave as if MSG_EOR
1936 if (sock
->type
== SOCK_STREAM
) {
1937 if (flags
& MSG_EOR
) {
1945 err
= dn_check_state(sk
, addr
, addr_len
, &timeo
, flags
);
1949 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1951 if (!(flags
& MSG_NOSIGNAL
))
1952 send_sig(SIGPIPE
, current
, 0);
1956 if ((flags
& MSG_TRYHARD
) && sk
->sk_dst_cache
)
1957 dst_negative_advice(sk
);
1959 mss
= scp
->segsize_rem
;
1960 fctype
= scp
->services_rem
& NSP_FC_MASK
;
1962 mss
= dn_current_mss(sk
, flags
);
1964 if (flags
& MSG_OOB
) {
1965 queue
= &scp
->other_xmit_queue
;
1972 scp
->persist_fxn
= dn_nsp_xmit_timeout
;
1974 while(sent
< size
) {
1975 err
= sock_error(sk
);
1979 if (signal_pending(current
)) {
1980 err
= sock_intr_errno(timeo
);
1985 * Calculate size that we wish to send.
1993 * Wait for queue size to go down below the window
1996 if (dn_queue_too_long(scp
, queue
, flags
)) {
1997 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
1999 if (flags
& MSG_DONTWAIT
) {
2004 add_wait_queue(sk_sleep(sk
), &wait
);
2005 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
2006 sk_wait_event(sk
, &timeo
,
2007 !dn_queue_too_long(scp
, queue
, flags
), &wait
);
2008 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
2009 remove_wait_queue(sk_sleep(sk
), &wait
);
2014 * Get a suitably sized skb.
2015 * 64 is a bit of a hack really, but its larger than any
2016 * link-layer headers and has served us well as a good
2017 * guess as to their real length.
2019 skb
= dn_alloc_send_pskb(sk
, len
+ 64 + DN_MAX_NSP_DATA_HEADER
,
2020 flags
& MSG_DONTWAIT
, &err
);
2028 cb
= DN_SKB_CB(skb
);
2030 skb_reserve(skb
, 64 + DN_MAX_NSP_DATA_HEADER
);
2032 if (memcpy_from_msg(skb_put(skb
, len
), msg
, len
)) {
2037 if (flags
& MSG_OOB
) {
2038 cb
->nsp_flags
= 0x30;
2039 if (fctype
!= NSP_FC_NONE
)
2042 cb
->nsp_flags
= 0x00;
2043 if (scp
->seg_total
== 0)
2044 cb
->nsp_flags
|= 0x20;
2046 scp
->seg_total
+= len
;
2048 if (((sent
+ len
) == size
) && (flags
& MSG_EOR
)) {
2049 cb
->nsp_flags
|= 0x40;
2051 if (fctype
== NSP_FC_SCMC
)
2054 if (fctype
== NSP_FC_SRC
)
2059 dn_nsp_queue_xmit(sk
, skb
, sk
->sk_allocation
, flags
& MSG_OOB
);
2062 scp
->persist
= dn_nsp_persist(sk
);
2071 return sent
? sent
: err
;
2074 err
= sk_stream_error(sk
, flags
, err
);
2079 static int dn_device_event(struct notifier_block
*this, unsigned long event
,
2082 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2084 if (!net_eq(dev_net(dev
), &init_net
))
2101 static struct notifier_block dn_dev_notifier
= {
2102 .notifier_call
= dn_device_event
,
2105 static struct packet_type dn_dix_packet_type __read_mostly
= {
2106 .type
= cpu_to_be16(ETH_P_DNA_RT
),
2107 .func
= dn_route_rcv
,
2110 #ifdef CONFIG_PROC_FS
2111 struct dn_iter_state
{
2115 static struct sock
*dn_socket_get_first(struct seq_file
*seq
)
2117 struct dn_iter_state
*state
= seq
->private;
2118 struct sock
*n
= NULL
;
2120 for(state
->bucket
= 0;
2121 state
->bucket
< DN_SK_HASH_SIZE
;
2123 n
= sk_head(&dn_sk_hash
[state
->bucket
]);
2131 static struct sock
*dn_socket_get_next(struct seq_file
*seq
,
2134 struct dn_iter_state
*state
= seq
->private;
2140 if (++state
->bucket
>= DN_SK_HASH_SIZE
)
2142 n
= sk_head(&dn_sk_hash
[state
->bucket
]);
2148 static struct sock
*socket_get_idx(struct seq_file
*seq
, loff_t
*pos
)
2150 struct sock
*sk
= dn_socket_get_first(seq
);
2153 while(*pos
&& (sk
= dn_socket_get_next(seq
, sk
)))
2156 return *pos
? NULL
: sk
;
2159 static void *dn_socket_get_idx(struct seq_file
*seq
, loff_t pos
)
2162 read_lock_bh(&dn_hash_lock
);
2163 rc
= socket_get_idx(seq
, &pos
);
2165 read_unlock_bh(&dn_hash_lock
);
2170 static void *dn_socket_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2172 return *pos
? dn_socket_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2175 static void *dn_socket_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2179 if (v
== SEQ_START_TOKEN
) {
2180 rc
= dn_socket_get_idx(seq
, 0);
2184 rc
= dn_socket_get_next(seq
, v
);
2187 read_unlock_bh(&dn_hash_lock
);
2193 static void dn_socket_seq_stop(struct seq_file
*seq
, void *v
)
2195 if (v
&& v
!= SEQ_START_TOKEN
)
2196 read_unlock_bh(&dn_hash_lock
);
2199 #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
2201 static void dn_printable_object(struct sockaddr_dn
*dn
, unsigned char *buf
)
2205 switch (le16_to_cpu(dn
->sdn_objnamel
)) {
2207 sprintf(buf
, "%d", dn
->sdn_objnum
);
2210 for (i
= 0; i
< le16_to_cpu(dn
->sdn_objnamel
); i
++) {
2211 buf
[i
] = dn
->sdn_objname
[i
];
2212 if (IS_NOT_PRINTABLE(buf
[i
]))
2219 static char *dn_state2asc(unsigned char state
)
2259 static inline void dn_socket_format_entry(struct seq_file
*seq
, struct sock
*sk
)
2261 struct dn_scp
*scp
= DN_SK(sk
);
2262 char buf1
[DN_ASCBUF_LEN
];
2263 char buf2
[DN_ASCBUF_LEN
];
2264 char local_object
[DN_MAXOBJL
+3];
2265 char remote_object
[DN_MAXOBJL
+3];
2267 dn_printable_object(&scp
->addr
, local_object
);
2268 dn_printable_object(&scp
->peer
, remote_object
);
2271 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
2272 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
2273 dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp
->addr
)), buf1
),
2281 dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp
->peer
)), buf2
),
2289 dn_state2asc(scp
->state
),
2290 ((scp
->accept_mode
== ACC_IMMED
) ? "IMMED" : "DEFER"));
2293 static int dn_socket_seq_show(struct seq_file
*seq
, void *v
)
2295 if (v
== SEQ_START_TOKEN
) {
2296 seq_puts(seq
, "Local Remote\n");
2298 dn_socket_format_entry(seq
, v
);
2303 static const struct seq_operations dn_socket_seq_ops
= {
2304 .start
= dn_socket_seq_start
,
2305 .next
= dn_socket_seq_next
,
2306 .stop
= dn_socket_seq_stop
,
2307 .show
= dn_socket_seq_show
,
2311 static const struct net_proto_family dn_family_ops
= {
2312 .family
= AF_DECnet
,
2313 .create
= dn_create
,
2314 .owner
= THIS_MODULE
,
2317 static const struct proto_ops dn_proto_ops
= {
2318 .family
= AF_DECnet
,
2319 .owner
= THIS_MODULE
,
2320 .release
= dn_release
,
2322 .connect
= dn_connect
,
2323 .socketpair
= sock_no_socketpair
,
2324 .accept
= dn_accept
,
2325 .getname
= dn_getname
,
2328 .listen
= dn_listen
,
2329 .shutdown
= dn_shutdown
,
2330 .setsockopt
= dn_setsockopt
,
2331 .getsockopt
= dn_getsockopt
,
2332 .sendmsg
= dn_sendmsg
,
2333 .recvmsg
= dn_recvmsg
,
2334 .mmap
= sock_no_mmap
,
2335 .sendpage
= sock_no_sendpage
,
2338 MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
2339 MODULE_AUTHOR("Linux DECnet Project Team");
2340 MODULE_LICENSE("GPL");
2341 MODULE_ALIAS_NETPROTO(PF_DECnet
);
2343 static const char banner
[] __initconst
= KERN_INFO
2344 "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
2346 static int __init
decnet_init(void)
2352 rc
= proto_register(&dn_proto
, 1);
2361 sock_register(&dn_family_ops
);
2362 dev_add_pack(&dn_dix_packet_type
);
2363 register_netdevice_notifier(&dn_dev_notifier
);
2365 proc_create_seq_private("decnet", 0444, init_net
.proc_net
,
2366 &dn_socket_seq_ops
, sizeof(struct dn_iter_state
),
2368 dn_register_sysctl();
2373 module_init(decnet_init
);
2376 * Prevent DECnet module unloading until its fixed properly.
2377 * Requires an audit of the code to check for memory leaks and
2378 * initialisation problems etc.
2381 static void __exit
decnet_exit(void)
2383 sock_unregister(AF_DECnet
);
2384 rtnl_unregister_all(PF_DECnet
);
2385 dev_remove_pack(&dn_dix_packet_type
);
2387 dn_unregister_sysctl();
2389 unregister_netdevice_notifier(&dn_dev_notifier
);
2396 remove_proc_entry("decnet", init_net
.proc_net
);
2398 proto_unregister(&dn_proto
);
2400 rcu_barrier(); /* Wait for completion of call_rcu()'s */
2402 module_exit(decnet_exit
);