ax25_in.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
  11. */
  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/timer.h>
  18. #include <linux/string.h>
  19. #include <linux/sockios.h>
  20. #include <linux/net.h>
  21. #include <linux/slab.h>
  22. #include <net/ax25.h>
  23. #include <linux/inet.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <net/sock.h>
  27. #include <net/tcp_states.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. /*
  33. * Given a fragment, queue it on the fragment queue and if the fragment
  34. * is complete, send it back to ax25_rx_iframe.
  35. */
  36. static int ax25_rx_fragment(ax25_cb *ax25, struct sk_buff *skb)
  37. {
  38. struct sk_buff *skbn, *skbo;
  39. if (ax25->fragno != 0) {
  40. if (!(*skb->data & AX25_SEG_FIRST)) {
  41. if ((ax25->fragno - 1) == (*skb->data & AX25_SEG_REM)) {
  42. /* Enqueue fragment */
  43. ax25->fragno = *skb->data & AX25_SEG_REM;
  44. skb_pull(skb, 1); /* skip fragno */
  45. ax25->fraglen += skb->len;
  46. skb_queue_tail(&ax25->frag_queue, skb);
  47. /* Last fragment received ? */
  48. if (ax25->fragno == 0) {
  49. skbn = alloc_skb(AX25_MAX_HEADER_LEN +
  50. ax25->fraglen,
  51. GFP_ATOMIC);
  52. if (!skbn) {
  53. skb_queue_purge(&ax25->frag_queue);
  54. return 1;
  55. }
  56. skb_reserve(skbn, AX25_MAX_HEADER_LEN);
  57. skbn->dev = ax25->ax25_dev->dev;
  58. skb_reset_network_header(skbn);
  59. skb_reset_transport_header(skbn);
  60. /* Copy data from the fragments */
  61. while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
  62. skb_copy_from_linear_data(skbo,
  63. skb_put(skbn, skbo->len),
  64. skbo->len);
  65. kfree_skb(skbo);
  66. }
  67. ax25->fraglen = 0;
  68. if (ax25_rx_iframe(ax25, skbn) == 0)
  69. kfree_skb(skbn);
  70. }
  71. return 1;
  72. }
  73. }
  74. } else {
  75. /* First fragment received */
  76. if (*skb->data & AX25_SEG_FIRST) {
  77. skb_queue_purge(&ax25->frag_queue);
  78. ax25->fragno = *skb->data & AX25_SEG_REM;
  79. skb_pull(skb, 1); /* skip fragno */
  80. ax25->fraglen = skb->len;
  81. skb_queue_tail(&ax25->frag_queue, skb);
  82. return 1;
  83. }
  84. }
  85. return 0;
  86. }
  87. /*
  88. * This is where all valid I frames are sent to, to be dispatched to
  89. * whichever protocol requires them.
  90. */
  91. int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
  92. {
  93. int (*func)(struct sk_buff *, ax25_cb *);
  94. unsigned char pid;
  95. int queued = 0;
  96. if (skb == NULL) return 0;
  97. ax25_start_idletimer(ax25);
  98. pid = *skb->data;
  99. if (pid == AX25_P_IP) {
  100. /* working around a TCP bug to keep additional listeners
  101. * happy. TCP re-uses the buffer and destroys the original
  102. * content.
  103. */
  104. struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
  105. if (skbn != NULL) {
  106. kfree_skb(skb);
  107. skb = skbn;
  108. }
  109. skb_pull(skb, 1); /* Remove PID */
  110. skb->mac_header = skb->network_header;
  111. skb_reset_network_header(skb);
  112. skb->dev = ax25->ax25_dev->dev;
  113. skb->pkt_type = PACKET_HOST;
  114. skb->protocol = htons(ETH_P_IP);
  115. netif_rx(skb);
  116. return 1;
  117. }
  118. if (pid == AX25_P_SEGMENT) {
  119. skb_pull(skb, 1); /* Remove PID */
  120. return ax25_rx_fragment(ax25, skb);
  121. }
  122. if ((func = ax25_protocol_function(pid)) != NULL) {
  123. skb_pull(skb, 1); /* Remove PID */
  124. return (*func)(skb, ax25);
  125. }
  126. if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
  127. if ((!ax25->pidincl && ax25->sk->sk_protocol == pid) ||
  128. ax25->pidincl) {
  129. if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
  130. queued = 1;
  131. else
  132. ax25->condition |= AX25_COND_OWN_RX_BUSY;
  133. }
  134. }
  135. return queued;
  136. }
  137. /*
  138. * Higher level upcall for a LAPB frame
  139. */
  140. static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
  141. {
  142. int queued = 0;
  143. if (ax25->state == AX25_STATE_0)
  144. return 0;
  145. switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
  146. case AX25_PROTO_STD_SIMPLEX:
  147. case AX25_PROTO_STD_DUPLEX:
  148. queued = ax25_std_frame_in(ax25, skb, type);
  149. break;
  150. #ifdef CONFIG_AX25_DAMA_SLAVE
  151. case AX25_PROTO_DAMA_SLAVE:
  152. if (dama || ax25->ax25_dev->dama.slave)
  153. queued = ax25_ds_frame_in(ax25, skb, type);
  154. else
  155. queued = ax25_std_frame_in(ax25, skb, type);
  156. break;
  157. #endif
  158. }
  159. return queued;
  160. }
  161. static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,
  162. ax25_address *dev_addr, struct packet_type *ptype)
  163. {
  164. ax25_address src, dest, *next_digi = NULL;
  165. int type = 0, mine = 0, dama;
  166. struct sock *make, *sk;
  167. ax25_digi dp, reverse_dp;
  168. ax25_cb *ax25;
  169. ax25_dev *ax25_dev;
  170. /*
  171. * Process the AX.25/LAPB frame.
  172. */
  173. skb_reset_transport_header(skb);
  174. if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
  175. goto free;
  176. /*
  177. * Parse the address header.
  178. */
  179. if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL)
  180. goto free;
  181. /*
  182. * Ours perhaps ?
  183. */
  184. if (dp.lastrepeat + 1 < dp.ndigi) /* Not yet digipeated completely */
  185. next_digi = &dp.calls[dp.lastrepeat + 1];
  186. /*
  187. * Pull of the AX.25 headers leaving the CTRL/PID bytes
  188. */
  189. skb_pull(skb, ax25_addr_size(&dp));
  190. /* For our port addresses ? */
  191. if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
  192. mine = 1;
  193. /* Also match on any registered callsign from L3/4 */
  194. if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
  195. mine = 1;
  196. /* UI frame - bypass LAPB processing */
  197. if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
  198. skb_set_transport_header(skb, 2); /* skip control and pid */
  199. ax25_send_to_raw(&dest, skb, skb->data[1]);
  200. if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0)
  201. goto free;
  202. /* Now we are pointing at the pid byte */
  203. switch (skb->data[1]) {
  204. case AX25_P_IP:
  205. skb_pull(skb,2); /* drop PID/CTRL */
  206. skb_reset_transport_header(skb);
  207. skb_reset_network_header(skb);
  208. skb->dev = dev;
  209. skb->pkt_type = PACKET_HOST;
  210. skb->protocol = htons(ETH_P_IP);
  211. netif_rx(skb);
  212. break;
  213. case AX25_P_ARP:
  214. skb_pull(skb,2);
  215. skb_reset_transport_header(skb);
  216. skb_reset_network_header(skb);
  217. skb->dev = dev;
  218. skb->pkt_type = PACKET_HOST;
  219. skb->protocol = htons(ETH_P_ARP);
  220. netif_rx(skb);
  221. break;
  222. case AX25_P_TEXT:
  223. /* Now find a suitable dgram socket */
  224. sk = ax25_get_socket(&dest, &src, SOCK_DGRAM);
  225. if (sk != NULL) {
  226. bh_lock_sock(sk);
  227. if (atomic_read(&sk->sk_rmem_alloc) >=
  228. sk->sk_rcvbuf) {
  229. kfree_skb(skb);
  230. } else {
  231. /*
  232. * Remove the control and PID.
  233. */
  234. skb_pull(skb, 2);
  235. if (sock_queue_rcv_skb(sk, skb) != 0)
  236. kfree_skb(skb);
  237. }
  238. bh_unlock_sock(sk);
  239. sock_put(sk);
  240. } else {
  241. kfree_skb(skb);
  242. }
  243. break;
  244. default:
  245. kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
  246. break;
  247. }
  248. return 0;
  249. }
  250. /*
  251. * Is connected mode supported on this device ?
  252. * If not, should we DM the incoming frame (except DMs) or
  253. * silently ignore them. For now we stay quiet.
  254. */
  255. if (ax25_dev->values[AX25_VALUES_CONMODE] == 0)
  256. goto free;
  257. /* LAPB */
  258. /* AX.25 state 1-4 */
  259. ax25_digi_invert(&dp, &reverse_dp);
  260. if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
  261. /*
  262. * Process the frame. If it is queued up internally it
  263. * returns one otherwise we free it immediately. This
  264. * routine itself wakes the user context layers so we do
  265. * no further work
  266. */
  267. if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
  268. kfree_skb(skb);
  269. ax25_cb_put(ax25);
  270. return 0;
  271. }
  272. /* AX.25 state 0 (disconnected) */
  273. /* a) received not a SABM(E) */
  274. if ((*skb->data & ~AX25_PF) != AX25_SABM &&
  275. (*skb->data & ~AX25_PF) != AX25_SABME) {
  276. /*
  277. * Never reply to a DM. Also ignore any connects for
  278. * addresses that are not our interfaces and not a socket.
  279. */
  280. if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
  281. ax25_return_dm(dev, &src, &dest, &dp);
  282. goto free;
  283. }
  284. /* b) received SABM(E) */
  285. if (dp.lastrepeat + 1 == dp.ndigi)
  286. sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
  287. else
  288. sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
  289. if (sk != NULL) {
  290. bh_lock_sock(sk);
  291. if (sk_acceptq_is_full(sk) ||
  292. (make = ax25_make_new(sk, ax25_dev)) == NULL) {
  293. if (mine)
  294. ax25_return_dm(dev, &src, &dest, &dp);
  295. kfree_skb(skb);
  296. bh_unlock_sock(sk);
  297. sock_put(sk);
  298. return 0;
  299. }
  300. ax25 = sk_to_ax25(make);
  301. skb_set_owner_r(skb, make);
  302. skb_queue_head(&sk->sk_receive_queue, skb);
  303. make->sk_state = TCP_ESTABLISHED;
  304. sk->sk_ack_backlog++;
  305. bh_unlock_sock(sk);
  306. } else {
  307. if (!mine)
  308. goto free;
  309. if ((ax25 = ax25_create_cb()) == NULL) {
  310. ax25_return_dm(dev, &src, &dest, &dp);
  311. goto free;
  312. }
  313. ax25_fillin_cb(ax25, ax25_dev);
  314. }
  315. ax25->source_addr = dest;
  316. ax25->dest_addr = src;
  317. /*
  318. * Sort out any digipeated paths.
  319. */
  320. if (dp.ndigi && !ax25->digipeat &&
  321. (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  322. kfree_skb(skb);
  323. ax25_destroy_socket(ax25);
  324. if (sk)
  325. sock_put(sk);
  326. return 0;
  327. }
  328. if (dp.ndigi == 0) {
  329. kfree(ax25->digipeat);
  330. ax25->digipeat = NULL;
  331. } else {
  332. /* Reverse the source SABM's path */
  333. memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
  334. }
  335. if ((*skb->data & ~AX25_PF) == AX25_SABME) {
  336. ax25->modulus = AX25_EMODULUS;
  337. ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
  338. } else {
  339. ax25->modulus = AX25_MODULUS;
  340. ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
  341. }
  342. ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
  343. #ifdef CONFIG_AX25_DAMA_SLAVE
  344. if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
  345. ax25_dama_on(ax25);
  346. #endif
  347. ax25->state = AX25_STATE_3;
  348. ax25_cb_add(ax25);
  349. ax25_start_heartbeat(ax25);
  350. ax25_start_t3timer(ax25);
  351. ax25_start_idletimer(ax25);
  352. if (sk) {
  353. if (!sock_flag(sk, SOCK_DEAD))
  354. sk->sk_data_ready(sk);
  355. sock_put(sk);
  356. } else {
  357. free:
  358. kfree_skb(skb);
  359. }
  360. return 0;
  361. }
  362. /*
  363. * Receive an AX.25 frame via a SLIP interface.
  364. */
  365. int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
  366. struct packet_type *ptype, struct net_device *orig_dev)
  367. {
  368. skb_orphan(skb);
  369. if (!net_eq(dev_net(dev), &init_net)) {
  370. kfree_skb(skb);
  371. return 0;
  372. }
  373. if ((*skb->data & 0x0F) != 0) {
  374. kfree_skb(skb); /* Not a KISS data frame */
  375. return 0;
  376. }
  377. skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
  378. return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
  379. }