mkiss.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*
  2. * MKISS Driver
  3. *
  4. * This module:
  5. * This module is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * This module implements the AX.25 protocol for kernel-based
  11. * devices like TTYs. It interfaces between a raw TTY, and the
  12. * kernel's AX.25 protocol layers, just like slip.c.
  13. * AX.25 needs to be separated from slip.c while slip.c is no
  14. * longer a static kernel device since it is a module.
  15. * This method clears the way to implement other kiss protocols
  16. * like mkiss smack g8bpq ..... so far only mkiss is implemented.
  17. *
  18. * Hans Alblas <hans@esrac.ele.tue.nl>
  19. *
  20. * History
  21. * Jonathan (G4KLX) Fixed to match Linux networking changes - 2.1.15.
  22. * Matthias (DG2FEF) Added support for FlexNet CRC (on special request)
  23. * Fixed bug in ax25_close(): dev_lock_wait() was
  24. * called twice, causing a deadlock.
  25. * Jeroen (PE1RXQ) Removed old MKISS_MAGIC stuff and calls to
  26. * MOD_*_USE_COUNT
  27. * Remove cli() and fix rtnl lock usage.
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <asm/system.h>
  32. #include <linux/bitops.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/string.h>
  35. #include <linux/mm.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/in.h>
  38. #include <linux/inet.h>
  39. #include <linux/tty.h>
  40. #include <linux/errno.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/major.h>
  43. #include <linux/init.h>
  44. #include <linux/rtnetlink.h>
  45. #include <linux/etherdevice.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/if_arp.h>
  48. #include <linux/jiffies.h>
  49. #include <net/ax25.h>
  50. #include "mkiss.h"
  51. #ifdef CONFIG_INET
  52. #include <linux/ip.h>
  53. #include <linux/tcp.h>
  54. #endif
  55. static char banner[] __initdata = KERN_INFO "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
  56. typedef struct ax25_ctrl {
  57. struct ax_disp ctrl; /* */
  58. struct net_device dev; /* the device */
  59. } ax25_ctrl_t;
  60. static ax25_ctrl_t **ax25_ctrls;
  61. int ax25_maxdev = AX25_MAXDEV; /* Can be overridden with insmod! */
  62. static struct tty_ldisc ax_ldisc;
  63. static int ax25_init(struct net_device *);
  64. static int kiss_esc(unsigned char *, unsigned char *, int);
  65. static int kiss_esc_crc(unsigned char *, unsigned char *, unsigned short, int);
  66. static void kiss_unesc(struct ax_disp *, unsigned char);
  67. /*---------------------------------------------------------------------------*/
  68. static const unsigned short Crc_flex_table[] = {
  69. 0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
  70. 0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
  71. 0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
  72. 0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
  73. 0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
  74. 0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
  75. 0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
  76. 0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
  77. 0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
  78. 0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
  79. 0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
  80. 0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
  81. 0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
  82. 0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
  83. 0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
  84. 0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
  85. 0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
  86. 0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
  87. 0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
  88. 0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
  89. 0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
  90. 0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
  91. 0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
  92. 0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
  93. 0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
  94. 0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
  95. 0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
  96. 0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
  97. 0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
  98. 0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
  99. 0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
  100. 0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
  101. };
  102. /*---------------------------------------------------------------------------*/
  103. static unsigned short calc_crc_flex(unsigned char *cp, int size)
  104. {
  105. unsigned short crc = 0xffff;
  106. while (size--)
  107. crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
  108. return crc;
  109. }
  110. /*---------------------------------------------------------------------------*/
  111. static int check_crc_flex(unsigned char *cp, int size)
  112. {
  113. unsigned short crc = 0xffff;
  114. if (size < 3)
  115. return -1;
  116. while (size--)
  117. crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
  118. if ((crc & 0xffff) != 0x7070)
  119. return -1;
  120. return 0;
  121. }
  122. /*---------------------------------------------------------------------------*/
  123. /* Find a free channel, and link in this `tty' line. */
  124. static inline struct ax_disp *ax_alloc(void)
  125. {
  126. ax25_ctrl_t *axp=NULL;
  127. int i;
  128. for (i = 0; i < ax25_maxdev; i++) {
  129. axp = ax25_ctrls[i];
  130. /* Not allocated ? */
  131. if (axp == NULL)
  132. break;
  133. /* Not in use ? */
  134. if (!test_and_set_bit(AXF_INUSE, &axp->ctrl.flags))
  135. break;
  136. }
  137. /* Sorry, too many, all slots in use */
  138. if (i >= ax25_maxdev)
  139. return NULL;
  140. /* If no channels are available, allocate one */
  141. if (axp == NULL && (ax25_ctrls[i] = kmalloc(sizeof(ax25_ctrl_t), GFP_KERNEL)) != NULL) {
  142. axp = ax25_ctrls[i];
  143. }
  144. memset(axp, 0, sizeof(ax25_ctrl_t));
  145. /* Initialize channel control data */
  146. set_bit(AXF_INUSE, &axp->ctrl.flags);
  147. sprintf(axp->dev.name, "ax%d", i++);
  148. axp->ctrl.tty = NULL;
  149. axp->dev.base_addr = i;
  150. axp->dev.priv = (void *)&axp->ctrl;
  151. axp->dev.next = NULL;
  152. axp->dev.init = ax25_init;
  153. if (axp != NULL) {
  154. /*
  155. * register device so that it can be ifconfig'ed
  156. * ax25_init() will be called as a side-effect
  157. * SIDE-EFFECT WARNING: ax25_init() CLEARS axp->ctrl !
  158. */
  159. if (register_netdev(&axp->dev) == 0) {
  160. /* (Re-)Set the INUSE bit. Very Important! */
  161. set_bit(AXF_INUSE, &axp->ctrl.flags);
  162. axp->ctrl.dev = &axp->dev;
  163. axp->dev.priv = (void *) &axp->ctrl;
  164. return &axp->ctrl;
  165. } else {
  166. clear_bit(AXF_INUSE,&axp->ctrl.flags);
  167. printk(KERN_ERR "mkiss: ax_alloc() - register_netdev() failure.\n");
  168. }
  169. }
  170. return NULL;
  171. }
  172. /* Free an AX25 channel. */
  173. static inline void ax_free(struct ax_disp *ax)
  174. {
  175. /* Free all AX25 frame buffers. */
  176. if (ax->rbuff)
  177. kfree(ax->rbuff);
  178. ax->rbuff = NULL;
  179. if (ax->xbuff)
  180. kfree(ax->xbuff);
  181. ax->xbuff = NULL;
  182. if (!test_and_clear_bit(AXF_INUSE, &ax->flags))
  183. printk(KERN_ERR "mkiss: %s: ax_free for already free unit.\n", ax->dev->name);
  184. }
  185. static void ax_changedmtu(struct ax_disp *ax)
  186. {
  187. struct net_device *dev = ax->dev;
  188. unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
  189. int len;
  190. len = dev->mtu * 2;
  191. /*
  192. * allow for arrival of larger UDP packets, even if we say not to
  193. * also fixes a bug in which SunOS sends 512-byte packets even with
  194. * an MSS of 128
  195. */
  196. if (len < 576 * 2)
  197. len = 576 * 2;
  198. xbuff = kmalloc(len + 4, GFP_ATOMIC);
  199. rbuff = kmalloc(len + 4, GFP_ATOMIC);
  200. if (xbuff == NULL || rbuff == NULL) {
  201. printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, MTU change cancelled.\n",
  202. ax->dev->name);
  203. dev->mtu = ax->mtu;
  204. if (xbuff != NULL)
  205. kfree(xbuff);
  206. if (rbuff != NULL)
  207. kfree(rbuff);
  208. return;
  209. }
  210. spin_lock_bh(&ax->buflock);
  211. oxbuff = ax->xbuff;
  212. ax->xbuff = xbuff;
  213. orbuff = ax->rbuff;
  214. ax->rbuff = rbuff;
  215. if (ax->xleft) {
  216. if (ax->xleft <= len) {
  217. memcpy(ax->xbuff, ax->xhead, ax->xleft);
  218. } else {
  219. ax->xleft = 0;
  220. ax->tx_dropped++;
  221. }
  222. }
  223. ax->xhead = ax->xbuff;
  224. if (ax->rcount) {
  225. if (ax->rcount <= len) {
  226. memcpy(ax->rbuff, orbuff, ax->rcount);
  227. } else {
  228. ax->rcount = 0;
  229. ax->rx_over_errors++;
  230. set_bit(AXF_ERROR, &ax->flags);
  231. }
  232. }
  233. ax->mtu = dev->mtu + 73;
  234. ax->buffsize = len;
  235. spin_unlock_bh(&ax->buflock);
  236. if (oxbuff != NULL)
  237. kfree(oxbuff);
  238. if (orbuff != NULL)
  239. kfree(orbuff);
  240. }
  241. /* Set the "sending" flag. This must be atomic. */
  242. static inline void ax_lock(struct ax_disp *ax)
  243. {
  244. netif_stop_queue(ax->dev);
  245. }
  246. /* Clear the "sending" flag. This must be atomic. */
  247. static inline void ax_unlock(struct ax_disp *ax)
  248. {
  249. netif_start_queue(ax->dev);
  250. }
  251. /* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
  252. static void ax_bump(struct ax_disp *ax)
  253. {
  254. struct sk_buff *skb;
  255. int count;
  256. spin_lock_bh(&ax->buflock);
  257. if (ax->rbuff[0] > 0x0f) {
  258. if (ax->rbuff[0] & 0x20) {
  259. ax->crcmode = CRC_MODE_FLEX;
  260. if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
  261. ax->rx_errors++;
  262. return;
  263. }
  264. ax->rcount -= 2;
  265. /* dl9sau bugfix: the trailling two bytes flexnet crc
  266. * will not be passed to the kernel. thus we have
  267. * to correct the kissparm signature, because it
  268. * indicates a crc but there's none
  269. */
  270. *ax->rbuff &= ~0x20;
  271. }
  272. }
  273. spin_unlock_bh(&ax->buflock);
  274. count = ax->rcount;
  275. if ((skb = dev_alloc_skb(count)) == NULL) {
  276. printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n", ax->dev->name);
  277. ax->rx_dropped++;
  278. return;
  279. }
  280. spin_lock_bh(&ax->buflock);
  281. memcpy(skb_put(skb,count), ax->rbuff, count);
  282. spin_unlock_bh(&ax->buflock);
  283. skb->protocol = ax25_type_trans(skb, ax->dev);
  284. netif_rx(skb);
  285. ax->dev->last_rx = jiffies;
  286. ax->rx_packets++;
  287. ax->rx_bytes+=count;
  288. }
  289. /* Encapsulate one AX.25 packet and stuff into a TTY queue. */
  290. static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
  291. {
  292. unsigned char *p;
  293. int actual, count;
  294. if (ax->mtu != ax->dev->mtu + 73) /* Someone has been ifconfigging */
  295. ax_changedmtu(ax);
  296. if (len > ax->mtu) { /* Sigh, shouldn't occur BUT ... */
  297. len = ax->mtu;
  298. printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
  299. ax->tx_dropped++;
  300. ax_unlock(ax);
  301. return;
  302. }
  303. p = icp;
  304. spin_lock_bh(&ax->buflock);
  305. switch (ax->crcmode) {
  306. unsigned short crc;
  307. case CRC_MODE_FLEX:
  308. *p |= 0x20;
  309. crc = calc_crc_flex(p, len);
  310. count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
  311. break;
  312. default:
  313. count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
  314. break;
  315. }
  316. ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
  317. actual = ax->tty->driver->write(ax->tty, ax->xbuff, count);
  318. ax->tx_packets++;
  319. ax->tx_bytes+=actual;
  320. ax->dev->trans_start = jiffies;
  321. ax->xleft = count - actual;
  322. ax->xhead = ax->xbuff + actual;
  323. spin_unlock_bh(&ax->buflock);
  324. }
  325. /*
  326. * Called by the driver when there's room for more data. If we have
  327. * more packets to send, we send them here.
  328. */
  329. static void ax25_write_wakeup(struct tty_struct *tty)
  330. {
  331. int actual;
  332. struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
  333. /* First make sure we're connected. */
  334. if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
  335. return;
  336. if (ax->xleft <= 0) {
  337. /* Now serial buffer is almost free & we can start
  338. * transmission of another packet
  339. */
  340. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  341. netif_wake_queue(ax->dev);
  342. return;
  343. }
  344. actual = tty->driver->write(tty, ax->xhead, ax->xleft);
  345. ax->xleft -= actual;
  346. ax->xhead += actual;
  347. }
  348. /* Encapsulate an AX.25 packet and kick it into a TTY queue. */
  349. static int ax_xmit(struct sk_buff *skb, struct net_device *dev)
  350. {
  351. struct ax_disp *ax = netdev_priv(dev);
  352. if (!netif_running(dev)) {
  353. printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
  354. return 1;
  355. }
  356. if (netif_queue_stopped(dev)) {
  357. /*
  358. * May be we must check transmitter timeout here ?
  359. * 14 Oct 1994 Dmitry Gorodchanin.
  360. */
  361. if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
  362. /* 20 sec timeout not reached */
  363. return 1;
  364. }
  365. printk(KERN_ERR "mkiss: %s: transmit timed out, %s?\n", dev->name,
  366. (ax->tty->driver->chars_in_buffer(ax->tty) || ax->xleft) ?
  367. "bad line quality" : "driver error");
  368. ax->xleft = 0;
  369. ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  370. ax_unlock(ax);
  371. }
  372. /* We were not busy, so we are now... :-) */
  373. if (skb != NULL) {
  374. ax_lock(ax);
  375. ax_encaps(ax, skb->data, skb->len);
  376. kfree_skb(skb);
  377. }
  378. return 0;
  379. }
  380. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  381. /* Return the frame type ID */
  382. static int ax_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
  383. void *daddr, void *saddr, unsigned len)
  384. {
  385. #ifdef CONFIG_INET
  386. if (type != htons(ETH_P_AX25))
  387. return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
  388. #endif
  389. return 0;
  390. }
  391. static int ax_rebuild_header(struct sk_buff *skb)
  392. {
  393. #ifdef CONFIG_INET
  394. return ax25_rebuild_header(skb);
  395. #else
  396. return 0;
  397. #endif
  398. }
  399. #endif /* CONFIG_{AX25,AX25_MODULE} */
  400. /* Open the low-level part of the AX25 channel. Easy! */
  401. static int ax_open(struct net_device *dev)
  402. {
  403. struct ax_disp *ax = netdev_priv(dev);
  404. unsigned long len;
  405. if (ax->tty == NULL)
  406. return -ENODEV;
  407. /*
  408. * Allocate the frame buffers:
  409. *
  410. * rbuff Receive buffer.
  411. * xbuff Transmit buffer.
  412. */
  413. len = dev->mtu * 2;
  414. /*
  415. * allow for arrival of larger UDP packets, even if we say not to
  416. * also fixes a bug in which SunOS sends 512-byte packets even with
  417. * an MSS of 128
  418. */
  419. if (len < 576 * 2)
  420. len = 576 * 2;
  421. if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
  422. goto norbuff;
  423. if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
  424. goto noxbuff;
  425. ax->mtu = dev->mtu + 73;
  426. ax->buffsize = len;
  427. ax->rcount = 0;
  428. ax->xleft = 0;
  429. ax->flags &= (1 << AXF_INUSE); /* Clear ESCAPE & ERROR flags */
  430. spin_lock_init(&ax->buflock);
  431. netif_start_queue(dev);
  432. return 0;
  433. noxbuff:
  434. kfree(ax->rbuff);
  435. norbuff:
  436. return -ENOMEM;
  437. }
  438. /* Close the low-level part of the AX25 channel. Easy! */
  439. static int ax_close(struct net_device *dev)
  440. {
  441. struct ax_disp *ax = netdev_priv(dev);
  442. if (ax->tty == NULL)
  443. return -EBUSY;
  444. ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  445. netif_stop_queue(dev);
  446. return 0;
  447. }
  448. static int ax25_receive_room(struct tty_struct *tty)
  449. {
  450. return 65536; /* We can handle an infinite amount of data. :-) */
  451. }
  452. /*
  453. * Handle the 'receiver data ready' interrupt.
  454. * This function is called by the 'tty_io' module in the kernel when
  455. * a block of data has been received, which can now be decapsulated
  456. * and sent on to the AX.25 layer for further processing.
  457. */
  458. static void ax25_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
  459. {
  460. struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
  461. if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
  462. return;
  463. /*
  464. * Argh! mtu change time! - costs us the packet part received
  465. * at the change
  466. */
  467. if (ax->mtu != ax->dev->mtu + 73)
  468. ax_changedmtu(ax);
  469. /* Read the characters out of the buffer */
  470. while (count--) {
  471. if (fp != NULL && *fp++) {
  472. if (!test_and_set_bit(AXF_ERROR, &ax->flags))
  473. ax->rx_errors++;
  474. cp++;
  475. continue;
  476. }
  477. kiss_unesc(ax, *cp++);
  478. }
  479. }
  480. static int ax25_open(struct tty_struct *tty)
  481. {
  482. struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
  483. int err;
  484. /* First make sure we're not already connected. */
  485. if (ax && ax->magic == AX25_MAGIC)
  486. return -EEXIST;
  487. /* OK. Find a free AX25 channel to use. */
  488. if ((ax = ax_alloc()) == NULL)
  489. return -ENFILE;
  490. ax->tty = tty;
  491. tty->disc_data = ax;
  492. if (tty->driver->flush_buffer)
  493. tty->driver->flush_buffer(tty);
  494. /* Restore default settings */
  495. ax->dev->type = ARPHRD_AX25;
  496. /* Perform the low-level AX25 initialization. */
  497. if ((err = ax_open(ax->dev)))
  498. return err;
  499. /* Done. We have linked the TTY line to a channel. */
  500. return ax->dev->base_addr;
  501. }
  502. static void ax25_close(struct tty_struct *tty)
  503. {
  504. struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
  505. /* First make sure we're connected. */
  506. if (ax == NULL || ax->magic != AX25_MAGIC)
  507. return;
  508. unregister_netdev(ax->dev);
  509. tty->disc_data = NULL;
  510. ax->tty = NULL;
  511. ax_free(ax);
  512. }
  513. static struct net_device_stats *ax_get_stats(struct net_device *dev)
  514. {
  515. static struct net_device_stats stats;
  516. struct ax_disp *ax = netdev_priv(dev);
  517. memset(&stats, 0, sizeof(struct net_device_stats));
  518. stats.rx_packets = ax->rx_packets;
  519. stats.tx_packets = ax->tx_packets;
  520. stats.rx_bytes = ax->rx_bytes;
  521. stats.tx_bytes = ax->tx_bytes;
  522. stats.rx_dropped = ax->rx_dropped;
  523. stats.tx_dropped = ax->tx_dropped;
  524. stats.tx_errors = ax->tx_errors;
  525. stats.rx_errors = ax->rx_errors;
  526. stats.rx_over_errors = ax->rx_over_errors;
  527. return &stats;
  528. }
  529. /************************************************************************
  530. * STANDARD ENCAPSULATION *
  531. ************************************************************************/
  532. static int kiss_esc(unsigned char *s, unsigned char *d, int len)
  533. {
  534. unsigned char *ptr = d;
  535. unsigned char c;
  536. /*
  537. * Send an initial END character to flush out any
  538. * data that may have accumulated in the receiver
  539. * due to line noise.
  540. */
  541. *ptr++ = END;
  542. while (len-- > 0) {
  543. switch (c = *s++) {
  544. case END:
  545. *ptr++ = ESC;
  546. *ptr++ = ESC_END;
  547. break;
  548. case ESC:
  549. *ptr++ = ESC;
  550. *ptr++ = ESC_ESC;
  551. break;
  552. default:
  553. *ptr++ = c;
  554. break;
  555. }
  556. }
  557. *ptr++ = END;
  558. return ptr - d;
  559. }
  560. /*
  561. * MW:
  562. * OK its ugly, but tell me a better solution without copying the
  563. * packet to a temporary buffer :-)
  564. */
  565. static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc, int len)
  566. {
  567. unsigned char *ptr = d;
  568. unsigned char c=0;
  569. *ptr++ = END;
  570. while (len > 0) {
  571. if (len > 2)
  572. c = *s++;
  573. else if (len > 1)
  574. c = crc >> 8;
  575. else if (len > 0)
  576. c = crc & 0xff;
  577. len--;
  578. switch (c) {
  579. case END:
  580. *ptr++ = ESC;
  581. *ptr++ = ESC_END;
  582. break;
  583. case ESC:
  584. *ptr++ = ESC;
  585. *ptr++ = ESC_ESC;
  586. break;
  587. default:
  588. *ptr++ = c;
  589. break;
  590. }
  591. }
  592. *ptr++ = END;
  593. return ptr - d;
  594. }
  595. static void kiss_unesc(struct ax_disp *ax, unsigned char s)
  596. {
  597. switch (s) {
  598. case END:
  599. /* drop keeptest bit = VSV */
  600. if (test_bit(AXF_KEEPTEST, &ax->flags))
  601. clear_bit(AXF_KEEPTEST, &ax->flags);
  602. if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
  603. ax_bump(ax);
  604. clear_bit(AXF_ESCAPE, &ax->flags);
  605. ax->rcount = 0;
  606. return;
  607. case ESC:
  608. set_bit(AXF_ESCAPE, &ax->flags);
  609. return;
  610. case ESC_ESC:
  611. if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
  612. s = ESC;
  613. break;
  614. case ESC_END:
  615. if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
  616. s = END;
  617. break;
  618. }
  619. spin_lock_bh(&ax->buflock);
  620. if (!test_bit(AXF_ERROR, &ax->flags)) {
  621. if (ax->rcount < ax->buffsize) {
  622. ax->rbuff[ax->rcount++] = s;
  623. spin_unlock_bh(&ax->buflock);
  624. return;
  625. }
  626. ax->rx_over_errors++;
  627. set_bit(AXF_ERROR, &ax->flags);
  628. }
  629. spin_unlock_bh(&ax->buflock);
  630. }
  631. static int ax_set_mac_address(struct net_device *dev, void __user *addr)
  632. {
  633. if (copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN))
  634. return -EFAULT;
  635. return 0;
  636. }
  637. static int ax_set_dev_mac_address(struct net_device *dev, void *addr)
  638. {
  639. struct sockaddr *sa = addr;
  640. memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
  641. return 0;
  642. }
  643. /* Perform I/O control on an active ax25 channel. */
  644. static int ax25_disp_ioctl(struct tty_struct *tty, void *file, int cmd, void __user *arg)
  645. {
  646. struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
  647. unsigned int tmp;
  648. /* First make sure we're connected. */
  649. if (ax == NULL || ax->magic != AX25_MAGIC)
  650. return -EINVAL;
  651. switch (cmd) {
  652. case SIOCGIFNAME:
  653. if (copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1))
  654. return -EFAULT;
  655. return 0;
  656. case SIOCGIFENCAP:
  657. return put_user(4, (int __user *)arg);
  658. case SIOCSIFENCAP:
  659. if (get_user(tmp, (int __user *)arg))
  660. return -EFAULT;
  661. ax->mode = tmp;
  662. ax->dev->addr_len = AX25_ADDR_LEN; /* sizeof an AX.25 addr */
  663. ax->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
  664. ax->dev->type = ARPHRD_AX25;
  665. return 0;
  666. case SIOCSIFHWADDR:
  667. return ax_set_mac_address(ax->dev, arg);
  668. default:
  669. return -ENOIOCTLCMD;
  670. }
  671. }
  672. static int ax_open_dev(struct net_device *dev)
  673. {
  674. struct ax_disp *ax = netdev_priv(dev);
  675. if (ax->tty == NULL)
  676. return -ENODEV;
  677. return 0;
  678. }
  679. /* Initialize the driver. Called by network startup. */
  680. static int ax25_init(struct net_device *dev)
  681. {
  682. struct ax_disp *ax = netdev_priv(dev);
  683. static char ax25_bcast[AX25_ADDR_LEN] =
  684. {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
  685. static char ax25_test[AX25_ADDR_LEN] =
  686. {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
  687. if (ax == NULL) /* Allocation failed ?? */
  688. return -ENODEV;
  689. /* Set up the "AX25 Control Block". (And clear statistics) */
  690. memset(ax, 0, sizeof (struct ax_disp));
  691. ax->magic = AX25_MAGIC;
  692. ax->dev = dev;
  693. /* Finish setting up the DEVICE info. */
  694. dev->mtu = AX_MTU;
  695. dev->hard_start_xmit = ax_xmit;
  696. dev->open = ax_open_dev;
  697. dev->stop = ax_close;
  698. dev->get_stats = ax_get_stats;
  699. dev->set_mac_address = ax_set_dev_mac_address;
  700. dev->hard_header_len = 0;
  701. dev->addr_len = 0;
  702. dev->type = ARPHRD_AX25;
  703. dev->tx_queue_len = 10;
  704. dev->hard_header = ax_header;
  705. dev->rebuild_header = ax_rebuild_header;
  706. memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
  707. memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
  708. /* New-style flags. */
  709. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  710. return 0;
  711. }
  712. /* ******************************************************************** */
  713. /* * Init MKISS driver * */
  714. /* ******************************************************************** */
  715. static int __init mkiss_init_driver(void)
  716. {
  717. int status;
  718. printk(banner);
  719. if (ax25_maxdev < 4)
  720. ax25_maxdev = 4; /* Sanity */
  721. if ((ax25_ctrls = kmalloc(sizeof(void *) * ax25_maxdev, GFP_KERNEL)) == NULL) {
  722. printk(KERN_ERR "mkiss: Can't allocate ax25_ctrls[] array!\n");
  723. return -ENOMEM;
  724. }
  725. /* Clear the pointer array, we allocate devices when we need them */
  726. memset(ax25_ctrls, 0, sizeof(void*) * ax25_maxdev); /* Pointers */
  727. /* Fill in our line protocol discipline, and register it */
  728. ax_ldisc.magic = TTY_LDISC_MAGIC;
  729. ax_ldisc.name = "mkiss";
  730. ax_ldisc.open = ax25_open;
  731. ax_ldisc.close = ax25_close;
  732. ax_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
  733. unsigned int, unsigned long))ax25_disp_ioctl;
  734. ax_ldisc.receive_buf = ax25_receive_buf;
  735. ax_ldisc.receive_room = ax25_receive_room;
  736. ax_ldisc.write_wakeup = ax25_write_wakeup;
  737. if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0) {
  738. printk(KERN_ERR "mkiss: can't register line discipline (err = %d)\n", status);
  739. kfree(ax25_ctrls);
  740. }
  741. return status;
  742. }
  743. static void __exit mkiss_exit_driver(void)
  744. {
  745. int i;
  746. for (i = 0; i < ax25_maxdev; i++) {
  747. if (ax25_ctrls[i]) {
  748. /*
  749. * VSV = if dev->start==0, then device
  750. * unregistered while close proc.
  751. */
  752. if (netif_running(&ax25_ctrls[i]->dev))
  753. unregister_netdev(&ax25_ctrls[i]->dev);
  754. kfree(ax25_ctrls[i]);
  755. }
  756. }
  757. kfree(ax25_ctrls);
  758. ax25_ctrls = NULL;
  759. if ((i = tty_unregister_ldisc(N_AX25)))
  760. printk(KERN_ERR "mkiss: can't unregister line discipline (err = %d)\n", i);
  761. }
  762. MODULE_AUTHOR("Hans Albas PE1AYX <hans@esrac.ele.tue.nl>");
  763. MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
  764. MODULE_PARM(ax25_maxdev, "i");
  765. MODULE_PARM_DESC(ax25_maxdev, "number of MKISS devices");
  766. MODULE_LICENSE("GPL");
  767. MODULE_ALIAS_LDISC(N_AX25);
  768. module_init(mkiss_init_driver);
  769. module_exit(mkiss_exit_driver);