]> git.ozlabs.org Git - ppp.git/blob - linux/ppp.c
new demand-dial method with backup_tty;
[ppp.git] / linux / ppp.c
1 /*  PPP for Linux
2  *
3  *  Michael Callahan <callahan@maths.ox.ac.uk>
4  *  Al Longyear <longyear@netcom.com>
5  *
6  *  Dynamic PPP devices by Jim Freeman <jfree@caldera.com>.
7  *  ppp_tty_receive ``noisy-raise-bug'' fixed by Ove Ewerlid <ewerlid@syscon.uu.se>
8  *
9  *  ==FILEVERSION 960926==
10  *
11  *  NOTE TO MAINTAINERS:
12  *     If you modify this file at all, please set the number above to the
13  *     date of the modification as YYMMDD (year month day).
14  *     ppp.c is shipped with a PPP distribution as well as with the kernel;
15  *     if everyone increases the FILEVERSION number above, then scripts
16  *     can do the right thing when deciding whether to install a new ppp.c
17  *     file.  Don't change the format of that line otherwise, so the
18  *     installation script can recognize it.
19  */
20
21 /*
22    Sources:
23
24    slip.c
25
26    RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
27    Multi-protocol Datagrams over Point-to-Point Links
28
29    RFC1332: IPCP
30
31    ppp-2.0
32
33    Flags for this module (any combination is acceptable for testing.):
34
35    OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
36                         character. This is normally set to ((HZ * 3) / 2).
37                         This is 1.5 seconds. If zero then the leading
38                         flag is always sent.
39
40    CHECK_CHARACTERS   - Enable the checking on all received characters for
41                         8 data bits, no parity. This adds a small amount of
42                         processing for each received character.
43 */
44
45 #define OPTIMIZE_FLAG_TIME      ((HZ * 3)/2)
46
47 #define CHECK_CHARACTERS        1
48 #define PPP_COMPRESS            1
49
50 #ifndef PPP_MAX_DEV
51 #define PPP_MAX_DEV     256
52 #endif
53
54 /* $Id: ppp.c,v 1.9 1996/09/26 06:26:01 paulus Exp $
55  * Added dynamic allocation of channels to eliminate
56  *   compiled-in limits on the number of channels.
57  *
58  * Dynamic channel allocation code Copyright 1995 Caldera, Inc.,
59  *   released under the GNU General Public License Version 2.
60  */
61
62 #include <linux/module.h>
63 #include <linux/kernel.h>
64 #include <linux/sched.h>
65 #include <linux/types.h>
66 #include <linux/fcntl.h>
67 #include <linux/interrupt.h>
68 #include <linux/ptrace.h>
69 #include <linux/ioport.h>
70 #include <linux/in.h>
71 #include <linux/malloc.h>
72 #include <linux/tty.h>
73 #include <linux/errno.h>
74 #include <linux/sched.h>        /* to get the struct task_struct */
75 #include <linux/string.h>       /* used in new tty drivers */
76 #include <linux/signal.h>       /* used in new tty drivers */
77 #include <asm/system.h>
78 #include <asm/bitops.h>
79 #include <asm/segment.h>
80 #include <linux/if.h>
81 #include <linux/if_ether.h>
82 #include <linux/netdevice.h>
83 #include <linux/skbuff.h>
84 #include <linux/inet.h>
85 #include <linux/ioctl.h>
86
87 typedef struct sk_buff       sk_buff;
88 #define skb_data(skb)        ((__u8 *) (skb)->data)
89
90 #include <linux/ip.h>
91 #include <linux/tcp.h>
92 #include <linux/if_arp.h>
93 #include <net/slhc_vj.h>
94
95 #define fcstab  ppp_crc16_table         /* Name of the table in the kernel */
96 #include <linux/ppp_defs.h>
97
98 #include <linux/socket.h>
99 #include <linux/if_ppp.h>
100 #include <linux/if_pppvar.h>
101 #include <linux/ppp-comp.h>
102
103 #ifndef PPP_IPX
104 #define PPP_IPX 0x2b  /* IPX protocol over PPP */
105 #endif
106
107 #ifndef PPP_LQR
108 #define PPP_LQR 0xc025  /* Link Quality Reporting Protocol */
109 #endif
110
111 static int ppp_register_compressor (struct compressor *cp);
112 static void ppp_unregister_compressor (struct compressor *cp);
113
114 /*
115  * Local functions
116  */
117
118 static struct compressor *find_compressor (int type);
119 static void ppp_init_ctrl_blk (register struct ppp *);
120 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
121 static int ppp_doframe (struct ppp *);
122 static struct ppp *ppp_alloc (void);
123 static struct ppp *ppp_find (int pid_value);
124 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
125 extern inline void ppp_stuff_char (struct ppp *ppp,
126                                    register struct ppp_buffer *buf,
127                                    register __u8 chr);
128 extern inline int lock_buffer (register struct ppp_buffer *buf);
129
130 static int rcv_proto_ip         (struct ppp *, __u16, __u8 *, int);
131 static int rcv_proto_ipx        (struct ppp *, __u16, __u8 *, int);
132 static int rcv_proto_vjc_comp   (struct ppp *, __u16, __u8 *, int);
133 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
134 static int rcv_proto_unknown    (struct ppp *, __u16, __u8 *, int);
135 static int rcv_proto_lqr        (struct ppp *, __u16, __u8 *, int);
136 static void ppp_doframe_lower   (struct ppp *, __u8 *, int);
137 static int ppp_doframe          (struct ppp *);
138
139 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
140 static int  rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
141
142 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
143
144 #ifndef OPTIMIZE_FLAG_TIME
145 #define OPTIMIZE_FLAG_TIME      0
146 #endif
147
148 #ifndef PPP_MAX_DEV
149 #define PPP_MAX_DEV 256
150 #endif
151
152 /*
153  * Parameters which may be changed via insmod.
154  */
155
156 static int  flag_time = OPTIMIZE_FLAG_TIME;
157 static int  max_dev   = PPP_MAX_DEV;
158
159 /*
160  * The "main" procedure to the ppp device
161  */
162
163 int ppp_init (struct device *);
164
165 /*
166  * Network device driver callback routines
167  */
168
169 static int ppp_dev_open (struct device *);
170 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
171 static int ppp_dev_close (struct device *);
172 static int ppp_dev_xmit (sk_buff *, struct device *);
173 static struct enet_statistics *ppp_dev_stats (struct device *);
174 static int ppp_dev_header (sk_buff *, struct device *, __u16,
175                            void *, void *, unsigned int);
176 static int ppp_dev_rebuild (void *eth, struct device *dev,
177                             unsigned long raddr, struct sk_buff *skb);
178 /*
179  * TTY callbacks
180  */
181
182 static int ppp_tty_read (struct tty_struct *, struct file *, __u8 *,
183                          unsigned int);
184 static int ppp_tty_write (struct tty_struct *, struct file *, const __u8 *,
185                           unsigned int);
186 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
187                           unsigned long);
188 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
189                       struct file *filp, int sel_type, select_table * wait);
190 static int ppp_tty_open (struct tty_struct *);
191 static void ppp_tty_close (struct tty_struct *);
192 static int ppp_tty_room (struct tty_struct *tty);
193 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
194                              char *fp, int count);
195 static void ppp_tty_wakeup (struct tty_struct *tty);
196
197 #define CHECK_PPP(a)  if (!ppp->inuse) { printk (ppp_warning, __LINE__); return a;}
198 #define CHECK_PPP_VOID()  if (!ppp->inuse) { printk (ppp_warning, __LINE__); return;}
199
200 #define in_xmap(ppp,c)  (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
201 #define in_rmap(ppp,c)  ((((unsigned int) (__u8) (c)) < 0x20) && \
202                         ppp->recv_async_map & (1 << (c)))
203
204 #define bset(p,b)       ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
205
206 #define tty2ppp(tty)    ((struct ppp *) (tty->disc_data))
207 #define dev2ppp(dev)    ((struct ppp *) (dev->priv))
208 #define ppp2tty(ppp)    ((struct tty_struct *) ppp->tty)
209 #define ppp2dev(ppp)    ((struct device *) ppp->dev)
210
211 struct ppp_hdr {
212         __u8 address;
213         __u8 control;
214         __u8 protocol[2];
215 };
216
217 #define PPP_HARD_HDR_LEN        (sizeof (struct ppp_hdr))
218
219 typedef struct  ppp_ctrl {
220         struct ppp_ctrl *next;          /* Next structure in the list   */
221         char            name [8];       /* Name of the device           */
222         struct ppp      ppp;            /* PPP control table            */
223         struct device   dev;            /* Device information table     */
224 } ppp_ctrl_t;
225
226 static ppp_ctrl_t *ppp_list = NULL;
227
228 #define ctl2ppp(ctl) (struct ppp *)    &ctl->ppp
229 #define ctl2dev(ctl) (struct device *) &ctl->dev
230 #undef  PPP_NRUNIT
231
232 /* Buffer types */
233 #define BUFFER_TYPE_DEV_RD      0  /* ppp read buffer       */
234 #define BUFFER_TYPE_TTY_WR      1  /* tty write buffer      */
235 #define BUFFER_TYPE_DEV_WR      2  /* ppp write buffer      */
236 #define BUFFER_TYPE_TTY_RD      3  /* tty read buffer       */
237 #define BUFFER_TYPE_VJ          4  /* vj compression buffer */
238
239 /* Define this string only once for all macro invocations */
240 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
241
242 static char szVersion[]         = PPP_VERSION;
243  
244 /*
245  * Information for the protocol decoder
246  */
247
248 typedef int (*pfn_proto)  (struct ppp *, __u16, __u8 *, int);
249
250 typedef struct ppp_proto_struct {
251         int             proto;
252         pfn_proto       func;
253 } ppp_proto_type;
254
255 static
256 ppp_proto_type proto_list[] = {
257         { PPP_IP,         rcv_proto_ip         },
258         { PPP_IPX,        rcv_proto_ipx        },
259         { PPP_VJC_COMP,   rcv_proto_vjc_comp   },
260         { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
261         { PPP_LQR,        rcv_proto_lqr        },
262         { PPP_CCP,        rcv_proto_ccp        },
263         { 0,              rcv_proto_unknown    }  /* !!! MUST BE LAST !!! */
264 };
265
266 __u16 ppp_crc16_table[256] =
267 {
268         0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
269         0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
270         0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
271         0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
272         0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
273         0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
274         0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
275         0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
276         0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
277         0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
278         0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
279         0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
280         0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
281         0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
282         0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
283         0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
284         0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
285         0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
286         0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
287         0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
288         0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
289         0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
290         0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
291         0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
292         0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
293         0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
294         0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
295         0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
296         0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
297         0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
298         0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
299         0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
300 };
301
302 #ifdef CHECK_CHARACTERS
303 static __u32 paritytab[8] =
304 {
305         0x96696996, 0x69969669, 0x69969669, 0x96696996,
306         0x69969669, 0x96696996, 0x96696996, 0x69969669
307 };
308 #endif
309
310 /* local function to store a value into the LQR frame */
311 extern inline __u8 * store_long (register __u8 *p, register int value) {
312         *p++ = (__u8) (value >> 24);
313         *p++ = (__u8) (value >> 16);
314         *p++ = (__u8) (value >>  8);
315         *p++ = (__u8) value;
316         return p;
317 }
318
319 /*************************************************************
320  * INITIALIZATION
321  *************************************************************/
322
323 /* This procedure is called once and once only to define who we are to
324  * the operating system and the various procedures that it may use in
325  * accessing the ppp protocol.
326  */
327
328 static int
329 ppp_first_time (void)
330 {
331         static struct tty_ldisc ppp_ldisc;
332         int    status;
333
334         printk (KERN_INFO
335                 "PPP: version %s (demand dialling)"
336                 "\n", szVersion);
337
338 #ifndef MODULE /* slhc module logic has its own copyright announcement */
339         printk (KERN_INFO
340                 "TCP compression code copyright 1989 Regents of the "
341                 "University of California\n");
342 #endif
343         
344         printk (KERN_INFO
345                 "PPP Dynamic channel allocation code copyright 1995 "
346                 "Caldera, Inc.\n");
347 /*
348  * Register the tty discipline
349  */     
350         (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
351         ppp_ldisc.magic         = TTY_LDISC_MAGIC;
352         ppp_ldisc.open          = ppp_tty_open;
353         ppp_ldisc.close         = ppp_tty_close;
354         ppp_ldisc.read          = ppp_tty_read;
355         ppp_ldisc.write         = ppp_tty_write;
356         ppp_ldisc.ioctl         = ppp_tty_ioctl;
357         ppp_ldisc.select        = ppp_tty_select;
358         ppp_ldisc.receive_room  = ppp_tty_room;
359         ppp_ldisc.receive_buf   = ppp_tty_receive;
360         ppp_ldisc.write_wakeup  = ppp_tty_wakeup;
361         
362         status = tty_register_ldisc (N_PPP, &ppp_ldisc);
363         if (status == 0)
364                 printk (KERN_INFO "PPP line discipline registered.\n");
365         else
366                 printk (KERN_ERR "error registering line discipline: %d\n",
367                         status);
368         return status;
369 }
370
371 /*************************************************************
372  * INITIALIZATION
373  *************************************************************/
374
375 /* called when the device is actually created */
376
377 static int
378 ppp_init_dev (struct device *dev)
379 {
380         int    indx;
381
382         dev->hard_header      = ppp_dev_header;
383         dev->rebuild_header   = ppp_dev_rebuild;
384         dev->hard_header_len  = PPP_HARD_HDR_LEN;
385
386         /* device INFO */
387         dev->mtu              = PPP_MTU;
388         dev->hard_start_xmit  = ppp_dev_xmit;
389         dev->open             = ppp_dev_open;
390         dev->stop             = ppp_dev_close;
391         dev->get_stats        = ppp_dev_stats;
392         dev->do_ioctl         = ppp_dev_ioctl;
393         dev->addr_len         = 0;
394         dev->tx_queue_len     = 10;
395         dev->type             = ARPHRD_PPP;
396
397         for (indx = 0; indx < DEV_NUMBUFFS; indx++)
398                 skb_queue_head_init (&dev->buffs[indx]);
399
400         /* New-style flags */
401         dev->flags      = IFF_POINTOPOINT;
402         dev->family     = AF_INET;
403         dev->pa_addr    = 0;
404         dev->pa_brdaddr = 0;
405         dev->pa_mask    = 0;
406         dev->pa_alen    = 4; /* sizeof (__u32) */
407
408         return 0;
409 }
410
411 /*
412  * Local procedure to initialize the ppp structure
413  */
414
415 static void
416 ppp_init_ctrl_blk (register struct ppp *ppp)
417 {
418         ppp->magic  = PPP_MAGIC;
419         ppp->toss   = 0xE0;
420         ppp->escape = 0;
421
422         ppp->flags  = 0;
423         ppp->mtu    = PPP_MTU;
424         ppp->mru    = PPP_MRU;
425
426         memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
427         ppp->xmit_async_map[0] = 0xffffffff;
428         ppp->xmit_async_map[3] = 0x60000000;
429         ppp->recv_async_map    = 0x00000000;
430
431         ppp->rbuf       = NULL;
432         ppp->wbuf       = NULL;
433         ppp->ubuf       = NULL;
434         ppp->cbuf       = NULL;
435         ppp->slcomp     = NULL;
436         ppp->read_wait  = NULL;
437         ppp->write_wait = NULL;
438         ppp->last_xmit  = jiffies - flag_time;
439
440         /* clear statistics */
441         memset(&ppp->stats, 0, sizeof (struct pppstat));
442         memset(&ppp->estats, 0, sizeof(struct enet_statistics));
443
444         /* Reset the demand dial information */
445         ppp->ddinfo.xmit_idle=         /* time since last NP packet sent */
446         ppp->ddinfo.recv_idle=jiffies; /* time since last NP packet received */
447
448         /* PPP compression data */
449         ppp->sc_xc_state =
450         ppp->sc_rc_state = NULL;
451 }
452
453 static struct symbol_table ppp_syms = {
454 #include <linux/symtab_begin.h>
455         X(ppp_register_compressor),
456         X(ppp_unregister_compressor),
457         X(ppp_crc16_table),
458 #include <linux/symtab_end.h>
459 };
460
461 /* called at boot/load time for each ppp device defined in the kernel */
462
463 #ifndef MODULE
464 int
465 ppp_init (struct device *dev)
466 {
467         static int first_time = 1;
468         int    answer = 0;
469
470         if (first_time) {
471                 first_time = 0;
472                 answer     = ppp_first_time();
473                 if (answer == 0)
474                         (void) register_symtab (&ppp_syms);
475         }
476         if (answer == 0)
477                 answer = -ENODEV;
478         return answer;
479 }
480 #endif
481
482 /*
483  * Routine to allocate a buffer for later use by the driver.
484  */
485
486 static struct ppp_buffer *
487 ppp_alloc_buf (int size, int type)
488 {
489         struct ppp_buffer *buf;
490
491         buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
492                                              GFP_ATOMIC);
493
494         if (buf != NULL) {
495                 buf->size   = size - 1; /* Mask for the buffer size */
496                 buf->type   = type;
497                 buf->locked = 0;
498                 buf->count  = 0;
499                 buf->head   = 0;
500                 buf->tail   = 0;
501                 buf->fcs    = PPP_INITFCS;
502
503         }
504         return (buf);
505 }
506
507 /*
508  * Routine to release the allocated buffer.
509  */
510
511 static void
512 ppp_free_buf (struct ppp_buffer *ptr)
513 {
514         if (ptr != NULL)
515                 kfree (ptr);
516 }
517
518 /*
519  * Lock the indicated transmit buffer
520  */
521
522 extern inline int
523 lock_buffer (register struct ppp_buffer *buf)
524 {
525         register int state;
526         unsigned long flags;
527 /*
528  * Save the current state and if free then set it to the "busy" state
529  */
530         save_flags (flags);
531         cli ();
532         state = buf->locked;
533         if (state == 0)
534                 buf->locked = 2;
535
536         restore_flags (flags);
537         return (state);
538 }
539
540 /*
541  * MTU has been changed by the IP layer. Unfortunately we are not told
542  * about this, but we spot it ourselves and fix things up. We could be
543  * in an upcall from the tty driver, or in an ip packet queue.
544  */
545
546 static int
547 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
548 {
549         struct device *dev;
550
551         struct ppp_buffer *new_rbuf;
552         struct ppp_buffer *new_wbuf;
553         struct ppp_buffer *new_cbuf;
554         struct ppp_buffer *new_tbuf;
555
556         struct ppp_buffer *old_rbuf;
557         struct ppp_buffer *old_wbuf;
558         struct ppp_buffer *old_cbuf;
559         struct ppp_buffer *old_tbuf;
560
561         int mtu, mru;
562 /*
563  *  Allocate the buffer from the kernel for the data
564  */
565         dev = ppp2dev (ppp);
566         mru = new_mru;
567         /* allow for possible escaping of every character */
568         mtu = (new_mtu * 2) + 20;
569
570         /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
571         if (mru < PPP_MRU)
572                 mru = PPP_MRU;
573
574         mru += 10;
575
576         new_wbuf = ppp_alloc_buf (mtu+PPP_HARD_HDR_LEN, BUFFER_TYPE_DEV_WR);
577         new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24,   BUFFER_TYPE_TTY_WR);
578         new_rbuf = ppp_alloc_buf (mru + 84,             BUFFER_TYPE_DEV_RD);
579         new_cbuf = ppp_alloc_buf (mru+PPP_HARD_HDR_LEN, BUFFER_TYPE_VJ);
580 /*
581  *  If the buffers failed to allocate then complain and release the partial
582  *  allocations.
583  */
584         if (new_wbuf == NULL || new_tbuf == NULL ||
585             new_rbuf == NULL || new_cbuf == NULL) {
586                 if (ppp->flags & SC_DEBUG)
587                         printk (KERN_ERR
588                                 "ppp: failed to allocate new buffers\n");
589
590                 ppp_free_buf (new_wbuf);
591                 ppp_free_buf (new_tbuf);
592                 ppp_free_buf (new_rbuf);
593                 ppp_free_buf (new_cbuf);
594                 return 0;
595         }
596 /*
597  *  Update the pointers to the new buffer structures.
598  */
599         cli ();
600         old_wbuf = ppp->wbuf;
601         old_rbuf = ppp->rbuf;
602         old_cbuf = ppp->cbuf;
603         old_tbuf = ppp->tbuf;
604
605         ppp->wbuf = new_wbuf;
606         ppp->rbuf = new_rbuf;
607         ppp->cbuf = new_cbuf;
608         ppp->tbuf = new_tbuf;
609
610         ppp->rbuf->size -= 80;  /* reserve space for vj header expansion */
611
612         dev->mem_start  = (unsigned long) buf_base (new_wbuf);
613         dev->mem_end    = (unsigned long) (dev->mem_start + mtu);
614         dev->rmem_start = (unsigned long) buf_base (new_rbuf);
615         dev->rmem_end   = (unsigned long) (dev->rmem_start + mru);
616 /*
617  *  Update the parameters for the new buffer sizes
618  */
619         ppp->toss   = 0xE0;     /* To ignore characters until new FLAG */
620         ppp->escape = 0;        /* No pending escape character */
621
622         dev->mtu    =
623         ppp->mtu    = new_mtu;
624         ppp->mru    = new_mru;
625
626         ppp->s1buf  = NULL;
627         ppp->s2buf  = NULL;
628         ppp->xbuf   = NULL;
629
630         ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
631         ppp->flags      &= ~SC_XMIT_BUSY;
632
633         sti ();
634 /*
635  *  Release old buffer pointers
636  */
637         ppp_free_buf (old_rbuf);
638         ppp_free_buf (old_wbuf);
639         ppp_free_buf (old_cbuf);
640         ppp_free_buf (old_tbuf);
641         return 1;
642 }
643
644 /*
645  * CCP is down; free (de)compressor state if necessary.
646  */
647
648 static void
649 ppp_ccp_closed (struct ppp *ppp)
650 {
651         if (ppp->sc_xc_state) {
652                 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
653                 ppp->sc_xc_state = NULL;
654         }
655
656         if (ppp->sc_rc_state) {
657                 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
658                 ppp->sc_rc_state = NULL;
659         }
660 }
661
662 /*
663  * Called to release all of the information in the current PPP structure.
664  *
665  * It is called when the ppp device goes down or if it is unable to go
666  * up.
667  */
668
669 static void
670 ppp_release (struct ppp *ppp)
671 {
672         struct tty_struct *tty;
673         struct device *dev;
674
675         tty = ppp2tty (ppp);
676         dev = ppp2dev (ppp);
677
678         ppp_ccp_closed (ppp);
679
680         /* Ensure that the pppd process is not hanging on select() */
681         wake_up_interruptible (&ppp->read_wait);
682         wake_up_interruptible (&ppp->write_wait);
683
684         if (tty != NULL && tty->disc_data == ppp)
685                 tty->disc_data = NULL;  /* Break the tty->ppp link */
686
687         if (dev && dev->flags & IFF_UP) {
688                 dev_close (dev); /* close the device properly */
689                 dev->flags = 0;  /* prevent recursion */
690         }
691
692         ppp_free_buf (ppp->rbuf);
693         ppp_free_buf (ppp->wbuf);
694         ppp_free_buf (ppp->cbuf);
695         ppp_free_buf (ppp->ubuf);
696         ppp_free_buf (ppp->tbuf);
697
698         ppp->rbuf  =
699         ppp->wbuf  =
700         ppp->cbuf  =
701         ppp->tbuf  =
702         ppp->xbuf  =
703         ppp->s1buf =
704         ppp->s2buf =
705         ppp->ubuf  = NULL;
706
707         if (ppp->slcomp) {
708                 slhc_free (ppp->slcomp);
709                 ppp->slcomp = NULL;
710         }
711
712         ppp->inuse = 0;
713         ppp->tty   = NULL;
714         ppp->backup_tty = NULL;
715 }
716
717 /*
718  * TTY callback.
719  *
720  * Called when the line discipline is changed to something
721  * else, the tty is closed, or the tty detects a hangup.
722  */
723
724 static void
725 ppp_tty_close (struct tty_struct *tty)
726 {
727         struct ppp *ppp = tty2ppp (tty);
728
729         if (ppp != NULL) {
730                 if (ppp->magic != PPP_MAGIC) {
731                         if (ppp->flags & SC_DEBUG)
732                                 printk (KERN_WARNING
733                                        "ppp: trying to close unopened tty!\n");
734                         return;
735                 }
736                 CHECK_PPP_VOID();
737                 tty->disc_data = NULL;
738                 if (tty == ppp->backup_tty)
739                         ppp->backup_tty = 0;
740                 if (tty != ppp->tty)
741                         return;
742                 if (ppp->backup_tty) {
743                         ppp->tty = ppp->backup_tty;
744                 } else {
745                         ppp->sc_xfer = 0;
746                         if (ppp->flags & SC_DEBUG)
747                                 printk (KERN_INFO "ppp: channel %s closing.\n",
748                                         ppp2dev(ppp)->name);
749                         ppp_release (ppp);
750                         MOD_DEC_USE_COUNT;
751                 }
752         }
753 }
754
755 /*
756  * TTY callback.
757  *
758  * Called when the tty discipline is switched to PPP.
759  */
760
761 static int
762 ppp_tty_open (struct tty_struct *tty)
763 {
764         struct ppp *ppp = tty2ppp (tty);
765         int indx;
766 /*
767  * There should not be an existing table for this slot.
768  */
769         if (ppp) {
770                 if (ppp->flags & SC_DEBUG)
771                         printk (KERN_ERR
772                         "ppp_tty_open: gack! tty already associated to %s!\n",
773                         ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
774                                                 : "unknown");
775                 return -EEXIST;
776         }
777 /*
778  * Allocate the structure from the system
779  */
780         ppp = ppp_find(current->pid);
781         if (ppp != NULL) {
782                 /*
783                  * If we are taking over a ppp unit which is currently
784                  * connected to a loopback pty, there's not much to do.
785                  */
786                 ppp->tty       = tty;
787                 tty->disc_data = ppp;
788
789         } else {
790                 ppp = ppp_alloc();
791                 if (ppp == NULL) {
792                         if (ppp->flags & SC_DEBUG)
793                                 printk (KERN_ERR "ppp_alloc failed\n");
794                         return -ENFILE;
795                 }
796 /*
797  * Initialize the control block
798  */
799                 ppp_init_ctrl_blk (ppp);
800                 ppp->tty       = tty;
801                 tty->disc_data = ppp;
802 /*
803  * Allocate space for the default VJ header compression slots
804  */
805                 ppp->slcomp = slhc_init (16, 16);
806                 if (ppp->slcomp == NULL) {
807                         if (ppp->flags & SC_DEBUG)
808                                 printk (KERN_ERR "ppp_tty_open: "
809                                         "no space for compression buffers!\n");
810                         ppp_release (ppp);
811                         return -ENOMEM;
812                 }
813 /*
814  * Allocate space for the MTU and MRU buffers
815  */
816                 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
817                         ppp_release (ppp);
818                         return -ENOMEM;
819                 }
820 /*
821  * Allocate space for a user level buffer
822  */
823                 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
824                 if (ppp->ubuf == NULL) {
825                         if (ppp->flags & SC_DEBUG)
826                                 printk (KERN_ERR "ppp_tty_open: "
827                                         "no space for user receive buffer\n");
828                         ppp_release (ppp);
829                         return -ENOMEM;
830                 }
831
832                 if (ppp->flags & SC_DEBUG)
833                         printk (KERN_INFO "ppp: channel %s open\n",
834                                 ppp2dev(ppp)->name);
835
836                 for (indx = 0; indx < NUM_NP; ++indx)
837                         ppp->sc_npmode[indx] = NPMODE_PASS;
838
839                 MOD_INC_USE_COUNT;
840         }
841 /*
842  * Flush any pending characters in the driver and discipline.
843  */
844         if (tty->ldisc.flush_buffer)
845                 tty->ldisc.flush_buffer (tty);
846
847         if (tty->driver.flush_buffer)
848                 tty->driver.flush_buffer (tty);
849         return (ppp->line);
850 }
851
852 /*
853  * Local function to send the next portion of the buffer.
854  *
855  * Called by the tty driver's tty_wakeup function should it be entered
856  * because the partial buffer was transmitted.
857  *
858  * Called by kick_tty to send the initial portion of the buffer.
859  *
860  * Completion processing of the buffer transmission is handled here.
861  */
862
863 static void
864 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
865                      struct ppp_buffer *xbuf)
866 {
867         register int count, actual;
868 /*
869  * Prevent re-entrancy by ensuring that this routine is called only once.
870  */
871         cli ();
872         if (ppp->flags & SC_XMIT_BUSY) {
873                 sti ();
874                 return;
875         }
876         ppp->flags |= SC_XMIT_BUSY;
877         sti ();
878 /*
879  * Send the next block of data to the modem
880  */
881         count = xbuf->count - xbuf->tail;
882         actual = tty->driver.write (tty, 0,
883                                     buf_base (xbuf) + xbuf->tail, count);
884 /*
885  * Terminate transmission of any block which may have an error.
886  * This could occur should the carrier drop.
887  */
888         if (actual < 0) {
889                 ppp->stats.ppp_oerrors++;
890                 actual = count;
891         } else
892                 ppp->bytes_sent += actual;
893 /*
894  * If the buffer has been transmitted then clear the indicators.
895  */
896         xbuf->tail += actual;
897         if (actual == count) {
898                 xbuf = NULL;
899                 ppp->flags &= ~SC_XMIT_BUSY;
900 /*
901  * Complete the transmission on the current buffer.
902  */
903                 xbuf = ppp->xbuf;
904                 if (xbuf != NULL) {
905                         tty->flags  &= ~(1 << TTY_DO_WRITE_WAKEUP);
906                         xbuf->locked = 0;
907                         ppp->xbuf    = NULL;
908 /*
909  * If the completed buffer came from the device write, then complete the
910  * transmission block.
911  */
912                         if (ppp2dev (ppp) -> flags & IFF_UP) {
913                                 if (xbuf->type == BUFFER_TYPE_DEV_WR)
914                                         ppp2dev (ppp)->tbusy = 0;
915                                 mark_bh (NET_BH);
916                         }
917 /*
918  * Wake up the transmission queue for all completion events.
919  */
920                         wake_up_interruptible (&ppp->write_wait);
921 /*
922  * Look at the priorities. Choose a daemon write over the device driver.
923  */
924                         cli();
925                         xbuf = ppp->s1buf;
926                         ppp->s1buf = NULL;
927                         if (xbuf == NULL) {
928                                 xbuf = ppp->s2buf;
929                                 ppp->s2buf = NULL;
930                         }
931                         sti();
932 /*
933  * If there is a pending buffer then transmit it now.
934  */
935                         if (xbuf != NULL) {
936                                 ppp->flags &= ~SC_XMIT_BUSY;
937                                 ppp_kick_tty (ppp, xbuf);
938                                 return;
939                         }
940                 }
941         }
942 /*
943  * Clear the re-entry flag
944  */
945         ppp->flags &= ~SC_XMIT_BUSY;
946 }
947
948 /*
949  * This function is called by the tty driver when the transmit buffer has
950  * additional space. It is used by the ppp code to continue to transmit
951  * the current buffer should the buffer have been partially sent.
952  *
953  * In addition, it is used to send the first part of the buffer since the
954  * logic and the inter-locking would be identical.
955  */
956
957 static void
958 ppp_tty_wakeup (struct tty_struct *tty)
959 {
960         struct ppp_buffer *xbuf;
961         struct ppp *ppp = tty2ppp (tty);
962
963         if (!ppp)
964                 return;
965
966         if (ppp->magic != PPP_MAGIC)
967                 return;
968
969         if (tty != ppp->tty) {
970                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
971                 return;
972         }
973 /*
974  * Ensure that there is a transmission pending. Clear the re-entry flag if
975  * there is no pending buffer. Otherwise, send the buffer.
976  */
977         xbuf = ppp->xbuf;
978         if (xbuf == NULL)
979                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
980         else
981                 ppp_tty_wakeup_code (ppp, tty, xbuf);
982 }
983
984 /*
985  * This function is called to transmit a buffer to the remote. The buffer
986  * is placed on the pending queue if there is presently a buffer being
987  * sent or it is transmitted with the aid of ppp_tty_wakeup.
988  */
989
990 static void
991 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
992 {
993         unsigned long flags;
994 /*
995  * Hold interrupts.
996  */
997         save_flags (flags);
998         cli ();
999 /*
1000  * Control the flags which are best performed with the interrupts masked.
1001  */
1002         xbuf->locked     = 1;
1003         xbuf->tail       = 0;
1004 /*
1005  * If the transmitter is busy then place the buffer on the appropriate
1006  * priority queue.
1007  */
1008         if (ppp->xbuf != NULL) {
1009                 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1010                         ppp->s1buf = xbuf;
1011                 else
1012                         ppp->s2buf = xbuf;
1013                 restore_flags (flags);
1014                 return;
1015         }
1016 /*
1017  * If the transmitter is not busy then this is the highest priority frame
1018  */
1019         ppp->flags      &= ~SC_XMIT_BUSY;
1020         ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1021         ppp->xbuf        = xbuf;
1022         restore_flags (flags);
1023 /*
1024  * Do the "tty wakeup_code" to actually send this buffer.
1025  */
1026         ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1027 }
1028
1029 /*************************************************************
1030  * TTY INPUT
1031  *    The following functions handle input that arrives from
1032  *    the TTY.  It recognizes PPP frames and either hands them
1033  *    to the network layer or queues them for delivery to a
1034  *    user process reading this TTY.
1035  *************************************************************/
1036
1037 /*
1038  * Callback function from tty driver. Return the amount of space left
1039  * in the receiver's buffer to decide if remote transmitter is to be
1040  * throttled.
1041  */
1042
1043 static int
1044 ppp_tty_room (struct tty_struct *tty)
1045 {
1046         return 65536;       /* We can handle an infinite amount of data. :-) */
1047 }
1048
1049 /*
1050  * Callback function when data is available at the tty driver.
1051  */
1052 static void
1053 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1054                  char *flags, int count)
1055 {
1056         register struct ppp *ppp = tty2ppp (tty);
1057         register struct ppp_buffer *buf = NULL;
1058         __u8 chr;
1059
1060         /*
1061          * This can happen if stuff comes in on the backup tty.
1062          */
1063         if (tty != ppp->tty)
1064                 return;
1065 /*
1066  * Fetch the pointer to the buffer. Be careful about race conditions.
1067  */
1068         if (ppp != NULL)
1069                 buf = ppp->rbuf;
1070
1071         if (buf == NULL)
1072                 return;
1073 /*
1074  * Verify the table pointer and ensure that the line is
1075  * still in PPP discipline.
1076  */
1077         if (ppp->magic != PPP_MAGIC) {
1078                 if (ppp->flags & SC_DEBUG)
1079                         printk (KERN_DEBUG
1080                                 "PPP: handler called but couldn't find "
1081                                 "PPP struct.\n");
1082                 return;
1083         }
1084         CHECK_PPP_VOID ();
1085 /*
1086  * Print the buffer if desired
1087  */
1088         if (ppp->flags & SC_LOG_RAWIN)
1089                 ppp_print_buffer ("receive buffer", data, count);
1090 /*
1091  * Collect the character and error condition for the character. Set the toss
1092  * flag for the first character error.
1093  */
1094         while (count-- > 0) {
1095                 ppp->bytes_rcvd++;
1096                 chr = *data++;
1097                 if (flags) {
1098                         if (*flags && ppp->toss == 0) {
1099                                 ppp->toss = *flags;
1100                                 switch (ppp->toss) {
1101                                 case TTY_OVERRUN:
1102                                         ++ppp->estats.rx_fifo_errors;
1103                                         break;
1104                                 case TTY_FRAME:
1105                                 case TTY_BREAK:
1106                                         ++ppp->estats.rx_frame_errors;
1107                                         break;
1108                                 }
1109                         }
1110                         ++flags;
1111                 }
1112 /*
1113  * Set the flags for d7 being 0/1 and parity being even/odd so that
1114  * the normal processing would have all flags set at the end of the
1115  * session.  A missing flag bit indicates an error condition.
1116  */
1117
1118 #ifdef CHECK_CHARACTERS
1119                 if (chr & 0x80)
1120                         ppp->flags |= SC_RCV_B7_1;
1121                 else
1122                         ppp->flags |= SC_RCV_B7_0;
1123
1124                 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1125                         ppp->flags |= SC_RCV_ODDP;
1126                 else
1127                         ppp->flags |= SC_RCV_EVNP;
1128 #endif
1129 /*
1130  * Branch on the character.
1131  */
1132                 switch (chr) {
1133 /*
1134  * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1135  * then the block is to be ignored. In addition, characters before the very
1136  * first FLAG are also tossed by this procedure.
1137  */
1138                 case PPP_FLAG:  /* PPP_FLAG: end of frame */
1139                         ppp->stats.ppp_ibytes += ppp->rbuf->count;
1140                         if (ppp->escape)
1141                                 ppp->toss |= 0x80;
1142 /*
1143  * Process frames which are not to be ignored. If the processing failed,
1144  * then clean up the VJ tables.
1145  */
1146                         if (ppp_doframe (ppp) == 0) {
1147                                 ++ppp->stats.ppp_ierrors;
1148                                 slhc_toss (ppp->slcomp);
1149                         }
1150 /*
1151  * Reset all indicators for the new frame to follow.
1152  */
1153                         buf->count  = 0;
1154                         buf->fcs    = PPP_INITFCS;
1155                         ppp->escape = 0;
1156                         ppp->toss   = 0;
1157                         break;
1158 /*
1159  * All other characters in the data come here. If the character is in the
1160  * receive mask then ignore the character.
1161  */
1162                 default:
1163                         /* If we're tossing, look no further. */
1164                         if (ppp->toss != 0)
1165                                 break;
1166
1167                         /* If this is a control char to be ignored, do so */
1168                         if (in_rmap (ppp, chr))
1169                                 break;
1170
1171                         /*
1172                          * Modify the next character if preceded by escape.
1173                          * The escape character (0x7d) could be an escaped
1174                          * 0x5d, if it follows an escape :-)
1175                          */
1176                         if (ppp->escape) {
1177                                 chr ^= ppp->escape;
1178                                 ppp->escape = 0;
1179                         } else if (chr == PPP_ESCAPE) {
1180                                 ppp->escape = PPP_TRANS;
1181                                 break;
1182                         }
1183
1184 /*
1185  * If the count sent is within reason then store the character, bump the
1186  * count, and update the FCS for the character.
1187  */
1188                         if (buf->count < buf->size) {
1189                                 buf_base (buf)[buf->count++] = chr;
1190                                 buf->fcs = PPP_FCS (buf->fcs, chr);
1191                                 break;
1192                         }
1193 /*
1194  * The peer sent too much data. Set the flags to discard the current frame
1195  * and wait for the re-synchronization FLAG to be sent.
1196  */
1197                         ++ppp->estats.rx_length_errors;
1198                         ppp->toss |= 0xC0;
1199                         break;
1200                 }
1201         }
1202 }
1203
1204 /*
1205  * Put the input frame into the networking system for the indicated protocol
1206  */
1207
1208 static int
1209 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1210 {
1211         sk_buff *skb = dev_alloc_skb (count);
1212 /*
1213  * Generate a skb buffer for the new frame.
1214  */
1215         if (skb == NULL) {
1216                 if (ppp->flags & SC_DEBUG)
1217                         printk (KERN_ERR
1218                          "ppp_do_ip: packet dropped on %s (no memory)!\n",
1219                          ppp2dev (ppp)->name);
1220                 return 0;
1221         }
1222 /*
1223  * Move the received data from the input buffer to the skb buffer.
1224  */
1225         skb->dev      = ppp2dev (ppp);  /* We are the device */
1226         skb->protocol = proto;
1227         skb->mac.raw  = skb_data(skb);
1228         memcpy (skb_put(skb,count), data, count);       /* move data */
1229 /*
1230  * Tag the frame and kick it to the proper receive routine
1231  */
1232         skb->free = 1;
1233         ppp->ddinfo.recv_idle = jiffies;
1234         netif_rx (skb);
1235         return 1;
1236 }
1237
1238 /*
1239  * Process the receipt of an IP frame
1240  */
1241
1242 static int
1243 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1244 {
1245         if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1246                 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1247                         return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1248         return 0;
1249 }
1250
1251 /*
1252  * Process the receipt of an IPX frame
1253  */
1254
1255 static int
1256 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1257 {
1258         if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1259                 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1260         return 0;
1261 }
1262
1263 /*
1264  * Process the receipt of an VJ Compressed frame
1265  */
1266
1267 static int
1268 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1269                     __u8 *data, int count)
1270 {
1271         if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1272                 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1273                 if (new_count >= 0) {
1274                         return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1275                 }
1276                 if (ppp->flags & SC_DEBUG)
1277                         printk (KERN_NOTICE
1278                                 "ppp: error in VJ decompression\n");
1279         }
1280         return 0;
1281 }
1282
1283 /*
1284  * Process the receipt of an VJ Un-compressed frame
1285  */
1286
1287 static int
1288 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1289                       __u8 *data, int count)
1290 {
1291         if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1292                 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1293                         return rcv_proto_ip (ppp, PPP_IP, data, count);
1294                 }
1295                 if (ppp->flags & SC_DEBUG)
1296                         printk (KERN_NOTICE
1297                                 "ppp: error in VJ memorizing\n");
1298         }
1299         return 0;
1300 }
1301
1302 /*
1303  * Receive all unclassified protocols.
1304  */
1305
1306 static int
1307 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1308                    __u8 *data, int len)
1309 {
1310         int totlen;
1311         register int current_idx;
1312
1313 #define PUTC(c)                                          \
1314 {                                                        \
1315     buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1316     current_idx &= ppp->ubuf->size;                      \
1317     if (current_idx == ppp->ubuf->tail)                  \
1318             goto failure;                                \
1319 }
1320
1321 /*
1322  * The total length includes the protocol data.
1323  * Lock the user information buffer.
1324  */
1325         if (set_bit (0, &ppp->ubuf->locked)) {
1326                 if (ppp->flags & SC_DEBUG)
1327                         printk (KERN_DEBUG
1328                                 "ppp_us_queue: can't get lock\n");
1329         } else {
1330                 current_idx = ppp->ubuf->head;
1331 /*
1332  * Insert the buffer length (not counted), the protocol, and the data
1333  */
1334                 totlen = len + 2;
1335                 PUTC (totlen >> 8);
1336                 PUTC (totlen);
1337
1338                 PUTC (proto >> 8);
1339                 PUTC (proto);
1340
1341                 totlen -= 2;
1342                 while (totlen-- > 0) {
1343                         PUTC (*data++);
1344                 }
1345 #undef PUTC
1346 /*
1347  * The frame is complete. Update the head pointer and wakeup the pppd
1348  * process.
1349  */
1350                 ppp->ubuf->head = current_idx;
1351
1352                 clear_bit (0, &ppp->ubuf->locked);
1353                 wake_up_interruptible (&ppp->read_wait);
1354                 if (ppp->tty->fasync != NULL)
1355                         kill_fasync (ppp->tty->fasync, SIGIO);
1356
1357                 return 1;
1358 /*
1359  * The buffer is full. Unlock the header
1360  */
1361 failure:
1362                 clear_bit (0, &ppp->ubuf->locked);
1363                 if (ppp->flags & SC_DEBUG)
1364                         printk (KERN_DEBUG
1365                                 "ppp_us_queue: ran out of buffer space.\n");
1366         }
1367 /*
1368  * Discard the frame. There are no takers for this protocol.
1369  */
1370         if (ppp->flags & SC_DEBUG)
1371                 printk (KERN_DEBUG
1372                         "ppp: dropping packet on the floor.\n");
1373         slhc_toss (ppp->slcomp);
1374         return 0;
1375 }
1376
1377 /*
1378  * Handle a CCP packet.
1379  *
1380  * The CCP packet is passed along to the pppd process just like any
1381  * other PPP frame. The difference is that some processing needs to be
1382  * immediate or the compressors will become confused on the peer.
1383  */
1384
1385 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1386 {
1387         int slen    = CCP_LENGTH(dp);
1388         __u8 *opt = dp   + CCP_HDRLEN;
1389         int opt_len = slen - CCP_HDRLEN;
1390
1391         if (slen > len)
1392                 return;
1393
1394         switch (CCP_CODE(dp)) {
1395         case CCP_CONFREQ:
1396         case CCP_TERMREQ:
1397         case CCP_TERMACK:
1398 /*
1399  * CCP must be going down - disable compression
1400  */
1401                 if (ppp->flags & SC_CCP_UP) {
1402                         ppp->flags &= ~(SC_CCP_UP   |
1403                                         SC_COMP_RUN |
1404                                         SC_DECOMP_RUN);
1405                 }
1406                 break;
1407
1408         case CCP_CONFACK:
1409                 if ((ppp->flags & SC_CCP_OPEN) == 0)
1410                         break;
1411                 if (ppp->flags & SC_CCP_UP)
1412                         break;
1413                 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1414                         break;
1415                 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1416                         break;
1417 /*
1418  * we're agreeing to send compressed packets.
1419  */
1420                 if (!rcvd) {
1421                         if (ppp->sc_xc_state == NULL)
1422                                 break;
1423
1424                         if ((*ppp->sc_xcomp->comp_init)
1425                             (ppp->sc_xc_state,
1426                              opt,
1427                              opt_len,
1428                              ppp2dev (ppp)->base_addr,
1429                              0,
1430                              ppp->flags & SC_DEBUG))
1431                                 ppp->flags |= SC_COMP_RUN;
1432                         break;
1433                 }
1434 /*
1435  * peer is agreeing to send compressed packets.
1436  */
1437                 if (ppp->sc_rc_state == NULL)
1438                         break;
1439
1440                 if ((*ppp->sc_rcomp->decomp_init)
1441                     (ppp->sc_rc_state,
1442                      opt,
1443                      opt_len,
1444                      ppp2dev (ppp)->base_addr,
1445                      0,
1446                      ppp->mru,
1447                      ppp->flags & SC_DEBUG)) {
1448                         ppp->flags |= SC_DECOMP_RUN;
1449                         ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1450                 }
1451                 break;
1452 /*
1453  * The protocol sequence is complete at this end
1454  */
1455         case CCP_RESETACK:
1456                 if ((ppp->flags & SC_CCP_UP) == 0)
1457                         break;
1458
1459                 if (!rcvd) {
1460                         if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN))
1461                                 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1462                 } else {
1463                         if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1464                               (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1465                                 ppp->flags &= ~SC_DC_ERROR;
1466                         }
1467                 }
1468                 break;
1469         }
1470 }
1471
1472 static int
1473 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1474 {
1475         ppp_proto_ccp (ppp, dp, len, 1);
1476         return rcv_proto_unknown (ppp, proto, dp, len);
1477 }
1478
1479 /*
1480  * Handle a LQR packet.
1481  */
1482
1483 static int
1484 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1485 {
1486         return rcv_proto_unknown (ppp, proto, data, len);
1487 }
1488
1489 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1490 {
1491         __u16           proto = PPP_PROTOCOL (data);
1492         ppp_proto_type  *proto_ptr;
1493 /*
1494  * Ignore empty frames
1495  */
1496         if (count <= PPP_HDRLEN)
1497                 return;
1498 /*
1499  * Count the frame and print it
1500  */
1501         ++ppp->stats.ppp_ipackets;
1502         if (ppp->flags & SC_LOG_INPKT)
1503                 ppp_print_buffer ("receive frame", data, count);
1504 /*
1505  * Find the procedure to handle this protocol. The last one is marked
1506  * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1507  */
1508         proto_ptr = proto_list;
1509         while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1510                 ++proto_ptr;
1511 /*
1512  * Update the appropriate statistic counter.
1513  */
1514         if ((*proto_ptr->func) (ppp, proto,
1515                                 &data[PPP_HARD_HDR_LEN],
1516                                 count - PPP_HARD_HDR_LEN))
1517                 ppp->stats.ppp_ioctects += count;
1518         else
1519                 ++ppp->stats.ppp_discards;
1520 }
1521
1522 /* on entry, a received frame is in ppp->rbuf.bufr
1523    check it and dispose as appropriate */
1524
1525 static int
1526 ppp_doframe (struct ppp *ppp)
1527 {
1528         __u8    *data = buf_base (ppp->rbuf);
1529         int     count = ppp->rbuf->count;
1530         int     addr, ctrl, proto;
1531         int     new_count;
1532         __u8 *new_data;
1533 /*
1534  * If there is a pending error from the receiver then log it and discard
1535  * the damaged frame.
1536  */
1537         if (ppp->toss) {
1538                 if ((ppp->flags & SC_DEBUG) && count > 0)
1539                         printk (KERN_DEBUG
1540                                 "ppp_toss: tossing frame, reason = %x\n",
1541                                 ppp->toss);
1542                 return 0;
1543         }
1544 /*
1545  * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1546  * follows each frame.
1547  */
1548         if (count == 0)
1549                 return 1;
1550 /*
1551  * Generate an error if the frame is too small.
1552  */
1553         if (count < PPP_HARD_HDR_LEN) {
1554                 if (ppp->flags & SC_DEBUG)
1555                         printk (KERN_DEBUG
1556                                 "ppp: got runt ppp frame, %d chars\n", count);
1557                 ++ppp->estats.rx_length_errors;
1558                 return 0;
1559         }
1560 /*
1561  * Verify the CRC of the frame and discard the CRC characters from the
1562  * end of the buffer.
1563  */
1564         if (ppp->rbuf->fcs != PPP_GOODFCS) {
1565                 if (ppp->flags & SC_DEBUG) {
1566                         printk (KERN_DEBUG
1567                                 "ppp: frame with bad fcs, length = %d\n",
1568                                 count);
1569                         ppp_print_buffer("bad frame", data, count);
1570                 }
1571                 ++ppp->estats.rx_crc_errors;
1572                 return 0;
1573         }
1574         count -= 2;             /* ignore the fcs characters */
1575 /*
1576  * Ignore the leading ADDRESS and CONTROL fields in the frame.
1577  */
1578         addr   = PPP_ALLSTATIONS;
1579         ctrl   = PPP_UI;
1580
1581         if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1582                 data  += 2;
1583                 count -= 2;
1584         }
1585 /*
1586  * Obtain the protocol from the frame
1587  */
1588         proto = (__u16) *data++;
1589         if ((proto & 1) == 0) {
1590                 proto = (proto << 8) | (__u16) *data++;
1591                 --count;
1592         }
1593 /*
1594  * Rewrite the header with the full information. This may encroach upon
1595  * the 'filler' area in the buffer header. This is the purpose for the
1596  * filler.
1597  */
1598         *(--data) = proto;
1599         *(--data) = proto >> 8;
1600         *(--data) = ctrl;
1601         *(--data) = addr;
1602         count    += 3;
1603 /*
1604  * Process the active decompressor.
1605  */
1606         if ((ppp->sc_rc_state != (void *) 0) &&
1607             (ppp->flags & SC_DECOMP_RUN)     &&
1608             ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1609                 if (proto == PPP_COMP) {
1610 /*
1611  * If the frame is compressed then decompress it.
1612  */
1613                         new_data = kmalloc (ppp->mru + PPP_HDRLEN, GFP_ATOMIC);
1614                         if (new_data == NULL) {
1615                                 if (ppp->flags & SC_DEBUG)
1616                                         printk (KERN_ERR
1617                                                 "ppp_doframe: no memory\n");
1618                                 new_count = DECOMP_ERROR;
1619                         } else {
1620                                 new_count = (*ppp->sc_rcomp->decompress)
1621                                         (ppp->sc_rc_state, data, count,
1622                                          new_data, ppp->mru + PPP_HDRLEN);
1623                         }
1624                         switch (new_count) {
1625                         default:
1626                                 ppp_doframe_lower (ppp, new_data, new_count);
1627                                 kfree (new_data);
1628                                 return 1;
1629
1630                         case DECOMP_ERROR:
1631                                 ppp->flags |= SC_DC_ERROR;
1632                                 break;
1633
1634                         case DECOMP_FATALERROR:
1635                                 ppp->flags |= SC_DC_FERROR;
1636                                 if (ppp->flags & SC_DEBUG)
1637                                         printk(KERN_ERR "ppp: fatal decomp error\n");
1638                                 break;
1639                         }
1640 /*
1641  * Log the error condition and discard the frame.
1642  */
1643                         if (new_data != 0)
1644                                 kfree (new_data);
1645                         slhc_toss (ppp->slcomp);
1646                         ++ppp->stats.ppp_ierrors;
1647                 } else {
1648 /*
1649  * The frame is not special. Pass it through the compressor without
1650  * actually compressing the data
1651  */
1652                         (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1653                                                   data, count);
1654                 }
1655         }
1656 /*
1657  * Process the uncompressed frame.
1658  */
1659         ppp_doframe_lower (ppp, data, count);
1660         return 1;
1661 }
1662
1663 /*************************************************************
1664  * LINE DISCIPLINE SUPPORT
1665  *    The following functions form support user programs
1666  *    which read and write data on a TTY with the PPP line
1667  *    discipline.  Reading is done from a circular queue,
1668  *    filled by the lower TTY levels.
1669  *************************************************************/
1670
1671 /* read a PPP frame from the us_rbuff circular buffer,
1672    waiting if necessary
1673 */
1674
1675 static int
1676 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1677               unsigned int nr)
1678 {
1679         struct ppp *ppp = tty2ppp (tty);
1680         __u8 c;
1681         int len, indx;
1682
1683 #define GETC(c)                                         \
1684 {                                                       \
1685         c = buf_base (ppp->ubuf) [ppp->ubuf->tail++];   \
1686         ppp->ubuf->tail &= ppp->ubuf->size;             \
1687 }
1688
1689 /*
1690  * Validate the pointers
1691  */
1692         if (!ppp)
1693                 return -EIO;
1694
1695         if (ppp->magic != PPP_MAGIC)
1696                 return -EIO;
1697
1698         CHECK_PPP (-ENXIO);
1699
1700 /*
1701  * Acquire the read lock.
1702  */
1703         for (;;) {
1704                 ppp = tty2ppp (tty);
1705                 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
1706                     || tty != ppp->tty)
1707                         return 0;
1708
1709                 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1710                         if (ppp->flags & SC_DEBUG)
1711                                 printk (KERN_DEBUG
1712                                      "ppp_tty_read: sleeping(ubuf)\n");
1713
1714                         current->timeout = 0;
1715                         current->state   = TASK_INTERRUPTIBLE;
1716                         schedule ();
1717
1718                         if (current->signal & ~current->blocked)
1719                                 return -EINTR;
1720                         continue;
1721                 }
1722 /*
1723  * Before we attempt to write the frame to the user, ensure that the
1724  * user has access to the pages for the total buffer length.
1725  */
1726                 indx = verify_area (VERIFY_WRITE, buf, nr);
1727                 if (indx != 0)
1728                         return (indx);
1729 /*
1730  * Fetch the length of the buffer from the first two bytes.
1731  */
1732                 if (ppp->ubuf->head == ppp->ubuf->tail)
1733                         len = 0;
1734                 else {
1735                         GETC (c);
1736                         len = c << 8;
1737                         GETC (c);
1738                         len += c;
1739                 }
1740 /*
1741  * If there is no length then wait for the data to arrive.
1742  */
1743                 if (len == 0) {
1744                         /* no data */
1745                         clear_bit (0, &ppp->ubuf->locked);
1746                         if (file->f_flags & O_NONBLOCK)
1747                                 return -EAGAIN;
1748                         current->timeout = 0;
1749
1750                         if (ppp->flags & SC_DEBUG)
1751                                 printk (KERN_DEBUG
1752                                         "ppp_tty_read: sleeping(read_wait)\n");
1753
1754                         interruptible_sleep_on (&ppp->read_wait);
1755                         if (current->signal & ~current->blocked)
1756                                 return -EINTR;
1757                         continue;
1758                 }
1759 /*
1760  * Ensure that the frame will fit within the caller's buffer. If not, then
1761  * discard the frame from the input buffer.
1762  */
1763                 if (len + 2 > nr) {
1764                         /* Can't copy it, update us_rbuff_head */
1765
1766                         if (ppp->flags & SC_DEBUG)
1767                                 printk (KERN_DEBUG
1768                                 "ppp: read of %u bytes too small for %d "
1769                                 "frame\n", nr, len + 2);
1770                         ppp->ubuf->tail += len;
1771                         ppp->ubuf->tail &= ppp->ubuf->size;
1772                         clear_bit (0, &ppp->ubuf->locked);
1773                         ppp->stats.ppp_ierrors++;
1774                         return -EOVERFLOW;
1775                 }
1776 /*
1777  * Before we attempt to write the frame to the user, ensure that the
1778  * page tables are proper.
1779  */
1780                 indx = verify_area (VERIFY_WRITE, buf, len + 2);
1781                 if (indx != 0) {
1782                         ppp->ubuf->tail += len;
1783                         ppp->ubuf->tail &= ppp->ubuf->size;
1784                         clear_bit (0, &ppp->ubuf->locked);
1785                         return (indx);
1786                 }
1787 /*
1788  * Fake the insertion of the ADDRESS and CONTROL information because these
1789  * were not saved in the buffer.
1790  */
1791                 put_user (PPP_ALLSTATIONS, buf++);
1792                 put_user (PPP_UI,          buf++);
1793
1794                 indx = len;
1795 /*
1796  * Copy the received data from the buffer to the caller's area.
1797  */
1798                 while (indx-- > 0) {
1799                         GETC (c);
1800                         put_user (c, buf);
1801                         ++buf;
1802                 }
1803
1804                 clear_bit (0, &ppp->ubuf->locked);
1805                 len += 2; /* Account for ADDRESS and CONTROL bytes */
1806                 if (ppp->flags & SC_DEBUG)
1807                         printk (KERN_DEBUG
1808                                 "ppp_tty_read: passing %d bytes up\n", len);
1809                 return len;
1810         }
1811 #undef GETC
1812 }
1813
1814 /* stuff a character into the transmit buffer, using PPP's way of escaping
1815    special characters.
1816    also, update fcs to take account of new character */
1817
1818 extern inline void
1819 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1820                 register __u8 chr)
1821 {
1822 /*
1823  * The buffer should not be full.
1824  */
1825         if (ppp->flags & SC_DEBUG) {
1826                 if ((buf->count < 0) || (buf->count > 3000))
1827                         printk (KERN_DEBUG "ppp_stuff_char: %d %x\n",
1828                                 (unsigned int) buf->count,
1829                                 (unsigned int) chr);
1830         }
1831 /*
1832  * Update the FCS and if the character needs to be escaped, do it.
1833  */
1834         buf->fcs = PPP_FCS (buf->fcs, chr);
1835         if (in_xmap (ppp, chr)) {
1836                 chr ^= PPP_TRANS;
1837                 ins_char (buf, PPP_ESCAPE);
1838         }
1839 /*
1840  * Add the character to the buffer.
1841  */
1842         ins_char (buf, chr);
1843 }
1844
1845 /*
1846  * Procedure to encode the data with the proper escaping and send the
1847  * data to the remote system.
1848  */
1849
1850 static void
1851 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
1852                     __u8 *data, int count, int non_ip)
1853 {
1854         __u16   write_fcs;
1855         int     address, control;
1856         int     proto;
1857
1858         ++ppp->stats.ppp_opackets;
1859         ppp->stats.ppp_ooctects += count;
1860
1861 /*
1862  * Insert the leading FLAG character
1863  */
1864         buf->count = 0;
1865
1866         if (non_ip || flag_time == 0)
1867                 ins_char (buf, PPP_FLAG);
1868         else {
1869                 if (jiffies - ppp->last_xmit > flag_time)
1870                         ins_char (buf, PPP_FLAG);
1871         }
1872         ppp->last_xmit = jiffies;
1873         buf->fcs       = PPP_INITFCS;
1874 /*
1875  * Emit the address/control information if needed
1876  */
1877         address = PPP_ADDRESS  (data);
1878         control = PPP_CONTROL  (data);
1879         proto   = PPP_PROTOCOL (data);
1880
1881         if (address != PPP_ALLSTATIONS ||
1882             control != PPP_UI ||
1883             (ppp->flags & SC_COMP_AC) == 0) {
1884                 ppp_stuff_char (ppp, buf, address);
1885                 ppp_stuff_char (ppp, buf, control);
1886         }
1887 /*
1888  * Emit the protocol (compressed if possible)
1889  */
1890         if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
1891                 ppp_stuff_char (ppp, buf, proto >> 8);
1892
1893         ppp_stuff_char (ppp, buf, proto);
1894 /*
1895  * Insert the data
1896  */
1897         data  += 4;
1898         count -= 4;
1899
1900         while (count-- > 0)
1901                 ppp_stuff_char (ppp, buf, *data++);
1902 /*
1903  * Add the trailing CRC and the final flag character
1904  */
1905         write_fcs = buf->fcs ^ 0xFFFF;
1906         ppp_stuff_char (ppp, buf, write_fcs);
1907         ppp_stuff_char (ppp, buf, write_fcs >> 8);
1908 /*
1909  * Add the trailing flag character
1910  */
1911         ins_char (buf, PPP_FLAG);
1912 /*
1913  * Print the buffer
1914  */
1915         if (ppp->flags & SC_LOG_FLUSH)
1916                 ppp_print_buffer ("ppp flush", buf_base (buf),
1917                                   buf->count);
1918 /*
1919  * Send the block to the tty driver.
1920  */
1921         ppp->stats.ppp_obytes += buf->count;
1922         ppp_kick_tty (ppp, buf);
1923 }
1924
1925 /*
1926  * Send an frame to the remote with the proper bsd compression.
1927  *
1928  * Return 0 if frame was queued for transmission.
1929  *        1 if frame must be re-queued for later driver support.
1930  */
1931
1932 static int
1933 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
1934                     __u8 *data, int count)
1935 {
1936         int     proto;
1937         int     address, control;
1938         __u8 *new_data;
1939         int     new_count;
1940 /*
1941  * Print the buffer
1942  */
1943         if (ppp->flags & SC_LOG_OUTPKT)
1944                 ppp_print_buffer ("write frame", data, count);
1945 /*
1946  * Determine if the frame may be compressed. Attempt to compress the
1947  * frame if possible.
1948  */
1949         proto   = PPP_PROTOCOL (data);
1950         address = PPP_ADDRESS  (data);
1951         control = PPP_CONTROL  (data);
1952
1953         if (((ppp->flags & SC_COMP_RUN) != 0)   &&
1954             (ppp->sc_xc_state != (void *) 0)    &&
1955             (address == PPP_ALLSTATIONS)        &&
1956             (control == PPP_UI)                 &&
1957             (proto != PPP_LCP)                  &&
1958             (proto != PPP_CCP)) {
1959                 new_data = kmalloc (count, GFP_ATOMIC);
1960                 if (new_data == NULL) {
1961                         if (ppp->flags & SC_DEBUG)
1962                                 printk (KERN_ERR
1963                                         "ppp_dev_xmit_frame: no memory\n");
1964                         return 1;
1965                 }
1966
1967                 new_count = (*ppp->sc_xcomp->compress)
1968                     (ppp->sc_xc_state, data, new_data, count, count);
1969
1970                 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
1971                         ppp_dev_xmit_lower (ppp, buf, new_data, new_count, 0);
1972                         kfree (new_data);
1973                         return 0;
1974                 }
1975 /*
1976  * The frame could not be compressed, or it could not be sent in
1977  * compressed form because CCP is not yet up.
1978  */
1979                 kfree (new_data);
1980         }
1981 /*
1982  * Go to the escape encoding
1983  */
1984         ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
1985         return 0;
1986 }
1987
1988 /*
1989  * Revise the tty frame for specific protocols.
1990  */
1991
1992 static int
1993 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
1994 {
1995         __u8 *p;
1996
1997         switch (PPP_PROTOCOL (data)) {
1998 /*
1999  * Update the LQR frame with the current MIB information. This saves having
2000  * the daemon read old MIB data from the driver.
2001  */
2002         case PPP_LQR:
2003                 len = 48;                       /* total size of this frame */
2004                 p   = (__u8 *) &data [40];      /* Point to last two items. */
2005                 p   = store_long (p, ppp->stats.ppp_opackets + 1);
2006                 p   = store_long (p, ppp->stats.ppp_ooctects + len);
2007                 break;
2008 /*
2009  * Outbound compression frames
2010  */
2011         case PPP_CCP:
2012                 ppp_proto_ccp (ppp,
2013                                data + PPP_HARD_HDR_LEN,
2014                                len  - PPP_HARD_HDR_LEN,
2015                                0);
2016                 break;
2017
2018         default:
2019                 break;
2020         }
2021
2022         return len;
2023 }
2024
2025 /*
2026  * write a frame with NR chars from BUF to TTY
2027  * we have to put the FCS field on ourselves
2028  */
2029
2030 static int
2031 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2032                unsigned int count)
2033 {
2034         struct ppp *ppp = tty2ppp (tty);
2035         __u8 *new_data;
2036         int status;
2037 /*
2038  * Verify the pointers.
2039  */
2040         if (!ppp)
2041                 return -EIO;
2042
2043         if (ppp->magic != PPP_MAGIC)
2044                 return -EIO;
2045
2046         CHECK_PPP (-ENXIO);
2047 /*
2048  * Ensure that the caller does not wish to send too much.
2049  */
2050         if (count > PPP_MTU + PPP_HDRLEN) {
2051                 if (ppp->flags & SC_DEBUG)
2052                         printk (KERN_WARNING
2053                                 "ppp_tty_write: truncating user packet "
2054                                 "from %u to mtu %d\n", count,
2055                                 PPP_MTU + PPP_HDRLEN);
2056                 count = PPP_MTU + PPP_HDRLEN;
2057         }
2058 /*
2059  * Allocate a buffer for the data and fetch it from the user space.
2060  */
2061         new_data = kmalloc (count, GFP_KERNEL);
2062         if (new_data == NULL) {
2063                 if (ppp->flags & SC_DEBUG)
2064                         printk (KERN_ERR
2065                                 "ppp_tty_write: no memory\n");
2066                 return 0;
2067         }
2068 /*
2069  * lock this PPP unit so we will be the only writer;
2070  * sleep if necessary
2071  */
2072         while (lock_buffer (ppp->tbuf) != 0) {
2073                 current->timeout = 0;
2074                 if (ppp->flags & SC_DEBUG)
2075                         printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2076                 interruptible_sleep_on (&ppp->write_wait);
2077
2078                 ppp = tty2ppp (tty);
2079                 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
2080                     || tty != ppp->tty) {
2081                         kfree (new_data);
2082                         return 0;
2083                 }
2084
2085                 if (current->signal & ~current->blocked) {
2086                         kfree (new_data);
2087                         return -EINTR;
2088                 }
2089         }
2090 /*
2091  * Ensure that the caller's buffer is valid.
2092  */
2093         status = verify_area (VERIFY_READ, data, count);
2094         if (status != 0) {
2095                 kfree (new_data);
2096                 ppp->tbuf->locked = 0;
2097                 return status;
2098         }
2099
2100         memcpy_fromfs (new_data, data, count);
2101 /*
2102  * Change the LQR frame
2103  */
2104         count = send_revise_frame (ppp, new_data, count);
2105 /*
2106  * Send the data
2107  */
2108         ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2109         kfree (new_data);
2110         return (int) count;
2111 }
2112
2113 /*
2114  * Process the BSD compression IOCTL event for the tty device.
2115  */
2116
2117 static int
2118 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2119 {
2120         struct compressor *cp;
2121         struct ppp_option_data data;
2122         int error;
2123         int nb;
2124         __u8 *ptr;
2125         __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2126 /*
2127  * Fetch the compression parameters
2128  */
2129         error = verify_area (VERIFY_READ, odp, sizeof (data));
2130         if (error == 0) {
2131                 memcpy_fromfs (&data, odp, sizeof (data));
2132                 nb  = data.length;
2133                 ptr = data.ptr;
2134                 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2135                         nb = CCP_MAX_OPTION_LENGTH;
2136         
2137                 error = verify_area (VERIFY_READ, ptr, nb);
2138         }
2139
2140         if (error != 0)
2141                 return error;
2142
2143         memcpy_fromfs (ccp_option, ptr, nb);
2144
2145         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
2146                 return (-EINVAL);
2147
2148         cp = find_compressor ((int) (unsigned int) (__u8) ccp_option[0]);
2149         if (cp != (struct compressor *) 0) {
2150                 /*
2151                  * Found a handler for the protocol - try to allocate
2152                  * a compressor or decompressor.
2153                  */
2154                 error = 0;
2155                 if (data.transmit) {
2156                         if (ppp->sc_xc_state != NULL)
2157                                 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2158
2159                         ppp->sc_xcomp    = cp;
2160                         ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2161
2162                         if (ppp->sc_xc_state == NULL) {
2163                                 if (ppp->flags & SC_DEBUG)
2164                                         printk("ppp%ld: comp_alloc failed\n",
2165                                                ppp2dev (ppp)->base_addr);
2166                                 error = -ENOBUFS;
2167                         }
2168                         ppp->flags &= ~SC_COMP_RUN;
2169                 } else {
2170                         if (ppp->sc_rc_state != NULL)
2171                                 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2172                         ppp->sc_rcomp    = cp;
2173                         ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2174                         if (ppp->sc_rc_state == NULL) {
2175                                 if (ppp->flags & SC_DEBUG)
2176                                         printk("ppp%ld: decomp_alloc failed\n",
2177                                                ppp2dev (ppp)->base_addr);
2178                                 error = ENOBUFS;
2179                         }
2180                         ppp->flags &= ~SC_DECOMP_RUN;
2181                 }
2182                 return (error);
2183         }
2184
2185         if (ppp->flags & SC_DEBUG)
2186                 printk(KERN_DEBUG "ppp%ld: no compressor for [%x %x %x], %x\n",
2187                        ppp2dev (ppp)->base_addr, ccp_option[0], ccp_option[1],
2188                        ccp_option[2], nb);
2189         return (-EINVAL);       /* no handler found */
2190 }
2191
2192 /*
2193  * Process the IOCTL event for the tty device.
2194  */
2195
2196 static int
2197 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2198                unsigned int param2, unsigned long param3)
2199 {
2200         struct ppp *ppp = tty2ppp (tty);
2201         register int temp_i = 0;
2202         int error = 0;
2203 /*
2204  * Verify the status of the PPP device.
2205  */
2206         if (!ppp)
2207                 return -EBADF;
2208
2209         if (ppp->magic != PPP_MAGIC)
2210                 return -EBADF;
2211
2212         CHECK_PPP (-ENXIO);
2213 /*
2214  * The user must have an euid of root to do these requests.
2215  */
2216         if (!suser ())
2217                 return -EPERM;
2218 /*
2219  * Set the MRU value
2220  */
2221         switch (param2) {
2222         case PPPIOCSMRU:
2223                 error = verify_area (VERIFY_READ, (void *) param3,
2224                                      sizeof (temp_i));
2225                 if (error == 0) {
2226                         temp_i = get_user ((int *) param3);
2227                         if (ppp->flags & SC_DEBUG)
2228                                 printk (KERN_INFO
2229                                  "ppp_tty_ioctl: set mru to %x\n", temp_i);
2230
2231                         if (ppp->mru != temp_i)
2232                                 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2233                 }
2234                 break;
2235 /*
2236  * Fetch the flags
2237  */
2238         case PPPIOCGFLAGS:
2239                 error = verify_area (VERIFY_WRITE, (void *) param3,
2240                                      sizeof (temp_i));
2241                 if (error == 0) {
2242                         temp_i = (ppp->flags & SC_MASK);
2243 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2244                         temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2245                                   SC_RCV_ODDP | SC_RCV_EVNP;
2246 #endif
2247                         put_user (temp_i, (int *) param3);
2248                 }
2249                 break;
2250 /*
2251  * Set the flags for the various options
2252  */
2253         case PPPIOCSFLAGS:
2254                 error = verify_area (VERIFY_READ, (void *) param3,
2255                                      sizeof (temp_i));
2256                 if (error == 0) {
2257                         temp_i  = get_user ((int *) param3) & SC_MASK;
2258                         temp_i |= (ppp->flags & ~SC_MASK);
2259
2260                         if ((ppp->flags & SC_CCP_OPEN) &&
2261                             (temp_i & SC_CCP_OPEN) == 0)
2262                                 ppp_ccp_closed (ppp);
2263
2264                         if ((ppp->flags | temp_i) & SC_DEBUG)
2265                                 printk (KERN_INFO
2266                                "ppp_tty_ioctl: set flags to %x\n", temp_i);
2267                         ppp->flags = temp_i;
2268                 }
2269                 break;
2270 /*
2271  * Set the compression mode
2272  */
2273         case PPPIOCSCOMPRESS:
2274                 error = ppp_set_compression (ppp,
2275                                             (struct ppp_option_data *) param3);
2276                 break;
2277 /*
2278  * Retrieve the transmit async map
2279  */
2280         case PPPIOCGASYNCMAP:
2281                 error = verify_area (VERIFY_WRITE, (void *) param3,
2282                                      sizeof (temp_i));
2283                 if (error == 0) {
2284                         put_user (ppp->xmit_async_map[0], (int *) param3);
2285                 }
2286                 break;
2287 /*
2288  * Set the transmit async map
2289  */
2290         case PPPIOCSASYNCMAP:
2291                 error = verify_area (VERIFY_READ, (void *) param3,
2292                                      sizeof (temp_i));
2293                 if (error == 0) {
2294                         ppp->xmit_async_map[0] = get_user ((int *) param3);
2295                         if (ppp->flags & SC_DEBUG)
2296                                 printk (KERN_INFO
2297                                      "ppp_tty_ioctl: set xmit asyncmap %x\n",
2298                                      ppp->xmit_async_map[0]);
2299                 }
2300                 break;
2301 /*
2302  * Set the receive async map
2303  */
2304         case PPPIOCSRASYNCMAP:
2305                 error = verify_area (VERIFY_READ, (void *) param3,
2306                                      sizeof (temp_i));
2307                 if (error == 0) {
2308                         ppp->recv_async_map = get_user ((int *) param3);
2309                         if (ppp->flags & SC_DEBUG)
2310                                 printk (KERN_INFO
2311                                      "ppp_tty_ioctl: set rcv asyncmap %x\n",
2312                                      ppp->recv_async_map);
2313                 }
2314                 break;
2315 /*
2316  * Obtain the unit number for this device.
2317  */
2318         case PPPIOCGUNIT:
2319                 error = verify_area (VERIFY_WRITE, (void *) param3,
2320                                      sizeof (temp_i));
2321                 if (error == 0) {
2322                         put_user (ppp2dev (ppp)->base_addr, (int *) param3);
2323                         if (ppp->flags & SC_DEBUG)
2324                                 printk (KERN_INFO
2325                                         "ppp_tty_ioctl: get unit: %ld\n",
2326                                         ppp2dev (ppp)->base_addr);
2327                 }
2328                 break;
2329 /*
2330  * Set the debug level
2331  */
2332         case PPPIOCSDEBUG:
2333                 error = verify_area (VERIFY_READ, (void *) param3,
2334                                      sizeof (temp_i));
2335                 if (error == 0) {
2336                         temp_i  = (get_user ((int *) param3) & 0x1F) << 16;
2337                         temp_i |= (ppp->flags & ~0x1F0000);
2338
2339                         if ((ppp->flags | temp_i) & SC_DEBUG)
2340                                 printk (KERN_INFO
2341                                "ppp_tty_ioctl: set flags to %x\n", temp_i);
2342                         ppp->flags = temp_i;
2343                 }
2344                 break;
2345 /*
2346  * Get the debug level
2347  */
2348         case PPPIOCGDEBUG:
2349                 error = verify_area (VERIFY_WRITE, (void *) param3,
2350                                      sizeof (temp_i));
2351                 if (error == 0) {
2352                         temp_i = (ppp->flags >> 16) & 0x1F;
2353                         put_user (temp_i, (int *) param3);
2354                 }
2355                 break;
2356 /*
2357  * Get the times since the last send/receive frame operation
2358  */
2359         case PPPIOCGIDLE:
2360                 error = verify_area (VERIFY_WRITE, (void *) param3,
2361                                      sizeof (struct ppp_idle));
2362                 if (error == 0) {
2363                         struct ppp_idle cur_ddinfo;
2364                         __u32 cur_jiffies = jiffies;
2365
2366                         /* change absolute times to relative times. */
2367                         cur_ddinfo.xmit_idle = (cur_jiffies - ppp->ddinfo.xmit_idle) / HZ;
2368                         cur_ddinfo.recv_idle = (cur_jiffies - ppp->ddinfo.recv_idle) / HZ;
2369                         memcpy_tofs ((void *) param3, &cur_ddinfo,
2370                                      sizeof (cur_ddinfo));
2371                 }
2372                 break;
2373 /*
2374  * Retrieve the extended async map
2375  */
2376         case PPPIOCGXASYNCMAP:
2377                 error = verify_area (VERIFY_WRITE,
2378                                      (void *) param3,
2379                                      sizeof (ppp->xmit_async_map));
2380                 if (error == 0) {
2381                         memcpy_tofs ((void *) param3,
2382                                      ppp->xmit_async_map,
2383                                      sizeof (ppp->xmit_async_map));
2384                 }
2385                 break;
2386 /*
2387  * Set the async extended map
2388  */
2389         case PPPIOCSXASYNCMAP:
2390                 error = verify_area (VERIFY_READ, (void *) param3,
2391                                      sizeof (ppp->xmit_async_map));
2392                 if (error == 0) {
2393                         __u32 temp_tbl[8];
2394
2395                         memcpy_fromfs (temp_tbl, (void *) param3,
2396                                        sizeof (ppp->xmit_async_map));
2397                         temp_tbl[1]  =  0x00000000;
2398                         temp_tbl[2] &= ~0x40000000;
2399                         temp_tbl[3] |=  0x60000000;
2400
2401                         if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2402                             (temp_tbl[4] & temp_tbl[5]) != 0 ||
2403                             (temp_tbl[6] & temp_tbl[7]) != 0)
2404                                 error = -EINVAL;
2405                         else {
2406                                 memcpy (ppp->xmit_async_map, temp_tbl,
2407                                         sizeof (ppp->xmit_async_map));
2408
2409                                 if (ppp->flags & SC_DEBUG)
2410                                         printk (KERN_INFO
2411                                         "ppp_tty_ioctl: set xasyncmap\n");
2412                         }
2413                 }
2414                 break;
2415 /*
2416  * Set the maximum VJ header compression slot number.
2417  */
2418         case PPPIOCSMAXCID:
2419                 error = verify_area (VERIFY_READ, (void *) param3,
2420                                      sizeof (temp_i));
2421                 if (error == 0) {
2422                         temp_i = get_user ((int *) param3) + 1;
2423                         if (ppp->flags & SC_DEBUG)
2424                                 printk (KERN_INFO
2425                                      "ppp_tty_ioctl: set maxcid to %d\n",
2426                                      temp_i);
2427                         if (ppp->slcomp != NULL)
2428                                 slhc_free (ppp->slcomp);
2429                         ppp->slcomp = slhc_init (16, temp_i);
2430
2431                         if (ppp->slcomp == NULL) {
2432                                 if (ppp->flags & SC_DEBUG)
2433                                         printk (KERN_ERR
2434                                         "ppp: no space for compression buffers!\n");
2435                                 ppp_release (ppp);
2436                                 error = -ENOMEM;
2437                         }
2438                 }
2439                 break;
2440
2441     case PPPIOCXFERUNIT:
2442                 ppp->backup_tty = tty;
2443                 ppp->sc_xfer = current->pid;
2444                 break;
2445
2446     case PPPIOCGNPMODE:
2447     case PPPIOCSNPMODE:
2448                 error = verify_area (VERIFY_READ, (void *) param3,
2449                                      sizeof (struct npioctl));
2450                 if (error == 0) {
2451                         struct npioctl npi;
2452                         memcpy_fromfs (&npi,
2453                                        (void *) param3,
2454                                        sizeof (npi));
2455
2456                         switch (npi.protocol) {
2457                         case PPP_IP:
2458                                 npi.protocol = NP_IP;
2459                                 break;
2460                         default:
2461                                 if (ppp->flags & SC_DEBUG)
2462                                         printk(KERN_DEBUG "pppioc[gs]npmode: "
2463                                                "invalid proto %d\n", npi.protocol);
2464                                 error = -EINVAL;
2465                         }
2466
2467                         if (error != 0)
2468                                 break;
2469
2470                         if (param2 == PPPIOCGNPMODE) {
2471                                 npi.mode = ppp->sc_npmode[npi.protocol];
2472                                 error = verify_area (VERIFY_WRITE,
2473                                                      (void *) param3,
2474                                                      sizeof (npi));
2475                                 if (error != 0)
2476                                         break;
2477
2478                                 memcpy_tofs ((void *) param3,
2479                                              &npi,
2480                                              sizeof (npi));
2481                                 break;
2482                         }
2483
2484                         if (npi.mode != ppp->sc_npmode[npi.protocol]) {
2485                                 ppp->sc_npmode[npi.protocol] = npi.mode;
2486                                 if (npi.mode != NPMODE_QUEUE) {
2487                                         /* ppp_requeue(ppp); maybe needed */
2488                                         ppp_tty_wakeup (ppp2tty(ppp));
2489                                 }
2490                         }
2491                 }
2492                 break;
2493 /*
2494  * Allow users to read, but not set, the serial port parameters
2495  */
2496         case TCGETS:
2497         case TCGETA:
2498                 error = n_tty_ioctl (tty, file, param2, param3);
2499                 break;
2500
2501         case FIONREAD:
2502                 error = verify_area (VERIFY_WRITE,
2503                                      (void *) param3,
2504                                      sizeof (int));
2505                 if (error == 0) {
2506                         int count = ppp->ubuf->tail - ppp->ubuf->head;
2507                         if (count < 0)
2508                                 count += (ppp->ubuf->size + 1);
2509
2510                         put_user (count, (int *) param3);
2511                 }
2512                 break;
2513 /*
2514  *  All other ioctl() events will come here.
2515  */
2516         default:
2517                 if (ppp->flags & SC_DEBUG)
2518                         printk (KERN_ERR
2519                                 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2520                                 param2,
2521                                 param3);
2522
2523                 error = -ENOIOCTLCMD;
2524                 break;
2525         }
2526         return error;
2527 }
2528
2529 /*
2530  * TTY callback.
2531  *
2532  * Process the select() statement for the PPP device.
2533  */
2534
2535 static int
2536 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2537                 struct file *filp, int sel_type, select_table * wait)
2538 {
2539         struct ppp *ppp = tty2ppp (tty);
2540         int result = 1;
2541 /*
2542  * Verify the status of the PPP device.
2543  */
2544         if (!ppp)
2545                 return -EBADF;
2546
2547         if (ppp->magic != PPP_MAGIC || tty != ppp->tty)
2548                 return -EBADF;
2549
2550         CHECK_PPP (0);
2551 /*
2552  * Branch on the type of select mode. A read request must lock the user
2553  * buffer area.
2554  */
2555         switch (sel_type) {
2556         case SEL_IN:
2557                 if (set_bit (0, &ppp->ubuf->locked) == 0) {
2558                         /* Test for the presence of data in the queue */
2559                         if (ppp->ubuf->head != ppp->ubuf->tail) {
2560                                 clear_bit (0, &ppp->ubuf->locked);
2561                                 break;
2562                         }
2563                         clear_bit (0, &ppp->ubuf->locked);
2564                 }               /* fall through */
2565 /*
2566  * Exceptions or read errors.
2567  */
2568         case SEL_EX:
2569                 /* Is this a pty link and the remote disconnected? */
2570                 if (tty->flags & (1 << TTY_OTHER_CLOSED))
2571                         break;
2572
2573                 /* Is this a local link and the modem disconnected? */
2574                 if (tty_hung_up_p (filp))
2575                         break;
2576
2577                 select_wait (&ppp->read_wait, wait);
2578                 result = 0;
2579                 break;
2580 /*
2581  * Write mode. A write is allowed if there is no current transmission.
2582  */
2583         case SEL_OUT:
2584                 if (ppp->tbuf->locked != 0) {
2585                         select_wait (&ppp->write_wait, wait);
2586                         result = 0;
2587                 }
2588                 break;
2589         }
2590         return result;
2591 }
2592
2593 /*************************************************************
2594  * NETWORK OUTPUT
2595  *    This routine accepts requests from the network layer
2596  *    and attempts to deliver the packets.
2597  *    It also includes various routines we are compelled to
2598  *    have to make the network layer work (arp, etc...).
2599  *************************************************************/
2600
2601 /*
2602  * Callback from the network layer when the device goes up.
2603  */
2604
2605 static int
2606 ppp_dev_open (struct device *dev)
2607 {
2608         struct ppp *ppp = dev2ppp (dev);
2609
2610         /* reset POINTOPOINT every time, since dev_close zaps it! */
2611         dev->flags |= IFF_POINTOPOINT;
2612
2613         if (ppp2tty (ppp) == NULL) {
2614                 if (ppp->flags & SC_DEBUG)
2615                         printk (KERN_ERR
2616                         "ppp: %s not connected to a TTY! can't go open!\n",
2617                         dev->name);
2618                 return -ENXIO;
2619         }
2620
2621         if (ppp->flags & SC_DEBUG)
2622                 printk (KERN_INFO
2623                         "ppp: channel %s going up for IP packets!\n",
2624                         dev->name);
2625
2626         CHECK_PPP (-ENXIO);
2627         return 0;
2628 }
2629
2630 /*
2631  * Callback from the network layer when the ppp device goes down.
2632  */
2633
2634 static int
2635 ppp_dev_close (struct device *dev)
2636 {
2637         struct ppp *ppp = dev2ppp (dev);
2638
2639         if (ppp2tty (ppp) == NULL) {
2640                 return -ENXIO;
2641         }
2642 /*
2643  * We don't do anything about the device going down. It is not important
2644  * for us.
2645  */
2646         if (ppp->flags & SC_DEBUG)
2647                 printk (KERN_INFO
2648                         "ppp: channel %s going down for IP packets!\n",
2649                         dev->name);
2650         CHECK_PPP (-ENXIO);
2651         return 0;
2652 }
2653
2654 /*
2655  * IOCTL operation to read the version of the driver.
2656  */
2657
2658 static int
2659 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2660 {
2661         int error;
2662         int len;
2663         char *result;
2664 /*
2665  * Must have write access to the buffer.
2666  */
2667         result = (char *) ifr->ifr_ifru.ifru_data;
2668         len    = strlen (szVersion) + 1;
2669         error  = verify_area (VERIFY_WRITE, result, len);
2670 /*
2671  * Move the version data
2672  */
2673         if (error == 0)
2674                 memcpy_tofs (result, szVersion, len);
2675
2676         return error;
2677 }
2678
2679 /*
2680  * IOCTL to read the statistics for the pppstats program.
2681  */
2682
2683 static int
2684 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2685 {
2686         struct ppp_stats *result, temp;
2687         int    error;
2688 /*
2689  * Must have write access to the buffer.
2690  */
2691         result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2692         error = verify_area (VERIFY_WRITE,
2693                              result,
2694                              sizeof (temp));
2695 /*
2696  * Supply the information for the caller. First move the version data
2697  * then move the ppp stats; and finally the vj stats.
2698  */
2699         memset (&temp, 0, sizeof(temp));
2700         if (error == 0 && dev->flags & IFF_UP) {
2701                 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2702                 if (ppp->slcomp != NULL) {
2703                         temp.vj.vjs_packets    = ppp->slcomp->sls_o_compressed+
2704                                                  ppp->slcomp->sls_o_uncompressed;
2705                         temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2706                         temp.vj.vjs_searches   = ppp->slcomp->sls_o_searches;
2707                         temp.vj.vjs_misses     = ppp->slcomp->sls_o_misses;
2708                         temp.vj.vjs_errorin    = ppp->slcomp->sls_i_error;
2709                         temp.vj.vjs_tossed     = ppp->slcomp->sls_i_tossed;
2710                         temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2711                         temp.vj.vjs_compressedin   = ppp->slcomp->sls_i_compressed;
2712                 }
2713         }
2714
2715         if (error == 0)
2716                 memcpy_tofs (result, &temp, sizeof (temp));
2717         return error;
2718 }
2719
2720 /*
2721  * IOCTL to read the compression statistics for the pppstats program.
2722  */
2723
2724 static int
2725 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2726 {
2727         struct ppp_comp_stats *result, temp;
2728         int    error;
2729 /*
2730  * Must have write access to the buffer.
2731  */
2732         result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2733         error = verify_area (VERIFY_WRITE,
2734                              result,
2735                              sizeof (temp));
2736 /*
2737  * Supply the information for the caller.
2738  */
2739         memset (&temp, 0, sizeof(temp));
2740         if (error == 0 && dev->flags & IFF_UP) {
2741                 if (ppp->sc_xc_state != NULL)
2742                         (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2743                                                      &temp.c);
2744
2745                 if (ppp->sc_rc_state != NULL)
2746                         (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2747                                                        &temp.d);
2748         }
2749 /*
2750  * Move the data to the caller's buffer
2751  */
2752         if (error == 0)
2753                 memcpy_tofs (result, &temp, sizeof (temp));
2754         return error;
2755 }
2756
2757 /*
2758  * Callback from the network layer to process the sockioctl functions.
2759  */
2760
2761 static int
2762 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2763 {
2764         struct ppp *ppp = dev2ppp (dev);
2765         int error;
2766 /*
2767  * Process the requests
2768  */
2769         switch (cmd) {
2770         case SIOCGPPPSTATS:
2771                 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2772                 break;
2773
2774         case SIOCGPPPCSTATS:
2775                 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2776                 break;
2777
2778         case SIOCGPPPVER:
2779                 error = ppp_dev_ioctl_version (ppp, ifr);
2780                 break;
2781
2782         default:
2783                 error = -EINVAL;
2784                 break;
2785         }
2786         return error;
2787 }
2788
2789 /*
2790  * Send an IP frame to the remote with vj header compression.
2791  *
2792  * Return 0 if frame was queued for transmission.
2793  *        1 if frame must be re-queued for later driver support.
2794  */
2795
2796 static int
2797 ppp_dev_xmit_ip (struct device *dev, struct ppp *ppp, __u8 *data)
2798 {
2799         int               proto = PPP_IP;
2800         int               len;
2801         struct ppp_hdr    *hdr;
2802         struct tty_struct *tty = ppp2tty (ppp);
2803 /*
2804  * Obtain the length from the IP header.
2805  */
2806         len = ((struct iphdr *)data) -> tot_len;
2807         len = ntohs (len);
2808 /*
2809  * Validate the tty interface
2810  */
2811         if (tty == NULL) {
2812                 if (ppp->flags & SC_DEBUG)
2813                         printk (KERN_ERR
2814                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
2815                                 dev->name);
2816                 return 0;
2817         }
2818 /*
2819  * Ensure that the PPP device is still up
2820  */
2821         if (!(dev->flags & IFF_UP)) {
2822                 if (ppp->flags & SC_DEBUG)
2823                         printk (KERN_WARNING
2824                                 "ppp_dev_xmit: packet sent on interface %s,"
2825                                 " which is down for IP\n",
2826                                 dev->name);
2827                 return 0;
2828         }
2829 /*
2830  * Branch on the type of processing for the IP frame.
2831  */
2832         switch (ppp->sc_npmode[NP_IP]) {
2833         case NPMODE_PASS:
2834                 break;
2835
2836         case NPMODE_ERROR:
2837         case NPMODE_DROP:
2838                 if (ppp->flags & SC_DEBUG)
2839                         printk (KERN_DEBUG
2840                                 "ppp_dev_xmit: npmode = %d on %s\n",
2841                                 ppp->sc_npmode[NP_IP], dev->name);
2842                 return 0;
2843
2844         case NPMODE_QUEUE:
2845                 break;
2846
2847         default:
2848                 if (ppp->flags & SC_DEBUG)
2849                         printk (KERN_WARNING
2850                                 "ppp_dev_xmit: unknown npmode %d on %s\n",
2851                                 ppp->sc_npmode[NP_IP],
2852                                 dev->name);
2853                 return 0;
2854         }
2855 /*
2856  * Detect a change in the transfer size
2857  */
2858         if (ppp->mtu != ppp2dev (ppp)->mtu) {
2859                 ppp_changedmtu (ppp,
2860                                 ppp2dev (ppp)->mtu,
2861                                 ppp->mru);
2862         }
2863 /*
2864  * Acquire the lock on the transmission buffer. If the buffer was busy then
2865  * mark the device as busy.
2866  */
2867         if (lock_buffer (ppp->wbuf) != 0) {
2868                 dev->tbusy = 1;
2869                 return 1;
2870         }
2871 /*
2872  * Print the frame being sent
2873  */
2874         if (ppp->flags & SC_LOG_OUTPKT)
2875                 ppp_print_buffer ("ppp outpkt", data, len);
2876 /*
2877  * At this point, the buffer will be transmitted. There is no other exit.
2878  *
2879  * Try to compress the header.
2880  */
2881         if (ppp->flags & SC_COMP_TCP) {
2882                 len = slhc_compress (ppp->slcomp, data, len,
2883                                      buf_base (ppp->cbuf) + PPP_HARD_HDR_LEN,
2884                                      &data,
2885                                      (ppp->flags & SC_NO_TCP_CCID) == 0);
2886
2887                 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2888                         proto    = PPP_VJC_COMP;
2889                         data[0] ^= SL_TYPE_COMPRESSED_TCP;
2890                 } else {
2891                         if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2892                                 proto = PPP_VJC_UNCOMP;
2893                         data[0] = (data[0] & 0x0f) | 0x40;
2894                 }
2895         }
2896 /*
2897  * Send the frame
2898  */
2899         len  += PPP_HARD_HDR_LEN;
2900         hdr   = &((struct ppp_hdr *) data)[-1];
2901
2902         hdr->address     = PPP_ALLSTATIONS;
2903         hdr->control     = PPP_UI;
2904         hdr->protocol[0] = 0;
2905         hdr->protocol[1] = proto;
2906
2907         return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
2908 }
2909
2910 /*
2911  * Send an IPX (or any other non-IP) frame to the remote.
2912  *
2913  * Return 0 if frame was queued for transmission.
2914  *        1 if frame must be re-queued for later driver support.
2915  */
2916 static int
2917 ppp_dev_xmit_ipx (struct device *dev, struct ppp *ppp,
2918                   __u8 *data, int len, int proto)
2919 {
2920         struct tty_struct *tty = ppp2tty (ppp);
2921         struct ppp_hdr    *hdr;
2922 /*
2923  * Validate the tty interface
2924  */
2925         if (tty == NULL) {
2926                 if (ppp->flags & SC_DEBUG)
2927                         printk (KERN_ERR
2928                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
2929                                 dev->name);
2930                 return 0;
2931         }
2932 /*
2933  * Ensure that the PPP device is still up
2934  */
2935         if (!(dev->flags & IFF_UP)) {
2936                 if (ppp->flags & SC_DEBUG)
2937                         printk (KERN_WARNING
2938                                 "ppp_dev_xmit: packet sent on interface %s,"
2939                                 " which is down\n",
2940                                 dev->name);
2941                 return 0;
2942         }
2943 /*
2944  * Detect a change in the transfer size
2945  */
2946         if (ppp->mtu != ppp2dev (ppp)->mtu) {
2947                 ppp_changedmtu (ppp,
2948                                 ppp2dev (ppp)->mtu,
2949                                 ppp->mru);
2950         }
2951 /*
2952  * Acquire the lock on the transmission buffer. If the buffer was busy then
2953  * mark the device as busy.
2954  */
2955         if (lock_buffer (ppp->wbuf) != 0) {
2956                 dev->tbusy = 1;
2957                 return 1;
2958         }
2959 /*
2960  * Print the frame being sent
2961  */
2962         if (ppp->flags & SC_LOG_OUTPKT)
2963                 ppp_print_buffer ("ppp outpkt", data, len);
2964 /*
2965  * Send the frame
2966  */
2967         len  += PPP_HARD_HDR_LEN;
2968         hdr   = &((struct ppp_hdr *) data)[-1];
2969
2970         hdr->address     = PPP_ALLSTATIONS;
2971         hdr->control     = PPP_UI;
2972         hdr->protocol[0] = proto >> 8;
2973         hdr->protocol[1] = proto;
2974
2975         return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
2976 }
2977
2978 /*
2979  * Send a frame to the remote.
2980  */
2981
2982 static int
2983 ppp_dev_xmit (sk_buff *skb, struct device *dev)
2984 {
2985         int answer, len;
2986         __u8              *data;
2987         struct ppp        *ppp = dev2ppp (dev);
2988         struct tty_struct *tty = ppp2tty (ppp);
2989 /*
2990  * just a little sanity check.
2991  */
2992         if (skb == NULL) {
2993                 if (ppp->flags & SC_DEBUG)
2994                         printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
2995                 return 0;
2996         }
2997 /*
2998  * Avoid timing problem should tty hangup while data is queued to be sent
2999  */
3000         if (!ppp->inuse) {
3001                 dev_kfree_skb (skb, FREE_WRITE);
3002                 dev_close (dev);
3003                 return 0;
3004         }
3005 /*
3006  * Validate the tty interface
3007  */
3008         if (tty == NULL) {
3009                 if (ppp->flags & SC_DEBUG)
3010                         printk (KERN_ERR
3011                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
3012                                 dev->name);
3013                 dev_kfree_skb (skb, FREE_WRITE);
3014                 return 0;
3015         }
3016 /*
3017  * Fetch the pointer to the data
3018  */
3019         len   = skb->len;
3020         data  = skb_data(skb);
3021 /*
3022  * Look at the protocol in the skb to determine the difference between
3023  * an IP frame and an IPX frame.
3024  */
3025         switch (ntohs (skb->protocol)) {
3026         case ETH_P_IPX:
3027                 answer = ppp_dev_xmit_ipx (dev, ppp, data, len, PPP_IPX);
3028                 break;
3029
3030         case ETH_P_IP:
3031                 answer = ppp_dev_xmit_ip (dev, ppp, data);
3032                 break;
3033
3034         default: /* All others have no support at this time. */
3035                 dev_kfree_skb (skb, FREE_WRITE);
3036                 return 0;
3037         }
3038 /*
3039  * This is the end of the transmission. Release the buffer if it was sent.
3040  */
3041         if (answer == 0) {
3042                 dev_kfree_skb (skb, FREE_WRITE);
3043                 ppp->ddinfo.xmit_idle = jiffies;
3044         }
3045         return answer;
3046 }
3047
3048 /*
3049  * Generate the statistic information for the /proc/net/dev listing.
3050  */
3051
3052 static struct enet_statistics *
3053 ppp_dev_stats (struct device *dev)
3054 {
3055         struct ppp *ppp = dev2ppp (dev);
3056
3057         ppp->estats.rx_packets        = ppp->stats.ppp_ipackets;
3058         ppp->estats.rx_errors         = ppp->stats.ppp_ierrors;
3059         ppp->estats.tx_packets        = ppp->stats.ppp_opackets;
3060         ppp->estats.tx_errors         = ppp->stats.ppp_oerrors;
3061
3062         return &ppp->estats;
3063 }
3064
3065 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3066                            __u16 type, void *daddr,
3067                            void *saddr, unsigned int len)
3068 {
3069         return (0);
3070 }
3071
3072 static int
3073 ppp_dev_rebuild (void *eth, struct device *dev,
3074                  unsigned long raddr, struct sk_buff *skb)
3075 {
3076         return (0);
3077 }
3078
3079 /*************************************************************
3080  * UTILITIES
3081  *    Miscellany called by various functions above.
3082  *************************************************************/
3083
3084 /* Locate the previous instance of the PPP channel */
3085 static struct ppp *
3086 ppp_find (int pid_value)
3087 {
3088         int             if_num;
3089         ppp_ctrl_t      *ctl;
3090         struct ppp      *ppp;
3091
3092         /* try to find the exact same free device which we had before */
3093         ctl      = ppp_list;
3094         if_num   = 0;
3095
3096         while (ctl) {
3097                 ppp = ctl2ppp (ctl);
3098                 if (ppp->sc_xfer == pid_value) {
3099                         set_bit(0, &ppp->inuse);
3100                         ppp->sc_xfer = 0;
3101                         return (ppp);
3102                 }
3103                 ctl = ctl->next;
3104                 if (++if_num == max_dev)
3105                         break;
3106         }
3107         return NULL;
3108 }
3109
3110 /* allocate or create a PPP channel */
3111 static struct ppp *
3112 ppp_alloc (void)
3113 {
3114         int             if_num;
3115         int             status;
3116         ppp_ctrl_t      *ctl;
3117         struct device   *dev;
3118         struct ppp      *ppp;
3119
3120         /* try to find an free device */
3121         ctl      = ppp_list;
3122         if_num   = 0;
3123   
3124         while (ctl) {
3125                 ppp = ctl2ppp (ctl);
3126                 if (!set_bit(0, &ppp->inuse))
3127                         return (ppp);
3128                 ctl = ctl->next;
3129                 if (++if_num == max_dev)
3130                         return (NULL);
3131         }
3132 /*
3133  * There are no available items. Allocate a device from the system pool
3134  */
3135         ctl = (ppp_ctrl_t *) kmalloc (sizeof(ppp_ctrl_t), GFP_KERNEL);
3136         if (ctl) {
3137                 (void) memset(ctl, 0, sizeof(ppp_ctrl_t));
3138                 ppp = ctl2ppp (ctl);
3139                 dev = ctl2dev (ctl);
3140
3141                 /* initialize channel control data */
3142                 set_bit(0, &ppp->inuse);
3143
3144                 ppp->line      = if_num;
3145                 ppp->tty       = NULL;
3146                 ppp->backup_tty = NULL;
3147                 ppp->dev       = dev;
3148     
3149                 dev->next      = NULL;
3150                 dev->init      = ppp_init_dev;
3151                 dev->name      = ctl->name;
3152                 dev->base_addr = (__u32) if_num;
3153                 dev->priv      = (void *) ppp;
3154
3155                 sprintf (dev->name, "ppp%d", if_num);
3156     
3157                 /* link in the new channel */
3158                 ctl->next      = ppp_list;
3159                 ppp_list       = ctl;
3160
3161                 /* register device so that we can be ifconfig'd */
3162                 /* ppp_init_dev() will be called as a side-effect */
3163
3164                 status = register_netdev (dev);
3165                 if (status == 0) {
3166                         printk (KERN_INFO "registered device %s\n", dev->name);
3167                         return (ppp);
3168                 }
3169
3170                 printk (KERN_ERR
3171                        "ppp_alloc - register_netdev(%s) = %d failure.\n",
3172                         dev->name, status);
3173                 /* This one will forever be busy as it is not initialized */
3174         }
3175         return (NULL);
3176 }
3177
3178 /*
3179  * Utility procedures to print a buffer in hex/ascii
3180  */
3181
3182 static void
3183 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3184 {
3185         register __u8 next_ch;
3186         static char hex[] = "0123456789ABCDEF";
3187
3188         while (count-- > 0) {
3189                 next_ch = *in++;
3190                 *out++ = hex[(next_ch >> 4) & 0x0F];
3191                 *out++ = hex[next_ch & 0x0F];
3192                 ++out;
3193         }
3194 }
3195
3196 static void
3197 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3198 {
3199         register __u8 next_ch;
3200
3201         while (count-- > 0) {
3202                 next_ch = *in++;
3203
3204                 if (next_ch < 0x20 || next_ch > 0x7e)
3205                         *out++ = '.';
3206                 else {
3207                         *out++ = next_ch;
3208                         if (next_ch == '%')   /* printk/syslogd has a bug !! */
3209                                 *out++ = '%';
3210                 }
3211         }
3212         *out = '\0';
3213 }
3214
3215 static void
3216 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3217 {
3218         __u8 line[44];
3219
3220         if (name != (__u8 *) NULL)
3221                 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3222
3223         while (count > 8) {
3224                 memset (line, 32, 44);
3225                 ppp_print_hex (line, buf, 8);
3226                 ppp_print_char (&line[8 * 3], buf, 8);
3227                 printk (KERN_DEBUG "%s\n", line);
3228                 count -= 8;
3229                 buf += 8;
3230         }
3231
3232         if (count > 0) {
3233                 memset (line, 32, 44);
3234                 ppp_print_hex (line, buf, count);
3235                 ppp_print_char (&line[8 * 3], buf, count);
3236                 printk (KERN_DEBUG "%s\n", line);
3237         }
3238 }
3239
3240 /*************************************************************
3241  * Compressor module interface
3242  *************************************************************/
3243
3244 struct compressor_link {
3245         struct compressor_link  *next;
3246         struct compressor       *comp;
3247 };
3248
3249 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3250
3251 static struct compressor *find_compressor (int type)
3252 {
3253         struct compressor_link *lnk;
3254         unsigned long flags;
3255
3256         save_flags(flags);
3257         cli();
3258
3259         lnk = ppp_compressors;
3260         while (lnk != (struct compressor_link *) 0) {
3261                 if ((int) (__u8) lnk->comp->compress_proto == type) {
3262                         restore_flags(flags);
3263                         return lnk->comp;
3264                 }
3265                 lnk = lnk->next;
3266         }
3267
3268         restore_flags(flags);
3269         return (struct compressor *) 0;
3270 }
3271
3272 static int ppp_register_compressor (struct compressor *cp)
3273 {
3274         struct compressor_link *new;
3275         unsigned long flags;
3276
3277         new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3278
3279         if (new == (struct compressor_link *) 0)
3280                 return 1;
3281
3282         save_flags(flags);
3283         cli();
3284
3285         if (find_compressor (cp->compress_proto)) {
3286                 restore_flags(flags);
3287                 kfree (new);
3288                 return 0;
3289         }
3290
3291         new->next       = ppp_compressors;
3292         new->comp       = cp;
3293         ppp_compressors = new;
3294
3295         restore_flags(flags);
3296         return 0;
3297 }
3298
3299 static void ppp_unregister_compressor (struct compressor *cp)
3300 {
3301         struct compressor_link *prev = (struct compressor_link *) 0;
3302         struct compressor_link *lnk;
3303         unsigned long flags;
3304
3305         save_flags(flags);
3306         cli();
3307
3308         lnk  = ppp_compressors;
3309         while (lnk != (struct compressor_link *) 0) {
3310                 if (lnk->comp == cp) {
3311                         if (prev)
3312                                 prev->next = lnk->next;
3313                         else
3314                                 ppp_compressors = lnk->next;
3315                         kfree (lnk);
3316                         break;
3317                 }
3318                 prev = lnk;
3319                 lnk  = lnk->next;
3320         }
3321         restore_flags(flags);
3322 }
3323
3324 /*************************************************************
3325  * Module support routines
3326  *************************************************************/
3327
3328 #ifdef MODULE
3329 int
3330 init_module(void)
3331 {
3332         int status;
3333
3334         /* register our line disciplines */
3335         status = ppp_first_time();
3336         if (status != 0)
3337                 printk (KERN_INFO
3338                        "PPP: ppp_init() failure %d\n", status);
3339         else
3340                 (void) register_symtab (&ppp_syms);
3341         return (status);
3342 }
3343
3344 void
3345 cleanup_module(void)
3346 {
3347         int status;
3348         ppp_ctrl_t *ctl, *next_ctl;
3349         struct device *dev;
3350         struct ppp *ppp;
3351         int busy_flag = 0;
3352 /*
3353  * Ensure that the devices are not in operation.
3354  */
3355         ctl = ppp_list;
3356         while (ctl) {
3357                 ppp = ctl2ppp (ctl);
3358                 if (ppp->inuse && ppp->tty != NULL) {
3359                         busy_flag = 1;
3360                         break;
3361                 }
3362
3363                 dev = ctl2dev (ctl);
3364                 if (dev->start || dev->flags & IFF_UP) {
3365                         busy_flag = 1;
3366                         break;
3367                 }
3368                 ctl = ctl->next;
3369         }
3370 /*
3371  * Ensure that there are no compressor modules registered
3372  */
3373         if (ppp_compressors != NULL)
3374                 busy_flag = 1;
3375
3376         if (busy_flag) {
3377                 printk (KERN_INFO
3378                         "PPP: device busy, remove delayed\n");
3379                 return;
3380         }
3381 /*
3382  * Release the tty registration of the line discipline so that no new entries
3383  * may be created.
3384  */
3385         status = tty_register_ldisc (N_PPP, NULL);
3386         if (status != 0)
3387                 printk (KERN_INFO
3388                         "PPP: Unable to unregister ppp line discipline "
3389                         "(err = %d)\n", status);
3390         else
3391                 printk (KERN_INFO
3392                        "PPP: ppp line discipline successfully unregistered\n");
3393 /*
3394  * De-register the devices so that there is no problem with them
3395  */     
3396         next_ctl = ppp_list;
3397         while (next_ctl) {
3398                 ctl      = next_ctl;
3399                 next_ctl = ctl->next;
3400                 ppp      = ctl2ppp (ctl);
3401                 dev      = ctl2dev (ctl);
3402
3403                 ppp_release       (ppp);
3404                 unregister_netdev (dev);
3405                 kfree (ctl);
3406         }
3407 }
3408 #endif