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 <linux/device.h>
49 #include <net/slhc_vj.h>
50 #include <asm/atomic.h>
52 #define PPP_VERSION "2.4.2"
55 * Network protocols we support.
57 #define NP_IP 0 /* Internet Protocol V4 */
58 #define NP_IPV6 1 /* Internet Protocol V6 */
59 #define NP_IPX 2 /* IPX protocol */
60 #define NP_AT 3 /* Appletalk protocol */
61 #define NP_MPLS_UC 4 /* MPLS unicast */
62 #define NP_MPLS_MC 5 /* MPLS multicast */
63 #define NUM_NP 6 /* Number of NPs. */
65 #define MPHDRLEN 6 /* multilink protocol header length */
66 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
67 #define MIN_FRAG_SIZE 64
70 * An instance of /dev/ppp can be associated with either a ppp
71 * interface unit or a ppp channel. In both cases, file->private_data
72 * points to one of these.
78 struct sk_buff_head xq; /* pppd transmit queue */
79 struct sk_buff_head rq; /* receive queue for pppd */
80 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
81 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
82 int hdrlen; /* space to leave for headers */
83 int index; /* interface unit / channel number */
84 int dead; /* unit/channel has been shut down */
87 #define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
89 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
90 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
92 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
95 * Data structure describing one ppp unit.
96 * A ppp unit corresponds to a ppp network interface device
97 * and represents a multilink bundle.
98 * It can have 0 or more ppp channels connected to it.
101 struct ppp_file file; /* stuff for read/write/poll 0 */
102 struct file *owner; /* file that owns this unit 48 */
103 struct list_head channels; /* list of attached channels 4c */
104 int n_channels; /* how many channels are attached 54 */
105 spinlock_t rlock; /* lock for receive side 58 */
106 spinlock_t wlock; /* lock for transmit side 5c */
107 int mru; /* max receive unit 60 */
108 unsigned int flags; /* control bits 64 */
109 unsigned int xstate; /* transmit state bits 68 */
110 unsigned int rstate; /* receive state bits 6c */
111 int debug; /* debug flags 70 */
112 struct slcompress *vj; /* state for VJ header compression */
113 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
114 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
115 struct compressor *xcomp; /* transmit packet compressor 8c */
116 void *xc_state; /* its internal state 90 */
117 struct compressor *rcomp; /* receive decompressor 94 */
118 void *rc_state; /* its internal state 98 */
119 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
120 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
121 struct net_device *dev; /* network interface device a4 */
122 #ifdef CONFIG_PPP_MULTILINK
123 int nxchan; /* next channel to send something on */
124 u32 nxseq; /* next sequence number to send */
125 int mrru; /* MP: max reconst. receive unit */
126 u32 nextseq; /* MP: seq no of next packet */
127 u32 minseq; /* MP: min of most recent seqnos */
128 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
129 #endif /* CONFIG_PPP_MULTILINK */
130 struct net_device_stats stats; /* statistics */
131 #ifdef CONFIG_PPP_FILTER
132 struct sock_fprog pass_filter; /* filter for packets to pass */
133 struct sock_fprog active_filter;/* filter for pkts to reset idle */
134 #endif /* CONFIG_PPP_FILTER */
138 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
139 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
140 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
141 * Bits in xstate: SC_COMP_RUN
143 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
144 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
145 |SC_COMP_TCP|SC_REJ_COMP_TCP)
148 * Private data structure for each channel.
149 * This includes the data structure used for multilink.
152 struct ppp_file file; /* stuff for read/write/poll */
153 struct list_head list; /* link in all/new_channels list */
154 struct ppp_channel *chan; /* public channel data structure */
155 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
156 spinlock_t downl; /* protects `chan', file.xq dequeue */
157 struct ppp *ppp; /* ppp unit we're connected to */
158 struct list_head clist; /* link in list of channels per unit */
159 rwlock_t upl; /* protects `ppp' */
160 #ifdef CONFIG_PPP_MULTILINK
161 u8 avail; /* flag used in multilink stuff */
162 u8 had_frag; /* >= 1 fragments have been sent */
163 u32 lastseq; /* MP: last sequence # received */
164 #endif /* CONFIG_PPP_MULTILINK */
168 * SMP locking issues:
169 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
170 * list and the ppp.n_channels field, you need to take both locks
171 * before you modify them.
172 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
177 * A cardmap represents a mapping from unsigned integers to pointers,
178 * and provides a fast "find lowest unused number" operation.
179 * It uses a broad (32-way) tree with a bitmap at each level.
180 * It is designed to be space-efficient for small numbers of entries
181 * and time-efficient for large numbers of entries.
183 #define CARDMAP_ORDER 5
184 #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
185 #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
190 struct cardmap *parent;
191 void *ptr[CARDMAP_WIDTH];
193 static void *cardmap_get(struct cardmap *map, unsigned int nr);
194 static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
195 static unsigned int cardmap_find_first_free(struct cardmap *map);
196 static void cardmap_destroy(struct cardmap **map);
199 * all_ppp_sem protects the all_ppp_units mapping.
200 * It also ensures that finding a ppp unit in the all_ppp_units map
201 * and updating its file.refcnt field is atomic.
203 static DECLARE_MUTEX(all_ppp_sem);
204 static struct cardmap *all_ppp_units;
205 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
208 * all_channels_lock protects all_channels and last_channel_index,
209 * and the atomicity of find a channel and updating its file.refcnt
212 static spinlock_t all_channels_lock = SPIN_LOCK_UNLOCKED;
213 static LIST_HEAD(all_channels);
214 static LIST_HEAD(new_channels);
215 static int last_channel_index;
216 static atomic_t channel_count = ATOMIC_INIT(0);
218 /* Get the PPP protocol number from a skb */
219 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
221 /* We limit the length of ppp->file.rq to this (arbitrary) value */
222 #define PPP_MAX_RQLEN 32
225 * Maximum number of multilink fragments queued up.
226 * This has to be large enough to cope with the maximum latency of
227 * the slowest channel relative to the others. Strictly it should
228 * depend on the number of channels and their characteristics.
230 #define PPP_MP_MAX_QLEN 128
232 /* Multilink header bits. */
233 #define B 0x80 /* this fragment begins a packet */
234 #define E 0x40 /* this fragment ends a packet */
236 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
237 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
238 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
241 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
242 unsigned int cmd, unsigned long arg);
243 static void ppp_xmit_process(struct ppp *ppp);
244 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
245 static void ppp_push(struct ppp *ppp);
246 static void ppp_channel_push(struct channel *pch);
247 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
248 struct channel *pch);
249 static void ppp_receive_error(struct ppp *ppp);
250 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
251 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
252 struct sk_buff *skb);
253 #ifdef CONFIG_PPP_MULTILINK
254 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
255 struct channel *pch);
256 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
257 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
258 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
259 #endif /* CONFIG_PPP_MULTILINK */
260 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
261 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
262 static void ppp_ccp_closed(struct ppp *ppp);
263 static struct compressor *find_compressor(int type);
264 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
265 static struct ppp *ppp_create_interface(int unit, int *retp);
266 static void init_ppp_file(struct ppp_file *pf, int kind);
267 static void ppp_shutdown_interface(struct ppp *ppp);
268 static void ppp_destroy_interface(struct ppp *ppp);
269 static struct ppp *ppp_find_unit(int unit);
270 static struct channel *ppp_find_channel(int unit);
271 static int ppp_connect_channel(struct channel *pch, int unit);
272 static int ppp_disconnect_channel(struct channel *pch);
273 static void ppp_destroy_channel(struct channel *pch);
275 static struct class_simple *ppp_class;
277 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
278 static inline int proto_to_npindex(int proto)
297 /* Translates an NP index into a PPP protocol number */
298 static const int npindex_to_proto[NUM_NP] = {
307 /* Translates an ethertype into an NP index */
308 static inline int ethertype_to_npindex(int ethertype)
328 /* Translates an NP index into an ethertype */
329 static const int npindex_to_ethertype[NUM_NP] = {
341 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
342 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
343 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
344 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
345 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
346 ppp_recv_lock(ppp); } while (0)
347 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
348 ppp_xmit_unlock(ppp); } while (0)
351 * /dev/ppp device routines.
352 * The /dev/ppp device is used by pppd to control the ppp unit.
353 * It supports the read, write, ioctl and poll functions.
354 * Open instances of /dev/ppp can be in one of three states:
355 * unattached, attached to a ppp unit, or attached to a ppp channel.
357 static int ppp_open(struct inode *inode, struct file *file)
360 * This could (should?) be enforced by the permissions on /dev/ppp.
362 if (!capable(CAP_NET_ADMIN))
367 static int ppp_release(struct inode *inode, struct file *file)
369 struct ppp_file *pf = file->private_data;
373 file->private_data = 0;
374 if (pf->kind == INTERFACE) {
376 if (file == ppp->owner)
377 ppp_shutdown_interface(ppp);
379 if (atomic_dec_and_test(&pf->refcnt)) {
382 ppp_destroy_interface(PF_TO_PPP(pf));
385 ppp_destroy_channel(PF_TO_CHANNEL(pf));
393 static ssize_t ppp_read(struct file *file, char __user *buf,
394 size_t count, loff_t *ppos)
396 struct ppp_file *pf = file->private_data;
397 DECLARE_WAITQUEUE(wait, current);
399 struct sk_buff *skb = 0;
405 add_wait_queue(&pf->rwait, &wait);
407 set_current_state(TASK_INTERRUPTIBLE);
408 skb = skb_dequeue(&pf->rq);
415 if (file->f_flags & O_NONBLOCK)
418 if (signal_pending(current))
422 set_current_state(TASK_RUNNING);
423 remove_wait_queue(&pf->rwait, &wait);
429 if (skb->len > count)
432 if (copy_to_user(buf, skb->data, skb->len))
442 static ssize_t ppp_write(struct file *file, const char __user *buf,
443 size_t count, loff_t *ppos)
445 struct ppp_file *pf = file->private_data;
452 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
455 skb_reserve(skb, pf->hdrlen);
457 if (copy_from_user(skb_put(skb, count), buf, count)) {
462 skb_queue_tail(&pf->xq, skb);
466 ppp_xmit_process(PF_TO_PPP(pf));
469 ppp_channel_push(PF_TO_CHANNEL(pf));
479 /* No kernel lock - fine */
480 static unsigned int ppp_poll(struct file *file, poll_table *wait)
482 struct ppp_file *pf = file->private_data;
487 poll_wait(file, &pf->rwait, wait);
488 mask = POLLOUT | POLLWRNORM;
489 if (skb_peek(&pf->rq) != 0)
490 mask |= POLLIN | POLLRDNORM;
496 static int ppp_ioctl(struct inode *inode, struct file *file,
497 unsigned int cmd, unsigned long arg)
499 struct ppp_file *pf = file->private_data;
501 int err = -EFAULT, val, val2, i;
502 struct ppp_idle idle;
505 struct slcompress *vj;
508 return ppp_unattached_ioctl(pf, file, cmd, arg);
510 if (cmd == PPPIOCDETACH) {
512 * We have to be careful here... if the file descriptor
513 * has been dup'd, we could have another process in the
514 * middle of a poll using the same file *, so we had
515 * better not free the interface data structures -
516 * instead we fail the ioctl. Even in this case, we
517 * shut down the interface if we are the owner of it.
518 * Actually, we should get rid of PPPIOCDETACH, userland
519 * (i.e. pppd) could achieve the same effect by closing
520 * this fd and reopening /dev/ppp.
523 if (pf->kind == INTERFACE) {
525 if (file == ppp->owner)
526 ppp_shutdown_interface(ppp);
528 if (atomic_read(&file->f_count) <= 2) {
529 ppp_release(inode, file);
532 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
533 atomic_read(&file->f_count));
537 if (pf->kind == CHANNEL) {
538 struct channel *pch = PF_TO_CHANNEL(pf);
539 struct ppp_channel *chan;
543 if (get_user(unit, (int *) arg))
545 err = ppp_connect_channel(pch, unit);
549 err = ppp_disconnect_channel(pch);
553 down_read(&pch->chan_sem);
556 if (chan && chan->ops->ioctl)
557 err = chan->ops->ioctl(chan, cmd, arg);
558 up_read(&pch->chan_sem);
563 if (pf->kind != INTERFACE) {
565 printk(KERN_ERR "PPP: not interface or channel??\n");
572 if (get_user(val, (int *) arg))
579 if (get_user(val, (int *) arg))
582 cflags = ppp->flags & ~val;
583 ppp->flags = val & SC_FLAG_BITS;
585 if (cflags & SC_CCP_OPEN)
591 val = ppp->flags | ppp->xstate | ppp->rstate;
592 if (put_user(val, (int *) arg))
597 case PPPIOCSCOMPRESS:
598 err = ppp_set_compress(ppp, arg);
602 if (put_user(ppp->file.index, (int *) arg))
608 if (get_user(val, (int *) arg))
615 if (put_user(ppp->debug, (int *) arg))
621 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
622 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
623 if (copy_to_user((void __user *) arg, &idle, sizeof(idle)))
629 if (get_user(val, (int *) arg))
632 if ((val >> 16) != 0) {
636 vj = slhc_init(val2+1, val+1);
638 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
652 if (copy_from_user(&npi, (void __user *) arg, sizeof(npi)))
654 err = proto_to_npindex(npi.protocol);
658 if (cmd == PPPIOCGNPMODE) {
660 npi.mode = ppp->npmode[i];
661 if (copy_to_user((void __user *) arg, &npi, sizeof(npi)))
664 ppp->npmode[i] = npi.mode;
665 /* we may be able to transmit more packets now (??) */
666 netif_wake_queue(ppp->dev);
671 #ifdef CONFIG_PPP_FILTER
675 struct sock_fprog uprog, *filtp;
676 struct sock_filter *code = NULL;
679 if (copy_from_user(&uprog, (void __user *) arg, sizeof(uprog)))
682 if (uprog.len > BPF_MAXINSNS)
686 len = uprog.len * sizeof(struct sock_filter);
687 code = kmalloc(len, GFP_KERNEL);
691 if (copy_from_user(code, (void __user *) uprog.filter, len)) {
695 err = sk_chk_filter(code, uprog.len);
701 filtp = (cmd == PPPIOCSPASS)? &ppp->pass_filter: &ppp->active_filter;
704 kfree(filtp->filter);
705 filtp->filter = code;
706 filtp->len = uprog.len;
711 #endif /* CONFIG_PPP_FILTER */
713 #ifdef CONFIG_PPP_MULTILINK
715 if (get_user(val, (int *) arg))
719 ppp_recv_unlock(ppp);
722 #endif /* CONFIG_PPP_MULTILINK */
731 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
732 unsigned int cmd, unsigned long arg)
734 int unit, err = -EFAULT;
736 struct channel *chan;
740 /* Create a new ppp unit */
741 if (get_user(unit, (int *) arg))
743 ppp = ppp_create_interface(unit, &err);
746 file->private_data = &ppp->file;
749 if (put_user(ppp->file.index, (int *) arg))
755 /* Attach to an existing ppp unit */
756 if (get_user(unit, (int *) arg))
760 ppp = ppp_find_unit(unit);
762 atomic_inc(&ppp->file.refcnt);
763 file->private_data = &ppp->file;
770 if (get_user(unit, (int *) arg))
772 spin_lock_bh(&all_channels_lock);
774 chan = ppp_find_channel(unit);
776 atomic_inc(&chan->file.refcnt);
777 file->private_data = &chan->file;
780 spin_unlock_bh(&all_channels_lock);
789 static struct file_operations ppp_device_fops = {
790 .owner = THIS_MODULE,
796 .release = ppp_release
799 #define PPP_MAJOR 108
801 /* Called at boot time if ppp is compiled into the kernel,
802 or at module load time (from init_module) if compiled as a module. */
803 static int __init ppp_init(void)
807 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
808 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
810 ppp_class = class_simple_create(THIS_MODULE, "ppp");
811 if (IS_ERR(ppp_class)) {
812 err = PTR_ERR(ppp_class);
815 class_simple_device_add(ppp_class, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
816 err = devfs_mk_cdev(MKDEV(PPP_MAJOR, 0),
817 S_IFCHR|S_IRUSR|S_IWUSR, "ppp");
824 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
828 class_simple_device_remove(MKDEV(PPP_MAJOR,0));
829 class_simple_destroy(ppp_class);
831 unregister_chrdev(PPP_MAJOR, "ppp");
836 * Network interface unit routines.
839 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
841 struct ppp *ppp = (struct ppp *) dev->priv;
845 npi = ethertype_to_npindex(ntohs(skb->protocol));
849 /* Drop, accept or reject the packet */
850 switch (ppp->npmode[npi]) {
854 /* it would be nice to have a way to tell the network
855 system to queue this one up for later. */
862 /* Put the 2-byte PPP protocol number on the front,
863 making sure there is room for the address and control fields. */
864 if (skb_headroom(skb) < PPP_HDRLEN) {
867 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
870 skb_reserve(ns, dev->hard_header_len);
871 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
875 pp = skb_push(skb, 2);
876 proto = npindex_to_proto[npi];
880 netif_stop_queue(dev);
881 skb_queue_tail(&ppp->file.xq, skb);
882 ppp_xmit_process(ppp);
887 ++ppp->stats.tx_dropped;
891 static struct net_device_stats *
892 ppp_net_stats(struct net_device *dev)
894 struct ppp *ppp = (struct ppp *) dev->priv;
900 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
902 struct ppp *ppp = dev->priv;
904 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
905 struct ppp_stats stats;
906 struct ppp_comp_stats cstats;
911 ppp_get_stats(ppp, &stats);
912 if (copy_to_user(addr, &stats, sizeof(stats)))
918 memset(&cstats, 0, sizeof(cstats));
919 if (ppp->xc_state != 0)
920 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
921 if (ppp->rc_state != 0)
922 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
923 if (copy_to_user(addr, &cstats, sizeof(cstats)))
930 if (copy_to_user(addr, vers, strlen(vers) + 1))
942 static void ppp_setup(struct net_device *dev)
944 dev->hard_header_len = PPP_HDRLEN;
947 dev->tx_queue_len = 3;
948 dev->type = ARPHRD_PPP;
949 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
953 * Transmit-side routines.
957 * Called to do any work queued up on the transmit side
958 * that can now be done.
961 ppp_xmit_process(struct ppp *ppp)
968 while (ppp->xmit_pending == 0
969 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
970 ppp_send_frame(ppp, skb);
971 /* If there's no work left to do, tell the core net
972 code that we can accept some more. */
973 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
974 netif_wake_queue(ppp->dev);
976 ppp_xmit_unlock(ppp);
980 * Compress and send a frame.
981 * The caller should have locked the xmit path,
982 * and xmit_pending should be 0.
985 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
987 int proto = PPP_PROTO(skb);
988 struct sk_buff *new_skb;
992 if (proto < 0x8000) {
993 #ifdef CONFIG_PPP_FILTER
994 /* check if we should pass this packet */
995 /* the filter instructions are constructed assuming
996 a four-byte PPP header on each packet */
998 u_int16_t *p = (u_int16_t *) skb_push(skb, 2);
1000 *p = htons(4); /* indicate outbound in DLT_LINUX_SLL */;
1002 if (ppp->pass_filter.filter
1003 && sk_run_filter(skb, ppp->pass_filter.filter,
1004 ppp->pass_filter.len) == 0) {
1006 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
1010 /* if this packet passes the active filter, record the time */
1011 if (!(ppp->active_filter.filter
1012 && sk_run_filter(skb, ppp->active_filter.filter,
1013 ppp->active_filter.len) == 0))
1014 ppp->last_xmit = jiffies;
1017 /* for data packets, record the time */
1018 ppp->last_xmit = jiffies;
1019 #endif /* CONFIG_PPP_FILTER */
1022 ++ppp->stats.tx_packets;
1023 ppp->stats.tx_bytes += skb->len - 2;
1027 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
1029 /* try to do VJ TCP header compression */
1030 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1033 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1036 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1038 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1039 new_skb->data + 2, &cp,
1040 !(ppp->flags & SC_NO_TCP_CCID));
1041 if (cp == skb->data + 2) {
1042 /* didn't compress */
1045 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1046 proto = PPP_VJC_COMP;
1047 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1049 proto = PPP_VJC_UNCOMP;
1050 cp[0] = skb->data[2];
1054 cp = skb_put(skb, len + 2);
1061 /* peek at outbound CCP frames */
1062 ppp_ccp_peek(ppp, skb, 0);
1066 /* try to do packet compression */
1067 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1068 && proto != PPP_LCP && proto != PPP_CCP) {
1069 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
1072 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1075 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1076 skb_reserve(new_skb,
1077 ppp->dev->hard_header_len - PPP_HDRLEN);
1079 /* compressor still expects A/C bytes in hdr */
1080 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1081 new_skb->data, skb->len + 2,
1082 ppp->dev->mtu + PPP_HDRLEN);
1083 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1087 skb_pull(skb, 2); /* pull off A/C bytes */
1089 /* didn't compress, or CCP not up yet */
1095 * If we are waiting for traffic (demand dialling),
1096 * queue it up for pppd to receive.
1098 if (ppp->flags & SC_LOOP_TRAFFIC) {
1099 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1101 skb_queue_tail(&ppp->file.rq, skb);
1102 wake_up_interruptible(&ppp->file.rwait);
1106 ppp->xmit_pending = skb;
1112 ++ppp->stats.tx_errors;
1116 * Try to send the frame in xmit_pending.
1117 * The caller should have the xmit path locked.
1120 ppp_push(struct ppp *ppp)
1122 struct list_head *list;
1123 struct channel *pch;
1124 struct sk_buff *skb = ppp->xmit_pending;
1129 list = &ppp->channels;
1130 if (list_empty(list)) {
1131 /* nowhere to send the packet, just drop it */
1132 ppp->xmit_pending = 0;
1137 if ((ppp->flags & SC_MULTILINK) == 0) {
1138 /* not doing multilink: send it down the first channel */
1140 pch = list_entry(list, struct channel, clist);
1142 spin_lock_bh(&pch->downl);
1144 if (pch->chan->ops->start_xmit(pch->chan, skb))
1145 ppp->xmit_pending = 0;
1147 /* channel got unregistered */
1149 ppp->xmit_pending = 0;
1151 spin_unlock_bh(&pch->downl);
1155 #ifdef CONFIG_PPP_MULTILINK
1156 /* Multilink: fragment the packet over as many links
1157 as can take the packet at the moment. */
1158 if (!ppp_mp_explode(ppp, skb))
1160 #endif /* CONFIG_PPP_MULTILINK */
1162 ppp->xmit_pending = 0;
1166 #ifdef CONFIG_PPP_MULTILINK
1168 * Divide a packet to be transmitted into fragments and
1169 * send them out the individual links.
1171 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1173 int nch, len, fragsize;
1174 int i, bits, hdrlen, mtu;
1176 unsigned char *p, *q;
1177 struct list_head *list;
1178 struct channel *pch;
1179 struct sk_buff *frag;
1180 struct ppp_channel *chan;
1183 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1184 list = &ppp->channels;
1185 while ((list = list->next) != &ppp->channels) {
1186 pch = list_entry(list, struct channel, clist);
1187 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1189 * If a channel hasn't had a fragment yet, it has to get
1190 * one before we send any fragments on later channels.
1191 * If it can't take a fragment now, don't give any
1192 * to subsequent channels.
1194 if (!pch->had_frag && !pch->avail) {
1195 while ((list = list->next) != &ppp->channels) {
1196 pch = list_entry(list, struct channel, clist);
1203 return 0; /* can't take now, leave it in xmit_pending */
1205 /* Do protocol field compression (XXX this should be optional) */
1213 /* decide on fragment size */
1216 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1219 fragsize = ROUNDUP(fragsize, nch);
1222 /* skip to the channel after the one we last used
1223 and start at that one */
1224 for (i = 0; i < ppp->nxchan; ++i) {
1226 if (list == &ppp->channels) {
1232 /* create a fragment for each channel */
1236 if (list == &ppp->channels) {
1240 pch = list_entry(list, struct channel, clist);
1245 /* check the channel's mtu and whether it is still attached. */
1246 spin_lock_bh(&pch->downl);
1247 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1248 /* can't use this channel */
1249 spin_unlock_bh(&pch->downl);
1257 * We have to create multiple fragments for this channel
1258 * if fragsize is greater than the channel's mtu.
1262 for (flen = fragsize; flen > 0; flen -= fnb) {
1264 if (fnb > mtu + 2 - hdrlen)
1265 fnb = mtu + 2 - hdrlen;
1268 frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1271 q = skb_put(frag, fnb + hdrlen);
1272 /* make the MP header */
1275 if (ppp->flags & SC_MP_XSHORTSEQ) {
1276 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1280 q[3] = ppp->nxseq >> 16;
1281 q[4] = ppp->nxseq >> 8;
1285 /* copy the data in */
1286 memcpy(q + hdrlen, p, fnb);
1288 /* try to send it down the channel */
1290 if (!chan->ops->start_xmit(chan, frag))
1291 skb_queue_tail(&pch->file.xq, frag);
1298 spin_unlock_bh(&pch->downl);
1305 spin_unlock_bh(&pch->downl);
1307 printk(KERN_ERR "PPP: no memory (fragment)\n");
1308 ++ppp->stats.tx_errors;
1310 return 1; /* abandon the frame */
1312 #endif /* CONFIG_PPP_MULTILINK */
1315 * Try to send data out on a channel.
1318 ppp_channel_push(struct channel *pch)
1320 struct sk_buff *skb;
1323 spin_lock_bh(&pch->downl);
1324 if (pch->chan != 0) {
1325 while (skb_queue_len(&pch->file.xq) > 0) {
1326 skb = skb_dequeue(&pch->file.xq);
1327 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1328 /* put the packet back and try again later */
1329 skb_queue_head(&pch->file.xq, skb);
1334 /* channel got deregistered */
1335 skb_queue_purge(&pch->file.xq);
1337 spin_unlock_bh(&pch->downl);
1338 /* see if there is anything from the attached unit to be sent */
1339 if (skb_queue_len(&pch->file.xq) == 0) {
1340 read_lock_bh(&pch->upl);
1343 ppp_xmit_process(ppp);
1344 read_unlock_bh(&pch->upl);
1349 * Receive-side routines.
1352 /* misuse a few fields of the skb for MP reconstruction */
1353 #define sequence priority
1354 #define BEbits cb[0]
1357 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1360 /* ppp->dev == 0 means interface is closing down */
1362 ppp_receive_frame(ppp, skb, pch);
1365 ppp_recv_unlock(ppp);
1369 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1371 struct channel *pch = chan->ppp;
1374 if (pch == 0 || skb->len == 0) {
1379 proto = PPP_PROTO(skb);
1380 read_lock_bh(&pch->upl);
1381 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1382 /* put it on the channel queue */
1383 skb_queue_tail(&pch->file.rq, skb);
1384 /* drop old frames if queue too long */
1385 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1386 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1388 wake_up_interruptible(&pch->file.rwait);
1390 ppp_do_recv(pch->ppp, skb, pch);
1392 read_unlock_bh(&pch->upl);
1395 /* Put a 0-length skb in the receive queue as an error indication */
1397 ppp_input_error(struct ppp_channel *chan, int code)
1399 struct channel *pch = chan->ppp;
1400 struct sk_buff *skb;
1405 read_lock_bh(&pch->upl);
1406 if (pch->ppp != 0) {
1407 skb = alloc_skb(0, GFP_ATOMIC);
1409 skb->len = 0; /* probably unnecessary */
1411 ppp_do_recv(pch->ppp, skb, pch);
1414 read_unlock_bh(&pch->upl);
1418 * We come in here to process a received frame.
1419 * The receive side of the ppp unit is locked.
1422 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1424 if (skb->len >= 2) {
1425 #ifdef CONFIG_PPP_MULTILINK
1426 /* XXX do channel-level decompression here */
1427 if (PPP_PROTO(skb) == PPP_MP)
1428 ppp_receive_mp_frame(ppp, skb, pch);
1430 #endif /* CONFIG_PPP_MULTILINK */
1431 ppp_receive_nonmp_frame(ppp, skb);
1436 /* note: a 0-length skb is used as an error indication */
1437 ++ppp->stats.rx_length_errors;
1440 ppp_receive_error(ppp);
1444 ppp_receive_error(struct ppp *ppp)
1446 ++ppp->stats.rx_errors;
1452 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1455 int proto, len, npi;
1458 * Decompress the frame, if compressed.
1459 * Note that some decompressors need to see uncompressed frames
1460 * that come in as well as compressed frames.
1462 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1463 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1464 skb = ppp_decompress_frame(ppp, skb);
1466 proto = PPP_PROTO(skb);
1469 /* decompress VJ compressed packets */
1470 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1473 if (skb_tailroom(skb) < 124) {
1474 /* copy to a new sk_buff with more tailroom */
1475 ns = dev_alloc_skb(skb->len + 128);
1477 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1481 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
1485 else if (!pskb_may_pull(skb, skb->len))
1488 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1490 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1495 skb_put(skb, len - skb->len);
1496 else if (len < skb->len)
1501 case PPP_VJC_UNCOMP:
1502 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1505 /* Until we fix the decompressor need to make sure
1506 * data portion is linear.
1508 if (!pskb_may_pull(skb, skb->len))
1511 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1512 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1519 ppp_ccp_peek(ppp, skb, 1);
1523 ++ppp->stats.rx_packets;
1524 ppp->stats.rx_bytes += skb->len - 2;
1526 npi = proto_to_npindex(proto);
1528 /* control or unknown frame - pass it to pppd */
1529 skb_queue_tail(&ppp->file.rq, skb);
1530 /* limit queue length by dropping old frames */
1531 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1532 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1534 /* wake up any process polling or blocking on read */
1535 wake_up_interruptible(&ppp->file.rwait);
1538 /* network protocol frame - give it to the kernel */
1540 #ifdef CONFIG_PPP_FILTER
1541 /* check if the packet passes the pass and active filters */
1542 /* the filter instructions are constructed assuming
1543 a four-byte PPP header on each packet */
1545 u_int16_t *p = (u_int16_t *) skb_push(skb, 2);
1547 *p = 0; /* indicate inbound in DLT_LINUX_SLL */
1549 if (ppp->pass_filter.filter
1550 && sk_run_filter(skb, ppp->pass_filter.filter,
1551 ppp->pass_filter.len) == 0) {
1553 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1557 if (!(ppp->active_filter.filter
1558 && sk_run_filter(skb, ppp->active_filter.filter,
1559 ppp->active_filter.len) == 0))
1560 ppp->last_recv = jiffies;
1563 ppp->last_recv = jiffies;
1564 #endif /* CONFIG_PPP_FILTER */
1566 if ((ppp->dev->flags & IFF_UP) == 0
1567 || ppp->npmode[npi] != NPMODE_PASS) {
1570 skb_pull(skb, 2); /* chop off protocol */
1571 skb->dev = ppp->dev;
1572 skb->protocol = htons(npindex_to_ethertype[npi]);
1573 skb->mac.raw = skb->data;
1575 ppp->dev->last_rx = jiffies;
1582 ppp_receive_error(ppp);
1585 static struct sk_buff *
1586 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1588 int proto = PPP_PROTO(skb);
1592 /* Until we fix all the decompressor's need to make sure
1593 * data portion is linear.
1595 if (!pskb_may_pull(skb, skb->len))
1598 if (proto == PPP_COMP) {
1599 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1601 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1604 /* the decompressor still expects the A/C bytes in the hdr */
1605 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1606 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1608 /* Pass the compressed frame to pppd as an
1609 error indication. */
1610 if (len == DECOMP_FATALERROR)
1611 ppp->rstate |= SC_DC_FERROR;
1619 skb_pull(skb, 2); /* pull off the A/C bytes */
1622 /* Uncompressed frame - pass to decompressor so it
1623 can update its dictionary if necessary. */
1624 if (ppp->rcomp->incomp)
1625 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1632 ppp->rstate |= SC_DC_ERROR;
1633 ppp_receive_error(ppp);
1637 #ifdef CONFIG_PPP_MULTILINK
1639 * Receive a multilink frame.
1640 * We put it on the reconstruction queue and then pull off
1641 * as many completed frames as we can.
1644 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1647 struct list_head *l;
1648 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1650 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
1651 goto err; /* no good, throw it away */
1653 /* Decode sequence number and begin/end bits */
1654 if (ppp->flags & SC_MP_SHORTSEQ) {
1655 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1658 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1661 skb->BEbits = skb->data[2];
1662 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1665 * Do protocol ID decompression on the first fragment of each packet.
1667 if ((skb->BEbits & B) && (skb->data[0] & 1))
1668 *skb_push(skb, 1) = 0;
1671 * Expand sequence number to 32 bits, making it as close
1672 * as possible to ppp->minseq.
1674 seq |= ppp->minseq & ~mask;
1675 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1677 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1678 seq -= mask + 1; /* should never happen */
1679 skb->sequence = seq;
1683 * If this packet comes before the next one we were expecting,
1686 if (seq_before(seq, ppp->nextseq)) {
1688 ++ppp->stats.rx_dropped;
1689 ppp_receive_error(ppp);
1694 * Reevaluate minseq, the minimum over all channels of the
1695 * last sequence number received on each channel. Because of
1696 * the increasing sequence number rule, we know that any fragment
1697 * before `minseq' which hasn't arrived is never going to arrive.
1698 * The list of channels can't change because we have the receive
1699 * side of the ppp unit locked.
1701 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1702 struct channel *ch = list_entry(l, struct channel, clist);
1703 if (seq_before(ch->lastseq, seq))
1706 if (seq_before(ppp->minseq, seq))
1709 /* Put the fragment on the reconstruction queue */
1710 ppp_mp_insert(ppp, skb);
1712 /* If the queue is getting long, don't wait any longer for packets
1713 before the start of the queue. */
1714 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1715 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1716 ppp->minseq = ppp->mrq.next->sequence;
1718 /* Pull completed packets off the queue and receive them. */
1719 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1720 ppp_receive_nonmp_frame(ppp, skb);
1726 ppp_receive_error(ppp);
1730 * Insert a fragment on the MP reconstruction queue.
1731 * The queue is ordered by increasing sequence number.
1734 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1737 struct sk_buff_head *list = &ppp->mrq;
1738 u32 seq = skb->sequence;
1740 /* N.B. we don't need to lock the list lock because we have the
1741 ppp unit receive-side lock. */
1742 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1743 if (seq_before(seq, p->sequence))
1745 __skb_insert(skb, p->prev, p, list);
1749 * Reconstruct a packet from the MP fragment queue.
1750 * We go through increasing sequence numbers until we find a
1751 * complete packet, or we get to the sequence number for a fragment
1752 * which hasn't arrived but might still do so.
1755 ppp_mp_reconstruct(struct ppp *ppp)
1757 u32 seq = ppp->nextseq;
1758 u32 minseq = ppp->minseq;
1759 struct sk_buff_head *list = &ppp->mrq;
1760 struct sk_buff *p, *next;
1761 struct sk_buff *head, *tail;
1762 struct sk_buff *skb = NULL;
1763 int lost = 0, len = 0;
1765 if (ppp->mrru == 0) /* do nothing until mrru is set */
1769 for (p = head; p != (struct sk_buff *) list; p = next) {
1771 if (seq_before(p->sequence, seq)) {
1772 /* this can't happen, anyway ignore the skb */
1773 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1778 if (p->sequence != seq) {
1779 /* Fragment `seq' is missing. If it is after
1780 minseq, it might arrive later, so stop here. */
1781 if (seq_after(seq, minseq))
1783 /* Fragment `seq' is lost, keep going. */
1785 seq = seq_before(minseq, p->sequence)?
1786 minseq + 1: p->sequence;
1792 * At this point we know that all the fragments from
1793 * ppp->nextseq to seq are either present or lost.
1794 * Also, there are no complete packets in the queue
1795 * that have no missing fragments and end before this
1799 /* B bit set indicates this fragment starts a packet */
1800 if (p->BEbits & B) {
1808 /* Got a complete packet yet? */
1809 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1810 if (len > ppp->mrru + 2) {
1811 ++ppp->stats.rx_length_errors;
1812 printk(KERN_DEBUG "PPP: reconstructed packet"
1813 " is too long (%d)\n", len);
1814 } else if (p == head) {
1815 /* fragment is complete packet - reuse skb */
1819 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1820 ++ppp->stats.rx_missed_errors;
1821 printk(KERN_DEBUG "PPP: no memory for "
1822 "reconstructed packet");
1827 ppp->nextseq = seq + 1;
1831 * If this is the ending fragment of a packet,
1832 * and we haven't found a complete valid packet yet,
1833 * we can discard up to and including this fragment.
1841 /* If we have a complete packet, copy it all into one skb. */
1843 /* If we have discarded any fragments,
1844 signal a receive error. */
1845 if (head->sequence != ppp->nextseq) {
1847 printk(KERN_DEBUG " missed pkts %u..%u\n",
1848 ppp->nextseq, head->sequence-1);
1849 ++ppp->stats.rx_dropped;
1850 ppp_receive_error(ppp);
1854 /* copy to a single skb */
1855 for (p = head; p != tail->next; p = p->next)
1856 skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
1857 ppp->nextseq = tail->sequence + 1;
1861 /* Discard all the skbuffs that we have copied the data out of
1862 or that we can't use. */
1863 while ((p = list->next) != head) {
1864 __skb_unlink(p, list);
1870 #endif /* CONFIG_PPP_MULTILINK */
1873 * Channel interface.
1877 * Create a new, unattached ppp channel.
1880 ppp_register_channel(struct ppp_channel *chan)
1882 struct channel *pch;
1884 pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
1887 memset(pch, 0, sizeof(struct channel));
1891 init_ppp_file(&pch->file, CHANNEL);
1892 pch->file.hdrlen = chan->hdrlen;
1893 #ifdef CONFIG_PPP_MULTILINK
1895 #endif /* CONFIG_PPP_MULTILINK */
1896 init_rwsem(&pch->chan_sem);
1897 spin_lock_init(&pch->downl);
1898 pch->upl = RW_LOCK_UNLOCKED;
1899 spin_lock_bh(&all_channels_lock);
1900 pch->file.index = ++last_channel_index;
1901 list_add(&pch->list, &new_channels);
1902 atomic_inc(&channel_count);
1903 spin_unlock_bh(&all_channels_lock);
1908 * Return the index of a channel.
1910 int ppp_channel_index(struct ppp_channel *chan)
1912 struct channel *pch = chan->ppp;
1915 return pch->file.index;
1920 * Return the PPP unit number to which a channel is connected.
1922 int ppp_unit_number(struct ppp_channel *chan)
1924 struct channel *pch = chan->ppp;
1928 read_lock_bh(&pch->upl);
1930 unit = pch->ppp->file.index;
1931 read_unlock_bh(&pch->upl);
1937 * Disconnect a channel from the generic layer.
1938 * This must be called in process context.
1941 ppp_unregister_channel(struct ppp_channel *chan)
1943 struct channel *pch = chan->ppp;
1946 return; /* should never happen */
1950 * This ensures that we have returned from any calls into the
1951 * the channel's start_xmit or ioctl routine before we proceed.
1953 down_write(&pch->chan_sem);
1954 spin_lock_bh(&pch->downl);
1956 spin_unlock_bh(&pch->downl);
1957 up_write(&pch->chan_sem);
1958 ppp_disconnect_channel(pch);
1959 spin_lock_bh(&all_channels_lock);
1960 list_del(&pch->list);
1961 spin_unlock_bh(&all_channels_lock);
1963 wake_up_interruptible(&pch->file.rwait);
1964 if (atomic_dec_and_test(&pch->file.refcnt))
1965 ppp_destroy_channel(pch);
1969 * Callback from a channel when it can accept more to transmit.
1970 * This should be called at BH/softirq level, not interrupt level.
1973 ppp_output_wakeup(struct ppp_channel *chan)
1975 struct channel *pch = chan->ppp;
1979 ppp_channel_push(pch);
1983 * Compression control.
1986 /* Process the PPPIOCSCOMPRESS ioctl. */
1988 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1991 struct compressor *cp, *ocomp;
1992 struct ppp_option_data data;
1993 void *state, *ostate;
1994 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1997 if (copy_from_user(&data, (void __user *) arg, sizeof(data))
1998 || (data.length <= CCP_MAX_OPTION_LENGTH
1999 && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
2002 if (data.length > CCP_MAX_OPTION_LENGTH
2003 || ccp_option[1] < 2 || ccp_option[1] > data.length)
2006 cp = find_compressor(ccp_option[0]);
2009 request_module("ppp-compress-%d", ccp_option[0]);
2010 cp = find_compressor(ccp_option[0]);
2012 #endif /* CONFIG_KMOD */
2017 if (data.transmit) {
2018 state = cp->comp_alloc(ccp_option, data.length);
2021 ppp->xstate &= ~SC_COMP_RUN;
2023 ostate = ppp->xc_state;
2025 ppp->xc_state = state;
2026 ppp_xmit_unlock(ppp);
2028 ocomp->comp_free(ostate);
2029 module_put(ocomp->owner);
2033 module_put(cp->owner);
2036 state = cp->decomp_alloc(ccp_option, data.length);
2039 ppp->rstate &= ~SC_DECOMP_RUN;
2041 ostate = ppp->rc_state;
2043 ppp->rc_state = state;
2044 ppp_recv_unlock(ppp);
2046 ocomp->decomp_free(ostate);
2047 module_put(ocomp->owner);
2051 module_put(cp->owner);
2059 * Look at a CCP packet and update our state accordingly.
2060 * We assume the caller has the xmit or recv path locked.
2063 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2068 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2069 return; /* no header */
2072 switch (CCP_CODE(dp)) {
2075 /* A ConfReq starts negotiation of compression
2076 * in one direction of transmission,
2077 * and hence brings it down...but which way?
2080 * A ConfReq indicates what the sender would like to receive
2083 /* He is proposing what I should send */
2084 ppp->xstate &= ~SC_COMP_RUN;
2086 /* I am proposing to what he should send */
2087 ppp->rstate &= ~SC_DECOMP_RUN;
2094 * CCP is going down, both directions of transmission
2096 ppp->rstate &= ~SC_DECOMP_RUN;
2097 ppp->xstate &= ~SC_COMP_RUN;
2101 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2103 len = CCP_LENGTH(dp);
2104 if (!pskb_may_pull(skb, len + 2))
2105 return; /* too short */
2108 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2111 /* we will start receiving compressed packets */
2112 if (ppp->rc_state == 0)
2114 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2115 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2116 ppp->rstate |= SC_DECOMP_RUN;
2117 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2120 /* we will soon start sending compressed packets */
2121 if (ppp->xc_state == 0)
2123 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2124 ppp->file.index, 0, ppp->debug))
2125 ppp->xstate |= SC_COMP_RUN;
2130 /* reset the [de]compressor */
2131 if ((ppp->flags & SC_CCP_UP) == 0)
2134 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2135 ppp->rcomp->decomp_reset(ppp->rc_state);
2136 ppp->rstate &= ~SC_DC_ERROR;
2139 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2140 ppp->xcomp->comp_reset(ppp->xc_state);
2146 /* Free up compression resources. */
2148 ppp_ccp_closed(struct ppp *ppp)
2150 void *xstate, *rstate;
2151 struct compressor *xcomp, *rcomp;
2154 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2157 xstate = ppp->xc_state;
2161 rstate = ppp->rc_state;
2166 xcomp->comp_free(xstate);
2167 module_put(xcomp->owner);
2170 rcomp->decomp_free(rstate);
2171 module_put(rcomp->owner);
2175 /* List of compressors. */
2176 static LIST_HEAD(compressor_list);
2177 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2179 struct compressor_entry {
2180 struct list_head list;
2181 struct compressor *comp;
2184 static struct compressor_entry *
2185 find_comp_entry(int proto)
2187 struct compressor_entry *ce;
2188 struct list_head *list = &compressor_list;
2190 while ((list = list->next) != &compressor_list) {
2191 ce = list_entry(list, struct compressor_entry, list);
2192 if (ce->comp->compress_proto == proto)
2198 /* Register a compressor */
2200 ppp_register_compressor(struct compressor *cp)
2202 struct compressor_entry *ce;
2204 spin_lock(&compressor_list_lock);
2206 if (find_comp_entry(cp->compress_proto) != 0)
2209 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2214 list_add(&ce->list, &compressor_list);
2216 spin_unlock(&compressor_list_lock);
2220 /* Unregister a compressor */
2222 ppp_unregister_compressor(struct compressor *cp)
2224 struct compressor_entry *ce;
2226 spin_lock(&compressor_list_lock);
2227 ce = find_comp_entry(cp->compress_proto);
2228 if (ce != 0 && ce->comp == cp) {
2229 list_del(&ce->list);
2232 spin_unlock(&compressor_list_lock);
2235 /* Find a compressor. */
2236 static struct compressor *
2237 find_compressor(int type)
2239 struct compressor_entry *ce;
2240 struct compressor *cp = 0;
2242 spin_lock(&compressor_list_lock);
2243 ce = find_comp_entry(type);
2246 if (!try_module_get(cp->owner))
2249 spin_unlock(&compressor_list_lock);
2254 * Miscelleneous stuff.
2258 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2260 struct slcompress *vj = ppp->vj;
2262 memset(st, 0, sizeof(*st));
2263 st->p.ppp_ipackets = ppp->stats.rx_packets;
2264 st->p.ppp_ierrors = ppp->stats.rx_errors;
2265 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2266 st->p.ppp_opackets = ppp->stats.tx_packets;
2267 st->p.ppp_oerrors = ppp->stats.tx_errors;
2268 st->p.ppp_obytes = ppp->stats.tx_bytes;
2271 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2272 st->vj.vjs_compressed = vj->sls_o_compressed;
2273 st->vj.vjs_searches = vj->sls_o_searches;
2274 st->vj.vjs_misses = vj->sls_o_misses;
2275 st->vj.vjs_errorin = vj->sls_i_error;
2276 st->vj.vjs_tossed = vj->sls_i_tossed;
2277 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2278 st->vj.vjs_compressedin = vj->sls_i_compressed;
2282 * Stuff for handling the lists of ppp units and channels
2283 * and for initialization.
2287 * Create a new ppp interface unit. Fails if it can't allocate memory
2288 * or if there is already a unit with the requested number.
2289 * unit == -1 means allocate a new number.
2292 ppp_create_interface(int unit, int *retp)
2295 struct net_device *dev = NULL;
2299 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2302 dev = alloc_netdev(0, "", ppp_setup);
2305 memset(ppp, 0, sizeof(struct ppp));
2308 init_ppp_file(&ppp->file, INTERFACE);
2309 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2310 for (i = 0; i < NUM_NP; ++i)
2311 ppp->npmode[i] = NPMODE_PASS;
2312 INIT_LIST_HEAD(&ppp->channels);
2313 spin_lock_init(&ppp->rlock);
2314 spin_lock_init(&ppp->wlock);
2315 #ifdef CONFIG_PPP_MULTILINK
2317 skb_queue_head_init(&ppp->mrq);
2318 #endif /* CONFIG_PPP_MULTILINK */
2322 dev->hard_start_xmit = ppp_start_xmit;
2323 dev->get_stats = ppp_net_stats;
2324 dev->do_ioctl = ppp_net_ioctl;
2329 unit = cardmap_find_first_free(all_ppp_units);
2330 else if (cardmap_get(all_ppp_units, unit) != NULL)
2331 goto out2; /* unit already exists */
2333 /* Initialize the new ppp unit */
2334 ppp->file.index = unit;
2335 sprintf(dev->name, "ppp%d", unit);
2337 ret = register_netdev(dev);
2339 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2344 atomic_inc(&ppp_unit_count);
2345 cardmap_set(&all_ppp_units, unit, ppp);
2361 * Initialize a ppp_file structure.
2364 init_ppp_file(struct ppp_file *pf, int kind)
2367 skb_queue_head_init(&pf->xq);
2368 skb_queue_head_init(&pf->rq);
2369 atomic_set(&pf->refcnt, 1);
2370 init_waitqueue_head(&pf->rwait);
2374 * Take down a ppp interface unit - called when the owning file
2375 * (the one that created the unit) is closed or detached.
2377 static void ppp_shutdown_interface(struct ppp *ppp)
2379 struct net_device *dev;
2386 /* This will call dev_close() for us. */
2388 unregister_netdev(dev);
2391 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2394 wake_up_interruptible(&ppp->file.rwait);
2399 * Free the memory used by a ppp unit. This is only called once
2400 * there are no channels connected to the unit and no file structs
2401 * that reference the unit.
2403 static void ppp_destroy_interface(struct ppp *ppp)
2405 atomic_dec(&ppp_unit_count);
2407 if (!ppp->file.dead || ppp->n_channels) {
2408 /* "can't happen" */
2409 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2410 "n_channels=%d !\n", ppp, ppp->file.dead,
2415 ppp_ccp_closed(ppp);
2420 skb_queue_purge(&ppp->file.xq);
2421 skb_queue_purge(&ppp->file.rq);
2422 #ifdef CONFIG_PPP_MULTILINK
2423 skb_queue_purge(&ppp->mrq);
2424 #endif /* CONFIG_PPP_MULTILINK */
2425 #ifdef CONFIG_PPP_FILTER
2426 if (ppp->pass_filter.filter) {
2427 kfree(ppp->pass_filter.filter);
2428 ppp->pass_filter.filter = NULL;
2430 if (ppp->active_filter.filter) {
2431 kfree(ppp->active_filter.filter);
2432 ppp->active_filter.filter = 0;
2434 #endif /* CONFIG_PPP_FILTER */
2440 * Locate an existing ppp unit.
2441 * The caller should have locked the all_ppp_sem.
2444 ppp_find_unit(int unit)
2446 return cardmap_get(all_ppp_units, unit);
2450 * Locate an existing ppp channel.
2451 * The caller should have locked the all_channels_lock.
2452 * First we look in the new_channels list, then in the
2453 * all_channels list. If found in the new_channels list,
2454 * we move it to the all_channels list. This is for speed
2455 * when we have a lot of channels in use.
2457 static struct channel *
2458 ppp_find_channel(int unit)
2460 struct channel *pch;
2461 struct list_head *list;
2463 list = &new_channels;
2464 while ((list = list->next) != &new_channels) {
2465 pch = list_entry(list, struct channel, list);
2466 if (pch->file.index == unit) {
2467 list_del(&pch->list);
2468 list_add(&pch->list, &all_channels);
2472 list = &all_channels;
2473 while ((list = list->next) != &all_channels) {
2474 pch = list_entry(list, struct channel, list);
2475 if (pch->file.index == unit)
2482 * Connect a PPP channel to a PPP interface unit.
2485 ppp_connect_channel(struct channel *pch, int unit)
2492 ppp = ppp_find_unit(unit);
2495 write_lock_bh(&pch->upl);
2501 if (pch->file.hdrlen > ppp->file.hdrlen)
2502 ppp->file.hdrlen = pch->file.hdrlen;
2503 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2504 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2505 ppp->dev->hard_header_len = hdrlen;
2506 list_add_tail(&pch->clist, &ppp->channels);
2509 atomic_inc(&ppp->file.refcnt);
2514 write_unlock_bh(&pch->upl);
2521 * Disconnect a channel from its ppp unit.
2524 ppp_disconnect_channel(struct channel *pch)
2529 write_lock_bh(&pch->upl);
2532 write_unlock_bh(&pch->upl);
2534 /* remove it from the ppp unit's list */
2536 list_del(&pch->clist);
2539 if (atomic_dec_and_test(&ppp->file.refcnt))
2540 ppp_destroy_interface(ppp);
2547 * Free up the resources used by a ppp channel.
2549 static void ppp_destroy_channel(struct channel *pch)
2551 atomic_dec(&channel_count);
2553 if (!pch->file.dead) {
2554 /* "can't happen" */
2555 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2559 skb_queue_purge(&pch->file.xq);
2560 skb_queue_purge(&pch->file.rq);
2564 static void __exit ppp_cleanup(void)
2566 /* should never happen */
2567 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2568 printk(KERN_ERR "PPP: removing module but units remain!\n");
2569 cardmap_destroy(&all_ppp_units);
2570 if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2571 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2572 devfs_remove("ppp");
2573 class_simple_device_remove(MKDEV(PPP_MAJOR, 0));
2574 class_simple_destroy(ppp_class);
2578 * Cardmap implementation.
2580 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2585 for (p = map; p != NULL; ) {
2586 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2590 nr &= ~(CARDMAP_MASK << p->shift);
2596 static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2602 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2604 /* need a new top level */
2605 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2606 memset(np, 0, sizeof(*np));
2609 np->shift = p->shift + CARDMAP_ORDER;
2614 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2617 while (p->shift > 0) {
2618 i = (nr >> p->shift) & CARDMAP_MASK;
2619 if (p->ptr[i] == NULL) {
2620 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2621 memset(np, 0, sizeof(*np));
2622 np->shift = p->shift - CARDMAP_ORDER;
2627 clear_bit(i, &p->inuse);
2630 i = nr & CARDMAP_MASK;
2633 set_bit(i, &p->inuse);
2635 clear_bit(i, &p->inuse);
2638 static unsigned int cardmap_find_first_free(struct cardmap *map)
2641 unsigned int nr = 0;
2644 if ((p = map) == NULL)
2647 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2648 if (i >= CARDMAP_WIDTH) {
2649 if (p->parent == NULL)
2650 return CARDMAP_WIDTH << p->shift;
2652 i = (nr >> p->shift) & CARDMAP_MASK;
2653 set_bit(i, &p->inuse);
2656 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2657 if (p->shift == 0 || p->ptr[i] == NULL)
2663 static void cardmap_destroy(struct cardmap **pmap)
2665 struct cardmap *p, *np;
2668 for (p = *pmap; p != NULL; p = np) {
2669 if (p->shift != 0) {
2670 for (i = 0; i < CARDMAP_WIDTH; ++i)
2671 if (p->ptr[i] != NULL)
2673 if (i < CARDMAP_WIDTH) {
2685 /* Module/initialization stuff */
2687 module_init(ppp_init);
2688 module_exit(ppp_cleanup);
2690 EXPORT_SYMBOL(ppp_register_channel);
2691 EXPORT_SYMBOL(ppp_unregister_channel);
2692 EXPORT_SYMBOL(ppp_channel_index);
2693 EXPORT_SYMBOL(ppp_unit_number);
2694 EXPORT_SYMBOL(ppp_input);
2695 EXPORT_SYMBOL(ppp_input_error);
2696 EXPORT_SYMBOL(ppp_output_wakeup);
2697 EXPORT_SYMBOL(ppp_register_compressor);
2698 EXPORT_SYMBOL(ppp_unregister_compressor);
2699 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2700 EXPORT_SYMBOL(all_channels); /* for debugging */
2701 MODULE_LICENSE("GPL");
2702 MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
2703 MODULE_ALIAS("/dev/ppp");