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 20020217==
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/filter.h>
36 #include <linux/if_ppp.h>
37 #include <linux/ppp_channel.h>
38 #include <linux/ppp-comp.h>
39 #include <linux/skbuff.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/if_arp.h>
43 #include <linux/tcp.h>
44 #include <linux/spinlock.h>
45 #include <linux/smp_lock.h>
46 #include <linux/rwsem.h>
47 #include <linux/stddef.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_fprog pass_filter; /* filter for packets to pass */
132 struct sock_fprog active_filter;/* filter for pkts to reset idle */
133 #endif /* CONFIG_PPP_FILTER */
137 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
138 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
139 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
140 * Bits in xstate: SC_COMP_RUN
142 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
143 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
144 |SC_COMP_TCP|SC_REJ_COMP_TCP)
147 * Private data structure for each channel.
148 * This includes the data structure used for multilink.
151 struct ppp_file file; /* stuff for read/write/poll */
152 struct list_head list; /* link in all/new_channels list */
153 struct ppp_channel *chan; /* public channel data structure */
154 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
155 spinlock_t downl; /* protects `chan', file.xq dequeue */
156 struct ppp *ppp; /* ppp unit we're connected to */
157 struct list_head clist; /* link in list of channels per unit */
158 rwlock_t upl; /* protects `ppp' */
159 #ifdef CONFIG_PPP_MULTILINK
160 u8 avail; /* flag used in multilink stuff */
161 u8 had_frag; /* >= 1 fragments have been sent */
162 u32 lastseq; /* MP: last sequence # received */
163 #endif /* CONFIG_PPP_MULTILINK */
167 * SMP locking issues:
168 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
169 * list and the ppp.n_channels field, you need to take both locks
170 * before you modify them.
171 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
176 * A cardmap represents a mapping from unsigned integers to pointers,
177 * and provides a fast "find lowest unused number" operation.
178 * It uses a broad (32-way) tree with a bitmap at each level.
179 * It is designed to be space-efficient for small numbers of entries
180 * and time-efficient for large numbers of entries.
182 #define CARDMAP_ORDER 5
183 #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
184 #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
189 struct cardmap *parent;
190 void *ptr[CARDMAP_WIDTH];
192 static void *cardmap_get(struct cardmap *map, unsigned int nr);
193 static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
194 static unsigned int cardmap_find_first_free(struct cardmap *map);
195 static void cardmap_destroy(struct cardmap **map);
198 * all_ppp_sem protects the all_ppp_units mapping.
199 * It also ensures that finding a ppp unit in the all_ppp_units map
200 * and updating its file.refcnt field is atomic.
202 static DECLARE_MUTEX(all_ppp_sem);
203 static struct cardmap *all_ppp_units;
204 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
207 * all_channels_lock protects all_channels and last_channel_index,
208 * and the atomicity of find a channel and updating its file.refcnt
211 static spinlock_t all_channels_lock = SPIN_LOCK_UNLOCKED;
212 static LIST_HEAD(all_channels);
213 static LIST_HEAD(new_channels);
214 static int last_channel_index;
215 static atomic_t channel_count = ATOMIC_INIT(0);
217 /* Get the PPP protocol number from a skb */
218 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
220 /* We limit the length of ppp->file.rq to this (arbitrary) value */
221 #define PPP_MAX_RQLEN 32
224 * Maximum number of multilink fragments queued up.
225 * This has to be large enough to cope with the maximum latency of
226 * the slowest channel relative to the others. Strictly it should
227 * depend on the number of channels and their characteristics.
229 #define PPP_MP_MAX_QLEN 128
231 /* Multilink header bits. */
232 #define B 0x80 /* this fragment begins a packet */
233 #define E 0x40 /* this fragment ends a packet */
235 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
236 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
237 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
240 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
241 unsigned int cmd, unsigned long arg);
242 static void ppp_xmit_process(struct ppp *ppp);
243 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
244 static void ppp_push(struct ppp *ppp);
245 static void ppp_channel_push(struct channel *pch);
246 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
247 struct channel *pch);
248 static void ppp_receive_error(struct ppp *ppp);
249 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
250 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
251 struct sk_buff *skb);
252 #ifdef CONFIG_PPP_MULTILINK
253 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
254 struct channel *pch);
255 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
256 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
257 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
258 #endif /* CONFIG_PPP_MULTILINK */
259 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
260 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
261 static void ppp_ccp_closed(struct ppp *ppp);
262 static struct compressor *find_compressor(int type);
263 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
264 static struct ppp *ppp_create_interface(int unit, int *retp);
265 static void init_ppp_file(struct ppp_file *pf, int kind);
266 static void ppp_shutdown_interface(struct ppp *ppp);
267 static void ppp_destroy_interface(struct ppp *ppp);
268 static struct ppp *ppp_find_unit(int unit);
269 static struct channel *ppp_find_channel(int unit);
270 static int ppp_connect_channel(struct channel *pch, int unit);
271 static int ppp_disconnect_channel(struct channel *pch);
272 static void ppp_destroy_channel(struct channel *pch);
274 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
275 static inline int proto_to_npindex(int proto)
294 /* Translates an NP index into a PPP protocol number */
295 static const int npindex_to_proto[NUM_NP] = {
304 /* Translates an ethertype into an NP index */
305 static inline int ethertype_to_npindex(int ethertype)
325 /* Translates an NP index into an ethertype */
326 static const int npindex_to_ethertype[NUM_NP] = {
338 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
339 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
340 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
341 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
342 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
343 ppp_recv_lock(ppp); } while (0)
344 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
345 ppp_xmit_unlock(ppp); } while (0)
348 * /dev/ppp device routines.
349 * The /dev/ppp device is used by pppd to control the ppp unit.
350 * It supports the read, write, ioctl and poll functions.
351 * Open instances of /dev/ppp can be in one of three states:
352 * unattached, attached to a ppp unit, or attached to a ppp channel.
354 static int ppp_open(struct inode *inode, struct file *file)
357 * This could (should?) be enforced by the permissions on /dev/ppp.
359 if (!capable(CAP_NET_ADMIN))
364 static int ppp_release(struct inode *inode, struct file *file)
366 struct ppp_file *pf = file->private_data;
370 file->private_data = 0;
371 if (pf->kind == INTERFACE) {
373 if (file == ppp->owner)
374 ppp_shutdown_interface(ppp);
376 if (atomic_dec_and_test(&pf->refcnt)) {
379 ppp_destroy_interface(PF_TO_PPP(pf));
382 ppp_destroy_channel(PF_TO_CHANNEL(pf));
390 static ssize_t ppp_read(struct file *file, char __user *buf,
391 size_t count, loff_t *ppos)
393 struct ppp_file *pf = file->private_data;
394 DECLARE_WAITQUEUE(wait, current);
396 struct sk_buff *skb = 0;
402 add_wait_queue(&pf->rwait, &wait);
404 set_current_state(TASK_INTERRUPTIBLE);
405 skb = skb_dequeue(&pf->rq);
412 if (file->f_flags & O_NONBLOCK)
415 if (signal_pending(current))
419 set_current_state(TASK_RUNNING);
420 remove_wait_queue(&pf->rwait, &wait);
426 if (skb->len > count)
429 if (copy_to_user(buf, skb->data, skb->len))
439 static ssize_t ppp_write(struct file *file, const char __user *buf,
440 size_t count, loff_t *ppos)
442 struct ppp_file *pf = file->private_data;
449 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
452 skb_reserve(skb, pf->hdrlen);
454 if (copy_from_user(skb_put(skb, count), buf, count)) {
459 skb_queue_tail(&pf->xq, skb);
463 ppp_xmit_process(PF_TO_PPP(pf));
466 ppp_channel_push(PF_TO_CHANNEL(pf));
476 /* No kernel lock - fine */
477 static unsigned int ppp_poll(struct file *file, poll_table *wait)
479 struct ppp_file *pf = file->private_data;
484 poll_wait(file, &pf->rwait, wait);
485 mask = POLLOUT | POLLWRNORM;
486 if (skb_peek(&pf->rq) != 0)
487 mask |= POLLIN | POLLRDNORM;
493 static int ppp_ioctl(struct inode *inode, struct file *file,
494 unsigned int cmd, unsigned long arg)
496 struct ppp_file *pf = file->private_data;
498 int err = -EFAULT, val, val2, i;
499 struct ppp_idle idle;
502 struct slcompress *vj;
505 return ppp_unattached_ioctl(pf, file, cmd, arg);
507 if (cmd == PPPIOCDETACH) {
509 * We have to be careful here... if the file descriptor
510 * has been dup'd, we could have another process in the
511 * middle of a poll using the same file *, so we had
512 * better not free the interface data structures -
513 * instead we fail the ioctl. Even in this case, we
514 * shut down the interface if we are the owner of it.
515 * Actually, we should get rid of PPPIOCDETACH, userland
516 * (i.e. pppd) could achieve the same effect by closing
517 * this fd and reopening /dev/ppp.
520 if (pf->kind == INTERFACE) {
522 if (file == ppp->owner)
523 ppp_shutdown_interface(ppp);
525 if (atomic_read(&file->f_count) <= 2) {
526 ppp_release(inode, file);
529 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
530 atomic_read(&file->f_count));
534 if (pf->kind == CHANNEL) {
535 struct channel *pch = PF_TO_CHANNEL(pf);
536 struct ppp_channel *chan;
540 if (get_user(unit, (int *) arg))
542 err = ppp_connect_channel(pch, unit);
546 err = ppp_disconnect_channel(pch);
550 down_read(&pch->chan_sem);
553 if (chan && chan->ops->ioctl)
554 err = chan->ops->ioctl(chan, cmd, arg);
555 up_read(&pch->chan_sem);
560 if (pf->kind != INTERFACE) {
562 printk(KERN_ERR "PPP: not interface or channel??\n");
569 if (get_user(val, (int *) arg))
576 if (get_user(val, (int *) arg))
579 cflags = ppp->flags & ~val;
580 ppp->flags = val & SC_FLAG_BITS;
582 if (cflags & SC_CCP_OPEN)
588 val = ppp->flags | ppp->xstate | ppp->rstate;
589 if (put_user(val, (int *) arg))
594 case PPPIOCSCOMPRESS:
595 err = ppp_set_compress(ppp, arg);
599 if (put_user(ppp->file.index, (int *) arg))
605 if (get_user(val, (int *) arg))
612 if (put_user(ppp->debug, (int *) arg))
618 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
619 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
620 if (copy_to_user((void __user *) arg, &idle, sizeof(idle)))
626 if (get_user(val, (int *) arg))
629 if ((val >> 16) != 0) {
633 vj = slhc_init(val2+1, val+1);
635 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
649 if (copy_from_user(&npi, (void __user *) arg, sizeof(npi)))
651 err = proto_to_npindex(npi.protocol);
655 if (cmd == PPPIOCGNPMODE) {
657 npi.mode = ppp->npmode[i];
658 if (copy_to_user((void __user *) arg, &npi, sizeof(npi)))
661 ppp->npmode[i] = npi.mode;
662 /* we may be able to transmit more packets now (??) */
663 netif_wake_queue(ppp->dev);
668 #ifdef CONFIG_PPP_FILTER
672 struct sock_fprog uprog, *filtp;
673 struct sock_filter *code = NULL;
676 if (copy_from_user(&uprog, (void __user *) arg, sizeof(uprog)))
679 if (uprog.len > BPF_MAXINSNS)
683 len = uprog.len * sizeof(struct sock_filter);
684 code = kmalloc(len, GFP_KERNEL);
688 if (copy_from_user(code, (void __user *) uprog.filter, len)) {
692 err = sk_chk_filter(code, uprog.len);
698 filtp = (cmd == PPPIOCSPASS)? &ppp->pass_filter: &ppp->active_filter;
701 kfree(filtp->filter);
702 filtp->filter = code;
703 filtp->len = uprog.len;
708 #endif /* CONFIG_PPP_FILTER */
710 #ifdef CONFIG_PPP_MULTILINK
712 if (get_user(val, (int *) arg))
716 ppp_recv_unlock(ppp);
719 #endif /* CONFIG_PPP_MULTILINK */
728 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
729 unsigned int cmd, unsigned long arg)
731 int unit, err = -EFAULT;
733 struct channel *chan;
737 /* Create a new ppp unit */
738 if (get_user(unit, (int *) arg))
740 ppp = ppp_create_interface(unit, &err);
743 file->private_data = &ppp->file;
746 if (put_user(ppp->file.index, (int *) arg))
752 /* Attach to an existing ppp unit */
753 if (get_user(unit, (int *) arg))
757 ppp = ppp_find_unit(unit);
759 atomic_inc(&ppp->file.refcnt);
760 file->private_data = &ppp->file;
767 if (get_user(unit, (int *) arg))
769 spin_lock_bh(&all_channels_lock);
771 chan = ppp_find_channel(unit);
773 atomic_inc(&chan->file.refcnt);
774 file->private_data = &chan->file;
777 spin_unlock_bh(&all_channels_lock);
786 static struct file_operations ppp_device_fops = {
787 .owner = THIS_MODULE,
793 .release = ppp_release
796 #define PPP_MAJOR 108
798 /* Called at boot time if ppp is compiled into the kernel,
799 or at module load time (from init_module) if compiled as a module. */
800 static int __init ppp_init(void)
804 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
805 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
807 err = devfs_mk_cdev(MKDEV(PPP_MAJOR, 0),
808 S_IFCHR|S_IRUSR|S_IWUSR, "ppp");
810 unregister_chrdev(PPP_MAJOR, "ppp");
814 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
819 * Network interface unit routines.
822 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
824 struct ppp *ppp = (struct ppp *) dev->priv;
828 npi = ethertype_to_npindex(ntohs(skb->protocol));
832 /* Drop, accept or reject the packet */
833 switch (ppp->npmode[npi]) {
837 /* it would be nice to have a way to tell the network
838 system to queue this one up for later. */
845 /* Put the 2-byte PPP protocol number on the front,
846 making sure there is room for the address and control fields. */
847 if (skb_headroom(skb) < PPP_HDRLEN) {
850 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
853 skb_reserve(ns, dev->hard_header_len);
854 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
858 pp = skb_push(skb, 2);
859 proto = npindex_to_proto[npi];
863 netif_stop_queue(dev);
864 skb_queue_tail(&ppp->file.xq, skb);
865 ppp_xmit_process(ppp);
870 ++ppp->stats.tx_dropped;
874 static struct net_device_stats *
875 ppp_net_stats(struct net_device *dev)
877 struct ppp *ppp = (struct ppp *) dev->priv;
883 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
885 struct ppp *ppp = dev->priv;
887 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
888 struct ppp_stats stats;
889 struct ppp_comp_stats cstats;
894 ppp_get_stats(ppp, &stats);
895 if (copy_to_user(addr, &stats, sizeof(stats)))
901 memset(&cstats, 0, sizeof(cstats));
902 if (ppp->xc_state != 0)
903 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
904 if (ppp->rc_state != 0)
905 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
906 if (copy_to_user(addr, &cstats, sizeof(cstats)))
913 if (copy_to_user(addr, vers, strlen(vers) + 1))
925 static void ppp_setup(struct net_device *dev)
927 dev->hard_header_len = PPP_HDRLEN;
930 dev->tx_queue_len = 3;
931 dev->type = ARPHRD_PPP;
932 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
936 * Transmit-side routines.
940 * Called to do any work queued up on the transmit side
941 * that can now be done.
944 ppp_xmit_process(struct ppp *ppp)
951 while (ppp->xmit_pending == 0
952 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
953 ppp_send_frame(ppp, skb);
954 /* If there's no work left to do, tell the core net
955 code that we can accept some more. */
956 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
957 netif_wake_queue(ppp->dev);
959 ppp_xmit_unlock(ppp);
963 * Compress and send a frame.
964 * The caller should have locked the xmit path,
965 * and xmit_pending should be 0.
968 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
970 int proto = PPP_PROTO(skb);
971 struct sk_buff *new_skb;
975 if (proto < 0x8000) {
976 #ifdef CONFIG_PPP_FILTER
977 /* check if we should pass this packet */
978 /* the filter instructions are constructed assuming
979 a four-byte PPP header on each packet */
980 *skb_push(skb, 2) = 1;
981 if (ppp->pass_filter.filter
982 && sk_run_filter(skb, ppp->pass_filter.filter,
983 ppp->pass_filter.len) == 0) {
985 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
989 /* if this packet passes the active filter, record the time */
990 if (!(ppp->active_filter.filter
991 && sk_run_filter(skb, ppp->active_filter.filter,
992 ppp->active_filter.len) == 0))
993 ppp->last_xmit = jiffies;
996 /* for data packets, record the time */
997 ppp->last_xmit = jiffies;
998 #endif /* CONFIG_PPP_FILTER */
1001 ++ppp->stats.tx_packets;
1002 ppp->stats.tx_bytes += skb->len - 2;
1006 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
1008 /* try to do VJ TCP header compression */
1009 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1012 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1015 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1017 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1018 new_skb->data + 2, &cp,
1019 !(ppp->flags & SC_NO_TCP_CCID));
1020 if (cp == skb->data + 2) {
1021 /* didn't compress */
1024 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1025 proto = PPP_VJC_COMP;
1026 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1028 proto = PPP_VJC_UNCOMP;
1029 cp[0] = skb->data[2];
1033 cp = skb_put(skb, len + 2);
1040 /* peek at outbound CCP frames */
1041 ppp_ccp_peek(ppp, skb, 0);
1045 /* try to do packet compression */
1046 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1047 && proto != PPP_LCP && proto != PPP_CCP) {
1048 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
1051 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1054 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1055 skb_reserve(new_skb,
1056 ppp->dev->hard_header_len - PPP_HDRLEN);
1058 /* compressor still expects A/C bytes in hdr */
1059 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1060 new_skb->data, skb->len + 2,
1061 ppp->dev->mtu + PPP_HDRLEN);
1062 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1066 skb_pull(skb, 2); /* pull off A/C bytes */
1068 /* didn't compress, or CCP not up yet */
1074 * If we are waiting for traffic (demand dialling),
1075 * queue it up for pppd to receive.
1077 if (ppp->flags & SC_LOOP_TRAFFIC) {
1078 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1080 skb_queue_tail(&ppp->file.rq, skb);
1081 wake_up_interruptible(&ppp->file.rwait);
1085 ppp->xmit_pending = skb;
1091 ++ppp->stats.tx_errors;
1095 * Try to send the frame in xmit_pending.
1096 * The caller should have the xmit path locked.
1099 ppp_push(struct ppp *ppp)
1101 struct list_head *list;
1102 struct channel *pch;
1103 struct sk_buff *skb = ppp->xmit_pending;
1108 list = &ppp->channels;
1109 if (list_empty(list)) {
1110 /* nowhere to send the packet, just drop it */
1111 ppp->xmit_pending = 0;
1116 if ((ppp->flags & SC_MULTILINK) == 0) {
1117 /* not doing multilink: send it down the first channel */
1119 pch = list_entry(list, struct channel, clist);
1121 spin_lock_bh(&pch->downl);
1123 if (pch->chan->ops->start_xmit(pch->chan, skb))
1124 ppp->xmit_pending = 0;
1126 /* channel got unregistered */
1128 ppp->xmit_pending = 0;
1130 spin_unlock_bh(&pch->downl);
1134 #ifdef CONFIG_PPP_MULTILINK
1135 /* Multilink: fragment the packet over as many links
1136 as can take the packet at the moment. */
1137 if (!ppp_mp_explode(ppp, skb))
1139 #endif /* CONFIG_PPP_MULTILINK */
1141 ppp->xmit_pending = 0;
1145 #ifdef CONFIG_PPP_MULTILINK
1147 * Divide a packet to be transmitted into fragments and
1148 * send them out the individual links.
1150 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1152 int nch, len, fragsize;
1153 int i, bits, hdrlen, mtu;
1155 unsigned char *p, *q;
1156 struct list_head *list;
1157 struct channel *pch;
1158 struct sk_buff *frag;
1159 struct ppp_channel *chan;
1162 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1163 list = &ppp->channels;
1164 while ((list = list->next) != &ppp->channels) {
1165 pch = list_entry(list, struct channel, clist);
1166 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1168 * If a channel hasn't had a fragment yet, it has to get
1169 * one before we send any fragments on later channels.
1170 * If it can't take a fragment now, don't give any
1171 * to subsequent channels.
1173 if (!pch->had_frag && !pch->avail) {
1174 while ((list = list->next) != &ppp->channels) {
1175 pch = list_entry(list, struct channel, clist);
1182 return 0; /* can't take now, leave it in xmit_pending */
1184 /* Do protocol field compression (XXX this should be optional) */
1192 /* decide on fragment size */
1195 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1198 fragsize = ROUNDUP(fragsize, nch);
1201 /* skip to the channel after the one we last used
1202 and start at that one */
1203 for (i = 0; i < ppp->nxchan; ++i) {
1205 if (list == &ppp->channels) {
1211 /* create a fragment for each channel */
1215 if (list == &ppp->channels) {
1219 pch = list_entry(list, struct channel, clist);
1224 /* check the channel's mtu and whether it is still attached. */
1225 spin_lock_bh(&pch->downl);
1226 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1227 /* can't use this channel */
1228 spin_unlock_bh(&pch->downl);
1236 * We have to create multiple fragments for this channel
1237 * if fragsize is greater than the channel's mtu.
1241 for (flen = fragsize; flen > 0; flen -= fnb) {
1243 if (fnb > mtu + 2 - hdrlen)
1244 fnb = mtu + 2 - hdrlen;
1247 frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1250 q = skb_put(frag, fnb + hdrlen);
1251 /* make the MP header */
1254 if (ppp->flags & SC_MP_XSHORTSEQ) {
1255 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1259 q[3] = ppp->nxseq >> 16;
1260 q[4] = ppp->nxseq >> 8;
1264 /* copy the data in */
1265 memcpy(q + hdrlen, p, fnb);
1267 /* try to send it down the channel */
1269 if (!chan->ops->start_xmit(chan, frag))
1270 skb_queue_tail(&pch->file.xq, frag);
1277 spin_unlock_bh(&pch->downl);
1284 spin_unlock_bh(&pch->downl);
1286 printk(KERN_ERR "PPP: no memory (fragment)\n");
1287 ++ppp->stats.tx_errors;
1289 return 1; /* abandon the frame */
1291 #endif /* CONFIG_PPP_MULTILINK */
1294 * Try to send data out on a channel.
1297 ppp_channel_push(struct channel *pch)
1299 struct sk_buff *skb;
1302 spin_lock_bh(&pch->downl);
1303 if (pch->chan != 0) {
1304 while (skb_queue_len(&pch->file.xq) > 0) {
1305 skb = skb_dequeue(&pch->file.xq);
1306 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1307 /* put the packet back and try again later */
1308 skb_queue_head(&pch->file.xq, skb);
1313 /* channel got deregistered */
1314 skb_queue_purge(&pch->file.xq);
1316 spin_unlock_bh(&pch->downl);
1317 /* see if there is anything from the attached unit to be sent */
1318 if (skb_queue_len(&pch->file.xq) == 0) {
1319 read_lock_bh(&pch->upl);
1322 ppp_xmit_process(ppp);
1323 read_unlock_bh(&pch->upl);
1328 * Receive-side routines.
1331 /* misuse a few fields of the skb for MP reconstruction */
1332 #define sequence priority
1333 #define BEbits cb[0]
1336 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1339 /* ppp->dev == 0 means interface is closing down */
1341 ppp_receive_frame(ppp, skb, pch);
1344 ppp_recv_unlock(ppp);
1348 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1350 struct channel *pch = chan->ppp;
1353 if (pch == 0 || skb->len == 0) {
1358 proto = PPP_PROTO(skb);
1359 read_lock_bh(&pch->upl);
1360 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1361 /* put it on the channel queue */
1362 skb_queue_tail(&pch->file.rq, skb);
1363 /* drop old frames if queue too long */
1364 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1365 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1367 wake_up_interruptible(&pch->file.rwait);
1369 ppp_do_recv(pch->ppp, skb, pch);
1371 read_unlock_bh(&pch->upl);
1374 /* Put a 0-length skb in the receive queue as an error indication */
1376 ppp_input_error(struct ppp_channel *chan, int code)
1378 struct channel *pch = chan->ppp;
1379 struct sk_buff *skb;
1384 read_lock_bh(&pch->upl);
1385 if (pch->ppp != 0) {
1386 skb = alloc_skb(0, GFP_ATOMIC);
1388 skb->len = 0; /* probably unnecessary */
1390 ppp_do_recv(pch->ppp, skb, pch);
1393 read_unlock_bh(&pch->upl);
1397 * We come in here to process a received frame.
1398 * The receive side of the ppp unit is locked.
1401 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1403 if (skb->len >= 2) {
1404 #ifdef CONFIG_PPP_MULTILINK
1405 /* XXX do channel-level decompression here */
1406 if (PPP_PROTO(skb) == PPP_MP)
1407 ppp_receive_mp_frame(ppp, skb, pch);
1409 #endif /* CONFIG_PPP_MULTILINK */
1410 ppp_receive_nonmp_frame(ppp, skb);
1415 /* note: a 0-length skb is used as an error indication */
1416 ++ppp->stats.rx_length_errors;
1419 ppp_receive_error(ppp);
1423 ppp_receive_error(struct ppp *ppp)
1425 ++ppp->stats.rx_errors;
1431 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1434 int proto, len, npi;
1437 * Decompress the frame, if compressed.
1438 * Note that some decompressors need to see uncompressed frames
1439 * that come in as well as compressed frames.
1441 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1442 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1443 skb = ppp_decompress_frame(ppp, skb);
1445 proto = PPP_PROTO(skb);
1448 /* decompress VJ compressed packets */
1449 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1452 if (skb_tailroom(skb) < 124) {
1453 /* copy to a new sk_buff with more tailroom */
1454 ns = dev_alloc_skb(skb->len + 128);
1456 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1460 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
1464 else if (!pskb_may_pull(skb, skb->len))
1467 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1469 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1474 skb_put(skb, len - skb->len);
1475 else if (len < skb->len)
1480 case PPP_VJC_UNCOMP:
1481 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1484 /* Until we fix the decompressor need to make sure
1485 * data portion is linear.
1487 if (!pskb_may_pull(skb, skb->len))
1490 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1491 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1498 ppp_ccp_peek(ppp, skb, 1);
1502 ++ppp->stats.rx_packets;
1503 ppp->stats.rx_bytes += skb->len - 2;
1505 npi = proto_to_npindex(proto);
1507 /* control or unknown frame - pass it to pppd */
1508 skb_queue_tail(&ppp->file.rq, skb);
1509 /* limit queue length by dropping old frames */
1510 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1511 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1513 /* wake up any process polling or blocking on read */
1514 wake_up_interruptible(&ppp->file.rwait);
1517 /* network protocol frame - give it to the kernel */
1519 #ifdef CONFIG_PPP_FILTER
1520 /* check if the packet passes the pass and active filters */
1521 /* the filter instructions are constructed assuming
1522 a four-byte PPP header on each packet */
1523 *skb_push(skb, 2) = 0;
1524 if (ppp->pass_filter.filter
1525 && sk_run_filter(skb, ppp->pass_filter.filter,
1526 ppp->pass_filter.len) == 0) {
1528 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1532 if (!(ppp->active_filter.filter
1533 && sk_run_filter(skb, ppp->active_filter.filter,
1534 ppp->active_filter.len) == 0))
1535 ppp->last_recv = jiffies;
1538 ppp->last_recv = jiffies;
1539 #endif /* CONFIG_PPP_FILTER */
1541 if ((ppp->dev->flags & IFF_UP) == 0
1542 || ppp->npmode[npi] != NPMODE_PASS) {
1545 skb_pull(skb, 2); /* chop off protocol */
1546 skb->dev = ppp->dev;
1547 skb->protocol = htons(npindex_to_ethertype[npi]);
1548 skb->mac.raw = skb->data;
1550 ppp->dev->last_rx = jiffies;
1557 ppp_receive_error(ppp);
1560 static struct sk_buff *
1561 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1563 int proto = PPP_PROTO(skb);
1567 /* Until we fix all the decompressor's need to make sure
1568 * data portion is linear.
1570 if (!pskb_may_pull(skb, skb->len))
1573 if (proto == PPP_COMP) {
1574 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1576 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1579 /* the decompressor still expects the A/C bytes in the hdr */
1580 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1581 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1583 /* Pass the compressed frame to pppd as an
1584 error indication. */
1585 if (len == DECOMP_FATALERROR)
1586 ppp->rstate |= SC_DC_FERROR;
1594 skb_pull(skb, 2); /* pull off the A/C bytes */
1597 /* Uncompressed frame - pass to decompressor so it
1598 can update its dictionary if necessary. */
1599 if (ppp->rcomp->incomp)
1600 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1607 ppp->rstate |= SC_DC_ERROR;
1608 ppp_receive_error(ppp);
1612 #ifdef CONFIG_PPP_MULTILINK
1614 * Receive a multilink frame.
1615 * We put it on the reconstruction queue and then pull off
1616 * as many completed frames as we can.
1619 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1622 struct list_head *l;
1623 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1625 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
1626 goto err; /* no good, throw it away */
1628 /* Decode sequence number and begin/end bits */
1629 if (ppp->flags & SC_MP_SHORTSEQ) {
1630 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1633 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1636 skb->BEbits = skb->data[2];
1637 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1640 * Do protocol ID decompression on the first fragment of each packet.
1642 if ((skb->BEbits & B) && (skb->data[0] & 1))
1643 *skb_push(skb, 1) = 0;
1646 * Expand sequence number to 32 bits, making it as close
1647 * as possible to ppp->minseq.
1649 seq |= ppp->minseq & ~mask;
1650 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1652 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1653 seq -= mask + 1; /* should never happen */
1654 skb->sequence = seq;
1658 * If this packet comes before the next one we were expecting,
1661 if (seq_before(seq, ppp->nextseq)) {
1663 ++ppp->stats.rx_dropped;
1664 ppp_receive_error(ppp);
1669 * Reevaluate minseq, the minimum over all channels of the
1670 * last sequence number received on each channel. Because of
1671 * the increasing sequence number rule, we know that any fragment
1672 * before `minseq' which hasn't arrived is never going to arrive.
1673 * The list of channels can't change because we have the receive
1674 * side of the ppp unit locked.
1676 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1677 struct channel *ch = list_entry(l, struct channel, clist);
1678 if (seq_before(ch->lastseq, seq))
1681 if (seq_before(ppp->minseq, seq))
1684 /* Put the fragment on the reconstruction queue */
1685 ppp_mp_insert(ppp, skb);
1687 /* If the queue is getting long, don't wait any longer for packets
1688 before the start of the queue. */
1689 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1690 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1691 ppp->minseq = ppp->mrq.next->sequence;
1693 /* Pull completed packets off the queue and receive them. */
1694 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1695 ppp_receive_nonmp_frame(ppp, skb);
1701 ppp_receive_error(ppp);
1705 * Insert a fragment on the MP reconstruction queue.
1706 * The queue is ordered by increasing sequence number.
1709 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1712 struct sk_buff_head *list = &ppp->mrq;
1713 u32 seq = skb->sequence;
1715 /* N.B. we don't need to lock the list lock because we have the
1716 ppp unit receive-side lock. */
1717 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1718 if (seq_before(seq, p->sequence))
1720 __skb_insert(skb, p->prev, p, list);
1724 * Reconstruct a packet from the MP fragment queue.
1725 * We go through increasing sequence numbers until we find a
1726 * complete packet, or we get to the sequence number for a fragment
1727 * which hasn't arrived but might still do so.
1730 ppp_mp_reconstruct(struct ppp *ppp)
1732 u32 seq = ppp->nextseq;
1733 u32 minseq = ppp->minseq;
1734 struct sk_buff_head *list = &ppp->mrq;
1735 struct sk_buff *p, *next;
1736 struct sk_buff *head, *tail;
1737 struct sk_buff *skb = NULL;
1738 int lost = 0, len = 0;
1740 if (ppp->mrru == 0) /* do nothing until mrru is set */
1744 for (p = head; p != (struct sk_buff *) list; p = next) {
1746 if (seq_before(p->sequence, seq)) {
1747 /* this can't happen, anyway ignore the skb */
1748 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1753 if (p->sequence != seq) {
1754 /* Fragment `seq' is missing. If it is after
1755 minseq, it might arrive later, so stop here. */
1756 if (seq_after(seq, minseq))
1758 /* Fragment `seq' is lost, keep going. */
1760 seq = seq_before(minseq, p->sequence)?
1761 minseq + 1: p->sequence;
1767 * At this point we know that all the fragments from
1768 * ppp->nextseq to seq are either present or lost.
1769 * Also, there are no complete packets in the queue
1770 * that have no missing fragments and end before this
1774 /* B bit set indicates this fragment starts a packet */
1775 if (p->BEbits & B) {
1783 /* Got a complete packet yet? */
1784 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1785 if (len > ppp->mrru + 2) {
1786 ++ppp->stats.rx_length_errors;
1787 printk(KERN_DEBUG "PPP: reconstructed packet"
1788 " is too long (%d)\n", len);
1789 } else if (p == head) {
1790 /* fragment is complete packet - reuse skb */
1794 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1795 ++ppp->stats.rx_missed_errors;
1796 printk(KERN_DEBUG "PPP: no memory for "
1797 "reconstructed packet");
1802 ppp->nextseq = seq + 1;
1806 * If this is the ending fragment of a packet,
1807 * and we haven't found a complete valid packet yet,
1808 * we can discard up to and including this fragment.
1816 /* If we have a complete packet, copy it all into one skb. */
1818 /* If we have discarded any fragments,
1819 signal a receive error. */
1820 if (head->sequence != ppp->nextseq) {
1822 printk(KERN_DEBUG " missed pkts %u..%u\n",
1823 ppp->nextseq, head->sequence-1);
1824 ++ppp->stats.rx_dropped;
1825 ppp_receive_error(ppp);
1829 /* copy to a single skb */
1830 for (p = head; p != tail->next; p = p->next)
1831 skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
1832 ppp->nextseq = tail->sequence + 1;
1836 /* Discard all the skbuffs that we have copied the data out of
1837 or that we can't use. */
1838 while ((p = list->next) != head) {
1839 __skb_unlink(p, list);
1845 #endif /* CONFIG_PPP_MULTILINK */
1848 * Channel interface.
1852 * Create a new, unattached ppp channel.
1855 ppp_register_channel(struct ppp_channel *chan)
1857 struct channel *pch;
1859 pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
1862 memset(pch, 0, sizeof(struct channel));
1866 init_ppp_file(&pch->file, CHANNEL);
1867 pch->file.hdrlen = chan->hdrlen;
1868 #ifdef CONFIG_PPP_MULTILINK
1870 #endif /* CONFIG_PPP_MULTILINK */
1871 init_rwsem(&pch->chan_sem);
1872 spin_lock_init(&pch->downl);
1873 pch->upl = RW_LOCK_UNLOCKED;
1874 spin_lock_bh(&all_channels_lock);
1875 pch->file.index = ++last_channel_index;
1876 list_add(&pch->list, &new_channels);
1877 atomic_inc(&channel_count);
1878 spin_unlock_bh(&all_channels_lock);
1883 * Return the index of a channel.
1885 int ppp_channel_index(struct ppp_channel *chan)
1887 struct channel *pch = chan->ppp;
1890 return pch->file.index;
1895 * Return the PPP unit number to which a channel is connected.
1897 int ppp_unit_number(struct ppp_channel *chan)
1899 struct channel *pch = chan->ppp;
1903 read_lock_bh(&pch->upl);
1905 unit = pch->ppp->file.index;
1906 read_unlock_bh(&pch->upl);
1912 * Disconnect a channel from the generic layer.
1913 * This must be called in process context.
1916 ppp_unregister_channel(struct ppp_channel *chan)
1918 struct channel *pch = chan->ppp;
1921 return; /* should never happen */
1925 * This ensures that we have returned from any calls into the
1926 * the channel's start_xmit or ioctl routine before we proceed.
1928 down_write(&pch->chan_sem);
1929 spin_lock_bh(&pch->downl);
1931 spin_unlock_bh(&pch->downl);
1932 up_write(&pch->chan_sem);
1933 ppp_disconnect_channel(pch);
1934 spin_lock_bh(&all_channels_lock);
1935 list_del(&pch->list);
1936 spin_unlock_bh(&all_channels_lock);
1938 wake_up_interruptible(&pch->file.rwait);
1939 if (atomic_dec_and_test(&pch->file.refcnt))
1940 ppp_destroy_channel(pch);
1944 * Callback from a channel when it can accept more to transmit.
1945 * This should be called at BH/softirq level, not interrupt level.
1948 ppp_output_wakeup(struct ppp_channel *chan)
1950 struct channel *pch = chan->ppp;
1954 ppp_channel_push(pch);
1958 * Compression control.
1961 /* Process the PPPIOCSCOMPRESS ioctl. */
1963 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1966 struct compressor *cp, *ocomp;
1967 struct ppp_option_data data;
1968 void *state, *ostate;
1969 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1972 if (copy_from_user(&data, (void __user *) arg, sizeof(data))
1973 || (data.length <= CCP_MAX_OPTION_LENGTH
1974 && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
1977 if (data.length > CCP_MAX_OPTION_LENGTH
1978 || ccp_option[1] < 2 || ccp_option[1] > data.length)
1981 cp = find_compressor(ccp_option[0]);
1984 request_module("ppp-compress-%d", ccp_option[0]);
1985 cp = find_compressor(ccp_option[0]);
1987 #endif /* CONFIG_KMOD */
1992 if (data.transmit) {
1993 state = cp->comp_alloc(ccp_option, data.length);
1996 ppp->xstate &= ~SC_COMP_RUN;
1998 ostate = ppp->xc_state;
2000 ppp->xc_state = state;
2001 ppp_xmit_unlock(ppp);
2003 ocomp->comp_free(ostate);
2004 module_put(ocomp->owner);
2008 module_put(cp->owner);
2011 state = cp->decomp_alloc(ccp_option, data.length);
2014 ppp->rstate &= ~SC_DECOMP_RUN;
2016 ostate = ppp->rc_state;
2018 ppp->rc_state = state;
2019 ppp_recv_unlock(ppp);
2021 ocomp->decomp_free(ostate);
2022 module_put(ocomp->owner);
2026 module_put(cp->owner);
2034 * Look at a CCP packet and update our state accordingly.
2035 * We assume the caller has the xmit or recv path locked.
2038 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2043 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2044 return; /* no header */
2047 switch (CCP_CODE(dp)) {
2050 /* A ConfReq starts negotiation of compression
2051 * in one direction of transmission,
2052 * and hence brings it down...but which way?
2055 * A ConfReq indicates what the sender would like to receive
2058 /* He is proposing what I should send */
2059 ppp->xstate &= ~SC_COMP_RUN;
2061 /* I am proposing to what he should send */
2062 ppp->rstate &= ~SC_DECOMP_RUN;
2069 * CCP is going down, both directions of transmission
2071 ppp->rstate &= ~SC_DECOMP_RUN;
2072 ppp->xstate &= ~SC_COMP_RUN;
2076 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2078 len = CCP_LENGTH(dp);
2079 if (!pskb_may_pull(skb, len + 2))
2080 return; /* too short */
2083 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2086 /* we will start receiving compressed packets */
2087 if (ppp->rc_state == 0)
2089 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2090 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2091 ppp->rstate |= SC_DECOMP_RUN;
2092 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2095 /* we will soon start sending compressed packets */
2096 if (ppp->xc_state == 0)
2098 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2099 ppp->file.index, 0, ppp->debug))
2100 ppp->xstate |= SC_COMP_RUN;
2105 /* reset the [de]compressor */
2106 if ((ppp->flags & SC_CCP_UP) == 0)
2109 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2110 ppp->rcomp->decomp_reset(ppp->rc_state);
2111 ppp->rstate &= ~SC_DC_ERROR;
2114 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2115 ppp->xcomp->comp_reset(ppp->xc_state);
2121 /* Free up compression resources. */
2123 ppp_ccp_closed(struct ppp *ppp)
2125 void *xstate, *rstate;
2126 struct compressor *xcomp, *rcomp;
2129 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2132 xstate = ppp->xc_state;
2136 rstate = ppp->rc_state;
2141 xcomp->comp_free(xstate);
2142 module_put(xcomp->owner);
2145 rcomp->decomp_free(rstate);
2146 module_put(rcomp->owner);
2150 /* List of compressors. */
2151 static LIST_HEAD(compressor_list);
2152 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2154 struct compressor_entry {
2155 struct list_head list;
2156 struct compressor *comp;
2159 static struct compressor_entry *
2160 find_comp_entry(int proto)
2162 struct compressor_entry *ce;
2163 struct list_head *list = &compressor_list;
2165 while ((list = list->next) != &compressor_list) {
2166 ce = list_entry(list, struct compressor_entry, list);
2167 if (ce->comp->compress_proto == proto)
2173 /* Register a compressor */
2175 ppp_register_compressor(struct compressor *cp)
2177 struct compressor_entry *ce;
2179 spin_lock(&compressor_list_lock);
2181 if (find_comp_entry(cp->compress_proto) != 0)
2184 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2189 list_add(&ce->list, &compressor_list);
2191 spin_unlock(&compressor_list_lock);
2195 /* Unregister a compressor */
2197 ppp_unregister_compressor(struct compressor *cp)
2199 struct compressor_entry *ce;
2201 spin_lock(&compressor_list_lock);
2202 ce = find_comp_entry(cp->compress_proto);
2203 if (ce != 0 && ce->comp == cp) {
2204 list_del(&ce->list);
2207 spin_unlock(&compressor_list_lock);
2210 /* Find a compressor. */
2211 static struct compressor *
2212 find_compressor(int type)
2214 struct compressor_entry *ce;
2215 struct compressor *cp = 0;
2217 spin_lock(&compressor_list_lock);
2218 ce = find_comp_entry(type);
2221 if (!try_module_get(cp->owner))
2224 spin_unlock(&compressor_list_lock);
2229 * Miscelleneous stuff.
2233 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2235 struct slcompress *vj = ppp->vj;
2237 memset(st, 0, sizeof(*st));
2238 st->p.ppp_ipackets = ppp->stats.rx_packets;
2239 st->p.ppp_ierrors = ppp->stats.rx_errors;
2240 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2241 st->p.ppp_opackets = ppp->stats.tx_packets;
2242 st->p.ppp_oerrors = ppp->stats.tx_errors;
2243 st->p.ppp_obytes = ppp->stats.tx_bytes;
2246 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2247 st->vj.vjs_compressed = vj->sls_o_compressed;
2248 st->vj.vjs_searches = vj->sls_o_searches;
2249 st->vj.vjs_misses = vj->sls_o_misses;
2250 st->vj.vjs_errorin = vj->sls_i_error;
2251 st->vj.vjs_tossed = vj->sls_i_tossed;
2252 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2253 st->vj.vjs_compressedin = vj->sls_i_compressed;
2257 * Stuff for handling the lists of ppp units and channels
2258 * and for initialization.
2262 * Create a new ppp interface unit. Fails if it can't allocate memory
2263 * or if there is already a unit with the requested number.
2264 * unit == -1 means allocate a new number.
2267 ppp_create_interface(int unit, int *retp)
2270 struct net_device *dev = NULL;
2274 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2277 dev = alloc_netdev(0, "", ppp_setup);
2280 memset(ppp, 0, sizeof(struct ppp));
2283 init_ppp_file(&ppp->file, INTERFACE);
2284 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2285 for (i = 0; i < NUM_NP; ++i)
2286 ppp->npmode[i] = NPMODE_PASS;
2287 INIT_LIST_HEAD(&ppp->channels);
2288 spin_lock_init(&ppp->rlock);
2289 spin_lock_init(&ppp->wlock);
2290 #ifdef CONFIG_PPP_MULTILINK
2292 skb_queue_head_init(&ppp->mrq);
2293 #endif /* CONFIG_PPP_MULTILINK */
2297 dev->hard_start_xmit = ppp_start_xmit;
2298 dev->get_stats = ppp_net_stats;
2299 dev->do_ioctl = ppp_net_ioctl;
2304 unit = cardmap_find_first_free(all_ppp_units);
2305 else if (cardmap_get(all_ppp_units, unit) != NULL)
2306 goto out2; /* unit already exists */
2308 /* Initialize the new ppp unit */
2309 ppp->file.index = unit;
2310 sprintf(dev->name, "ppp%d", unit);
2312 ret = register_netdev(dev);
2314 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2319 atomic_inc(&ppp_unit_count);
2320 cardmap_set(&all_ppp_units, unit, ppp);
2336 * Initialize a ppp_file structure.
2339 init_ppp_file(struct ppp_file *pf, int kind)
2342 skb_queue_head_init(&pf->xq);
2343 skb_queue_head_init(&pf->rq);
2344 atomic_set(&pf->refcnt, 1);
2345 init_waitqueue_head(&pf->rwait);
2349 * Take down a ppp interface unit - called when the owning file
2350 * (the one that created the unit) is closed or detached.
2352 static void ppp_shutdown_interface(struct ppp *ppp)
2354 struct net_device *dev;
2361 /* This will call dev_close() for us. */
2363 unregister_netdev(dev);
2366 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2369 wake_up_interruptible(&ppp->file.rwait);
2374 * Free the memory used by a ppp unit. This is only called once
2375 * there are no channels connected to the unit and no file structs
2376 * that reference the unit.
2378 static void ppp_destroy_interface(struct ppp *ppp)
2380 atomic_dec(&ppp_unit_count);
2382 if (!ppp->file.dead || ppp->n_channels) {
2383 /* "can't happen" */
2384 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2385 "n_channels=%d !\n", ppp, ppp->file.dead,
2390 ppp_ccp_closed(ppp);
2395 skb_queue_purge(&ppp->file.xq);
2396 skb_queue_purge(&ppp->file.rq);
2397 #ifdef CONFIG_PPP_MULTILINK
2398 skb_queue_purge(&ppp->mrq);
2399 #endif /* CONFIG_PPP_MULTILINK */
2400 #ifdef CONFIG_PPP_FILTER
2401 if (ppp->pass_filter.filter) {
2402 kfree(ppp->pass_filter.filter);
2403 ppp->pass_filter.filter = NULL;
2405 if (ppp->active_filter.filter) {
2406 kfree(ppp->active_filter.filter);
2407 ppp->active_filter.filter = 0;
2409 #endif /* CONFIG_PPP_FILTER */
2415 * Locate an existing ppp unit.
2416 * The caller should have locked the all_ppp_sem.
2419 ppp_find_unit(int unit)
2421 return cardmap_get(all_ppp_units, unit);
2425 * Locate an existing ppp channel.
2426 * The caller should have locked the all_channels_lock.
2427 * First we look in the new_channels list, then in the
2428 * all_channels list. If found in the new_channels list,
2429 * we move it to the all_channels list. This is for speed
2430 * when we have a lot of channels in use.
2432 static struct channel *
2433 ppp_find_channel(int unit)
2435 struct channel *pch;
2436 struct list_head *list;
2438 list = &new_channels;
2439 while ((list = list->next) != &new_channels) {
2440 pch = list_entry(list, struct channel, list);
2441 if (pch->file.index == unit) {
2442 list_del(&pch->list);
2443 list_add(&pch->list, &all_channels);
2447 list = &all_channels;
2448 while ((list = list->next) != &all_channels) {
2449 pch = list_entry(list, struct channel, list);
2450 if (pch->file.index == unit)
2457 * Connect a PPP channel to a PPP interface unit.
2460 ppp_connect_channel(struct channel *pch, int unit)
2467 ppp = ppp_find_unit(unit);
2470 write_lock_bh(&pch->upl);
2476 if (pch->file.hdrlen > ppp->file.hdrlen)
2477 ppp->file.hdrlen = pch->file.hdrlen;
2478 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2479 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2480 ppp->dev->hard_header_len = hdrlen;
2481 list_add_tail(&pch->clist, &ppp->channels);
2484 atomic_inc(&ppp->file.refcnt);
2489 write_unlock_bh(&pch->upl);
2496 * Disconnect a channel from its ppp unit.
2499 ppp_disconnect_channel(struct channel *pch)
2504 write_lock_bh(&pch->upl);
2507 write_unlock_bh(&pch->upl);
2509 /* remove it from the ppp unit's list */
2511 list_del(&pch->clist);
2514 if (atomic_dec_and_test(&ppp->file.refcnt))
2515 ppp_destroy_interface(ppp);
2522 * Free up the resources used by a ppp channel.
2524 static void ppp_destroy_channel(struct channel *pch)
2526 atomic_dec(&channel_count);
2528 if (!pch->file.dead) {
2529 /* "can't happen" */
2530 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2534 skb_queue_purge(&pch->file.xq);
2535 skb_queue_purge(&pch->file.rq);
2539 static void __exit ppp_cleanup(void)
2541 /* should never happen */
2542 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2543 printk(KERN_ERR "PPP: removing module but units remain!\n");
2544 cardmap_destroy(&all_ppp_units);
2545 if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2546 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2547 devfs_remove("ppp");
2551 * Cardmap implementation.
2553 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2558 for (p = map; p != NULL; ) {
2559 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2563 nr &= ~(CARDMAP_MASK << p->shift);
2569 static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2575 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2577 /* need a new top level */
2578 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2579 memset(np, 0, sizeof(*np));
2582 np->shift = p->shift + CARDMAP_ORDER;
2587 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2590 while (p->shift > 0) {
2591 i = (nr >> p->shift) & CARDMAP_MASK;
2592 if (p->ptr[i] == NULL) {
2593 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2594 memset(np, 0, sizeof(*np));
2595 np->shift = p->shift - CARDMAP_ORDER;
2600 clear_bit(i, &p->inuse);
2603 i = nr & CARDMAP_MASK;
2606 set_bit(i, &p->inuse);
2608 clear_bit(i, &p->inuse);
2611 static unsigned int cardmap_find_first_free(struct cardmap *map)
2614 unsigned int nr = 0;
2617 if ((p = map) == NULL)
2620 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2621 if (i >= CARDMAP_WIDTH) {
2622 if (p->parent == NULL)
2623 return CARDMAP_WIDTH << p->shift;
2625 i = (nr >> p->shift) & CARDMAP_MASK;
2626 set_bit(i, &p->inuse);
2629 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2630 if (p->shift == 0 || p->ptr[i] == NULL)
2636 static void cardmap_destroy(struct cardmap **pmap)
2638 struct cardmap *p, *np;
2641 for (p = *pmap; p != NULL; p = np) {
2642 if (p->shift != 0) {
2643 for (i = 0; i < CARDMAP_WIDTH; ++i)
2644 if (p->ptr[i] != NULL)
2646 if (i < CARDMAP_WIDTH) {
2658 /* Module/initialization stuff */
2660 module_init(ppp_init);
2661 module_exit(ppp_cleanup);
2663 EXPORT_SYMBOL(ppp_register_channel);
2664 EXPORT_SYMBOL(ppp_unregister_channel);
2665 EXPORT_SYMBOL(ppp_channel_index);
2666 EXPORT_SYMBOL(ppp_unit_number);
2667 EXPORT_SYMBOL(ppp_input);
2668 EXPORT_SYMBOL(ppp_input_error);
2669 EXPORT_SYMBOL(ppp_output_wakeup);
2670 EXPORT_SYMBOL(ppp_register_compressor);
2671 EXPORT_SYMBOL(ppp_unregister_compressor);
2672 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2673 EXPORT_SYMBOL(all_channels); /* for debugging */
2674 MODULE_LICENSE("GPL");
2675 MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
2676 MODULE_ALIAS("/dev/ppp");