]> git.ozlabs.org Git - ppp.git/blob - linux/ppp.c
fix from Al Longyear
[ppp.git] / linux / ppp.c
1 /*
2    PPP for Linux
3
4    $Id: ppp.c,v 1.2 1994/05/30 02:42:55 paulus Exp $
5 */
6
7 /*
8    Sources:
9
10    slip.c
11
12    RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
13    Multi-protocol Datagrams over Point-to-Point Links
14
15    RFC1332: IPCP
16
17    ppp-2.0
18
19    Flags for this module (any combination is acceptable for testing.):
20
21    NET02D             - Define if using Net-2-Debugged in kernels earler
22                         than v1.1.4.
23
24    NEW_TTY_DRIVERS    - Define if using new Ted Ts'o's alpha TTY drivers
25                         from tsx-11.mit.edu. From Ted Ts'o.
26
27    OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
28                         character. This is normally set to ((HZ * 3) / 2).
29                         This is 1.5 seconds. If not defined then the leading
30                         flag is always sent.  
31 */
32
33 /* #define NET02D                                       /* */
34 /* #define NEW_TTY_DRIVERS                      /* */
35 #define OPTIMIZE_FLAG_TIME  ((HZ * 3)/2)        /* */
36
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/ioport.h>
44 #include <linux/in.h>
45 #include <linux/malloc.h>
46 #include <linux/tty.h>
47 #include <linux/errno.h>
48 #include <linux/sched.h>   /* to get the struct task_struct */
49 #include <linux/string.h>  /* used in new tty drivers */
50 #include <linux/signal.h>  /* used in new tty drivers */
51 #include <asm/system.h>
52 #include <asm/bitops.h>
53 #include <asm/segment.h>
54
55 #ifdef NET02D                           /* v1.1.4 net code and earlier */
56 #include <dev.h>
57 #include <skbuff.h>
58 #define skb_queue_head_init(buf)        *(buf) = NULL
59 #else                                   /* v1.1.5 and later */
60 #include <linux/netdevice.h>
61 #include <linux/skbuff.h>
62 #endif
63
64 #include <linux/ppp.h>
65
66 #include <ip.h>
67 #include <tcp.h>
68 #include <inet.h>
69 #include "slhc.h"
70
71 #include <linux/if_arp.h>
72 #ifndef ARPHRD_PPP
73 #define ARPHRD_PPP 0
74 #endif
75
76 #define PRINTK(p) printk p ;
77 #define ASSERT(p) if (!p) PRINTK ((KERN_CRIT "assertion failed: " # p))
78 #define PRINTKN(n,p) {if (ppp_debug >= n) PRINTK (p)}
79 #define CHECK_PPP(a)  if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return a;}
80 #define CHECK_PPP_VOID()  if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return;}
81
82 #define in_xmap(ppp,c)  (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
83 #define in_rmap(ppp,c)  ((((unsigned int) (unsigned char) (c)) < 0x20) && \
84                         ppp->recv_async_map & (1 << (c)))
85
86 #define bset(p,b)       ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
87
88 int ppp_debug = 2;
89 int ppp_debug_netpackets = 0;
90
91 /* Define this string only once for all macro envocations */
92 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
93
94 int ppp_init(struct device *);
95 static void ppp_init_ctrl_blk(struct ppp *);
96 static int ppp_dev_open(struct device *);
97 static int ppp_dev_close(struct device *);
98 static void ppp_kick_tty(struct ppp *);
99
100 #ifdef NEW_TTY_DRIVERS
101 #define ppp_find(tty) ((struct ppp *) tty->disc_data)
102 #else
103 static void ppp_output_done(void *);
104 static void ppp_unesc(struct ppp *ppp, unsigned char *c, int n);
105 static struct ppp *ppp_find(struct tty_struct *);
106 #endif
107
108 static void ppp_doframe(struct ppp *);
109 static int ppp_do_ip(struct ppp *, unsigned short, unsigned char *, int);
110 static int ppp_us_queue(struct ppp *, unsigned short, unsigned char *, int);
111 static int ppp_xmit(struct sk_buff *, struct device *);
112 static unsigned short ppp_type_trans(struct sk_buff *, struct device *);
113
114 #ifdef NET02D
115 static int ppp_header(unsigned char *buff, struct device *dev,
116                       unsigned short type, unsigned long daddr,
117                       unsigned long saddr, unsigned len);
118 static int ppp_rebuild_header(void *buff, struct device *dev);
119 static void ppp_add_arp(unsigned long addr, struct sk_buff *skb,
120                         struct device *dev);
121 #else
122 static int ppp_header(unsigned char *, struct device *, unsigned short,
123                       void *, void *, unsigned, struct sk_buff *);
124 static int ppp_rebuild_header(void *, struct device *, unsigned long,
125                               struct sk_buff *);
126 #endif
127
128 static struct enet_statistics *ppp_get_stats (struct device *);
129 static struct ppp *ppp_alloc(void);
130 static int ppp_lock(struct ppp *);
131 static void ppp_unlock(struct ppp *);
132 static void ppp_add_fcs(struct ppp *);
133 static int ppp_check_fcs(struct ppp *);
134 static void ppp_print_buffer(const char *,char *,int,int);
135
136 static int ppp_read(struct tty_struct *, struct file *, unsigned char *,
137                     unsigned int);
138 static int ppp_write(struct tty_struct *, struct file *, unsigned char *,
139                      unsigned int);
140 static int ppp_ioctl(struct tty_struct *, struct file *, unsigned int,
141                      unsigned long);
142 static int ppp_select(struct tty_struct *tty, struct inode * inode,
143                       struct file * filp, int sel_type, select_table * wait);
144 static int ppp_open(struct tty_struct *);
145 static void ppp_close(struct tty_struct *);
146
147 #ifdef NEW_TTY_DRIVERS
148 static void ppp_receive_buf(struct tty_struct *tty, unsigned char *cp,
149                             char *fp, int count);
150 static void ppp_write_wakeup(struct tty_struct *tty);
151 #else
152 static void ppp_tty_input_ready(struct tty_struct *);
153 #endif
154
155 /* FCS table from RFC1331 */
156
157 static unsigned short fcstab[256] = {
158   0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
159   0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
160   0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
161   0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
162   0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
163   0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
164   0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
165   0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
166   0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
167   0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
168   0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
169   0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
170   0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
171   0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
172   0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
173   0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
174   0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
175   0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
176   0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
177   0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
178   0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
179   0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
180   0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
181   0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
182   0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
183   0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
184   0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
185   0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
186   0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
187   0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
188   0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
189   0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
190   };
191
192 struct tty_ldisc ppp_ldisc;
193
194 static struct ppp ppp_ctrl[PPP_NRUNIT];
195
196 /*************************************************************
197  * INITIALIZATION
198  *************************************************************/
199
200 static int first_time = 1;
201
202 /* called at boot time for each ppp device */
203
204 int
205 ppp_init(struct device *dev)
206 {
207   struct ppp *ppp;
208   int i;
209
210   ppp = &ppp_ctrl[dev->base_addr];
211
212   if (first_time) {
213     first_time = 0;
214
215     printk (KERN_INFO "PPP: version %s (%d channels)"
216 #ifdef NET02D
217            " NET02D"
218 #endif
219 #ifdef NEW_TTY_DRIVERS
220            " NEW_TTY_DRIVERS"
221 #endif
222 #ifdef OPTIMIZE_FLAG_TIME
223            " OPTIMIZE_FLAGS"
224 #endif
225            "\n", PPP_VERSION, PPP_NRUNIT);
226
227     printk (KERN_INFO
228            "TCP compression code copyright 1989 Regents of the "
229            "University of California\n");
230
231     (void) memset(&ppp_ldisc, 0, sizeof(ppp_ldisc));
232     ppp_ldisc.open    = ppp_open;
233     ppp_ldisc.close   = ppp_close;
234     ppp_ldisc.read    = ppp_read;
235     ppp_ldisc.write   = ppp_write;
236     ppp_ldisc.ioctl   = ppp_ioctl;
237     ppp_ldisc.select  = ppp_select;
238
239 #ifdef NEW_TTY_DRIVERS
240     ppp_ldisc.magic       = TTY_LDISC_MAGIC;
241     ppp_ldisc.receive_buf = ppp_receive_buf;
242     ppp_ldisc.write_wakeup = ppp_write_wakeup;
243 #else
244     ppp_ldisc.handler     = ppp_tty_input_ready;
245 #endif
246
247     if ((i = tty_register_ldisc(N_PPP, &ppp_ldisc)) == 0)
248       printk(KERN_INFO "PPP line discipline registered.\n");
249     else
250       printk(KERN_ERR "error registering line discipline: %d\n", i);
251   }
252
253   /* initialize PPP control block */
254   ppp_init_ctrl_blk (ppp);
255   ppp->inuse = 0;
256   ppp->line  = dev->base_addr;
257   ppp->tty   = NULL;
258   ppp->dev   = dev;
259
260   /* clear statistics */
261   memset (&ppp->stats, '\0', sizeof (struct ppp_stats));
262
263   /* device INFO */
264   dev->mtu             = PPP_MTU;
265   dev->hard_start_xmit = ppp_xmit;
266   dev->open            = ppp_dev_open;
267   dev->stop            = ppp_dev_close;
268   dev->get_stats       = ppp_get_stats;
269   dev->hard_header     = ppp_header;
270   dev->type_trans      = ppp_type_trans;
271   dev->rebuild_header  = ppp_rebuild_header;
272   dev->hard_header_len = 0;
273   dev->addr_len        = 0;
274   dev->type            = ARPHRD_PPP;
275
276 #ifdef NET02D
277   dev->add_arp         = ppp_add_arp;
278   dev->queue_xmit      = dev_queue_xmit;
279 #endif
280
281   for (i = 0; i < DEV_NUMBUFFS; i++)
282     skb_queue_head_init(&dev->buffs[i]);  /* = NULL if NET02D */
283
284   /* New-style flags */
285   dev->flags      = IFF_POINTOPOINT;
286   dev->family     = AF_INET;
287   dev->pa_addr    = 0;
288   dev->pa_brdaddr = 0;
289   dev->pa_mask    = 0;
290   dev->pa_alen    = sizeof(unsigned long);
291
292   return 0;
293 }
294
295 static void
296 ppp_init_ctrl_blk(struct ppp *ppp)
297 {
298   ppp->magic            = PPP_MAGIC;
299   ppp->sending          = 0;
300   ppp->toss             = 0xFE;
301   ppp->escape           = 0;
302
303   ppp->flags            = 0;
304   ppp->mtu              = PPP_MTU;
305   ppp->mru              = PPP_MRU;
306   ppp->fcs              = 0;
307
308   memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
309   ppp->xmit_async_map[0] = 0xffffffff;
310   ppp->xmit_async_map[3] = 0x60000000;
311   ppp->recv_async_map    = 0x00000000;
312
313   ppp->slcomp           = NULL;
314   ppp->rbuff            = NULL;
315   ppp->xbuff            = NULL;
316   ppp->cbuff            = NULL;
317
318   ppp->rhead            = NULL;
319   ppp->rend             = NULL;
320   ppp->rcount           = 0;
321   ppp->xhead            = NULL;
322   ppp->xtail            = NULL;
323
324   ppp->us_rbuff         = NULL;
325   ppp->us_rbuff_end     = NULL;
326   ppp->us_rbuff_head    = NULL;
327   ppp->us_rbuff_tail    = NULL;
328   ppp->read_wait        = NULL;
329   ppp->write_wait       = NULL;
330   ppp->us_rbuff_lock    = 0;
331   ppp->inp_sig          = 0;
332   ppp->inp_sig_pid      = 0;
333
334 #ifdef OPTIMIZE_FLAG_TIME /* ensure flag will always be sent first time */
335   ppp->last_xmit        = jiffies - OPTIMIZE_FLAG_TIME;
336 #else
337   ppp->last_xmit        = 0;
338 #endif
339
340   /* clear statistics */
341   memset (&ppp->stats, '\0', sizeof (struct ppp_stats));
342
343   /* Reset the demand dial information */
344   ppp->ddinfo.ip_sjiffies  =
345   ppp->ddinfo.ip_rjiffies  =
346   ppp->ddinfo.nip_sjiffies =
347   ppp->ddinfo.nip_rjiffies = jiffies;
348 }
349
350 /*
351  * MTU has been changed by the IP layer. Unfortunately we are not told
352  * about this, but we spot it ourselves and fix things up. We could be
353  * in an upcall from the tty driver, or in an ip packet queue.
354  */
355    
356 static void
357 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
358 {
359   struct device *dev;
360   unsigned char *new_rbuff, *new_xbuff, *new_cbuff;
361   unsigned char *old_rbuff, *old_xbuff, *old_cbuff;
362   int mtu, mru;
363 /*
364  *  Allocate the buffer from the kernel for the data
365  */
366   dev = ppp->dev;
367   mru = new_mru;
368   mtu = new_mtu;
369
370   /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
371   if (mru < PPP_MRU)
372     mru = PPP_MRU;
373
374   mtu = (mtu * 2) + 20;
375   mru = (mru * 2) + 20;
376
377   PRINTKN (2,(KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
378               dev->name, new_mtu, new_mru));
379         
380   new_xbuff = (unsigned char *) kmalloc(mtu + 4, GFP_ATOMIC);
381   new_rbuff = (unsigned char *) kmalloc(mru + 4, GFP_ATOMIC);
382   new_cbuff = (unsigned char *) kmalloc(mru + 4, GFP_ATOMIC);
383 /*
384  *  If the buffers failed to allocate then complain.
385  */
386   if (new_xbuff == NULL || new_rbuff == NULL || new_cbuff == NULL)
387     {
388       PRINTKN (2,(KERN_ERR "ppp: failed to allocate new buffers\n"));
389 /*
390  *  Release new buffer pointers if the updates were not performed
391  */
392       if (new_rbuff != NULL)
393         kfree (new_rbuff);
394
395       if (new_xbuff != NULL)
396         kfree (new_xbuff);
397
398       if (new_cbuff != NULL)
399         kfree (new_cbuff);
400     }
401 /*
402  *  Update the pointers to the new buffer structures.
403  */
404   else
405     {
406       cli();
407       old_xbuff       = ppp->xbuff;
408       old_rbuff       = ppp->rbuff;
409       old_cbuff       = ppp->cbuff;
410
411       ppp->xbuff      = new_xbuff;
412       ppp->rbuff      = new_rbuff;
413       ppp->cbuff      = new_cbuff;
414
415       dev->mem_start  = (unsigned long) new_xbuff;
416       dev->mem_end    = (unsigned long) (dev->mem_start + mtu);
417
418       dev->rmem_start = (unsigned long) new_rbuff;
419       ppp->rend       = (unsigned char *)
420       dev->rmem_end   = (unsigned long) (dev->rmem_start + mru);
421
422       ppp->rhead      = new_rbuff;
423 /*
424  *  Update the parameters for the new buffer sizes
425  */
426       ppp->toss         = 0xFE;
427       ppp->escape       = 0;
428       ppp->sending      = 0;
429       ppp->rcount       = 0;
430
431       ppp->mru          = new_mru;
432
433       ppp->mtu          =
434       dev->mtu          = new_mtu;
435
436       sti();
437 /*
438  *  Release old buffer pointers
439  */
440       if (old_rbuff != NULL)
441         kfree (old_rbuff);
442
443       if (old_xbuff != NULL)
444         kfree (old_xbuff);
445
446       if (old_cbuff != NULL)
447         kfree (old_cbuff);
448     }
449 }
450
451 /* called when we abandon the PPP line discipline */
452
453 static void
454 ppp_release(struct ppp *ppp)
455 {
456 #ifdef NEW_TTY_DRIVERS
457   if (ppp->tty != NULL && ppp->tty->disc_data == ppp)
458     ppp->tty->disc_data = NULL; /* Break the tty->ppp link */
459 #endif
460
461   if (ppp->dev) {
462     ppp->dev->flags &= ~IFF_UP; /* down the device */
463     ppp->dev->flags |= IFF_POINTOPOINT;
464   }
465
466   kfree (ppp->xbuff);
467   kfree (ppp->cbuff);
468   kfree (ppp->rbuff);
469   kfree (ppp->us_rbuff);
470
471   ppp->xbuff    =
472   ppp->cbuff    =
473   ppp->rbuff    =
474   ppp->us_rbuff = NULL;
475
476   if (ppp->slcomp) {
477     slhc_free(ppp->slcomp);
478     ppp->slcomp = NULL;
479   }
480
481   ppp->inuse = 0;
482   ppp->tty   = NULL;
483 }
484
485 static void
486 ppp_close(struct tty_struct *tty)
487 {
488   struct ppp *ppp = ppp_find(tty);
489
490   if (ppp == NULL || ppp->magic != PPP_MAGIC) {
491     PRINTKN (1,(KERN_WARNING "ppp: trying to close unopened tty!\n"));
492   } else {
493     CHECK_PPP_VOID();
494     ppp_release (ppp);
495
496     PRINTKN (2,(KERN_INFO "ppp: channel %s closing.\n", ppp->dev->name));
497   }
498 }
499
500 /* called when PPP line discipline is selected on a tty */
501 static int
502 ppp_open(struct tty_struct *tty)
503 {
504   struct ppp *ppp = ppp_find(tty);
505
506   if (ppp) {
507     PRINTKN (1,(KERN_ERR "ppp_open: gack! tty already associated to %s!\n",
508                 ppp->magic == PPP_MAGIC ? ppp->dev->name : "unknown"));
509     return -EEXIST;
510   }
511
512   ppp = ppp_alloc();
513   if (ppp == NULL) {
514     PRINTKN (1,(KERN_ERR "ppp_open: couldn't allocate ppp channel\n"));
515     return -ENFILE;
516   }
517
518   /* make sure the channel is actually open */
519   ppp_init_ctrl_blk (ppp);
520
521   ppp->tty = tty;
522
523 #ifdef NEW_TTY_DRIVERS
524   tty->disc_data = ppp;
525   if (tty->driver.flush_buffer)
526     tty->driver.flush_buffer(tty);
527   if (tty->ldisc.flush_buffer)
528     tty->ldisc.flush_buffer(tty);
529 #else
530   tty_read_flush (tty);
531   tty_write_flush (tty);
532 #endif
533
534   if ((ppp->slcomp = slhc_init(16, 16)) == NULL) {
535     PRINTKN (1,(KERN_ERR "ppp: no space for compression buffers!\n"));
536     ppp_release (ppp);
537     return -ENOMEM;
538   }
539
540   /* Define the buffers for operation */
541   ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru);
542   if (ppp->rbuff == NULL) {
543     ppp_release (ppp);
544     return -ENOMEM;
545   }
546
547   /* Allocate a user-level receive buffer */
548   ppp->us_rbuff = kmalloc (RBUFSIZE, GFP_KERNEL);
549   if (ppp->us_rbuff == NULL) {
550     PRINTKN (1,(KERN_ERR "ppp: no space for user receive buffer\n"));
551     ppp_release (ppp);
552     return -ENOMEM;
553   }
554
555   ppp->us_rbuff_head =
556   ppp->us_rbuff_tail = ppp->us_rbuff;
557   ppp->us_rbuff_end  = ppp->us_rbuff + RBUFSIZE;
558
559   PRINTKN (2,(KERN_INFO "ppp: channel %s open\n", ppp->dev->name));
560
561   return (ppp->line);
562 }
563
564 /* called when ppp interface goes "up".  here this just means we start
565    passing IP packets */
566 static int
567 ppp_dev_open(struct device *dev)
568 {
569   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
570
571   /* reset POINTOPOINT every time, since dev_close zaps it! */
572   dev->flags |= IFF_POINTOPOINT;
573
574   if (ppp->tty == NULL) {
575     PRINTKN (1,(KERN_ERR "ppp: %s not connected to a TTY! can't go open!\n",
576                 dev->name));
577     return -ENXIO;
578   }
579
580   PRINTKN (2,(KERN_INFO "ppp: channel %s going up for IP packets!\n",
581               dev->name));
582
583   CHECK_PPP(-ENXIO);
584   return 0;
585 }
586
587 static int
588 ppp_dev_close(struct device *dev)
589 {
590   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
591
592   if (ppp->tty == NULL) {
593     PRINTKN (1,(KERN_ERR "ppp: %s not connected to a TTY! can't go down!\n",
594                 dev->name));
595     return -ENXIO;
596   }
597
598   PRINTKN (2,(KERN_INFO "ppp: channel %s going down for IP packets!\n",
599               dev->name));
600   CHECK_PPP(-ENXIO);
601   return 0;
602 }
603
604 /*************************************************************
605  * TTY OUTPUT
606  *    The following function delivers a fully-formed PPP
607  *    frame in ppp->xbuff to the TTY for output.
608  *************************************************************/
609
610 #ifdef NEW_TTY_DRIVERS
611 static inline void
612 #else
613 static void
614 #endif
615 ppp_output_done (void *ppp)
616 {
617   /* unlock the transmitter queue */
618   ppp_unlock ((struct ppp *) ppp);
619
620   /* If the device is still up then enable the transmitter of the
621      next frame. */
622   if (((struct ppp *) ppp)->dev->flags & IFF_UP)
623     dev_tint (((struct ppp *) ppp)->dev);
624
625   /* enable any blocked process pending transmission */
626   wake_up_interruptible (&((struct ppp *) ppp)->write_wait);
627 }
628
629 #ifndef NEW_TTY_DRIVERS
630 static void
631 ppp_kick_tty (struct ppp *ppp)
632 {
633   register int count = ppp->xhead - ppp->xbuff;
634   register int answer;
635
636   ppp->stats.sbytes += count;
637
638   answer = tty_write_data (ppp->tty,
639                            ppp->xbuff,
640                            count,
641                            ppp_output_done,
642                            (void *) ppp);
643
644   if (answer == 0)
645     ppp_output_done (ppp);   /* Should not happen */
646   else
647     if (answer < 0) {
648       ppp->stats.serrors++;
649       ppp_output_done (ppp); /* unlock the transmitter */
650     }
651 }
652
653 #else
654
655 static void
656 ppp_kick_tty (struct ppp *ppp)
657 {
658         register int count, actual;
659         
660         count = ppp->xhead - ppp->xbuff;
661         
662         actual = ppp->tty->driver.write(ppp->tty, 0, ppp->xbuff, count);
663         ppp->stats.sbytes += actual;
664         if (actual == count) {
665                 ppp_output_done(ppp);
666         } else {
667                 ppp->xtail = ppp->xbuff + actual;
668                 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
669         }
670 }
671
672 static void ppp_write_wakeup(struct tty_struct *tty)
673 {
674         register int count, actual;
675         struct ppp *ppp = ppp_find(tty);
676
677         if (!ppp || ppp->magic != PPP_MAGIC) {
678                 PRINTKN (1,
679                          (KERN_ERR "PPP: write_wakeup called but couldn't "
680                           "find PPP struct.\n"));
681                 return;
682         }
683
684         if (!ppp->xtail || (ppp->flags & SC_XMIT_BUSY))
685                 return;
686
687         cli();
688         if (ppp->flags & SC_XMIT_BUSY)
689                 return;
690         ppp->flags |= SC_XMIT_BUSY;
691         sti();
692         
693         count = ppp->xhead - ppp->xtail;
694         
695         actual = tty->driver.write(tty, 0, ppp->xtail, count);
696         ppp->stats.sbytes += actual;
697         if (actual == count) {
698                 ppp->xtail = 0;
699                 tty->flags &= ~TTY_DO_WRITE_WAKEUP;
700
701                 ppp_output_done(ppp);
702         } else {
703                 ppp->xtail += actual;
704         }
705         ppp->flags &= ~SC_XMIT_BUSY;
706 }
707 #endif
708
709 /*************************************************************
710  * TTY INPUT
711  *    The following functions handle input that arrives from
712  *    the TTY.  It recognizes PPP frames and either hands them
713  *    to the network layer or queues them for delivery to a
714  *    user process reading this TTY.
715  *************************************************************/
716
717 /* stuff a single character into the receive buffer */
718
719 inline void
720 ppp_enqueue(struct ppp *ppp, unsigned char c)
721 {
722   unsigned long flags;
723
724   save_flags(flags);
725   cli();
726   if (ppp->rhead < ppp->rend) {
727     *ppp->rhead = c;
728     ppp->rhead++;
729     ppp->rcount++;
730   } else
731     ppp->stats.roverrun++;
732   restore_flags(flags);
733 }
734
735 #ifdef CHECK_CHARACTERS
736 static unsigned paritytab[8] = {
737     0x96696996, 0x69969669, 0x69969669, 0x96696996,
738     0x69969669, 0x96696996, 0x96696996, 0x69969669
739 };
740 #endif
741
742 #ifndef NEW_TTY_DRIVERS
743 static void
744 ppp_dump_inqueue(struct tty_struct *tty)
745 {
746   int  head = tty->read_q.head,
747        tail = tty->read_q.tail,
748        i, count;
749   char buffer[8];
750
751   PRINTK ((KERN_DEBUG "INQUEUE: head %d tail %d imode %x:\n", head, tail, 
752            (unsigned int) tty->termios->c_iflag))
753
754   i     = tail;
755   count = 0;
756
757   while (i != head) {
758     buffer [count] = tty->read_q.buf[i];
759     if (++count == 8) {
760       ppp_print_buffer (NULL, buffer, 8, KERNEL_DS);
761       count = 0;
762     }
763     i = (i + 1) & (TTY_BUF_SIZE - 1);
764   }
765   ppp_print_buffer (NULL, buffer, count, KERNEL_DS);
766 }
767
768 /* called by lower levels of TTY driver when data becomes available.
769    all incoming data comes through this function. */
770
771 void ppp_tty_input_ready(struct tty_struct *tty)
772 {
773   struct ppp *ppp = ppp_find(tty);
774   int n, error;
775   unsigned char buff[128];
776
777 /*  PRINTK( (KERN_DEBUG "PPP: handler called.\n") ) */
778   if (!ppp || ppp->magic != PPP_MAGIC) {
779     PRINTKN (1,
780              (KERN_ERR "PPP: handler called but couldn't find PPP struct.\n"));
781     return;
782   }
783
784   CHECK_PPP_VOID();
785
786   /* ZZZ */
787   if (ppp_debug >= 5)
788     ppp_dump_inqueue(ppp->tty);
789
790   do {
791     n = tty_read_raw_data(tty, buff, 128);
792     if ( n == 0 )               /* nothing there */
793       break;
794
795     if (ppp_debug >= 5)
796       ppp_print_buffer ("receive buffer", buff, n > 0 ? n : -n, KERNEL_DS);
797
798     if ( n < 0 ) {
799       /* Last character is error flag.
800          Process the previous characters, then set toss flag. */
801       n = (-n) - 1;
802       error = buff[n];
803     } else error = 0;
804     ppp->stats.rbytes += n;
805     ppp_unesc(ppp,buff,n);
806     if (error)
807       ppp->toss = error;
808   } while (1);
809 }
810
811 /* recover frame by undoing PPP escape mechanism;
812    copies N chars of input data from C into PPP->rbuff
813    calls ppp_doframe to dispose of any frames it finds
814 */
815
816 static void
817 ppp_unesc(struct ppp *ppp, unsigned char *c, int n)
818 {
819   int i;
820
821   for (i = 0; i < n; i++, c++) {
822     PRINTKN (6,(KERN_DEBUG "(%x)", (unsigned int) *c));
823
824 #ifdef CHECK_CHARACTERS
825     if (*c & 0x80)
826         sc->sc_flags |= SC_RCV_B7_1;
827     else
828         sc->sc_flags |= SC_RCV_B7_0;
829
830     if (paritytab[*c >> 5] & (1 << (*c & 0x1F)))
831         sc->sc_flags |= SC_RCV_ODDP;
832     else
833         sc->sc_flags |= SC_RCV_EVNP;
834 #endif
835
836     switch (*c) {
837     case PPP_ESC:               /* PPP_ESC: invert 0x20 in next character */
838       ppp->escape = PPP_TRANS;
839       break;
840
841     case PPP_FLAG:              /* PPP_FLAG: end of frame */
842       if (ppp->escape)          /* PPP_ESC just before PPP_FLAG is illegal */
843         ppp->toss = 0xFF;
844
845       if ((ppp->toss & 0x80) == 0)
846         ppp_doframe(ppp);       /* pass frame on to next layers */
847
848       ppp->rcount = 0;
849       ppp->rhead  = ppp->rbuff;
850       ppp->escape = 0;
851       ppp->toss   = 0;
852       break;
853
854     default:                    /* regular character */
855       if (!in_rmap (ppp, *c)) {
856         if (ppp->toss == 0)
857           ppp_enqueue (ppp, *c ^ ppp->escape);
858         ppp->escape = 0;
859       }
860       break;
861     }
862   }
863 }
864
865 #else
866 static void ppp_receive_buf(struct tty_struct *tty, unsigned char *cp,
867                             char *fp, int count)
868 {
869   register struct ppp *ppp = ppp_find (tty);
870   unsigned char c;
871  
872 /*  PRINTK( ("PPP: handler called.\n") ); */
873
874   if (!ppp || ppp->magic != PPP_MAGIC) {
875     PRINTKN (1,("PPP: handler called but couldn't find "
876                 "PPP struct.\n"));
877     return;
878   }
879
880   CHECK_PPP_VOID();
881  
882   if (ppp_debug >= 5) {
883     ppp_print_buffer ("receive buffer", cp, count, KERNEL_DS);
884   }
885  
886   while (count-- > 0) {
887     c = *cp++;
888
889     if (fp) {
890       if (*fp && ppp->toss == 0)
891         ppp->toss = *fp;
892       fp++;
893     }
894
895 #ifdef CHECK_CHARACTERS
896     if (c & 0x80)
897         sc->sc_flags |= SC_RCV_B7_1;
898     else
899         sc->sc_flags |= SC_RCV_B7_0;
900
901     if (paritytab[c >> 5] & (1 << (c & 0x1F)))
902         sc->sc_flags |= SC_RCV_ODDP;
903     else
904         sc->sc_flags |= SC_RCV_EVNP;
905 #endif
906
907     switch (c) {
908     case PPP_ESC:               /* PPP_ESC: invert 0x20 in next character */
909       ppp->escape = PPP_TRANS;
910       break;
911
912     case PPP_FLAG:              /* PPP_FLAG: end of frame */
913       if (ppp->escape)          /* PPP_ESC just before PPP_FLAG is "cancel"*/
914         ppp->toss = 0xFF;
915
916       if ((ppp->toss & 0x80) == 0)
917         ppp_doframe(ppp);       /* pass frame on to next layers */
918
919       ppp->rcount = 0;
920       ppp->rhead  = ppp->rbuff;
921       ppp->escape = 0;
922       ppp->toss   = 0;
923       break;
924
925     default:                    /* regular character */
926       if (!in_rmap (ppp, c)) {
927         if (ppp->toss == 0)
928           ppp_enqueue (ppp, c ^ ppp->escape);
929         ppp->escape = 0;
930       }
931     }
932   }
933 }
934 #endif
935
936 /* on entry, a received frame is in ppp->rbuff
937    check it and dispose as appropriate */
938 static void
939 ppp_doframe(struct ppp *ppp)
940 {
941   u_char *c = ppp->rbuff;
942   u_short proto;
943   int count = ppp->rcount;
944
945   /* forget it if we've already noticed an error */
946   if (ppp->toss) {
947     PRINTKN (1, (KERN_WARNING "ppp_toss: tossing frame, reason = %d\n",
948                  ppp->toss));
949     ppp->stats.rerrors++;
950     return;
951   }
952
953   /* do this before printing buffer to avoid generating copious output */
954   if (count == 0)
955     return;
956
957   if (ppp_debug >= 3)
958     ppp_print_buffer ("receive frame", c, count, KERNEL_DS);
959
960   if (count < 4) {
961     PRINTKN (1,(KERN_WARNING "ppp: got runt ppp frame, %d chars\n", count));
962     ppp->stats.runts++;
963     return;
964   }
965
966   /* check PPP error detection field */
967   if (!ppp_check_fcs(ppp)) {
968     PRINTKN (1,(KERN_WARNING "ppp: frame with bad fcs\n"));
969     ppp->stats.rerrors++;
970     return;
971   }
972
973   count -= 2;                   /* ignore last two characters */
974
975   /* now we have a good frame */
976   /* figure out the protocol field */
977   if ((c[0] == PPP_ADDRESS) && (c[1] == PPP_CONTROL)) {
978     c = c + 2;                  /* ADDR/CTRL not compressed, so skip */
979     count -= 2;
980   }
981
982   proto = (u_short) *c++;               /* PROTO compressed */
983   if (proto & 1) {
984     count--;
985   } else {
986     proto = (proto << 8) | (u_short) *c++; /* PROTO uncompressed */
987     count -= 2;
988   }
989
990   /* Send the frame to the network if the ppp device is up */
991   if ((ppp->dev->flags & IFF_UP) && ppp_do_ip(ppp, proto, c, count)) {
992     ppp->ddinfo.ip_rjiffies = jiffies;
993     return;
994   }
995
996   /* If we got here, it has to go to a user process doing a read,
997      so queue it.
998
999      User process expects to get whole frame (for some reason), so
1000      use count+2 so as to include FCS field. */
1001
1002   if (ppp_us_queue (ppp, proto, c, count+2)) {
1003     ppp->ddinfo.nip_rjiffies = jiffies;
1004     ppp->stats.rothers++;
1005     return;
1006   }
1007
1008   /* couldn't cope. */
1009   PRINTKN (1,(KERN_WARNING
1010               "ppp: dropping packet on the floor: nobody could take it.\n"));
1011   ppp->stats.tossed++;
1012 }
1013
1014 /* Examine packet at C, attempt to pass up to net layer. 
1015    PROTO is the protocol field from the PPP frame.
1016    Return 1 if could handle it, 0 otherwise.  */
1017
1018 static int
1019 ppp_do_ip (struct ppp *ppp, unsigned short proto, unsigned char *c,
1020           int count)
1021 {
1022   int flags, done;
1023
1024   PRINTKN (4,(KERN_DEBUG "ppp_do_ip: proto %x len %d first byte %x\n",
1025               (int) proto, count, c[0]));
1026
1027   if (ppp_debug_netpackets) {
1028     PRINTK (("KERN_DEBUG %s <-- proto %x len %d\n", ppp->dev->name,
1029              (int) proto, count));
1030   }
1031     
1032   if (proto == PROTO_IP) {
1033     ppp->stats.runcomp++;
1034     goto sendit;
1035   }
1036
1037   if ((proto == PROTO_VJCOMP) && !(ppp->flags & SC_REJ_COMP_TCP)) {
1038     /* get space for uncompressing the header */
1039     done = 0;
1040     save_flags (flags);
1041     cli();
1042     if ((ppp->rhead + 80) < ppp->rend) {
1043       ppp->rhead += 80;
1044       ppp->rcount += 80;
1045       done = 1;
1046     }
1047     restore_flags(flags);
1048
1049     if (! done) {
1050       PRINTKN (1,(KERN_NOTICE
1051                   "ppp: no space to decompress VJ compressed TCP header.\n"));
1052       ppp->stats.roverrun++;
1053       return 1;
1054     }
1055
1056     count = slhc_uncompress(ppp->slcomp, c, count);
1057     if (count <= 0) {
1058       ppp->stats.rerrors++;
1059       PRINTKN (1,(KERN_NOTICE "ppp: error in VJ decompression\n"));
1060       return 1;
1061     }
1062     ppp->stats.rcomp++;
1063     goto sendit;
1064   }
1065   
1066   if ((proto == PROTO_VJUNCOMP) && !(ppp->flags & SC_REJ_COMP_TCP)) {
1067     if (slhc_remember(ppp->slcomp, c, count) <= 0) {
1068       ppp->stats.rerrors++;
1069       PRINTKN (1,(KERN_NOTICE "ppp: error in VJ memorizing\n"));
1070       return 1;
1071     }
1072     ppp->stats.runcomp++;
1073     goto sendit;
1074   }
1075
1076   /* not ours */
1077   return 0;
1078
1079  sendit:
1080   if (ppp_debug_netpackets) {
1081     struct iphdr *iph = (struct iphdr *) c;
1082     PRINTK ((KERN_INFO "%s <--    src %lx dst %lx len %d\n", ppp->dev->name, 
1083              iph->saddr, iph->daddr, count))
1084   }
1085
1086   /* receive the frame through the network software */
1087   while ((dev_rint(c, count, 0, ppp->dev) & ~1) != 0)
1088     ;
1089
1090   return 1;
1091 }
1092
1093 /* stuff packet at BUF, length LEN, into the us_rbuff buffer
1094    prepend PROTO information */
1095
1096 #define PUTC(c,label) *ppp->us_rbuff_head++ = c; \
1097                 if (ppp->us_rbuff_head == ppp->us_rbuff_end) \
1098                      ppp->us_rbuff_head = ppp->us_rbuff; \
1099                 if (ppp->us_rbuff_head == ppp->us_rbuff_tail) \
1100                      goto label;
1101 #define GETC(c) c = *ppp->us_rbuff_tail++; \
1102                 if (ppp->us_rbuff_tail == ppp->us_rbuff_end) \
1103                      ppp->us_rbuff_tail = ppp->us_rbuff;
1104
1105 static int
1106 ppp_us_queue(struct ppp *ppp, unsigned short proto, 
1107              unsigned char *buf, int len)
1108 {
1109   int totlen;
1110   unsigned char *saved_head;
1111
1112   totlen = len+2;               /* including protocol */
1113
1114   if (set_bit(1, &ppp->us_rbuff_lock)) {
1115     PRINTKN (1, (KERN_NOTICE "ppp_us_queue: can't get lock\n"));
1116     return 0;
1117   }
1118   saved_head = ppp->us_rbuff_head;
1119
1120   PUTC((totlen & 0xff00) >> 8, failure);
1121   PUTC(totlen & 0x00ff, failure);
1122   PUTC((proto & 0xff00) >> 8, failure);
1123   PUTC(proto & 0x00ff, failure);
1124
1125   while (len-- > 0) {
1126     PUTC(*buf++, failure);
1127   }
1128
1129   PRINTKN (3, (KERN_INFO "ppp: successfully queued %d bytes\n", totlen));
1130   clear_bit(1, &ppp->us_rbuff_lock);
1131   wake_up_interruptible (&ppp->read_wait);
1132
1133 #ifdef NEW_TTY_DRIVERS
1134   kill_fasync(ppp->tty->fasync, SIGIO);
1135 #endif
1136
1137   if (ppp->inp_sig && ppp->inp_sig_pid)
1138     if (kill_proc (ppp->inp_sig_pid, ppp->inp_sig, 1) != 0) {
1139       /* process is gone */
1140       PRINTKN (2,(KERN_NOTICE
1141                   "ppp: process that requested notification is gone\n"));
1142       ppp->inp_sig = 0;
1143       ppp->inp_sig_pid = 0;
1144     }
1145   return 1;
1146
1147  failure:
1148   ppp->us_rbuff_head = saved_head;
1149   clear_bit(1, &ppp->us_rbuff_lock);
1150
1151   PRINTKN (1, (KERN_NOTICE "ppp_us_queue: ran out of buffer space.\n"));
1152
1153   return 0;
1154 }
1155
1156 /*************************************************************
1157  * LINE DISCIPLINE SUPPORT
1158  *    The following functions form support user programs
1159  *    which read and write data on a TTY with the PPP line
1160  *    discipline.  Reading is done from a circular queue,
1161  *    filled by the lower TTY levels.
1162  *************************************************************/
1163
1164 /* read a PPP frame from the us_rbuff circular buffer, 
1165    waiting if necessary
1166 */
1167
1168 static int
1169 ppp_read(struct tty_struct *tty, struct file *file, unsigned char *buf, unsigned int nr)
1170 {
1171   struct ppp *ppp = ppp_find(tty);
1172   unsigned char c;
1173   int len, i;
1174
1175   if (!ppp || ppp->magic != PPP_MAGIC) {
1176     PRINTKN (1,(KERN_ERR "ppp_read: cannnot find ppp channel\n"));
1177     return -EIO;
1178   }
1179
1180   CHECK_PPP(-ENXIO);
1181
1182   PRINTKN (4,(KERN_DEBUG "ppp_read: called %x num %u\n",
1183               (unsigned int) buf,
1184               nr));
1185
1186   do {
1187     /* try to acquire read lock */
1188     if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1189       /* got lock */
1190       if (ppp->us_rbuff_head == ppp->us_rbuff_tail) {
1191         /* no data */
1192         PRINTKN (4,(KERN_DEBUG "ppp_read: no data\n"));
1193         clear_bit(0, &ppp->us_rbuff_lock);
1194         if (ppp->inp_sig) {
1195           PRINTKN (4,(KERN_DEBUG "ppp_read: EWOULDBLOCK\n"));
1196           return -EWOULDBLOCK;
1197         } else goto wait;
1198       }
1199
1200       /* reset the time of the last read operation */
1201       ppp->ddinfo.nip_rjiffies = jiffies;
1202
1203       GETC (c); len = c << 8; GETC (c); len += c;
1204
1205       PRINTKN (4,(KERN_DEBUG "ppp_read: len = %d\n", len));
1206
1207       if (len + 2 > nr) {
1208         /* frame too big; can't copy it, but do update us_rbuff_head */
1209         PRINTKN (1,(KERN_DEBUG
1210                     "ppp: read of %u bytes too small for %d frame\n",
1211                     nr, len+2));
1212         ppp->us_rbuff_head += len;
1213         if (ppp->us_rbuff_head > ppp->us_rbuff_end)
1214           ppp->us_rbuff_head += - (ppp->us_rbuff_end - ppp->us_rbuff);
1215         clear_bit(0, &ppp->us_rbuff_lock);
1216         wake_up_interruptible (&ppp->read_wait);
1217         ppp->stats.rgiants++;
1218         return -EOVERFLOW;              /* ZZZ; HACK! */
1219       } else {
1220         /* have the space: copy the packet, faking the first two bytes */
1221         put_fs_byte (PPP_ADDRESS, buf++);
1222         put_fs_byte (PPP_CONTROL, buf++);
1223         i = len;
1224         while (i-- > 0) {
1225           GETC (c);
1226           put_fs_byte (c, buf++);
1227         }
1228       }
1229
1230       clear_bit(0, &ppp->us_rbuff_lock);
1231       PRINTKN (3,(KERN_DEBUG "ppp_read: passing %d bytes up\n", len + 2));
1232       ppp->stats.rothers++;
1233       return len + 2;
1234     }
1235
1236     /* need to wait */
1237   wait:
1238     current->timeout = 0;
1239     PRINTKN (3,(KERN_DEBUG "ppp_read: sleeping\n"));
1240     interruptible_sleep_on (&ppp->read_wait);
1241     if (current->signal & ~current->blocked)
1242       return -EINTR;
1243   } while (1);
1244 }
1245
1246 /* stuff a character into the transmit buffer, using PPP's way of escaping
1247    special characters.
1248    also, update ppp->fcs to take account of new character */
1249 static inline void
1250 ppp_stuff_char(struct ppp *ppp, unsigned char c)
1251 {
1252   int curpt = ppp->xhead - ppp->xbuff;
1253   if ((curpt < 0) || (curpt > 3000)) {
1254     PRINTK ((KERN_DEBUG "ppp_stuff_char: %x %x %d\n",
1255              (unsigned int) ppp->xbuff, (unsigned int) ppp->xhead, curpt))
1256   }
1257   if (in_xmap (ppp, c)) {
1258     *ppp->xhead++ = PPP_ESC;
1259     *ppp->xhead++ = c ^ PPP_TRANS;
1260   } else
1261     *ppp->xhead++ = c;
1262   ppp->fcs = (ppp->fcs >> 8) ^ fcstab[(ppp->fcs ^ c) & 0xff];
1263 }
1264
1265 /* write a frame with NR chars from BUF to TTY
1266    we have to put the FCS field on ourselves
1267 */
1268
1269 static int
1270 ppp_write(struct tty_struct *tty, struct file *file, unsigned char *buf, unsigned int nr)
1271 {
1272   struct ppp *ppp = ppp_find(tty);
1273   int i;
1274
1275   if (!ppp || ppp->magic != PPP_MAGIC) {
1276     PRINTKN (1,(KERN_ERR "ppp_write: cannot find ppp unit\n"));
1277     return -EIO;
1278   }
1279
1280   CHECK_PPP(-ENXIO);
1281   
1282   if (ppp->mtu != ppp->dev->mtu)        /* Someone has been ifconfigging */
1283     ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru);
1284
1285   if (nr > ppp->mtu) {
1286     PRINTKN (1,(KERN_WARNING
1287                 "ppp_write: truncating user packet from %u to mtu %d\n",
1288                 nr, ppp->mtu));
1289     nr = ppp->mtu;
1290   }
1291
1292   if (ppp_debug >= 3)
1293     ppp_print_buffer ("write frame", buf, nr, USER_DS);
1294
1295   /* lock this PPP unit so we will be the only writer;
1296      sleep if necessary */
1297   while ((ppp->sending == 1) || !ppp_lock(ppp)) {
1298     current->timeout = 0;
1299     PRINTKN (3,(KERN_DEBUG "ppp_write: sleeping\n"));
1300     interruptible_sleep_on(&ppp->write_wait);
1301     if (current->signal & ~current->blocked)
1302       return -EINTR;
1303   }
1304
1305   /* OK, locked.  Stuff the given bytes into the buffer. */
1306
1307   PRINTKN(4,(KERN_DEBUG "ppp_write: acquired write lock\n"));
1308   ppp->xhead = ppp->xbuff;
1309
1310 #ifdef OPTIMIZE_FLAG_TIME
1311   if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1312     *ppp->xhead++ = PPP_FLAG;
1313   ppp->last_xmit = jiffies;
1314 #else      
1315   *ppp->xhead++ = PPP_FLAG;
1316 #endif
1317
1318   ppp->fcs = PPP_FCS_INIT;
1319   i = nr;
1320   while (i-- > 0)
1321     ppp_stuff_char(ppp,get_fs_byte(buf++));
1322
1323   ppp_add_fcs(ppp);             /* concatenate FCS at end */
1324
1325   *ppp->xhead++ = PPP_FLAG;
1326   
1327   /* reset the time of the last write operation */
1328   ppp->ddinfo.nip_sjiffies = jiffies;
1329
1330   if (ppp_debug >= 6)
1331     ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1332   else {
1333     PRINTKN (4,(KERN_DEBUG
1334                 "ppp_write: writing %d chars\n", ppp->xhead - ppp->xbuff));
1335   }
1336
1337   /* packet is ready-to-go */
1338   ++ppp->stats.sothers;
1339   ppp_kick_tty(ppp);
1340
1341   return((int)nr);
1342 }
1343  
1344 static int
1345 ppp_ioctl(struct tty_struct *tty, struct file *file, unsigned int i,
1346           unsigned long l)
1347 {
1348   struct ppp *ppp = ppp_find(tty);
1349   register int temp_i = 0;
1350   int error;
1351
1352   if (!ppp || ppp->magic != PPP_MAGIC) {
1353     PRINTK ((KERN_ERR "ppp_ioctl: can't find PPP block from tty!\n"))
1354     return -EBADF;
1355   }
1356
1357   CHECK_PPP(-ENXIO);
1358
1359   /* This must be root user */
1360   if (!suser())
1361     return -EPERM;
1362
1363   switch (i) {
1364   case PPPIOCSMRU:
1365     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1366     if (error == 0) {
1367       PRINTKN (3,(KERN_INFO "ppp_ioctl: set mru to %x\n", temp_i));
1368       temp_i = (int) get_fs_long (l);
1369       if (ppp->mru != temp_i)
1370         ppp_changedmtu (ppp, ppp->mtu, temp_i);
1371     }
1372     break;
1373
1374   case PPPIOCGFLAGS:
1375     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1376     if (error == 0) {
1377       temp_i = (ppp->flags & SC_MASK);
1378 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
1379       temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP;
1380 #endif
1381       put_fs_long ((long) temp_i, l);
1382       PRINTKN (3,(KERN_DEBUG "ppp_ioctl: get flags: addr %lx flags %x\n",
1383                   l,
1384                   temp_i));
1385     }
1386     break;
1387
1388   case PPPIOCSFLAGS:
1389     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1390     if (error == 0) {
1391       temp_i      = (int) get_fs_long (l);
1392       ppp->flags ^= ((ppp->flags ^ temp_i) & SC_MASK);
1393       PRINTKN (3,(KERN_INFO "ppp_ioctl: set flags to %x\n", temp_i));
1394     }
1395     break;
1396
1397   case PPPIOCGASYNCMAP:
1398     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1399     if (error == 0) {
1400       put_fs_long (ppp->xmit_async_map[0], l);
1401       PRINTKN (3,(KERN_INFO "ppp_ioctl: get asyncmap: addr %lx asyncmap %lx\n",
1402                   l, ppp->xmit_async_map[0]));
1403     }
1404     break;
1405
1406   case PPPIOCSASYNCMAP:
1407     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1408     if (error == 0) {
1409       memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
1410       ppp->xmit_async_map[0] = get_fs_long (l);
1411       bset (ppp->xmit_async_map, PPP_FLAG);
1412       bset (ppp->xmit_async_map, PPP_ESC);
1413       PRINTKN (3,(KERN_INFO "ppp_ioctl: set xmit asyncmap %lx\n",
1414                   ppp->xmit_async_map[0]));
1415     }
1416     break;
1417
1418   case PPPIOCRASYNCMAP:
1419     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1420     if (error == 0) {
1421       ppp->recv_async_map = get_fs_long (l);
1422       PRINTKN (3,(KERN_INFO "ppp_ioctl: set recv asyncmap %lx\n",
1423                   ppp->recv_async_map));
1424     }
1425     break;
1426
1427   case PPPIOCGUNIT:
1428     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1429     if (error == 0) {
1430       put_fs_long (ppp->dev->base_addr, l);
1431       PRINTKN (3,(KERN_INFO "ppp_ioctl: get unit: %d", ppp->dev->base_addr));
1432     }
1433     break;
1434
1435   case PPPIOCSINPSIG:
1436     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1437     if (error == 0) {
1438       ppp->inp_sig     = (int) get_fs_long (l);
1439       ppp->inp_sig_pid = current->pid;
1440       PRINTKN (3,(KERN_INFO "ppp_ioctl: set input signal %d\n", ppp->inp_sig));
1441     }
1442     break;
1443
1444   case PPPIOCSDEBUG:
1445     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1446     if (error == 0) {
1447       ppp_debug = (int) get_fs_long (l);
1448       ppp_debug_netpackets = (ppp_debug & 0xff00) >> 8;
1449       ppp_debug &= 0xff;
1450       PRINTKN (1, (KERN_INFO "ppp_ioctl: set debug level %d, netpacket %d\n", 
1451                    ppp_debug, ppp_debug_netpackets));
1452     }
1453     break;
1454
1455   case PPPIOCGDEBUG:
1456     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1457     if (error == 0) {
1458       put_fs_long ((long) (ppp_debug | (ppp_debug_netpackets << 8)), l);
1459       PRINTKN (3,(KERN_INFO "ppp_ioctl: get debug level %d\n", 
1460                   ppp_debug | (ppp_debug_netpackets << 8)));
1461     }
1462     break;
1463
1464   case PPPIOCGSTAT:
1465     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_stats));
1466     if (error == 0) {
1467       memcpy_tofs ((void *) l, &ppp->stats, sizeof (struct ppp_stats));
1468       PRINTKN (3,(KERN_INFO "ppp_ioctl: read statistics\n"));
1469     }
1470     break;
1471
1472   case PPPIOCGTIME:
1473     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_ddinfo));
1474     if (error == 0) {
1475       struct ppp_ddinfo cur_ddinfo;
1476       unsigned long cur_jiffies = jiffies;
1477
1478       /* change absolute times to relative times. */
1479       cur_ddinfo.ip_sjiffies  = cur_jiffies - ppp->ddinfo.ip_sjiffies;
1480       cur_ddinfo.ip_rjiffies  = cur_jiffies - ppp->ddinfo.ip_rjiffies;
1481       cur_ddinfo.nip_sjiffies = cur_jiffies - ppp->ddinfo.nip_sjiffies;
1482       cur_ddinfo.nip_rjiffies = cur_jiffies - ppp->ddinfo.nip_rjiffies;
1483       
1484       memcpy_tofs ((void *) l, &cur_ddinfo, sizeof (struct ppp_ddinfo));
1485       PRINTKN (3,(KERN_INFO "ppp_ioctl: read demand dial info\n"));
1486     }
1487     break;
1488
1489   case PPPIOCGXASYNCMAP:
1490     error = verify_area (VERIFY_WRITE,
1491                          (void *) l,
1492                          sizeof (ppp->xmit_async_map));
1493     if (error == 0) {
1494       memcpy_tofs ((void *) l,
1495                    ppp->xmit_async_map,
1496                    sizeof (ppp->xmit_async_map));
1497       PRINTKN (3,(KERN_INFO "ppp_ioctl: get xasyncmap: addr %lx\n", l));
1498     }
1499     break;
1500
1501   case PPPIOCSXASYNCMAP:
1502     error = verify_area (VERIFY_READ, (void *) l,
1503                          sizeof (ppp->xmit_async_map));
1504     if (error == 0) {
1505       unsigned long temp_tbl [8];
1506
1507       memcpy_fromfs (temp_tbl, (void *) l, sizeof (ppp->xmit_async_map));
1508       temp_tbl[1]  =  0x00000000; /* must not escape 0x20 - 0x3f */
1509       temp_tbl[2] &= ~0x40000000; /* must not escape 0x5e        */
1510       temp_tbl[3] |=  0x60000000; /* must escape 0x7d and 0x7e   */
1511
1512       if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
1513           (temp_tbl[4] & temp_tbl[5]) != 0 ||
1514           (temp_tbl[6] & temp_tbl[7]) != 0)
1515         error = -EINVAL;
1516       else {
1517         memcpy (ppp->xmit_async_map, temp_tbl, sizeof (ppp->xmit_async_map));
1518         PRINTKN (3,(KERN_INFO "ppp_ioctl: set xasyncmap\n"));
1519       }
1520     }
1521     break;
1522
1523   case PPPIOCSMAXCID:
1524     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1525     if (error == 0) {
1526       temp_i = (int) get_fs_long (l) + 1;
1527       PRINTKN (3,(KERN_INFO "ppp_ioctl: set maxcid to %d\n", temp_i));
1528       if (ppp->slcomp != NULL)
1529         slhc_free (ppp->slcomp);
1530
1531       ppp->slcomp = slhc_init (temp_i, temp_i);
1532
1533       if (ppp->slcomp == NULL) {
1534         PRINTKN (1,(KERN_ERR "ppp: no space for compression buffers!\n"));
1535         ppp_release (ppp);
1536         error = -ENOMEM;
1537       }
1538     }
1539     break;
1540
1541 #ifdef NEW_TTY_DRIVERS
1542     /* Allow stty to read, but not set, the serial port */
1543   case TCGETS:
1544   case TCGETA:
1545     error = n_tty_ioctl(tty, file, i, l);
1546     break;
1547 #endif
1548
1549 /*
1550  *  All other ioctl() events will come here.
1551  */
1552
1553   default:
1554     PRINTKN (1,(KERN_ERR "ppp_ioctl: invalid ioctl: %x, addr %lx\n",
1555                 i,
1556                 l));
1557 #ifdef NEW_TTY_DRIVERS
1558     error = -ENOIOCTLCMD;
1559 #else
1560     error = -EINVAL;
1561 #endif
1562     break;
1563   }
1564   return error;
1565 }
1566
1567 static int
1568 ppp_select (struct tty_struct *tty, struct inode * inode,
1569             struct file * filp, int sel_type, select_table * wait)
1570 {
1571   struct ppp *ppp = ppp_find (tty);
1572   
1573   if (!ppp || ppp->magic != PPP_MAGIC) {
1574     PRINTK ((KERN_ERR "ppp_select: can't find PPP block from tty!\n"))
1575     return -EBADF;
1576   }
1577   
1578   /* If the PPP protocol is no longer active, return false */
1579   CHECK_PPP (0);
1580   
1581   /* Process the request based upon the type desired */
1582   switch (sel_type) {
1583   case SEL_IN:
1584     if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1585       /* Test for the presence of data in the queue */
1586       if (ppp->us_rbuff_head != ppp->us_rbuff_tail) {
1587         clear_bit (0, &ppp->us_rbuff_lock);
1588         return 1;
1589       }
1590       clear_bit (0, &ppp->us_rbuff_lock);
1591     } /* fall through */
1592
1593   case SEL_EX:
1594     /* Is there a pending error condition? */
1595     if (tty->packet && tty->link->ctrl_status)
1596       return 1;
1597     
1598     /* closed? */
1599     if (tty->flags & (1 << TTY_SLAVE_CLOSED))
1600       return 1;
1601     
1602     /* If the tty is disconnected, then this is an exception too */
1603     if (tty_hung_up_p(filp))
1604       return 1;
1605
1606     select_wait (&ppp->read_wait, wait);
1607     break;
1608     
1609   case SEL_OUT:
1610     if (ppp_lock (ppp)) {
1611       if (ppp->sending == 0) {
1612         ppp_unlock (ppp);
1613         return 1;
1614       }
1615       ppp_unlock (ppp);
1616     }
1617     select_wait (&ppp->write_wait, wait);
1618     break;
1619   }
1620   return 0;
1621 }
1622
1623 /*************************************************************
1624  * NETWORK OUTPUT
1625  *    This routine accepts requests from the network layer
1626  *    and attempts to deliver the packets.
1627  *    It also includes various routines we are compelled to
1628  *    have to make the network layer work (arp, etc...).
1629  *************************************************************/
1630
1631 int
1632 ppp_xmit(struct sk_buff *skb, struct device *dev)
1633 {
1634   struct tty_struct *tty;
1635   struct ppp *ppp;
1636   unsigned char *p;
1637   unsigned short proto;
1638   int len;
1639
1640   /* just a little sanity check. */
1641   if (skb == NULL) {
1642     PRINTKN(3,(KERN_WARNING "ppp_xmit: null packet!\n"));
1643     return 0;
1644   }
1645
1646   /* Get pointers to the various components */
1647   ppp   = &ppp_ctrl[dev->base_addr];
1648   tty   = ppp->tty;
1649   p     = (unsigned char *) (skb + 1);
1650   len   = skb->len;
1651   proto = PROTO_IP;
1652
1653   PRINTKN(4,(KERN_DEBUG "ppp_xmit [%s]: skb %lX busy %d\n", dev->name, 
1654              (unsigned long int) skb, ppp->sending));
1655
1656   CHECK_PPP(0);
1657
1658   if (tty == NULL) {
1659     PRINTKN(1,(KERN_ERR "ppp_xmit: %s not connected to a TTY!\n", dev->name));
1660     goto done;
1661   }
1662
1663   if (!(dev->flags & IFF_UP)) {
1664     PRINTKN(1,(KERN_WARNING
1665                "ppp_xmit: packet sent on interface %s, which is down for IP\n",
1666                dev->name));
1667     goto done;
1668   }
1669
1670   /* get length from IP header as per Alan Cox bugfix for slip.c */
1671   if (len < sizeof(struct iphdr)) {
1672     PRINTKN(0,(KERN_ERR "ppp_xmit: given runt packet, ignoring\n"));
1673     return 1;
1674   }
1675   len = ntohs( ((struct iphdr *)(skb->data)) -> tot_len );
1676
1677   /* If doing demand dial then divert the first frame to pppd. */
1678   if (ppp->flags & SC_IP_DOWN) {
1679     if (ppp->flags & SC_IP_FLUSH == 0) {
1680       if (ppp_us_queue (ppp, proto, p, len))
1681         ppp->flags |= SC_IP_FLUSH;
1682     }
1683     goto done;
1684   }
1685
1686   /* Attempt to acquire send lock */
1687   if (ppp->sending || !ppp_lock(ppp)) {
1688     PRINTKN(3,(KERN_WARNING "ppp_xmit: busy\n"));
1689     ppp->stats.sbusy++;
1690     return 1;
1691   }
1692
1693   ppp->xhead = ppp->xbuff;
1694
1695   /* try to compress, if VJ compression mode is on */
1696   if (ppp->flags & SC_COMP_TCP) {
1697     /* NOTE: last 0 argument says never to compress connection ID */
1698     len = slhc_compress(ppp->slcomp, p, len, ppp->cbuff, &p, 0);
1699     if (p[0] & SL_TYPE_COMPRESSED_TCP)
1700       proto = PROTO_VJCOMP;
1701     else {
1702       if (p[0] >= SL_TYPE_UNCOMPRESSED_TCP) {
1703         proto = PROTO_VJUNCOMP;
1704         p[0] = (p[0] & 0x0f) | 0x40; 
1705       }
1706     }
1707   }
1708
1709   /* increment appropriate counter */
1710   if (proto == PROTO_VJCOMP)
1711     ++ppp->stats.scomp;
1712   else
1713     ++ppp->stats.suncomp;
1714       
1715   if (ppp_debug_netpackets) {
1716     struct iphdr *iph = (struct iphdr *) (skb + 1);
1717     PRINTK ((KERN_DEBUG "%s ==> proto %x len %d src %x dst %x proto %d\n",
1718             dev->name, (int) proto, (int) len, (int) iph->saddr,
1719             (int) iph->daddr, (int) iph->protocol))
1720   }
1721
1722   /* start of frame:   FLAG  ALL_STATIONS  CONTROL  <protohi> <protolo> */
1723 #ifdef OPTIMIZE_FLAG_TIME
1724   if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1725     *ppp->xhead++ = PPP_FLAG;
1726   ppp->last_xmit = jiffies;
1727 #else      
1728   *ppp->xhead++ = PPP_FLAG;
1729 #endif
1730
1731   ppp->fcs = PPP_FCS_INIT;
1732   if (!(ppp->flags & SC_COMP_AC)) { 
1733     ppp_stuff_char(ppp, PPP_ADDRESS);
1734     ppp_stuff_char(ppp, PPP_CONTROL);
1735   }
1736
1737   if (!(ppp->flags & SC_COMP_PROT) || (proto & 0xff00))
1738     ppp_stuff_char(ppp, proto>>8);
1739   ppp_stuff_char(ppp, proto&0xff);
1740
1741   /* data part */
1742   while (len-- > 0)
1743     ppp_stuff_char(ppp, *p++);
1744
1745   /* fcs and flag */
1746   ppp_add_fcs(ppp);
1747   *ppp->xhead++ = PPP_FLAG;
1748
1749   /* update the time for demand dial function */
1750   ppp->ddinfo.ip_sjiffies = jiffies;
1751
1752   /* send it! */
1753   if (ppp_debug >= 6)
1754     ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1755   else {
1756     PRINTKN (4,(KERN_DEBUG
1757                 "ppp_write: writing %d chars\n", ppp->xhead - ppp->xbuff));
1758   }
1759
1760   ppp_kick_tty(ppp);
1761
1762  done:
1763   if (skb->free) 
1764     kfree_skb(skb, FREE_WRITE);
1765   return 0;
1766 }
1767   
1768 static unsigned short
1769 ppp_type_trans (struct sk_buff *skb, struct device *dev)
1770 {
1771   return(htons(ETH_P_IP));
1772 }
1773
1774 #ifdef NET02D
1775 static int
1776 ppp_header(unsigned char *buff, struct device *dev, unsigned short type,
1777            unsigned long daddr, unsigned long saddr, unsigned len)
1778 {
1779   return(0);
1780 }
1781
1782 static int
1783 ppp_rebuild_header(void *buff, struct device *dev)
1784 {
1785   return(0);
1786 }
1787
1788 static void
1789 ppp_add_arp(unsigned long addr, struct sk_buff *skb, struct device *dev)
1790 {
1791 }
1792
1793 #else
1794
1795 static int
1796 ppp_header(unsigned char *buff, struct device *dev, unsigned short type,
1797            void *daddr, void *saddr, unsigned len, struct sk_buff *skb)
1798 {
1799   return(0);
1800 }
1801
1802 static int
1803 ppp_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
1804                    struct sk_buff *skb)
1805 {
1806   return(0);
1807 }
1808 #endif
1809
1810 static struct enet_statistics *
1811 ppp_get_stats (struct device *dev)
1812 {
1813   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1814   static struct enet_statistics ppp_stats;
1815
1816   ppp_stats.rx_packets = ppp->stats.rcomp + ppp->stats.runcomp;
1817   ppp_stats.rx_errors = ppp->stats.rerrors;
1818   ppp_stats.rx_dropped = ppp->stats.tossed;
1819   ppp_stats.rx_fifo_errors = 0;
1820   ppp_stats.rx_length_errors = ppp->stats.runts;
1821   ppp_stats.rx_over_errors = ppp->stats.roverrun;
1822   ppp_stats.rx_crc_errors = 0;
1823   ppp_stats.rx_frame_errors = 0;
1824   ppp_stats.tx_packets = ppp->stats.scomp + ppp->stats.suncomp;
1825   ppp_stats.tx_errors = ppp->stats.serrors;
1826   ppp_stats.tx_dropped = 0;
1827   ppp_stats.tx_fifo_errors = 0;
1828   ppp_stats.collisions = ppp->stats.sbusy;
1829   ppp_stats.tx_carrier_errors = 0;
1830   ppp_stats.tx_aborted_errors = 0;
1831   ppp_stats.tx_window_errors = 0;
1832   ppp_stats.tx_heartbeat_errors = 0;
1833
1834   PRINTKN (3, (KERN_INFO "ppp_get_stats called"));
1835   return &ppp_stats;
1836 }
1837
1838 /*************************************************************
1839  * UTILITIES
1840  *    Miscellany called by various functions above.
1841  *************************************************************/
1842
1843 #ifndef NEW_TTY_DRIVERS
1844 /* find a PPP channel given a TTY */
1845 struct ppp *
1846 ppp_find(struct tty_struct *tty)
1847 {
1848   int i;
1849   for (i = 0; i < PPP_NRUNIT; i++)
1850     if (ppp_ctrl[i].inuse && (ppp_ctrl[i].tty == tty)) return &ppp_ctrl[i];
1851
1852   return NULL;
1853 }
1854 #endif
1855
1856 /* allocate a PPP channel */
1857 static struct ppp *
1858 ppp_alloc(void)
1859 {
1860   int i;
1861   for (i = 0; i < PPP_NRUNIT; i++)
1862     if (!set_bit(0, &ppp_ctrl[i].inuse)) return &ppp_ctrl[i];
1863
1864   return NULL;
1865 }
1866
1867 /* marks a PPP interface 'busy'.  user processes will wait, if
1868    they try to write, and the network code will refrain from sending
1869    return nonzero if succeeded in acquiring lock
1870 */
1871
1872 static int
1873 ppp_lock(struct ppp *ppp)
1874 {
1875   int flags, locked;
1876   save_flags(flags);
1877   cli();
1878   locked = ppp->sending;
1879   ppp->sending = 1;
1880   if (ppp->dev->flags & IFF_UP)
1881     ppp->dev->tbusy = 1;
1882   restore_flags(flags);
1883   return locked == 0;
1884 }
1885
1886 static void
1887 ppp_unlock(struct ppp *ppp)
1888 {
1889   int flags;
1890   save_flags(flags);
1891   cli();
1892   ppp->sending = 0;
1893   if (ppp->dev->flags & IFF_UP)
1894     ppp->dev->tbusy = 0;
1895   restore_flags(flags);
1896 }
1897
1898 /* FCS support functions */
1899
1900 static void
1901 ppp_add_fcs(struct ppp *ppp)
1902 {
1903   unsigned short fcs = ppp->fcs;
1904
1905   fcs ^= 0xffff;
1906   ppp_stuff_char(ppp, fcs & 0x00ff);
1907   ppp_stuff_char(ppp, (fcs & 0xff00) >> 8);
1908   ASSERT (ppp->fcs == PPP_FCS_GOOD);
1909   PRINTKN (4,(KERN_DEBUG "ppp_add_fcs: fcs is %lx\n",
1910               (long) (unsigned long) fcs));
1911 }
1912
1913 static int
1914 ppp_check_fcs(struct ppp *ppp)
1915 {
1916   unsigned short fcs = PPP_FCS_INIT, msgfcs;
1917   unsigned char *c = ppp->rbuff;
1918   int i;
1919
1920   for (i = 0; i < ppp->rcount - 2; i++, c++)
1921     fcs = (fcs >> 8) ^ fcstab[(fcs ^ *c) & 0xff];
1922
1923   fcs ^= 0xffff;
1924   msgfcs = (c[1] << 8) + c[0];
1925   PRINTKN (4,(KERN_INFO "ppp_check_fcs: got %lx want %lx\n",
1926               (unsigned long) msgfcs, (unsigned long) fcs));
1927   return fcs == msgfcs;
1928 }
1929
1930 static char hex[] = "0123456789ABCDEF";
1931
1932 inline void ppp_print_hex (register char *out, char *in, int count);
1933 inline void ppp_print_hex (register char *out, char *in, int count)
1934 {
1935   register unsigned char next_ch;
1936
1937   while (count-- > 0) {
1938     next_ch = (unsigned char) get_fs_byte (in);
1939
1940     *out++  = hex[(next_ch >> 4) & 0x0F];
1941     *out++  = hex[next_ch        & 0x0F];
1942     ++out;
1943     ++in;
1944   }
1945 }
1946
1947 inline void ppp_print_char (register char *out, char *in, int count);
1948 inline void ppp_print_char (register char *out, char *in, int count)
1949 {
1950   register unsigned char next_ch;
1951
1952   while (count-- > 0) {
1953     next_ch = (unsigned char) get_fs_byte (in);
1954
1955     if (next_ch < 0x20 || next_ch > 0x7e)
1956       *out++ = '.';
1957     else {
1958       *out++ = next_ch;
1959       if (next_ch == '%')       /* printk/syslogd has a bug !! */
1960         *out++ = '%';
1961     }
1962     ++in;
1963   }
1964   *out = '\0';
1965 }
1966
1967 static void ppp_print_buffer(const char *name, char *buf, int count, int seg)
1968 {
1969   char line [44];
1970   int  old_fs = get_fs();
1971
1972   set_fs (seg);
1973
1974   if (name != (char *) NULL)
1975     PRINTK ((KERN_DEBUG "ppp: %s, count = %d\n", name, count));
1976
1977   while (count > 8) {
1978     memset         (line, ' ', sizeof (line));
1979     ppp_print_hex  (line, buf, 8);
1980     ppp_print_char (&line[8 * 3], buf, 8);
1981     PRINTK ((KERN_DEBUG "%s\n", line));
1982     count -= 8;
1983     buf   += 8;
1984   }
1985
1986   if (count > 0) {
1987     memset         (line, ' ', sizeof (line));
1988     ppp_print_hex  (line, buf, count);
1989     ppp_print_char (&line[8 * 3], buf, count);
1990     PRINTK ((KERN_DEBUG "%s\n", line));
1991   }
1992
1993   set_fs (old_fs);
1994 }