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 NUM_NP 4 /* Number of NPs. */
62 #define MPHDRLEN 6 /* multilink protocol header length */
63 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
64 #define MIN_FRAG_SIZE 64
67 * An instance of /dev/ppp can be associated with either a ppp
68 * interface unit or a ppp channel. In both cases, file->private_data
69 * points to one of these.
75 struct sk_buff_head xq; /* pppd transmit queue */
76 struct sk_buff_head rq; /* receive queue for pppd */
77 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
78 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
79 int hdrlen; /* space to leave for headers */
80 int index; /* interface unit / channel number */
81 int dead; /* unit/channel has been shut down */
84 #define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
86 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
87 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
89 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
92 * Data structure describing one ppp unit.
93 * A ppp unit corresponds to a ppp network interface device
94 * and represents a multilink bundle.
95 * It can have 0 or more ppp channels connected to it.
98 struct ppp_file file; /* stuff for read/write/poll 0 */
99 struct file *owner; /* file that owns this unit 48 */
100 struct list_head channels; /* list of attached channels 4c */
101 int n_channels; /* how many channels are attached 54 */
102 spinlock_t rlock; /* lock for receive side 58 */
103 spinlock_t wlock; /* lock for transmit side 5c */
104 int mru; /* max receive unit 60 */
105 unsigned int flags; /* control bits 64 */
106 unsigned int xstate; /* transmit state bits 68 */
107 unsigned int rstate; /* receive state bits 6c */
108 int debug; /* debug flags 70 */
109 struct slcompress *vj; /* state for VJ header compression */
110 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
111 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
112 struct compressor *xcomp; /* transmit packet compressor 8c */
113 void *xc_state; /* its internal state 90 */
114 struct compressor *rcomp; /* receive decompressor 94 */
115 void *rc_state; /* its internal state 98 */
116 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
117 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
118 struct net_device *dev; /* network interface device a4 */
119 #ifdef CONFIG_PPP_MULTILINK
120 int nxchan; /* next channel to send something on */
121 u32 nxseq; /* next sequence number to send */
122 int mrru; /* MP: max reconst. receive unit */
123 u32 nextseq; /* MP: seq no of next packet */
124 u32 minseq; /* MP: min of most recent seqnos */
125 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
126 #endif /* CONFIG_PPP_MULTILINK */
127 struct net_device_stats stats; /* statistics */
128 #ifdef CONFIG_PPP_FILTER
129 struct sock_fprog pass_filter; /* filter for packets to pass */
130 struct sock_fprog active_filter;/* filter for pkts to reset idle */
131 #endif /* CONFIG_PPP_FILTER */
135 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
136 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
137 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
138 * Bits in xstate: SC_COMP_RUN
140 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
141 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
142 |SC_COMP_TCP|SC_REJ_COMP_TCP)
145 * Private data structure for each channel.
146 * This includes the data structure used for multilink.
149 struct ppp_file file; /* stuff for read/write/poll */
150 struct list_head list; /* link in all/new_channels list */
151 struct ppp_channel *chan; /* public channel data structure */
152 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
153 spinlock_t downl; /* protects `chan', file.xq dequeue */
154 struct ppp *ppp; /* ppp unit we're connected to */
155 struct list_head clist; /* link in list of channels per unit */
156 rwlock_t upl; /* protects `ppp' */
157 #ifdef CONFIG_PPP_MULTILINK
158 u8 avail; /* flag used in multilink stuff */
159 u8 had_frag; /* >= 1 fragments have been sent */
160 u32 lastseq; /* MP: last sequence # received */
161 #endif /* CONFIG_PPP_MULTILINK */
165 * SMP locking issues:
166 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
167 * list and the ppp.n_channels field, you need to take both locks
168 * before you modify them.
169 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
174 * A cardmap represents a mapping from unsigned integers to pointers,
175 * and provides a fast "find lowest unused number" operation.
176 * It uses a broad (32-way) tree with a bitmap at each level.
177 * It is designed to be space-efficient for small numbers of entries
178 * and time-efficient for large numbers of entries.
180 #define CARDMAP_ORDER 5
181 #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
182 #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
187 struct cardmap *parent;
188 void *ptr[CARDMAP_WIDTH];
190 static void *cardmap_get(struct cardmap *map, unsigned int nr);
191 static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
192 static unsigned int cardmap_find_first_free(struct cardmap *map);
193 static void cardmap_destroy(struct cardmap **map);
196 * all_ppp_sem protects the all_ppp_units mapping.
197 * It also ensures that finding a ppp unit in the all_ppp_units map
198 * and updating its file.refcnt field is atomic.
200 static DECLARE_MUTEX(all_ppp_sem);
201 static struct cardmap *all_ppp_units;
202 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
205 * all_channels_lock protects all_channels and last_channel_index,
206 * and the atomicity of find a channel and updating its file.refcnt
209 static spinlock_t all_channels_lock = SPIN_LOCK_UNLOCKED;
210 static LIST_HEAD(all_channels);
211 static LIST_HEAD(new_channels);
212 static int last_channel_index;
213 static atomic_t channel_count = ATOMIC_INIT(0);
215 /* Get the PPP protocol number from a skb */
216 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
218 /* We limit the length of ppp->file.rq to this (arbitrary) value */
219 #define PPP_MAX_RQLEN 32
222 * Maximum number of multilink fragments queued up.
223 * This has to be large enough to cope with the maximum latency of
224 * the slowest channel relative to the others. Strictly it should
225 * depend on the number of channels and their characteristics.
227 #define PPP_MP_MAX_QLEN 128
229 /* Multilink header bits. */
230 #define B 0x80 /* this fragment begins a packet */
231 #define E 0x40 /* this fragment ends a packet */
233 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
234 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
235 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
238 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
239 unsigned int cmd, unsigned long arg);
240 static void ppp_xmit_process(struct ppp *ppp);
241 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
242 static void ppp_push(struct ppp *ppp);
243 static void ppp_channel_push(struct channel *pch);
244 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
245 struct channel *pch);
246 static void ppp_receive_error(struct ppp *ppp);
247 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
248 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
249 struct sk_buff *skb);
250 #ifdef CONFIG_PPP_MULTILINK
251 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
252 struct channel *pch);
253 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
254 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
255 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
256 #endif /* CONFIG_PPP_MULTILINK */
257 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
258 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
259 static void ppp_ccp_closed(struct ppp *ppp);
260 static struct compressor *find_compressor(int type);
261 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
262 static struct ppp *ppp_create_interface(int unit, int *retp);
263 static void init_ppp_file(struct ppp_file *pf, int kind);
264 static void ppp_shutdown_interface(struct ppp *ppp);
265 static void ppp_destroy_interface(struct ppp *ppp);
266 static struct ppp *ppp_find_unit(int unit);
267 static struct channel *ppp_find_channel(int unit);
268 static int ppp_connect_channel(struct channel *pch, int unit);
269 static int ppp_disconnect_channel(struct channel *pch);
270 static void ppp_destroy_channel(struct channel *pch);
272 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
273 static inline int proto_to_npindex(int proto)
288 /* Translates an NP index into a PPP protocol number */
289 static const int npindex_to_proto[NUM_NP] = {
296 /* Translates an ethertype into an NP index */
297 static inline int ethertype_to_npindex(int ethertype)
313 /* Translates an NP index into an ethertype */
314 static const int npindex_to_ethertype[NUM_NP] = {
324 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
325 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
326 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
327 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
328 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
329 ppp_recv_lock(ppp); } while (0)
330 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
331 ppp_xmit_unlock(ppp); } while (0)
334 * /dev/ppp device routines.
335 * The /dev/ppp device is used by pppd to control the ppp unit.
336 * It supports the read, write, ioctl and poll functions.
337 * Open instances of /dev/ppp can be in one of three states:
338 * unattached, attached to a ppp unit, or attached to a ppp channel.
340 static int ppp_open(struct inode *inode, struct file *file)
343 * This could (should?) be enforced by the permissions on /dev/ppp.
345 if (!capable(CAP_NET_ADMIN))
350 static int ppp_release(struct inode *inode, struct file *file)
352 struct ppp_file *pf = file->private_data;
356 file->private_data = 0;
357 if (pf->kind == INTERFACE) {
359 if (file == ppp->owner)
360 ppp_shutdown_interface(ppp);
362 if (atomic_dec_and_test(&pf->refcnt)) {
365 ppp_destroy_interface(PF_TO_PPP(pf));
368 ppp_destroy_channel(PF_TO_CHANNEL(pf));
376 static ssize_t ppp_read(struct file *file, char *buf,
377 size_t count, loff_t *ppos)
379 struct ppp_file *pf = file->private_data;
380 DECLARE_WAITQUEUE(wait, current);
382 struct sk_buff *skb = 0;
386 add_wait_queue(&pf->rwait, &wait);
388 set_current_state(TASK_INTERRUPTIBLE);
389 skb = skb_dequeue(&pf->rq);
396 if (file->f_flags & O_NONBLOCK)
399 if (signal_pending(current))
403 set_current_state(TASK_RUNNING);
404 remove_wait_queue(&pf->rwait, &wait);
410 if (skb->len > count)
413 if (copy_to_user(buf, skb->data, skb->len))
423 static ssize_t ppp_write(struct file *file, const char *buf,
424 size_t count, loff_t *ppos)
426 struct ppp_file *pf = file->private_data;
433 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
436 skb_reserve(skb, pf->hdrlen);
438 if (copy_from_user(skb_put(skb, count), buf, count)) {
443 skb_queue_tail(&pf->xq, skb);
447 ppp_xmit_process(PF_TO_PPP(pf));
450 ppp_channel_push(PF_TO_CHANNEL(pf));
460 /* No kernel lock - fine */
461 static unsigned int ppp_poll(struct file *file, poll_table *wait)
463 struct ppp_file *pf = file->private_data;
468 poll_wait(file, &pf->rwait, wait);
469 mask = POLLOUT | POLLWRNORM;
470 if (skb_peek(&pf->rq) != 0)
471 mask |= POLLIN | POLLRDNORM;
477 static int ppp_ioctl(struct inode *inode, struct file *file,
478 unsigned int cmd, unsigned long arg)
480 struct ppp_file *pf = file->private_data;
482 int err = -EFAULT, val, val2, i;
483 struct ppp_idle idle;
486 struct slcompress *vj;
489 return ppp_unattached_ioctl(pf, file, cmd, arg);
491 if (cmd == PPPIOCDETACH) {
493 * We have to be careful here... if the file descriptor
494 * has been dup'd, we could have another process in the
495 * middle of a poll using the same file *, so we had
496 * better not free the interface data structures -
497 * instead we fail the ioctl. Even in this case, we
498 * shut down the interface if we are the owner of it.
499 * Actually, we should get rid of PPPIOCDETACH, userland
500 * (i.e. pppd) could achieve the same effect by closing
501 * this fd and reopening /dev/ppp.
504 if (pf->kind == INTERFACE) {
506 if (file == ppp->owner)
507 ppp_shutdown_interface(ppp);
509 if (atomic_read(&file->f_count) <= 2) {
510 ppp_release(inode, file);
513 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
514 atomic_read(&file->f_count));
518 if (pf->kind == CHANNEL) {
519 struct channel *pch = PF_TO_CHANNEL(pf);
520 struct ppp_channel *chan;
524 if (get_user(unit, (int *) arg))
526 err = ppp_connect_channel(pch, unit);
530 err = ppp_disconnect_channel(pch);
534 down_read(&pch->chan_sem);
537 if (chan && chan->ops->ioctl)
538 err = chan->ops->ioctl(chan, cmd, arg);
539 up_read(&pch->chan_sem);
544 if (pf->kind != INTERFACE) {
546 printk(KERN_ERR "PPP: not interface or channel??\n");
553 if (get_user(val, (int *) arg))
560 if (get_user(val, (int *) arg))
563 cflags = ppp->flags & ~val;
564 ppp->flags = val & SC_FLAG_BITS;
566 if (cflags & SC_CCP_OPEN)
572 val = ppp->flags | ppp->xstate | ppp->rstate;
573 if (put_user(val, (int *) arg))
578 case PPPIOCSCOMPRESS:
579 err = ppp_set_compress(ppp, arg);
583 if (put_user(ppp->file.index, (int *) arg))
589 if (get_user(val, (int *) arg))
596 if (put_user(ppp->debug, (int *) arg))
602 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
603 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
604 if (copy_to_user((void *) arg, &idle, sizeof(idle)))
610 if (get_user(val, (int *) arg))
613 if ((val >> 16) != 0) {
617 vj = slhc_init(val2+1, val+1);
619 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
633 if (copy_from_user(&npi, (void *) arg, sizeof(npi)))
635 err = proto_to_npindex(npi.protocol);
639 if (cmd == PPPIOCGNPMODE) {
641 npi.mode = ppp->npmode[i];
642 if (copy_to_user((void *) arg, &npi, sizeof(npi)))
645 ppp->npmode[i] = npi.mode;
646 /* we may be able to transmit more packets now (??) */
647 netif_wake_queue(ppp->dev);
652 #ifdef CONFIG_PPP_FILTER
656 struct sock_fprog uprog, *filtp;
657 struct sock_filter *code = NULL;
660 if (copy_from_user(&uprog, (void *) arg, sizeof(uprog)))
662 if (uprog.len > 0 && uprog.len < 65536) {
664 len = uprog.len * sizeof(struct sock_filter);
665 code = kmalloc(len, GFP_KERNEL);
669 if (copy_from_user(code, uprog.filter, len))
671 err = sk_chk_filter(code, uprog.len);
677 filtp = (cmd == PPPIOCSPASS)? &ppp->pass_filter: &ppp->active_filter;
680 kfree(filtp->filter);
681 filtp->filter = code;
682 filtp->len = uprog.len;
687 #endif /* CONFIG_PPP_FILTER */
689 #ifdef CONFIG_PPP_MULTILINK
691 if (get_user(val, (int *) arg))
695 ppp_recv_unlock(ppp);
698 #endif /* CONFIG_PPP_MULTILINK */
707 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
708 unsigned int cmd, unsigned long arg)
710 int unit, err = -EFAULT;
712 struct channel *chan;
716 /* Create a new ppp unit */
717 if (get_user(unit, (int *) arg))
719 ppp = ppp_create_interface(unit, &err);
722 file->private_data = &ppp->file;
725 if (put_user(ppp->file.index, (int *) arg))
731 /* Attach to an existing ppp unit */
732 if (get_user(unit, (int *) arg))
736 ppp = ppp_find_unit(unit);
738 atomic_inc(&ppp->file.refcnt);
739 file->private_data = &ppp->file;
746 if (get_user(unit, (int *) arg))
748 spin_lock_bh(&all_channels_lock);
750 chan = ppp_find_channel(unit);
752 atomic_inc(&chan->file.refcnt);
753 file->private_data = &chan->file;
756 spin_unlock_bh(&all_channels_lock);
765 static struct file_operations ppp_device_fops = {
775 #define PPP_MAJOR 108
777 static devfs_handle_t devfs_handle;
779 /* Called at boot time if ppp is compiled into the kernel,
780 or at module load time (from init_module) if compiled as a module. */
781 int __init ppp_init(void)
785 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
786 err = devfs_register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
788 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
789 devfs_handle = devfs_register(NULL, "ppp", DEVFS_FL_DEFAULT,
791 S_IFCHR | S_IRUSR | S_IWUSR,
792 &ppp_device_fops, NULL);
798 * Network interface unit routines.
801 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
803 struct ppp *ppp = (struct ppp *) dev->priv;
807 npi = ethertype_to_npindex(ntohs(skb->protocol));
811 /* Drop, accept or reject the packet */
812 switch (ppp->npmode[npi]) {
816 /* it would be nice to have a way to tell the network
817 system to queue this one up for later. */
824 /* Put the 2-byte PPP protocol number on the front,
825 making sure there is room for the address and control fields. */
826 if (skb_headroom(skb) < PPP_HDRLEN) {
829 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
832 skb_reserve(ns, dev->hard_header_len);
833 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
837 pp = skb_push(skb, 2);
838 proto = npindex_to_proto[npi];
842 netif_stop_queue(dev);
843 skb_queue_tail(&ppp->file.xq, skb);
844 ppp_xmit_process(ppp);
849 ++ppp->stats.tx_dropped;
853 static struct net_device_stats *
854 ppp_net_stats(struct net_device *dev)
856 struct ppp *ppp = (struct ppp *) dev->priv;
862 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
864 struct ppp *ppp = dev->priv;
866 void *addr = (void *) ifr->ifr_ifru.ifru_data;
867 struct ppp_stats stats;
868 struct ppp_comp_stats cstats;
873 ppp_get_stats(ppp, &stats);
874 if (copy_to_user(addr, &stats, sizeof(stats)))
880 memset(&cstats, 0, sizeof(cstats));
881 if (ppp->xc_state != 0)
882 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
883 if (ppp->rc_state != 0)
884 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
885 if (copy_to_user(addr, &cstats, sizeof(cstats)))
892 if (copy_to_user(addr, vers, strlen(vers) + 1))
905 ppp_net_init(struct net_device *dev)
907 dev->hard_header_len = PPP_HDRLEN;
909 dev->hard_start_xmit = ppp_start_xmit;
910 dev->get_stats = ppp_net_stats;
911 dev->do_ioctl = ppp_net_ioctl;
913 dev->tx_queue_len = 3;
914 dev->type = ARPHRD_PPP;
915 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
920 * Transmit-side routines.
924 * Called to do any work queued up on the transmit side
925 * that can now be done.
928 ppp_xmit_process(struct ppp *ppp)
935 while (ppp->xmit_pending == 0
936 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
937 ppp_send_frame(ppp, skb);
938 /* If there's no work left to do, tell the core net
939 code that we can accept some more. */
940 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
941 netif_wake_queue(ppp->dev);
943 ppp_xmit_unlock(ppp);
947 * Compress and send a frame.
948 * The caller should have locked the xmit path,
949 * and xmit_pending should be 0.
952 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
954 int proto = PPP_PROTO(skb);
955 struct sk_buff *new_skb;
959 if (proto < 0x8000) {
960 #ifdef CONFIG_PPP_FILTER
961 /* check if we should pass this packet */
962 /* the filter instructions are constructed assuming
963 a four-byte PPP header on each packet */
964 *skb_push(skb, 2) = 1;
965 if (ppp->pass_filter.filter
966 && sk_run_filter(skb, ppp->pass_filter.filter,
967 ppp->pass_filter.len) == 0) {
969 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
973 /* if this packet passes the active filter, record the time */
974 if (!(ppp->active_filter.filter
975 && sk_run_filter(skb, ppp->active_filter.filter,
976 ppp->active_filter.len) == 0))
977 ppp->last_xmit = jiffies;
980 /* for data packets, record the time */
981 ppp->last_xmit = jiffies;
982 #endif /* CONFIG_PPP_FILTER */
985 ++ppp->stats.tx_packets;
986 ppp->stats.tx_bytes += skb->len - 2;
990 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
992 /* try to do VJ TCP header compression */
993 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
996 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
999 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1001 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1002 new_skb->data + 2, &cp,
1003 !(ppp->flags & SC_NO_TCP_CCID));
1004 if (cp == skb->data + 2) {
1005 /* didn't compress */
1008 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1009 proto = PPP_VJC_COMP;
1010 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1012 proto = PPP_VJC_UNCOMP;
1013 cp[0] = skb->data[2];
1017 cp = skb_put(skb, len + 2);
1024 /* peek at outbound CCP frames */
1025 ppp_ccp_peek(ppp, skb, 0);
1029 /* try to do packet compression */
1030 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1031 && proto != PPP_LCP && proto != PPP_CCP) {
1032 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
1035 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1038 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1039 skb_reserve(new_skb,
1040 ppp->dev->hard_header_len - PPP_HDRLEN);
1042 /* compressor still expects A/C bytes in hdr */
1043 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1044 new_skb->data, skb->len + 2,
1045 ppp->dev->mtu + PPP_HDRLEN);
1046 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1050 skb_pull(skb, 2); /* pull off A/C bytes */
1052 /* didn't compress, or CCP not up yet */
1058 * If we are waiting for traffic (demand dialling),
1059 * queue it up for pppd to receive.
1061 if (ppp->flags & SC_LOOP_TRAFFIC) {
1062 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1064 skb_queue_tail(&ppp->file.rq, skb);
1065 wake_up_interruptible(&ppp->file.rwait);
1069 ppp->xmit_pending = skb;
1075 ++ppp->stats.tx_errors;
1079 * Try to send the frame in xmit_pending.
1080 * The caller should have the xmit path locked.
1083 ppp_push(struct ppp *ppp)
1085 struct list_head *list;
1086 struct channel *pch;
1087 struct sk_buff *skb = ppp->xmit_pending;
1092 list = &ppp->channels;
1093 if (list_empty(list)) {
1094 /* nowhere to send the packet, just drop it */
1095 ppp->xmit_pending = 0;
1100 if ((ppp->flags & SC_MULTILINK) == 0) {
1101 /* not doing multilink: send it down the first channel */
1103 pch = list_entry(list, struct channel, clist);
1105 spin_lock_bh(&pch->downl);
1107 if (pch->chan->ops->start_xmit(pch->chan, skb))
1108 ppp->xmit_pending = 0;
1110 /* channel got unregistered */
1112 ppp->xmit_pending = 0;
1114 spin_unlock_bh(&pch->downl);
1118 #ifdef CONFIG_PPP_MULTILINK
1119 /* Multilink: fragment the packet over as many links
1120 as can take the packet at the moment. */
1121 if (!ppp_mp_explode(ppp, skb))
1123 #endif /* CONFIG_PPP_MULTILINK */
1125 ppp->xmit_pending = 0;
1129 #ifdef CONFIG_PPP_MULTILINK
1131 * Divide a packet to be transmitted into fragments and
1132 * send them out the individual links.
1134 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1136 int nch, len, fragsize;
1137 int i, bits, hdrlen, mtu;
1139 unsigned char *p, *q;
1140 struct list_head *list;
1141 struct channel *pch;
1142 struct sk_buff *frag;
1143 struct ppp_channel *chan;
1146 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1147 list = &ppp->channels;
1148 while ((list = list->next) != &ppp->channels) {
1149 pch = list_entry(list, struct channel, clist);
1150 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1152 * If a channel hasn't had a fragment yet, it has to get
1153 * one before we send any fragments on later channels.
1154 * If it can't take a fragment now, don't give any
1155 * to subsequent channels.
1157 if (!pch->had_frag && !pch->avail) {
1158 while ((list = list->next) != &ppp->channels) {
1159 pch = list_entry(list, struct channel, clist);
1166 return 0; /* can't take now, leave it in xmit_pending */
1168 /* Do protocol field compression (XXX this should be optional) */
1176 /* decide on fragment size */
1179 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1182 fragsize = ROUNDUP(fragsize, nch);
1185 /* skip to the channel after the one we last used
1186 and start at that one */
1187 for (i = 0; i < ppp->nxchan; ++i) {
1189 if (list == &ppp->channels) {
1195 /* create a fragment for each channel */
1199 if (list == &ppp->channels) {
1203 pch = list_entry(list, struct channel, clist);
1208 /* check the channel's mtu and whether it is still attached. */
1209 spin_lock_bh(&pch->downl);
1210 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1211 /* can't use this channel */
1212 spin_unlock_bh(&pch->downl);
1220 * We have to create multiple fragments for this channel
1221 * if fragsize is greater than the channel's mtu.
1225 for (flen = fragsize; flen > 0; flen -= fnb) {
1227 if (fnb > mtu + 2 - hdrlen)
1228 fnb = mtu + 2 - hdrlen;
1231 frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1234 q = skb_put(frag, fnb + hdrlen);
1235 /* make the MP header */
1238 if (ppp->flags & SC_MP_XSHORTSEQ) {
1239 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1243 q[3] = ppp->nxseq >> 16;
1244 q[4] = ppp->nxseq >> 8;
1248 /* copy the data in */
1249 memcpy(q + hdrlen, p, fnb);
1251 /* try to send it down the channel */
1253 if (!chan->ops->start_xmit(chan, frag))
1254 skb_queue_tail(&pch->file.xq, frag);
1261 spin_unlock_bh(&pch->downl);
1268 spin_unlock_bh(&pch->downl);
1270 printk(KERN_ERR "PPP: no memory (fragment)\n");
1271 ++ppp->stats.tx_errors;
1273 return 1; /* abandon the frame */
1275 #endif /* CONFIG_PPP_MULTILINK */
1278 * Try to send data out on a channel.
1281 ppp_channel_push(struct channel *pch)
1283 struct sk_buff *skb;
1286 spin_lock_bh(&pch->downl);
1287 if (pch->chan != 0) {
1288 while (skb_queue_len(&pch->file.xq) > 0) {
1289 skb = skb_dequeue(&pch->file.xq);
1290 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1291 /* put the packet back and try again later */
1292 skb_queue_head(&pch->file.xq, skb);
1297 /* channel got deregistered */
1298 skb_queue_purge(&pch->file.xq);
1300 spin_unlock_bh(&pch->downl);
1301 /* see if there is anything from the attached unit to be sent */
1302 if (skb_queue_len(&pch->file.xq) == 0) {
1303 read_lock_bh(&pch->upl);
1306 ppp_xmit_process(ppp);
1307 read_unlock_bh(&pch->upl);
1312 * Receive-side routines.
1315 /* misuse a few fields of the skb for MP reconstruction */
1316 #define sequence priority
1317 #define BEbits cb[0]
1320 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1323 /* ppp->dev == 0 means interface is closing down */
1325 ppp_receive_frame(ppp, skb, pch);
1328 ppp_recv_unlock(ppp);
1332 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1334 struct channel *pch = chan->ppp;
1337 if (pch == 0 || skb->len == 0) {
1342 proto = PPP_PROTO(skb);
1343 read_lock_bh(&pch->upl);
1344 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1345 /* put it on the channel queue */
1346 skb_queue_tail(&pch->file.rq, skb);
1347 /* drop old frames if queue too long */
1348 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1349 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1351 wake_up_interruptible(&pch->file.rwait);
1353 ppp_do_recv(pch->ppp, skb, pch);
1355 read_unlock_bh(&pch->upl);
1358 /* Put a 0-length skb in the receive queue as an error indication */
1360 ppp_input_error(struct ppp_channel *chan, int code)
1362 struct channel *pch = chan->ppp;
1363 struct sk_buff *skb;
1368 read_lock_bh(&pch->upl);
1369 if (pch->ppp != 0) {
1370 skb = alloc_skb(0, GFP_ATOMIC);
1372 skb->len = 0; /* probably unnecessary */
1374 ppp_do_recv(pch->ppp, skb, pch);
1377 read_unlock_bh(&pch->upl);
1381 * We come in here to process a received frame.
1382 * The receive side of the ppp unit is locked.
1385 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1387 if (skb->len >= 2) {
1388 #ifdef CONFIG_PPP_MULTILINK
1389 /* XXX do channel-level decompression here */
1390 if (PPP_PROTO(skb) == PPP_MP)
1391 ppp_receive_mp_frame(ppp, skb, pch);
1393 #endif /* CONFIG_PPP_MULTILINK */
1394 ppp_receive_nonmp_frame(ppp, skb);
1399 /* note: a 0-length skb is used as an error indication */
1400 ++ppp->stats.rx_length_errors;
1403 ppp_receive_error(ppp);
1407 ppp_receive_error(struct ppp *ppp)
1409 ++ppp->stats.rx_errors;
1415 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1418 int proto, len, npi;
1421 * Decompress the frame, if compressed.
1422 * Note that some decompressors need to see uncompressed frames
1423 * that come in as well as compressed frames.
1425 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1426 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1427 skb = ppp_decompress_frame(ppp, skb);
1429 proto = PPP_PROTO(skb);
1432 /* decompress VJ compressed packets */
1433 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1435 if (skb_tailroom(skb) < 124) {
1436 /* copy to a new sk_buff with more tailroom */
1437 ns = dev_alloc_skb(skb->len + 128);
1439 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1443 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
1447 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1449 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1454 skb_put(skb, len - skb->len);
1455 else if (len < skb->len)
1460 case PPP_VJC_UNCOMP:
1461 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1463 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1464 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1471 ppp_ccp_peek(ppp, skb, 1);
1475 ++ppp->stats.rx_packets;
1476 ppp->stats.rx_bytes += skb->len - 2;
1478 npi = proto_to_npindex(proto);
1480 /* control or unknown frame - pass it to pppd */
1481 skb_queue_tail(&ppp->file.rq, skb);
1482 /* limit queue length by dropping old frames */
1483 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1484 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1486 /* wake up any process polling or blocking on read */
1487 wake_up_interruptible(&ppp->file.rwait);
1490 /* network protocol frame - give it to the kernel */
1492 #ifdef CONFIG_PPP_FILTER
1493 /* check if the packet passes the pass and active filters */
1494 /* the filter instructions are constructed assuming
1495 a four-byte PPP header on each packet */
1496 *skb_push(skb, 2) = 0;
1497 if (ppp->pass_filter.filter
1498 && sk_run_filter(skb, ppp->pass_filter.filter,
1499 ppp->pass_filter.len) == 0) {
1501 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1505 if (!(ppp->active_filter.filter
1506 && sk_run_filter(skb, ppp->active_filter.filter,
1507 ppp->active_filter.len) == 0))
1508 ppp->last_recv = jiffies;
1511 ppp->last_recv = jiffies;
1512 #endif /* CONFIG_PPP_FILTER */
1514 if ((ppp->dev->flags & IFF_UP) == 0
1515 || ppp->npmode[npi] != NPMODE_PASS) {
1518 skb_pull(skb, 2); /* chop off protocol */
1519 skb->dev = ppp->dev;
1520 skb->protocol = htons(npindex_to_ethertype[npi]);
1521 skb->mac.raw = skb->data;
1523 ppp->dev->last_rx = jiffies;
1530 ppp_receive_error(ppp);
1533 static struct sk_buff *
1534 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1536 int proto = PPP_PROTO(skb);
1540 if (proto == PPP_COMP) {
1541 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1543 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1546 /* the decompressor still expects the A/C bytes in the hdr */
1547 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1548 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1550 /* Pass the compressed frame to pppd as an
1551 error indication. */
1552 if (len == DECOMP_FATALERROR)
1553 ppp->rstate |= SC_DC_FERROR;
1561 skb_pull(skb, 2); /* pull off the A/C bytes */
1564 /* Uncompressed frame - pass to decompressor so it
1565 can update its dictionary if necessary. */
1566 if (ppp->rcomp->incomp)
1567 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1574 ppp->rstate |= SC_DC_ERROR;
1575 ppp_receive_error(ppp);
1579 #ifdef CONFIG_PPP_MULTILINK
1581 * Receive a multilink frame.
1582 * We put it on the reconstruction queue and then pull off
1583 * as many completed frames as we can.
1586 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1589 struct list_head *l;
1590 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1592 if (skb->len < mphdrlen + 1 || ppp->mrru == 0)
1593 goto err; /* no good, throw it away */
1595 /* Decode sequence number and begin/end bits */
1596 if (ppp->flags & SC_MP_SHORTSEQ) {
1597 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1600 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1603 skb->BEbits = skb->data[2];
1604 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1607 * Do protocol ID decompression on the first fragment of each packet.
1609 if ((skb->BEbits & B) && (skb->data[0] & 1))
1610 *skb_push(skb, 1) = 0;
1613 * Expand sequence number to 32 bits, making it as close
1614 * as possible to ppp->minseq.
1616 seq |= ppp->minseq & ~mask;
1617 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1619 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1620 seq -= mask + 1; /* should never happen */
1621 skb->sequence = seq;
1625 * If this packet comes before the next one we were expecting,
1628 if (seq_before(seq, ppp->nextseq)) {
1630 ++ppp->stats.rx_dropped;
1631 ppp_receive_error(ppp);
1636 * Reevaluate minseq, the minimum over all channels of the
1637 * last sequence number received on each channel. Because of
1638 * the increasing sequence number rule, we know that any fragment
1639 * before `minseq' which hasn't arrived is never going to arrive.
1640 * The list of channels can't change because we have the receive
1641 * side of the ppp unit locked.
1643 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1644 struct channel *ch = list_entry(l, struct channel, clist);
1645 if (seq_before(ch->lastseq, seq))
1648 if (seq_before(ppp->minseq, seq))
1651 /* Put the fragment on the reconstruction queue */
1652 ppp_mp_insert(ppp, skb);
1654 /* If the queue is getting long, don't wait any longer for packets
1655 before the start of the queue. */
1656 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1657 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1658 ppp->minseq = ppp->mrq.next->sequence;
1660 /* Pull completed packets off the queue and receive them. */
1661 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1662 ppp_receive_nonmp_frame(ppp, skb);
1668 ppp_receive_error(ppp);
1672 * Insert a fragment on the MP reconstruction queue.
1673 * The queue is ordered by increasing sequence number.
1676 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1679 struct sk_buff_head *list = &ppp->mrq;
1680 u32 seq = skb->sequence;
1682 /* N.B. we don't need to lock the list lock because we have the
1683 ppp unit receive-side lock. */
1684 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1685 if (seq_before(seq, p->sequence))
1687 __skb_insert(skb, p->prev, p, list);
1691 * Reconstruct a packet from the MP fragment queue.
1692 * We go through increasing sequence numbers until we find a
1693 * complete packet, or we get to the sequence number for a fragment
1694 * which hasn't arrived but might still do so.
1697 ppp_mp_reconstruct(struct ppp *ppp)
1699 u32 seq = ppp->nextseq;
1700 u32 minseq = ppp->minseq;
1701 struct sk_buff_head *list = &ppp->mrq;
1702 struct sk_buff *p, *next;
1703 struct sk_buff *head, *tail;
1704 struct sk_buff *skb = NULL;
1705 int lost = 0, len = 0;
1707 if (ppp->mrru == 0) /* do nothing until mrru is set */
1711 for (p = head; p != (struct sk_buff *) list; p = next) {
1713 if (seq_before(p->sequence, seq)) {
1714 /* this can't happen, anyway ignore the skb */
1715 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1720 if (p->sequence != seq) {
1721 /* Fragment `seq' is missing. If it is after
1722 minseq, it might arrive later, so stop here. */
1723 if (seq_after(seq, minseq))
1725 /* Fragment `seq' is lost, keep going. */
1727 seq = seq_before(minseq, p->sequence)?
1728 minseq + 1: p->sequence;
1734 * At this point we know that all the fragments from
1735 * ppp->nextseq to seq are either present or lost.
1736 * Also, there are no complete packets in the queue
1737 * that have no missing fragments and end before this
1741 /* B bit set indicates this fragment starts a packet */
1742 if (p->BEbits & B) {
1750 /* Got a complete packet yet? */
1751 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1752 if (len > ppp->mrru + 2) {
1753 ++ppp->stats.rx_length_errors;
1754 printk(KERN_DEBUG "PPP: reconstructed packet"
1755 " is too long (%d)\n", len);
1756 } else if (p == head) {
1757 /* fragment is complete packet - reuse skb */
1761 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1762 ++ppp->stats.rx_missed_errors;
1763 printk(KERN_DEBUG "PPP: no memory for "
1764 "reconstructed packet");
1769 ppp->nextseq = seq + 1;
1773 * If this is the ending fragment of a packet,
1774 * and we haven't found a complete valid packet yet,
1775 * we can discard up to and including this fragment.
1783 /* If we have a complete packet, copy it all into one skb. */
1785 /* If we have discarded any fragments,
1786 signal a receive error. */
1787 if (head->sequence != ppp->nextseq) {
1789 printk(KERN_DEBUG " missed pkts %u..%u\n",
1790 ppp->nextseq, head->sequence-1);
1791 ++ppp->stats.rx_dropped;
1792 ppp_receive_error(ppp);
1796 /* copy to a single skb */
1797 for (p = head; p != tail->next; p = p->next)
1798 memcpy(skb_put(skb, p->len), p->data, p->len);
1799 ppp->nextseq = tail->sequence + 1;
1803 /* Discard all the skbuffs that we have copied the data out of
1804 or that we can't use. */
1805 while ((p = list->next) != head) {
1806 __skb_unlink(p, list);
1812 #endif /* CONFIG_PPP_MULTILINK */
1815 * Channel interface.
1819 * Create a new, unattached ppp channel.
1822 ppp_register_channel(struct ppp_channel *chan)
1824 struct channel *pch;
1826 pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
1829 memset(pch, 0, sizeof(struct channel));
1833 init_ppp_file(&pch->file, CHANNEL);
1834 pch->file.hdrlen = chan->hdrlen;
1835 #ifdef CONFIG_PPP_MULTILINK
1837 #endif /* CONFIG_PPP_MULTILINK */
1838 init_rwsem(&pch->chan_sem);
1839 spin_lock_init(&pch->downl);
1840 pch->upl = RW_LOCK_UNLOCKED;
1841 spin_lock_bh(&all_channels_lock);
1842 pch->file.index = ++last_channel_index;
1843 list_add(&pch->list, &new_channels);
1844 atomic_inc(&channel_count);
1845 spin_unlock_bh(&all_channels_lock);
1851 * Return the index of a channel.
1853 int ppp_channel_index(struct ppp_channel *chan)
1855 struct channel *pch = chan->ppp;
1858 return pch->file.index;
1863 * Return the PPP unit number to which a channel is connected.
1865 int ppp_unit_number(struct ppp_channel *chan)
1867 struct channel *pch = chan->ppp;
1871 read_lock_bh(&pch->upl);
1873 unit = pch->ppp->file.index;
1874 read_unlock_bh(&pch->upl);
1880 * Disconnect a channel from the generic layer.
1881 * This must be called in process context.
1884 ppp_unregister_channel(struct ppp_channel *chan)
1886 struct channel *pch = chan->ppp;
1889 return; /* should never happen */
1893 * This ensures that we have returned from any calls into the
1894 * the channel's start_xmit or ioctl routine before we proceed.
1896 down_write(&pch->chan_sem);
1897 spin_lock_bh(&pch->downl);
1899 spin_unlock_bh(&pch->downl);
1900 up_write(&pch->chan_sem);
1901 ppp_disconnect_channel(pch);
1902 spin_lock_bh(&all_channels_lock);
1903 list_del(&pch->list);
1904 spin_unlock_bh(&all_channels_lock);
1906 wake_up_interruptible(&pch->file.rwait);
1907 if (atomic_dec_and_test(&pch->file.refcnt))
1908 ppp_destroy_channel(pch);
1913 * Callback from a channel when it can accept more to transmit.
1914 * This should be called at BH/softirq level, not interrupt level.
1917 ppp_output_wakeup(struct ppp_channel *chan)
1919 struct channel *pch = chan->ppp;
1923 ppp_channel_push(pch);
1927 * Compression control.
1930 /* Process the PPPIOCSCOMPRESS ioctl. */
1932 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1935 struct compressor *cp, *ocomp;
1936 struct ppp_option_data data;
1937 void *state, *ostate;
1938 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1944 if (copy_from_user(&data, (void *) arg, sizeof(data))
1945 || (data.length <= CCP_MAX_OPTION_LENGTH
1946 && copy_from_user(ccp_option, data.ptr, data.length)))
1949 if (data.length > CCP_MAX_OPTION_LENGTH
1950 || ccp_option[1] < 2 || ccp_option[1] > data.length)
1953 cp = find_compressor(ccp_option[0]);
1956 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
1957 request_module(modname);
1958 cp = find_compressor(ccp_option[0]);
1960 #endif /* CONFIG_KMOD */
1964 * XXX race: the compressor module could get unloaded between
1965 * here and when we do the comp_alloc or decomp_alloc call below.
1969 if (data.transmit) {
1970 state = cp->comp_alloc(ccp_option, data.length);
1973 ppp->xstate &= ~SC_COMP_RUN;
1975 ostate = ppp->xc_state;
1977 ppp->xc_state = state;
1978 ppp_xmit_unlock(ppp);
1980 ocomp->comp_free(ostate);
1985 state = cp->decomp_alloc(ccp_option, data.length);
1988 ppp->rstate &= ~SC_DECOMP_RUN;
1990 ostate = ppp->rc_state;
1992 ppp->rc_state = state;
1993 ppp_recv_unlock(ppp);
1995 ocomp->decomp_free(ostate);
2005 * Look at a CCP packet and update our state accordingly.
2006 * We assume the caller has the xmit or recv path locked.
2009 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2011 unsigned char *dp = skb->data + 2;
2014 if (skb->len < CCP_HDRLEN + 2
2015 || skb->len < (len = CCP_LENGTH(dp)) + 2)
2016 return; /* too short */
2018 switch (CCP_CODE(dp)) {
2021 /* A ConfReq starts negotiation of compression
2022 * in one direction of transmission,
2023 * and hence brings it down...but which way?
2026 * A ConfReq indicates what the sender would like to receive
2029 /* He is proposing what I should send */
2030 ppp->xstate &= ~SC_COMP_RUN;
2032 /* I am proposing to what he should send */
2033 ppp->rstate &= ~SC_DECOMP_RUN;
2040 * CCP is going down, both directions of transmission
2042 ppp->rstate &= ~SC_DECOMP_RUN;
2043 ppp->xstate &= ~SC_COMP_RUN;
2047 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2051 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2054 /* we will start receiving compressed packets */
2055 if (ppp->rc_state == 0)
2057 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2058 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2059 ppp->rstate |= SC_DECOMP_RUN;
2060 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2063 /* we will soon start sending compressed packets */
2064 if (ppp->xc_state == 0)
2066 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2067 ppp->file.index, 0, ppp->debug))
2068 ppp->xstate |= SC_COMP_RUN;
2073 /* reset the [de]compressor */
2074 if ((ppp->flags & SC_CCP_UP) == 0)
2077 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2078 ppp->rcomp->decomp_reset(ppp->rc_state);
2079 ppp->rstate &= ~SC_DC_ERROR;
2082 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2083 ppp->xcomp->comp_reset(ppp->xc_state);
2089 /* Free up compression resources. */
2091 ppp_ccp_closed(struct ppp *ppp)
2093 void *xstate, *rstate;
2094 struct compressor *xcomp, *rcomp;
2097 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2100 xstate = ppp->xc_state;
2104 rstate = ppp->rc_state;
2109 xcomp->comp_free(xstate);
2111 rcomp->decomp_free(rstate);
2114 /* List of compressors. */
2115 static LIST_HEAD(compressor_list);
2116 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2118 struct compressor_entry {
2119 struct list_head list;
2120 struct compressor *comp;
2123 static struct compressor_entry *
2124 find_comp_entry(int proto)
2126 struct compressor_entry *ce;
2127 struct list_head *list = &compressor_list;
2129 while ((list = list->next) != &compressor_list) {
2130 ce = list_entry(list, struct compressor_entry, list);
2131 if (ce->comp->compress_proto == proto)
2137 /* Register a compressor */
2139 ppp_register_compressor(struct compressor *cp)
2141 struct compressor_entry *ce;
2143 spin_lock(&compressor_list_lock);
2145 if (find_comp_entry(cp->compress_proto) != 0)
2148 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2153 list_add(&ce->list, &compressor_list);
2155 spin_unlock(&compressor_list_lock);
2159 /* Unregister a compressor */
2161 ppp_unregister_compressor(struct compressor *cp)
2163 struct compressor_entry *ce;
2165 spin_lock(&compressor_list_lock);
2166 ce = find_comp_entry(cp->compress_proto);
2167 if (ce != 0 && ce->comp == cp) {
2168 list_del(&ce->list);
2171 spin_unlock(&compressor_list_lock);
2174 /* Find a compressor. */
2175 static struct compressor *
2176 find_compressor(int type)
2178 struct compressor_entry *ce;
2179 struct compressor *cp = 0;
2181 spin_lock(&compressor_list_lock);
2182 ce = find_comp_entry(type);
2185 spin_unlock(&compressor_list_lock);
2190 * Miscelleneous stuff.
2194 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2196 struct slcompress *vj = ppp->vj;
2198 memset(st, 0, sizeof(*st));
2199 st->p.ppp_ipackets = ppp->stats.rx_packets;
2200 st->p.ppp_ierrors = ppp->stats.rx_errors;
2201 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2202 st->p.ppp_opackets = ppp->stats.tx_packets;
2203 st->p.ppp_oerrors = ppp->stats.tx_errors;
2204 st->p.ppp_obytes = ppp->stats.tx_bytes;
2207 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2208 st->vj.vjs_compressed = vj->sls_o_compressed;
2209 st->vj.vjs_searches = vj->sls_o_searches;
2210 st->vj.vjs_misses = vj->sls_o_misses;
2211 st->vj.vjs_errorin = vj->sls_i_error;
2212 st->vj.vjs_tossed = vj->sls_i_tossed;
2213 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2214 st->vj.vjs_compressedin = vj->sls_i_compressed;
2218 * Stuff for handling the lists of ppp units and channels
2219 * and for initialization.
2223 * Create a new ppp interface unit. Fails if it can't allocate memory
2224 * or if there is already a unit with the requested number.
2225 * unit == -1 means allocate a new number.
2228 ppp_create_interface(int unit, int *retp)
2231 struct net_device *dev = NULL;
2235 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2238 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
2241 memset(ppp, 0, sizeof(struct ppp));
2242 memset(dev, 0, sizeof(struct net_device));
2247 unit = cardmap_find_first_free(all_ppp_units);
2248 else if (cardmap_get(all_ppp_units, unit) != NULL)
2249 goto err_unlock; /* unit already exists */
2251 /* Initialize the new ppp unit */
2252 ppp->file.index = unit;
2254 init_ppp_file(&ppp->file, INTERFACE);
2255 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2256 for (i = 0; i < NUM_NP; ++i)
2257 ppp->npmode[i] = NPMODE_PASS;
2258 INIT_LIST_HEAD(&ppp->channels);
2259 spin_lock_init(&ppp->rlock);
2260 spin_lock_init(&ppp->wlock);
2261 #ifdef CONFIG_PPP_MULTILINK
2263 skb_queue_head_init(&ppp->mrq);
2264 #endif /* CONFIG_PPP_MULTILINK */
2267 dev->init = ppp_net_init;
2268 sprintf(dev->name, "ppp%d", unit);
2270 dev->features |= NETIF_F_DYNALLOC;
2273 ret = register_netdevice(dev);
2276 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2281 atomic_inc(&ppp_unit_count);
2282 cardmap_set(&all_ppp_units, unit, ppp);
2299 * Initialize a ppp_file structure.
2302 init_ppp_file(struct ppp_file *pf, int kind)
2305 skb_queue_head_init(&pf->xq);
2306 skb_queue_head_init(&pf->rq);
2307 atomic_set(&pf->refcnt, 1);
2308 init_waitqueue_head(&pf->rwait);
2312 * Take down a ppp interface unit - called when the owning file
2313 * (the one that created the unit) is closed or detached.
2315 static void ppp_shutdown_interface(struct ppp *ppp)
2317 struct net_device *dev;
2327 unregister_netdevice(dev);
2330 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2333 wake_up_interruptible(&ppp->file.rwait);
2338 * Free the memory used by a ppp unit. This is only called once
2339 * there are no channels connected to the unit and no file structs
2340 * that reference the unit.
2342 static void ppp_destroy_interface(struct ppp *ppp)
2344 atomic_dec(&ppp_unit_count);
2346 if (!ppp->file.dead || ppp->n_channels) {
2347 /* "can't happen" */
2348 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2349 "n_channels=%d !\n", ppp, ppp->file.dead,
2354 ppp_ccp_closed(ppp);
2359 skb_queue_purge(&ppp->file.xq);
2360 skb_queue_purge(&ppp->file.rq);
2361 #ifdef CONFIG_PPP_MULTILINK
2362 skb_queue_purge(&ppp->mrq);
2363 #endif /* CONFIG_PPP_MULTILINK */
2364 #ifdef CONFIG_PPP_FILTER
2365 if (ppp->pass_filter.filter) {
2366 kfree(ppp->pass_filter.filter);
2367 ppp->pass_filter.filter = NULL;
2369 if (ppp->active_filter.filter) {
2370 kfree(ppp->active_filter.filter);
2371 ppp->active_filter.filter = 0;
2373 #endif /* CONFIG_PPP_FILTER */
2379 * Locate an existing ppp unit.
2380 * The caller should have locked the all_ppp_sem.
2383 ppp_find_unit(int unit)
2385 return cardmap_get(all_ppp_units, unit);
2389 * Locate an existing ppp channel.
2390 * The caller should have locked the all_channels_lock.
2391 * First we look in the new_channels list, then in the
2392 * all_channels list. If found in the new_channels list,
2393 * we move it to the all_channels list. This is for speed
2394 * when we have a lot of channels in use.
2396 static struct channel *
2397 ppp_find_channel(int unit)
2399 struct channel *pch;
2400 struct list_head *list;
2402 list = &new_channels;
2403 while ((list = list->next) != &new_channels) {
2404 pch = list_entry(list, struct channel, list);
2405 if (pch->file.index == unit) {
2406 list_del(&pch->list);
2407 list_add(&pch->list, &all_channels);
2411 list = &all_channels;
2412 while ((list = list->next) != &all_channels) {
2413 pch = list_entry(list, struct channel, list);
2414 if (pch->file.index == unit)
2421 * Connect a PPP channel to a PPP interface unit.
2424 ppp_connect_channel(struct channel *pch, int unit)
2431 ppp = ppp_find_unit(unit);
2435 write_lock_bh(&pch->upl);
2441 if (pch->file.hdrlen > ppp->file.hdrlen)
2442 ppp->file.hdrlen = pch->file.hdrlen;
2443 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2444 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2445 ppp->dev->hard_header_len = hdrlen;
2446 list_add_tail(&pch->clist, &ppp->channels);
2449 atomic_inc(&ppp->file.refcnt);
2454 write_unlock_bh(&pch->upl);
2461 * Disconnect a channel from its ppp unit.
2464 ppp_disconnect_channel(struct channel *pch)
2469 write_lock_bh(&pch->upl);
2472 write_unlock_bh(&pch->upl);
2474 /* remove it from the ppp unit's list */
2476 list_del(&pch->clist);
2479 if (atomic_dec_and_test(&ppp->file.refcnt))
2480 ppp_destroy_interface(ppp);
2487 * Free up the resources used by a ppp channel.
2489 static void ppp_destroy_channel(struct channel *pch)
2491 atomic_dec(&channel_count);
2493 if (!pch->file.dead) {
2494 /* "can't happen" */
2495 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2499 skb_queue_purge(&pch->file.xq);
2500 skb_queue_purge(&pch->file.rq);
2504 static void __exit ppp_cleanup(void)
2506 /* should never happen */
2507 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2508 printk(KERN_ERR "PPP: removing module but units remain!\n");
2509 cardmap_destroy(&all_ppp_units);
2510 if (devfs_unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2511 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2512 devfs_unregister(devfs_handle);
2516 * Cardmap implementation.
2518 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2523 for (p = map; p != NULL; ) {
2524 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2528 nr &= ~(CARDMAP_MASK << p->shift);
2534 static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2540 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2542 /* need a new top level */
2543 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2544 memset(np, 0, sizeof(*np));
2547 np->shift = p->shift + CARDMAP_ORDER;
2552 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2555 while (p->shift > 0) {
2556 i = (nr >> p->shift) & CARDMAP_MASK;
2557 if (p->ptr[i] == NULL) {
2558 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2559 memset(np, 0, sizeof(*np));
2560 np->shift = p->shift - CARDMAP_ORDER;
2565 clear_bit(i, &p->inuse);
2568 i = nr & CARDMAP_MASK;
2571 set_bit(i, &p->inuse);
2573 clear_bit(i, &p->inuse);
2576 static unsigned int cardmap_find_first_free(struct cardmap *map)
2579 unsigned int nr = 0;
2582 if ((p = map) == NULL)
2585 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2586 if (i >= CARDMAP_WIDTH) {
2587 if (p->parent == NULL)
2588 return CARDMAP_WIDTH << p->shift;
2590 i = (nr >> p->shift) & CARDMAP_MASK;
2591 set_bit(i, &p->inuse);
2594 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2595 if (p->shift == 0 || p->ptr[i] == NULL)
2601 static void cardmap_destroy(struct cardmap **pmap)
2603 struct cardmap *p, *np;
2606 for (p = *pmap; p != NULL; p = np) {
2607 if (p->shift != 0) {
2608 for (i = 0; i < CARDMAP_WIDTH; ++i)
2609 if (p->ptr[i] != NULL)
2611 if (i < CARDMAP_WIDTH) {
2623 /* Module/initialization stuff */
2625 module_init(ppp_init);
2626 module_exit(ppp_cleanup);
2628 EXPORT_SYMBOL(ppp_register_channel);
2629 EXPORT_SYMBOL(ppp_unregister_channel);
2630 EXPORT_SYMBOL(ppp_channel_index);
2631 EXPORT_SYMBOL(ppp_unit_number);
2632 EXPORT_SYMBOL(ppp_input);
2633 EXPORT_SYMBOL(ppp_input_error);
2634 EXPORT_SYMBOL(ppp_output_wakeup);
2635 EXPORT_SYMBOL(ppp_register_compressor);
2636 EXPORT_SYMBOL(ppp_unregister_compressor);
2637 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2638 EXPORT_SYMBOL(all_channels); /* for debugging */
2639 MODULE_LICENSE("GPL");