ppp_async.c 24 KB

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