root/net/netrom/nr_subr.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nr_clear_tx_queue
  2. nr_frames_acked
  3. nr_requeue_frames
  4. nr_validate_nr
  5. nr_in_rx_window
  6. nr_write_internal
  7. nr_transmit_dm
  8. nr_calculate_t1
  9. nr_calculate_rtt

   1 /*
   2  *      NET/ROM release 003
   3  *
   4  *      This is ALPHA test software. This code may break your machine, randomly fail to work with new 
   5  *      releases, misbehave and/or generally screw up. It might even work. 
   6  *
   7  *      This code REQUIRES 1.2.1 or higher/ NET3.029
   8  *
   9  *      This module:
  10  *              This module is free software; you can redistribute it and/or
  11  *              modify it under the terms of the GNU General Public License
  12  *              as published by the Free Software Foundation; either version
  13  *              2 of the License, or (at your option) any later version.
  14  *
  15  *      History
  16  *      NET/ROM 001     Jonathan(G4KLX) Cloned from ax25_subr.c
  17  *      NET/ROM 003     Jonathan(G4KLX) Added G8BPQ NET/ROM extensions.
  18  */
  19  
  20 #include <linux/config.h>
  21 #ifdef CONFIG_NETROM
  22 #include <linux/errno.h>
  23 #include <linux/types.h>
  24 #include <linux/socket.h>
  25 #include <linux/in.h>
  26 #include <linux/kernel.h>
  27 #include <linux/sched.h>
  28 #include <linux/timer.h>
  29 #include <linux/string.h>
  30 #include <linux/sockios.h>
  31 #include <linux/net.h>
  32 #include <net/ax25.h>
  33 #include <linux/inet.h>
  34 #include <linux/netdevice.h>
  35 #include <linux/skbuff.h>
  36 #include <net/sock.h>
  37 #include <asm/segment.h>
  38 #include <asm/system.h>
  39 #include <linux/fcntl.h>
  40 #include <linux/mm.h>
  41 #include <linux/interrupt.h>
  42 #include <net/netrom.h>
  43 
  44 /*
  45  * This routine purges the input queue of frames.
  46  */
  47 void nr_clear_tx_queue(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         struct sk_buff *skb;
  50 
  51         while ((skb = skb_dequeue(&sk->write_queue)) != NULL) {
  52                 skb->sk   = sk;
  53                 skb->free = 1;
  54                 kfree_skb(skb, FREE_WRITE);
  55         }
  56 
  57         while ((skb = skb_dequeue(&sk->nr->ack_queue)) != NULL) {
  58                 skb->sk   = sk;
  59                 skb->free = 1;
  60                 kfree_skb(skb, FREE_WRITE);
  61         }
  62 
  63         while ((skb = skb_dequeue(&sk->nr->reseq_queue)) != NULL) {
  64                 skb->free = 1;
  65                 kfree_skb(skb, FREE_READ);
  66         }
  67 }
  68 
  69 /*
  70  * This routine purges the input queue of those frames that have been
  71  * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
  72  * SDL diagram.
  73  */
  74 void nr_frames_acked(struct sock *sk, unsigned short nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         struct sk_buff *skb;
  77 
  78         /*
  79          * Remove all the ack-ed frames from the ack queue.
  80          */
  81         if (sk->nr->va != nr) {
  82                 while (skb_peek(&sk->nr->ack_queue) != NULL && sk->nr->va != nr) {
  83                         skb = skb_dequeue(&sk->nr->ack_queue);
  84                         skb->sk   = sk;
  85                         skb->free = 1;
  86                         kfree_skb(skb, FREE_WRITE);
  87                         sk->nr->va = (sk->nr->va + 1) % NR_MODULUS;
  88                 }
  89         }
  90 }
  91 
  92 /*
  93  * Requeue all the un-ack-ed frames on the output queue to be picked
  94  * up by nr_kick called from the timer. This arrangement handles the
  95  * possibility of an empty output queue.
  96  */
  97 void nr_requeue_frames(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         struct sk_buff *skb, *skb_prev = NULL;
 100 
 101         while ((skb = skb_dequeue(&sk->nr->ack_queue)) != NULL) {
 102                 if (skb_prev == NULL)
 103                         skb_queue_head(&sk->write_queue, skb);
 104                 else
 105                         skb_append(skb_prev, skb);
 106                 skb_prev = skb;
 107         }
 108 }
 109 
 110 /*
 111  *      Validate that the value of nr is between va and vs. Return true or
 112  *      false for testing.
 113  */
 114 int nr_validate_nr(struct sock *sk, unsigned short nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116         unsigned short vc = sk->nr->va;
 117 
 118         while (vc != sk->nr->vs) {
 119                 if (nr == vc) return 1;
 120                 vc = (vc + 1) % NR_MODULUS;
 121         }
 122         
 123         if (nr == sk->nr->vs) return 1;
 124 
 125         return 0;
 126 }
 127 
 128 /*
 129  *      Check that ns is within the receive window.
 130  */
 131 int nr_in_rx_window(struct sock *sk, unsigned short ns)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133         unsigned short vc = sk->nr->vl;
 134         unsigned short vt = (sk->nr->vl + sk->window) % NR_MODULUS;
 135 
 136         while (vc != vt) {
 137                 if (ns == vc) return 1;
 138                 vc = (vc + 1) % NR_MODULUS;
 139         }
 140         
 141         if (ns == vt) return 1;
 142 
 143         return 0;
 144 }
 145 
 146 /* 
 147  *  This routine is called when the HDLC layer internally generates a
 148  *  control frame.
 149  */
 150 void nr_write_internal(struct sock *sk, int frametype)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152         struct sk_buff *skb;
 153         unsigned char  *dptr;
 154         int len, timeout;
 155 
 156         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 2 + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
 157         
 158         switch (frametype & 0x0F) {
 159                 case NR_CONNREQ:
 160                         len += 17;
 161                         break;
 162                 case NR_CONNACK:
 163                         len += (sk->nr->bpqext) ? 2 : 1;
 164                         break;
 165                 case NR_DISCREQ:
 166                 case NR_DISCACK:
 167                 case NR_INFOACK:
 168                         break;
 169                 default:
 170                         printk("nr_write_internal: invalid frame type %d\n", frametype);
 171                         return;
 172         }
 173         
 174         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
 175                 return;
 176 
 177         /*
 178          *      Space for AX.25 and NET/ROM network header
 179          */
 180         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 2 + NR_NETWORK_LEN);
 181         
 182         dptr = skb_put(skb, skb_tailroom(skb));
 183 
 184         switch (frametype & 0x0F) {
 185 
 186                 case NR_CONNREQ:
 187                         timeout  = (sk->nr->rtt / PR_SLOWHZ) * 2;
 188                         *dptr++  = sk->nr->my_index;
 189                         *dptr++  = sk->nr->my_id;
 190                         *dptr++  = 0;
 191                         *dptr++  = 0;
 192                         *dptr++  = frametype;
 193                         *dptr++  = sk->window;
 194                         memcpy(dptr, &sk->nr->user_addr, sizeof(ax25_address));
 195                         dptr[6] &= ~LAPB_C;
 196                         dptr[6] &= ~LAPB_E;
 197                         dptr[6] |= SSID_SPARE;
 198                         dptr    += AX25_ADDR_LEN;
 199                         memcpy(dptr, &sk->nr->source_addr, sizeof(ax25_address));
 200                         dptr[6] &= ~LAPB_C;
 201                         dptr[6] &= ~LAPB_E;
 202                         dptr[6] |= SSID_SPARE;
 203                         dptr    += AX25_ADDR_LEN;
 204                         *dptr++  = timeout % 256;
 205                         *dptr++  = timeout / 256;
 206                         break;
 207 
 208                 case NR_CONNACK:
 209                         *dptr++ = sk->nr->your_index;
 210                         *dptr++ = sk->nr->your_id;
 211                         *dptr++ = sk->nr->my_index;
 212                         *dptr++ = sk->nr->my_id;
 213                         *dptr++ = frametype;
 214                         *dptr++ = sk->window;
 215                         if (sk->nr->bpqext) *dptr++ = nr_default.ttl;
 216                         break;
 217 
 218                 case NR_DISCREQ:
 219                 case NR_DISCACK:
 220                         *dptr++ = sk->nr->your_index;
 221                         *dptr++ = sk->nr->your_id;
 222                         *dptr++ = 0;
 223                         *dptr++ = 0;
 224                         *dptr++ = frametype;
 225                         break;
 226 
 227                 case NR_INFOACK:
 228                         *dptr++ = sk->nr->your_index;
 229                         *dptr++ = sk->nr->your_id;
 230                         *dptr++ = 0;
 231                         *dptr++ = sk->nr->vr;
 232                         *dptr++ = frametype;
 233                         break;
 234         }
 235 
 236         skb->free = 1;
 237 
 238         nr_transmit_buffer(sk, skb);
 239 }
 240 
 241 /*
 242  * This routine is called when a Connect Acknowledge with the Choke Flag
 243  * set is needed to refuse a connection.
 244  */
 245 void nr_transmit_dm(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 246 {
 247         struct sk_buff *skbn;
 248         unsigned char *dptr;
 249         int len;
 250 
 251         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 2 + NR_NETWORK_LEN + NR_TRANSPORT_LEN + 1;
 252 
 253         if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL)
 254                 return;
 255 
 256         skb_reserve(skbn, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 2);
 257 
 258         dptr = skb_put(skbn, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
 259 
 260         memcpy(dptr, skb->data + 7, AX25_ADDR_LEN);
 261         dptr[6] &= ~LAPB_C;
 262         dptr[6] &= ~LAPB_E;
 263         dptr[6] |= SSID_SPARE;
 264         dptr += AX25_ADDR_LEN;
 265         
 266         memcpy(dptr, skb->data + 0, AX25_ADDR_LEN);
 267         dptr[6] &= ~LAPB_C;
 268         dptr[6] |= LAPB_E;
 269         dptr[6] |= SSID_SPARE;
 270         dptr += AX25_ADDR_LEN;
 271 
 272         *dptr++ = nr_default.ttl;
 273 
 274         *dptr++ = skb->data[15];
 275         *dptr++ = skb->data[16];
 276         *dptr++ = 0;
 277         *dptr++ = 0;
 278         *dptr++ = NR_CONNACK + NR_CHOKE_FLAG;
 279         *dptr++ = 0;
 280 
 281         skbn->free = 1;
 282         skbn->sk   = NULL;
 283 
 284         if (!nr_route_frame(skbn, NULL))
 285                 kfree_skb(skbn, FREE_WRITE);
 286 }
 287 
 288 /*
 289  *      Exponential backoff for NET/ROM
 290  */
 291 unsigned short nr_calculate_t1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 292 {
 293         int n, t;
 294         
 295         for (t = 2, n = 0; n < sk->nr->n2count; n++)
 296                 t *= 2;
 297 
 298         return t * sk->nr->rtt;
 299 }
 300 
 301 /*
 302  *      Calculate the Round Trip Time
 303  */
 304 void nr_calculate_rtt(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 305 {
 306         if (sk->nr->t1timer > 0 && sk->nr->n2count == 0)
 307                 sk->nr->rtt = (9 * sk->nr->rtt + sk->nr->t1 - sk->nr->t1timer) / 10;
 308 
 309         /* Don't go below one second */
 310         if (sk->nr->rtt < 1 * PR_SLOWHZ)
 311                 sk->nr->rtt = 1 * PR_SLOWHZ;
 312 }
 313 
 314 #endif

/* [previous][next][first][last][top][bottom][index][help] */