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