ppp_async.c 24 KB

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