2 * Generic PPP layer for Linux.
4 * Copyright 1999-2002 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 20041108==
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/kmod.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/netdevice.h>
31 #include <linux/poll.h>
32 #include <linux/ppp_defs.h>
33 #include <linux/filter.h>
34 #include <linux/if_ppp.h>
35 #include <linux/ppp_channel.h>
36 #include <linux/ppp-comp.h>
37 #include <linux/skbuff.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/if_arp.h>
41 #include <linux/tcp.h>
42 #include <linux/spinlock.h>
43 #include <linux/smp_lock.h>
44 #include <linux/rwsem.h>
45 #include <linux/stddef.h>
46 #include <linux/device.h>
47 #include <linux/mutex.h>
48 #include <net/slhc_vj.h>
49 #include <asm/atomic.h>
51 #define PPP_VERSION "2.4.2"
54 * Network protocols we support.
56 #define NP_IP 0 /* Internet Protocol V4 */
57 #define NP_IPV6 1 /* Internet Protocol V6 */
58 #define NP_IPX 2 /* IPX protocol */
59 #define NP_AT 3 /* Appletalk protocol */
60 #define NP_MPLS_UC 4 /* MPLS unicast */
61 #define NP_MPLS_MC 5 /* MPLS multicast */
62 #define NUM_NP 6 /* Number of NPs. */
64 #define MPHDRLEN 6 /* multilink protocol header length */
65 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
66 #define MIN_FRAG_SIZE 64
69 * An instance of /dev/ppp can be associated with either a ppp
70 * interface unit or a ppp channel. In both cases, file->private_data
71 * points to one of these.
77 struct sk_buff_head xq; /* pppd transmit queue */
78 struct sk_buff_head rq; /* receive queue for pppd */
79 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
80 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
81 int hdrlen; /* space to leave for headers */
82 int index; /* interface unit / channel number */
83 int dead; /* unit/channel has been shut down */
86 #define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
88 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
89 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
91 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
94 * Data structure describing one ppp unit.
95 * A ppp unit corresponds to a ppp network interface device
96 * and represents a multilink bundle.
97 * It can have 0 or more ppp channels connected to it.
100 struct ppp_file file; /* stuff for read/write/poll 0 */
101 struct file *owner; /* file that owns this unit 48 */
102 struct list_head channels; /* list of attached channels 4c */
103 int n_channels; /* how many channels are attached 54 */
104 spinlock_t rlock; /* lock for receive side 58 */
105 spinlock_t wlock; /* lock for transmit side 5c */
106 int mru; /* max receive unit 60 */
107 unsigned int flags; /* control bits 64 */
108 unsigned int xstate; /* transmit state bits 68 */
109 unsigned int rstate; /* receive state bits 6c */
110 int debug; /* debug flags 70 */
111 struct slcompress *vj; /* state for VJ header compression */
112 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
113 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
114 struct compressor *xcomp; /* transmit packet compressor 8c */
115 void *xc_state; /* its internal state 90 */
116 struct compressor *rcomp; /* receive decompressor 94 */
117 void *rc_state; /* its internal state 98 */
118 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
119 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
120 struct net_device *dev; /* network interface device a4 */
121 #ifdef CONFIG_PPP_MULTILINK
122 int nxchan; /* next channel to send something on */
123 u32 nxseq; /* next sequence number to send */
124 int mrru; /* MP: max reconst. receive unit */
125 u32 nextseq; /* MP: seq no of next packet */
126 u32 minseq; /* MP: min of most recent seqnos */
127 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
128 #endif /* CONFIG_PPP_MULTILINK */
129 struct net_device_stats stats; /* statistics */
130 #ifdef CONFIG_PPP_FILTER
131 struct sock_filter *pass_filter; /* filter for packets to pass */
132 struct sock_filter *active_filter;/* filter for pkts to reset idle */
133 unsigned pass_len, active_len;
134 #endif /* CONFIG_PPP_FILTER */
138 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
139 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
141 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
142 * Bits in xstate: SC_COMP_RUN
144 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
145 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
146 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
149 * Private data structure for each channel.
150 * This includes the data structure used for multilink.
153 struct ppp_file file; /* stuff for read/write/poll */
154 struct list_head list; /* link in all/new_channels list */
155 struct ppp_channel *chan; /* public channel data structure */
156 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
157 spinlock_t downl; /* protects `chan', file.xq dequeue */
158 struct ppp *ppp; /* ppp unit we're connected to */
159 struct list_head clist; /* link in list of channels per unit */
160 rwlock_t upl; /* protects `ppp' */
161 #ifdef CONFIG_PPP_MULTILINK
162 u8 avail; /* flag used in multilink stuff */
163 u8 had_frag; /* >= 1 fragments have been sent */
164 u32 lastseq; /* MP: last sequence # received */
165 #endif /* CONFIG_PPP_MULTILINK */
169 * SMP locking issues:
170 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
171 * list and the ppp.n_channels field, you need to take both locks
172 * before you modify them.
173 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
178 * A cardmap represents a mapping from unsigned integers to pointers,
179 * and provides a fast "find lowest unused number" operation.
180 * It uses a broad (32-way) tree with a bitmap at each level.
181 * It is designed to be space-efficient for small numbers of entries
182 * and time-efficient for large numbers of entries.
184 #define CARDMAP_ORDER 5
185 #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
186 #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
191 struct cardmap *parent;
192 void *ptr[CARDMAP_WIDTH];
194 static void *cardmap_get(struct cardmap *map, unsigned int nr);
195 static int cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
196 static unsigned int cardmap_find_first_free(struct cardmap *map);
197 static void cardmap_destroy(struct cardmap **map);
200 * all_ppp_mutex protects the all_ppp_units mapping.
201 * It also ensures that finding a ppp unit in the all_ppp_units map
202 * and updating its file.refcnt field is atomic.
204 static DEFINE_MUTEX(all_ppp_mutex);
205 static struct cardmap *all_ppp_units;
206 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
209 * all_channels_lock protects all_channels and last_channel_index,
210 * and the atomicity of find a channel and updating its file.refcnt
213 static DEFINE_SPINLOCK(all_channels_lock);
214 static LIST_HEAD(all_channels);
215 static LIST_HEAD(new_channels);
216 static int last_channel_index;
217 static atomic_t channel_count = ATOMIC_INIT(0);
219 /* Get the PPP protocol number from a skb */
220 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
222 /* We limit the length of ppp->file.rq to this (arbitrary) value */
223 #define PPP_MAX_RQLEN 32
226 * Maximum number of multilink fragments queued up.
227 * This has to be large enough to cope with the maximum latency of
228 * the slowest channel relative to the others. Strictly it should
229 * depend on the number of channels and their characteristics.
231 #define PPP_MP_MAX_QLEN 128
233 /* Multilink header bits. */
234 #define B 0x80 /* this fragment begins a packet */
235 #define E 0x40 /* this fragment ends a packet */
237 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
238 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
239 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
242 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
243 unsigned int cmd, unsigned long arg);
244 static void ppp_xmit_process(struct ppp *ppp);
245 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
246 static void ppp_push(struct ppp *ppp);
247 static void ppp_channel_push(struct channel *pch);
248 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
249 struct channel *pch);
250 static void ppp_receive_error(struct ppp *ppp);
251 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
252 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
253 struct sk_buff *skb);
254 #ifdef CONFIG_PPP_MULTILINK
255 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
256 struct channel *pch);
257 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
258 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
259 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
260 #endif /* CONFIG_PPP_MULTILINK */
261 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
262 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
263 static void ppp_ccp_closed(struct ppp *ppp);
264 static struct compressor *find_compressor(int type);
265 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
266 static struct ppp *ppp_create_interface(int unit, int *retp);
267 static void init_ppp_file(struct ppp_file *pf, int kind);
268 static void ppp_shutdown_interface(struct ppp *ppp);
269 static void ppp_destroy_interface(struct ppp *ppp);
270 static struct ppp *ppp_find_unit(int unit);
271 static struct channel *ppp_find_channel(int unit);
272 static int ppp_connect_channel(struct channel *pch, int unit);
273 static int ppp_disconnect_channel(struct channel *pch);
274 static void ppp_destroy_channel(struct channel *pch);
276 static struct class *ppp_class;
278 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
279 static inline int proto_to_npindex(int proto)
298 /* Translates an NP index into a PPP protocol number */
299 static const int npindex_to_proto[NUM_NP] = {
308 /* Translates an ethertype into an NP index */
309 static inline int ethertype_to_npindex(int ethertype)
329 /* Translates an NP index into an ethertype */
330 static const int npindex_to_ethertype[NUM_NP] = {
342 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
343 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
344 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
345 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
346 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
347 ppp_recv_lock(ppp); } while (0)
348 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
349 ppp_xmit_unlock(ppp); } while (0)
352 * /dev/ppp device routines.
353 * The /dev/ppp device is used by pppd to control the ppp unit.
354 * It supports the read, write, ioctl and poll functions.
355 * Open instances of /dev/ppp can be in one of three states:
356 * unattached, attached to a ppp unit, or attached to a ppp channel.
358 static int ppp_open(struct inode *inode, struct file *file)
361 * This could (should?) be enforced by the permissions on /dev/ppp.
363 if (!capable(CAP_NET_ADMIN))
368 static int ppp_release(struct inode *inode, struct file *file)
370 struct ppp_file *pf = file->private_data;
374 file->private_data = NULL;
375 if (pf->kind == INTERFACE) {
377 if (file == ppp->owner)
378 ppp_shutdown_interface(ppp);
380 if (atomic_dec_and_test(&pf->refcnt)) {
383 ppp_destroy_interface(PF_TO_PPP(pf));
386 ppp_destroy_channel(PF_TO_CHANNEL(pf));
394 static ssize_t ppp_read(struct file *file, char __user *buf,
395 size_t count, loff_t *ppos)
397 struct ppp_file *pf = file->private_data;
398 DECLARE_WAITQUEUE(wait, current);
400 struct sk_buff *skb = NULL;
406 add_wait_queue(&pf->rwait, &wait);
408 set_current_state(TASK_INTERRUPTIBLE);
409 skb = skb_dequeue(&pf->rq);
415 if (pf->kind == INTERFACE) {
417 * Return 0 (EOF) on an interface that has no
418 * channels connected, unless it is looping
419 * network traffic (demand mode).
421 struct ppp *ppp = PF_TO_PPP(pf);
422 if (ppp->n_channels == 0
423 && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
427 if (file->f_flags & O_NONBLOCK)
430 if (signal_pending(current))
434 set_current_state(TASK_RUNNING);
435 remove_wait_queue(&pf->rwait, &wait);
441 if (skb->len > count)
444 if (copy_to_user(buf, skb->data, skb->len))
454 static ssize_t ppp_write(struct file *file, const char __user *buf,
455 size_t count, loff_t *ppos)
457 struct ppp_file *pf = file->private_data;
464 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
467 skb_reserve(skb, pf->hdrlen);
469 if (copy_from_user(skb_put(skb, count), buf, count)) {
474 skb_queue_tail(&pf->xq, skb);
478 ppp_xmit_process(PF_TO_PPP(pf));
481 ppp_channel_push(PF_TO_CHANNEL(pf));
491 /* No kernel lock - fine */
492 static unsigned int ppp_poll(struct file *file, poll_table *wait)
494 struct ppp_file *pf = file->private_data;
499 poll_wait(file, &pf->rwait, wait);
500 mask = POLLOUT | POLLWRNORM;
501 if (skb_peek(&pf->rq) != 0)
502 mask |= POLLIN | POLLRDNORM;
505 else if (pf->kind == INTERFACE) {
506 /* see comment in ppp_read */
507 struct ppp *ppp = PF_TO_PPP(pf);
508 if (ppp->n_channels == 0
509 && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
510 mask |= POLLIN | POLLRDNORM;
516 #ifdef CONFIG_PPP_FILTER
517 static int get_filter(void __user *arg, struct sock_filter **p)
519 struct sock_fprog uprog;
520 struct sock_filter *code = NULL;
523 if (copy_from_user(&uprog, arg, sizeof(uprog)))
531 len = uprog.len * sizeof(struct sock_filter);
532 code = kmalloc(len, GFP_KERNEL);
536 if (copy_from_user(code, uprog.filter, len)) {
541 err = sk_chk_filter(code, uprog.len);
550 #endif /* CONFIG_PPP_FILTER */
552 static int ppp_ioctl(struct inode *inode, struct file *file,
553 unsigned int cmd, unsigned long arg)
555 struct ppp_file *pf = file->private_data;
557 int err = -EFAULT, val, val2, i;
558 struct ppp_idle idle;
561 struct slcompress *vj;
562 void __user *argp = (void __user *)arg;
563 int __user *p = argp;
566 return ppp_unattached_ioctl(pf, file, cmd, arg);
568 if (cmd == PPPIOCDETACH) {
570 * We have to be careful here... if the file descriptor
571 * has been dup'd, we could have another process in the
572 * middle of a poll using the same file *, so we had
573 * better not free the interface data structures -
574 * instead we fail the ioctl. Even in this case, we
575 * shut down the interface if we are the owner of it.
576 * Actually, we should get rid of PPPIOCDETACH, userland
577 * (i.e. pppd) could achieve the same effect by closing
578 * this fd and reopening /dev/ppp.
581 if (pf->kind == INTERFACE) {
583 if (file == ppp->owner)
584 ppp_shutdown_interface(ppp);
586 if (atomic_read(&file->f_count) <= 2) {
587 ppp_release(inode, file);
590 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
591 atomic_read(&file->f_count));
595 if (pf->kind == CHANNEL) {
596 struct channel *pch = PF_TO_CHANNEL(pf);
597 struct ppp_channel *chan;
601 if (get_user(unit, p))
603 err = ppp_connect_channel(pch, unit);
607 err = ppp_disconnect_channel(pch);
611 down_read(&pch->chan_sem);
614 if (chan && chan->ops->ioctl)
615 err = chan->ops->ioctl(chan, cmd, arg);
616 up_read(&pch->chan_sem);
621 if (pf->kind != INTERFACE) {
623 printk(KERN_ERR "PPP: not interface or channel??\n");
630 if (get_user(val, p))
637 if (get_user(val, p))
640 cflags = ppp->flags & ~val;
641 ppp->flags = val & SC_FLAG_BITS;
643 if (cflags & SC_CCP_OPEN)
649 val = ppp->flags | ppp->xstate | ppp->rstate;
650 if (put_user(val, p))
655 case PPPIOCSCOMPRESS:
656 err = ppp_set_compress(ppp, arg);
660 if (put_user(ppp->file.index, p))
666 if (get_user(val, p))
673 if (put_user(ppp->debug, p))
679 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
680 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
681 if (copy_to_user(argp, &idle, sizeof(idle)))
687 if (get_user(val, p))
690 if ((val >> 16) != 0) {
694 vj = slhc_init(val2+1, val+1);
696 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
710 if (copy_from_user(&npi, argp, sizeof(npi)))
712 err = proto_to_npindex(npi.protocol);
716 if (cmd == PPPIOCGNPMODE) {
718 npi.mode = ppp->npmode[i];
719 if (copy_to_user(argp, &npi, sizeof(npi)))
722 ppp->npmode[i] = npi.mode;
723 /* we may be able to transmit more packets now (??) */
724 netif_wake_queue(ppp->dev);
729 #ifdef CONFIG_PPP_FILTER
732 struct sock_filter *code;
733 err = get_filter(argp, &code);
736 kfree(ppp->pass_filter);
737 ppp->pass_filter = code;
746 struct sock_filter *code;
747 err = get_filter(argp, &code);
750 kfree(ppp->active_filter);
751 ppp->active_filter = code;
752 ppp->active_len = err;
758 #endif /* CONFIG_PPP_FILTER */
760 #ifdef CONFIG_PPP_MULTILINK
762 if (get_user(val, p))
766 ppp_recv_unlock(ppp);
769 #endif /* CONFIG_PPP_MULTILINK */
778 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
779 unsigned int cmd, unsigned long arg)
781 int unit, err = -EFAULT;
783 struct channel *chan;
784 int __user *p = (int __user *)arg;
788 /* Create a new ppp unit */
789 if (get_user(unit, p))
791 ppp = ppp_create_interface(unit, &err);
794 file->private_data = &ppp->file;
797 if (put_user(ppp->file.index, p))
803 /* Attach to an existing ppp unit */
804 if (get_user(unit, p))
806 mutex_lock(&all_ppp_mutex);
808 ppp = ppp_find_unit(unit);
810 atomic_inc(&ppp->file.refcnt);
811 file->private_data = &ppp->file;
814 mutex_unlock(&all_ppp_mutex);
818 if (get_user(unit, p))
820 spin_lock_bh(&all_channels_lock);
822 chan = ppp_find_channel(unit);
824 atomic_inc(&chan->file.refcnt);
825 file->private_data = &chan->file;
828 spin_unlock_bh(&all_channels_lock);
837 static struct file_operations ppp_device_fops = {
838 .owner = THIS_MODULE,
844 .release = ppp_release
847 #define PPP_MAJOR 108
849 /* Called at boot time if ppp is compiled into the kernel,
850 or at module load time (from init_module) if compiled as a module. */
851 static int __init ppp_init(void)
855 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
856 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
858 ppp_class = class_create(THIS_MODULE, "ppp");
859 if (IS_ERR(ppp_class)) {
860 err = PTR_ERR(ppp_class);
863 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), "ppp");
868 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
872 unregister_chrdev(PPP_MAJOR, "ppp");
877 * Network interface unit routines.
880 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
882 struct ppp *ppp = (struct ppp *) dev->priv;
886 npi = ethertype_to_npindex(ntohs(skb->protocol));
890 /* Drop, accept or reject the packet */
891 switch (ppp->npmode[npi]) {
895 /* it would be nice to have a way to tell the network
896 system to queue this one up for later. */
903 /* Put the 2-byte PPP protocol number on the front,
904 making sure there is room for the address and control fields. */
905 if (skb_headroom(skb) < PPP_HDRLEN) {
908 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
911 skb_reserve(ns, dev->hard_header_len);
912 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
916 pp = skb_push(skb, 2);
917 proto = npindex_to_proto[npi];
921 netif_stop_queue(dev);
922 skb_queue_tail(&ppp->file.xq, skb);
923 ppp_xmit_process(ppp);
928 ++ppp->stats.tx_dropped;
932 static struct net_device_stats *
933 ppp_net_stats(struct net_device *dev)
935 struct ppp *ppp = (struct ppp *) dev->priv;
941 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
943 struct ppp *ppp = dev->priv;
945 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
946 struct ppp_stats stats;
947 struct ppp_comp_stats cstats;
952 ppp_get_stats(ppp, &stats);
953 if (copy_to_user(addr, &stats, sizeof(stats)))
959 memset(&cstats, 0, sizeof(cstats));
960 if (ppp->xc_state != 0)
961 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
962 if (ppp->rc_state != 0)
963 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
964 if (copy_to_user(addr, &cstats, sizeof(cstats)))
971 if (copy_to_user(addr, vers, strlen(vers) + 1))
983 static void ppp_setup(struct net_device *dev)
985 dev->hard_header_len = PPP_HDRLEN;
988 dev->tx_queue_len = 3;
989 dev->type = ARPHRD_PPP;
990 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
994 * Transmit-side routines.
998 * Called to do any work queued up on the transmit side
999 * that can now be done.
1002 ppp_xmit_process(struct ppp *ppp)
1004 struct sk_buff *skb;
1007 if (ppp->dev != 0) {
1009 while (ppp->xmit_pending == 0
1010 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
1011 ppp_send_frame(ppp, skb);
1012 /* If there's no work left to do, tell the core net
1013 code that we can accept some more. */
1014 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
1015 netif_wake_queue(ppp->dev);
1017 ppp_xmit_unlock(ppp);
1020 static inline struct sk_buff *
1021 pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1023 struct sk_buff *new_skb;
1025 int new_skb_size = ppp->dev->mtu +
1026 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1027 int compressor_skb_size = ppp->dev->mtu +
1028 ppp->xcomp->comp_extra + PPP_HDRLEN;
1029 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1031 if (net_ratelimit())
1032 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1035 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1036 skb_reserve(new_skb,
1037 ppp->dev->hard_header_len - PPP_HDRLEN);
1039 /* compressor still expects A/C bytes in hdr */
1040 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1041 new_skb->data, skb->len + 2,
1042 compressor_skb_size);
1043 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1047 skb_pull(skb, 2); /* pull off A/C bytes */
1048 } else if (len == 0) {
1049 /* didn't compress, or CCP not up yet */
1055 * MPPE requires that we do not send unencrypted
1056 * frames. The compressor will return -1 if we
1057 * should drop the frame. We cannot simply test
1058 * the compress_proto because MPPE and MPPC share
1061 if (net_ratelimit())
1062 printk(KERN_ERR "ppp: compressor dropped pkt\n");
1071 * Compress and send a frame.
1072 * The caller should have locked the xmit path,
1073 * and xmit_pending should be 0.
1076 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1078 int proto = PPP_PROTO(skb);
1079 struct sk_buff *new_skb;
1083 if (proto < 0x8000) {
1084 #ifdef CONFIG_PPP_FILTER
1085 /* check if we should pass this packet */
1086 /* the filter instructions are constructed assuming
1087 a four-byte PPP header on each packet */
1088 *skb_push(skb, 2) = 1;
1089 if (ppp->pass_filter
1090 && sk_run_filter(skb, ppp->pass_filter,
1091 ppp->pass_len) == 0) {
1093 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
1097 /* if this packet passes the active filter, record the time */
1098 if (!(ppp->active_filter
1099 && sk_run_filter(skb, ppp->active_filter,
1100 ppp->active_len) == 0))
1101 ppp->last_xmit = jiffies;
1104 /* for data packets, record the time */
1105 ppp->last_xmit = jiffies;
1106 #endif /* CONFIG_PPP_FILTER */
1109 ++ppp->stats.tx_packets;
1110 ppp->stats.tx_bytes += skb->len - 2;
1114 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
1116 /* try to do VJ TCP header compression */
1117 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1120 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1123 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1125 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1126 new_skb->data + 2, &cp,
1127 !(ppp->flags & SC_NO_TCP_CCID));
1128 if (cp == skb->data + 2) {
1129 /* didn't compress */
1132 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1133 proto = PPP_VJC_COMP;
1134 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1136 proto = PPP_VJC_UNCOMP;
1137 cp[0] = skb->data[2];
1141 cp = skb_put(skb, len + 2);
1148 /* peek at outbound CCP frames */
1149 ppp_ccp_peek(ppp, skb, 0);
1153 /* try to do packet compression */
1154 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1155 && proto != PPP_LCP && proto != PPP_CCP) {
1156 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1157 if (net_ratelimit())
1158 printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
1161 skb = pad_compress_skb(ppp, skb);
1167 * If we are waiting for traffic (demand dialling),
1168 * queue it up for pppd to receive.
1170 if (ppp->flags & SC_LOOP_TRAFFIC) {
1171 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1173 skb_queue_tail(&ppp->file.rq, skb);
1174 wake_up_interruptible(&ppp->file.rwait);
1178 ppp->xmit_pending = skb;
1185 ++ppp->stats.tx_errors;
1189 * Try to send the frame in xmit_pending.
1190 * The caller should have the xmit path locked.
1193 ppp_push(struct ppp *ppp)
1195 struct list_head *list;
1196 struct channel *pch;
1197 struct sk_buff *skb = ppp->xmit_pending;
1202 list = &ppp->channels;
1203 if (list_empty(list)) {
1204 /* nowhere to send the packet, just drop it */
1205 ppp->xmit_pending = NULL;
1210 if ((ppp->flags & SC_MULTILINK) == 0) {
1211 /* not doing multilink: send it down the first channel */
1213 pch = list_entry(list, struct channel, clist);
1215 spin_lock_bh(&pch->downl);
1217 if (pch->chan->ops->start_xmit(pch->chan, skb))
1218 ppp->xmit_pending = NULL;
1220 /* channel got unregistered */
1222 ppp->xmit_pending = NULL;
1224 spin_unlock_bh(&pch->downl);
1228 #ifdef CONFIG_PPP_MULTILINK
1229 /* Multilink: fragment the packet over as many links
1230 as can take the packet at the moment. */
1231 if (!ppp_mp_explode(ppp, skb))
1233 #endif /* CONFIG_PPP_MULTILINK */
1235 ppp->xmit_pending = NULL;
1239 #ifdef CONFIG_PPP_MULTILINK
1241 * Divide a packet to be transmitted into fragments and
1242 * send them out the individual links.
1244 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1247 int i, bits, hdrlen, mtu;
1251 unsigned char *p, *q;
1252 struct list_head *list;
1253 struct channel *pch;
1254 struct sk_buff *frag;
1255 struct ppp_channel *chan;
1257 nfree = 0; /* # channels which have no packet already queued */
1258 navail = 0; /* total # of usable channels (not deregistered) */
1259 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1261 list_for_each_entry(pch, &ppp->channels, clist) {
1262 navail += pch->avail = (pch->chan != NULL);
1264 if (skb_queue_empty(&pch->file.xq) ||
1269 if (!pch->had_frag && i < ppp->nxchan)
1276 * Don't start sending this packet unless at least half of
1277 * the channels are free. This gives much better TCP
1278 * performance if we have a lot of channels.
1280 if (nfree == 0 || nfree < navail / 2)
1281 return 0; /* can't take now, leave it in xmit_pending */
1283 /* Do protocol field compression (XXX this should be optional) */
1292 * Decide on fragment size.
1293 * We create a fragment for each free channel regardless of
1294 * how small they are (i.e. even 0 length) in order to minimize
1295 * the time that it will take to detect when a channel drops
1300 fragsize = ROUNDUP(fragsize, nfree);
1301 /* nbigger channels get fragsize bytes, the rest get fragsize-1,
1302 except if nbigger==0, then they all get fragsize. */
1303 nbigger = len % nfree;
1305 /* skip to the channel after the one we last used
1306 and start at that one */
1307 list = &ppp->channels;
1308 for (i = 0; i < ppp->nxchan; ++i) {
1310 if (list == &ppp->channels) {
1316 /* create a fragment for each channel */
1318 while (nfree > 0 || len > 0) {
1320 if (list == &ppp->channels) {
1324 pch = list_entry(list, struct channel, clist);
1330 * Skip this channel if it has a fragment pending already and
1331 * we haven't given a fragment to all of the free channels.
1333 if (pch->avail == 1) {
1341 /* check the channel's mtu and whether it is still attached. */
1342 spin_lock_bh(&pch->downl);
1343 if (pch->chan == NULL) {
1344 /* can't use this channel, it's being deregistered */
1345 spin_unlock_bh(&pch->downl);
1353 * Create a fragment for this channel of
1354 * min(max(mtu+2-hdrlen, 4), fragsize, len) bytes.
1355 * If mtu+2-hdrlen < 4, that is a ridiculously small
1356 * MTU, so we use mtu = 2 + hdrlen.
1361 mtu = pch->chan->mtu + 2 - hdrlen;
1366 if (flen == len && nfree == 0)
1368 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
1371 q = skb_put(frag, flen + hdrlen);
1373 /* make the MP header */
1376 if (ppp->flags & SC_MP_XSHORTSEQ) {
1377 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1381 q[3] = ppp->nxseq >> 16;
1382 q[4] = ppp->nxseq >> 8;
1388 * Unfortunately there is a bug in older versions of
1389 * the Linux PPP multilink reconstruction code where it
1390 * drops 0-length fragments. Therefore we make sure the
1391 * fragment has at least one byte of data. Any bytes
1392 * we add in this situation will end up as padding on the
1393 * end of the reconstructed packet.
1396 *skb_put(frag, 1) = 0;
1398 memcpy(q + hdrlen, p, flen);
1400 /* try to send it down the channel */
1402 if (!skb_queue_empty(&pch->file.xq) ||
1403 !chan->ops->start_xmit(chan, frag))
1404 skb_queue_tail(&pch->file.xq, frag);
1410 spin_unlock_bh(&pch->downl);
1412 if (--nbigger == 0 && fragsize > 0)
1420 spin_unlock_bh(&pch->downl);
1422 printk(KERN_ERR "PPP: no memory (fragment)\n");
1423 ++ppp->stats.tx_errors;
1425 return 1; /* abandon the frame */
1427 #endif /* CONFIG_PPP_MULTILINK */
1430 * Try to send data out on a channel.
1433 ppp_channel_push(struct channel *pch)
1435 struct sk_buff *skb;
1438 spin_lock_bh(&pch->downl);
1439 if (pch->chan != 0) {
1440 while (!skb_queue_empty(&pch->file.xq)) {
1441 skb = skb_dequeue(&pch->file.xq);
1442 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1443 /* put the packet back and try again later */
1444 skb_queue_head(&pch->file.xq, skb);
1449 /* channel got deregistered */
1450 skb_queue_purge(&pch->file.xq);
1452 spin_unlock_bh(&pch->downl);
1453 /* see if there is anything from the attached unit to be sent */
1454 if (skb_queue_empty(&pch->file.xq)) {
1455 read_lock_bh(&pch->upl);
1458 ppp_xmit_process(ppp);
1459 read_unlock_bh(&pch->upl);
1464 * Receive-side routines.
1467 /* misuse a few fields of the skb for MP reconstruction */
1468 #define sequence priority
1469 #define BEbits cb[0]
1472 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1475 /* ppp->dev == 0 means interface is closing down */
1477 ppp_receive_frame(ppp, skb, pch);
1480 ppp_recv_unlock(ppp);
1484 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1486 struct channel *pch = chan->ppp;
1489 if (pch == 0 || skb->len == 0) {
1494 proto = PPP_PROTO(skb);
1495 read_lock_bh(&pch->upl);
1496 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1497 /* put it on the channel queue */
1498 skb_queue_tail(&pch->file.rq, skb);
1499 /* drop old frames if queue too long */
1500 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1501 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1503 wake_up_interruptible(&pch->file.rwait);
1505 ppp_do_recv(pch->ppp, skb, pch);
1507 read_unlock_bh(&pch->upl);
1510 /* Put a 0-length skb in the receive queue as an error indication */
1512 ppp_input_error(struct ppp_channel *chan, int code)
1514 struct channel *pch = chan->ppp;
1515 struct sk_buff *skb;
1520 read_lock_bh(&pch->upl);
1521 if (pch->ppp != 0) {
1522 skb = alloc_skb(0, GFP_ATOMIC);
1524 skb->len = 0; /* probably unnecessary */
1526 ppp_do_recv(pch->ppp, skb, pch);
1529 read_unlock_bh(&pch->upl);
1533 * We come in here to process a received frame.
1534 * The receive side of the ppp unit is locked.
1537 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1539 if (skb->len >= 2) {
1540 #ifdef CONFIG_PPP_MULTILINK
1541 /* XXX do channel-level decompression here */
1542 if (PPP_PROTO(skb) == PPP_MP)
1543 ppp_receive_mp_frame(ppp, skb, pch);
1545 #endif /* CONFIG_PPP_MULTILINK */
1546 ppp_receive_nonmp_frame(ppp, skb);
1551 /* note: a 0-length skb is used as an error indication */
1552 ++ppp->stats.rx_length_errors;
1555 ppp_receive_error(ppp);
1559 ppp_receive_error(struct ppp *ppp)
1561 ++ppp->stats.rx_errors;
1567 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1570 int proto, len, npi;
1573 * Decompress the frame, if compressed.
1574 * Note that some decompressors need to see uncompressed frames
1575 * that come in as well as compressed frames.
1577 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1578 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1579 skb = ppp_decompress_frame(ppp, skb);
1581 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
1584 proto = PPP_PROTO(skb);
1587 /* decompress VJ compressed packets */
1588 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1591 if (skb_tailroom(skb) < 124) {
1592 /* copy to a new sk_buff with more tailroom */
1593 ns = dev_alloc_skb(skb->len + 128);
1595 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1599 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
1604 skb->ip_summed = CHECKSUM_NONE;
1606 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1608 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1613 skb_put(skb, len - skb->len);
1614 else if (len < skb->len)
1619 case PPP_VJC_UNCOMP:
1620 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1623 /* Until we fix the decompressor need to make sure
1624 * data portion is linear.
1626 if (!pskb_may_pull(skb, skb->len))
1629 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1630 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1637 ppp_ccp_peek(ppp, skb, 1);
1641 ++ppp->stats.rx_packets;
1642 ppp->stats.rx_bytes += skb->len - 2;
1644 npi = proto_to_npindex(proto);
1646 /* control or unknown frame - pass it to pppd */
1647 skb_queue_tail(&ppp->file.rq, skb);
1648 /* limit queue length by dropping old frames */
1649 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1650 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1652 /* wake up any process polling or blocking on read */
1653 wake_up_interruptible(&ppp->file.rwait);
1656 /* network protocol frame - give it to the kernel */
1658 #ifdef CONFIG_PPP_FILTER
1659 /* check if the packet passes the pass and active filters */
1660 /* the filter instructions are constructed assuming
1661 a four-byte PPP header on each packet */
1662 *skb_push(skb, 2) = 0;
1663 if (ppp->pass_filter
1664 && sk_run_filter(skb, ppp->pass_filter,
1665 ppp->pass_len) == 0) {
1667 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1671 if (!(ppp->active_filter
1672 && sk_run_filter(skb, ppp->active_filter,
1673 ppp->active_len) == 0))
1674 ppp->last_recv = jiffies;
1677 ppp->last_recv = jiffies;
1678 #endif /* CONFIG_PPP_FILTER */
1680 if ((ppp->dev->flags & IFF_UP) == 0
1681 || ppp->npmode[npi] != NPMODE_PASS) {
1684 /* chop off protocol */
1685 skb_pull_rcsum(skb, 2);
1686 skb->dev = ppp->dev;
1687 skb->protocol = htons(npindex_to_ethertype[npi]);
1688 skb->mac.raw = skb->data;
1690 ppp->dev->last_rx = jiffies;
1697 ppp_receive_error(ppp);
1700 static struct sk_buff *
1701 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1703 int proto = PPP_PROTO(skb);
1707 /* Until we fix all the decompressor's need to make sure
1708 * data portion is linear.
1710 if (!pskb_may_pull(skb, skb->len))
1713 if (proto == PPP_COMP) {
1714 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1716 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1719 /* the decompressor still expects the A/C bytes in the hdr */
1720 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1721 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1723 /* Pass the compressed frame to pppd as an
1724 error indication. */
1725 if (len == DECOMP_FATALERROR)
1726 ppp->rstate |= SC_DC_FERROR;
1734 skb_pull(skb, 2); /* pull off the A/C bytes */
1737 /* Uncompressed frame - pass to decompressor so it
1738 can update its dictionary if necessary. */
1739 if (ppp->rcomp->incomp)
1740 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1747 ppp->rstate |= SC_DC_ERROR;
1748 ppp_receive_error(ppp);
1752 #ifdef CONFIG_PPP_MULTILINK
1754 * Receive a multilink frame.
1755 * We put it on the reconstruction queue and then pull off
1756 * as many completed frames as we can.
1759 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1763 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1765 if (!pskb_may_pull(skb, mphdrlen) || ppp->mrru == 0)
1766 goto err; /* no good, throw it away */
1768 /* Decode sequence number and begin/end bits */
1769 if (ppp->flags & SC_MP_SHORTSEQ) {
1770 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1773 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1776 skb->BEbits = skb->data[2];
1777 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1780 * Do protocol ID decompression on the first fragment of each packet.
1782 if ((skb->BEbits & B) && (skb->data[0] & 1))
1783 *skb_push(skb, 1) = 0;
1786 * Expand sequence number to 32 bits, making it as close
1787 * as possible to ppp->minseq.
1789 seq |= ppp->minseq & ~mask;
1790 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1792 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1793 seq -= mask + 1; /* should never happen */
1794 skb->sequence = seq;
1798 * If this packet comes before the next one we were expecting,
1801 if (seq_before(seq, ppp->nextseq)) {
1803 ++ppp->stats.rx_dropped;
1804 ppp_receive_error(ppp);
1809 * Reevaluate minseq, the minimum over all channels of the
1810 * last sequence number received on each channel. Because of
1811 * the increasing sequence number rule, we know that any fragment
1812 * before `minseq' which hasn't arrived is never going to arrive.
1813 * The list of channels can't change because we have the receive
1814 * side of the ppp unit locked.
1816 list_for_each_entry(ch, &ppp->channels, clist) {
1817 if (seq_before(ch->lastseq, seq))
1820 if (seq_before(ppp->minseq, seq))
1823 /* Put the fragment on the reconstruction queue */
1824 ppp_mp_insert(ppp, skb);
1826 /* If the queue is getting long, don't wait any longer for packets
1827 before the start of the queue. */
1828 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1829 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1830 ppp->minseq = ppp->mrq.next->sequence;
1832 /* Pull completed packets off the queue and receive them. */
1833 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1834 ppp_receive_nonmp_frame(ppp, skb);
1840 ppp_receive_error(ppp);
1844 * Insert a fragment on the MP reconstruction queue.
1845 * The queue is ordered by increasing sequence number.
1848 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1851 struct sk_buff_head *list = &ppp->mrq;
1852 u32 seq = skb->sequence;
1854 /* N.B. we don't need to lock the list lock because we have the
1855 ppp unit receive-side lock. */
1856 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1857 if (seq_before(seq, p->sequence))
1859 __skb_insert(skb, p->prev, p, list);
1863 * Reconstruct a packet from the MP fragment queue.
1864 * We go through increasing sequence numbers until we find a
1865 * complete packet, or we get to the sequence number for a fragment
1866 * which hasn't arrived but might still do so.
1869 ppp_mp_reconstruct(struct ppp *ppp)
1871 u32 seq = ppp->nextseq;
1872 u32 minseq = ppp->minseq;
1873 struct sk_buff_head *list = &ppp->mrq;
1874 struct sk_buff *p, *next;
1875 struct sk_buff *head, *tail;
1876 struct sk_buff *skb = NULL;
1877 int lost = 0, len = 0;
1879 if (ppp->mrru == 0) /* do nothing until mrru is set */
1883 for (p = head; p != (struct sk_buff *) list; p = next) {
1885 if (seq_before(p->sequence, seq)) {
1886 /* this can't happen, anyway ignore the skb */
1887 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1892 if (p->sequence != seq) {
1893 /* Fragment `seq' is missing. If it is after
1894 minseq, it might arrive later, so stop here. */
1895 if (seq_after(seq, minseq))
1897 /* Fragment `seq' is lost, keep going. */
1899 seq = seq_before(minseq, p->sequence)?
1900 minseq + 1: p->sequence;
1906 * At this point we know that all the fragments from
1907 * ppp->nextseq to seq are either present or lost.
1908 * Also, there are no complete packets in the queue
1909 * that have no missing fragments and end before this
1913 /* B bit set indicates this fragment starts a packet */
1914 if (p->BEbits & B) {
1922 /* Got a complete packet yet? */
1923 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1924 if (len > ppp->mrru + 2) {
1925 ++ppp->stats.rx_length_errors;
1926 printk(KERN_DEBUG "PPP: reconstructed packet"
1927 " is too long (%d)\n", len);
1928 } else if (p == head) {
1929 /* fragment is complete packet - reuse skb */
1933 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1934 ++ppp->stats.rx_missed_errors;
1935 printk(KERN_DEBUG "PPP: no memory for "
1936 "reconstructed packet");
1941 ppp->nextseq = seq + 1;
1945 * If this is the ending fragment of a packet,
1946 * and we haven't found a complete valid packet yet,
1947 * we can discard up to and including this fragment.
1955 /* If we have a complete packet, copy it all into one skb. */
1957 /* If we have discarded any fragments,
1958 signal a receive error. */
1959 if (head->sequence != ppp->nextseq) {
1961 printk(KERN_DEBUG " missed pkts %u..%u\n",
1962 ppp->nextseq, head->sequence-1);
1963 ++ppp->stats.rx_dropped;
1964 ppp_receive_error(ppp);
1968 /* copy to a single skb */
1969 for (p = head; p != tail->next; p = p->next)
1970 skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
1971 ppp->nextseq = tail->sequence + 1;
1975 /* Discard all the skbuffs that we have copied the data out of
1976 or that we can't use. */
1977 while ((p = list->next) != head) {
1978 __skb_unlink(p, list);
1984 #endif /* CONFIG_PPP_MULTILINK */
1987 * Channel interface.
1991 * Create a new, unattached ppp channel.
1994 ppp_register_channel(struct ppp_channel *chan)
1996 struct channel *pch;
1998 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
2004 init_ppp_file(&pch->file, CHANNEL);
2005 pch->file.hdrlen = chan->hdrlen;
2006 #ifdef CONFIG_PPP_MULTILINK
2008 #endif /* CONFIG_PPP_MULTILINK */
2009 init_rwsem(&pch->chan_sem);
2010 spin_lock_init(&pch->downl);
2011 rwlock_init(&pch->upl);
2012 spin_lock_bh(&all_channels_lock);
2013 pch->file.index = ++last_channel_index;
2014 list_add(&pch->list, &new_channels);
2015 atomic_inc(&channel_count);
2016 spin_unlock_bh(&all_channels_lock);
2021 * Return the index of a channel.
2023 int ppp_channel_index(struct ppp_channel *chan)
2025 struct channel *pch = chan->ppp;
2028 return pch->file.index;
2033 * Return the PPP unit number to which a channel is connected.
2035 int ppp_unit_number(struct ppp_channel *chan)
2037 struct channel *pch = chan->ppp;
2041 read_lock_bh(&pch->upl);
2043 unit = pch->ppp->file.index;
2044 read_unlock_bh(&pch->upl);
2050 * Disconnect a channel from the generic layer.
2051 * This must be called in process context.
2054 ppp_unregister_channel(struct ppp_channel *chan)
2056 struct channel *pch = chan->ppp;
2059 return; /* should never happen */
2063 * This ensures that we have returned from any calls into the
2064 * the channel's start_xmit or ioctl routine before we proceed.
2066 down_write(&pch->chan_sem);
2067 spin_lock_bh(&pch->downl);
2069 spin_unlock_bh(&pch->downl);
2070 up_write(&pch->chan_sem);
2071 ppp_disconnect_channel(pch);
2072 spin_lock_bh(&all_channels_lock);
2073 list_del(&pch->list);
2074 spin_unlock_bh(&all_channels_lock);
2076 wake_up_interruptible(&pch->file.rwait);
2077 if (atomic_dec_and_test(&pch->file.refcnt))
2078 ppp_destroy_channel(pch);
2082 * Callback from a channel when it can accept more to transmit.
2083 * This should be called at BH/softirq level, not interrupt level.
2086 ppp_output_wakeup(struct ppp_channel *chan)
2088 struct channel *pch = chan->ppp;
2092 ppp_channel_push(pch);
2096 * Compression control.
2099 /* Process the PPPIOCSCOMPRESS ioctl. */
2101 ppp_set_compress(struct ppp *ppp, unsigned long arg)
2104 struct compressor *cp, *ocomp;
2105 struct ppp_option_data data;
2106 void *state, *ostate;
2107 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2110 if (copy_from_user(&data, (void __user *) arg, sizeof(data))
2111 || (data.length <= CCP_MAX_OPTION_LENGTH
2112 && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
2115 if (data.length > CCP_MAX_OPTION_LENGTH
2116 || ccp_option[1] < 2 || ccp_option[1] > data.length)
2119 cp = find_compressor(ccp_option[0]);
2122 request_module("ppp-compress-%d", ccp_option[0]);
2123 cp = find_compressor(ccp_option[0]);
2125 #endif /* CONFIG_KMOD */
2130 if (data.transmit) {
2131 state = cp->comp_alloc(ccp_option, data.length);
2134 ppp->xstate &= ~SC_COMP_RUN;
2136 ostate = ppp->xc_state;
2138 ppp->xc_state = state;
2139 ppp_xmit_unlock(ppp);
2141 ocomp->comp_free(ostate);
2142 module_put(ocomp->owner);
2146 module_put(cp->owner);
2149 state = cp->decomp_alloc(ccp_option, data.length);
2152 ppp->rstate &= ~SC_DECOMP_RUN;
2154 ostate = ppp->rc_state;
2156 ppp->rc_state = state;
2157 ppp_recv_unlock(ppp);
2159 ocomp->decomp_free(ostate);
2160 module_put(ocomp->owner);
2164 module_put(cp->owner);
2172 * Look at a CCP packet and update our state accordingly.
2173 * We assume the caller has the xmit or recv path locked.
2176 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2181 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2182 return; /* no header */
2185 switch (CCP_CODE(dp)) {
2188 /* A ConfReq starts negotiation of compression
2189 * in one direction of transmission,
2190 * and hence brings it down...but which way?
2193 * A ConfReq indicates what the sender would like to receive
2196 /* He is proposing what I should send */
2197 ppp->xstate &= ~SC_COMP_RUN;
2199 /* I am proposing to what he should send */
2200 ppp->rstate &= ~SC_DECOMP_RUN;
2207 * CCP is going down, both directions of transmission
2209 ppp->rstate &= ~SC_DECOMP_RUN;
2210 ppp->xstate &= ~SC_COMP_RUN;
2214 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2216 len = CCP_LENGTH(dp);
2217 if (!pskb_may_pull(skb, len + 2))
2218 return; /* too short */
2221 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2224 /* we will start receiving compressed packets */
2225 if (ppp->rc_state == 0)
2227 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2228 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2229 ppp->rstate |= SC_DECOMP_RUN;
2230 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2233 /* we will soon start sending compressed packets */
2234 if (ppp->xc_state == 0)
2236 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2237 ppp->file.index, 0, ppp->debug))
2238 ppp->xstate |= SC_COMP_RUN;
2243 /* reset the [de]compressor */
2244 if ((ppp->flags & SC_CCP_UP) == 0)
2247 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2248 ppp->rcomp->decomp_reset(ppp->rc_state);
2249 ppp->rstate &= ~SC_DC_ERROR;
2252 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2253 ppp->xcomp->comp_reset(ppp->xc_state);
2259 /* Free up compression resources. */
2261 ppp_ccp_closed(struct ppp *ppp)
2263 void *xstate, *rstate;
2264 struct compressor *xcomp, *rcomp;
2267 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2270 xstate = ppp->xc_state;
2271 ppp->xc_state = NULL;
2274 rstate = ppp->rc_state;
2275 ppp->rc_state = NULL;
2279 xcomp->comp_free(xstate);
2280 module_put(xcomp->owner);
2283 rcomp->decomp_free(rstate);
2284 module_put(rcomp->owner);
2288 /* List of compressors. */
2289 static LIST_HEAD(compressor_list);
2290 static DEFINE_SPINLOCK(compressor_list_lock);
2292 struct compressor_entry {
2293 struct list_head list;
2294 struct compressor *comp;
2297 static struct compressor_entry *
2298 find_comp_entry(int proto)
2300 struct compressor_entry *ce;
2302 list_for_each_entry(ce, &compressor_list, list) {
2303 if (ce->comp->compress_proto == proto)
2309 /* Register a compressor */
2311 ppp_register_compressor(struct compressor *cp)
2313 struct compressor_entry *ce;
2315 spin_lock(&compressor_list_lock);
2317 if (find_comp_entry(cp->compress_proto) != 0)
2320 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2325 list_add(&ce->list, &compressor_list);
2327 spin_unlock(&compressor_list_lock);
2331 /* Unregister a compressor */
2333 ppp_unregister_compressor(struct compressor *cp)
2335 struct compressor_entry *ce;
2337 spin_lock(&compressor_list_lock);
2338 ce = find_comp_entry(cp->compress_proto);
2339 if (ce != 0 && ce->comp == cp) {
2340 list_del(&ce->list);
2343 spin_unlock(&compressor_list_lock);
2346 /* Find a compressor. */
2347 static struct compressor *
2348 find_compressor(int type)
2350 struct compressor_entry *ce;
2351 struct compressor *cp = NULL;
2353 spin_lock(&compressor_list_lock);
2354 ce = find_comp_entry(type);
2357 if (!try_module_get(cp->owner))
2360 spin_unlock(&compressor_list_lock);
2365 * Miscelleneous stuff.
2369 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2371 struct slcompress *vj = ppp->vj;
2373 memset(st, 0, sizeof(*st));
2374 st->p.ppp_ipackets = ppp->stats.rx_packets;
2375 st->p.ppp_ierrors = ppp->stats.rx_errors;
2376 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2377 st->p.ppp_opackets = ppp->stats.tx_packets;
2378 st->p.ppp_oerrors = ppp->stats.tx_errors;
2379 st->p.ppp_obytes = ppp->stats.tx_bytes;
2382 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2383 st->vj.vjs_compressed = vj->sls_o_compressed;
2384 st->vj.vjs_searches = vj->sls_o_searches;
2385 st->vj.vjs_misses = vj->sls_o_misses;
2386 st->vj.vjs_errorin = vj->sls_i_error;
2387 st->vj.vjs_tossed = vj->sls_i_tossed;
2388 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2389 st->vj.vjs_compressedin = vj->sls_i_compressed;
2393 * Stuff for handling the lists of ppp units and channels
2394 * and for initialization.
2398 * Create a new ppp interface unit. Fails if it can't allocate memory
2399 * or if there is already a unit with the requested number.
2400 * unit == -1 means allocate a new number.
2403 ppp_create_interface(int unit, int *retp)
2406 struct net_device *dev = NULL;
2410 ppp = kzalloc(sizeof(struct ppp), GFP_KERNEL);
2413 dev = alloc_netdev(0, "", ppp_setup);
2418 init_ppp_file(&ppp->file, INTERFACE);
2419 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2420 for (i = 0; i < NUM_NP; ++i)
2421 ppp->npmode[i] = NPMODE_PASS;
2422 INIT_LIST_HEAD(&ppp->channels);
2423 spin_lock_init(&ppp->rlock);
2424 spin_lock_init(&ppp->wlock);
2425 #ifdef CONFIG_PPP_MULTILINK
2427 skb_queue_head_init(&ppp->mrq);
2428 #endif /* CONFIG_PPP_MULTILINK */
2432 dev->hard_start_xmit = ppp_start_xmit;
2433 dev->get_stats = ppp_net_stats;
2434 dev->do_ioctl = ppp_net_ioctl;
2437 mutex_lock(&all_ppp_mutex);
2439 unit = cardmap_find_first_free(all_ppp_units);
2440 else if (cardmap_get(all_ppp_units, unit) != NULL)
2441 goto out2; /* unit already exists */
2443 /* Initialize the new ppp unit */
2444 ppp->file.index = unit;
2445 sprintf(dev->name, "ppp%d", unit);
2447 ret = register_netdev(dev);
2449 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2454 atomic_inc(&ppp_unit_count);
2455 ret = cardmap_set(&all_ppp_units, unit, ppp);
2459 mutex_unlock(&all_ppp_mutex);
2464 atomic_dec(&ppp_unit_count);
2466 mutex_unlock(&all_ppp_mutex);
2476 * Initialize a ppp_file structure.
2479 init_ppp_file(struct ppp_file *pf, int kind)
2482 skb_queue_head_init(&pf->xq);
2483 skb_queue_head_init(&pf->rq);
2484 atomic_set(&pf->refcnt, 1);
2485 init_waitqueue_head(&pf->rwait);
2489 * Take down a ppp interface unit - called when the owning file
2490 * (the one that created the unit) is closed or detached.
2492 static void ppp_shutdown_interface(struct ppp *ppp)
2494 struct net_device *dev;
2496 mutex_lock(&all_ppp_mutex);
2501 /* This will call dev_close() for us. */
2503 unregister_netdev(dev);
2506 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2509 wake_up_interruptible(&ppp->file.rwait);
2510 mutex_unlock(&all_ppp_mutex);
2514 * Free the memory used by a ppp unit. This is only called once
2515 * there are no channels connected to the unit and no file structs
2516 * that reference the unit.
2518 static void ppp_destroy_interface(struct ppp *ppp)
2520 atomic_dec(&ppp_unit_count);
2522 if (!ppp->file.dead || ppp->n_channels) {
2523 /* "can't happen" */
2524 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2525 "n_channels=%d !\n", ppp, ppp->file.dead,
2530 ppp_ccp_closed(ppp);
2535 skb_queue_purge(&ppp->file.xq);
2536 skb_queue_purge(&ppp->file.rq);
2537 #ifdef CONFIG_PPP_MULTILINK
2538 skb_queue_purge(&ppp->mrq);
2539 #endif /* CONFIG_PPP_MULTILINK */
2540 #ifdef CONFIG_PPP_FILTER
2541 kfree(ppp->pass_filter);
2542 ppp->pass_filter = NULL;
2543 kfree(ppp->active_filter);
2544 ppp->active_filter = NULL;
2545 #endif /* CONFIG_PPP_FILTER */
2551 * Locate an existing ppp unit.
2552 * The caller should have locked the all_ppp_mutex.
2555 ppp_find_unit(int unit)
2557 return cardmap_get(all_ppp_units, unit);
2561 * Locate an existing ppp channel.
2562 * The caller should have locked the all_channels_lock.
2563 * First we look in the new_channels list, then in the
2564 * all_channels list. If found in the new_channels list,
2565 * we move it to the all_channels list. This is for speed
2566 * when we have a lot of channels in use.
2568 static struct channel *
2569 ppp_find_channel(int unit)
2571 struct channel *pch;
2573 list_for_each_entry(pch, &new_channels, list) {
2574 if (pch->file.index == unit) {
2575 list_move(&pch->list, &all_channels);
2579 list_for_each_entry(pch, &all_channels, list) {
2580 if (pch->file.index == unit)
2587 * Connect a PPP channel to a PPP interface unit.
2590 ppp_connect_channel(struct channel *pch, int unit)
2596 mutex_lock(&all_ppp_mutex);
2597 ppp = ppp_find_unit(unit);
2600 write_lock_bh(&pch->upl);
2606 if (pch->file.hdrlen > ppp->file.hdrlen)
2607 ppp->file.hdrlen = pch->file.hdrlen;
2608 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2609 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2610 ppp->dev->hard_header_len = hdrlen;
2611 list_add_tail(&pch->clist, &ppp->channels);
2614 atomic_inc(&ppp->file.refcnt);
2619 write_unlock_bh(&pch->upl);
2621 mutex_unlock(&all_ppp_mutex);
2626 * Disconnect a channel from its ppp unit.
2629 ppp_disconnect_channel(struct channel *pch)
2634 write_lock_bh(&pch->upl);
2637 write_unlock_bh(&pch->upl);
2639 /* remove it from the ppp unit's list */
2641 list_del(&pch->clist);
2642 if (--ppp->n_channels == 0)
2643 wake_up_interruptible(&ppp->file.rwait);
2645 if (atomic_dec_and_test(&ppp->file.refcnt))
2646 ppp_destroy_interface(ppp);
2653 * Free up the resources used by a ppp channel.
2655 static void ppp_destroy_channel(struct channel *pch)
2657 atomic_dec(&channel_count);
2659 if (!pch->file.dead) {
2660 /* "can't happen" */
2661 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2665 skb_queue_purge(&pch->file.xq);
2666 skb_queue_purge(&pch->file.rq);
2670 static void __exit ppp_cleanup(void)
2672 /* should never happen */
2673 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2674 printk(KERN_ERR "PPP: removing module but units remain!\n");
2675 cardmap_destroy(&all_ppp_units);
2676 if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2677 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2678 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
2679 class_destroy(ppp_class);
2683 * Cardmap implementation.
2685 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2690 for (p = map; p != NULL; ) {
2691 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2695 nr &= ~(CARDMAP_MASK << p->shift);
2701 static int cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2707 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2709 /* need a new top level */
2710 struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
2715 np->shift = p->shift + CARDMAP_ORDER;
2720 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2723 while (p->shift > 0) {
2724 i = (nr >> p->shift) & CARDMAP_MASK;
2725 if (p->ptr[i] == NULL) {
2726 struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
2729 np->shift = p->shift - CARDMAP_ORDER;
2734 clear_bit(i, &p->inuse);
2737 i = nr & CARDMAP_MASK;
2740 set_bit(i, &p->inuse);
2742 clear_bit(i, &p->inuse);
2748 static unsigned int cardmap_find_first_free(struct cardmap *map)
2751 unsigned int nr = 0;
2754 if ((p = map) == NULL)
2757 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2758 if (i >= CARDMAP_WIDTH) {
2759 if (p->parent == NULL)
2760 return CARDMAP_WIDTH << p->shift;
2762 i = (nr >> p->shift) & CARDMAP_MASK;
2763 set_bit(i, &p->inuse);
2766 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2767 if (p->shift == 0 || p->ptr[i] == NULL)
2773 static void cardmap_destroy(struct cardmap **pmap)
2775 struct cardmap *p, *np;
2778 for (p = *pmap; p != NULL; p = np) {
2779 if (p->shift != 0) {
2780 for (i = 0; i < CARDMAP_WIDTH; ++i)
2781 if (p->ptr[i] != NULL)
2783 if (i < CARDMAP_WIDTH) {
2795 /* Module/initialization stuff */
2797 module_init(ppp_init);
2798 module_exit(ppp_cleanup);
2800 EXPORT_SYMBOL(ppp_register_channel);
2801 EXPORT_SYMBOL(ppp_unregister_channel);
2802 EXPORT_SYMBOL(ppp_channel_index);
2803 EXPORT_SYMBOL(ppp_unit_number);
2804 EXPORT_SYMBOL(ppp_input);
2805 EXPORT_SYMBOL(ppp_input_error);
2806 EXPORT_SYMBOL(ppp_output_wakeup);
2807 EXPORT_SYMBOL(ppp_register_compressor);
2808 EXPORT_SYMBOL(ppp_unregister_compressor);
2809 MODULE_LICENSE("GPL");
2810 MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
2811 MODULE_ALIAS("/dev/ppp");