Import 2.1.36
[davej-history.git] / drivers / net / ppp.c
bloba1dbb50200a80d06ad215116ea6ee52a90e66c9b
1 /* PPP for Linux
3 * Michael Callahan <callahan@maths.ox.ac.uk>
4 * Al Longyear <longyear@netcom.com>
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>
9 * ==FILEVERSION 970126==
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.
22 Sources:
24 slip.c
26 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
27 Multi-protocol Datagrams over Point-to-Point Links
29 RFC1332: IPCP
31 ppp-2.0
33 Flags for this module (any combination is acceptable for testing.):
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.
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.
45 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
47 #define CHECK_CHARACTERS 1
48 #define PPP_COMPRESS 1
50 #ifndef PPP_MAX_DEV
51 #define PPP_MAX_DEV 256
52 #endif
54 /* $Id: ppp.c,v 1.27 1997/01/26 07:13:29 davem Exp $
55 * Added dynamic allocation of channels to eliminate
56 * compiled-in limits on the number of channels.
58 * Dynamic channel allocation code Copyright 1995 Caldera, Inc.,
59 * released under the GNU General Public License Version 2.
62 #include <linux/config.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/poll.h>
69 #include <linux/interrupt.h>
70 #include <linux/ptrace.h>
71 #include <linux/ioport.h>
72 #include <linux/in.h>
73 #include <linux/malloc.h>
74 #include <linux/tty.h>
75 #include <linux/errno.h>
76 #include <linux/sched.h> /* to get the struct task_struct */
77 #include <linux/string.h> /* used in new tty drivers */
78 #include <linux/signal.h> /* used in new tty drivers */
79 #include <asm/system.h>
80 #include <asm/bitops.h>
81 #include <asm/uaccess.h>
82 #include <linux/if.h>
83 #include <linux/if_ether.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inet.h>
87 #include <linux/ioctl.h>
88 #include <linux/init.h>
90 typedef struct sk_buff sk_buff;
91 #define skb_data(skb) ((__u8 *) (skb)->data)
93 #include <linux/ip.h>
94 #include <linux/tcp.h>
95 #include <linux/if_arp.h>
96 #include <net/slhc_vj.h>
98 #define fcstab ppp_crc16_table /* Name of the table in the kernel */
99 #include <linux/ppp_defs.h>
101 #include <linux/socket.h>
102 #include <linux/if_ppp.h>
103 #include <linux/if_pppvar.h>
105 #undef PACKETPTR
106 #define PACKETPTR 1
107 #include <linux/ppp-comp.h>
108 #undef PACKETPTR
110 #define bsd_decompress (*ppp->sc_rcomp->decompress)
111 #define bsd_compress (*ppp->sc_xcomp->compress)
113 #ifndef PPP_IPX
114 #define PPP_IPX 0x2b /* IPX protocol over PPP */
115 #endif
117 #ifndef PPP_LQR
118 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
119 #endif
121 static int ppp_register_compressor (struct compressor *cp);
122 static void ppp_unregister_compressor (struct compressor *cp);
125 * Local functions
128 static struct compressor *find_compressor (int type);
129 static void ppp_init_ctrl_blk (register struct ppp *);
130 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
131 static int ppp_doframe (struct ppp *);
132 static struct ppp *ppp_alloc (void);
133 static struct ppp *ppp_find (int pid_value);
134 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
135 extern inline void ppp_stuff_char (struct ppp *ppp,
136 register struct ppp_buffer *buf,
137 register __u8 chr);
138 extern inline int lock_buffer (register struct ppp_buffer *buf);
140 static int rcv_proto_ip (struct ppp *, __u16, __u8 *, int);
141 static int rcv_proto_ipx (struct ppp *, __u16, __u8 *, int);
142 static int rcv_proto_vjc_comp (struct ppp *, __u16, __u8 *, int);
143 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
144 static int rcv_proto_unknown (struct ppp *, __u16, __u8 *, int);
145 static int rcv_proto_lqr (struct ppp *, __u16, __u8 *, int);
146 static void ppp_doframe_lower (struct ppp *, __u8 *, int);
147 static int ppp_doframe (struct ppp *);
149 extern int ppp_bsd_compressor_init(void);
150 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
151 static int rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
153 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
155 #ifndef OPTIMIZE_FLAG_TIME
156 #define OPTIMIZE_FLAG_TIME 0
157 #endif
159 #ifndef PPP_MAX_DEV
160 #define PPP_MAX_DEV 256
161 #endif
164 * Parameters which may be changed via insmod.
167 static int flag_time = OPTIMIZE_FLAG_TIME;
168 static int max_dev = PPP_MAX_DEV;
170 MODULE_PARM(flag_time, "i");
171 MODULE_PARM(max_dev, "i");
174 * The "main" procedure to the ppp device
177 int ppp_init (struct device *);
180 * Network device driver callback routines
183 static int ppp_dev_open (struct device *);
184 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
185 static int ppp_dev_close (struct device *);
186 static int ppp_dev_xmit (sk_buff *, struct device *);
187 static struct net_device_stats *ppp_dev_stats (struct device *);
190 * TTY callbacks
193 static int ppp_tty_read (struct tty_struct *, struct file *, __u8 *,
194 unsigned int);
195 static int ppp_tty_write (struct tty_struct *, struct file *, const __u8 *,
196 unsigned int);
197 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
198 unsigned long);
199 static unsigned int ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait);
200 static int ppp_tty_open (struct tty_struct *);
201 static void ppp_tty_close (struct tty_struct *);
202 static int ppp_tty_room (struct tty_struct *tty);
203 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
204 char *fp, int count);
205 static void ppp_tty_wakeup (struct tty_struct *tty);
207 #define CHECK_PPP(a) if (!ppp->inuse) { printk (ppp_warning, __LINE__); return a;}
208 #define CHECK_PPP_VOID() if (!ppp->inuse) { printk (ppp_warning, __LINE__); return;}
210 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
211 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
212 ppp->recv_async_map & (1 << (c)))
214 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
216 #define tty2ppp(tty) ((struct ppp *) (tty->disc_data))
217 #define dev2ppp(dev) ((struct ppp *) (dev->priv))
218 #define ppp2tty(ppp) ((struct tty_struct *) ppp->tty)
219 #define ppp2dev(ppp) ((struct device *) ppp->dev)
221 struct ppp_hdr {
222 __u8 address;
223 __u8 control;
224 __u8 protocol[2];
227 #define PPP_HARD_HDR_LEN (sizeof (struct ppp_hdr))
229 typedef struct ppp_ctrl {
230 struct ppp_ctrl *next; /* Next structure in the list */
231 char name [8]; /* Name of the device */
232 struct ppp ppp; /* PPP control table */
233 struct device dev; /* Device information table */
234 } ppp_ctrl_t;
236 static ppp_ctrl_t *ppp_list = NULL;
238 #define ctl2ppp(ctl) (struct ppp *) &ctl->ppp
239 #define ctl2dev(ctl) (struct device *) &ctl->dev
240 #undef PPP_NRUNIT
242 /* Buffer types */
243 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
244 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
245 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
246 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
247 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
249 /* Define this string only once for all macro invocations */
250 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
252 static char szVersion[] = PPP_VERSION;
255 * Information for the protocol decoder
258 typedef int (*pfn_proto) (struct ppp *, __u16, __u8 *, int);
260 typedef struct ppp_proto_struct {
261 int proto;
262 pfn_proto func;
263 } ppp_proto_type;
265 static
266 ppp_proto_type proto_list[] = {
267 { PPP_IP, rcv_proto_ip },
268 { PPP_IPX, rcv_proto_ipx },
269 { PPP_VJC_COMP, rcv_proto_vjc_comp },
270 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
271 { PPP_LQR, rcv_proto_lqr },
272 { PPP_CCP, rcv_proto_ccp },
273 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
276 __u16 ppp_crc16_table[256] =
278 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
279 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
280 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
281 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
282 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
283 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
284 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
285 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
286 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
287 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
288 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
289 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
290 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
291 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
292 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
293 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
294 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
295 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
296 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
297 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
298 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
299 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
300 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
301 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
302 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
303 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
304 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
305 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
306 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
307 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
308 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
309 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
312 #ifdef CHECK_CHARACTERS
313 static __u32 paritytab[8] =
315 0x96696996, 0x69969669, 0x69969669, 0x96696996,
316 0x69969669, 0x96696996, 0x96696996, 0x69969669
318 #endif
320 /* local function to store a value into the LQR frame */
321 extern inline __u8 * store_long (register __u8 *p, register int value) {
322 *p++ = (__u8) (value >> 24);
323 *p++ = (__u8) (value >> 16);
324 *p++ = (__u8) (value >> 8);
325 *p++ = (__u8) value;
326 return p;
329 /*************************************************************
330 * INITIALIZATION
331 *************************************************************/
333 /* This procedure is called once and once only to define who we are to
334 * the operating system and the various procedures that it may use in
335 * accessing the ppp protocol.
338 __initfunc(static int
339 ppp_first_time (void))
341 static struct tty_ldisc ppp_ldisc;
342 int status;
344 printk (KERN_INFO
345 "PPP: version %s (dynamic channel allocation)"
346 "\n", szVersion);
348 #ifndef MODULE /* slhc module logic has its own copyright announcement */
349 printk (KERN_INFO
350 "TCP compression code copyright 1989 Regents of the "
351 "University of California\n");
352 #endif
354 printk (KERN_INFO
355 "PPP Dynamic channel allocation code copyright 1995 "
356 "Caldera, Inc.\n");
358 * Register the tty discipline
360 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
361 ppp_ldisc.magic = TTY_LDISC_MAGIC;
362 ppp_ldisc.name = "ppp";
363 ppp_ldisc.open = ppp_tty_open;
364 ppp_ldisc.close = ppp_tty_close;
365 ppp_ldisc.read = ppp_tty_read;
366 ppp_ldisc.write = ppp_tty_write;
367 ppp_ldisc.ioctl = ppp_tty_ioctl;
368 ppp_ldisc.poll = ppp_tty_poll;
369 ppp_ldisc.receive_room = ppp_tty_room;
370 ppp_ldisc.receive_buf = ppp_tty_receive;
371 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
373 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
374 if (status == 0)
375 printk (KERN_INFO "PPP line discipline registered.\n");
376 else
377 printk (KERN_ERR "error registering line discipline: %d\n",
378 status);
379 return status;
382 /*************************************************************
383 * INITIALIZATION
384 *************************************************************/
386 /* called when the device is actually created */
388 static int
389 ppp_init_dev (struct device *dev)
391 dev->hard_header_len = PPP_HARD_HDR_LEN;
393 /* device INFO */
394 dev->mtu = PPP_MTU;
395 dev->hard_start_xmit = ppp_dev_xmit;
396 dev->open = ppp_dev_open;
397 dev->stop = ppp_dev_close;
398 dev->get_stats = ppp_dev_stats;
399 dev->do_ioctl = ppp_dev_ioctl;
400 dev->addr_len = 0;
401 dev->tx_queue_len = 10;
402 dev->type = ARPHRD_PPP;
404 dev_init_buffers(dev);
406 /* New-style flags */
407 dev->flags = IFF_POINTOPOINT;
408 dev->family = AF_INET;
409 dev->pa_addr = 0;
410 dev->pa_brdaddr = 0;
411 dev->pa_mask = 0;
412 dev->pa_alen = 4; /* sizeof (__u32) */
414 return 0;
418 * Local procedure to initialize the ppp structure
421 static void
422 ppp_init_ctrl_blk (register struct ppp *ppp)
424 ppp->magic = PPP_MAGIC;
425 ppp->toss = 0xE0;
426 ppp->escape = 0;
428 ppp->flags = 0;
429 ppp->mtu = PPP_MTU;
430 ppp->mru = PPP_MRU;
432 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
433 ppp->xmit_async_map[0] = 0xffffffff;
434 ppp->xmit_async_map[3] = 0x60000000;
435 ppp->recv_async_map = 0x00000000;
437 ppp->rbuf = NULL;
438 ppp->wbuf = NULL;
439 ppp->ubuf = NULL;
440 ppp->cbuf = NULL;
441 ppp->slcomp = NULL;
442 ppp->read_wait = NULL;
443 ppp->write_wait = NULL;
444 ppp->last_xmit = jiffies - flag_time;
446 /* clear statistics */
447 memset (&ppp->stats, '\0', sizeof (struct pppstat));
449 /* Reset the demand dial information */
450 ppp->ddinfo.xmit_idle= /* time since last NP packet sent */
451 ppp->ddinfo.recv_idle=jiffies; /* time since last NP packet received */
453 /* PPP compression data */
454 ppp->sc_xc_state =
455 ppp->sc_rc_state = NULL;
458 EXPORT_SYMBOL(ppp_register_compressor);
459 EXPORT_SYMBOL(ppp_unregister_compressor);
460 EXPORT_SYMBOL(ppp_crc16_table);
462 /* called at boot/load time for each ppp device defined in the kernel */
464 #ifndef MODULE
466 ppp_init (struct device *dev)
468 static int first_time = 1;
469 int answer = 0;
471 if (first_time) {
472 first_time = 0;
473 answer = ppp_first_time();
475 if (answer == 0)
476 answer = -ENODEV;
477 return answer;
479 #endif
482 * Routine to allocate a buffer for later use by the driver.
485 static struct ppp_buffer *
486 ppp_alloc_buf (int size, int type)
488 struct ppp_buffer *buf;
490 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
491 GFP_ATOMIC);
493 if (buf != NULL) {
494 buf->size = size - 1; /* Mask for the buffer size */
495 buf->type = type;
496 buf->locked = 0;
497 buf->count = 0;
498 buf->head = 0;
499 buf->tail = 0;
500 buf->fcs = PPP_INITFCS;
503 return (buf);
507 * Routine to release the allocated buffer.
510 static void
511 ppp_free_buf (struct ppp_buffer *ptr)
513 if (ptr != NULL)
514 kfree (ptr);
518 * Lock the indicated transmit buffer
521 extern inline int
522 lock_buffer (register struct ppp_buffer *buf)
524 register int state;
525 int flags;
527 * Save the current state and if free then set it to the "busy" state
529 save_flags (flags);
530 cli ();
531 state = buf->locked;
532 if (state == 0)
533 buf->locked = 2;
535 restore_flags (flags);
536 return (state);
540 * MTU has been changed by the IP layer. Unfortunately we are not told
541 * about this, but we spot it ourselves and fix things up. We could be
542 * in an upcall from the tty driver, or in an ip packet queue.
545 static int
546 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
548 struct device *dev;
550 struct ppp_buffer *new_rbuf;
551 struct ppp_buffer *new_wbuf;
552 struct ppp_buffer *new_cbuf;
553 struct ppp_buffer *new_tbuf;
555 struct ppp_buffer *old_rbuf;
556 struct ppp_buffer *old_wbuf;
557 struct ppp_buffer *old_cbuf;
558 struct ppp_buffer *old_tbuf;
560 int mtu, mru;
562 * Allocate the buffer from the kernel for the data
564 dev = ppp2dev (ppp);
565 mru = new_mru;
566 /* allow for possible escaping of every character */
567 mtu = (new_mtu * 2) + 20;
569 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
570 if (mru < PPP_MRU)
571 mru = PPP_MRU;
573 mru += 10;
575 if (ppp->flags & SC_DEBUG)
576 printk (KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
577 dev->name, new_mtu, new_mru);
579 new_wbuf = ppp_alloc_buf (mtu+PPP_HARD_HDR_LEN, BUFFER_TYPE_DEV_WR);
580 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
581 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
582 new_cbuf = ppp_alloc_buf (mru+PPP_HARD_HDR_LEN, BUFFER_TYPE_VJ);
584 * If the buffers failed to allocate then complain and release the partial
585 * allocations.
587 if (new_wbuf == NULL || new_tbuf == NULL ||
588 new_rbuf == NULL || new_cbuf == NULL) {
589 if (ppp->flags & SC_DEBUG)
590 printk (KERN_ERR
591 "ppp: failed to allocate new buffers\n");
593 ppp_free_buf (new_wbuf);
594 ppp_free_buf (new_tbuf);
595 ppp_free_buf (new_rbuf);
596 ppp_free_buf (new_cbuf);
597 return 0;
600 * Update the pointers to the new buffer structures.
602 cli ();
603 old_wbuf = ppp->wbuf;
604 old_rbuf = ppp->rbuf;
605 old_cbuf = ppp->cbuf;
606 old_tbuf = ppp->tbuf;
608 ppp->wbuf = new_wbuf;
609 ppp->rbuf = new_rbuf;
610 ppp->cbuf = new_cbuf;
611 ppp->tbuf = new_tbuf;
613 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
615 dev->mem_start = (unsigned long) buf_base (new_wbuf);
616 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
617 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
618 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
620 * Update the parameters for the new buffer sizes
622 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
623 ppp->escape = 0; /* No pending escape character */
625 dev->mtu =
626 ppp->mtu = new_mtu;
627 ppp->mru = new_mru;
629 ppp->s1buf = NULL;
630 ppp->s2buf = NULL;
631 ppp->xbuf = NULL;
633 ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
634 ppp->flags &= ~SC_XMIT_BUSY;
636 sti ();
638 * Release old buffer pointers
640 ppp_free_buf (old_rbuf);
641 ppp_free_buf (old_wbuf);
642 ppp_free_buf (old_cbuf);
643 ppp_free_buf (old_tbuf);
644 return 1;
648 * CCP is down; free (de)compressor state if necessary.
651 static void
652 ppp_ccp_closed (struct ppp *ppp)
654 if (ppp->sc_xc_state) {
655 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
656 ppp->sc_xc_state = NULL;
659 if (ppp->sc_rc_state) {
660 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
661 ppp->sc_rc_state = NULL;
666 * Called to release all of the information in the current PPP structure.
668 * It is called when the ppp device goes down or if it is unable to go
669 * up.
672 static void
673 ppp_release (struct ppp *ppp)
675 struct tty_struct *tty;
676 struct device *dev;
678 tty = ppp2tty (ppp);
679 dev = ppp2dev (ppp);
681 ppp_ccp_closed (ppp);
683 /* Ensure that the pppd process is not hanging on poll() */
684 wake_up_interruptible (&ppp->read_wait);
685 wake_up_interruptible (&ppp->write_wait);
687 if (tty != NULL && tty->disc_data == ppp)
688 tty->disc_data = NULL; /* Break the tty->ppp link */
690 if (dev && dev->flags & IFF_UP) {
691 dev_close (dev); /* close the device properly */
692 dev->flags = 0; /* prevent recursion */
695 ppp_free_buf (ppp->rbuf);
696 ppp_free_buf (ppp->wbuf);
697 ppp_free_buf (ppp->cbuf);
698 ppp_free_buf (ppp->ubuf);
699 ppp_free_buf (ppp->tbuf);
701 ppp->rbuf =
702 ppp->wbuf =
703 ppp->cbuf =
704 ppp->tbuf =
705 ppp->xbuf =
706 ppp->s1buf =
707 ppp->s2buf =
708 ppp->ubuf = NULL;
710 if (ppp->slcomp) {
711 slhc_free (ppp->slcomp);
712 ppp->slcomp = NULL;
715 ppp->inuse = 0;
716 ppp->tty = NULL;
720 * Device callback.
722 * Called when the PPP device goes down in response to an ifconfig request.
725 static void
726 ppp_tty_close_local (struct tty_struct *tty, int sc_xfer)
728 struct ppp *ppp = tty2ppp (tty);
730 if (ppp != NULL) {
731 if (ppp->magic != PPP_MAGIC) {
732 if (ppp->flags & SC_DEBUG)
733 printk (KERN_WARNING
734 "ppp: trying to close unopened tty!\n");
735 } else {
736 CHECK_PPP_VOID();
737 ppp->sc_xfer = sc_xfer;
738 if (ppp->flags & SC_DEBUG)
739 printk (KERN_INFO "ppp: channel %s closing.\n",
740 ppp2dev(ppp) -> name);
741 ppp_release (ppp);
742 MOD_DEC_USE_COUNT;
747 static void
748 ppp_tty_close (struct tty_struct *tty)
750 ppp_tty_close_local (tty, 0);
754 * TTY callback.
756 * Called when the tty discipline is switched to PPP.
759 static int
760 ppp_tty_open (struct tty_struct *tty)
762 struct ppp *ppp = tty2ppp (tty);
763 int indx;
765 * There should not be an existing table for this slot.
767 if (ppp) {
768 if (ppp->flags & SC_DEBUG)
769 printk (KERN_ERR
770 "ppp_tty_open: gack! tty already associated to %s!\n",
771 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
772 : "unknown");
773 return -EEXIST;
776 * Allocate the structure from the system
778 ppp = ppp_find(current->pid);
779 if (ppp == NULL) {
780 ppp = ppp_find(0);
781 if (ppp == NULL)
782 ppp = ppp_alloc();
785 if (ppp == NULL) {
786 if (ppp->flags & SC_DEBUG)
787 printk (KERN_ERR
788 "ppp_tty_open: couldn't allocate ppp channel\n");
789 return -ENFILE;
792 * Initialize the control block
794 ppp_init_ctrl_blk (ppp);
795 ppp->tty = tty;
796 tty->disc_data = ppp;
798 * Flush any pending characters in the driver and discipline.
800 if (tty->ldisc.flush_buffer)
801 tty->ldisc.flush_buffer (tty);
803 if (tty->driver.flush_buffer)
804 tty->driver.flush_buffer (tty);
806 * Allocate space for the default VJ header compression slots
808 ppp->slcomp = slhc_init (16, 16);
809 if (ppp->slcomp == NULL) {
810 if (ppp->flags & SC_DEBUG)
811 printk (KERN_ERR
812 "ppp_tty_open: no space for compression buffers!\n");
813 ppp_release (ppp);
814 return -ENOMEM;
817 * Allocate space for the MTU and MRU buffers
819 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
820 ppp_release (ppp);
821 return -ENOMEM;
824 * Allocate space for a user level buffer
826 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
827 if (ppp->ubuf == NULL) {
828 if (ppp->flags & SC_DEBUG)
829 printk (KERN_ERR
830 "ppp_tty_open: no space for user receive buffer\n");
831 ppp_release (ppp);
832 return -ENOMEM;
835 if (ppp->flags & SC_DEBUG)
836 printk (KERN_INFO "ppp: channel %s open\n",
837 ppp2dev(ppp)->name);
839 for (indx = 0; indx < NUM_NP; ++indx)
840 ppp->sc_npmode[indx] = NPMODE_PASS;
842 MOD_INC_USE_COUNT;
843 return (ppp->line);
847 * Local function to send the next portion of the buffer.
849 * Called by the tty driver's tty_wakeup function should it be entered
850 * because the partial buffer was transmitted.
852 * Called by kick_tty to send the initial portion of the buffer.
854 * Completion processing of the buffer transmission is handled here.
857 static void
858 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
859 struct ppp_buffer *xbuf)
861 register int count, actual;
863 * Prevent re-entrancy by ensuring that this routine is called only once.
865 cli ();
866 if (ppp->flags & SC_XMIT_BUSY) {
867 sti ();
868 return;
870 ppp->flags |= SC_XMIT_BUSY;
871 sti ();
873 * Send the next block of data to the modem
875 count = xbuf->count - xbuf->tail;
876 actual = tty->driver.write (tty, 0,
877 buf_base (xbuf) + xbuf->tail, count);
879 * Terminate transmission of any block which may have an error.
880 * This could occur should the carrier drop.
882 if (actual < 0) {
883 ppp->stats.ppp_oerrors++;
884 actual = count;
885 } else
886 ppp->bytes_sent += actual;
888 * If the buffer has been transmitted then clear the indicators.
890 xbuf->tail += actual;
891 if (actual == count) {
892 xbuf = NULL;
893 ppp->flags &= ~SC_XMIT_BUSY;
895 * Complete the transmission on the current buffer.
897 xbuf = ppp->xbuf;
898 if (xbuf != NULL) {
899 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
900 xbuf->locked = 0;
901 ppp->xbuf = NULL;
903 * If the completed buffer came from the device write, then complete the
904 * transmission block.
906 if (ppp2dev (ppp) -> flags & IFF_UP) {
907 if (xbuf->type == BUFFER_TYPE_DEV_WR)
908 ppp2dev (ppp)->tbusy = 0;
909 mark_bh (NET_BH);
912 * Wake up the transmission queue for all completion events.
914 wake_up_interruptible (&ppp->write_wait);
916 * Look at the priorities. Choose a daemon write over the device driver.
918 cli();
919 xbuf = ppp->s1buf;
920 ppp->s1buf = NULL;
921 if (xbuf == NULL) {
922 xbuf = ppp->s2buf;
923 ppp->s2buf = NULL;
925 sti();
927 * If there is a pending buffer then transmit it now.
929 if (xbuf != NULL) {
930 ppp->flags &= ~SC_XMIT_BUSY;
931 ppp_kick_tty (ppp, xbuf);
932 return;
937 * Clear the re-entry flag
939 ppp->flags &= ~SC_XMIT_BUSY;
943 * This function is called by the tty driver when the transmit buffer has
944 * additional space. It is used by the ppp code to continue to transmit
945 * the current buffer should the buffer have been partially sent.
947 * In addition, it is used to send the first part of the buffer since the
948 * logic and the inter-locking would be identical.
951 static void
952 ppp_tty_wakeup (struct tty_struct *tty)
954 struct ppp_buffer *xbuf;
955 struct ppp *ppp = tty2ppp (tty);
957 if (!ppp)
958 return;
960 if (ppp->magic != PPP_MAGIC)
961 return;
963 * Ensure that there is a transmission pending. Clear the re-entry flag if
964 * there is no pending buffer. Otherwise, send the buffer.
966 xbuf = ppp->xbuf;
967 if (xbuf == NULL)
968 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
969 else
970 ppp_tty_wakeup_code (ppp, tty, xbuf);
974 * This function is called to transmit a buffer to the remote. The buffer
975 * is placed on the pending queue if there is presently a buffer being
976 * sent or it is transmitted with the aid of ppp_tty_wakeup.
979 static void
980 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
982 register int flags;
984 * Hold interrupts.
986 save_flags (flags);
987 cli ();
989 * Control the flags which are best performed with the interrupts masked.
991 xbuf->locked = 1;
992 xbuf->tail = 0;
994 * If the transmitter is busy then place the buffer on the appropriate
995 * priority queue.
997 if (ppp->xbuf != NULL) {
998 if (xbuf->type == BUFFER_TYPE_TTY_WR)
999 ppp->s1buf = xbuf;
1000 else
1001 ppp->s2buf = xbuf;
1002 restore_flags (flags);
1003 return;
1006 * If the transmitter is not busy then this is the highest priority frame
1008 ppp->flags &= ~SC_XMIT_BUSY;
1009 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1010 ppp->xbuf = xbuf;
1011 restore_flags (flags);
1013 * Do the "tty wakeup_code" to actually send this buffer.
1015 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1018 /*************************************************************
1019 * TTY INPUT
1020 * The following functions handle input that arrives from
1021 * the TTY. It recognizes PPP frames and either hands them
1022 * to the network layer or queues them for delivery to a
1023 * user process reading this TTY.
1024 *************************************************************/
1027 * Callback function from tty driver. Return the amount of space left
1028 * in the receiver's buffer to decide if remote transmitter is to be
1029 * throttled.
1032 static int
1033 ppp_tty_room (struct tty_struct *tty)
1035 return 65536; /* We can handle an infinite amount of data. :-) */
1039 * Callback function when data is available at the tty driver.
1042 static void
1043 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1044 char *flags, int count)
1046 register struct ppp *ppp = tty2ppp (tty);
1047 register struct ppp_buffer *buf = NULL;
1048 __u8 chr;
1050 * Fetch the pointer to the buffer. Be careful about race conditions.
1052 if (ppp != NULL)
1053 buf = ppp->rbuf;
1055 if (buf == NULL)
1056 return;
1058 * Verify the table pointer and ensure that the line is
1059 * still in PPP discipline.
1061 if (ppp->magic != PPP_MAGIC) {
1062 if (ppp->flags & SC_DEBUG)
1063 printk (KERN_DEBUG
1064 "PPP: handler called but couldn't find "
1065 "PPP struct.\n");
1066 return;
1068 CHECK_PPP_VOID ();
1070 * Print the buffer if desired
1072 if (ppp->flags & SC_LOG_RAWIN)
1073 ppp_print_buffer ("receive buffer", data, count);
1075 * Collect the character and error condition for the character. Set the toss
1076 * flag for the first character error.
1078 while (count-- > 0) {
1079 ppp->bytes_rcvd++;
1080 chr = *data++;
1081 if (flags) {
1082 if (*flags && ppp->toss == 0)
1083 ppp->toss = *flags;
1084 ++flags;
1087 * Set the flags for 8 data bits and no parity.
1089 * Actually, it sets the flags for d7 being 0/1 and parity being even/odd
1090 * so that the normal processing would have all flags set at the end of the
1091 * session. A missing flag bit would denote an error condition.
1094 #ifdef CHECK_CHARACTERS
1095 if (chr & 0x80)
1096 ppp->flags |= SC_RCV_B7_1;
1097 else
1098 ppp->flags |= SC_RCV_B7_0;
1100 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1101 ppp->flags |= SC_RCV_ODDP;
1102 else
1103 ppp->flags |= SC_RCV_EVNP;
1104 #endif
1106 * Branch on the character. Process the escape character. The sequence ESC ESC
1107 * is defined to be ESC.
1109 switch (chr) {
1110 case PPP_ESCAPE: /* PPP_ESCAPE: invert bit in next character */
1111 ppp->escape = PPP_TRANS;
1112 break;
1114 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1115 * then the block is to be ignored. In addition, characters before the very
1116 * first FLAG are also tossed by this procedure.
1118 case PPP_FLAG: /* PPP_FLAG: end of frame */
1119 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1120 if (ppp->escape)
1121 ppp->toss |= 0x80;
1123 * Process frames which are not to be ignored. If the processing failed,
1124 * then clean up the VJ tables.
1126 if ((ppp->toss & 0x80) != 0 ||
1127 ppp_doframe (ppp) == 0) {
1128 slhc_toss (ppp->slcomp);
1131 * Reset all indicators for the new frame to follow.
1133 buf->count = 0;
1134 buf->fcs = PPP_INITFCS;
1135 ppp->escape = 0;
1136 ppp->toss = 0;
1137 break;
1139 * All other characters in the data come here. If the character is in the
1140 * receive mask then ignore the character.
1142 default:
1143 if (in_rmap (ppp, chr))
1144 break;
1146 * Adjust the character and if the frame is to be discarded then simply
1147 * ignore the character until the ending FLAG is received.
1149 chr ^= ppp->escape;
1150 ppp->escape = 0;
1152 if (ppp->toss != 0)
1153 break;
1155 * If the count sent is within reason then store the character, bump the
1156 * count, and update the FCS for the character.
1158 if (buf->count < buf->size) {
1159 buf_base (buf)[buf->count++] = chr;
1160 buf->fcs = PPP_FCS (buf->fcs, chr);
1161 break;
1164 * The peer sent too much data. Set the flags to discard the current frame
1165 * and wait for the re-synchronization FLAG to be sent.
1167 ppp->stats.ppp_ierrors++;
1168 ppp->toss |= 0xC0;
1169 break;
1175 * Put the input frame into the networking system for the indicated protocol
1178 static int
1179 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1181 sk_buff *skb = dev_alloc_skb (count);
1183 * Generate a skb buffer for the new frame.
1185 if (skb == NULL) {
1186 if (ppp->flags & SC_DEBUG)
1187 printk (KERN_ERR
1188 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1189 ppp2dev (ppp)->name);
1190 return 0;
1193 * Move the received data from the input buffer to the skb buffer.
1195 skb->dev = ppp2dev (ppp); /* We are the device */
1196 skb->protocol = proto;
1197 skb->mac.raw = skb_data(skb);
1198 memcpy (skb_put(skb,count), data, count); /* move data */
1200 * Tag the frame and kick it to the proper receive routine
1202 ppp->ddinfo.recv_idle = jiffies;
1203 netif_rx (skb);
1204 return 1;
1208 * Process the receipt of an IP frame
1211 static int
1212 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1214 if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1215 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1216 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1217 return 0;
1221 * Process the receipt of an IPX frame
1224 static int
1225 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1227 if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1228 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1229 return 0;
1233 * Process the receipt of an VJ Compressed frame
1236 static int
1237 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1238 __u8 *data, int count)
1240 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1241 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1242 if (new_count >= 0) {
1243 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1245 if (ppp->flags & SC_DEBUG)
1246 printk (KERN_NOTICE
1247 "ppp: error in VJ decompression\n");
1249 return 0;
1253 * Process the receipt of an VJ Un-compressed frame
1256 static int
1257 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1258 __u8 *data, int count)
1260 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1261 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1262 return rcv_proto_ip (ppp, PPP_IP, data, count);
1264 if (ppp->flags & SC_DEBUG)
1265 printk (KERN_NOTICE
1266 "ppp: error in VJ memorizing\n");
1268 return 0;
1272 * Receive all unclassified protocols.
1275 static int
1276 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1277 __u8 *data, int len)
1279 int totlen;
1280 register int current_idx;
1282 #define PUTC(c) \
1284 buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1285 current_idx &= ppp->ubuf->size; \
1286 if (current_idx == ppp->ubuf->tail) \
1287 goto failure; \
1291 * The total length includes the protocol data.
1292 * Lock the user information buffer.
1294 if (set_bit (0, &ppp->ubuf->locked)) {
1295 if (ppp->flags & SC_DEBUG)
1296 printk (KERN_DEBUG
1297 "ppp_us_queue: can't get lock\n");
1298 } else {
1299 current_idx = ppp->ubuf->head;
1301 * Insert the buffer length (not counted), the protocol, and the data
1303 totlen = len + 2;
1304 PUTC (totlen >> 8);
1305 PUTC (totlen);
1307 PUTC (proto >> 8);
1308 PUTC (proto);
1310 totlen -= 2;
1311 while (totlen-- > 0) {
1312 PUTC (*data++);
1314 #undef PUTC
1316 * The frame is complete. Update the head pointer and wakeup the pppd
1317 * process.
1319 ppp->ubuf->head = current_idx;
1321 clear_bit (0, &ppp->ubuf->locked);
1322 wake_up_interruptible (&ppp->read_wait);
1323 if (ppp->tty->fasync != NULL)
1324 kill_fasync (ppp->tty->fasync, SIGIO);
1326 if (ppp->flags & SC_DEBUG)
1327 printk (KERN_INFO
1328 "ppp: successfully queued %d bytes, flags = %x\n",
1329 len + 2, ppp->flags);
1331 return 1;
1333 * The buffer is full. Unlock the header
1335 failure:
1336 clear_bit (0, &ppp->ubuf->locked);
1337 if (ppp->flags & SC_DEBUG)
1338 printk (KERN_INFO
1339 "ppp_us_queue: ran out of buffer space.\n");
1342 * Discard the frame. There are no takers for this protocol.
1344 if (ppp->flags & SC_DEBUG)
1345 printk (KERN_WARNING
1346 "ppp: dropping packet on the floor.\n");
1347 slhc_toss (ppp->slcomp);
1348 return 0;
1352 * Handle a CCP packet.
1354 * The CCP packet is passed along to the pppd process just like any
1355 * other PPP frame. The difference is that some processing needs to be
1356 * immediate or the compressors will become confused on the peer.
1359 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1361 int slen = CCP_LENGTH(dp);
1362 __u8 *opt = dp + CCP_HDRLEN;
1363 int opt_len = slen - CCP_HDRLEN;
1365 if (slen > len)
1366 return;
1368 switch (CCP_CODE(dp)) {
1369 case CCP_CONFREQ:
1370 case CCP_TERMREQ:
1371 case CCP_TERMACK:
1373 * CCP must be going down - disable compression
1375 if (ppp->flags & SC_CCP_UP) {
1376 ppp->flags &= ~(SC_CCP_UP |
1377 SC_COMP_RUN |
1378 SC_DECOMP_RUN);
1380 break;
1382 case CCP_CONFACK:
1383 if ((ppp->flags & SC_CCP_OPEN) == 0)
1384 break;
1385 if (ppp->flags & SC_CCP_UP)
1386 break;
1387 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1388 break;
1389 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1390 break;
1392 * we're agreeing to send compressed packets.
1394 if (!rcvd) {
1395 if (ppp->sc_xc_state == NULL)
1396 break;
1398 if ((*ppp->sc_xcomp->comp_init)
1399 (ppp->sc_xc_state,
1400 opt,
1401 opt_len,
1402 ppp2dev (ppp)->base_addr,
1404 ppp->flags))
1405 ppp->flags |= SC_COMP_RUN;
1406 break;
1409 * peer is agreeing to send compressed packets.
1411 if (ppp->sc_rc_state == NULL)
1412 break;
1414 if ((*ppp->sc_rcomp->decomp_init)
1415 (ppp->sc_rc_state,
1416 opt,
1417 opt_len,
1418 ppp2dev (ppp)->base_addr,
1420 ppp->mru,
1421 ppp->flags)) {
1422 ppp->flags |= SC_DECOMP_RUN;
1423 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1425 break;
1427 * The protocol sequence is complete at this end
1429 case CCP_RESETACK:
1430 if ((ppp->flags & SC_CCP_UP) == 0)
1431 break;
1433 if (!rcvd) {
1434 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN))
1435 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1436 } else {
1437 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1438 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1439 ppp->flags &= ~SC_DC_ERROR;
1442 break;
1446 static int
1447 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1449 ppp_proto_ccp (ppp, dp, len, 1);
1450 return rcv_proto_unknown (ppp, proto, dp, len);
1454 * Handle a LQR packet.
1457 static int
1458 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1460 return rcv_proto_unknown (ppp, proto, data, len);
1463 /* on entry, a received frame is in ppp->rbuf.bufr
1464 check it and dispose as appropriate */
1466 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1468 __u16 proto = PPP_PROTOCOL (data);
1469 ppp_proto_type *proto_ptr;
1471 * Ignore empty frames
1473 if (count <= 4)
1474 return;
1476 * Count the frame and print it
1478 ++ppp->stats.ppp_ipackets;
1479 if (ppp->flags & SC_LOG_INPKT)
1480 ppp_print_buffer ("receive frame", data, count);
1482 * Find the procedure to handle this protocol. The last one is marked
1483 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1485 proto_ptr = proto_list;
1486 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1487 ++proto_ptr;
1489 * Update the appropriate statistic counter.
1491 if ((*proto_ptr->func) (ppp, proto,
1492 &data[PPP_HARD_HDR_LEN],
1493 count - PPP_HARD_HDR_LEN))
1494 ppp->stats.ppp_ioctects += count;
1495 else
1496 ++ppp->stats.ppp_discards;
1499 /* on entry, a received frame is in ppp->rbuf.bufr
1500 check it and dispose as appropriate */
1502 static int
1503 ppp_doframe (struct ppp *ppp)
1505 __u8 *data = buf_base (ppp->rbuf);
1506 int count = ppp->rbuf->count;
1507 int addr, ctrl, proto;
1508 int new_count;
1509 __u8 *new_data;
1511 * If there is a pending error from the receiver then log it and discard
1512 * the damaged frame.
1514 if (ppp->toss) {
1515 if (ppp->flags & SC_DEBUG)
1516 printk (KERN_WARNING
1517 "ppp_toss: tossing frame, reason = %d\n",
1518 ppp->toss);
1519 ppp->stats.ppp_ierrors++;
1520 return 0;
1523 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1524 * follows each frame.
1526 if (count == 0)
1527 return 1;
1529 * Generate an error if the frame is too small.
1531 if (count < PPP_HARD_HDR_LEN) {
1532 if (ppp->flags & SC_DEBUG)
1533 printk (KERN_WARNING
1534 "ppp: got runt ppp frame, %d chars\n", count);
1535 slhc_toss (ppp->slcomp);
1536 ppp->stats.ppp_ierrors++;
1537 return 1;
1540 * Verify the CRC of the frame and discard the CRC characters from the
1541 * end of the buffer.
1543 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1544 if (ppp->flags & SC_DEBUG)
1545 printk (KERN_WARNING
1546 "ppp: frame with bad fcs, excess = %x\n",
1547 ppp->rbuf->fcs ^ PPP_GOODFCS);
1548 ppp->stats.ppp_ierrors++;
1549 return 0;
1551 count -= 2; /* ignore the fcs characters */
1553 * Ignore the leading ADDRESS and CONTROL fields in the frame.
1555 addr = PPP_ALLSTATIONS;
1556 ctrl = PPP_UI;
1558 if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1559 data += 2;
1560 count -= 2;
1563 * Obtain the protocol from the frame
1565 proto = (__u16) *data++;
1566 if ((proto & 1) == 0) {
1567 proto = (proto << 8) | (__u16) *data++;
1568 --count;
1571 * Rewrite the header with the full information. This may encroach upon
1572 * the 'filler' area in the buffer header. This is the purpose for the
1573 * filler.
1575 *(--data) = proto;
1576 *(--data) = proto >> 8;
1577 *(--data) = ctrl;
1578 *(--data) = addr;
1579 count += 3;
1581 * Process the active decompressor.
1583 if ((ppp->sc_rc_state != (void *) 0) &&
1584 (ppp->flags & SC_DECOMP_RUN) &&
1585 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1586 if (proto == PPP_COMP) {
1588 * If the frame is compressed then decompress it.
1590 new_data = kmalloc (ppp->mru + 4, GFP_ATOMIC);
1591 if (new_data == NULL) {
1592 if (ppp->flags & SC_DEBUG)
1593 printk (KERN_ERR
1594 "ppp_doframe: no memory\n");
1595 slhc_toss (ppp->slcomp);
1596 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1597 data,
1598 count);
1599 return 1;
1602 * Decompress the frame
1604 new_count = bsd_decompress (ppp->sc_rc_state,
1605 data,
1606 count,
1607 new_data,
1608 ppp->mru + 4);
1609 switch (new_count) {
1610 default:
1611 ppp_doframe_lower (ppp, new_data, new_count);
1612 kfree (new_data);
1613 return 1;
1615 case DECOMP_OK:
1616 break;
1618 case DECOMP_ERROR:
1619 ppp->flags |= SC_DC_ERROR;
1620 break;
1622 case DECOMP_FATALERROR:
1623 ppp->flags |= SC_DC_FERROR;
1624 break;
1627 * Log the error condition and discard the frame.
1629 if (ppp->flags & SC_DEBUG)
1630 printk (KERN_ERR
1631 "ppp_proto_comp: "
1632 "decompress err %d\n", new_count);
1633 kfree (new_data);
1634 slhc_toss (ppp->slcomp);
1635 return 1;
1638 * The frame is not special. Pass it through the compressor without
1639 * actually compressing the data
1641 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1642 data,
1643 count);
1646 * Process the uncompressed frame.
1648 ppp_doframe_lower (ppp, data, count);
1649 return 1;
1652 /*************************************************************
1653 * LINE DISCIPLINE SUPPORT
1654 * The following functions form support user programs
1655 * which read and write data on a TTY with the PPP line
1656 * discipline. Reading is done from a circular queue,
1657 * filled by the lower TTY levels.
1658 *************************************************************/
1660 /* read a PPP frame from the us_rbuff circular buffer,
1661 waiting if necessary
1664 static int
1665 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1666 unsigned int nr)
1668 struct ppp *ppp = tty2ppp (tty);
1669 __u8 c;
1670 int len, indx;
1672 #define GETC(c) \
1674 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1675 ppp->ubuf->tail &= ppp->ubuf->size; \
1679 * Validate the pointers
1681 if (!ppp)
1682 return -EIO;
1684 if (ppp->magic != PPP_MAGIC)
1685 return -EIO;
1687 CHECK_PPP (-ENXIO);
1689 if (ppp->flags & SC_DEBUG)
1690 printk (KERN_DEBUG
1691 "ppp_tty_read: called buf=%p nr=%u\n",
1692 buf, nr);
1694 * Acquire the read lock.
1696 for (;;) {
1697 ppp = tty2ppp (tty);
1698 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse)
1699 return 0;
1701 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1702 if (ppp->flags & SC_DEBUG)
1703 printk (KERN_DEBUG
1704 "ppp_tty_read: sleeping(ubuf)\n");
1706 current->timeout = 0;
1707 current->state = TASK_INTERRUPTIBLE;
1708 schedule ();
1710 if (current->signal & ~current->blocked)
1711 return -EINTR;
1712 continue;
1715 * Before we attempt to write the frame to the user, ensure that the
1716 * user has access to the pages for the total buffer length.
1718 indx = verify_area (VERIFY_WRITE, buf, nr);
1719 if (indx != 0)
1720 return (indx);
1722 * Fetch the length of the buffer from the first two bytes.
1724 if (ppp->ubuf->head == ppp->ubuf->tail)
1725 len = 0;
1726 else {
1727 GETC (c);
1728 len = c << 8;
1729 GETC (c);
1730 len += c;
1733 * If there is no length then wait for the data to arrive.
1735 if (len == 0) {
1736 /* no data */
1737 clear_bit (0, &ppp->ubuf->locked);
1738 if (file->f_flags & O_NONBLOCK) {
1739 if (ppp->flags & SC_DEBUG)
1740 printk (KERN_DEBUG
1741 "ppp_tty_read: no data "
1742 "(EAGAIN)\n");
1743 return -EAGAIN;
1745 current->timeout = 0;
1747 if (ppp->flags & SC_DEBUG)
1748 printk (KERN_DEBUG
1749 "ppp_tty_read: sleeping(read_wait)\n");
1751 interruptible_sleep_on (&ppp->read_wait);
1752 if (current->signal & ~current->blocked)
1753 return -EINTR;
1754 continue;
1757 * Reset the time of the last read operation.
1759 if (ppp->flags & SC_DEBUG)
1760 printk (KERN_DEBUG "ppp_tty_read: len = %d\n", len);
1762 * Ensure that the frame will fit within the caller's buffer. If not, then
1763 * discard the frame from the input buffer.
1765 if (len + 2 > nr) {
1766 /* Can't copy it, update us_rbuff_head */
1768 if (ppp->flags & SC_DEBUG)
1769 printk (KERN_DEBUG
1770 "ppp: read of %u bytes too small for %d "
1771 "frame\n", nr, len + 2);
1772 ppp->ubuf->tail += len;
1773 ppp->ubuf->tail &= ppp->ubuf->size;
1774 clear_bit (0, &ppp->ubuf->locked);
1775 ppp->stats.ppp_ierrors++;
1776 return -EOVERFLOW;
1779 * Before we attempt to write the frame to the user, ensure that the
1780 * page tables are proper.
1782 indx = verify_area (VERIFY_WRITE, buf, len + 2);
1783 if (indx != 0) {
1784 ppp->ubuf->tail += len;
1785 ppp->ubuf->tail &= ppp->ubuf->size;
1786 clear_bit (0, &ppp->ubuf->locked);
1787 return (indx);
1790 * Fake the insertion of the ADDRESS and CONTROL information because these
1791 * were not saved in the buffer.
1793 put_user (PPP_ALLSTATIONS, buf++);
1794 put_user (PPP_UI, buf++);
1796 indx = len;
1798 * Copy the received data from the buffer to the caller's area.
1800 while (indx-- > 0) {
1801 GETC (c);
1802 put_user (c, buf);
1803 ++buf;
1806 clear_bit (0, &ppp->ubuf->locked);
1807 len += 2; /* Account for ADDRESS and CONTROL bytes */
1808 if (ppp->flags & SC_DEBUG)
1809 printk (KERN_DEBUG
1810 "ppp_tty_read: passing %d bytes up\n", len);
1811 return len;
1813 #undef GETC
1816 /* stuff a character into the transmit buffer, using PPP's way of escaping
1817 special characters.
1818 also, update fcs to take account of new character */
1820 extern inline void
1821 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1822 register __u8 chr)
1825 * The buffer should not be full.
1827 if (ppp->flags & SC_DEBUG) {
1828 if ((buf->count < 0) || (buf->count > 3000))
1829 printk (KERN_DEBUG "ppp_stuff_char: %x %d\n",
1830 (unsigned int) buf->count,
1831 (unsigned int) chr);
1834 * Update the FCS and if the character needs to be escaped, do it.
1836 buf->fcs = PPP_FCS (buf->fcs, chr);
1837 if (in_xmap (ppp, chr)) {
1838 chr ^= PPP_TRANS;
1839 ins_char (buf, PPP_ESCAPE);
1842 * Add the character to the buffer.
1844 ins_char (buf, chr);
1848 * Procedure to encode the data with the proper escaping and send the
1849 * data to the remote system.
1852 static void
1853 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
1854 __u8 *data, int count, int non_ip)
1856 __u16 write_fcs;
1857 int address, control;
1858 int proto;
1860 * Insert the leading FLAG character
1862 buf->count = 0;
1864 if (non_ip || flag_time == 0)
1865 ins_char (buf, PPP_FLAG);
1866 else {
1867 if (jiffies - ppp->last_xmit > flag_time)
1868 ins_char (buf, PPP_FLAG);
1870 ppp->last_xmit = jiffies;
1871 buf->fcs = PPP_INITFCS;
1873 * Emit the address/control information if needed
1875 address = PPP_ADDRESS (data);
1876 control = PPP_CONTROL (data);
1877 proto = PPP_PROTOCOL (data);
1879 if (address != PPP_ALLSTATIONS ||
1880 control != PPP_UI ||
1881 (ppp->flags & SC_COMP_AC) == 0) {
1882 ppp_stuff_char (ppp, buf, address);
1883 ppp_stuff_char (ppp, buf, control);
1886 * Emit the protocol (compressed if possible)
1888 if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
1889 ppp_stuff_char (ppp, buf, proto >> 8);
1891 ppp_stuff_char (ppp, buf, proto);
1893 * Insert the data
1895 data += 4;
1896 count -= 4;
1898 while (count-- > 0)
1899 ppp_stuff_char (ppp, buf, *data++);
1901 * Add the trailing CRC and the final flag character
1903 write_fcs = buf->fcs ^ 0xFFFF;
1904 ppp_stuff_char (ppp, buf, write_fcs);
1905 ppp_stuff_char (ppp, buf, write_fcs >> 8);
1907 if (ppp->flags & SC_DEBUG)
1908 printk (KERN_DEBUG "ppp_dev_xmit_lower: fcs is %hx\n",
1909 write_fcs);
1911 * Add the trailing flag character
1913 ins_char (buf, PPP_FLAG);
1915 * Print the buffer
1917 if (ppp->flags & SC_LOG_FLUSH)
1918 ppp_print_buffer ("ppp flush", buf_base (buf),
1919 buf->count);
1920 else {
1921 if (ppp->flags & SC_DEBUG)
1922 printk (KERN_DEBUG
1923 "ppp_dev_xmit: writing %d chars\n",
1924 buf->count);
1927 * Send the block to the tty driver.
1929 ppp->stats.ppp_obytes += buf->count;
1930 ppp_kick_tty (ppp, buf);
1934 * Send an frame to the remote with the proper bsd compression.
1936 * Return 0 if frame was queued for transmission.
1937 * 1 if frame must be re-queued for later driver support.
1940 static int
1941 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
1942 __u8 *data, int count)
1944 int proto;
1945 int address, control;
1946 __u8 *new_data;
1947 int new_count;
1949 * Print the buffer
1951 if (ppp->flags & SC_LOG_OUTPKT)
1952 ppp_print_buffer ("write frame", data, count);
1954 * Determine if the frame may be compressed. Attempt to compress the
1955 * frame if possible.
1957 proto = PPP_PROTOCOL (data);
1958 address = PPP_ADDRESS (data);
1959 control = PPP_CONTROL (data);
1961 if (((ppp->flags & SC_COMP_RUN) != 0) &&
1962 (ppp->sc_xc_state != (void *) 0) &&
1963 (address == PPP_ALLSTATIONS) &&
1964 (control == PPP_UI) &&
1965 (proto != PPP_LCP) &&
1966 (proto != PPP_CCP)) {
1967 new_data = kmalloc (count, GFP_ATOMIC);
1968 if (new_data == NULL) {
1969 if (ppp->flags & SC_DEBUG)
1970 printk (KERN_ERR
1971 "ppp_dev_xmit_frame: no memory\n");
1972 return 1;
1975 new_count = bsd_compress (ppp->sc_xc_state,
1976 data,
1977 new_data,
1978 count,
1979 count);
1981 if (new_count > 0) {
1982 ++ppp->stats.ppp_opackets;
1983 ppp->stats.ppp_ooctects += new_count;
1985 ppp_dev_xmit_lower (ppp, buf, new_data,
1986 new_count, 0);
1987 kfree (new_data);
1988 return 0;
1991 * The frame could not be compressed.
1993 kfree (new_data);
1996 * The frame may not be compressed. Update the statistics before the
1997 * count field is destroyed. The frame will be transmitted.
1999 ++ppp->stats.ppp_opackets;
2000 ppp->stats.ppp_ooctects += count;
2002 * Go to the escape encoding
2004 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2005 return 0;
2009 * Revise the tty frame for specific protocols.
2012 static int
2013 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
2015 __u8 *p;
2017 switch (PPP_PROTOCOL (data)) {
2019 * Update the LQR frame with the current MIB information. This saves having
2020 * the daemon read old MIB data from the driver.
2022 case PPP_LQR:
2023 len = 48; /* total size of this frame */
2024 p = (__u8 *) &data [40]; /* Point to last two items. */
2025 p = store_long (p, ppp->stats.ppp_opackets + 1);
2026 p = store_long (p, ppp->stats.ppp_ooctects + len);
2027 break;
2029 * Outbound compression frames
2031 case PPP_CCP:
2032 ppp_proto_ccp (ppp,
2033 data + PPP_HARD_HDR_LEN,
2034 len - PPP_HARD_HDR_LEN,
2036 break;
2038 default:
2039 break;
2042 return len;
2046 * write a frame with NR chars from BUF to TTY
2047 * we have to put the FCS field on ourselves
2050 static int
2051 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2052 unsigned int count)
2054 struct ppp *ppp = tty2ppp (tty);
2055 __u8 *new_data;
2056 int status;
2058 * Verify the pointers.
2060 if (!ppp)
2061 return -EIO;
2063 if (ppp->magic != PPP_MAGIC)
2064 return -EIO;
2066 CHECK_PPP (-ENXIO);
2068 * Ensure that the caller does not wish to send too much.
2070 if (count > PPP_MTU) {
2071 if (ppp->flags & SC_DEBUG)
2072 printk (KERN_WARNING
2073 "ppp_tty_write: truncating user packet "
2074 "from %u to mtu %d\n", count, PPP_MTU);
2075 count = PPP_MTU;
2078 * Allocate a buffer for the data and fetch it from the user space.
2080 new_data = kmalloc (count, GFP_KERNEL);
2081 if (new_data == NULL) {
2082 if (ppp->flags & SC_DEBUG)
2083 printk (KERN_ERR
2084 "ppp_tty_write: no memory\n");
2085 return 0;
2088 * lock this PPP unit so we will be the only writer;
2089 * sleep if necessary
2091 while (lock_buffer (ppp->tbuf) != 0) {
2092 current->timeout = 0;
2093 if (ppp->flags & SC_DEBUG)
2094 printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2095 interruptible_sleep_on (&ppp->write_wait);
2097 ppp = tty2ppp (tty);
2098 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse) {
2099 kfree (new_data);
2100 return 0;
2103 if (current->signal & ~current->blocked) {
2104 kfree (new_data);
2105 return -EINTR;
2109 * Ensure that the caller's buffer is valid.
2111 status = verify_area (VERIFY_READ, data, count);
2112 if (status != 0) {
2113 kfree (new_data);
2114 ppp->tbuf->locked = 0;
2115 return status;
2118 copy_from_user (new_data, data, count);
2120 * Change the LQR frame
2122 count = send_revise_frame (ppp, new_data, count);
2124 * Send the data
2126 ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2127 kfree (new_data);
2128 return (int) count;
2132 * Process the BSD compression IOCTL event for the tty device.
2135 static int
2136 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2138 struct compressor *cp;
2139 struct ppp_option_data data;
2140 int error;
2141 int nb;
2142 __u8 *ptr;
2143 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2145 * Fetch the compression parameters
2147 error = verify_area (VERIFY_READ, odp, sizeof (data));
2148 if (error == 0) {
2149 copy_from_user (&data, odp, sizeof (data));
2150 nb = data.length;
2151 ptr = data.ptr;
2152 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2153 nb = CCP_MAX_OPTION_LENGTH;
2155 error = verify_area (VERIFY_READ, ptr, nb);
2156 if(!error)
2157 copy_from_user (ccp_option, ptr, nb);
2159 if (error != 0)
2160 return error;
2162 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2163 return (-EINVAL);
2165 cp = find_compressor ((int) (unsigned int) (__u8) ccp_option[0]);
2166 if (cp != (struct compressor *) 0) {
2168 * Found a handler for the protocol - try to allocate
2169 * a compressor or decompressor.
2171 error = 0;
2172 if (data.transmit) {
2173 if (ppp->sc_xc_state != NULL)
2174 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2176 ppp->sc_xcomp = cp;
2177 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2179 if (ppp->sc_xc_state == NULL) {
2180 if (ppp->flags & SC_DEBUG)
2181 printk("ppp%ld: comp_alloc failed\n",
2182 ppp2dev (ppp)->base_addr);
2183 error = -ENOBUFS;
2185 ppp->flags &= ~SC_COMP_RUN;
2186 } else {
2187 if (ppp->sc_rc_state != NULL)
2188 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2189 ppp->sc_rcomp = cp;
2190 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2191 if (ppp->sc_rc_state == NULL) {
2192 if (ppp->flags & SC_DEBUG)
2193 printk("ppp%ld: decomp_alloc failed\n",
2194 ppp2dev (ppp)->base_addr);
2195 error = ENOBUFS;
2197 ppp->flags &= ~SC_DECOMP_RUN;
2199 return (error);
2202 if (ppp->flags & SC_DEBUG)
2203 printk(KERN_DEBUG "ppp%ld: no compressor for [%x %x %x], %x\n",
2204 ppp2dev (ppp)->base_addr, ccp_option[0], ccp_option[1],
2205 ccp_option[2], nb);
2206 return (-EINVAL); /* no handler found */
2210 * Process the IOCTL event for the tty device.
2213 static int
2214 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2215 unsigned int param2, unsigned long param3)
2217 struct ppp *ppp = tty2ppp (tty);
2218 register int temp_i = 0;
2219 int error = 0;
2221 * Verify the status of the PPP device.
2223 if (!ppp)
2224 return -EBADF;
2226 if (ppp->magic != PPP_MAGIC)
2227 return -EBADF;
2229 CHECK_PPP (-ENXIO);
2231 * The user must have an euid of root to do these requests.
2233 if (!suser ())
2234 return -EPERM;
2236 * Set the MRU value
2238 switch (param2) {
2239 case PPPIOCSMRU:
2240 error = verify_area (VERIFY_READ, (void *) param3,
2241 sizeof (temp_i));
2242 if (error == 0) {
2243 get_user (temp_i, (int *) param3);
2244 if (ppp->flags & SC_DEBUG)
2245 printk (KERN_INFO
2246 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2248 if (ppp->mru != temp_i)
2249 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2251 break;
2253 * Fetch the flags
2255 case PPPIOCGFLAGS:
2256 error = verify_area (VERIFY_WRITE, (void *) param3,
2257 sizeof (temp_i));
2258 if (error == 0) {
2259 temp_i = (ppp->flags & SC_MASK);
2260 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2261 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2262 SC_RCV_ODDP | SC_RCV_EVNP;
2263 #endif
2264 put_user (temp_i, (int *) param3);
2265 if (ppp->flags & SC_DEBUG)
2266 printk (KERN_DEBUG
2267 "ppp_tty_ioctl: get flags: addr %lx flags "
2268 "%x\n", param3, temp_i);
2270 break;
2272 * Set the flags for the various options
2274 case PPPIOCSFLAGS:
2275 error = verify_area (VERIFY_READ, (void *) param3,
2276 sizeof (temp_i));
2277 if (error == 0) {
2278 get_user (temp_i, (int *) param3);
2279 temp_i &= SC_MASK;
2280 temp_i |= (ppp->flags & ~SC_MASK);
2282 if ((ppp->flags & SC_CCP_OPEN) &&
2283 (temp_i & SC_CCP_OPEN) == 0)
2284 ppp_ccp_closed (ppp);
2286 if ((ppp->flags | temp_i) & SC_DEBUG)
2287 printk (KERN_INFO
2288 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2289 ppp->flags = temp_i;
2291 break;
2293 * Set the compression mode
2295 case PPPIOCSCOMPRESS:
2296 error = ppp_set_compression (ppp,
2297 (struct ppp_option_data *) param3);
2298 break;
2300 * Retrieve the transmit async map
2302 case PPPIOCGASYNCMAP:
2303 error = verify_area (VERIFY_WRITE, (void *) param3,
2304 sizeof (temp_i));
2305 if (error == 0) {
2306 put_user (ppp->xmit_async_map[0], (int *) param3);
2307 if (ppp->flags & SC_DEBUG)
2308 printk (KERN_INFO
2309 "ppp_tty_ioctl: get asyncmap: addr "
2310 "%lx asyncmap %x\n",
2311 param3,
2312 ppp->xmit_async_map[0]);
2314 break;
2316 * Set the transmit async map
2318 case PPPIOCSASYNCMAP:
2319 error = verify_area (VERIFY_READ, (void *) param3,
2320 sizeof (temp_i));
2321 if (error == 0) {
2322 get_user (ppp->xmit_async_map[0],(int *) param3);
2323 if (ppp->flags & SC_DEBUG)
2324 printk (KERN_INFO
2325 "ppp_tty_ioctl: set xmit asyncmap %x\n",
2326 ppp->xmit_async_map[0]);
2328 break;
2330 * Set the receive async map
2332 case PPPIOCSRASYNCMAP:
2333 error = verify_area (VERIFY_READ, (void *) param3,
2334 sizeof (temp_i));
2335 if (error == 0) {
2336 get_user (ppp->recv_async_map,(int *) param3);
2337 if (ppp->flags & SC_DEBUG)
2338 printk (KERN_INFO
2339 "ppp_tty_ioctl: set rcv asyncmap %x\n",
2340 ppp->recv_async_map);
2342 break;
2344 * Obtain the unit number for this device.
2346 case PPPIOCGUNIT:
2347 error = verify_area (VERIFY_WRITE, (void *) param3,
2348 sizeof (temp_i));
2349 if (error == 0) {
2350 put_user (ppp2dev (ppp)->base_addr, (int *) param3);
2351 if (ppp->flags & SC_DEBUG)
2352 printk (KERN_INFO
2353 "ppp_tty_ioctl: get unit: %ld",
2354 ppp2dev (ppp)->base_addr);
2356 break;
2358 * Set the debug level
2360 case PPPIOCSDEBUG:
2361 error = verify_area (VERIFY_READ, (void *) param3,
2362 sizeof (temp_i));
2363 if (error == 0) {
2364 get_user (temp_i, (int *) param3);
2365 temp_i = (temp_i & 0x1F) << 16;
2366 temp_i |= (ppp->flags & ~0x1F0000);
2368 if ((ppp->flags | temp_i) & SC_DEBUG)
2369 printk (KERN_INFO
2370 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2371 ppp->flags = temp_i;
2373 break;
2375 * Get the debug level
2377 case PPPIOCGDEBUG:
2378 error = verify_area (VERIFY_WRITE, (void *) param3,
2379 sizeof (temp_i));
2380 if (error == 0) {
2381 temp_i = (ppp->flags >> 16) & 0x1F;
2382 put_user (temp_i, (int *) param3);
2384 if (ppp->flags & SC_DEBUG)
2385 printk (KERN_INFO
2386 "ppp_tty_ioctl: get debug level %d\n",
2387 temp_i);
2389 break;
2391 * Get the times since the last send/receive frame operation
2393 case PPPIOCGIDLE:
2394 error = verify_area (VERIFY_WRITE, (void *) param3,
2395 sizeof (struct ppp_idle));
2396 if (error == 0) {
2397 struct ppp_idle cur_ddinfo;
2398 __u32 cur_jiffies = jiffies;
2400 /* change absolute times to relative times. */
2401 cur_ddinfo.xmit_idle = (cur_jiffies - ppp->ddinfo.xmit_idle) / HZ;
2402 cur_ddinfo.recv_idle = (cur_jiffies - ppp->ddinfo.recv_idle) / HZ;
2403 copy_to_user ((void *) param3, &cur_ddinfo,
2404 sizeof (cur_ddinfo));
2405 if (ppp->flags & SC_DEBUG)
2406 printk (KERN_INFO
2407 "ppp_tty_ioctl: read demand dial info\n");
2409 break;
2411 * Retrieve the extended async map
2413 case PPPIOCGXASYNCMAP:
2414 error = verify_area (VERIFY_WRITE,
2415 (void *) param3,
2416 sizeof (ppp->xmit_async_map));
2417 if (error == 0) {
2418 copy_to_user ((void *) param3,
2419 ppp->xmit_async_map,
2420 sizeof (ppp->xmit_async_map));
2422 if (ppp->flags & SC_DEBUG)
2423 printk (KERN_INFO
2424 "ppp_tty_ioctl: get xasyncmap: addr %lx\n",
2425 param3);
2427 break;
2429 * Set the async extended map
2431 case PPPIOCSXASYNCMAP:
2432 error = verify_area (VERIFY_READ, (void *) param3,
2433 sizeof (ppp->xmit_async_map));
2434 if (error == 0) {
2435 __u32 temp_tbl[8];
2437 copy_from_user (temp_tbl, (void *) param3,
2438 sizeof (ppp->xmit_async_map));
2439 temp_tbl[1] = 0x00000000;
2440 temp_tbl[2] &= ~0x40000000;
2441 temp_tbl[3] |= 0x60000000;
2443 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2444 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2445 (temp_tbl[6] & temp_tbl[7]) != 0)
2446 error = -EINVAL;
2447 else {
2448 memcpy (ppp->xmit_async_map, temp_tbl,
2449 sizeof (ppp->xmit_async_map));
2451 if (ppp->flags & SC_DEBUG)
2452 printk (KERN_INFO
2453 "ppp_tty_ioctl: set xasyncmap\n");
2456 break;
2458 * Set the maximum VJ header compression slot number.
2460 case PPPIOCSMAXCID:
2461 error = verify_area (VERIFY_READ, (void *) param3,
2462 sizeof (temp_i));
2463 if (error == 0) {
2464 get_user (temp_i, (int *) param3);
2465 ++temp_i;
2466 if (ppp->flags & SC_DEBUG)
2467 printk (KERN_INFO
2468 "ppp_tty_ioctl: set maxcid to %d\n",
2469 temp_i);
2470 if (ppp->slcomp != NULL)
2471 slhc_free (ppp->slcomp);
2472 ppp->slcomp = slhc_init (16, temp_i);
2474 if (ppp->slcomp == NULL) {
2475 if (ppp->flags & SC_DEBUG)
2476 printk (KERN_ERR
2477 "ppp: no space for compression buffers!\n");
2478 ppp_release (ppp);
2479 error = -ENOMEM;
2482 break;
2484 case PPPIOCXFERUNIT:
2485 ppp_tty_close_local (tty, current->pid);
2486 break;
2488 case PPPIOCGNPMODE:
2489 case PPPIOCSNPMODE:
2490 error = verify_area (VERIFY_READ, (void *) param3,
2491 sizeof (struct npioctl));
2492 if (error == 0) {
2493 struct npioctl npi;
2494 copy_from_user (&npi,
2495 (void *) param3,
2496 sizeof (npi));
2498 switch (npi.protocol) {
2499 case PPP_IP:
2500 npi.protocol = NP_IP;
2501 break;
2502 default:
2503 error = -EINVAL;
2506 if (error != 0)
2507 break;
2509 if (param2 == PPPIOCGNPMODE) {
2510 npi.mode = ppp->sc_npmode[npi.protocol];
2511 error = verify_area (VERIFY_WRITE,
2512 (void *) param3,
2513 sizeof (npi));
2514 if (error != 0)
2515 break;
2517 copy_to_user ((void *) param3,
2518 &npi,
2519 sizeof (npi));
2520 break;
2523 if (npi.mode != ppp->sc_npmode[npi.protocol]) {
2524 ppp->sc_npmode[npi.protocol] = npi.mode;
2525 if (npi.mode != NPMODE_QUEUE) {
2526 /* ppp_requeue(ppp); maybe needed */
2527 ppp_tty_wakeup (ppp2tty(ppp));
2531 break;
2533 * Allow users to read, but not set, the serial port parameters
2535 case TCGETS:
2536 case TCGETA:
2537 error = n_tty_ioctl (tty, file, param2, param3);
2538 break;
2540 case FIONREAD:
2541 error = verify_area (VERIFY_WRITE,
2542 (void *) param3,
2543 sizeof (int));
2544 if (error == 0) {
2545 int count = ppp->ubuf->tail - ppp->ubuf->head;
2546 if (count < 0)
2547 count += (ppp->ubuf->size + 1);
2549 put_user (count, (int *) param3);
2551 break;
2553 * All other ioctl() events will come here.
2555 default:
2556 if (ppp->flags & SC_DEBUG)
2557 printk (KERN_ERR
2558 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2559 param2,
2560 param3);
2562 error = -ENOIOCTLCMD;
2563 break;
2565 return error;
2569 * TTY callback.
2571 * Process the poll() statement for the PPP device.
2574 static unsigned int
2575 ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait)
2577 struct ppp *ppp = tty2ppp (tty);
2578 unsigned int mask = 0;
2580 if(ppp && ppp->magic == PPP_MAGIC) {
2581 CHECK_PPP (0);
2583 poll_wait(&ppp->read_wait, wait);
2584 poll_wait(&ppp->write_wait, wait);
2586 /* Must lock the user buffer area while checking. */
2587 if(set_bit(0, &ppp->ubuf->locked) == 0) {
2588 if(ppp->ubuf->head != ppp->ubuf->tail)
2589 mask |= POLLIN | POLLRDNORM;
2590 clear_bit(0, &ppp->ubuf->locked);
2592 if(tty->flags & (1 << TTY_OTHER_CLOSED))
2593 mask |= POLLHUP;
2594 if(tty_hung_up_p(filp))
2595 mask |= POLLHUP;
2596 if(ppp->tbuf->locked == 0)
2597 mask |= POLLOUT | POLLWRNORM;
2599 return mask;
2602 /*************************************************************
2603 * NETWORK OUTPUT
2604 * This routine accepts requests from the network layer
2605 * and attempts to deliver the packets.
2606 * It also includes various routines we are compelled to
2607 * have to make the network layer work (arp, etc...).
2608 *************************************************************/
2611 * Callback from the network layer when the device goes up.
2614 static int
2615 ppp_dev_open (struct device *dev)
2617 struct ppp *ppp = dev2ppp (dev);
2619 /* reset POINTOPOINT every time, since dev_close zaps it! */
2620 dev->flags |= IFF_POINTOPOINT;
2622 if (ppp2tty (ppp) == NULL) {
2623 if (ppp->flags & SC_DEBUG)
2624 printk (KERN_ERR
2625 "ppp: %s not connected to a TTY! can't go open!\n",
2626 dev->name);
2627 return -ENXIO;
2630 if (ppp->flags & SC_DEBUG)
2631 printk (KERN_INFO
2632 "ppp: channel %s going up for IP packets!\n",
2633 dev->name);
2635 CHECK_PPP (-ENXIO);
2636 return 0;
2640 * Callback from the network layer when the ppp device goes down.
2643 static int
2644 ppp_dev_close (struct device *dev)
2646 struct ppp *ppp = dev2ppp (dev);
2648 if (ppp2tty (ppp) == NULL) {
2649 if (ppp->flags & SC_DEBUG)
2650 printk (KERN_ERR
2651 "ppp: %s not connected to a TTY! can't go down!\n",
2652 dev->name);
2653 return -ENXIO;
2656 * We don't do anything about the device going down. It is not important
2657 * for us.
2659 if (ppp->flags & SC_DEBUG)
2660 printk (KERN_INFO
2661 "ppp: channel %s going down for IP packets!\n",
2662 dev->name);
2663 CHECK_PPP (-ENXIO);
2664 return 0;
2668 * IOCTL operation to read the version of the driver.
2671 static int
2672 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2674 int error;
2675 int len;
2676 char *result;
2678 * Must have write access to the buffer.
2680 result = (char *) ifr->ifr_ifru.ifru_data;
2681 len = strlen (szVersion) + 1;
2682 error = verify_area (VERIFY_WRITE, result, len);
2684 * Move the version data
2686 if (error == 0)
2687 copy_to_user (result, szVersion, len);
2689 return error;
2693 * IOCTL to read the statistics for the pppstats program.
2696 static int
2697 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2699 struct ppp_stats *result, temp;
2700 int error;
2702 * Must have write access to the buffer.
2704 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2705 error = verify_area (VERIFY_WRITE,
2706 result,
2707 sizeof (temp));
2709 * Supply the information for the caller. First move the version data
2710 * then move the ppp stats; and finally the vj stats.
2712 memset (&temp, 0, sizeof(temp));
2713 if (error == 0 && dev->flags & IFF_UP) {
2714 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2715 if (ppp->slcomp != NULL) {
2716 temp.vj.vjs_packets = ppp->slcomp->sls_o_compressed+
2717 ppp->slcomp->sls_o_uncompressed;
2718 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2719 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2720 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2721 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2722 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2723 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2724 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2728 if (error == 0)
2729 copy_to_user (result, &temp, sizeof (temp));
2730 return error;
2734 * IOCTL to read the compression statistics for the pppstats program.
2737 static int
2738 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2740 struct ppp_comp_stats *result, temp;
2741 int error;
2743 * Must have write access to the buffer.
2745 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2746 error = verify_area (VERIFY_WRITE,
2747 result,
2748 sizeof (temp));
2750 * Supply the information for the caller.
2752 memset (&temp, 0, sizeof(temp));
2753 if (error == 0 && dev->flags & IFF_UP) {
2754 if (ppp->sc_xc_state != NULL)
2755 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2756 &temp.c);
2758 if (ppp->sc_rc_state != NULL)
2759 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2760 &temp.d);
2763 * Move the data to the caller's buffer
2765 if (error == 0)
2766 copy_to_user (result, &temp, sizeof (temp));
2767 return error;
2771 * Callback from the network layer to process the sockioctl functions.
2774 static int
2775 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2777 struct ppp *ppp = dev2ppp (dev);
2778 int error;
2780 * Process the requests
2782 switch (cmd) {
2783 case SIOCGPPPSTATS:
2784 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2785 break;
2787 case SIOCGPPPCSTATS:
2788 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2789 break;
2791 case SIOCGPPPVER:
2792 error = ppp_dev_ioctl_version (ppp, ifr);
2793 break;
2795 default:
2796 error = -EINVAL;
2797 break;
2799 return error;
2803 * Send an IP frame to the remote with vj header compression.
2805 * Return 0 if frame was queued for transmission.
2806 * 1 if frame must be re-queued for later driver support.
2809 static int
2810 ppp_dev_xmit_ip (struct device *dev, struct ppp *ppp, __u8 *data)
2812 int proto = PPP_IP;
2813 int len;
2814 struct ppp_hdr *hdr;
2815 struct tty_struct *tty = ppp2tty (ppp);
2817 * Obtain the length from the IP header.
2819 len = ((struct iphdr *)data) -> tot_len;
2820 len = ntohs (len);
2822 * Validate the tty interface
2824 if (tty == NULL) {
2825 if (ppp->flags & SC_DEBUG)
2826 printk (KERN_ERR
2827 "ppp_dev_xmit: %s not connected to a TTY!\n",
2828 dev->name);
2829 return 0;
2832 * Ensure that the PPP device is still up
2834 if (!(dev->flags & IFF_UP)) {
2835 if (ppp->flags & SC_DEBUG)
2836 printk (KERN_WARNING
2837 "ppp_dev_xmit: packet sent on interface %s,"
2838 " which is down for IP\n",
2839 dev->name);
2840 return 0;
2843 * Branch on the type of processing for the IP frame.
2845 switch (ppp->sc_npmode[NP_IP]) {
2846 case NPMODE_PASS:
2847 break;
2849 case NPMODE_ERROR:
2850 if (ppp->flags & SC_DEBUG)
2851 printk (KERN_WARNING
2852 "ppp_dev_xmit: npmode = NPMODE_ERROR on %s\n",
2853 dev->name);
2854 return 0;
2856 case NPMODE_DROP:
2857 if (ppp->flags & SC_DEBUG)
2858 printk (KERN_WARNING
2859 "ppp_dev_xmit: npmode = NPMODE_DROP on %s\n",
2860 dev->name);
2861 return 0;
2863 case NPMODE_QUEUE:
2864 break;
2866 default:
2867 if (ppp->flags & SC_DEBUG)
2868 printk (KERN_WARNING
2869 "ppp_dev_xmit: unknown npmode %d on %s\n",
2870 ppp->sc_npmode[NP_IP],
2871 dev->name);
2872 return 0;
2875 * Detect a change in the transfer size
2877 if (ppp->mtu != ppp2dev (ppp)->mtu) {
2878 ppp_changedmtu (ppp,
2879 ppp2dev (ppp)->mtu,
2880 ppp->mru);
2883 * Acquire the lock on the transmission buffer. If the buffer was busy then
2884 * mark the device as busy.
2886 if (lock_buffer (ppp->wbuf) != 0) {
2887 dev->tbusy = 1;
2888 return 1;
2891 * Print the frame being sent
2893 if (ppp->flags & SC_LOG_OUTPKT)
2894 ppp_print_buffer ("ppp outpkt", data, len);
2896 * At this point, the buffer will be transmitted. There is no other exit.
2898 * Try to compress the header.
2900 if (ppp->flags & SC_COMP_TCP) {
2901 len = slhc_compress (ppp->slcomp, data, len,
2902 buf_base (ppp->cbuf) + PPP_HARD_HDR_LEN,
2903 &data,
2904 (ppp->flags & SC_NO_TCP_CCID) == 0);
2906 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2907 proto = PPP_VJC_COMP;
2908 data[0] ^= SL_TYPE_COMPRESSED_TCP;
2909 } else {
2910 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2911 proto = PPP_VJC_UNCOMP;
2912 data[0] = (data[0] & 0x0f) | 0x40;
2916 * Send the frame
2918 len += PPP_HARD_HDR_LEN;
2919 hdr = &((struct ppp_hdr *) data)[-1];
2921 hdr->address = PPP_ALLSTATIONS;
2922 hdr->control = PPP_UI;
2923 hdr->protocol[0] = 0;
2924 hdr->protocol[1] = proto;
2926 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
2930 * Send an IPX (or any other non-IP) frame to the remote.
2932 * Return 0 if frame was queued for transmission.
2933 * 1 if frame must be re-queued for later driver support.
2935 static int
2936 ppp_dev_xmit_ipx (struct device *dev, struct ppp *ppp,
2937 __u8 *data, int len, int proto)
2939 struct tty_struct *tty = ppp2tty (ppp);
2940 struct ppp_hdr *hdr;
2942 * Validate the tty interface
2944 if (tty == NULL) {
2945 if (ppp->flags & SC_DEBUG)
2946 printk (KERN_ERR
2947 "ppp_dev_xmit: %s not connected to a TTY!\n",
2948 dev->name);
2949 return 0;
2952 * Ensure that the PPP device is still up
2954 if (!(dev->flags & IFF_UP)) {
2955 if (ppp->flags & SC_DEBUG)
2956 printk (KERN_WARNING
2957 "ppp_dev_xmit: packet sent on interface %s,"
2958 " which is down\n",
2959 dev->name);
2960 return 0;
2963 * Detect a change in the transfer size
2965 if (ppp->mtu != ppp2dev (ppp)->mtu) {
2966 ppp_changedmtu (ppp,
2967 ppp2dev (ppp)->mtu,
2968 ppp->mru);
2971 * Acquire the lock on the transmission buffer. If the buffer was busy then
2972 * mark the device as busy.
2974 if (lock_buffer (ppp->wbuf) != 0) {
2975 dev->tbusy = 1;
2976 return 1;
2979 * Print the frame being sent
2981 if (ppp->flags & SC_LOG_OUTPKT)
2982 ppp_print_buffer ("ppp outpkt", data, len);
2984 * Send the frame
2986 len += PPP_HARD_HDR_LEN;
2987 hdr = &((struct ppp_hdr *) data)[-1];
2989 hdr->address = PPP_ALLSTATIONS;
2990 hdr->control = PPP_UI;
2991 hdr->protocol[0] = proto >> 8;
2992 hdr->protocol[1] = proto;
2994 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
2998 * Send a frame to the remote.
3001 static int
3002 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3004 int answer, len;
3005 __u8 *data;
3006 struct ppp *ppp = dev2ppp (dev);
3007 struct tty_struct *tty = ppp2tty (ppp);
3009 * just a little sanity check.
3011 if (skb == NULL) {
3012 if (ppp->flags & SC_DEBUG)
3013 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3014 return 0;
3017 * Avoid timing problem should tty hangup while data is queued to be sent
3019 if (!ppp->inuse) {
3020 dev_kfree_skb (skb, FREE_WRITE);
3021 dev_close (dev);
3022 return 0;
3025 * Validate the tty linkage
3027 if (ppp->flags & SC_DEBUG)
3028 printk (KERN_DEBUG "ppp_dev_xmit [%s]: skb %p\n",
3029 dev->name, skb);
3031 * Validate the tty interface
3033 if (tty == NULL) {
3034 if (ppp->flags & SC_DEBUG)
3035 printk (KERN_ERR
3036 "ppp_dev_xmit: %s not connected to a TTY!\n",
3037 dev->name);
3038 dev_kfree_skb (skb, FREE_WRITE);
3039 return 0;
3042 * Fetch the pointer to the data
3044 len = skb->len;
3045 data = skb_data(skb);
3047 * Bug trap for null data. Release the skb and bail out.
3049 if(data == NULL) {
3050 printk("ppp_dev_xmit: data=NULL before ppp_dev_xmit_ip.\n");
3051 dev_kfree_skb (skb, FREE_WRITE);
3052 return 0;
3055 * Look at the protocol in the skb to determine the difference between
3056 * an IP frame and an IPX frame.
3058 switch (ntohs (skb->protocol)) {
3059 case ETH_P_IPX:
3060 answer = ppp_dev_xmit_ipx (dev, ppp, data, len, PPP_IPX);
3061 break;
3063 case ETH_P_IP:
3064 answer = ppp_dev_xmit_ip (dev, ppp, data);
3065 break;
3067 default: /* All others have no support at this time. */
3068 dev_kfree_skb (skb, FREE_WRITE);
3069 return 0;
3072 * This is the end of the transmission. Release the buffer if it was sent.
3074 if (answer == 0) {
3075 dev_kfree_skb (skb, FREE_WRITE);
3076 ppp->ddinfo.xmit_idle = jiffies;
3078 return answer;
3082 * Generate the statistic information for the /proc/net/dev listing.
3085 static struct net_device_stats *
3086 ppp_dev_stats (struct device *dev)
3088 struct ppp *ppp = dev2ppp (dev);
3089 static struct net_device_stats ppp_stats;
3091 ppp_stats.rx_packets = ppp->stats.ppp_ipackets;
3092 ppp_stats.rx_errors = ppp->stats.ppp_ierrors;
3093 ppp_stats.rx_dropped = ppp->stats.ppp_ierrors;
3094 ppp_stats.rx_fifo_errors = 0;
3095 ppp_stats.rx_length_errors = 0;
3096 ppp_stats.rx_over_errors = 0;
3097 ppp_stats.rx_crc_errors = 0;
3098 ppp_stats.rx_frame_errors = 0;
3099 ppp_stats.tx_packets = ppp->stats.ppp_opackets;
3100 ppp_stats.tx_errors = ppp->stats.ppp_oerrors;
3101 ppp_stats.tx_dropped = 0;
3102 ppp_stats.tx_fifo_errors = 0;
3103 ppp_stats.collisions = 0;
3104 ppp_stats.tx_carrier_errors = 0;
3105 ppp_stats.tx_aborted_errors = 0;
3106 ppp_stats.tx_window_errors = 0;
3107 ppp_stats.tx_heartbeat_errors = 0;
3109 if (ppp->flags & SC_DEBUG)
3110 printk (KERN_INFO "ppp_dev_stats called");
3111 return &ppp_stats;
3114 /*************************************************************
3115 * UTILITIES
3116 * Miscellany called by various functions above.
3117 *************************************************************/
3119 /* Locate the previous instance of the PPP channel */
3120 static struct ppp *
3121 ppp_find (int pid_value)
3123 int if_num;
3124 ppp_ctrl_t *ctl;
3125 struct ppp *ppp;
3127 /* try to find the exact same free device which we had before */
3128 ctl = ppp_list;
3129 if_num = 0;
3131 while (ctl) {
3132 ppp = ctl2ppp (ctl);
3133 if (!set_bit(0, &ppp->inuse)) {
3134 if (ppp->sc_xfer == pid_value) {
3135 ppp->sc_xfer = 0;
3136 return (ppp);
3138 clear_bit (0, &ppp->inuse);
3140 ctl = ctl->next;
3141 if (++if_num == max_dev)
3142 break;
3144 return NULL;
3147 /* allocate or create a PPP channel */
3148 static struct ppp *
3149 ppp_alloc (void)
3151 int if_num;
3152 int status;
3153 ppp_ctrl_t *ctl;
3154 struct device *dev;
3155 struct ppp *ppp;
3157 /* try to find an free device */
3158 ctl = ppp_list;
3159 if_num = 0;
3161 while (ctl) {
3162 ppp = ctl2ppp (ctl);
3163 if (!set_bit(0, &ppp->inuse))
3164 return (ppp);
3165 ctl = ctl->next;
3166 if (++if_num == max_dev)
3167 return (NULL);
3170 * There are no available items. Allocate a device from the system pool
3172 ctl = (ppp_ctrl_t *) kmalloc (sizeof(ppp_ctrl_t), GFP_KERNEL);
3173 if (ctl) {
3174 (void) memset(ctl, 0, sizeof(ppp_ctrl_t));
3175 ppp = ctl2ppp (ctl);
3176 dev = ctl2dev (ctl);
3178 /* initialize channel control data */
3179 set_bit(0, &ppp->inuse);
3181 ppp->line = if_num;
3182 ppp->tty = NULL;
3183 ppp->dev = dev;
3185 dev->next = NULL;
3186 dev->init = ppp_init_dev;
3187 dev->name = ctl->name;
3188 dev->base_addr = (__u32) if_num;
3189 dev->priv = (void *) ppp;
3191 sprintf (dev->name, "ppp%d", if_num);
3193 /* link in the new channel */
3194 ctl->next = ppp_list;
3195 ppp_list = ctl;
3197 /* register device so that we can be ifconfig'd */
3198 /* ppp_init_dev() will be called as a side-effect */
3200 status = register_netdev (dev);
3201 if (status == 0) {
3202 printk (KERN_INFO "registered device %s\n", dev->name);
3203 return (ppp);
3206 printk (KERN_ERR
3207 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3208 dev->name, status);
3209 /* This one will forever be busy as it is not initialized */
3211 return (NULL);
3215 * Utility procedures to print a buffer in hex/ascii
3218 static void
3219 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3221 register __u8 next_ch;
3222 static char hex[] = "0123456789ABCDEF";
3224 while (count-- > 0) {
3225 next_ch = *in++;
3226 *out++ = hex[(next_ch >> 4) & 0x0F];
3227 *out++ = hex[next_ch & 0x0F];
3228 ++out;
3232 static void
3233 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3235 register __u8 next_ch;
3237 while (count-- > 0) {
3238 next_ch = *in++;
3240 if (next_ch < 0x20 || next_ch > 0x7e)
3241 *out++ = '.';
3242 else {
3243 *out++ = next_ch;
3244 if (next_ch == '%') /* printk/syslogd has a bug !! */
3245 *out++ = '%';
3248 *out = '\0';
3251 static void
3252 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3254 __u8 line[44];
3256 if (name != (__u8 *) NULL)
3257 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3259 while (count > 8) {
3260 memset (line, 32, 44);
3261 ppp_print_hex (line, buf, 8);
3262 ppp_print_char (&line[8 * 3], buf, 8);
3263 printk (KERN_DEBUG "%s\n", line);
3264 count -= 8;
3265 buf += 8;
3268 if (count > 0) {
3269 memset (line, 32, 44);
3270 ppp_print_hex (line, buf, count);
3271 ppp_print_char (&line[8 * 3], buf, count);
3272 printk (KERN_DEBUG "%s\n", line);
3276 /*************************************************************
3277 * Compressor module interface
3278 *************************************************************/
3280 struct compressor_link {
3281 struct compressor_link *next;
3282 struct compressor *comp;
3285 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3287 static struct compressor *find_compressor (int type)
3289 struct compressor_link *lnk;
3290 __u32 flags;
3292 save_flags(flags);
3293 cli();
3295 lnk = ppp_compressors;
3296 while (lnk != (struct compressor_link *) 0) {
3297 if ((int) (__u8) lnk->comp->compress_proto == type) {
3298 restore_flags(flags);
3299 return lnk->comp;
3301 lnk = lnk->next;
3304 restore_flags(flags);
3305 return (struct compressor *) 0;
3308 #ifdef CONFIG_MODULES
3310 static int ppp_register_compressor (struct compressor *cp)
3312 struct compressor_link *new;
3313 __u32 flags;
3315 new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3317 if (new == (struct compressor_link *) 0)
3318 return 1;
3320 save_flags(flags);
3321 cli();
3323 if (find_compressor (cp->compress_proto)) {
3324 restore_flags(flags);
3325 kfree (new);
3326 return 0;
3329 new->next = ppp_compressors;
3330 new->comp = cp;
3331 ppp_compressors = new;
3333 restore_flags(flags);
3334 return 0;
3337 static void ppp_unregister_compressor (struct compressor *cp)
3339 struct compressor_link *prev = (struct compressor_link *) 0;
3340 struct compressor_link *lnk;
3341 __u32 flags;
3343 save_flags(flags);
3344 cli();
3346 lnk = ppp_compressors;
3347 while (lnk != (struct compressor_link *) 0) {
3348 if (lnk->comp == cp) {
3349 if (prev)
3350 prev->next = lnk->next;
3351 else
3352 ppp_compressors = lnk->next;
3353 kfree (lnk);
3354 break;
3356 prev = lnk;
3357 lnk = lnk->next;
3359 restore_flags(flags);
3362 #endif
3364 /*************************************************************
3365 * Module support routines
3366 *************************************************************/
3368 #ifdef MODULE
3370 init_module(void)
3372 int status;
3374 /* register our line disciplines */
3375 status = ppp_first_time();
3376 if (status != 0)
3377 printk (KERN_INFO
3378 "PPP: ppp_init() failure %d\n", status);
3380 return (status);
3383 void
3384 cleanup_module(void)
3386 int status;
3387 ppp_ctrl_t *ctl, *next_ctl;
3388 struct device *dev;
3389 struct ppp *ppp;
3390 int busy_flag = 0;
3392 * Ensure that the devices are not in operation.
3394 ctl = ppp_list;
3395 while (ctl) {
3396 ppp = ctl2ppp (ctl);
3397 if (ppp->inuse && ppp->tty != NULL) {
3398 busy_flag = 1;
3399 break;
3402 dev = ctl2dev (ctl);
3403 if (dev->start || dev->flags & IFF_UP) {
3404 busy_flag = 1;
3405 break;
3407 ctl = ctl->next;
3410 * Ensure that there are no compressor modules registered
3412 if (ppp_compressors != NULL)
3413 busy_flag = 1;
3415 if (busy_flag) {
3416 printk (KERN_INFO
3417 "PPP: device busy, remove delayed\n");
3418 return;
3421 * Release the tty registration of the line discipline so that no new entries
3422 * may be created.
3424 status = tty_register_ldisc (N_PPP, NULL);
3425 if (status != 0)
3426 printk (KERN_INFO
3427 "PPP: Unable to unregister ppp line discipline "
3428 "(err = %d)\n", status);
3429 else
3430 printk (KERN_INFO
3431 "PPP: ppp line discipline successfully unregistered\n");
3433 * De-register the devices so that there is no problem with them
3435 next_ctl = ppp_list;
3436 while (next_ctl) {
3437 ctl = next_ctl;
3438 next_ctl = ctl->next;
3439 ppp = ctl2ppp (ctl);
3440 dev = ctl2dev (ctl);
3442 ppp_release (ppp);
3443 unregister_netdev (dev);
3444 kfree (ctl);
3447 #endif