2 * Generic PPP layer for Linux.
4 * Copyright 1999-2000 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 20000417==
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/kmod.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/devfs_fs_kernel.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 <linux/smp_lock.h>
45 #include <net/slhc_vj.h>
46 #include <asm/atomic.h>
48 #define PPP_VERSION "2.4.1"
51 * Network protocols we support.
53 #define NP_IP 0 /* Internet Protocol V4 */
54 #define NP_IPV6 1 /* Internet Protocol V6 */
55 #define NP_IPX 2 /* IPX protocol */
56 #define NP_AT 3 /* Appletalk protocol */
57 #define NUM_NP 4 /* Number of NPs. */
59 #define MPHDRLEN 6 /* multilink protocol header length */
60 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
61 #define MIN_FRAG_SIZE 64
64 * An instance of /dev/ppp can be associated with either a ppp
65 * interface unit or a ppp channel. In both cases, file->private_data
66 * points to one of these.
72 struct sk_buff_head xq
; /* pppd transmit queue */
73 struct sk_buff_head rq
; /* receive queue for pppd */
74 wait_queue_head_t rwait
; /* for poll on reading /dev/ppp */
75 atomic_t refcnt
; /* # refs (incl /dev/ppp attached) */
76 int hdrlen
; /* space to leave for headers */
77 struct list_head list
; /* link in all_* list */
78 int index
; /* interface unit / channel number */
81 #define PF_TO_X(pf, X) ((X *)((char *)(pf)-(unsigned long)(&((X *)0)->file)))
83 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
84 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
86 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
89 * Data structure describing one ppp unit.
90 * A ppp unit corresponds to a ppp network interface device
91 * and represents a multilink bundle.
92 * It can have 0 or more ppp channels connected to it.
95 struct ppp_file file
; /* stuff for read/write/poll */
96 struct list_head channels
; /* list of attached channels */
97 int n_channels
; /* how many channels are attached */
98 spinlock_t rlock
; /* lock for receive side */
99 spinlock_t wlock
; /* lock for transmit side */
100 int mru
; /* max receive unit */
101 unsigned int flags
; /* control bits */
102 unsigned int xstate
; /* transmit state bits */
103 unsigned int rstate
; /* receive state bits */
104 int debug
; /* debug flags */
105 struct slcompress
*vj
; /* state for VJ header compression */
106 enum NPmode npmode
[NUM_NP
]; /* what to do with each net proto */
107 struct sk_buff
*xmit_pending
; /* a packet ready to go out */
108 struct compressor
*xcomp
; /* transmit packet compressor */
109 void *xc_state
; /* its internal state */
110 struct compressor
*rcomp
; /* receive decompressor */
111 void *rc_state
; /* its internal state */
112 unsigned long last_xmit
; /* jiffies when last pkt sent */
113 unsigned long last_recv
; /* jiffies when last pkt rcvd */
114 struct net_device
*dev
; /* network interface device */
115 #ifdef CONFIG_PPP_MULTILINK
116 int nxchan
; /* next channel to send something on */
117 u32 nxseq
; /* next sequence number to send */
118 int mrru
; /* MP: max reconst. receive unit */
119 u32 nextseq
; /* MP: seq no of next packet */
120 u32 minseq
; /* MP: min of most recent seqnos */
121 struct sk_buff_head mrq
; /* MP: receive reconstruction queue */
122 #endif /* CONFIG_PPP_MULTILINK */
123 struct net_device_stats stats
; /* statistics */
127 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
128 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
129 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
130 * Bits in xstate: SC_COMP_RUN
132 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
133 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
134 |SC_COMP_TCP|SC_REJ_COMP_TCP)
137 * Private data structure for each channel.
138 * This includes the data structure used for multilink.
141 struct ppp_file file
; /* stuff for read/write/poll */
142 struct ppp_channel
*chan
; /* public channel data structure */
143 spinlock_t downl
; /* protects `chan', file.xq dequeue */
144 struct ppp
*ppp
; /* ppp unit we're connected to */
145 struct list_head clist
; /* link in list of channels per unit */
146 rwlock_t upl
; /* protects `ppp' and `ulist' */
147 #ifdef CONFIG_PPP_MULTILINK
148 u8 avail
; /* flag used in multilink stuff */
149 u8 had_frag
; /* >= 1 fragments have been sent */
150 u32 lastseq
; /* MP: last sequence # received */
151 #endif /* CONFIG_PPP_MULTILINK */
155 * SMP locking issues:
156 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
157 * list and the ppp.n_channels field, you need to take both locks
158 * before you modify them.
159 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
164 * all_ppp_lock protects the all_ppp_units.
165 * It also ensures that finding a ppp unit in the all_ppp_units list
166 * and updating its file.refcnt field is atomic.
168 static spinlock_t all_ppp_lock
= SPIN_LOCK_UNLOCKED
;
169 static LIST_HEAD(all_ppp_units
);
172 * all_channels_lock protects all_channels and last_channel_index,
173 * and the atomicity of find a channel and updating its file.refcnt
176 static spinlock_t all_channels_lock
= SPIN_LOCK_UNLOCKED
;
177 static LIST_HEAD(all_channels
);
178 static int last_channel_index
;
180 /* Get the PPP protocol number from a skb */
181 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
183 /* We limit the length of ppp->file.rq to this (arbitrary) value */
184 #define PPP_MAX_RQLEN 32
187 * Maximum number of multilink fragments queued up.
188 * This has to be large enough to cope with the maximum latency of
189 * the slowest channel relative to the others. Strictly it should
190 * depend on the number of channels and their characteristics.
192 #define PPP_MP_MAX_QLEN 128
194 /* Multilink header bits. */
195 #define B 0x80 /* this fragment begins a packet */
196 #define E 0x40 /* this fragment ends a packet */
198 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
199 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
200 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
203 static ssize_t
ppp_file_read(struct ppp_file
*pf
, struct file
*file
,
204 char *buf
, size_t count
);
205 static ssize_t
ppp_file_write(struct ppp_file
*pf
, const char *buf
,
207 static int ppp_unattached_ioctl(struct ppp_file
*pf
, struct file
*file
,
208 unsigned int cmd
, unsigned long arg
);
209 static void ppp_xmit_process(struct ppp
*ppp
);
210 static void ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
211 static void ppp_push(struct ppp
*ppp
);
212 static void ppp_channel_push(struct channel
*pch
);
213 static void ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
,
214 struct channel
*pch
);
215 static void ppp_receive_error(struct ppp
*ppp
);
216 static void ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
217 static struct sk_buff
*ppp_decompress_frame(struct ppp
*ppp
,
218 struct sk_buff
*skb
);
219 #ifdef CONFIG_PPP_MULTILINK
220 static void ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
,
221 struct channel
*pch
);
222 static void ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
);
223 static struct sk_buff
*ppp_mp_reconstruct(struct ppp
*ppp
);
224 static int ppp_mp_explode(struct ppp
*ppp
, struct sk_buff
*skb
);
225 #endif /* CONFIG_PPP_MULTILINK */
226 static int ppp_set_compress(struct ppp
*ppp
, unsigned long arg
);
227 static void ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
);
228 static void ppp_ccp_closed(struct ppp
*ppp
);
229 static struct compressor
*find_compressor(int type
);
230 static void ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
);
231 static struct ppp
*ppp_create_interface(int unit
, int *retp
);
232 static void init_ppp_file(struct ppp_file
*pf
, int kind
);
233 static void ppp_destroy_interface(struct ppp
*ppp
);
234 static struct ppp
*ppp_find_unit(int unit
);
235 static struct channel
*ppp_find_channel(int unit
);
236 static int ppp_connect_channel(struct channel
*pch
, int unit
);
237 static int ppp_disconnect_channel(struct channel
*pch
);
238 static void ppp_destroy_channel(struct channel
*pch
);
240 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
241 static inline int proto_to_npindex(int proto
)
256 /* Translates an NP index into a PPP protocol number */
257 static const int npindex_to_proto
[NUM_NP
] = {
264 /* Translates an ethertype into an NP index */
265 static inline int ethertype_to_npindex(int ethertype
)
281 /* Translates an NP index into an ethertype */
282 static const int npindex_to_ethertype
[NUM_NP
] = {
292 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
293 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
294 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
295 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
296 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
297 ppp_recv_lock(ppp); } while (0)
298 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
299 ppp_xmit_unlock(ppp); } while (0)
303 * /dev/ppp device routines.
304 * The /dev/ppp device is used by pppd to control the ppp unit.
305 * It supports the read, write, ioctl and poll functions.
306 * Open instances of /dev/ppp can be in one of three states:
307 * unattached, attached to a ppp unit, or attached to a ppp channel.
309 static int ppp_open(struct inode
*inode
, struct file
*file
)
312 * This could (should?) be enforced by the permissions on /dev/ppp.
314 if (!capable(CAP_NET_ADMIN
))
319 static int ppp_release(struct inode
*inode
, struct file
*file
)
321 struct ppp_file
*pf
= (struct ppp_file
*) file
->private_data
;
325 file
->private_data
= 0;
326 if (atomic_dec_and_test(&pf
->refcnt
)) {
329 ppp_destroy_interface(PF_TO_PPP(pf
));
332 ppp_destroy_channel(PF_TO_CHANNEL(pf
));
341 static ssize_t
ppp_read(struct file
*file
, char *buf
,
342 size_t count
, loff_t
*ppos
)
344 struct ppp_file
*pf
= (struct ppp_file
*) file
->private_data
;
346 return ppp_file_read(pf
, file
, buf
, count
);
349 static ssize_t
ppp_file_read(struct ppp_file
*pf
, struct file
*file
,
350 char *buf
, size_t count
)
352 DECLARE_WAITQUEUE(wait
, current
);
354 struct sk_buff
*skb
= 0;
358 goto out
; /* not currently attached */
360 add_wait_queue(&pf
->rwait
, &wait
);
361 current
->state
= TASK_INTERRUPTIBLE
;
363 skb
= skb_dequeue(&pf
->rq
);
367 if (pf
->kind
== CHANNEL
&& PF_TO_CHANNEL(pf
)->chan
== 0)
370 if (file
->f_flags
& O_NONBLOCK
)
373 if (signal_pending(current
))
377 current
->state
= TASK_RUNNING
;
378 remove_wait_queue(&pf
->rwait
, &wait
);
384 if (skb
->len
> count
)
387 if (copy_to_user(buf
, skb
->data
, skb
->len
))
397 static ssize_t
ppp_write(struct file
*file
, const char *buf
,
398 size_t count
, loff_t
*ppos
)
400 struct ppp_file
*pf
= (struct ppp_file
*) file
->private_data
;
402 return ppp_file_write(pf
, buf
, count
);
405 static ssize_t
ppp_file_write(struct ppp_file
*pf
, const char *buf
,
416 skb
= alloc_skb(count
+ pf
->hdrlen
, GFP_KERNEL
);
419 skb_reserve(skb
, pf
->hdrlen
);
421 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
426 skb_queue_tail(&pf
->xq
, skb
);
430 ppp_xmit_process(PF_TO_PPP(pf
));
433 ppp_channel_push(PF_TO_CHANNEL(pf
));
443 /* No kernel lock - fine */
444 static unsigned int ppp_poll(struct file
*file
, poll_table
*wait
)
446 struct ppp_file
*pf
= (struct ppp_file
*) file
->private_data
;
451 poll_wait(file
, &pf
->rwait
, wait
);
452 mask
= POLLOUT
| POLLWRNORM
;
453 if (skb_peek(&pf
->rq
) != 0)
454 mask
|= POLLIN
| POLLRDNORM
;
455 if (pf
->kind
== CHANNEL
) {
456 struct channel
*pch
= PF_TO_CHANNEL(pf
);
463 static int ppp_ioctl(struct inode
*inode
, struct file
*file
,
464 unsigned int cmd
, unsigned long arg
)
466 struct ppp_file
*pf
= (struct ppp_file
*) file
->private_data
;
468 int err
= -EFAULT
, val
, val2
, i
;
469 struct ppp_idle idle
;
472 struct slcompress
*vj
;
475 return ppp_unattached_ioctl(pf
, file
, cmd
, arg
);
477 if (pf
->kind
== CHANNEL
) {
478 struct channel
*pch
= PF_TO_CHANNEL(pf
);
479 struct ppp_channel
*chan
;
483 if (get_user(unit
, (int *) arg
))
485 err
= ppp_connect_channel(pch
, unit
);
489 err
= ppp_disconnect_channel(pch
);
493 file
->private_data
= 0;
494 if (atomic_dec_and_test(&pf
->refcnt
))
495 ppp_destroy_channel(pch
);
500 spin_lock_bh(&pch
->downl
);
503 if (chan
&& chan
->ops
->ioctl
)
504 err
= chan
->ops
->ioctl(chan
, cmd
, arg
);
505 spin_unlock_bh(&pch
->downl
);
510 if (pf
->kind
!= INTERFACE
) {
512 printk(KERN_ERR
"PPP: not interface or channel??\n");
519 file
->private_data
= 0;
520 if (atomic_dec_and_test(&pf
->refcnt
))
521 ppp_destroy_interface(ppp
);
526 if (get_user(val
, (int *) arg
))
533 if (get_user(val
, (int *) arg
))
536 if (ppp
->flags
& ~val
& SC_CCP_OPEN
)
538 ppp
->flags
= val
& SC_FLAG_BITS
;
544 val
= ppp
->flags
| ppp
->xstate
| ppp
->rstate
;
545 if (put_user(val
, (int *) arg
))
550 case PPPIOCSCOMPRESS
:
551 err
= ppp_set_compress(ppp
, arg
);
555 if (put_user(ppp
->file
.index
, (int *) arg
))
561 if (get_user(val
, (int *) arg
))
568 if (put_user(ppp
->debug
, (int *) arg
))
574 idle
.xmit_idle
= (jiffies
- ppp
->last_xmit
) / HZ
;
575 idle
.recv_idle
= (jiffies
- ppp
->last_recv
) / HZ
;
576 if (copy_to_user((void *) arg
, &idle
, sizeof(idle
)))
582 if (get_user(val
, (int *) arg
))
585 if ((val
>> 16) != 0) {
589 vj
= slhc_init(val2
+1, val
+1);
591 printk(KERN_ERR
"PPP: no memory (VJ compressor)\n");
605 if (copy_from_user(&npi
, (void *) arg
, sizeof(npi
)))
607 err
= proto_to_npindex(npi
.protocol
);
611 if (cmd
== PPPIOCGNPMODE
) {
613 npi
.mode
= ppp
->npmode
[i
];
614 if (copy_to_user((void *) arg
, &npi
, sizeof(npi
)))
617 ppp
->npmode
[i
] = npi
.mode
;
618 /* we may be able to transmit more packets now (??) */
619 netif_wake_queue(ppp
->dev
);
624 #ifdef CONFIG_PPP_MULTILINK
626 if (get_user(val
, (int *) arg
))
630 ppp_recv_unlock(ppp
);
633 #endif /* CONFIG_PPP_MULTILINK */
641 static int ppp_unattached_ioctl(struct ppp_file
*pf
, struct file
*file
,
642 unsigned int cmd
, unsigned long arg
)
644 int unit
, err
= -EFAULT
;
646 struct channel
*chan
;
650 /* Create a new ppp unit */
651 if (get_user(unit
, (int *) arg
))
653 ppp
= ppp_create_interface(unit
, &err
);
656 file
->private_data
= &ppp
->file
;
658 if (put_user(ppp
->file
.index
, (int *) arg
))
664 /* Attach to an existing ppp unit */
665 if (get_user(unit
, (int *) arg
))
667 spin_lock(&all_ppp_lock
);
668 ppp
= ppp_find_unit(unit
);
670 atomic_inc(&ppp
->file
.refcnt
);
671 spin_unlock(&all_ppp_lock
);
675 file
->private_data
= &ppp
->file
;
680 if (get_user(unit
, (int *) arg
))
682 spin_lock_bh(&all_channels_lock
);
683 chan
= ppp_find_channel(unit
);
685 atomic_inc(&chan
->file
.refcnt
);
686 spin_unlock_bh(&all_channels_lock
);
690 file
->private_data
= &chan
->file
;
700 static struct file_operations ppp_device_fops
= {
710 #define PPP_MAJOR 108
712 static devfs_handle_t devfs_handle
;
714 /* Called at boot time if ppp is compiled into the kernel,
715 or at module load time (from init_module) if compiled as a module. */
716 int __init
ppp_init(void)
720 printk(KERN_INFO
"PPP generic driver version " PPP_VERSION
"\n");
721 err
= devfs_register_chrdev(PPP_MAJOR
, "ppp", &ppp_device_fops
);
723 printk(KERN_ERR
"failed to register PPP device (%d)\n", err
);
724 devfs_handle
= devfs_register(NULL
, "ppp", DEVFS_FL_DEFAULT
,
726 S_IFCHR
| S_IRUSR
| S_IWUSR
,
727 &ppp_device_fops
, NULL
);
733 * Network interface unit routines.
736 ppp_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
738 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
742 npi
= ethertype_to_npindex(ntohs(skb
->protocol
));
746 /* Drop, accept or reject the packet */
747 switch (ppp
->npmode
[npi
]) {
751 /* it would be nice to have a way to tell the network
752 system to queue this one up for later. */
759 /* Put the 2-byte PPP protocol number on the front,
760 making sure there is room for the address and control fields. */
761 if (skb_headroom(skb
) < PPP_HDRLEN
) {
764 ns
= alloc_skb(skb
->len
+ dev
->hard_header_len
, GFP_ATOMIC
);
767 skb_reserve(ns
, dev
->hard_header_len
);
768 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
772 pp
= skb_push(skb
, 2);
773 proto
= npindex_to_proto
[npi
];
777 netif_stop_queue(dev
);
778 skb_queue_tail(&ppp
->file
.xq
, skb
);
779 ppp_xmit_process(ppp
);
784 ++ppp
->stats
.tx_dropped
;
788 static struct net_device_stats
*
789 ppp_net_stats(struct net_device
*dev
)
791 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
797 ppp_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
799 struct ppp
*ppp
= dev
->priv
;
801 void *addr
= (void *) ifr
->ifr_ifru
.ifru_data
;
802 struct ppp_stats stats
;
803 struct ppp_comp_stats cstats
;
808 ppp_get_stats(ppp
, &stats
);
809 if (copy_to_user(addr
, &stats
, sizeof(stats
)))
815 memset(&cstats
, 0, sizeof(cstats
));
816 if (ppp
->xc_state
!= 0)
817 ppp
->xcomp
->comp_stat(ppp
->xc_state
, &cstats
.c
);
818 if (ppp
->rc_state
!= 0)
819 ppp
->rcomp
->decomp_stat(ppp
->rc_state
, &cstats
.d
);
820 if (copy_to_user(addr
, &cstats
, sizeof(cstats
)))
827 if (copy_to_user(addr
, vers
, strlen(vers
) + 1))
840 ppp_net_init(struct net_device
*dev
)
842 dev
->hard_header_len
= PPP_HDRLEN
;
844 dev
->hard_start_xmit
= ppp_start_xmit
;
845 dev
->get_stats
= ppp_net_stats
;
846 dev
->do_ioctl
= ppp_net_ioctl
;
848 dev
->tx_queue_len
= 3;
849 dev
->type
= ARPHRD_PPP
;
850 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
852 dev_init_buffers(dev
);
857 * Transmit-side routines.
861 * Called to do any work queued up on the transmit side
862 * that can now be done.
865 ppp_xmit_process(struct ppp
*ppp
)
871 while (ppp
->xmit_pending
== 0
872 && (skb
= skb_dequeue(&ppp
->file
.xq
)) != 0)
873 ppp_send_frame(ppp
, skb
);
874 /* If there's no work left to do, tell the core net
875 code that we can accept some more. */
876 if (ppp
->xmit_pending
== 0 && skb_peek(&ppp
->file
.xq
) == 0
878 netif_wake_queue(ppp
->dev
);
879 ppp_xmit_unlock(ppp
);
883 * Compress and send a frame.
884 * The caller should have locked the xmit path,
885 * and xmit_pending should be 0.
888 ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
890 int proto
= PPP_PROTO(skb
);
891 struct sk_buff
*new_skb
;
895 ++ppp
->stats
.tx_packets
;
896 ppp
->stats
.tx_bytes
+= skb
->len
- 2;
900 if (ppp
->vj
== 0 || (ppp
->flags
& SC_COMP_TCP
) == 0)
902 /* try to do VJ TCP header compression */
903 new_skb
= alloc_skb(skb
->len
+ ppp
->dev
->hard_header_len
- 2,
906 printk(KERN_ERR
"PPP: no memory (VJ comp pkt)\n");
909 skb_reserve(new_skb
, ppp
->dev
->hard_header_len
- 2);
911 len
= slhc_compress(ppp
->vj
, cp
, skb
->len
- 2,
912 new_skb
->data
+ 2, &cp
,
913 !(ppp
->flags
& SC_NO_TCP_CCID
));
914 if (cp
== skb
->data
+ 2) {
915 /* didn't compress */
918 if (cp
[0] & SL_TYPE_COMPRESSED_TCP
) {
919 proto
= PPP_VJC_COMP
;
920 cp
[0] &= ~SL_TYPE_COMPRESSED_TCP
;
922 proto
= PPP_VJC_UNCOMP
;
923 cp
[0] = skb
->data
[2];
927 cp
= skb_put(skb
, len
+ 2);
934 /* peek at outbound CCP frames */
935 ppp_ccp_peek(ppp
, skb
, 0);
939 /* try to do packet compression */
940 if ((ppp
->xstate
& SC_COMP_RUN
) && ppp
->xc_state
!= 0
941 && proto
!= PPP_LCP
&& proto
!= PPP_CCP
) {
942 new_skb
= alloc_skb(ppp
->dev
->mtu
+ ppp
->dev
->hard_header_len
,
945 printk(KERN_ERR
"PPP: no memory (comp pkt)\n");
948 if (ppp
->dev
->hard_header_len
> PPP_HDRLEN
)
950 ppp
->dev
->hard_header_len
- PPP_HDRLEN
);
952 /* compressor still expects A/C bytes in hdr */
953 len
= ppp
->xcomp
->compress(ppp
->xc_state
, skb
->data
- 2,
954 new_skb
->data
, skb
->len
+ 2,
955 ppp
->dev
->mtu
+ PPP_HDRLEN
);
956 if (len
> 0 && (ppp
->flags
& SC_CCP_UP
)) {
960 skb_pull(skb
, 2); /* pull off A/C bytes */
962 /* didn't compress, or CCP not up yet */
967 /* for data packets, record the time */
969 ppp
->last_xmit
= jiffies
;
972 * If we are waiting for traffic (demand dialling),
973 * queue it up for pppd to receive.
975 if (ppp
->flags
& SC_LOOP_TRAFFIC
) {
976 if (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
)
978 skb_queue_tail(&ppp
->file
.rq
, skb
);
979 wake_up_interruptible(&ppp
->file
.rwait
);
983 ppp
->xmit_pending
= skb
;
989 ++ppp
->stats
.tx_errors
;
993 * Try to send the frame in xmit_pending.
994 * The caller should have the xmit path locked.
997 ppp_push(struct ppp
*ppp
)
999 struct list_head
*list
;
1000 struct channel
*pch
;
1001 struct sk_buff
*skb
= ppp
->xmit_pending
;
1006 list
= &ppp
->channels
;
1007 if (list_empty(list
)) {
1008 /* nowhere to send the packet, just drop it */
1009 ppp
->xmit_pending
= 0;
1014 if ((ppp
->flags
& SC_MULTILINK
) == 0) {
1015 /* not doing multilink: send it down the first channel */
1017 pch
= list_entry(list
, struct channel
, clist
);
1019 spin_lock_bh(&pch
->downl
);
1021 if (pch
->chan
->ops
->start_xmit(pch
->chan
, skb
))
1022 ppp
->xmit_pending
= 0;
1024 /* channel got unregistered */
1026 ppp
->xmit_pending
= 0;
1028 spin_unlock_bh(&pch
->downl
);
1032 #ifdef CONFIG_PPP_MULTILINK
1033 /* Multilink: fragment the packet over as many links
1034 as can take the packet at the moment. */
1035 if (!ppp_mp_explode(ppp
, skb
))
1037 #endif /* CONFIG_PPP_MULTILINK */
1039 ppp
->xmit_pending
= 0;
1043 #ifdef CONFIG_PPP_MULTILINK
1045 * Divide a packet to be transmitted into fragments and
1046 * send them out the individual links.
1048 static int ppp_mp_explode(struct ppp
*ppp
, struct sk_buff
*skb
)
1050 int nch
, len
, fragsize
;
1051 int i
, bits
, hdrlen
, mtu
;
1053 unsigned char *p
, *q
;
1054 struct list_head
*list
;
1055 struct channel
*pch
;
1056 struct sk_buff
*frag
;
1057 struct ppp_channel
*chan
;
1060 hdrlen
= (ppp
->flags
& SC_MP_XSHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1061 list
= &ppp
->channels
;
1062 while ((list
= list
->next
) != &ppp
->channels
) {
1063 pch
= list_entry(list
, struct channel
, clist
);
1064 nch
+= pch
->avail
= (skb_queue_len(&pch
->file
.xq
) == 0);
1066 * If a channel hasn't had a fragment yet, it has to get
1067 * one before we send any fragments on later channels.
1068 * If it can't take a fragment now, don't give any
1069 * to subsequent channels.
1071 if (!pch
->had_frag
&& !pch
->avail
) {
1072 while ((list
= list
->next
) != &ppp
->channels
) {
1073 pch
= list_entry(list
, struct channel
, clist
);
1080 return 0; /* can't take now, leave it in xmit_pending */
1082 /* Do protocol field compression (XXX this should be optional) */
1090 /* decide on fragment size */
1093 int maxch
= ROUNDUP(len
, MIN_FRAG_SIZE
);
1096 fragsize
= ROUNDUP(fragsize
, nch
);
1099 /* skip to the channel after the one we last used
1100 and start at that one */
1101 for (i
= 0; i
< ppp
->nxchan
; ++i
) {
1103 if (list
== &ppp
->channels
) {
1109 /* create a fragment for each channel */
1113 if (list
== &ppp
->channels
) {
1117 pch
= list_entry(list
, struct channel
, clist
);
1122 /* check the channel's mtu and whether it is still attached. */
1123 spin_lock_bh(&pch
->downl
);
1124 if (pch
->chan
== 0 || (mtu
= pch
->chan
->mtu
) < hdrlen
) {
1125 /* can't use this channel */
1126 spin_unlock_bh(&pch
->downl
);
1134 * We have to create multiple fragments for this channel
1135 * if fragsize is greater than the channel's mtu.
1139 for (flen
= fragsize
; flen
> 0; flen
-= fnb
) {
1141 if (fnb
> mtu
+ 2 - hdrlen
)
1142 fnb
= mtu
+ 2 - hdrlen
;
1145 frag
= alloc_skb(fnb
+ hdrlen
, GFP_ATOMIC
);
1148 q
= skb_put(frag
, fnb
+ hdrlen
);
1149 /* make the MP header */
1152 if (ppp
->flags
& SC_MP_XSHORTSEQ
) {
1153 q
[2] = bits
+ ((ppp
->nxseq
>> 8) & 0xf);
1157 q
[3] = ppp
->nxseq
>> 16;
1158 q
[4] = ppp
->nxseq
>> 8;
1162 /* copy the data in */
1163 memcpy(q
+ hdrlen
, p
, fnb
);
1165 /* try to send it down the channel */
1167 if (!chan
->ops
->start_xmit(chan
, frag
))
1168 skb_queue_tail(&pch
->file
.xq
, frag
);
1175 spin_unlock_bh(&pch
->downl
);
1182 spin_unlock_bh(&pch
->downl
);
1184 printk(KERN_ERR
"PPP: no memory (fragment)\n");
1185 ++ppp
->stats
.tx_errors
;
1187 return 1; /* abandon the frame */
1189 #endif /* CONFIG_PPP_MULTILINK */
1192 * Try to send data out on a channel.
1195 ppp_channel_push(struct channel
*pch
)
1197 struct sk_buff
*skb
;
1200 spin_lock_bh(&pch
->downl
);
1201 if (pch
->chan
!= 0) {
1202 while (skb_queue_len(&pch
->file
.xq
) > 0) {
1203 skb
= skb_dequeue(&pch
->file
.xq
);
1204 if (!pch
->chan
->ops
->start_xmit(pch
->chan
, skb
)) {
1205 /* put the packet back and try again later */
1206 skb_queue_head(&pch
->file
.xq
, skb
);
1211 /* channel got deregistered */
1212 skb_queue_purge(&pch
->file
.xq
);
1214 spin_unlock_bh(&pch
->downl
);
1215 /* see if there is anything from the attached unit to be sent */
1216 if (skb_queue_len(&pch
->file
.xq
) == 0) {
1217 read_lock_bh(&pch
->upl
);
1220 ppp_xmit_process(ppp
);
1221 read_unlock_bh(&pch
->upl
);
1226 * Receive-side routines.
1229 /* misuse a few fields of the skb for MP reconstruction */
1230 #define sequence priority
1231 #define BEbits cb[0]
1234 ppp_do_recv(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1237 /* ppp->dev == 0 means interface is closing down */
1239 ppp_receive_frame(ppp
, skb
, pch
);
1242 ppp_recv_unlock(ppp
);
1246 ppp_input(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1248 struct channel
*pch
= chan
->ppp
;
1251 if (pch
== 0 || skb
->len
== 0) {
1256 proto
= PPP_PROTO(skb
);
1257 read_lock_bh(&pch
->upl
);
1258 if (pch
->ppp
== 0 || proto
>= 0xc000 || proto
== PPP_CCPFRAG
) {
1259 /* put it on the channel queue */
1260 skb_queue_tail(&pch
->file
.rq
, skb
);
1261 /* drop old frames if queue too long */
1262 while (pch
->file
.rq
.qlen
> PPP_MAX_RQLEN
1263 && (skb
= skb_dequeue(&pch
->file
.rq
)) != 0)
1265 wake_up_interruptible(&pch
->file
.rwait
);
1267 ppp_do_recv(pch
->ppp
, skb
, pch
);
1269 read_unlock_bh(&pch
->upl
);
1272 /* Put a 0-length skb in the receive queue as an error indication */
1274 ppp_input_error(struct ppp_channel
*chan
, int code
)
1276 struct channel
*pch
= chan
->ppp
;
1277 struct sk_buff
*skb
;
1282 read_lock_bh(&pch
->upl
);
1283 if (pch
->ppp
!= 0) {
1284 skb
= alloc_skb(0, GFP_ATOMIC
);
1286 skb
->len
= 0; /* probably unnecessary */
1288 ppp_do_recv(pch
->ppp
, skb
, pch
);
1291 read_unlock_bh(&pch
->upl
);
1295 * We come in here to process a received frame.
1296 * The receive side of the ppp unit is locked.
1299 ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1301 if (skb
->len
>= 2) {
1302 #ifdef CONFIG_PPP_MULTILINK
1303 /* XXX do channel-level decompression here */
1304 if (PPP_PROTO(skb
) == PPP_MP
)
1305 ppp_receive_mp_frame(ppp
, skb
, pch
);
1307 #endif /* CONFIG_PPP_MULTILINK */
1308 ppp_receive_nonmp_frame(ppp
, skb
);
1313 /* note: a 0-length skb is used as an error indication */
1314 ++ppp
->stats
.rx_length_errors
;
1317 ppp_receive_error(ppp
);
1321 ppp_receive_error(struct ppp
*ppp
)
1323 ++ppp
->stats
.rx_errors
;
1329 ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1332 int proto
, len
, npi
;
1335 * Decompress the frame, if compressed.
1336 * Note that some decompressors need to see uncompressed frames
1337 * that come in as well as compressed frames.
1339 if (ppp
->rc_state
!= 0 && (ppp
->rstate
& SC_DECOMP_RUN
)
1340 && (ppp
->rstate
& (SC_DC_FERROR
| SC_DC_ERROR
)) == 0)
1341 skb
= ppp_decompress_frame(ppp
, skb
);
1343 proto
= PPP_PROTO(skb
);
1346 /* decompress VJ compressed packets */
1347 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
1349 if (skb_tailroom(skb
) < 124) {
1350 /* copy to a new sk_buff with more tailroom */
1351 ns
= dev_alloc_skb(skb
->len
+ 128);
1353 printk(KERN_ERR
"PPP: no memory (VJ decomp)\n");
1357 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
1361 len
= slhc_uncompress(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2);
1363 printk(KERN_DEBUG
"PPP: VJ decompression error\n");
1368 skb_put(skb
, len
- skb
->len
);
1369 else if (len
< skb
->len
)
1374 case PPP_VJC_UNCOMP
:
1375 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
1377 if (slhc_remember(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2) <= 0) {
1378 printk(KERN_ERR
"PPP: VJ uncompressed error\n");
1385 ppp_ccp_peek(ppp
, skb
, 1);
1389 ++ppp
->stats
.rx_packets
;
1390 ppp
->stats
.rx_bytes
+= skb
->len
- 2;
1392 npi
= proto_to_npindex(proto
);
1394 /* control or unknown frame - pass it to pppd */
1395 skb_queue_tail(&ppp
->file
.rq
, skb
);
1396 /* limit queue length by dropping old frames */
1397 while (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
1398 && (skb
= skb_dequeue(&ppp
->file
.rq
)) != 0)
1400 /* wake up any process polling or blocking on read */
1401 wake_up_interruptible(&ppp
->file
.rwait
);
1404 /* network protocol frame - give it to the kernel */
1405 ppp
->last_recv
= jiffies
;
1406 if ((ppp
->dev
->flags
& IFF_UP
) == 0
1407 || ppp
->npmode
[npi
] != NPMODE_PASS
) {
1410 skb_pull(skb
, 2); /* chop off protocol */
1411 skb
->dev
= ppp
->dev
;
1412 skb
->protocol
= htons(npindex_to_ethertype
[npi
]);
1413 skb
->mac
.raw
= skb
->data
;
1421 ppp_receive_error(ppp
);
1424 static struct sk_buff
*
1425 ppp_decompress_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1427 int proto
= PPP_PROTO(skb
);
1431 if (proto
== PPP_COMP
) {
1432 ns
= dev_alloc_skb(ppp
->mru
+ PPP_HDRLEN
);
1434 printk(KERN_ERR
"ppp_decompress_frame: no memory\n");
1437 /* the decompressor still expects the A/C bytes in the hdr */
1438 len
= ppp
->rcomp
->decompress(ppp
->rc_state
, skb
->data
- 2,
1439 skb
->len
+ 2, ns
->data
, ppp
->mru
+ PPP_HDRLEN
);
1441 /* Pass the compressed frame to pppd as an
1442 error indication. */
1443 if (len
== DECOMP_FATALERROR
)
1444 ppp
->rstate
|= SC_DC_FERROR
;
1451 skb_pull(skb
, 2); /* pull off the A/C bytes */
1454 /* Uncompressed frame - pass to decompressor so it
1455 can update its dictionary if necessary. */
1456 if (ppp
->rcomp
->incomp
)
1457 ppp
->rcomp
->incomp(ppp
->rc_state
, skb
->data
- 2,
1464 ppp
->rstate
|= SC_DC_ERROR
;
1465 ppp_receive_error(ppp
);
1469 #ifdef CONFIG_PPP_MULTILINK
1471 * Receive a multilink frame.
1472 * We put it on the reconstruction queue and then pull off
1473 * as many completed frames as we can.
1476 ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1479 struct list_head
*l
;
1480 int mphdrlen
= (ppp
->flags
& SC_MP_SHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1482 if (skb
->len
< mphdrlen
+ 1 || ppp
->mrru
== 0)
1483 goto err
; /* no good, throw it away */
1485 /* Decode sequence number and begin/end bits */
1486 if (ppp
->flags
& SC_MP_SHORTSEQ
) {
1487 seq
= ((skb
->data
[2] & 0x0f) << 8) | skb
->data
[3];
1490 seq
= (skb
->data
[3] << 16) | (skb
->data
[4] << 8)| skb
->data
[5];
1493 skb
->BEbits
= skb
->data
[2];
1494 skb_pull(skb
, mphdrlen
); /* pull off PPP and MP headers */
1497 * Do protocol ID decompression on the first fragment of each packet.
1499 if ((skb
->BEbits
& B
) && (skb
->data
[0] & 1))
1500 *skb_push(skb
, 1) = 0;
1503 * Expand sequence number to 32 bits, making it as close
1504 * as possible to ppp->minseq.
1506 seq
|= ppp
->minseq
& ~mask
;
1507 if ((int)(ppp
->minseq
- seq
) > (int)(mask
>> 1))
1509 else if ((int)(seq
- ppp
->minseq
) > (int)(mask
>> 1))
1510 seq
-= mask
+ 1; /* should never happen */
1511 skb
->sequence
= seq
;
1515 * If this packet comes before the next one we were expecting,
1518 if (seq_before(seq
, ppp
->nextseq
)) {
1520 ++ppp
->stats
.rx_dropped
;
1521 ppp_receive_error(ppp
);
1526 * Reevaluate minseq, the minimum over all channels of the
1527 * last sequence number received on each channel. Because of
1528 * the increasing sequence number rule, we know that any fragment
1529 * before `minseq' which hasn't arrived is never going to arrive.
1530 * The list of channels can't change because we have the receive
1531 * side of the ppp unit locked.
1533 for (l
= ppp
->channels
.next
; l
!= &ppp
->channels
; l
= l
->next
) {
1534 struct channel
*ch
= list_entry(l
, struct channel
, clist
);
1535 if (seq_before(ch
->lastseq
, seq
))
1538 if (seq_before(ppp
->minseq
, seq
))
1541 /* Put the fragment on the reconstruction queue */
1542 ppp_mp_insert(ppp
, skb
);
1544 /* If the queue is getting long, don't wait any longer for packets
1545 before the start of the queue. */
1546 if (skb_queue_len(&ppp
->mrq
) >= PPP_MP_MAX_QLEN
1547 && seq_before(ppp
->minseq
, ppp
->mrq
.next
->sequence
))
1548 ppp
->minseq
= ppp
->mrq
.next
->sequence
;
1550 /* Pull completed packets off the queue and receive them. */
1551 while ((skb
= ppp_mp_reconstruct(ppp
)) != 0)
1552 ppp_receive_nonmp_frame(ppp
, skb
);
1558 ppp_receive_error(ppp
);
1562 * Insert a fragment on the MP reconstruction queue.
1563 * The queue is ordered by increasing sequence number.
1566 ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
)
1569 struct sk_buff_head
*list
= &ppp
->mrq
;
1570 u32 seq
= skb
->sequence
;
1572 /* N.B. we don't need to lock the list lock because we have the
1573 ppp unit receive-side lock. */
1574 for (p
= list
->next
; p
!= (struct sk_buff
*)list
; p
= p
->next
)
1575 if (seq_before(seq
, p
->sequence
))
1577 __skb_insert(skb
, p
->prev
, p
, list
);
1581 * Reconstruct a packet from the MP fragment queue.
1582 * We go through increasing sequence numbers until we find a
1583 * complete packet, or we get to the sequence number for a fragment
1584 * which hasn't arrived but might still do so.
1587 ppp_mp_reconstruct(struct ppp
*ppp
)
1589 u32 seq
= ppp
->nextseq
;
1590 u32 minseq
= ppp
->minseq
;
1591 struct sk_buff_head
*list
= &ppp
->mrq
;
1592 struct sk_buff
*p
, *next
;
1593 struct sk_buff
*head
, *tail
;
1594 struct sk_buff
*skb
= NULL
;
1595 int lost
= 0, len
= 0;
1597 if (ppp
->mrru
== 0) /* do nothing until mrru is set */
1601 for (p
= head
; p
!= (struct sk_buff
*) list
; p
= next
) {
1603 if (seq_before(p
->sequence
, seq
)) {
1604 /* this can't happen, anyway ignore the skb */
1605 printk(KERN_ERR
"ppp_mp_reconstruct bad seq %u < %u\n",
1610 if (p
->sequence
!= seq
) {
1611 /* Fragment `seq' is missing. If it is after
1612 minseq, it might arrive later, so stop here. */
1613 if (seq_after(seq
, minseq
))
1615 /* Fragment `seq' is lost, keep going. */
1617 seq
= seq_before(minseq
, p
->sequence
)?
1618 minseq
+ 1: p
->sequence
;
1624 * At this point we know that all the fragments from
1625 * ppp->nextseq to seq are either present or lost.
1626 * Also, there are no complete packets in the queue
1627 * that have no missing fragments and end before this
1631 /* B bit set indicates this fragment starts a packet */
1632 if (p
->BEbits
& B
) {
1640 /* Got a complete packet yet? */
1641 if (lost
== 0 && (p
->BEbits
& E
) && (head
->BEbits
& B
)) {
1642 if (len
> ppp
->mrru
+ 2) {
1643 ++ppp
->stats
.rx_length_errors
;
1644 printk(KERN_DEBUG
"PPP: reconstructed packet"
1645 " is too long (%d)\n", len
);
1646 } else if (p
== head
) {
1647 /* fragment is complete packet - reuse skb */
1651 } else if ((skb
= dev_alloc_skb(len
)) == NULL
) {
1652 ++ppp
->stats
.rx_missed_errors
;
1653 printk(KERN_DEBUG
"PPP: no memory for "
1654 "reconstructed packet");
1659 ppp
->nextseq
= seq
+ 1;
1663 * If this is the ending fragment of a packet,
1664 * and we haven't found a complete valid packet yet,
1665 * we can discard up to and including this fragment.
1673 /* If we have a complete packet, copy it all into one skb. */
1675 /* If we have discarded any fragments,
1676 signal a receive error. */
1677 if (head
->sequence
!= ppp
->nextseq
) {
1679 printk(KERN_DEBUG
" missed pkts %u..%u\n",
1680 ppp
->nextseq
, head
->sequence
-1);
1681 ++ppp
->stats
.rx_dropped
;
1682 ppp_receive_error(ppp
);
1686 /* copy to a single skb */
1687 for (p
= head
; p
!= tail
->next
; p
= p
->next
)
1688 memcpy(skb_put(skb
, p
->len
), p
->data
, p
->len
);
1689 ppp
->nextseq
= tail
->sequence
+ 1;
1693 /* Discard all the skbuffs that we have copied the data out of
1694 or that we can't use. */
1695 while ((p
= list
->next
) != head
) {
1696 __skb_unlink(p
, list
);
1702 #endif /* CONFIG_PPP_MULTILINK */
1705 * Channel interface.
1709 * Create a new, unattached ppp channel.
1712 ppp_register_channel(struct ppp_channel
*chan
)
1714 struct channel
*pch
;
1716 pch
= kmalloc(sizeof(struct channel
), GFP_ATOMIC
);
1719 memset(pch
, 0, sizeof(struct channel
));
1723 init_ppp_file(&pch
->file
, CHANNEL
);
1724 pch
->file
.hdrlen
= chan
->hdrlen
;
1725 #ifdef CONFIG_PPP_MULTILINK
1727 #endif /* CONFIG_PPP_MULTILINK */
1728 spin_lock_init(&pch
->downl
);
1729 pch
->upl
= RW_LOCK_UNLOCKED
;
1730 spin_lock_bh(&all_channels_lock
);
1731 pch
->file
.index
= ++last_channel_index
;
1732 list_add(&pch
->file
.list
, &all_channels
);
1733 spin_unlock_bh(&all_channels_lock
);
1739 * Return the index of a channel.
1741 int ppp_channel_index(struct ppp_channel
*chan
)
1743 struct channel
*pch
= chan
->ppp
;
1745 return pch
->file
.index
;
1749 * Return the PPP unit number to which a channel is connected.
1751 int ppp_unit_number(struct ppp_channel
*chan
)
1753 struct channel
*pch
= chan
->ppp
;
1757 read_lock_bh(&pch
->upl
);
1759 unit
= pch
->ppp
->file
.index
;
1760 read_unlock_bh(&pch
->upl
);
1766 * Disconnect a channel from the generic layer.
1767 * This can be called from mainline or BH/softirq level.
1770 ppp_unregister_channel(struct ppp_channel
*chan
)
1772 struct channel
*pch
= chan
->ppp
;
1775 return; /* should never happen */
1779 * This ensures that we have returned from any calls into the
1780 * the channel's start_xmit or ioctl routine before we proceed.
1782 spin_lock_bh(&pch
->downl
);
1784 spin_unlock_bh(&pch
->downl
);
1785 ppp_disconnect_channel(pch
);
1786 wake_up_interruptible(&pch
->file
.rwait
);
1787 spin_lock_bh(&all_channels_lock
);
1788 list_del(&pch
->file
.list
);
1789 spin_unlock_bh(&all_channels_lock
);
1790 if (atomic_dec_and_test(&pch
->file
.refcnt
))
1791 ppp_destroy_channel(pch
);
1796 * Callback from a channel when it can accept more to transmit.
1797 * This should be called at BH/softirq level, not interrupt level.
1800 ppp_output_wakeup(struct ppp_channel
*chan
)
1802 struct channel
*pch
= chan
->ppp
;
1806 ppp_channel_push(pch
);
1810 * This is basically temporary compatibility stuff.
1813 ppp_channel_read(struct ppp_channel
*chan
, struct file
*file
,
1814 char *buf
, size_t count
)
1816 struct channel
*pch
= chan
->ppp
;
1820 return ppp_file_read(&pch
->file
, file
, buf
, count
);
1824 ppp_channel_write(struct ppp_channel
*chan
, const char *buf
, size_t count
)
1826 struct channel
*pch
= chan
->ppp
;
1830 return ppp_file_write(&pch
->file
, buf
, count
);
1833 /* No kernel lock - fine */
1835 ppp_channel_poll(struct ppp_channel
*chan
, struct file
*file
, poll_table
*wait
)
1838 struct channel
*pch
= chan
->ppp
;
1840 mask
= POLLOUT
| POLLWRNORM
;
1842 poll_wait(file
, &pch
->file
.rwait
, wait
);
1843 if (skb_peek(&pch
->file
.rq
) != 0)
1844 mask
|= POLLIN
| POLLRDNORM
;
1849 int ppp_channel_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
1852 struct channel
*pch
= chan
->ppp
;
1856 if (!capable(CAP_NET_ADMIN
))
1862 if (get_user(unit
, (int *) arg
))
1864 err
= ppp_connect_channel(pch
, unit
);
1867 err
= ppp_disconnect_channel(pch
);
1874 * Compression control.
1877 /* Process the PPPIOCSCOMPRESS ioctl. */
1879 ppp_set_compress(struct ppp
*ppp
, unsigned long arg
)
1882 struct compressor
*cp
;
1883 struct ppp_option_data data
;
1885 unsigned char ccp_option
[CCP_MAX_OPTION_LENGTH
];
1891 if (copy_from_user(&data
, (void *) arg
, sizeof(data
))
1892 || (data
.length
<= CCP_MAX_OPTION_LENGTH
1893 && copy_from_user(ccp_option
, data
.ptr
, data
.length
)))
1896 if (data
.length
> CCP_MAX_OPTION_LENGTH
1897 || ccp_option
[1] < 2 || ccp_option
[1] > data
.length
)
1900 cp
= find_compressor(ccp_option
[0]);
1903 sprintf(modname
, "ppp-compress-%d", ccp_option
[0]);
1904 request_module(modname
);
1905 cp
= find_compressor(ccp_option
[0]);
1907 #endif /* CONFIG_KMOD */
1912 if (data
.transmit
) {
1914 ppp
->xstate
&= ~SC_COMP_RUN
;
1915 if (ppp
->xc_state
!= 0) {
1916 ppp
->xcomp
->comp_free(ppp
->xc_state
);
1919 ppp_xmit_unlock(ppp
);
1921 state
= cp
->comp_alloc(ccp_option
, data
.length
);
1925 ppp
->xc_state
= state
;
1926 ppp_xmit_unlock(ppp
);
1932 ppp
->rstate
&= ~SC_DECOMP_RUN
;
1933 if (ppp
->rc_state
!= 0) {
1934 ppp
->rcomp
->decomp_free(ppp
->rc_state
);
1937 ppp_recv_unlock(ppp
);
1939 state
= cp
->decomp_alloc(ccp_option
, data
.length
);
1943 ppp
->rc_state
= state
;
1944 ppp_recv_unlock(ppp
);
1954 * Look at a CCP packet and update our state accordingly.
1955 * We assume the caller has the xmit or recv path locked.
1958 ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
)
1960 unsigned char *dp
= skb
->data
+ 2;
1963 if (skb
->len
< CCP_HDRLEN
+ 2
1964 || skb
->len
< (len
= CCP_LENGTH(dp
)) + 2)
1965 return; /* too short */
1967 switch (CCP_CODE(dp
)) {
1972 * CCP is going down - disable compression.
1975 ppp
->rstate
&= ~SC_DECOMP_RUN
;
1977 ppp
->xstate
&= ~SC_COMP_RUN
;
1981 if ((ppp
->flags
& (SC_CCP_OPEN
| SC_CCP_UP
)) != SC_CCP_OPEN
)
1985 if (len
< CCP_OPT_MINLEN
|| len
< CCP_OPT_LENGTH(dp
))
1988 /* we will start receiving compressed packets */
1989 if (ppp
->rc_state
== 0)
1991 if (ppp
->rcomp
->decomp_init(ppp
->rc_state
, dp
, len
,
1992 ppp
->file
.index
, 0, ppp
->mru
, ppp
->debug
)) {
1993 ppp
->rstate
|= SC_DECOMP_RUN
;
1994 ppp
->rstate
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
1997 /* we will soon start sending compressed packets */
1998 if (ppp
->xc_state
== 0)
2000 if (ppp
->xcomp
->comp_init(ppp
->xc_state
, dp
, len
,
2001 ppp
->file
.index
, 0, ppp
->debug
))
2002 ppp
->xstate
|= SC_COMP_RUN
;
2007 /* reset the [de]compressor */
2008 if ((ppp
->flags
& SC_CCP_UP
) == 0)
2011 if (ppp
->rc_state
&& (ppp
->rstate
& SC_DECOMP_RUN
)) {
2012 ppp
->rcomp
->decomp_reset(ppp
->rc_state
);
2013 ppp
->rstate
&= ~SC_DC_ERROR
;
2016 if (ppp
->xc_state
&& (ppp
->xstate
& SC_COMP_RUN
))
2017 ppp
->xcomp
->comp_reset(ppp
->xc_state
);
2023 /* Free up compression resources. */
2025 ppp_ccp_closed(struct ppp
*ppp
)
2027 ppp
->flags
&= ~(SC_CCP_OPEN
| SC_CCP_UP
);
2029 ppp
->xstate
&= ~SC_COMP_RUN
;
2030 if (ppp
->xc_state
) {
2031 ppp
->xcomp
->comp_free(ppp
->xc_state
);
2035 ppp
->xstate
&= ~SC_DECOMP_RUN
;
2036 if (ppp
->rc_state
) {
2037 ppp
->rcomp
->decomp_free(ppp
->rc_state
);
2042 /* List of compressors. */
2043 static LIST_HEAD(compressor_list
);
2044 static spinlock_t compressor_list_lock
= SPIN_LOCK_UNLOCKED
;
2046 struct compressor_entry
{
2047 struct list_head list
;
2048 struct compressor
*comp
;
2051 static struct compressor_entry
*
2052 find_comp_entry(int proto
)
2054 struct compressor_entry
*ce
;
2055 struct list_head
*list
= &compressor_list
;
2057 while ((list
= list
->next
) != &compressor_list
) {
2058 ce
= list_entry(list
, struct compressor_entry
, list
);
2059 if (ce
->comp
->compress_proto
== proto
)
2065 /* Register a compressor */
2067 ppp_register_compressor(struct compressor
*cp
)
2069 struct compressor_entry
*ce
;
2072 spin_lock(&compressor_list_lock
);
2074 if (find_comp_entry(cp
->compress_proto
) != 0)
2077 ce
= kmalloc(sizeof(struct compressor_entry
), GFP_KERNEL
);
2082 list_add(&ce
->list
, &compressor_list
);
2084 spin_unlock(&compressor_list_lock
);
2088 /* Unregister a compressor */
2090 ppp_unregister_compressor(struct compressor
*cp
)
2092 struct compressor_entry
*ce
;
2094 spin_lock(&compressor_list_lock
);
2095 ce
= find_comp_entry(cp
->compress_proto
);
2096 if (ce
!= 0 && ce
->comp
== cp
) {
2097 list_del(&ce
->list
);
2100 spin_unlock(&compressor_list_lock
);
2103 /* Find a compressor. */
2104 static struct compressor
*
2105 find_compressor(int type
)
2107 struct compressor_entry
*ce
;
2108 struct compressor
*cp
= 0;
2110 spin_lock(&compressor_list_lock
);
2111 ce
= find_comp_entry(type
);
2114 spin_unlock(&compressor_list_lock
);
2119 * Miscelleneous stuff.
2123 ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
)
2125 struct slcompress
*vj
= ppp
->vj
;
2127 memset(st
, 0, sizeof(*st
));
2128 st
->p
.ppp_ipackets
= ppp
->stats
.rx_packets
;
2129 st
->p
.ppp_ierrors
= ppp
->stats
.rx_errors
;
2130 st
->p
.ppp_ibytes
= ppp
->stats
.rx_bytes
;
2131 st
->p
.ppp_opackets
= ppp
->stats
.tx_packets
;
2132 st
->p
.ppp_oerrors
= ppp
->stats
.tx_errors
;
2133 st
->p
.ppp_obytes
= ppp
->stats
.tx_bytes
;
2136 st
->vj
.vjs_packets
= vj
->sls_o_compressed
+ vj
->sls_o_uncompressed
;
2137 st
->vj
.vjs_compressed
= vj
->sls_o_compressed
;
2138 st
->vj
.vjs_searches
= vj
->sls_o_searches
;
2139 st
->vj
.vjs_misses
= vj
->sls_o_misses
;
2140 st
->vj
.vjs_errorin
= vj
->sls_i_error
;
2141 st
->vj
.vjs_tossed
= vj
->sls_i_tossed
;
2142 st
->vj
.vjs_uncompressedin
= vj
->sls_i_uncompressed
;
2143 st
->vj
.vjs_compressedin
= vj
->sls_i_compressed
;
2147 * Stuff for handling the lists of ppp units and channels
2148 * and for initialization.
2152 * Create a new ppp interface unit. Fails if it can't allocate memory
2153 * or if there is already a unit with the requested number.
2154 * unit == -1 means allocate a new number.
2157 ppp_create_interface(int unit
, int *retp
)
2160 struct net_device
*dev
;
2161 struct list_head
*list
;
2166 spin_lock(&all_ppp_lock
);
2167 list
= &all_ppp_units
;
2168 while ((list
= list
->next
) != &all_ppp_units
) {
2169 ppp
= list_entry(list
, struct ppp
, file
.list
);
2170 if ((unit
< 0 && ppp
->file
.index
> last_unit
+ 1)
2171 || (unit
>= 0 && unit
< ppp
->file
.index
))
2173 if (unit
== ppp
->file
.index
)
2174 goto out
; /* unit already exists */
2175 last_unit
= ppp
->file
.index
;
2178 unit
= last_unit
+ 1;
2180 /* Create a new ppp structure and link it before `list'. */
2182 ppp
= kmalloc(sizeof(struct ppp
), GFP_KERNEL
);
2185 memset(ppp
, 0, sizeof(struct ppp
));
2186 dev
= kmalloc(sizeof(struct net_device
), GFP_KERNEL
);
2191 memset(dev
, 0, sizeof(struct net_device
));
2193 ppp
->file
.index
= unit
;
2195 init_ppp_file(&ppp
->file
, INTERFACE
);
2196 for (i
= 0; i
< NUM_NP
; ++i
)
2197 ppp
->npmode
[i
] = NPMODE_PASS
;
2198 INIT_LIST_HEAD(&ppp
->channels
);
2199 spin_lock_init(&ppp
->rlock
);
2200 spin_lock_init(&ppp
->wlock
);
2201 #ifdef CONFIG_PPP_MULTILINK
2203 skb_queue_head_init(&ppp
->mrq
);
2204 #endif /* CONFIG_PPP_MULTILINK */
2207 dev
->init
= ppp_net_init
;
2208 sprintf(dev
->name
, "ppp%d", unit
);
2210 dev
->features
|= NETIF_F_DYNALLOC
;
2213 ret
= register_netdevice(dev
);
2216 printk(KERN_ERR
"PPP: couldn't register device (%d)\n", ret
);
2222 list_add(&ppp
->file
.list
, list
->prev
);
2224 spin_unlock(&all_ppp_lock
);
2232 * Initialize a ppp_file structure.
2235 init_ppp_file(struct ppp_file
*pf
, int kind
)
2238 skb_queue_head_init(&pf
->xq
);
2239 skb_queue_head_init(&pf
->rq
);
2240 atomic_set(&pf
->refcnt
, 1);
2241 init_waitqueue_head(&pf
->rwait
);
2245 * Free up all the resources used by a ppp interface unit.
2247 static void ppp_destroy_interface(struct ppp
*ppp
)
2249 struct net_device
*dev
;
2251 spin_lock(&all_ppp_lock
);
2252 list_del(&ppp
->file
.list
);
2254 /* Last fd open to this ppp unit is being closed or detached:
2255 mark the interface down, free the ppp unit */
2257 ppp_ccp_closed(ppp
);
2262 skb_queue_purge(&ppp
->file
.xq
);
2263 skb_queue_purge(&ppp
->file
.rq
);
2264 #ifdef CONFIG_PPP_MULTILINK
2265 skb_queue_purge(&ppp
->mrq
);
2266 #endif /* CONFIG_PPP_MULTILINK */
2274 unregister_netdevice(dev
);
2279 * We can't acquire any new channels (since we have the
2280 * all_ppp_lock) so if n_channels is 0, we can free the
2281 * ppp structure. Otherwise we leave it around until the
2282 * last channel disconnects from it.
2284 if (ppp
->n_channels
== 0)
2287 spin_unlock(&all_ppp_lock
);
2291 * Locate an existing ppp unit.
2292 * The caller should have locked the all_ppp_lock.
2295 ppp_find_unit(int unit
)
2298 struct list_head
*list
;
2300 list
= &all_ppp_units
;
2301 while ((list
= list
->next
) != &all_ppp_units
) {
2302 ppp
= list_entry(list
, struct ppp
, file
.list
);
2303 if (ppp
->file
.index
== unit
)
2310 * Locate an existing ppp channel.
2311 * The caller should have locked the all_channels_lock.
2313 static struct channel
*
2314 ppp_find_channel(int unit
)
2316 struct channel
*pch
;
2317 struct list_head
*list
;
2319 list
= &all_channels
;
2320 while ((list
= list
->next
) != &all_channels
) {
2321 pch
= list_entry(list
, struct channel
, file
.list
);
2322 if (pch
->file
.index
== unit
)
2329 * Connect a PPP channel to a PPP interface unit.
2332 ppp_connect_channel(struct channel
*pch
, int unit
)
2338 spin_lock(&all_ppp_lock
);
2339 ppp
= ppp_find_unit(unit
);
2342 write_lock_bh(&pch
->upl
);
2347 spin_lock_bh(&pch
->downl
);
2348 if (pch
->chan
== 0) /* need to check this?? */
2351 if (pch
->file
.hdrlen
> ppp
->file
.hdrlen
)
2352 ppp
->file
.hdrlen
= pch
->file
.hdrlen
;
2353 hdrlen
= pch
->file
.hdrlen
+ 2; /* for protocol bytes */
2354 if (ppp
->dev
&& hdrlen
> ppp
->dev
->hard_header_len
)
2355 ppp
->dev
->hard_header_len
= hdrlen
;
2356 list_add_tail(&pch
->clist
, &ppp
->channels
);
2362 spin_unlock_bh(&pch
->downl
);
2365 write_unlock_bh(&pch
->upl
);
2367 spin_unlock(&all_ppp_lock
);
2372 * Disconnect a channel from its ppp unit.
2375 ppp_disconnect_channel(struct channel
*pch
)
2381 write_lock_bh(&pch
->upl
);
2384 /* remove it from the ppp unit's list */
2387 list_del(&pch
->clist
);
2389 dead
= ppp
->dev
== 0 && ppp
->n_channels
== 0;
2392 /* Last disconnect from a ppp unit
2393 that is already dead: free it. */
2397 write_unlock_bh(&pch
->upl
);
2402 * Free up the resources used by a ppp channel.
2404 static void ppp_destroy_channel(struct channel
*pch
)
2406 skb_queue_purge(&pch
->file
.xq
);
2407 skb_queue_purge(&pch
->file
.rq
);
2411 void __exit
ppp_cleanup(void)
2413 /* should never happen */
2414 if (!list_empty(&all_ppp_units
) || !list_empty(&all_channels
))
2415 printk(KERN_ERR
"PPP: removing module but units remain!\n");
2416 if (devfs_unregister_chrdev(PPP_MAJOR
, "ppp") != 0)
2417 printk(KERN_ERR
"PPP: failed to unregister PPP device\n");
2418 devfs_unregister(devfs_handle
);
2421 module_init(ppp_init
);
2422 module_exit(ppp_cleanup
);
2424 EXPORT_SYMBOL(ppp_register_channel
);
2425 EXPORT_SYMBOL(ppp_unregister_channel
);
2426 EXPORT_SYMBOL(ppp_channel_index
);
2427 EXPORT_SYMBOL(ppp_unit_number
);
2428 EXPORT_SYMBOL(ppp_input
);
2429 EXPORT_SYMBOL(ppp_input_error
);
2430 EXPORT_SYMBOL(ppp_output_wakeup
);
2431 EXPORT_SYMBOL(ppp_register_compressor
);
2432 EXPORT_SYMBOL(ppp_unregister_compressor
);
2433 EXPORT_SYMBOL(ppp_channel_read
);
2434 EXPORT_SYMBOL(ppp_channel_write
);
2435 EXPORT_SYMBOL(ppp_channel_poll
);
2436 EXPORT_SYMBOL(ppp_channel_ioctl
);
2437 EXPORT_SYMBOL(all_ppp_units
); /* for debugging */
2438 EXPORT_SYMBOL(all_channels
); /* for debugging */