- Revert TCP delayed ACK fix, and fix correctly.
[davej-history.git] / drivers / net / ppp.c
blobde70cd00107a878aaa117e124a5e9352c0cb91cb
1 /* PPP for Linux
3 * Michael Callahan <callahan@maths.ox.ac.uk>
4 * Al Longyear <longyear@netcom.com>
5 * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
6 * Cyrus Durgin <cider@speakeasy.org> (changes for kmod)
8 * Dynamic PPP devices by Jim Freeman <jfree@caldera.com>.
9 * ppp_tty_receive ``noisy-raise-bug'' fixed by Ove Ewerlid <ewerlid@syscon.uu.se>
11 * ==FILEVERSION 980123==
13 * NOTE TO MAINTAINERS:
14 * If you modify this file at all, please set the number above to the
15 * date of the modification as YYMMDD (year month day).
16 * ppp.c is shipped with a PPP distribution as well as with the kernel;
17 * if everyone increases the FILEVERSION number above, then scripts
18 * can do the right thing when deciding whether to install a new ppp.c
19 * file. Don't change the format of that line otherwise, so the
20 * installation script can recognize it.
24 Sources:
26 slip.c
28 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
29 Multi-protocol Datagrams over Point-to-Point Links
31 RFC1332: IPCP
33 ppp-2.0
35 Flags for this module (any combination is acceptable for testing.):
37 OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
38 character. This is normally set to ((HZ * 3) / 2).
39 This is 1.5 seconds. If zero then the leading
40 flag is always sent.
42 CHECK_CHARACTERS - Enable the checking on all received characters for
43 8 data bits, no parity. This adds a small amount of
44 processing for each received character.
47 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
49 #define CHECK_CHARACTERS 1
50 #define PPP_COMPRESS 1
52 /* $Id: ppp.c,v 1.14 1997/11/27 06:04:45 paulus Exp $ */
54 #include <linux/config.h>/* for CONFIG_KMOD */
55 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/sched.h>
58 #include <linux/types.h>
59 #include <linux/fcntl.h>
60 #include <linux/poll.h>
61 #include <linux/interrupt.h>
62 #include <linux/ptrace.h>
63 #include <linux/in.h>
64 #include <linux/malloc.h>
65 #include <linux/tty.h>
66 #include <linux/errno.h>
67 #include <linux/sched.h>/* to get the struct task_struct */
68 #include <linux/string.h>/* used in new tty drivers */
69 #include <linux/signal.h>/* used in new tty drivers */
70 #include <asm/system.h>
71 #include <asm/bitops.h>
72 #include <asm/uaccess.h>
73 #include <linux/if.h>
74 #include <linux/if_ether.h>
75 #include <linux/netdevice.h>
76 #include <linux/skbuff.h>
77 #include <linux/rtnetlink.h>
78 #include <linux/inet.h>
79 #include <linux/ioctl.h>
80 #include <linux/init.h>
82 typedefstruct sk_buff sk_buff;
83 #define skb_data(skb) ((__u8 *) (skb)->data)
85 #include <linux/ip.h>
86 #include <linux/tcp.h>
87 #include <linux/if_arp.h>
88 #include <net/slhc_vj.h>
90 #define fcstab ppp_crc16_table/* Name of the table in the kernel */
91 #include <linux/ppp_defs.h>
93 #include <linux/socket.h>
94 #include <linux/if_ppp.h>
95 #include <linux/if_pppvar.h>
96 #include <linux/ppp-comp.h>
98 #ifdef CONFIG_KMOD
99 #include <linux/kmod.h>
100 #endif
102 #ifndef PPP_IPX
103 #define PPP_IPX 0x2b/* IPX protocol over PPP */
104 #endif
106 #ifndef PPP_LQR
107 #define PPP_LQR 0xc025/* Link Quality Reporting Protocol */
108 #endif
110 static intppp_register_compressor(struct compressor *cp);
111 static voidppp_unregister_compressor(struct compressor *cp);
114 * Local functions
117 static struct compressor *find_compressor(int type);
118 static voidppp_init_ctrl_blk(registerstruct ppp *);
119 static voidppp_kick_tty(struct ppp *,struct ppp_buffer *bfr);
120 static struct ppp *ppp_alloc(void);
121 static struct ppp *ppp_find(int pid_value);
122 static voidppp_print_buffer(const __u8 *,const __u8 *,int);
123 extern inlinevoidppp_stuff_char(struct ppp *ppp,
124 registerstruct ppp_buffer *buf,
125 register __u8 chr);
126 extern inlineintlock_buffer(registerstruct ppp_buffer *buf);
127 static intppp_dev_xmit_ip(struct ppp *ppp,struct ppp_buffer *buf,
128 __u8 *data,int len,enum NPmode npmode);
130 static intrcv_proto_ip(struct ppp *, __u16, __u8 *,int);
131 static intrcv_proto_ipx(struct ppp *, __u16, __u8 *,int);
132 static intrcv_proto_vjc_comp(struct ppp *, __u16, __u8 *,int);
133 static intrcv_proto_vjc_uncomp(struct ppp *, __u16, __u8 *,int);
134 static intrcv_proto_unknown(struct ppp *, __u16, __u8 *,int);
135 static intrcv_proto_lqr(struct ppp *, __u16, __u8 *,int);
136 static voidppp_doframe_lower(struct ppp *, __u8 *,int);
137 static intppp_doframe(struct ppp *);
139 static voidppp_proto_ccp(struct ppp *ppp, __u8 *dp,int len,int rcvd);
140 static intrcv_proto_ccp(struct ppp *, __u16, __u8 *,int);
142 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
144 #ifndef OPTIMIZE_FLAG_TIME
145 #define OPTIMIZE_FLAG_TIME 0
146 #endif
149 * Parameters which may be changed via insmod.
152 static int flag_time = OPTIMIZE_FLAG_TIME;
153 MODULE_PARM(flag_time,"i");
156 * The "main" procedure to the ppp device
159 intppp_init(struct device *);
162 * Network device driver callback routines
165 static intppp_dev_open(struct device *);
166 static intppp_dev_ioctl(struct device *dev,struct ifreq *ifr,int cmd);
167 static intppp_dev_close(struct device *);
168 static intppp_dev_xmit(sk_buff *,struct device *);
169 static struct net_device_stats *ppp_dev_stats(struct device *);
172 * TTY callbacks
175 static ssize_t ppp_tty_read(struct tty_struct *,struct file *, __u8 *,
176 size_t);
177 static ssize_t ppp_tty_write(struct tty_struct *,struct file *,
178 const __u8 *,size_t);
179 static intppp_tty_ioctl(struct tty_struct *,struct file *,unsigned int,
180 unsigned long);
181 static unsigned intppp_tty_poll(struct tty_struct *tty,struct file *filp,
182 poll_table * wait);
183 static intppp_tty_open(struct tty_struct *);
184 static voidppp_tty_close(struct tty_struct *);
185 static intppp_tty_room(struct tty_struct *tty);
186 static voidppp_tty_receive(struct tty_struct *tty,const __u8 * cp,
187 char*fp,int count);
188 static voidppp_tty_wakeup(struct tty_struct *tty);
190 #define CHECK_PPP_MAGIC(ppp) do { \
191 if (ppp->magic != PPP_MAGIC) { \
192 printk(KERN_WARNING"bad magic for ppp %p at %s:%d\n", \
193 ppp, __FILE__, __LINE__); \
195 } while (0)
196 #define CHECK_PPP(a) do { \
197 CHECK_PPP_MAGIC(ppp); \
198 if (!ppp->inuse) { \
199 printk (ppp_warning, __LINE__); \
200 return a; \
202 } while (0)
203 #define CHECK_PPP_VOID() do { \
204 CHECK_PPP_MAGIC(ppp); \
205 if (!ppp->inuse) { \
206 printk (ppp_warning, __LINE__); \
208 } while (0)
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) ((ppp)->tty)
219 #define ppp2dev(ppp) (&(ppp)->dev)
221 static struct ppp *ppp_list = NULL;
222 static struct ppp *ppp_last = NULL;
224 /* Buffer types */
225 #define BUFFER_TYPE_DEV_RD 0/* ppp read buffer */
226 #define BUFFER_TYPE_TTY_WR 1/* tty write buffer */
227 #define BUFFER_TYPE_DEV_WR 2/* ppp write buffer */
228 #define BUFFER_TYPE_TTY_RD 3/* tty read buffer */
229 #define BUFFER_TYPE_VJ 4/* vj compression buffer */
231 /* Define this string only once for all macro invocations */
232 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
234 static char szVersion[] = PPP_VERSION;
237 * Information for the protocol decoder
240 typedefint(*pfn_proto) (struct ppp *, __u16, __u8 *,int);
242 typedefstruct ppp_proto_struct {
243 int proto;
244 pfn_proto func;
245 } ppp_proto_type;
247 static
248 ppp_proto_type proto_list[] = {
249 { PPP_IP, rcv_proto_ip },
250 { PPP_IPX, rcv_proto_ipx },
251 { PPP_VJC_COMP, rcv_proto_vjc_comp },
252 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
253 { PPP_LQR, rcv_proto_lqr },
254 { PPP_CCP, rcv_proto_ccp },
255 {0, rcv_proto_unknown }/* !!! MUST BE LAST !!! */
258 __u16 ppp_crc16_table[256] =
260 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,
261 0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7,
262 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,
263 0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876,
264 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,
265 0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5,
266 0x3183,0x200a,0x1291,0x0318,0x77a7,0x662e,0x54b5,0x453c,
267 0xbdcb,0xac42,0x9ed9,0x8f50,0xfbef,0xea66,0xd8fd,0xc974,
268 0x4204,0x538d,0x6116,0x709f,0x0420,0x15a9,0x2732,0x36bb,
269 0xce4c,0xdfc5,0xed5e,0xfcd7,0x8868,0x99e1,0xab7a,0xbaf3,
270 0x5285,0x430c,0x7197,0x601e,0x14a1,0x0528,0x37b3,0x263a,
271 0xdecd,0xcf44,0xfddf,0xec56,0x98e9,0x8960,0xbbfb,0xaa72,
272 0x6306,0x728f,0x4014,0x519d,0x2522,0x34ab,0x0630,0x17b9,
273 0xef4e,0xfec7,0xcc5c,0xddd5,0xa96a,0xb8e3,0x8a78,0x9bf1,
274 0x7387,0x620e,0x5095,0x411c,0x35a3,0x242a,0x16b1,0x0738,
275 0xffcf,0xee46,0xdcdd,0xcd54,0xb9eb,0xa862,0x9af9,0x8b70,
276 0x8408,0x9581,0xa71a,0xb693,0xc22c,0xd3a5,0xe13e,0xf0b7,
277 0x0840,0x19c9,0x2b52,0x3adb,0x4e64,0x5fed,0x6d76,0x7cff,
278 0x9489,0x8500,0xb79b,0xa612,0xd2ad,0xc324,0xf1bf,0xe036,
279 0x18c1,0x0948,0x3bd3,0x2a5a,0x5ee5,0x4f6c,0x7df7,0x6c7e,
280 0xa50a,0xb483,0x8618,0x9791,0xe32e,0xf2a7,0xc03c,0xd1b5,
281 0x2942,0x38cb,0x0a50,0x1bd9,0x6f66,0x7eef,0x4c74,0x5dfd,
282 0xb58b,0xa402,0x9699,0x8710,0xf3af,0xe226,0xd0bd,0xc134,
283 0x39c3,0x284a,0x1ad1,0x0b58,0x7fe7,0x6e6e,0x5cf5,0x4d7c,
284 0xc60c,0xd785,0xe51e,0xf497,0x8028,0x91a1,0xa33a,0xb2b3,
285 0x4a44,0x5bcd,0x6956,0x78df,0x0c60,0x1de9,0x2f72,0x3efb,
286 0xd68d,0xc704,0xf59f,0xe416,0x90a9,0x8120,0xb3bb,0xa232,
287 0x5ac5,0x4b4c,0x79d7,0x685e,0x1ce1,0x0d68,0x3ff3,0x2e7a,
288 0xe70e,0xf687,0xc41c,0xd595,0xa12a,0xb0a3,0x8238,0x93b1,
289 0x6b46,0x7acf,0x4854,0x59dd,0x2d62,0x3ceb,0x0e70,0x1ff9,
290 0xf78f,0xe606,0xd49d,0xc514,0xb1ab,0xa022,0x92b9,0x8330,
291 0x7bc7,0x6a4e,0x58d5,0x495c,0x3de3,0x2c6a,0x1ef1,0x0f78
294 #ifdef CHECK_CHARACTERS
295 static __u32 paritytab[8] =
297 0x96696996,0x69969669,0x69969669,0x96696996,
298 0x69969669,0x96696996,0x96696996,0x69969669
300 #endif
302 /* local function to store a value into the LQR frame */
303 extern inline __u8 *store_long(register __u8 *p,registerint value) {
304 *p++ = (__u8) (value >>24);
305 *p++ = (__u8) (value >>16);
306 *p++ = (__u8) (value >>8);
307 *p++ = (__u8) value;
308 return p;
311 /*************************************************************
312 * INITIALIZATION
313 *************************************************************/
315 /* This procedure is called once and once only to define who we are to
316 * the operating system and the various procedures that it may use in
317 * accessing the ppp protocol.
320 __initfunc(static int
321 ppp_first_time(void))
323 static struct tty_ldisc ppp_ldisc;
324 int status;
326 printk(KERN_INFO
327 "PPP: version %s (demand dialling)"
328 "\n", szVersion);
330 #ifndef MODULE/* slhc module logic has its own copyright announcement */
331 printk(KERN_INFO
332 "TCP compression code copyright 1989 Regents of the "
333 "University of California\n");
334 #endif
337 * Register the tty discipline
339 (void)memset(&ppp_ldisc,0,sizeof(ppp_ldisc));
340 ppp_ldisc.magic = TTY_LDISC_MAGIC;
341 ppp_ldisc.name ="ppp";
342 ppp_ldisc.open = ppp_tty_open;
343 ppp_ldisc.close = ppp_tty_close;
344 ppp_ldisc.read = ppp_tty_read;
345 ppp_ldisc.write = ppp_tty_write;
346 ppp_ldisc.ioctl = ppp_tty_ioctl;
347 ppp_ldisc.poll = ppp_tty_poll;
348 ppp_ldisc.receive_room = ppp_tty_room;
349 ppp_ldisc.receive_buf = ppp_tty_receive;
350 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
352 status =tty_register_ldisc(N_PPP, &ppp_ldisc);
353 if(status ==0)
354 printk(KERN_INFO "PPP line discipline registered.\n");
355 else
356 printk(KERN_ERR "error registering line discipline: %d\n",
357 status);
358 return status;
361 /*************************************************************
362 * INITIALIZATION
363 *************************************************************/
365 /* called when the device is actually created */
367 static int
368 ppp_init_dev(struct device *dev)
370 dev->hard_header_len = PPP_HDRLEN;
372 /* device INFO */
373 dev->mtu = PPP_MTU;
374 dev->hard_start_xmit = ppp_dev_xmit;
375 dev->open = ppp_dev_open;
376 dev->stop = ppp_dev_close;
377 dev->get_stats = ppp_dev_stats;
378 dev->do_ioctl = ppp_dev_ioctl;
379 dev->addr_len =0;
380 dev->tx_queue_len =10;
381 dev->type = ARPHRD_PPP;
383 dev_init_buffers(dev);
385 /* New-style flags */
386 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
388 return0;
392 * Local procedure to initialize the ppp structure
395 static void
396 ppp_init_ctrl_blk(registerstruct ppp *ppp)
398 ppp->magic = PPP_MAGIC;
399 ppp->toss =0xE0;
400 ppp->escape =0;
402 ppp->flags =0;
403 ppp->mtu = PPP_MTU;
404 ppp->mru = PPP_MRU;
406 memset(ppp->xmit_async_map,0,sizeof(ppp->xmit_async_map));
407 ppp->xmit_async_map[0] =0xffffffff;
408 ppp->xmit_async_map[3] =0x60000000;
409 ppp->recv_async_map =0x00000000;
411 ppp->rbuf = NULL;
412 ppp->wbuf = NULL;
413 ppp->ubuf = NULL;
414 ppp->cbuf = NULL;
415 ppp->slcomp = NULL;
416 ppp->read_wait = NULL;
417 ppp->write_wait = NULL;
418 ppp->last_xmit = jiffies - flag_time;
419 ppp->last_recv = jiffies;
421 /* clear statistics */
422 memset(&ppp->stats,0,sizeof(struct pppstat));
423 memset(&ppp->estats,0,sizeof(ppp->estats));
425 /* PPP compression data */
426 ppp->sc_xc_state =
427 ppp->sc_rc_state = NULL;
430 EXPORT_SYMBOL(ppp_register_compressor);
431 EXPORT_SYMBOL(ppp_unregister_compressor);
432 EXPORT_SYMBOL(ppp_crc16_table);
434 /* called at boot/load time for each ppp device defined in the kernel */
436 #ifndef MODULE
438 ppp_init(struct device *dev)
440 static int first_time =1;
441 int answer =0;
443 if(first_time) {
444 first_time =0;
445 answer =ppp_first_time();
447 if(answer ==0)
448 answer = -ENODEV;
449 return answer;
451 #endif
453 #define BUFFER_MAGIC 0x1d10
454 #define CHECK_BUF_MAGIC(buf) do { \
455 if (buf->magic != BUFFER_MAGIC) { \
456 printk(KERN_WARNING"bad magic for ppp buffer %p at %s:%d\n", \
457 buf, __FILE__, __LINE__); \
459 } while (0)
462 * Routine to allocate a buffer for later use by the driver.
465 static struct ppp_buffer *
466 ppp_alloc_buf(int size,int type)
468 struct ppp_buffer *buf;
470 buf = (struct ppp_buffer *)kmalloc(size +sizeof(struct ppp_buffer),
471 GFP_ATOMIC);
473 if(buf != NULL) {
474 buf->size = size -1;/* Mask for the buffer size */
475 buf->type = type;
476 buf->locked =0;
477 buf->count =0;
478 buf->head =0;
479 buf->tail =0;
480 buf->fcs = PPP_INITFCS;
481 buf->magic = BUFFER_MAGIC;
483 return(buf);
487 * Routine to release the allocated buffer.
490 static void
491 ppp_free_buf(struct ppp_buffer *ptr)
493 if(ptr != NULL) {
494 CHECK_BUF_MAGIC(ptr);
495 kfree(ptr);
500 * Lock the indicated transmit buffer
503 extern inlineint
504 lock_buffer(registerstruct ppp_buffer *buf)
506 unsigned long state;
507 unsigned long flags;
509 * Save the current state and if free then set it to the "busy" state
511 CHECK_BUF_MAGIC(buf);
512 save_flags(flags);
513 cli();
514 state = buf->locked;
515 if(state ==0)
516 buf->locked =2;
518 restore_flags(flags);
519 return(state);
523 * MTU has been changed by the IP layer. Unfortunately we are not told
524 * about this, but we spot it ourselves and fix things up. We could be
525 * in an upcall from the tty driver, or in an ip packet queue.
528 static int
529 ppp_changedmtu(struct ppp *ppp,int new_mtu,int new_mru)
531 struct device *dev;
532 unsigned long flags;
534 struct ppp_buffer *new_rbuf;
535 struct ppp_buffer *new_wbuf;
536 struct ppp_buffer *new_cbuf;
537 struct ppp_buffer *new_tbuf;
539 struct ppp_buffer *old_rbuf;
540 struct ppp_buffer *old_wbuf;
541 struct ppp_buffer *old_cbuf;
542 struct ppp_buffer *old_tbuf;
544 int mtu, mru;
546 * Allocate the buffer from the kernel for the data
548 CHECK_PPP(0);
549 dev =ppp2dev(ppp);
550 if(ppp->flags & SC_DEBUG)
551 printk(KERN_DEBUG "%s: changedmtu %d %d\n", ppp->name,
552 new_mtu, new_mru);
553 mru = new_mru;
554 /* allow for possible escaping of every character */
555 mtu = (new_mtu *2) +20;
557 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
558 if(mru < PPP_MRU)
559 mru = PPP_MRU;
561 mru +=10;
563 new_wbuf =ppp_alloc_buf(mtu+PPP_HDRLEN, BUFFER_TYPE_DEV_WR);
564 new_tbuf =ppp_alloc_buf((PPP_MTU *2) +24, BUFFER_TYPE_TTY_WR);
565 new_rbuf =ppp_alloc_buf(mru +84, BUFFER_TYPE_DEV_RD);
566 new_cbuf =ppp_alloc_buf(mru+PPP_HDRLEN, BUFFER_TYPE_VJ);
568 * If the buffers failed to allocate then complain and release the partial
569 * allocations.
571 if(new_wbuf == NULL || new_tbuf == NULL ||
572 new_rbuf == NULL || new_cbuf == NULL) {
573 printk(KERN_ERR "ppp: failed to allocate new buffers\n");
575 ppp_free_buf(new_wbuf);
576 ppp_free_buf(new_tbuf);
577 ppp_free_buf(new_rbuf);
578 ppp_free_buf(new_cbuf);
579 return0;
582 * Update the pointers to the new buffer structures.
584 save_flags(flags);
585 cli();
586 old_wbuf = ppp->wbuf;
587 old_rbuf = ppp->rbuf;
588 old_cbuf = ppp->cbuf;
589 old_tbuf = ppp->tbuf;
591 ppp->wbuf = new_wbuf;
592 ppp->rbuf = new_rbuf;
593 ppp->cbuf = new_cbuf;
594 ppp->tbuf = new_tbuf;
596 if(old_wbuf)
597 new_wbuf->locked = old_wbuf->locked;
599 ppp->rbuf->size -=80;/* reserve space for vj header expansion */
601 dev->mem_start = (unsigned long)buf_base(new_wbuf);
602 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
603 dev->rmem_start = (unsigned long)buf_base(new_rbuf);
604 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
606 * Update the parameters for the new buffer sizes
608 ppp->toss =0xE0;/* To ignore characters until new FLAG */
609 ppp->escape =0;/* No pending escape character */
611 dev->mtu =
612 ppp->mtu = new_mtu;
613 ppp->mru = new_mru;
615 ppp->s1buf = NULL;
616 ppp->s2buf = NULL;
617 ppp->xbuf = NULL;
619 ppp->tty->flags &= ~(1<< TTY_DO_WRITE_WAKEUP);
620 ppp->flags &= ~SC_XMIT_BUSY;
622 restore_flags(flags);
624 * Release old buffer pointers
626 ppp_free_buf(old_rbuf);
627 ppp_free_buf(old_wbuf);
628 ppp_free_buf(old_cbuf);
629 ppp_free_buf(old_tbuf);
630 return1;
634 * CCP is down; free (de)compressor state if necessary.
637 static void
638 ppp_ccp_closed(struct ppp *ppp)
640 unsigned long flags;
642 save_flags(flags);
643 cli();
644 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
645 restore_flags(flags);
646 if(ppp->flags & SC_DEBUG)
647 printk(KERN_DEBUG "%s: ccp closed\n", ppp->name);
648 if(ppp->sc_xc_state) {
649 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
650 ppp->sc_xc_state = NULL;
653 if(ppp->sc_rc_state) {
654 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
655 ppp->sc_rc_state = NULL;
660 * Called to release all of the information in the current PPP structure.
662 * It is called when the ppp device goes down or if it is unable to go
663 * up.
666 static void
667 ppp_release(struct ppp *ppp)
669 struct tty_struct *tty;
670 struct device *dev;
672 CHECK_PPP_MAGIC(ppp);
673 tty =ppp2tty(ppp);
674 dev =ppp2dev(ppp);
676 if(ppp->flags & SC_DEBUG)
677 printk(KERN_DEBUG "%s released\n", ppp->name);
679 ppp_ccp_closed(ppp);
681 /* Ensure that the pppd process is not hanging on poll() */
682 wake_up_interruptible(&ppp->read_wait);
683 wake_up_interruptible(&ppp->write_wait);
685 if(tty != NULL && tty->disc_data == ppp)
686 tty->disc_data = NULL;/* Break the tty->ppp link */
688 ppp_free_buf(ppp->rbuf);
689 ppp_free_buf(ppp->wbuf);
690 ppp_free_buf(ppp->cbuf);
691 ppp_free_buf(ppp->ubuf);
692 ppp_free_buf(ppp->tbuf);
694 ppp->rbuf =
695 ppp->wbuf =
696 ppp->cbuf =
697 ppp->tbuf =
698 ppp->xbuf =
699 ppp->s1buf =
700 ppp->s2buf =
701 ppp->ubuf = NULL;
703 if(ppp->slcomp) {
704 slhc_free(ppp->slcomp);
705 ppp->slcomp = NULL;
708 ppp->inuse =0;
709 ppp->tty = NULL;
710 ppp->backup_tty = NULL;
714 * TTY callback.
716 * Called when the line discipline is changed to something
717 * else, the tty is closed, or the tty detects a hangup.
720 static void
721 ppp_tty_close(struct tty_struct *tty)
723 struct ppp *ppp =tty2ppp(tty);
725 if(ppp != NULL) {
726 if(ppp->magic != PPP_MAGIC) {
727 if(ppp->flags & SC_DEBUG)
728 printk(KERN_WARNING
729 "ppp: trying to close unopened tty!\n");
730 return;
732 CHECK_PPP_VOID();
733 tty->disc_data = NULL;
734 if(tty == ppp->backup_tty)
735 ppp->backup_tty =0;
736 if(tty != ppp->tty)
737 return;
738 if(ppp->backup_tty) {
739 ppp->tty = ppp->backup_tty;
740 }else{
741 ppp->sc_xfer =0;
742 if(ppp->flags & SC_DEBUG)
743 printk(KERN_INFO "ppp: channel %s closing.\n",
744 ppp2dev(ppp)->name);
745 ppp_release(ppp);
746 MOD_DEC_USE_COUNT;
752 * TTY callback.
754 * Called when the tty discipline is switched to PPP.
757 static int
758 ppp_tty_open(struct tty_struct *tty)
760 struct ppp *ppp =tty2ppp(tty);
761 int indx;
763 * There should not be an existing table for this slot.
765 if(ppp) {
766 printk(KERN_ERR
767 "ppp_tty_open: gack! tty already associated to %s!\n",
768 ppp->magic == PPP_MAGIC ?ppp2dev(ppp)->name
769 :"unknown");
770 return-EEXIST;
773 * Allocate the structure from the system
775 ppp =ppp_find(current->pid);
776 if(ppp != NULL) {
778 * If we are taking over a ppp unit which is currently
779 * connected to a loopback pty, there's not much to do.
781 CHECK_PPP(-EINVAL);
782 tty->disc_data = ppp;
783 ppp->tty = tty;
785 }else{
786 ppp =ppp_alloc();
787 if(ppp == NULL) {
788 printk(KERN_ERR "ppp_alloc failed\n");
789 return-ENFILE;
792 * Initialize the control block
794 ppp_init_ctrl_blk(ppp);
795 tty->disc_data = ppp;
796 ppp->tty = tty;
798 * Allocate space for the default VJ header compression slots
800 ppp->slcomp =slhc_init(16,16);
801 if(ppp->slcomp == NULL) {
802 printk(KERN_ERR "ppp_tty_open: "
803 "no space for compression buffers!\n");
804 ppp_release(ppp);
805 return-ENOMEM;
808 * Allocate space for the MTU and MRU buffers
810 if(ppp_changedmtu(ppp,ppp2dev(ppp)->mtu, ppp->mru) ==0) {
811 ppp_release(ppp);
812 return-ENOMEM;
815 * Allocate space for a user level buffer
817 ppp->ubuf =ppp_alloc_buf(RBUFSIZE, BUFFER_TYPE_TTY_RD);
818 if(ppp->ubuf == NULL) {
819 printk(KERN_ERR "ppp_tty_open: "
820 "no space for user receive buffer\n");
821 ppp_release(ppp);
822 return-ENOMEM;
825 if(ppp->flags & SC_DEBUG)
826 printk(KERN_INFO "ppp: channel %s open\n",
827 ppp2dev(ppp)->name);
829 for(indx =0; indx < NUM_NP; ++indx)
830 ppp->sc_npmode[indx] = NPMODE_PASS;
832 MOD_INC_USE_COUNT;
835 * Flush any pending characters in the driver and discipline.
837 if(tty->ldisc.flush_buffer)
838 tty->ldisc.flush_buffer(tty);
840 if(tty->driver.flush_buffer)
841 tty->driver.flush_buffer(tty);
842 return(ppp->line);
846 * Local function to send the next portion of the buffer.
848 * Called by the tty driver's tty_wakeup function should it be entered
849 * because the partial buffer was transmitted.
851 * Called by kick_tty to send the initial portion of the buffer.
853 * Completion processing of the buffer transmission is handled here.
856 static void
857 ppp_tty_wakeup_code(struct ppp *ppp,struct tty_struct *tty,
858 struct ppp_buffer *xbuf)
860 registerint count, actual;
861 unsigned long flags;
863 CHECK_PPP_VOID();
864 CHECK_BUF_MAGIC(xbuf);
866 * Prevent re-entrancy by ensuring that this routine is called only once.
868 save_flags(flags);
869 cli();
870 if(ppp->flags & SC_XMIT_BUSY) {
871 restore_flags(flags);
872 return;
874 ppp->flags |= SC_XMIT_BUSY;
875 restore_flags(flags);
877 * Send the next block of data to the modem
879 count = xbuf->count - xbuf->tail;
880 actual = tty->driver.write(tty,0,
881 buf_base(xbuf) + xbuf->tail, count);
883 * Terminate transmission of any block which may have an error.
884 * This could occur should the carrier drop.
886 if(actual <0) {
887 ppp->stats.ppp_oerrors++;
888 actual = count;
889 }else
890 ppp->bytes_sent += actual;
892 * If the buffer has been transmitted then clear the indicators.
894 xbuf->tail += actual;
895 if(actual == count) {
896 xbuf = NULL;
897 ppp->flags &= ~SC_XMIT_BUSY;
899 * Complete the transmission on the current buffer.
901 xbuf = ppp->xbuf;
902 if(xbuf != NULL) {
903 tty->flags &= ~(1<< TTY_DO_WRITE_WAKEUP);
904 xbuf->locked =0;
905 ppp->xbuf = NULL;
907 * If the completed buffer came from the device write, then complete the
908 * transmission block.
910 ppp2dev(ppp)->tbusy =0;
911 mark_bh(NET_BH);
913 * Wake up the transmission queue for all completion events.
915 wake_up_interruptible(&ppp->write_wait);
917 * Look at the priorities. Choose a daemon write over the device driver.
919 save_flags(flags);
920 cli();
921 xbuf = ppp->s1buf;
922 ppp->s1buf = NULL;
923 if(xbuf == NULL) {
924 xbuf = ppp->s2buf;
925 ppp->s2buf = NULL;
928 * If there is a pending buffer then transmit it now.
930 if(xbuf != NULL) {
931 ppp->flags &= ~SC_XMIT_BUSY;
932 ppp_kick_tty(ppp, xbuf);
933 restore_flags(flags);
934 return;
936 restore_flags(flags);
940 * Clear the re-entry flag
942 save_flags(flags);/* &=~ may not be atomic */
943 cli();
944 ppp->flags &= ~SC_XMIT_BUSY;
945 restore_flags(flags);
949 * This function is called by the tty driver when the transmit buffer has
950 * additional space. It is used by the ppp code to continue to transmit
951 * the current buffer should the buffer have been partially sent.
953 * In addition, it is used to send the first part of the buffer since the
954 * logic and the inter-locking would be identical.
957 static void
958 ppp_tty_wakeup(struct tty_struct *tty)
960 struct ppp_buffer *xbuf;
961 struct ppp *ppp =tty2ppp(tty);
963 if(!ppp)
964 return;
965 CHECK_PPP_VOID();
967 if(tty != ppp->tty) {
968 tty->flags &= ~(1<< TTY_DO_WRITE_WAKEUP);
969 return;
972 * Ensure that there is a transmission pending. Clear the re-entry flag if
973 * there is no pending buffer. Otherwise, send the buffer.
975 xbuf = ppp->xbuf;
976 if(xbuf == NULL)
977 tty->flags &= ~(1<< TTY_DO_WRITE_WAKEUP);
978 else
979 ppp_tty_wakeup_code(ppp, tty, xbuf);
983 * This function is called to transmit a buffer to the remote. The buffer
984 * is placed on the pending queue if there is presently a buffer being
985 * sent or it is transmitted with the aid of ppp_tty_wakeup.
988 static void
989 ppp_kick_tty(struct ppp *ppp,struct ppp_buffer *xbuf)
991 unsigned long flags;
993 CHECK_PPP_VOID();
994 CHECK_BUF_MAGIC(xbuf);
996 * Hold interrupts.
998 save_flags(flags);
999 cli();
1001 * Control the flags which are best performed with the interrupts masked.
1003 xbuf->locked =1;
1004 xbuf->tail =0;
1006 * If the transmitter is busy then place the buffer on the appropriate
1007 * priority queue.
1009 if(ppp->xbuf != NULL) {
1010 if(xbuf->type == BUFFER_TYPE_TTY_WR)
1011 ppp->s1buf = xbuf;
1012 else
1013 ppp->s2buf = xbuf;
1014 restore_flags(flags);
1015 return;
1018 * If the transmitter is not busy then this is the highest priority frame
1020 ppp->flags &= ~SC_XMIT_BUSY;
1021 ppp->tty->flags |= (1<< TTY_DO_WRITE_WAKEUP);
1022 ppp->xbuf = xbuf;
1023 restore_flags(flags);
1025 * Do the "tty wakeup_code" to actually send this buffer.
1027 ppp_tty_wakeup_code(ppp,ppp2tty(ppp), xbuf);
1030 /*************************************************************
1031 * TTY INPUT
1032 * The following functions handle input that arrives from
1033 * the TTY. It recognizes PPP frames and either hands them
1034 * to the network layer or queues them for delivery to a
1035 * user process reading this TTY.
1036 *************************************************************/
1039 * Callback function from tty driver. Return the amount of space left
1040 * in the receiver's buffer to decide if remote transmitter is to be
1041 * throttled.
1044 static int
1045 ppp_tty_room(struct tty_struct *tty)
1047 return65536;/* We can handle an infinite amount of data. :-) */
1051 * Callback function when data is available at the tty driver.
1053 static void
1054 ppp_tty_receive(struct tty_struct *tty,const __u8 * data,
1055 char*flags,int count)
1057 registerstruct ppp *ppp =tty2ppp(tty);
1058 registerstruct ppp_buffer *buf = NULL;
1059 __u8 chr;
1061 if(ppp !=0)
1062 CHECK_PPP_VOID();
1064 * This can happen if stuff comes in on the backup tty.
1066 if(ppp ==0|| tty != ppp->tty)
1067 return;
1069 * Fetch the pointer to the buffer. Be careful about race conditions.
1071 buf = ppp->rbuf;
1072 if(buf == NULL)
1073 return;
1075 * Verify the table pointer and ensure that the line is
1076 * still in PPP discipline.
1078 if(ppp->magic != PPP_MAGIC) {
1079 if(ppp->flags & SC_DEBUG)
1080 printk(KERN_DEBUG
1081 "PPP: tty_receive called but couldn't find "
1082 "PPP struct.\n");
1083 return;
1085 CHECK_PPP_VOID();
1087 * Print the buffer if desired
1089 if(ppp->flags & SC_LOG_RAWIN)
1090 ppp_print_buffer("receive buffer", data, count);
1093 * Collect the character and error condition for the character. Set the toss
1094 * flag for the first character error.
1096 while(count-- >0) {
1097 ppp->bytes_rcvd++;
1098 chr = *data++;
1099 if(flags) {
1100 if(*flags && ppp->toss ==0) {
1101 ppp->toss = *flags;
1102 switch(ppp->toss) {
1103 case TTY_OVERRUN:
1104 ++ppp->estats.rx_fifo_errors;
1105 break;
1106 case TTY_FRAME:
1107 case TTY_BREAK:
1108 ++ppp->estats.rx_frame_errors;
1109 break;
1112 ++flags;
1116 * Set the flags for d7 being 0/1 and parity being even/odd so that
1117 * the normal processing would have all flags set at the end of the
1118 * session. A missing flag bit indicates an error condition.
1121 #ifdef CHECK_CHARACTERS
1122 if(chr &0x80)
1123 ppp->flags |= SC_RCV_B7_1;
1124 else
1125 ppp->flags |= SC_RCV_B7_0;
1127 if(paritytab[chr >>5] & (1<< (chr &0x1F)))
1128 ppp->flags |= SC_RCV_ODDP;
1129 else
1130 ppp->flags |= SC_RCV_EVNP;
1131 #endif
1133 * Branch on the character.
1135 switch(chr) {
1137 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1138 * then the block is to be ignored. In addition, characters before the very
1139 * first FLAG are also tossed by this procedure.
1141 case PPP_FLAG:/* PPP_FLAG: end of frame */
1142 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1143 if(ppp->escape)
1144 ppp->toss |=0x80;
1146 * Process frames which are not to be ignored. If the processing failed,
1147 * then clean up the VJ tables.
1149 if(ppp_doframe(ppp) ==0) {
1150 ++ppp->stats.ppp_ierrors;
1151 slhc_toss(ppp->slcomp);
1154 * Reset all indicators for the new frame to follow.
1156 buf->count =0;
1157 buf->fcs = PPP_INITFCS;
1158 ppp->escape =0;
1159 ppp->toss =0;
1160 break;
1162 * All other characters in the data come here. If the character is in the
1163 * receive mask then ignore the character.
1165 default:
1166 /* If we're tossing, look no further. */
1167 if(ppp->toss !=0)
1168 break;
1170 /* If this is a control char to be ignored, do so */
1171 if(in_rmap(ppp, chr))
1172 break;
1175 * Modify the next character if preceded by escape.
1176 * The escape character (0x7d) could be an escaped
1177 * 0x5d, if it follows an escape :-)
1179 if(ppp->escape) {
1180 chr ^= PPP_TRANS;
1181 ppp->escape =0;
1182 }else if(chr == PPP_ESCAPE) {
1183 ppp->escape = PPP_TRANS;
1184 break;
1188 * Decompress A/C and protocol compression here.
1190 if(buf->count ==0&& chr != PPP_ALLSTATIONS) {
1191 buf_base(buf)[0] = PPP_ALLSTATIONS;
1192 buf_base(buf)[1] = PPP_UI;
1193 buf->count =2;
1195 if(buf->count ==2&& (chr &1) !=0) {
1196 buf_base(buf)[2] =0;
1197 buf->count =3;
1200 * If the count sent is within reason then store the character, bump the
1201 * count, and update the FCS for the character.
1203 if(buf->count < buf->size) {
1204 buf_base(buf)[buf->count++] = chr;
1205 buf->fcs =PPP_FCS(buf->fcs, chr);
1206 break;
1209 * The peer sent too much data. Set the flags to discard the current frame
1210 * and wait for the re-synchronization FLAG to be sent.
1212 ++ppp->estats.rx_length_errors;
1213 ppp->toss |=0xC0;
1214 break;
1219 /* on entry, a received frame is in ppp->rbuf.bufr
1220 check it and dispose as appropriate */
1222 static int
1223 ppp_doframe(struct ppp *ppp)
1225 __u8 *data =buf_base(ppp->rbuf);
1226 int count = ppp->rbuf->count;
1227 int proto;
1228 int new_count;
1229 __u8 *new_data;
1231 CHECK_PPP(0);
1232 CHECK_BUF_MAGIC(ppp->rbuf);
1235 * If there is a pending error from the receiver then log it and discard
1236 * the damaged frame.
1238 if(ppp->toss) {
1239 if((ppp->flags & SC_DEBUG) && count >0)
1240 printk(KERN_DEBUG
1241 "ppp_toss: tossing frame, reason = %x\n",
1242 ppp->toss);
1243 return0;
1246 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1247 * follows each frame.
1249 if(count ==0)
1250 return1;
1252 * Generate an error if the frame is too small.
1254 if(count < PPP_HDRLEN +2) {
1255 if(ppp->flags & SC_DEBUG)
1256 printk(KERN_DEBUG
1257 "ppp: got runt ppp frame, %d chars\n", count);
1258 ++ppp->estats.rx_length_errors;
1259 return0;
1262 * Verify the CRC of the frame and discard the CRC characters from the
1263 * end of the buffer.
1265 if(ppp->rbuf->fcs != PPP_GOODFCS) {
1266 if(ppp->flags & SC_DEBUG) {
1267 printk(KERN_DEBUG
1268 "ppp: frame with bad fcs, length = %d\n",
1269 count);
1270 ppp_print_buffer("bad frame", data, count);
1272 ++ppp->estats.rx_crc_errors;
1273 return0;
1275 count -=2;/* ignore the fcs characters */
1277 * Obtain the protocol from the frame
1279 proto =PPP_PROTOCOL(data);
1281 * Process the active decompressor.
1283 if((ppp->sc_rc_state != (void*)0) &&
1284 (ppp->flags & SC_DECOMP_RUN) &&
1285 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) ==0)) {
1286 if(proto == PPP_COMP) {
1288 * If the frame is compressed then decompress it.
1290 new_data =kmalloc(ppp->mru + PPP_HDRLEN, GFP_ATOMIC);
1291 if(new_data == NULL) {
1292 printk(KERN_ERR "ppp_doframe: no memory\n");
1293 new_count = DECOMP_ERROR;
1294 }else{
1295 new_count = (*ppp->sc_rcomp->decompress)
1296 (ppp->sc_rc_state, data, count,
1297 new_data, ppp->mru + PPP_HDRLEN);
1299 switch(new_count) {
1300 default:
1301 ppp_doframe_lower(ppp, new_data, new_count);
1302 kfree(new_data);
1303 return1;
1305 case DECOMP_ERROR:
1306 ppp->flags |= SC_DC_ERROR;
1307 break;
1309 case DECOMP_FATALERROR:
1310 ppp->flags |= SC_DC_FERROR;
1311 printk(KERN_ERR "ppp: fatal decomp error\n");
1312 break;
1315 * Log the error condition and discard the frame.
1317 if(new_data !=0)
1318 kfree(new_data);
1319 slhc_toss(ppp->slcomp);
1320 ++ppp->stats.ppp_ierrors;
1321 }else{
1323 * The frame is not special. Pass it through the compressor without
1324 * actually compressing the data
1326 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1327 data, count);
1331 * Process the uncompressed frame.
1333 ppp_doframe_lower(ppp, data, count);
1334 return1;
1337 static voidppp_doframe_lower(struct ppp *ppp, __u8 *data,int count)
1339 __u16 proto =PPP_PROTOCOL(data);
1340 ppp_proto_type *proto_ptr;
1342 CHECK_PPP_VOID();
1344 * Ignore empty frames
1346 if(count <= PPP_HDRLEN)
1347 return;
1349 * Count the frame and print it
1351 ++ppp->stats.ppp_ipackets;
1352 if(ppp->flags & SC_LOG_INPKT)
1353 ppp_print_buffer("receive frame", data, count);
1355 * Find the procedure to handle this protocol. The last one is marked
1356 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1358 proto_ptr = proto_list;
1359 while(proto_ptr->proto !=0&& proto_ptr->proto != proto)
1360 ++proto_ptr;
1362 * Update the appropriate statistic counter.
1364 if((*proto_ptr->func) (ppp, proto,
1365 &data[PPP_HDRLEN],
1366 count - PPP_HDRLEN))
1367 ppp->stats.ppp_ioctects += count;
1368 else
1369 ++ppp->stats.ppp_discards;
1373 * Put the input frame into the networking system for the indicated protocol
1376 static int
1377 ppp_rcv_rx(struct ppp *ppp, __u16 proto, __u8 * data,int count)
1379 sk_buff *skb =dev_alloc_skb(count);
1381 * Generate a skb buffer for the new frame.
1383 if(skb == NULL) {
1384 if(ppp->flags & SC_DEBUG)
1385 printk(KERN_ERR
1386 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1387 ppp2dev(ppp)->name);
1388 return0;
1391 * Move the received data from the input buffer to the skb buffer.
1393 skb->dev =ppp2dev(ppp);/* We are the device */
1394 skb->protocol = proto;
1395 skb->mac.raw =skb_data(skb);
1396 memcpy(skb_put(skb,count), data, count);/* move data */
1398 * Tag the frame and kick it to the proper receive routine
1400 ppp->last_recv = jiffies;
1401 netif_rx(skb);
1402 return1;
1406 * Process the receipt of an IP frame
1409 static int
1410 rcv_proto_ip(struct ppp *ppp, __u16 proto, __u8 * data,int count)
1412 CHECK_PPP(0);
1413 if((ppp2dev(ppp)->flags & IFF_UP) && (count >0))
1414 if(ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1415 returnppp_rcv_rx(ppp,htons(ETH_P_IP), data, count);
1416 return0;
1420 * Process the receipt of an IPX frame
1423 static int
1424 rcv_proto_ipx(struct ppp *ppp, __u16 proto, __u8 * data,int count)
1426 CHECK_PPP(0);
1427 if(((ppp2dev(ppp)->flags & IFF_UP) !=0) && (count >0))
1428 returnppp_rcv_rx(ppp,htons(ETH_P_IPX), data, count);
1429 return0;
1433 * Process the receipt of an VJ Compressed frame
1436 static int
1437 rcv_proto_vjc_comp(struct ppp *ppp, __u16 proto,
1438 __u8 *data,int count)
1440 CHECK_PPP(0);
1441 if((ppp->flags & SC_REJ_COMP_TCP) ==0) {
1442 int new_count =slhc_uncompress(ppp->slcomp, data, count);
1443 if(new_count >=0) {
1444 returnrcv_proto_ip(ppp, PPP_IP, data, new_count);
1446 if(ppp->flags & SC_DEBUG)
1447 printk(KERN_NOTICE
1448 "ppp: error in VJ decompression\n");
1450 return0;
1454 * Process the receipt of an VJ Un-compressed frame
1457 static int
1458 rcv_proto_vjc_uncomp(struct ppp *ppp, __u16 proto,
1459 __u8 *data,int count)
1461 CHECK_PPP(0);
1462 if((ppp->flags & SC_REJ_COMP_TCP) ==0) {
1463 if(slhc_remember(ppp->slcomp, data, count) >0) {
1464 returnrcv_proto_ip(ppp, PPP_IP, data, count);
1466 if(ppp->flags & SC_DEBUG)
1467 printk(KERN_NOTICE
1468 "ppp: error in VJ memorizing\n");
1470 return0;
1474 * Receive all unclassified protocols.
1477 static int
1478 rcv_proto_unknown(struct ppp *ppp, __u16 proto,
1479 __u8 *data,int len)
1481 int totlen;
1482 registerint current_idx;
1484 #define PUTC(c) \
1486 buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1487 current_idx &= ppp->ubuf->size; \
1488 if (current_idx == ppp->ubuf->tail) \
1489 goto failure; \
1492 CHECK_PPP(0);
1494 * The total length includes the protocol data.
1495 * Lock the user information buffer.
1497 if(test_and_set_bit(0, &ppp->ubuf->locked)) {
1498 if(ppp->flags & SC_DEBUG)
1499 printk(KERN_DEBUG
1500 "ppp: rcv_proto_unknown: can't get lock\n");
1501 }else{
1502 CHECK_BUF_MAGIC(ppp->ubuf);
1503 current_idx = ppp->ubuf->head;
1505 * Insert the buffer length (not counted), the protocol, and the data
1507 totlen = len +2;
1508 PUTC(totlen >>8);
1509 PUTC(totlen);
1511 PUTC(proto >>8);
1512 PUTC(proto);
1514 totlen -=2;
1515 while(totlen-- >0) {
1516 PUTC(*data++);
1518 #undef PUTC
1520 * The frame is complete. Update the head pointer and wakeup the pppd
1521 * process.
1523 ppp->ubuf->head = current_idx;
1525 clear_bit(0, &ppp->ubuf->locked);
1526 wake_up_interruptible(&ppp->read_wait);
1527 if(ppp->tty->fasync != NULL)
1528 kill_fasync(ppp->tty->fasync, SIGIO);
1530 return1;
1532 * The buffer is full. Unlock the header
1534 failure:
1535 clear_bit(0, &ppp->ubuf->locked);
1536 if(ppp->flags & SC_DEBUG)
1537 printk(KERN_DEBUG
1538 "ppp: rcv_proto_unknown: buffer overflow\n");
1541 * Discard the frame. There are no takers for this protocol.
1543 if(ppp->flags & SC_DEBUG)
1544 printk(KERN_DEBUG
1545 "ppp: rcv_proto_unknown: dropping packet\n");
1546 return0;
1550 * Handle a CCP packet.
1552 * The CCP packet is passed along to the pppd process just like any
1553 * other PPP frame. The difference is that some processing needs to be
1554 * immediate or the compressors will become confused on the peer.
1557 static voidppp_proto_ccp(struct ppp *ppp, __u8 *dp,int len,int rcvd)
1559 int slen =CCP_LENGTH(dp);
1560 __u8 *opt = dp + CCP_HDRLEN;
1561 int opt_len = slen - CCP_HDRLEN;
1562 unsigned long flags;
1564 if(slen > len)
1565 return;
1567 save_flags(flags);
1568 switch(CCP_CODE(dp)) {
1569 case CCP_CONFREQ:
1570 case CCP_TERMREQ:
1571 case CCP_TERMACK:
1573 * CCP must be going down - disable compression
1575 if(ppp->flags & SC_CCP_UP) {
1576 cli();
1577 ppp->flags &= ~(SC_CCP_UP |
1578 SC_COMP_RUN |
1579 SC_DECOMP_RUN);
1581 break;
1583 case CCP_CONFACK:
1584 if((ppp->flags & SC_CCP_OPEN) ==0)
1585 break;
1586 if(ppp->flags & SC_CCP_UP)
1587 break;
1588 if(slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1589 break;
1590 if(slen < (CCP_OPT_LENGTH(opt) + CCP_HDRLEN))
1591 break;
1593 * we're agreeing to send compressed packets.
1595 if(!rcvd) {
1596 if(ppp->sc_xc_state == NULL)
1597 break;
1599 if((*ppp->sc_xcomp->comp_init)
1600 (ppp->sc_xc_state,
1601 opt,
1602 opt_len,
1603 ppp2dev(ppp)->base_addr,
1605 ppp->flags & SC_DEBUG)) {
1606 if(ppp->flags & SC_DEBUG)
1607 printk(KERN_DEBUG "%s: comp running\n",
1608 ppp->name);
1609 cli();
1610 ppp->flags |= SC_COMP_RUN;
1612 break;
1615 * peer is agreeing to send compressed packets.
1617 if(ppp->sc_rc_state == NULL)
1618 break;
1620 if((*ppp->sc_rcomp->decomp_init)
1621 (ppp->sc_rc_state,
1622 opt,
1623 opt_len,
1624 ppp2dev(ppp)->base_addr,
1626 ppp->mru,
1627 ppp->flags & SC_DEBUG)) {
1628 if(ppp->flags & SC_DEBUG)
1629 printk(KERN_DEBUG "%s: decomp running\n",
1630 ppp->name);
1631 cli();
1632 ppp->flags |= SC_DECOMP_RUN;
1633 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1635 break;
1637 * CCP Reset-ack resets compressors and decompressors as it passes through.
1639 case CCP_RESETACK:
1640 if((ppp->flags & SC_CCP_UP) ==0)
1641 break;
1643 if(!rcvd) {
1644 if(ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN)) {
1645 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1646 if(ppp->flags & SC_DEBUG)
1647 printk(KERN_DEBUG "%s: comp reset\n",
1648 ppp->name);
1650 }else{
1651 if(ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1652 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1653 if(ppp->flags & SC_DEBUG)
1654 printk(KERN_DEBUG "%s: decomp reset\n",
1655 ppp->name);
1656 cli();
1657 ppp->flags &= ~SC_DC_ERROR;
1660 break;
1662 restore_flags(flags);
1665 static int
1666 rcv_proto_ccp(struct ppp *ppp, __u16 proto, __u8 *dp,int len)
1668 CHECK_PPP(0);
1669 ppp_proto_ccp(ppp, dp, len,1);
1670 returnrcv_proto_unknown(ppp, proto, dp, len);
1674 * Handle a LQR packet.
1677 static int
1678 rcv_proto_lqr(struct ppp *ppp, __u16 proto, __u8 * data,int len)
1680 returnrcv_proto_unknown(ppp, proto, data, len);
1683 /*************************************************************
1684 * LINE DISCIPLINE SUPPORT
1685 * The following functions form support user programs
1686 * which read and write data on a TTY with the PPP line
1687 * discipline. Reading is done from a circular queue,
1688 * filled by the lower TTY levels.
1689 *************************************************************/
1691 /* read a PPP frame from the us_rbuff circular buffer,
1692 waiting if necessary
1695 static ssize_t
1696 ppp_tty_read(struct tty_struct *tty,struct file *file, __u8 * buf,size_t nr)
1698 struct ppp *ppp =tty2ppp(tty);
1699 __u8 c;
1700 int error;
1701 ssize_t len, ret;
1703 #define GETC(c) \
1705 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1706 ppp->ubuf->tail &= ppp->ubuf->size; \
1710 * Validate the pointers
1712 if(!ppp)
1713 return-EIO;
1715 /* if (ppp->magic != PPP_MAGIC)
1716 return -EIO; */
1718 CHECK_PPP(-ENXIO);
1721 * Before we attempt to write the frame to the user, ensure that the
1722 * user has access to the pages for the total buffer length.
1724 error =verify_area(VERIFY_WRITE, buf, nr);
1725 if(error !=0)
1726 return(error);
1729 * Acquire the read lock.
1731 for(;;) {
1732 ppp =tty2ppp(tty);
1733 if(!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
1734 || tty != ppp->tty)
1735 return0;
1737 if(test_and_set_bit(0, &ppp->ubuf->locked) !=0) {
1738 #if 0
1739 if(ppp->flags & SC_DEBUG)
1740 printk(KERN_DEBUG
1741 "ppp_tty_read: sleeping(ubuf)\n");
1742 #endif
1743 current->timeout =0;
1744 current->state = TASK_INTERRUPTIBLE;
1745 schedule();
1747 if(signal_pending(current))
1748 return-EINTR;
1749 continue;
1753 * Fetch the length of the buffer from the first two bytes.
1755 if(ppp->ubuf->head == ppp->ubuf->tail)
1756 len =0;
1757 else{
1758 GETC(c);
1759 len = c <<8;
1760 GETC(c);
1761 len += c;
1762 if(len)
1763 break;
1767 * If there is no length then wait for the data to arrive.
1769 /* no data */
1770 clear_bit(0, &ppp->ubuf->locked);
1771 if(file->f_flags & O_NONBLOCK)
1772 return-EAGAIN;
1773 current->timeout =0;
1774 #if 0
1775 if(ppp->flags & SC_DEBUG)
1776 printk(KERN_DEBUG
1777 "ppp_tty_read: sleeping(read_wait)\n");
1778 #endif
1779 interruptible_sleep_on(&ppp->read_wait);
1780 if(signal_pending(current))
1781 return-EINTR;
1785 * Ensure that the frame will fit within the caller's buffer. If not, then
1786 * discard the frame from the input buffer.
1788 if(len +2> nr) {
1789 /* Can't copy it, update us_rbuff_head */
1791 if(ppp->flags & SC_DEBUG)
1792 printk(KERN_DEBUG
1793 "ppp: read of %lu bytes too small for %ld "
1794 "frame\n", (unsigned long) nr, (long) len +2);
1795 ppp->stats.ppp_ierrors++;
1796 error = -EOVERFLOW;
1797 goto out;
1801 * Fake the insertion of the ADDRESS and CONTROL information because these
1802 * were not saved in the buffer.
1804 error =put_user((u_char) PPP_ALLSTATIONS, buf);
1805 if(error)
1806 goto out;
1807 ++buf;
1808 error =put_user((u_char) PPP_UI, buf);
1809 if(error)
1810 goto out;
1811 ++buf;
1814 * Copy the received data from the buffer to the caller's area.
1816 ret = len +2;/* Account for ADDRESS and CONTROL bytes */
1817 while(len-- >0) {
1818 GETC(c);
1819 error =put_user(c, buf);
1820 if(error)
1821 goto out;
1822 ++buf;
1825 clear_bit(0, &ppp->ubuf->locked);
1826 return ret;
1828 out:
1829 ppp->ubuf->tail += len;
1830 ppp->ubuf->tail &= ppp->ubuf->size;
1831 clear_bit(0, &ppp->ubuf->locked);
1832 return error;
1833 #undef GETC
1836 /* stuff a character into the transmit buffer, using PPP's way of escaping
1837 special characters.
1838 also, update fcs to take account of new character */
1840 extern inlinevoid
1841 ppp_stuff_char(struct ppp *ppp,registerstruct ppp_buffer *buf,
1842 register __u8 chr)
1845 * The buffer should not be full.
1847 if(ppp->flags & SC_DEBUG) {
1848 if((buf->count <0) || (buf->count >3000))
1849 printk(KERN_DEBUG "ppp_stuff_char: %d %x\n",
1850 (unsigned int) buf->count,
1851 (unsigned int) chr);
1854 * Update the FCS and if the character needs to be escaped, do it.
1856 buf->fcs =PPP_FCS(buf->fcs, chr);
1857 if(in_xmap(ppp, chr)) {
1858 chr ^= PPP_TRANS;
1859 ins_char(buf, PPP_ESCAPE);
1862 * Add the character to the buffer.
1864 ins_char(buf, chr);
1868 * Procedure to encode the data with the proper escaping and send the
1869 * data to the remote system.
1872 static void
1873 ppp_dev_xmit_lower(struct ppp *ppp,struct ppp_buffer *buf,
1874 __u8 *data,int count,int non_ip)
1876 __u16 write_fcs;
1877 int address, control;
1878 int proto;
1880 CHECK_PPP_VOID();
1881 CHECK_BUF_MAGIC(buf);
1882 ++ppp->stats.ppp_opackets;
1883 ppp->stats.ppp_ooctects += count;
1886 * Insert the leading FLAG character
1888 buf->count =0;
1890 if(non_ip || flag_time ==0)
1891 ins_char(buf, PPP_FLAG);
1892 else{
1893 if(jiffies - ppp->last_xmit >= flag_time)
1894 ins_char(buf, PPP_FLAG);
1896 ppp->last_xmit = jiffies;
1897 buf->fcs = PPP_INITFCS;
1899 * Emit the address/control information if needed
1901 address =PPP_ADDRESS(data);
1902 control =PPP_CONTROL(data);
1903 proto =PPP_PROTOCOL(data);
1905 if(address != PPP_ALLSTATIONS ||
1906 control != PPP_UI ||
1907 (ppp->flags & SC_COMP_AC) ==0) {
1908 ppp_stuff_char(ppp, buf, address);
1909 ppp_stuff_char(ppp, buf, control);
1912 * Emit the protocol (compressed if possible)
1914 if((ppp->flags & SC_COMP_PROT) ==0|| (proto &0xFF00))
1915 ppp_stuff_char(ppp, buf, proto >>8);
1917 ppp_stuff_char(ppp, buf, proto);
1919 * Insert the data
1921 data +=4;
1922 count -=4;
1924 while(count-- >0)
1925 ppp_stuff_char(ppp, buf, *data++);
1927 * Add the trailing CRC and the final flag character
1929 write_fcs = buf->fcs ^0xFFFF;
1930 ppp_stuff_char(ppp, buf, write_fcs);
1931 ppp_stuff_char(ppp, buf, write_fcs >>8);
1933 * Add the trailing flag character
1935 ins_char(buf, PPP_FLAG);
1937 * Send the block to the tty driver.
1939 ppp->stats.ppp_obytes += buf->count;
1940 ppp_kick_tty(ppp, buf);
1944 * Compress and send an frame to the peer.
1946 * Return 0 if frame was queued for transmission.
1947 * 1 if frame must be re-queued for later driver support.
1950 static int
1951 ppp_dev_xmit_frame(struct ppp *ppp,struct ppp_buffer *buf,
1952 __u8 *data,int count)
1954 int proto;
1955 int address, control;
1956 __u8 *new_data;
1957 int new_count;
1959 CHECK_PPP(0);
1960 CHECK_BUF_MAGIC(buf);
1962 * Print the buffer
1964 if(ppp->flags & SC_LOG_OUTPKT)
1965 ppp_print_buffer("write frame", data, count);
1967 * Determine if the frame may be compressed. Attempt to compress the
1968 * frame if possible.
1970 proto =PPP_PROTOCOL(data);
1971 address =PPP_ADDRESS(data);
1972 control =PPP_CONTROL(data);
1974 if(((ppp->flags & SC_COMP_RUN) !=0) &&
1975 (ppp->sc_xc_state != (void*)0) &&
1976 (address == PPP_ALLSTATIONS) &&
1977 (control == PPP_UI) &&
1978 (proto != PPP_LCP) &&
1979 (proto != PPP_CCP)) {
1980 new_data =kmalloc(ppp->mtu, GFP_ATOMIC);
1981 if(new_data == NULL) {
1982 if(ppp->flags & SC_DEBUG)
1983 printk(KERN_ERR
1984 "ppp_dev_xmit_frame: no memory\n");
1985 return1;
1988 new_count = (*ppp->sc_xcomp->compress)
1989 (ppp->sc_xc_state, data, new_data, count, ppp->mtu);
1991 if(new_count >0&& (ppp->flags & SC_CCP_UP)) {
1992 ppp_dev_xmit_lower(ppp, buf, new_data, new_count,0);
1993 kfree(new_data);
1994 return0;
1997 * The frame could not be compressed, or it could not be sent in
1998 * compressed form because CCP is not yet up.
2000 kfree(new_data);
2003 * Go to the escape encoding
2005 ppp_dev_xmit_lower(ppp, buf, data, count, !!(proto &0xFF00));
2006 return0;
2010 * Revise the tty frame for specific protocols.
2013 static int
2014 send_revise_frame(registerstruct ppp *ppp, __u8 *data,int len)
2016 __u8 *p;
2018 switch(PPP_PROTOCOL(data)) {
2020 * Update the LQR frame with the current MIB information. This saves having
2021 * the daemon read old MIB data from the driver.
2023 case PPP_LQR:
2024 len =48;/* total size of this frame */
2025 p = (__u8 *) &data [40];/* Point to last two items. */
2026 p =store_long(p, ppp->stats.ppp_opackets +1);
2027 p =store_long(p, ppp->stats.ppp_ooctects + len);
2028 break;
2030 * Outbound compression frames
2032 case PPP_CCP:
2033 ppp_proto_ccp(ppp,
2034 data + PPP_HDRLEN,
2035 len - PPP_HDRLEN,
2037 break;
2039 default:
2040 break;
2043 return len;
2047 * write a frame with NR chars from BUF to TTY
2048 * we have to put the FCS field on ourselves
2051 static ssize_t
2052 ppp_tty_write(struct tty_struct *tty,struct file *file,const __u8 * data,
2053 size_t count)
2055 struct ppp *ppp =tty2ppp(tty);
2056 __u8 *new_data;
2057 int error;
2058 struct wait_queue wait = {current, NULL};
2061 * Verify the pointers.
2063 error = -EIO;
2064 if(!ppp)
2065 goto out;
2066 if(ppp->magic != PPP_MAGIC)
2067 goto out;
2069 CHECK_PPP(-ENXIO);
2071 * Ensure that the caller does not wish to send too much.
2073 if(count > PPP_MTU + PPP_HDRLEN) {
2074 if(ppp->flags & SC_DEBUG)
2075 printk(KERN_WARNING
2076 "ppp_tty_write: truncating user packet "
2077 "from %lu to mtu %d\n", (unsigned long) count,
2078 PPP_MTU + PPP_HDRLEN);
2079 count = PPP_MTU + PPP_HDRLEN;
2082 * Allocate a buffer for the data and fetch it from the user space.
2084 new_data =kmalloc(count, GFP_KERNEL);
2085 if(new_data == NULL) {
2086 printk(KERN_ERR "ppp_tty_write: no memory\n");
2087 return0;
2090 * Retrieve the user's buffer
2092 error = -EFAULT;
2093 if(copy_from_user(new_data, data, count))
2094 goto out_free;
2096 * Lock this PPP unit so we will be the only writer,
2097 * sleeping if necessary.
2099 * Note that we add our task to the wait queue before
2100 * attempting to lock, as the lock flag may be cleared
2101 * from an interrupt.
2103 add_wait_queue(&ppp->write_wait, &wait);
2104 while(1) {
2105 error =0;
2106 current->timeout =0;
2107 current->state = TASK_INTERRUPTIBLE;
2108 if(lock_buffer(ppp->tbuf) ==0)
2109 break;
2110 schedule();
2112 error = -EINVAL;
2113 ppp =tty2ppp(tty);
2114 if(!ppp || ppp->magic != PPP_MAGIC ||
2115 !ppp->inuse || tty != ppp->tty) {
2116 printk("ppp_tty_write: %p invalid after wait!\n", ppp);
2117 break;
2119 error = -EINTR;
2120 if(signal_pending(current))
2121 break;
2123 current->state = TASK_RUNNING;
2124 remove_wait_queue(&ppp->write_wait, &wait);
2125 if(error)
2126 goto out_free;
2129 * Change the LQR frame
2131 count =send_revise_frame(ppp, new_data, count);
2133 * Send the data
2135 if(PPP_PROTOCOL(new_data) == PPP_IP) {
2137 * IP frames can be sent by pppd when we're doing
2138 * demand-dialling. We send them via ppp_dev_xmit_ip
2139 * to make sure that VJ compression happens properly.
2141 ppp_dev_xmit_ip(ppp, ppp->tbuf, new_data + PPP_HDRLEN,
2142 count - PPP_HDRLEN, NPMODE_PASS);
2144 }else{
2145 ppp_dev_xmit_frame(ppp, ppp->tbuf, new_data, count);
2147 error = count;
2149 out_free:
2150 kfree(new_data);
2151 out:
2152 return error;
2156 * Process the BSD compression IOCTL event for the tty device.
2159 static int
2160 ppp_set_compression(struct ppp *ppp,struct ppp_option_data *odp)
2162 struct compressor *cp;
2163 int error, nb;
2164 unsigned long flags;
2165 __u8 *ptr;
2166 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2167 struct ppp_option_data data;
2170 * Fetch the compression parameters
2172 error = -EFAULT;
2173 if(copy_from_user(&data, odp,sizeof(data)))
2174 goto out;
2176 nb = data.length;
2177 ptr = data.ptr;
2178 if((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2179 nb = CCP_MAX_OPTION_LENGTH;
2181 if(copy_from_user(ccp_option, ptr, nb))
2182 goto out;
2184 error = -EINVAL;
2185 if(ccp_option[1] <2)/* preliminary check on the length byte */
2186 goto out;
2188 save_flags(flags);
2189 cli();
2190 ppp->flags &= ~(SC_COMP_RUN | SC_DECOMP_RUN);
2191 restore_flags(flags);
2193 cp =find_compressor(ccp_option[0]);
2194 #ifdef CONFIG_KMOD
2195 if(cp == NULL) {
2196 char modname[32];
2197 sprintf(modname,"ppp-compress-%d", ccp_option[0]);
2198 request_module(modname);
2199 cp =find_compressor(ccp_option[0]);
2201 #endif/* CONFIG_KMOD */
2203 if(cp == NULL)
2204 goto out_no_comp;
2206 * Found a handler for the protocol - try to allocate
2207 * a compressor or decompressor.
2209 error =0;
2210 if(data.transmit) {
2211 if(ppp->sc_xc_state != NULL)
2212 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2213 ppp->sc_xc_state = NULL;
2215 ppp->sc_xcomp = cp;
2216 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2217 if(ppp->sc_xc_state == NULL) {
2218 printk(KERN_WARNING "%s: comp_alloc failed\n",
2219 ppp->name);
2220 error = -ENOBUFS;
2221 }else if(ppp->flags & SC_DEBUG)
2222 printk(KERN_DEBUG "%s: comp_alloc -> %p\n",
2223 ppp->name, ppp->sc_xc_state);
2224 }else{
2225 if(ppp->sc_rc_state != NULL)
2226 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2227 ppp->sc_rc_state = NULL;
2229 ppp->sc_rcomp = cp;
2230 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2231 if(ppp->sc_rc_state == NULL) {
2232 printk(KERN_WARNING "%s: decomp_alloc failed\n",
2233 ppp->name);
2234 error = -ENOBUFS;
2235 }else if(ppp->flags & SC_DEBUG)
2236 printk(KERN_DEBUG "%s: decomp_alloc -> %p\n",
2237 ppp->name, ppp->sc_rc_state);
2239 out:
2240 return error;
2242 out_no_comp:
2243 error = -EINVAL;/* no handler found */
2244 if(ppp->flags & SC_DEBUG)
2245 printk(KERN_DEBUG "%s: no compressor for [%x %x %x], %x\n",
2246 ppp->name, ccp_option[0], ccp_option[1],
2247 ccp_option[2], nb);
2248 goto out;
2252 * Process the IOCTL event for the tty device.
2255 static int
2256 ppp_tty_ioctl(struct tty_struct *tty,struct file * file,
2257 unsigned int param2,unsigned long param3)
2259 struct ppp *ppp =tty2ppp(tty);
2260 registerint temp_i =0;
2261 int error =0;
2263 * Verify the status of the PPP device.
2265 if(!ppp)
2266 return-EBADF;
2268 if(ppp->magic != PPP_MAGIC)
2269 return-EBADF;
2271 CHECK_PPP(-ENXIO);
2273 * The user must have an euid of root to do these requests.
2275 if(!suser())
2276 return-EPERM;
2278 * Set the MRU value
2280 switch(param2) {
2281 case PPPIOCSMRU:
2282 error =get_user(temp_i, (int*) param3);
2283 if(error !=0)
2284 break;
2285 if(ppp->flags & SC_DEBUG)
2286 printk(KERN_INFO
2287 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2289 if(ppp->mru != temp_i)
2290 ppp_changedmtu(ppp,ppp2dev(ppp)->mtu, temp_i);
2291 break;
2293 * Fetch the flags
2295 case PPPIOCGFLAGS:
2296 temp_i = (ppp->flags & SC_MASK);
2297 #ifndef CHECK_CHARACTERS/* Don't generate errors if we don't check chars. */
2298 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2299 SC_RCV_ODDP | SC_RCV_EVNP;
2300 #endif
2301 error =put_user(temp_i, (int*) param3);
2302 break;
2304 * Set the flags for the various options
2306 case PPPIOCSFLAGS:
2307 error =get_user(temp_i, (int*) param3);
2308 if(error !=0)
2309 break;
2310 temp_i &= SC_MASK;
2311 temp_i |= (ppp->flags & ~SC_MASK);
2313 if((ppp->flags & SC_CCP_OPEN) &&
2314 (temp_i & SC_CCP_OPEN) ==0)
2315 ppp_ccp_closed(ppp);
2317 if((ppp->flags | temp_i) & SC_DEBUG)
2318 printk(KERN_INFO
2319 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2320 ppp->flags = temp_i;
2321 break;
2323 * Set the compression mode
2325 case PPPIOCSCOMPRESS:
2326 error =ppp_set_compression(ppp,
2327 (struct ppp_option_data *) param3);
2328 break;
2330 * Retrieve the transmit async map
2332 case PPPIOCGASYNCMAP:
2333 error =put_user(ppp->xmit_async_map[0], (int*) param3);
2334 break;
2336 * Set the transmit async map
2338 case PPPIOCSASYNCMAP:
2339 error =get_user(temp_i, (int*) param3);
2340 if(error !=0)
2341 break;
2342 ppp->xmit_async_map[0] = temp_i;
2343 if(ppp->flags & SC_DEBUG)
2344 printk(KERN_INFO
2345 "ppp_tty_ioctl: set xmit asyncmap %x\n",
2346 ppp->xmit_async_map[0]);
2347 break;
2349 * Set the receive async map
2351 case PPPIOCSRASYNCMAP:
2352 error =get_user(temp_i, (int*) param3);
2353 if(error !=0)
2354 break;
2355 ppp->recv_async_map = temp_i;
2356 if(ppp->flags & SC_DEBUG)
2357 printk(KERN_INFO
2358 "ppp_tty_ioctl: set rcv asyncmap %x\n",
2359 ppp->recv_async_map);
2360 break;
2362 * Obtain the unit number for this device.
2364 case PPPIOCGUNIT:
2365 error =put_user(ppp2dev(ppp)->base_addr, (int*) param3);
2366 if(error !=0)
2367 break;
2368 if(ppp->flags & SC_DEBUG)
2369 printk(KERN_INFO
2370 "ppp_tty_ioctl: get unit: %ld\n",
2371 ppp2dev(ppp)->base_addr);
2372 break;
2374 * Set the debug level
2376 case PPPIOCSDEBUG:
2377 error =get_user(temp_i, (int*) param3);
2378 if(error !=0)
2379 break;
2380 temp_i = (temp_i &0x1F) <<16;
2381 temp_i |= (ppp->flags & ~0x1F0000);
2383 if((ppp->flags | temp_i) & SC_DEBUG)
2384 printk(KERN_INFO
2385 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2386 ppp->flags = temp_i;
2387 break;
2389 * Get the debug level
2391 case PPPIOCGDEBUG:
2392 temp_i = (ppp->flags >>16) &0x1F;
2393 error =put_user(temp_i, (int*) param3);
2394 break;
2396 * Get the times since the last send/receive frame operation
2398 case PPPIOCGIDLE:
2400 struct ppp_idle cur_ddinfo;
2402 /* change absolute times to relative times. */
2403 cur_ddinfo.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
2404 cur_ddinfo.recv_idle = (jiffies - ppp->last_recv) / HZ;
2405 error = -EFAULT;
2406 if(!copy_to_user((void*) param3, &cur_ddinfo,
2407 sizeof(cur_ddinfo)))
2408 error =0;
2410 break;
2412 * Retrieve the extended async map
2414 case PPPIOCGXASYNCMAP:
2415 error = -EFAULT;
2416 if(!copy_to_user((void*) param3, ppp->xmit_async_map,
2417 sizeof(ppp->xmit_async_map)))
2418 error =0;
2419 break;
2421 * Set the async extended map
2423 case PPPIOCSXASYNCMAP:
2425 __u32 temp_tbl[8];
2427 error = -EFAULT;
2428 if(copy_from_user(temp_tbl, (void*) param3,
2429 sizeof(temp_tbl)))
2430 break;
2432 temp_tbl[1] =0x00000000;
2433 temp_tbl[2] &= ~0x40000000;
2434 temp_tbl[3] |=0x60000000;
2436 error =0;
2437 if((temp_tbl[2] & temp_tbl[3]) !=0||
2438 (temp_tbl[4] & temp_tbl[5]) !=0||
2439 (temp_tbl[6] & temp_tbl[7]) !=0)
2440 error = -EINVAL;
2441 else{
2442 memcpy(ppp->xmit_async_map,
2443 temp_tbl,
2444 sizeof(ppp->xmit_async_map));
2446 if(ppp->flags & SC_DEBUG)
2447 printk(KERN_INFO
2448 "ppp_tty_ioctl: set xasyncmap\n");
2451 break;
2453 * Set the maximum VJ header compression slot number.
2455 case PPPIOCSMAXCID:
2456 error =get_user(temp_i, (int*) param3);
2457 if(error !=0)
2458 break;
2459 temp_i = (temp_i &255) +1;
2460 if(ppp->flags & SC_DEBUG)
2461 printk(KERN_INFO
2462 "ppp_tty_ioctl: set maxcid to %d\n", temp_i);
2463 if(ppp->slcomp != NULL)
2464 slhc_free(ppp->slcomp);
2465 ppp->slcomp = NULL;
2467 ppp->slcomp =slhc_init(16, temp_i);
2468 if(ppp->slcomp == NULL) {
2469 printk(KERN_ERR "ppp_tty_ioctl: "
2470 "no space for compression buffers!\n");
2471 ppp_release(ppp);
2472 error = -ENOMEM;
2474 break;
2476 case PPPIOCXFERUNIT:
2477 ppp->backup_tty = tty;
2478 ppp->sc_xfer = current->pid;
2479 break;
2481 case PPPIOCGNPMODE:
2482 case PPPIOCSNPMODE:
2484 struct npioctl npi;
2486 error = -EFAULT;
2487 if(copy_from_user(&npi, (void*) param3,sizeof(npi)))
2488 break;
2490 if(npi.protocol != PPP_IP) {
2491 if(ppp->flags & SC_DEBUG)
2492 printk(KERN_DEBUG "pppioc[gs]npmode: "
2493 "invalid protocol %d\n",
2494 npi.protocol);
2495 error = -EINVAL;
2496 break;
2498 npi.protocol = NP_IP;
2500 if(param2 == PPPIOCGNPMODE) {
2501 npi.mode = ppp->sc_npmode[npi.protocol];
2502 if(copy_to_user((void*) param3, &npi,
2503 sizeof(npi)))
2504 break;
2507 ppp->sc_npmode[npi.protocol] = npi.mode;
2508 if(ppp->flags & SC_DEBUG)
2509 printk(KERN_DEBUG "ppp: set np %d to %d\n",
2510 npi.protocol, npi.mode);
2511 /* N.B. Why is the busy flag cleared here? */
2512 ppp2dev(ppp)->tbusy =0;
2513 mark_bh(NET_BH);
2514 error =0;
2516 break;
2518 * Allow users to read, but not set, the serial port parameters
2520 case TCGETS:
2521 case TCGETA:
2522 error =n_tty_ioctl(tty, file, param2, param3);
2523 break;
2525 case FIONREAD:
2527 int count = ppp->ubuf->tail - ppp->ubuf->head;
2528 if(count <0)
2529 count += (ppp->ubuf->size +1);
2530 error =put_user(count, (int*) param3);
2532 break;
2534 * All other ioctl() events will come here.
2536 default:
2537 if(ppp->flags & SC_DEBUG)
2538 printk(KERN_WARNING "ppp_tty_ioctl: "
2539 "invalid ioctl=%x, addr=%lx\n", param2, param3);
2540 error = -ENOIOCTLCMD;
2541 break;
2543 return error;
2547 * TTY callback.
2549 * Process the poll() statement for the PPP device.
2552 static unsigned int
2553 ppp_tty_poll(struct tty_struct *tty,struct file *filp, poll_table * wait)
2555 struct ppp *ppp =tty2ppp(tty);
2556 unsigned int mask =0;
2558 if(ppp && ppp->magic == PPP_MAGIC && tty == ppp->tty) {
2559 CHECK_PPP(0);
2561 poll_wait(filp, &ppp->read_wait, wait);
2562 poll_wait(filp, &ppp->write_wait, wait);
2564 /* Must lock the user buffer area while checking. */
2565 CHECK_BUF_MAGIC(ppp->ubuf);
2566 if(test_and_set_bit(0, &ppp->ubuf->locked) ==0) {
2567 if(ppp->ubuf->head != ppp->ubuf->tail)
2568 mask |= POLLIN | POLLRDNORM;
2569 clear_bit(0, &ppp->ubuf->locked);
2571 if(tty->flags & (1<< TTY_OTHER_CLOSED))
2572 mask |= POLLHUP;
2573 if(tty_hung_up_p(filp))
2574 mask |= POLLHUP;
2575 if(ppp->tbuf->locked ==0)
2576 mask |= POLLOUT | POLLWRNORM;
2578 return mask;
2581 /*************************************************************
2582 * NETWORK OUTPUT
2583 * This routine accepts requests from the network layer
2584 * and attempts to deliver the packets.
2585 * It also includes various routines we are compelled to
2586 * have to make the network layer work (arp, etc...).
2587 *************************************************************/
2590 * Callback from the network layer when the device goes up.
2593 static int
2594 ppp_dev_open(struct device *dev)
2596 struct ppp *ppp =dev2ppp(dev);
2598 if(ppp2tty(ppp) == NULL) {
2599 printk(KERN_ERR
2600 "ppp: %s not connected to a TTY! can't go open!\n",
2601 dev->name);
2602 return-ENXIO;
2605 if(ppp->flags & SC_DEBUG)
2606 printk(KERN_INFO
2607 "ppp: channel %s going up for IP packets!\n",
2608 dev->name);
2610 CHECK_PPP(-ENXIO);
2611 return0;
2615 * Callback from the network layer when the ppp device goes down.
2618 static int
2619 ppp_dev_close(struct device *dev)
2621 struct ppp *ppp =dev2ppp(dev);
2623 if(ppp2tty(ppp) == NULL) {
2624 return-ENXIO;
2627 * We don't do anything about the device going down. It is not important
2628 * for us.
2630 if(ppp->flags & SC_DEBUG)
2631 printk(KERN_INFO
2632 "ppp: channel %s going down for IP packets!\n",
2633 dev->name);
2634 CHECK_PPP(-ENXIO);
2635 return0;
2639 * IOCTL operation to read the version of the driver.
2642 static int
2643 ppp_dev_ioctl_version(struct ppp *ppp,struct ifreq *ifr)
2645 int error;
2646 char*result = (char*) ifr->ifr_ifru.ifru_data;
2647 int len =strlen(szVersion) +1;
2649 * Move the version data
2651 error = -EFAULT;
2652 if(!copy_to_user(result, szVersion, len))
2653 error =0;
2654 return error;
2658 * IOCTL to read the statistics for the pppstats program.
2661 static int
2662 ppp_dev_ioctl_stats(struct ppp *ppp,struct ifreq *ifr,struct device *dev)
2664 struct ppp_stats *result, temp;
2665 int error;
2667 * Supply the information for the caller. First move the version data
2668 * then move the ppp stats; and finally the vj stats.
2670 memset(&temp,0,sizeof(temp));
2671 if(dev->flags & IFF_UP) {
2672 memcpy(&temp.p, &ppp->stats,sizeof(struct pppstat));
2673 if(ppp->slcomp != NULL) {
2674 temp.vj.vjs_packets = ppp->slcomp->sls_o_compressed+
2675 ppp->slcomp->sls_o_uncompressed;
2676 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2677 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2678 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2679 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2680 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2681 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2682 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2686 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2688 error = -EFAULT;
2689 if(!copy_to_user(result, &temp,sizeof(temp)))
2690 error =0;
2691 return error;
2695 * IOCTL to read the compression statistics for the pppstats program.
2698 static int
2699 ppp_dev_ioctl_comp_stats(struct ppp *ppp,struct ifreq *ifr,struct device *dev)
2701 struct ppp_comp_stats *result, temp;
2702 int error;
2704 * Supply the information for the caller.
2706 memset(&temp,0,sizeof(temp));
2707 if(dev->flags & IFF_UP) {
2708 if(ppp->sc_xc_state != NULL)
2709 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2710 &temp.c);
2712 if(ppp->sc_rc_state != NULL)
2713 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2714 &temp.d);
2717 * Move the data to the caller's buffer
2719 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2721 error = -EFAULT;
2722 if(!copy_to_user(result, &temp,sizeof(temp)))
2723 error =0;
2724 return error;
2728 * Callback from the network layer to process the sockioctl functions.
2731 static int
2732 ppp_dev_ioctl(struct device *dev,struct ifreq *ifr,int cmd)
2734 struct ppp *ppp =dev2ppp(dev);
2735 int error;
2737 CHECK_PPP_MAGIC(ppp);
2739 * Process the requests
2741 switch(cmd) {
2742 case SIOCGPPPSTATS:
2743 error =ppp_dev_ioctl_stats(ppp, ifr, dev);
2744 break;
2746 case SIOCGPPPCSTATS:
2747 error =ppp_dev_ioctl_comp_stats(ppp, ifr, dev);
2748 break;
2750 case SIOCGPPPVER:
2751 error =ppp_dev_ioctl_version(ppp, ifr);
2752 break;
2754 default:
2755 error = -EINVAL;
2756 break;
2758 return error;
2762 * Send an IP frame to the remote with vj header compression.
2764 * Return 0 if frame was queued for transmission.
2765 * 1 if frame must be re-queued for later driver support.
2766 * -1 if frame should be dropped.
2769 static int
2770 ppp_dev_xmit_ip(struct ppp *ppp,struct ppp_buffer *buf,
2771 __u8 *data,int len,enum NPmode npmode)
2773 int proto = PPP_IP;
2774 __u8 *hdr;
2776 * Branch on the type of processing for the IP frame.
2778 switch(npmode) {
2779 case NPMODE_PASS:
2780 break;
2782 case NPMODE_QUEUE:
2784 * We may not send the packet now, so drop it.
2785 * XXX It would be nice to be able to return it to the
2786 * network system to be queued and retransmitted later.
2788 if(ppp->flags & SC_DEBUG)
2789 printk(KERN_DEBUG "%s: returning frame\n",
2790 ppp->name);
2791 return-1;
2793 case NPMODE_ERROR:
2794 case NPMODE_DROP:
2795 if(ppp->flags & SC_DEBUG)
2796 printk(KERN_DEBUG
2797 "ppp_dev_xmit: npmode = %d on %s\n",
2798 ppp->sc_npmode[NP_IP], ppp->name);
2799 return-1;
2801 default:
2802 if(ppp->flags & SC_DEBUG)
2803 printk(KERN_WARNING
2804 "ppp_dev_xmit: unknown npmode %d on %s\n",
2805 ppp->sc_npmode[NP_IP], ppp->name);
2806 return-1;
2809 * At this point, the buffer will be transmitted. There is no other exit.
2811 * Try to compress the header.
2813 if(ppp->flags & SC_COMP_TCP) {
2814 len =slhc_compress(ppp->slcomp, data, len,
2815 buf_base(ppp->cbuf) + PPP_HDRLEN,
2816 &data,
2817 (ppp->flags & SC_NO_TCP_CCID) ==0);
2819 if(data[0] & SL_TYPE_COMPRESSED_TCP) {
2820 proto = PPP_VJC_COMP;
2821 data[0] ^= SL_TYPE_COMPRESSED_TCP;
2822 }else{
2823 if(data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2824 proto = PPP_VJC_UNCOMP;
2825 data[0] = (data[0] &0x0f) |0x40;
2829 * Send the frame
2831 len += PPP_HDRLEN;
2832 hdr = data - PPP_HDRLEN;
2834 hdr[0] = PPP_ALLSTATIONS;
2835 hdr[1] = PPP_UI;
2836 hdr[2] =0;
2837 hdr[3] = proto;
2839 returnppp_dev_xmit_frame(ppp, buf, hdr, len);
2843 * Send a non-IP data frame (such as an IPX frame) to the remote.
2845 * Return 0 if frame was queued for transmission.
2846 * 1 if frame must be re-queued for later driver support.
2848 static int
2849 ppp_dev_xmit_other(struct device *dev,struct ppp *ppp,
2850 __u8 *data,int len,int proto)
2852 __u8 *hdr;
2854 * Send the frame
2856 len += PPP_HDRLEN;
2857 hdr = data - PPP_HDRLEN;
2859 hdr[0] = PPP_ALLSTATIONS;
2860 hdr[1] = PPP_UI;
2861 hdr[2] = proto >>8;
2862 hdr[3] = proto;
2864 returnppp_dev_xmit_frame(ppp, ppp->wbuf, hdr, len);
2868 * Send a frame to the remote.
2871 static int
2872 ppp_dev_xmit(sk_buff *skb,struct device *dev)
2874 int answer, len;
2875 __u8 *data;
2876 struct ppp *ppp =dev2ppp(dev);
2877 struct tty_struct *tty =ppp2tty(ppp);
2879 * just a little sanity check.
2881 if(skb == NULL) {
2882 if(ppp->flags & SC_DEBUG)
2883 printk(KERN_WARNING "ppp_dev_xmit: null packet!\n");
2884 return0;
2887 * Avoid timing problem should tty hangup while data is queued to be sent
2889 if(!ppp->inuse) {
2890 dev_kfree_skb(skb);
2891 return0;
2894 * Validate the tty interface
2896 if(tty == NULL) {
2897 if(ppp->flags & SC_DEBUG)
2898 printk(KERN_ERR
2899 "ppp_dev_xmit: %s not connected to a TTY!\n",
2900 dev->name);
2901 dev_kfree_skb(skb);
2902 return0;
2905 * Fetch the pointer to the data
2907 len = skb->len;
2908 data =skb_data(skb);
2910 if(data == (__u8 *)0) {
2911 if(ppp->flags & SC_DEBUG)
2912 printk(KERN_CRIT "ppp_dev_xmit: %s Null skb data\n",
2913 dev->name);
2914 dev_kfree_skb(skb);
2915 return0;
2918 * Detect a change in the transfer size
2920 if(ppp->mtu !=ppp2dev(ppp)->mtu) {
2921 ppp_changedmtu(ppp,
2922 ppp2dev(ppp)->mtu,
2923 ppp->mru);
2926 * Acquire the lock on the transmission buffer. If the buffer was busy then
2927 * mark the device as busy.
2928 * We also require that ppp->tbuf be unlocked, in order to serialize
2929 * calls to ppp_dev_xmit_frame (which does compression) and the output
2930 * of frames w.r.t. tty writes from pppd.
2932 CHECK_BUF_MAGIC(ppp->wbuf);
2933 if(ppp->tbuf->locked ||lock_buffer(ppp->wbuf) !=0) {
2934 dev->tbusy =1;
2935 if(ppp->flags & SC_DEBUG)
2936 printk(KERN_DEBUG "dev_xmit blocked, t=%lu w=%lu\n",
2937 ppp->tbuf->locked, ppp->wbuf->locked);
2938 return1;
2941 * Look at the protocol in the skb to determine the difference between
2942 * an IP frame and an IPX frame.
2944 switch(ntohs(skb->protocol)) {
2945 case ETH_P_IPX:
2946 answer =ppp_dev_xmit_other(dev, ppp, data, len, PPP_IPX);
2947 break;
2949 case ETH_P_IP:
2950 answer =ppp_dev_xmit_ip(ppp, ppp->wbuf, data, len,
2951 ppp->sc_npmode[NP_IP]);
2952 break;
2954 default:/* All others have no support at this time. */
2955 dev_kfree_skb(skb);
2956 return0;
2959 * This is the end of the transmission. Release the buffer if it was sent.
2961 if(answer ==0) {
2962 /* packet queued OK */
2963 dev_kfree_skb(skb);
2964 }else{
2965 ppp->wbuf->locked =0;
2966 if(answer <0) {
2967 /* packet should be dropped */
2968 dev_kfree_skb(skb);
2969 answer =0;
2970 }else{
2971 /* packet should be queued for later */
2972 dev->tbusy =1;
2975 return answer;
2979 * Generate the statistic information for the /proc/net/dev listing.
2982 static struct net_device_stats *
2983 ppp_dev_stats(struct device *dev)
2985 struct ppp *ppp =dev2ppp(dev);
2987 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
2988 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
2989 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
2990 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
2991 ppp->estats.rx_bytes = ppp->stats.ppp_ibytes;
2992 ppp->estats.tx_bytes = ppp->stats.ppp_obytes;
2994 return&ppp->estats;
2997 /*************************************************************
2998 * UTILITIES
2999 * Miscellany called by various functions above.
3000 *************************************************************/
3002 /* Locate the previous instance of the PPP channel */
3003 static struct ppp *
3004 ppp_find(int pid_value)
3006 struct ppp *ppp;
3008 /* try to find the device which this pid is already using */
3009 for(ppp = ppp_list; ppp !=0; ppp = ppp->next) {
3010 if(ppp->inuse && ppp->sc_xfer == pid_value) {
3011 ppp->sc_xfer =0;
3012 break;
3015 return ppp;
3018 /* Collect hanged up channels */
3020 static voidppp_sync(void)
3022 struct device *dev;
3023 struct ppp *ppp;
3025 rtnl_lock();
3026 for(ppp = ppp_list; ppp !=0; ppp = ppp->next) {
3027 if(!ppp->inuse) {
3028 dev =ppp2dev(ppp);
3029 if(dev->flags&IFF_UP)
3030 dev_close(dev);
3033 rtnl_unlock();
3037 /* allocate or create a PPP channel */
3038 static struct ppp *
3039 ppp_alloc(void)
3041 int if_num;
3042 int status;
3043 struct device *dev;
3044 struct ppp *ppp;
3046 ppp_sync();
3048 /* try to find an free device */
3049 if_num =0;
3050 for(ppp = ppp_list; ppp !=0; ppp = ppp->next) {
3051 if(!test_and_set_bit(0, &ppp->inuse)) {
3053 /* Reregister device */
3055 dev =ppp2dev(ppp);
3056 unregister_netdev(dev);
3058 if(register_netdev(dev)) {
3059 printk(KERN_DEBUG "cannot reregister ppp device\n");
3060 return NULL;
3062 return ppp;
3064 ++if_num;
3067 * There are no available units, so make a new one.
3069 ppp = (struct ppp *)kmalloc(sizeof(struct ppp), GFP_KERNEL);
3070 if(ppp ==0)
3071 return0;
3072 memset(ppp,0,sizeof(*ppp));
3074 /* initialize channel control data */
3075 set_bit(0, &ppp->inuse);
3077 ppp->line = if_num;
3078 ppp->tty = NULL;
3079 ppp->backup_tty = NULL;
3080 if(ppp_last ==0)
3081 ppp_list = ppp;
3082 else
3083 ppp_last->next = ppp;
3084 ppp_last = ppp;
3085 ppp->next =0;
3087 dev =ppp2dev(ppp);
3088 dev->next = NULL;
3089 dev->init = ppp_init_dev;
3090 dev->name = ppp->name;
3091 sprintf(dev->name,"ppp%d", if_num);
3092 dev->base_addr = (__u32) if_num;
3093 dev->priv = (void*) ppp;
3095 /* register device so that we can be ifconfig'd */
3096 /* ppp_init_dev() will be called as a side-effect */
3097 status =register_netdev(dev);
3098 if(status ==0) {
3099 printk(KERN_INFO "registered device %s\n", dev->name);
3100 }else{
3101 printk(KERN_ERR
3102 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3103 dev->name, status);
3104 ppp = NULL;
3105 /* This one will forever be busy as it is not initialized */
3107 return ppp;
3111 * Utility procedures to print a buffer in hex/ascii
3114 static void
3115 ppp_print_hex(register __u8 * out,const __u8 * in,int count)
3117 register __u8 next_ch;
3118 static char hex[] ="0123456789ABCDEF";
3120 while(count-- >0) {
3121 next_ch = *in++;
3122 *out++ = hex[(next_ch >>4) &0x0F];
3123 *out++ = hex[next_ch &0x0F];
3124 ++out;
3128 static void
3129 ppp_print_char(register __u8 * out,const __u8 * in,int count)
3131 register __u8 next_ch;
3133 while(count-- >0) {
3134 next_ch = *in++;
3136 if(next_ch <0x20|| next_ch >0x7e)
3137 *out++ ='.';
3138 else{
3139 *out++ = next_ch;
3140 if(next_ch =='%')/* printk/syslogd has a bug !! */
3141 *out++ ='%';
3144 *out ='\0';
3147 static void
3148 ppp_print_buffer(const __u8 * name,const __u8 * buf,int count)
3150 __u8 line[44];
3152 if(name != (__u8 *) NULL)
3153 printk(KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3155 while(count >8) {
3156 memset(line,32,44);
3157 ppp_print_hex(line, buf,8);
3158 ppp_print_char(&line[8*3], buf,8);
3159 printk(KERN_DEBUG "%s\n", line);
3160 count -=8;
3161 buf +=8;
3164 if(count >0) {
3165 memset(line,32,44);
3166 ppp_print_hex(line, buf, count);
3167 ppp_print_char(&line[8*3], buf, count);
3168 printk(KERN_DEBUG "%s\n", line);
3172 /*************************************************************
3173 * Compressor module interface
3174 *************************************************************/
3176 struct compressor_link {
3177 struct compressor_link *next;
3178 struct compressor *comp;
3181 static struct compressor_link *ppp_compressors = (struct compressor_link *)0;
3183 static struct compressor *find_compressor(int type)
3185 struct compressor_link *lnk;
3186 unsigned long flags;
3188 save_flags(flags);
3189 cli();
3191 lnk = ppp_compressors;
3192 while(lnk != (struct compressor_link *)0) {
3193 if((int) (__u8) lnk->comp->compress_proto == type) {
3194 restore_flags(flags);
3195 return lnk->comp;
3197 lnk = lnk->next;
3200 restore_flags(flags);
3201 return(struct compressor *)0;
3204 static intppp_register_compressor(struct compressor *cp)
3206 struct compressor_link *new;
3207 unsigned long flags;
3209 new= (struct compressor_link *)kmalloc(sizeof(struct compressor_link), GFP_KERNEL);
3211 if(new== (struct compressor_link *)0)
3212 return1;
3214 save_flags(flags);
3215 cli();
3217 if(find_compressor(cp->compress_proto)) {
3218 restore_flags(flags);
3219 kfree(new);
3220 return0;
3223 new->next = ppp_compressors;
3224 new->comp = cp;
3225 ppp_compressors =new;
3227 restore_flags(flags);
3228 return0;
3231 static voidppp_unregister_compressor(struct compressor *cp)
3233 struct compressor_link *prev = (struct compressor_link *)0;
3234 struct compressor_link *lnk;
3235 unsigned long flags;
3237 save_flags(flags);
3238 cli();
3240 lnk = ppp_compressors;
3241 while(lnk != (struct compressor_link *)0) {
3242 if(lnk->comp == cp) {
3243 if(prev)
3244 prev->next = lnk->next;
3245 else
3246 ppp_compressors = lnk->next;
3247 kfree(lnk);
3248 break;
3250 prev = lnk;
3251 lnk = lnk->next;
3253 restore_flags(flags);
3256 /*************************************************************
3257 * Module support routines
3258 *************************************************************/
3260 #ifdef MODULE
3262 init_module(void)
3264 int status;
3266 /* register our line disciplines */
3267 status =ppp_first_time();
3268 if(status !=0)
3269 printk(KERN_INFO
3270 "PPP: ppp_init() failure %d\n", status);
3271 return(status);
3274 void
3275 cleanup_module(void)
3277 int status;
3278 struct device *dev;
3279 struct ppp *ppp, *next_ppp;
3280 int busy_flag =0;
3282 * Ensure that the devices are not in operation.
3284 for(ppp = ppp_list; ppp !=0; ppp = ppp->next) {
3285 if(ppp->inuse && ppp->tty != NULL) {
3286 busy_flag =1;
3287 break;
3290 dev =ppp2dev(ppp);
3291 if(dev->start || dev->flags & IFF_UP) {
3292 busy_flag =1;
3293 break;
3297 * Ensure that there are no compressor modules registered
3299 if(ppp_compressors != NULL)
3300 busy_flag =1;
3302 if(busy_flag) {
3303 printk(KERN_INFO
3304 "PPP: device busy, remove delayed\n");
3305 return;
3308 * Release the tty registration of the line discipline so that no new entries
3309 * may be created.
3311 status =tty_register_ldisc(N_PPP, NULL);
3312 if(status !=0)
3313 printk(KERN_INFO
3314 "PPP: Unable to unregister ppp line discipline "
3315 "(err = %d)\n", status);
3316 else
3317 printk(KERN_INFO
3318 "PPP: ppp line discipline successfully unregistered\n");
3320 * De-register the devices so that there is no problem with them
3322 for(ppp = ppp_list; ppp !=0; ppp = next_ppp) {
3323 next_ppp = ppp->next;
3324 ppp_release(ppp);
3325 unregister_netdev(&ppp->dev);
3326 kfree(ppp);
3329 #endif
close