2 * Generic PPP layer for Linux.
4 * Copyright 1999 Paul Mackerras.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * The generic PPP layer handles the PPP network interfaces, the
12 * /dev/ppp device, packet and VJ compression, and multilink.
13 * It talks to PPP `channels' via the interface defined in
14 * include/linux/ppp_channel.h. Channels provide the basic means for
15 * sending and receiving PPP frames on some kind of communications
18 * Part of the code in this driver was inspired by the old async-only
19 * PPP driver, written by Michael Callahan and Al Longyear, and
20 * subsequently hacked by Paul Mackerras.
22 * ==FILEVERSION 990806==
25 /* $Id: ppp_generic.c,v 1.3 1999/09/02 05:30:12 paulus Exp $ */
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/kmod.h>
31 #include <linux/list.h>
32 #include <linux/netdevice.h>
33 #include <linux/poll.h>
34 #include <linux/ppp_defs.h>
35 #include <linux/if_ppp.h>
36 #include <linux/ppp_channel.h>
37 #include <linux/ppp-comp.h>
38 #include <linux/skbuff.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/if_arp.h>
42 #include <linux/tcp.h>
43 #include <linux/spinlock.h>
44 #include <net/slhc_vj.h>
46 #define PPP_VERSION "2.4.0"
48 EXPORT_SYMBOL(ppp_register_channel
);
49 EXPORT_SYMBOL(ppp_unregister_channel
);
50 EXPORT_SYMBOL(ppp_input
);
51 EXPORT_SYMBOL(ppp_input_error
);
52 EXPORT_SYMBOL(ppp_output_wakeup
);
53 EXPORT_SYMBOL(ppp_register_compressor
);
54 EXPORT_SYMBOL(ppp_unregister_compressor
);
57 * Network protocols we support.
59 #define NP_IP 0 /* Internet Protocol V4 */
60 #define NP_IPV6 1 /* Internet Protocol V6 */
61 #define NP_IPX 2 /* IPX protocol */
62 #define NP_AT 3 /* Appletalk protocol */
63 #define NUM_NP 4 /* Number of NPs. */
66 * Data structure describing one ppp unit.
67 * A ppp unit corresponds to a ppp network interface device
68 * and represents a multilink bundle.
69 * It may have 0 or more ppp channels connected to it.
72 struct list_head list
; /* link in list of ppp units */
73 int index
; /* interface unit number */
74 char name
[16]; /* unit name */
75 int refcnt
; /* # open /dev/ppp attached */
76 unsigned long busy
; /* lock and other bits */
77 struct list_head channels
; /* list of attached channels */
78 int n_channels
; /* how many channels are attached */
79 int mru
; /* max receive unit */
80 unsigned int flags
; /* control bits */
81 unsigned int xstate
; /* transmit state bits */
82 unsigned int rstate
; /* receive state bits */
83 int debug
; /* debug flags */
84 struct slcompress
*vj
; /* state for VJ header compression */
85 struct sk_buff_head xq
; /* pppd transmit queue */
86 struct sk_buff_head rq
; /* receive queue for pppd */
87 wait_queue_head_t rwait
; /* for poll on reading /dev/ppp */
88 enum NPmode npmode
[NUM_NP
]; /* what to do with each net proto */
89 struct sk_buff
*xmit_pending
; /* a packet ready to go out */
90 struct sk_buff_head recv_pending
;/* pending input packets */
91 struct compressor
*xcomp
; /* transmit packet compressor */
92 void *xc_state
; /* its internal state */
93 struct compressor
*rcomp
; /* receive decompressor */
94 void *rc_state
; /* its internal state */
95 unsigned long last_xmit
; /* jiffies when last pkt sent */
96 unsigned long last_recv
; /* jiffies when last pkt rcvd */
97 struct net_device
*dev
; /* network interface device */
98 struct net_device_stats stats
; /* statistics */
101 static LIST_HEAD(all_ppp_units
);
102 static spinlock_t all_ppp_lock
= SPIN_LOCK_UNLOCKED
;
105 * Private data structure for each channel.
106 * Ultimately this will have multilink stuff etc. in it.
109 struct list_head list
; /* link in list of channels per unit */
110 struct ppp_channel
*chan
; /* public channel data structure */
111 int blocked
; /* if channel refused last packet */
112 struct ppp
*ppp
; /* ppp unit we're connected to */
115 /* Bit numbers in busy */
118 #define XMIT_WAKEUP 2
121 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC.
122 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
123 * Bits in xstate: SC_COMP_RUN
125 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC)
127 /* Get the PPP protocol number from a skb */
128 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
130 /* We limit the length of ppp->rq to this (arbitrary) value */
131 #define PPP_MAX_RQLEN 32
134 static void ppp_xmit_unlock(struct ppp
*ppp
);
135 static void ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
136 static void ppp_push(struct ppp
*ppp
);
137 static void ppp_recv_unlock(struct ppp
*ppp
);
138 static void ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
139 static struct sk_buff
*ppp_decompress_frame(struct ppp
*ppp
,
140 struct sk_buff
*skb
);
141 static int ppp_set_compress(struct ppp
*ppp
, unsigned long arg
);
142 static void ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
);
143 static void ppp_ccp_closed(struct ppp
*ppp
);
144 static struct compressor
*find_compressor(int type
);
145 static void ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
);
146 static struct ppp
*ppp_create_unit(int unit
, int *retp
);
147 static void ppp_release_unit(struct ppp
*ppp
);
148 static struct ppp
*ppp_find_unit(int unit
);
150 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
151 static inline int proto_to_npindex(int proto
)
166 /* Translates an NP index into a PPP protocol number */
167 static const int npindex_to_proto
[NUM_NP
] = {
174 /* Translates an ethertype into an NP index */
175 static inline int ethertype_to_npindex(int ethertype
)
191 /* Translates an NP index into an ethertype */
192 static const int npindex_to_ethertype
[NUM_NP
] = {
200 * Routines for locking and unlocking the transmit and receive paths
204 lock_path(struct ppp
*ppp
, int bit
)
206 int timeout
= 1000000;
209 while (test_bit(bit
, &ppp
->busy
)) {
211 if (--timeout
== 0) {
212 printk(KERN_ERR
"lock_path timeout ppp=%p bit=%x\n", ppp
, bit
);
216 } while (test_and_set_bit(bit
, &ppp
->busy
));
221 trylock_path(struct ppp
*ppp
, int bit
)
223 if (test_and_set_bit(bit
, &ppp
->busy
))
230 unlock_path(struct ppp
*ppp
, int bit
)
233 clear_bit(bit
, &ppp
->busy
);
236 #define lock_xmit_path(ppp) lock_path(ppp, XMIT_BUSY)
237 #define trylock_xmit_path(ppp) trylock_path(ppp, XMIT_BUSY)
238 #define unlock_xmit_path(ppp) unlock_path(ppp, XMIT_BUSY)
239 #define lock_recv_path(ppp) lock_path(ppp, RECV_BUSY)
240 #define trylock_recv_path(ppp) trylock_path(ppp, RECV_BUSY)
241 #define unlock_recv_path(ppp) unlock_path(ppp, RECV_BUSY)
244 free_skbs(struct sk_buff_head
*head
)
248 while ((skb
= skb_dequeue(head
)) != 0)
253 * /dev/ppp device routines.
254 * The /dev/ppp device is used by pppd to control the ppp unit.
255 * It supports the read, write, ioctl and poll functions.
257 static int ppp_open(struct inode
*inode
, struct file
*file
)
260 * This could (should?) be enforced by the permissions on /dev/ppp.
262 if (!capable(CAP_NET_ADMIN
))
268 static int ppp_release(struct inode
*inode
, struct file
*file
)
270 struct ppp
*ppp
= (struct ppp
*) file
->private_data
;
273 file
->private_data
= 0;
274 ppp_release_unit(ppp
);
280 static ssize_t
ppp_read(struct file
*file
, char *buf
,
281 size_t count
, loff_t
*ppos
)
283 struct ppp
*ppp
= (struct ppp
*) file
->private_data
;
284 DECLARE_WAITQUEUE(wait
, current
);
286 struct sk_buff
*skb
= 0;
290 goto out
; /* not currently attached */
292 add_wait_queue(&ppp
->rwait
, &wait
);
293 current
->state
= TASK_INTERRUPTIBLE
;
296 skb
= skb_dequeue(&ppp
->rq
);
299 if (file
->f_flags
& O_NONBLOCK
)
302 if (signal_pending(current
))
306 current
->state
= TASK_RUNNING
;
307 remove_wait_queue(&ppp
->rwait
, &wait
);
313 if (skb
->len
> count
)
316 if (copy_to_user(buf
, skb
->data
, skb
->len
))
326 static ssize_t
ppp_write(struct file
*file
, const char *buf
,
327 size_t count
, loff_t
*ppos
)
329 struct ppp
*ppp
= (struct ppp
*) file
->private_data
;
338 skb
= alloc_skb(count
+ 2, GFP_KERNEL
);
343 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
348 skb_queue_tail(&ppp
->xq
, skb
);
349 if (trylock_xmit_path(ppp
))
350 ppp_xmit_unlock(ppp
);
358 static unsigned int ppp_poll(struct file
*file
, poll_table
*wait
)
360 struct ppp
*ppp
= (struct ppp
*) file
->private_data
;
365 poll_wait(file
, &ppp
->rwait
, wait
);
366 mask
= POLLOUT
| POLLWRNORM
;
367 if (skb_peek(&ppp
->rq
) != 0)
368 mask
|= POLLIN
| POLLRDNORM
;
372 static int ppp_ioctl(struct inode
*inode
, struct file
*file
,
373 unsigned int cmd
, unsigned long arg
)
375 struct ppp
*ppp
= (struct ppp
*) file
->private_data
;
376 int err
, val
, val2
, i
;
377 struct ppp_idle idle
;
380 if (cmd
== PPPIOCNEWUNIT
) {
381 /* Create a new ppp unit */
386 if (get_user(unit
, (int *) arg
))
388 ppp
= ppp_create_unit(unit
, &ret
);
391 file
->private_data
= ppp
;
392 if (put_user(ppp
->index
, (int *) arg
))
396 if (cmd
== PPPIOCATTACH
) {
397 /* Attach to an existing ppp unit */
402 if (get_user(unit
, (int *) arg
))
404 spin_lock(&all_ppp_lock
);
405 ppp
= ppp_find_unit(unit
);
408 spin_unlock(&all_ppp_lock
);
411 file
->private_data
= ppp
;
420 file
->private_data
= 0;
421 ppp_release_unit(ppp
);
426 if (get_user(val
, (int *) arg
))
433 if (get_user(val
, (int *) arg
))
435 if (ppp
->flags
& ~val
& SC_CCP_OPEN
)
437 ppp
->flags
= val
& SC_FLAG_BITS
;
442 val
= ppp
->flags
| ppp
->xstate
| ppp
->rstate
;
443 if (put_user(val
, (int *) arg
))
448 case PPPIOCSCOMPRESS
:
449 err
= ppp_set_compress(ppp
, arg
);
453 if (put_user(ppp
->index
, (int *) arg
))
459 if (get_user(val
, (int *) arg
))
466 if (put_user(ppp
->debug
, (int *) arg
))
472 idle
.xmit_idle
= (jiffies
- ppp
->last_xmit
) / HZ
;
473 idle
.recv_idle
= (jiffies
- ppp
->last_recv
) / HZ
;
474 if (copy_to_user((void *) arg
, &idle
, sizeof(idle
)))
480 if (get_user(val
, (int *) arg
))
483 if ((val
>> 16) != 0) {
491 ppp
->vj
= slhc_init(val2
+1, val
+1);
492 ppp_recv_unlock(ppp
);
493 ppp_xmit_unlock(ppp
);
496 printk(KERN_ERR
"PPP: no memory (VJ compressor)\n");
504 if (copy_from_user(&npi
, (void *) arg
, sizeof(npi
)))
506 err
= proto_to_npindex(npi
.protocol
);
510 if (cmd
== PPPIOCGNPMODE
) {
512 npi
.mode
= ppp
->npmode
[i
];
513 if (copy_to_user((void *) arg
, &npi
, sizeof(npi
)))
516 ppp
->npmode
[i
] = npi
.mode
;
517 /* we may be able to transmit more packets now (??) */
529 static struct file_operations ppp_device_fops
= {
542 #define PPP_MAJOR 108
544 /* Called at boot time if ppp is compiled into the kernel,
545 or at module load time (from init_module) if compiled as a module. */
547 ppp_init(struct net_device
*dev
)
551 extern struct compressor ppp_deflate
, ppp_deflate_draft
;
552 extern int ppp_async_init(void);
555 printk(KERN_INFO
"PPP generic driver version " PPP_VERSION
"\n");
556 err
= register_chrdev(PPP_MAJOR
, "ppp", &ppp_device_fops
);
558 printk(KERN_ERR
"failed to register PPP device (%d)\n", err
);
560 #ifdef CONFIG_PPP_ASYNC
563 #ifdef CONFIG_PPP_DEFLATE
564 if (ppp_register_compressor(&ppp_deflate
) == 0)
565 printk(KERN_INFO
"PPP Deflate compression module registered\n");
566 ppp_register_compressor(&ppp_deflate_draft
);
574 * Network interface unit routines.
577 ppp_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
579 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
585 /* can skb->data ever be 0? */
587 npi
= ethertype_to_npindex(ntohs(skb
->protocol
));
591 /* Drop, accept or reject the packet */
592 switch (ppp
->npmode
[npi
]) {
596 /* it would be nice to have a way to tell the network
597 system to queue this one up for later. */
604 /* The transmit side of the ppp interface is serialized by
605 the XMIT_BUSY bit in ppp->busy. */
606 if (!trylock_xmit_path(ppp
)) {
610 if (ppp
->xmit_pending
)
612 if (ppp
->xmit_pending
) {
614 ppp_xmit_unlock(ppp
);
619 /* Put the 2-byte PPP protocol number on the front,
620 making sure there is room for the address and control fields. */
621 if (skb_headroom(skb
) < PPP_HDRLEN
) {
624 ns
= alloc_skb(skb
->len
+ PPP_HDRLEN
, GFP_ATOMIC
);
627 skb_reserve(ns
, PPP_HDRLEN
);
628 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
632 pp
= skb_push(skb
, 2);
633 proto
= npindex_to_proto
[npi
];
637 ppp_send_frame(ppp
, skb
);
638 ppp_xmit_unlock(ppp
);
642 ppp_xmit_unlock(ppp
);
649 static struct net_device_stats
*
650 ppp_net_stats(struct net_device
*dev
)
652 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
658 ppp_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
660 struct ppp
*ppp
= dev
->priv
;
662 void *addr
= (void *) ifr
->ifr_ifru
.ifru_data
;
663 struct ppp_stats stats
;
664 struct ppp_comp_stats cstats
;
669 ppp_get_stats(ppp
, &stats
);
670 if (copy_to_user(addr
, &stats
, sizeof(stats
)))
676 memset(&cstats
, 0, sizeof(cstats
));
677 if (ppp
->xc_state
!= 0)
678 ppp
->xcomp
->comp_stat(ppp
->xc_state
, &cstats
.c
);
679 if (ppp
->rc_state
!= 0)
680 ppp
->rcomp
->decomp_stat(ppp
->rc_state
, &cstats
.d
);
681 if (copy_to_user(addr
, &cstats
, sizeof(cstats
)))
688 if (copy_to_user(addr
, vers
, strlen(vers
) + 1))
701 ppp_net_init(struct net_device
*dev
)
703 dev
->hard_header_len
= PPP_HDRLEN
;
705 dev
->hard_start_xmit
= ppp_start_xmit
;
706 dev
->get_stats
= ppp_net_stats
;
707 dev
->do_ioctl
= ppp_net_ioctl
;
709 dev
->tx_queue_len
= 3;
710 dev
->type
= ARPHRD_PPP
;
711 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
713 dev_init_buffers(dev
);
718 * Transmit-side routines.
722 * Called to unlock the transmit side of the ppp unit,
723 * making sure that any work queued up gets done.
726 ppp_xmit_unlock(struct ppp
*ppp
)
731 if (test_and_clear_bit(XMIT_WAKEUP
, &ppp
->busy
))
733 while (ppp
->xmit_pending
== 0
734 && (skb
= skb_dequeue(&ppp
->xq
)) != 0)
735 ppp_send_frame(ppp
, skb
);
736 unlock_xmit_path(ppp
);
737 if (!(test_bit(XMIT_WAKEUP
, &ppp
->busy
)
738 || (ppp
->xmit_pending
== 0 && skb_peek(&ppp
->xq
))))
740 if (!trylock_xmit_path(ppp
))
746 * Compress and send a frame.
747 * The caller should have locked the xmit path,
748 * and xmit_pending should be 0.
751 ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
753 int proto
= PPP_PROTO(skb
);
754 struct sk_buff
*new_skb
;
758 ++ppp
->stats
.tx_packets
;
759 ppp
->stats
.tx_bytes
+= skb
->len
- 2;
763 if (ppp
->vj
== 0 || (ppp
->flags
& SC_COMP_TCP
) == 0)
765 /* try to do VJ TCP header compression */
766 new_skb
= alloc_skb(skb
->len
+ 2, GFP_ATOMIC
);
768 printk(KERN_ERR
"PPP: no memory (VJ comp pkt)\n");
771 skb_reserve(new_skb
, 2);
773 len
= slhc_compress(ppp
->vj
, cp
, skb
->len
- 2,
774 new_skb
->data
+ 2, &cp
,
775 !(ppp
->flags
& SC_NO_TCP_CCID
));
776 if (cp
== skb
->data
+ 2) {
777 /* didn't compress */
780 if (cp
[0] & SL_TYPE_COMPRESSED_TCP
) {
781 proto
= PPP_VJC_COMP
;
782 cp
[0] &= ~SL_TYPE_COMPRESSED_TCP
;
784 proto
= PPP_VJC_UNCOMP
;
785 cp
[0] = skb
->data
[2];
789 cp
= skb_put(skb
, len
+ 2);
796 /* peek at outbound CCP frames */
797 ppp_ccp_peek(ppp
, skb
, 0);
801 /* try to do packet compression */
802 if ((ppp
->xstate
& SC_COMP_RUN
) && ppp
->xc_state
!= 0
803 && proto
!= PPP_LCP
&& proto
!= PPP_CCP
) {
804 new_skb
= alloc_skb(ppp
->dev
->mtu
+ PPP_HDRLEN
, GFP_ATOMIC
);
806 printk(KERN_ERR
"PPP: no memory (comp pkt)\n");
810 /* compressor still expects A/C bytes in hdr */
811 len
= ppp
->xcomp
->compress(ppp
->xc_state
, skb
->data
- 2,
812 new_skb
->data
, skb
->len
+ 2,
813 ppp
->dev
->mtu
+ PPP_HDRLEN
);
814 if (len
> 0 && (ppp
->flags
& SC_CCP_UP
)) {
818 skb_pull(skb
, 2); /* pull off A/C bytes */
820 /* didn't compress, or CCP not up yet */
825 /* for data packets, record the time */
827 ppp
->last_xmit
= jiffies
;
830 * If we are waiting for traffic (demand dialling),
831 * queue it up for pppd to receive.
833 if (ppp
->flags
& SC_LOOP_TRAFFIC
) {
834 if (ppp
->rq
.qlen
> PPP_MAX_RQLEN
)
836 skb_queue_tail(&ppp
->rq
, skb
);
837 wake_up_interruptible(&ppp
->rwait
);
841 ppp
->xmit_pending
= skb
;
847 ++ppp
->stats
.tx_errors
;
851 * Try to send the frame in xmit_pending.
852 * The caller should have the xmit path locked.
855 ppp_push(struct ppp
*ppp
)
857 struct list_head
*list
;
858 struct channel
*chan
;
859 struct sk_buff
*skb
= ppp
->xmit_pending
;
864 list
= &ppp
->channels
;
865 if (list_empty(list
)) {
866 /* nowhere to send the packet, just drop it */
867 ppp
->xmit_pending
= 0;
872 /* If we are doing multilink, decide which channel gets the
873 packet, and/or fragment the packet over several links. */
874 /* XXX for now, just take the first channel */
876 chan
= list_entry(list
, struct channel
, list
);
878 if (chan
->chan
->ops
->start_xmit(chan
->chan
, skb
)) {
879 ppp
->xmit_pending
= 0;
886 * Receive-side routines.
889 ppp_do_recv(struct ppp
*ppp
, struct sk_buff
*skb
)
891 skb_queue_tail(&ppp
->recv_pending
, skb
);
892 if (trylock_recv_path(ppp
))
893 ppp_recv_unlock(ppp
);
897 ppp_input(struct ppp_channel
*chan
, struct sk_buff
*skb
)
899 struct channel
*pch
= chan
->ppp
;
901 if (pch
== 0 || skb
->len
== 0) {
905 ppp_do_recv(pch
->ppp
, skb
);
908 /* Put a 0-length skb in the receive queue as an error indication */
910 ppp_input_error(struct ppp_channel
*chan
, int code
)
912 struct channel
*pch
= chan
->ppp
;
917 skb
= alloc_skb(0, GFP_ATOMIC
);
920 skb
->len
= 0; /* probably unnecessary */
922 ppp_do_recv(pch
->ppp
, skb
);
926 ppp_recv_unlock(struct ppp
*ppp
)
931 while ((skb
= skb_dequeue(&ppp
->recv_pending
)) != 0)
932 ppp_receive_frame(ppp
, skb
);
933 unlock_recv_path(ppp
);
934 if (skb_peek(&ppp
->recv_pending
) == 0)
936 if (!trylock_recv_path(ppp
))
942 ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
948 /* XXX should do something with code in skb->cb[0] */
949 goto err
; /* error indication */
953 ++ppp
->stats
.rx_length_errors
;
957 /* Decompress the frame, if compressed. */
958 if (ppp
->rc_state
!= 0 && (ppp
->rstate
& SC_DECOMP_RUN
)
959 && (ppp
->rstate
& (SC_DC_FERROR
| SC_DC_ERROR
)) == 0)
960 skb
= ppp_decompress_frame(ppp
, skb
);
962 proto
= PPP_PROTO(skb
);
965 /* decompress VJ compressed packets */
966 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
968 if (skb_tailroom(skb
) < 124) {
969 /* copy to a new sk_buff with more tailroom */
970 ns
= dev_alloc_skb(skb
->len
+ 128);
972 printk(KERN_ERR
"PPP: no memory (VJ decomp)\n");
976 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
980 len
= slhc_uncompress(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2);
982 printk(KERN_ERR
"PPP: VJ decompression error\n");
987 skb_put(skb
, len
- skb
->len
);
988 else if (len
< skb
->len
)
994 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
996 if (slhc_remember(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2) <= 0) {
997 printk(KERN_ERR
"PPP: VJ uncompressed error\n");
1004 ppp_ccp_peek(ppp
, skb
, 1);
1008 ++ppp
->stats
.rx_packets
;
1009 ppp
->stats
.rx_bytes
+= skb
->len
- 2;
1011 npi
= proto_to_npindex(proto
);
1013 /* control or unknown frame - pass it to pppd */
1014 skb_queue_tail(&ppp
->rq
, skb
);
1015 /* limit queue length by dropping old frames */
1016 while (ppp
->rq
.qlen
> PPP_MAX_RQLEN
) {
1017 skb
= skb_dequeue(&ppp
->rq
);
1021 /* wake up any process polling or blocking on read */
1022 wake_up_interruptible(&ppp
->rwait
);
1025 /* network protocol frame - give it to the kernel */
1026 ppp
->last_recv
= jiffies
;
1027 if ((ppp
->dev
->flags
& IFF_UP
) == 0
1028 || ppp
->npmode
[npi
] != NPMODE_PASS
) {
1031 skb_pull(skb
, 2); /* chop off protocol */
1032 skb
->dev
= ppp
->dev
;
1033 skb
->protocol
= htons(npindex_to_ethertype
[npi
]);
1034 skb
->mac
.raw
= skb
->data
;
1041 ++ppp
->stats
.rx_errors
;
1047 static struct sk_buff
*
1048 ppp_decompress_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1050 int proto
= PPP_PROTO(skb
);
1054 if (proto
== PPP_COMP
) {
1055 ns
= dev_alloc_skb(ppp
->mru
+ PPP_HDRLEN
);
1057 printk(KERN_ERR
"ppp_receive: no memory\n");
1060 /* the decompressor still expects the A/C bytes in the hdr */
1061 len
= ppp
->rcomp
->decompress(ppp
->rc_state
, skb
->data
- 2,
1062 skb
->len
+ 2, ns
->data
, ppp
->mru
+ PPP_HDRLEN
);
1064 /* Pass the compressed frame to pppd as an
1065 error indication. */
1066 if (len
== DECOMP_FATALERROR
)
1067 ppp
->rstate
|= SC_DC_FERROR
;
1074 skb_pull(skb
, 2); /* pull off the A/C bytes */
1077 /* Uncompressed frame - pass to decompressor so it
1078 can update its dictionary if necessary. */
1079 if (ppp
->rcomp
->incomp
)
1080 ppp
->rcomp
->incomp(ppp
->rc_state
, skb
->data
- 2,
1087 ppp
->rstate
|= SC_DC_ERROR
;
1090 ++ppp
->stats
.rx_errors
;
1095 * Channel interface.
1099 * Connect a channel to a given PPP unit.
1100 * The channel MUST NOT be connected to a PPP unit already.
1103 ppp_register_channel(struct ppp_channel
*chan
, int unit
)
1106 struct channel
*pch
;
1109 spin_lock(&all_ppp_lock
);
1110 ppp
= ppp_find_unit(unit
);
1113 pch
= kmalloc(sizeof(struct channel
), GFP_ATOMIC
);
1117 memset(pch
, 0, sizeof(struct channel
));
1120 list_add(&pch
->list
, &ppp
->channels
);
1125 spin_unlock(&all_ppp_lock
);
1130 * Disconnect a channel from its PPP unit.
1133 ppp_unregister_channel(struct ppp_channel
*chan
)
1135 struct channel
*pch
;
1137 spin_lock(&all_ppp_lock
);
1138 if ((pch
= chan
->ppp
) != 0) {
1140 list_del(&pch
->list
);
1141 --pch
->ppp
->n_channels
;
1144 spin_unlock(&all_ppp_lock
);
1148 * Callback from a channel when it can accept more to transmit.
1149 * This should ideally be called at BH level, not interrupt level.
1152 ppp_output_wakeup(struct ppp_channel
*chan
)
1154 struct channel
*pch
= chan
->ppp
;
1161 set_bit(XMIT_WAKEUP
, &ppp
->busy
);
1162 if (trylock_xmit_path(ppp
))
1163 ppp_xmit_unlock(ppp
);
1164 if (ppp
->xmit_pending
== 0) {
1165 ppp
->dev
->tbusy
= 0;
1171 * Compression control.
1174 /* Process the PPPIOCSCOMPRESS ioctl. */
1176 ppp_set_compress(struct ppp
*ppp
, unsigned long arg
)
1179 struct compressor
*cp
;
1180 struct ppp_option_data data
;
1181 unsigned char ccp_option
[CCP_MAX_OPTION_LENGTH
];
1187 if (copy_from_user(&data
, (void *) arg
, sizeof(data
))
1188 || (data
.length
<= CCP_MAX_OPTION_LENGTH
1189 && copy_from_user(ccp_option
, data
.ptr
, data
.length
)))
1192 if (data
.length
> CCP_MAX_OPTION_LENGTH
1193 || ccp_option
[1] < 2 || ccp_option
[1] > data
.length
)
1196 cp
= find_compressor(ccp_option
[0]);
1199 sprintf(modname
, "ppp-compress-%d", ccp_option
[0]);
1200 request_module(modname
);
1201 cp
= find_compressor(ccp_option
[0]);
1203 #endif /* CONFIG_KMOD */
1208 if (data
.transmit
) {
1209 lock_xmit_path(ppp
);
1210 ppp
->xstate
&= ~SC_COMP_RUN
;
1211 if (ppp
->xc_state
!= 0) {
1212 ppp
->xcomp
->comp_free(ppp
->xc_state
);
1217 ppp
->xc_state
= cp
->comp_alloc(ccp_option
, data
.length
);
1218 ppp_xmit_unlock(ppp
);
1219 if (ppp
->xc_state
== 0)
1223 lock_recv_path(ppp
);
1224 ppp
->rstate
&= ~SC_DECOMP_RUN
;
1225 if (ppp
->rc_state
!= 0) {
1226 ppp
->rcomp
->decomp_free(ppp
->rc_state
);
1231 ppp
->rc_state
= cp
->decomp_alloc(ccp_option
, data
.length
);
1232 ppp_recv_unlock(ppp
);
1233 if (ppp
->rc_state
== 0)
1243 * Look at a CCP packet and update our state accordingly.
1244 * We assume the caller has the xmit or recv path locked.
1247 ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
)
1249 unsigned char *dp
= skb
->data
+ 2;
1252 if (skb
->len
< CCP_HDRLEN
+ 2
1253 || skb
->len
< (len
= CCP_LENGTH(dp
)) + 2)
1254 return; /* too short */
1256 switch (CCP_CODE(dp
)) {
1261 * CCP is going down - disable compression.
1264 ppp
->rstate
&= ~SC_DECOMP_RUN
;
1266 ppp
->xstate
&= ~SC_COMP_RUN
;
1270 if ((ppp
->flags
& (SC_CCP_OPEN
| SC_CCP_UP
)) != SC_CCP_OPEN
)
1274 if (len
< CCP_OPT_MINLEN
|| len
< CCP_OPT_LENGTH(dp
))
1277 /* we will start receiving compressed packets */
1278 if (ppp
->rc_state
== 0)
1280 if (ppp
->rcomp
->decomp_init(ppp
->rc_state
, dp
, len
,
1281 ppp
->index
, 0, ppp
->mru
, ppp
->debug
)) {
1282 ppp
->rstate
|= SC_DECOMP_RUN
;
1283 ppp
->rstate
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
1286 /* we will soon start sending compressed packets */
1287 if (ppp
->xc_state
== 0)
1289 if (ppp
->xcomp
->comp_init(ppp
->xc_state
, dp
, len
,
1290 ppp
->index
, 0, ppp
->debug
))
1291 ppp
->xstate
|= SC_COMP_RUN
;
1296 /* reset the [de]compressor */
1297 if ((ppp
->flags
& SC_CCP_UP
) == 0)
1300 if (ppp
->rc_state
&& (ppp
->rstate
& SC_DECOMP_RUN
)) {
1301 ppp
->rcomp
->decomp_reset(ppp
->rc_state
);
1302 ppp
->rstate
&= ~SC_DC_ERROR
;
1305 if (ppp
->xc_state
&& (ppp
->xstate
& SC_COMP_RUN
))
1306 ppp
->xcomp
->comp_reset(ppp
->xc_state
);
1312 /* Free up compression resources. */
1314 ppp_ccp_closed(struct ppp
*ppp
)
1316 ppp
->flags
&= ~(SC_CCP_OPEN
| SC_CCP_UP
);
1318 lock_xmit_path(ppp
);
1319 ppp
->xstate
&= ~SC_COMP_RUN
;
1320 if (ppp
->xc_state
) {
1321 ppp
->xcomp
->comp_free(ppp
->xc_state
);
1324 ppp_xmit_unlock(ppp
);
1326 lock_recv_path(ppp
);
1327 ppp
->xstate
&= ~SC_DECOMP_RUN
;
1328 if (ppp
->rc_state
) {
1329 ppp
->rcomp
->decomp_free(ppp
->rc_state
);
1332 ppp_recv_unlock(ppp
);
1335 /* List of compressors. */
1336 static LIST_HEAD(compressor_list
);
1337 static spinlock_t compressor_list_lock
= SPIN_LOCK_UNLOCKED
;
1339 struct compressor_entry
{
1340 struct list_head list
;
1341 struct compressor
*comp
;
1344 static struct compressor_entry
*
1345 find_comp_entry(int proto
)
1347 struct compressor_entry
*ce
;
1348 struct list_head
*list
= &compressor_list
;
1350 while ((list
= list
->next
) != &compressor_list
) {
1351 ce
= list_entry(list
, struct compressor_entry
, list
);
1352 if (ce
->comp
->compress_proto
== proto
)
1358 /* Register a compressor */
1360 ppp_register_compressor(struct compressor
*cp
)
1362 struct compressor_entry
*ce
;
1365 spin_lock(&compressor_list_lock
);
1367 if (find_comp_entry(cp
->compress_proto
) != 0)
1370 ce
= kmalloc(sizeof(struct compressor_entry
), GFP_KERNEL
);
1375 list_add(&ce
->list
, &compressor_list
);
1377 spin_unlock(&compressor_list_lock
);
1381 /* Unregister a compressor */
1383 ppp_unregister_compressor(struct compressor
*cp
)
1385 struct compressor_entry
*ce
;
1387 spin_lock(&compressor_list_lock
);
1388 ce
= find_comp_entry(cp
->compress_proto
);
1389 if (ce
!= 0 && ce
->comp
== cp
) {
1390 list_del(&ce
->list
);
1393 spin_unlock(&compressor_list_lock
);
1396 /* Find a compressor. */
1397 static struct compressor
*
1398 find_compressor(int type
)
1400 struct compressor_entry
*ce
;
1401 struct compressor
*cp
= 0;
1403 spin_lock(&compressor_list_lock
);
1404 ce
= find_comp_entry(type
);
1407 spin_unlock(&compressor_list_lock
);
1412 * Miscelleneous stuff.
1416 ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
)
1418 struct slcompress
*vj
= ppp
->vj
;
1420 memset(st
, 0, sizeof(*st
));
1421 st
->p
.ppp_ipackets
= ppp
->stats
.rx_packets
;
1422 st
->p
.ppp_ierrors
= ppp
->stats
.rx_errors
;
1423 st
->p
.ppp_ibytes
= ppp
->stats
.rx_bytes
;
1424 st
->p
.ppp_opackets
= ppp
->stats
.tx_packets
;
1425 st
->p
.ppp_oerrors
= ppp
->stats
.tx_errors
;
1426 st
->p
.ppp_obytes
= ppp
->stats
.tx_bytes
;
1429 st
->vj
.vjs_packets
= vj
->sls_o_compressed
+ vj
->sls_o_uncompressed
;
1430 st
->vj
.vjs_compressed
= vj
->sls_o_compressed
;
1431 st
->vj
.vjs_searches
= vj
->sls_o_searches
;
1432 st
->vj
.vjs_misses
= vj
->sls_o_misses
;
1433 st
->vj
.vjs_errorin
= vj
->sls_i_error
;
1434 st
->vj
.vjs_tossed
= vj
->sls_i_tossed
;
1435 st
->vj
.vjs_uncompressedin
= vj
->sls_i_uncompressed
;
1436 st
->vj
.vjs_compressedin
= vj
->sls_i_compressed
;
1440 * Stuff for handling the list of ppp units and for initialization.
1444 * Create a new ppp unit. Fails if it can't allocate memory or
1445 * if there is already a unit with the requested number.
1446 * unit == -1 means allocate a new number.
1449 ppp_create_unit(int unit
, int *retp
)
1452 struct net_device
*dev
;
1453 struct list_head
*list
;
1458 spin_lock(&all_ppp_lock
);
1459 list
= &all_ppp_units
;
1460 while ((list
= list
->next
) != &all_ppp_units
) {
1461 ppp
= list_entry(list
, struct ppp
, list
);
1462 if ((unit
< 0 && ppp
->index
> last_unit
+ 1)
1463 || (unit
>= 0 && unit
< ppp
->index
))
1465 if (unit
== ppp
->index
)
1466 goto out
; /* unit already exists */
1467 last_unit
= ppp
->index
;
1470 unit
= last_unit
+ 1;
1472 /* Create a new ppp structure and link it before `list'. */
1474 ppp
= kmalloc(sizeof(struct ppp
), GFP_KERNEL
);
1477 memset(ppp
, 0, sizeof(struct ppp
));
1478 dev
= kmalloc(sizeof(struct net_device
), GFP_KERNEL
);
1483 memset(dev
, 0, sizeof(struct net_device
));
1486 sprintf(ppp
->name
, "ppp%d", unit
);
1488 skb_queue_head_init(&ppp
->xq
);
1489 skb_queue_head_init(&ppp
->rq
);
1490 init_waitqueue_head(&ppp
->rwait
);
1492 for (i
= 0; i
< NUM_NP
; ++i
)
1493 ppp
->npmode
[i
] = NPMODE_PASS
;
1494 INIT_LIST_HEAD(&ppp
->channels
);
1495 skb_queue_head_init(&ppp
->recv_pending
);
1498 dev
->init
= ppp_net_init
;
1499 dev
->name
= ppp
->name
;
1504 ret
= register_netdevice(dev
);
1507 printk(KERN_ERR
"PPP: couldn't register device (%d)\n", ret
);
1513 list_add(&ppp
->list
, list
->prev
);
1515 spin_unlock(&all_ppp_lock
);
1523 * Remove a reference to a ppp unit, and destroy it if
1524 * the reference count goes to 0.
1526 static void ppp_release_unit(struct ppp
*ppp
)
1528 struct list_head
*list
, *next
;
1531 spin_lock(&all_ppp_lock
);
1532 ref
= --ppp
->refcnt
;
1534 list_del(&ppp
->list
);
1535 spin_unlock(&all_ppp_lock
);
1539 /* Last fd open to this ppp unit is being closed or detached:
1540 mark the interface down, free the ppp unit */
1543 dev_close(ppp
->dev
);
1546 for (list
= ppp
->channels
.next
; list
!= &ppp
->channels
; list
= next
) {
1547 /* forcibly detach this channel */
1548 struct channel
*chan
;
1549 chan
= list_entry(list
, struct channel
, list
);
1550 chan
->chan
->ppp
= 0;
1555 /* Free up resources. */
1556 ppp_ccp_closed(ppp
);
1557 lock_xmit_path(ppp
);
1558 lock_recv_path(ppp
);
1563 free_skbs(&ppp
->xq
);
1564 free_skbs(&ppp
->rq
);
1565 free_skbs(&ppp
->recv_pending
);
1568 unregister_netdevice(ppp
->dev
);
1576 * Locate an existing ppp unit.
1577 * The caller should have locked the all_ppp_lock.
1580 ppp_find_unit(int unit
)
1583 struct list_head
*list
;
1585 list
= &all_ppp_units
;
1586 while ((list
= list
->next
) != &all_ppp_units
) {
1587 ppp
= list_entry(list
, struct ppp
, list
);
1588 if (ppp
->index
== unit
)
1606 cleanup_module(void)
1608 /* should never happen */
1609 if (!list_empty(&all_ppp_units
))
1610 printk(KERN_ERR
"PPP: removing module but units remain!\n");
1611 if (unregister_chrdev(PPP_MAJOR
, "ppp") != 0)
1612 printk(KERN_ERR
"PPP: failed to unregister PPP device\n");