ppp_synctty.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * PPP synchronous tty channel driver for Linux.
  3. *
  4. * This is a ppp channel driver that can be used with tty device drivers
  5. * that are frame oriented, such as synchronous HDLC devices.
  6. *
  7. * Complete PPP frames without encoding/decoding are exchanged between
  8. * the channel driver and the device driver.
  9. *
  10. * The async map IOCTL codes are implemented to keep the user mode
  11. * applications happy if they call them. Synchronous PPP does not use
  12. * the async maps.
  13. *
  14. * Copyright 1999 Paul Mackerras.
  15. *
  16. * Also touched by the grubby hands of Paul Fulghum paulkf@microgate.com
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. * This driver provides the encapsulation and framing for sending
  24. * and receiving PPP frames over sync serial lines. It relies on
  25. * the generic PPP layer to give it frames to send and to process
  26. * received frames. It implements the PPP line discipline.
  27. *
  28. * Part of the code in this driver was inspired by the old async-only
  29. * PPP driver, written by Michael Callahan and Al Longyear, and
  30. * subsequently hacked by Paul Mackerras.
  31. *
  32. * ==FILEVERSION 20040616==
  33. */
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/tty.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/poll.h>
  40. #include <linux/ppp_defs.h>
  41. #include <linux/ppp-ioctl.h>
  42. #include <linux/ppp_channel.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/completion.h>
  45. #include <linux/init.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/slab.h>
  48. #include <asm/unaligned.h>
  49. #include <asm/uaccess.h>
  50. #define PPP_VERSION "2.4.2"
  51. /* Structure for storing local state. */
  52. struct syncppp {
  53. struct tty_struct *tty;
  54. unsigned int flags;
  55. unsigned int rbits;
  56. int mru;
  57. spinlock_t xmit_lock;
  58. spinlock_t recv_lock;
  59. unsigned long xmit_flags;
  60. u32 xaccm[8];
  61. u32 raccm;
  62. unsigned int bytes_sent;
  63. unsigned int bytes_rcvd;
  64. struct sk_buff *tpkt;
  65. unsigned long last_xmit;
  66. struct sk_buff_head rqueue;
  67. struct tasklet_struct tsk;
  68. atomic_t refcnt;
  69. struct completion dead_cmp;
  70. struct ppp_channel chan; /* interface to generic ppp layer */
  71. };
  72. /* Bit numbers in xmit_flags */
  73. #define XMIT_WAKEUP 0
  74. #define XMIT_FULL 1
  75. /* Bits in rbits */
  76. #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
  77. #define PPPSYNC_MAX_RQLEN 32 /* arbitrary */
  78. /*
  79. * Prototypes.
  80. */
  81. static struct sk_buff* ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *);
  82. static int ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb);
  83. static int ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd,
  84. unsigned long arg);
  85. static void ppp_sync_process(unsigned long arg);
  86. static int ppp_sync_push(struct syncppp *ap);
  87. static void ppp_sync_flush_output(struct syncppp *ap);
  88. static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
  89. char *flags, int count);
  90. static const struct ppp_channel_ops sync_ops = {
  91. .start_xmit = ppp_sync_send,
  92. .ioctl = ppp_sync_ioctl,
  93. };
  94. /*
  95. * Utility procedures to print a buffer in hex/ascii
  96. */
  97. static void
  98. ppp_print_hex (register __u8 * out, const __u8 * in, int count)
  99. {
  100. register __u8 next_ch;
  101. static const char hex[] = "0123456789ABCDEF";
  102. while (count-- > 0) {
  103. next_ch = *in++;
  104. *out++ = hex[(next_ch >> 4) & 0x0F];
  105. *out++ = hex[next_ch & 0x0F];
  106. ++out;
  107. }
  108. }
  109. static void
  110. ppp_print_char (register __u8 * out, const __u8 * in, int count)
  111. {
  112. register __u8 next_ch;
  113. while (count-- > 0) {
  114. next_ch = *in++;
  115. if (next_ch < 0x20 || next_ch > 0x7e)
  116. *out++ = '.';
  117. else {
  118. *out++ = next_ch;
  119. if (next_ch == '%') /* printk/syslogd has a bug !! */
  120. *out++ = '%';
  121. }
  122. }
  123. *out = '\0';
  124. }
  125. static void
  126. ppp_print_buffer (const char *name, const __u8 *buf, int count)
  127. {
  128. __u8 line[44];
  129. if (name != NULL)
  130. printk(KERN_DEBUG "ppp_synctty: %s, count = %d\n", name, count);
  131. while (count > 8) {
  132. memset (line, 32, 44);
  133. ppp_print_hex (line, buf, 8);
  134. ppp_print_char (&line[8 * 3], buf, 8);
  135. printk(KERN_DEBUG "%s\n", line);
  136. count -= 8;
  137. buf += 8;
  138. }
  139. if (count > 0) {
  140. memset (line, 32, 44);
  141. ppp_print_hex (line, buf, count);
  142. ppp_print_char (&line[8 * 3], buf, count);
  143. printk(KERN_DEBUG "%s\n", line);
  144. }
  145. }
  146. /*
  147. * Routines implementing the synchronous PPP line discipline.
  148. */
  149. /*
  150. * We have a potential race on dereferencing tty->disc_data,
  151. * because the tty layer provides no locking at all - thus one
  152. * cpu could be running ppp_synctty_receive while another
  153. * calls ppp_synctty_close, which zeroes tty->disc_data and
  154. * frees the memory that ppp_synctty_receive is using. The best
  155. * way to fix this is to use a rwlock in the tty struct, but for now
  156. * we use a single global rwlock for all ttys in ppp line discipline.
  157. *
  158. * FIXME: Fixed in tty_io nowadays.
  159. */
  160. static DEFINE_RWLOCK(disc_data_lock);
  161. static struct syncppp *sp_get(struct tty_struct *tty)
  162. {
  163. struct syncppp *ap;
  164. read_lock(&disc_data_lock);
  165. ap = tty->disc_data;
  166. if (ap != NULL)
  167. atomic_inc(&ap->refcnt);
  168. read_unlock(&disc_data_lock);
  169. return ap;
  170. }
  171. static void sp_put(struct syncppp *ap)
  172. {
  173. if (atomic_dec_and_test(&ap->refcnt))
  174. complete(&ap->dead_cmp);
  175. }
  176. /*
  177. * Called when a tty is put into sync-PPP line discipline.
  178. */
  179. static int
  180. ppp_sync_open(struct tty_struct *tty)
  181. {
  182. struct syncppp *ap;
  183. int err;
  184. int speed;
  185. if (tty->ops->write == NULL)
  186. return -EOPNOTSUPP;
  187. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  188. err = -ENOMEM;
  189. if (!ap)
  190. goto out;
  191. /* initialize the syncppp structure */
  192. ap->tty = tty;
  193. ap->mru = PPP_MRU;
  194. spin_lock_init(&ap->xmit_lock);
  195. spin_lock_init(&ap->recv_lock);
  196. ap->xaccm[0] = ~0U;
  197. ap->xaccm[3] = 0x60000000U;
  198. ap->raccm = ~0U;
  199. skb_queue_head_init(&ap->rqueue);
  200. tasklet_init(&ap->tsk, ppp_sync_process, (unsigned long) ap);
  201. atomic_set(&ap->refcnt, 1);
  202. init_completion(&ap->dead_cmp);
  203. ap->chan.private = ap;
  204. ap->chan.ops = &sync_ops;
  205. ap->chan.mtu = PPP_MRU;
  206. ap->chan.hdrlen = 2; /* for A/C bytes */
  207. speed = tty_get_baud_rate(tty);
  208. ap->chan.speed = speed;
  209. err = ppp_register_channel(&ap->chan);
  210. if (err)
  211. goto out_free;
  212. tty->disc_data = ap;
  213. tty->receive_room = 65536;
  214. return 0;
  215. out_free:
  216. kfree(ap);
  217. out:
  218. return err;
  219. }
  220. /*
  221. * Called when the tty is put into another line discipline
  222. * or it hangs up. We have to wait for any cpu currently
  223. * executing in any of the other ppp_synctty_* routines to
  224. * finish before we can call ppp_unregister_channel and free
  225. * the syncppp struct. This routine must be called from
  226. * process context, not interrupt or softirq context.
  227. */
  228. static void
  229. ppp_sync_close(struct tty_struct *tty)
  230. {
  231. struct syncppp *ap;
  232. write_lock_irq(&disc_data_lock);
  233. ap = tty->disc_data;
  234. tty->disc_data = NULL;
  235. write_unlock_irq(&disc_data_lock);
  236. if (!ap)
  237. return;
  238. /*
  239. * We have now ensured that nobody can start using ap from now
  240. * on, but we have to wait for all existing users to finish.
  241. * Note that ppp_unregister_channel ensures that no calls to
  242. * our channel ops (i.e. ppp_sync_send/ioctl) are in progress
  243. * by the time it returns.
  244. */
  245. if (!atomic_dec_and_test(&ap->refcnt))
  246. wait_for_completion(&ap->dead_cmp);
  247. tasklet_kill(&ap->tsk);
  248. ppp_unregister_channel(&ap->chan);
  249. skb_queue_purge(&ap->rqueue);
  250. kfree_skb(ap->tpkt);
  251. kfree(ap);
  252. }
  253. /*
  254. * Called on tty hangup in process context.
  255. *
  256. * Wait for I/O to driver to complete and unregister PPP channel.
  257. * This is already done by the close routine, so just call that.
  258. */
  259. static int ppp_sync_hangup(struct tty_struct *tty)
  260. {
  261. ppp_sync_close(tty);
  262. return 0;
  263. }
  264. /*
  265. * Read does nothing - no data is ever available this way.
  266. * Pppd reads and writes packets via /dev/ppp instead.
  267. */
  268. static ssize_t
  269. ppp_sync_read(struct tty_struct *tty, struct file *file,
  270. unsigned char __user *buf, size_t count)
  271. {
  272. return -EAGAIN;
  273. }
  274. /*
  275. * Write on the tty does nothing, the packets all come in
  276. * from the ppp generic stuff.
  277. */
  278. static ssize_t
  279. ppp_sync_write(struct tty_struct *tty, struct file *file,
  280. const unsigned char *buf, size_t count)
  281. {
  282. return -EAGAIN;
  283. }
  284. static int
  285. ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
  286. unsigned int cmd, unsigned long arg)
  287. {
  288. struct syncppp *ap = sp_get(tty);
  289. int __user *p = (int __user *)arg;
  290. int err, val;
  291. if (!ap)
  292. return -ENXIO;
  293. err = -EFAULT;
  294. switch (cmd) {
  295. case PPPIOCGCHAN:
  296. err = -EFAULT;
  297. if (put_user(ppp_channel_index(&ap->chan), p))
  298. break;
  299. err = 0;
  300. break;
  301. case PPPIOCGUNIT:
  302. err = -EFAULT;
  303. if (put_user(ppp_unit_number(&ap->chan), p))
  304. break;
  305. err = 0;
  306. break;
  307. case TCFLSH:
  308. /* flush our buffers and the serial port's buffer */
  309. if (arg == TCIOFLUSH || arg == TCOFLUSH)
  310. ppp_sync_flush_output(ap);
  311. err = tty_perform_flush(tty, arg);
  312. break;
  313. case FIONREAD:
  314. val = 0;
  315. if (put_user(val, p))
  316. break;
  317. err = 0;
  318. break;
  319. default:
  320. err = tty_mode_ioctl(tty, file, cmd, arg);
  321. break;
  322. }
  323. sp_put(ap);
  324. return err;
  325. }
  326. /* No kernel lock - fine */
  327. static unsigned int
  328. ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
  329. {
  330. return 0;
  331. }
  332. /* May sleep, don't call from interrupt level or with interrupts disabled */
  333. static void
  334. ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
  335. char *cflags, int count)
  336. {
  337. struct syncppp *ap = sp_get(tty);
  338. unsigned long flags;
  339. if (!ap)
  340. return;
  341. spin_lock_irqsave(&ap->recv_lock, flags);
  342. ppp_sync_input(ap, buf, cflags, count);
  343. spin_unlock_irqrestore(&ap->recv_lock, flags);
  344. if (!skb_queue_empty(&ap->rqueue))
  345. tasklet_schedule(&ap->tsk);
  346. sp_put(ap);
  347. tty_unthrottle(tty);
  348. }
  349. static void
  350. ppp_sync_wakeup(struct tty_struct *tty)
  351. {
  352. struct syncppp *ap = sp_get(tty);
  353. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  354. if (!ap)
  355. return;
  356. set_bit(XMIT_WAKEUP, &ap->xmit_flags);
  357. tasklet_schedule(&ap->tsk);
  358. sp_put(ap);
  359. }
  360. static struct tty_ldisc_ops ppp_sync_ldisc = {
  361. .owner = THIS_MODULE,
  362. .magic = TTY_LDISC_MAGIC,
  363. .name = "pppsync",
  364. .open = ppp_sync_open,
  365. .close = ppp_sync_close,
  366. .hangup = ppp_sync_hangup,
  367. .read = ppp_sync_read,
  368. .write = ppp_sync_write,
  369. .ioctl = ppp_synctty_ioctl,
  370. .poll = ppp_sync_poll,
  371. .receive_buf = ppp_sync_receive,
  372. .write_wakeup = ppp_sync_wakeup,
  373. };
  374. static int __init
  375. ppp_sync_init(void)
  376. {
  377. int err;
  378. err = tty_register_ldisc(N_SYNC_PPP, &ppp_sync_ldisc);
  379. if (err != 0)
  380. printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
  381. err);
  382. return err;
  383. }
  384. /*
  385. * The following routines provide the PPP channel interface.
  386. */
  387. static int
  388. ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
  389. {
  390. struct syncppp *ap = chan->private;
  391. int err, val;
  392. u32 accm[8];
  393. void __user *argp = (void __user *)arg;
  394. u32 __user *p = argp;
  395. err = -EFAULT;
  396. switch (cmd) {
  397. case PPPIOCGFLAGS:
  398. val = ap->flags | ap->rbits;
  399. if (put_user(val, (int __user *) argp))
  400. break;
  401. err = 0;
  402. break;
  403. case PPPIOCSFLAGS:
  404. if (get_user(val, (int __user *) argp))
  405. break;
  406. ap->flags = val & ~SC_RCV_BITS;
  407. spin_lock_irq(&ap->recv_lock);
  408. ap->rbits = val & SC_RCV_BITS;
  409. spin_unlock_irq(&ap->recv_lock);
  410. err = 0;
  411. break;
  412. case PPPIOCGASYNCMAP:
  413. if (put_user(ap->xaccm[0], p))
  414. break;
  415. err = 0;
  416. break;
  417. case PPPIOCSASYNCMAP:
  418. if (get_user(ap->xaccm[0], p))
  419. break;
  420. err = 0;
  421. break;
  422. case PPPIOCGRASYNCMAP:
  423. if (put_user(ap->raccm, p))
  424. break;
  425. err = 0;
  426. break;
  427. case PPPIOCSRASYNCMAP:
  428. if (get_user(ap->raccm, p))
  429. break;
  430. err = 0;
  431. break;
  432. case PPPIOCGXASYNCMAP:
  433. if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  434. break;
  435. err = 0;
  436. break;
  437. case PPPIOCSXASYNCMAP:
  438. if (copy_from_user(accm, argp, sizeof(accm)))
  439. break;
  440. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  441. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  442. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  443. err = 0;
  444. break;
  445. case PPPIOCGMRU:
  446. if (put_user(ap->mru, (int __user *) argp))
  447. break;
  448. err = 0;
  449. break;
  450. case PPPIOCSMRU:
  451. if (get_user(val, (int __user *) argp))
  452. break;
  453. if (val < PPP_MRU)
  454. val = PPP_MRU;
  455. ap->mru = val;
  456. err = 0;
  457. break;
  458. default:
  459. err = -ENOTTY;
  460. }
  461. return err;
  462. }
  463. /*
  464. * This is called at softirq level to deliver received packets
  465. * to the ppp_generic code, and to tell the ppp_generic code
  466. * if we can accept more output now.
  467. */
  468. static void ppp_sync_process(unsigned long arg)
  469. {
  470. struct syncppp *ap = (struct syncppp *) arg;
  471. struct sk_buff *skb;
  472. /* process received packets */
  473. while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
  474. if (skb->len == 0) {
  475. /* zero length buffers indicate error */
  476. ppp_input_error(&ap->chan, 0);
  477. kfree_skb(skb);
  478. }
  479. else
  480. ppp_input(&ap->chan, skb);
  481. }
  482. /* try to push more stuff out */
  483. if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_sync_push(ap))
  484. ppp_output_wakeup(&ap->chan);
  485. }
  486. /*
  487. * Procedures for encapsulation and framing.
  488. */
  489. static struct sk_buff*
  490. ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *skb)
  491. {
  492. int proto;
  493. unsigned char *data;
  494. int islcp;
  495. data = skb->data;
  496. proto = get_unaligned_be16(data);
  497. /* LCP packets with codes between 1 (configure-request)
  498. * and 7 (code-reject) must be sent as though no options
  499. * have been negotiated.
  500. */
  501. islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
  502. /* compress protocol field if option enabled */
  503. if (data[0] == 0 && (ap->flags & SC_COMP_PROT) && !islcp)
  504. skb_pull(skb,1);
  505. /* prepend address/control fields if necessary */
  506. if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
  507. if (skb_headroom(skb) < 2) {
  508. struct sk_buff *npkt = dev_alloc_skb(skb->len + 2);
  509. if (npkt == NULL) {
  510. kfree_skb(skb);
  511. return NULL;
  512. }
  513. skb_reserve(npkt,2);
  514. skb_copy_from_linear_data(skb,
  515. skb_put(npkt, skb->len), skb->len);
  516. kfree_skb(skb);
  517. skb = npkt;
  518. }
  519. skb_push(skb,2);
  520. skb->data[0] = PPP_ALLSTATIONS;
  521. skb->data[1] = PPP_UI;
  522. }
  523. ap->last_xmit = jiffies;
  524. if (skb && ap->flags & SC_LOG_OUTPKT)
  525. ppp_print_buffer ("send buffer", skb->data, skb->len);
  526. return skb;
  527. }
  528. /*
  529. * Transmit-side routines.
  530. */
  531. /*
  532. * Send a packet to the peer over an sync tty line.
  533. * Returns 1 iff the packet was accepted.
  534. * If the packet was not accepted, we will call ppp_output_wakeup
  535. * at some later time.
  536. */
  537. static int
  538. ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb)
  539. {
  540. struct syncppp *ap = chan->private;
  541. ppp_sync_push(ap);
  542. if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
  543. return 0; /* already full */
  544. skb = ppp_sync_txmunge(ap, skb);
  545. if (skb != NULL)
  546. ap->tpkt = skb;
  547. else
  548. clear_bit(XMIT_FULL, &ap->xmit_flags);
  549. ppp_sync_push(ap);
  550. return 1;
  551. }
  552. /*
  553. * Push as much data as possible out to the tty.
  554. */
  555. static int
  556. ppp_sync_push(struct syncppp *ap)
  557. {
  558. int sent, done = 0;
  559. struct tty_struct *tty = ap->tty;
  560. int tty_stuffed = 0;
  561. if (!spin_trylock_bh(&ap->xmit_lock))
  562. return 0;
  563. for (;;) {
  564. if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
  565. tty_stuffed = 0;
  566. if (!tty_stuffed && ap->tpkt) {
  567. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  568. sent = tty->ops->write(tty, ap->tpkt->data, ap->tpkt->len);
  569. if (sent < 0)
  570. goto flush; /* error, e.g. loss of CD */
  571. if (sent < ap->tpkt->len) {
  572. tty_stuffed = 1;
  573. } else {
  574. kfree_skb(ap->tpkt);
  575. ap->tpkt = NULL;
  576. clear_bit(XMIT_FULL, &ap->xmit_flags);
  577. done = 1;
  578. }
  579. continue;
  580. }
  581. /* haven't made any progress */
  582. spin_unlock_bh(&ap->xmit_lock);
  583. if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags) ||
  584. (!tty_stuffed && ap->tpkt)))
  585. break;
  586. if (!spin_trylock_bh(&ap->xmit_lock))
  587. break;
  588. }
  589. return done;
  590. flush:
  591. if (ap->tpkt) {
  592. kfree_skb(ap->tpkt);
  593. ap->tpkt = NULL;
  594. clear_bit(XMIT_FULL, &ap->xmit_flags);
  595. done = 1;
  596. }
  597. spin_unlock_bh(&ap->xmit_lock);
  598. return done;
  599. }
  600. /*
  601. * Flush output from our internal buffers.
  602. * Called for the TCFLSH ioctl.
  603. */
  604. static void
  605. ppp_sync_flush_output(struct syncppp *ap)
  606. {
  607. int done = 0;
  608. spin_lock_bh(&ap->xmit_lock);
  609. if (ap->tpkt != NULL) {
  610. kfree_skb(ap->tpkt);
  611. ap->tpkt = NULL;
  612. clear_bit(XMIT_FULL, &ap->xmit_flags);
  613. done = 1;
  614. }
  615. spin_unlock_bh(&ap->xmit_lock);
  616. if (done)
  617. ppp_output_wakeup(&ap->chan);
  618. }
  619. /*
  620. * Receive-side routines.
  621. */
  622. /* called when the tty driver has data for us.
  623. *
  624. * Data is frame oriented: each call to ppp_sync_input is considered
  625. * a whole frame. If the 1st flag byte is non-zero then the whole
  626. * frame is considered to be in error and is tossed.
  627. */
  628. static void
  629. ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
  630. char *flags, int count)
  631. {
  632. struct sk_buff *skb;
  633. unsigned char *p;
  634. if (count == 0)
  635. return;
  636. if (ap->flags & SC_LOG_INPKT)
  637. ppp_print_buffer ("receive buffer", buf, count);
  638. /* stuff the chars in the skb */
  639. skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
  640. if (!skb) {
  641. printk(KERN_ERR "PPPsync: no memory (input pkt)\n");
  642. goto err;
  643. }
  644. /* Try to get the payload 4-byte aligned */
  645. if (buf[0] != PPP_ALLSTATIONS)
  646. skb_reserve(skb, 2 + (buf[0] & 1));
  647. if (flags && *flags) {
  648. /* error flag set, ignore frame */
  649. goto err;
  650. } else if (count > skb_tailroom(skb)) {
  651. /* packet overflowed MRU */
  652. goto err;
  653. }
  654. p = skb_put(skb, count);
  655. memcpy(p, buf, count);
  656. /* strip address/control field if present */
  657. p = skb->data;
  658. if (p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
  659. /* chop off address/control */
  660. if (skb->len < 3)
  661. goto err;
  662. p = skb_pull(skb, 2);
  663. }
  664. /* decompress protocol field if compressed */
  665. if (p[0] & 1) {
  666. /* protocol is compressed */
  667. skb_push(skb, 1)[0] = 0;
  668. } else if (skb->len < 2)
  669. goto err;
  670. /* queue the frame to be processed */
  671. skb_queue_tail(&ap->rqueue, skb);
  672. return;
  673. err:
  674. /* queue zero length packet as error indication */
  675. if (skb || (skb = dev_alloc_skb(0))) {
  676. skb_trim(skb, 0);
  677. skb_queue_tail(&ap->rqueue, skb);
  678. }
  679. }
  680. static void __exit
  681. ppp_sync_cleanup(void)
  682. {
  683. if (tty_unregister_ldisc(N_SYNC_PPP) != 0)
  684. printk(KERN_ERR "failed to unregister Sync PPP line discipline\n");
  685. }
  686. module_init(ppp_sync_init);
  687. module_exit(ppp_sync_cleanup);
  688. MODULE_LICENSE("GPL");
  689. MODULE_ALIAS_LDISC(N_SYNC_PPP);