ppp_synctty.c 18 KB

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