More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / ppp_generic.c
blobac0b2abaf3244878cbac2ca9e362999df6a6b105
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 memcpy(skb_put(ns, skb->len), skb->data, 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;
1449 if (skb_tailroom(skb) < 124) {
1450 /* copy to a new sk_buff with more tailroom */
1451 ns = dev_alloc_skb(skb->len + 128);
1452 if (ns == 0) {
1453 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1454 goto err;
1456 skb_reserve(ns, 2);
1457 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
1458 kfree_skb(skb);
1459 skb = ns;
1461 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1462 if (len <= 0) {
1463 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1464 goto err;
1466 len += 2;
1467 if (len > skb->len)
1468 skb_put(skb, len - skb->len);
1469 else if (len < skb->len)
1470 skb_trim(skb, len);
1471 proto = PPP_IP;
1472 break;
1474 case PPP_VJC_UNCOMP:
1475 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1476 goto err;
1477 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1478 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1479 goto err;
1481 proto = PPP_IP;
1482 break;
1484 case PPP_CCP:
1485 ppp_ccp_peek(ppp, skb, 1);
1486 break;
1489 ++ppp->stats.rx_packets;
1490 ppp->stats.rx_bytes += skb->len - 2;
1492 npi = proto_to_npindex(proto);
1493 if (npi < 0) {
1494 /* control or unknown frame - pass it to pppd */
1495 skb_queue_tail(&ppp->file.rq, skb);
1496 /* limit queue length by dropping old frames */
1497 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1498 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1499 kfree_skb(skb);
1500 /* wake up any process polling or blocking on read */
1501 wake_up_interruptible(&ppp->file.rwait);
1503 } else {
1504 /* network protocol frame - give it to the kernel */
1506 #ifdef CONFIG_PPP_FILTER
1507 /* check if the packet passes the pass and active filters */
1508 /* the filter instructions are constructed assuming
1509 a four-byte PPP header on each packet */
1510 *skb_push(skb, 2) = 0;
1511 if (ppp->pass_filter.filter
1512 && sk_run_filter(skb, ppp->pass_filter.filter,
1513 ppp->pass_filter.len) == 0) {
1514 if (ppp->debug & 1)
1515 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1516 kfree_skb(skb);
1517 return;
1519 if (!(ppp->active_filter.filter
1520 && sk_run_filter(skb, ppp->active_filter.filter,
1521 ppp->active_filter.len) == 0))
1522 ppp->last_recv = jiffies;
1523 skb_pull(skb, 2);
1524 #else
1525 ppp->last_recv = jiffies;
1526 #endif /* CONFIG_PPP_FILTER */
1528 if ((ppp->dev->flags & IFF_UP) == 0
1529 || ppp->npmode[npi] != NPMODE_PASS) {
1530 kfree_skb(skb);
1531 } else {
1532 skb_pull(skb, 2); /* chop off protocol */
1533 skb->dev = ppp->dev;
1534 skb->protocol = htons(npindex_to_ethertype[npi]);
1535 skb->mac.raw = skb->data;
1536 netif_rx(skb);
1537 ppp->dev->last_rx = jiffies;
1540 return;
1542 err:
1543 kfree_skb(skb);
1544 ppp_receive_error(ppp);
1547 static struct sk_buff *
1548 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1550 int proto = PPP_PROTO(skb);
1551 struct sk_buff *ns;
1552 int len;
1554 if (proto == PPP_COMP) {
1555 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1556 if (ns == 0) {
1557 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1558 goto err;
1560 /* the decompressor still expects the A/C bytes in the hdr */
1561 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1562 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1563 if (len < 0) {
1564 /* Pass the compressed frame to pppd as an
1565 error indication. */
1566 if (len == DECOMP_FATALERROR)
1567 ppp->rstate |= SC_DC_FERROR;
1568 kfree_skb(ns);
1569 goto err;
1572 kfree_skb(skb);
1573 skb = ns;
1574 skb_put(skb, len);
1575 skb_pull(skb, 2); /* pull off the A/C bytes */
1577 } else {
1578 /* Uncompressed frame - pass to decompressor so it
1579 can update its dictionary if necessary. */
1580 if (ppp->rcomp->incomp)
1581 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1582 skb->len + 2);
1585 return skb;
1587 err:
1588 ppp->rstate |= SC_DC_ERROR;
1589 ppp_receive_error(ppp);
1590 return skb;
1593 #ifdef CONFIG_PPP_MULTILINK
1595 * Receive a multilink frame.
1596 * We put it on the reconstruction queue and then pull off
1597 * as many completed frames as we can.
1599 static void
1600 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1602 u32 mask, seq;
1603 struct list_head *l;
1604 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1606 if (skb->len < mphdrlen + 1 || ppp->mrru == 0)
1607 goto err; /* no good, throw it away */
1609 /* Decode sequence number and begin/end bits */
1610 if (ppp->flags & SC_MP_SHORTSEQ) {
1611 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1612 mask = 0xfff;
1613 } else {
1614 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1615 mask = 0xffffff;
1617 skb->BEbits = skb->data[2];
1618 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1621 * Do protocol ID decompression on the first fragment of each packet.
1623 if ((skb->BEbits & B) && (skb->data[0] & 1))
1624 *skb_push(skb, 1) = 0;
1627 * Expand sequence number to 32 bits, making it as close
1628 * as possible to ppp->minseq.
1630 seq |= ppp->minseq & ~mask;
1631 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1632 seq += mask + 1;
1633 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1634 seq -= mask + 1; /* should never happen */
1635 skb->sequence = seq;
1636 pch->lastseq = seq;
1639 * If this packet comes before the next one we were expecting,
1640 * drop it.
1642 if (seq_before(seq, ppp->nextseq)) {
1643 kfree_skb(skb);
1644 ++ppp->stats.rx_dropped;
1645 ppp_receive_error(ppp);
1646 return;
1650 * Reevaluate minseq, the minimum over all channels of the
1651 * last sequence number received on each channel. Because of
1652 * the increasing sequence number rule, we know that any fragment
1653 * before `minseq' which hasn't arrived is never going to arrive.
1654 * The list of channels can't change because we have the receive
1655 * side of the ppp unit locked.
1657 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1658 struct channel *ch = list_entry(l, struct channel, clist);
1659 if (seq_before(ch->lastseq, seq))
1660 seq = ch->lastseq;
1662 if (seq_before(ppp->minseq, seq))
1663 ppp->minseq = seq;
1665 /* Put the fragment on the reconstruction queue */
1666 ppp_mp_insert(ppp, skb);
1668 /* If the queue is getting long, don't wait any longer for packets
1669 before the start of the queue. */
1670 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1671 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1672 ppp->minseq = ppp->mrq.next->sequence;
1674 /* Pull completed packets off the queue and receive them. */
1675 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1676 ppp_receive_nonmp_frame(ppp, skb);
1678 return;
1680 err:
1681 kfree_skb(skb);
1682 ppp_receive_error(ppp);
1686 * Insert a fragment on the MP reconstruction queue.
1687 * The queue is ordered by increasing sequence number.
1689 static void
1690 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1692 struct sk_buff *p;
1693 struct sk_buff_head *list = &ppp->mrq;
1694 u32 seq = skb->sequence;
1696 /* N.B. we don't need to lock the list lock because we have the
1697 ppp unit receive-side lock. */
1698 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1699 if (seq_before(seq, p->sequence))
1700 break;
1701 __skb_insert(skb, p->prev, p, list);
1705 * Reconstruct a packet from the MP fragment queue.
1706 * We go through increasing sequence numbers until we find a
1707 * complete packet, or we get to the sequence number for a fragment
1708 * which hasn't arrived but might still do so.
1710 struct sk_buff *
1711 ppp_mp_reconstruct(struct ppp *ppp)
1713 u32 seq = ppp->nextseq;
1714 u32 minseq = ppp->minseq;
1715 struct sk_buff_head *list = &ppp->mrq;
1716 struct sk_buff *p, *next;
1717 struct sk_buff *head, *tail;
1718 struct sk_buff *skb = NULL;
1719 int lost = 0, len = 0;
1721 if (ppp->mrru == 0) /* do nothing until mrru is set */
1722 return NULL;
1723 head = list->next;
1724 tail = NULL;
1725 for (p = head; p != (struct sk_buff *) list; p = next) {
1726 next = p->next;
1727 if (seq_before(p->sequence, seq)) {
1728 /* this can't happen, anyway ignore the skb */
1729 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1730 p->sequence, seq);
1731 head = next;
1732 continue;
1734 if (p->sequence != seq) {
1735 /* Fragment `seq' is missing. If it is after
1736 minseq, it might arrive later, so stop here. */
1737 if (seq_after(seq, minseq))
1738 break;
1739 /* Fragment `seq' is lost, keep going. */
1740 lost = 1;
1741 seq = seq_before(minseq, p->sequence)?
1742 minseq + 1: p->sequence;
1743 next = p;
1744 continue;
1748 * At this point we know that all the fragments from
1749 * ppp->nextseq to seq are either present or lost.
1750 * Also, there are no complete packets in the queue
1751 * that have no missing fragments and end before this
1752 * fragment.
1755 /* B bit set indicates this fragment starts a packet */
1756 if (p->BEbits & B) {
1757 head = p;
1758 lost = 0;
1759 len = 0;
1762 len += p->len;
1764 /* Got a complete packet yet? */
1765 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1766 if (len > ppp->mrru + 2) {
1767 ++ppp->stats.rx_length_errors;
1768 printk(KERN_DEBUG "PPP: reconstructed packet"
1769 " is too long (%d)\n", len);
1770 } else if (p == head) {
1771 /* fragment is complete packet - reuse skb */
1772 tail = p;
1773 skb = skb_get(p);
1774 break;
1775 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1776 ++ppp->stats.rx_missed_errors;
1777 printk(KERN_DEBUG "PPP: no memory for "
1778 "reconstructed packet");
1779 } else {
1780 tail = p;
1781 break;
1783 ppp->nextseq = seq + 1;
1787 * If this is the ending fragment of a packet,
1788 * and we haven't found a complete valid packet yet,
1789 * we can discard up to and including this fragment.
1791 if (p->BEbits & E)
1792 head = next;
1794 ++seq;
1797 /* If we have a complete packet, copy it all into one skb. */
1798 if (tail != NULL) {
1799 /* If we have discarded any fragments,
1800 signal a receive error. */
1801 if (head->sequence != ppp->nextseq) {
1802 if (ppp->debug & 1)
1803 printk(KERN_DEBUG " missed pkts %u..%u\n",
1804 ppp->nextseq, head->sequence-1);
1805 ++ppp->stats.rx_dropped;
1806 ppp_receive_error(ppp);
1809 if (head != tail)
1810 /* copy to a single skb */
1811 for (p = head; p != tail->next; p = p->next)
1812 memcpy(skb_put(skb, p->len), p->data, p->len);
1813 ppp->nextseq = tail->sequence + 1;
1814 head = tail->next;
1817 /* Discard all the skbuffs that we have copied the data out of
1818 or that we can't use. */
1819 while ((p = list->next) != head) {
1820 __skb_unlink(p, list);
1821 kfree_skb(p);
1824 return skb;
1826 #endif /* CONFIG_PPP_MULTILINK */
1829 * Channel interface.
1833 * Create a new, unattached ppp channel.
1836 ppp_register_channel(struct ppp_channel *chan)
1838 struct channel *pch;
1840 pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
1841 if (pch == 0)
1842 return -ENOMEM;
1843 memset(pch, 0, sizeof(struct channel));
1844 pch->ppp = NULL;
1845 pch->chan = chan;
1846 chan->ppp = pch;
1847 init_ppp_file(&pch->file, CHANNEL);
1848 pch->file.hdrlen = chan->hdrlen;
1849 #ifdef CONFIG_PPP_MULTILINK
1850 pch->lastseq = -1;
1851 #endif /* CONFIG_PPP_MULTILINK */
1852 init_rwsem(&pch->chan_sem);
1853 spin_lock_init(&pch->downl);
1854 pch->upl = RW_LOCK_UNLOCKED;
1855 spin_lock_bh(&all_channels_lock);
1856 pch->file.index = ++last_channel_index;
1857 list_add(&pch->list, &new_channels);
1858 atomic_inc(&channel_count);
1859 spin_unlock_bh(&all_channels_lock);
1860 return 0;
1864 * Return the index of a channel.
1866 int ppp_channel_index(struct ppp_channel *chan)
1868 struct channel *pch = chan->ppp;
1870 if (pch != 0)
1871 return pch->file.index;
1872 return -1;
1876 * Return the PPP unit number to which a channel is connected.
1878 int ppp_unit_number(struct ppp_channel *chan)
1880 struct channel *pch = chan->ppp;
1881 int unit = -1;
1883 if (pch != 0) {
1884 read_lock_bh(&pch->upl);
1885 if (pch->ppp != 0)
1886 unit = pch->ppp->file.index;
1887 read_unlock_bh(&pch->upl);
1889 return unit;
1893 * Disconnect a channel from the generic layer.
1894 * This must be called in process context.
1896 void
1897 ppp_unregister_channel(struct ppp_channel *chan)
1899 struct channel *pch = chan->ppp;
1901 if (pch == 0)
1902 return; /* should never happen */
1903 chan->ppp = 0;
1906 * This ensures that we have returned from any calls into the
1907 * the channel's start_xmit or ioctl routine before we proceed.
1909 down_write(&pch->chan_sem);
1910 spin_lock_bh(&pch->downl);
1911 pch->chan = 0;
1912 spin_unlock_bh(&pch->downl);
1913 up_write(&pch->chan_sem);
1914 ppp_disconnect_channel(pch);
1915 spin_lock_bh(&all_channels_lock);
1916 list_del(&pch->list);
1917 spin_unlock_bh(&all_channels_lock);
1918 pch->file.dead = 1;
1919 wake_up_interruptible(&pch->file.rwait);
1920 if (atomic_dec_and_test(&pch->file.refcnt))
1921 ppp_destroy_channel(pch);
1925 * Callback from a channel when it can accept more to transmit.
1926 * This should be called at BH/softirq level, not interrupt level.
1928 void
1929 ppp_output_wakeup(struct ppp_channel *chan)
1931 struct channel *pch = chan->ppp;
1933 if (pch == 0)
1934 return;
1935 ppp_channel_push(pch);
1939 * Compression control.
1942 /* Process the PPPIOCSCOMPRESS ioctl. */
1943 static int
1944 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1946 int err;
1947 struct compressor *cp, *ocomp;
1948 struct ppp_option_data data;
1949 void *state, *ostate;
1950 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1952 err = -EFAULT;
1953 if (copy_from_user(&data, (void __user *) arg, sizeof(data))
1954 || (data.length <= CCP_MAX_OPTION_LENGTH
1955 && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
1956 goto out;
1957 err = -EINVAL;
1958 if (data.length > CCP_MAX_OPTION_LENGTH
1959 || ccp_option[1] < 2 || ccp_option[1] > data.length)
1960 goto out;
1962 cp = find_compressor(ccp_option[0]);
1963 #ifdef CONFIG_KMOD
1964 if (cp == 0) {
1965 request_module("ppp-compress-%d", ccp_option[0]);
1966 cp = find_compressor(ccp_option[0]);
1968 #endif /* CONFIG_KMOD */
1969 if (cp == 0)
1970 goto out;
1972 err = -ENOBUFS;
1973 if (data.transmit) {
1974 state = cp->comp_alloc(ccp_option, data.length);
1975 if (state != 0) {
1976 ppp_xmit_lock(ppp);
1977 ppp->xstate &= ~SC_COMP_RUN;
1978 ocomp = ppp->xcomp;
1979 ostate = ppp->xc_state;
1980 ppp->xcomp = cp;
1981 ppp->xc_state = state;
1982 ppp_xmit_unlock(ppp);
1983 if (ostate != 0) {
1984 ocomp->comp_free(ostate);
1985 module_put(ocomp->owner);
1987 err = 0;
1988 } else
1989 module_put(cp->owner);
1991 } else {
1992 state = cp->decomp_alloc(ccp_option, data.length);
1993 if (state != 0) {
1994 ppp_recv_lock(ppp);
1995 ppp->rstate &= ~SC_DECOMP_RUN;
1996 ocomp = ppp->rcomp;
1997 ostate = ppp->rc_state;
1998 ppp->rcomp = cp;
1999 ppp->rc_state = state;
2000 ppp_recv_unlock(ppp);
2001 if (ostate != 0) {
2002 ocomp->decomp_free(ostate);
2003 module_put(ocomp->owner);
2005 err = 0;
2006 } else
2007 module_put(cp->owner);
2010 out:
2011 return err;
2015 * Look at a CCP packet and update our state accordingly.
2016 * We assume the caller has the xmit or recv path locked.
2018 static void
2019 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2021 unsigned char *dp = skb->data + 2;
2022 int len;
2024 if (skb->len < CCP_HDRLEN + 2
2025 || skb->len < (len = CCP_LENGTH(dp)) + 2)
2026 return; /* too short */
2028 switch (CCP_CODE(dp)) {
2029 case CCP_CONFREQ:
2031 /* A ConfReq starts negotiation of compression
2032 * in one direction of transmission,
2033 * and hence brings it down...but which way?
2035 * Remember:
2036 * A ConfReq indicates what the sender would like to receive
2038 if(inbound)
2039 /* He is proposing what I should send */
2040 ppp->xstate &= ~SC_COMP_RUN;
2041 else
2042 /* I am proposing to what he should send */
2043 ppp->rstate &= ~SC_DECOMP_RUN;
2045 break;
2047 case CCP_TERMREQ:
2048 case CCP_TERMACK:
2050 * CCP is going down, both directions of transmission
2052 ppp->rstate &= ~SC_DECOMP_RUN;
2053 ppp->xstate &= ~SC_COMP_RUN;
2054 break;
2056 case CCP_CONFACK:
2057 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2058 break;
2059 dp += CCP_HDRLEN;
2060 len -= CCP_HDRLEN;
2061 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2062 break;
2063 if (inbound) {
2064 /* we will start receiving compressed packets */
2065 if (ppp->rc_state == 0)
2066 break;
2067 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2068 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2069 ppp->rstate |= SC_DECOMP_RUN;
2070 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2072 } else {
2073 /* we will soon start sending compressed packets */
2074 if (ppp->xc_state == 0)
2075 break;
2076 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2077 ppp->file.index, 0, ppp->debug))
2078 ppp->xstate |= SC_COMP_RUN;
2080 break;
2082 case CCP_RESETACK:
2083 /* reset the [de]compressor */
2084 if ((ppp->flags & SC_CCP_UP) == 0)
2085 break;
2086 if (inbound) {
2087 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2088 ppp->rcomp->decomp_reset(ppp->rc_state);
2089 ppp->rstate &= ~SC_DC_ERROR;
2091 } else {
2092 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2093 ppp->xcomp->comp_reset(ppp->xc_state);
2095 break;
2099 /* Free up compression resources. */
2100 static void
2101 ppp_ccp_closed(struct ppp *ppp)
2103 void *xstate, *rstate;
2104 struct compressor *xcomp, *rcomp;
2106 ppp_lock(ppp);
2107 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2108 ppp->xstate = 0;
2109 xcomp = ppp->xcomp;
2110 xstate = ppp->xc_state;
2111 ppp->xc_state = 0;
2112 ppp->rstate = 0;
2113 rcomp = ppp->rcomp;
2114 rstate = ppp->rc_state;
2115 ppp->rc_state = 0;
2116 ppp_unlock(ppp);
2118 if (xstate) {
2119 xcomp->comp_free(xstate);
2120 module_put(xcomp->owner);
2122 if (rstate) {
2123 rcomp->decomp_free(rstate);
2124 module_put(rcomp->owner);
2128 /* List of compressors. */
2129 static LIST_HEAD(compressor_list);
2130 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2132 struct compressor_entry {
2133 struct list_head list;
2134 struct compressor *comp;
2137 static struct compressor_entry *
2138 find_comp_entry(int proto)
2140 struct compressor_entry *ce;
2141 struct list_head *list = &compressor_list;
2143 while ((list = list->next) != &compressor_list) {
2144 ce = list_entry(list, struct compressor_entry, list);
2145 if (ce->comp->compress_proto == proto)
2146 return ce;
2148 return 0;
2151 /* Register a compressor */
2153 ppp_register_compressor(struct compressor *cp)
2155 struct compressor_entry *ce;
2156 int ret;
2157 spin_lock(&compressor_list_lock);
2158 ret = -EEXIST;
2159 if (find_comp_entry(cp->compress_proto) != 0)
2160 goto out;
2161 ret = -ENOMEM;
2162 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2163 if (ce == 0)
2164 goto out;
2165 ret = 0;
2166 ce->comp = cp;
2167 list_add(&ce->list, &compressor_list);
2168 out:
2169 spin_unlock(&compressor_list_lock);
2170 return ret;
2173 /* Unregister a compressor */
2174 void
2175 ppp_unregister_compressor(struct compressor *cp)
2177 struct compressor_entry *ce;
2179 spin_lock(&compressor_list_lock);
2180 ce = find_comp_entry(cp->compress_proto);
2181 if (ce != 0 && ce->comp == cp) {
2182 list_del(&ce->list);
2183 kfree(ce);
2185 spin_unlock(&compressor_list_lock);
2188 /* Find a compressor. */
2189 static struct compressor *
2190 find_compressor(int type)
2192 struct compressor_entry *ce;
2193 struct compressor *cp = 0;
2195 spin_lock(&compressor_list_lock);
2196 ce = find_comp_entry(type);
2197 if (ce != 0) {
2198 cp = ce->comp;
2199 if (!try_module_get(cp->owner))
2200 cp = NULL;
2202 spin_unlock(&compressor_list_lock);
2203 return cp;
2207 * Miscelleneous stuff.
2210 static void
2211 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2213 struct slcompress *vj = ppp->vj;
2215 memset(st, 0, sizeof(*st));
2216 st->p.ppp_ipackets = ppp->stats.rx_packets;
2217 st->p.ppp_ierrors = ppp->stats.rx_errors;
2218 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2219 st->p.ppp_opackets = ppp->stats.tx_packets;
2220 st->p.ppp_oerrors = ppp->stats.tx_errors;
2221 st->p.ppp_obytes = ppp->stats.tx_bytes;
2222 if (vj == 0)
2223 return;
2224 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2225 st->vj.vjs_compressed = vj->sls_o_compressed;
2226 st->vj.vjs_searches = vj->sls_o_searches;
2227 st->vj.vjs_misses = vj->sls_o_misses;
2228 st->vj.vjs_errorin = vj->sls_i_error;
2229 st->vj.vjs_tossed = vj->sls_i_tossed;
2230 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2231 st->vj.vjs_compressedin = vj->sls_i_compressed;
2235 * Stuff for handling the lists of ppp units and channels
2236 * and for initialization.
2239 static void ppp_device_destructor(struct net_device *dev)
2241 kfree(dev);
2245 * Create a new ppp interface unit. Fails if it can't allocate memory
2246 * or if there is already a unit with the requested number.
2247 * unit == -1 means allocate a new number.
2249 static struct ppp *
2250 ppp_create_interface(int unit, int *retp)
2252 struct ppp *ppp;
2253 struct net_device *dev = NULL;
2254 int ret = -ENOMEM;
2255 int i;
2257 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2258 if (ppp == 0)
2259 goto err;
2260 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
2261 if (dev == 0)
2262 goto err;
2263 memset(ppp, 0, sizeof(struct ppp));
2264 memset(dev, 0, sizeof(struct net_device));
2266 ret = -EEXIST;
2267 down(&all_ppp_sem);
2268 if (unit < 0)
2269 unit = cardmap_find_first_free(all_ppp_units);
2270 else if (cardmap_get(all_ppp_units, unit) != NULL)
2271 goto err_unlock; /* unit already exists */
2273 /* Initialize the new ppp unit */
2274 ppp->file.index = unit;
2275 ppp->mru = PPP_MRU;
2276 init_ppp_file(&ppp->file, INTERFACE);
2277 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2278 for (i = 0; i < NUM_NP; ++i)
2279 ppp->npmode[i] = NPMODE_PASS;
2280 INIT_LIST_HEAD(&ppp->channels);
2281 spin_lock_init(&ppp->rlock);
2282 spin_lock_init(&ppp->wlock);
2283 #ifdef CONFIG_PPP_MULTILINK
2284 ppp->minseq = -1;
2285 skb_queue_head_init(&ppp->mrq);
2286 #endif /* CONFIG_PPP_MULTILINK */
2288 ppp->dev = dev;
2289 dev->init = ppp_net_init;
2290 sprintf(dev->name, "ppp%d", unit);
2291 dev->priv = ppp;
2292 dev->destructor = ppp_device_destructor;
2294 rtnl_lock();
2295 ret = register_netdevice(dev);
2296 rtnl_unlock();
2297 if (ret != 0) {
2298 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2299 dev->name, ret);
2300 goto err_unlock;
2303 atomic_inc(&ppp_unit_count);
2304 cardmap_set(&all_ppp_units, unit, ppp);
2305 up(&all_ppp_sem);
2306 *retp = 0;
2307 return ppp;
2309 err_unlock:
2310 up(&all_ppp_sem);
2311 err:
2312 *retp = ret;
2313 if (ppp)
2314 kfree(ppp);
2315 if (dev)
2316 kfree(dev);
2317 return NULL;
2321 * Initialize a ppp_file structure.
2323 static void
2324 init_ppp_file(struct ppp_file *pf, int kind)
2326 pf->kind = kind;
2327 skb_queue_head_init(&pf->xq);
2328 skb_queue_head_init(&pf->rq);
2329 atomic_set(&pf->refcnt, 1);
2330 init_waitqueue_head(&pf->rwait);
2334 * Take down a ppp interface unit - called when the owning file
2335 * (the one that created the unit) is closed or detached.
2337 static void ppp_shutdown_interface(struct ppp *ppp)
2339 struct net_device *dev;
2341 down(&all_ppp_sem);
2342 ppp_lock(ppp);
2343 dev = ppp->dev;
2344 ppp->dev = 0;
2345 ppp_unlock(ppp);
2346 /* This will call dev_close() for us. */
2347 if (dev)
2348 unregister_netdev(dev);
2349 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2350 ppp->file.dead = 1;
2351 ppp->owner = NULL;
2352 wake_up_interruptible(&ppp->file.rwait);
2353 up(&all_ppp_sem);
2357 * Free the memory used by a ppp unit. This is only called once
2358 * there are no channels connected to the unit and no file structs
2359 * that reference the unit.
2361 static void ppp_destroy_interface(struct ppp *ppp)
2363 atomic_dec(&ppp_unit_count);
2365 if (!ppp->file.dead || ppp->n_channels) {
2366 /* "can't happen" */
2367 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2368 "n_channels=%d !\n", ppp, ppp->file.dead,
2369 ppp->n_channels);
2370 return;
2373 ppp_ccp_closed(ppp);
2374 if (ppp->vj) {
2375 slhc_free(ppp->vj);
2376 ppp->vj = 0;
2378 skb_queue_purge(&ppp->file.xq);
2379 skb_queue_purge(&ppp->file.rq);
2380 #ifdef CONFIG_PPP_MULTILINK
2381 skb_queue_purge(&ppp->mrq);
2382 #endif /* CONFIG_PPP_MULTILINK */
2383 #ifdef CONFIG_PPP_FILTER
2384 if (ppp->pass_filter.filter) {
2385 kfree(ppp->pass_filter.filter);
2386 ppp->pass_filter.filter = NULL;
2388 if (ppp->active_filter.filter) {
2389 kfree(ppp->active_filter.filter);
2390 ppp->active_filter.filter = 0;
2392 #endif /* CONFIG_PPP_FILTER */
2394 kfree(ppp);
2398 * Locate an existing ppp unit.
2399 * The caller should have locked the all_ppp_sem.
2401 static struct ppp *
2402 ppp_find_unit(int unit)
2404 return cardmap_get(all_ppp_units, unit);
2408 * Locate an existing ppp channel.
2409 * The caller should have locked the all_channels_lock.
2410 * First we look in the new_channels list, then in the
2411 * all_channels list. If found in the new_channels list,
2412 * we move it to the all_channels list. This is for speed
2413 * when we have a lot of channels in use.
2415 static struct channel *
2416 ppp_find_channel(int unit)
2418 struct channel *pch;
2419 struct list_head *list;
2421 list = &new_channels;
2422 while ((list = list->next) != &new_channels) {
2423 pch = list_entry(list, struct channel, list);
2424 if (pch->file.index == unit) {
2425 list_del(&pch->list);
2426 list_add(&pch->list, &all_channels);
2427 return pch;
2430 list = &all_channels;
2431 while ((list = list->next) != &all_channels) {
2432 pch = list_entry(list, struct channel, list);
2433 if (pch->file.index == unit)
2434 return pch;
2436 return 0;
2440 * Connect a PPP channel to a PPP interface unit.
2442 static int
2443 ppp_connect_channel(struct channel *pch, int unit)
2445 struct ppp *ppp;
2446 int ret = -ENXIO;
2447 int hdrlen;
2449 down(&all_ppp_sem);
2450 ppp = ppp_find_unit(unit);
2451 if (ppp == 0)
2452 goto out;
2453 write_lock_bh(&pch->upl);
2454 ret = -EINVAL;
2455 if (pch->ppp != 0)
2456 goto outl;
2458 ppp_lock(ppp);
2459 if (pch->file.hdrlen > ppp->file.hdrlen)
2460 ppp->file.hdrlen = pch->file.hdrlen;
2461 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2462 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2463 ppp->dev->hard_header_len = hdrlen;
2464 list_add_tail(&pch->clist, &ppp->channels);
2465 ++ppp->n_channels;
2466 pch->ppp = ppp;
2467 atomic_inc(&ppp->file.refcnt);
2468 ppp_unlock(ppp);
2469 ret = 0;
2471 outl:
2472 write_unlock_bh(&pch->upl);
2473 out:
2474 up(&all_ppp_sem);
2475 return ret;
2479 * Disconnect a channel from its ppp unit.
2481 static int
2482 ppp_disconnect_channel(struct channel *pch)
2484 struct ppp *ppp;
2485 int err = -EINVAL;
2487 write_lock_bh(&pch->upl);
2488 ppp = pch->ppp;
2489 pch->ppp = NULL;
2490 write_unlock_bh(&pch->upl);
2491 if (ppp != 0) {
2492 /* remove it from the ppp unit's list */
2493 ppp_lock(ppp);
2494 list_del(&pch->clist);
2495 --ppp->n_channels;
2496 ppp_unlock(ppp);
2497 if (atomic_dec_and_test(&ppp->file.refcnt))
2498 ppp_destroy_interface(ppp);
2499 err = 0;
2501 return err;
2505 * Free up the resources used by a ppp channel.
2507 static void ppp_destroy_channel(struct channel *pch)
2509 atomic_dec(&channel_count);
2511 if (!pch->file.dead) {
2512 /* "can't happen" */
2513 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2514 pch);
2515 return;
2517 skb_queue_purge(&pch->file.xq);
2518 skb_queue_purge(&pch->file.rq);
2519 kfree(pch);
2522 static void __exit ppp_cleanup(void)
2524 /* should never happen */
2525 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2526 printk(KERN_ERR "PPP: removing module but units remain!\n");
2527 cardmap_destroy(&all_ppp_units);
2528 if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2529 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2530 devfs_remove("ppp");
2534 * Cardmap implementation.
2536 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2538 struct cardmap *p;
2539 int i;
2541 for (p = map; p != NULL; ) {
2542 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2543 return NULL;
2544 if (p->shift == 0)
2545 return p->ptr[i];
2546 nr &= ~(CARDMAP_MASK << p->shift);
2547 p = p->ptr[i];
2549 return NULL;
2552 static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2554 struct cardmap *p;
2555 int i;
2557 p = *pmap;
2558 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2559 do {
2560 /* need a new top level */
2561 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2562 memset(np, 0, sizeof(*np));
2563 np->ptr[0] = p;
2564 if (p != NULL) {
2565 np->shift = p->shift + CARDMAP_ORDER;
2566 p->parent = np;
2567 } else
2568 np->shift = 0;
2569 p = np;
2570 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2571 *pmap = p;
2573 while (p->shift > 0) {
2574 i = (nr >> p->shift) & CARDMAP_MASK;
2575 if (p->ptr[i] == NULL) {
2576 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2577 memset(np, 0, sizeof(*np));
2578 np->shift = p->shift - CARDMAP_ORDER;
2579 np->parent = p;
2580 p->ptr[i] = np;
2582 if (ptr == NULL)
2583 clear_bit(i, &p->inuse);
2584 p = p->ptr[i];
2586 i = nr & CARDMAP_MASK;
2587 p->ptr[i] = ptr;
2588 if (ptr != NULL)
2589 set_bit(i, &p->inuse);
2590 else
2591 clear_bit(i, &p->inuse);
2594 static unsigned int cardmap_find_first_free(struct cardmap *map)
2596 struct cardmap *p;
2597 unsigned int nr = 0;
2598 int i;
2600 if ((p = map) == NULL)
2601 return 0;
2602 for (;;) {
2603 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2604 if (i >= CARDMAP_WIDTH) {
2605 if (p->parent == NULL)
2606 return CARDMAP_WIDTH << p->shift;
2607 p = p->parent;
2608 i = (nr >> p->shift) & CARDMAP_MASK;
2609 set_bit(i, &p->inuse);
2610 continue;
2612 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2613 if (p->shift == 0 || p->ptr[i] == NULL)
2614 return nr;
2615 p = p->ptr[i];
2619 static void cardmap_destroy(struct cardmap **pmap)
2621 struct cardmap *p, *np;
2622 int i;
2624 for (p = *pmap; p != NULL; p = np) {
2625 if (p->shift != 0) {
2626 for (i = 0; i < CARDMAP_WIDTH; ++i)
2627 if (p->ptr[i] != NULL)
2628 break;
2629 if (i < CARDMAP_WIDTH) {
2630 np = p->ptr[i];
2631 p->ptr[i] = NULL;
2632 continue;
2635 np = p->parent;
2636 kfree(p);
2638 *pmap = NULL;
2641 /* Module/initialization stuff */
2643 module_init(ppp_init);
2644 module_exit(ppp_cleanup);
2646 EXPORT_SYMBOL(ppp_register_channel);
2647 EXPORT_SYMBOL(ppp_unregister_channel);
2648 EXPORT_SYMBOL(ppp_channel_index);
2649 EXPORT_SYMBOL(ppp_unit_number);
2650 EXPORT_SYMBOL(ppp_input);
2651 EXPORT_SYMBOL(ppp_input_error);
2652 EXPORT_SYMBOL(ppp_output_wakeup);
2653 EXPORT_SYMBOL(ppp_register_compressor);
2654 EXPORT_SYMBOL(ppp_unregister_compressor);
2655 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2656 EXPORT_SYMBOL(all_channels); /* for debugging */
2657 MODULE_LICENSE("GPL");