ax25_in.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:12k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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