Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / net / ppp_generic.c
blob8fc567262e434e579c918f76f02955648982609a
1 /*
2 * Generic PPP layer for Linux.
4 * Copyright 1999-2000 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 20000417==
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/if_ppp.h>
36 #include <linux/ppp_channel.h>
37 #include <linux/ppp-comp.h>
38 #include <linux/skbuff.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/if_arp.h>
41 #include <linux/ip.h>
42 #include <linux/tcp.h>
43 #include <linux/spinlock.h>
44 #include <linux/smp_lock.h>
45 #include <net/slhc_vj.h>
46 #include <asm/atomic.h>
48 #define PPP_VERSION "2.4.1"
51 * Network protocols we support.
53 #define NP_IP 0 /* Internet Protocol V4 */
54 #define NP_IPV6 1 /* Internet Protocol V6 */
55 #define NP_IPX 2 /* IPX protocol */
56 #define NP_AT 3 /* Appletalk protocol */
57 #define NUM_NP 4 /* Number of NPs. */
59 #define MPHDRLEN 6 /* multilink protocol header length */
60 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
61 #define MIN_FRAG_SIZE 64
64 * An instance of /dev/ppp can be associated with either a ppp
65 * interface unit or a ppp channel. In both cases, file->private_data
66 * points to one of these.
68 struct ppp_file {
69 enum {
70 INTERFACE=1, CHANNEL
71 } kind;
72 struct sk_buff_head xq; /* pppd transmit queue */
73 struct sk_buff_head rq; /* receive queue for pppd */
74 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
75 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
76 int hdrlen; /* space to leave for headers */
77 struct list_head list; /* link in all_* list */
78 int index; /* interface unit / channel number */
81 #define PF_TO_X(pf, X) ((X *)((char *)(pf)-(unsigned long)(&((X *)0)->file)))
83 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
84 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
86 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
89 * Data structure describing one ppp unit.
90 * A ppp unit corresponds to a ppp network interface device
91 * and represents a multilink bundle.
92 * It can have 0 or more ppp channels connected to it.
94 struct ppp {
95 struct ppp_file file; /* stuff for read/write/poll */
96 struct list_head channels; /* list of attached channels */
97 int n_channels; /* how many channels are attached */
98 spinlock_t rlock; /* lock for receive side */
99 spinlock_t wlock; /* lock for transmit side */
100 int mru; /* max receive unit */
101 unsigned int flags; /* control bits */
102 unsigned int xstate; /* transmit state bits */
103 unsigned int rstate; /* receive state bits */
104 int debug; /* debug flags */
105 struct slcompress *vj; /* state for VJ header compression */
106 enum NPmode npmode[NUM_NP]; /* what to do with each net proto */
107 struct sk_buff *xmit_pending; /* a packet ready to go out */
108 struct compressor *xcomp; /* transmit packet compressor */
109 void *xc_state; /* its internal state */
110 struct compressor *rcomp; /* receive decompressor */
111 void *rc_state; /* its internal state */
112 unsigned long last_xmit; /* jiffies when last pkt sent */
113 unsigned long last_recv; /* jiffies when last pkt rcvd */
114 struct net_device *dev; /* network interface device */
115 #ifdef CONFIG_PPP_MULTILINK
116 int nxchan; /* next channel to send something on */
117 u32 nxseq; /* next sequence number to send */
118 int mrru; /* MP: max reconst. receive unit */
119 u32 nextseq; /* MP: seq no of next packet */
120 u32 minseq; /* MP: min of most recent seqnos */
121 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
122 #endif /* CONFIG_PPP_MULTILINK */
123 struct net_device_stats stats; /* statistics */
127 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
128 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
129 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
130 * Bits in xstate: SC_COMP_RUN
132 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
133 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
134 |SC_COMP_TCP|SC_REJ_COMP_TCP)
137 * Private data structure for each channel.
138 * This includes the data structure used for multilink.
140 struct channel {
141 struct ppp_file file; /* stuff for read/write/poll */
142 struct ppp_channel *chan; /* public channel data structure */
143 spinlock_t downl; /* protects `chan', file.xq dequeue */
144 struct ppp *ppp; /* ppp unit we're connected to */
145 struct list_head clist; /* link in list of channels per unit */
146 rwlock_t upl; /* protects `ppp' and `ulist' */
147 #ifdef CONFIG_PPP_MULTILINK
148 u8 avail; /* flag used in multilink stuff */
149 u8 had_frag; /* >= 1 fragments have been sent */
150 u32 lastseq; /* MP: last sequence # received */
151 #endif /* CONFIG_PPP_MULTILINK */
155 * SMP locking issues:
156 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
157 * list and the ppp.n_channels field, you need to take both locks
158 * before you modify them.
159 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
160 * channel.downl.
164 * all_ppp_lock protects the all_ppp_units.
165 * It also ensures that finding a ppp unit in the all_ppp_units list
166 * and updating its file.refcnt field is atomic.
168 static spinlock_t all_ppp_lock = SPIN_LOCK_UNLOCKED;
169 static LIST_HEAD(all_ppp_units);
172 * all_channels_lock protects all_channels and last_channel_index,
173 * and the atomicity of find a channel and updating its file.refcnt
174 * field.
176 static spinlock_t all_channels_lock = SPIN_LOCK_UNLOCKED;
177 static LIST_HEAD(all_channels);
178 static int last_channel_index;
180 /* Get the PPP protocol number from a skb */
181 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
183 /* We limit the length of ppp->file.rq to this (arbitrary) value */
184 #define PPP_MAX_RQLEN 32
187 * Maximum number of multilink fragments queued up.
188 * This has to be large enough to cope with the maximum latency of
189 * the slowest channel relative to the others. Strictly it should
190 * depend on the number of channels and their characteristics.
192 #define PPP_MP_MAX_QLEN 128
194 /* Multilink header bits. */
195 #define B 0x80 /* this fragment begins a packet */
196 #define E 0x40 /* this fragment ends a packet */
198 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
199 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
200 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
202 /* Prototypes. */
203 static ssize_t ppp_file_read(struct ppp_file *pf, struct file *file,
204 char *buf, size_t count);
205 static ssize_t ppp_file_write(struct ppp_file *pf, const char *buf,
206 size_t count);
207 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
208 unsigned int cmd, unsigned long arg);
209 static void ppp_xmit_process(struct ppp *ppp);
210 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
211 static void ppp_push(struct ppp *ppp);
212 static void ppp_channel_push(struct channel *pch);
213 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
214 struct channel *pch);
215 static void ppp_receive_error(struct ppp *ppp);
216 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
217 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
218 struct sk_buff *skb);
219 #ifdef CONFIG_PPP_MULTILINK
220 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
221 struct channel *pch);
222 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
223 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
224 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
225 #endif /* CONFIG_PPP_MULTILINK */
226 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
227 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
228 static void ppp_ccp_closed(struct ppp *ppp);
229 static struct compressor *find_compressor(int type);
230 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
231 static struct ppp *ppp_create_interface(int unit, int *retp);
232 static void init_ppp_file(struct ppp_file *pf, int kind);
233 static void ppp_destroy_interface(struct ppp *ppp);
234 static struct ppp *ppp_find_unit(int unit);
235 static struct channel *ppp_find_channel(int unit);
236 static int ppp_connect_channel(struct channel *pch, int unit);
237 static int ppp_disconnect_channel(struct channel *pch);
238 static void ppp_destroy_channel(struct channel *pch);
240 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
241 static inline int proto_to_npindex(int proto)
243 switch (proto) {
244 case PPP_IP:
245 return NP_IP;
246 case PPP_IPV6:
247 return NP_IPV6;
248 case PPP_IPX:
249 return NP_IPX;
250 case PPP_AT:
251 return NP_AT;
253 return -EINVAL;
256 /* Translates an NP index into a PPP protocol number */
257 static const int npindex_to_proto[NUM_NP] = {
258 PPP_IP,
259 PPP_IPV6,
260 PPP_IPX,
261 PPP_AT,
264 /* Translates an ethertype into an NP index */
265 static inline int ethertype_to_npindex(int ethertype)
267 switch (ethertype) {
268 case ETH_P_IP:
269 return NP_IP;
270 case ETH_P_IPV6:
271 return NP_IPV6;
272 case ETH_P_IPX:
273 return NP_IPX;
274 case ETH_P_PPPTALK:
275 case ETH_P_ATALK:
276 return NP_AT;
278 return -1;
281 /* Translates an NP index into an ethertype */
282 static const int npindex_to_ethertype[NUM_NP] = {
283 ETH_P_IP,
284 ETH_P_IPV6,
285 ETH_P_IPX,
286 ETH_P_PPPTALK,
290 * Locking shorthand.
292 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
293 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
294 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
295 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
296 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
297 ppp_recv_lock(ppp); } while (0)
298 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
299 ppp_xmit_unlock(ppp); } while (0)
303 * /dev/ppp device routines.
304 * The /dev/ppp device is used by pppd to control the ppp unit.
305 * It supports the read, write, ioctl and poll functions.
306 * Open instances of /dev/ppp can be in one of three states:
307 * unattached, attached to a ppp unit, or attached to a ppp channel.
309 static int ppp_open(struct inode *inode, struct file *file)
312 * This could (should?) be enforced by the permissions on /dev/ppp.
314 if (!capable(CAP_NET_ADMIN))
315 return -EPERM;
316 return 0;
319 static int ppp_release(struct inode *inode, struct file *file)
321 struct ppp_file *pf = (struct ppp_file *) file->private_data;
323 lock_kernel();
324 if (pf != 0) {
325 file->private_data = 0;
326 if (atomic_dec_and_test(&pf->refcnt)) {
327 switch (pf->kind) {
328 case INTERFACE:
329 ppp_destroy_interface(PF_TO_PPP(pf));
330 break;
331 case CHANNEL:
332 ppp_destroy_channel(PF_TO_CHANNEL(pf));
333 break;
337 unlock_kernel();
338 return 0;
341 static ssize_t ppp_read(struct file *file, char *buf,
342 size_t count, loff_t *ppos)
344 struct ppp_file *pf = (struct ppp_file *) file->private_data;
346 return ppp_file_read(pf, file, buf, count);
349 static ssize_t ppp_file_read(struct ppp_file *pf, struct file *file,
350 char *buf, size_t count)
352 DECLARE_WAITQUEUE(wait, current);
353 ssize_t ret;
354 struct sk_buff *skb = 0;
356 ret = -ENXIO;
357 if (pf == 0)
358 goto out; /* not currently attached */
360 add_wait_queue(&pf->rwait, &wait);
361 current->state = TASK_INTERRUPTIBLE;
362 for (;;) {
363 skb = skb_dequeue(&pf->rq);
364 if (skb)
365 break;
366 ret = 0;
367 if (pf->kind == CHANNEL && PF_TO_CHANNEL(pf)->chan == 0)
368 break;
369 ret = -EAGAIN;
370 if (file->f_flags & O_NONBLOCK)
371 break;
372 ret = -ERESTARTSYS;
373 if (signal_pending(current))
374 break;
375 schedule();
377 current->state = TASK_RUNNING;
378 remove_wait_queue(&pf->rwait, &wait);
380 if (skb == 0)
381 goto out;
383 ret = -EOVERFLOW;
384 if (skb->len > count)
385 goto outf;
386 ret = -EFAULT;
387 if (copy_to_user(buf, skb->data, skb->len))
388 goto outf;
389 ret = skb->len;
391 outf:
392 kfree_skb(skb);
393 out:
394 return ret;
397 static ssize_t ppp_write(struct file *file, const char *buf,
398 size_t count, loff_t *ppos)
400 struct ppp_file *pf = (struct ppp_file *) file->private_data;
402 return ppp_file_write(pf, buf, count);
405 static ssize_t ppp_file_write(struct ppp_file *pf, const char *buf,
406 size_t count)
408 struct sk_buff *skb;
409 ssize_t ret;
411 ret = -ENXIO;
412 if (pf == 0)
413 goto out;
415 ret = -ENOMEM;
416 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
417 if (skb == 0)
418 goto out;
419 skb_reserve(skb, pf->hdrlen);
420 ret = -EFAULT;
421 if (copy_from_user(skb_put(skb, count), buf, count)) {
422 kfree_skb(skb);
423 goto out;
426 skb_queue_tail(&pf->xq, skb);
428 switch (pf->kind) {
429 case INTERFACE:
430 ppp_xmit_process(PF_TO_PPP(pf));
431 break;
432 case CHANNEL:
433 ppp_channel_push(PF_TO_CHANNEL(pf));
434 break;
437 ret = count;
439 out:
440 return ret;
443 /* No kernel lock - fine */
444 static unsigned int ppp_poll(struct file *file, poll_table *wait)
446 struct ppp_file *pf = (struct ppp_file *) file->private_data;
447 unsigned int mask;
449 if (pf == 0)
450 return 0;
451 poll_wait(file, &pf->rwait, wait);
452 mask = POLLOUT | POLLWRNORM;
453 if (skb_peek(&pf->rq) != 0)
454 mask |= POLLIN | POLLRDNORM;
455 if (pf->kind == CHANNEL) {
456 struct channel *pch = PF_TO_CHANNEL(pf);
457 if (pch->chan == 0)
458 mask |= POLLHUP;
460 return mask;
463 static int ppp_ioctl(struct inode *inode, struct file *file,
464 unsigned int cmd, unsigned long arg)
466 struct ppp_file *pf = (struct ppp_file *) file->private_data;
467 struct ppp *ppp;
468 int err = -EFAULT, val, val2, i;
469 struct ppp_idle idle;
470 struct npioctl npi;
471 int unit;
472 struct slcompress *vj;
474 if (pf == 0)
475 return ppp_unattached_ioctl(pf, file, cmd, arg);
477 if (pf->kind == CHANNEL) {
478 struct channel *pch = PF_TO_CHANNEL(pf);
479 struct ppp_channel *chan;
481 switch (cmd) {
482 case PPPIOCCONNECT:
483 if (get_user(unit, (int *) arg))
484 break;
485 err = ppp_connect_channel(pch, unit);
486 break;
488 case PPPIOCDISCONN:
489 err = ppp_disconnect_channel(pch);
490 break;
492 case PPPIOCDETACH:
493 file->private_data = 0;
494 if (atomic_dec_and_test(&pf->refcnt))
495 ppp_destroy_channel(pch);
496 err = 0;
497 break;
499 default:
500 spin_lock_bh(&pch->downl);
501 chan = pch->chan;
502 err = -ENOTTY;
503 if (chan && chan->ops->ioctl)
504 err = chan->ops->ioctl(chan, cmd, arg);
505 spin_unlock_bh(&pch->downl);
507 return err;
510 if (pf->kind != INTERFACE) {
511 /* can't happen */
512 printk(KERN_ERR "PPP: not interface or channel??\n");
513 return -EINVAL;
516 ppp = PF_TO_PPP(pf);
517 switch (cmd) {
518 case PPPIOCDETACH:
519 file->private_data = 0;
520 if (atomic_dec_and_test(&pf->refcnt))
521 ppp_destroy_interface(ppp);
522 err = 0;
523 break;
525 case PPPIOCSMRU:
526 if (get_user(val, (int *) arg))
527 break;
528 ppp->mru = val;
529 err = 0;
530 break;
532 case PPPIOCSFLAGS:
533 if (get_user(val, (int *) arg))
534 break;
535 ppp_lock(ppp);
536 if (ppp->flags & ~val & SC_CCP_OPEN)
537 ppp_ccp_closed(ppp);
538 ppp->flags = val & SC_FLAG_BITS;
539 ppp_unlock(ppp);
540 err = 0;
541 break;
543 case PPPIOCGFLAGS:
544 val = ppp->flags | ppp->xstate | ppp->rstate;
545 if (put_user(val, (int *) arg))
546 break;
547 err = 0;
548 break;
550 case PPPIOCSCOMPRESS:
551 err = ppp_set_compress(ppp, arg);
552 break;
554 case PPPIOCGUNIT:
555 if (put_user(ppp->file.index, (int *) arg))
556 break;
557 err = 0;
558 break;
560 case PPPIOCSDEBUG:
561 if (get_user(val, (int *) arg))
562 break;
563 ppp->debug = val;
564 err = 0;
565 break;
567 case PPPIOCGDEBUG:
568 if (put_user(ppp->debug, (int *) arg))
569 break;
570 err = 0;
571 break;
573 case PPPIOCGIDLE:
574 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
575 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
576 if (copy_to_user((void *) arg, &idle, sizeof(idle)))
577 break;
578 err = 0;
579 break;
581 case PPPIOCSMAXCID:
582 if (get_user(val, (int *) arg))
583 break;
584 val2 = 15;
585 if ((val >> 16) != 0) {
586 val2 = val >> 16;
587 val &= 0xffff;
589 vj = slhc_init(val2+1, val+1);
590 if (vj == 0) {
591 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
592 err = -ENOMEM;
593 break;
595 ppp_lock(ppp);
596 if (ppp->vj != 0)
597 slhc_free(ppp->vj);
598 ppp->vj = vj;
599 ppp_unlock(ppp);
600 err = 0;
601 break;
603 case PPPIOCGNPMODE:
604 case PPPIOCSNPMODE:
605 if (copy_from_user(&npi, (void *) arg, sizeof(npi)))
606 break;
607 err = proto_to_npindex(npi.protocol);
608 if (err < 0)
609 break;
610 i = err;
611 if (cmd == PPPIOCGNPMODE) {
612 err = -EFAULT;
613 npi.mode = ppp->npmode[i];
614 if (copy_to_user((void *) arg, &npi, sizeof(npi)))
615 break;
616 } else {
617 ppp->npmode[i] = npi.mode;
618 /* we may be able to transmit more packets now (??) */
619 netif_wake_queue(ppp->dev);
621 err = 0;
622 break;
624 #ifdef CONFIG_PPP_MULTILINK
625 case PPPIOCSMRRU:
626 if (get_user(val, (int *) arg))
627 break;
628 ppp_recv_lock(ppp);
629 ppp->mrru = val;
630 ppp_recv_unlock(ppp);
631 err = 0;
632 break;
633 #endif /* CONFIG_PPP_MULTILINK */
635 default:
636 err = -ENOTTY;
638 return err;
641 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
642 unsigned int cmd, unsigned long arg)
644 int unit, err = -EFAULT;
645 struct ppp *ppp;
646 struct channel *chan;
648 switch (cmd) {
649 case PPPIOCNEWUNIT:
650 /* Create a new ppp unit */
651 if (get_user(unit, (int *) arg))
652 break;
653 ppp = ppp_create_interface(unit, &err);
654 if (ppp == 0)
655 break;
656 file->private_data = &ppp->file;
657 err = -EFAULT;
658 if (put_user(ppp->file.index, (int *) arg))
659 break;
660 err = 0;
661 break;
663 case PPPIOCATTACH:
664 /* Attach to an existing ppp unit */
665 if (get_user(unit, (int *) arg))
666 break;
667 spin_lock(&all_ppp_lock);
668 ppp = ppp_find_unit(unit);
669 if (ppp != 0)
670 atomic_inc(&ppp->file.refcnt);
671 spin_unlock(&all_ppp_lock);
672 err = -ENXIO;
673 if (ppp == 0)
674 break;
675 file->private_data = &ppp->file;
676 err = 0;
677 break;
679 case PPPIOCATTCHAN:
680 if (get_user(unit, (int *) arg))
681 break;
682 spin_lock_bh(&all_channels_lock);
683 chan = ppp_find_channel(unit);
684 if (chan != 0)
685 atomic_inc(&chan->file.refcnt);
686 spin_unlock_bh(&all_channels_lock);
687 err = -ENXIO;
688 if (chan == 0)
689 break;
690 file->private_data = &chan->file;
691 err = 0;
692 break;
694 default:
695 err = -ENOTTY;
697 return err;
700 static struct file_operations ppp_device_fops = {
701 owner: THIS_MODULE,
702 read: ppp_read,
703 write: ppp_write,
704 poll: ppp_poll,
705 ioctl: ppp_ioctl,
706 open: ppp_open,
707 release: ppp_release
710 #define PPP_MAJOR 108
712 static devfs_handle_t devfs_handle;
714 /* Called at boot time if ppp is compiled into the kernel,
715 or at module load time (from init_module) if compiled as a module. */
716 int __init ppp_init(void)
718 int err;
720 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
721 err = devfs_register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
722 if (err)
723 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
724 devfs_handle = devfs_register(NULL, "ppp", DEVFS_FL_DEFAULT,
725 PPP_MAJOR, 0,
726 S_IFCHR | S_IRUSR | S_IWUSR,
727 &ppp_device_fops, NULL);
729 return 0;
733 * Network interface unit routines.
735 static int
736 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
738 struct ppp *ppp = (struct ppp *) dev->priv;
739 int npi, proto;
740 unsigned char *pp;
742 npi = ethertype_to_npindex(ntohs(skb->protocol));
743 if (npi < 0)
744 goto outf;
746 /* Drop, accept or reject the packet */
747 switch (ppp->npmode[npi]) {
748 case NPMODE_PASS:
749 break;
750 case NPMODE_QUEUE:
751 /* it would be nice to have a way to tell the network
752 system to queue this one up for later. */
753 goto outf;
754 case NPMODE_DROP:
755 case NPMODE_ERROR:
756 goto outf;
759 /* Put the 2-byte PPP protocol number on the front,
760 making sure there is room for the address and control fields. */
761 if (skb_headroom(skb) < PPP_HDRLEN) {
762 struct sk_buff *ns;
764 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
765 if (ns == 0)
766 goto outf;
767 skb_reserve(ns, dev->hard_header_len);
768 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
769 kfree_skb(skb);
770 skb = ns;
772 pp = skb_push(skb, 2);
773 proto = npindex_to_proto[npi];
774 pp[0] = proto >> 8;
775 pp[1] = proto;
777 netif_stop_queue(dev);
778 skb_queue_tail(&ppp->file.xq, skb);
779 ppp_xmit_process(ppp);
780 return 0;
782 outf:
783 kfree_skb(skb);
784 ++ppp->stats.tx_dropped;
785 return 0;
788 static struct net_device_stats *
789 ppp_net_stats(struct net_device *dev)
791 struct ppp *ppp = (struct ppp *) dev->priv;
793 return &ppp->stats;
796 static int
797 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
799 struct ppp *ppp = dev->priv;
800 int err = -EFAULT;
801 void *addr = (void *) ifr->ifr_ifru.ifru_data;
802 struct ppp_stats stats;
803 struct ppp_comp_stats cstats;
804 char *vers;
806 switch (cmd) {
807 case SIOCGPPPSTATS:
808 ppp_get_stats(ppp, &stats);
809 if (copy_to_user(addr, &stats, sizeof(stats)))
810 break;
811 err = 0;
812 break;
814 case SIOCGPPPCSTATS:
815 memset(&cstats, 0, sizeof(cstats));
816 if (ppp->xc_state != 0)
817 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
818 if (ppp->rc_state != 0)
819 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
820 if (copy_to_user(addr, &cstats, sizeof(cstats)))
821 break;
822 err = 0;
823 break;
825 case SIOCGPPPVER:
826 vers = PPP_VERSION;
827 if (copy_to_user(addr, vers, strlen(vers) + 1))
828 break;
829 err = 0;
830 break;
832 default:
833 err = -EINVAL;
836 return err;
840 ppp_net_init(struct net_device *dev)
842 dev->hard_header_len = PPP_HDRLEN;
843 dev->mtu = PPP_MTU;
844 dev->hard_start_xmit = ppp_start_xmit;
845 dev->get_stats = ppp_net_stats;
846 dev->do_ioctl = ppp_net_ioctl;
847 dev->addr_len = 0;
848 dev->tx_queue_len = 3;
849 dev->type = ARPHRD_PPP;
850 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
852 dev_init_buffers(dev);
853 return 0;
857 * Transmit-side routines.
861 * Called to do any work queued up on the transmit side
862 * that can now be done.
864 static void
865 ppp_xmit_process(struct ppp *ppp)
867 struct sk_buff *skb;
869 ppp_xmit_lock(ppp);
870 ppp_push(ppp);
871 while (ppp->xmit_pending == 0
872 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
873 ppp_send_frame(ppp, skb);
874 /* If there's no work left to do, tell the core net
875 code that we can accept some more. */
876 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0
877 && ppp->dev != 0)
878 netif_wake_queue(ppp->dev);
879 ppp_xmit_unlock(ppp);
883 * Compress and send a frame.
884 * The caller should have locked the xmit path,
885 * and xmit_pending should be 0.
887 static void
888 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
890 int proto = PPP_PROTO(skb);
891 struct sk_buff *new_skb;
892 int len;
893 unsigned char *cp;
895 ++ppp->stats.tx_packets;
896 ppp->stats.tx_bytes += skb->len - 2;
898 switch (proto) {
899 case PPP_IP:
900 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
901 break;
902 /* try to do VJ TCP header compression */
903 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
904 GFP_ATOMIC);
905 if (new_skb == 0) {
906 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
907 goto drop;
909 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
910 cp = skb->data + 2;
911 len = slhc_compress(ppp->vj, cp, skb->len - 2,
912 new_skb->data + 2, &cp,
913 !(ppp->flags & SC_NO_TCP_CCID));
914 if (cp == skb->data + 2) {
915 /* didn't compress */
916 kfree_skb(new_skb);
917 } else {
918 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
919 proto = PPP_VJC_COMP;
920 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
921 } else {
922 proto = PPP_VJC_UNCOMP;
923 cp[0] = skb->data[2];
925 kfree_skb(skb);
926 skb = new_skb;
927 cp = skb_put(skb, len + 2);
928 cp[0] = 0;
929 cp[1] = proto;
931 break;
933 case PPP_CCP:
934 /* peek at outbound CCP frames */
935 ppp_ccp_peek(ppp, skb, 0);
936 break;
939 /* try to do packet compression */
940 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
941 && proto != PPP_LCP && proto != PPP_CCP) {
942 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
943 GFP_ATOMIC);
944 if (new_skb == 0) {
945 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
946 goto drop;
948 if (ppp->dev->hard_header_len > PPP_HDRLEN)
949 skb_reserve(new_skb,
950 ppp->dev->hard_header_len - PPP_HDRLEN);
952 /* compressor still expects A/C bytes in hdr */
953 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
954 new_skb->data, skb->len + 2,
955 ppp->dev->mtu + PPP_HDRLEN);
956 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
957 kfree_skb(skb);
958 skb = new_skb;
959 skb_put(skb, len);
960 skb_pull(skb, 2); /* pull off A/C bytes */
961 } else {
962 /* didn't compress, or CCP not up yet */
963 kfree_skb(new_skb);
967 /* for data packets, record the time */
968 if (proto < 0x8000)
969 ppp->last_xmit = jiffies;
972 * If we are waiting for traffic (demand dialling),
973 * queue it up for pppd to receive.
975 if (ppp->flags & SC_LOOP_TRAFFIC) {
976 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
977 goto drop;
978 skb_queue_tail(&ppp->file.rq, skb);
979 wake_up_interruptible(&ppp->file.rwait);
980 return;
983 ppp->xmit_pending = skb;
984 ppp_push(ppp);
985 return;
987 drop:
988 kfree_skb(skb);
989 ++ppp->stats.tx_errors;
993 * Try to send the frame in xmit_pending.
994 * The caller should have the xmit path locked.
996 static void
997 ppp_push(struct ppp *ppp)
999 struct list_head *list;
1000 struct channel *pch;
1001 struct sk_buff *skb = ppp->xmit_pending;
1003 if (skb == 0)
1004 return;
1006 list = &ppp->channels;
1007 if (list_empty(list)) {
1008 /* nowhere to send the packet, just drop it */
1009 ppp->xmit_pending = 0;
1010 kfree_skb(skb);
1011 return;
1014 if ((ppp->flags & SC_MULTILINK) == 0) {
1015 /* not doing multilink: send it down the first channel */
1016 list = list->next;
1017 pch = list_entry(list, struct channel, clist);
1019 spin_lock_bh(&pch->downl);
1020 if (pch->chan) {
1021 if (pch->chan->ops->start_xmit(pch->chan, skb))
1022 ppp->xmit_pending = 0;
1023 } else {
1024 /* channel got unregistered */
1025 kfree_skb(skb);
1026 ppp->xmit_pending = 0;
1028 spin_unlock_bh(&pch->downl);
1029 return;
1032 #ifdef CONFIG_PPP_MULTILINK
1033 /* Multilink: fragment the packet over as many links
1034 as can take the packet at the moment. */
1035 if (!ppp_mp_explode(ppp, skb))
1036 return;
1037 #endif /* CONFIG_PPP_MULTILINK */
1039 ppp->xmit_pending = 0;
1040 kfree_skb(skb);
1043 #ifdef CONFIG_PPP_MULTILINK
1045 * Divide a packet to be transmitted into fragments and
1046 * send them out the individual links.
1048 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1050 int nch, len, fragsize;
1051 int i, bits, hdrlen, mtu;
1052 int flen, fnb;
1053 unsigned char *p, *q;
1054 struct list_head *list;
1055 struct channel *pch;
1056 struct sk_buff *frag;
1057 struct ppp_channel *chan;
1059 nch = 0;
1060 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1061 list = &ppp->channels;
1062 while ((list = list->next) != &ppp->channels) {
1063 pch = list_entry(list, struct channel, clist);
1064 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1066 * If a channel hasn't had a fragment yet, it has to get
1067 * one before we send any fragments on later channels.
1068 * If it can't take a fragment now, don't give any
1069 * to subsequent channels.
1071 if (!pch->had_frag && !pch->avail) {
1072 while ((list = list->next) != &ppp->channels) {
1073 pch = list_entry(list, struct channel, clist);
1074 pch->avail = 0;
1076 break;
1079 if (nch == 0)
1080 return 0; /* can't take now, leave it in xmit_pending */
1082 /* Do protocol field compression (XXX this should be optional) */
1083 p = skb->data;
1084 len = skb->len;
1085 if (*p == 0) {
1086 ++p;
1087 --len;
1090 /* decide on fragment size */
1091 fragsize = len;
1092 if (nch > 1) {
1093 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1094 if (nch > maxch)
1095 nch = maxch;
1096 fragsize = ROUNDUP(fragsize, nch);
1099 /* skip to the channel after the one we last used
1100 and start at that one */
1101 for (i = 0; i < ppp->nxchan; ++i) {
1102 list = list->next;
1103 if (list == &ppp->channels) {
1104 i = 0;
1105 break;
1109 /* create a fragment for each channel */
1110 bits = B;
1111 do {
1112 list = list->next;
1113 if (list == &ppp->channels) {
1114 i = 0;
1115 continue;
1117 pch = list_entry(list, struct channel, clist);
1118 ++i;
1119 if (!pch->avail)
1120 continue;
1122 /* check the channel's mtu and whether it is still attached. */
1123 spin_lock_bh(&pch->downl);
1124 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1125 /* can't use this channel */
1126 spin_unlock_bh(&pch->downl);
1127 pch->avail = 0;
1128 if (--nch == 0)
1129 break;
1130 continue;
1134 * We have to create multiple fragments for this channel
1135 * if fragsize is greater than the channel's mtu.
1137 if (fragsize > len)
1138 fragsize = len;
1139 for (flen = fragsize; flen > 0; flen -= fnb) {
1140 fnb = flen;
1141 if (fnb > mtu + 2 - hdrlen)
1142 fnb = mtu + 2 - hdrlen;
1143 if (fnb >= len)
1144 bits |= E;
1145 frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1146 if (frag == 0)
1147 goto noskb;
1148 q = skb_put(frag, fnb + hdrlen);
1149 /* make the MP header */
1150 q[0] = PPP_MP >> 8;
1151 q[1] = PPP_MP;
1152 if (ppp->flags & SC_MP_XSHORTSEQ) {
1153 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1154 q[3] = ppp->nxseq;
1155 } else {
1156 q[2] = bits;
1157 q[3] = ppp->nxseq >> 16;
1158 q[4] = ppp->nxseq >> 8;
1159 q[5] = ppp->nxseq;
1162 /* copy the data in */
1163 memcpy(q + hdrlen, p, fnb);
1165 /* try to send it down the channel */
1166 chan = pch->chan;
1167 if (!chan->ops->start_xmit(chan, frag))
1168 skb_queue_tail(&pch->file.xq, frag);
1169 pch->had_frag = 1;
1170 p += fnb;
1171 len -= fnb;
1172 ++ppp->nxseq;
1173 bits = 0;
1175 spin_unlock_bh(&pch->downl);
1176 } while (len > 0);
1177 ppp->nxchan = i;
1179 return 1;
1181 noskb:
1182 spin_unlock_bh(&pch->downl);
1183 if (ppp->debug & 1)
1184 printk(KERN_ERR "PPP: no memory (fragment)\n");
1185 ++ppp->stats.tx_errors;
1186 ++ppp->nxseq;
1187 return 1; /* abandon the frame */
1189 #endif /* CONFIG_PPP_MULTILINK */
1192 * Try to send data out on a channel.
1194 static void
1195 ppp_channel_push(struct channel *pch)
1197 struct sk_buff *skb;
1198 struct ppp *ppp;
1200 spin_lock_bh(&pch->downl);
1201 if (pch->chan != 0) {
1202 while (skb_queue_len(&pch->file.xq) > 0) {
1203 skb = skb_dequeue(&pch->file.xq);
1204 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1205 /* put the packet back and try again later */
1206 skb_queue_head(&pch->file.xq, skb);
1207 break;
1210 } else {
1211 /* channel got deregistered */
1212 skb_queue_purge(&pch->file.xq);
1214 spin_unlock_bh(&pch->downl);
1215 /* see if there is anything from the attached unit to be sent */
1216 if (skb_queue_len(&pch->file.xq) == 0) {
1217 read_lock_bh(&pch->upl);
1218 ppp = pch->ppp;
1219 if (ppp != 0)
1220 ppp_xmit_process(ppp);
1221 read_unlock_bh(&pch->upl);
1226 * Receive-side routines.
1229 /* misuse a few fields of the skb for MP reconstruction */
1230 #define sequence priority
1231 #define BEbits cb[0]
1233 static inline void
1234 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1236 ppp_recv_lock(ppp);
1237 /* ppp->dev == 0 means interface is closing down */
1238 if (ppp->dev != 0)
1239 ppp_receive_frame(ppp, skb, pch);
1240 else
1241 kfree_skb(skb);
1242 ppp_recv_unlock(ppp);
1245 void
1246 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1248 struct channel *pch = chan->ppp;
1249 int proto;
1251 if (pch == 0 || skb->len == 0) {
1252 kfree_skb(skb);
1253 return;
1256 proto = PPP_PROTO(skb);
1257 read_lock_bh(&pch->upl);
1258 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1259 /* put it on the channel queue */
1260 skb_queue_tail(&pch->file.rq, skb);
1261 /* drop old frames if queue too long */
1262 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1263 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1264 kfree_skb(skb);
1265 wake_up_interruptible(&pch->file.rwait);
1266 } else {
1267 ppp_do_recv(pch->ppp, skb, pch);
1269 read_unlock_bh(&pch->upl);
1272 /* Put a 0-length skb in the receive queue as an error indication */
1273 void
1274 ppp_input_error(struct ppp_channel *chan, int code)
1276 struct channel *pch = chan->ppp;
1277 struct sk_buff *skb;
1279 if (pch == 0)
1280 return;
1282 read_lock_bh(&pch->upl);
1283 if (pch->ppp != 0) {
1284 skb = alloc_skb(0, GFP_ATOMIC);
1285 if (skb != 0) {
1286 skb->len = 0; /* probably unnecessary */
1287 skb->cb[0] = code;
1288 ppp_do_recv(pch->ppp, skb, pch);
1291 read_unlock_bh(&pch->upl);
1295 * We come in here to process a received frame.
1296 * The receive side of the ppp unit is locked.
1298 static void
1299 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1301 if (skb->len >= 2) {
1302 #ifdef CONFIG_PPP_MULTILINK
1303 /* XXX do channel-level decompression here */
1304 if (PPP_PROTO(skb) == PPP_MP)
1305 ppp_receive_mp_frame(ppp, skb, pch);
1306 else
1307 #endif /* CONFIG_PPP_MULTILINK */
1308 ppp_receive_nonmp_frame(ppp, skb);
1309 return;
1312 if (skb->len > 0)
1313 /* note: a 0-length skb is used as an error indication */
1314 ++ppp->stats.rx_length_errors;
1316 kfree_skb(skb);
1317 ppp_receive_error(ppp);
1320 static void
1321 ppp_receive_error(struct ppp *ppp)
1323 ++ppp->stats.rx_errors;
1324 if (ppp->vj != 0)
1325 slhc_toss(ppp->vj);
1328 static void
1329 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1331 struct sk_buff *ns;
1332 int proto, len, npi;
1335 * Decompress the frame, if compressed.
1336 * Note that some decompressors need to see uncompressed frames
1337 * that come in as well as compressed frames.
1339 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1340 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1341 skb = ppp_decompress_frame(ppp, skb);
1343 proto = PPP_PROTO(skb);
1344 switch (proto) {
1345 case PPP_VJC_COMP:
1346 /* decompress VJ compressed packets */
1347 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1348 goto err;
1349 if (skb_tailroom(skb) < 124) {
1350 /* copy to a new sk_buff with more tailroom */
1351 ns = dev_alloc_skb(skb->len + 128);
1352 if (ns == 0) {
1353 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1354 goto err;
1356 skb_reserve(ns, 2);
1357 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
1358 kfree_skb(skb);
1359 skb = ns;
1361 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1362 if (len <= 0) {
1363 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1364 goto err;
1366 len += 2;
1367 if (len > skb->len)
1368 skb_put(skb, len - skb->len);
1369 else if (len < skb->len)
1370 skb_trim(skb, len);
1371 proto = PPP_IP;
1372 break;
1374 case PPP_VJC_UNCOMP:
1375 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1376 goto err;
1377 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1378 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1379 goto err;
1381 proto = PPP_IP;
1382 break;
1384 case PPP_CCP:
1385 ppp_ccp_peek(ppp, skb, 1);
1386 break;
1389 ++ppp->stats.rx_packets;
1390 ppp->stats.rx_bytes += skb->len - 2;
1392 npi = proto_to_npindex(proto);
1393 if (npi < 0) {
1394 /* control or unknown frame - pass it to pppd */
1395 skb_queue_tail(&ppp->file.rq, skb);
1396 /* limit queue length by dropping old frames */
1397 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1398 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1399 kfree_skb(skb);
1400 /* wake up any process polling or blocking on read */
1401 wake_up_interruptible(&ppp->file.rwait);
1403 } else {
1404 /* network protocol frame - give it to the kernel */
1405 ppp->last_recv = jiffies;
1406 if ((ppp->dev->flags & IFF_UP) == 0
1407 || ppp->npmode[npi] != NPMODE_PASS) {
1408 kfree_skb(skb);
1409 } else {
1410 skb_pull(skb, 2); /* chop off protocol */
1411 skb->dev = ppp->dev;
1412 skb->protocol = htons(npindex_to_ethertype[npi]);
1413 skb->mac.raw = skb->data;
1414 netif_rx(skb);
1417 return;
1419 err:
1420 kfree_skb(skb);
1421 ppp_receive_error(ppp);
1424 static struct sk_buff *
1425 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1427 int proto = PPP_PROTO(skb);
1428 struct sk_buff *ns;
1429 int len;
1431 if (proto == PPP_COMP) {
1432 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1433 if (ns == 0) {
1434 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1435 goto err;
1437 /* the decompressor still expects the A/C bytes in the hdr */
1438 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1439 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1440 if (len < 0) {
1441 /* Pass the compressed frame to pppd as an
1442 error indication. */
1443 if (len == DECOMP_FATALERROR)
1444 ppp->rstate |= SC_DC_FERROR;
1445 goto err;
1448 kfree_skb(skb);
1449 skb = ns;
1450 skb_put(skb, len);
1451 skb_pull(skb, 2); /* pull off the A/C bytes */
1453 } else {
1454 /* Uncompressed frame - pass to decompressor so it
1455 can update its dictionary if necessary. */
1456 if (ppp->rcomp->incomp)
1457 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1458 skb->len + 2);
1461 return skb;
1463 err:
1464 ppp->rstate |= SC_DC_ERROR;
1465 ppp_receive_error(ppp);
1466 return skb;
1469 #ifdef CONFIG_PPP_MULTILINK
1471 * Receive a multilink frame.
1472 * We put it on the reconstruction queue and then pull off
1473 * as many completed frames as we can.
1475 static void
1476 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1478 u32 mask, seq;
1479 struct list_head *l;
1480 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1482 if (skb->len < mphdrlen + 1 || ppp->mrru == 0)
1483 goto err; /* no good, throw it away */
1485 /* Decode sequence number and begin/end bits */
1486 if (ppp->flags & SC_MP_SHORTSEQ) {
1487 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1488 mask = 0xfff;
1489 } else {
1490 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1491 mask = 0xffffff;
1493 skb->BEbits = skb->data[2];
1494 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1497 * Do protocol ID decompression on the first fragment of each packet.
1499 if ((skb->BEbits & B) && (skb->data[0] & 1))
1500 *skb_push(skb, 1) = 0;
1503 * Expand sequence number to 32 bits, making it as close
1504 * as possible to ppp->minseq.
1506 seq |= ppp->minseq & ~mask;
1507 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1508 seq += mask + 1;
1509 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1510 seq -= mask + 1; /* should never happen */
1511 skb->sequence = seq;
1512 pch->lastseq = seq;
1515 * If this packet comes before the next one we were expecting,
1516 * drop it.
1518 if (seq_before(seq, ppp->nextseq)) {
1519 kfree_skb(skb);
1520 ++ppp->stats.rx_dropped;
1521 ppp_receive_error(ppp);
1522 return;
1526 * Reevaluate minseq, the minimum over all channels of the
1527 * last sequence number received on each channel. Because of
1528 * the increasing sequence number rule, we know that any fragment
1529 * before `minseq' which hasn't arrived is never going to arrive.
1530 * The list of channels can't change because we have the receive
1531 * side of the ppp unit locked.
1533 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1534 struct channel *ch = list_entry(l, struct channel, clist);
1535 if (seq_before(ch->lastseq, seq))
1536 seq = ch->lastseq;
1538 if (seq_before(ppp->minseq, seq))
1539 ppp->minseq = seq;
1541 /* Put the fragment on the reconstruction queue */
1542 ppp_mp_insert(ppp, skb);
1544 /* If the queue is getting long, don't wait any longer for packets
1545 before the start of the queue. */
1546 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1547 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1548 ppp->minseq = ppp->mrq.next->sequence;
1550 /* Pull completed packets off the queue and receive them. */
1551 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1552 ppp_receive_nonmp_frame(ppp, skb);
1554 return;
1556 err:
1557 kfree_skb(skb);
1558 ppp_receive_error(ppp);
1562 * Insert a fragment on the MP reconstruction queue.
1563 * The queue is ordered by increasing sequence number.
1565 static void
1566 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1568 struct sk_buff *p;
1569 struct sk_buff_head *list = &ppp->mrq;
1570 u32 seq = skb->sequence;
1572 /* N.B. we don't need to lock the list lock because we have the
1573 ppp unit receive-side lock. */
1574 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1575 if (seq_before(seq, p->sequence))
1576 break;
1577 __skb_insert(skb, p->prev, p, list);
1581 * Reconstruct a packet from the MP fragment queue.
1582 * We go through increasing sequence numbers until we find a
1583 * complete packet, or we get to the sequence number for a fragment
1584 * which hasn't arrived but might still do so.
1586 struct sk_buff *
1587 ppp_mp_reconstruct(struct ppp *ppp)
1589 u32 seq = ppp->nextseq;
1590 u32 minseq = ppp->minseq;
1591 struct sk_buff_head *list = &ppp->mrq;
1592 struct sk_buff *p, *next;
1593 struct sk_buff *head, *tail;
1594 struct sk_buff *skb = NULL;
1595 int lost = 0, len = 0;
1597 if (ppp->mrru == 0) /* do nothing until mrru is set */
1598 return NULL;
1599 head = list->next;
1600 tail = NULL;
1601 for (p = head; p != (struct sk_buff *) list; p = next) {
1602 next = p->next;
1603 if (seq_before(p->sequence, seq)) {
1604 /* this can't happen, anyway ignore the skb */
1605 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1606 p->sequence, seq);
1607 head = next;
1608 continue;
1610 if (p->sequence != seq) {
1611 /* Fragment `seq' is missing. If it is after
1612 minseq, it might arrive later, so stop here. */
1613 if (seq_after(seq, minseq))
1614 break;
1615 /* Fragment `seq' is lost, keep going. */
1616 lost = 1;
1617 seq = seq_before(minseq, p->sequence)?
1618 minseq + 1: p->sequence;
1619 next = p;
1620 continue;
1624 * At this point we know that all the fragments from
1625 * ppp->nextseq to seq are either present or lost.
1626 * Also, there are no complete packets in the queue
1627 * that have no missing fragments and end before this
1628 * fragment.
1631 /* B bit set indicates this fragment starts a packet */
1632 if (p->BEbits & B) {
1633 head = p;
1634 lost = 0;
1635 len = 0;
1638 len += p->len;
1640 /* Got a complete packet yet? */
1641 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1642 if (len > ppp->mrru + 2) {
1643 ++ppp->stats.rx_length_errors;
1644 printk(KERN_DEBUG "PPP: reconstructed packet"
1645 " is too long (%d)\n", len);
1646 } else if (p == head) {
1647 /* fragment is complete packet - reuse skb */
1648 tail = p;
1649 skb = skb_get(p);
1650 break;
1651 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1652 ++ppp->stats.rx_missed_errors;
1653 printk(KERN_DEBUG "PPP: no memory for "
1654 "reconstructed packet");
1655 } else {
1656 tail = p;
1657 break;
1659 ppp->nextseq = seq + 1;
1663 * If this is the ending fragment of a packet,
1664 * and we haven't found a complete valid packet yet,
1665 * we can discard up to and including this fragment.
1667 if (p->BEbits & E)
1668 head = next;
1670 ++seq;
1673 /* If we have a complete packet, copy it all into one skb. */
1674 if (tail != NULL) {
1675 /* If we have discarded any fragments,
1676 signal a receive error. */
1677 if (head->sequence != ppp->nextseq) {
1678 if (ppp->debug & 1)
1679 printk(KERN_DEBUG " missed pkts %u..%u\n",
1680 ppp->nextseq, head->sequence-1);
1681 ++ppp->stats.rx_dropped;
1682 ppp_receive_error(ppp);
1685 if (head != tail)
1686 /* copy to a single skb */
1687 for (p = head; p != tail->next; p = p->next)
1688 memcpy(skb_put(skb, p->len), p->data, p->len);
1689 ppp->nextseq = tail->sequence + 1;
1690 head = tail->next;
1693 /* Discard all the skbuffs that we have copied the data out of
1694 or that we can't use. */
1695 while ((p = list->next) != head) {
1696 __skb_unlink(p, list);
1697 kfree_skb(p);
1700 return skb;
1702 #endif /* CONFIG_PPP_MULTILINK */
1705 * Channel interface.
1709 * Create a new, unattached ppp channel.
1712 ppp_register_channel(struct ppp_channel *chan)
1714 struct channel *pch;
1716 pch = kmalloc(sizeof(struct channel), GFP_ATOMIC);
1717 if (pch == 0)
1718 return -ENOMEM;
1719 memset(pch, 0, sizeof(struct channel));
1720 pch->ppp = NULL;
1721 pch->chan = chan;
1722 chan->ppp = pch;
1723 init_ppp_file(&pch->file, CHANNEL);
1724 pch->file.hdrlen = chan->hdrlen;
1725 #ifdef CONFIG_PPP_MULTILINK
1726 pch->lastseq = -1;
1727 #endif /* CONFIG_PPP_MULTILINK */
1728 spin_lock_init(&pch->downl);
1729 pch->upl = RW_LOCK_UNLOCKED;
1730 spin_lock_bh(&all_channels_lock);
1731 pch->file.index = ++last_channel_index;
1732 list_add(&pch->file.list, &all_channels);
1733 spin_unlock_bh(&all_channels_lock);
1734 MOD_INC_USE_COUNT;
1735 return 0;
1739 * Return the index of a channel.
1741 int ppp_channel_index(struct ppp_channel *chan)
1743 struct channel *pch = chan->ppp;
1745 return pch->file.index;
1749 * Return the PPP unit number to which a channel is connected.
1751 int ppp_unit_number(struct ppp_channel *chan)
1753 struct channel *pch = chan->ppp;
1754 int unit = -1;
1756 if (pch != 0) {
1757 read_lock_bh(&pch->upl);
1758 if (pch->ppp != 0)
1759 unit = pch->ppp->file.index;
1760 read_unlock_bh(&pch->upl);
1762 return unit;
1766 * Disconnect a channel from the generic layer.
1767 * This can be called from mainline or BH/softirq level.
1769 void
1770 ppp_unregister_channel(struct ppp_channel *chan)
1772 struct channel *pch = chan->ppp;
1774 if (pch == 0)
1775 return; /* should never happen */
1776 chan->ppp = 0;
1779 * This ensures that we have returned from any calls into the
1780 * the channel's start_xmit or ioctl routine before we proceed.
1782 spin_lock_bh(&pch->downl);
1783 pch->chan = 0;
1784 spin_unlock_bh(&pch->downl);
1785 ppp_disconnect_channel(pch);
1786 wake_up_interruptible(&pch->file.rwait);
1787 spin_lock_bh(&all_channels_lock);
1788 list_del(&pch->file.list);
1789 spin_unlock_bh(&all_channels_lock);
1790 if (atomic_dec_and_test(&pch->file.refcnt))
1791 ppp_destroy_channel(pch);
1792 MOD_DEC_USE_COUNT;
1796 * Callback from a channel when it can accept more to transmit.
1797 * This should be called at BH/softirq level, not interrupt level.
1799 void
1800 ppp_output_wakeup(struct ppp_channel *chan)
1802 struct channel *pch = chan->ppp;
1804 if (pch == 0)
1805 return;
1806 ppp_channel_push(pch);
1810 * This is basically temporary compatibility stuff.
1812 ssize_t
1813 ppp_channel_read(struct ppp_channel *chan, struct file *file,
1814 char *buf, size_t count)
1816 struct channel *pch = chan->ppp;
1818 if (pch == 0)
1819 return -ENXIO;
1820 return ppp_file_read(&pch->file, file, buf, count);
1823 ssize_t
1824 ppp_channel_write(struct ppp_channel *chan, const char *buf, size_t count)
1826 struct channel *pch = chan->ppp;
1828 if (pch == 0)
1829 return -ENXIO;
1830 return ppp_file_write(&pch->file, buf, count);
1833 /* No kernel lock - fine */
1834 unsigned int
1835 ppp_channel_poll(struct ppp_channel *chan, struct file *file, poll_table *wait)
1837 unsigned int mask;
1838 struct channel *pch = chan->ppp;
1840 mask = POLLOUT | POLLWRNORM;
1841 if (pch != 0) {
1842 poll_wait(file, &pch->file.rwait, wait);
1843 if (skb_peek(&pch->file.rq) != 0)
1844 mask |= POLLIN | POLLRDNORM;
1846 return mask;
1849 int ppp_channel_ioctl(struct ppp_channel *chan, unsigned int cmd,
1850 unsigned long arg)
1852 struct channel *pch = chan->ppp;
1853 int err = -ENOTTY;
1854 int unit;
1856 if (!capable(CAP_NET_ADMIN))
1857 return -EPERM;
1858 if (pch == 0)
1859 return -EINVAL;
1860 switch (cmd) {
1861 case PPPIOCATTACH:
1862 if (get_user(unit, (int *) arg))
1863 break;
1864 err = ppp_connect_channel(pch, unit);
1865 break;
1866 case PPPIOCDETACH:
1867 err = ppp_disconnect_channel(pch);
1868 break;
1870 return err;
1874 * Compression control.
1877 /* Process the PPPIOCSCOMPRESS ioctl. */
1878 static int
1879 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1881 int err;
1882 struct compressor *cp;
1883 struct ppp_option_data data;
1884 void *state;
1885 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1886 #ifdef CONFIG_KMOD
1887 char modname[32];
1888 #endif
1890 err = -EFAULT;
1891 if (copy_from_user(&data, (void *) arg, sizeof(data))
1892 || (data.length <= CCP_MAX_OPTION_LENGTH
1893 && copy_from_user(ccp_option, data.ptr, data.length)))
1894 goto out;
1895 err = -EINVAL;
1896 if (data.length > CCP_MAX_OPTION_LENGTH
1897 || ccp_option[1] < 2 || ccp_option[1] > data.length)
1898 goto out;
1900 cp = find_compressor(ccp_option[0]);
1901 #ifdef CONFIG_KMOD
1902 if (cp == 0) {
1903 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
1904 request_module(modname);
1905 cp = find_compressor(ccp_option[0]);
1907 #endif /* CONFIG_KMOD */
1908 if (cp == 0)
1909 goto out;
1911 err = -ENOBUFS;
1912 if (data.transmit) {
1913 ppp_xmit_lock(ppp);
1914 ppp->xstate &= ~SC_COMP_RUN;
1915 if (ppp->xc_state != 0) {
1916 ppp->xcomp->comp_free(ppp->xc_state);
1917 ppp->xc_state = 0;
1919 ppp_xmit_unlock(ppp);
1921 state = cp->comp_alloc(ccp_option, data.length);
1922 if (state != 0) {
1923 ppp_xmit_lock(ppp);
1924 ppp->xcomp = cp;
1925 ppp->xc_state = state;
1926 ppp_xmit_unlock(ppp);
1927 err = 0;
1930 } else {
1931 ppp_recv_lock(ppp);
1932 ppp->rstate &= ~SC_DECOMP_RUN;
1933 if (ppp->rc_state != 0) {
1934 ppp->rcomp->decomp_free(ppp->rc_state);
1935 ppp->rc_state = 0;
1937 ppp_recv_unlock(ppp);
1939 state = cp->decomp_alloc(ccp_option, data.length);
1940 if (state != 0) {
1941 ppp_recv_lock(ppp);
1942 ppp->rcomp = cp;
1943 ppp->rc_state = state;
1944 ppp_recv_unlock(ppp);
1945 err = 0;
1949 out:
1950 return err;
1954 * Look at a CCP packet and update our state accordingly.
1955 * We assume the caller has the xmit or recv path locked.
1957 static void
1958 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
1960 unsigned char *dp = skb->data + 2;
1961 int len;
1963 if (skb->len < CCP_HDRLEN + 2
1964 || skb->len < (len = CCP_LENGTH(dp)) + 2)
1965 return; /* too short */
1967 switch (CCP_CODE(dp)) {
1968 case CCP_CONFREQ:
1969 case CCP_TERMREQ:
1970 case CCP_TERMACK:
1972 * CCP is going down - disable compression.
1974 if (inbound)
1975 ppp->rstate &= ~SC_DECOMP_RUN;
1976 else
1977 ppp->xstate &= ~SC_COMP_RUN;
1978 break;
1980 case CCP_CONFACK:
1981 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
1982 break;
1983 dp += CCP_HDRLEN;
1984 len -= CCP_HDRLEN;
1985 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
1986 break;
1987 if (inbound) {
1988 /* we will start receiving compressed packets */
1989 if (ppp->rc_state == 0)
1990 break;
1991 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
1992 ppp->file.index, 0, ppp->mru, ppp->debug)) {
1993 ppp->rstate |= SC_DECOMP_RUN;
1994 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
1996 } else {
1997 /* we will soon start sending compressed packets */
1998 if (ppp->xc_state == 0)
1999 break;
2000 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2001 ppp->file.index, 0, ppp->debug))
2002 ppp->xstate |= SC_COMP_RUN;
2004 break;
2006 case CCP_RESETACK:
2007 /* reset the [de]compressor */
2008 if ((ppp->flags & SC_CCP_UP) == 0)
2009 break;
2010 if (inbound) {
2011 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2012 ppp->rcomp->decomp_reset(ppp->rc_state);
2013 ppp->rstate &= ~SC_DC_ERROR;
2015 } else {
2016 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2017 ppp->xcomp->comp_reset(ppp->xc_state);
2019 break;
2023 /* Free up compression resources. */
2024 static void
2025 ppp_ccp_closed(struct ppp *ppp)
2027 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2029 ppp->xstate &= ~SC_COMP_RUN;
2030 if (ppp->xc_state) {
2031 ppp->xcomp->comp_free(ppp->xc_state);
2032 ppp->xc_state = 0;
2035 ppp->xstate &= ~SC_DECOMP_RUN;
2036 if (ppp->rc_state) {
2037 ppp->rcomp->decomp_free(ppp->rc_state);
2038 ppp->rc_state = 0;
2042 /* List of compressors. */
2043 static LIST_HEAD(compressor_list);
2044 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2046 struct compressor_entry {
2047 struct list_head list;
2048 struct compressor *comp;
2051 static struct compressor_entry *
2052 find_comp_entry(int proto)
2054 struct compressor_entry *ce;
2055 struct list_head *list = &compressor_list;
2057 while ((list = list->next) != &compressor_list) {
2058 ce = list_entry(list, struct compressor_entry, list);
2059 if (ce->comp->compress_proto == proto)
2060 return ce;
2062 return 0;
2065 /* Register a compressor */
2067 ppp_register_compressor(struct compressor *cp)
2069 struct compressor_entry *ce;
2070 int ret;
2072 spin_lock(&compressor_list_lock);
2073 ret = -EEXIST;
2074 if (find_comp_entry(cp->compress_proto) != 0)
2075 goto out;
2076 ret = -ENOMEM;
2077 ce = kmalloc(sizeof(struct compressor_entry), GFP_KERNEL);
2078 if (ce == 0)
2079 goto out;
2080 ret = 0;
2081 ce->comp = cp;
2082 list_add(&ce->list, &compressor_list);
2083 out:
2084 spin_unlock(&compressor_list_lock);
2085 return ret;
2088 /* Unregister a compressor */
2089 void
2090 ppp_unregister_compressor(struct compressor *cp)
2092 struct compressor_entry *ce;
2094 spin_lock(&compressor_list_lock);
2095 ce = find_comp_entry(cp->compress_proto);
2096 if (ce != 0 && ce->comp == cp) {
2097 list_del(&ce->list);
2098 kfree(ce);
2100 spin_unlock(&compressor_list_lock);
2103 /* Find a compressor. */
2104 static struct compressor *
2105 find_compressor(int type)
2107 struct compressor_entry *ce;
2108 struct compressor *cp = 0;
2110 spin_lock(&compressor_list_lock);
2111 ce = find_comp_entry(type);
2112 if (ce != 0)
2113 cp = ce->comp;
2114 spin_unlock(&compressor_list_lock);
2115 return cp;
2119 * Miscelleneous stuff.
2122 static void
2123 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2125 struct slcompress *vj = ppp->vj;
2127 memset(st, 0, sizeof(*st));
2128 st->p.ppp_ipackets = ppp->stats.rx_packets;
2129 st->p.ppp_ierrors = ppp->stats.rx_errors;
2130 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2131 st->p.ppp_opackets = ppp->stats.tx_packets;
2132 st->p.ppp_oerrors = ppp->stats.tx_errors;
2133 st->p.ppp_obytes = ppp->stats.tx_bytes;
2134 if (vj == 0)
2135 return;
2136 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2137 st->vj.vjs_compressed = vj->sls_o_compressed;
2138 st->vj.vjs_searches = vj->sls_o_searches;
2139 st->vj.vjs_misses = vj->sls_o_misses;
2140 st->vj.vjs_errorin = vj->sls_i_error;
2141 st->vj.vjs_tossed = vj->sls_i_tossed;
2142 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2143 st->vj.vjs_compressedin = vj->sls_i_compressed;
2147 * Stuff for handling the lists of ppp units and channels
2148 * and for initialization.
2152 * Create a new ppp interface unit. Fails if it can't allocate memory
2153 * or if there is already a unit with the requested number.
2154 * unit == -1 means allocate a new number.
2156 static struct ppp *
2157 ppp_create_interface(int unit, int *retp)
2159 struct ppp *ppp;
2160 struct net_device *dev;
2161 struct list_head *list;
2162 int last_unit = -1;
2163 int ret = -EEXIST;
2164 int i;
2166 spin_lock(&all_ppp_lock);
2167 list = &all_ppp_units;
2168 while ((list = list->next) != &all_ppp_units) {
2169 ppp = list_entry(list, struct ppp, file.list);
2170 if ((unit < 0 && ppp->file.index > last_unit + 1)
2171 || (unit >= 0 && unit < ppp->file.index))
2172 break;
2173 if (unit == ppp->file.index)
2174 goto out; /* unit already exists */
2175 last_unit = ppp->file.index;
2177 if (unit < 0)
2178 unit = last_unit + 1;
2180 /* Create a new ppp structure and link it before `list'. */
2181 ret = -ENOMEM;
2182 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2183 if (ppp == 0)
2184 goto out;
2185 memset(ppp, 0, sizeof(struct ppp));
2186 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
2187 if (dev == 0) {
2188 kfree(ppp);
2189 goto out;
2191 memset(dev, 0, sizeof(struct net_device));
2193 ppp->file.index = unit;
2194 ppp->mru = PPP_MRU;
2195 init_ppp_file(&ppp->file, INTERFACE);
2196 for (i = 0; i < NUM_NP; ++i)
2197 ppp->npmode[i] = NPMODE_PASS;
2198 INIT_LIST_HEAD(&ppp->channels);
2199 spin_lock_init(&ppp->rlock);
2200 spin_lock_init(&ppp->wlock);
2201 #ifdef CONFIG_PPP_MULTILINK
2202 ppp->minseq = -1;
2203 skb_queue_head_init(&ppp->mrq);
2204 #endif /* CONFIG_PPP_MULTILINK */
2206 ppp->dev = dev;
2207 dev->init = ppp_net_init;
2208 sprintf(dev->name, "ppp%d", unit);
2209 dev->priv = ppp;
2210 dev->features |= NETIF_F_DYNALLOC;
2212 rtnl_lock();
2213 ret = register_netdevice(dev);
2214 rtnl_unlock();
2215 if (ret != 0) {
2216 printk(KERN_ERR "PPP: couldn't register device (%d)\n", ret);
2217 kfree(dev);
2218 kfree(ppp);
2219 goto out;
2222 list_add(&ppp->file.list, list->prev);
2223 out:
2224 spin_unlock(&all_ppp_lock);
2225 *retp = ret;
2226 if (ret != 0)
2227 ppp = 0;
2228 return ppp;
2232 * Initialize a ppp_file structure.
2234 static void
2235 init_ppp_file(struct ppp_file *pf, int kind)
2237 pf->kind = kind;
2238 skb_queue_head_init(&pf->xq);
2239 skb_queue_head_init(&pf->rq);
2240 atomic_set(&pf->refcnt, 1);
2241 init_waitqueue_head(&pf->rwait);
2245 * Free up all the resources used by a ppp interface unit.
2247 static void ppp_destroy_interface(struct ppp *ppp)
2249 struct net_device *dev;
2251 spin_lock(&all_ppp_lock);
2252 list_del(&ppp->file.list);
2254 /* Last fd open to this ppp unit is being closed or detached:
2255 mark the interface down, free the ppp unit */
2256 ppp_lock(ppp);
2257 ppp_ccp_closed(ppp);
2258 if (ppp->vj) {
2259 slhc_free(ppp->vj);
2260 ppp->vj = 0;
2262 skb_queue_purge(&ppp->file.xq);
2263 skb_queue_purge(&ppp->file.rq);
2264 #ifdef CONFIG_PPP_MULTILINK
2265 skb_queue_purge(&ppp->mrq);
2266 #endif /* CONFIG_PPP_MULTILINK */
2267 dev = ppp->dev;
2268 ppp->dev = 0;
2269 ppp_unlock(ppp);
2271 if (dev) {
2272 rtnl_lock();
2273 dev_close(dev);
2274 unregister_netdevice(dev);
2275 rtnl_unlock();
2279 * We can't acquire any new channels (since we have the
2280 * all_ppp_lock) so if n_channels is 0, we can free the
2281 * ppp structure. Otherwise we leave it around until the
2282 * last channel disconnects from it.
2284 if (ppp->n_channels == 0)
2285 kfree(ppp);
2287 spin_unlock(&all_ppp_lock);
2291 * Locate an existing ppp unit.
2292 * The caller should have locked the all_ppp_lock.
2294 static struct ppp *
2295 ppp_find_unit(int unit)
2297 struct ppp *ppp;
2298 struct list_head *list;
2300 list = &all_ppp_units;
2301 while ((list = list->next) != &all_ppp_units) {
2302 ppp = list_entry(list, struct ppp, file.list);
2303 if (ppp->file.index == unit)
2304 return ppp;
2306 return 0;
2310 * Locate an existing ppp channel.
2311 * The caller should have locked the all_channels_lock.
2313 static struct channel *
2314 ppp_find_channel(int unit)
2316 struct channel *pch;
2317 struct list_head *list;
2319 list = &all_channels;
2320 while ((list = list->next) != &all_channels) {
2321 pch = list_entry(list, struct channel, file.list);
2322 if (pch->file.index == unit)
2323 return pch;
2325 return 0;
2329 * Connect a PPP channel to a PPP interface unit.
2331 static int
2332 ppp_connect_channel(struct channel *pch, int unit)
2334 struct ppp *ppp;
2335 int ret = -ENXIO;
2336 int hdrlen;
2338 spin_lock(&all_ppp_lock);
2339 ppp = ppp_find_unit(unit);
2340 if (ppp == 0)
2341 goto out;
2342 write_lock_bh(&pch->upl);
2343 ret = -EINVAL;
2344 if (pch->ppp != 0)
2345 goto outw;
2346 ppp_lock(ppp);
2347 spin_lock_bh(&pch->downl);
2348 if (pch->chan == 0) /* need to check this?? */
2349 goto outr;
2351 if (pch->file.hdrlen > ppp->file.hdrlen)
2352 ppp->file.hdrlen = pch->file.hdrlen;
2353 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2354 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2355 ppp->dev->hard_header_len = hdrlen;
2356 list_add_tail(&pch->clist, &ppp->channels);
2357 ++ppp->n_channels;
2358 pch->ppp = ppp;
2359 ret = 0;
2361 outr:
2362 spin_unlock_bh(&pch->downl);
2363 ppp_unlock(ppp);
2364 outw:
2365 write_unlock_bh(&pch->upl);
2366 out:
2367 spin_unlock(&all_ppp_lock);
2368 return ret;
2372 * Disconnect a channel from its ppp unit.
2374 static int
2375 ppp_disconnect_channel(struct channel *pch)
2377 struct ppp *ppp;
2378 int err = -EINVAL;
2379 int dead;
2381 write_lock_bh(&pch->upl);
2382 ppp = pch->ppp;
2383 if (ppp != 0) {
2384 /* remove it from the ppp unit's list */
2385 pch->ppp = NULL;
2386 ppp_lock(ppp);
2387 list_del(&pch->clist);
2388 --ppp->n_channels;
2389 dead = ppp->dev == 0 && ppp->n_channels == 0;
2390 ppp_unlock(ppp);
2391 if (dead)
2392 /* Last disconnect from a ppp unit
2393 that is already dead: free it. */
2394 kfree(ppp);
2395 err = 0;
2397 write_unlock_bh(&pch->upl);
2398 return err;
2402 * Free up the resources used by a ppp channel.
2404 static void ppp_destroy_channel(struct channel *pch)
2406 skb_queue_purge(&pch->file.xq);
2407 skb_queue_purge(&pch->file.rq);
2408 kfree(pch);
2411 void __exit ppp_cleanup(void)
2413 /* should never happen */
2414 if (!list_empty(&all_ppp_units) || !list_empty(&all_channels))
2415 printk(KERN_ERR "PPP: removing module but units remain!\n");
2416 if (devfs_unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2417 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2418 devfs_unregister(devfs_handle);
2421 module_init(ppp_init);
2422 module_exit(ppp_cleanup);
2424 EXPORT_SYMBOL(ppp_register_channel);
2425 EXPORT_SYMBOL(ppp_unregister_channel);
2426 EXPORT_SYMBOL(ppp_channel_index);
2427 EXPORT_SYMBOL(ppp_unit_number);
2428 EXPORT_SYMBOL(ppp_input);
2429 EXPORT_SYMBOL(ppp_input_error);
2430 EXPORT_SYMBOL(ppp_output_wakeup);
2431 EXPORT_SYMBOL(ppp_register_compressor);
2432 EXPORT_SYMBOL(ppp_unregister_compressor);
2433 EXPORT_SYMBOL(ppp_channel_read);
2434 EXPORT_SYMBOL(ppp_channel_write);
2435 EXPORT_SYMBOL(ppp_channel_poll);
2436 EXPORT_SYMBOL(ppp_channel_ioctl);
2437 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2438 EXPORT_SYMBOL(all_channels); /* for debugging */