ax25_out.c 8.9 KB

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