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
30 /******************************************************************************
31 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
33 This program is free software; you can redistribute it and/or modify
34 it under the terms of the GNU General Public License as published by
35 the Free Software Foundation; either version 2 of the License, or
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
42 *******************************************************************************/
44 #include <linux/config.h>
45 #include <linux/errno.h>
46 #include <linux/types.h>
47 #include <linux/socket.h>
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/timer.h>
52 #include <linux/string.h>
53 #include <linux/sockios.h>
54 #include <linux/net.h>
55 #include <linux/netdevice.h>
56 #include <linux/inet.h>
57 #include <linux/route.h>
59 #include <asm/segment.h>
60 #include <asm/system.h>
61 #include <linux/fcntl.h>
63 #include <linux/termios.h>
64 #include <linux/interrupt.h>
65 #include <linux/proc_fs.h>
66 #include <linux/stat.h>
67 #include <linux/init.h>
68 #include <linux/poll.h>
69 #include <linux/netfilter_decnet.h>
70 #include <net/neighbour.h>
72 #include <net/dn_nsp.h>
73 #include <net/dn_dev.h>
74 #include <net/dn_route.h>
76 extern int decnet_log_martians
;
78 static void dn_log_martian(struct sk_buff
*skb
, const char *msg
)
80 if (decnet_log_martians
&& net_ratelimit()) {
81 char *devname
= skb
->dev
? skb
->dev
->name
: "???";
82 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
83 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
);
88 * For this function we've flipped the cross-subchannel bit
89 * if the message is an otherdata or linkservice message. Thus
90 * we can use it to work out what to update.
92 static void dn_ack(struct sock
*sk
, struct sk_buff
*skb
, unsigned short ack
)
94 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
95 unsigned short type
= ((ack
>> 12) & 0x0003);
99 case 0: /* ACK - Data */
100 if (after(ack
, scp
->ackrcv_dat
)) {
101 scp
->ackrcv_dat
= ack
& 0x0fff;
102 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
, &scp
->data_xmit_queue
, ack
);
105 case 1: /* NAK - Data */
107 case 2: /* ACK - OtherData */
108 if (after(ack
, scp
->ackrcv_oth
)) {
109 scp
->ackrcv_oth
= ack
& 0x0fff;
110 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
, &scp
->other_xmit_queue
, ack
);
113 case 3: /* NAK - OtherData */
117 if (wakeup
&& !sk
->dead
)
118 sk
->state_change(sk
);
122 * This function is a universal ack processor.
124 static int dn_process_ack(struct sock
*sk
, struct sk_buff
*skb
, int oth
)
126 unsigned short *ptr
= (unsigned short *)skb
->data
;
133 if ((ack
= dn_ntohs(*ptr
)) & 0x8000) {
137 if ((ack
& 0x4000) == 0) {
140 dn_ack(sk
, skb
, ack
);
147 if ((ack
= dn_ntohs(*ptr
)) & 0x8000) {
150 if ((ack
& 0x4000) == 0) {
153 dn_ack(sk
, skb
, ack
);
162 * dn_check_idf - Check an image data field format is correct.
163 * @pptr: Pointer to pointer to image data
164 * @len: Pointer to length of image data
165 * @max: The maximum allowed length of the data in the image data field
166 * @follow_on: Check that this many bytes exist beyond the end of the image data
168 * Returns: 0 if ok, -1 on error
170 static inline int dn_check_idf(unsigned char **pptr
, int *len
, unsigned char max
, unsigned char follow_on
)
172 unsigned char *ptr
= *pptr
;
173 unsigned char flen
= *ptr
++;
178 if ((flen
+ follow_on
) > *len
)
187 * Table of reason codes to pass back to node which sent us a badly
188 * formed message, plus text messages for the log. A zero entry in
189 * the reason field means "don't reply" otherwise a disc init is sent with
190 * the specified reason code.
193 unsigned short reason
;
196 { 0, "CI: Truncated message" },
197 { NSP_REASON_ID
, "CI: Destination username error" },
198 { NSP_REASON_ID
, "CI: Destination username type" },
199 { NSP_REASON_US
, "CI: Source username error" },
200 { 0, "CI: Truncated at menuver" },
201 { 0, "CI: Truncated before access or user data" },
202 { NSP_REASON_IO
, "CI: Access data format error" },
203 { NSP_REASON_IO
, "CI: User data format error" }
207 * This function uses a slightly different lookup method
208 * to find its sockets, since it searches on object name/number
209 * rather than port numbers. Various tests are done to ensure that
210 * the incoming data is in the correct format before it is queued to
213 static struct sock
*dn_find_listener(struct sk_buff
*skb
, unsigned short *reason
)
215 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
216 struct nsp_conn_init_msg
*msg
= (struct nsp_conn_init_msg
*)skb
->data
;
217 struct sockaddr_dn dstaddr
;
218 struct sockaddr_dn srcaddr
;
219 unsigned char type
= 0;
225 unsigned char menuver
;
227 memset(&dstaddr
, 0, sizeof(struct sockaddr_dn
));
228 memset(&srcaddr
, 0, sizeof(struct sockaddr_dn
));
231 * 1. Decode & remove message header
233 cb
->src_port
= msg
->srcaddr
;
234 cb
->dst_port
= msg
->dstaddr
;
235 cb
->services
= msg
->services
;
236 cb
->info
= msg
->info
;
237 cb
->segsize
= dn_ntohs(msg
->segsize
);
239 if (skb
->len
< sizeof(*msg
))
242 skb_pull(skb
, sizeof(*msg
));
248 * 2. Check destination end username format
250 dstlen
= dn_username2sockaddr(ptr
, len
, &dstaddr
, &type
);
263 * 3. Check source end username format
265 srclen
= dn_username2sockaddr(ptr
, len
, &srcaddr
, &type
);
281 * 4. Check that optional data actually exists if menuver says it does
284 if ((menuver
& (DN_MENUVER_ACC
| DN_MENUVER_USR
)) && (len
< 1))
288 * 5. Check optional access data format
291 if (menuver
& DN_MENUVER_ACC
) {
292 if (dn_check_idf(&ptr
, &len
, 39, 1))
294 if (dn_check_idf(&ptr
, &len
, 39, 1))
296 if (dn_check_idf(&ptr
, &len
, 39, (menuver
& DN_MENUVER_USR
) ? 1 : 0))
301 * 6. Check optional user data format
304 if (menuver
& DN_MENUVER_USR
) {
305 if (dn_check_idf(&ptr
, &len
, 16, 0))
310 * 7. Look up socket based on destination end username
312 return dn_sklist_find_listener(&dstaddr
);
314 dn_log_martian(skb
, ci_err_table
[err
].text
);
315 *reason
= ci_err_table
[err
].reason
;
320 static void dn_nsp_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
322 if (sk
->ack_backlog
>= sk
->max_ack_backlog
) {
328 skb_queue_tail(&sk
->receive_queue
, skb
);
329 sk
->state_change(sk
);
332 static void dn_nsp_conn_conf(struct sock
*sk
, struct sk_buff
*skb
)
334 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
335 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
340 cb
->services
= *skb
->data
;
341 cb
->info
= *(skb
->data
+1);
343 cb
->segsize
= dn_ntohs(*(__u16
*)skb
->data
);
347 * FIXME: Check out services and info fields to check that
348 * we can talk to this kind of node.
351 if ((scp
->state
== DN_CI
) || (scp
->state
== DN_CD
)) {
353 scp
->addrrem
= cb
->src_port
;
354 sk
->state
= TCP_ESTABLISHED
;
357 if (scp
->mss
> cb
->segsize
)
358 scp
->mss
= cb
->segsize
;
363 unsigned char dlen
= *skb
->data
;
364 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
365 scp
->conndata_in
.opt_optl
= dlen
;
366 memcpy(scp
->conndata_in
.opt_data
, skb
->data
+ 1, dlen
);
369 dn_nsp_send_lnk(sk
, DN_NOCHANGE
);
371 sk
->state_change(sk
);
378 static void dn_nsp_conn_ack(struct sock
*sk
, struct sk_buff
*skb
)
380 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
382 if (scp
->state
== DN_CI
) {
390 static void dn_nsp_disc_init(struct sock
*sk
, struct sk_buff
*skb
)
392 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
393 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
394 unsigned short reason
;
399 reason
= dn_ntohs(*(__u16
*)skb
->data
);
402 scp
->discdata_in
.opt_status
= reason
;
403 scp
->discdata_in
.opt_optl
= 0;
404 memset(scp
->discdata_in
.opt_data
, 0, 16);
407 unsigned char dlen
= *skb
->data
;
408 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
409 scp
->discdata_in
.opt_optl
= dlen
;
410 memcpy(scp
->discdata_in
.opt_data
, skb
->data
+ 1, dlen
);
414 scp
->addrrem
= cb
->src_port
;
415 sk
->state
= TCP_CLOSE
;
423 sk
->shutdown
|= SHUTDOWN_MASK
;
432 if (sk
->socket
->state
!= SS_UNCONNECTED
)
433 sk
->socket
->state
= SS_DISCONNECTING
;
434 sk
->state_change(sk
);
437 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
, GFP_ATOMIC
);
438 scp
->persist_fxn
= dn_destroy_timer
;
439 scp
->persist
= dn_nsp_persist(sk
);
446 * disc_conf messages are also called no_resources or no_link
447 * messages depending upon the "reason" field.
449 static void dn_nsp_disc_conf(struct sock
*sk
, struct sk_buff
*skb
)
451 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
452 unsigned short reason
;
457 reason
= dn_ntohs(*(__u16
*)skb
->data
);
459 sk
->state
= TCP_CLOSE
;
466 if (reason
== NSP_REASON_DC
)
468 if (reason
== NSP_REASON_NL
)
475 sk
->shutdown
|= SHUTDOWN_MASK
;
481 if (sk
->socket
->state
!= SS_UNCONNECTED
)
482 sk
->socket
->state
= SS_DISCONNECTING
;
483 sk
->state_change(sk
);
486 scp
->persist_fxn
= dn_destroy_timer
;
487 scp
->persist
= dn_nsp_persist(sk
);
493 static void dn_nsp_linkservice(struct sock
*sk
, struct sk_buff
*skb
)
495 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
496 unsigned short segnum
;
497 unsigned char lsflags
;
503 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
505 lsflags
= *(unsigned char *)skb
->data
;
507 fcval
= *(char *)skb
->data
;
512 if (((sk
->protinfo
.dn
.numoth_rcv
+ 1) & 0x0FFF) == (segnum
& 0x0FFF)) {
513 sk
->protinfo
.dn
.numoth_rcv
+= 1;
514 switch(lsflags
& 0x03) {
518 sk
->protinfo
.dn
.flowrem_sw
= DN_DONTSEND
;
521 sk
->protinfo
.dn
.flowrem_sw
= DN_SEND
;
524 sk
->state_change(sk
);
529 dn_nsp_send_oth_ack(sk
);
536 * Copy of sock_queue_rcv_skb (from sock.h) without
537 * bh_lock_sock() (its already held when this is called) which
538 * also allows data and other data to be queued to a socket.
540 static __inline__
int dn_queue_skb(struct sock
*sk
, struct sk_buff
*skb
, int sig
, struct sk_buff_head
*queue
)
543 struct sk_filter
*filter
;
546 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
547 number of warnings when compiling with -W --ANK
549 if (atomic_read(&sk
->rmem_alloc
) + skb
->truesize
>= (unsigned)sk
->rcvbuf
556 if ((filter
= sk
->filter
) != NULL
&& sk_filter(skb
, sk
->filter
))
557 err
= -EPERM
; /* Toss packet */
561 #endif /* CONFIG_FILTER */
563 skb_set_owner_r(skb
, sk
);
564 skb_queue_tail(queue
, skb
);
566 /* This code only runs from BH or BH protected context.
567 * Therefore the plain read_lock is ok here. -DaveM
569 read_lock(&sk
->callback_lock
);
571 struct socket
*sock
= sk
->socket
;
572 wake_up_interruptible(sk
->sleep
);
573 if (sock
&& sock
->fasync_list
&&
574 !test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
575 __kill_fasync(sock
->fasync_list
, sig
,
576 (sig
== SIGURG
) ? POLL_PRI
: POLL_IN
);
578 read_unlock(&sk
->callback_lock
);
583 static void dn_nsp_otherdata(struct sock
*sk
, struct sk_buff
*skb
)
585 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
586 unsigned short segnum
;
587 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
593 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
596 if (((sk
->protinfo
.dn
.numoth_rcv
+ 1) & 0x0fff) == (segnum
& 0x0fff)) {
598 if (dn_queue_skb(sk
, skb
, SIGURG
, &scp
->other_receive_queue
) == 0) {
599 sk
->protinfo
.dn
.numoth_rcv
++;
600 scp
->other_report
= 0;
605 dn_nsp_send_oth_ack(sk
);
611 static void dn_nsp_data(struct sock
*sk
, struct sk_buff
*skb
)
614 unsigned short segnum
;
615 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
616 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
621 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
624 if (((sk
->protinfo
.dn
.numdat_rcv
+ 1) & 0x0FFF) ==
627 if (dn_queue_skb(sk
, skb
, SIGIO
, &sk
->receive_queue
) == 0) {
628 sk
->protinfo
.dn
.numdat_rcv
++;
632 if ((scp
->flowloc_sw
== DN_SEND
) && dn_congested(sk
)) {
633 scp
->flowloc_sw
= DN_DONTSEND
;
634 dn_nsp_send_lnk(sk
, DN_DONTSEND
);
638 dn_nsp_send_data_ack(sk
);
645 * If one of our conninit messages is returned, this function
646 * deals with it. It puts the socket into the NO_COMMUNICATION
649 static void dn_returned_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
651 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
653 if (scp
->state
== DN_CI
) {
655 sk
->state
= TCP_CLOSE
;
657 sk
->state_change(sk
);
663 static void dn_nsp_no_socket(struct sk_buff
*skb
, unsigned short reason
)
665 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
667 if ((reason
!= NSP_REASON_OK
) && ((cb
->nsp_flags
& 0x0c) == 0x08)) {
668 switch(cb
->nsp_flags
& 0x70) {
670 case 0x60: /* (Retransmitted) Connect Init */
671 dn_nsp_return_disc(skb
, NSP_DISCINIT
, reason
);
673 case 0x20: /* Connect Confirm */
674 dn_nsp_return_disc(skb
, NSP_DISCCONF
, reason
);
682 static int dn_nsp_rx_packet(struct sk_buff
*skb
)
684 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
685 struct sock
*sk
= NULL
;
686 unsigned char *ptr
= (unsigned char *)skb
->data
;
687 unsigned short reason
= NSP_REASON_NL
;
689 skb
->h
.raw
= skb
->data
;
690 cb
->nsp_flags
= *ptr
++;
692 if (decnet_debug_level
& 2)
693 printk(KERN_DEBUG
"dn_nsp_rx: Message type 0x%02x\n", (int)cb
->nsp_flags
);
698 if (cb
->nsp_flags
& 0x83)
702 * Returned packets...
703 * Swap src & dst and look up in the normal way.
705 if (cb
->rt_flags
& DN_RT_F_RTS
) {
706 unsigned short tmp
= cb
->dst_port
;
707 cb
->dst_port
= cb
->src_port
;
712 sk
= dn_find_by_skb(skb
);
717 * Filter out conninits and useless packet types
719 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
720 switch(cb
->nsp_flags
& 0x70) {
722 case 0x70: /* Reserved */
723 case 0x50: /* Reserved, Phase II node init */
727 sk
= dn_find_listener(skb
, &reason
);
736 * Grab the destination address.
738 cb
->dst_port
= *(unsigned short *)ptr
;
743 * If not a connack, grab the source address too.
746 cb
->src_port
= *(unsigned short *)ptr
;
752 * Find the socket to which this skb is destined.
754 sk
= dn_find_by_skb(skb
);
757 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
761 scp
->nsp_rxtshift
= 0;
765 if (sk
->lock
.users
== 0)
766 ret
= dn_nsp_backlog_rcv(sk
, skb
);
768 sk_add_backlog(sk
, skb
);
775 dn_nsp_no_socket(skb
, reason
);
783 int dn_nsp_rx(struct sk_buff
*skb
)
785 return NF_HOOK(PF_DECnet
, NF_DN_LOCAL_IN
, skb
, skb
->dev
, NULL
, dn_nsp_rx_packet
);
789 * This is the main receive routine for sockets. It is called
790 * from the above when the socket is not busy, and also from
791 * sock_release() when there is a backlog queued up.
793 int dn_nsp_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
795 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
796 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
798 if (cb
->rt_flags
& DN_RT_F_RTS
) {
799 dn_returned_conn_init(sk
, skb
);
806 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
807 switch(cb
->nsp_flags
& 0x70) {
810 dn_nsp_conn_init(sk
, skb
);
813 dn_nsp_conn_conf(sk
, skb
);
816 dn_nsp_disc_init(sk
, skb
);
819 dn_nsp_disc_conf(sk
, skb
);
823 } else if (cb
->nsp_flags
== 0x24) {
825 * Special for connacks, 'cos they don't have
826 * ack data or ack otherdata info.
828 dn_nsp_conn_ack(sk
, skb
);
832 /* both data and ack frames can kick a CC socket into RUN */
833 if ((scp
->state
== DN_CC
) && !sk
->dead
) {
835 sk
->state
= TCP_ESTABLISHED
;
836 sk
->state_change(sk
);
839 if ((cb
->nsp_flags
& 0x1c) == 0)
841 if (cb
->nsp_flags
== 0x04)
845 * Read out ack data here, this applies equally
846 * to data, other data, link serivce and both
847 * ack data and ack otherdata.
849 dn_process_ack(sk
, skb
, other
);
852 * If we've some sort of data here then call a
853 * suitable routine for dealing with it, otherwise
854 * the packet is an ack and can be discarded.
856 if ((cb
->nsp_flags
& 0x0c) == 0) {
858 if (scp
->state
!= DN_RUN
)
861 switch(cb
->nsp_flags
) {
863 dn_nsp_linkservice(sk
, skb
);
866 dn_nsp_otherdata(sk
, skb
);
869 dn_nsp_data(sk
, skb
);
872 } else { /* Ack, chuck it out here */