ax25_out.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  8. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  9. * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
  10. */
  11. #include <linux/config.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/socket.h>
  15. #include <linux/in.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/timer.h>
  19. #include <linux/string.h>
  20. #include <linux/sockios.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/net.h>
  23. #include <net/ax25.h>
  24. #include <linux/inet.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/netfilter.h>
  28. #include <net/sock.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/system.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. static DEFINE_SPINLOCK(ax25_frag_lock);
  35. ax25_cb *ax25_send_frame(struct sk_buff *skb, int paclen, ax25_address *src, ax25_address *dest, ax25_digi *digi, struct net_device *dev)
  36. {
  37. ax25_dev *ax25_dev;
  38. ax25_cb *ax25;
  39. /*
  40. * Take the default packet length for the device if zero is
  41. * specified.
  42. */
  43. if (paclen == 0) {
  44. if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
  45. return NULL;
  46. paclen = ax25_dev->values[AX25_VALUES_PACLEN];
  47. }
  48. /*
  49. * Look for an existing connection.
  50. */
  51. if ((ax25 = ax25_find_cb(src, dest, digi, dev)) != NULL) {
  52. ax25_output(ax25, paclen, skb);
  53. return ax25; /* It already existed */
  54. }
  55. if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
  56. return NULL;
  57. if ((ax25 = ax25_create_cb()) == NULL)
  58. return NULL;
  59. ax25_fillin_cb(ax25, ax25_dev);
  60. ax25->source_addr = *src;
  61. ax25->dest_addr = *dest;
  62. if (digi != NULL) {
  63. if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  64. ax25_cb_put(ax25);
  65. return NULL;
  66. }
  67. memcpy(ax25->digipeat, digi, sizeof(ax25_digi));
  68. }
  69. switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
  70. case AX25_PROTO_STD_SIMPLEX:
  71. case AX25_PROTO_STD_DUPLEX:
  72. ax25_std_establish_data_link(ax25);
  73. break;
  74. #ifdef CONFIG_AX25_DAMA_SLAVE
  75. case AX25_PROTO_DAMA_SLAVE:
  76. if (ax25_dev->dama.slave)
  77. ax25_ds_establish_data_link(ax25);
  78. else
  79. ax25_std_establish_data_link(ax25);
  80. break;
  81. #endif
  82. }
  83. ax25_cb_add(ax25);
  84. ax25->state = AX25_STATE_1;
  85. ax25_start_heartbeat(ax25);
  86. ax25_output(ax25, paclen, skb);
  87. return ax25; /* We had to create it */
  88. }
  89. /*
  90. * All outgoing AX.25 I frames pass via this routine. Therefore this is
  91. * where the fragmentation of frames takes place. If fragment is set to
  92. * zero then we are not allowed to do fragmentation, even if the frame
  93. * is too large.
  94. */
  95. void ax25_output(ax25_cb *ax25, int paclen, struct sk_buff *skb)
  96. {
  97. struct sk_buff *skbn;
  98. unsigned char *p;
  99. int frontlen, len, fragno, ka9qfrag, first = 1;
  100. if ((skb->len - 1) > paclen) {
  101. if (*skb->data == AX25_P_TEXT) {
  102. skb_pull(skb, 1); /* skip PID */
  103. ka9qfrag = 0;
  104. } else {
  105. paclen -= 2; /* Allow for fragment control info */
  106. ka9qfrag = 1;
  107. }
  108. fragno = skb->len / paclen;
  109. if (skb->len % paclen == 0) fragno--;
  110. frontlen = skb_headroom(skb); /* Address space + CTRL */
  111. while (skb->len > 0) {
  112. spin_lock_bh(&ax25_frag_lock);
  113. if ((skbn = alloc_skb(paclen + 2 + frontlen, GFP_ATOMIC)) == NULL) {
  114. spin_unlock_bh(&ax25_frag_lock);
  115. printk(KERN_CRIT "AX.25: ax25_output - out of memory\n");
  116. return;
  117. }
  118. if (skb->sk != NULL)
  119. skb_set_owner_w(skbn, skb->sk);
  120. spin_unlock_bh(&ax25_frag_lock);
  121. len = (paclen > skb->len) ? skb->len : paclen;
  122. if (ka9qfrag == 1) {
  123. skb_reserve(skbn, frontlen + 2);
  124. skbn->nh.raw = skbn->data + (skb->nh.raw - skb->data);
  125. memcpy(skb_put(skbn, len), skb->data, len);
  126. p = skb_push(skbn, 2);
  127. *p++ = AX25_P_SEGMENT;
  128. *p = fragno--;
  129. if (first) {
  130. *p |= AX25_SEG_FIRST;
  131. first = 0;
  132. }
  133. } else {
  134. skb_reserve(skbn, frontlen + 1);
  135. skbn->nh.raw = skbn->data + (skb->nh.raw - skb->data);
  136. memcpy(skb_put(skbn, len), skb->data, len);
  137. p = skb_push(skbn, 1);
  138. *p = AX25_P_TEXT;
  139. }
  140. skb_pull(skb, len);
  141. skb_queue_tail(&ax25->write_queue, skbn); /* Throw it on the queue */
  142. }
  143. kfree_skb(skb);
  144. } else {
  145. skb_queue_tail(&ax25->write_queue, skb); /* Throw it on the queue */
  146. }
  147. switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
  148. case AX25_PROTO_STD_SIMPLEX:
  149. case AX25_PROTO_STD_DUPLEX:
  150. ax25_kick(ax25);
  151. break;
  152. #ifdef CONFIG_AX25_DAMA_SLAVE
  153. /*
  154. * A DAMA slave is _required_ to work as normal AX.25L2V2
  155. * if no DAMA master is available.
  156. */
  157. case AX25_PROTO_DAMA_SLAVE:
  158. if (!ax25->ax25_dev->dama.slave) ax25_kick(ax25);
  159. break;
  160. #endif
  161. }
  162. }
  163. /*
  164. * This procedure is passed a buffer descriptor for an iframe. It builds
  165. * the rest of the control part of the frame and then writes it out.
  166. */
  167. static void ax25_send_iframe(ax25_cb *ax25, struct sk_buff *skb, int poll_bit)
  168. {
  169. unsigned char *frame;
  170. if (skb == NULL)
  171. return;
  172. skb->nh.raw = skb->data;
  173. if (ax25->modulus == AX25_MODULUS) {
  174. frame = skb_push(skb, 1);
  175. *frame = AX25_I;
  176. *frame |= (poll_bit) ? AX25_PF : 0;
  177. *frame |= (ax25->vr << 5);
  178. *frame |= (ax25->vs << 1);
  179. } else {
  180. frame = skb_push(skb, 2);
  181. frame[0] = AX25_I;
  182. frame[0] |= (ax25->vs << 1);
  183. frame[1] = (poll_bit) ? AX25_EPF : 0;
  184. frame[1] |= (ax25->vr << 1);
  185. }
  186. ax25_start_idletimer(ax25);
  187. ax25_transmit_buffer(ax25, skb, AX25_COMMAND);
  188. }
  189. void ax25_kick(ax25_cb *ax25)
  190. {
  191. struct sk_buff *skb, *skbn;
  192. int last = 1;
  193. unsigned short start, end, next;
  194. if (ax25->state != AX25_STATE_3 && ax25->state != AX25_STATE_4)
  195. return;
  196. if (ax25->condition & AX25_COND_PEER_RX_BUSY)
  197. return;
  198. if (skb_peek(&ax25->write_queue) == NULL)
  199. return;
  200. start = (skb_peek(&ax25->ack_queue) == NULL) ? ax25->va : ax25->vs;
  201. end = (ax25->va + ax25->window) % ax25->modulus;
  202. if (start == end)
  203. return;
  204. ax25->vs = start;
  205. /*
  206. * Transmit data until either we're out of data to send or
  207. * the window is full. Send a poll on the final I frame if
  208. * the window is filled.
  209. */
  210. /*
  211. * Dequeue the frame and copy it.
  212. */
  213. skb = skb_dequeue(&ax25->write_queue);
  214. do {
  215. if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  216. skb_queue_head(&ax25->write_queue, skb);
  217. break;
  218. }
  219. if (skb->sk != NULL)
  220. skb_set_owner_w(skbn, skb->sk);
  221. next = (ax25->vs + 1) % ax25->modulus;
  222. last = (next == end);
  223. /*
  224. * Transmit the frame copy.
  225. * bke 960114: do not set the Poll bit on the last frame
  226. * in DAMA mode.
  227. */
  228. switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
  229. case AX25_PROTO_STD_SIMPLEX:
  230. case AX25_PROTO_STD_DUPLEX:
  231. ax25_send_iframe(ax25, skbn, (last) ? AX25_POLLON : AX25_POLLOFF);
  232. break;
  233. #ifdef CONFIG_AX25_DAMA_SLAVE
  234. case AX25_PROTO_DAMA_SLAVE:
  235. ax25_send_iframe(ax25, skbn, AX25_POLLOFF);
  236. break;
  237. #endif
  238. }
  239. ax25->vs = next;
  240. /*
  241. * Requeue the original data frame.
  242. */
  243. skb_queue_tail(&ax25->ack_queue, skb);
  244. } while (!last && (skb = skb_dequeue(&ax25->write_queue)) != NULL);
  245. ax25->condition &= ~AX25_COND_ACK_PENDING;
  246. if (!ax25_t1timer_running(ax25)) {
  247. ax25_stop_t3timer(ax25);
  248. ax25_calculate_t1(ax25);
  249. ax25_start_t1timer(ax25);
  250. }
  251. }
  252. void ax25_transmit_buffer(ax25_cb *ax25, struct sk_buff *skb, int type)
  253. {
  254. struct sk_buff *skbn;
  255. unsigned char *ptr;
  256. int headroom;
  257. if (ax25->ax25_dev == NULL) {
  258. ax25_disconnect(ax25, ENETUNREACH);
  259. return;
  260. }
  261. headroom = ax25_addr_size(ax25->digipeat);
  262. if (skb_headroom(skb) < headroom) {
  263. if ((skbn = skb_realloc_headroom(skb, headroom)) == NULL) {
  264. printk(KERN_CRIT "AX.25: ax25_transmit_buffer - out of memory\n");
  265. kfree_skb(skb);
  266. return;
  267. }
  268. if (skb->sk != NULL)
  269. skb_set_owner_w(skbn, skb->sk);
  270. kfree_skb(skb);
  271. skb = skbn;
  272. }
  273. ptr = skb_push(skb, headroom);
  274. ax25_addr_build(ptr, &ax25->source_addr, &ax25->dest_addr, ax25->digipeat, type, ax25->modulus);
  275. ax25_queue_xmit(skb, ax25->ax25_dev->dev);
  276. }
  277. /*
  278. * A small shim to dev_queue_xmit to add the KISS control byte, and do
  279. * any packet forwarding in operation.
  280. */
  281. void ax25_queue_xmit(struct sk_buff *skb, struct net_device *dev)
  282. {
  283. unsigned char *ptr;
  284. skb->protocol = ax25_type_trans(skb, ax25_fwd_dev(dev));
  285. ptr = skb_push(skb, 1);
  286. *ptr = 0x00; /* KISS */
  287. dev_queue_xmit(skb);
  288. }
  289. int ax25_check_iframes_acked(ax25_cb *ax25, unsigned short nr)
  290. {
  291. if (ax25->vs == nr) {
  292. ax25_frames_acked(ax25, nr);
  293. ax25_calculate_rtt(ax25);
  294. ax25_stop_t1timer(ax25);
  295. ax25_start_t3timer(ax25);
  296. return 1;
  297. } else {
  298. if (ax25->va != nr) {
  299. ax25_frames_acked(ax25, nr);
  300. ax25_calculate_t1(ax25);
  301. ax25_start_t1timer(ax25);
  302. return 1;
  303. }
  304. }
  305. return 0;
  306. }