ax25_in.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <net/ax25.h>
  22. #include <linux/inet.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/netfilter.h>
  26. #include <net/sock.h>
  27. #include <net/tcp_states.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/system.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_reset_mac_header(skb);
  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. kfree_skb(skb);
  177. return 0;
  178. }
  179. /*
  180. * Parse the address header.
  181. */
  182. if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL) {
  183. kfree_skb(skb);
  184. return 0;
  185. }
  186. /*
  187. * Ours perhaps ?
  188. */
  189. if (dp.lastrepeat + 1 < dp.ndigi) /* Not yet digipeated completely */
  190. next_digi = &dp.calls[dp.lastrepeat + 1];
  191. /*
  192. * Pull of the AX.25 headers leaving the CTRL/PID bytes
  193. */
  194. skb_pull(skb, ax25_addr_size(&dp));
  195. /* For our port addresses ? */
  196. if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
  197. mine = 1;
  198. /* Also match on any registered callsign from L3/4 */
  199. if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
  200. mine = 1;
  201. /* UI frame - bypass LAPB processing */
  202. if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
  203. skb_set_transport_header(skb, 2); /* skip control and pid */
  204. ax25_send_to_raw(&dest, skb, skb->data[1]);
  205. if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
  206. kfree_skb(skb);
  207. return 0;
  208. }
  209. /* Now we are pointing at the pid byte */
  210. switch (skb->data[1]) {
  211. case AX25_P_IP:
  212. skb_pull(skb,2); /* drop PID/CTRL */
  213. skb_reset_transport_header(skb);
  214. skb_reset_network_header(skb);
  215. skb->dev = dev;
  216. skb->pkt_type = PACKET_HOST;
  217. skb->protocol = htons(ETH_P_IP);
  218. netif_rx(skb);
  219. break;
  220. case AX25_P_ARP:
  221. skb_pull(skb,2);
  222. skb_reset_transport_header(skb);
  223. skb_reset_network_header(skb);
  224. skb->dev = dev;
  225. skb->pkt_type = PACKET_HOST;
  226. skb->protocol = htons(ETH_P_ARP);
  227. netif_rx(skb);
  228. break;
  229. case AX25_P_TEXT:
  230. /* Now find a suitable dgram socket */
  231. sk = ax25_get_socket(&dest, &src, SOCK_DGRAM);
  232. if (sk != NULL) {
  233. bh_lock_sock(sk);
  234. if (atomic_read(&sk->sk_rmem_alloc) >=
  235. sk->sk_rcvbuf) {
  236. kfree_skb(skb);
  237. } else {
  238. /*
  239. * Remove the control and PID.
  240. */
  241. skb_pull(skb, 2);
  242. if (sock_queue_rcv_skb(sk, skb) != 0)
  243. kfree_skb(skb);
  244. }
  245. bh_unlock_sock(sk);
  246. sock_put(sk);
  247. } else {
  248. kfree_skb(skb);
  249. }
  250. break;
  251. default:
  252. kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
  253. break;
  254. }
  255. return 0;
  256. }
  257. /*
  258. * Is connected mode supported on this device ?
  259. * If not, should we DM the incoming frame (except DMs) or
  260. * silently ignore them. For now we stay quiet.
  261. */
  262. if (ax25_dev->values[AX25_VALUES_CONMODE] == 0) {
  263. kfree_skb(skb);
  264. return 0;
  265. }
  266. /* LAPB */
  267. /* AX.25 state 1-4 */
  268. ax25_digi_invert(&dp, &reverse_dp);
  269. if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
  270. /*
  271. * Process the frame. If it is queued up internally it
  272. * returns one otherwise we free it immediately. This
  273. * routine itself wakes the user context layers so we do
  274. * no further work
  275. */
  276. if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
  277. kfree_skb(skb);
  278. ax25_cb_put(ax25);
  279. return 0;
  280. }
  281. /* AX.25 state 0 (disconnected) */
  282. /* a) received not a SABM(E) */
  283. if ((*skb->data & ~AX25_PF) != AX25_SABM &&
  284. (*skb->data & ~AX25_PF) != AX25_SABME) {
  285. /*
  286. * Never reply to a DM. Also ignore any connects for
  287. * addresses that are not our interfaces and not a socket.
  288. */
  289. if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
  290. ax25_return_dm(dev, &src, &dest, &dp);
  291. kfree_skb(skb);
  292. return 0;
  293. }
  294. /* b) received SABM(E) */
  295. if (dp.lastrepeat + 1 == dp.ndigi)
  296. sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
  297. else
  298. sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
  299. if (sk != NULL) {
  300. bh_lock_sock(sk);
  301. if (sk_acceptq_is_full(sk) ||
  302. (make = ax25_make_new(sk, ax25_dev)) == NULL) {
  303. if (mine)
  304. ax25_return_dm(dev, &src, &dest, &dp);
  305. kfree_skb(skb);
  306. bh_unlock_sock(sk);
  307. sock_put(sk);
  308. return 0;
  309. }
  310. ax25 = ax25_sk(make);
  311. skb_set_owner_r(skb, make);
  312. skb_queue_head(&sk->sk_receive_queue, skb);
  313. make->sk_state = TCP_ESTABLISHED;
  314. sk->sk_ack_backlog++;
  315. bh_unlock_sock(sk);
  316. } else {
  317. if (!mine) {
  318. kfree_skb(skb);
  319. return 0;
  320. }
  321. if ((ax25 = ax25_create_cb()) == NULL) {
  322. ax25_return_dm(dev, &src, &dest, &dp);
  323. kfree_skb(skb);
  324. return 0;
  325. }
  326. ax25_fillin_cb(ax25, ax25_dev);
  327. }
  328. ax25->source_addr = dest;
  329. ax25->dest_addr = src;
  330. /*
  331. * Sort out any digipeated paths.
  332. */
  333. if (dp.ndigi && !ax25->digipeat &&
  334. (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  335. kfree_skb(skb);
  336. ax25_destroy_socket(ax25);
  337. if (sk)
  338. sock_put(sk);
  339. return 0;
  340. }
  341. if (dp.ndigi == 0) {
  342. kfree(ax25->digipeat);
  343. ax25->digipeat = NULL;
  344. } else {
  345. /* Reverse the source SABM's path */
  346. memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
  347. }
  348. if ((*skb->data & ~AX25_PF) == AX25_SABME) {
  349. ax25->modulus = AX25_EMODULUS;
  350. ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
  351. } else {
  352. ax25->modulus = AX25_MODULUS;
  353. ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
  354. }
  355. ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
  356. #ifdef CONFIG_AX25_DAMA_SLAVE
  357. if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
  358. ax25_dama_on(ax25);
  359. #endif
  360. ax25->state = AX25_STATE_3;
  361. ax25_cb_add(ax25);
  362. ax25_start_heartbeat(ax25);
  363. ax25_start_t3timer(ax25);
  364. ax25_start_idletimer(ax25);
  365. if (sk) {
  366. if (!sock_flag(sk, SOCK_DEAD))
  367. sk->sk_data_ready(sk, skb->len);
  368. sock_put(sk);
  369. } else
  370. kfree_skb(skb);
  371. return 0;
  372. }
  373. /*
  374. * Receive an AX.25 frame via a SLIP interface.
  375. */
  376. int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
  377. struct packet_type *ptype, struct net_device *orig_dev)
  378. {
  379. skb->sk = NULL; /* Initially we don't know who it's for */
  380. skb->destructor = NULL; /* Who initializes this, dammit?! */
  381. if (dev->nd_net != &init_net) {
  382. kfree_skb(skb);
  383. return 0;
  384. }
  385. if ((*skb->data & 0x0F) != 0) {
  386. kfree_skb(skb); /* Not a KISS data frame */
  387. return 0;
  388. }
  389. skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
  390. return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
  391. }