ppp_async.c 23 KB

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