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 20000406==
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 <net/slhc_vj.h>
45 #include <asm/atomic.h>
47 #define PPP_VERSION "2.4.1"
50 * Network protocols we support.
52 #define NP_IP 0 /* Internet Protocol V4 */
53 #define NP_IPV6 1 /* Internet Protocol V6 */
54 #define NP_IPX 2 /* IPX protocol */
55 #define NP_AT 3 /* Appletalk protocol */
56 #define NUM_NP 4 /* Number of NPs. */
58 #define MPHDRLEN 6 /* multilink protocol header length */
59 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
60 #define MIN_FRAG_SIZE 64
63 * An instance of /dev/ppp can be associated with either a ppp
64 * interface unit or a ppp channel. In both cases, file->private_data
65 * points to one of these.
71 struct sk_buff_head xq
; /* pppd transmit queue */
72 struct sk_buff_head rq
; /* receive queue for pppd */
73 wait_queue_head_t rwait
; /* for poll on reading /dev/ppp */
74 atomic_t refcnt
; /* # refs (incl /dev/ppp attached) */
75 int hdrlen
; /* space to leave for headers */
76 struct list_head list
; /* link in all_* list */
77 int index
; /* interface unit / channel number */
80 #define PF_TO_X(pf, X) ((X *)((char *)(pf)-(unsigned long)(&((X *)0)->file)))
82 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
83 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
85 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
88 * Data structure describing one ppp unit.
89 * A ppp unit corresponds to a ppp network interface device
90 * and represents a multilink bundle.
91 * It can have 0 or more ppp channels connected to it.
94 struct ppp_file file
; /* stuff for read/write/poll */
95 char name
[16]; /* unit name */
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
, int wakeup
);
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
))
320 static int ppp_release(struct inode
*inode
, struct file
*file
)
322 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
), 0);
433 ppp_channel_push(PF_TO_CHANNEL(pf
));
443 static unsigned int ppp_poll(struct file
*file
, poll_table
*wait
)
445 struct ppp_file
*pf
= (struct ppp_file
*) file
->private_data
;
450 poll_wait(file
, &pf
->rwait
, wait
);
451 mask
= POLLOUT
| POLLWRNORM
;
452 if (skb_peek(&pf
->rq
) != 0)
453 mask
|= POLLIN
| POLLRDNORM
;
454 if (pf
->kind
== CHANNEL
) {
455 struct channel
*pch
= PF_TO_CHANNEL(pf
);
462 static int ppp_ioctl(struct inode
*inode
, struct file
*file
,
463 unsigned int cmd
, unsigned long arg
)
465 struct ppp_file
*pf
= (struct ppp_file
*) file
->private_data
;
467 int err
= -EFAULT
, val
, val2
, i
;
468 struct ppp_idle idle
;
471 struct slcompress
*vj
;
474 return ppp_unattached_ioctl(pf
, file
, cmd
, arg
);
476 if (pf
->kind
== CHANNEL
) {
477 struct channel
*pch
= PF_TO_CHANNEL(pf
);
478 struct ppp_channel
*chan
;
482 if (get_user(unit
, (int *) arg
))
484 err
= ppp_connect_channel(pch
, unit
);
488 err
= ppp_disconnect_channel(pch
);
492 file
->private_data
= 0;
493 if (atomic_dec_and_test(&pf
->refcnt
))
494 ppp_destroy_channel(pch
);
499 spin_lock_bh(&pch
->downl
);
502 if (chan
&& chan
->ops
->ioctl
)
503 err
= chan
->ops
->ioctl(chan
, cmd
, arg
);
504 spin_unlock_bh(&pch
->downl
);
509 if (pf
->kind
!= INTERFACE
) {
511 printk(KERN_ERR
"PPP: not interface or channel??\n");
518 file
->private_data
= 0;
519 if (atomic_dec_and_test(&pf
->refcnt
))
520 ppp_destroy_interface(ppp
);
525 if (get_user(val
, (int *) arg
))
532 if (get_user(val
, (int *) arg
))
535 if (ppp
->flags
& ~val
& SC_CCP_OPEN
)
537 ppp
->flags
= val
& SC_FLAG_BITS
;
543 val
= ppp
->flags
| ppp
->xstate
| ppp
->rstate
;
544 if (put_user(val
, (int *) arg
))
549 case PPPIOCSCOMPRESS
:
550 err
= ppp_set_compress(ppp
, arg
);
554 if (put_user(ppp
->file
.index
, (int *) arg
))
560 if (get_user(val
, (int *) arg
))
567 if (put_user(ppp
->debug
, (int *) arg
))
573 idle
.xmit_idle
= (jiffies
- ppp
->last_xmit
) / HZ
;
574 idle
.recv_idle
= (jiffies
- ppp
->last_recv
) / HZ
;
575 if (copy_to_user((void *) arg
, &idle
, sizeof(idle
)))
581 if (get_user(val
, (int *) arg
))
584 if ((val
>> 16) != 0) {
588 vj
= slhc_init(val2
+1, val
+1);
590 printk(KERN_ERR
"PPP: no memory (VJ compressor)\n");
604 if (copy_from_user(&npi
, (void *) arg
, sizeof(npi
)))
606 err
= proto_to_npindex(npi
.protocol
);
610 if (cmd
== PPPIOCGNPMODE
) {
612 npi
.mode
= ppp
->npmode
[i
];
613 if (copy_to_user((void *) arg
, &npi
, sizeof(npi
)))
616 ppp
->npmode
[i
] = npi
.mode
;
617 /* we may be able to transmit more packets now (??) */
618 netif_wake_queue(ppp
->dev
);
623 #ifdef CONFIG_PPP_MULTILINK
625 if (get_user(val
, (int *) arg
))
629 ppp_recv_unlock(ppp
);
632 #endif /* CONFIG_PPP_MULTILINK */
640 static int ppp_unattached_ioctl(struct ppp_file
*pf
, struct file
*file
,
641 unsigned int cmd
, unsigned long arg
)
643 int unit
, err
= -EFAULT
;
645 struct channel
*chan
;
649 /* Create a new ppp unit */
650 if (get_user(unit
, (int *) arg
))
652 ppp
= ppp_create_interface(unit
, &err
);
655 file
->private_data
= &ppp
->file
;
657 if (put_user(ppp
->file
.index
, (int *) arg
))
663 /* Attach to an existing ppp unit */
664 if (get_user(unit
, (int *) arg
))
666 spin_lock(&all_ppp_lock
);
667 ppp
= ppp_find_unit(unit
);
669 atomic_inc(&ppp
->file
.refcnt
);
670 spin_unlock(&all_ppp_lock
);
674 file
->private_data
= &ppp
->file
;
679 if (get_user(unit
, (int *) arg
))
681 spin_lock_bh(&all_channels_lock
);
682 chan
= ppp_find_channel(unit
);
684 atomic_inc(&chan
->file
.refcnt
);
685 spin_unlock_bh(&all_channels_lock
);
689 file
->private_data
= &chan
->file
;
699 static struct file_operations ppp_device_fops
= {
708 #define PPP_MAJOR 108
710 static devfs_handle_t devfs_handle
= NULL
;
712 /* Called at boot time if ppp is compiled into the kernel,
713 or at module load time (from init_module) if compiled as a module. */
714 int __init
ppp_init(void)
718 printk(KERN_INFO
"PPP generic driver version " PPP_VERSION
"\n");
719 err
= devfs_register_chrdev(PPP_MAJOR
, "ppp", &ppp_device_fops
);
721 printk(KERN_ERR
"failed to register PPP device (%d)\n", err
);
722 devfs_handle
= devfs_register(NULL
, "ppp", 0, DEVFS_FL_NONE
,
724 S_IFCHR
| S_IRUSR
| S_IWUSR
, 0, 0,
725 &ppp_device_fops
, NULL
);
731 * Network interface unit routines.
734 ppp_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
736 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
740 npi
= ethertype_to_npindex(ntohs(skb
->protocol
));
744 /* Drop, accept or reject the packet */
745 switch (ppp
->npmode
[npi
]) {
749 /* it would be nice to have a way to tell the network
750 system to queue this one up for later. */
757 /* Put the 2-byte PPP protocol number on the front,
758 making sure there is room for the address and control fields. */
759 if (skb_headroom(skb
) < PPP_HDRLEN
) {
762 ns
= alloc_skb(skb
->len
+ dev
->hard_header_len
, GFP_ATOMIC
);
765 skb_reserve(ns
, dev
->hard_header_len
);
766 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
770 pp
= skb_push(skb
, 2);
771 proto
= npindex_to_proto
[npi
];
775 netif_stop_queue(dev
);
776 skb_queue_tail(&ppp
->file
.xq
, skb
);
777 ppp_xmit_process(ppp
, 0);
782 ++ppp
->stats
.tx_dropped
;
786 static struct net_device_stats
*
787 ppp_net_stats(struct net_device
*dev
)
789 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
795 ppp_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
797 struct ppp
*ppp
= dev
->priv
;
799 void *addr
= (void *) ifr
->ifr_ifru
.ifru_data
;
800 struct ppp_stats stats
;
801 struct ppp_comp_stats cstats
;
806 ppp_get_stats(ppp
, &stats
);
807 if (copy_to_user(addr
, &stats
, sizeof(stats
)))
813 memset(&cstats
, 0, sizeof(cstats
));
814 if (ppp
->xc_state
!= 0)
815 ppp
->xcomp
->comp_stat(ppp
->xc_state
, &cstats
.c
);
816 if (ppp
->rc_state
!= 0)
817 ppp
->rcomp
->decomp_stat(ppp
->rc_state
, &cstats
.d
);
818 if (copy_to_user(addr
, &cstats
, sizeof(cstats
)))
825 if (copy_to_user(addr
, vers
, strlen(vers
) + 1))
838 ppp_net_init(struct net_device
*dev
)
840 dev
->hard_header_len
= PPP_HDRLEN
;
842 dev
->hard_start_xmit
= ppp_start_xmit
;
843 dev
->get_stats
= ppp_net_stats
;
844 dev
->do_ioctl
= ppp_net_ioctl
;
846 dev
->tx_queue_len
= 3;
847 dev
->type
= ARPHRD_PPP
;
848 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
850 dev_init_buffers(dev
);
855 * Transmit-side routines.
859 * Called to do any work queued up on the transmit side
860 * that can now be done.
863 ppp_xmit_process(struct ppp
*ppp
, int wakeup
)
870 while (ppp
->xmit_pending
== 0
871 && (skb
= skb_dequeue(&ppp
->file
.xq
)) != 0)
872 ppp_send_frame(ppp
, skb
);
873 /* If there's no work left to do, tell the core net
874 code that we can accept some more. */
875 if (ppp
->xmit_pending
== 0 && skb_peek(&ppp
->file
.xq
) == 0
877 netif_wake_queue(ppp
->dev
);
878 ppp_xmit_unlock(ppp
);
882 * Compress and send a frame.
883 * The caller should have locked the xmit path,
884 * and xmit_pending should be 0.
887 ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
889 int proto
= PPP_PROTO(skb
);
890 struct sk_buff
*new_skb
;
894 ++ppp
->stats
.tx_packets
;
895 ppp
->stats
.tx_bytes
+= skb
->len
- 2;
899 if (ppp
->vj
== 0 || (ppp
->flags
& SC_COMP_TCP
) == 0)
901 /* try to do VJ TCP header compression */
902 new_skb
= alloc_skb(skb
->len
+ ppp
->dev
->hard_header_len
- 2,
905 printk(KERN_ERR
"PPP: no memory (VJ comp pkt)\n");
908 skb_reserve(new_skb
, ppp
->dev
->hard_header_len
- 2);
910 len
= slhc_compress(ppp
->vj
, cp
, skb
->len
- 2,
911 new_skb
->data
+ 2, &cp
,
912 !(ppp
->flags
& SC_NO_TCP_CCID
));
913 if (cp
== skb
->data
+ 2) {
914 /* didn't compress */
917 if (cp
[0] & SL_TYPE_COMPRESSED_TCP
) {
918 proto
= PPP_VJC_COMP
;
919 cp
[0] &= ~SL_TYPE_COMPRESSED_TCP
;
921 proto
= PPP_VJC_UNCOMP
;
922 cp
[0] = skb
->data
[2];
926 cp
= skb_put(skb
, len
+ 2);
933 /* peek at outbound CCP frames */
934 ppp_ccp_peek(ppp
, skb
, 0);
938 /* try to do packet compression */
939 if ((ppp
->xstate
& SC_COMP_RUN
) && ppp
->xc_state
!= 0
940 && proto
!= PPP_LCP
&& proto
!= PPP_CCP
) {
941 new_skb
= alloc_skb(ppp
->dev
->mtu
+ ppp
->dev
->hard_header_len
,
944 printk(KERN_ERR
"PPP: no memory (comp pkt)\n");
947 if (ppp
->dev
->hard_header_len
> PPP_HDRLEN
)
949 ppp
->dev
->hard_header_len
- PPP_HDRLEN
);
951 /* compressor still expects A/C bytes in hdr */
952 len
= ppp
->xcomp
->compress(ppp
->xc_state
, skb
->data
- 2,
953 new_skb
->data
, skb
->len
+ 2,
954 ppp
->dev
->mtu
+ PPP_HDRLEN
);
955 if (len
> 0 && (ppp
->flags
& SC_CCP_UP
)) {
959 skb_pull(skb
, 2); /* pull off A/C bytes */
961 /* didn't compress, or CCP not up yet */
966 /* for data packets, record the time */
968 ppp
->last_xmit
= jiffies
;
971 * If we are waiting for traffic (demand dialling),
972 * queue it up for pppd to receive.
974 if (ppp
->flags
& SC_LOOP_TRAFFIC
) {
975 if (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
)
977 skb_queue_tail(&ppp
->file
.rq
, skb
);
978 wake_up_interruptible(&ppp
->file
.rwait
);
982 ppp
->xmit_pending
= skb
;
988 ++ppp
->stats
.tx_errors
;
992 * Try to send the frame in xmit_pending.
993 * The caller should have the xmit path locked.
996 ppp_push(struct ppp
*ppp
)
998 struct list_head
*list
;
1000 struct sk_buff
*skb
= ppp
->xmit_pending
;
1005 list
= &ppp
->channels
;
1006 if (list_empty(list
)) {
1007 /* nowhere to send the packet, just drop it */
1008 ppp
->xmit_pending
= 0;
1013 if ((ppp
->flags
& SC_MULTILINK
) == 0) {
1014 /* not doing multilink: send it down the first channel */
1016 pch
= list_entry(list
, struct channel
, clist
);
1018 spin_lock_bh(&pch
->downl
);
1020 if (pch
->chan
->ops
->start_xmit(pch
->chan
, skb
))
1023 /* channel got unregistered */
1027 if (skb_queue_len(&pch
->file
.xq
) == 0 && skb
== 0)
1028 ppp
->xmit_pending
= 0;
1029 spin_unlock_bh(&pch
->downl
);
1033 #ifdef CONFIG_PPP_MULTILINK
1034 /* Multilink: fragment the packet over as many links
1035 as can take the packet at the moment. */
1036 if (!ppp_mp_explode(ppp
, skb
))
1038 #endif /* CONFIG_PPP_MULTILINK */
1040 ppp
->xmit_pending
= 0;
1044 #ifdef CONFIG_PPP_MULTILINK
1046 * Divide a packet to be transmitted into fragments and
1047 * send them out the individual links.
1049 static int ppp_mp_explode(struct ppp
*ppp
, struct sk_buff
*skb
)
1051 int nch
, len
, fragsize
;
1052 int i
, bits
, hdrlen
, mtu
;
1054 unsigned char *p
, *q
;
1055 struct list_head
*list
;
1056 struct channel
*pch
;
1057 struct sk_buff
*frag
;
1058 struct ppp_channel
*chan
;
1061 hdrlen
= (ppp
->flags
& SC_MP_XSHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1062 list
= &ppp
->channels
;
1063 while ((list
= list
->next
) != &ppp
->channels
) {
1064 pch
= list_entry(list
, struct channel
, clist
);
1065 nch
+= pch
->avail
= (skb_queue_len(&pch
->file
.xq
) == 0);
1067 * If a channel hasn't had a fragment yet, it has to get
1068 * one before we send any fragments on later channels.
1069 * If it can't take a fragment now, don't give any
1070 * to subsequent channels.
1072 if (!pch
->had_frag
&& !pch
->avail
) {
1073 while ((list
= list
->next
) != &ppp
->channels
) {
1074 pch
= list_entry(list
, struct channel
, clist
);
1081 return 0; /* can't take now, leave it in xmit_pending */
1083 /* Do protocol field compression (XXX this should be optional) */
1091 /* decide on fragment size */
1094 int maxch
= ROUNDUP(len
, MIN_FRAG_SIZE
);
1097 fragsize
= ROUNDUP(fragsize
, nch
);
1100 /* skip to the channel after the one we last used
1101 and start at that one */
1102 for (i
= 0; i
< ppp
->nxchan
; ++i
) {
1104 if (list
== &ppp
->channels
) {
1110 /* create a fragment for each channel */
1114 if (list
== &ppp
->channels
) {
1118 pch
= list_entry(list
, struct channel
, clist
);
1123 /* check the channel's mtu and whether it is still attached. */
1124 spin_lock_bh(&pch
->downl
);
1125 if (pch
->chan
== 0 || (mtu
= pch
->chan
->mtu
) < hdrlen
) {
1126 /* can't use this channel */
1127 spin_unlock_bh(&pch
->downl
);
1135 * We have to create multiple fragments for this channel
1136 * if fragsize is greater than the channel's mtu.
1140 for (flen
= fragsize
; flen
> 0; flen
-= fnb
) {
1142 if (fnb
> mtu
+ 2 - hdrlen
)
1143 fnb
= mtu
+ 2 - hdrlen
;
1146 frag
= alloc_skb(fnb
+ hdrlen
, GFP_ATOMIC
);
1149 q
= skb_put(frag
, fnb
+ hdrlen
);
1150 /* make the MP header */
1153 if (ppp
->flags
& SC_MP_XSHORTSEQ
) {
1154 q
[2] = bits
+ ((ppp
->nxseq
>> 8) & 0xf);
1158 q
[3] = ppp
->nxseq
>> 16;
1159 q
[4] = ppp
->nxseq
>> 8;
1163 /* copy the data in */
1164 memcpy(q
+ hdrlen
, p
, fnb
);
1166 /* try to send it down the channel */
1168 if (!chan
->ops
->start_xmit(chan
, frag
))
1169 skb_queue_tail(&pch
->file
.xq
, frag
);
1176 spin_unlock_bh(&pch
->downl
);
1183 spin_unlock_bh(&pch
->downl
);
1185 printk(KERN_ERR
"PPP: no memory (fragment)\n");
1186 ++ppp
->stats
.tx_errors
;
1188 return 1; /* abandon the frame */
1190 #endif /* CONFIG_PPP_MULTILINK */
1193 * Try to send data out on a channel.
1196 ppp_channel_push(struct channel
*pch
)
1198 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
);
1218 * Receive-side routines.
1221 /* misuse a few fields of the skb for MP reconstruction */
1222 #define sequence priority
1223 #define BEbits cb[0]
1226 ppp_do_recv(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1229 /* ppp->dev == 0 means interface is closing down */
1231 ppp_receive_frame(ppp
, skb
, pch
);
1234 ppp_recv_unlock(ppp
);
1238 ppp_input(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1240 struct channel
*pch
= chan
->ppp
;
1243 if (pch
== 0 || skb
->len
== 0) {
1248 proto
= PPP_PROTO(skb
);
1249 read_lock_bh(&pch
->upl
);
1250 if (pch
->ppp
== 0 || proto
>= 0xc000 || proto
== PPP_CCPFRAG
) {
1251 /* put it on the channel queue */
1252 skb_queue_tail(&pch
->file
.rq
, skb
);
1253 /* drop old frames if queue too long */
1254 while (pch
->file
.rq
.qlen
> PPP_MAX_RQLEN
1255 && (skb
= skb_dequeue(&pch
->file
.rq
)) != 0)
1257 wake_up_interruptible(&pch
->file
.rwait
);
1259 ppp_do_recv(pch
->ppp
, skb
, pch
);
1261 read_unlock_bh(&pch
->upl
);
1264 /* Put a 0-length skb in the receive queue as an error indication */
1266 ppp_input_error(struct ppp_channel
*chan
, int code
)
1268 struct channel
*pch
= chan
->ppp
;
1269 struct sk_buff
*skb
;
1274 read_lock_bh(&pch
->upl
);
1275 if (pch
->ppp
!= 0) {
1276 skb
= alloc_skb(0, GFP_ATOMIC
);
1278 skb
->len
= 0; /* probably unnecessary */
1280 ppp_do_recv(pch
->ppp
, skb
, pch
);
1283 read_unlock_bh(&pch
->upl
);
1287 * We come in here to process a received frame.
1288 * The receive side of the ppp unit is locked.
1291 ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1293 if (skb
->len
>= 2) {
1294 #ifdef CONFIG_PPP_MULTILINK
1295 /* XXX do channel-level decompression here */
1296 if (PPP_PROTO(skb
) == PPP_MP
)
1297 ppp_receive_mp_frame(ppp
, skb
, pch
);
1299 #endif /* CONFIG_PPP_MULTILINK */
1300 ppp_receive_nonmp_frame(ppp
, skb
);
1305 /* note: a 0-length skb is used as an error indication */
1306 ++ppp
->stats
.rx_length_errors
;
1309 ppp_receive_error(ppp
);
1313 ppp_receive_error(struct ppp
*ppp
)
1315 ++ppp
->stats
.rx_errors
;
1321 ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1324 int proto
, len
, npi
;
1327 * Decompress the frame, if compressed.
1328 * Note that some decompressors need to see uncompressed frames
1329 * that come in as well as compressed frames.
1331 if (ppp
->rc_state
!= 0 && (ppp
->rstate
& SC_DECOMP_RUN
)
1332 && (ppp
->rstate
& (SC_DC_FERROR
| SC_DC_ERROR
)) == 0)
1333 skb
= ppp_decompress_frame(ppp
, skb
);
1335 proto
= PPP_PROTO(skb
);
1338 /* decompress VJ compressed packets */
1339 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
1341 if (skb_tailroom(skb
) < 124) {
1342 /* copy to a new sk_buff with more tailroom */
1343 ns
= dev_alloc_skb(skb
->len
+ 128);
1345 printk(KERN_ERR
"PPP: no memory (VJ decomp)\n");
1349 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
1353 len
= slhc_uncompress(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2);
1355 printk(KERN_DEBUG
"PPP: VJ decompression error\n");
1360 skb_put(skb
, len
- skb
->len
);
1361 else if (len
< skb
->len
)
1366 case PPP_VJC_UNCOMP
:
1367 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
1369 if (slhc_remember(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2) <= 0) {
1370 printk(KERN_ERR
"PPP: VJ uncompressed error\n");
1377 ppp_ccp_peek(ppp
, skb
, 1);
1381 ++ppp
->stats
.rx_packets
;
1382 ppp
->stats
.rx_bytes
+= skb
->len
- 2;
1384 npi
= proto_to_npindex(proto
);
1386 /* control or unknown frame - pass it to pppd */
1387 skb_queue_tail(&ppp
->file
.rq
, skb
);
1388 /* limit queue length by dropping old frames */
1389 while (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
1390 && (skb
= skb_dequeue(&ppp
->file
.rq
)) != 0)
1392 /* wake up any process polling or blocking on read */
1393 wake_up_interruptible(&ppp
->file
.rwait
);
1396 /* network protocol frame - give it to the kernel */
1397 ppp
->last_recv
= jiffies
;
1398 if ((ppp
->dev
->flags
& IFF_UP
) == 0
1399 || ppp
->npmode
[npi
] != NPMODE_PASS
) {
1402 skb_pull(skb
, 2); /* chop off protocol */
1403 skb
->dev
= ppp
->dev
;
1404 skb
->protocol
= htons(npindex_to_ethertype
[npi
]);
1405 skb
->mac
.raw
= skb
->data
;
1413 ppp_receive_error(ppp
);
1416 static struct sk_buff
*
1417 ppp_decompress_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1419 int proto
= PPP_PROTO(skb
);
1423 if (proto
== PPP_COMP
) {
1424 ns
= dev_alloc_skb(ppp
->mru
+ PPP_HDRLEN
);
1426 printk(KERN_ERR
"ppp_decompress_frame: no memory\n");
1429 /* the decompressor still expects the A/C bytes in the hdr */
1430 len
= ppp
->rcomp
->decompress(ppp
->rc_state
, skb
->data
- 2,
1431 skb
->len
+ 2, ns
->data
, ppp
->mru
+ PPP_HDRLEN
);
1433 /* Pass the compressed frame to pppd as an
1434 error indication. */
1435 if (len
== DECOMP_FATALERROR
)
1436 ppp
->rstate
|= SC_DC_FERROR
;
1443 skb_pull(skb
, 2); /* pull off the A/C bytes */
1446 /* Uncompressed frame - pass to decompressor so it
1447 can update its dictionary if necessary. */
1448 if (ppp
->rcomp
->incomp
)
1449 ppp
->rcomp
->incomp(ppp
->rc_state
, skb
->data
- 2,
1456 ppp
->rstate
|= SC_DC_ERROR
;
1457 ppp_receive_error(ppp
);
1461 #ifdef CONFIG_PPP_MULTILINK
1463 * Receive a multilink frame.
1464 * We put it on the reconstruction queue and then pull off
1465 * as many completed frames as we can.
1468 ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1471 struct list_head
*l
;
1472 int mphdrlen
= (ppp
->flags
& SC_MP_SHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1474 if (skb
->len
< mphdrlen
+ 1 || ppp
->mrru
== 0)
1475 goto err
; /* no good, throw it away */
1477 /* Decode sequence number and begin/end bits */
1478 if (ppp
->flags
& SC_MP_SHORTSEQ
) {
1479 seq
= ((skb
->data
[2] & 0x0f) << 8) | skb
->data
[3];
1482 seq
= (skb
->data
[3] << 16) | (skb
->data
[4] << 8)| skb
->data
[5];
1485 skb
->BEbits
= skb
->data
[2];
1486 skb_pull(skb
, mphdrlen
); /* pull off PPP and MP headers */
1489 * Do protocol ID decompression on the first fragment of each packet.
1491 if ((skb
->BEbits
& B
) && (skb
->data
[0] & 1))
1492 *skb_push(skb
, 1) = 0;
1495 * Expand sequence number to 32 bits, making it as close
1496 * as possible to ppp->minseq.
1498 seq
|= ppp
->minseq
& ~mask
;
1499 if ((int)(ppp
->minseq
- seq
) > (int)(mask
>> 1))
1501 else if ((int)(seq
- ppp
->minseq
) > (int)(mask
>> 1))
1502 seq
-= mask
+ 1; /* should never happen */
1503 skb
->sequence
= seq
;
1507 * If this packet comes before the next one we were expecting,
1510 if (seq_before(seq
, ppp
->nextseq
)) {
1512 ++ppp
->stats
.rx_dropped
;
1513 ppp_receive_error(ppp
);
1518 * Reevaluate minseq, the minimum over all channels of the
1519 * last sequence number received on each channel. Because of
1520 * the increasing sequence number rule, we know that any fragment
1521 * before `minseq' which hasn't arrived is never going to arrive.
1522 * The list of channels can't change because we have the receive
1523 * side of the ppp unit locked.
1525 for (l
= ppp
->channels
.next
; l
!= &ppp
->channels
; l
= l
->next
) {
1526 struct channel
*ch
= list_entry(l
, struct channel
, clist
);
1527 if (seq_before(ch
->lastseq
, seq
))
1530 if (seq_before(ppp
->minseq
, seq
))
1533 /* Put the fragment on the reconstruction queue */
1534 ppp_mp_insert(ppp
, skb
);
1536 /* If the queue is getting long, don't wait any longer for packets
1537 before the start of the queue. */
1538 if (skb_queue_len(&ppp
->mrq
) >= PPP_MP_MAX_QLEN
1539 && seq_before(ppp
->minseq
, ppp
->mrq
.next
->sequence
))
1540 ppp
->minseq
= ppp
->mrq
.next
->sequence
;
1542 /* Pull completed packets off the queue and receive them. */
1543 while ((skb
= ppp_mp_reconstruct(ppp
)) != 0)
1544 ppp_receive_nonmp_frame(ppp
, skb
);
1550 ppp_receive_error(ppp
);
1554 * Insert a fragment on the MP reconstruction queue.
1555 * The queue is ordered by increasing sequence number.
1558 ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
)
1561 struct sk_buff_head
*list
= &ppp
->mrq
;
1562 u32 seq
= skb
->sequence
;
1564 /* N.B. we don't need to lock the list lock because we have the
1565 ppp unit receive-side lock. */
1566 for (p
= list
->next
; p
!= (struct sk_buff
*)list
; p
= p
->next
)
1567 if (seq_before(seq
, p
->sequence
))
1569 __skb_insert(skb
, p
->prev
, p
, list
);
1573 * Reconstruct a packet from the MP fragment queue.
1574 * We go through increasing sequence numbers until we find a
1575 * complete packet, or we get to the sequence number for a fragment
1576 * which hasn't arrived but might still do so.
1579 ppp_mp_reconstruct(struct ppp
*ppp
)
1581 u32 seq
= ppp
->nextseq
;
1582 u32 minseq
= ppp
->minseq
;
1583 struct sk_buff_head
*list
= &ppp
->mrq
;
1584 struct sk_buff
*p
, *next
;
1585 struct sk_buff
*head
, *tail
;
1586 struct sk_buff
*skb
= NULL
;
1587 int lost
= 0, len
= 0;
1589 if (ppp
->mrru
== 0) /* do nothing until mrru is set */
1593 for (p
= head
; p
!= (struct sk_buff
*) list
; p
= next
) {
1595 if (seq_before(p
->sequence
, seq
)) {
1596 /* this can't happen, anyway ignore the skb */
1597 printk(KERN_ERR
"ppp_mp_reconstruct bad seq %u < %u\n",
1602 if (p
->sequence
!= seq
) {
1603 /* Fragment `seq' is missing. If it is after
1604 minseq, it might arrive later, so stop here. */
1605 if (seq_after(seq
, minseq
))
1607 /* Fragment `seq' is lost, keep going. */
1609 seq
= seq_before(minseq
, p
->sequence
)?
1610 minseq
+ 1: p
->sequence
;
1616 * At this point we know that all the fragments from
1617 * ppp->nextseq to seq are either present or lost.
1618 * Also, there are no complete packets in the queue
1619 * that have no missing fragments and end before this
1623 /* B bit set indicates this fragment starts a packet */
1624 if (p
->BEbits
& B
) {
1632 /* Got a complete packet yet? */
1633 if (lost
== 0 && (p
->BEbits
& E
) && (head
->BEbits
& B
)) {
1634 if (len
> ppp
->mrru
+ 2) {
1635 ++ppp
->stats
.rx_length_errors
;
1636 printk(KERN_DEBUG
"PPP: reconstructed packet"
1637 " is too long (%d)\n", len
);
1638 } else if (p
== head
) {
1639 /* fragment is complete packet - reuse skb */
1643 } else if ((skb
= dev_alloc_skb(len
)) == NULL
) {
1644 ++ppp
->stats
.rx_missed_errors
;
1645 printk(KERN_DEBUG
"PPP: no memory for "
1646 "reconstructed packet");
1651 ppp
->nextseq
= seq
+ 1;
1655 * If this is the ending fragment of a packet,
1656 * and we haven't found a complete valid packet yet,
1657 * we can discard up to and including this fragment.
1665 /* If we have a complete packet, copy it all into one skb. */
1667 /* If we have discarded any fragments,
1668 signal a receive error. */
1669 if (head
->sequence
!= ppp
->nextseq
) {
1671 printk(KERN_DEBUG
" missed pkts %u..%u\n",
1672 ppp
->nextseq
, head
->sequence
-1);
1673 ++ppp
->stats
.rx_dropped
;
1674 ppp_receive_error(ppp
);
1678 /* copy to a single skb */
1679 for (p
= head
; p
!= tail
->next
; p
= p
->next
)
1680 memcpy(skb_put(skb
, p
->len
), p
->data
, p
->len
);
1681 ppp
->nextseq
= tail
->sequence
+ 1;
1685 /* Discard all the skbuffs that we have copied the data out of
1686 or that we can't use. */
1687 while ((p
= list
->next
) != head
) {
1688 __skb_unlink(p
, list
);
1694 #endif /* CONFIG_PPP_MULTILINK */
1697 * Channel interface.
1701 * Create a new, unattached ppp channel.
1704 ppp_register_channel(struct ppp_channel
*chan
)
1706 struct channel
*pch
;
1708 pch
= kmalloc(sizeof(struct channel
), GFP_ATOMIC
);
1711 memset(pch
, 0, sizeof(struct channel
));
1715 init_ppp_file(&pch
->file
, CHANNEL
);
1716 pch
->file
.hdrlen
= chan
->hdrlen
;
1717 #ifdef CONFIG_PPP_MULTILINK
1719 #endif /* CONFIG_PPP_MULTILINK */
1720 spin_lock_init(&pch
->downl
);
1721 pch
->upl
= RW_LOCK_UNLOCKED
;
1722 spin_lock_bh(&all_channels_lock
);
1723 pch
->file
.index
= ++last_channel_index
;
1724 list_add(&pch
->file
.list
, &all_channels
);
1725 spin_unlock_bh(&all_channels_lock
);
1731 * Return the index of a channel.
1733 int ppp_channel_index(struct ppp_channel
*chan
)
1735 struct channel
*pch
= chan
->ppp
;
1737 return pch
->file
.index
;
1741 * Return the PPP unit number to which a channel is connected.
1743 int ppp_unit_number(struct ppp_channel
*chan
)
1745 struct channel
*pch
= chan
->ppp
;
1749 read_lock_bh(&pch
->upl
);
1751 unit
= pch
->ppp
->file
.index
;
1752 read_unlock_bh(&pch
->upl
);
1758 * Disconnect a channel from the generic layer.
1759 * This can be called from mainline or BH/softirq level.
1762 ppp_unregister_channel(struct ppp_channel
*chan
)
1764 struct channel
*pch
= chan
->ppp
;
1767 return; /* should never happen */
1771 * This ensures that we have returned from any calls into the
1772 * the channel's start_xmit or ioctl routine before we proceed.
1774 spin_lock_bh(&pch
->downl
);
1776 spin_unlock_bh(&pch
->downl
);
1777 ppp_disconnect_channel(pch
);
1778 wake_up_interruptible(&pch
->file
.rwait
);
1779 spin_lock_bh(&all_channels_lock
);
1780 list_del(&pch
->file
.list
);
1781 spin_unlock_bh(&all_channels_lock
);
1782 if (atomic_dec_and_test(&pch
->file
.refcnt
))
1783 ppp_destroy_channel(pch
);
1788 * Callback from a channel when it can accept more to transmit.
1789 * This should be called at BH/softirq level, not interrupt level.
1792 ppp_output_wakeup(struct ppp_channel
*chan
)
1794 struct channel
*pch
= chan
->ppp
;
1799 ppp_channel_push(pch
);
1800 if (skb_queue_len(&pch
->file
.xq
) == 0) {
1801 read_lock_bh(&pch
->upl
);
1804 ppp_xmit_process(ppp
, 1);
1805 read_unlock_bh(&pch
->upl
);
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
);
1834 ppp_channel_poll(struct ppp_channel
*chan
, struct file
*file
, poll_table
*wait
)
1837 struct channel
*pch
= chan
->ppp
;
1839 mask
= POLLOUT
| POLLWRNORM
;
1841 poll_wait(file
, &pch
->file
.rwait
, wait
);
1842 if (skb_peek(&pch
->file
.rq
) != 0)
1843 mask
|= POLLIN
| POLLRDNORM
;
1848 int ppp_channel_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
1851 struct channel
*pch
= chan
->ppp
;
1855 if (!capable(CAP_NET_ADMIN
))
1861 if (get_user(unit
, (int *) arg
))
1863 err
= ppp_connect_channel(pch
, unit
);
1866 err
= ppp_disconnect_channel(pch
);
1873 * Compression control.
1876 /* Process the PPPIOCSCOMPRESS ioctl. */
1878 ppp_set_compress(struct ppp
*ppp
, unsigned long arg
)
1881 struct compressor
*cp
;
1882 struct ppp_option_data data
;
1884 unsigned char ccp_option
[CCP_MAX_OPTION_LENGTH
];
1890 if (copy_from_user(&data
, (void *) arg
, sizeof(data
))
1891 || (data
.length
<= CCP_MAX_OPTION_LENGTH
1892 && copy_from_user(ccp_option
, data
.ptr
, data
.length
)))
1895 if (data
.length
> CCP_MAX_OPTION_LENGTH
1896 || ccp_option
[1] < 2 || ccp_option
[1] > data
.length
)
1899 cp
= find_compressor(ccp_option
[0]);
1902 sprintf(modname
, "ppp-compress-%d", ccp_option
[0]);
1903 request_module(modname
);
1904 cp
= find_compressor(ccp_option
[0]);
1906 #endif /* CONFIG_KMOD */
1911 if (data
.transmit
) {
1913 ppp
->xstate
&= ~SC_COMP_RUN
;
1914 if (ppp
->xc_state
!= 0) {
1915 ppp
->xcomp
->comp_free(ppp
->xc_state
);
1918 ppp_xmit_unlock(ppp
);
1920 state
= cp
->comp_alloc(ccp_option
, data
.length
);
1924 ppp
->xc_state
= state
;
1925 ppp_xmit_unlock(ppp
);
1931 ppp
->rstate
&= ~SC_DECOMP_RUN
;
1932 if (ppp
->rc_state
!= 0) {
1933 ppp
->rcomp
->decomp_free(ppp
->rc_state
);
1936 ppp_recv_unlock(ppp
);
1938 state
= cp
->decomp_alloc(ccp_option
, data
.length
);
1942 ppp
->rc_state
= state
;
1943 ppp_recv_unlock(ppp
);
1953 * Look at a CCP packet and update our state accordingly.
1954 * We assume the caller has the xmit or recv path locked.
1957 ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
)
1959 unsigned char *dp
= skb
->data
+ 2;
1962 if (skb
->len
< CCP_HDRLEN
+ 2
1963 || skb
->len
< (len
= CCP_LENGTH(dp
)) + 2)
1964 return; /* too short */
1966 switch (CCP_CODE(dp
)) {
1971 * CCP is going down - disable compression.
1974 ppp
->rstate
&= ~SC_DECOMP_RUN
;
1976 ppp
->xstate
&= ~SC_COMP_RUN
;
1980 if ((ppp
->flags
& (SC_CCP_OPEN
| SC_CCP_UP
)) != SC_CCP_OPEN
)
1984 if (len
< CCP_OPT_MINLEN
|| len
< CCP_OPT_LENGTH(dp
))
1987 /* we will start receiving compressed packets */
1988 if (ppp
->rc_state
== 0)
1990 if (ppp
->rcomp
->decomp_init(ppp
->rc_state
, dp
, len
,
1991 ppp
->file
.index
, 0, ppp
->mru
, ppp
->debug
)) {
1992 ppp
->rstate
|= SC_DECOMP_RUN
;
1993 ppp
->rstate
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
1996 /* we will soon start sending compressed packets */
1997 if (ppp
->xc_state
== 0)
1999 if (ppp
->xcomp
->comp_init(ppp
->xc_state
, dp
, len
,
2000 ppp
->file
.index
, 0, ppp
->debug
))
2001 ppp
->xstate
|= SC_COMP_RUN
;
2006 /* reset the [de]compressor */
2007 if ((ppp
->flags
& SC_CCP_UP
) == 0)
2010 if (ppp
->rc_state
&& (ppp
->rstate
& SC_DECOMP_RUN
)) {
2011 ppp
->rcomp
->decomp_reset(ppp
->rc_state
);
2012 ppp
->rstate
&= ~SC_DC_ERROR
;
2015 if (ppp
->xc_state
&& (ppp
->xstate
& SC_COMP_RUN
))
2016 ppp
->xcomp
->comp_reset(ppp
->xc_state
);
2022 /* Free up compression resources. */
2024 ppp_ccp_closed(struct ppp
*ppp
)
2026 ppp
->flags
&= ~(SC_CCP_OPEN
| SC_CCP_UP
);
2028 ppp
->xstate
&= ~SC_COMP_RUN
;
2029 if (ppp
->xc_state
) {
2030 ppp
->xcomp
->comp_free(ppp
->xc_state
);
2034 ppp
->xstate
&= ~SC_DECOMP_RUN
;
2035 if (ppp
->rc_state
) {
2036 ppp
->rcomp
->decomp_free(ppp
->rc_state
);
2041 /* List of compressors. */
2042 static LIST_HEAD(compressor_list
);
2043 static spinlock_t compressor_list_lock
= SPIN_LOCK_UNLOCKED
;
2045 struct compressor_entry
{
2046 struct list_head list
;
2047 struct compressor
*comp
;
2050 static struct compressor_entry
*
2051 find_comp_entry(int proto
)
2053 struct compressor_entry
*ce
;
2054 struct list_head
*list
= &compressor_list
;
2056 while ((list
= list
->next
) != &compressor_list
) {
2057 ce
= list_entry(list
, struct compressor_entry
, list
);
2058 if (ce
->comp
->compress_proto
== proto
)
2064 /* Register a compressor */
2066 ppp_register_compressor(struct compressor
*cp
)
2068 struct compressor_entry
*ce
;
2071 spin_lock(&compressor_list_lock
);
2073 if (find_comp_entry(cp
->compress_proto
) != 0)
2076 ce
= kmalloc(sizeof(struct compressor_entry
), GFP_KERNEL
);
2081 list_add(&ce
->list
, &compressor_list
);
2083 spin_unlock(&compressor_list_lock
);
2087 /* Unregister a compressor */
2089 ppp_unregister_compressor(struct compressor
*cp
)
2091 struct compressor_entry
*ce
;
2093 spin_lock(&compressor_list_lock
);
2094 ce
= find_comp_entry(cp
->compress_proto
);
2095 if (ce
!= 0 && ce
->comp
== cp
) {
2096 list_del(&ce
->list
);
2099 spin_unlock(&compressor_list_lock
);
2102 /* Find a compressor. */
2103 static struct compressor
*
2104 find_compressor(int type
)
2106 struct compressor_entry
*ce
;
2107 struct compressor
*cp
= 0;
2109 spin_lock(&compressor_list_lock
);
2110 ce
= find_comp_entry(type
);
2113 spin_unlock(&compressor_list_lock
);
2118 * Miscelleneous stuff.
2122 ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
)
2124 struct slcompress
*vj
= ppp
->vj
;
2126 memset(st
, 0, sizeof(*st
));
2127 st
->p
.ppp_ipackets
= ppp
->stats
.rx_packets
;
2128 st
->p
.ppp_ierrors
= ppp
->stats
.rx_errors
;
2129 st
->p
.ppp_ibytes
= ppp
->stats
.rx_bytes
;
2130 st
->p
.ppp_opackets
= ppp
->stats
.tx_packets
;
2131 st
->p
.ppp_oerrors
= ppp
->stats
.tx_errors
;
2132 st
->p
.ppp_obytes
= ppp
->stats
.tx_bytes
;
2135 st
->vj
.vjs_packets
= vj
->sls_o_compressed
+ vj
->sls_o_uncompressed
;
2136 st
->vj
.vjs_compressed
= vj
->sls_o_compressed
;
2137 st
->vj
.vjs_searches
= vj
->sls_o_searches
;
2138 st
->vj
.vjs_misses
= vj
->sls_o_misses
;
2139 st
->vj
.vjs_errorin
= vj
->sls_i_error
;
2140 st
->vj
.vjs_tossed
= vj
->sls_i_tossed
;
2141 st
->vj
.vjs_uncompressedin
= vj
->sls_i_uncompressed
;
2142 st
->vj
.vjs_compressedin
= vj
->sls_i_compressed
;
2146 * Stuff for handling the lists of ppp units and channels
2147 * and for initialization.
2151 * Create a new ppp interface unit. Fails if it can't allocate memory
2152 * or if there is already a unit with the requested number.
2153 * unit == -1 means allocate a new number.
2156 ppp_create_interface(int unit
, int *retp
)
2159 struct net_device
*dev
;
2160 struct list_head
*list
;
2165 spin_lock(&all_ppp_lock
);
2166 list
= &all_ppp_units
;
2167 while ((list
= list
->next
) != &all_ppp_units
) {
2168 ppp
= list_entry(list
, struct ppp
, file
.list
);
2169 if ((unit
< 0 && ppp
->file
.index
> last_unit
+ 1)
2170 || (unit
>= 0 && unit
< ppp
->file
.index
))
2172 if (unit
== ppp
->file
.index
)
2173 goto out
; /* unit already exists */
2174 last_unit
= ppp
->file
.index
;
2177 unit
= last_unit
+ 1;
2179 /* Create a new ppp structure and link it before `list'. */
2181 ppp
= kmalloc(sizeof(struct ppp
), GFP_KERNEL
);
2184 memset(ppp
, 0, sizeof(struct ppp
));
2185 dev
= kmalloc(sizeof(struct net_device
), GFP_KERNEL
);
2190 memset(dev
, 0, sizeof(struct net_device
));
2192 ppp
->file
.index
= unit
;
2193 sprintf(ppp
->name
, "ppp%d", 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 dev
->name
= ppp
->name
;
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
)
2380 write_lock_bh(&pch
->upl
);
2383 /* remove it from the ppp unit's list */
2386 list_del(&pch
->clist
);
2388 if (ppp
->dev
== 0 && ppp
->n_channels
== 0)
2389 /* Last disconnect from a ppp unit
2390 that is already dead: free it. */
2396 write_unlock_bh(&pch
->upl
);
2401 * Free up the resources used by a ppp channel.
2403 static void ppp_destroy_channel(struct channel
*pch
)
2405 skb_queue_purge(&pch
->file
.xq
);
2406 skb_queue_purge(&pch
->file
.rq
);
2410 void __exit
ppp_cleanup(void)
2412 /* should never happen */
2413 if (!list_empty(&all_ppp_units
) || !list_empty(&all_channels
))
2414 printk(KERN_ERR
"PPP: removing module but units remain!\n");
2415 if (devfs_unregister_chrdev(PPP_MAJOR
, "ppp") != 0)
2416 printk(KERN_ERR
"PPP: failed to unregister PPP device\n");
2417 devfs_unregister(devfs_handle
);
2420 module_init(ppp_init
);
2421 module_exit(ppp_cleanup
);
2423 EXPORT_SYMBOL(ppp_register_channel
);
2424 EXPORT_SYMBOL(ppp_unregister_channel
);
2425 EXPORT_SYMBOL(ppp_channel_index
);
2426 EXPORT_SYMBOL(ppp_unit_number
);
2427 EXPORT_SYMBOL(ppp_input
);
2428 EXPORT_SYMBOL(ppp_input_error
);
2429 EXPORT_SYMBOL(ppp_output_wakeup
);
2430 EXPORT_SYMBOL(ppp_register_compressor
);
2431 EXPORT_SYMBOL(ppp_unregister_compressor
);
2432 EXPORT_SYMBOL(ppp_channel_read
);
2433 EXPORT_SYMBOL(ppp_channel_write
);
2434 EXPORT_SYMBOL(ppp_channel_poll
);
2435 EXPORT_SYMBOL(ppp_channel_ioctl
);
2436 EXPORT_SYMBOL(all_ppp_units
); /* for debugging */
2437 EXPORT_SYMBOL(all_channels
); /* for debugging */