ppp_async.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * PPP async serial channel driver for Linux.
  3. *
  4. * Copyright 1999 Paul Mackerras.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * This driver provides the encapsulation and framing for sending
  12. * and receiving PPP frames over async serial lines. It relies on
  13. * the generic PPP layer to give it frames to send and to process
  14. * received frames. It implements the PPP line discipline.
  15. *
  16. * Part of the code in this driver was inspired by the old async-only
  17. * PPP driver, written by Michael Callahan and Al Longyear, and
  18. * subsequently hacked by Paul Mackerras.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/tty.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/poll.h>
  26. #include <linux/crc-ccitt.h>
  27. #include <linux/ppp_defs.h>
  28. #include <linux/if_ppp.h>
  29. #include <linux/ppp_channel.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/init.h>
  32. #include <asm/uaccess.h>
  33. #define PPP_VERSION "2.4.2"
  34. #define OBUFSIZE 256
  35. /* Structure for storing local state. */
  36. struct asyncppp {
  37. struct tty_struct *tty;
  38. unsigned int flags;
  39. unsigned int state;
  40. unsigned int rbits;
  41. int mru;
  42. spinlock_t xmit_lock;
  43. spinlock_t recv_lock;
  44. unsigned long xmit_flags;
  45. u32 xaccm[8];
  46. u32 raccm;
  47. unsigned int bytes_sent;
  48. unsigned int bytes_rcvd;
  49. struct sk_buff *tpkt;
  50. int tpkt_pos;
  51. u16 tfcs;
  52. unsigned char *optr;
  53. unsigned char *olim;
  54. unsigned long last_xmit;
  55. struct sk_buff *rpkt;
  56. int lcp_fcs;
  57. struct sk_buff_head rqueue;
  58. struct tasklet_struct tsk;
  59. atomic_t refcnt;
  60. struct semaphore dead_sem;
  61. struct ppp_channel chan; /* interface to generic ppp layer */
  62. unsigned char obuf[OBUFSIZE];
  63. };
  64. /* Bit numbers in xmit_flags */
  65. #define XMIT_WAKEUP 0
  66. #define XMIT_FULL 1
  67. #define XMIT_BUSY 2
  68. /* State bits */
  69. #define SC_TOSS 1
  70. #define SC_ESCAPE 2
  71. #define SC_PREV_ERROR 4
  72. /* Bits in rbits */
  73. #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
  74. static int flag_time = HZ;
  75. module_param(flag_time, int, 0);
  76. MODULE_PARM_DESC(flag_time, "ppp_async: interval between flagged packets (in clock ticks)");
  77. MODULE_LICENSE("GPL");
  78. MODULE_ALIAS_LDISC(N_PPP);
  79. /*
  80. * Prototypes.
  81. */
  82. static int ppp_async_encode(struct asyncppp *ap);
  83. static int ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb);
  84. static int ppp_async_push(struct asyncppp *ap);
  85. static void ppp_async_flush_output(struct asyncppp *ap);
  86. static void ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
  87. char *flags, int count);
  88. static int ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd,
  89. unsigned long arg);
  90. static void ppp_async_process(unsigned long arg);
  91. static void async_lcp_peek(struct asyncppp *ap, unsigned char *data,
  92. int len, int inbound);
  93. static struct ppp_channel_ops async_ops = {
  94. ppp_async_send,
  95. ppp_async_ioctl
  96. };
  97. /*
  98. * Routines implementing the PPP line discipline.
  99. */
  100. /*
  101. * We have a potential race on dereferencing tty->disc_data,
  102. * because the tty layer provides no locking at all - thus one
  103. * cpu could be running ppp_asynctty_receive while another
  104. * calls ppp_asynctty_close, which zeroes tty->disc_data and
  105. * frees the memory that ppp_asynctty_receive is using. The best
  106. * way to fix this is to use a rwlock in the tty struct, but for now
  107. * we use a single global rwlock for all ttys in ppp line discipline.
  108. *
  109. * FIXME: this is no longer true. The _close path for the ldisc is
  110. * now guaranteed to be sane.
  111. */
  112. static DEFINE_RWLOCK(disc_data_lock);
  113. static struct asyncppp *ap_get(struct tty_struct *tty)
  114. {
  115. struct asyncppp *ap;
  116. read_lock(&disc_data_lock);
  117. ap = tty->disc_data;
  118. if (ap != NULL)
  119. atomic_inc(&ap->refcnt);
  120. read_unlock(&disc_data_lock);
  121. return ap;
  122. }
  123. static void ap_put(struct asyncppp *ap)
  124. {
  125. if (atomic_dec_and_test(&ap->refcnt))
  126. up(&ap->dead_sem);
  127. }
  128. /*
  129. * Called when a tty is put into PPP line discipline. Called in process
  130. * context.
  131. */
  132. static int
  133. ppp_asynctty_open(struct tty_struct *tty)
  134. {
  135. struct asyncppp *ap;
  136. int err;
  137. err = -ENOMEM;
  138. ap = kmalloc(sizeof(*ap), GFP_KERNEL);
  139. if (ap == 0)
  140. goto out;
  141. /* initialize the asyncppp structure */
  142. memset(ap, 0, sizeof(*ap));
  143. ap->tty = tty;
  144. ap->mru = PPP_MRU;
  145. spin_lock_init(&ap->xmit_lock);
  146. spin_lock_init(&ap->recv_lock);
  147. ap->xaccm[0] = ~0U;
  148. ap->xaccm[3] = 0x60000000U;
  149. ap->raccm = ~0U;
  150. ap->optr = ap->obuf;
  151. ap->olim = ap->obuf;
  152. ap->lcp_fcs = -1;
  153. skb_queue_head_init(&ap->rqueue);
  154. tasklet_init(&ap->tsk, ppp_async_process, (unsigned long) ap);
  155. atomic_set(&ap->refcnt, 1);
  156. init_MUTEX_LOCKED(&ap->dead_sem);
  157. ap->chan.private = ap;
  158. ap->chan.ops = &async_ops;
  159. ap->chan.mtu = PPP_MRU;
  160. err = ppp_register_channel(&ap->chan);
  161. if (err)
  162. goto out_free;
  163. tty->disc_data = ap;
  164. return 0;
  165. out_free:
  166. kfree(ap);
  167. out:
  168. return err;
  169. }
  170. /*
  171. * Called when the tty is put into another line discipline
  172. * or it hangs up. We have to wait for any cpu currently
  173. * executing in any of the other ppp_asynctty_* routines to
  174. * finish before we can call ppp_unregister_channel and free
  175. * the asyncppp struct. This routine must be called from
  176. * process context, not interrupt or softirq context.
  177. */
  178. static void
  179. ppp_asynctty_close(struct tty_struct *tty)
  180. {
  181. struct asyncppp *ap;
  182. write_lock_irq(&disc_data_lock);
  183. ap = tty->disc_data;
  184. tty->disc_data = NULL;
  185. write_unlock_irq(&disc_data_lock);
  186. if (ap == 0)
  187. return;
  188. /*
  189. * We have now ensured that nobody can start using ap from now
  190. * on, but we have to wait for all existing users to finish.
  191. * Note that ppp_unregister_channel ensures that no calls to
  192. * our channel ops (i.e. ppp_async_send/ioctl) are in progress
  193. * by the time it returns.
  194. */
  195. if (!atomic_dec_and_test(&ap->refcnt))
  196. down(&ap->dead_sem);
  197. tasklet_kill(&ap->tsk);
  198. ppp_unregister_channel(&ap->chan);
  199. if (ap->rpkt != 0)
  200. kfree_skb(ap->rpkt);
  201. skb_queue_purge(&ap->rqueue);
  202. if (ap->tpkt != 0)
  203. kfree_skb(ap->tpkt);
  204. kfree(ap);
  205. }
  206. /*
  207. * Called on tty hangup in process context.
  208. *
  209. * Wait for I/O to driver to complete and unregister PPP channel.
  210. * This is already done by the close routine, so just call that.
  211. */
  212. static int ppp_asynctty_hangup(struct tty_struct *tty)
  213. {
  214. ppp_asynctty_close(tty);
  215. return 0;
  216. }
  217. /*
  218. * Read does nothing - no data is ever available this way.
  219. * Pppd reads and writes packets via /dev/ppp instead.
  220. */
  221. static ssize_t
  222. ppp_asynctty_read(struct tty_struct *tty, struct file *file,
  223. unsigned char __user *buf, size_t count)
  224. {
  225. return -EAGAIN;
  226. }
  227. /*
  228. * Write on the tty does nothing, the packets all come in
  229. * from the ppp generic stuff.
  230. */
  231. static ssize_t
  232. ppp_asynctty_write(struct tty_struct *tty, struct file *file,
  233. const unsigned char *buf, size_t count)
  234. {
  235. return -EAGAIN;
  236. }
  237. /*
  238. * Called in process context only. May be re-entered by multiple
  239. * ioctl calling threads.
  240. */
  241. static int
  242. ppp_asynctty_ioctl(struct tty_struct *tty, struct file *file,
  243. unsigned int cmd, unsigned long arg)
  244. {
  245. struct asyncppp *ap = ap_get(tty);
  246. int err, val;
  247. int __user *p = (int __user *)arg;
  248. if (ap == 0)
  249. return -ENXIO;
  250. err = -EFAULT;
  251. switch (cmd) {
  252. case PPPIOCGCHAN:
  253. err = -ENXIO;
  254. if (ap == 0)
  255. break;
  256. err = -EFAULT;
  257. if (put_user(ppp_channel_index(&ap->chan), p))
  258. break;
  259. err = 0;
  260. break;
  261. case PPPIOCGUNIT:
  262. err = -ENXIO;
  263. if (ap == 0)
  264. break;
  265. err = -EFAULT;
  266. if (put_user(ppp_unit_number(&ap->chan), p))
  267. break;
  268. err = 0;
  269. break;
  270. case TCGETS:
  271. case TCGETA:
  272. err = n_tty_ioctl(tty, file, cmd, arg);
  273. break;
  274. case TCFLSH:
  275. /* flush our buffers and the serial port's buffer */
  276. if (arg == TCIOFLUSH || arg == TCOFLUSH)
  277. ppp_async_flush_output(ap);
  278. err = n_tty_ioctl(tty, file, cmd, arg);
  279. break;
  280. case FIONREAD:
  281. val = 0;
  282. if (put_user(val, p))
  283. break;
  284. err = 0;
  285. break;
  286. default:
  287. err = -ENOIOCTLCMD;
  288. }
  289. ap_put(ap);
  290. return err;
  291. }
  292. /* No kernel lock - fine */
  293. static unsigned int
  294. ppp_asynctty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
  295. {
  296. return 0;
  297. }
  298. static int
  299. ppp_asynctty_room(struct tty_struct *tty)
  300. {
  301. return 65535;
  302. }
  303. /*
  304. * This can now be called from hard interrupt level as well
  305. * as soft interrupt level or mainline.
  306. */
  307. static void
  308. ppp_asynctty_receive(struct tty_struct *tty, const unsigned char *buf,
  309. char *cflags, int count)
  310. {
  311. struct asyncppp *ap = ap_get(tty);
  312. unsigned long flags;
  313. if (ap == 0)
  314. return;
  315. spin_lock_irqsave(&ap->recv_lock, flags);
  316. ppp_async_input(ap, buf, cflags, count);
  317. spin_unlock_irqrestore(&ap->recv_lock, flags);
  318. if (!skb_queue_empty(&ap->rqueue))
  319. tasklet_schedule(&ap->tsk);
  320. ap_put(ap);
  321. if (test_and_clear_bit(TTY_THROTTLED, &tty->flags)
  322. && tty->driver->unthrottle)
  323. tty->driver->unthrottle(tty);
  324. }
  325. static void
  326. ppp_asynctty_wakeup(struct tty_struct *tty)
  327. {
  328. struct asyncppp *ap = ap_get(tty);
  329. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  330. if (ap == 0)
  331. return;
  332. set_bit(XMIT_WAKEUP, &ap->xmit_flags);
  333. tasklet_schedule(&ap->tsk);
  334. ap_put(ap);
  335. }
  336. static struct tty_ldisc ppp_ldisc = {
  337. .owner = THIS_MODULE,
  338. .magic = TTY_LDISC_MAGIC,
  339. .name = "ppp",
  340. .open = ppp_asynctty_open,
  341. .close = ppp_asynctty_close,
  342. .hangup = ppp_asynctty_hangup,
  343. .read = ppp_asynctty_read,
  344. .write = ppp_asynctty_write,
  345. .ioctl = ppp_asynctty_ioctl,
  346. .poll = ppp_asynctty_poll,
  347. .receive_room = ppp_asynctty_room,
  348. .receive_buf = ppp_asynctty_receive,
  349. .write_wakeup = ppp_asynctty_wakeup,
  350. };
  351. static int __init
  352. ppp_async_init(void)
  353. {
  354. int err;
  355. err = tty_register_ldisc(N_PPP, &ppp_ldisc);
  356. if (err != 0)
  357. printk(KERN_ERR "PPP_async: error %d registering line disc.\n",
  358. err);
  359. return err;
  360. }
  361. /*
  362. * The following routines provide the PPP channel interface.
  363. */
  364. static int
  365. ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
  366. {
  367. struct asyncppp *ap = chan->private;
  368. void __user *argp = (void __user *)arg;
  369. int __user *p = argp;
  370. int err, val;
  371. u32 accm[8];
  372. err = -EFAULT;
  373. switch (cmd) {
  374. case PPPIOCGFLAGS:
  375. val = ap->flags | ap->rbits;
  376. if (put_user(val, p))
  377. break;
  378. err = 0;
  379. break;
  380. case PPPIOCSFLAGS:
  381. if (get_user(val, p))
  382. break;
  383. ap->flags = val & ~SC_RCV_BITS;
  384. spin_lock_irq(&ap->recv_lock);
  385. ap->rbits = val & SC_RCV_BITS;
  386. spin_unlock_irq(&ap->recv_lock);
  387. err = 0;
  388. break;
  389. case PPPIOCGASYNCMAP:
  390. if (put_user(ap->xaccm[0], (u32 __user *)argp))
  391. break;
  392. err = 0;
  393. break;
  394. case PPPIOCSASYNCMAP:
  395. if (get_user(ap->xaccm[0], (u32 __user *)argp))
  396. break;
  397. err = 0;
  398. break;
  399. case PPPIOCGRASYNCMAP:
  400. if (put_user(ap->raccm, (u32 __user *)argp))
  401. break;
  402. err = 0;
  403. break;
  404. case PPPIOCSRASYNCMAP:
  405. if (get_user(ap->raccm, (u32 __user *)argp))
  406. break;
  407. err = 0;
  408. break;
  409. case PPPIOCGXASYNCMAP:
  410. if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  411. break;
  412. err = 0;
  413. break;
  414. case PPPIOCSXASYNCMAP:
  415. if (copy_from_user(accm, argp, sizeof(accm)))
  416. break;
  417. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  418. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  419. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  420. err = 0;
  421. break;
  422. case PPPIOCGMRU:
  423. if (put_user(ap->mru, p))
  424. break;
  425. err = 0;
  426. break;
  427. case PPPIOCSMRU:
  428. if (get_user(val, p))
  429. break;
  430. if (val < PPP_MRU)
  431. val = PPP_MRU;
  432. ap->mru = val;
  433. err = 0;
  434. break;
  435. default:
  436. err = -ENOTTY;
  437. }
  438. return err;
  439. }
  440. /*
  441. * This is called at softirq level to deliver received packets
  442. * to the ppp_generic code, and to tell the ppp_generic code
  443. * if we can accept more output now.
  444. */
  445. static void ppp_async_process(unsigned long arg)
  446. {
  447. struct asyncppp *ap = (struct asyncppp *) arg;
  448. struct sk_buff *skb;
  449. /* process received packets */
  450. while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
  451. if (skb->cb[0])
  452. ppp_input_error(&ap->chan, 0);
  453. ppp_input(&ap->chan, skb);
  454. }
  455. /* try to push more stuff out */
  456. if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_async_push(ap))
  457. ppp_output_wakeup(&ap->chan);
  458. }
  459. /*
  460. * Procedures for encapsulation and framing.
  461. */
  462. /*
  463. * Procedure to encode the data for async serial transmission.
  464. * Does octet stuffing (escaping), puts the address/control bytes
  465. * on if A/C compression is disabled, and does protocol compression.
  466. * Assumes ap->tpkt != 0 on entry.
  467. * Returns 1 if we finished the current frame, 0 otherwise.
  468. */
  469. #define PUT_BYTE(ap, buf, c, islcp) do { \
  470. if ((islcp && c < 0x20) || (ap->xaccm[c >> 5] & (1 << (c & 0x1f)))) {\
  471. *buf++ = PPP_ESCAPE; \
  472. *buf++ = c ^ 0x20; \
  473. } else \
  474. *buf++ = c; \
  475. } while (0)
  476. static int
  477. ppp_async_encode(struct asyncppp *ap)
  478. {
  479. int fcs, i, count, c, proto;
  480. unsigned char *buf, *buflim;
  481. unsigned char *data;
  482. int islcp;
  483. buf = ap->obuf;
  484. ap->olim = buf;
  485. ap->optr = buf;
  486. i = ap->tpkt_pos;
  487. data = ap->tpkt->data;
  488. count = ap->tpkt->len;
  489. fcs = ap->tfcs;
  490. proto = (data[0] << 8) + data[1];
  491. /*
  492. * LCP packets with code values between 1 (configure-reqest)
  493. * and 7 (code-reject) must be sent as though no options
  494. * had been negotiated.
  495. */
  496. islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
  497. if (i == 0) {
  498. if (islcp)
  499. async_lcp_peek(ap, data, count, 0);
  500. /*
  501. * Start of a new packet - insert the leading FLAG
  502. * character if necessary.
  503. */
  504. if (islcp || flag_time == 0
  505. || jiffies - ap->last_xmit >= flag_time)
  506. *buf++ = PPP_FLAG;
  507. ap->last_xmit = jiffies;
  508. fcs = PPP_INITFCS;
  509. /*
  510. * Put in the address/control bytes if necessary
  511. */
  512. if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
  513. PUT_BYTE(ap, buf, 0xff, islcp);
  514. fcs = PPP_FCS(fcs, 0xff);
  515. PUT_BYTE(ap, buf, 0x03, islcp);
  516. fcs = PPP_FCS(fcs, 0x03);
  517. }
  518. }
  519. /*
  520. * Once we put in the last byte, we need to put in the FCS
  521. * and closing flag, so make sure there is at least 7 bytes
  522. * of free space in the output buffer.
  523. */
  524. buflim = ap->obuf + OBUFSIZE - 6;
  525. while (i < count && buf < buflim) {
  526. c = data[i++];
  527. if (i == 1 && c == 0 && (ap->flags & SC_COMP_PROT))
  528. continue; /* compress protocol field */
  529. fcs = PPP_FCS(fcs, c);
  530. PUT_BYTE(ap, buf, c, islcp);
  531. }
  532. if (i < count) {
  533. /*
  534. * Remember where we are up to in this packet.
  535. */
  536. ap->olim = buf;
  537. ap->tpkt_pos = i;
  538. ap->tfcs = fcs;
  539. return 0;
  540. }
  541. /*
  542. * We have finished the packet. Add the FCS and flag.
  543. */
  544. fcs = ~fcs;
  545. c = fcs & 0xff;
  546. PUT_BYTE(ap, buf, c, islcp);
  547. c = (fcs >> 8) & 0xff;
  548. PUT_BYTE(ap, buf, c, islcp);
  549. *buf++ = PPP_FLAG;
  550. ap->olim = buf;
  551. kfree_skb(ap->tpkt);
  552. ap->tpkt = NULL;
  553. return 1;
  554. }
  555. /*
  556. * Transmit-side routines.
  557. */
  558. /*
  559. * Send a packet to the peer over an async tty line.
  560. * Returns 1 iff the packet was accepted.
  561. * If the packet was not accepted, we will call ppp_output_wakeup
  562. * at some later time.
  563. */
  564. static int
  565. ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb)
  566. {
  567. struct asyncppp *ap = chan->private;
  568. ppp_async_push(ap);
  569. if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
  570. return 0; /* already full */
  571. ap->tpkt = skb;
  572. ap->tpkt_pos = 0;
  573. ppp_async_push(ap);
  574. return 1;
  575. }
  576. /*
  577. * Push as much data as possible out to the tty.
  578. */
  579. static int
  580. ppp_async_push(struct asyncppp *ap)
  581. {
  582. int avail, sent, done = 0;
  583. struct tty_struct *tty = ap->tty;
  584. int tty_stuffed = 0;
  585. /*
  586. * We can get called recursively here if the tty write
  587. * function calls our wakeup function. This can happen
  588. * for example on a pty with both the master and slave
  589. * set to PPP line discipline.
  590. * We use the XMIT_BUSY bit to detect this and get out,
  591. * leaving the XMIT_WAKEUP bit set to tell the other
  592. * instance that it may now be able to write more now.
  593. */
  594. if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
  595. return 0;
  596. spin_lock_bh(&ap->xmit_lock);
  597. for (;;) {
  598. if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
  599. tty_stuffed = 0;
  600. if (!tty_stuffed && ap->optr < ap->olim) {
  601. avail = ap->olim - ap->optr;
  602. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  603. sent = tty->driver->write(tty, ap->optr, avail);
  604. if (sent < 0)
  605. goto flush; /* error, e.g. loss of CD */
  606. ap->optr += sent;
  607. if (sent < avail)
  608. tty_stuffed = 1;
  609. continue;
  610. }
  611. if (ap->optr >= ap->olim && ap->tpkt != 0) {
  612. if (ppp_async_encode(ap)) {
  613. /* finished processing ap->tpkt */
  614. clear_bit(XMIT_FULL, &ap->xmit_flags);
  615. done = 1;
  616. }
  617. continue;
  618. }
  619. /*
  620. * We haven't made any progress this time around.
  621. * Clear XMIT_BUSY to let other callers in, but
  622. * after doing so we have to check if anyone set
  623. * XMIT_WAKEUP since we last checked it. If they
  624. * did, we should try again to set XMIT_BUSY and go
  625. * around again in case XMIT_BUSY was still set when
  626. * the other caller tried.
  627. */
  628. clear_bit(XMIT_BUSY, &ap->xmit_flags);
  629. /* any more work to do? if not, exit the loop */
  630. if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags)
  631. || (!tty_stuffed && ap->tpkt != 0)))
  632. break;
  633. /* more work to do, see if we can do it now */
  634. if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
  635. break;
  636. }
  637. spin_unlock_bh(&ap->xmit_lock);
  638. return done;
  639. flush:
  640. clear_bit(XMIT_BUSY, &ap->xmit_flags);
  641. if (ap->tpkt != 0) {
  642. kfree_skb(ap->tpkt);
  643. ap->tpkt = NULL;
  644. clear_bit(XMIT_FULL, &ap->xmit_flags);
  645. done = 1;
  646. }
  647. ap->optr = ap->olim;
  648. spin_unlock_bh(&ap->xmit_lock);
  649. return done;
  650. }
  651. /*
  652. * Flush output from our internal buffers.
  653. * Called for the TCFLSH ioctl. Can be entered in parallel
  654. * but this is covered by the xmit_lock.
  655. */
  656. static void
  657. ppp_async_flush_output(struct asyncppp *ap)
  658. {
  659. int done = 0;
  660. spin_lock_bh(&ap->xmit_lock);
  661. ap->optr = ap->olim;
  662. if (ap->tpkt != NULL) {
  663. kfree_skb(ap->tpkt);
  664. ap->tpkt = NULL;
  665. clear_bit(XMIT_FULL, &ap->xmit_flags);
  666. done = 1;
  667. }
  668. spin_unlock_bh(&ap->xmit_lock);
  669. if (done)
  670. ppp_output_wakeup(&ap->chan);
  671. }
  672. /*
  673. * Receive-side routines.
  674. */
  675. /* see how many ordinary chars there are at the start of buf */
  676. static inline int
  677. scan_ordinary(struct asyncppp *ap, const unsigned char *buf, int count)
  678. {
  679. int i, c;
  680. for (i = 0; i < count; ++i) {
  681. c = buf[i];
  682. if (c == PPP_ESCAPE || c == PPP_FLAG
  683. || (c < 0x20 && (ap->raccm & (1 << c)) != 0))
  684. break;
  685. }
  686. return i;
  687. }
  688. /* called when a flag is seen - do end-of-packet processing */
  689. static void
  690. process_input_packet(struct asyncppp *ap)
  691. {
  692. struct sk_buff *skb;
  693. unsigned char *p;
  694. unsigned int len, fcs, proto;
  695. skb = ap->rpkt;
  696. if (ap->state & (SC_TOSS | SC_ESCAPE))
  697. goto err;
  698. if (skb == NULL)
  699. return; /* 0-length packet */
  700. /* check the FCS */
  701. p = skb->data;
  702. len = skb->len;
  703. if (len < 3)
  704. goto err; /* too short */
  705. fcs = PPP_INITFCS;
  706. for (; len > 0; --len)
  707. fcs = PPP_FCS(fcs, *p++);
  708. if (fcs != PPP_GOODFCS)
  709. goto err; /* bad FCS */
  710. skb_trim(skb, skb->len - 2);
  711. /* check for address/control and protocol compression */
  712. p = skb->data;
  713. if (p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
  714. /* chop off address/control */
  715. if (skb->len < 3)
  716. goto err;
  717. p = skb_pull(skb, 2);
  718. }
  719. proto = p[0];
  720. if (proto & 1) {
  721. /* protocol is compressed */
  722. skb_push(skb, 1)[0] = 0;
  723. } else {
  724. if (skb->len < 2)
  725. goto err;
  726. proto = (proto << 8) + p[1];
  727. if (proto == PPP_LCP)
  728. async_lcp_peek(ap, p, skb->len, 1);
  729. }
  730. /* queue the frame to be processed */
  731. skb->cb[0] = ap->state;
  732. skb_queue_tail(&ap->rqueue, skb);
  733. ap->rpkt = NULL;
  734. ap->state = 0;
  735. return;
  736. err:
  737. /* frame had an error, remember that, reset SC_TOSS & SC_ESCAPE */
  738. ap->state = SC_PREV_ERROR;
  739. if (skb)
  740. skb_trim(skb, 0);
  741. }
  742. /* Called when the tty driver has data for us. Runs parallel with the
  743. other ldisc functions but will not be re-entered */
  744. static void
  745. ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
  746. char *flags, int count)
  747. {
  748. struct sk_buff *skb;
  749. int c, i, j, n, s, f;
  750. unsigned char *sp;
  751. /* update bits used for 8-bit cleanness detection */
  752. if (~ap->rbits & SC_RCV_BITS) {
  753. s = 0;
  754. for (i = 0; i < count; ++i) {
  755. c = buf[i];
  756. if (flags != 0 && flags[i] != 0)
  757. continue;
  758. s |= (c & 0x80)? SC_RCV_B7_1: SC_RCV_B7_0;
  759. c = ((c >> 4) ^ c) & 0xf;
  760. s |= (0x6996 & (1 << c))? SC_RCV_ODDP: SC_RCV_EVNP;
  761. }
  762. ap->rbits |= s;
  763. }
  764. while (count > 0) {
  765. /* scan through and see how many chars we can do in bulk */
  766. if ((ap->state & SC_ESCAPE) && buf[0] == PPP_ESCAPE)
  767. n = 1;
  768. else
  769. n = scan_ordinary(ap, buf, count);
  770. f = 0;
  771. if (flags != 0 && (ap->state & SC_TOSS) == 0) {
  772. /* check the flags to see if any char had an error */
  773. for (j = 0; j < n; ++j)
  774. if ((f = flags[j]) != 0)
  775. break;
  776. }
  777. if (f != 0) {
  778. /* start tossing */
  779. ap->state |= SC_TOSS;
  780. } else if (n > 0 && (ap->state & SC_TOSS) == 0) {
  781. /* stuff the chars in the skb */
  782. skb = ap->rpkt;
  783. if (skb == 0) {
  784. skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
  785. if (skb == 0)
  786. goto nomem;
  787. /* Try to get the payload 4-byte aligned */
  788. if (buf[0] != PPP_ALLSTATIONS)
  789. skb_reserve(skb, 2 + (buf[0] & 1));
  790. ap->rpkt = skb;
  791. }
  792. if (n > skb_tailroom(skb)) {
  793. /* packet overflowed MRU */
  794. ap->state |= SC_TOSS;
  795. } else {
  796. sp = skb_put(skb, n);
  797. memcpy(sp, buf, n);
  798. if (ap->state & SC_ESCAPE) {
  799. sp[0] ^= 0x20;
  800. ap->state &= ~SC_ESCAPE;
  801. }
  802. }
  803. }
  804. if (n >= count)
  805. break;
  806. c = buf[n];
  807. if (flags != NULL && flags[n] != 0) {
  808. ap->state |= SC_TOSS;
  809. } else if (c == PPP_FLAG) {
  810. process_input_packet(ap);
  811. } else if (c == PPP_ESCAPE) {
  812. ap->state |= SC_ESCAPE;
  813. } else if (I_IXON(ap->tty)) {
  814. if (c == START_CHAR(ap->tty))
  815. start_tty(ap->tty);
  816. else if (c == STOP_CHAR(ap->tty))
  817. stop_tty(ap->tty);
  818. }
  819. /* otherwise it's a char in the recv ACCM */
  820. ++n;
  821. buf += n;
  822. if (flags != 0)
  823. flags += n;
  824. count -= n;
  825. }
  826. return;
  827. nomem:
  828. printk(KERN_ERR "PPPasync: no memory (input pkt)\n");
  829. ap->state |= SC_TOSS;
  830. }
  831. /*
  832. * We look at LCP frames going past so that we can notice
  833. * and react to the LCP configure-ack from the peer.
  834. * In the situation where the peer has been sent a configure-ack
  835. * already, LCP is up once it has sent its configure-ack
  836. * so the immediately following packet can be sent with the
  837. * configured LCP options. This allows us to process the following
  838. * packet correctly without pppd needing to respond quickly.
  839. *
  840. * We only respond to the received configure-ack if we have just
  841. * sent a configure-request, and the configure-ack contains the
  842. * same data (this is checked using a 16-bit crc of the data).
  843. */
  844. #define CONFREQ 1 /* LCP code field values */
  845. #define CONFACK 2
  846. #define LCP_MRU 1 /* LCP option numbers */
  847. #define LCP_ASYNCMAP 2
  848. static void async_lcp_peek(struct asyncppp *ap, unsigned char *data,
  849. int len, int inbound)
  850. {
  851. int dlen, fcs, i, code;
  852. u32 val;
  853. data += 2; /* skip protocol bytes */
  854. len -= 2;
  855. if (len < 4) /* 4 = code, ID, length */
  856. return;
  857. code = data[0];
  858. if (code != CONFACK && code != CONFREQ)
  859. return;
  860. dlen = (data[2] << 8) + data[3];
  861. if (len < dlen)
  862. return; /* packet got truncated or length is bogus */
  863. if (code == (inbound? CONFACK: CONFREQ)) {
  864. /*
  865. * sent confreq or received confack:
  866. * calculate the crc of the data from the ID field on.
  867. */
  868. fcs = PPP_INITFCS;
  869. for (i = 1; i < dlen; ++i)
  870. fcs = PPP_FCS(fcs, data[i]);
  871. if (!inbound) {
  872. /* outbound confreq - remember the crc for later */
  873. ap->lcp_fcs = fcs;
  874. return;
  875. }
  876. /* received confack, check the crc */
  877. fcs ^= ap->lcp_fcs;
  878. ap->lcp_fcs = -1;
  879. if (fcs != 0)
  880. return;
  881. } else if (inbound)
  882. return; /* not interested in received confreq */
  883. /* process the options in the confack */
  884. data += 4;
  885. dlen -= 4;
  886. /* data[0] is code, data[1] is length */
  887. while (dlen >= 2 && dlen >= data[1] && data[1] >= 2) {
  888. switch (data[0]) {
  889. case LCP_MRU:
  890. val = (data[2] << 8) + data[3];
  891. if (inbound)
  892. ap->mru = val;
  893. else
  894. ap->chan.mtu = val;
  895. break;
  896. case LCP_ASYNCMAP:
  897. val = (data[2] << 24) + (data[3] << 16)
  898. + (data[4] << 8) + data[5];
  899. if (inbound)
  900. ap->raccm = val;
  901. else
  902. ap->xaccm[0] = val;
  903. break;
  904. }
  905. dlen -= data[1];
  906. data += data[1];
  907. }
  908. }
  909. static void __exit ppp_async_cleanup(void)
  910. {
  911. if (tty_unregister_ldisc(N_PPP) != 0)
  912. printk(KERN_ERR "failed to unregister PPP line discipline\n");
  913. }
  914. module_init(ppp_async_init);
  915. module_exit(ppp_async_cleanup);