ppp_async.c 24 KB

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