ax25_in.c 11 KB

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