ppp_async.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. 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(unsigned long arg);
  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_init(&ap->tsk, ppp_async_process, (unsigned long) ap);
  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 int ppp_asynctty_hangup(struct tty_struct *tty)
  217. {
  218. ppp_asynctty_close(tty);
  219. return 0;
  220. }
  221. /*
  222. * Read does nothing - no data is ever available this way.
  223. * Pppd reads and writes packets via /dev/ppp instead.
  224. */
  225. static ssize_t
  226. ppp_asynctty_read(struct tty_struct *tty, struct file *file,
  227. unsigned char __user *buf, size_t count)
  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, struct file *file,
  247. unsigned int cmd, unsigned long arg)
  248. {
  249. struct asyncppp *ap = ap_get(tty);
  250. int err, val;
  251. int __user *p = (int __user *)arg;
  252. if (!ap)
  253. return -ENXIO;
  254. err = -EFAULT;
  255. switch (cmd) {
  256. case PPPIOCGCHAN:
  257. err = -EFAULT;
  258. if (put_user(ppp_channel_index(&ap->chan), p))
  259. break;
  260. err = 0;
  261. break;
  262. case PPPIOCGUNIT:
  263. err = -EFAULT;
  264. if (put_user(ppp_unit_number(&ap->chan), p))
  265. break;
  266. err = 0;
  267. break;
  268. case TCFLSH:
  269. /* flush our buffers and the serial port's buffer */
  270. if (arg == TCIOFLUSH || arg == TCOFLUSH)
  271. ppp_async_flush_output(ap);
  272. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  273. break;
  274. case FIONREAD:
  275. val = 0;
  276. if (put_user(val, p))
  277. break;
  278. err = 0;
  279. break;
  280. default:
  281. /* Try the various mode ioctls */
  282. err = tty_mode_ioctl(tty, file, cmd, arg);
  283. }
  284. ap_put(ap);
  285. return err;
  286. }
  287. /* No kernel lock - fine */
  288. static __poll_t
  289. ppp_asynctty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
  290. {
  291. return 0;
  292. }
  293. /* May sleep, don't call from interrupt level or with interrupts disabled */
  294. static void
  295. ppp_asynctty_receive(struct tty_struct *tty, const unsigned char *buf,
  296. char *cflags, int count)
  297. {
  298. struct asyncppp *ap = ap_get(tty);
  299. unsigned long flags;
  300. if (!ap)
  301. return;
  302. spin_lock_irqsave(&ap->recv_lock, flags);
  303. ppp_async_input(ap, buf, cflags, count);
  304. spin_unlock_irqrestore(&ap->recv_lock, flags);
  305. if (!skb_queue_empty(&ap->rqueue))
  306. tasklet_schedule(&ap->tsk);
  307. ap_put(ap);
  308. tty_unthrottle(tty);
  309. }
  310. static void
  311. ppp_asynctty_wakeup(struct tty_struct *tty)
  312. {
  313. struct asyncppp *ap = ap_get(tty);
  314. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  315. if (!ap)
  316. return;
  317. set_bit(XMIT_WAKEUP, &ap->xmit_flags);
  318. tasklet_schedule(&ap->tsk);
  319. ap_put(ap);
  320. }
  321. static struct tty_ldisc_ops ppp_ldisc = {
  322. .owner = THIS_MODULE,
  323. .magic = TTY_LDISC_MAGIC,
  324. .name = "ppp",
  325. .open = ppp_asynctty_open,
  326. .close = ppp_asynctty_close,
  327. .hangup = ppp_asynctty_hangup,
  328. .read = ppp_asynctty_read,
  329. .write = ppp_asynctty_write,
  330. .ioctl = ppp_asynctty_ioctl,
  331. .poll = ppp_asynctty_poll,
  332. .receive_buf = ppp_asynctty_receive,
  333. .write_wakeup = ppp_asynctty_wakeup,
  334. };
  335. static int __init
  336. ppp_async_init(void)
  337. {
  338. int err;
  339. err = tty_register_ldisc(N_PPP, &ppp_ldisc);
  340. if (err != 0)
  341. printk(KERN_ERR "PPP_async: error %d registering line disc.\n",
  342. err);
  343. return err;
  344. }
  345. /*
  346. * The following routines provide the PPP channel interface.
  347. */
  348. static int
  349. ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
  350. {
  351. struct asyncppp *ap = chan->private;
  352. void __user *argp = (void __user *)arg;
  353. int __user *p = argp;
  354. int err, val;
  355. u32 accm[8];
  356. err = -EFAULT;
  357. switch (cmd) {
  358. case PPPIOCGFLAGS:
  359. val = ap->flags | ap->rbits;
  360. if (put_user(val, p))
  361. break;
  362. err = 0;
  363. break;
  364. case PPPIOCSFLAGS:
  365. if (get_user(val, p))
  366. break;
  367. ap->flags = val & ~SC_RCV_BITS;
  368. spin_lock_irq(&ap->recv_lock);
  369. ap->rbits = val & SC_RCV_BITS;
  370. spin_unlock_irq(&ap->recv_lock);
  371. err = 0;
  372. break;
  373. case PPPIOCGASYNCMAP:
  374. if (put_user(ap->xaccm[0], (u32 __user *)argp))
  375. break;
  376. err = 0;
  377. break;
  378. case PPPIOCSASYNCMAP:
  379. if (get_user(ap->xaccm[0], (u32 __user *)argp))
  380. break;
  381. err = 0;
  382. break;
  383. case PPPIOCGRASYNCMAP:
  384. if (put_user(ap->raccm, (u32 __user *)argp))
  385. break;
  386. err = 0;
  387. break;
  388. case PPPIOCSRASYNCMAP:
  389. if (get_user(ap->raccm, (u32 __user *)argp))
  390. break;
  391. err = 0;
  392. break;
  393. case PPPIOCGXASYNCMAP:
  394. if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  395. break;
  396. err = 0;
  397. break;
  398. case PPPIOCSXASYNCMAP:
  399. if (copy_from_user(accm, argp, sizeof(accm)))
  400. break;
  401. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  402. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  403. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  404. err = 0;
  405. break;
  406. case PPPIOCGMRU:
  407. if (put_user(ap->mru, p))
  408. break;
  409. err = 0;
  410. break;
  411. case PPPIOCSMRU:
  412. if (get_user(val, p))
  413. break;
  414. if (val < PPP_MRU)
  415. val = PPP_MRU;
  416. ap->mru = val;
  417. err = 0;
  418. break;
  419. default:
  420. err = -ENOTTY;
  421. }
  422. return err;
  423. }
  424. /*
  425. * This is called at softirq level to deliver received packets
  426. * to the ppp_generic code, and to tell the ppp_generic code
  427. * if we can accept more output now.
  428. */
  429. static void ppp_async_process(unsigned long arg)
  430. {
  431. struct asyncppp *ap = (struct asyncppp *) arg;
  432. struct sk_buff *skb;
  433. /* process received packets */
  434. while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
  435. if (skb->cb[0])
  436. ppp_input_error(&ap->chan, 0);
  437. ppp_input(&ap->chan, skb);
  438. }
  439. /* try to push more stuff out */
  440. if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_async_push(ap))
  441. ppp_output_wakeup(&ap->chan);
  442. }
  443. /*
  444. * Procedures for encapsulation and framing.
  445. */
  446. /*
  447. * Procedure to encode the data for async serial transmission.
  448. * Does octet stuffing (escaping), puts the address/control bytes
  449. * on if A/C compression is disabled, and does protocol compression.
  450. * Assumes ap->tpkt != 0 on entry.
  451. * Returns 1 if we finished the current frame, 0 otherwise.
  452. */
  453. #define PUT_BYTE(ap, buf, c, islcp) do { \
  454. if ((islcp && c < 0x20) || (ap->xaccm[c >> 5] & (1 << (c & 0x1f)))) {\
  455. *buf++ = PPP_ESCAPE; \
  456. *buf++ = c ^ PPP_TRANS; \
  457. } else \
  458. *buf++ = c; \
  459. } while (0)
  460. static int
  461. ppp_async_encode(struct asyncppp *ap)
  462. {
  463. int fcs, i, count, c, proto;
  464. unsigned char *buf, *buflim;
  465. unsigned char *data;
  466. int islcp;
  467. buf = ap->obuf;
  468. ap->olim = buf;
  469. ap->optr = buf;
  470. i = ap->tpkt_pos;
  471. data = ap->tpkt->data;
  472. count = ap->tpkt->len;
  473. fcs = ap->tfcs;
  474. proto = get_unaligned_be16(data);
  475. /*
  476. * LCP packets with code values between 1 (configure-reqest)
  477. * and 7 (code-reject) must be sent as though no options
  478. * had been negotiated.
  479. */
  480. islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
  481. if (i == 0) {
  482. if (islcp)
  483. async_lcp_peek(ap, data, count, 0);
  484. /*
  485. * Start of a new packet - insert the leading FLAG
  486. * character if necessary.
  487. */
  488. if (islcp || flag_time == 0 ||
  489. time_after_eq(jiffies, ap->last_xmit + flag_time))
  490. *buf++ = PPP_FLAG;
  491. ap->last_xmit = jiffies;
  492. fcs = PPP_INITFCS;
  493. /*
  494. * Put in the address/control bytes if necessary
  495. */
  496. if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
  497. PUT_BYTE(ap, buf, 0xff, islcp);
  498. fcs = PPP_FCS(fcs, 0xff);
  499. PUT_BYTE(ap, buf, 0x03, islcp);
  500. fcs = PPP_FCS(fcs, 0x03);
  501. }
  502. }
  503. /*
  504. * Once we put in the last byte, we need to put in the FCS
  505. * and closing flag, so make sure there is at least 7 bytes
  506. * of free space in the output buffer.
  507. */
  508. buflim = ap->obuf + OBUFSIZE - 6;
  509. while (i < count && buf < buflim) {
  510. c = data[i++];
  511. if (i == 1 && c == 0 && (ap->flags & SC_COMP_PROT))
  512. continue; /* compress protocol field */
  513. fcs = PPP_FCS(fcs, c);
  514. PUT_BYTE(ap, buf, c, islcp);
  515. }
  516. if (i < count) {
  517. /*
  518. * Remember where we are up to in this packet.
  519. */
  520. ap->olim = buf;
  521. ap->tpkt_pos = i;
  522. ap->tfcs = fcs;
  523. return 0;
  524. }
  525. /*
  526. * We have finished the packet. Add the FCS and flag.
  527. */
  528. fcs = ~fcs;
  529. c = fcs & 0xff;
  530. PUT_BYTE(ap, buf, c, islcp);
  531. c = (fcs >> 8) & 0xff;
  532. PUT_BYTE(ap, buf, c, islcp);
  533. *buf++ = PPP_FLAG;
  534. ap->olim = buf;
  535. consume_skb(ap->tpkt);
  536. ap->tpkt = NULL;
  537. return 1;
  538. }
  539. /*
  540. * Transmit-side routines.
  541. */
  542. /*
  543. * Send a packet to the peer over an async tty line.
  544. * Returns 1 iff the packet was accepted.
  545. * If the packet was not accepted, we will call ppp_output_wakeup
  546. * at some later time.
  547. */
  548. static int
  549. ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb)
  550. {
  551. struct asyncppp *ap = chan->private;
  552. ppp_async_push(ap);
  553. if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
  554. return 0; /* already full */
  555. ap->tpkt = skb;
  556. ap->tpkt_pos = 0;
  557. ppp_async_push(ap);
  558. return 1;
  559. }
  560. /*
  561. * Push as much data as possible out to the tty.
  562. */
  563. static int
  564. ppp_async_push(struct asyncppp *ap)
  565. {
  566. int avail, sent, done = 0;
  567. struct tty_struct *tty = ap->tty;
  568. int tty_stuffed = 0;
  569. /*
  570. * We can get called recursively here if the tty write
  571. * function calls our wakeup function. This can happen
  572. * for example on a pty with both the master and slave
  573. * set to PPP line discipline.
  574. * We use the XMIT_BUSY bit to detect this and get out,
  575. * leaving the XMIT_WAKEUP bit set to tell the other
  576. * instance that it may now be able to write more now.
  577. */
  578. if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
  579. return 0;
  580. spin_lock_bh(&ap->xmit_lock);
  581. for (;;) {
  582. if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
  583. tty_stuffed = 0;
  584. if (!tty_stuffed && ap->optr < ap->olim) {
  585. avail = ap->olim - ap->optr;
  586. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  587. sent = tty->ops->write(tty, ap->optr, avail);
  588. if (sent < 0)
  589. goto flush; /* error, e.g. loss of CD */
  590. ap->optr += sent;
  591. if (sent < avail)
  592. tty_stuffed = 1;
  593. continue;
  594. }
  595. if (ap->optr >= ap->olim && ap->tpkt) {
  596. if (ppp_async_encode(ap)) {
  597. /* finished processing ap->tpkt */
  598. clear_bit(XMIT_FULL, &ap->xmit_flags);
  599. done = 1;
  600. }
  601. continue;
  602. }
  603. /*
  604. * We haven't made any progress this time around.
  605. * Clear XMIT_BUSY to let other callers in, but
  606. * after doing so we have to check if anyone set
  607. * XMIT_WAKEUP since we last checked it. If they
  608. * did, we should try again to set XMIT_BUSY and go
  609. * around again in case XMIT_BUSY was still set when
  610. * the other caller tried.
  611. */
  612. clear_bit(XMIT_BUSY, &ap->xmit_flags);
  613. /* any more work to do? if not, exit the loop */
  614. if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags) ||
  615. (!tty_stuffed && ap->tpkt)))
  616. break;
  617. /* more work to do, see if we can do it now */
  618. if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
  619. break;
  620. }
  621. spin_unlock_bh(&ap->xmit_lock);
  622. return done;
  623. flush:
  624. clear_bit(XMIT_BUSY, &ap->xmit_flags);
  625. if (ap->tpkt) {
  626. kfree_skb(ap->tpkt);
  627. ap->tpkt = NULL;
  628. clear_bit(XMIT_FULL, &ap->xmit_flags);
  629. done = 1;
  630. }
  631. ap->optr = ap->olim;
  632. spin_unlock_bh(&ap->xmit_lock);
  633. return done;
  634. }
  635. /*
  636. * Flush output from our internal buffers.
  637. * Called for the TCFLSH ioctl. Can be entered in parallel
  638. * but this is covered by the xmit_lock.
  639. */
  640. static void
  641. ppp_async_flush_output(struct asyncppp *ap)
  642. {
  643. int done = 0;
  644. spin_lock_bh(&ap->xmit_lock);
  645. ap->optr = ap->olim;
  646. if (ap->tpkt != NULL) {
  647. kfree_skb(ap->tpkt);
  648. ap->tpkt = NULL;
  649. clear_bit(XMIT_FULL, &ap->xmit_flags);
  650. done = 1;
  651. }
  652. spin_unlock_bh(&ap->xmit_lock);
  653. if (done)
  654. ppp_output_wakeup(&ap->chan);
  655. }
  656. /*
  657. * Receive-side routines.
  658. */
  659. /* see how many ordinary chars there are at the start of buf */
  660. static inline int
  661. scan_ordinary(struct asyncppp *ap, const unsigned char *buf, int count)
  662. {
  663. int i, c;
  664. for (i = 0; i < count; ++i) {
  665. c = buf[i];
  666. if (c == PPP_ESCAPE || c == PPP_FLAG ||
  667. (c < 0x20 && (ap->raccm & (1 << c)) != 0))
  668. break;
  669. }
  670. return i;
  671. }
  672. /* called when a flag is seen - do end-of-packet processing */
  673. static void
  674. process_input_packet(struct asyncppp *ap)
  675. {
  676. struct sk_buff *skb;
  677. unsigned char *p;
  678. unsigned int len, fcs;
  679. skb = ap->rpkt;
  680. if (ap->state & (SC_TOSS | SC_ESCAPE))
  681. goto err;
  682. if (skb == NULL)
  683. return; /* 0-length packet */
  684. /* check the FCS */
  685. p = skb->data;
  686. len = skb->len;
  687. if (len < 3)
  688. goto err; /* too short */
  689. fcs = PPP_INITFCS;
  690. for (; len > 0; --len)
  691. fcs = PPP_FCS(fcs, *p++);
  692. if (fcs != PPP_GOODFCS)
  693. goto err; /* bad FCS */
  694. skb_trim(skb, skb->len - 2);
  695. /* check for address/control and protocol compression */
  696. p = skb->data;
  697. if (p[0] == PPP_ALLSTATIONS) {
  698. /* chop off address/control */
  699. if (p[1] != PPP_UI || skb->len < 3)
  700. goto err;
  701. p = skb_pull(skb, 2);
  702. }
  703. /* If protocol field is not compressed, it can be LCP packet */
  704. if (!(p[0] & 0x01)) {
  705. unsigned int proto;
  706. if (skb->len < 2)
  707. goto err;
  708. proto = (p[0] << 8) + p[1];
  709. if (proto == PPP_LCP)
  710. async_lcp_peek(ap, p, skb->len, 1);
  711. }
  712. /* queue the frame to be processed */
  713. skb->cb[0] = ap->state;
  714. skb_queue_tail(&ap->rqueue, skb);
  715. ap->rpkt = NULL;
  716. ap->state = 0;
  717. return;
  718. err:
  719. /* frame had an error, remember that, reset SC_TOSS & SC_ESCAPE */
  720. ap->state = SC_PREV_ERROR;
  721. if (skb) {
  722. /* make skb appear as freshly allocated */
  723. skb_trim(skb, 0);
  724. skb_reserve(skb, - skb_headroom(skb));
  725. }
  726. }
  727. /* Called when the tty driver has data for us. Runs parallel with the
  728. other ldisc functions but will not be re-entered */
  729. static void
  730. ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
  731. char *flags, int count)
  732. {
  733. struct sk_buff *skb;
  734. int c, i, j, n, s, f;
  735. unsigned char *sp;
  736. /* update bits used for 8-bit cleanness detection */
  737. if (~ap->rbits & SC_RCV_BITS) {
  738. s = 0;
  739. for (i = 0; i < count; ++i) {
  740. c = buf[i];
  741. if (flags && flags[i] != 0)
  742. continue;
  743. s |= (c & 0x80)? SC_RCV_B7_1: SC_RCV_B7_0;
  744. c = ((c >> 4) ^ c) & 0xf;
  745. s |= (0x6996 & (1 << c))? SC_RCV_ODDP: SC_RCV_EVNP;
  746. }
  747. ap->rbits |= s;
  748. }
  749. while (count > 0) {
  750. /* scan through and see how many chars we can do in bulk */
  751. if ((ap->state & SC_ESCAPE) && buf[0] == PPP_ESCAPE)
  752. n = 1;
  753. else
  754. n = scan_ordinary(ap, buf, count);
  755. f = 0;
  756. if (flags && (ap->state & SC_TOSS) == 0) {
  757. /* check the flags to see if any char had an error */
  758. for (j = 0; j < n; ++j)
  759. if ((f = flags[j]) != 0)
  760. break;
  761. }
  762. if (f != 0) {
  763. /* start tossing */
  764. ap->state |= SC_TOSS;
  765. } else if (n > 0 && (ap->state & SC_TOSS) == 0) {
  766. /* stuff the chars in the skb */
  767. skb = ap->rpkt;
  768. if (!skb) {
  769. skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
  770. if (!skb)
  771. goto nomem;
  772. ap->rpkt = skb;
  773. }
  774. if (skb->len == 0) {
  775. /* Try to get the payload 4-byte aligned.
  776. * This should match the
  777. * PPP_ALLSTATIONS/PPP_UI/compressed tests in
  778. * process_input_packet, but we do not have
  779. * enough chars here to test buf[1] and buf[2].
  780. */
  781. if (buf[0] != PPP_ALLSTATIONS)
  782. skb_reserve(skb, 2 + (buf[0] & 1));
  783. }
  784. if (n > skb_tailroom(skb)) {
  785. /* packet overflowed MRU */
  786. ap->state |= SC_TOSS;
  787. } else {
  788. sp = skb_put_data(skb, buf, n);
  789. if (ap->state & SC_ESCAPE) {
  790. sp[0] ^= PPP_TRANS;
  791. ap->state &= ~SC_ESCAPE;
  792. }
  793. }
  794. }
  795. if (n >= count)
  796. break;
  797. c = buf[n];
  798. if (flags != NULL && flags[n] != 0) {
  799. ap->state |= SC_TOSS;
  800. } else if (c == PPP_FLAG) {
  801. process_input_packet(ap);
  802. } else if (c == PPP_ESCAPE) {
  803. ap->state |= SC_ESCAPE;
  804. } else if (I_IXON(ap->tty)) {
  805. if (c == START_CHAR(ap->tty))
  806. start_tty(ap->tty);
  807. else if (c == STOP_CHAR(ap->tty))
  808. stop_tty(ap->tty);
  809. }
  810. /* otherwise it's a char in the recv ACCM */
  811. ++n;
  812. buf += n;
  813. if (flags)
  814. flags += n;
  815. count -= n;
  816. }
  817. return;
  818. nomem:
  819. printk(KERN_ERR "PPPasync: no memory (input pkt)\n");
  820. ap->state |= SC_TOSS;
  821. }
  822. /*
  823. * We look at LCP frames going past so that we can notice
  824. * and react to the LCP configure-ack from the peer.
  825. * In the situation where the peer has been sent a configure-ack
  826. * already, LCP is up once it has sent its configure-ack
  827. * so the immediately following packet can be sent with the
  828. * configured LCP options. This allows us to process the following
  829. * packet correctly without pppd needing to respond quickly.
  830. *
  831. * We only respond to the received configure-ack if we have just
  832. * sent a configure-request, and the configure-ack contains the
  833. * same data (this is checked using a 16-bit crc of the data).
  834. */
  835. #define CONFREQ 1 /* LCP code field values */
  836. #define CONFACK 2
  837. #define LCP_MRU 1 /* LCP option numbers */
  838. #define LCP_ASYNCMAP 2
  839. static void async_lcp_peek(struct asyncppp *ap, unsigned char *data,
  840. int len, int inbound)
  841. {
  842. int dlen, fcs, i, code;
  843. u32 val;
  844. data += 2; /* skip protocol bytes */
  845. len -= 2;
  846. if (len < 4) /* 4 = code, ID, length */
  847. return;
  848. code = data[0];
  849. if (code != CONFACK && code != CONFREQ)
  850. return;
  851. dlen = get_unaligned_be16(data + 2);
  852. if (len < dlen)
  853. return; /* packet got truncated or length is bogus */
  854. if (code == (inbound? CONFACK: CONFREQ)) {
  855. /*
  856. * sent confreq or received confack:
  857. * calculate the crc of the data from the ID field on.
  858. */
  859. fcs = PPP_INITFCS;
  860. for (i = 1; i < dlen; ++i)
  861. fcs = PPP_FCS(fcs, data[i]);
  862. if (!inbound) {
  863. /* outbound confreq - remember the crc for later */
  864. ap->lcp_fcs = fcs;
  865. return;
  866. }
  867. /* received confack, check the crc */
  868. fcs ^= ap->lcp_fcs;
  869. ap->lcp_fcs = -1;
  870. if (fcs != 0)
  871. return;
  872. } else if (inbound)
  873. return; /* not interested in received confreq */
  874. /* process the options in the confack */
  875. data += 4;
  876. dlen -= 4;
  877. /* data[0] is code, data[1] is length */
  878. while (dlen >= 2 && dlen >= data[1] && data[1] >= 2) {
  879. switch (data[0]) {
  880. case LCP_MRU:
  881. val = get_unaligned_be16(data + 2);
  882. if (inbound)
  883. ap->mru = val;
  884. else
  885. ap->chan.mtu = val;
  886. break;
  887. case LCP_ASYNCMAP:
  888. val = get_unaligned_be32(data + 2);
  889. if (inbound)
  890. ap->raccm = val;
  891. else
  892. ap->xaccm[0] = val;
  893. break;
  894. }
  895. dlen -= data[1];
  896. data += data[1];
  897. }
  898. }
  899. static void __exit ppp_async_cleanup(void)
  900. {
  901. if (tty_unregister_ldisc(N_PPP) != 0)
  902. printk(KERN_ERR "failed to unregister PPP line discipline\n");
  903. }
  904. module_init(ppp_async_init);
  905. module_exit(ppp_async_cleanup);