updated linux kernel driver for 2.2, from Al Longyear
[ppp.git] / linux / ppp.c
1 /*
2    PPP for Linux
3
4    $Id: ppp.c,v 1.3 1994/12/08 02:03:55 paulus Exp $
5 */
6
7 /*
8    Sources:
9
10    slip.c
11
12    RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
13    Multi-protocol Datagrams over Point-to-Point Links
14
15    RFC1332: IPCP
16
17    ppp-2.0
18
19    Flags for this module (any combination is acceptable for testing.):
20
21    OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
22                         character. This is normally set to ((HZ * 3) / 2).
23                         This is 1.5 seconds. If not defined then the leading
24                         flag is always sent.
25
26    CHECK_CHARACTERS   - Enable the checking on all received characters for
27                         8 data bits, no parity. This adds a small amount of
28                         processing for each received character.
29                         
30    NEW_SKBUFF         - Use NET3.020 sk_buff's
31 */
32
33 /* #define NEW_SKBUFF */
34 #define OPTIMIZE_FLAG_TIME  ((HZ * 3)/2)        /* */
35
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
43 #include <linux/in.h>
44 #include <linux/malloc.h>
45 #include <linux/tty.h>
46 #include <linux/errno.h>
47 #include <linux/sched.h>        /* to get the struct task_struct */
48 #include <linux/string.h>       /* used in new tty drivers */
49 #include <linux/signal.h>       /* used in new tty drivers */
50 #include <asm/system.h>
51 #include <asm/bitops.h>
52 #include <asm/segment.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_route.h>
56 #include <linux/if_ether.h>
57 #include <linux/netdevice.h>
58 #include <linux/skbuff.h>
59 #include <linux/inet.h>
60 #include <linux/ioctl.h>
61
62 #ifdef NEW_SKBUFF
63 #include <linux/netprotocol.h>
64 #else
65 #define skb_data(skb)   ((skb)->data)
66 typedef struct sk_buff  sk_buff;
67 #endif
68
69 #include <ip.h>
70 #include <tcp.h>
71 #include <linux/if_arp.h>
72 #include "slhc.h"
73 #include <net/ppp_defs.h>
74 #include <linux/socket.h>
75 #include <net/if_ppp.h>
76 #include <net/if_pppvar.h>
77
78 #ifndef PPP_IPX
79 #define PPP_IPX 0x2b  /* IPX protocol over PPP */
80 #endif
81
82 /*
83  * Local functions
84  */
85
86 static void ppp_init_ctrl_blk (register struct ppp *);
87 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
88 static int ppp_doframe (struct ppp *);
89 static int ppp_do_ip (struct ppp *, unsigned short, u_char *, int);
90 static int ppp_us_queue (struct ppp *, unsigned short, u_char *, int);
91 static struct ppp *ppp_alloc (void);
92 static void ppp_print_buffer (const u_char *, u_char *, int, int);
93 extern inline void ppp_stuff_char (struct ppp *ppp,
94                                    register struct ppp_buffer *buf,
95                                    register u_char chr);
96 extern inline int lock_buffer (register struct ppp_buffer *buf);
97
98 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (u_char)(c))
99
100 /*
101  * The "main" procedure to the ppp device
102  */
103
104 int ppp_init (struct device *);
105
106 /*
107  * Network device driver callback routines
108  */
109
110 static int ppp_dev_open (struct device *);
111 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
112 static int ppp_dev_close (struct device *);
113 static int ppp_dev_xmit (sk_buff *, struct device *);
114 static struct enet_statistics *ppp_dev_stats (struct device *);
115
116 #ifdef NEW_SKBUFF
117 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
118                           sk_buff *skb, void *saddr, void *daddr);
119 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
120                            int subid, void *saddr, void *daddr, void *opt);
121 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key);
122 #else
123 static int ppp_dev_header (u_char *, struct device *, unsigned short,
124                            void *, void *, unsigned, sk_buff *);
125 static int ppp_dev_rebuild (void *, struct device *, unsigned long,
126                             sk_buff *);
127 static unsigned short ppp_dev_type (sk_buff *, struct device *);
128 #endif
129
130 /*
131  * TTY callbacks
132  */
133
134 static int ppp_tty_read (struct tty_struct *, struct file *, u_char *,
135                          unsigned int);
136 static int ppp_tty_write (struct tty_struct *, struct file *, u_char *,
137                           unsigned int);
138 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
139                           unsigned long);
140 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
141                       struct file *filp, int sel_type, select_table * wait);
142 static int ppp_tty_open (struct tty_struct *);
143 static void ppp_tty_close (struct tty_struct *);
144 static int ppp_tty_room (struct tty_struct *tty);
145 static void ppp_tty_receive (struct tty_struct *tty, u_char * cp,
146                              char *fp, int count);
147 static void ppp_tty_wakeup (struct tty_struct *tty);
148
149
150 #define PRINTK(p) printk p ;
151 #define ASSERT(p) if (!p) PRINTK ((KERN_CRIT "assertion failed: " # p))
152 #define PRINTKN(n,p) {if (ppp_debug >= n) PRINTK (p)}
153 #define CHECK_PPP(a)  if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return a;}
154 #define CHECK_PPP_VOID()  if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return;}
155
156 #define in_xmap(ppp,c)  (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
157 #define in_rmap(ppp,c)  ((((unsigned int) (u_char) (c)) < 0x20) && \
158                         ppp->recv_async_map & (1 << (c)))
159
160 #define bset(p,b)       ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
161
162 /* Buffer types */
163 #define BUFFER_TYPE_DEV_RD    0  /* ppp? read buffer      */
164 #define BUFFER_TYPE_TTY_WR    1  /* tty? write buffer     */
165 #define BUFFER_TYPE_DEV_WR    2  /* ppp? write buffer     */
166 #define BUFFER_TYPE_TTY_RD    3  /* tty? read buffer      */
167 #define BUFFER_TYPE_VJ        4  /* vj compression buffer */
168
169 /* Define this string only once for all macro envocations */
170 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
171
172 static int first_time           = 1;
173 static char szVersion[]         = PPP_VERSION;
174 static int ppp_debug            = 5;
175 static int ppp_debug_netpackets = 0;
176 static struct tty_ldisc ppp_ldisc;
177 static struct ppp       ppp_ctrl   [PPP_NRUNIT];
178  
179 #ifdef NEW_SKBUFF
180 struct protocol proto_ppp;
181 #endif
182
183 /* FCS table from RFC1331 */
184
185 static unsigned short fcstab[256] =
186 {
187         0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
188         0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
189         0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
190         0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
191         0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
192         0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
193         0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
194         0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
195         0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
196         0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
197         0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
198         0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
199         0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
200         0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
201         0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
202         0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
203         0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
204         0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
205         0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
206         0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
207         0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
208         0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
209         0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
210         0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
211         0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
212         0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
213         0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
214         0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
215         0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
216         0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
217         0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
218         0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
219 };
220
221 /*************************************************************
222  * INITIALIZATION
223  *************************************************************/
224
225 /* called at boot time for each ppp device */
226
227 int
228 ppp_init (struct device *dev)
229 {
230         struct ppp *ppp;
231         int i;
232
233         ppp = &ppp_ctrl[dev->base_addr];
234
235         if (first_time) {
236                 first_time = 0;
237
238                 printk (KERN_INFO "PPP: version %s (%d channels)"
239 #ifdef NEW_SKBUFF
240                         " NEW_SKBUFF"
241 #endif
242                         "\n", szVersion, PPP_NRUNIT);
243
244                 printk (KERN_INFO
245                         "TCP compression code copyright 1989 Regents of the "
246                         "University of California\n");
247
248                 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
249                 ppp_ldisc.magic         = TTY_LDISC_MAGIC;
250                 ppp_ldisc.open          = ppp_tty_open;
251                 ppp_ldisc.close         = ppp_tty_close;
252                 ppp_ldisc.read          = ppp_tty_read;
253                 ppp_ldisc.write         = ppp_tty_write;
254                 ppp_ldisc.ioctl         = ppp_tty_ioctl;
255                 ppp_ldisc.select        = ppp_tty_select;
256                 ppp_ldisc.receive_room  = ppp_tty_room;
257                 ppp_ldisc.receive_buf   = ppp_tty_receive;
258                 ppp_ldisc.write_wakeup  = ppp_tty_wakeup;
259
260                 i = tty_register_ldisc (N_PPP, &ppp_ldisc);
261                 if (i == 0) {
262                         printk (KERN_INFO "PPP line discipline registered.\n");
263                 } else {
264                         printk (KERN_ERR "error registering line discipline: %d\n", i);
265                 }
266
267 #ifdef NEW_SKBUFF  
268                 memset (&proto_ppp, 0, sizeof (proto_ppp));
269
270                 proto_ppp.name          = "PPP";
271                 proto_ppp.output        = ppp_dev_output;
272                 proto_ppp.input         = ppp_dev_input;
273                 proto_ppp.bh_input      = ppp_dev_input;
274                 proto_ppp.control_event = default_protocol_control;
275                 proto_ppp.get_binding   = ppp_dev_getkey;
276
277                 protocol_register(&proto_ppp);
278 #endif
279
280         }
281         /* initialize PPP control block */
282         ppp_init_ctrl_blk (ppp);
283         ppp->inuse = 0;
284         ppp->line  = dev->base_addr;
285         ppp->tty   = NULL;
286         ppp->dev   = dev;
287
288         /* clear statistics */
289         memset (&ppp->p, '\0', sizeof (struct ppp_stats));
290
291 #ifdef NEW_SKBUFF  
292         dev->default_protocol = &proto_ppp;     /* Our protocol layer is PPP */
293 #else
294         dev->hard_header      = ppp_dev_header;
295         dev->type_trans       = ppp_dev_type;
296         dev->rebuild_header   = ppp_dev_rebuild;
297         dev->hard_header_len  = 0;
298 #endif
299
300         /* device INFO */
301         dev->mtu              = PPP_MTU;
302         dev->hard_start_xmit  = ppp_dev_xmit;
303         dev->open             = ppp_dev_open;
304         dev->stop             = ppp_dev_close;
305         dev->get_stats        = ppp_dev_stats;
306         dev->do_ioctl         = ppp_dev_ioctl;
307         dev->addr_len         = 0;
308         dev->type             = ARPHRD_PPP;
309
310         for (i = 0; i < DEV_NUMBUFFS; i++) {
311                 skb_queue_head_init (&dev->buffs[i]);
312         }
313
314         /* New-style flags */
315         dev->flags      = IFF_POINTOPOINT;
316         dev->family     = AF_INET;
317         dev->pa_addr    = 0;
318         dev->pa_brdaddr = 0;
319         dev->pa_mask    = 0;
320         dev->pa_alen    = sizeof (unsigned long);
321
322         return 0;
323 }
324
325 /*
326  * Local procedure to initialize the ppp structure
327  */
328
329 static void
330 ppp_init_ctrl_blk (register struct ppp *ppp)
331 {
332         ppp->magic  = PPP_MAGIC;
333         ppp->toss   = 0xE0;
334         ppp->escape = 0;
335
336         ppp->flags  = 0;
337         ppp->mtu    = PPP_MTU;
338         ppp->mru    = PPP_MRU;
339
340         memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
341         ppp->xmit_async_map[0] = 0xffffffff;
342         ppp->xmit_async_map[3] = 0x60000000;
343         ppp->recv_async_map    = 0x00000000;
344
345         ppp->rbuf       = NULL;
346         ppp->wbuf       = NULL;
347         ppp->ubuf       = NULL;
348         ppp->cbuf       = NULL;
349         ppp->slcomp     = NULL;
350         ppp->read_wait  = NULL;
351         ppp->write_wait = NULL;
352
353 #ifdef OPTIMIZE_FLAG_TIME   /* ensure flag will always be sent first time */
354         ppp->last_xmit = jiffies - OPTIMIZE_FLAG_TIME;
355 #else
356         ppp->last_xmit = 0;
357 #endif
358
359         /* clear statistics */
360         memset (&ppp->p, '\0', sizeof (struct ppp_stats));
361
362         /* Reset the demand dial information */
363         ppp->ddinfo.ip_sjiffies  =
364         ppp->ddinfo.ip_rjiffies  =
365         ppp->ddinfo.nip_sjiffies =
366         ppp->ddinfo.nip_rjiffies = jiffies;
367 }
368
369 /*
370  * Routine to allocate a buffer for later use by the driver.
371  */
372
373 static struct ppp_buffer *
374 ppp_alloc_buf (int size, int type)
375 {
376         struct ppp_buffer *buf;
377
378         buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
379                                              GFP_ATOMIC);
380
381         if (buf != NULL) {
382                 buf->size   = size - 1; /* Mask for the buffer size */
383                 buf->type   = type;
384                 buf->locked = 0;
385                 buf->count  = 0;
386                 buf->head   = 0;
387                 buf->tail   = 0;
388                 buf->fcs    = PPP_INITFCS;
389         }
390         return (buf);
391 }
392
393 /*
394  * Routine to release the allocated buffer.
395  */
396
397 static void
398 ppp_free_buf (struct ppp_buffer *ptr)
399 {
400         if (ptr != NULL) {
401                 kfree (ptr);
402         }
403 }
404
405 /*
406  * Lock the indicated transmit buffer
407  */
408
409 extern inline int
410 lock_buffer (register struct ppp_buffer *buf)
411 {
412         register int state;
413         int flags;
414 /*
415  * Save the current state and if free then set it to the "busy" state
416  */
417         save_flags (flags);
418         cli ();
419         state = buf->locked;
420         if (state == 0) {
421                 buf->locked = 2;
422         }
423 /*
424  * Restore the flags and return the previous state. 0 implies success.
425  */
426         restore_flags (flags);
427         return (state);
428 }
429
430 /*
431  * MTU has been changed by the IP layer. Unfortunately we are not told
432  * about this, but we spot it ourselves and fix things up. We could be
433  * in an upcall from the tty driver, or in an ip packet queue.
434  */
435
436 static int
437 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
438 {
439         struct device *dev;
440
441         struct ppp_buffer *new_rbuf;
442         struct ppp_buffer *new_wbuf;
443         struct ppp_buffer *new_cbuf;
444         struct ppp_buffer *new_tbuf;
445
446         struct ppp_buffer *old_rbuf;
447         struct ppp_buffer *old_wbuf;
448         struct ppp_buffer *old_cbuf;
449         struct ppp_buffer *old_tbuf;
450
451         int mtu, mru;
452 /*
453  *  Allocate the buffer from the kernel for the data
454  */
455         dev = ppp->dev;
456         mru = new_mru;
457         mtu = (new_mtu * 2) + 20;
458
459         /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
460         if (mru < PPP_MRU) {
461                 mru = PPP_MRU;
462         }
463         mru += 10;
464
465         PRINTKN (2, (KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
466                      dev->name, new_mtu, new_mru));
467
468         new_wbuf = ppp_alloc_buf (mtu + 4, BUFFER_TYPE_DEV_WR);
469         new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
470         new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
471         new_cbuf = ppp_alloc_buf (mru + 4, BUFFER_TYPE_VJ);
472 /*
473  *  If the buffers failed to allocate then complain and release the partial
474  *  allocations.
475  */
476         if (new_wbuf == NULL || new_tbuf == NULL ||
477             new_rbuf == NULL || new_cbuf == NULL) {
478                 PRINTKN (2,(KERN_ERR "ppp: failed to allocate new buffers\n"));
479                 ppp_free_buf (new_wbuf);
480                 ppp_free_buf (new_tbuf);
481                 ppp_free_buf (new_rbuf);
482                 ppp_free_buf (new_cbuf);
483                 return 0;
484         }
485 /*
486  *  Update the pointers to the new buffer structures.
487  */
488         cli ();
489         old_wbuf = ppp->wbuf;
490         old_rbuf = ppp->rbuf;
491         old_cbuf = ppp->cbuf;
492         old_tbuf = ppp->tbuf;
493
494         ppp->wbuf = new_wbuf;
495         ppp->rbuf = new_rbuf;
496         ppp->cbuf = new_cbuf;
497         ppp->tbuf = new_tbuf;
498
499         ppp->rbuf->size -= 80;  /* reserve space for vj header expansion */
500
501         dev->mem_start  = (unsigned long) buf_base (new_wbuf);
502         dev->mem_end    = (unsigned long) (dev->mem_start + mtu);
503         dev->rmem_start = (unsigned long) buf_base (new_rbuf);
504         dev->rmem_end   = (unsigned long) (dev->rmem_start + mru);
505 /*
506  *  Update the parameters for the new buffer sizes
507  */
508         ppp->toss = 0xE0;       /* To ignore characters until new FLAG */
509         ppp->escape = 0;        /* No pending escape character */
510
511         dev->mtu   =
512         ppp->mtu   = new_mtu;
513         ppp->mru   = new_mru;
514
515         ppp->s1buf = NULL;
516         ppp->s2buf = NULL;
517         ppp->xbuf  = NULL;
518
519         ppp->tty->flags &= ~TTY_DO_WRITE_WAKEUP;
520         ppp->flags      &= ~SC_XMIT_BUSY;
521
522         sti ();
523 /*
524  *  Release old buffer pointers
525  */
526         ppp_free_buf (old_rbuf);
527         ppp_free_buf (old_wbuf);
528         ppp_free_buf (old_cbuf);
529         ppp_free_buf (old_tbuf);
530         return 1;
531 }
532
533 /*
534  * Called to release all of the information in the current PPP structure.
535  *
536  * It is called when the ppp device goes down or if it is unable to go
537  * up.
538  */
539
540 static void
541 ppp_release (struct ppp *ppp)
542 {
543         if (ppp->tty != NULL && ppp->tty->disc_data == ppp) {
544                 ppp->tty->disc_data = NULL;     /* Break the tty->ppp link */
545         }
546         if (ppp->dev) {
547                 ppp->dev->flags &= ~IFF_UP;     /* down the device */
548                 ppp->dev->flags |= IFF_POINTOPOINT;
549         }
550         ppp_free_buf (ppp->rbuf);
551         ppp_free_buf (ppp->wbuf);
552         ppp_free_buf (ppp->cbuf);
553         ppp_free_buf (ppp->ubuf);
554         ppp_free_buf (ppp->tbuf);
555
556         ppp->rbuf  =
557         ppp->wbuf  =
558         ppp->cbuf  =
559         ppp->tbuf  =
560         ppp->xbuf  =
561         ppp->s1buf =
562         ppp->s2buf =
563         ppp->ubuf  = NULL;
564
565         if (ppp->slcomp) {
566                 slhc_free (ppp->slcomp);
567                 ppp->slcomp = NULL;
568         }
569         ppp->inuse = 0;
570         ppp->tty = NULL;
571 }
572
573 /*
574  * Device callback.
575  *
576  * Called when the PPP device goes down in response to an ifconfig request.
577  */
578
579 static void
580 ppp_tty_close (struct tty_struct *tty)
581 {
582         struct ppp *ppp = (struct ppp *) tty->disc_data;
583
584         if (ppp == NULL || ppp->magic != PPP_MAGIC) {
585                 PRINTKN (1,
586                      (KERN_WARNING "ppp: trying to close unopened tty!\n"));
587         } else {
588                 CHECK_PPP_VOID ();
589                 PRINTKN (2,
590                   (KERN_INFO "ppp: channel %s closing.\n", ppp->dev->name));
591                 ppp_release (ppp);
592         }
593 }
594
595 /*
596  * TTY callback.
597  *
598  * Called when the tty dicipline is switched to PPP.
599  */
600
601 static int
602 ppp_tty_open (struct tty_struct *tty)
603 {
604         struct ppp *ppp = (struct ppp *) tty->disc_data;
605 /*
606  * There should not be an existing table for this slot.
607  */
608         if (ppp) {
609                 PRINTKN (1, (KERN_ERR
610                       "ppp_tty_open: gack! tty already associated to %s!\n",
611                              ppp->magic == PPP_MAGIC ? ppp->dev->name
612                              : "unknown"));
613                 return -EEXIST;
614         }
615 /*
616  * Allocate the structure from the system
617  */
618         ppp = ppp_alloc ();
619         if (ppp == NULL) {
620                 PRINTKN (1, (KERN_ERR
621                           "ppp_tty_open: couldn't allocate ppp channel\n"));
622                 return -ENFILE;
623         }
624 /*
625  * Initialize the control block
626  */
627         ppp_init_ctrl_blk (ppp);
628         ppp->tty       = tty;
629         tty->disc_data = ppp;
630 /*
631  * Flush any pending characters in the driver and dicipline.
632  */
633         if (tty->ldisc.flush_buffer) {
634                 tty->ldisc.flush_buffer (tty);
635         }
636
637         if (tty->driver.flush_buffer) {
638                 tty->driver.flush_buffer (tty);
639         }
640 /*
641  * Allocate space for the default VJ header compression slots (16)
642  */
643         ppp->slcomp = slhc_init (16, 16);
644         if (ppp->slcomp == NULL) {
645                 PRINTKN (1, (KERN_ERR
646                       "ppp_tty_open: no space for compression buffers!\n"));
647                 ppp_release (ppp);
648                 return -ENOMEM;
649         }
650 /*
651  * Allocate space for the MTU and MRU buffers
652  */
653         if (ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru) == 0) {
654                 ppp_release (ppp);
655                 return -ENOMEM;
656         }
657 /*
658  * Allocate space for a user level buffer
659  */
660         ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
661         if (ppp->ubuf == NULL) {
662                 PRINTKN (1, (KERN_ERR
663                        "ppp_tty_open: no space for user receive buffer\n"));
664                 ppp_release (ppp);
665                 return -ENOMEM;
666         }
667
668         PRINTKN (2, (KERN_INFO "ppp: channel %s open\n", ppp->dev->name));
669         return (ppp->line);
670 }
671
672 /*
673  * Local function to send the next portion of the buffer.
674  *
675  * Called by the tty driver's tty_wakeup function should it be entered
676  * because the partial buffer was transmitted.
677  *
678  * Called by kick_tty to send the initial portion of the buffer.
679  *
680  * Completion processing of the buffer transmission is handled here.
681  */
682
683 static void
684 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
685                      struct ppp_buffer *xbuf)
686 {
687         register int count, actual;
688 /*
689  * Prevent re-entrancy by ensuring that this routine is called only once.
690  */
691         cli ();
692         if (ppp->flags & SC_XMIT_BUSY) {
693                 sti ();
694                 return;
695         }
696         ppp->flags |= SC_XMIT_BUSY;
697         sti ();
698 /*
699  * Send the next block of data to the modem
700  */
701         count = xbuf->count - xbuf->tail;
702         actual = tty->driver.write (tty, 0,
703                                     buf_base (xbuf) + xbuf->tail, count);
704 /*
705  * Terminate transmission of any block which may have an error.
706  * This could occur should the carrier drop.
707  */
708         if (actual < 0) {
709                 ppp->p.ppp_oerrors++;
710                 actual = count;
711         } else {
712                 ppp->bytes_sent += actual;
713         }
714 /*
715  * If the buffer has been transmitted then clear the indicators.
716  */
717         xbuf->tail += actual;
718         if (actual == count) {
719                 xbuf = NULL;
720                 ppp->flags &= ~SC_XMIT_BUSY;
721 /*
722  * Complete the transmisson on the current buffer.
723  */
724                 xbuf = ppp->xbuf;
725                 if (xbuf != NULL) {
726                         tty->flags  &= ~TTY_DO_WRITE_WAKEUP;
727                         xbuf->locked = 0;
728                         ppp->xbuf    = NULL;
729 /*
730  * If the completed buffer came from the device write, then complete the
731  * transmission block.
732  */
733                         if (ppp->dev->flags & IFF_UP) {
734                                 ppp->dev->tbusy = 0;
735                                 mark_bh (NET_BH);
736                                 dev_tint (ppp->dev);
737                         }
738 /*
739  * Wake up the transmission queue for all completion events.
740  */
741                         wake_up_interruptible (&ppp->write_wait);
742 /*
743  * Look at the priorities. Choose a daemon write over the device driver.
744  */
745                         xbuf = ppp->s1buf;
746                         ppp->s1buf = NULL;
747                         if (xbuf == NULL) {
748                                 xbuf = ppp->s2buf;
749                                 ppp->s2buf = NULL;
750                         }
751 /*
752  * If there is a pending buffer then transmit it now.
753  */
754                         if (xbuf != NULL) {
755                                 ppp->flags &= ~SC_XMIT_BUSY;
756                                 ppp_kick_tty (ppp, xbuf);
757                         }
758                 }
759         }
760 /*
761  * Clear the re-entry flag
762  */
763         ppp->flags &= ~SC_XMIT_BUSY;
764 }
765
766 /*
767  * This function is called by the tty driver when the transmit buffer has
768  * additional space. It is used by the ppp code to continue to transmit
769  * the current buffer should the buffer have been partially sent.
770  *
771  * In addition, it is used to send the first part of the buffer since the
772  * logic and the inter-locking would be identical.
773  */
774
775 static void
776 ppp_tty_wakeup (struct tty_struct *tty)
777 {
778         struct ppp_buffer *xbuf;
779         struct ppp *ppp = (struct ppp *) tty->disc_data;
780
781         if (!ppp || ppp->magic != PPP_MAGIC) {
782                 PRINTKN (1, (KERN_ERR "PPP: write_wakeup called but couldn't "
783                              "find PPP struct.\n"));
784                 return;
785         }
786 /*
787  * Ensure that there is a transmission pending. Clear the re-entry flag if
788  * there is no pending buffer. Otherwise, send the buffer.
789  */
790         xbuf = ppp->xbuf;
791         if (xbuf == NULL) {
792                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
793         } else {
794                 ppp_tty_wakeup_code (ppp, tty, xbuf);
795         }
796 }
797
798 /*
799  * This function is called to transmit a buffer to the rmeote. The buffer
800  * is placed on the pending queue if there is presently a buffer being
801  * sent or it is transmitted with the aid of ppp_tty_wakeup.
802  */
803
804 static void
805 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
806 {
807         register int flags;
808 /*
809  * Hold interrupts.
810  */
811         save_flags (flags);
812         cli ();
813 /*
814  * Control the flags which are best performed with the interrupts masked.
815  */
816         xbuf->locked     = 1;
817         xbuf->tail       = 0;
818 /*
819  * If the transmitter is busy then place the buffer on the appropriate
820  * priority queue.
821  */
822         if (ppp->xbuf != NULL) {
823                 if (xbuf->type == BUFFER_TYPE_TTY_WR) {
824                         ppp->s1buf = xbuf;
825                 } else {
826                         ppp->s2buf = xbuf;
827                 }
828                 restore_flags (flags);
829                 return;
830         }
831 /*
832  * If the transmitter is not busy then this is the highest priority frame
833  */
834         ppp->flags      &= ~SC_XMIT_BUSY;
835         ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
836         ppp->xbuf        = xbuf;
837         restore_flags (flags);
838 /*
839  * Do the "tty wakeup_code" to actually send this buffer.
840  */
841         ppp_tty_wakeup_code (ppp, ppp->tty, xbuf);
842 }
843
844 /*************************************************************
845  * TTY INPUT
846  *    The following functions handle input that arrives from
847  *    the TTY.  It recognizes PPP frames and either hands them
848  *    to the network layer or queues them for delivery to a
849  *    user process reading this TTY.
850  *************************************************************/
851
852 #ifdef CHECK_CHARACTERS
853 static unsigned paritytab[8] =
854 {
855         0x96696996, 0x69969669, 0x69969669, 0x96696996,
856         0x69969669, 0x96696996, 0x96696996, 0x69969669
857 };
858 #endif
859
860 /*
861  * Callback function from tty driver. Return the amount of space left
862  * in the receiver's buffer to decide if remote transmitter is to be
863  * throttled.
864  */
865
866 static int
867 ppp_tty_room (struct tty_struct *tty)
868 {
869         return 65536;       /* We can handle an infinite amount of data. :-) */
870 }
871
872 /*
873  * Callback function when data is available at the tty driver.
874  */
875
876 static void
877 ppp_tty_receive (struct tty_struct *tty, u_char * data, char *flags, int count)
878 {
879         register struct ppp *ppp = (struct ppp *) tty->disc_data;
880         register struct ppp_buffer *buf = ppp->rbuf;
881         u_char chr;
882 /*
883  * Verify the table pointer and ensure that the line is still in PPP dicipline.
884  */
885         if (!ppp || ppp->magic != PPP_MAGIC) {
886                 PRINTKN (1, ("PPP: handler called but couldn't find "
887                              "PPP struct.\n"));
888                 return;
889         }
890         CHECK_PPP_VOID ();
891 /*
892  * Print the buffer if desired
893  */
894         if (ppp_debug >= 5) {
895                 ppp_print_buffer ("receive buffer", data, count, KERNEL_DS);
896         }
897 /*
898  * Collect the character and error condition for the character. Set the toss
899  * flag for the first character error.
900  */
901         while (count-- > 0) {
902                 ppp->bytes_rcvd++;
903                 chr = *data++;
904                 if (flags) {
905                         if (*flags && ppp->toss == 0) {
906                                 ppp->toss = *flags;
907                         }
908                         ++flags;
909                 }
910 /*
911  * Set the flags for 8 data bits and no parity.
912  *
913  * Actually, it sets the flags for d7 being 0/1 and parity being even/odd
914  * so that the normal processing would have all flags set at the end of the
915  * session. A missing flag bit would denote an error condition.
916  */
917 #ifdef CHECK_CHARACTERS
918                 if (chr & 0x80) {
919                         ppp->flags |= SC_RCV_B7_1;
920                 } else {
921                         ppp->flags |= SC_RCV_B7_0;
922                 }
923
924                 if (paritytab[chr >> 5] & (1 << (chr & 0x1F))) {
925                         ppp->flags |= SC_RCV_ODDP;
926                 } else {
927                         ppp->flags |= SC_RCV_EVNP;
928                 }
929 #endif
930 /*
931  * Branch on the character. Process the escape character. The sequence ESC ESC
932  * is defined to be ESC.
933  */
934                 switch (chr) {
935                 case PPP_ESCAPE: /* PPP_ESCAPE: invert bit in next character */
936                         ppp->escape = PPP_TRANS;
937                         break;
938 /*
939  * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
940  * then the block is to be ignored. In addition, characters before the very
941  * first FLAG are also tossed by this procedure.
942  */
943                 case PPP_FLAG:  /* PPP_FLAG: end of frame */
944                         ppp->p.ppp_ibytes = ppp->bytes_rcvd;
945                         if (ppp->escape) {
946                                 ppp->toss |= 0x80;
947                         }
948 /*
949  * Process frames which are not to be ignored. If the processing failed,
950  * then clean up the VJ tables.
951  */
952                         if ((ppp->toss & 0x80) != 0 ||
953                             ppp_doframe (ppp) == 0) {
954                                 slhc_toss (ppp->slcomp);
955                         }
956 /*
957  * Reset all indicators for the new frame to follow.
958  */
959                         buf->count  = 0;
960                         buf->fcs    = PPP_INITFCS;
961                         ppp->escape = 0;
962                         ppp->toss   = 0;
963                         break;
964 /*
965  * All other characters in the data come here. If the character is in the
966  * recieve mask then ignore the character.
967  */
968                 default:
969                         if (in_rmap (ppp, chr)) {
970                                 break;
971                         }
972 /*
973  * Adjust the character and if the frame is to be discarded then simply
974  * ignore the character until the ending FLAG is received.
975  */
976                         chr ^= ppp->escape;
977                         ppp->escape = 0;
978
979                         if (ppp->toss != 0) {
980                                 break;
981                         }
982 /*
983  * If the count sent is within reason then store the character, bump the
984  * count, and update the FCS for the character.
985  */
986                         if (buf->count < buf->size) {
987                                 buf_base (buf)[buf->count++] = chr;
988                                 buf->fcs = PPP_FCS (buf->fcs, chr);
989                                 break;
990                         }
991 /*
992  * The peer sent too much data. Set the flags to discard the current frame
993  * and wait for the re-synchronization FLAG to be sent.
994  */
995                         ppp->p.ppp_ierrors++;
996                         ppp->toss |= 0xC0;
997                         break;
998                 }
999         }
1000 }
1001
1002 /* on entry, a received frame is in ppp->rbuf.bufr
1003    check it and dispose as appropriate */
1004
1005 static int
1006 ppp_doframe (struct ppp *ppp)
1007 {
1008         u_short proto;
1009         u_char *data = buf_base (ppp->rbuf);
1010         int count    = ppp->rbuf->count;
1011 /*
1012  * If there is a pending error from the receiver then log it and discard
1013  * the damaged frame.
1014  */
1015         if (ppp->toss) {
1016                 PRINTKN (1, (KERN_WARNING
1017                              "ppp_toss: tossing frame, reason = %d\n",
1018                              ppp->toss));
1019                 ppp->p.ppp_ierrors++;
1020                 return 0;
1021         }
1022 /*
1023  * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1024  * follows each frame.
1025  */
1026         if (count == 0) {
1027                 return 1;
1028         }
1029 /*
1030  * Print the received data.
1031  */
1032         if (ppp_debug >= 3) {
1033                 ppp_print_buffer ("receive frame", data, count, KERNEL_DS);
1034         }
1035 /*
1036  * Generate an error if the frame is too small.
1037  */
1038         if (count < 4) {
1039                 PRINTKN (1, (KERN_WARNING
1040                              "ppp: got runt ppp frame, %d chars\n", count));
1041                 ppp->p.ppp_ierrors++;
1042                 return 1;
1043         }
1044 /*
1045  * Verify the CRC of the frame and discard the CRC characters from the
1046  * end of the buffer.
1047  */
1048         if (ppp->rbuf->fcs != PPP_GOODFCS) {
1049                 PRINTKN (1, (KERN_WARNING
1050                              "ppp: frame with bad fcs, excess = %x\n",
1051                              ppp->rbuf->fcs ^ PPP_GOODFCS));
1052                 ppp->p.ppp_ierrors++;
1053                 return 0;
1054         }
1055         count -= 2;             /* ignore the fcs characters */
1056 /*
1057  * Ignore the leading ADDRESS and CONTROL fields in the frame.
1058  */
1059         if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1060                 data  += 2;
1061                 count -= 2;
1062         }
1063
1064         proto = (u_short) * data++;     /* PROTO compressed */
1065         if (proto & 1) {
1066                 count--;
1067         } else {
1068                 proto = (proto << 8) | (u_short) * data++;
1069                 count -= 2;
1070         }
1071
1072         /* Send the frame to the network if the ppp device is up */
1073         if (ppp->dev->flags & IFF_UP) {
1074                 if (ppp_do_ip (ppp, proto, data, count)) {
1075                         ppp->ddinfo.ip_rjiffies = jiffies;
1076                         return 1;
1077                 }
1078         }
1079
1080         /* If we got here, it has to go to a user process doing a read,
1081            so queue it. */
1082         if (ppp_us_queue (ppp, proto, data, count)) {
1083                 ppp->ddinfo.nip_rjiffies = jiffies;
1084                 return 1;
1085         }
1086
1087         /* couldn't cope. */
1088         PRINTKN (1, (KERN_WARNING
1089              "ppp: dropping packet on the floor: nobody could take it.\n"));
1090         ppp->p.ppp_ierrors++;
1091         return 1;
1092 }
1093
1094 /* Examine packet at C, attempt to pass up to net layer.
1095    PROTO is the protocol field from the PPP frame.
1096    Return 1 if could handle it, 0 otherwise.  */
1097
1098 static int
1099 ppp_do_ip (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1100 {
1101         sk_buff *skb;
1102 /*
1103  * Log the IP information
1104  */
1105         PRINTKN (4, (KERN_DEBUG "ppp_do_ip: proto %x len %d first byte %x\n",
1106                      (int) proto, count, data[0]));
1107
1108         if (ppp_debug_netpackets) {
1109                 PRINTK ((KERN_DEBUG "%s <-- proto %x len %d\n", ppp->dev->name,
1110                          (int) proto, count));
1111         }
1112 /*
1113  * If this is uncompressed IP data then receive the data
1114  */
1115         switch (proto) {
1116         case PPP_IP:
1117                 break;
1118 /*
1119  * For now, reject the IPX frames. Return 1 to indicate that it has been
1120  * processed so that it is simply discarded.
1121  */
1122         case PPP_IPX:
1123                 return 1;
1124 /*
1125  * Process compressed IP frame. If the remote told us to reject frames then
1126  * do so now. Otherwise ensure that there is space in the buffer.
1127  */
1128         case PPP_VJC_COMP:
1129                 if (ppp->flags & SC_REJ_COMP_TCP) {
1130                         return 1;
1131                 }
1132 /*
1133  * Uncompress the header. We now can _guarantee_ that there is room.
1134  */
1135                 count = slhc_uncompress (ppp->slcomp, data, count);
1136                 if (count <= 0) {
1137                         ppp->p.ppp_ierrors++;
1138                         PRINTKN (1, (KERN_NOTICE
1139                                      "ppp: error in VJ decompression\n"));
1140                         return 1;
1141                 }
1142                 proto = PPP_IP;
1143                 break;
1144 /*
1145  * Process uncompressed IP frame
1146  */
1147         case PPP_VJC_UNCOMP:
1148                 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1149                         if (slhc_remember (ppp->slcomp,
1150                                            data, count) <= 0) {
1151                                 ppp->p.ppp_ierrors++;
1152                                 PRINTKN (1, (KERN_NOTICE
1153                                           "ppp: error in VJ memorizing\n"));
1154                                 return 1;
1155                         }
1156                 }
1157                 proto = PPP_IP;
1158                 break;
1159 /*
1160  * The frame is not a valid IP frame. Ignore it.
1161  */
1162         default:
1163                 return 0;
1164         }
1165 /*
1166  * If debugging net packets then print the information. Process the IP
1167  * frames first.
1168  */
1169         if (ppp_debug_netpackets && proto == PPP_IP) {
1170                 struct iphdr *iph = (struct iphdr *) data;
1171                 PRINTK ((KERN_INFO
1172                         "%s <--   src %lx dst %lx len %d\n",
1173                         ppp->dev->name,
1174                         iph->saddr,
1175                         iph->daddr,
1176                         count));
1177         }
1178 /*
1179  * Generate a skb buffer for the new frame.
1180  */
1181         skb = alloc_skb (count, GFP_ATOMIC);
1182         if (skb == NULL) {
1183                 PRINTK ((KERN_ERR
1184                          "ppp_do_ip: packet dropped on %s (no memory)!\n",
1185                          ppp->dev->name));
1186         }
1187 /*
1188  * Move the received data from the input buffer to the skb buffer.
1189  */
1190         else {
1191
1192                 skb->len = count;       /* Store the length */
1193                 skb->dev = ppp->dev;    /* We are the device */
1194                 memcpy ((u_char *) skb_data(skb), data, count); /* move data */
1195 /*
1196  * Tag the frame and kick it to the proper receive routine
1197  */
1198                 skb->free = 1;
1199                 netif_rx (skb); /* Receive the buffer */
1200         }
1201         return 1;
1202 }
1203
1204 /* stuff packet at BUF, length LEN, into the us_rbuff buffer
1205    prepend PROTO information */
1206
1207 static int
1208 ppp_us_queue (struct ppp *ppp, unsigned short proto, u_char * data, int len)
1209 {
1210         int totlen;
1211         register int current_idx;
1212 /*
1213  * The total length includes the protocol data.
1214  * Lock the user information buffer.
1215  */
1216         if (set_bit (0, &ppp->ubuf->locked)) {
1217                 PRINTKN (1, (KERN_NOTICE "ppp_us_queue: can't get lock\n"));
1218                 return 0;
1219         }
1220         current_idx = ppp->ubuf->head;
1221
1222 #define PUTC(c)                                         \
1223 {                                                       \
1224     buf_base (ppp->ubuf) [current_idx++] = (u_char) (c);\
1225     current_idx &= ppp->ubuf->size;                     \
1226     if (current_idx == ppp->ubuf->tail) {               \
1227             goto failure;                               \
1228     }                                                   \
1229 }
1230
1231 /*
1232  * Insert the buffer length (not counted), the protocol, and the data
1233  */
1234         totlen = len + 2;
1235         PUTC (totlen >> 8);
1236         PUTC (totlen);
1237
1238         PUTC (proto >> 8);
1239         PUTC (proto);
1240
1241         while (len-- > 0) {
1242                 PUTC (*data++);
1243         }
1244 #undef PUTC
1245 /*
1246  * The frame is complete. Update the head pointer and wakeup the pppd
1247  * process.
1248  */
1249         ppp->ubuf->head = current_idx;
1250
1251         clear_bit (0, &ppp->ubuf->locked);       /* Unlock the buffer header */
1252         wake_up_interruptible (&ppp->read_wait); /* select() processing */
1253         if (ppp->tty->fasync != NULL) {
1254                 kill_fasync (ppp->tty->fasync, SIGIO);  /* SIGIO processing */
1255         }
1256
1257         PRINTKN (3, (KERN_INFO "ppp: successfully queued %d bytes\n", totlen));
1258         return 1;
1259 /*
1260  * The buffer is full. Unlock the header and return the failure condition.
1261  */
1262       failure:
1263         clear_bit (0, &ppp->ubuf->locked);
1264         PRINTKN (1, (KERN_NOTICE "ppp_us_queue: ran out of buffer space.\n"));
1265         return 0;
1266 }
1267
1268 /*************************************************************
1269  * LINE DISCIPLINE SUPPORT
1270  *    The following functions form support user programs
1271  *    which read and write data on a TTY with the PPP line
1272  *    discipline.  Reading is done from a circular queue,
1273  *    filled by the lower TTY levels.
1274  *************************************************************/
1275
1276 /* read a PPP frame from the us_rbuff circular buffer,
1277    waiting if necessary
1278 */
1279
1280 static int
1281 ppp_tty_read (struct tty_struct *tty, struct file *file, u_char * buf,
1282               unsigned int nr)
1283 {
1284         struct ppp *ppp = (struct ppp *) tty->disc_data;
1285         u_char c;
1286         int len, indx;
1287
1288 #define GETC(c)                                         \
1289 {                                                       \
1290     c = buf_base (ppp->ubuf) [ppp->ubuf->tail++];       \
1291     ppp->ubuf->tail &= ppp->ubuf->size;                 \
1292 }
1293
1294 /*
1295  * Validate the pointer to the PPP structure
1296  */
1297         if (!ppp || ppp->magic != PPP_MAGIC) {
1298                 PRINTKN (1, (KERN_ERR
1299                              "ppp_tty_read: cannnot find ppp channel\n"));
1300                 return -EIO;
1301         }
1302         CHECK_PPP (-ENXIO);
1303
1304         PRINTKN (4, (KERN_DEBUG "ppp_tty_read: called %x num %u\n",
1305                      (unsigned int) buf,
1306                      nr));
1307 /*
1308  * Acquire the read lock.
1309  */
1310         for (;;) {
1311                 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1312                         PRINTKN (3, (KERN_DEBUG
1313                                      "ppp_tty_read: sleeping(ubuf)\n"));
1314
1315                         current->timeout = 0;
1316                         current->state = TASK_INTERRUPTIBLE;
1317                         schedule ();
1318
1319                         if (current->signal & ~current->blocked) {
1320                                 return -EINTR;
1321                         }
1322                         continue;
1323                 }
1324 /*
1325  * Fetch the length of the buffer from the first two bytes.
1326  */
1327                 if (ppp->ubuf->head == ppp->ubuf->tail) {
1328                         len = 0;
1329                 } else {
1330                         GETC (c);
1331                         len = c << 8;
1332                         GETC (c);
1333                         len += c;
1334                 }
1335 /*
1336  * If there is no length then wait for the data to arrive.
1337  */
1338                 if (len == 0) {
1339                         /* no data */
1340                         clear_bit (0, &ppp->ubuf->locked);
1341                         if (file->f_flags & O_NONBLOCK) {
1342                                 PRINTKN (4, (KERN_DEBUG
1343                                   "ppp_tty_read: no data (EWOULDBLOCK)\n"));
1344                                 return -EWOULDBLOCK;
1345                         }
1346                         current->timeout = 0;
1347                         PRINTKN (3, (KERN_DEBUG
1348                                      "ppp_tty_read: sleeping(read_wait)\n"));
1349                         interruptible_sleep_on (&ppp->read_wait);
1350                         if (current->signal & ~current->blocked) {
1351                                 return -EINTR;
1352                         }
1353                         continue;
1354                 }
1355 /*
1356  * Reset the time of the last read operation.
1357  */
1358                 ppp->ddinfo.nip_rjiffies = jiffies;
1359                 PRINTKN (4, (KERN_DEBUG "ppp_tty_read: len = %d\n", len));
1360 /*
1361  * Ensure that the frame will fit within the caller's buffer. If not, then
1362  * discard the frame from the input buffer and return an error to the caller.
1363  */
1364                 if (len + 2 > nr) {
1365                         /* Can't copy it, update us_rbuff_head */
1366                         PRINTKN (1, (KERN_DEBUG
1367                            "ppp: read of %u bytes too small for %d frame\n",
1368                                      nr, len + 2));
1369                         ppp->ubuf->tail += len;
1370                         ppp->ubuf->tail &= ppp->ubuf->size;
1371                         clear_bit (0, &ppp->ubuf->locked);
1372                         ppp->p.ppp_ierrors++;
1373                         return -EOVERFLOW;
1374                 }
1375 /*
1376  * Fake the insertion of the ADDRESS and CONTROL information because these
1377  * were not saved in the buffer.
1378  */
1379                 put_fs_byte (PPP_ALLSTATIONS, buf++);
1380                 put_fs_byte (PPP_UI,          buf++);
1381                 indx = len;
1382 /*
1383  * Copy the received data from the buffer to the caller's area.
1384  */
1385                 while (indx-- > 0) {
1386                         GETC (c);
1387                         put_fs_byte (c, buf++);
1388                 }
1389 /*
1390  * Release the lock and return the character count in the buffer area.
1391  */
1392                 clear_bit (0, &ppp->ubuf->locked);
1393                 len += 2; /* Account for ADDRESS and CONTROL bytes */
1394                 PRINTKN (3,
1395                 (KERN_DEBUG "ppp_tty_read: passing %d bytes up\n", len));
1396                 return len;
1397         }
1398 #undef GETC
1399 }
1400
1401 /* stuff a character into the transmit buffer, using PPP's way of escaping
1402    special characters.
1403    also, update fcs to take account of new character */
1404
1405 extern inline void
1406 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1407                 register u_char chr)
1408 {
1409 /*
1410  * The buffer should not be full.
1411  */
1412         if ((buf->count < 0) || (buf->count > 3000)) {
1413                 PRINTK ((KERN_DEBUG "ppp_stuff_char: %x %d\n",
1414                          (unsigned int) buf->count,
1415                          (unsigned int) buf->count))
1416         }
1417 /*
1418  * Update the FCS and if the character needs to be escaped, do it.
1419  */
1420         buf->fcs = PPP_FCS (buf->fcs, chr);
1421         if (in_xmap (ppp, chr)) {
1422                 chr ^= PPP_TRANS;
1423                 ins_char (buf, PPP_ESCAPE);
1424         }
1425 /*
1426  * Add the character to the buffer.
1427  */
1428         ins_char (buf, chr);
1429 }
1430
1431 /*
1432  * write a frame with NR chars from BUF to TTY
1433  * we have to put the FCS field on ourselves
1434  */
1435
1436 static int
1437 ppp_tty_write (struct tty_struct *tty, struct file *file, u_char * buf,
1438                unsigned int count)
1439 {
1440         struct ppp *ppp = (struct ppp *) tty->disc_data;
1441         int indx;
1442         unsigned short write_fcs;
1443 /*
1444  * Verify the pointer to the PPP data and that the tty is still in PPP mode.
1445  */
1446         if (!ppp || ppp->magic != PPP_MAGIC) {
1447                 PRINTKN (1,(KERN_ERR "ppp_tty_write: cannot find ppp unit\n"));
1448                 return -EIO;
1449         }
1450         CHECK_PPP (-ENXIO);
1451 /*
1452  * Detech a change in the transfer size
1453  */
1454         if (ppp->mtu != ppp->dev->mtu) {    /* Someone has been ifconfigging */
1455                 ppp_changedmtu (ppp,
1456                                 ppp->dev->mtu,
1457                                 ppp->mru);
1458         }
1459 /*
1460  * Ensure that the caller does not wish to send too much.
1461  */
1462         if (count > PPP_MTU) {
1463                 PRINTKN (1, (KERN_WARNING
1464                 "ppp_tty_write: truncating user packet from %u to mtu %d\n",
1465                              count, PPP_MTU));
1466                 count = PPP_MTU;
1467         }
1468 /*
1469  * Print the buffer
1470  */
1471         if (ppp_debug >= 3) {
1472                 ppp_print_buffer ("write frame", buf, count, USER_DS);
1473         }
1474 /*
1475  * lock this PPP unit so we will be the only writer;
1476  * sleep if necessary
1477  */
1478         while (lock_buffer (ppp->tbuf) != 0) {
1479                 current->timeout = 0;
1480                 PRINTKN (3, (KERN_DEBUG "ppp_tty_write: sleeping\n"));
1481                 interruptible_sleep_on (&ppp->write_wait);
1482                 if (current->signal & ~current->blocked) {
1483                         return -EINTR;
1484                 }
1485         }
1486 /*
1487  * OK, locked.  Add the leading FLAG character to the buffer.
1488  */
1489         PRINTKN (4, (KERN_DEBUG "ppp_tty_write: acquired write lock\n"));
1490         ppp->tbuf->count = 0;
1491
1492 #ifdef OPTIMIZE_FLAG_TIME
1493         if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME) {
1494                 ins_char (ppp->tbuf, PPP_FLAG);
1495         }
1496         ppp->last_xmit = jiffies;
1497 #else
1498         ins_char (ppp->tbuf, PPP_FLAG);
1499 #endif
1500 /*
1501  * Add the data for the frame to the buffer.
1502  */
1503         ppp->tbuf->fcs = PPP_INITFCS;
1504         indx = count;
1505         while (indx-- > 0) {
1506                 register char chr = get_fs_byte (buf++);
1507                 ppp_stuff_char (ppp, ppp->tbuf, chr);
1508         }
1509 /*
1510  * Add the trailing CRC and the final flag character
1511  */
1512         write_fcs = ppp->tbuf->fcs ^ 0xFFFF;
1513         ppp_stuff_char (ppp, ppp->tbuf, write_fcs);
1514         ppp_stuff_char (ppp, ppp->tbuf, write_fcs >> 8);
1515
1516         PRINTKN (4, (KERN_DEBUG "ppp_tty_write: fcs is %hx\n", write_fcs));
1517 /*
1518  * Add the trailing FLAG character
1519  */
1520         ins_char (ppp->tbuf, PPP_FLAG);
1521 /*
1522  * Update the time and print the data to the debug log.
1523  */
1524         ppp->ddinfo.nip_sjiffies = jiffies;
1525
1526         if (ppp_debug >= 6) {
1527                 ppp_print_buffer ("xmit buffer",
1528                                   buf_base (ppp->tbuf),
1529                                   ppp->tbuf->count,
1530                                   KERNEL_DS);
1531         } else {
1532                 PRINTKN (4, (KERN_DEBUG
1533                     "ppp_tty_write: writing %d chars\n", ppp->tbuf->count));
1534         }
1535 /*
1536  * Start the transmitter and the request is complete.
1537  */
1538         ppp->p.ppp_obytes += ppp->tbuf->count;
1539         ++ppp->p.ppp_opackets;
1540
1541         ppp_kick_tty (ppp, ppp->tbuf);
1542         return ((int) count);
1543 }
1544
1545 /*
1546  * Process the IOCTL event for the tty device.
1547  */
1548
1549 static int
1550 ppp_tty_ioctl (struct tty_struct *tty, struct file *file, unsigned int param2,
1551                unsigned long param3)
1552 {
1553         struct ppp *ppp = (struct ppp *) tty->disc_data;
1554         register int temp_i = 0;
1555         int error;
1556 /*
1557  * Verify the status of the PPP device.
1558  */
1559         if (!ppp || ppp->magic != PPP_MAGIC) {
1560                 PRINTK ((KERN_ERR
1561                          "ppp_tty_ioctl: can't find PPP block from tty!\n"));
1562                 return -EBADF;
1563         }
1564         CHECK_PPP (-ENXIO);
1565 /*
1566  * The user must have an euid of root to do these requests.
1567  */
1568         if (!suser ()) {
1569                 return -EPERM;
1570         }
1571 /*
1572  * Set the MRU value
1573  */
1574         switch (param2) {
1575         case PPPIOCSMRU:
1576                 error = verify_area (VERIFY_READ, (void *) param3,
1577                                      sizeof (temp_i));
1578                 if (error == 0) {
1579                         PRINTKN (3, (KERN_INFO
1580                                  "ppp_tty_ioctl: set mru to %x\n", temp_i));
1581                         temp_i = (int) get_fs_long (param3);
1582                         if (ppp->mru != temp_i) {
1583                                 ppp_changedmtu (ppp, ppp->dev->mtu, temp_i);
1584                         }
1585                 }
1586                 break;
1587 /*
1588  * Fetch the flags
1589  */
1590         case PPPIOCGFLAGS:
1591                 error = verify_area (VERIFY_WRITE, (void *) param3,
1592                                      sizeof (temp_i));
1593                 if (error == 0) {
1594                         temp_i = (ppp->flags & SC_MASK);
1595 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
1596                         temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
1597                                   SC_RCV_ODDP | SC_RCV_EVNP;
1598 #endif
1599                         put_fs_long ((long) temp_i, param3);
1600                         PRINTKN (3, (KERN_DEBUG
1601                             "ppp_tty_ioctl: get flags: addr %lx flags %x\n",
1602                                      param3, temp_i));
1603                 }
1604                 break;
1605 /*
1606  * Set the flags for the various options
1607  */
1608         case PPPIOCSFLAGS:
1609                 error = verify_area (VERIFY_READ, (void *) param3,
1610                                      sizeof (temp_i));
1611                 if (error == 0) {
1612                         temp_i = (int) get_fs_long (param3);
1613                         ppp->flags ^= ((ppp->flags ^ temp_i) & SC_MASK);
1614                         PRINTKN (3, (KERN_INFO
1615                                "ppp_tty_ioctl: set flags to %x\n", temp_i));
1616                 }
1617                 break;
1618 /*
1619  * Retrieve the transmit async map
1620  */
1621         case PPPIOCGASYNCMAP:
1622                 error = verify_area (VERIFY_WRITE, (void *) param3,
1623                                      sizeof (temp_i));
1624                 if (error == 0) {
1625                         put_fs_long (ppp->xmit_async_map[0], param3);
1626                         PRINTKN (3, (KERN_INFO
1627                         "ppp_tty_ioctl: get asyncmap: addr %lx asyncmap %lx\n",
1628                                      param3, ppp->xmit_async_map[0]));
1629                 }
1630                 break;
1631 /*
1632  * Set the transmit async map
1633  */
1634         case PPPIOCSASYNCMAP:
1635                 error = verify_area (VERIFY_READ, (void *) param3,
1636                                      sizeof (temp_i));
1637                 if (error == 0) {
1638                         ppp->xmit_async_map[0] = get_fs_long (param3);
1639                         PRINTKN (3, (KERN_INFO
1640                                      "ppp_tty_ioctl: set xmit asyncmap %lx\n",
1641                                      ppp->xmit_async_map[0]));
1642                 }
1643                 break;
1644 /*
1645  * Set the receive async map
1646  */
1647         case PPPIOCSRASYNCMAP:
1648                 error = verify_area (VERIFY_READ, (void *) param3,
1649                                      sizeof (temp_i));
1650                 if (error == 0) {
1651                         ppp->recv_async_map = get_fs_long (param3);
1652                         PRINTKN (3, (KERN_INFO
1653                                      "ppp_tty_ioctl: set rcv asyncmap %lx\n",
1654                                      ppp->recv_async_map));
1655                 }
1656                 break;
1657 /*
1658  * Obtain the unit number for this device.
1659  */
1660         case PPPIOCGUNIT:
1661                 error = verify_area (VERIFY_WRITE, (void *) param3,
1662                                      sizeof (temp_i));
1663                 if (error == 0) {
1664                         put_fs_long (ppp->dev->base_addr, param3);
1665                         PRINTKN (3,
1666                                  (KERN_INFO "ppp_tty_ioctl: get unit: %d",
1667                                   ppp->dev->base_addr));
1668                 }
1669                 break;
1670 /*
1671  * Set the debug level
1672  */
1673         case PPPIOCSDEBUG:
1674                 error = verify_area (VERIFY_READ, (void *) param3,
1675                                      sizeof (temp_i));
1676                 if (error == 0) {
1677                         ppp_debug = (int) get_fs_long (param3);
1678                         ppp_debug_netpackets = (ppp_debug & 0xff00) >> 8;
1679                         ppp_debug &= 0xff;
1680                         PRINTKN (1, (KERN_INFO
1681                         "ppp_tty_ioctl: set debug level %d, netpacket %d\n",
1682                                      ppp_debug, ppp_debug_netpackets));
1683                 }
1684                 break;
1685 /*
1686  * Get the debug level
1687  */
1688         case PPPIOCGDEBUG:
1689                 error = verify_area (VERIFY_WRITE, (void *) param3,
1690                                      sizeof (temp_i));
1691                 if (error == 0) {
1692                         put_fs_long ((long) (ppp_debug |
1693                                             (ppp_debug_netpackets << 8)),
1694                                      param3);
1695
1696                         PRINTKN (3, (KERN_INFO
1697                                      "ppp_tty_ioctl: get debug level %d\n",
1698                                   ppp_debug | (ppp_debug_netpackets << 8)));
1699                 }
1700                 break;
1701 /*
1702  * Get the times since the last send/receive frame operation
1703  */
1704         case PPPIOCGTIME:
1705                 error = verify_area (VERIFY_WRITE, (void *) param3,
1706                                      sizeof (struct ppp_ddinfo));
1707                 if (error == 0) {
1708                         struct ppp_ddinfo cur_ddinfo;
1709                         unsigned long cur_jiffies = jiffies;
1710
1711                         /* change absolute times to relative times. */
1712                         cur_ddinfo.ip_sjiffies = cur_jiffies - ppp->ddinfo.ip_sjiffies;
1713                         cur_ddinfo.ip_rjiffies = cur_jiffies - ppp->ddinfo.ip_rjiffies;
1714                         cur_ddinfo.nip_sjiffies = cur_jiffies - ppp->ddinfo.nip_sjiffies;
1715                         cur_ddinfo.nip_rjiffies = cur_jiffies - ppp->ddinfo.nip_rjiffies;
1716
1717                         memcpy_tofs ((void *) param3, &cur_ddinfo,
1718                                      sizeof (struct ppp_ddinfo));
1719                         PRINTKN (3, (KERN_INFO
1720                                  "ppp_tty_ioctl: read demand dial info\n"));
1721                 }
1722                 break;
1723 /*
1724  * Retrieve the extended async map
1725  */
1726         case PPPIOCGXASYNCMAP:
1727                 error = verify_area (VERIFY_WRITE,
1728                                      (void *) param3,
1729                                      sizeof (ppp->xmit_async_map));
1730                 if (error == 0) {
1731                         memcpy_tofs ((void *) param3,
1732                                      ppp->xmit_async_map,
1733                                      sizeof (ppp->xmit_async_map));
1734                         PRINTKN (3, (KERN_INFO
1735                                  "ppp_tty_ioctl: get xasyncmap: addr %lx\n",
1736                                      param3));
1737                 }
1738                 break;
1739 /*
1740  * Set the async extended map
1741  */
1742         case PPPIOCSXASYNCMAP:
1743                 error = verify_area (VERIFY_READ, (void *) param3,
1744                                      sizeof (ppp->xmit_async_map));
1745                 if (error == 0) {
1746                         unsigned long temp_tbl[8];
1747
1748                         memcpy_fromfs (temp_tbl, (void *) param3,
1749                                        sizeof (ppp->xmit_async_map));
1750                         temp_tbl[1]  = 0x00000000;
1751                         temp_tbl[2] &= ~0x40000000;
1752                         temp_tbl[3] |= 0x60000000;
1753
1754                         if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
1755                             (temp_tbl[4] & temp_tbl[5]) != 0 ||
1756                             (temp_tbl[6] & temp_tbl[7]) != 0) {
1757                                 error = -EINVAL;
1758                         } else {
1759                                 memcpy (ppp->xmit_async_map, temp_tbl,
1760                                         sizeof (ppp->xmit_async_map));
1761                                 PRINTKN (3, (KERN_INFO
1762                                          "ppp_tty_ioctl: set xasyncmap\n"));
1763                         }
1764                 }
1765                 break;
1766 /*
1767  * Set the maximum VJ header compression slot number.
1768  */
1769         case PPPIOCSMAXCID:
1770                 error = verify_area (VERIFY_READ, (void *) param3,
1771                                      sizeof (temp_i));
1772                 if (error == 0) {
1773                         temp_i = (int) get_fs_long (param3) + 1;
1774                         PRINTKN (3, (KERN_INFO
1775                                      "ppp_tty_ioctl: set maxcid to %d\n",
1776                                      temp_i));
1777                         if (ppp->slcomp != NULL) {
1778                                 slhc_free (ppp->slcomp);
1779                         }
1780                         ppp->slcomp = slhc_init (temp_i, temp_i);
1781
1782                         if (ppp->slcomp == NULL) {
1783                                 PRINTKN (1, (KERN_ERR
1784                                 "ppp: no space for compression buffers!\n"));
1785                                 ppp_release (ppp);
1786                                 error = -ENOMEM;
1787                         }
1788                 }
1789                 break;
1790 /*
1791  * Allow users to read, but not set, the serial port parameters
1792  */
1793         case TCGETS:
1794         case TCGETA:
1795                 error = n_tty_ioctl (tty, file, param2, param3);
1796                 break;
1797 /*
1798  *  All other ioctl() events will come here.
1799  */
1800         default:
1801                 PRINTKN (1, (KERN_ERR
1802                              "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
1803                              param2,
1804                              param3));
1805
1806                 error = -ENOIOCTLCMD;
1807                 break;
1808         }
1809         return error;
1810 }
1811
1812 /*
1813  * TTY callback.
1814  *
1815  * Process the select() statement for the PPP device.
1816  */
1817
1818 static int
1819 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
1820                 struct file *filp, int sel_type, select_table * wait)
1821 {
1822         struct ppp *ppp = (struct ppp *) tty->disc_data;
1823         int result = 1;
1824 /*
1825  * Verify the status of the PPP device.
1826  */
1827         if (!ppp || ppp->magic != PPP_MAGIC) {
1828                 PRINTK ((KERN_ERR
1829                        "ppp_tty_select: can't find PPP block from tty!\n"));
1830                 return -EBADF;
1831         }
1832         CHECK_PPP (0);
1833 /*
1834  * Branch on the type of select mode. A read request must lock the user
1835  * buffer area.
1836  */
1837         switch (sel_type) {
1838         case SEL_IN:
1839                 if (set_bit (0, &ppp->ubuf->locked) == 0) {
1840                         /* Test for the presence of data in the queue */
1841                         if (ppp->ubuf->head != ppp->ubuf->tail) {
1842                                 clear_bit (0, &ppp->ubuf->locked);
1843                                 break;
1844                         }
1845                         clear_bit (0, &ppp->ubuf->locked);
1846                 }               /* fall through */
1847                 /*
1848  * Exceptions or read errors.
1849  */
1850         case SEL_EX:
1851                 /* Is this a pty link and the remote disconnected? */
1852                 if (tty->flags & (1 << TTY_SLAVE_CLOSED)) {
1853                         break;
1854                 }
1855                 /* Is this a local link and the modem disconnected? */
1856                 if (tty_hung_up_p (filp)) {
1857                         break;
1858                 }
1859                 select_wait (&ppp->read_wait, wait);
1860                 result = 0;
1861                 break;
1862 /*
1863  * Write mode. A write is allowed if there is no current transmission.
1864  */
1865         case SEL_OUT:
1866                 if (ppp->tbuf->locked != 0) {
1867                         select_wait (&ppp->write_wait, wait);
1868                         result = 0;
1869                 }
1870                 break;
1871         }
1872
1873         return result;
1874 }
1875
1876 /*************************************************************
1877  * NETWORK OUTPUT
1878  *    This routine accepts requests from the network layer
1879  *    and attempts to deliver the packets.
1880  *    It also includes various routines we are compelled to
1881  *    have to make the network layer work (arp, etc...).
1882  *************************************************************/
1883
1884 /*
1885  * Callback from the network layer when the device goes up.
1886  */
1887
1888 static int
1889 ppp_dev_open (struct device *dev)
1890 {
1891         struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1892
1893         /* reset POINTOPOINT every time, since dev_close zaps it! */
1894         dev->flags |= IFF_POINTOPOINT;
1895
1896         if (ppp->tty == NULL) {
1897                 PRINTKN (1,
1898                 (KERN_ERR "ppp: %s not connected to a TTY! can't go open!\n",
1899                  dev->name));
1900                 return -ENXIO;
1901         }
1902
1903         PRINTKN (2, (KERN_INFO "ppp: channel %s going up for IP packets!\n",
1904                      dev->name));
1905
1906         CHECK_PPP (-ENXIO);
1907         return 0;
1908 }
1909
1910 /*
1911  * Callback from the network layer when the ppp device goes down.
1912  */
1913
1914 static int
1915 ppp_dev_close (struct device *dev)
1916 {
1917         struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1918
1919         if (ppp->tty == NULL) {
1920                 PRINTKN (1,
1921                 (KERN_ERR "ppp: %s not connected to a TTY! can't go down!\n",
1922                  dev->name));
1923                 return -ENXIO;
1924         }
1925 /*
1926  * We don't do anything about the device going down. It is not important
1927  * for us.
1928  */
1929         PRINTKN (2, (KERN_INFO "ppp: channel %s going down for IP packets!\n",
1930                      dev->name));
1931         CHECK_PPP (-ENXIO);
1932         return 0;
1933 }
1934
1935 /*
1936  * IOCTL operation to read the version of the driver.
1937  */
1938
1939 static int
1940 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
1941 {
1942         int error;
1943         int len;
1944         char *result;
1945 /*
1946  * Must have write access to the buffer.
1947  */
1948         result = (char *) ifr->ifr_ifru.ifru_data;
1949         len    = strlen (szVersion) + 1;
1950         error  = verify_area (VERIFY_WRITE, result, len);
1951 /*
1952  * Move the version data
1953  */
1954         if (error == 0)
1955                 memcpy_tofs (result, szVersion, len);
1956
1957         return error;
1958 }
1959
1960 /*
1961  * IOCTL to read the statistics for the pppstats program.
1962  */
1963
1964 static int
1965 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr)
1966 {
1967         struct ppp_stats *result, temp;
1968         int    error;
1969 /*
1970  * Must have write access to the buffer.
1971  */
1972         result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
1973         error = verify_area (VERIFY_WRITE,
1974                              result,
1975                              sizeof (temp));
1976 /*
1977  * Supply the information for the caller. First move the version data
1978  * then move the ppp stats; and finally the vj stats.
1979  */
1980         if (error == 0) {
1981                 memset (&temp, 0, sizeof(temp));
1982                 memcpy (&temp.p, &ppp->p, sizeof (struct pppstat));
1983 /*
1984  * Header Compression statistics
1985  */
1986                 if (ppp->slcomp != NULL) {
1987                         temp.vj.vjs_packets    = ppp->slcomp->sls_o_nontcp +
1988                                                  ppp->slcomp->sls_o_tcp;
1989                         temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
1990                         temp.vj.vjs_searches   = ppp->slcomp->sls_o_searches;
1991                         temp.vj.vjs_misses     = ppp->slcomp->sls_o_misses;
1992                         temp.vj.vjs_errorin    = ppp->slcomp->sls_i_error;
1993                         temp.vj.vjs_tossed     = ppp->slcomp->sls_i_tossed;
1994                         temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
1995                         temp.vj.vjs_compressedin   = ppp->slcomp->sls_i_compressed;
1996                 }
1997 /*
1998  * Frame data compression statistics
1999  */
2000 #ifdef PPP_COMPRESS
2001                 if (ppp->sc_xc_state != NULL)
2002                         (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2003                                                      &temp.c);
2004
2005                 if (ppp->sc_rc_state != NULL)
2006                         (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2007                                                        &temp.d);
2008 #endif /* PPP_COMPRESS */
2009
2010 /*
2011  * Move the data to the caller's buffer
2012  */
2013                 memcpy_tofs (result, &temp, sizeof (temp));
2014         }
2015         return error;
2016 }
2017
2018 /*
2019  * Callback from the network layer to process the sockioctl functions.
2020  */
2021
2022 static int
2023 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2024 {
2025         struct ppp *ppp = &ppp_ctrl[dev->base_addr];
2026         int error;
2027 /*
2028  * Process the requests
2029  */
2030         switch (cmd) {
2031         case SIOCGPPPSTATS:
2032                 error = ppp_dev_ioctl_stats (ppp, ifr);
2033                 break;
2034
2035         case SIOCGPPPVER:
2036                 error = ppp_dev_ioctl_version (ppp, ifr);
2037                 break;
2038
2039         default:
2040                 error = -EINVAL;
2041                 break;
2042         }
2043         return error;
2044 }
2045
2046 /*
2047  * Send a frame to the remote.
2048  */
2049
2050 int
2051 ppp_dev_xmit (sk_buff *skb, struct device *dev)
2052 {
2053         struct tty_struct *tty;
2054         struct ppp *ppp;
2055         u_char *data;
2056         unsigned short proto;
2057         int len;
2058         unsigned short write_fcs;
2059 /*
2060  * just a little sanity check.
2061  */
2062         if (skb == NULL) {
2063                 PRINTKN (3, (KERN_WARNING "ppp_dev_xmit: null packet!\n"));
2064                 return 0;
2065         }
2066 /*
2067  * Fetch the poitners to the data
2068  */
2069         ppp   = &ppp_ctrl[dev->base_addr];
2070         tty   = ppp->tty;
2071         data  = (u_char *) (&skb[1]);
2072         len   = skb->len;
2073         proto = PPP_IP;
2074
2075         PRINTKN (4, (KERN_DEBUG "ppp_dev_xmit [%s]: skb %lX busy %d\n",
2076                      dev->name,
2077                      (unsigned long int) skb, ppp->wbuf->locked));
2078
2079         CHECK_PPP (0);
2080 /*
2081  * Validate the tty interface
2082  */
2083         do {
2084                 if (tty == NULL) {
2085                         PRINTKN (1,
2086                         (KERN_ERR "ppp_dev_xmit: %s not connected to a TTY!\n",
2087                                   dev->name));
2088                         break;
2089                 }
2090 /*
2091  * Ensure that the PPP device is still up
2092  */
2093                 if (!(dev->flags & IFF_UP)) {
2094                         PRINTKN (1, (KERN_WARNING
2095                                 "ppp_dev_xmit: packet sent on interface %s,"
2096                                 " which is down for IP\n",
2097                                 dev->name));
2098                         break;
2099                 }
2100 /*
2101  * Detect a change in the transfer size
2102  */
2103                 if (ppp->mtu != ppp->dev->mtu) {
2104                         ppp_changedmtu (ppp,
2105                                         ppp->dev->mtu,
2106                                         ppp->mru);
2107                 }
2108 /*
2109  * Fetch the length from the IP header.
2110  */
2111                 if (len < sizeof (struct iphdr)) {
2112                         PRINTKN (0, (KERN_ERR
2113                             "ppp_dev_xmit: given runt packet, ignoring\n"));
2114                         break;
2115                 }
2116                 len = ntohs (((struct iphdr *) skb_data(skb))->tot_len);
2117 /*
2118  * Acquire the lock on the transmission buffer. If the buffer was busy then
2119  * mark the device as busy and return "failure to send, try back later" error.
2120  */
2121                 if (lock_buffer (ppp->wbuf) != 0) {
2122                         dev->tbusy = 1;
2123                         return 1;
2124                 }
2125 /*
2126  * At this point, the buffer will be transmitted. There is no other exit.
2127  *
2128  * Try to compress the header.
2129  */
2130                 if (ppp->flags & SC_COMP_TCP) {
2131                         /* last 0 argument says don't compress connection ID */
2132                         len = slhc_compress (ppp->slcomp, data, len,
2133                                              buf_base (ppp->cbuf),
2134                                              &data, 0);
2135
2136                         if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2137                                 proto = PPP_VJC_COMP;
2138                         } else {
2139                                 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP) {
2140                                         proto   = PPP_VJC_UNCOMP;
2141                                         data[0] = (data[0] & 0x0f) | 0x40;
2142                                 }
2143                         }
2144                 }
2145
2146                 if (ppp_debug_netpackets) {
2147                         struct iphdr *iph = (struct iphdr *) skb_data(skb);
2148                         PRINTK ((KERN_DEBUG "%s ==> proto %x len %d src %x "
2149                                  "dst %x proto %d\n",
2150                         dev->name, (int) proto, (int) len, (int) iph->saddr,
2151                                  (int) iph->daddr, (int) iph->protocol))
2152                 }
2153 /*
2154  * Insert the leading FLAG character
2155  */
2156                 ppp->wbuf->count = 0;
2157
2158 #ifdef OPTIMIZE_FLAG_TIME
2159                 if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME) {
2160                         ins_char (ppp->wbuf, PPP_FLAG);
2161                 }
2162                 ppp->last_xmit = jiffies;
2163 #else
2164                 ins_char (ppp->wbuf, PPP_FLAG);
2165 #endif
2166
2167                 ppp->wbuf->fcs = PPP_INITFCS;
2168 /*
2169  * Insert the address and control data
2170  */
2171                 if (!(ppp->flags & SC_COMP_AC)) {
2172                         ppp_stuff_char (ppp, ppp->wbuf, PPP_ALLSTATIONS);
2173                         ppp_stuff_char (ppp, ppp->wbuf, PPP_UI);
2174                 }
2175 /*
2176  * Insert the protocol.
2177  */
2178                 if (!(ppp->flags & SC_COMP_PROT) || (proto & 0xff00)) {
2179                         ppp_stuff_char (ppp, ppp->wbuf, proto >> 8);
2180                 }
2181                 ppp_stuff_char (ppp, ppp->wbuf, proto);
2182 /*
2183  * Insert the data
2184  */
2185                 while (len-- > 0) {
2186                         ppp_stuff_char (ppp, ppp->wbuf, *data++);
2187                 }
2188 /*
2189  * Add the trailing CRC and the final flag character
2190  */
2191                 write_fcs = ppp->wbuf->fcs ^ 0xFFFF;
2192                 ppp_stuff_char (ppp, ppp->wbuf, write_fcs);
2193                 ppp_stuff_char (ppp, ppp->wbuf, write_fcs >> 8);
2194
2195                 PRINTKN (4,
2196                       (KERN_DEBUG "ppp_dev_xmit: fcs is %hx\n", write_fcs));
2197 /*
2198  * Add the trailing flag character
2199  */
2200                 ins_char (ppp->wbuf, PPP_FLAG);
2201 /*
2202  * Update the times for the transmission.
2203  */
2204                 ppp->ddinfo.ip_sjiffies = jiffies;
2205 /*
2206  * Print the buffer
2207  */
2208                 if (ppp_debug >= 6) {
2209                         ppp_print_buffer ("xmit buffer", buf_base (ppp->wbuf),
2210                                           ppp->wbuf->count, KERNEL_DS);
2211                 } else {
2212                         PRINTKN (4, (KERN_DEBUG
2213                                      "ppp_dev_xmit: writing %d chars\n",
2214                                      ppp->wbuf->count));
2215                 }
2216 /*
2217  * Send the block to the tty driver.
2218  */
2219                 ppp->p.ppp_obytes += ppp->wbuf->count;
2220                 ++ppp->p.ppp_opackets;
2221                 ppp_kick_tty (ppp, ppp->wbuf);
2222         }
2223         while (0);
2224 /*
2225  * This is the end of the transmission. Release the buffer.
2226  */
2227         dev_kfree_skb (skb, FREE_WRITE);
2228         return 0;
2229 }
2230
2231 static struct enet_statistics *
2232 ppp_dev_stats (struct device *dev)
2233 {
2234         struct ppp *ppp = &ppp_ctrl[dev->base_addr];
2235         static struct enet_statistics ppp_stats;
2236
2237         ppp_stats.rx_packets          = ppp->p.ppp_ipackets;
2238         ppp_stats.rx_errors           = ppp->p.ppp_ierrors;
2239         ppp_stats.rx_dropped          = ppp->p.ppp_ierrors;
2240         ppp_stats.rx_fifo_errors      = 0;
2241         ppp_stats.rx_length_errors    = 0;
2242         ppp_stats.rx_over_errors      = 0;
2243         ppp_stats.rx_crc_errors       = 0;
2244         ppp_stats.rx_frame_errors     = 0;
2245         ppp_stats.tx_packets          = ppp->p.ppp_opackets;
2246         ppp_stats.tx_errors           = ppp->p.ppp_oerrors;
2247         ppp_stats.tx_dropped          = 0;
2248         ppp_stats.tx_fifo_errors      = 0;
2249         ppp_stats.collisions          = 0;
2250         ppp_stats.tx_carrier_errors   = 0;
2251         ppp_stats.tx_aborted_errors   = 0;
2252         ppp_stats.tx_window_errors    = 0;
2253         ppp_stats.tx_heartbeat_errors = 0;
2254
2255         PRINTKN (3, (KERN_INFO "ppp_dev_stats called"));
2256         return &ppp_stats;
2257 }
2258
2259 #ifdef NEW_SKBUFF
2260 /*
2261  *      The PPP protocol is currently pure IP (no IPX yet). This defines
2262  *      the protocol layer which is blank since the driver does all the
2263  *      cooking.
2264  */
2265
2266 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
2267                           sk_buff *skb, void *saddr, void *daddr)
2268 {
2269         return protocol_pass_demultiplex(self, NULL, skb, NULL, NULL);
2270 }
2271
2272 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
2273                            int subid, void *saddr, void *daddr, void *opt)
2274 {
2275         if(skb->dev==NULL)
2276         {
2277                 printk("ppp_dev_output: No device.\n");
2278                 kfree_skb(skb, FREE_WRITE);
2279                 return -1;
2280         }
2281         dev_queue_xmit(skb, skb->dev, skb->priority);
2282         return 0;
2283 }
2284
2285 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key)
2286 {
2287         switch (protocol)
2288         {
2289         case ETH_P_IP:
2290                 return 0;
2291
2292         default:
2293                 break;
2294         }
2295
2296         return -EAFNOSUPPORT;
2297 }
2298
2299 #else
2300
2301 /*
2302  * Called to enquire about the type of the frame in the buffer. Return
2303  * ETH_P_IP for an IP frame, ETH_P_IPX for an IPX frame.
2304  */
2305
2306 static unsigned short
2307 ppp_dev_type (sk_buff *skb, struct device *dev)
2308 {
2309         return (htons (ETH_P_IP));
2310 }
2311
2312 static int
2313 ppp_dev_header (u_char * buff, struct device *dev, unsigned short type,
2314                 void *daddr, void *saddr, unsigned len, sk_buff *skb)
2315 {
2316         return (0);
2317 }
2318
2319 static int
2320 ppp_dev_rebuild (void *buff, struct device *dev, unsigned long raddr,
2321                  sk_buff *skb)
2322 {
2323         return (0);
2324 }
2325 #endif
2326
2327 /*************************************************************
2328  * UTILITIES
2329  *    Miscellany called by various functions above.
2330  *************************************************************/
2331
2332 /* allocate a PPP channel */
2333 static struct ppp *
2334 ppp_alloc (void)
2335 {
2336         int i;
2337         for (i = 0; i < PPP_NRUNIT; i++) {
2338                 if (!set_bit (0, &ppp_ctrl[i].inuse)) {
2339                         return &ppp_ctrl[i];
2340                 }
2341         }
2342         return NULL;
2343 }
2344
2345 /*
2346  * Utility procedures to print a buffer in hex/ascii
2347  */
2348
2349 static void
2350 ppp_print_hex (register u_char * out, u_char * in, int count)
2351 {
2352         register u_char next_ch;
2353         static char hex[] = "0123456789ABCDEF";
2354
2355         while (count-- > 0) {
2356                 next_ch = (u_char) get_fs_byte (in);
2357                 *out++ = hex[(next_ch >> 4) & 0x0F];
2358                 *out++ = hex[next_ch & 0x0F];
2359                 ++out;
2360                 ++in;
2361         }
2362 }
2363
2364 static void
2365 ppp_print_char (register u_char * out, u_char * in, int count)
2366 {
2367         register u_char next_ch;
2368
2369         while (count-- > 0) {
2370                 next_ch = (u_char) get_fs_byte (in);
2371
2372                 if (next_ch < 0x20 || next_ch > 0x7e) {
2373                         *out++ = '.';
2374                 } else {
2375                         *out++ = next_ch;
2376                         if (next_ch == '%') { /* printk/syslogd has a bug !! */
2377                                 *out++ = '%';
2378                         }
2379                 }
2380                 ++in;
2381         }
2382         *out = '\0';
2383 }
2384
2385 static void
2386 ppp_print_buffer (const u_char * name, u_char * buf, int count, int seg)
2387 {
2388         u_char line[44];
2389         int old_fs = get_fs ();
2390
2391         set_fs (seg);
2392
2393         if (name != (u_char *) NULL) {
2394                 PRINTK ((KERN_DEBUG "ppp: %s, count = %d\n", name, count));
2395         }
2396         while (count > 8) {
2397                 memset (line, ' ', sizeof (line));
2398                 ppp_print_hex (line, buf, 8);
2399                 ppp_print_char (&line[8 * 3], buf, 8);
2400                 PRINTK ((KERN_DEBUG "%s\n", line));
2401                 count -= 8;
2402                 buf += 8;
2403         }
2404
2405         if (count > 0) {
2406                 memset (line, ' ', sizeof (line));
2407                 ppp_print_hex (line, buf, count);
2408                 ppp_print_char (&line[8 * 3], buf, count);
2409                 PRINTK ((KERN_DEBUG "%s\n", line));
2410         }
2411         set_fs (old_fs);
2412 }