ax25_in.c 10 KB

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