mkiss.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * This program is free software; you can distribute it and/or modify it
  3. * under the terms of the GNU General Public License (Version 2) as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9. * for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along
  12. * with this program; if not, write to the Free Software Foundation, Inc.,
  13. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  14. *
  15. * Copyright (C) Hans Alblas PE1AYX <hans@esrac.ele.tue.nl>
  16. * Copyright (C) 2004, 05 Ralf Baechle DL5RB <ralf@linux-mips.org>
  17. * Copyright (C) 2004, 05 Thomas Osterried DL9SAU <thomas@x-berg.in-berlin.de>
  18. */
  19. #include <linux/module.h>
  20. #include <asm/system.h>
  21. #include <linux/bitops.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/crc16.h>
  24. #include <linux/string.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/in.h>
  28. #include <linux/inet.h>
  29. #include <linux/slab.h>
  30. #include <linux/tty.h>
  31. #include <linux/errno.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/major.h>
  34. #include <linux/init.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/jiffies.h>
  40. #include <linux/compat.h>
  41. #include <net/ax25.h>
  42. #define AX_MTU 236
  43. /* SLIP/KISS protocol characters. */
  44. #define END 0300 /* indicates end of frame */
  45. #define ESC 0333 /* indicates byte stuffing */
  46. #define ESC_END 0334 /* ESC ESC_END means END 'data' */
  47. #define ESC_ESC 0335 /* ESC ESC_ESC means ESC 'data' */
  48. struct mkiss {
  49. struct tty_struct *tty; /* ptr to TTY structure */
  50. struct net_device *dev; /* easy for intr handling */
  51. /* These are pointers to the malloc()ed frame buffers. */
  52. spinlock_t buflock;/* lock for rbuf and xbuf */
  53. unsigned char *rbuff; /* receiver buffer */
  54. int rcount; /* received chars counter */
  55. unsigned char *xbuff; /* transmitter buffer */
  56. unsigned char *xhead; /* pointer to next byte to XMIT */
  57. int xleft; /* bytes left in XMIT queue */
  58. /* Detailed SLIP statistics. */
  59. int mtu; /* Our mtu (to spot changes!) */
  60. int buffsize; /* Max buffers sizes */
  61. unsigned long flags; /* Flag values/ mode etc */
  62. /* long req'd: used by set_bit --RR */
  63. #define AXF_INUSE 0 /* Channel in use */
  64. #define AXF_ESCAPE 1 /* ESC received */
  65. #define AXF_ERROR 2 /* Parity, etc. error */
  66. #define AXF_KEEPTEST 3 /* Keepalive test flag */
  67. #define AXF_OUTWAIT 4 /* is outpacket was flag */
  68. int mode;
  69. int crcmode; /* MW: for FlexNet, SMACK etc. */
  70. int crcauto; /* CRC auto mode */
  71. #define CRC_MODE_NONE 0
  72. #define CRC_MODE_FLEX 1
  73. #define CRC_MODE_SMACK 2
  74. #define CRC_MODE_FLEX_TEST 3
  75. #define CRC_MODE_SMACK_TEST 4
  76. atomic_t refcnt;
  77. struct semaphore dead_sem;
  78. };
  79. /*---------------------------------------------------------------------------*/
  80. static const unsigned short crc_flex_table[] = {
  81. 0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
  82. 0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
  83. 0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
  84. 0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
  85. 0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
  86. 0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
  87. 0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
  88. 0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
  89. 0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
  90. 0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
  91. 0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
  92. 0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
  93. 0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
  94. 0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
  95. 0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
  96. 0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
  97. 0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
  98. 0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
  99. 0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
  100. 0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
  101. 0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
  102. 0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
  103. 0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
  104. 0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
  105. 0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
  106. 0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
  107. 0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
  108. 0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
  109. 0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
  110. 0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
  111. 0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
  112. 0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
  113. };
  114. static unsigned short calc_crc_flex(unsigned char *cp, int size)
  115. {
  116. unsigned short crc = 0xffff;
  117. while (size--)
  118. crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
  119. return crc;
  120. }
  121. static int check_crc_flex(unsigned char *cp, int size)
  122. {
  123. unsigned short crc = 0xffff;
  124. if (size < 3)
  125. return -1;
  126. while (size--)
  127. crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
  128. if ((crc & 0xffff) != 0x7070)
  129. return -1;
  130. return 0;
  131. }
  132. static int check_crc_16(unsigned char *cp, int size)
  133. {
  134. unsigned short crc = 0x0000;
  135. if (size < 3)
  136. return -1;
  137. crc = crc16(0, cp, size);
  138. if (crc != 0x0000)
  139. return -1;
  140. return 0;
  141. }
  142. /*
  143. * Standard encapsulation
  144. */
  145. static int kiss_esc(unsigned char *s, unsigned char *d, int len)
  146. {
  147. unsigned char *ptr = d;
  148. unsigned char c;
  149. /*
  150. * Send an initial END character to flush out any data that may have
  151. * accumulated in the receiver due to line noise.
  152. */
  153. *ptr++ = END;
  154. while (len-- > 0) {
  155. switch (c = *s++) {
  156. case END:
  157. *ptr++ = ESC;
  158. *ptr++ = ESC_END;
  159. break;
  160. case ESC:
  161. *ptr++ = ESC;
  162. *ptr++ = ESC_ESC;
  163. break;
  164. default:
  165. *ptr++ = c;
  166. break;
  167. }
  168. }
  169. *ptr++ = END;
  170. return ptr - d;
  171. }
  172. /*
  173. * MW:
  174. * OK its ugly, but tell me a better solution without copying the
  175. * packet to a temporary buffer :-)
  176. */
  177. static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc,
  178. int len)
  179. {
  180. unsigned char *ptr = d;
  181. unsigned char c=0;
  182. *ptr++ = END;
  183. while (len > 0) {
  184. if (len > 2)
  185. c = *s++;
  186. else if (len > 1)
  187. c = crc >> 8;
  188. else if (len > 0)
  189. c = crc & 0xff;
  190. len--;
  191. switch (c) {
  192. case END:
  193. *ptr++ = ESC;
  194. *ptr++ = ESC_END;
  195. break;
  196. case ESC:
  197. *ptr++ = ESC;
  198. *ptr++ = ESC_ESC;
  199. break;
  200. default:
  201. *ptr++ = c;
  202. break;
  203. }
  204. }
  205. *ptr++ = END;
  206. return ptr - d;
  207. }
  208. /* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
  209. static void ax_bump(struct mkiss *ax)
  210. {
  211. struct sk_buff *skb;
  212. int count;
  213. spin_lock_bh(&ax->buflock);
  214. if (ax->rbuff[0] > 0x0f) {
  215. if (ax->rbuff[0] & 0x80) {
  216. if (check_crc_16(ax->rbuff, ax->rcount) < 0) {
  217. ax->dev->stats.rx_errors++;
  218. spin_unlock_bh(&ax->buflock);
  219. return;
  220. }
  221. if (ax->crcmode != CRC_MODE_SMACK && ax->crcauto) {
  222. printk(KERN_INFO
  223. "mkiss: %s: Switching to crc-smack\n",
  224. ax->dev->name);
  225. ax->crcmode = CRC_MODE_SMACK;
  226. }
  227. ax->rcount -= 2;
  228. *ax->rbuff &= ~0x80;
  229. } else if (ax->rbuff[0] & 0x20) {
  230. if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
  231. ax->dev->stats.rx_errors++;
  232. spin_unlock_bh(&ax->buflock);
  233. return;
  234. }
  235. if (ax->crcmode != CRC_MODE_FLEX && ax->crcauto) {
  236. printk(KERN_INFO
  237. "mkiss: %s: Switching to crc-flexnet\n",
  238. ax->dev->name);
  239. ax->crcmode = CRC_MODE_FLEX;
  240. }
  241. ax->rcount -= 2;
  242. /*
  243. * dl9sau bugfix: the trailling two bytes flexnet crc
  244. * will not be passed to the kernel. thus we have to
  245. * correct the kissparm signature, because it indicates
  246. * a crc but there's none
  247. */
  248. *ax->rbuff &= ~0x20;
  249. }
  250. }
  251. count = ax->rcount;
  252. if ((skb = dev_alloc_skb(count)) == NULL) {
  253. printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n",
  254. ax->dev->name);
  255. ax->dev->stats.rx_dropped++;
  256. spin_unlock_bh(&ax->buflock);
  257. return;
  258. }
  259. memcpy(skb_put(skb,count), ax->rbuff, count);
  260. skb->protocol = ax25_type_trans(skb, ax->dev);
  261. netif_rx(skb);
  262. ax->dev->stats.rx_packets++;
  263. ax->dev->stats.rx_bytes += count;
  264. spin_unlock_bh(&ax->buflock);
  265. }
  266. static void kiss_unesc(struct mkiss *ax, unsigned char s)
  267. {
  268. switch (s) {
  269. case END:
  270. /* drop keeptest bit = VSV */
  271. if (test_bit(AXF_KEEPTEST, &ax->flags))
  272. clear_bit(AXF_KEEPTEST, &ax->flags);
  273. if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
  274. ax_bump(ax);
  275. clear_bit(AXF_ESCAPE, &ax->flags);
  276. ax->rcount = 0;
  277. return;
  278. case ESC:
  279. set_bit(AXF_ESCAPE, &ax->flags);
  280. return;
  281. case ESC_ESC:
  282. if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
  283. s = ESC;
  284. break;
  285. case ESC_END:
  286. if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
  287. s = END;
  288. break;
  289. }
  290. spin_lock_bh(&ax->buflock);
  291. if (!test_bit(AXF_ERROR, &ax->flags)) {
  292. if (ax->rcount < ax->buffsize) {
  293. ax->rbuff[ax->rcount++] = s;
  294. spin_unlock_bh(&ax->buflock);
  295. return;
  296. }
  297. ax->dev->stats.rx_over_errors++;
  298. set_bit(AXF_ERROR, &ax->flags);
  299. }
  300. spin_unlock_bh(&ax->buflock);
  301. }
  302. static int ax_set_mac_address(struct net_device *dev, void *addr)
  303. {
  304. struct sockaddr_ax25 *sa = addr;
  305. netif_tx_lock_bh(dev);
  306. netif_addr_lock(dev);
  307. memcpy(dev->dev_addr, &sa->sax25_call, AX25_ADDR_LEN);
  308. netif_addr_unlock(dev);
  309. netif_tx_unlock_bh(dev);
  310. return 0;
  311. }
  312. /*---------------------------------------------------------------------------*/
  313. static void ax_changedmtu(struct mkiss *ax)
  314. {
  315. struct net_device *dev = ax->dev;
  316. unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
  317. int len;
  318. len = dev->mtu * 2;
  319. /*
  320. * allow for arrival of larger UDP packets, even if we say not to
  321. * also fixes a bug in which SunOS sends 512-byte packets even with
  322. * an MSS of 128
  323. */
  324. if (len < 576 * 2)
  325. len = 576 * 2;
  326. xbuff = kmalloc(len + 4, GFP_ATOMIC);
  327. rbuff = kmalloc(len + 4, GFP_ATOMIC);
  328. if (xbuff == NULL || rbuff == NULL) {
  329. printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, "
  330. "MTU change cancelled.\n",
  331. ax->dev->name);
  332. dev->mtu = ax->mtu;
  333. kfree(xbuff);
  334. kfree(rbuff);
  335. return;
  336. }
  337. spin_lock_bh(&ax->buflock);
  338. oxbuff = ax->xbuff;
  339. ax->xbuff = xbuff;
  340. orbuff = ax->rbuff;
  341. ax->rbuff = rbuff;
  342. if (ax->xleft) {
  343. if (ax->xleft <= len) {
  344. memcpy(ax->xbuff, ax->xhead, ax->xleft);
  345. } else {
  346. ax->xleft = 0;
  347. dev->stats.tx_dropped++;
  348. }
  349. }
  350. ax->xhead = ax->xbuff;
  351. if (ax->rcount) {
  352. if (ax->rcount <= len) {
  353. memcpy(ax->rbuff, orbuff, ax->rcount);
  354. } else {
  355. ax->rcount = 0;
  356. dev->stats.rx_over_errors++;
  357. set_bit(AXF_ERROR, &ax->flags);
  358. }
  359. }
  360. ax->mtu = dev->mtu + 73;
  361. ax->buffsize = len;
  362. spin_unlock_bh(&ax->buflock);
  363. kfree(oxbuff);
  364. kfree(orbuff);
  365. }
  366. /* Encapsulate one AX.25 packet and stuff into a TTY queue. */
  367. static void ax_encaps(struct net_device *dev, unsigned char *icp, int len)
  368. {
  369. struct mkiss *ax = netdev_priv(dev);
  370. unsigned char *p;
  371. int actual, count;
  372. if (ax->mtu != ax->dev->mtu + 73) /* Someone has been ifconfigging */
  373. ax_changedmtu(ax);
  374. if (len > ax->mtu) { /* Sigh, shouldn't occur BUT ... */
  375. len = ax->mtu;
  376. printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
  377. dev->stats.tx_dropped++;
  378. netif_start_queue(dev);
  379. return;
  380. }
  381. p = icp;
  382. spin_lock_bh(&ax->buflock);
  383. if ((*p & 0x0f) != 0) {
  384. /* Configuration Command (kissparms(1).
  385. * Protocol spec says: never append CRC.
  386. * This fixes a very old bug in the linux
  387. * kiss driver. -- dl9sau */
  388. switch (*p & 0xff) {
  389. case 0x85:
  390. /* command from userspace especially for us,
  391. * not for delivery to the tnc */
  392. if (len > 1) {
  393. int cmd = (p[1] & 0xff);
  394. switch(cmd) {
  395. case 3:
  396. ax->crcmode = CRC_MODE_SMACK;
  397. break;
  398. case 2:
  399. ax->crcmode = CRC_MODE_FLEX;
  400. break;
  401. case 1:
  402. ax->crcmode = CRC_MODE_NONE;
  403. break;
  404. case 0:
  405. default:
  406. ax->crcmode = CRC_MODE_SMACK_TEST;
  407. cmd = 0;
  408. }
  409. ax->crcauto = (cmd ? 0 : 1);
  410. printk(KERN_INFO "mkiss: %s: crc mode %s %d\n", ax->dev->name, (len) ? "set to" : "is", cmd);
  411. }
  412. spin_unlock_bh(&ax->buflock);
  413. netif_start_queue(dev);
  414. return;
  415. default:
  416. count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
  417. }
  418. } else {
  419. unsigned short crc;
  420. switch (ax->crcmode) {
  421. case CRC_MODE_SMACK_TEST:
  422. ax->crcmode = CRC_MODE_FLEX_TEST;
  423. printk(KERN_INFO "mkiss: %s: Trying crc-smack\n", ax->dev->name);
  424. // fall through
  425. case CRC_MODE_SMACK:
  426. *p |= 0x80;
  427. crc = swab16(crc16(0, p, len));
  428. count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
  429. break;
  430. case CRC_MODE_FLEX_TEST:
  431. ax->crcmode = CRC_MODE_NONE;
  432. printk(KERN_INFO "mkiss: %s: Trying crc-flexnet\n", ax->dev->name);
  433. // fall through
  434. case CRC_MODE_FLEX:
  435. *p |= 0x20;
  436. crc = calc_crc_flex(p, len);
  437. count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
  438. break;
  439. default:
  440. count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
  441. }
  442. }
  443. spin_unlock_bh(&ax->buflock);
  444. set_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
  445. actual = ax->tty->ops->write(ax->tty, ax->xbuff, count);
  446. dev->stats.tx_packets++;
  447. dev->stats.tx_bytes += actual;
  448. ax->dev->trans_start = jiffies;
  449. ax->xleft = count - actual;
  450. ax->xhead = ax->xbuff + actual;
  451. }
  452. /* Encapsulate an AX.25 packet and kick it into a TTY queue. */
  453. static netdev_tx_t ax_xmit(struct sk_buff *skb, struct net_device *dev)
  454. {
  455. struct mkiss *ax = netdev_priv(dev);
  456. if (!netif_running(dev)) {
  457. printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
  458. return NETDEV_TX_BUSY;
  459. }
  460. if (netif_queue_stopped(dev)) {
  461. /*
  462. * May be we must check transmitter timeout here ?
  463. * 14 Oct 1994 Dmitry Gorodchanin.
  464. */
  465. if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
  466. /* 20 sec timeout not reached */
  467. return NETDEV_TX_BUSY;
  468. }
  469. printk(KERN_ERR "mkiss: %s: transmit timed out, %s?\n", dev->name,
  470. (tty_chars_in_buffer(ax->tty) || ax->xleft) ?
  471. "bad line quality" : "driver error");
  472. ax->xleft = 0;
  473. clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
  474. netif_start_queue(dev);
  475. }
  476. /* We were not busy, so we are now... :-) */
  477. if (skb != NULL) {
  478. netif_stop_queue(dev);
  479. ax_encaps(dev, skb->data, skb->len);
  480. kfree_skb(skb);
  481. }
  482. return NETDEV_TX_OK;
  483. }
  484. static int ax_open_dev(struct net_device *dev)
  485. {
  486. struct mkiss *ax = netdev_priv(dev);
  487. if (ax->tty == NULL)
  488. return -ENODEV;
  489. return 0;
  490. }
  491. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  492. /* Return the frame type ID */
  493. static int ax_header(struct sk_buff *skb, struct net_device *dev,
  494. unsigned short type, const void *daddr,
  495. const void *saddr, unsigned len)
  496. {
  497. #ifdef CONFIG_INET
  498. if (type != ETH_P_AX25)
  499. return ax25_hard_header(skb, dev, type, daddr, saddr, len);
  500. #endif
  501. return 0;
  502. }
  503. static int ax_rebuild_header(struct sk_buff *skb)
  504. {
  505. #ifdef CONFIG_INET
  506. return ax25_rebuild_header(skb);
  507. #else
  508. return 0;
  509. #endif
  510. }
  511. #endif /* CONFIG_{AX25,AX25_MODULE} */
  512. /* Open the low-level part of the AX25 channel. Easy! */
  513. static int ax_open(struct net_device *dev)
  514. {
  515. struct mkiss *ax = netdev_priv(dev);
  516. unsigned long len;
  517. if (ax->tty == NULL)
  518. return -ENODEV;
  519. /*
  520. * Allocate the frame buffers:
  521. *
  522. * rbuff Receive buffer.
  523. * xbuff Transmit buffer.
  524. */
  525. len = dev->mtu * 2;
  526. /*
  527. * allow for arrival of larger UDP packets, even if we say not to
  528. * also fixes a bug in which SunOS sends 512-byte packets even with
  529. * an MSS of 128
  530. */
  531. if (len < 576 * 2)
  532. len = 576 * 2;
  533. if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
  534. goto norbuff;
  535. if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
  536. goto noxbuff;
  537. ax->mtu = dev->mtu + 73;
  538. ax->buffsize = len;
  539. ax->rcount = 0;
  540. ax->xleft = 0;
  541. ax->flags &= (1 << AXF_INUSE); /* Clear ESCAPE & ERROR flags */
  542. spin_lock_init(&ax->buflock);
  543. return 0;
  544. noxbuff:
  545. kfree(ax->rbuff);
  546. norbuff:
  547. return -ENOMEM;
  548. }
  549. /* Close the low-level part of the AX25 channel. Easy! */
  550. static int ax_close(struct net_device *dev)
  551. {
  552. struct mkiss *ax = netdev_priv(dev);
  553. if (ax->tty)
  554. clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
  555. netif_stop_queue(dev);
  556. return 0;
  557. }
  558. static const struct header_ops ax_header_ops = {
  559. .create = ax_header,
  560. .rebuild = ax_rebuild_header,
  561. };
  562. static const struct net_device_ops ax_netdev_ops = {
  563. .ndo_open = ax_open_dev,
  564. .ndo_stop = ax_close,
  565. .ndo_start_xmit = ax_xmit,
  566. .ndo_set_mac_address = ax_set_mac_address,
  567. };
  568. static void ax_setup(struct net_device *dev)
  569. {
  570. /* Finish setting up the DEVICE info. */
  571. dev->mtu = AX_MTU;
  572. dev->hard_header_len = 0;
  573. dev->addr_len = 0;
  574. dev->type = ARPHRD_AX25;
  575. dev->tx_queue_len = 10;
  576. dev->header_ops = &ax_header_ops;
  577. dev->netdev_ops = &ax_netdev_ops;
  578. memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
  579. memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
  580. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  581. }
  582. /*
  583. * We have a potential race on dereferencing tty->disc_data, because the tty
  584. * layer provides no locking at all - thus one cpu could be running
  585. * sixpack_receive_buf while another calls sixpack_close, which zeroes
  586. * tty->disc_data and frees the memory that sixpack_receive_buf is using. The
  587. * best way to fix this is to use a rwlock in the tty struct, but for now we
  588. * use a single global rwlock for all ttys in ppp line discipline.
  589. */
  590. static DEFINE_RWLOCK(disc_data_lock);
  591. static struct mkiss *mkiss_get(struct tty_struct *tty)
  592. {
  593. struct mkiss *ax;
  594. read_lock(&disc_data_lock);
  595. ax = tty->disc_data;
  596. if (ax)
  597. atomic_inc(&ax->refcnt);
  598. read_unlock(&disc_data_lock);
  599. return ax;
  600. }
  601. static void mkiss_put(struct mkiss *ax)
  602. {
  603. if (atomic_dec_and_test(&ax->refcnt))
  604. up(&ax->dead_sem);
  605. }
  606. static int crc_force = 0; /* Can be overridden with insmod */
  607. static int mkiss_open(struct tty_struct *tty)
  608. {
  609. struct net_device *dev;
  610. struct mkiss *ax;
  611. int err;
  612. if (!capable(CAP_NET_ADMIN))
  613. return -EPERM;
  614. if (tty->ops->write == NULL)
  615. return -EOPNOTSUPP;
  616. dev = alloc_netdev(sizeof(struct mkiss), "ax%d", ax_setup);
  617. if (!dev) {
  618. err = -ENOMEM;
  619. goto out;
  620. }
  621. ax = netdev_priv(dev);
  622. ax->dev = dev;
  623. spin_lock_init(&ax->buflock);
  624. atomic_set(&ax->refcnt, 1);
  625. init_MUTEX_LOCKED(&ax->dead_sem);
  626. ax->tty = tty;
  627. tty->disc_data = ax;
  628. tty->receive_room = 65535;
  629. tty_driver_flush_buffer(tty);
  630. /* Restore default settings */
  631. dev->type = ARPHRD_AX25;
  632. /* Perform the low-level AX25 initialization. */
  633. if ((err = ax_open(ax->dev))) {
  634. goto out_free_netdev;
  635. }
  636. if (register_netdev(dev))
  637. goto out_free_buffers;
  638. /* after register_netdev() - because else printk smashes the kernel */
  639. switch (crc_force) {
  640. case 3:
  641. ax->crcmode = CRC_MODE_SMACK;
  642. printk(KERN_INFO "mkiss: %s: crc mode smack forced.\n",
  643. ax->dev->name);
  644. break;
  645. case 2:
  646. ax->crcmode = CRC_MODE_FLEX;
  647. printk(KERN_INFO "mkiss: %s: crc mode flexnet forced.\n",
  648. ax->dev->name);
  649. break;
  650. case 1:
  651. ax->crcmode = CRC_MODE_NONE;
  652. printk(KERN_INFO "mkiss: %s: crc mode disabled.\n",
  653. ax->dev->name);
  654. break;
  655. case 0:
  656. /* fall through */
  657. default:
  658. crc_force = 0;
  659. printk(KERN_INFO "mkiss: %s: crc mode is auto.\n",
  660. ax->dev->name);
  661. ax->crcmode = CRC_MODE_SMACK_TEST;
  662. }
  663. ax->crcauto = (crc_force ? 0 : 1);
  664. netif_start_queue(dev);
  665. /* Done. We have linked the TTY line to a channel. */
  666. return 0;
  667. out_free_buffers:
  668. kfree(ax->rbuff);
  669. kfree(ax->xbuff);
  670. out_free_netdev:
  671. free_netdev(dev);
  672. out:
  673. return err;
  674. }
  675. static void mkiss_close(struct tty_struct *tty)
  676. {
  677. struct mkiss *ax;
  678. write_lock(&disc_data_lock);
  679. ax = tty->disc_data;
  680. tty->disc_data = NULL;
  681. write_unlock(&disc_data_lock);
  682. if (!ax)
  683. return;
  684. /*
  685. * We have now ensured that nobody can start using ap from now on, but
  686. * we have to wait for all existing users to finish.
  687. */
  688. if (!atomic_dec_and_test(&ax->refcnt))
  689. down(&ax->dead_sem);
  690. unregister_netdev(ax->dev);
  691. /* Free all AX25 frame buffers. */
  692. kfree(ax->rbuff);
  693. kfree(ax->xbuff);
  694. ax->tty = NULL;
  695. }
  696. /* Perform I/O control on an active ax25 channel. */
  697. static int mkiss_ioctl(struct tty_struct *tty, struct file *file,
  698. unsigned int cmd, unsigned long arg)
  699. {
  700. struct mkiss *ax = mkiss_get(tty);
  701. struct net_device *dev;
  702. unsigned int tmp, err;
  703. /* First make sure we're connected. */
  704. if (ax == NULL)
  705. return -ENXIO;
  706. dev = ax->dev;
  707. switch (cmd) {
  708. case SIOCGIFNAME:
  709. err = copy_to_user((void __user *) arg, ax->dev->name,
  710. strlen(ax->dev->name) + 1) ? -EFAULT : 0;
  711. break;
  712. case SIOCGIFENCAP:
  713. err = put_user(4, (int __user *) arg);
  714. break;
  715. case SIOCSIFENCAP:
  716. if (get_user(tmp, (int __user *) arg)) {
  717. err = -EFAULT;
  718. break;
  719. }
  720. ax->mode = tmp;
  721. dev->addr_len = AX25_ADDR_LEN;
  722. dev->hard_header_len = AX25_KISS_HEADER_LEN +
  723. AX25_MAX_HEADER_LEN + 3;
  724. dev->type = ARPHRD_AX25;
  725. err = 0;
  726. break;
  727. case SIOCSIFHWADDR: {
  728. char addr[AX25_ADDR_LEN];
  729. if (copy_from_user(&addr,
  730. (void __user *) arg, AX25_ADDR_LEN)) {
  731. err = -EFAULT;
  732. break;
  733. }
  734. netif_tx_lock_bh(dev);
  735. memcpy(dev->dev_addr, addr, AX25_ADDR_LEN);
  736. netif_tx_unlock_bh(dev);
  737. err = 0;
  738. break;
  739. }
  740. default:
  741. err = -ENOIOCTLCMD;
  742. }
  743. mkiss_put(ax);
  744. return err;
  745. }
  746. #ifdef CONFIG_COMPAT
  747. static long mkiss_compat_ioctl(struct tty_struct *tty, struct file *file,
  748. unsigned int cmd, unsigned long arg)
  749. {
  750. switch (cmd) {
  751. case SIOCGIFNAME:
  752. case SIOCGIFENCAP:
  753. case SIOCSIFENCAP:
  754. case SIOCSIFHWADDR:
  755. return mkiss_ioctl(tty, file, cmd,
  756. (unsigned long)compat_ptr(arg));
  757. }
  758. return -ENOIOCTLCMD;
  759. }
  760. #endif
  761. /*
  762. * Handle the 'receiver data ready' interrupt.
  763. * This function is called by the 'tty_io' module in the kernel when
  764. * a block of data has been received, which can now be decapsulated
  765. * and sent on to the AX.25 layer for further processing.
  766. */
  767. static void mkiss_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  768. char *fp, int count)
  769. {
  770. struct mkiss *ax = mkiss_get(tty);
  771. if (!ax)
  772. return;
  773. /*
  774. * Argh! mtu change time! - costs us the packet part received
  775. * at the change
  776. */
  777. if (ax->mtu != ax->dev->mtu + 73)
  778. ax_changedmtu(ax);
  779. /* Read the characters out of the buffer */
  780. while (count--) {
  781. if (fp != NULL && *fp++) {
  782. if (!test_and_set_bit(AXF_ERROR, &ax->flags))
  783. ax->dev->stats.rx_errors++;
  784. cp++;
  785. continue;
  786. }
  787. kiss_unesc(ax, *cp++);
  788. }
  789. mkiss_put(ax);
  790. tty_unthrottle(tty);
  791. }
  792. /*
  793. * Called by the driver when there's room for more data. If we have
  794. * more packets to send, we send them here.
  795. */
  796. static void mkiss_write_wakeup(struct tty_struct *tty)
  797. {
  798. struct mkiss *ax = mkiss_get(tty);
  799. int actual;
  800. if (!ax)
  801. return;
  802. if (ax->xleft <= 0) {
  803. /* Now serial buffer is almost free & we can start
  804. * transmission of another packet
  805. */
  806. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  807. netif_wake_queue(ax->dev);
  808. goto out;
  809. }
  810. actual = tty->ops->write(tty, ax->xhead, ax->xleft);
  811. ax->xleft -= actual;
  812. ax->xhead += actual;
  813. out:
  814. mkiss_put(ax);
  815. }
  816. static struct tty_ldisc_ops ax_ldisc = {
  817. .owner = THIS_MODULE,
  818. .magic = TTY_LDISC_MAGIC,
  819. .name = "mkiss",
  820. .open = mkiss_open,
  821. .close = mkiss_close,
  822. .ioctl = mkiss_ioctl,
  823. #ifdef CONFIG_COMPAT
  824. .compat_ioctl = mkiss_compat_ioctl,
  825. #endif
  826. .receive_buf = mkiss_receive_buf,
  827. .write_wakeup = mkiss_write_wakeup
  828. };
  829. static const char banner[] __initdata = KERN_INFO \
  830. "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
  831. static const char msg_regfail[] __initdata = KERN_ERR \
  832. "mkiss: can't register line discipline (err = %d)\n";
  833. static int __init mkiss_init_driver(void)
  834. {
  835. int status;
  836. printk(banner);
  837. status = tty_register_ldisc(N_AX25, &ax_ldisc);
  838. if (status != 0)
  839. printk(msg_regfail, status);
  840. return status;
  841. }
  842. static const char msg_unregfail[] __exitdata = KERN_ERR \
  843. "mkiss: can't unregister line discipline (err = %d)\n";
  844. static void __exit mkiss_exit_driver(void)
  845. {
  846. int ret;
  847. if ((ret = tty_unregister_ldisc(N_AX25)))
  848. printk(msg_unregfail, ret);
  849. }
  850. MODULE_AUTHOR("Ralf Baechle DL5RB <ralf@linux-mips.org>");
  851. MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
  852. module_param(crc_force, int, 0);
  853. MODULE_PARM_DESC(crc_force, "crc [0 = auto | 1 = none | 2 = flexnet | 3 = smack]");
  854. MODULE_LICENSE("GPL");
  855. MODULE_ALIAS_LDISC(N_AX25);
  856. module_init(mkiss_init_driver);
  857. module_exit(mkiss_exit_driver);