ppp_async.c 24 KB

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