ax25_in.c 11 KB

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