ax25_in.c 11 KB

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