mkiss.c 24 KB

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