3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * DECnet Network Services Protocol (Input)
9 * Author: Eduardo Marcelo Serrat <emserrat@geocities.com>
13 * Steve Whitehouse: Split into dn_nsp_in.c and dn_nsp_out.c from
15 * Steve Whitehouse: Updated to work with my new routing architecture.
16 * Steve Whitehouse: Add changes from Eduardo Serrat's patches.
17 * Steve Whitehouse: Put all ack handling code in a common routine.
18 * Steve Whitehouse: Put other common bits into dn_nsp_rx()
19 * Steve Whitehouse: More checks on skb->len to catch bogus packets
20 * Fixed various race conditions and possible nasties.
21 * Steve Whitehouse: Now handles returned conninit frames.
22 * David S. Miller: New socket locking
23 * Steve Whitehouse: Fixed lockup when socket filtering was enabled.
24 * Paul Koning: Fix to push CC sockets into RUN when acks are
28 /******************************************************************************
29 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
31 This program is free software; you can redistribute it and/or modify
32 it under the terms of the GNU General Public License as published by
33 the Free Software Foundation; either version 2 of the License, or
36 This program is distributed in the hope that it will be useful,
37 but WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 GNU General Public License for more details.
40 *******************************************************************************/
42 #include <linux/config.h>
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/timer.h>
50 #include <linux/string.h>
51 #include <linux/sockios.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/inet.h>
55 #include <linux/route.h>
57 #include <asm/segment.h>
58 #include <asm/system.h>
59 #include <linux/fcntl.h>
61 #include <linux/termios.h>
62 #include <linux/interrupt.h>
63 #include <linux/proc_fs.h>
64 #include <linux/stat.h>
65 #include <linux/init.h>
66 #include <linux/poll.h>
67 #include <linux/netfilter_decnet.h>
68 #include <net/neighbour.h>
70 #include <net/dn_nsp.h>
71 #include <net/dn_dev.h>
72 #include <net/dn_route.h>
76 * For this function we've flipped the cross-subchannel bit
77 * if the message is an otherdata or linkservice message. Thus
78 * we can use it to work out what to update.
80 static void dn_ack(struct sock
*sk
, struct sk_buff
*skb
, unsigned short ack
)
82 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
83 unsigned short type
= ((ack
>> 12) & 0x0003);
86 /* printk(KERN_DEBUG "dn_ack: %hd 0x%04hx\n", type, ack); */
89 case 0: /* ACK - Data */
90 if (after(ack
, scp
->ackrcv_dat
)) {
91 scp
->ackrcv_dat
= ack
& 0x0fff;
92 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
, &scp
->data_xmit_queue
, ack
);
95 case 1: /* NAK - Data */
97 case 2: /* ACK - OtherData */
98 if (after(ack
, scp
->ackrcv_oth
)) {
99 scp
->ackrcv_oth
= ack
& 0x0fff;
100 wakeup
|= dn_nsp_check_xmit_queue(sk
, skb
, &scp
->other_xmit_queue
, ack
);
103 case 3: /* NAK - OtherData */
107 if (wakeup
&& !sk
->dead
)
108 sk
->state_change(sk
);
112 * This function is a universal ack processor.
114 static int dn_process_ack(struct sock
*sk
, struct sk_buff
*skb
, int oth
)
116 unsigned short *ptr
= (unsigned short *)skb
->data
;
123 if ((ack
= dn_ntohs(*ptr
)) & 0x8000) {
127 if ((ack
& 0x4000) == 0) {
130 dn_ack(sk
, skb
, ack
);
137 if ((ack
= dn_ntohs(*ptr
)) & 0x8000) {
140 if ((ack
& 0x4000) == 0) {
143 dn_ack(sk
, skb
, ack
);
152 * This function uses a slightly different lookup method
153 * to find its sockets, since it searches on object name/number
154 * rather than port numbers
156 static struct sock
*dn_find_listener(struct sk_buff
*skb
)
158 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
159 struct nsp_conn_init_msg
*msg
= (struct nsp_conn_init_msg
*)skb
->data
;
160 struct sockaddr_dn addr
;
161 unsigned char type
= 0;
163 memset(&addr
, 0, sizeof(struct sockaddr_dn
));
165 cb
->src_port
= msg
->srcaddr
;
166 cb
->dst_port
= msg
->dstaddr
;
167 cb
->services
= msg
->services
;
168 cb
->info
= msg
->info
;
169 cb
->segsize
= dn_ntohs(msg
->segsize
);
171 skb_pull(skb
, sizeof(*msg
));
173 /* printk(KERN_DEBUG "username2sockaddr 1\n"); */
174 if (dn_username2sockaddr(skb
->data
, skb
->len
, &addr
, &type
) < 0)
180 /* printk(KERN_DEBUG "looking for listener...\n"); */
181 return dn_sklist_find_listener(&addr
);
186 static void dn_nsp_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
188 /* printk(KERN_DEBUG "checking backlog...\n"); */
189 if (sk
->ack_backlog
>= sk
->max_ack_backlog
) {
194 /* printk(KERN_DEBUG "waking up socket...\n"); */
196 skb_queue_tail(&sk
->receive_queue
, skb
);
197 sk
->state_change(sk
);
200 static void dn_nsp_conn_conf(struct sock
*sk
, struct sk_buff
*skb
)
202 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
203 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
208 cb
->services
= *skb
->data
;
209 cb
->info
= *(skb
->data
+1);
211 cb
->segsize
= dn_ntohs(*(__u16
*)skb
->data
);
215 * FIXME: Check out services and info fields to check that
216 * we can talk to this kind of node.
219 if ((scp
->state
== DN_CI
) || (scp
->state
== DN_CD
)) {
221 scp
->addrrem
= cb
->src_port
;
222 sk
->state
= TCP_ESTABLISHED
;
225 if (scp
->mss
> cb
->segsize
)
226 scp
->mss
= cb
->segsize
;
231 unsigned char dlen
= *skb
->data
;
232 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
233 scp
->conndata_in
.opt_optl
= dlen
;
234 memcpy(scp
->conndata_in
.opt_data
, skb
->data
+ 1, dlen
);
237 dn_nsp_send_lnk(sk
, DN_NOCHANGE
);
239 sk
->state_change(sk
);
246 static void dn_nsp_conn_ack(struct sock
*sk
, struct sk_buff
*skb
)
248 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
250 if (scp
->state
== DN_CI
) {
258 static void dn_nsp_disc_init(struct sock
*sk
, struct sk_buff
*skb
)
260 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
261 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
262 unsigned short reason
;
267 reason
= dn_ntohs(*(__u16
*)skb
->data
);
270 scp
->discdata_in
.opt_status
= reason
;
271 scp
->discdata_in
.opt_optl
= 0;
272 memset(scp
->discdata_in
.opt_data
, 0, 16);
275 unsigned char dlen
= *skb
->data
;
276 if ((dlen
<= 16) && (dlen
<= skb
->len
)) {
277 scp
->discdata_in
.opt_optl
= dlen
;
278 memcpy(scp
->discdata_in
.opt_data
, skb
->data
+ 1, dlen
);
282 scp
->addrrem
= cb
->src_port
;
283 sk
->state
= TCP_CLOSE
;
291 sk
->shutdown
|= SHUTDOWN_MASK
;
300 if (sk
->socket
->state
!= SS_UNCONNECTED
)
301 sk
->socket
->state
= SS_DISCONNECTING
;
302 sk
->state_change(sk
);
305 dn_nsp_send_disc(sk
, NSP_DISCCONF
, NSP_REASON_DC
, GFP_ATOMIC
);
306 scp
->persist_fxn
= dn_destroy_timer
;
307 scp
->persist
= dn_nsp_persist(sk
);
314 * disc_conf messages are also called no_resources or no_link
315 * messages depending upon the "reason" field.
317 static void dn_nsp_disc_conf(struct sock
*sk
, struct sk_buff
*skb
)
319 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
320 unsigned short reason
;
325 reason
= dn_ntohs(*(__u16
*)skb
->data
);
327 sk
->state
= TCP_CLOSE
;
334 if (reason
== NSP_REASON_DC
)
336 if (reason
== NSP_REASON_NL
)
343 sk
->shutdown
|= SHUTDOWN_MASK
;
349 if (sk
->socket
->state
!= SS_UNCONNECTED
)
350 sk
->socket
->state
= SS_DISCONNECTING
;
351 sk
->state_change(sk
);
354 scp
->persist_fxn
= dn_destroy_timer
;
355 scp
->persist
= dn_nsp_persist(sk
);
361 static void dn_nsp_linkservice(struct sock
*sk
, struct sk_buff
*skb
)
363 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
364 unsigned short segnum
;
365 unsigned char lsflags
;
371 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
373 lsflags
= *(unsigned char *)skb
->data
;
375 fcval
= *(char *)skb
->data
;
380 if (((sk
->protinfo
.dn
.numoth_rcv
+ 1) & 0x0FFF) == (segnum
& 0x0FFF)) {
381 sk
->protinfo
.dn
.numoth_rcv
+= 1;
382 switch(lsflags
& 0x03) {
386 sk
->protinfo
.dn
.flowrem_sw
= DN_DONTSEND
;
389 sk
->protinfo
.dn
.flowrem_sw
= DN_SEND
;
392 sk
->state_change(sk
);
397 dn_nsp_send_oth_ack(sk
);
404 * Copy of sock_queue_rcv_skb (from sock.h) without
405 * bh_lock_sock() (its already held when this is called) which
406 * also allows data and other data to be queued to a socket.
408 static __inline__
int dn_queue_skb(struct sock
*sk
, struct sk_buff
*skb
, int sig
, struct sk_buff_head
*queue
)
411 struct sk_filter
*filter
;
414 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
415 number of warnings when compiling with -W --ANK
417 if (atomic_read(&sk
->rmem_alloc
) + skb
->truesize
>= (unsigned)sk
->rcvbuf
424 if ((filter
= sk
->filter
) != NULL
&& sk_filter(skb
, sk
->filter
))
425 err
= -EPERM
; /* Toss packet */
429 #endif /* CONFIG_FILTER */
431 skb_set_owner_r(skb
, sk
);
432 skb_queue_tail(queue
, skb
);
434 /* This code only runs from BH or BH protected context.
435 * Therefore the plain read_lock is ok here. -DaveM
437 read_lock(&sk
->callback_lock
);
439 struct socket
*sock
= sk
->socket
;
440 wake_up_interruptible(sk
->sleep
);
441 if (sock
&& sock
->fasync_list
&&
442 !test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
443 __kill_fasync(sock
->fasync_list
, sig
,
444 (sig
== SIGURG
) ? POLL_PRI
: POLL_IN
);
446 read_unlock(&sk
->callback_lock
);
451 static void dn_nsp_otherdata(struct sock
*sk
, struct sk_buff
*skb
)
453 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
454 unsigned short segnum
;
455 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
461 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
464 if (((sk
->protinfo
.dn
.numoth_rcv
+ 1) & 0x0fff) == (segnum
& 0x0fff)) {
466 if (dn_queue_skb(sk
, skb
, SIGURG
, &scp
->other_receive_queue
) == 0) {
467 sk
->protinfo
.dn
.numoth_rcv
++;
468 scp
->other_report
= 0;
473 dn_nsp_send_oth_ack(sk
);
479 static void dn_nsp_data(struct sock
*sk
, struct sk_buff
*skb
)
482 unsigned short segnum
;
483 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
484 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
489 cb
->segnum
= segnum
= dn_ntohs(*(__u16
*)skb
->data
);
492 if (((sk
->protinfo
.dn
.numdat_rcv
+ 1) & 0x0FFF) ==
495 if (dn_queue_skb(sk
, skb
, SIGIO
, &sk
->receive_queue
) == 0) {
496 sk
->protinfo
.dn
.numdat_rcv
++;
500 if ((scp
->flowloc_sw
== DN_SEND
) && dn_congested(sk
)) {
501 scp
->flowloc_sw
= DN_DONTSEND
;
502 dn_nsp_send_lnk(sk
, DN_DONTSEND
);
506 dn_nsp_send_data_ack(sk
);
513 * If one of our conninit messages is returned, this function
514 * deals with it. It puts the socket into the NO_COMMUNICATION
517 static void dn_returned_conn_init(struct sock
*sk
, struct sk_buff
*skb
)
519 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
521 if (scp
->state
== DN_CI
) {
523 sk
->state
= TCP_CLOSE
;
525 sk
->state_change(sk
);
531 static void dn_nsp_no_socket(struct sk_buff
*skb
)
533 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
535 switch(cb
->nsp_flags
) {
536 case 0x28: /* Connect Confirm */
537 dn_nsp_return_disc(skb
, NSP_DISCCONF
, NSP_REASON_NL
);
543 static int dn_nsp_rx_packet(struct sk_buff
*skb
)
545 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
546 struct sock
*sk
= NULL
;
547 unsigned char *ptr
= (unsigned char *)skb
->data
;
549 skb
->h
.raw
= skb
->data
;
550 cb
->nsp_flags
= *ptr
++;
552 if (decnet_debug_level
& 2)
553 printk(KERN_DEBUG
"dn_nsp_rx: Message type 0x%02x\n", (int)cb
->nsp_flags
);
558 if (cb
->nsp_flags
& 0x83)
562 * Returned packets...
563 * Swap src & dst and look up in the normal way.
565 if (cb
->rt_flags
& DN_RT_F_RTS
) {
566 unsigned short tmp
= cb
->dst_port
;
567 cb
->dst_port
= cb
->src_port
;
572 sk
= dn_find_by_skb(skb
);
577 * Filter out conninits and useless packet types
579 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
580 switch(cb
->nsp_flags
& 0x70) {
582 case 0x70: /* Reserved */
583 case 0x50: /* Reserved, Phase II node init */
587 sk
= dn_find_listener(skb
);
596 * Grab the destination address.
598 cb
->dst_port
= *(unsigned short *)ptr
;
603 * If not a connack, grab the source address too.
606 cb
->src_port
= *(unsigned short *)ptr
;
612 * Find the socket to which this skb is destined.
614 sk
= dn_find_by_skb(skb
);
617 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
621 scp
->nsp_rxtshift
= 0;
625 if (sk
->lock
.users
== 0)
626 ret
= dn_nsp_backlog_rcv(sk
, skb
);
628 sk_add_backlog(sk
, skb
);
635 dn_nsp_no_socket(skb
);
643 int dn_nsp_rx(struct sk_buff
*skb
)
645 return NF_HOOK(PF_DECnet
, NF_DN_LOCAL_IN
, skb
, skb
->rx_dev
, NULL
, dn_nsp_rx_packet
);
649 * This is the main receive routine for sockets. It is called
650 * from the above when the socket is not busy, and also from
651 * sock_release() when there is a backlog queued up.
653 int dn_nsp_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
655 struct dn_scp
*scp
= &sk
->protinfo
.dn
;
656 struct dn_skb_cb
*cb
= (struct dn_skb_cb
*)skb
->cb
;
658 if (cb
->rt_flags
& DN_RT_F_RTS
) {
659 dn_returned_conn_init(sk
, skb
);
666 if ((cb
->nsp_flags
& 0x0c) == 0x08) {
667 /* printk(KERN_DEBUG "control type\n"); */
668 switch(cb
->nsp_flags
& 0x70) {
671 dn_nsp_conn_init(sk
, skb
);
674 dn_nsp_conn_conf(sk
, skb
);
677 dn_nsp_disc_init(sk
, skb
);
680 dn_nsp_disc_conf(sk
, skb
);
684 } else if (cb
->nsp_flags
== 0x24) {
686 * Special for connacks, 'cos they don't have
687 * ack data or ack otherdata info.
689 dn_nsp_conn_ack(sk
, skb
);
693 /* both data and ack frames can kick a CC socket into RUN */
694 if ((scp
->state
== DN_CC
) && !sk
->dead
) {
696 sk
->state
= TCP_ESTABLISHED
;
697 sk
->state_change(sk
);
700 if ((cb
->nsp_flags
& 0x1c) == 0)
702 if (cb
->nsp_flags
== 0x04)
706 * Read out ack data here, this applies equally
707 * to data, other data, link serivce and both
708 * ack data and ack otherdata.
710 dn_process_ack(sk
, skb
, other
);
713 * If we've some sort of data here then call a
714 * suitable routine for dealing with it, otherwise
715 * the packet is an ack and can be discarded.
717 if ((cb
->nsp_flags
& 0x0c) == 0) {
719 if (scp
->state
!= DN_RUN
)
722 switch(cb
->nsp_flags
) {
724 dn_nsp_linkservice(sk
, skb
);
727 dn_nsp_otherdata(sk
, skb
);
730 dn_nsp_data(sk
, skb
);
733 } else { /* Ack, chuck it out here */