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/errno.h>
49 #include <linux/types.h>
50 #include <linux/socket.h>
52 #include <linux/kernel.h>
53 #include <linux/timer.h>
54 #include <linux/string.h>
55 #include <linux/sockios.h>
56 #include <linux/net.h>
57 #include <linux/netdevice.h>
58 #include <linux/inet.h>
59 #include <linux/route.h>
60 #include <linux/slab.h>
62 #include <net/tcp_states.h>
63 #include <asm/system.h>
64 #include <linux/fcntl.h>
66 #include <linux/termios.h>
67 #include <linux/interrupt.h>
68 #include <linux/proc_fs.h>
69 #include <linux/stat.h>
70 #include <linux/init.h>
71 #include <linux/poll.h>
72 #include <linux/netfilter_decnet.h>
73 #include <net/neighbour.h>
76 #include <net/dn_nsp.h>
77 #include <net/dn_dev.h>
78 #include <net/dn_route.h>
80 extern int decnet_log_martians
;
82 static void dn_log_martian(struct sk_buff
*skb
, const char *msg
)
84 if (decnet_log_martians
&& net_ratelimit()) {
85 char *devname
= skb
->dev
? skb
->dev
->name
: "???";
86 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
87 printk(KERN_INFO
"DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n",
88 msg
, devname
, le16_to_cpu(cb
->src
), le16_to_cpu(cb
->dst
),
89 le16_to_cpu(cb
->src_port
), le16_to_cpu(cb
->dst_port
));
94 * For this function we've flipped the cross-subchannel bit
95 * if the message is an otherdata or linkservice message. Thus
96 * we can use it to work out what to update.
98 static void dn_ack(struct sock
*sk
, struct sk_buff
*skb
, unsigned short ack
)
100 struct dn_scp
*scp
= DN_SK(sk
);
101 unsigned short type
= ((ack
>> 12) & 0x0003);
105 case 0: /* ACK - Data */
106 if (dn_after(ack
, scp
->ackrcv_dat
)) {
107 scp
->ackrcv_dat
= ack
& 0x0fff;
108 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
,
109 &scp
->data_xmit_queue
,
113 case 1: /* NAK - Data */
115 case 2: /* ACK - OtherData */
116 if (dn_after(ack
, scp
->ackrcv_oth
)) {
117 scp
->ackrcv_oth
= ack
& 0x0fff;
118 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
,
119 &scp
->other_xmit_queue
,
123 case 3: /* NAK - OtherData */
127 if (wakeup
&& !sock_flag(sk
, SOCK_DEAD
))
128 sk
->sk_state_change(sk
);
132 * This function is a universal ack processor.
134 static int dn_process_ack(struct sock
*sk
, struct sk_buff
*skb
, int oth
)
136 __le16
*ptr
= (__le16
*)skb
->data
;
143 if ((ack
= le16_to_cpu(*ptr
)) & 0x8000) {
147 if ((ack
& 0x4000) == 0) {
150 dn_ack(sk
, skb
, ack
);
157 if ((ack
= le16_to_cpu(*ptr
)) & 0x8000) {
160 if ((ack
& 0x4000) == 0) {
163 dn_ack(sk
, skb
, ack
);
172 * dn_check_idf - Check an image data field format is correct.
173 * @pptr: Pointer to pointer to image data
174 * @len: Pointer to length of image data
175 * @max: The maximum allowed length of the data in the image data field
176 * @follow_on: Check that this many bytes exist beyond the end of the image data
178 * Returns: 0 if ok, -1 on error
180 static inline int dn_check_idf(unsigned char **pptr
, int *len
, unsigned char max
, unsigned char follow_on
)
182 unsigned char *ptr
= *pptr
;
183 unsigned char flen
= *ptr
++;
188 if ((flen
+ follow_on
) > *len
)
197 * Table of reason codes to pass back to node which sent us a badly
198 * formed message, plus text messages for the log. A zero entry in
199 * the reason field means "don't reply" otherwise a disc init is sent with
200 * the specified reason code.
203 unsigned short reason
;
206 { 0, "CI: Truncated message" },
207 { NSP_REASON_ID
, "CI: Destination username error" },
208 { NSP_REASON_ID
, "CI: Destination username type" },
209 { NSP_REASON_US
, "CI: Source username error" },
210 { 0, "CI: Truncated at menuver" },
211 { 0, "CI: Truncated before access or user data" },
212 { NSP_REASON_IO
, "CI: Access data format error" },
213 { NSP_REASON_IO
, "CI: User data format error" }
217 * This function uses a slightly different lookup method
218 * to find its sockets, since it searches on object name/number
219 * rather than port numbers. Various tests are done to ensure that
220 * the incoming data is in the correct format before it is queued to
223 static struct sock
*dn_find_listener(struct sk_buff
*skb
, unsigned short *reason
)
225 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
226 struct nsp_conn_init_msg
*msg
= (struct nsp_conn_init_msg
*)skb
->data
;
227 struct sockaddr_dn dstaddr
;
228 struct sockaddr_dn srcaddr
;
229 unsigned char type
= 0;
235 unsigned char menuver
;
237 memset(&dstaddr
, 0, sizeof(struct sockaddr_dn
));
238 memset(&srcaddr
, 0, sizeof(struct sockaddr_dn
));
241 * 1. Decode & remove message header
243 cb
->src_port
= msg
->srcaddr
;
244 cb
->dst_port
= msg
->dstaddr
;
245 cb
->services
= msg
->services
;
246 cb
->info
= msg
->info
;
247 cb
->segsize
= le16_to_cpu(msg
->segsize
);
249 if (!pskb_may_pull(skb
, sizeof(*msg
)))
252 skb_pull(skb
, sizeof(*msg
));
258 * 2. Check destination end username format
260 dstlen
= dn_username2sockaddr(ptr
, len
, &dstaddr
, &type
);
273 * 3. Check source end username format
275 srclen
= dn_username2sockaddr(ptr
, len
, &srcaddr
, &type
);
291 * 4. Check that optional data actually exists if menuver says it does
294 if ((menuver
& (DN_MENUVER_ACC
| DN_MENUVER_USR
)) && (len
< 1))
298 * 5. Check optional access data format
301 if (menuver
& DN_MENUVER_ACC
) {
302 if (dn_check_idf(&ptr
, &len
, 39, 1))
304 if (dn_check_idf(&ptr
, &len
, 39, 1))
306 if (dn_check_idf(&ptr
, &len
, 39, (menuver
& DN_MENUVER_USR
) ? 1 : 0))
311 * 6. Check optional user data format
314 if (menuver
& DN_MENUVER_USR
) {
315 if (dn_check_idf(&ptr
, &len
, 16, 0))
320 * 7. Look up socket based on destination end username
322 return dn_sklist_find_listener(&dstaddr
);
324 dn_log_martian(skb
, ci_err_table
[err
].text
);
325 *reason
= ci_err_table
[err
].reason
;
330 static void dn_nsp_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
332 if (sk_acceptq_is_full(sk
)) {
337 sk
->sk_ack_backlog
++;
338 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
339 sk
->sk_state_change(sk
);
342 static void dn_nsp_conn_conf(struct sock
*sk
, struct sk_buff
*skb
)
344 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
345 struct dn_scp
*scp
= DN_SK(sk
);
352 cb
->services
= *ptr
++;
354 cb
->segsize
= le16_to_cpu(*(__le16
*)ptr
);
356 if ((scp
->state
== DN_CI
) || (scp
->state
== DN_CD
)) {
358 scp
->addrrem
= cb
->src_port
;
359 sk
->sk_state
= TCP_ESTABLISHED
;
361 scp
->services_rem
= cb
->services
;
362 scp
->info_rem
= cb
->info
;
363 scp
->segsize_rem
= cb
->segsize
;
365 if ((scp
->services_rem
& NSP_FC_MASK
) == NSP_FC_NONE
)
366 scp
->max_window
= decnet_no_fc_max_cwnd
;
369 u16 dlen
= *skb
->data
;
370 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
371 scp
->conndata_in
.opt_optl
= cpu_to_le16(dlen
);
372 skb_copy_from_linear_data_offset(skb
, 1,
373 scp
->conndata_in
.opt_data
, dlen
);
376 dn_nsp_send_link(sk
, DN_NOCHANGE
, 0);
377 if (!sock_flag(sk
, SOCK_DEAD
))
378 sk
->sk_state_change(sk
);
385 static void dn_nsp_conn_ack(struct sock
*sk
, struct sk_buff
*skb
)
387 struct dn_scp
*scp
= DN_SK(sk
);
389 if (scp
->state
== DN_CI
) {
397 static void dn_nsp_disc_init(struct sock
*sk
, struct sk_buff
*skb
)
399 struct dn_scp
*scp
= DN_SK(sk
);
400 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
401 unsigned short reason
;
406 reason
= le16_to_cpu(*(__le16
*)skb
->data
);
409 scp
->discdata_in
.opt_status
= cpu_to_le16(reason
);
410 scp
->discdata_in
.opt_optl
= 0;
411 memset(scp
->discdata_in
.opt_data
, 0, 16);
414 u16 dlen
= *skb
->data
;
415 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
416 scp
->discdata_in
.opt_optl
= cpu_to_le16(dlen
);
417 skb_copy_from_linear_data_offset(skb
, 1, scp
->discdata_in
.opt_data
, dlen
);
421 scp
->addrrem
= cb
->src_port
;
422 sk
->sk_state
= TCP_CLOSE
;
424 switch (scp
->state
) {
428 sk
->sk_err
= ECONNREFUSED
;
431 sk
->sk_shutdown
|= SHUTDOWN_MASK
;
439 if (!sock_flag(sk
, SOCK_DEAD
)) {
440 if (sk
->sk_socket
->state
!= SS_UNCONNECTED
)
441 sk
->sk_socket
->state
= SS_DISCONNECTING
;
442 sk
->sk_state_change(sk
);
446 * It appears that its possible for remote machines to send disc
447 * init messages with no port identifier if we are in the CI and
448 * possibly also the CD state. Obviously we shouldn't reply with
449 * a message if we don't know what the end point is.
452 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
, GFP_ATOMIC
);
454 scp
->persist_fxn
= dn_destroy_timer
;
455 scp
->persist
= dn_nsp_persist(sk
);
462 * disc_conf messages are also called no_resources or no_link
463 * messages depending upon the "reason" field.
465 static void dn_nsp_disc_conf(struct sock
*sk
, struct sk_buff
*skb
)
467 struct dn_scp
*scp
= DN_SK(sk
);
468 unsigned short reason
;
473 reason
= le16_to_cpu(*(__le16
*)skb
->data
);
475 sk
->sk_state
= TCP_CLOSE
;
477 switch (scp
->state
) {
482 if (reason
== NSP_REASON_DC
)
484 if (reason
== NSP_REASON_NL
)
491 sk
->sk_shutdown
|= SHUTDOWN_MASK
;
496 if (!sock_flag(sk
, SOCK_DEAD
)) {
497 if (sk
->sk_socket
->state
!= SS_UNCONNECTED
)
498 sk
->sk_socket
->state
= SS_DISCONNECTING
;
499 sk
->sk_state_change(sk
);
502 scp
->persist_fxn
= dn_destroy_timer
;
503 scp
->persist
= dn_nsp_persist(sk
);
509 static void dn_nsp_linkservice(struct sock
*sk
, struct sk_buff
*skb
)
511 struct dn_scp
*scp
= DN_SK(sk
);
512 unsigned short segnum
;
513 unsigned char lsflags
;
516 char *ptr
= skb
->data
;
517 unsigned char fctype
= scp
->services_rem
& NSP_FC_MASK
;
522 segnum
= le16_to_cpu(*(__le16
*)ptr
);
524 lsflags
= *(unsigned char *)ptr
++;
528 * Here we ignore erronous packets which should really
529 * should cause a connection abort. It is not critical
535 if (seq_next(scp
->numoth_rcv
, segnum
)) {
536 seq_add(&scp
->numoth_rcv
, 1);
537 switch(lsflags
& 0x04) { /* FCVAL INT */
538 case 0x00: /* Normal Request */
539 switch(lsflags
& 0x03) { /* FCVAL MOD */
540 case 0x00: /* Request count */
542 unsigned char p_fcval
= -fcval
;
543 if ((scp
->flowrem_dat
> p_fcval
) &&
544 (fctype
== NSP_FC_SCMC
)) {
545 scp
->flowrem_dat
-= p_fcval
;
547 } else if (fcval
> 0) {
548 scp
->flowrem_dat
+= fcval
;
552 case 0x01: /* Stop outgoing data */
553 scp
->flowrem_sw
= DN_DONTSEND
;
555 case 0x02: /* Ok to start again */
556 scp
->flowrem_sw
= DN_SEND
;
561 case 0x04: /* Interrupt Request */
563 scp
->flowrem_oth
+= fcval
;
568 if (wake_up
&& !sock_flag(sk
, SOCK_DEAD
))
569 sk
->sk_state_change(sk
);
572 dn_nsp_send_oth_ack(sk
);
579 * Copy of sock_queue_rcv_skb (from sock.h) without
580 * bh_lock_sock() (its already held when this is called) which
581 * also allows data and other data to be queued to a socket.
583 static __inline__
int dn_queue_skb(struct sock
*sk
, struct sk_buff
*skb
, int sig
, struct sk_buff_head
*queue
)
588 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
589 number of warnings when compiling with -W --ANK
591 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
592 (unsigned)sk
->sk_rcvbuf
) {
597 err
= sk_filter(sk
, skb
);
602 skb_set_owner_r(skb
, sk
);
603 skb_queue_tail(queue
, skb
);
605 if (!sock_flag(sk
, SOCK_DEAD
))
606 sk
->sk_data_ready(sk
, skb_len
);
611 static void dn_nsp_otherdata(struct sock
*sk
, struct sk_buff
*skb
)
613 struct dn_scp
*scp
= DN_SK(sk
);
614 unsigned short segnum
;
615 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
621 cb
->segnum
= segnum
= le16_to_cpu(*(__le16
*)skb
->data
);
624 if (seq_next(scp
->numoth_rcv
, segnum
)) {
626 if (dn_queue_skb(sk
, skb
, SIGURG
, &scp
->other_receive_queue
) == 0) {
627 seq_add(&scp
->numoth_rcv
, 1);
628 scp
->other_report
= 0;
633 dn_nsp_send_oth_ack(sk
);
639 static void dn_nsp_data(struct sock
*sk
, struct sk_buff
*skb
)
642 unsigned short segnum
;
643 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
644 struct dn_scp
*scp
= DN_SK(sk
);
649 cb
->segnum
= segnum
= le16_to_cpu(*(__le16
*)skb
->data
);
652 if (seq_next(scp
->numdat_rcv
, segnum
)) {
653 if (dn_queue_skb(sk
, skb
, SIGIO
, &sk
->sk_receive_queue
) == 0) {
654 seq_add(&scp
->numdat_rcv
, 1);
658 if ((scp
->flowloc_sw
== DN_SEND
) && dn_congested(sk
)) {
659 scp
->flowloc_sw
= DN_DONTSEND
;
660 dn_nsp_send_link(sk
, DN_DONTSEND
, 0);
664 dn_nsp_send_data_ack(sk
);
671 * If one of our conninit messages is returned, this function
672 * deals with it. It puts the socket into the NO_COMMUNICATION
675 static void dn_returned_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
677 struct dn_scp
*scp
= DN_SK(sk
);
679 if (scp
->state
== DN_CI
) {
681 sk
->sk_state
= TCP_CLOSE
;
682 if (!sock_flag(sk
, SOCK_DEAD
))
683 sk
->sk_state_change(sk
);
689 static int dn_nsp_no_socket(struct sk_buff
*skb
, unsigned short reason
)
691 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
692 int ret
= NET_RX_DROP
;
694 /* Must not reply to returned packets */
695 if (cb
->rt_flags
& DN_RT_F_RTS
)
698 if ((reason
!= NSP_REASON_OK
) && ((cb
->nsp_flags
& 0x0c) == 0x08)) {
699 switch (cb
->nsp_flags
& 0x70) {
701 case 0x60: /* (Retransmitted) Connect Init */
702 dn_nsp_return_disc(skb
, NSP_DISCINIT
, reason
);
703 ret
= NET_RX_SUCCESS
;
705 case 0x20: /* Connect Confirm */
706 dn_nsp_return_disc(skb
, NSP_DISCCONF
, reason
);
707 ret
= NET_RX_SUCCESS
;
717 static int dn_nsp_rx_packet(struct sk_buff
*skb
)
719 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
720 struct sock
*sk
= NULL
;
721 unsigned char *ptr
= (unsigned char *)skb
->data
;
722 unsigned short reason
= NSP_REASON_NL
;
724 if (!pskb_may_pull(skb
, 2))
727 skb_reset_transport_header(skb
);
728 cb
->nsp_flags
= *ptr
++;
730 if (decnet_debug_level
& 2)
731 printk(KERN_DEBUG
"dn_nsp_rx: Message type 0x%02x\n", (int)cb
->nsp_flags
);
733 if (cb
->nsp_flags
& 0x83)
737 * Filter out conninits and useless packet types
739 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
740 switch (cb
->nsp_flags
& 0x70) {
742 case 0x70: /* Reserved */
743 case 0x50: /* Reserved, Phase II node init */
747 if (unlikely(cb
->rt_flags
& DN_RT_F_RTS
))
749 sk
= dn_find_listener(skb
, &reason
);
754 if (!pskb_may_pull(skb
, 3))
758 * Grab the destination address.
760 cb
->dst_port
= *(__le16
*)ptr
;
765 * If not a connack, grab the source address too.
767 if (pskb_may_pull(skb
, 5)) {
768 cb
->src_port
= *(__le16
*)ptr
;
774 * Returned packets...
775 * Swap src & dst and look up in the normal way.
777 if (unlikely(cb
->rt_flags
& DN_RT_F_RTS
)) {
778 __le16 tmp
= cb
->dst_port
;
779 cb
->dst_port
= cb
->src_port
;
787 * Find the socket to which this skb is destined.
789 sk
= dn_find_by_skb(skb
);
792 struct dn_scp
*scp
= DN_SK(sk
);
795 scp
->nsp_rxtshift
= 0;
798 * We linearize everything except data segments here.
800 if (cb
->nsp_flags
& ~0x60) {
801 if (unlikely(skb_linearize(skb
)))
805 return sk_receive_skb(sk
, skb
, 0);
808 return dn_nsp_no_socket(skb
, reason
);
815 int dn_nsp_rx(struct sk_buff
*skb
)
817 return NF_HOOK(NFPROTO_DECNET
, NF_DN_LOCAL_IN
, skb
, skb
->dev
, NULL
,
822 * This is the main receive routine for sockets. It is called
823 * from the above when the socket is not busy, and also from
824 * sock_release() when there is a backlog queued up.
826 int dn_nsp_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
828 struct dn_scp
*scp
= DN_SK(sk
);
829 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
831 if (cb
->rt_flags
& DN_RT_F_RTS
) {
832 if (cb
->nsp_flags
== 0x18 || cb
->nsp_flags
== 0x68)
833 dn_returned_conn_init(sk
, skb
);
836 return NET_RX_SUCCESS
;
842 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
843 switch (cb
->nsp_flags
& 0x70) {
846 dn_nsp_conn_init(sk
, skb
);
849 dn_nsp_conn_conf(sk
, skb
);
852 dn_nsp_disc_init(sk
, skb
);
855 dn_nsp_disc_conf(sk
, skb
);
859 } else if (cb
->nsp_flags
== 0x24) {
861 * Special for connacks, 'cos they don't have
862 * ack data or ack otherdata info.
864 dn_nsp_conn_ack(sk
, skb
);
868 /* both data and ack frames can kick a CC socket into RUN */
869 if ((scp
->state
== DN_CC
) && !sock_flag(sk
, SOCK_DEAD
)) {
871 sk
->sk_state
= TCP_ESTABLISHED
;
872 sk
->sk_state_change(sk
);
875 if ((cb
->nsp_flags
& 0x1c) == 0)
877 if (cb
->nsp_flags
== 0x04)
881 * Read out ack data here, this applies equally
882 * to data, other data, link serivce and both
883 * ack data and ack otherdata.
885 dn_process_ack(sk
, skb
, other
);
888 * If we've some sort of data here then call a
889 * suitable routine for dealing with it, otherwise
890 * the packet is an ack and can be discarded.
892 if ((cb
->nsp_flags
& 0x0c) == 0) {
894 if (scp
->state
!= DN_RUN
)
897 switch (cb
->nsp_flags
) {
899 dn_nsp_linkservice(sk
, skb
);
902 dn_nsp_otherdata(sk
, skb
);
905 dn_nsp_data(sk
, skb
);
908 } else { /* Ack, chuck it out here */
914 return NET_RX_SUCCESS
;