ax25_in.c 11 KB

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