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 (Output)
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: Added changes from Eduardo Serrat's patches.
17 * Steve Whitehouse: Now conninits have the "return" bit set.
18 * Steve Whitehouse: Fixes to check alloc'd skbs are non NULL!
19 * Moved output state machine into one function
20 * Steve Whitehouse: New output state machine
21 * Paul Koning: Connect Confirm message fix.
22 * Eduardo Serrat: Fix to stop dn_nsp_do_disc() sending malformed packets.
23 * Steve Whitehouse: dn_nsp_output() and friends needed a spring clean
24 * Steve Whitehouse: Moved dn_nsp_send() in here from route.h
27 /******************************************************************************
28 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
30 *******************************************************************************/
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/socket.h>
36 #include <linux/kernel.h>
37 #include <linux/timer.h>
38 #include <linux/string.h>
39 #include <linux/sockios.h>
40 #include <linux/net.h>
41 #include <linux/netdevice.h>
42 #include <linux/inet.h>
43 #include <linux/route.h>
44 #include <linux/slab.h>
46 #include <linux/fcntl.h>
48 #include <linux/termios.h>
49 #include <linux/interrupt.h>
50 #include <linux/proc_fs.h>
51 #include <linux/stat.h>
52 #include <linux/init.h>
53 #include <linux/poll.h>
54 #include <linux/if_packet.h>
55 #include <net/neighbour.h>
59 #include <net/dn_nsp.h>
60 #include <net/dn_dev.h>
61 #include <net/dn_route.h>
64 static int nsp_backoff[NSP_MAXRXTSHIFT + 1] = { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
66 static void dn_nsp_send(struct sk_buff *skb)
68 struct sock *sk = skb->sk;
69 struct dn_scp *scp = DN_SK(sk);
70 struct dst_entry *dst;
73 skb_reset_transport_header(skb);
76 dst = sk_dst_check(sk, 0);
79 skb_dst_set(skb, dst);
80 dst_output(&init_net, skb->sk, skb);
84 memset(&fld, 0, sizeof(fld));
85 fld.flowidn_oif = sk->sk_bound_dev_if;
86 fld.saddr = dn_saddr2dn(&scp->addr);
87 fld.daddr = dn_saddr2dn(&scp->peer);
88 dn_sk_ports_copy(&fld, scp);
89 fld.flowidn_proto = DNPROTO_NSP;
90 if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, 0) == 0) {
92 sk->sk_route_caps = dst->dev->features;
96 sk->sk_err = EHOSTUNREACH;
97 if (!sock_flag(sk, SOCK_DEAD))
98 sk->sk_state_change(sk);
103 * If sk == NULL, then we assume that we are supposed to be making
104 * a routing layer skb. If sk != NULL, then we are supposed to be
105 * creating an skb for the NSP layer.
107 * The eventual aim is for each socket to have a cached header size
108 * for its outgoing packets, and to set hdr from this when sk != NULL.
110 struct sk_buff *dn_alloc_skb(struct sock *sk, int size, gfp_t pri)
115 if ((skb = alloc_skb(size + hdr, pri)) == NULL)
118 skb->protocol = htons(ETH_P_DNA_RT);
119 skb->pkt_type = PACKET_OUTGOING;
122 skb_set_owner_w(skb, sk);
124 skb_reserve(skb, hdr);
130 * Calculate persist timer based upon the smoothed round
131 * trip time and the variance. Backoff according to the
132 * nsp_backoff[] array.
134 unsigned long dn_nsp_persist(struct sock *sk)
136 struct dn_scp *scp = DN_SK(sk);
138 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
140 t *= nsp_backoff[scp->nsp_rxtshift];
143 if (t > (600*HZ)) t = (600*HZ);
145 if (scp->nsp_rxtshift < NSP_MAXRXTSHIFT)
148 /* printk(KERN_DEBUG "rxtshift %lu, t=%lu\n", scp->nsp_rxtshift, t); */
154 * This is called each time we get an estimate for the rtt
157 static void dn_nsp_rtt(struct sock *sk, long rtt)
159 struct dn_scp *scp = DN_SK(sk);
160 long srtt = (long)scp->nsp_srtt;
161 long rttvar = (long)scp->nsp_rttvar;
165 * If the jiffies clock flips over in the middle of timestamp
166 * gathering this value might turn out negative, so we make sure
167 * that is it always positive here.
172 * Add new rtt to smoothed average
174 delta = ((rtt << 3) - srtt);
175 srtt += (delta >> 3);
177 scp->nsp_srtt = (unsigned long)srtt;
182 * Add new rtt variance to smoothed varience
185 rttvar += ((((delta>0)?(delta):(-delta)) - rttvar) >> 2);
187 scp->nsp_rttvar = (unsigned long)rttvar;
191 /* printk(KERN_DEBUG "srtt=%lu rttvar=%lu\n", scp->nsp_srtt, scp->nsp_rttvar); */
195 * dn_nsp_clone_and_send - Send a data packet by cloning it
196 * @skb: The packet to clone and transmit
197 * @gfp: memory allocation flag
199 * Clone a queued data or other data packet and transmit it.
201 * Returns: The number of times the packet has been sent previously
203 static inline unsigned int dn_nsp_clone_and_send(struct sk_buff *skb,
206 struct dn_skb_cb *cb = DN_SKB_CB(skb);
207 struct sk_buff *skb2;
210 if ((skb2 = skb_clone(skb, gfp)) != NULL) {
211 ret = cb->xmit_count;
222 * dn_nsp_output - Try and send something from socket queues
223 * @sk: The socket whose queues are to be investigated
225 * Try and send the packet on the end of the data and other data queues.
226 * Other data gets priority over data, and if we retransmit a packet we
227 * reduce the window by dividing it in two.
230 void dn_nsp_output(struct sock *sk)
232 struct dn_scp *scp = DN_SK(sk);
234 unsigned int reduce_win = 0;
237 * First we check for otherdata/linkservice messages
239 if ((skb = skb_peek(&scp->other_xmit_queue)) != NULL)
240 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
243 * If we may not send any data, we don't.
244 * If we are still trying to get some other data down the
245 * channel, we don't try and send any data.
247 if (reduce_win || (scp->flowrem_sw != DN_SEND))
250 if ((skb = skb_peek(&scp->data_xmit_queue)) != NULL)
251 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
254 * If we've sent any frame more than once, we cut the
255 * send window size in half. There is always a minimum
256 * window size of one available.
260 scp->snd_window >>= 1;
261 if (scp->snd_window < NSP_MIN_WINDOW)
262 scp->snd_window = NSP_MIN_WINDOW;
266 int dn_nsp_xmit_timeout(struct sock *sk)
268 struct dn_scp *scp = DN_SK(sk);
272 if (!skb_queue_empty(&scp->data_xmit_queue) ||
273 !skb_queue_empty(&scp->other_xmit_queue))
274 scp->persist = dn_nsp_persist(sk);
279 static inline __le16 *dn_mk_common_header(struct dn_scp *scp, struct sk_buff *skb, unsigned char msgflag, int len)
281 unsigned char *ptr = skb_push(skb, len);
286 *((__le16 *)ptr) = scp->addrrem;
288 *((__le16 *)ptr) = scp->addrloc;
290 return (__le16 __force *)ptr;
293 static __le16 *dn_mk_ack_header(struct sock *sk, struct sk_buff *skb, unsigned char msgflag, int hlen, int other)
295 struct dn_scp *scp = DN_SK(sk);
296 unsigned short acknum = scp->numdat_rcv & 0x0FFF;
297 unsigned short ackcrs = scp->numoth_rcv & 0x0FFF;
302 scp->ackxmt_dat = acknum;
303 scp->ackxmt_oth = ackcrs;
307 /* If this is an "other data/ack" message, swap acknum and ackcrs */
309 swap(acknum, ackcrs);
311 /* Set "cross subchannel" bit in ackcrs */
314 ptr = dn_mk_common_header(scp, skb, msgflag, hlen);
316 *ptr++ = cpu_to_le16(acknum);
317 *ptr++ = cpu_to_le16(ackcrs);
322 static __le16 *dn_nsp_mk_data_header(struct sock *sk, struct sk_buff *skb, int oth)
324 struct dn_scp *scp = DN_SK(sk);
325 struct dn_skb_cb *cb = DN_SKB_CB(skb);
326 __le16 *ptr = dn_mk_ack_header(sk, skb, cb->nsp_flags, 11, oth);
329 cb->segnum = scp->numoth;
330 seq_add(&scp->numoth, 1);
332 cb->segnum = scp->numdat;
333 seq_add(&scp->numdat, 1);
335 *(ptr++) = cpu_to_le16(cb->segnum);
340 void dn_nsp_queue_xmit(struct sock *sk, struct sk_buff *skb,
343 struct dn_scp *scp = DN_SK(sk);
344 struct dn_skb_cb *cb = DN_SKB_CB(skb);
345 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
348 dn_nsp_mk_data_header(sk, skb, oth);
351 * Slow start: If we have been idle for more than
352 * one RTT, then reset window to min size.
354 if ((jiffies - scp->stamp) > t)
355 scp->snd_window = NSP_MIN_WINDOW;
358 skb_queue_tail(&scp->other_xmit_queue, skb);
360 skb_queue_tail(&scp->data_xmit_queue, skb);
362 if (scp->flowrem_sw != DN_SEND)
365 dn_nsp_clone_and_send(skb, gfp);
369 int dn_nsp_check_xmit_queue(struct sock *sk, struct sk_buff *skb, struct sk_buff_head *q, unsigned short acknum)
371 struct dn_skb_cb *cb = DN_SKB_CB(skb);
372 struct dn_scp *scp = DN_SK(sk);
373 struct sk_buff *skb2, *n, *ack = NULL;
376 unsigned long reftime = cb->stamp;
377 unsigned long pkttime;
378 unsigned short xmit_count;
379 unsigned short segnum;
381 skb_queue_walk_safe(q, skb2, n) {
382 struct dn_skb_cb *cb2 = DN_SKB_CB(skb2);
384 if (dn_before_or_equal(cb2->segnum, acknum))
387 /* printk(KERN_DEBUG "ack: %s %04x %04x\n", ack ? "ACK" : "SKIP", (int)cb2->segnum, (int)acknum); */
392 /* printk(KERN_DEBUG "check_xmit_queue: %04x, %d\n", acknum, cb2->xmit_count); */
394 /* Does _last_ packet acked have xmit_count > 1 */
396 /* Remember to wake up the sending process */
398 /* Keep various statistics */
399 pkttime = cb2->stamp;
400 xmit_count = cb2->xmit_count;
401 segnum = cb2->segnum;
402 /* Remove and drop ack'ed packet */
408 * We don't expect to see acknowledgements for packets we
411 WARN_ON(xmit_count == 0);
414 * If the packet has only been sent once, we can use it
415 * to calculate the RTT and also open the window a little
418 if (xmit_count == 1) {
419 if (dn_equal(segnum, acknum))
420 dn_nsp_rtt(sk, (long)(pkttime - reftime));
422 if (scp->snd_window < scp->max_window)
427 * Packet has been sent more than once. If this is the last
428 * packet to be acknowledged then we want to send the next
429 * packet in the send queue again (assumes the remote host does
430 * go-back-N error control).
442 void dn_nsp_send_data_ack(struct sock *sk)
444 struct sk_buff *skb = NULL;
446 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
450 dn_mk_ack_header(sk, skb, 0x04, 9, 0);
454 void dn_nsp_send_oth_ack(struct sock *sk)
456 struct sk_buff *skb = NULL;
458 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
462 dn_mk_ack_header(sk, skb, 0x14, 9, 1);
467 void dn_send_conn_ack (struct sock *sk)
469 struct dn_scp *scp = DN_SK(sk);
470 struct sk_buff *skb = NULL;
471 struct nsp_conn_ack_msg *msg;
473 if ((skb = dn_alloc_skb(sk, 3, sk->sk_allocation)) == NULL)
476 msg = skb_put(skb, 3);
478 msg->dstaddr = scp->addrrem;
483 static int dn_nsp_retrans_conn_conf(struct sock *sk)
485 struct dn_scp *scp = DN_SK(sk);
487 if (scp->state == DN_CC)
488 dn_send_conn_conf(sk, GFP_ATOMIC);
493 void dn_send_conn_conf(struct sock *sk, gfp_t gfp)
495 struct dn_scp *scp = DN_SK(sk);
496 struct sk_buff *skb = NULL;
497 struct nsp_conn_init_msg *msg;
498 __u8 len = (__u8)le16_to_cpu(scp->conndata_out.opt_optl);
500 if ((skb = dn_alloc_skb(sk, 50 + len, gfp)) == NULL)
503 msg = skb_put(skb, sizeof(*msg));
505 msg->dstaddr = scp->addrrem;
506 msg->srcaddr = scp->addrloc;
507 msg->services = scp->services_loc;
508 msg->info = scp->info_loc;
509 msg->segsize = cpu_to_le16(scp->segsize_loc);
511 skb_put_u8(skb, len);
514 skb_put_data(skb, scp->conndata_out.opt_data, len);
519 scp->persist = dn_nsp_persist(sk);
520 scp->persist_fxn = dn_nsp_retrans_conn_conf;
524 static __inline__ void dn_nsp_do_disc(struct sock *sk, unsigned char msgflg,
525 unsigned short reason, gfp_t gfp,
526 struct dst_entry *dst,
527 int ddl, unsigned char *dd, __le16 rem, __le16 loc)
529 struct sk_buff *skb = NULL;
530 int size = 7 + ddl + ((msgflg == NSP_DISCINIT) ? 1 : 0);
533 if ((dst == NULL) || (rem == 0)) {
534 net_dbg_ratelimited("DECnet: dn_nsp_do_disc: BUG! Please report this to SteveW@ACM.org rem=%u dst=%p\n",
535 le16_to_cpu(rem), dst);
539 if ((skb = dn_alloc_skb(sk, size, gfp)) == NULL)
542 msg = skb_put(skb, size);
544 *(__le16 *)msg = rem;
546 *(__le16 *)msg = loc;
548 *(__le16 *)msg = cpu_to_le16(reason);
550 if (msgflg == NSP_DISCINIT)
554 memcpy(msg, dd, ddl);
558 * This doesn't go via the dn_nsp_send() function since we need
559 * to be able to send disc packets out which have no socket
562 skb_dst_set(skb, dst_clone(dst));
563 dst_output(&init_net, skb->sk, skb);
567 void dn_nsp_send_disc(struct sock *sk, unsigned char msgflg,
568 unsigned short reason, gfp_t gfp)
570 struct dn_scp *scp = DN_SK(sk);
573 if (msgflg == NSP_DISCINIT)
574 ddl = le16_to_cpu(scp->discdata_out.opt_optl);
577 reason = le16_to_cpu(scp->discdata_out.opt_status);
579 dn_nsp_do_disc(sk, msgflg, reason, gfp, __sk_dst_get(sk), ddl,
580 scp->discdata_out.opt_data, scp->addrrem, scp->addrloc);
584 void dn_nsp_return_disc(struct sk_buff *skb, unsigned char msgflg,
585 unsigned short reason)
587 struct dn_skb_cb *cb = DN_SKB_CB(skb);
589 gfp_t gfp = GFP_ATOMIC;
591 dn_nsp_do_disc(NULL, msgflg, reason, gfp, skb_dst(skb), ddl,
592 NULL, cb->src_port, cb->dst_port);
596 void dn_nsp_send_link(struct sock *sk, unsigned char lsflags, char fcval)
598 struct dn_scp *scp = DN_SK(sk);
601 gfp_t gfp = GFP_ATOMIC;
603 if ((skb = dn_alloc_skb(sk, DN_MAX_NSP_DATA_HEADER + 2, gfp)) == NULL)
606 skb_reserve(skb, DN_MAX_NSP_DATA_HEADER);
607 ptr = skb_put(skb, 2);
608 DN_SKB_CB(skb)->nsp_flags = 0x10;
612 dn_nsp_queue_xmit(sk, skb, gfp, 1);
614 scp->persist = dn_nsp_persist(sk);
615 scp->persist_fxn = dn_nsp_xmit_timeout;
618 static int dn_nsp_retrans_conninit(struct sock *sk)
620 struct dn_scp *scp = DN_SK(sk);
622 if (scp->state == DN_CI)
623 dn_nsp_send_conninit(sk, NSP_RCI);
628 void dn_nsp_send_conninit(struct sock *sk, unsigned char msgflg)
630 struct dn_scp *scp = DN_SK(sk);
631 struct nsp_conn_init_msg *msg;
633 unsigned char menuver;
634 struct dn_skb_cb *cb;
635 unsigned char type = 1;
636 gfp_t allocation = (msgflg == NSP_CI) ? sk->sk_allocation : GFP_ATOMIC;
637 struct sk_buff *skb = dn_alloc_skb(sk, 200, allocation);
643 msg = skb_put(skb, sizeof(*msg));
645 msg->msgflg = msgflg;
646 msg->dstaddr = 0x0000; /* Remote Node will assign it*/
648 msg->srcaddr = scp->addrloc;
649 msg->services = scp->services_loc; /* Requested flow control */
650 msg->info = scp->info_loc; /* Version Number */
651 msg->segsize = cpu_to_le16(scp->segsize_loc); /* Max segment size */
653 if (scp->peer.sdn_objnum)
656 skb_put(skb, dn_sockaddr2username(&scp->peer,
657 skb_tail_pointer(skb), type));
658 skb_put(skb, dn_sockaddr2username(&scp->addr,
659 skb_tail_pointer(skb), 2));
661 menuver = DN_MENUVER_ACC | DN_MENUVER_USR;
662 if (scp->peer.sdn_flags & SDF_PROXY)
663 menuver |= DN_MENUVER_PRX;
664 if (scp->peer.sdn_flags & SDF_UICPROXY)
665 menuver |= DN_MENUVER_UIC;
667 skb_put_u8(skb, menuver); /* Menu Version */
669 aux = scp->accessdata.acc_userl;
670 skb_put_u8(skb, aux);
672 skb_put_data(skb, scp->accessdata.acc_user, aux);
674 aux = scp->accessdata.acc_passl;
675 skb_put_u8(skb, aux);
677 skb_put_data(skb, scp->accessdata.acc_pass, aux);
679 aux = scp->accessdata.acc_accl;
680 skb_put_u8(skb, aux);
682 skb_put_data(skb, scp->accessdata.acc_acc, aux);
684 aux = (__u8)le16_to_cpu(scp->conndata_out.opt_optl);
685 skb_put_u8(skb, aux);
687 skb_put_data(skb, scp->conndata_out.opt_data, aux);
689 scp->persist = dn_nsp_persist(sk);
690 scp->persist_fxn = dn_nsp_retrans_conninit;
692 cb->rt_flags = DN_RT_F_RQR;