ppp_async.c 24 KB

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