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
26 /******************************************************************************
27 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
29 This program is free software; you can redistribute it and/or modify
30 it under the terms of the GNU General Public License as published by
31 the Free Software Foundation; either version 2 of the License, or
34 This program is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU General Public License for more details.
38 *******************************************************************************/
40 #include <linux/errno.h>
41 #include <linux/types.h>
42 #include <linux/socket.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/timer.h>
47 #include <linux/string.h>
48 #include <linux/sockios.h>
49 #include <linux/net.h>
50 #include <linux/netdevice.h>
51 #include <linux/inet.h>
52 #include <linux/route.h>
54 #include <asm/segment.h>
55 #include <asm/system.h>
56 #include <linux/fcntl.h>
58 #include <linux/termios.h>
59 #include <linux/interrupt.h>
60 #include <linux/proc_fs.h>
61 #include <linux/stat.h>
62 #include <linux/init.h>
63 #include <linux/poll.h>
64 #include <linux/if_packet.h>
65 #include <net/neighbour.h>
67 #include <net/dn_nsp.h>
68 #include <net/dn_dev.h>
69 #include <net/dn_route.h>
72 static int nsp_backoff[NSP_MAXRXTSHIFT + 1] = { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
75 * If sk == NULL, then we assume that we are supposed to be making
76 * a routing layer skb. If sk != NULL, then we are supposed to be
77 * creating an skb for the NSP layer.
79 * The eventual aim is for each socket to have a cached header size
80 * for its outgoing packets, and to set hdr from this when sk != NULL.
82 struct sk_buff *dn_alloc_skb(struct sock *sk, int size, int pri)
87 if ((skb = alloc_skb(size + hdr, pri)) == NULL)
90 skb->protocol = __constant_htons(ETH_P_DNA_RT);
91 skb->pkt_type = PACKET_OUTGOING;
94 skb_set_owner_w(skb, sk);
96 skb_reserve(skb, hdr);
102 * Wrapper for the above, for allocs of data skbs. We try and get the
103 * whole size thats been asked for (plus 11 bytes of header). If this
104 * fails, then we try for any size over 16 bytes for SOCK_STREAMS.
106 struct sk_buff *dn_alloc_send_skb(struct sock *sk, int *size, int noblock, int *err)
110 struct sk_buff *skb = NULL;
115 if (signal_pending(current)) {
120 if (sk->shutdown & SEND_SHUTDOWN) {
129 space = sk->sndbuf - atomic_read(&sk->wmem_alloc);
132 if ((sk->socket->type == SOCK_STREAM) && (space >= (16 + 11)))
137 set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
143 clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
146 if ((sk->sndbuf - atomic_read(&sk->wmem_alloc)) < len)
153 if ((skb = dn_alloc_skb(sk, len, sk->allocation)) == NULL)
163 * Calculate persist timer based upon the smoothed round
164 * trip time and the variance. Backoff according to the
165 * nsp_backoff[] array.
167 unsigned long dn_nsp_persist(struct sock *sk)
169 struct dn_scp *scp = DN_SK(sk);
171 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
173 t *= nsp_backoff[scp->nsp_rxtshift];
176 if (t > (600*HZ)) t = (600*HZ);
178 if (scp->nsp_rxtshift < NSP_MAXRXTSHIFT)
181 /* printk(KERN_DEBUG "rxtshift %lu, t=%lu\n", scp->nsp_rxtshift, t); */
187 * This is called each time we get an estimate for the rtt
190 static void dn_nsp_rtt(struct sock *sk, long rtt)
192 struct dn_scp *scp = DN_SK(sk);
193 long srtt = (long)scp->nsp_srtt;
194 long rttvar = (long)scp->nsp_rttvar;
198 * If the jiffies clock flips over in the middle of timestamp
199 * gathering this value might turn out negative, so we make sure
200 * that is it always positive here.
205 * Add new rtt to smoothed average
207 delta = ((rtt << 3) - srtt);
208 srtt += (delta >> 3);
210 scp->nsp_srtt = (unsigned long)srtt;
215 * Add new rtt varience to smoothed varience
218 rttvar += ((((delta>0)?(delta):(-delta)) - rttvar) >> 2);
220 scp->nsp_rttvar = (unsigned long)rttvar;
224 /* printk(KERN_DEBUG "srtt=%lu rttvar=%lu\n", scp->nsp_srtt, scp->nsp_rttvar); */
228 * dn_nsp_clone_and_send - Send a data packet by cloning it
229 * @skb: The packet to clone and transmit
230 * @gfp: memory allocation flag
232 * Clone a queued data or other data packet and transmit it.
234 * Returns: The number of times the packet has been sent previously
236 static inline unsigned dn_nsp_clone_and_send(struct sk_buff *skb, int gfp)
238 struct dn_skb_cb *cb = DN_SKB_CB(skb);
239 struct sk_buff *skb2;
242 if ((skb2 = skb_clone(skb, gfp)) != NULL) {
243 ret = cb->xmit_count;
254 * dn_nsp_output - Try and send something from socket queues
255 * @sk: The socket whose queues are to be investigated
256 * @gfp: The memory allocation flags
258 * Try and send the packet on the end of the data and other data queues.
259 * Other data gets priority over data, and if we retransmit a packet we
260 * reduce the window by dividing it in two.
263 void dn_nsp_output(struct sock *sk)
265 struct dn_scp *scp = DN_SK(sk);
267 unsigned reduce_win = 0;
270 * First we check for otherdata/linkservice messages
272 if ((skb = skb_peek(&scp->other_xmit_queue)) != NULL)
273 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
276 * If we may not send any data, we don't.
277 * If we are still trying to get some other data down the
278 * channel, we don't try and send any data.
280 if (reduce_win || (scp->flowrem_sw != DN_SEND))
283 if ((skb = skb_peek(&scp->data_xmit_queue)) != NULL)
284 reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC);
287 * If we've sent any frame more than once, we cut the
288 * send window size in half. There is always a minimum
289 * window size of one available.
293 scp->snd_window >>= 1;
294 if (scp->snd_window < NSP_MIN_WINDOW)
295 scp->snd_window = NSP_MIN_WINDOW;
299 int dn_nsp_xmit_timeout(struct sock *sk)
301 struct dn_scp *scp = DN_SK(sk);
305 if (skb_queue_len(&scp->data_xmit_queue) || skb_queue_len(&scp->other_xmit_queue))
306 scp->persist = dn_nsp_persist(sk);
311 static inline unsigned char *dn_mk_common_header(struct dn_scp *scp, struct sk_buff *skb, unsigned char msgflag, int len)
313 unsigned char *ptr = skb_push(skb, len);
319 *((unsigned short *)ptr) = scp->addrrem;
321 *((unsigned short *)ptr) = scp->addrloc;
326 static unsigned short *dn_mk_ack_header(struct sock *sk, struct sk_buff *skb, unsigned char msgflag, int hlen, int other)
328 struct dn_scp *scp = DN_SK(sk);
329 unsigned short acknum = scp->numdat_rcv & 0x0FFF;
330 unsigned short ackcrs = scp->numoth_rcv & 0x0FFF;
336 scp->ackxmt_dat = acknum;
337 scp->ackxmt_oth = ackcrs;
341 /* If this is an "other data/ack" message, swap acknum and ackcrs */
343 unsigned short tmp = acknum;
348 /* Set "cross subchannel" bit in ackcrs */
351 ptr = (unsigned short *)dn_mk_common_header(scp, skb, msgflag, hlen);
353 *ptr++ = dn_htons(acknum);
354 *ptr++ = dn_htons(ackcrs);
359 void dn_nsp_queue_xmit(struct sock *sk, struct sk_buff *skb, int gfp, int oth)
361 struct dn_scp *scp = DN_SK(sk);
362 struct dn_skb_cb *cb = DN_SKB_CB(skb);
363 unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1;
366 * Slow start: If we have been idle for more than
367 * one RTT, then reset window to min size.
369 if ((jiffies - scp->stamp) > t)
370 scp->snd_window = NSP_MIN_WINDOW;
372 /* printk(KERN_DEBUG "Window: %lu\n", scp->snd_window); */
377 skb_queue_tail(&scp->other_xmit_queue, skb);
379 skb_queue_tail(&scp->data_xmit_queue, skb);
381 if (scp->flowrem_sw != DN_SEND)
384 dn_nsp_clone_and_send(skb, gfp);
388 int dn_nsp_check_xmit_queue(struct sock *sk, struct sk_buff *skb, struct sk_buff_head *q, unsigned short acknum)
390 struct dn_skb_cb *cb = DN_SKB_CB(skb);
391 struct dn_scp *scp = DN_SK(sk);
392 struct sk_buff *skb2, *list, *ack = NULL;
395 unsigned long reftime = cb->stamp;
396 unsigned long pkttime;
397 unsigned short xmit_count;
398 unsigned short segnum;
401 list = (struct sk_buff *)q;
402 while(list != skb2) {
403 struct dn_skb_cb *cb2 = DN_SKB_CB(skb2);
405 if (before_or_equal(cb2->segnum, acknum))
408 /* printk(KERN_DEBUG "ack: %s %04x %04x\n", ack ? "ACK" : "SKIP", (int)cb2->segnum, (int)acknum); */
415 /* printk(KERN_DEBUG "check_xmit_queue: %04x, %d\n", acknum, cb2->xmit_count); */
417 /* Does _last_ packet acked have xmit_count > 1 */
419 /* Remember to wake up the sending process */
421 /* Keep various statistics */
422 pkttime = cb2->stamp;
423 xmit_count = cb2->xmit_count;
424 segnum = cb2->segnum;
425 /* Remove and drop ack'ed packet */
431 * We don't expect to see acknowledgements for packets we
437 * If the packet has only been sent once, we can use it
438 * to calculate the RTT and also open the window a little
441 if (xmit_count == 1) {
442 if (equal(segnum, acknum))
443 dn_nsp_rtt(sk, (long)(pkttime - reftime));
445 if (scp->snd_window < scp->max_window)
450 * Packet has been sent more than once. If this is the last
451 * packet to be acknowledged then we want to send the next
452 * packet in the send queue again (assumes the remote host does
453 * go-back-N error control).
465 void dn_nsp_send_data_ack(struct sock *sk)
467 struct sk_buff *skb = NULL;
469 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
473 dn_mk_ack_header(sk, skb, 0x04, 9, 0);
477 void dn_nsp_send_oth_ack(struct sock *sk)
479 struct sk_buff *skb = NULL;
481 if ((skb = dn_alloc_skb(sk, 9, GFP_ATOMIC)) == NULL)
485 dn_mk_ack_header(sk, skb, 0x14, 9, 1);
490 void dn_send_conn_ack (struct sock *sk)
492 struct dn_scp *scp = DN_SK(sk);
493 struct sk_buff *skb = NULL;
494 struct nsp_conn_ack_msg *msg;
496 if ((skb = dn_alloc_skb(sk, 3, sk->allocation)) == NULL)
499 msg = (struct nsp_conn_ack_msg *)skb_put(skb, 3);
501 msg->dstaddr = scp->addrrem;
506 void dn_nsp_delayed_ack(struct sock *sk)
508 struct dn_scp *scp = DN_SK(sk);
510 if (scp->ackxmt_oth != scp->numoth_rcv)
511 dn_nsp_send_oth_ack(sk);
513 if (scp->ackxmt_dat != scp->numdat_rcv)
514 dn_nsp_send_data_ack(sk);
517 static int dn_nsp_retrans_conn_conf(struct sock *sk)
519 struct dn_scp *scp = DN_SK(sk);
521 if (scp->state == DN_CC)
522 dn_send_conn_conf(sk, GFP_ATOMIC);
527 void dn_send_conn_conf(struct sock *sk, int gfp)
529 struct dn_scp *scp = DN_SK(sk);
530 struct sk_buff *skb = NULL;
531 struct nsp_conn_init_msg *msg;
532 unsigned char len = scp->conndata_out.opt_optl;
534 if ((skb = dn_alloc_skb(sk, 50 + scp->conndata_out.opt_optl, gfp)) == NULL)
537 msg = (struct nsp_conn_init_msg *)skb_put(skb, sizeof(*msg));
539 msg->dstaddr = scp->addrrem;
540 msg->srcaddr = scp->addrloc;
541 msg->services = scp->services_loc;
542 msg->info = scp->info_loc;
543 msg->segsize = dn_htons(scp->segsize_loc);
545 *skb_put(skb,1) = len;
548 memcpy(skb_put(skb, len), scp->conndata_out.opt_data, len);
553 scp->persist = dn_nsp_persist(sk);
554 scp->persist_fxn = dn_nsp_retrans_conn_conf;
558 static __inline__ void dn_nsp_do_disc(struct sock *sk, unsigned char msgflg,
559 unsigned short reason, int gfp, struct dst_entry *dst,
560 int ddl, unsigned char *dd, __u16 rem, __u16 loc)
562 struct sk_buff *skb = NULL;
563 int size = 7 + ddl + ((msgflg == NSP_DISCINIT) ? 1 : 0);
566 if ((dst == NULL) || (rem == 0)) {
568 printk(KERN_DEBUG "DECnet: dn_nsp_do_disc: BUG! Please report this to SteveW@ACM.org rem=%u dst=%p\n", (unsigned)rem, dst);
572 if ((skb = dn_alloc_skb(sk, size, gfp)) == NULL)
575 msg = skb_put(skb, size);
581 *(__u16 *)msg = dn_htons(reason);
583 if (msgflg == NSP_DISCINIT)
587 memcpy(msg, dd, ddl);
591 * This doesn't go via the dn_nsp_send() fucntion since we need
592 * to be able to send disc packets out which have no socket
595 skb->dst = dst_clone(dst);
596 skb->dst->output(skb);
600 void dn_nsp_send_disc(struct sock *sk, unsigned char msgflg,
601 unsigned short reason, int gfp)
603 struct dn_scp *scp = DN_SK(sk);
606 if (msgflg == NSP_DISCINIT)
607 ddl = scp->discdata_out.opt_optl;
610 reason = scp->discdata_out.opt_status;
612 dn_nsp_do_disc(sk, msgflg, reason, gfp, sk->dst_cache, ddl,
613 scp->discdata_out.opt_data, scp->addrrem, scp->addrloc);
617 void dn_nsp_return_disc(struct sk_buff *skb, unsigned char msgflg,
618 unsigned short reason)
620 struct dn_skb_cb *cb = DN_SKB_CB(skb);
622 int gfp = GFP_ATOMIC;
624 dn_nsp_do_disc(NULL, msgflg, reason, gfp, skb->dst, ddl,
625 NULL, cb->src_port, cb->dst_port);
629 void dn_nsp_send_link(struct sock *sk, unsigned char lsflags, char fcval)
631 struct dn_scp *scp = DN_SK(sk);
633 unsigned short *segnum;
635 int gfp = GFP_ATOMIC;
637 if ((skb = dn_alloc_skb(sk, 13, gfp)) == NULL)
640 skb_reserve(skb, 13);
641 segnum = dn_mk_ack_header(sk, skb, 0x10, 13, 1);
642 *segnum = dn_htons(scp->numoth);
643 DN_SKB_CB(skb)->segnum = scp->numoth;
644 seq_add(&scp->numoth, 1);
645 ptr = (unsigned char *)(segnum + 1);
649 dn_nsp_queue_xmit(sk, skb, gfp, 1);
651 scp->persist = dn_nsp_persist(sk);
652 scp->persist_fxn = dn_nsp_xmit_timeout;
655 static int dn_nsp_retrans_conninit(struct sock *sk)
657 struct dn_scp *scp = DN_SK(sk);
659 if (scp->state == DN_CI)
660 dn_nsp_send_conninit(sk, NSP_RCI);
665 void dn_nsp_send_conninit(struct sock *sk, unsigned char msgflg)
667 struct dn_scp *scp = DN_SK(sk);
668 struct sk_buff *skb = NULL;
669 struct nsp_conn_init_msg *msg;
671 unsigned char menuver;
672 struct dn_skb_cb *cb;
673 unsigned char type = 1;
675 if ((skb = dn_alloc_skb(sk, 200, (msgflg == NSP_CI) ? sk->allocation : GFP_ATOMIC)) == NULL)
679 msg = (struct nsp_conn_init_msg *)skb_put(skb,sizeof(*msg));
681 msg->msgflg = msgflg;
682 msg->dstaddr = 0x0000; /* Remote Node will assign it*/
684 msg->srcaddr = scp->addrloc;
685 msg->services = scp->services_loc; /* Requested flow control */
686 msg->info = scp->info_loc; /* Version Number */
687 msg->segsize = dn_htons(scp->segsize_loc); /* Max segment size */
689 if (scp->peer.sdn_objnum)
692 skb_put(skb, dn_sockaddr2username(&scp->peer, skb->tail, type));
693 skb_put(skb, dn_sockaddr2username(&scp->addr, skb->tail, 2));
695 menuver = DN_MENUVER_ACC | DN_MENUVER_USR;
696 if (scp->peer.sdn_flags & SDF_PROXY)
697 menuver |= DN_MENUVER_PRX;
698 if (scp->peer.sdn_flags & SDF_UICPROXY)
699 menuver |= DN_MENUVER_UIC;
701 *skb_put(skb, 1) = menuver; /* Menu Version */
703 aux = scp->accessdata.acc_userl;
704 *skb_put(skb, 1) = aux;
706 memcpy(skb_put(skb, aux), scp->accessdata.acc_user, aux);
708 aux = scp->accessdata.acc_passl;
709 *skb_put(skb, 1) = aux;
711 memcpy(skb_put(skb, aux), scp->accessdata.acc_pass, aux);
713 aux = scp->accessdata.acc_accl;
714 *skb_put(skb, 1) = aux;
716 memcpy(skb_put(skb, aux), scp->accessdata.acc_acc, aux);
718 aux = scp->conndata_out.opt_optl;
719 *skb_put(skb, 1) = aux;
721 memcpy(skb_put(skb,aux), scp->conndata_out.opt_data, aux);
723 scp->persist = dn_nsp_persist(sk);
724 scp->persist_fxn = dn_nsp_retrans_conninit;
726 cb->rt_flags = DN_RT_F_RQR;