1 // SPDX-License-Identifier: GPL-2.0-or-later
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
27 * Patrick Caulfield: Checking conninits for correctness & sending of error
29 * Steve Whitehouse: Added backlog congestion level return codes.
31 * Steve Whitehouse: Added flow control support (outbound)
32 * Steve Whitehouse: Prepare for nonlinear skbs
35 /******************************************************************************
36 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
38 *******************************************************************************/
40 #include <linux/errno.h>
41 #include <linux/types.h>
42 #include <linux/socket.h>
44 #include <linux/kernel.h>
45 #include <linux/timer.h>
46 #include <linux/string.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/inet.h>
51 #include <linux/route.h>
52 #include <linux/slab.h>
54 #include <net/tcp_states.h>
55 #include <linux/fcntl.h>
57 #include <linux/termios.h>
58 #include <linux/interrupt.h>
59 #include <linux/proc_fs.h>
60 #include <linux/stat.h>
61 #include <linux/init.h>
62 #include <linux/poll.h>
63 #include <linux/netfilter_decnet.h>
64 #include <net/neighbour.h>
67 #include <net/dn_nsp.h>
68 #include <net/dn_dev.h>
69 #include <net/dn_route.h>
71 extern int decnet_log_martians;
73 static void dn_log_martian(struct sk_buff *skb, const char *msg)
75 if (decnet_log_martians) {
76 char *devname = skb->dev ? skb->dev->name : "???";
77 struct dn_skb_cb *cb = DN_SKB_CB(skb);
78 net_info_ratelimited("DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n",
82 le16_to_cpu(cb->src_port),
83 le16_to_cpu(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 = DN_SK(sk);
95 unsigned short type = ((ack >> 12) & 0x0003);
99 case 0: /* ACK - Data */
100 if (dn_after(ack, scp->ackrcv_dat)) {
101 scp->ackrcv_dat = ack & 0x0fff;
102 wakeup |= dn_nsp_check_xmit_queue(sk, skb,
103 &scp->data_xmit_queue,
107 case 1: /* NAK - Data */
109 case 2: /* ACK - OtherData */
110 if (dn_after(ack, scp->ackrcv_oth)) {
111 scp->ackrcv_oth = ack & 0x0fff;
112 wakeup |= dn_nsp_check_xmit_queue(sk, skb,
113 &scp->other_xmit_queue,
117 case 3: /* NAK - OtherData */
121 if (wakeup && !sock_flag(sk, SOCK_DEAD))
122 sk->sk_state_change(sk);
126 * This function is a universal ack processor.
128 static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
130 __le16 *ptr = (__le16 *)skb->data;
137 if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
141 if ((ack & 0x4000) == 0) {
144 dn_ack(sk, skb, ack);
151 if ((ack = le16_to_cpu(*ptr)) & 0x8000) {
154 if ((ack & 0x4000) == 0) {
157 dn_ack(sk, skb, ack);
166 * dn_check_idf - Check an image data field format is correct.
167 * @pptr: Pointer to pointer to image data
168 * @len: Pointer to length of image data
169 * @max: The maximum allowed length of the data in the image data field
170 * @follow_on: Check that this many bytes exist beyond the end of the image data
172 * Returns: 0 if ok, -1 on error
174 static inline int dn_check_idf(unsigned char **pptr, int *len, unsigned char max, unsigned char follow_on)
176 unsigned char *ptr = *pptr;
177 unsigned char flen = *ptr++;
182 if ((flen + follow_on) > *len)
191 * Table of reason codes to pass back to node which sent us a badly
192 * formed message, plus text messages for the log. A zero entry in
193 * the reason field means "don't reply" otherwise a disc init is sent with
194 * the specified reason code.
197 unsigned short reason;
200 { 0, "CI: Truncated message" },
201 { NSP_REASON_ID, "CI: Destination username error" },
202 { NSP_REASON_ID, "CI: Destination username type" },
203 { NSP_REASON_US, "CI: Source username error" },
204 { 0, "CI: Truncated at menuver" },
205 { 0, "CI: Truncated before access or user data" },
206 { NSP_REASON_IO, "CI: Access data format error" },
207 { NSP_REASON_IO, "CI: User data format error" }
211 * This function uses a slightly different lookup method
212 * to find its sockets, since it searches on object name/number
213 * rather than port numbers. Various tests are done to ensure that
214 * the incoming data is in the correct format before it is queued to
217 static struct sock *dn_find_listener(struct sk_buff *skb, unsigned short *reason)
219 struct dn_skb_cb *cb = DN_SKB_CB(skb);
220 struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
221 struct sockaddr_dn dstaddr;
222 struct sockaddr_dn srcaddr;
223 unsigned char type = 0;
229 unsigned char menuver;
231 memset(&dstaddr, 0, sizeof(struct sockaddr_dn));
232 memset(&srcaddr, 0, sizeof(struct sockaddr_dn));
235 * 1. Decode & remove message header
237 cb->src_port = msg->srcaddr;
238 cb->dst_port = msg->dstaddr;
239 cb->services = msg->services;
240 cb->info = msg->info;
241 cb->segsize = le16_to_cpu(msg->segsize);
243 if (!pskb_may_pull(skb, sizeof(*msg)))
246 skb_pull(skb, sizeof(*msg));
252 * 2. Check destination end username format
254 dstlen = dn_username2sockaddr(ptr, len, &dstaddr, &type);
267 * 3. Check source end username format
269 srclen = dn_username2sockaddr(ptr, len, &srcaddr, &type);
285 * 4. Check that optional data actually exists if menuver says it does
288 if ((menuver & (DN_MENUVER_ACC | DN_MENUVER_USR)) && (len < 1))
292 * 5. Check optional access data format
295 if (menuver & DN_MENUVER_ACC) {
296 if (dn_check_idf(&ptr, &len, 39, 1))
298 if (dn_check_idf(&ptr, &len, 39, 1))
300 if (dn_check_idf(&ptr, &len, 39, (menuver & DN_MENUVER_USR) ? 1 : 0))
305 * 6. Check optional user data format
308 if (menuver & DN_MENUVER_USR) {
309 if (dn_check_idf(&ptr, &len, 16, 0))
314 * 7. Look up socket based on destination end username
316 return dn_sklist_find_listener(&dstaddr);
318 dn_log_martian(skb, ci_err_table[err].text);
319 *reason = ci_err_table[err].reason;
324 static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
326 if (sk_acceptq_is_full(sk)) {
331 sk_acceptq_added(sk);
332 skb_queue_tail(&sk->sk_receive_queue, skb);
333 sk->sk_state_change(sk);
336 static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
338 struct dn_skb_cb *cb = DN_SKB_CB(skb);
339 struct dn_scp *scp = DN_SK(sk);
346 cb->services = *ptr++;
348 cb->segsize = le16_to_cpu(*(__le16 *)ptr);
350 if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
352 scp->addrrem = cb->src_port;
353 sk->sk_state = TCP_ESTABLISHED;
355 scp->services_rem = cb->services;
356 scp->info_rem = cb->info;
357 scp->segsize_rem = cb->segsize;
359 if ((scp->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
360 scp->max_window = decnet_no_fc_max_cwnd;
363 u16 dlen = *skb->data;
364 if ((dlen <= 16) && (dlen <= skb->len)) {
365 scp->conndata_in.opt_optl = cpu_to_le16(dlen);
366 skb_copy_from_linear_data_offset(skb, 1,
367 scp->conndata_in.opt_data, 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 = le16_to_cpu(*(__le16 *)skb->data);
403 scp->discdata_in.opt_status = cpu_to_le16(reason);
404 scp->discdata_in.opt_optl = 0;
405 memset(scp->discdata_in.opt_data, 0, 16);
408 u16 dlen = *skb->data;
409 if ((dlen <= 16) && (dlen <= skb->len)) {
410 scp->discdata_in.opt_optl = cpu_to_le16(dlen);
411 skb_copy_from_linear_data_offset(skb, 1, scp->discdata_in.opt_data, dlen);
415 scp->addrrem = cb->src_port;
416 sk->sk_state = TCP_CLOSE;
418 switch (scp->state) {
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 = le16_to_cpu(*(__le16 *)skb->data);
469 sk->sk_state = TCP_CLOSE;
471 switch (scp->state) {
476 if (reason == NSP_REASON_DC)
478 if (reason == NSP_REASON_NL)
485 sk->sk_shutdown |= SHUTDOWN_MASK;
491 if (!sock_flag(sk, SOCK_DEAD)) {
492 if (sk->sk_socket->state != SS_UNCONNECTED)
493 sk->sk_socket->state = SS_DISCONNECTING;
494 sk->sk_state_change(sk);
497 scp->persist_fxn = dn_destroy_timer;
498 scp->persist = dn_nsp_persist(sk);
504 static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
506 struct dn_scp *scp = DN_SK(sk);
507 unsigned short segnum;
508 unsigned char lsflags;
511 char *ptr = skb->data;
512 unsigned char fctype = scp->services_rem & NSP_FC_MASK;
517 segnum = le16_to_cpu(*(__le16 *)ptr);
519 lsflags = *(unsigned char *)ptr++;
523 * Here we ignore erroneous packets which should really
524 * should cause a connection abort. It is not critical
530 if (seq_next(scp->numoth_rcv, segnum)) {
531 seq_add(&scp->numoth_rcv, 1);
532 switch(lsflags & 0x04) { /* FCVAL INT */
533 case 0x00: /* Normal Request */
534 switch(lsflags & 0x03) { /* FCVAL MOD */
535 case 0x00: /* Request count */
537 unsigned char p_fcval = -fcval;
538 if ((scp->flowrem_dat > p_fcval) &&
539 (fctype == NSP_FC_SCMC)) {
540 scp->flowrem_dat -= p_fcval;
542 } else if (fcval > 0) {
543 scp->flowrem_dat += fcval;
547 case 0x01: /* Stop outgoing data */
548 scp->flowrem_sw = DN_DONTSEND;
550 case 0x02: /* Ok to start again */
551 scp->flowrem_sw = DN_SEND;
556 case 0x04: /* Interrupt Request */
558 scp->flowrem_oth += fcval;
563 if (wake_up && !sock_flag(sk, SOCK_DEAD))
564 sk->sk_state_change(sk);
567 dn_nsp_send_oth_ack(sk);
574 * Copy of sock_queue_rcv_skb (from sock.h) without
575 * bh_lock_sock() (its already held when this is called) which
576 * also allows data and other data to be queued to a socket.
578 static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
582 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
583 number of warnings when compiling with -W --ANK
585 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
586 (unsigned int)sk->sk_rcvbuf) {
591 err = sk_filter(sk, skb);
595 skb_set_owner_r(skb, sk);
596 skb_queue_tail(queue, skb);
598 if (!sock_flag(sk, SOCK_DEAD))
599 sk->sk_data_ready(sk);
604 static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
606 struct dn_scp *scp = DN_SK(sk);
607 unsigned short segnum;
608 struct dn_skb_cb *cb = DN_SKB_CB(skb);
614 cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
617 if (seq_next(scp->numoth_rcv, segnum)) {
619 if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
620 seq_add(&scp->numoth_rcv, 1);
621 scp->other_report = 0;
626 dn_nsp_send_oth_ack(sk);
632 static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
635 unsigned short segnum;
636 struct dn_skb_cb *cb = DN_SKB_CB(skb);
637 struct dn_scp *scp = DN_SK(sk);
642 cb->segnum = segnum = le16_to_cpu(*(__le16 *)skb->data);
645 if (seq_next(scp->numdat_rcv, segnum)) {
646 if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
647 seq_add(&scp->numdat_rcv, 1);
651 if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
652 scp->flowloc_sw = DN_DONTSEND;
653 dn_nsp_send_link(sk, DN_DONTSEND, 0);
657 dn_nsp_send_data_ack(sk);
664 * If one of our conninit messages is returned, this function
665 * deals with it. It puts the socket into the NO_COMMUNICATION
668 static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
670 struct dn_scp *scp = DN_SK(sk);
672 if (scp->state == DN_CI) {
674 sk->sk_state = TCP_CLOSE;
675 if (!sock_flag(sk, SOCK_DEAD))
676 sk->sk_state_change(sk);
682 static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
684 struct dn_skb_cb *cb = DN_SKB_CB(skb);
685 int ret = NET_RX_DROP;
687 /* Must not reply to returned packets */
688 if (cb->rt_flags & DN_RT_F_RTS)
691 if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
692 switch (cb->nsp_flags & 0x70) {
694 case 0x60: /* (Retransmitted) Connect Init */
695 dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
696 ret = NET_RX_SUCCESS;
698 case 0x20: /* Connect Confirm */
699 dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
700 ret = NET_RX_SUCCESS;
710 static int dn_nsp_rx_packet(struct net *net, struct sock *sk2,
713 struct dn_skb_cb *cb = DN_SKB_CB(skb);
714 struct sock *sk = NULL;
715 unsigned char *ptr = (unsigned char *)skb->data;
716 unsigned short reason = NSP_REASON_NL;
718 if (!pskb_may_pull(skb, 2))
721 skb_reset_transport_header(skb);
722 cb->nsp_flags = *ptr++;
724 if (decnet_debug_level & 2)
725 printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
727 if (cb->nsp_flags & 0x83)
731 * Filter out conninits and useless packet types
733 if ((cb->nsp_flags & 0x0c) == 0x08) {
734 switch (cb->nsp_flags & 0x70) {
736 case 0x70: /* Reserved */
737 case 0x50: /* Reserved, Phase II node init */
741 if (unlikely(cb->rt_flags & DN_RT_F_RTS))
743 sk = dn_find_listener(skb, &reason);
748 if (!pskb_may_pull(skb, 3))
752 * Grab the destination address.
754 cb->dst_port = *(__le16 *)ptr;
759 * If not a connack, grab the source address too.
761 if (pskb_may_pull(skb, 5)) {
762 cb->src_port = *(__le16 *)ptr;
768 * Returned packets...
769 * Swap src & dst and look up in the normal way.
771 if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
772 swap(cb->dst_port, cb->src_port);
773 swap(cb->dst, cb->src);
777 * Find the socket to which this skb is destined.
779 sk = dn_find_by_skb(skb);
782 struct dn_scp *scp = DN_SK(sk);
785 scp->nsp_rxtshift = 0;
788 * We linearize everything except data segments here.
790 if (cb->nsp_flags & ~0x60) {
791 if (unlikely(skb_linearize(skb)))
795 return sk_receive_skb(sk, skb, 0);
798 return dn_nsp_no_socket(skb, reason);
805 int dn_nsp_rx(struct sk_buff *skb)
807 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_IN,
808 &init_net, NULL, skb, skb->dev, NULL,
813 * This is the main receive routine for sockets. It is called
814 * from the above when the socket is not busy, and also from
815 * sock_release() when there is a backlog queued up.
817 int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
819 struct dn_scp *scp = DN_SK(sk);
820 struct dn_skb_cb *cb = DN_SKB_CB(skb);
822 if (cb->rt_flags & DN_RT_F_RTS) {
823 if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
824 dn_returned_conn_init(sk, skb);
827 return NET_RX_SUCCESS;
833 if ((cb->nsp_flags & 0x0c) == 0x08) {
834 switch (cb->nsp_flags & 0x70) {
837 dn_nsp_conn_init(sk, skb);
840 dn_nsp_conn_conf(sk, skb);
843 dn_nsp_disc_init(sk, skb);
846 dn_nsp_disc_conf(sk, skb);
850 } else if (cb->nsp_flags == 0x24) {
852 * Special for connacks, 'cos they don't have
853 * ack data or ack otherdata info.
855 dn_nsp_conn_ack(sk, skb);
859 /* both data and ack frames can kick a CC socket into RUN */
860 if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
862 sk->sk_state = TCP_ESTABLISHED;
863 sk->sk_state_change(sk);
866 if ((cb->nsp_flags & 0x1c) == 0)
868 if (cb->nsp_flags == 0x04)
872 * Read out ack data here, this applies equally
873 * to data, other data, link service and both
874 * ack data and ack otherdata.
876 dn_process_ack(sk, skb, other);
879 * If we've some sort of data here then call a
880 * suitable routine for dealing with it, otherwise
881 * the packet is an ack and can be discarded.
883 if ((cb->nsp_flags & 0x0c) == 0) {
885 if (scp->state != DN_RUN)
888 switch (cb->nsp_flags) {
890 dn_nsp_linkservice(sk, skb);
893 dn_nsp_otherdata(sk, skb);
896 dn_nsp_data(sk, skb);
899 } else { /* Ack, chuck it out here */
905 return NET_RX_SUCCESS;