2 * DECnet An implementation of the DECnet protocol suite for the LINUX
3 * operating system. DECnet is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * DECnet Network Services Protocol (Input)
8 * Author: Eduardo Marcelo Serrat <emserrat@geocities.com>
12 * Steve Whitehouse: Split into dn_nsp_in.c and dn_nsp_out.c from
14 * Steve Whitehouse: Updated to work with my new routing architecture.
15 * Steve Whitehouse: Add changes from Eduardo Serrat's patches.
16 * Steve Whitehouse: Put all ack handling code in a common routine.
17 * Steve Whitehouse: Put other common bits into dn_nsp_rx()
18 * Steve Whitehouse: More checks on skb->len to catch bogus packets
19 * Fixed various race conditions and possible nasties.
20 * Steve Whitehouse: Now handles returned conninit frames.
21 * David S. Miller: New socket locking
22 * Steve Whitehouse: Fixed lockup when socket filtering was enabled.
23 * Paul Koning: Fix to push CC sockets into RUN when acks are
26 * Patrick Caulfield: Checking conninits for correctness & sending of error
28 * Steve Whitehouse: Added backlog congestion level return codes.
30 * Steve Whitehouse: Added flow control support (outbound)
31 * Steve Whitehouse: Prepare for nonlinear skbs
34 /******************************************************************************
35 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
37 This program is free software; you can redistribute it and/or modify
38 it under the terms of the GNU General Public License as published by
39 the Free Software Foundation; either version 2 of the License, or
42 This program is distributed in the hope that it will be useful,
43 but WITHOUT ANY WARRANTY; without even the implied warranty of
44 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 GNU General Public License for more details.
46 *******************************************************************************/
48 #include <linux/config.h>
49 #include <linux/errno.h>
50 #include <linux/types.h>
51 #include <linux/socket.h>
53 #include <linux/kernel.h>
54 #include <linux/sched.h>
55 #include <linux/timer.h>
56 #include <linux/string.h>
57 #include <linux/sockios.h>
58 #include <linux/net.h>
59 #include <linux/netdevice.h>
60 #include <linux/inet.h>
61 #include <linux/route.h>
63 #include <net/tcp_states.h>
64 #include <asm/system.h>
65 #include <linux/fcntl.h>
67 #include <linux/termios.h>
68 #include <linux/interrupt.h>
69 #include <linux/proc_fs.h>
70 #include <linux/stat.h>
71 #include <linux/init.h>
72 #include <linux/poll.h>
73 #include <linux/netfilter_decnet.h>
74 #include <net/neighbour.h>
77 #include <net/dn_nsp.h>
78 #include <net/dn_dev.h>
79 #include <net/dn_route.h>
81 extern int decnet_log_martians
;
83 static void dn_log_martian(struct sk_buff
*skb
, const char *msg
)
85 if (decnet_log_martians
&& net_ratelimit()) {
86 char *devname
= skb
->dev
? skb
->dev
->name
: "???";
87 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
88 printk(KERN_INFO
"DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n", msg
, devname
, cb
->src
, cb
->dst
, cb
->src_port
, cb
->dst_port
);
93 * For this function we've flipped the cross-subchannel bit
94 * if the message is an otherdata or linkservice message. Thus
95 * we can use it to work out what to update.
97 static void dn_ack(struct sock
*sk
, struct sk_buff
*skb
, unsigned short ack
)
99 struct dn_scp
*scp
= DN_SK(sk
);
100 unsigned short type
= ((ack
>> 12) & 0x0003);
104 case 0: /* ACK - Data */
105 if (dn_after(ack
, scp
->ackrcv_dat
)) {
106 scp
->ackrcv_dat
= ack
& 0x0fff;
107 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
, &scp
->data_xmit_queue
, ack
);
110 case 1: /* NAK - Data */
112 case 2: /* ACK - OtherData */
113 if (dn_after(ack
, scp
->ackrcv_oth
)) {
114 scp
->ackrcv_oth
= ack
& 0x0fff;
115 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
, &scp
->other_xmit_queue
, ack
);
118 case 3: /* NAK - OtherData */
122 if (wakeup
&& !sock_flag(sk
, SOCK_DEAD
))
123 sk
->sk_state_change(sk
);
127 * This function is a universal ack processor.
129 static int dn_process_ack(struct sock
*sk
, struct sk_buff
*skb
, int oth
)
131 unsigned short *ptr
= (unsigned short *)skb
->data
;
138 if ((ack
= dn_ntohs(*ptr
)) & 0x8000) {
142 if ((ack
& 0x4000) == 0) {
145 dn_ack(sk
, skb
, ack
);
152 if ((ack
= dn_ntohs(*ptr
)) & 0x8000) {
155 if ((ack
& 0x4000) == 0) {
158 dn_ack(sk
, skb
, ack
);
167 * dn_check_idf - Check an image data field format is correct.
168 * @pptr: Pointer to pointer to image data
169 * @len: Pointer to length of image data
170 * @max: The maximum allowed length of the data in the image data field
171 * @follow_on: Check that this many bytes exist beyond the end of the image data
173 * Returns: 0 if ok, -1 on error
175 static inline int dn_check_idf(unsigned char **pptr
, int *len
, unsigned char max
, unsigned char follow_on
)
177 unsigned char *ptr
= *pptr
;
178 unsigned char flen
= *ptr
++;
183 if ((flen
+ follow_on
) > *len
)
192 * Table of reason codes to pass back to node which sent us a badly
193 * formed message, plus text messages for the log. A zero entry in
194 * the reason field means "don't reply" otherwise a disc init is sent with
195 * the specified reason code.
198 unsigned short reason
;
201 { 0, "CI: Truncated message" },
202 { NSP_REASON_ID
, "CI: Destination username error" },
203 { NSP_REASON_ID
, "CI: Destination username type" },
204 { NSP_REASON_US
, "CI: Source username error" },
205 { 0, "CI: Truncated at menuver" },
206 { 0, "CI: Truncated before access or user data" },
207 { NSP_REASON_IO
, "CI: Access data format error" },
208 { NSP_REASON_IO
, "CI: User data format error" }
212 * This function uses a slightly different lookup method
213 * to find its sockets, since it searches on object name/number
214 * rather than port numbers. Various tests are done to ensure that
215 * the incoming data is in the correct format before it is queued to
218 static struct sock
*dn_find_listener(struct sk_buff
*skb
, unsigned short *reason
)
220 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
221 struct nsp_conn_init_msg
*msg
= (struct nsp_conn_init_msg
*)skb
->data
;
222 struct sockaddr_dn dstaddr
;
223 struct sockaddr_dn srcaddr
;
224 unsigned char type
= 0;
230 unsigned char menuver
;
232 memset(&dstaddr
, 0, sizeof(struct sockaddr_dn
));
233 memset(&srcaddr
, 0, sizeof(struct sockaddr_dn
));
236 * 1. Decode & remove message header
238 cb
->src_port
= msg
->srcaddr
;
239 cb
->dst_port
= msg
->dstaddr
;
240 cb
->services
= msg
->services
;
241 cb
->info
= msg
->info
;
242 cb
->segsize
= dn_ntohs(msg
->segsize
);
244 if (!pskb_may_pull(skb
, sizeof(*msg
)))
247 skb_pull(skb
, sizeof(*msg
));
253 * 2. Check destination end username format
255 dstlen
= dn_username2sockaddr(ptr
, len
, &dstaddr
, &type
);
268 * 3. Check source end username format
270 srclen
= dn_username2sockaddr(ptr
, len
, &srcaddr
, &type
);
286 * 4. Check that optional data actually exists if menuver says it does
289 if ((menuver
& (DN_MENUVER_ACC
| DN_MENUVER_USR
)) && (len
< 1))
293 * 5. Check optional access data format
296 if (menuver
& DN_MENUVER_ACC
) {
297 if (dn_check_idf(&ptr
, &len
, 39, 1))
299 if (dn_check_idf(&ptr
, &len
, 39, 1))
301 if (dn_check_idf(&ptr
, &len
, 39, (menuver
& DN_MENUVER_USR
) ? 1 : 0))
306 * 6. Check optional user data format
309 if (menuver
& DN_MENUVER_USR
) {
310 if (dn_check_idf(&ptr
, &len
, 16, 0))
315 * 7. Look up socket based on destination end username
317 return dn_sklist_find_listener(&dstaddr
);
319 dn_log_martian(skb
, ci_err_table
[err
].text
);
320 *reason
= ci_err_table
[err
].reason
;
325 static void dn_nsp_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
327 if (sk_acceptq_is_full(sk
)) {
332 sk
->sk_ack_backlog
++;
333 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
334 sk
->sk_state_change(sk
);
337 static void dn_nsp_conn_conf(struct sock
*sk
, struct sk_buff
*skb
)
339 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
340 struct dn_scp
*scp
= DN_SK(sk
);
347 cb
->services
= *ptr
++;
349 cb
->segsize
= dn_ntohs(*(__u16
*)ptr
);
351 if ((scp
->state
== DN_CI
) || (scp
->state
== DN_CD
)) {
353 scp
->addrrem
= cb
->src_port
;
354 sk
->sk_state
= TCP_ESTABLISHED
;
356 scp
->services_rem
= cb
->services
;
357 scp
->info_rem
= cb
->info
;
358 scp
->segsize_rem
= cb
->segsize
;
360 if ((scp
->services_rem
& NSP_FC_MASK
) == NSP_FC_NONE
)
361 scp
->max_window
= decnet_no_fc_max_cwnd
;
364 unsigned char dlen
= *skb
->data
;
365 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
366 scp
->conndata_in
.opt_optl
= dlen
;
367 memcpy(scp
->conndata_in
.opt_data
, skb
->data
+ 1, dlen
);
370 dn_nsp_send_link(sk
, DN_NOCHANGE
, 0);
371 if (!sock_flag(sk
, SOCK_DEAD
))
372 sk
->sk_state_change(sk
);
379 static void dn_nsp_conn_ack(struct sock
*sk
, struct sk_buff
*skb
)
381 struct dn_scp
*scp
= DN_SK(sk
);
383 if (scp
->state
== DN_CI
) {
391 static void dn_nsp_disc_init(struct sock
*sk
, struct sk_buff
*skb
)
393 struct dn_scp
*scp
= DN_SK(sk
);
394 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
395 unsigned short reason
;
400 reason
= dn_ntohs(*(__u16
*)skb
->data
);
403 scp
->discdata_in
.opt_status
= reason
;
404 scp
->discdata_in
.opt_optl
= 0;
405 memset(scp
->discdata_in
.opt_data
, 0, 16);
408 unsigned char dlen
= *skb
->data
;
409 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
410 scp
->discdata_in
.opt_optl
= dlen
;
411 memcpy(scp
->discdata_in
.opt_data
, skb
->data
+ 1, dlen
);
415 scp
->addrrem
= cb
->src_port
;
416 sk
->sk_state
= TCP_CLOSE
;
422 sk
->sk_err
= ECONNREFUSED
;
425 sk
->sk_shutdown
|= SHUTDOWN_MASK
;
433 if (!sock_flag(sk
, SOCK_DEAD
)) {
434 if (sk
->sk_socket
->state
!= SS_UNCONNECTED
)
435 sk
->sk_socket
->state
= SS_DISCONNECTING
;
436 sk
->sk_state_change(sk
);
440 * It appears that its possible for remote machines to send disc
441 * init messages with no port identifier if we are in the CI and
442 * possibly also the CD state. Obviously we shouldn't reply with
443 * a message if we don't know what the end point is.
446 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
, GFP_ATOMIC
);
448 scp
->persist_fxn
= dn_destroy_timer
;
449 scp
->persist
= dn_nsp_persist(sk
);
456 * disc_conf messages are also called no_resources or no_link
457 * messages depending upon the "reason" field.
459 static void dn_nsp_disc_conf(struct sock
*sk
, struct sk_buff
*skb
)
461 struct dn_scp
*scp
= DN_SK(sk
);
462 unsigned short reason
;
467 reason
= dn_ntohs(*(__u16
*)skb
->data
);
469 sk
->sk_state
= TCP_CLOSE
;
476 if (reason
== NSP_REASON_DC
)
478 if (reason
== NSP_REASON_NL
)
485 sk
->sk_shutdown
|= SHUTDOWN_MASK
;
490 if (!sock_flag(sk
, SOCK_DEAD
)) {
491 if (sk
->sk_socket
->state
!= SS_UNCONNECTED
)
492 sk
->sk_socket
->state
= SS_DISCONNECTING
;
493 sk
->sk_state_change(sk
);
496 scp
->persist_fxn
= dn_destroy_timer
;
497 scp
->persist
= dn_nsp_persist(sk
);
503 static void dn_nsp_linkservice(struct sock
*sk
, struct sk_buff
*skb
)
505 struct dn_scp
*scp
= DN_SK(sk
);
506 unsigned short segnum
;
507 unsigned char lsflags
;
510 char *ptr
= skb
->data
;
511 unsigned char fctype
= scp
->services_rem
& NSP_FC_MASK
;
516 segnum
= dn_ntohs(*(__u16
*)ptr
);
518 lsflags
= *(unsigned char *)ptr
++;
522 * Here we ignore erronous packets which should really
523 * should cause a connection abort. It is not critical
529 if (seq_next(scp
->numoth_rcv
, segnum
)) {
530 seq_add(&scp
->numoth_rcv
, 1);
531 switch(lsflags
& 0x04) { /* FCVAL INT */
532 case 0x00: /* Normal Request */
533 switch(lsflags
& 0x03) { /* FCVAL MOD */
534 case 0x00: /* Request count */
536 unsigned char p_fcval
= -fcval
;
537 if ((scp
->flowrem_dat
> p_fcval
) &&
538 (fctype
== NSP_FC_SCMC
)) {
539 scp
->flowrem_dat
-= p_fcval
;
541 } else if (fcval
> 0) {
542 scp
->flowrem_dat
+= fcval
;
546 case 0x01: /* Stop outgoing data */
547 scp
->flowrem_sw
= DN_DONTSEND
;
549 case 0x02: /* Ok to start again */
550 scp
->flowrem_sw
= DN_SEND
;
555 case 0x04: /* Interrupt Request */
557 scp
->flowrem_oth
+= fcval
;
562 if (wake_up
&& !sock_flag(sk
, SOCK_DEAD
))
563 sk
->sk_state_change(sk
);
566 dn_nsp_send_oth_ack(sk
);
573 * Copy of sock_queue_rcv_skb (from sock.h) without
574 * bh_lock_sock() (its already held when this is called) which
575 * also allows data and other data to be queued to a socket.
577 static __inline__
int dn_queue_skb(struct sock
*sk
, struct sk_buff
*skb
, int sig
, struct sk_buff_head
*queue
)
581 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
582 number of warnings when compiling with -W --ANK
584 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
585 (unsigned)sk
->sk_rcvbuf
) {
590 err
= sk_filter(sk
, skb
, 0);
594 skb_set_owner_r(skb
, sk
);
595 skb_queue_tail(queue
, skb
);
597 /* This code only runs from BH or BH protected context.
598 * Therefore the plain read_lock is ok here. -DaveM
600 read_lock(&sk
->sk_callback_lock
);
601 if (!sock_flag(sk
, SOCK_DEAD
)) {
602 struct socket
*sock
= sk
->sk_socket
;
603 wake_up_interruptible(sk
->sk_sleep
);
604 if (sock
&& sock
->fasync_list
&&
605 !test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
606 __kill_fasync(sock
->fasync_list
, sig
,
607 (sig
== SIGURG
) ? POLL_PRI
: POLL_IN
);
609 read_unlock(&sk
->sk_callback_lock
);
614 static void dn_nsp_otherdata(struct sock
*sk
, struct sk_buff
*skb
)
616 struct dn_scp
*scp
= DN_SK(sk
);
617 unsigned short segnum
;
618 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
624 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
627 if (seq_next(scp
->numoth_rcv
, segnum
)) {
629 if (dn_queue_skb(sk
, skb
, SIGURG
, &scp
->other_receive_queue
) == 0) {
630 seq_add(&scp
->numoth_rcv
, 1);
631 scp
->other_report
= 0;
636 dn_nsp_send_oth_ack(sk
);
642 static void dn_nsp_data(struct sock
*sk
, struct sk_buff
*skb
)
645 unsigned short segnum
;
646 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
647 struct dn_scp
*scp
= DN_SK(sk
);
652 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
655 if (seq_next(scp
->numdat_rcv
, segnum
)) {
656 if (dn_queue_skb(sk
, skb
, SIGIO
, &sk
->sk_receive_queue
) == 0) {
657 seq_add(&scp
->numdat_rcv
, 1);
661 if ((scp
->flowloc_sw
== DN_SEND
) && dn_congested(sk
)) {
662 scp
->flowloc_sw
= DN_DONTSEND
;
663 dn_nsp_send_link(sk
, DN_DONTSEND
, 0);
667 dn_nsp_send_data_ack(sk
);
674 * If one of our conninit messages is returned, this function
675 * deals with it. It puts the socket into the NO_COMMUNICATION
678 static void dn_returned_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
680 struct dn_scp
*scp
= DN_SK(sk
);
682 if (scp
->state
== DN_CI
) {
684 sk
->sk_state
= TCP_CLOSE
;
685 if (!sock_flag(sk
, SOCK_DEAD
))
686 sk
->sk_state_change(sk
);
692 static int dn_nsp_no_socket(struct sk_buff
*skb
, unsigned short reason
)
694 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
695 int ret
= NET_RX_DROP
;
697 /* Must not reply to returned packets */
698 if (cb
->rt_flags
& DN_RT_F_RTS
)
701 if ((reason
!= NSP_REASON_OK
) && ((cb
->nsp_flags
& 0x0c) == 0x08)) {
702 switch(cb
->nsp_flags
& 0x70) {
704 case 0x60: /* (Retransmitted) Connect Init */
705 dn_nsp_return_disc(skb
, NSP_DISCINIT
, reason
);
706 ret
= NET_RX_SUCCESS
;
708 case 0x20: /* Connect Confirm */
709 dn_nsp_return_disc(skb
, NSP_DISCCONF
, reason
);
710 ret
= NET_RX_SUCCESS
;
720 static int dn_nsp_rx_packet(struct sk_buff
*skb
)
722 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
723 struct sock
*sk
= NULL
;
724 unsigned char *ptr
= (unsigned char *)skb
->data
;
725 unsigned short reason
= NSP_REASON_NL
;
727 if (!pskb_may_pull(skb
, 2))
730 skb
->h
.raw
= skb
->data
;
731 cb
->nsp_flags
= *ptr
++;
733 if (decnet_debug_level
& 2)
734 printk(KERN_DEBUG
"dn_nsp_rx: Message type 0x%02x\n", (int)cb
->nsp_flags
);
736 if (cb
->nsp_flags
& 0x83)
740 * Filter out conninits and useless packet types
742 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
743 switch(cb
->nsp_flags
& 0x70) {
745 case 0x70: /* Reserved */
746 case 0x50: /* Reserved, Phase II node init */
750 if (unlikely(cb
->rt_flags
& DN_RT_F_RTS
))
752 sk
= dn_find_listener(skb
, &reason
);
757 if (!pskb_may_pull(skb
, 3))
761 * Grab the destination address.
763 cb
->dst_port
= *(unsigned short *)ptr
;
768 * If not a connack, grab the source address too.
770 if (pskb_may_pull(skb
, 5)) {
771 cb
->src_port
= *(unsigned short *)ptr
;
777 * Returned packets...
778 * Swap src & dst and look up in the normal way.
780 if (unlikely(cb
->rt_flags
& DN_RT_F_RTS
)) {
781 unsigned short tmp
= cb
->dst_port
;
782 cb
->dst_port
= cb
->src_port
;
790 * Find the socket to which this skb is destined.
792 sk
= dn_find_by_skb(skb
);
795 struct dn_scp
*scp
= DN_SK(sk
);
798 scp
->nsp_rxtshift
= 0;
801 * We linearize everything except data segments here.
803 if (cb
->nsp_flags
& ~0x60) {
804 if (unlikely(skb_is_nonlinear(skb
)) &&
805 skb_linearize(skb
, GFP_ATOMIC
) != 0)
809 return sk_receive_skb(sk
, skb
);
812 return dn_nsp_no_socket(skb
, reason
);
819 int dn_nsp_rx(struct sk_buff
*skb
)
821 return NF_HOOK(PF_DECnet
, NF_DN_LOCAL_IN
, skb
, skb
->dev
, NULL
, dn_nsp_rx_packet
);
825 * This is the main receive routine for sockets. It is called
826 * from the above when the socket is not busy, and also from
827 * sock_release() when there is a backlog queued up.
829 int dn_nsp_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
831 struct dn_scp
*scp
= DN_SK(sk
);
832 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
834 if (cb
->rt_flags
& DN_RT_F_RTS
) {
835 if (cb
->nsp_flags
== 0x18 || cb
->nsp_flags
== 0x68)
836 dn_returned_conn_init(sk
, skb
);
839 return NET_RX_SUCCESS
;
845 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
846 switch(cb
->nsp_flags
& 0x70) {
849 dn_nsp_conn_init(sk
, skb
);
852 dn_nsp_conn_conf(sk
, skb
);
855 dn_nsp_disc_init(sk
, skb
);
858 dn_nsp_disc_conf(sk
, skb
);
862 } else if (cb
->nsp_flags
== 0x24) {
864 * Special for connacks, 'cos they don't have
865 * ack data or ack otherdata info.
867 dn_nsp_conn_ack(sk
, skb
);
871 /* both data and ack frames can kick a CC socket into RUN */
872 if ((scp
->state
== DN_CC
) && !sock_flag(sk
, SOCK_DEAD
)) {
874 sk
->sk_state
= TCP_ESTABLISHED
;
875 sk
->sk_state_change(sk
);
878 if ((cb
->nsp_flags
& 0x1c) == 0)
880 if (cb
->nsp_flags
== 0x04)
884 * Read out ack data here, this applies equally
885 * to data, other data, link serivce and both
886 * ack data and ack otherdata.
888 dn_process_ack(sk
, skb
, other
);
891 * If we've some sort of data here then call a
892 * suitable routine for dealing with it, otherwise
893 * the packet is an ack and can be discarded.
895 if ((cb
->nsp_flags
& 0x0c) == 0) {
897 if (scp
->state
!= DN_RUN
)
900 switch(cb
->nsp_flags
) {
902 dn_nsp_linkservice(sk
, skb
);
905 dn_nsp_otherdata(sk
, skb
);
908 dn_nsp_data(sk
, skb
);
911 } else { /* Ack, chuck it out here */
917 return NET_RX_SUCCESS
;