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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [arch/] [armnommu/] [drivers/] [net/] [ppp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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