[PATCH] DVB core update
[linux-2.6/history.git] / drivers / net / ppp_generic.c
blob606d7bf9842d41227f6221db95664086b9e73c42
1 /*
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
16 * channel.
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>
42 #include <linux/ip.h>
43 #include <linux/tcp.h>
44 #include <linux/spinlock.h>
45 #include <linux/smp_lock.h>
46 #include <linux/rwsem.h>
47 #include <linux/stddef.h>
48 #include <net/slhc_vj.h>
49 #include <asm/atomic.h>
51 #define PPP_VERSION "2.4.2"
54 * Network protocols we support.
56 #define NP_IP 0 /* Internet Protocol V4 */
57 #define NP_IPV6 1 /* Internet Protocol V6 */
58 #define NP_IPX 2 /* IPX protocol */
59 #define NP_AT 3 /* Appletalk protocol */
60 #define NP_MPLS_UC 4 /* MPLS unicast */
61 #define NP_MPLS_MC 5 /* MPLS multicast */
62 #define NUM_NP 6 /* Number of NPs. */
64 #define MPHDRLEN 6 /* multilink protocol header length */
65 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
66 #define MIN_FRAG_SIZE 64
69 * An instance of /dev/ppp can be associated with either a ppp
70 * interface unit or a ppp channel. In both cases, file->private_data
71 * points to one of these.
73 struct ppp_file {
74 enum {
75 INTERFACE=1, CHANNEL
76 } kind;
77 struct sk_buff_head xq; /* pppd transmit queue */
78 struct sk_buff_head rq; /* receive queue for pppd */
79 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
80 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
81 int hdrlen; /* space to leave for headers */
82 int index; /* interface unit / channel number */
83 int dead; /* unit/channel has been shut down */
86 #define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
88 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
89 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
91 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
94 * Data structure describing one ppp unit.
95 * A ppp unit corresponds to a ppp network interface device
96 * and represents a multilink bundle.
97 * It can have 0 or more ppp channels connected to it.
99 struct ppp {
100 struct ppp_file file; /* stuff for read/write/poll 0 */
101 struct file *owner; /* file that owns this unit 48 */
102 struct list_head channels; /* list of attached channels 4c */
103 int n_channels; /* how many channels are attached 54 */
104 spinlock_t rlock; /* lock for receive side 58 */
105 spinlock_t wlock; /* lock for transmit side 5c */
106 int mru; /* max receive unit 60 */
107 unsigned int flags; /* control bits 64 */
108 unsigned int xstate; /* transmit state bits 68 */
109 unsigned int rstate; /* receive state bits 6c */
110 int debug; /* debug flags 70 */
111 struct slcompress *vj; /* state for VJ header compression */
112 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
113 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
114 struct compressor *xcomp; /* transmit packet compressor 8c */
115 void *xc_state; /* its internal state 90 */
116 struct compressor *rcomp; /* receive decompressor 94 */
117 void *rc_state; /* its internal state 98 */
118 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
119 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
120 struct net_device *dev; /* network interface device a4 */
121 #ifdef CONFIG_PPP_MULTILINK
122 int nxchan; /* next channel to send something on */
123 u32 nxseq; /* next sequence number to send */
124 int mrru; /* MP: max reconst. receive unit */
125 u32 nextseq; /* MP: seq no of next packet */
126 u32 minseq; /* MP: min of most recent seqnos */
127 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
128 #endif /* CONFIG_PPP_MULTILINK */
129 struct net_device_stats stats; /* statistics */
130 #ifdef CONFIG_PPP_FILTER
131 struct sock_fprog pass_filter; /* filter for packets to pass */
132 struct sock_fprog active_filter;/* filter for pkts to reset idle */
133 #endif /* CONFIG_PPP_FILTER */
137 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
138 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
139 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
140 * Bits in xstate: SC_COMP_RUN
142 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
143 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
144 |SC_COMP_TCP|SC_REJ_COMP_TCP)
147 * Private data structure for each channel.
148 * This includes the data structure used for multilink.
150 struct channel {
151 struct ppp_file file; /* stuff for read/write/poll */
152 struct list_head list; /* link in all/new_channels list */
153 struct ppp_channel *chan; /* public channel data structure */
154 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
155 spinlock_t downl; /* protects `chan', file.xq dequeue */
156 struct ppp *ppp; /* ppp unit we're connected to */
157 struct list_head clist; /* link in list of channels per unit */
158 rwlock_t upl; /* protects `ppp' */
159 #ifdef CONFIG_PPP_MULTILINK
160 u8 avail; /* flag used in multilink stuff */
161 u8 had_frag; /* >= 1 fragments have been sent */
162 u32 lastseq; /* MP: last sequence # received */
163 #endif /* CONFIG_PPP_MULTILINK */
167 * SMP locking issues:
168 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
169 * list and the ppp.n_channels field, you need to take both locks
170 * before you modify them.
171 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
172 * channel.downl.
176 * A cardmap represents a mapping from unsigned integers to pointers,
177 * and provides a fast "find lowest unused number" operation.
178 * It uses a broad (32-way) tree with a bitmap at each level.
179 * It is designed to be space-efficient for small numbers of entries
180 * and time-efficient for large numbers of entries.
182 #define CARDMAP_ORDER 5
183 #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
184 #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
186 struct cardmap {
187 int shift;
188 unsigned long inuse;
189 struct cardmap *parent;
190 void *ptr[CARDMAP_WIDTH];
192 static void *cardmap_get(struct cardmap *map, unsigned int nr);
193 static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
194 static unsigned int cardmap_find_first_free(struct cardmap *map);
195 static void cardmap_destroy(struct cardmap **map);
198 * all_ppp_sem protects the all_ppp_units mapping.
199 * It also ensures that finding a ppp unit in the all_ppp_units map
200 * and updating its file.refcnt field is atomic.
202 static DECLARE_MUTEX(all_ppp_sem);
203 static struct cardmap *all_ppp_units;
204 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
207 * all_channels_lock protects all_channels and last_channel_index,
208 * and the atomicity of find a channel and updating its file.refcnt
209 * field.
211 static spinlock_t all_channels_lock = SPIN_LOCK_UNLOCKED;
212 static LIST_HEAD(all_channels);
213 static LIST_HEAD(new_channels);
214 static int last_channel_index;
215 static atomic_t channel_count = ATOMIC_INIT(0);
217 /* Get the PPP protocol number from a skb */
218 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
220 /* We limit the length of ppp->file.rq to this (arbitrary) value */
221 #define PPP_MAX_RQLEN 32
224 * Maximum number of multilink fragments queued up.
225 * This has to be large enough to cope with the maximum latency of
226 * the slowest channel relative to the others. Strictly it should
227 * depend on the number of channels and their characteristics.
229 #define PPP_MP_MAX_QLEN 128
231 /* Multilink header bits. */
232 #define B 0x80 /* this fragment begins a packet */
233 #define E 0x40 /* this fragment ends a packet */
235 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
236 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
237 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
239 /* Prototypes. */
240 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
241 unsigned int cmd, unsigned long arg);
242 static void ppp_xmit_process(struct ppp *ppp);
243 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
244 static void ppp_push(struct ppp *ppp);
245 static void ppp_channel_push(struct channel *pch);
246 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
247 struct channel *pch);
248 static void ppp_receive_error(struct ppp *ppp);
249 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
250 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
251 struct sk_buff *skb);
252 #ifdef CONFIG_PPP_MULTILINK
253 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
254 struct channel *pch);
255 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
256 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
257 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
258 #endif /* CONFIG_PPP_MULTILINK */
259 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
260 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
261 static void ppp_ccp_closed(struct ppp *ppp);
262 static struct compressor *find_compressor(int type);
263 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
264 static struct ppp *ppp_create_interface(int unit, int *retp);
265 static void init_ppp_file(struct ppp_file *pf, int kind);
266 static void ppp_shutdown_interface(struct ppp *ppp);
267 static void ppp_destroy_interface(struct ppp *ppp);
268 static struct ppp *ppp_find_unit(int unit);
269 static struct channel *ppp_find_channel(int unit);
270 static int ppp_connect_channel(struct channel *pch, int unit);
271 static int ppp_disconnect_channel(struct channel *pch);
272 static void ppp_destroy_channel(struct channel *pch);
274 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
275 static inline int proto_to_npindex(int proto)
277 switch (proto) {
278 case PPP_IP:
279 return NP_IP;
280 case PPP_IPV6:
281 return NP_IPV6;
282 case PPP_IPX:
283 return NP_IPX;
284 case PPP_AT:
285 return NP_AT;
286 case PPP_MPLS_UC:
287 return NP_MPLS_UC;
288 case PPP_MPLS_MC:
289 return NP_MPLS_MC;
291 return -EINVAL;
294 /* Translates an NP index into a PPP protocol number */
295 static const int npindex_to_proto[NUM_NP] = {
296 PPP_IP,
297 PPP_IPV6,
298 PPP_IPX,
299 PPP_AT,
300 PPP_MPLS_UC,
301 PPP_MPLS_MC,
304 /* Translates an ethertype into an NP index */
305 static inline int ethertype_to_npindex(int ethertype)
307 switch (ethertype) {
308 case ETH_P_IP:
309 return NP_IP;
310 case ETH_P_IPV6:
311 return NP_IPV6;
312 case ETH_P_IPX:
313 return NP_IPX;
314 case ETH_P_PPPTALK:
315 case ETH_P_ATALK:
316 return NP_AT;
317 case ETH_P_MPLS_UC:
318 return NP_MPLS_UC;
319 case ETH_P_MPLS_MC:
320 return NP_MPLS_MC;
322 return -1;
325 /* Translates an NP index into an ethertype */
326 static const int npindex_to_ethertype[NUM_NP] = {
327 ETH_P_IP,
328 ETH_P_IPV6,
329 ETH_P_IPX,
330 ETH_P_PPPTALK,
331 ETH_P_MPLS_UC,
332 ETH_P_MPLS_MC,
336 * Locking shorthand.
338 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
339 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
340 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
341 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
342 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
343 ppp_recv_lock(ppp); } while (0)
344 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
345 ppp_xmit_unlock(ppp); } while (0)
348 * /dev/ppp device routines.
349 * The /dev/ppp device is used by pppd to control the ppp unit.
350 * It supports the read, write, ioctl and poll functions.
351 * Open instances of /dev/ppp can be in one of three states:
352 * unattached, attached to a ppp unit, or attached to a ppp channel.
354 static int ppp_open(struct inode *inode, struct file *file)
357 * This could (should?) be enforced by the permissions on /dev/ppp.
359 if (!capable(CAP_NET_ADMIN))
360 return -EPERM;
361 return 0;
364 static int ppp_release(struct inode *inode, struct file *file)
366 struct ppp_file *pf = file->private_data;
367 struct ppp *ppp;
369 if (pf != 0) {
370 file->private_data = 0;
371 if (pf->kind == INTERFACE) {
372 ppp = PF_TO_PPP(pf);
373 if (file == ppp->owner)
374 ppp_shutdown_interface(ppp);
376 if (atomic_dec_and_test(&pf->refcnt)) {
377 switch (pf->kind) {
378 case INTERFACE:
379 ppp_destroy_interface(PF_TO_PPP(pf));
380 break;
381 case CHANNEL:
382 ppp_destroy_channel(PF_TO_CHANNEL(pf));
383 break;
387 return 0;
390 static ssize_t ppp_read(struct file *file, char __user *buf,
391 size_t count, loff_t *ppos)
393 struct ppp_file *pf = file->private_data;
394 DECLARE_WAITQUEUE(wait, current);
395 ssize_t ret;
396 struct sk_buff *skb = 0;
398 ret = count;
400 if (pf == 0)
401 return -ENXIO;
402 add_wait_queue(&pf->rwait, &wait);
403 for (;;) {
404 set_current_state(TASK_INTERRUPTIBLE);
405 skb = skb_dequeue(&pf->rq);
406 if (skb)
407 break;
408 ret = 0;
409 if (pf->dead)
410 break;
411 ret = -EAGAIN;
412 if (file->f_flags & O_NONBLOCK)
413 break;
414 ret = -ERESTARTSYS;
415 if (signal_pending(current))
416 break;
417 schedule();
419 set_current_state(TASK_RUNNING);
420 remove_wait_queue(&pf->rwait, &wait);
422 if (skb == 0)
423 goto out;
425 ret = -EOVERFLOW;
426 if (skb->len > count)
427 goto outf;
428 ret = -EFAULT;
429 if (copy_to_user(buf, skb->data, skb->len))
430 goto outf;
431 ret = skb->len;
433 outf:
434 kfree_skb(skb);
435 out:
436 return ret;
439 static ssize_t ppp_write(struct file *file, const char __user *buf,
440 size_t count, loff_t *ppos)
442 struct ppp_file *pf = file->private_data;
443 struct sk_buff *skb;
444 ssize_t ret;
446 if (pf == 0)
447 return -ENXIO;
448 ret = -ENOMEM;
449 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
450 if (skb == 0)
451 goto out;
452 skb_reserve(skb, pf->hdrlen);
453 ret = -EFAULT;
454 if (copy_from_user(skb_put(skb, count), buf, count)) {
455 kfree_skb(skb);
456 goto out;
459 skb_queue_tail(&pf->xq, skb);
461 switch (pf->kind) {
462 case INTERFACE:
463 ppp_xmit_process(PF_TO_PPP(pf));
464 break;
465 case CHANNEL:
466 ppp_channel_push(PF_TO_CHANNEL(pf));
467 break;
470 ret = count;
472 out:
473 return ret;
476 /* No kernel lock - fine */
477 static unsigned int ppp_poll(struct file *file, poll_table *wait)
479 struct ppp_file *pf = file->private_data;
480 unsigned int mask;
482 if (pf == 0)
483 return 0;
484 poll_wait(file, &pf->rwait, wait);
485 mask = POLLOUT | POLLWRNORM;
486 if (skb_peek(&pf->rq) != 0)
487 mask |= POLLIN | POLLRDNORM;
488 if (pf->dead)
489 mask |= POLLHUP;
490 return mask;
493 static int ppp_ioctl(struct inode *inode, struct file *file,
494 unsigned int cmd, unsigned long arg)
496 struct ppp_file *pf = file->private_data;
497 struct ppp *ppp;
498 int err = -EFAULT, val, val2, i;
499 struct ppp_idle idle;
500 struct npioctl npi;
501 int unit, cflags;
502 struct slcompress *vj;
504 if (pf == 0)
505 return ppp_unattached_ioctl(pf, file, cmd, arg);
507 if (cmd == PPPIOCDETACH) {
509 * We have to be careful here... if the file descriptor
510 * has been dup'd, we could have another process in the
511 * middle of a poll using the same file *, so we had
512 * better not free the interface data structures -
513 * instead we fail the ioctl. Even in this case, we
514 * shut down the interface if we are the owner of it.
515 * Actually, we should get rid of PPPIOCDETACH, userland
516 * (i.e. pppd) could achieve the same effect by closing
517 * this fd and reopening /dev/ppp.
519 err = -EINVAL;
520 if (pf->kind == INTERFACE) {
521 ppp = PF_TO_PPP(pf);
522 if (file == ppp->owner)
523 ppp_shutdown_interface(ppp);
525 if (atomic_read(&file->f_count) <= 2) {
526 ppp_release(inode, file);
527 err = 0;
528 } else
529 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
530 atomic_read(&file->f_count));
531 return err;
534 if (pf->kind == CHANNEL) {
535 struct channel *pch = PF_TO_CHANNEL(pf);
536 struct ppp_channel *chan;
538 switch (cmd) {
539 case PPPIOCCONNECT:
540 if (get_user(unit, (int *) arg))
541 break;
542 err = ppp_connect_channel(pch, unit);
543 break;
545 case PPPIOCDISCONN:
546 err = ppp_disconnect_channel(pch);
547 break;
549 default:
550 down_read(&pch->chan_sem);
551 chan = pch->chan;
552 err = -ENOTTY;
553 if (chan && chan->ops->ioctl)
554 err = chan->ops->ioctl(chan, cmd, arg);
555 up_read(&pch->chan_sem);
557 return err;
560 if (pf->kind != INTERFACE) {
561 /* can't happen */
562 printk(KERN_ERR "PPP: not interface or channel??\n");
563 return -EINVAL;
566 ppp = PF_TO_PPP(pf);
567 switch (cmd) {
568 case PPPIOCSMRU:
569 if (get_user(val, (int *) arg))
570 break;
571 ppp->mru = val;
572 err = 0;
573 break;
575 case PPPIOCSFLAGS:
576 if (get_user(val, (int *) arg))
577 break;
578 ppp_lock(ppp);
579 cflags = ppp->flags & ~val;
580 ppp->flags = val & SC_FLAG_BITS;
581 ppp_unlock(ppp);
582 if (cflags & SC_CCP_OPEN)
583 ppp_ccp_closed(ppp);
584 err = 0;
585 break;
587 case PPPIOCGFLAGS:
588 val = ppp->flags | ppp->xstate | ppp->rstate;
589 if (put_user(val, (int *) arg))
590 break;
591 err = 0;
592 break;
594 case PPPIOCSCOMPRESS:
595 err = ppp_set_compress(ppp, arg);
596 break;
598 case PPPIOCGUNIT:
599 if (put_user(ppp->file.index, (int *) arg))
600 break;
601 err = 0;
602 break;
604 case PPPIOCSDEBUG:
605 if (get_user(val, (int *) arg))
606 break;
607 ppp->debug = val;
608 err = 0;
609 break;
611 case PPPIOCGDEBUG:
612 if (put_user(ppp->debug, (int *) arg))
613 break;
614 err = 0;
615 break;
617 case PPPIOCGIDLE:
618 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
619 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
620 if (copy_to_user((void __user *) arg, &idle, sizeof(idle)))
621 break;
622 err = 0;
623 break;
625 case PPPIOCSMAXCID:
626 if (get_user(val, (int *) arg))
627 break;
628 val2 = 15;
629 if ((val >> 16) != 0) {
630 val2 = val >> 16;
631 val &= 0xffff;
633 vj = slhc_init(val2+1, val+1);
634 if (vj == 0) {
635 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
636 err = -ENOMEM;
637 break;
639 ppp_lock(ppp);
640 if (ppp->vj != 0)
641 slhc_free(ppp->vj);
642 ppp->vj = vj;
643 ppp_unlock(ppp);
644 err = 0;
645 break;
647 case PPPIOCGNPMODE:
648 case PPPIOCSNPMODE:
649 if (copy_from_user(&npi, (void __user *) arg, sizeof(npi)))
650 break;
651 err = proto_to_npindex(npi.protocol);
652 if (err < 0)
653 break;
654 i = err;
655 if (cmd == PPPIOCGNPMODE) {
656 err = -EFAULT;
657 npi.mode = ppp->npmode[i];
658 if (copy_to_user((void __user *) arg, &npi, sizeof(npi)))
659 break;
660 } else {
661 ppp->npmode[i] = npi.mode;
662 /* we may be able to transmit more packets now (??) */
663 netif_wake_queue(ppp->dev);
665 err = 0;
666 break;
668 #ifdef CONFIG_PPP_FILTER
669 case PPPIOCSPASS:
670 case PPPIOCSACTIVE:
672 struct sock_fprog uprog, *filtp;
673 struct sock_filter *code = NULL;
674 int len;
676 if (copy_from_user(&uprog, (void __user *) arg, sizeof(uprog)))
677 break;
678 err = -ENOMEM;
679 len = uprog.len * sizeof(struct sock_filter);
680 code = kmalloc(len, GFP_KERNEL);
681 if (code == 0)
682 break;
683 err = -EFAULT;
684 if (copy_from_user(code, (void __user *) uprog.filter, len)) {
685 kfree(code);
686 break;
688 err = sk_chk_filter(code, uprog.len);
689 if (err) {
690 kfree(code);
691 break;
693 filtp = (cmd == PPPIOCSPASS)? &ppp->pass_filter: &ppp->active_filter;
694 ppp_lock(ppp);
695 if (filtp->filter)
696 kfree(filtp->filter);
697 filtp->filter = code;
698 filtp->len = uprog.len;
699 ppp_unlock(ppp);
700 err = 0;
701 break;
703 #endif /* CONFIG_PPP_FILTER */
705 #ifdef CONFIG_PPP_MULTILINK
706 case PPPIOCSMRRU:
707 if (get_user(val, (int *) arg))
708 break;
709 ppp_recv_lock(ppp);
710 ppp->mrru = val;
711 ppp_recv_unlock(ppp);
712 err = 0;
713 break;
714 #endif /* CONFIG_PPP_MULTILINK */
716 default:
717 err = -ENOTTY;
720 return err;
723 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
724 unsigned int cmd, unsigned long arg)
726 int unit, err = -EFAULT;
727 struct ppp *ppp;
728 struct channel *chan;
730 switch (cmd) {
731 case PPPIOCNEWUNIT:
732 /* Create a new ppp unit */
733 if (get_user(unit, (int *) arg))
734 break;
735 ppp = ppp_create_interface(unit, &err);
736 if (ppp == 0)
737 break;
738 file->private_data = &ppp->file;
739 ppp->owner = file;
740 err = -EFAULT;
741 if (put_user(ppp->file.index, (int *) arg))
742 break;
743 err = 0;
744 break;
746 case PPPIOCATTACH:
747 /* Attach to an existing ppp unit */
748 if (get_user(unit, (int *) arg))
749 break;
750 down(&all_ppp_sem);
751 err = -ENXIO;
752 ppp = ppp_find_unit(unit);
753 if (ppp != 0) {
754 atomic_inc(&ppp->file.refcnt);
755 file->private_data = &ppp->file;
756 err = 0;
758 up(&all_ppp_sem);
759 break;
761 case PPPIOCATTCHAN:
762 if (get_user(unit, (int *) arg))
763 break;
764 spin_lock_bh(&all_channels_lock);
765 err = -ENXIO;
766 chan = ppp_find_channel(unit);
767 if (chan != 0) {
768 atomic_inc(&chan->file.refcnt);
769 file->private_data = &chan->file;
770 err = 0;
772 spin_unlock_bh(&all_channels_lock);
773 break;
775 default:
776 err = -ENOTTY;
778 return err;
781 static struct file_operations ppp_device_fops = {
782 .owner = THIS_MODULE,
783 .read = ppp_read,
784 .write = ppp_write,
785 .poll = ppp_poll,
786 .ioctl = ppp_ioctl,
787 .open = ppp_open,
788 .release = ppp_release
791 #define PPP_MAJOR 108
793 /* Called at boot time if ppp is compiled into the kernel,
794 or at module load time (from init_module) if compiled as a module. */
795 int __init ppp_init(void)
797 int err;
799 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
800 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
801 if (!err) {
802 err = devfs_mk_cdev(MKDEV(PPP_MAJOR, 0),
803 S_IFCHR|S_IRUSR|S_IWUSR, "ppp");
806 if (err)
807 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
808 return err;
812 * Network interface unit routines.
814 static int
815 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
817 struct ppp *ppp = (struct ppp *) dev->priv;
818 int npi, proto;
819 unsigned char *pp;
821 npi = ethertype_to_npindex(ntohs(skb->protocol));
822 if (npi < 0)
823 goto outf;
825 /* Drop, accept or reject the packet */
826 switch (ppp->npmode[npi]) {
827 case NPMODE_PASS:
828 break;
829 case NPMODE_QUEUE:
830 /* it would be nice to have a way to tell the network
831 system to queue this one up for later. */
832 goto outf;
833 case NPMODE_DROP:
834 case NPMODE_ERROR:
835 goto outf;
838 /* Put the 2-byte PPP protocol number on the front,
839 making sure there is room for the address and control fields. */
840 if (skb_headroom(skb) < PPP_HDRLEN) {
841 struct sk_buff *ns;
843 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
844 if (ns == 0)
845 goto outf;
846 skb_reserve(ns, dev->hard_header_len);
847 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
848 kfree_skb(skb);
849 skb = ns;
851 pp = skb_push(skb, 2);
852 proto = npindex_to_proto[npi];
853 pp[0] = proto >> 8;
854 pp[1] = proto;
856 netif_stop_queue(dev);
857 skb_queue_tail(&ppp->file.xq, skb);
858 ppp_xmit_process(ppp);
859 return 0;
861 outf:
862 kfree_skb(skb);
863 ++ppp->stats.tx_dropped;
864 return 0;
867 static struct net_device_stats *
868 ppp_net_stats(struct net_device *dev)
870 struct ppp *ppp = (struct ppp *) dev->priv;
872 return &ppp->stats;
875 static int
876 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
878 struct ppp *ppp = dev->priv;
879 int err = -EFAULT;
880 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
881 struct ppp_stats stats;
882 struct ppp_comp_stats cstats;
883 char *vers;
885 switch (cmd) {
886 case SIOCGPPPSTATS:
887 ppp_get_stats(ppp, &stats);
888 if (copy_to_user(addr, &stats, sizeof(stats)))
889 break;
890 err = 0;
891 break;
893 case SIOCGPPPCSTATS:
894 memset(&cstats, 0, sizeof(cstats));
895 if (ppp->xc_state != 0)
896 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
897 if (ppp->rc_state != 0)
898 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
899 if (copy_to_user(addr, &cstats, sizeof(cstats)))
900 break;
901 err = 0;
902 break;
904 case SIOCGPPPVER:
905 vers = PPP_VERSION;
906 if (copy_to_user(addr, vers, strlen(vers) + 1))
907 break;
908 err = 0;
909 break;
911 default:
912 err = -EINVAL;
915 return err;
918 static int
919 ppp_net_init(struct net_device *dev)
921 dev->hard_header_len = PPP_HDRLEN;
922 dev->mtu = PPP_MTU;
923 dev->hard_start_xmit = ppp_start_xmit;
924 dev->get_stats = ppp_net_stats;
925 dev->do_ioctl = ppp_net_ioctl;
926 dev->addr_len = 0;
927 dev->tx_queue_len = 3;
928 dev->type = ARPHRD_PPP;
929 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
930 return 0;
934 * Transmit-side routines.
938 * Called to do any work queued up on the transmit side
939 * that can now be done.
941 static void
942 ppp_xmit_process(struct ppp *ppp)
944 struct sk_buff *skb;
946 ppp_xmit_lock(ppp);
947 if (ppp->dev != 0) {
948 ppp_push(ppp);
949 while (ppp->xmit_pending == 0
950 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
951 ppp_send_frame(ppp, skb);
952 /* If there's no work left to do, tell the core net
953 code that we can accept some more. */
954 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
955 netif_wake_queue(ppp->dev);
957 ppp_xmit_unlock(ppp);
961 * Compress and send a frame.
962 * The caller should have locked the xmit path,
963 * and xmit_pending should be 0.
965 static void
966 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
968 int proto = PPP_PROTO(skb);
969 struct sk_buff *new_skb;
970 int len;
971 unsigned char *cp;
973 if (proto < 0x8000) {
974 #ifdef CONFIG_PPP_FILTER
975 /* check if we should pass this packet */
976 /* the filter instructions are constructed assuming
977 a four-byte PPP header on each packet */
978 *skb_push(skb, 2) = 1;
979 if (ppp->pass_filter.filter
980 && sk_run_filter(skb, ppp->pass_filter.filter,
981 ppp->pass_filter.len) == 0) {
982 if (ppp->debug & 1)
983 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
984 kfree_skb(skb);
985 return;
987 /* if this packet passes the active filter, record the time */
988 if (!(ppp->active_filter.filter
989 && sk_run_filter(skb, ppp->active_filter.filter,
990 ppp->active_filter.len) == 0))
991 ppp->last_xmit = jiffies;
992 skb_pull(skb, 2);
993 #else
994 /* for data packets, record the time */
995 ppp->last_xmit = jiffies;
996 #endif /* CONFIG_PPP_FILTER */
999 ++ppp->stats.tx_packets;
1000 ppp->stats.tx_bytes += skb->len - 2;
1002 switch (proto) {
1003 case PPP_IP:
1004 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
1005 break;
1006 /* try to do VJ TCP header compression */
1007 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1008 GFP_ATOMIC);
1009 if (new_skb == 0) {
1010 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1011 goto drop;
1013 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1014 cp = skb->data + 2;
1015 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1016 new_skb->data + 2, &cp,
1017 !(ppp->flags & SC_NO_TCP_CCID));
1018 if (cp == skb->data + 2) {
1019 /* didn't compress */
1020 kfree_skb(new_skb);
1021 } else {
1022 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1023 proto = PPP_VJC_COMP;
1024 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1025 } else {
1026 proto = PPP_VJC_UNCOMP;
1027 cp[0] = skb->data[2];
1029 kfree_skb(skb);
1030 skb = new_skb;
1031 cp = skb_put(skb, len + 2);
1032 cp[0] = 0;
1033 cp[1] = proto;
1035 break;
1037 case PPP_CCP:
1038 /* peek at outbound CCP frames */
1039 ppp_ccp_peek(ppp, skb, 0);
1040 break;
1043 /* try to do packet compression */
1044 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1045 && proto != PPP_LCP && proto != PPP_CCP) {
1046 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
1047 GFP_ATOMIC);
1048 if (new_skb == 0) {
1049 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1050 goto drop;
1052 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1053 skb_reserve(new_skb,
1054 ppp->dev->hard_header_len - PPP_HDRLEN);
1056 /* compressor still expects A/C bytes in hdr */
1057 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1058 new_skb->data, skb->len + 2,
1059 ppp->dev->mtu + PPP_HDRLEN);
1060 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1061 kfree_skb(skb);
1062 skb = new_skb;
1063 skb_put(skb, len);
1064 skb_pull(skb, 2); /* pull off A/C bytes */
1065 } else {
1066 /* didn't compress, or CCP not up yet */
1067 kfree_skb(new_skb);
1072 * If we are waiting for traffic (demand dialling),
1073 * queue it up for pppd to receive.
1075 if (ppp->flags & SC_LOOP_TRAFFIC) {
1076 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1077 goto drop;
1078 skb_queue_tail(&ppp->file.rq, skb);
1079 wake_up_interruptible(&ppp->file.rwait);
1080 return;
1083 ppp->xmit_pending = skb;
1084 ppp_push(ppp);
1085 return;
1087 drop:
1088 kfree_skb(skb);
1089 ++ppp->stats.tx_errors;
1093 * Try to send the frame in xmit_pending.
1094 * The caller should have the xmit path locked.
1096 static void
1097 ppp_push(struct ppp *ppp)
1099 struct list_head *list;
1100 struct channel *pch;
1101 struct sk_buff *skb = ppp->xmit_pending;
1103 if (skb == 0)
1104 return;
1106 list = &ppp->channels;
1107 if (list_empty(list)) {
1108 /* nowhere to send the packet, just drop it */
1109 ppp->xmit_pending = 0;
1110 kfree_skb(skb);
1111 return;
1114 if ((ppp->flags & SC_MULTILINK) == 0) {
1115 /* not doing multilink: send it down the first channel */
1116 list = list->next;
1117 pch = list_entry(list, struct channel, clist);
1119 spin_lock_bh(&pch->downl);
1120 if (pch->chan) {
1121 if (pch->chan->ops->start_xmit(pch->chan, skb))
1122 ppp->xmit_pending = 0;
1123 } else {
1124 /* channel got unregistered */
1125 kfree_skb(skb);
1126 ppp->xmit_pending = 0;
1128 spin_unlock_bh(&pch->downl);
1129 return;
1132 #ifdef CONFIG_PPP_MULTILINK
1133 /* Multilink: fragment the packet over as many links
1134 as can take the packet at the moment. */
1135 if (!ppp_mp_explode(ppp, skb))
1136 return;
1137 #endif /* CONFIG_PPP_MULTILINK */
1139 ppp->xmit_pending = 0;
1140 kfree_skb(skb);
1143 #ifdef CONFIG_PPP_MULTILINK
1145 * Divide a packet to be transmitted into fragments and
1146 * send them out the individual links.
1148 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1150 int nch, len, fragsize;
1151 int i, bits, hdrlen, mtu;
1152 int flen, fnb;
1153 unsigned char *p, *q;
1154 struct list_head *list;
1155 struct channel *pch;
1156 struct sk_buff *frag;
1157 struct ppp_channel *chan;
1159 nch = 0;
1160 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1161 list = &ppp->channels;
1162 while ((list = list->next) != &ppp->channels) {
1163 pch = list_entry(list, struct channel, clist);
1164 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1166 * If a channel hasn't had a fragment yet, it has to get
1167 * one before we send any fragments on later channels.
1168 * If it can't take a fragment now, don't give any
1169 * to subsequent channels.
1171 if (!pch->had_frag && !pch->avail) {
1172 while ((list = list->next) != &ppp->channels) {
1173 pch = list_entry(list, struct channel, clist);
1174 pch->avail = 0;
1176 break;
1179 if (nch == 0)
1180 return 0; /* can't take now, leave it in xmit_pending */
1182 /* Do protocol field compression (XXX this should be optional) */
1183 p = skb->data;
1184 len = skb->len;
1185 if (*p == 0) {
1186 ++p;
1187 --len;
1190 /* decide on fragment size */
1191 fragsize = len;
1192 if (nch > 1) {
1193 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1194 if (nch > maxch)
1195 nch = maxch;
1196 fragsize = ROUNDUP(fragsize, nch);
1199 /* skip to the channel after the one we last used
1200 and start at that one */
1201 for (i = 0; i < ppp->nxchan; ++i) {
1202 list = list->next;
1203 if (list == &ppp->channels) {
1204 i = 0;
1205 break;
1209 /* create a fragment for each channel */
1210 bits = B;
1211 do {
1212 list = list->next;
1213 if (list == &ppp->channels) {
1214 i = 0;
1215 continue;
1217 pch = list_entry(list, struct channel, clist);
1218 ++i;
1219 if (!pch->avail)
1220 continue;
1222 /* check the channel's mtu and whether it is still attached. */
1223 spin_lock_bh(&pch->downl);
1224 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1225 /* can't use this channel */
1226 spin_unlock_bh(&pch->downl);
1227 pch->avail = 0;
1228 if (--nch == 0)
1229 break;
1230 continue;
1234 * We have to create multiple fragments for this channel
1235 * if fragsize is greater than the channel's mtu.
1237 if (fragsize > len)
1238 fragsize = len;
1239 for (flen = fragsize; flen > 0; flen -= fnb) {
1240 fnb = flen;
1241 if (fnb > mtu + 2 - hdrlen)
1242 fnb = mtu + 2 - hdrlen;
1243 if (fnb >= len)
1244 bits |= E;
1245 frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1246 if (frag == 0)
1247 goto noskb;
1248 q = skb_put(frag, fnb + hdrlen);
1249 /* make the MP header */
1250 q[0] = PPP_MP >> 8;
1251 q[1] = PPP_MP;
1252 if (ppp->flags & SC_MP_XSHORTSEQ) {
1253 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1254 q[3] = ppp->nxseq;
1255 } else {
1256 q[2] = bits;
1257 q[3] = ppp->nxseq >> 16;
1258 q[4] = ppp->nxseq >> 8;
1259 q[5] = ppp->nxseq;
1262 /* copy the data in */
1263 memcpy(q + hdrlen, p, fnb);
1265 /* try to send it down the channel */
1266 chan = pch->chan;
1267 if (!chan->ops->start_xmit(chan, frag))
1268 skb_queue_tail(&pch->file.xq, frag);
1269 pch->had_frag = 1;
1270 p += fnb;
1271 len -= fnb;
1272 ++ppp->nxseq;
1273 bits = 0;
1275 spin_unlock_bh(&pch->downl);
1276 } while (len > 0);
1277 ppp->nxchan = i;
1279 return 1;
1281 noskb:
1282 spin_unlock_bh(&pch->downl);
1283 if (ppp->debug & 1)
1284 printk(KERN_ERR "PPP: no memory (fragment)\n");
1285 ++ppp->stats.tx_errors;
1286 ++ppp->nxseq;
1287 return 1; /* abandon the frame */
1289 #endif /* CONFIG_PPP_MULTILINK */
1292 * Try to send data out on a channel.
1294 static void
1295 ppp_channel_push(struct channel *pch)
1297 struct sk_buff *skb;
1298 struct ppp *ppp;
1300 spin_lock_bh(&pch->downl);
1301 if (pch->chan != 0) {
1302 while (skb_queue_len(&pch->file.xq) > 0) {
1303 skb = skb_dequeue(&pch->file.xq);
1304 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1305 /* put the packet back and try again later */
1306 skb_queue_head(&pch->file.xq, skb);
1307 break;
1310 } else {
1311 /* channel got deregistered */
1312 skb_queue_purge(&pch->file.xq);
1314 spin_unlock_bh(&pch->downl);
1315 /* see if there is anything from the attached unit to be sent */
1316 if (skb_queue_len(&pch->file.xq) == 0) {
1317 read_lock_bh(&pch->upl);
1318 ppp = pch->ppp;
1319 if (ppp != 0)
1320 ppp_xmit_process(ppp);
1321 read_unlock_bh(&pch->upl);
1326 * Receive-side routines.
1329 /* misuse a few fields of the skb for MP reconstruction */
1330 #define sequence priority
1331 #define BEbits cb[0]
1333 static inline void
1334 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1336 ppp_recv_lock(ppp);
1337 /* ppp->dev == 0 means interface is closing down */
1338 if (ppp->dev != 0)
1339 ppp_receive_frame(ppp, skb, pch);
1340 else
1341 kfree_skb(skb);
1342 ppp_recv_unlock(ppp);
1345 void
1346 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1348 struct channel *pch = chan->ppp;
1349 int proto;
1351 if (pch == 0 || skb->len == 0) {
1352 kfree_skb(skb);
1353 return;
1356 proto = PPP_PROTO(skb);
1357 read_lock_bh(&pch->upl);
1358 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1359 /* put it on the channel queue */
1360 skb_queue_tail(&pch->file.rq, skb);
1361 /* drop old frames if queue too long */
1362 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1363 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1364 kfree_skb(skb);
1365 wake_up_interruptible(&pch->file.rwait);
1366 } else {
1367 ppp_do_recv(pch->ppp, skb, pch);
1369 read_unlock_bh(&pch->upl);
1372 /* Put a 0-length skb in the receive queue as an error indication */
1373 void
1374 ppp_input_error(struct ppp_channel *chan, int code)
1376 struct channel *pch = chan->ppp;
1377 struct sk_buff *skb;
1379 if (pch == 0)
1380 return;
1382 read_lock_bh(&pch->upl);
1383 if (pch->ppp != 0) {
1384 skb = alloc_skb(0, GFP_ATOMIC);
1385 if (skb != 0) {
1386 skb->len = 0; /* probably unnecessary */
1387 skb->cb[0] = code;
1388 ppp_do_recv(pch->ppp, skb, pch);
1391 read_unlock_bh(&pch->upl);
1395 * We come in here to process a received frame.
1396 * The receive side of the ppp unit is locked.
1398 static void
1399 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1401 if (skb->len >= 2) {
1402 #ifdef CONFIG_PPP_MULTILINK
1403 /* XXX do channel-level decompression here */
1404 if (PPP_PROTO(skb) == PPP_MP)
1405 ppp_receive_mp_frame(ppp, skb, pch);
1406 else
1407 #endif /* CONFIG_PPP_MULTILINK */
1408 ppp_receive_nonmp_frame(ppp, skb);
1409 return;
1412 if (skb->len > 0)
1413 /* note: a 0-length skb is used as an error indication */
1414 ++ppp->stats.rx_length_errors;
1416 kfree_skb(skb);
1417 ppp_receive_error(ppp);
1420 static void
1421 ppp_receive_error(struct ppp *ppp)
1423 ++ppp->stats.rx_errors;
1424 if (ppp->vj != 0)
1425 slhc_toss(ppp->vj);
1428 static void
1429 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1431 struct sk_buff *ns;
1432 int proto, len, npi;
1435 * Decompress the frame, if compressed.
1436 * Note that some decompressors need to see uncompressed frames
1437 * that come in as well as compressed frames.
1439 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1440 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1441 skb = ppp_decompress_frame(ppp, skb);
1443 proto = PPP_PROTO(skb);
1444 switch (proto) {
1445 case PPP_VJC_COMP:
1446 /* decompress VJ compressed packets */
1447 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1448 goto err;
1450 if (skb_tailroom(skb) < 124) {
1451 /* copy to a new sk_buff with more tailroom */
1452 ns = dev_alloc_skb(skb->len + 128);
1453 if (ns == 0) {
1454 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1455 goto err;
1457 skb_reserve(ns, 2);
1458 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
1459 kfree_skb(skb);
1460 skb = ns;
1462 else if (!pskb_may_pull(skb, skb->len))
1463 goto err;
1465 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1466 if (len <= 0) {
1467 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1468 goto err;
1470 len += 2;
1471 if (len > skb->len)
1472 skb_put(skb, len - skb->len);
1473 else if (len < skb->len)
1474 skb_trim(skb, len);
1475 proto = PPP_IP;
1476 break;
1478 case PPP_VJC_UNCOMP:
1479 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1480 goto err;
1482 /* Until we fix the decompressor need to make sure
1483 * data portion is linear.
1485 if (!pskb_may_pull(skb, skb->len))
1486 goto err;
1488 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1489 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1490 goto err;
1492 proto = PPP_IP;
1493 break;
1495 case PPP_CCP:
1496 ppp_ccp_peek(ppp, skb, 1);
1497 break;
1500 ++ppp->stats.rx_packets;
1501 ppp->stats.rx_bytes += skb->len - 2;
1503 npi = proto_to_npindex(proto);
1504 if (npi < 0) {
1505 /* control or unknown frame - pass it to pppd */
1506 skb_queue_tail(&ppp->file.rq, skb);
1507 /* limit queue length by dropping old frames */
1508 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1509 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1510 kfree_skb(skb);
1511 /* wake up any process polling or blocking on read */
1512 wake_up_interruptible(&ppp->file.rwait);
1514 } else {
1515 /* network protocol frame - give it to the kernel */
1517 #ifdef CONFIG_PPP_FILTER
1518 /* check if the packet passes the pass and active filters */
1519 /* the filter instructions are constructed assuming
1520 a four-byte PPP header on each packet */
1521 *skb_push(skb, 2) = 0;
1522 if (ppp->pass_filter.filter
1523 && sk_run_filter(skb, ppp->pass_filter.filter,
1524 ppp->pass_filter.len) == 0) {
1525 if (ppp->debug & 1)
1526 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1527 kfree_skb(skb);
1528 return;
1530 if (!(ppp->active_filter.filter
1531 && sk_run_filter(skb, ppp->active_filter.filter,
1532 ppp->active_filter.len) == 0))
1533 ppp->last_recv = jiffies;
1534 skb_pull(skb, 2);
1535 #else
1536 ppp->last_recv = jiffies;
1537 #endif /* CONFIG_PPP_FILTER */
1539 if ((ppp->dev->flags & IFF_UP) == 0
1540 || ppp->npmode[npi] != NPMODE_PASS) {
1541 kfree_skb(skb);
1542 } else {
1543 skb_pull(skb, 2); /* chop off protocol */
1544 skb->dev = ppp->dev;
1545 skb->protocol = htons(npindex_to_ethertype[npi]);
1546 skb->mac.raw = skb->data;
1547 netif_rx(skb);
1548 ppp->dev->last_rx = jiffies;
1551 return;
1553 err:
1554 kfree_skb(skb);
1555 ppp_receive_error(ppp);
1558 static struct sk_buff *
1559 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1561 int proto = PPP_PROTO(skb);
1562 struct sk_buff *ns;
1563 int len;
1565 /* Until we fix all the decompressor's need to make sure
1566 * data portion is linear.
1568 if (!pskb_may_pull(skb, skb->len))
1569 goto err;
1571 if (proto == PPP_COMP) {
1572 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1573 if (ns == 0) {
1574 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1575 goto err;
1577 /* the decompressor still expects the A/C bytes in the hdr */
1578 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1579 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1580 if (len < 0) {
1581 /* Pass the compressed frame to pppd as an
1582 error indication. */
1583 if (len == DECOMP_FATALERROR)
1584 ppp->rstate |= SC_DC_FERROR;
1585 kfree_skb(ns);
1586 goto err;
1589 kfree_skb(skb);
1590 skb = ns;
1591 skb_put(skb, len);
1592 skb_pull(skb, 2); /* pull off the A/C bytes */
1594 } else {
1595 /* Uncompressed frame - pass to decompressor so it
1596 can update its dictionary if necessary. */
1597 if (ppp->rcomp->incomp)
1598 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1599 skb->len + 2);
1602 return skb;
1604 err:
1605 ppp->rstate |= SC_DC_ERROR;
1606 ppp_receive_error(ppp);
1607 return skb;
1610 #ifdef CONFIG_PPP_MULTILINK
1612 * Receive a multilink frame.
1613 * We put it on the reconstruction queue and then pull off
1614 * as many completed frames as we can.
1616 static void
1617 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1619 u32 mask, seq;
1620 struct list_head *l;
1621 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1623 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
1624 goto err; /* no good, throw it away */
1626 /* Decode sequence number and begin/end bits */
1627 if (ppp->flags & SC_MP_SHORTSEQ) {
1628 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1629 mask = 0xfff;
1630 } else {
1631 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1632 mask = 0xffffff;
1634 skb->BEbits = skb->data[2];
1635 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1638 * Do protocol ID decompression on the first fragment of each packet.
1640 if ((skb->BEbits & B) && (skb->data[0] & 1))
1641 *skb_push(skb, 1) = 0;
1644 * Expand sequence number to 32 bits, making it as close
1645 * as possible to ppp->minseq.
1647 seq |= ppp->minseq & ~mask;
1648 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1649 seq += mask + 1;
1650 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1651 seq -= mask + 1; /* should never happen */
1652 skb->sequence = seq;
1653 pch->lastseq = seq;
1656 * If this packet comes before the next one we were expecting,
1657 * drop it.
1659 if (seq_before(seq, ppp->nextseq)) {
1660 kfree_skb(skb);
1661 ++ppp->stats.rx_dropped;
1662 ppp_receive_error(ppp);
1663 return;
1667 * Reevaluate minseq, the minimum over all channels of the
1668 * last sequence number received on each channel. Because of
1669 * the increasing sequence number rule, we know that any fragment
1670 * before `minseq' which hasn't arrived is never going to arrive.
1671 * The list of channels can't change because we have the receive
1672 * side of the ppp unit locked.
1674 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1675 struct channel *ch = list_entry(l, struct channel, clist);
1676 if (seq_before(ch->lastseq, seq))
1677 seq = ch->lastseq;
1679 if (seq_before(ppp->minseq, seq))
1680 ppp->minseq = seq;
1682 /* Put the fragment on the reconstruction queue */
1683 ppp_mp_insert(ppp, skb);
1685 /* If the queue is getting long, don't wait any longer for packets
1686 before the start of the queue. */
1687 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1688 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1689 ppp->minseq = ppp->mrq.next->sequence;
1691 /* Pull completed packets off the queue and receive them. */
1692 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1693 ppp_receive_nonmp_frame(ppp, skb);
1695 return;
1697 err:
1698 kfree_skb(skb);
1699 ppp_receive_error(ppp);
1703 * Insert a fragment on the MP reconstruction queue.
1704 * The queue is ordered by increasing sequence number.
1706 static void
1707 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1709 struct sk_buff *p;
1710 struct sk_buff_head *list = &ppp->mrq;
1711 u32 seq = skb->sequence;
1713 /* N.B. we don't need to lock the list lock because we have the
1714 ppp unit receive-side lock. */
1715 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1716 if (seq_before(seq, p->sequence))
1717 break;
1718 __skb_insert(skb, p->prev, p, list);
1722 * Reconstruct a packet from the MP fragment queue.
1723 * We go through increasing sequence numbers until we find a
1724 * complete packet, or we get to the sequence number for a fragment
1725 * which hasn't arrived but might still do so.
1727 struct sk_buff *
1728 ppp_mp_reconstruct(struct ppp *ppp)
1730 u32 seq = ppp->nextseq;
1731 u32 minseq = ppp->minseq;
1732 struct sk_buff_head *list = &ppp->mrq;
1733 struct sk_buff *p, *next;
1734 struct sk_buff *head, *tail;
1735 struct sk_buff *skb = NULL;
1736 int lost = 0, len = 0;
1738 if (ppp->mrru == 0) /* do nothing until mrru is set */
1739 return NULL;
1740 head = list->next;
1741 tail = NULL;
1742 for (p = head; p != (struct sk_buff *) list; p = next) {
1743 next = p->next;
1744 if (seq_before(p->sequence, seq)) {
1745 /* this can't happen, anyway ignore the skb */
1746 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1747 p->sequence, seq);
1748 head = next;
1749 continue;
1751 if (p->sequence != seq) {
1752 /* Fragment `seq' is missing. If it is after
1753 minseq, it might arrive later, so stop here. */
1754 if (seq_after(seq, minseq))
1755 break;
1756 /* Fragment `seq' is lost, keep going. */
1757 lost = 1;
1758 seq = seq_before(minseq, p->sequence)?
1759 minseq + 1: p->sequence;
1760 next = p;
1761 continue;
1765 * At this point we know that all the fragments from
1766 * ppp->nextseq to seq are either present or lost.
1767 * Also, there are no complete packets in the queue
1768 * that have no missing fragments and end before this
1769 * fragment.
1772 /* B bit set indicates this fragment starts a packet */
1773 if (p->BEbits & B) {
1774 head = p;
1775 lost = 0;
1776 len = 0;
1779 len += p->len;
1781 /* Got a complete packet yet? */
1782 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1783 if (len > ppp->mrru + 2) {
1784 ++ppp->stats.rx_length_errors;
1785 printk(KERN_DEBUG "PPP: reconstructed packet"
1786 " is too long (%d)\n", len);
1787 } else if (p == head) {
1788 /* fragment is complete packet - reuse skb */
1789 tail = p;
1790 skb = skb_get(p);
1791 break;
1792 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1793 ++ppp->stats.rx_missed_errors;
1794 printk(KERN_DEBUG "PPP: no memory for "
1795 "reconstructed packet");
1796 } else {
1797 tail = p;
1798 break;
1800 ppp->nextseq = seq + 1;
1804 * If this is the ending fragment of a packet,
1805 * and we haven't found a complete valid packet yet,
1806 * we can discard up to and including this fragment.
1808 if (p->BEbits & E)
1809 head = next;
1811 ++seq;
1814 /* If we have a complete packet, copy it all into one skb. */
1815 if (tail != NULL) {
1816 /* If we have discarded any fragments,
1817 signal a receive error. */
1818 if (head->sequence != ppp->nextseq) {
1819 if (ppp->debug & 1)
1820 printk(KERN_DEBUG " missed pkts %u..%u\n",
1821 ppp->nextseq, head->sequence-1);
1822 ++ppp->stats.rx_dropped;
1823 ppp_receive_error(ppp);
1826 if (head != tail)
1827 /* copy to a single skb */
1828 for (p = head; p != tail->next; p = p->next)
1829 skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
1830 ppp->nextseq = tail->sequence + 1;
1831 head = tail->next;
1834 /* Discard all the skbuffs that we have copied the data out of
1835 or that we can't use. */
1836 while ((p = list->next) != head) {
1837 __skb_unlink(p, list);
1838 kfree_skb(p);
1841 return skb;
1843 #endif /* CONFIG_PPP_MULTILINK */
1846 * Channel interface.
1850 * Create a new, unattached ppp channel.
1853 ppp_register_channel(struct ppp_channel *chan)
1855 struct channel *pch;
1857 pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
1858 if (pch == 0)
1859 return -ENOMEM;
1860 memset(pch, 0, sizeof(struct channel));
1861 pch->ppp = NULL;
1862 pch->chan = chan;
1863 chan->ppp = pch;
1864 init_ppp_file(&pch->file, CHANNEL);
1865 pch->file.hdrlen = chan->hdrlen;
1866 #ifdef CONFIG_PPP_MULTILINK
1867 pch->lastseq = -1;
1868 #endif /* CONFIG_PPP_MULTILINK */
1869 init_rwsem(&pch->chan_sem);
1870 spin_lock_init(&pch->downl);
1871 pch->upl = RW_LOCK_UNLOCKED;
1872 spin_lock_bh(&all_channels_lock);
1873 pch->file.index = ++last_channel_index;
1874 list_add(&pch->list, &new_channels);
1875 atomic_inc(&channel_count);
1876 spin_unlock_bh(&all_channels_lock);
1877 return 0;
1881 * Return the index of a channel.
1883 int ppp_channel_index(struct ppp_channel *chan)
1885 struct channel *pch = chan->ppp;
1887 if (pch != 0)
1888 return pch->file.index;
1889 return -1;
1893 * Return the PPP unit number to which a channel is connected.
1895 int ppp_unit_number(struct ppp_channel *chan)
1897 struct channel *pch = chan->ppp;
1898 int unit = -1;
1900 if (pch != 0) {
1901 read_lock_bh(&pch->upl);
1902 if (pch->ppp != 0)
1903 unit = pch->ppp->file.index;
1904 read_unlock_bh(&pch->upl);
1906 return unit;
1910 * Disconnect a channel from the generic layer.
1911 * This must be called in process context.
1913 void
1914 ppp_unregister_channel(struct ppp_channel *chan)
1916 struct channel *pch = chan->ppp;
1918 if (pch == 0)
1919 return; /* should never happen */
1920 chan->ppp = 0;
1923 * This ensures that we have returned from any calls into the
1924 * the channel's start_xmit or ioctl routine before we proceed.
1926 down_write(&pch->chan_sem);
1927 spin_lock_bh(&pch->downl);
1928 pch->chan = 0;
1929 spin_unlock_bh(&pch->downl);
1930 up_write(&pch->chan_sem);
1931 ppp_disconnect_channel(pch);
1932 spin_lock_bh(&all_channels_lock);
1933 list_del(&pch->list);
1934 spin_unlock_bh(&all_channels_lock);
1935 pch->file.dead = 1;
1936 wake_up_interruptible(&pch->file.rwait);
1937 if (atomic_dec_and_test(&pch->file.refcnt))
1938 ppp_destroy_channel(pch);
1942 * Callback from a channel when it can accept more to transmit.
1943 * This should be called at BH/softirq level, not interrupt level.
1945 void
1946 ppp_output_wakeup(struct ppp_channel *chan)
1948 struct channel *pch = chan->ppp;
1950 if (pch == 0)
1951 return;
1952 ppp_channel_push(pch);
1956 * Compression control.
1959 /* Process the PPPIOCSCOMPRESS ioctl. */
1960 static int
1961 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1963 int err;
1964 struct compressor *cp, *ocomp;
1965 struct ppp_option_data data;
1966 void *state, *ostate;
1967 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1969 err = -EFAULT;
1970 if (copy_from_user(&data, (void __user *) arg, sizeof(data))
1971 || (data.length <= CCP_MAX_OPTION_LENGTH
1972 && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
1973 goto out;
1974 err = -EINVAL;
1975 if (data.length > CCP_MAX_OPTION_LENGTH
1976 || ccp_option[1] < 2 || ccp_option[1] > data.length)
1977 goto out;
1979 cp = find_compressor(ccp_option[0]);
1980 #ifdef CONFIG_KMOD
1981 if (cp == 0) {
1982 request_module("ppp-compress-%d", ccp_option[0]);
1983 cp = find_compressor(ccp_option[0]);
1985 #endif /* CONFIG_KMOD */
1986 if (cp == 0)
1987 goto out;
1989 err = -ENOBUFS;
1990 if (data.transmit) {
1991 state = cp->comp_alloc(ccp_option, data.length);
1992 if (state != 0) {
1993 ppp_xmit_lock(ppp);
1994 ppp->xstate &= ~SC_COMP_RUN;
1995 ocomp = ppp->xcomp;
1996 ostate = ppp->xc_state;
1997 ppp->xcomp = cp;
1998 ppp->xc_state = state;
1999 ppp_xmit_unlock(ppp);
2000 if (ostate != 0) {
2001 ocomp->comp_free(ostate);
2002 module_put(ocomp->owner);
2004 err = 0;
2005 } else
2006 module_put(cp->owner);
2008 } else {
2009 state = cp->decomp_alloc(ccp_option, data.length);
2010 if (state != 0) {
2011 ppp_recv_lock(ppp);
2012 ppp->rstate &= ~SC_DECOMP_RUN;
2013 ocomp = ppp->rcomp;
2014 ostate = ppp->rc_state;
2015 ppp->rcomp = cp;
2016 ppp->rc_state = state;
2017 ppp_recv_unlock(ppp);
2018 if (ostate != 0) {
2019 ocomp->decomp_free(ostate);
2020 module_put(ocomp->owner);
2022 err = 0;
2023 } else
2024 module_put(cp->owner);
2027 out:
2028 return err;
2032 * Look at a CCP packet and update our state accordingly.
2033 * We assume the caller has the xmit or recv path locked.
2035 static void
2036 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2038 unsigned char *dp;
2039 int len;
2041 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2042 return; /* no header */
2043 dp = skb->data + 2;
2045 switch (CCP_CODE(dp)) {
2046 case CCP_CONFREQ:
2048 /* A ConfReq starts negotiation of compression
2049 * in one direction of transmission,
2050 * and hence brings it down...but which way?
2052 * Remember:
2053 * A ConfReq indicates what the sender would like to receive
2055 if(inbound)
2056 /* He is proposing what I should send */
2057 ppp->xstate &= ~SC_COMP_RUN;
2058 else
2059 /* I am proposing to what he should send */
2060 ppp->rstate &= ~SC_DECOMP_RUN;
2062 break;
2064 case CCP_TERMREQ:
2065 case CCP_TERMACK:
2067 * CCP is going down, both directions of transmission
2069 ppp->rstate &= ~SC_DECOMP_RUN;
2070 ppp->xstate &= ~SC_COMP_RUN;
2071 break;
2073 case CCP_CONFACK:
2074 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2075 break;
2076 if (!pskb_may_pull(skb, len = CCP_LENGTH(dp)) + 2)
2077 return; /* too short */
2078 dp += CCP_HDRLEN;
2079 len -= CCP_HDRLEN;
2080 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2081 break;
2082 if (inbound) {
2083 /* we will start receiving compressed packets */
2084 if (ppp->rc_state == 0)
2085 break;
2086 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2087 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2088 ppp->rstate |= SC_DECOMP_RUN;
2089 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2091 } else {
2092 /* we will soon start sending compressed packets */
2093 if (ppp->xc_state == 0)
2094 break;
2095 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2096 ppp->file.index, 0, ppp->debug))
2097 ppp->xstate |= SC_COMP_RUN;
2099 break;
2101 case CCP_RESETACK:
2102 /* reset the [de]compressor */
2103 if ((ppp->flags & SC_CCP_UP) == 0)
2104 break;
2105 if (inbound) {
2106 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2107 ppp->rcomp->decomp_reset(ppp->rc_state);
2108 ppp->rstate &= ~SC_DC_ERROR;
2110 } else {
2111 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2112 ppp->xcomp->comp_reset(ppp->xc_state);
2114 break;
2118 /* Free up compression resources. */
2119 static void
2120 ppp_ccp_closed(struct ppp *ppp)
2122 void *xstate, *rstate;
2123 struct compressor *xcomp, *rcomp;
2125 ppp_lock(ppp);
2126 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2127 ppp->xstate = 0;
2128 xcomp = ppp->xcomp;
2129 xstate = ppp->xc_state;
2130 ppp->xc_state = 0;
2131 ppp->rstate = 0;
2132 rcomp = ppp->rcomp;
2133 rstate = ppp->rc_state;
2134 ppp->rc_state = 0;
2135 ppp_unlock(ppp);
2137 if (xstate) {
2138 xcomp->comp_free(xstate);
2139 module_put(xcomp->owner);
2141 if (rstate) {
2142 rcomp->decomp_free(rstate);
2143 module_put(rcomp->owner);
2147 /* List of compressors. */
2148 static LIST_HEAD(compressor_list);
2149 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2151 struct compressor_entry {
2152 struct list_head list;
2153 struct compressor *comp;
2156 static struct compressor_entry *
2157 find_comp_entry(int proto)
2159 struct compressor_entry *ce;
2160 struct list_head *list = &compressor_list;
2162 while ((list = list->next) != &compressor_list) {
2163 ce = list_entry(list, struct compressor_entry, list);
2164 if (ce->comp->compress_proto == proto)
2165 return ce;
2167 return 0;
2170 /* Register a compressor */
2172 ppp_register_compressor(struct compressor *cp)
2174 struct compressor_entry *ce;
2175 int ret;
2176 spin_lock(&compressor_list_lock);
2177 ret = -EEXIST;
2178 if (find_comp_entry(cp->compress_proto) != 0)
2179 goto out;
2180 ret = -ENOMEM;
2181 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2182 if (ce == 0)
2183 goto out;
2184 ret = 0;
2185 ce->comp = cp;
2186 list_add(&ce->list, &compressor_list);
2187 out:
2188 spin_unlock(&compressor_list_lock);
2189 return ret;
2192 /* Unregister a compressor */
2193 void
2194 ppp_unregister_compressor(struct compressor *cp)
2196 struct compressor_entry *ce;
2198 spin_lock(&compressor_list_lock);
2199 ce = find_comp_entry(cp->compress_proto);
2200 if (ce != 0 && ce->comp == cp) {
2201 list_del(&ce->list);
2202 kfree(ce);
2204 spin_unlock(&compressor_list_lock);
2207 /* Find a compressor. */
2208 static struct compressor *
2209 find_compressor(int type)
2211 struct compressor_entry *ce;
2212 struct compressor *cp = 0;
2214 spin_lock(&compressor_list_lock);
2215 ce = find_comp_entry(type);
2216 if (ce != 0) {
2217 cp = ce->comp;
2218 if (!try_module_get(cp->owner))
2219 cp = NULL;
2221 spin_unlock(&compressor_list_lock);
2222 return cp;
2226 * Miscelleneous stuff.
2229 static void
2230 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2232 struct slcompress *vj = ppp->vj;
2234 memset(st, 0, sizeof(*st));
2235 st->p.ppp_ipackets = ppp->stats.rx_packets;
2236 st->p.ppp_ierrors = ppp->stats.rx_errors;
2237 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2238 st->p.ppp_opackets = ppp->stats.tx_packets;
2239 st->p.ppp_oerrors = ppp->stats.tx_errors;
2240 st->p.ppp_obytes = ppp->stats.tx_bytes;
2241 if (vj == 0)
2242 return;
2243 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2244 st->vj.vjs_compressed = vj->sls_o_compressed;
2245 st->vj.vjs_searches = vj->sls_o_searches;
2246 st->vj.vjs_misses = vj->sls_o_misses;
2247 st->vj.vjs_errorin = vj->sls_i_error;
2248 st->vj.vjs_tossed = vj->sls_i_tossed;
2249 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2250 st->vj.vjs_compressedin = vj->sls_i_compressed;
2254 * Stuff for handling the lists of ppp units and channels
2255 * and for initialization.
2258 static void ppp_device_destructor(struct net_device *dev)
2260 kfree(dev);
2264 * Create a new ppp interface unit. Fails if it can't allocate memory
2265 * or if there is already a unit with the requested number.
2266 * unit == -1 means allocate a new number.
2268 static struct ppp *
2269 ppp_create_interface(int unit, int *retp)
2271 struct ppp *ppp;
2272 struct net_device *dev = NULL;
2273 int ret = -ENOMEM;
2274 int i;
2276 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2277 if (ppp == 0)
2278 goto err;
2279 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
2280 if (dev == 0)
2281 goto err;
2282 memset(ppp, 0, sizeof(struct ppp));
2283 memset(dev, 0, sizeof(struct net_device));
2285 ret = -EEXIST;
2286 down(&all_ppp_sem);
2287 if (unit < 0)
2288 unit = cardmap_find_first_free(all_ppp_units);
2289 else if (cardmap_get(all_ppp_units, unit) != NULL)
2290 goto err_unlock; /* unit already exists */
2292 /* Initialize the new ppp unit */
2293 ppp->file.index = unit;
2294 ppp->mru = PPP_MRU;
2295 init_ppp_file(&ppp->file, INTERFACE);
2296 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2297 for (i = 0; i < NUM_NP; ++i)
2298 ppp->npmode[i] = NPMODE_PASS;
2299 INIT_LIST_HEAD(&ppp->channels);
2300 spin_lock_init(&ppp->rlock);
2301 spin_lock_init(&ppp->wlock);
2302 #ifdef CONFIG_PPP_MULTILINK
2303 ppp->minseq = -1;
2304 skb_queue_head_init(&ppp->mrq);
2305 #endif /* CONFIG_PPP_MULTILINK */
2307 ppp->dev = dev;
2308 dev->init = ppp_net_init;
2309 sprintf(dev->name, "ppp%d", unit);
2310 dev->priv = ppp;
2311 dev->destructor = ppp_device_destructor;
2313 rtnl_lock();
2314 ret = register_netdevice(dev);
2315 rtnl_unlock();
2316 if (ret != 0) {
2317 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2318 dev->name, ret);
2319 goto err_unlock;
2322 atomic_inc(&ppp_unit_count);
2323 cardmap_set(&all_ppp_units, unit, ppp);
2324 up(&all_ppp_sem);
2325 *retp = 0;
2326 return ppp;
2328 err_unlock:
2329 up(&all_ppp_sem);
2330 err:
2331 *retp = ret;
2332 if (ppp)
2333 kfree(ppp);
2334 if (dev)
2335 kfree(dev);
2336 return NULL;
2340 * Initialize a ppp_file structure.
2342 static void
2343 init_ppp_file(struct ppp_file *pf, int kind)
2345 pf->kind = kind;
2346 skb_queue_head_init(&pf->xq);
2347 skb_queue_head_init(&pf->rq);
2348 atomic_set(&pf->refcnt, 1);
2349 init_waitqueue_head(&pf->rwait);
2353 * Take down a ppp interface unit - called when the owning file
2354 * (the one that created the unit) is closed or detached.
2356 static void ppp_shutdown_interface(struct ppp *ppp)
2358 struct net_device *dev;
2360 down(&all_ppp_sem);
2361 ppp_lock(ppp);
2362 dev = ppp->dev;
2363 ppp->dev = 0;
2364 ppp_unlock(ppp);
2365 /* This will call dev_close() for us. */
2366 if (dev)
2367 unregister_netdev(dev);
2368 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2369 ppp->file.dead = 1;
2370 ppp->owner = NULL;
2371 wake_up_interruptible(&ppp->file.rwait);
2372 up(&all_ppp_sem);
2376 * Free the memory used by a ppp unit. This is only called once
2377 * there are no channels connected to the unit and no file structs
2378 * that reference the unit.
2380 static void ppp_destroy_interface(struct ppp *ppp)
2382 atomic_dec(&ppp_unit_count);
2384 if (!ppp->file.dead || ppp->n_channels) {
2385 /* "can't happen" */
2386 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2387 "n_channels=%d !\n", ppp, ppp->file.dead,
2388 ppp->n_channels);
2389 return;
2392 ppp_ccp_closed(ppp);
2393 if (ppp->vj) {
2394 slhc_free(ppp->vj);
2395 ppp->vj = 0;
2397 skb_queue_purge(&ppp->file.xq);
2398 skb_queue_purge(&ppp->file.rq);
2399 #ifdef CONFIG_PPP_MULTILINK
2400 skb_queue_purge(&ppp->mrq);
2401 #endif /* CONFIG_PPP_MULTILINK */
2402 #ifdef CONFIG_PPP_FILTER
2403 if (ppp->pass_filter.filter) {
2404 kfree(ppp->pass_filter.filter);
2405 ppp->pass_filter.filter = NULL;
2407 if (ppp->active_filter.filter) {
2408 kfree(ppp->active_filter.filter);
2409 ppp->active_filter.filter = 0;
2411 #endif /* CONFIG_PPP_FILTER */
2413 kfree(ppp);
2417 * Locate an existing ppp unit.
2418 * The caller should have locked the all_ppp_sem.
2420 static struct ppp *
2421 ppp_find_unit(int unit)
2423 return cardmap_get(all_ppp_units, unit);
2427 * Locate an existing ppp channel.
2428 * The caller should have locked the all_channels_lock.
2429 * First we look in the new_channels list, then in the
2430 * all_channels list. If found in the new_channels list,
2431 * we move it to the all_channels list. This is for speed
2432 * when we have a lot of channels in use.
2434 static struct channel *
2435 ppp_find_channel(int unit)
2437 struct channel *pch;
2438 struct list_head *list;
2440 list = &new_channels;
2441 while ((list = list->next) != &new_channels) {
2442 pch = list_entry(list, struct channel, list);
2443 if (pch->file.index == unit) {
2444 list_del(&pch->list);
2445 list_add(&pch->list, &all_channels);
2446 return pch;
2449 list = &all_channels;
2450 while ((list = list->next) != &all_channels) {
2451 pch = list_entry(list, struct channel, list);
2452 if (pch->file.index == unit)
2453 return pch;
2455 return 0;
2459 * Connect a PPP channel to a PPP interface unit.
2461 static int
2462 ppp_connect_channel(struct channel *pch, int unit)
2464 struct ppp *ppp;
2465 int ret = -ENXIO;
2466 int hdrlen;
2468 down(&all_ppp_sem);
2469 ppp = ppp_find_unit(unit);
2470 if (ppp == 0)
2471 goto out;
2472 write_lock_bh(&pch->upl);
2473 ret = -EINVAL;
2474 if (pch->ppp != 0)
2475 goto outl;
2477 ppp_lock(ppp);
2478 if (pch->file.hdrlen > ppp->file.hdrlen)
2479 ppp->file.hdrlen = pch->file.hdrlen;
2480 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2481 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2482 ppp->dev->hard_header_len = hdrlen;
2483 list_add_tail(&pch->clist, &ppp->channels);
2484 ++ppp->n_channels;
2485 pch->ppp = ppp;
2486 atomic_inc(&ppp->file.refcnt);
2487 ppp_unlock(ppp);
2488 ret = 0;
2490 outl:
2491 write_unlock_bh(&pch->upl);
2492 out:
2493 up(&all_ppp_sem);
2494 return ret;
2498 * Disconnect a channel from its ppp unit.
2500 static int
2501 ppp_disconnect_channel(struct channel *pch)
2503 struct ppp *ppp;
2504 int err = -EINVAL;
2506 write_lock_bh(&pch->upl);
2507 ppp = pch->ppp;
2508 pch->ppp = NULL;
2509 write_unlock_bh(&pch->upl);
2510 if (ppp != 0) {
2511 /* remove it from the ppp unit's list */
2512 ppp_lock(ppp);
2513 list_del(&pch->clist);
2514 --ppp->n_channels;
2515 ppp_unlock(ppp);
2516 if (atomic_dec_and_test(&ppp->file.refcnt))
2517 ppp_destroy_interface(ppp);
2518 err = 0;
2520 return err;
2524 * Free up the resources used by a ppp channel.
2526 static void ppp_destroy_channel(struct channel *pch)
2528 atomic_dec(&channel_count);
2530 if (!pch->file.dead) {
2531 /* "can't happen" */
2532 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2533 pch);
2534 return;
2536 skb_queue_purge(&pch->file.xq);
2537 skb_queue_purge(&pch->file.rq);
2538 kfree(pch);
2541 static void __exit ppp_cleanup(void)
2543 /* should never happen */
2544 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2545 printk(KERN_ERR "PPP: removing module but units remain!\n");
2546 cardmap_destroy(&all_ppp_units);
2547 if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2548 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2549 devfs_remove("ppp");
2553 * Cardmap implementation.
2555 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2557 struct cardmap *p;
2558 int i;
2560 for (p = map; p != NULL; ) {
2561 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2562 return NULL;
2563 if (p->shift == 0)
2564 return p->ptr[i];
2565 nr &= ~(CARDMAP_MASK << p->shift);
2566 p = p->ptr[i];
2568 return NULL;
2571 static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2573 struct cardmap *p;
2574 int i;
2576 p = *pmap;
2577 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2578 do {
2579 /* need a new top level */
2580 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2581 memset(np, 0, sizeof(*np));
2582 np->ptr[0] = p;
2583 if (p != NULL) {
2584 np->shift = p->shift + CARDMAP_ORDER;
2585 p->parent = np;
2586 } else
2587 np->shift = 0;
2588 p = np;
2589 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2590 *pmap = p;
2592 while (p->shift > 0) {
2593 i = (nr >> p->shift) & CARDMAP_MASK;
2594 if (p->ptr[i] == NULL) {
2595 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2596 memset(np, 0, sizeof(*np));
2597 np->shift = p->shift - CARDMAP_ORDER;
2598 np->parent = p;
2599 p->ptr[i] = np;
2601 if (ptr == NULL)
2602 clear_bit(i, &p->inuse);
2603 p = p->ptr[i];
2605 i = nr & CARDMAP_MASK;
2606 p->ptr[i] = ptr;
2607 if (ptr != NULL)
2608 set_bit(i, &p->inuse);
2609 else
2610 clear_bit(i, &p->inuse);
2613 static unsigned int cardmap_find_first_free(struct cardmap *map)
2615 struct cardmap *p;
2616 unsigned int nr = 0;
2617 int i;
2619 if ((p = map) == NULL)
2620 return 0;
2621 for (;;) {
2622 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2623 if (i >= CARDMAP_WIDTH) {
2624 if (p->parent == NULL)
2625 return CARDMAP_WIDTH << p->shift;
2626 p = p->parent;
2627 i = (nr >> p->shift) & CARDMAP_MASK;
2628 set_bit(i, &p->inuse);
2629 continue;
2631 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2632 if (p->shift == 0 || p->ptr[i] == NULL)
2633 return nr;
2634 p = p->ptr[i];
2638 static void cardmap_destroy(struct cardmap **pmap)
2640 struct cardmap *p, *np;
2641 int i;
2643 for (p = *pmap; p != NULL; p = np) {
2644 if (p->shift != 0) {
2645 for (i = 0; i < CARDMAP_WIDTH; ++i)
2646 if (p->ptr[i] != NULL)
2647 break;
2648 if (i < CARDMAP_WIDTH) {
2649 np = p->ptr[i];
2650 p->ptr[i] = NULL;
2651 continue;
2654 np = p->parent;
2655 kfree(p);
2657 *pmap = NULL;
2660 /* Module/initialization stuff */
2662 module_init(ppp_init);
2663 module_exit(ppp_cleanup);
2665 EXPORT_SYMBOL(ppp_register_channel);
2666 EXPORT_SYMBOL(ppp_unregister_channel);
2667 EXPORT_SYMBOL(ppp_channel_index);
2668 EXPORT_SYMBOL(ppp_unit_number);
2669 EXPORT_SYMBOL(ppp_input);
2670 EXPORT_SYMBOL(ppp_input_error);
2671 EXPORT_SYMBOL(ppp_output_wakeup);
2672 EXPORT_SYMBOL(ppp_register_compressor);
2673 EXPORT_SYMBOL(ppp_unregister_compressor);
2674 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2675 EXPORT_SYMBOL(all_channels); /* for debugging */
2676 MODULE_LICENSE("GPL");