ppp_async.c 24 KB

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