OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [net/] [ppp.c] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.