ppp_async.c 24 KB

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