x25_asy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Things to sort out:
  3. *
  4. * o tbusy handling
  5. * o allow users to set the parameters
  6. * o sync/async switching ?
  7. *
  8. * Note: This does _not_ implement CCITT X.25 asynchronous framing
  9. * recommendations. Its primarily for testing purposes. If you wanted
  10. * to do CCITT then in theory all you need is to nick the HDLC async
  11. * checksum routines from ppp.c
  12. * Changes:
  13. *
  14. * 2000-10-29 Henner Eisen lapb_data_indication() return status.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/bitops.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/in.h>
  24. #include <linux/tty.h>
  25. #include <linux/errno.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/lapb.h>
  31. #include <linux/init.h>
  32. #include <linux/rtnetlink.h>
  33. #include <linux/compat.h>
  34. #include <linux/slab.h>
  35. #include <net/x25device.h>
  36. #include "x25_asy.h"
  37. static struct net_device **x25_asy_devs;
  38. static int x25_asy_maxdev = SL_NRUNIT;
  39. module_param(x25_asy_maxdev, int, 0);
  40. MODULE_LICENSE("GPL");
  41. static int x25_asy_esc(unsigned char *p, unsigned char *d, int len);
  42. static void x25_asy_unesc(struct x25_asy *sl, unsigned char c);
  43. static void x25_asy_setup(struct net_device *dev);
  44. /* Find a free X.25 channel, and link in this `tty' line. */
  45. static struct x25_asy *x25_asy_alloc(void)
  46. {
  47. struct net_device *dev = NULL;
  48. struct x25_asy *sl;
  49. int i;
  50. if (x25_asy_devs == NULL)
  51. return NULL; /* Master array missing ! */
  52. for (i = 0; i < x25_asy_maxdev; i++) {
  53. dev = x25_asy_devs[i];
  54. /* Not allocated ? */
  55. if (dev == NULL)
  56. break;
  57. sl = netdev_priv(dev);
  58. /* Not in use ? */
  59. if (!test_and_set_bit(SLF_INUSE, &sl->flags))
  60. return sl;
  61. }
  62. /* Sorry, too many, all slots in use */
  63. if (i >= x25_asy_maxdev)
  64. return NULL;
  65. /* If no channels are available, allocate one */
  66. if (!dev) {
  67. char name[IFNAMSIZ];
  68. sprintf(name, "x25asy%d", i);
  69. dev = alloc_netdev(sizeof(struct x25_asy), name,
  70. NET_NAME_UNKNOWN, x25_asy_setup);
  71. if (!dev)
  72. return NULL;
  73. /* Initialize channel control data */
  74. sl = netdev_priv(dev);
  75. dev->base_addr = i;
  76. /* register device so that it can be ifconfig'ed */
  77. if (register_netdev(dev) == 0) {
  78. /* (Re-)Set the INUSE bit. Very Important! */
  79. set_bit(SLF_INUSE, &sl->flags);
  80. x25_asy_devs[i] = dev;
  81. return sl;
  82. } else {
  83. pr_warn("%s(): register_netdev() failure\n", __func__);
  84. free_netdev(dev);
  85. }
  86. }
  87. return NULL;
  88. }
  89. /* Free an X.25 channel. */
  90. static void x25_asy_free(struct x25_asy *sl)
  91. {
  92. /* Free all X.25 frame buffers. */
  93. kfree(sl->rbuff);
  94. sl->rbuff = NULL;
  95. kfree(sl->xbuff);
  96. sl->xbuff = NULL;
  97. if (!test_and_clear_bit(SLF_INUSE, &sl->flags))
  98. netdev_err(sl->dev, "x25_asy_free for already free unit\n");
  99. }
  100. static int x25_asy_change_mtu(struct net_device *dev, int newmtu)
  101. {
  102. struct x25_asy *sl = netdev_priv(dev);
  103. unsigned char *xbuff, *rbuff;
  104. int len;
  105. if (newmtu > 65534)
  106. return -EINVAL;
  107. len = 2 * newmtu;
  108. xbuff = kmalloc(len + 4, GFP_ATOMIC);
  109. rbuff = kmalloc(len + 4, GFP_ATOMIC);
  110. if (xbuff == NULL || rbuff == NULL) {
  111. kfree(xbuff);
  112. kfree(rbuff);
  113. return -ENOMEM;
  114. }
  115. spin_lock_bh(&sl->lock);
  116. xbuff = xchg(&sl->xbuff, xbuff);
  117. if (sl->xleft) {
  118. if (sl->xleft <= len) {
  119. memcpy(sl->xbuff, sl->xhead, sl->xleft);
  120. } else {
  121. sl->xleft = 0;
  122. dev->stats.tx_dropped++;
  123. }
  124. }
  125. sl->xhead = sl->xbuff;
  126. rbuff = xchg(&sl->rbuff, rbuff);
  127. if (sl->rcount) {
  128. if (sl->rcount <= len) {
  129. memcpy(sl->rbuff, rbuff, sl->rcount);
  130. } else {
  131. sl->rcount = 0;
  132. dev->stats.rx_over_errors++;
  133. set_bit(SLF_ERROR, &sl->flags);
  134. }
  135. }
  136. dev->mtu = newmtu;
  137. sl->buffsize = len;
  138. spin_unlock_bh(&sl->lock);
  139. kfree(xbuff);
  140. kfree(rbuff);
  141. return 0;
  142. }
  143. /* Set the "sending" flag. This must be atomic, hence the ASM. */
  144. static inline void x25_asy_lock(struct x25_asy *sl)
  145. {
  146. netif_stop_queue(sl->dev);
  147. }
  148. /* Clear the "sending" flag. This must be atomic, hence the ASM. */
  149. static inline void x25_asy_unlock(struct x25_asy *sl)
  150. {
  151. netif_wake_queue(sl->dev);
  152. }
  153. /* Send one completely decapsulated IP datagram to the IP layer. */
  154. static void x25_asy_bump(struct x25_asy *sl)
  155. {
  156. struct net_device *dev = sl->dev;
  157. struct sk_buff *skb;
  158. int count;
  159. int err;
  160. count = sl->rcount;
  161. dev->stats.rx_bytes += count;
  162. skb = dev_alloc_skb(count+1);
  163. if (skb == NULL) {
  164. netdev_warn(sl->dev, "memory squeeze, dropping packet\n");
  165. dev->stats.rx_dropped++;
  166. return;
  167. }
  168. skb_push(skb, 1); /* LAPB internal control */
  169. memcpy(skb_put(skb, count), sl->rbuff, count);
  170. skb->protocol = x25_type_trans(skb, sl->dev);
  171. err = lapb_data_received(skb->dev, skb);
  172. if (err != LAPB_OK) {
  173. kfree_skb(skb);
  174. printk(KERN_DEBUG "x25_asy: data received err - %d\n", err);
  175. } else {
  176. netif_rx(skb);
  177. dev->stats.rx_packets++;
  178. }
  179. }
  180. /* Encapsulate one IP datagram and stuff into a TTY queue. */
  181. static void x25_asy_encaps(struct x25_asy *sl, unsigned char *icp, int len)
  182. {
  183. unsigned char *p;
  184. int actual, count, mtu = sl->dev->mtu;
  185. if (len > mtu) {
  186. /* Sigh, shouldn't occur BUT ... */
  187. len = mtu;
  188. printk(KERN_DEBUG "%s: truncating oversized transmit packet!\n",
  189. sl->dev->name);
  190. sl->dev->stats.tx_dropped++;
  191. x25_asy_unlock(sl);
  192. return;
  193. }
  194. p = icp;
  195. count = x25_asy_esc(p, sl->xbuff, len);
  196. /* Order of next two lines is *very* important.
  197. * When we are sending a little amount of data,
  198. * the transfer may be completed inside driver.write()
  199. * routine, because it's running with interrupts enabled.
  200. * In this case we *never* got WRITE_WAKEUP event,
  201. * if we did not request it before write operation.
  202. * 14 Oct 1994 Dmitry Gorodchanin.
  203. */
  204. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  205. actual = sl->tty->ops->write(sl->tty, sl->xbuff, count);
  206. sl->xleft = count - actual;
  207. sl->xhead = sl->xbuff + actual;
  208. /* VSV */
  209. clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
  210. }
  211. /*
  212. * Called by the driver when there's room for more data. If we have
  213. * more packets to send, we send them here.
  214. */
  215. static void x25_asy_write_wakeup(struct tty_struct *tty)
  216. {
  217. int actual;
  218. struct x25_asy *sl = tty->disc_data;
  219. /* First make sure we're connected. */
  220. if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
  221. return;
  222. if (sl->xleft <= 0) {
  223. /* Now serial buffer is almost free & we can start
  224. * transmission of another packet */
  225. sl->dev->stats.tx_packets++;
  226. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  227. x25_asy_unlock(sl);
  228. return;
  229. }
  230. actual = tty->ops->write(tty, sl->xhead, sl->xleft);
  231. sl->xleft -= actual;
  232. sl->xhead += actual;
  233. }
  234. static void x25_asy_timeout(struct net_device *dev)
  235. {
  236. struct x25_asy *sl = netdev_priv(dev);
  237. spin_lock(&sl->lock);
  238. if (netif_queue_stopped(dev)) {
  239. /* May be we must check transmitter timeout here ?
  240. * 14 Oct 1994 Dmitry Gorodchanin.
  241. */
  242. netdev_warn(dev, "transmit timed out, %s?\n",
  243. (tty_chars_in_buffer(sl->tty) || sl->xleft) ?
  244. "bad line quality" : "driver error");
  245. sl->xleft = 0;
  246. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  247. x25_asy_unlock(sl);
  248. }
  249. spin_unlock(&sl->lock);
  250. }
  251. /* Encapsulate an IP datagram and kick it into a TTY queue. */
  252. static netdev_tx_t x25_asy_xmit(struct sk_buff *skb,
  253. struct net_device *dev)
  254. {
  255. struct x25_asy *sl = netdev_priv(dev);
  256. int err;
  257. if (!netif_running(sl->dev)) {
  258. netdev_err(dev, "xmit call when iface is down\n");
  259. kfree_skb(skb);
  260. return NETDEV_TX_OK;
  261. }
  262. switch (skb->data[0]) {
  263. case X25_IFACE_DATA:
  264. break;
  265. case X25_IFACE_CONNECT: /* Connection request .. do nothing */
  266. err = lapb_connect_request(dev);
  267. if (err != LAPB_OK)
  268. netdev_err(dev, "lapb_connect_request error: %d\n",
  269. err);
  270. kfree_skb(skb);
  271. return NETDEV_TX_OK;
  272. case X25_IFACE_DISCONNECT: /* do nothing - hang up ?? */
  273. err = lapb_disconnect_request(dev);
  274. if (err != LAPB_OK)
  275. netdev_err(dev, "lapb_disconnect_request error: %d\n",
  276. err);
  277. default:
  278. kfree_skb(skb);
  279. return NETDEV_TX_OK;
  280. }
  281. skb_pull(skb, 1); /* Remove control byte */
  282. /*
  283. * If we are busy already- too bad. We ought to be able
  284. * to queue things at this point, to allow for a little
  285. * frame buffer. Oh well...
  286. * -----------------------------------------------------
  287. * I hate queues in X.25 driver. May be it's efficient,
  288. * but for me latency is more important. ;)
  289. * So, no queues !
  290. * 14 Oct 1994 Dmitry Gorodchanin.
  291. */
  292. err = lapb_data_request(dev, skb);
  293. if (err != LAPB_OK) {
  294. netdev_err(dev, "lapb_data_request error: %d\n", err);
  295. kfree_skb(skb);
  296. return NETDEV_TX_OK;
  297. }
  298. return NETDEV_TX_OK;
  299. }
  300. /*
  301. * LAPB interface boilerplate
  302. */
  303. /*
  304. * Called when I frame data arrives. We did the work above - throw it
  305. * at the net layer.
  306. */
  307. static int x25_asy_data_indication(struct net_device *dev, struct sk_buff *skb)
  308. {
  309. return netif_rx(skb);
  310. }
  311. /*
  312. * Data has emerged from the LAPB protocol machine. We don't handle
  313. * busy cases too well. Its tricky to see how to do this nicely -
  314. * perhaps lapb should allow us to bounce this ?
  315. */
  316. static void x25_asy_data_transmit(struct net_device *dev, struct sk_buff *skb)
  317. {
  318. struct x25_asy *sl = netdev_priv(dev);
  319. spin_lock(&sl->lock);
  320. if (netif_queue_stopped(sl->dev) || sl->tty == NULL) {
  321. spin_unlock(&sl->lock);
  322. netdev_err(dev, "tbusy drop\n");
  323. kfree_skb(skb);
  324. return;
  325. }
  326. /* We were not busy, so we are now... :-) */
  327. if (skb != NULL) {
  328. x25_asy_lock(sl);
  329. dev->stats.tx_bytes += skb->len;
  330. x25_asy_encaps(sl, skb->data, skb->len);
  331. dev_kfree_skb(skb);
  332. }
  333. spin_unlock(&sl->lock);
  334. }
  335. /*
  336. * LAPB connection establish/down information.
  337. */
  338. static void x25_asy_connected(struct net_device *dev, int reason)
  339. {
  340. struct x25_asy *sl = netdev_priv(dev);
  341. struct sk_buff *skb;
  342. unsigned char *ptr;
  343. skb = dev_alloc_skb(1);
  344. if (skb == NULL) {
  345. netdev_err(dev, "out of memory\n");
  346. return;
  347. }
  348. ptr = skb_put(skb, 1);
  349. *ptr = X25_IFACE_CONNECT;
  350. skb->protocol = x25_type_trans(skb, sl->dev);
  351. netif_rx(skb);
  352. }
  353. static void x25_asy_disconnected(struct net_device *dev, int reason)
  354. {
  355. struct x25_asy *sl = netdev_priv(dev);
  356. struct sk_buff *skb;
  357. unsigned char *ptr;
  358. skb = dev_alloc_skb(1);
  359. if (skb == NULL) {
  360. netdev_err(dev, "out of memory\n");
  361. return;
  362. }
  363. ptr = skb_put(skb, 1);
  364. *ptr = X25_IFACE_DISCONNECT;
  365. skb->protocol = x25_type_trans(skb, sl->dev);
  366. netif_rx(skb);
  367. }
  368. static const struct lapb_register_struct x25_asy_callbacks = {
  369. .connect_confirmation = x25_asy_connected,
  370. .connect_indication = x25_asy_connected,
  371. .disconnect_confirmation = x25_asy_disconnected,
  372. .disconnect_indication = x25_asy_disconnected,
  373. .data_indication = x25_asy_data_indication,
  374. .data_transmit = x25_asy_data_transmit,
  375. };
  376. /* Open the low-level part of the X.25 channel. Easy! */
  377. static int x25_asy_open(struct net_device *dev)
  378. {
  379. struct x25_asy *sl = netdev_priv(dev);
  380. unsigned long len;
  381. int err;
  382. if (sl->tty == NULL)
  383. return -ENODEV;
  384. /*
  385. * Allocate the X.25 frame buffers:
  386. *
  387. * rbuff Receive buffer.
  388. * xbuff Transmit buffer.
  389. */
  390. len = dev->mtu * 2;
  391. sl->rbuff = kmalloc(len + 4, GFP_KERNEL);
  392. if (sl->rbuff == NULL)
  393. goto norbuff;
  394. sl->xbuff = kmalloc(len + 4, GFP_KERNEL);
  395. if (sl->xbuff == NULL)
  396. goto noxbuff;
  397. sl->buffsize = len;
  398. sl->rcount = 0;
  399. sl->xleft = 0;
  400. sl->flags &= (1 << SLF_INUSE); /* Clear ESCAPE & ERROR flags */
  401. netif_start_queue(dev);
  402. /*
  403. * Now attach LAPB
  404. */
  405. err = lapb_register(dev, &x25_asy_callbacks);
  406. if (err == LAPB_OK)
  407. return 0;
  408. /* Cleanup */
  409. kfree(sl->xbuff);
  410. noxbuff:
  411. kfree(sl->rbuff);
  412. norbuff:
  413. return -ENOMEM;
  414. }
  415. /* Close the low-level part of the X.25 channel. Easy! */
  416. static int x25_asy_close(struct net_device *dev)
  417. {
  418. struct x25_asy *sl = netdev_priv(dev);
  419. spin_lock(&sl->lock);
  420. if (sl->tty)
  421. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  422. netif_stop_queue(dev);
  423. sl->rcount = 0;
  424. sl->xleft = 0;
  425. spin_unlock(&sl->lock);
  426. return 0;
  427. }
  428. /*
  429. * Handle the 'receiver data ready' interrupt.
  430. * This function is called by the 'tty_io' module in the kernel when
  431. * a block of X.25 data has been received, which can now be decapsulated
  432. * and sent on to some IP layer for further processing.
  433. */
  434. static void x25_asy_receive_buf(struct tty_struct *tty,
  435. const unsigned char *cp, char *fp, int count)
  436. {
  437. struct x25_asy *sl = tty->disc_data;
  438. if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
  439. return;
  440. /* Read the characters out of the buffer */
  441. while (count--) {
  442. if (fp && *fp++) {
  443. if (!test_and_set_bit(SLF_ERROR, &sl->flags))
  444. sl->dev->stats.rx_errors++;
  445. cp++;
  446. continue;
  447. }
  448. x25_asy_unesc(sl, *cp++);
  449. }
  450. }
  451. /*
  452. * Open the high-level part of the X.25 channel.
  453. * This function is called by the TTY module when the
  454. * X.25 line discipline is called for. Because we are
  455. * sure the tty line exists, we only have to link it to
  456. * a free X.25 channel...
  457. */
  458. static int x25_asy_open_tty(struct tty_struct *tty)
  459. {
  460. struct x25_asy *sl = tty->disc_data;
  461. int err;
  462. if (tty->ops->write == NULL)
  463. return -EOPNOTSUPP;
  464. /* First make sure we're not already connected. */
  465. if (sl && sl->magic == X25_ASY_MAGIC)
  466. return -EEXIST;
  467. /* OK. Find a free X.25 channel to use. */
  468. sl = x25_asy_alloc();
  469. if (sl == NULL)
  470. return -ENFILE;
  471. sl->tty = tty;
  472. tty->disc_data = sl;
  473. tty->receive_room = 65536;
  474. tty_driver_flush_buffer(tty);
  475. tty_ldisc_flush(tty);
  476. /* Restore default settings */
  477. sl->dev->type = ARPHRD_X25;
  478. /* Perform the low-level X.25 async init */
  479. err = x25_asy_open(sl->dev);
  480. if (err)
  481. return err;
  482. /* Done. We have linked the TTY line to a channel. */
  483. return 0;
  484. }
  485. /*
  486. * Close down an X.25 channel.
  487. * This means flushing out any pending queues, and then restoring the
  488. * TTY line discipline to what it was before it got hooked to X.25
  489. * (which usually is TTY again).
  490. */
  491. static void x25_asy_close_tty(struct tty_struct *tty)
  492. {
  493. struct x25_asy *sl = tty->disc_data;
  494. int err;
  495. /* First make sure we're connected. */
  496. if (!sl || sl->magic != X25_ASY_MAGIC)
  497. return;
  498. rtnl_lock();
  499. if (sl->dev->flags & IFF_UP)
  500. dev_close(sl->dev);
  501. rtnl_unlock();
  502. err = lapb_unregister(sl->dev);
  503. if (err != LAPB_OK)
  504. pr_err("x25_asy_close: lapb_unregister error: %d\n",
  505. err);
  506. tty->disc_data = NULL;
  507. sl->tty = NULL;
  508. x25_asy_free(sl);
  509. }
  510. /************************************************************************
  511. * STANDARD X.25 ENCAPSULATION *
  512. ************************************************************************/
  513. static int x25_asy_esc(unsigned char *s, unsigned char *d, int len)
  514. {
  515. unsigned char *ptr = d;
  516. unsigned char c;
  517. /*
  518. * Send an initial END character to flush out any
  519. * data that may have accumulated in the receiver
  520. * due to line noise.
  521. */
  522. *ptr++ = X25_END; /* Send 10111110 bit seq */
  523. /*
  524. * For each byte in the packet, send the appropriate
  525. * character sequence, according to the X.25 protocol.
  526. */
  527. while (len-- > 0) {
  528. switch (c = *s++) {
  529. case X25_END:
  530. *ptr++ = X25_ESC;
  531. *ptr++ = X25_ESCAPE(X25_END);
  532. break;
  533. case X25_ESC:
  534. *ptr++ = X25_ESC;
  535. *ptr++ = X25_ESCAPE(X25_ESC);
  536. break;
  537. default:
  538. *ptr++ = c;
  539. break;
  540. }
  541. }
  542. *ptr++ = X25_END;
  543. return ptr - d;
  544. }
  545. static void x25_asy_unesc(struct x25_asy *sl, unsigned char s)
  546. {
  547. switch (s) {
  548. case X25_END:
  549. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  550. sl->rcount > 2)
  551. x25_asy_bump(sl);
  552. clear_bit(SLF_ESCAPE, &sl->flags);
  553. sl->rcount = 0;
  554. return;
  555. case X25_ESC:
  556. set_bit(SLF_ESCAPE, &sl->flags);
  557. return;
  558. case X25_ESCAPE(X25_ESC):
  559. case X25_ESCAPE(X25_END):
  560. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  561. s = X25_UNESCAPE(s);
  562. break;
  563. }
  564. if (!test_bit(SLF_ERROR, &sl->flags)) {
  565. if (sl->rcount < sl->buffsize) {
  566. sl->rbuff[sl->rcount++] = s;
  567. return;
  568. }
  569. sl->dev->stats.rx_over_errors++;
  570. set_bit(SLF_ERROR, &sl->flags);
  571. }
  572. }
  573. /* Perform I/O control on an active X.25 channel. */
  574. static int x25_asy_ioctl(struct tty_struct *tty, struct file *file,
  575. unsigned int cmd, unsigned long arg)
  576. {
  577. struct x25_asy *sl = tty->disc_data;
  578. /* First make sure we're connected. */
  579. if (!sl || sl->magic != X25_ASY_MAGIC)
  580. return -EINVAL;
  581. switch (cmd) {
  582. case SIOCGIFNAME:
  583. if (copy_to_user((void __user *)arg, sl->dev->name,
  584. strlen(sl->dev->name) + 1))
  585. return -EFAULT;
  586. return 0;
  587. case SIOCSIFHWADDR:
  588. return -EINVAL;
  589. default:
  590. return tty_mode_ioctl(tty, file, cmd, arg);
  591. }
  592. }
  593. #ifdef CONFIG_COMPAT
  594. static long x25_asy_compat_ioctl(struct tty_struct *tty, struct file *file,
  595. unsigned int cmd, unsigned long arg)
  596. {
  597. switch (cmd) {
  598. case SIOCGIFNAME:
  599. case SIOCSIFHWADDR:
  600. return x25_asy_ioctl(tty, file, cmd,
  601. (unsigned long)compat_ptr(arg));
  602. }
  603. return -ENOIOCTLCMD;
  604. }
  605. #endif
  606. static int x25_asy_open_dev(struct net_device *dev)
  607. {
  608. struct x25_asy *sl = netdev_priv(dev);
  609. if (sl->tty == NULL)
  610. return -ENODEV;
  611. return 0;
  612. }
  613. static const struct net_device_ops x25_asy_netdev_ops = {
  614. .ndo_open = x25_asy_open_dev,
  615. .ndo_stop = x25_asy_close,
  616. .ndo_start_xmit = x25_asy_xmit,
  617. .ndo_tx_timeout = x25_asy_timeout,
  618. .ndo_change_mtu = x25_asy_change_mtu,
  619. };
  620. /* Initialise the X.25 driver. Called by the device init code */
  621. static void x25_asy_setup(struct net_device *dev)
  622. {
  623. struct x25_asy *sl = netdev_priv(dev);
  624. sl->magic = X25_ASY_MAGIC;
  625. sl->dev = dev;
  626. spin_lock_init(&sl->lock);
  627. set_bit(SLF_INUSE, &sl->flags);
  628. /*
  629. * Finish setting up the DEVICE info.
  630. */
  631. dev->mtu = SL_MTU;
  632. dev->netdev_ops = &x25_asy_netdev_ops;
  633. dev->watchdog_timeo = HZ*20;
  634. dev->hard_header_len = 0;
  635. dev->addr_len = 0;
  636. dev->type = ARPHRD_X25;
  637. dev->tx_queue_len = 10;
  638. /* New-style flags. */
  639. dev->flags = IFF_NOARP;
  640. }
  641. static struct tty_ldisc_ops x25_ldisc = {
  642. .owner = THIS_MODULE,
  643. .magic = TTY_LDISC_MAGIC,
  644. .name = "X.25",
  645. .open = x25_asy_open_tty,
  646. .close = x25_asy_close_tty,
  647. .ioctl = x25_asy_ioctl,
  648. #ifdef CONFIG_COMPAT
  649. .compat_ioctl = x25_asy_compat_ioctl,
  650. #endif
  651. .receive_buf = x25_asy_receive_buf,
  652. .write_wakeup = x25_asy_write_wakeup,
  653. };
  654. static int __init init_x25_asy(void)
  655. {
  656. if (x25_asy_maxdev < 4)
  657. x25_asy_maxdev = 4; /* Sanity */
  658. pr_info("X.25 async: version 0.00 ALPHA (dynamic channels, max=%d)\n",
  659. x25_asy_maxdev);
  660. x25_asy_devs = kcalloc(x25_asy_maxdev, sizeof(struct net_device *),
  661. GFP_KERNEL);
  662. if (!x25_asy_devs)
  663. return -ENOMEM;
  664. return tty_register_ldisc(N_X25, &x25_ldisc);
  665. }
  666. static void __exit exit_x25_asy(void)
  667. {
  668. struct net_device *dev;
  669. int i;
  670. for (i = 0; i < x25_asy_maxdev; i++) {
  671. dev = x25_asy_devs[i];
  672. if (dev) {
  673. struct x25_asy *sl = netdev_priv(dev);
  674. spin_lock_bh(&sl->lock);
  675. if (sl->tty)
  676. tty_hangup(sl->tty);
  677. spin_unlock_bh(&sl->lock);
  678. /*
  679. * VSV = if dev->start==0, then device
  680. * unregistered while close proc.
  681. */
  682. unregister_netdev(dev);
  683. free_netdev(dev);
  684. }
  685. }
  686. kfree(x25_asy_devs);
  687. tty_unregister_ldisc(N_X25);
  688. }
  689. module_init(init_x25_asy);
  690. module_exit(exit_x25_asy);