ppp_async.c 23 KB

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