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)
33 /******************************************************************************
34 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
36 This program is free software; you can redistribute it and/or modify
37 it under the terms of the GNU General Public License as published by
38 the Free Software Foundation; either version 2 of the License, or
41 This program is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 GNU General Public License for more details.
45 *******************************************************************************/
47 #include <linux/config.h>
48 #include <linux/errno.h>
49 #include <linux/types.h>
50 #include <linux/socket.h>
52 #include <linux/kernel.h>
53 #include <linux/sched.h>
54 #include <linux/timer.h>
55 #include <linux/string.h>
56 #include <linux/sockios.h>
57 #include <linux/net.h>
58 #include <linux/netdevice.h>
59 #include <linux/inet.h>
60 #include <linux/route.h>
62 #include <asm/segment.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>
75 #include <net/dn_nsp.h>
76 #include <net/dn_dev.h>
77 #include <net/dn_route.h>
79 extern int decnet_log_martians;
81 static void dn_log_martian(struct sk_buff *skb, const char *msg)
83 if (decnet_log_martians && net_ratelimit()) {
84 char *devname = skb->dev ? skb->dev->name : "???";
85 struct dn_skb_cb *cb = DN_SKB_CB(skb);
86 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);
91 * For this function we've flipped the cross-subchannel bit
92 * if the message is an otherdata or linkservice message. Thus
93 * we can use it to work out what to update.
95 static void dn_ack(struct sock *sk, struct sk_buff *skb, unsigned short ack)
97 struct dn_scp *scp = DN_SK(sk);
98 unsigned short type = ((ack >> 12) & 0x0003);
102 case 0: /* ACK - Data */
103 if (after(ack, scp->ackrcv_dat)) {
104 scp->ackrcv_dat = ack & 0x0fff;
105 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->data_xmit_queue, ack);
108 case 1: /* NAK - Data */
110 case 2: /* ACK - OtherData */
111 if (after(ack, scp->ackrcv_oth)) {
112 scp->ackrcv_oth = ack & 0x0fff;
113 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->other_xmit_queue, ack);
116 case 3: /* NAK - OtherData */
120 if (wakeup && !sk->dead)
121 sk->state_change(sk);
125 * This function is a universal ack processor.
127 static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
129 unsigned short *ptr = (unsigned short *)skb->data;
136 if ((ack = dn_ntohs(*ptr)) & 0x8000) {
140 if ((ack & 0x4000) == 0) {
143 dn_ack(sk, skb, ack);
150 if ((ack = dn_ntohs(*ptr)) & 0x8000) {
153 if ((ack & 0x4000) == 0) {
156 dn_ack(sk, skb, ack);
165 * dn_check_idf - Check an image data field format is correct.
166 * @pptr: Pointer to pointer to image data
167 * @len: Pointer to length of image data
168 * @max: The maximum allowed length of the data in the image data field
169 * @follow_on: Check that this many bytes exist beyond the end of the image data
171 * Returns: 0 if ok, -1 on error
173 static inline int dn_check_idf(unsigned char **pptr, int *len, unsigned char max, unsigned char follow_on)
175 unsigned char *ptr = *pptr;
176 unsigned char flen = *ptr++;
181 if ((flen + follow_on) > *len)
190 * Table of reason codes to pass back to node which sent us a badly
191 * formed message, plus text messages for the log. A zero entry in
192 * the reason field means "don't reply" otherwise a disc init is sent with
193 * the specified reason code.
196 unsigned short reason;
199 { 0, "CI: Truncated message" },
200 { NSP_REASON_ID, "CI: Destination username error" },
201 { NSP_REASON_ID, "CI: Destination username type" },
202 { NSP_REASON_US, "CI: Source username error" },
203 { 0, "CI: Truncated at menuver" },
204 { 0, "CI: Truncated before access or user data" },
205 { NSP_REASON_IO, "CI: Access data format error" },
206 { NSP_REASON_IO, "CI: User data format error" }
210 * This function uses a slightly different lookup method
211 * to find its sockets, since it searches on object name/number
212 * rather than port numbers. Various tests are done to ensure that
213 * the incoming data is in the correct format before it is queued to
216 static struct sock *dn_find_listener(struct sk_buff *skb, unsigned short *reason)
218 struct dn_skb_cb *cb = DN_SKB_CB(skb);
219 struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
220 struct sockaddr_dn dstaddr;
221 struct sockaddr_dn srcaddr;
222 unsigned char type = 0;
228 unsigned char menuver;
230 memset(&dstaddr, 0, sizeof(struct sockaddr_dn));
231 memset(&srcaddr, 0, sizeof(struct sockaddr_dn));
234 * 1. Decode & remove message header
236 cb->src_port = msg->srcaddr;
237 cb->dst_port = msg->dstaddr;
238 cb->services = msg->services;
239 cb->info = msg->info;
240 cb->segsize = dn_ntohs(msg->segsize);
242 if (skb->len < sizeof(*msg))
245 skb_pull(skb, sizeof(*msg));
251 * 2. Check destination end username format
253 dstlen = dn_username2sockaddr(ptr, len, &dstaddr, &type);
266 * 3. Check source end username format
268 srclen = dn_username2sockaddr(ptr, len, &srcaddr, &type);
284 * 4. Check that optional data actually exists if menuver says it does
287 if ((menuver & (DN_MENUVER_ACC | DN_MENUVER_USR)) && (len < 1))
291 * 5. Check optional access data format
294 if (menuver & DN_MENUVER_ACC) {
295 if (dn_check_idf(&ptr, &len, 39, 1))
297 if (dn_check_idf(&ptr, &len, 39, 1))
299 if (dn_check_idf(&ptr, &len, 39, (menuver & DN_MENUVER_USR) ? 1 : 0))
304 * 6. Check optional user data format
307 if (menuver & DN_MENUVER_USR) {
308 if (dn_check_idf(&ptr, &len, 16, 0))
313 * 7. Look up socket based on destination end username
315 return dn_sklist_find_listener(&dstaddr);
317 dn_log_martian(skb, ci_err_table[err].text);
318 *reason = ci_err_table[err].reason;
323 static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
325 if (sk->ack_backlog >= sk->max_ack_backlog) {
331 skb_queue_tail(&sk->receive_queue, skb);
332 sk->state_change(sk);
335 static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
337 struct dn_skb_cb *cb = DN_SKB_CB(skb);
338 struct dn_scp *scp = DN_SK(sk);
345 cb->services = *ptr++;
347 cb->segsize = dn_ntohs(*(__u16 *)ptr);
349 if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
351 scp->addrrem = cb->src_port;
352 sk->state = TCP_ESTABLISHED;
354 scp->services_rem = cb->services;
355 scp->info_rem = cb->info;
356 scp->segsize_rem = cb->segsize;
358 if ((scp->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
359 scp->max_window = decnet_no_fc_max_cwnd;
362 unsigned char dlen = *skb->data;
363 if ((dlen <= 16) && (dlen <= skb->len)) {
364 scp->conndata_in.opt_optl = dlen;
365 memcpy(scp->conndata_in.opt_data, skb->data + 1, dlen);
368 dn_nsp_send_link(sk, DN_NOCHANGE, 0);
370 sk->state_change(sk);
377 static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
379 struct dn_scp *scp = DN_SK(sk);
381 if (scp->state == DN_CI) {
389 static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
391 struct dn_scp *scp = DN_SK(sk);
392 struct dn_skb_cb *cb = DN_SKB_CB(skb);
393 unsigned short reason;
398 reason = dn_ntohs(*(__u16 *)skb->data);
401 scp->discdata_in.opt_status = reason;
402 scp->discdata_in.opt_optl = 0;
403 memset(scp->discdata_in.opt_data, 0, 16);
406 unsigned char dlen = *skb->data;
407 if ((dlen <= 16) && (dlen <= skb->len)) {
408 scp->discdata_in.opt_optl = dlen;
409 memcpy(scp->discdata_in.opt_data, skb->data + 1, dlen);
413 scp->addrrem = cb->src_port;
414 sk->state = TCP_CLOSE;
422 sk->shutdown |= SHUTDOWN_MASK;
431 if (sk->socket->state != SS_UNCONNECTED)
432 sk->socket->state = SS_DISCONNECTING;
433 sk->state_change(sk);
436 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
437 scp->persist_fxn = dn_destroy_timer;
438 scp->persist = dn_nsp_persist(sk);
445 * disc_conf messages are also called no_resources or no_link
446 * messages depending upon the "reason" field.
448 static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
450 struct dn_scp *scp = DN_SK(sk);
451 unsigned short reason;
456 reason = dn_ntohs(*(__u16 *)skb->data);
458 sk->state = TCP_CLOSE;
465 if (reason == NSP_REASON_DC)
467 if (reason == NSP_REASON_NL)
474 sk->shutdown |= SHUTDOWN_MASK;
480 if (sk->socket->state != SS_UNCONNECTED)
481 sk->socket->state = SS_DISCONNECTING;
482 sk->state_change(sk);
485 scp->persist_fxn = dn_destroy_timer;
486 scp->persist = dn_nsp_persist(sk);
492 static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
494 struct dn_scp *scp = DN_SK(sk);
495 unsigned short segnum;
496 unsigned char lsflags;
499 char *ptr = skb->data;
500 unsigned char fctype = scp->services_rem & NSP_FC_MASK;
505 segnum = dn_ntohs(*(__u16 *)ptr);
507 lsflags = *(unsigned char *)ptr++;
511 * Here we ignore erronous packets which should really
512 * should cause a connection abort. It is not critical
518 if (seq_next(scp->numoth_rcv, segnum)) {
519 seq_add(&scp->numoth_rcv, 1);
520 switch(lsflags & 0x04) { /* FCVAL INT */
521 case 0x00: /* Normal Request */
522 switch(lsflags & 0x03) { /* FCVAL MOD */
523 case 0x00: /* Request count */
525 unsigned char p_fcval = -fcval;
526 if ((scp->flowrem_dat > p_fcval) &&
527 (fctype == NSP_FC_SCMC)) {
528 scp->flowrem_dat -= p_fcval;
530 } else if (fcval > 0) {
531 scp->flowrem_dat += fcval;
535 case 0x01: /* Stop outgoing data */
536 scp->flowrem_sw = DN_DONTSEND;
538 case 0x02: /* Ok to start again */
539 scp->flowrem_sw = DN_SEND;
544 case 0x04: /* Interrupt Request */
546 scp->flowrem_oth += fcval;
551 if (wake_up && !sk->dead)
552 sk->state_change(sk);
555 dn_nsp_send_oth_ack(sk);
562 * Copy of sock_queue_rcv_skb (from sock.h) without
563 * bh_lock_sock() (its already held when this is called) which
564 * also allows data and other data to be queued to a socket.
566 static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
570 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
571 number of warnings when compiling with -W --ANK
573 if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf) {
578 err = sk_filter(sk, skb, 0);
582 skb_set_owner_r(skb, sk);
583 skb_queue_tail(queue, skb);
585 /* This code only runs from BH or BH protected context.
586 * Therefore the plain read_lock is ok here. -DaveM
588 read_lock(&sk->callback_lock);
590 struct socket *sock = sk->socket;
591 wake_up_interruptible(sk->sleep);
592 if (sock && sock->fasync_list &&
593 !test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
594 __kill_fasync(sock->fasync_list, sig,
595 (sig == SIGURG) ? POLL_PRI : POLL_IN);
597 read_unlock(&sk->callback_lock);
602 static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
604 struct dn_scp *scp = DN_SK(sk);
605 unsigned short segnum;
606 struct dn_skb_cb *cb = DN_SKB_CB(skb);
612 cb->segnum = segnum = dn_ntohs(*(__u16 *)skb->data);
615 if (seq_next(scp->numoth_rcv, segnum)) {
617 if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
618 seq_add(&scp->numoth_rcv, 1);
619 scp->other_report = 0;
624 dn_nsp_send_oth_ack(sk);
630 static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
633 unsigned short segnum;
634 struct dn_skb_cb *cb = DN_SKB_CB(skb);
635 struct dn_scp *scp = DN_SK(sk);
640 cb->segnum = segnum = dn_ntohs(*(__u16 *)skb->data);
643 if (seq_next(scp->numdat_rcv, segnum)) {
644 if (dn_queue_skb(sk, skb, SIGIO, &sk->receive_queue) == 0) {
645 seq_add(&scp->numdat_rcv, 1);
649 if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
650 scp->flowloc_sw = DN_DONTSEND;
651 dn_nsp_send_link(sk, DN_DONTSEND, 0);
655 dn_nsp_send_data_ack(sk);
662 * If one of our conninit messages is returned, this function
663 * deals with it. It puts the socket into the NO_COMMUNICATION
666 static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
668 struct dn_scp *scp = DN_SK(sk);
670 if (scp->state == DN_CI) {
672 sk->state = TCP_CLOSE;
674 sk->state_change(sk);
680 static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
682 struct dn_skb_cb *cb = DN_SKB_CB(skb);
683 int ret = NET_RX_DROP;
685 /* Must not reply to returned packets */
686 if (cb->rt_flags & DN_RT_F_RTS)
689 if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
690 switch(cb->nsp_flags & 0x70) {
692 case 0x60: /* (Retransmitted) Connect Init */
693 dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
694 ret = NET_RX_SUCCESS;
696 case 0x20: /* Connect Confirm */
697 dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
698 ret = NET_RX_SUCCESS;
708 static int dn_nsp_rx_packet(struct sk_buff *skb)
710 struct dn_skb_cb *cb = DN_SKB_CB(skb);
711 struct sock *sk = NULL;
712 unsigned char *ptr = (unsigned char *)skb->data;
713 unsigned short reason = NSP_REASON_NL;
715 skb->h.raw = skb->data;
716 cb->nsp_flags = *ptr++;
718 if (decnet_debug_level & 2)
719 printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
724 if (cb->nsp_flags & 0x83)
728 * Returned packets...
729 * Swap src & dst and look up in the normal way.
731 if (cb->rt_flags & DN_RT_F_RTS) {
732 unsigned short tmp = cb->dst_port;
733 cb->dst_port = cb->src_port;
738 sk = dn_find_by_skb(skb);
743 * Filter out conninits and useless packet types
745 if ((cb->nsp_flags & 0x0c) == 0x08) {
746 switch(cb->nsp_flags & 0x70) {
748 case 0x70: /* Reserved */
749 case 0x50: /* Reserved, Phase II node init */
753 sk = dn_find_listener(skb, &reason);
762 * Grab the destination address.
764 cb->dst_port = *(unsigned short *)ptr;
769 * If not a connack, grab the source address too.
772 cb->src_port = *(unsigned short *)ptr;
778 * Find the socket to which this skb is destined.
780 sk = dn_find_by_skb(skb);
783 struct dn_scp *scp = DN_SK(sk);
787 scp->nsp_rxtshift = 0;
790 ret = NET_RX_SUCCESS;
791 if (decnet_debug_level & 8)
792 printk(KERN_DEBUG "NSP: 0x%02x 0x%02x 0x%04x 0x%04x %d\n",
793 (int)cb->rt_flags, (int)cb->nsp_flags,
794 (int)cb->src_port, (int)cb->dst_port,
795 (int)sk->lock.users);
796 if (sk->lock.users == 0)
797 ret = dn_nsp_backlog_rcv(sk, skb);
799 sk_add_backlog(sk, skb);
806 return dn_nsp_no_socket(skb, reason);
813 int dn_nsp_rx(struct sk_buff *skb)
815 return NF_HOOK(PF_DECnet, NF_DN_LOCAL_IN, skb, skb->dev, NULL, dn_nsp_rx_packet);
819 * This is the main receive routine for sockets. It is called
820 * from the above when the socket is not busy, and also from
821 * sock_release() when there is a backlog queued up.
823 int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
825 struct dn_scp *scp = DN_SK(sk);
826 struct dn_skb_cb *cb = DN_SKB_CB(skb);
828 if (cb->rt_flags & DN_RT_F_RTS) {
829 dn_returned_conn_init(sk, skb);
830 return NET_RX_SUCCESS;
836 if ((cb->nsp_flags & 0x0c) == 0x08) {
837 switch(cb->nsp_flags & 0x70) {
840 dn_nsp_conn_init(sk, skb);
843 dn_nsp_conn_conf(sk, skb);
846 dn_nsp_disc_init(sk, skb);
849 dn_nsp_disc_conf(sk, skb);
853 } else if (cb->nsp_flags == 0x24) {
855 * Special for connacks, 'cos they don't have
856 * ack data or ack otherdata info.
858 dn_nsp_conn_ack(sk, skb);
862 /* both data and ack frames can kick a CC socket into RUN */
863 if ((scp->state == DN_CC) && !sk->dead) {
865 sk->state = TCP_ESTABLISHED;
866 sk->state_change(sk);
869 if ((cb->nsp_flags & 0x1c) == 0)
871 if (cb->nsp_flags == 0x04)
875 * Read out ack data here, this applies equally
876 * to data, other data, link serivce and both
877 * ack data and ack otherdata.
879 dn_process_ack(sk, skb, other);
882 * If we've some sort of data here then call a
883 * suitable routine for dealing with it, otherwise
884 * the packet is an ack and can be discarded.
886 if ((cb->nsp_flags & 0x0c) == 0) {
888 if (scp->state != DN_RUN)
891 switch(cb->nsp_flags) {
893 dn_nsp_linkservice(sk, skb);
896 dn_nsp_otherdata(sk, skb);
899 dn_nsp_data(sk, skb);
902 } else { /* Ack, chuck it out here */
908 return NET_RX_SUCCESS;