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