mkiss.c 24 KB

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