ax25_subr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:8k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * AX.25 release 037
  3.  *
  4.  * This code REQUIRES 2.1.15 or higher/ NET3.038
  5.  *
  6.  * This module:
  7.  * This module is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version
  10.  * 2 of the License, or (at your option) any later version.
  11.  *
  12.  * Most of this code is based on the SDL diagrams published in the 7th
  13.  * ARRL Computer Networking Conference papers. The diagrams have mistakes
  14.  * in them, but are mostly correct. Before you modify the code could you
  15.  * read the SDL diagrams as the code is not obvious and probably very
  16.  * easy to break;
  17.  *
  18.  * History
  19.  * AX.25 029 Alan(GW4PTS) Switched to KA9Q constant names. Removed
  20.  * old BSD code.
  21.  * AX.25 030 Jonathan(G4KLX) Added support for extended AX.25.
  22.  * Added fragmentation support.
  23.  * Darryl(G7LED) Added function ax25_requeue_frames() to split
  24.  * it up from ax25_frames_acked().
  25.  * AX.25 031 Joerg(DL1BKE) DAMA needs KISS Fullduplex ON/OFF.
  26.  * Thus we have ax25_kiss_cmd() now... ;-)
  27.  * Dave Brown(N2RJT)
  28.  * Killed a silly bug in the DAMA code.
  29.  * Joerg(DL1BKE) Found the real bug in ax25.h, sri.
  30.  * AX.25 032 Joerg(DL1BKE) Added ax25_queue_length to count the number of
  31.  * enqueued buffers of a socket..
  32.  * AX.25 035 Frederic(F1OAT) Support for pseudo-digipeating.
  33.  * AX.25 037 Jonathan(G4KLX) New timer architecture.
  34.  */
  35. #include <linux/errno.h>
  36. #include <linux/types.h>
  37. #include <linux/socket.h>
  38. #include <linux/in.h>
  39. #include <linux/kernel.h>
  40. #include <linux/sched.h>
  41. #include <linux/timer.h>
  42. #include <linux/string.h>
  43. #include <linux/sockios.h>
  44. #include <linux/net.h>
  45. #include <net/ax25.h>
  46. #include <linux/inet.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/skbuff.h>
  49. #include <net/sock.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/system.h>
  52. #include <linux/fcntl.h>
  53. #include <linux/mm.h>
  54. #include <linux/interrupt.h>
  55. /*
  56.  * This routine purges all the queues of frames.
  57.  */
  58. void ax25_clear_queues(ax25_cb *ax25)
  59. {
  60. skb_queue_purge(&ax25->write_queue);
  61. skb_queue_purge(&ax25->ack_queue);
  62. skb_queue_purge(&ax25->reseq_queue);
  63. skb_queue_purge(&ax25->frag_queue);
  64. }
  65. /*
  66.  * This routine purges the input queue of those frames that have been
  67.  * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
  68.  * SDL diagram.
  69.  */
  70. void ax25_frames_acked(ax25_cb *ax25, unsigned short nr)
  71. {
  72. struct sk_buff *skb;
  73. /*
  74.  * Remove all the ack-ed frames from the ack queue.
  75.  */
  76. if (ax25->va != nr) {
  77. while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) {
  78.         skb = skb_dequeue(&ax25->ack_queue);
  79. kfree_skb(skb);
  80. ax25->va = (ax25->va + 1) % ax25->modulus;
  81. }
  82. }
  83. }
  84. void ax25_requeue_frames(ax25_cb *ax25)
  85. {
  86.         struct sk_buff *skb, *skb_prev = NULL;
  87. /*
  88.  * Requeue all the un-ack-ed frames on the output queue to be picked
  89.  * up by ax25_kick called from the timer. This arrangement handles the
  90.  * possibility of an empty output queue.
  91.  */
  92. while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) {
  93. if (skb_prev == NULL)
  94. skb_queue_head(&ax25->write_queue, skb);
  95. else
  96. skb_append(skb_prev, skb);
  97. skb_prev = skb;
  98. }
  99. }
  100. /*
  101.  * Validate that the value of nr is between va and vs. Return true or
  102.  * false for testing.
  103.  */
  104. int ax25_validate_nr(ax25_cb *ax25, unsigned short nr)
  105. {
  106. unsigned short vc = ax25->va;
  107. while (vc != ax25->vs) {
  108. if (nr == vc) return 1;
  109. vc = (vc + 1) % ax25->modulus;
  110. }
  111. if (nr == ax25->vs) return 1;
  112. return 0;
  113. }
  114. /*
  115.  * This routine is the centralised routine for parsing the control
  116.  * information for the different frame formats.
  117.  */
  118. int ax25_decode(ax25_cb *ax25, struct sk_buff *skb, int *ns, int *nr, int *pf)
  119. {
  120. unsigned char *frame;
  121. int frametype = AX25_ILLEGAL;
  122. frame = skb->data;
  123. *ns = *nr = *pf = 0;
  124. if (ax25->modulus == AX25_MODULUS) {
  125. if ((frame[0] & AX25_S) == 0) {
  126. frametype = AX25_I; /* I frame - carries NR/NS/PF */
  127. *ns = (frame[0] >> 1) & 0x07;
  128. *nr = (frame[0] >> 5) & 0x07;
  129. *pf = frame[0] & AX25_PF;
  130. } else if ((frame[0] & AX25_U) == 1) {  /* S frame - take out PF/NR */
  131. frametype = frame[0] & 0x0F;
  132. *nr = (frame[0] >> 5) & 0x07;
  133. *pf = frame[0] & AX25_PF;
  134. } else if ((frame[0] & AX25_U) == 3) {  /* U frame - take out PF */
  135. frametype = frame[0] & ~AX25_PF;
  136. *pf = frame[0] & AX25_PF;
  137. }
  138. skb_pull(skb, 1);
  139. } else {
  140. if ((frame[0] & AX25_S) == 0) {
  141. frametype = AX25_I; /* I frame - carries NR/NS/PF */
  142. *ns = (frame[0] >> 1) & 0x7F;
  143. *nr = (frame[1] >> 1) & 0x7F;
  144. *pf = frame[1] & AX25_EPF;
  145. skb_pull(skb, 2);
  146. } else if ((frame[0] & AX25_U) == 1) {  /* S frame - take out PF/NR */
  147. frametype = frame[0] & 0x0F;
  148. *nr = (frame[1] >> 1) & 0x7F;
  149. *pf = frame[1] & AX25_EPF;
  150. skb_pull(skb, 2);
  151. } else if ((frame[0] & AX25_U) == 3) {  /* U frame - take out PF */
  152. frametype = frame[0] & ~AX25_PF;
  153. *pf = frame[0] & AX25_PF;
  154. skb_pull(skb, 1);
  155. }
  156. }
  157. return frametype;
  158. }
  159. /* 
  160.  * This routine is called when the HDLC layer internally  generates a
  161.  * command or  response  for  the remote machine ( eg. RR, UA etc. ). 
  162.  * Only supervisory or unnumbered frames are processed.
  163.  */
  164. void ax25_send_control(ax25_cb *ax25, int frametype, int poll_bit, int type)
  165. {
  166. struct sk_buff *skb;
  167. unsigned char  *dptr;
  168. if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + ax25_addr_size(ax25->digipeat) + 2, GFP_ATOMIC)) == NULL)
  169. return;
  170. skb_reserve(skb, AX25_BPQ_HEADER_LEN + ax25_addr_size(ax25->digipeat));
  171. skb->nh.raw = skb->data;
  172. /* Assume a response - address structure for DTE */
  173. if (ax25->modulus == AX25_MODULUS) {
  174. dptr = skb_put(skb, 1);
  175. *dptr = frametype;
  176. *dptr |= (poll_bit) ? AX25_PF : 0;
  177. if ((frametype & AX25_U) == AX25_S) /* S frames carry NR */
  178. *dptr |= (ax25->vr << 5);
  179. } else {
  180. if ((frametype & AX25_U) == AX25_U) {
  181. dptr = skb_put(skb, 1);
  182. *dptr = frametype;
  183. *dptr |= (poll_bit) ? AX25_PF : 0;
  184. } else {
  185. dptr = skb_put(skb, 2);
  186. dptr[0] = frametype;
  187. dptr[1] = (ax25->vr << 1);
  188. dptr[1] |= (poll_bit) ? AX25_EPF : 0;
  189. }
  190. }
  191. ax25_transmit_buffer(ax25, skb, type);
  192. }
  193. /*
  194.  * Send a 'DM' to an unknown connection attempt, or an invalid caller.
  195.  *
  196.  * Note: src here is the sender, thus it's the target of the DM
  197.  */
  198. void ax25_return_dm(struct net_device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi)
  199. {
  200. struct sk_buff *skb;
  201. char *dptr;
  202. ax25_digi retdigi;
  203. if (dev == NULL)
  204. return;
  205. if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + ax25_addr_size(digi) + 1, GFP_ATOMIC)) == NULL)
  206. return; /* Next SABM will get DM'd */
  207. skb_reserve(skb, AX25_BPQ_HEADER_LEN + ax25_addr_size(digi));
  208. skb->nh.raw = skb->data;
  209. ax25_digi_invert(digi, &retdigi);
  210. dptr = skb_put(skb, 1);
  211. *dptr = AX25_DM | AX25_PF;
  212. /*
  213.  * Do the address ourselves
  214.  */
  215. dptr  = skb_push(skb, ax25_addr_size(digi));
  216. dptr += ax25_addr_build(dptr, dest, src, &retdigi, AX25_RESPONSE, AX25_MODULUS);
  217. skb->dev      = dev;
  218. ax25_queue_xmit(skb);
  219. }
  220. /*
  221.  * Exponential backoff for AX.25
  222.  */
  223. void ax25_calculate_t1(ax25_cb *ax25)
  224. {
  225. int n, t = 2;
  226. switch (ax25->backoff) {
  227. case 0:
  228. break;
  229. case 1:
  230. t += 2 * ax25->n2count;
  231. break;
  232. case 2:
  233. for (n = 0; n < ax25->n2count; n++)
  234. t *= 2;
  235. if (t > 8) t = 8;
  236. break;
  237. }
  238. ax25->t1 = t * ax25->rtt;
  239. }
  240. /*
  241.  * Calculate the Round Trip Time
  242.  */
  243. void ax25_calculate_rtt(ax25_cb *ax25)
  244. {
  245. if (ax25->backoff == 0)
  246. return;
  247. if (ax25_t1timer_running(ax25) && ax25->n2count == 0)
  248. ax25->rtt = (9 * ax25->rtt + ax25->t1 - ax25_display_timer(&ax25->t1timer)) / 10;
  249. if (ax25->rtt < AX25_T1CLAMPLO)
  250. ax25->rtt = AX25_T1CLAMPLO;
  251. if (ax25->rtt > AX25_T1CLAMPHI)
  252. ax25->rtt = AX25_T1CLAMPHI;
  253. }
  254. void ax25_disconnect(ax25_cb *ax25, int reason)
  255. {
  256. ax25_clear_queues(ax25);
  257. ax25_stop_t1timer(ax25);
  258. ax25_stop_t2timer(ax25);
  259. ax25_stop_t3timer(ax25);
  260. ax25_stop_idletimer(ax25);
  261. ax25->state = AX25_STATE_0;
  262. ax25_link_failed(ax25, reason);
  263. if (ax25->sk != NULL) {
  264. ax25->sk->state     = TCP_CLOSE;
  265. ax25->sk->err       = reason;
  266. ax25->sk->shutdown |= SEND_SHUTDOWN;
  267. if (!ax25->sk->dead)
  268. ax25->sk->state_change(ax25->sk);
  269. ax25->sk->dead      = 1;
  270. }
  271. }