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