mkiss.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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/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 <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. struct net_device_stats stats;
  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->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: Switchting 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->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: Switchting 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->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->last_rx = jiffies;
  262. ax->stats.rx_packets++;
  263. ax->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->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. memcpy(dev->dev_addr, &sa->sax25_call, AX25_ADDR_LEN);
  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. ax->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. ax->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. ax->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, (unsigned char *)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, (unsigned char *)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, (unsigned char *)ax->xbuff, crc, len+2);
  436. break;
  437. default:
  438. count = kiss_esc(p, (unsigned char *)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. ax->stats.tx_packets++;
  445. ax->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 int ax_xmit(struct sk_buff *skb, struct net_device *dev)
  452. {
  453. struct mkiss *ax = netdev_priv(dev);
  454. if (!netif_running(dev)) {
  455. printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
  456. return 1;
  457. }
  458. if (netif_queue_stopped(dev)) {
  459. /*
  460. * May be we must check transmitter timeout here ?
  461. * 14 Oct 1994 Dmitry Gorodchanin.
  462. */
  463. if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
  464. /* 20 sec timeout not reached */
  465. return 1;
  466. }
  467. printk(KERN_ERR "mkiss: %s: transmit timed out, %s?\n", dev->name,
  468. (ax->tty->ops->chars_in_buffer(ax->tty) || ax->xleft) ?
  469. "bad line quality" : "driver error");
  470. ax->xleft = 0;
  471. clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
  472. netif_start_queue(dev);
  473. }
  474. /* We were not busy, so we are now... :-) */
  475. if (skb != NULL) {
  476. netif_stop_queue(dev);
  477. ax_encaps(dev, skb->data, skb->len);
  478. kfree_skb(skb);
  479. }
  480. return 0;
  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. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  490. /* Return the frame type ID */
  491. static int ax_header(struct sk_buff *skb, struct net_device *dev,
  492. unsigned short type, const void *daddr,
  493. const void *saddr, unsigned len)
  494. {
  495. #ifdef CONFIG_INET
  496. if (type != ETH_P_AX25)
  497. return ax25_hard_header(skb, dev, type, daddr, saddr, len);
  498. #endif
  499. return 0;
  500. }
  501. static int ax_rebuild_header(struct sk_buff *skb)
  502. {
  503. #ifdef CONFIG_INET
  504. return ax25_rebuild_header(skb);
  505. #else
  506. return 0;
  507. #endif
  508. }
  509. #endif /* CONFIG_{AX25,AX25_MODULE} */
  510. /* Open the low-level part of the AX25 channel. Easy! */
  511. static int ax_open(struct net_device *dev)
  512. {
  513. struct mkiss *ax = netdev_priv(dev);
  514. unsigned long len;
  515. if (ax->tty == NULL)
  516. return -ENODEV;
  517. /*
  518. * Allocate the frame buffers:
  519. *
  520. * rbuff Receive buffer.
  521. * xbuff Transmit buffer.
  522. */
  523. len = dev->mtu * 2;
  524. /*
  525. * allow for arrival of larger UDP packets, even if we say not to
  526. * also fixes a bug in which SunOS sends 512-byte packets even with
  527. * an MSS of 128
  528. */
  529. if (len < 576 * 2)
  530. len = 576 * 2;
  531. if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
  532. goto norbuff;
  533. if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
  534. goto noxbuff;
  535. ax->mtu = dev->mtu + 73;
  536. ax->buffsize = len;
  537. ax->rcount = 0;
  538. ax->xleft = 0;
  539. ax->flags &= (1 << AXF_INUSE); /* Clear ESCAPE & ERROR flags */
  540. spin_lock_init(&ax->buflock);
  541. return 0;
  542. noxbuff:
  543. kfree(ax->rbuff);
  544. norbuff:
  545. return -ENOMEM;
  546. }
  547. /* Close the low-level part of the AX25 channel. Easy! */
  548. static int ax_close(struct net_device *dev)
  549. {
  550. struct mkiss *ax = netdev_priv(dev);
  551. if (ax->tty)
  552. clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
  553. netif_stop_queue(dev);
  554. return 0;
  555. }
  556. static struct net_device_stats *ax_get_stats(struct net_device *dev)
  557. {
  558. struct mkiss *ax = netdev_priv(dev);
  559. return &ax->stats;
  560. }
  561. static const struct header_ops ax_header_ops = {
  562. .create = ax_header,
  563. .rebuild = ax_rebuild_header,
  564. };
  565. static void ax_setup(struct net_device *dev)
  566. {
  567. /* Finish setting up the DEVICE info. */
  568. dev->mtu = AX_MTU;
  569. dev->hard_start_xmit = ax_xmit;
  570. dev->open = ax_open_dev;
  571. dev->stop = ax_close;
  572. dev->get_stats = ax_get_stats;
  573. dev->set_mac_address = ax_set_mac_address;
  574. dev->hard_header_len = 0;
  575. dev->addr_len = 0;
  576. dev->type = ARPHRD_AX25;
  577. dev->tx_queue_len = 10;
  578. dev->header_ops = &ax_header_ops;
  579. memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
  580. memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
  581. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  582. }
  583. /*
  584. * We have a potential race on dereferencing tty->disc_data, because the tty
  585. * layer provides no locking at all - thus one cpu could be running
  586. * sixpack_receive_buf while another calls sixpack_close, which zeroes
  587. * tty->disc_data and frees the memory that sixpack_receive_buf is using. The
  588. * best way to fix this is to use a rwlock in the tty struct, but for now we
  589. * use a single global rwlock for all ttys in ppp line discipline.
  590. */
  591. static DEFINE_RWLOCK(disc_data_lock);
  592. static struct mkiss *mkiss_get(struct tty_struct *tty)
  593. {
  594. struct mkiss *ax;
  595. read_lock(&disc_data_lock);
  596. ax = tty->disc_data;
  597. if (ax)
  598. atomic_inc(&ax->refcnt);
  599. read_unlock(&disc_data_lock);
  600. return ax;
  601. }
  602. static void mkiss_put(struct mkiss *ax)
  603. {
  604. if (atomic_dec_and_test(&ax->refcnt))
  605. up(&ax->dead_sem);
  606. }
  607. static int crc_force = 0; /* Can be overridden with insmod */
  608. static int mkiss_open(struct tty_struct *tty)
  609. {
  610. struct net_device *dev;
  611. struct mkiss *ax;
  612. int err;
  613. if (!capable(CAP_NET_ADMIN))
  614. return -EPERM;
  615. if (tty->ops->write == NULL)
  616. return -EOPNOTSUPP;
  617. dev = alloc_netdev(sizeof(struct mkiss), "ax%d", ax_setup);
  618. if (!dev) {
  619. err = -ENOMEM;
  620. goto out;
  621. }
  622. ax = netdev_priv(dev);
  623. ax->dev = dev;
  624. spin_lock_init(&ax->buflock);
  625. atomic_set(&ax->refcnt, 1);
  626. init_MUTEX_LOCKED(&ax->dead_sem);
  627. ax->tty = tty;
  628. tty->disc_data = ax;
  629. tty->receive_room = 65535;
  630. tty_driver_flush_buffer(tty);
  631. /* Restore default settings */
  632. dev->type = ARPHRD_AX25;
  633. /* Perform the low-level AX25 initialization. */
  634. if ((err = ax_open(ax->dev))) {
  635. goto out_free_netdev;
  636. }
  637. if (register_netdev(dev))
  638. goto out_free_buffers;
  639. /* after register_netdev() - because else printk smashes the kernel */
  640. switch (crc_force) {
  641. case 3:
  642. ax->crcmode = CRC_MODE_SMACK;
  643. printk(KERN_INFO "mkiss: %s: crc mode smack forced.\n",
  644. ax->dev->name);
  645. break;
  646. case 2:
  647. ax->crcmode = CRC_MODE_FLEX;
  648. printk(KERN_INFO "mkiss: %s: crc mode flexnet forced.\n",
  649. ax->dev->name);
  650. break;
  651. case 1:
  652. ax->crcmode = CRC_MODE_NONE;
  653. printk(KERN_INFO "mkiss: %s: crc mode disabled.\n",
  654. ax->dev->name);
  655. break;
  656. case 0:
  657. /* fall through */
  658. default:
  659. crc_force = 0;
  660. printk(KERN_INFO "mkiss: %s: crc mode is auto.\n",
  661. ax->dev->name);
  662. ax->crcmode = CRC_MODE_SMACK_TEST;
  663. }
  664. ax->crcauto = (crc_force ? 0 : 1);
  665. netif_start_queue(dev);
  666. /* Done. We have linked the TTY line to a channel. */
  667. return 0;
  668. out_free_buffers:
  669. kfree(ax->rbuff);
  670. kfree(ax->xbuff);
  671. out_free_netdev:
  672. free_netdev(dev);
  673. out:
  674. return err;
  675. }
  676. static void mkiss_close(struct tty_struct *tty)
  677. {
  678. struct mkiss *ax;
  679. write_lock(&disc_data_lock);
  680. ax = tty->disc_data;
  681. tty->disc_data = NULL;
  682. write_unlock(&disc_data_lock);
  683. if (!ax)
  684. return;
  685. /*
  686. * We have now ensured that nobody can start using ap from now on, but
  687. * we have to wait for all existing users to finish.
  688. */
  689. if (!atomic_dec_and_test(&ax->refcnt))
  690. down(&ax->dead_sem);
  691. unregister_netdev(ax->dev);
  692. /* Free all AX25 frame buffers. */
  693. kfree(ax->rbuff);
  694. kfree(ax->xbuff);
  695. ax->tty = NULL;
  696. }
  697. /* Perform I/O control on an active ax25 channel. */
  698. static int mkiss_ioctl(struct tty_struct *tty, struct file *file,
  699. unsigned int cmd, unsigned long arg)
  700. {
  701. struct mkiss *ax = mkiss_get(tty);
  702. struct net_device *dev = ax->dev;
  703. unsigned int tmp, err;
  704. /* First make sure we're connected. */
  705. if (ax == NULL)
  706. return -ENXIO;
  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. /*
  747. * Handle the 'receiver data ready' interrupt.
  748. * This function is called by the 'tty_io' module in the kernel when
  749. * a block of data has been received, which can now be decapsulated
  750. * and sent on to the AX.25 layer for further processing.
  751. */
  752. static void mkiss_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  753. char *fp, int count)
  754. {
  755. struct mkiss *ax = mkiss_get(tty);
  756. if (!ax)
  757. return;
  758. /*
  759. * Argh! mtu change time! - costs us the packet part received
  760. * at the change
  761. */
  762. if (ax->mtu != ax->dev->mtu + 73)
  763. ax_changedmtu(ax);
  764. /* Read the characters out of the buffer */
  765. while (count--) {
  766. if (fp != NULL && *fp++) {
  767. if (!test_and_set_bit(AXF_ERROR, &ax->flags))
  768. ax->stats.rx_errors++;
  769. cp++;
  770. continue;
  771. }
  772. kiss_unesc(ax, *cp++);
  773. }
  774. mkiss_put(ax);
  775. tty_unthrottle(tty);
  776. }
  777. /*
  778. * Called by the driver when there's room for more data. If we have
  779. * more packets to send, we send them here.
  780. */
  781. static void mkiss_write_wakeup(struct tty_struct *tty)
  782. {
  783. struct mkiss *ax = mkiss_get(tty);
  784. int actual;
  785. if (!ax)
  786. return;
  787. if (ax->xleft <= 0) {
  788. /* Now serial buffer is almost free & we can start
  789. * transmission of another packet
  790. */
  791. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  792. netif_wake_queue(ax->dev);
  793. goto out;
  794. }
  795. actual = tty->ops->write(tty, ax->xhead, ax->xleft);
  796. ax->xleft -= actual;
  797. ax->xhead += actual;
  798. out:
  799. mkiss_put(ax);
  800. }
  801. static struct tty_ldisc ax_ldisc = {
  802. .owner = THIS_MODULE,
  803. .magic = TTY_LDISC_MAGIC,
  804. .name = "mkiss",
  805. .open = mkiss_open,
  806. .close = mkiss_close,
  807. .ioctl = mkiss_ioctl,
  808. .receive_buf = mkiss_receive_buf,
  809. .write_wakeup = mkiss_write_wakeup
  810. };
  811. static char banner[] __initdata = KERN_INFO \
  812. "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
  813. static char msg_regfail[] __initdata = KERN_ERR \
  814. "mkiss: can't register line discipline (err = %d)\n";
  815. static int __init mkiss_init_driver(void)
  816. {
  817. int status;
  818. printk(banner);
  819. if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0)
  820. printk(msg_regfail);
  821. return status;
  822. }
  823. static const char msg_unregfail[] __exitdata = KERN_ERR \
  824. "mkiss: can't unregister line discipline (err = %d)\n";
  825. static void __exit mkiss_exit_driver(void)
  826. {
  827. int ret;
  828. if ((ret = tty_unregister_ldisc(N_AX25)))
  829. printk(msg_unregfail, ret);
  830. }
  831. MODULE_AUTHOR("Ralf Baechle DL5RB <ralf@linux-mips.org>");
  832. MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
  833. module_param(crc_force, int, 0);
  834. MODULE_PARM_DESC(crc_force, "crc [0 = auto | 1 = none | 2 = flexnet | 3 = smack]");
  835. MODULE_LICENSE("GPL");
  836. MODULE_ALIAS_LDISC(N_AX25);
  837. module_init(mkiss_init_driver);
  838. module_exit(mkiss_exit_driver);