ppp_async.c 24 KB

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