Merge with Linux 2.3.99-pre4.
[linux-2.6/linux-mips.git] / drivers / net / ppp_generic.c
bloba70c1a8db66fd5a52546b93e84f235f05ec722f9
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 20000406==
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 <net/slhc_vj.h>
45 #include <asm/atomic.h>
47 #define PPP_VERSION "2.4.1"
50 * Network protocols we support.
52 #define NP_IP 0 /* Internet Protocol V4 */
53 #define NP_IPV6 1 /* Internet Protocol V6 */
54 #define NP_IPX 2 /* IPX protocol */
55 #define NP_AT 3 /* Appletalk protocol */
56 #define NUM_NP 4 /* Number of NPs. */
58 #define MPHDRLEN 6 /* multilink protocol header length */
59 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
60 #define MIN_FRAG_SIZE 64
63 * An instance of /dev/ppp can be associated with either a ppp
64 * interface unit or a ppp channel. In both cases, file->private_data
65 * points to one of these.
67 struct ppp_file {
68 enum {
69 INTERFACE=1, CHANNEL
70 } kind;
71 struct sk_buff_head xq; /* pppd transmit queue */
72 struct sk_buff_head rq; /* receive queue for pppd */
73 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
74 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
75 int hdrlen; /* space to leave for headers */
76 struct list_head list; /* link in all_* list */
77 int index; /* interface unit / channel number */
80 #define PF_TO_X(pf, X) ((X *)((char *)(pf)-(unsigned long)(&((X *)0)->file)))
82 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
83 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
85 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
88 * Data structure describing one ppp unit.
89 * A ppp unit corresponds to a ppp network interface device
90 * and represents a multilink bundle.
91 * It can have 0 or more ppp channels connected to it.
93 struct ppp {
94 struct ppp_file file; /* stuff for read/write/poll */
95 char name[16]; /* unit name */
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, int wakeup);
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 MOD_INC_USE_COUNT;
317 return 0;
320 static int ppp_release(struct inode *inode, struct file *file)
322 struct ppp_file *pf = (struct ppp_file *) file->private_data;
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 MOD_DEC_USE_COUNT;
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), 0);
431 break;
432 case CHANNEL:
433 ppp_channel_push(PF_TO_CHANNEL(pf));
434 break;
437 ret = count;
439 out:
440 return ret;
443 static unsigned int ppp_poll(struct file *file, poll_table *wait)
445 struct ppp_file *pf = (struct ppp_file *) file->private_data;
446 unsigned int mask;
448 if (pf == 0)
449 return 0;
450 poll_wait(file, &pf->rwait, wait);
451 mask = POLLOUT | POLLWRNORM;
452 if (skb_peek(&pf->rq) != 0)
453 mask |= POLLIN | POLLRDNORM;
454 if (pf->kind == CHANNEL) {
455 struct channel *pch = PF_TO_CHANNEL(pf);
456 if (pch->chan == 0)
457 mask |= POLLHUP;
459 return mask;
462 static int ppp_ioctl(struct inode *inode, struct file *file,
463 unsigned int cmd, unsigned long arg)
465 struct ppp_file *pf = (struct ppp_file *) file->private_data;
466 struct ppp *ppp;
467 int err = -EFAULT, val, val2, i;
468 struct ppp_idle idle;
469 struct npioctl npi;
470 int unit;
471 struct slcompress *vj;
473 if (pf == 0)
474 return ppp_unattached_ioctl(pf, file, cmd, arg);
476 if (pf->kind == CHANNEL) {
477 struct channel *pch = PF_TO_CHANNEL(pf);
478 struct ppp_channel *chan;
480 switch (cmd) {
481 case PPPIOCCONNECT:
482 if (get_user(unit, (int *) arg))
483 break;
484 err = ppp_connect_channel(pch, unit);
485 break;
487 case PPPIOCDISCONN:
488 err = ppp_disconnect_channel(pch);
489 break;
491 case PPPIOCDETACH:
492 file->private_data = 0;
493 if (atomic_dec_and_test(&pf->refcnt))
494 ppp_destroy_channel(pch);
495 err = 0;
496 break;
498 default:
499 spin_lock_bh(&pch->downl);
500 chan = pch->chan;
501 err = -ENOTTY;
502 if (chan && chan->ops->ioctl)
503 err = chan->ops->ioctl(chan, cmd, arg);
504 spin_unlock_bh(&pch->downl);
506 return err;
509 if (pf->kind != INTERFACE) {
510 /* can't happen */
511 printk(KERN_ERR "PPP: not interface or channel??\n");
512 return -EINVAL;
515 ppp = PF_TO_PPP(pf);
516 switch (cmd) {
517 case PPPIOCDETACH:
518 file->private_data = 0;
519 if (atomic_dec_and_test(&pf->refcnt))
520 ppp_destroy_interface(ppp);
521 err = 0;
522 break;
524 case PPPIOCSMRU:
525 if (get_user(val, (int *) arg))
526 break;
527 ppp->mru = val;
528 err = 0;
529 break;
531 case PPPIOCSFLAGS:
532 if (get_user(val, (int *) arg))
533 break;
534 ppp_lock(ppp);
535 if (ppp->flags & ~val & SC_CCP_OPEN)
536 ppp_ccp_closed(ppp);
537 ppp->flags = val & SC_FLAG_BITS;
538 ppp_unlock(ppp);
539 err = 0;
540 break;
542 case PPPIOCGFLAGS:
543 val = ppp->flags | ppp->xstate | ppp->rstate;
544 if (put_user(val, (int *) arg))
545 break;
546 err = 0;
547 break;
549 case PPPIOCSCOMPRESS:
550 err = ppp_set_compress(ppp, arg);
551 break;
553 case PPPIOCGUNIT:
554 if (put_user(ppp->file.index, (int *) arg))
555 break;
556 err = 0;
557 break;
559 case PPPIOCSDEBUG:
560 if (get_user(val, (int *) arg))
561 break;
562 ppp->debug = val;
563 err = 0;
564 break;
566 case PPPIOCGDEBUG:
567 if (put_user(ppp->debug, (int *) arg))
568 break;
569 err = 0;
570 break;
572 case PPPIOCGIDLE:
573 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
574 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
575 if (copy_to_user((void *) arg, &idle, sizeof(idle)))
576 break;
577 err = 0;
578 break;
580 case PPPIOCSMAXCID:
581 if (get_user(val, (int *) arg))
582 break;
583 val2 = 15;
584 if ((val >> 16) != 0) {
585 val2 = val >> 16;
586 val &= 0xffff;
588 vj = slhc_init(val2+1, val+1);
589 if (vj == 0) {
590 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
591 err = -ENOMEM;
592 break;
594 ppp_lock(ppp);
595 if (ppp->vj != 0)
596 slhc_free(ppp->vj);
597 ppp->vj = vj;
598 ppp_unlock(ppp);
599 err = 0;
600 break;
602 case PPPIOCGNPMODE:
603 case PPPIOCSNPMODE:
604 if (copy_from_user(&npi, (void *) arg, sizeof(npi)))
605 break;
606 err = proto_to_npindex(npi.protocol);
607 if (err < 0)
608 break;
609 i = err;
610 if (cmd == PPPIOCGNPMODE) {
611 err = -EFAULT;
612 npi.mode = ppp->npmode[i];
613 if (copy_to_user((void *) arg, &npi, sizeof(npi)))
614 break;
615 } else {
616 ppp->npmode[i] = npi.mode;
617 /* we may be able to transmit more packets now (??) */
618 netif_wake_queue(ppp->dev);
620 err = 0;
621 break;
623 #ifdef CONFIG_PPP_MULTILINK
624 case PPPIOCSMRRU:
625 if (get_user(val, (int *) arg))
626 break;
627 ppp_recv_lock(ppp);
628 ppp->mrru = val;
629 ppp_recv_unlock(ppp);
630 err = 0;
631 break;
632 #endif /* CONFIG_PPP_MULTILINK */
634 default:
635 err = -ENOTTY;
637 return err;
640 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
641 unsigned int cmd, unsigned long arg)
643 int unit, err = -EFAULT;
644 struct ppp *ppp;
645 struct channel *chan;
647 switch (cmd) {
648 case PPPIOCNEWUNIT:
649 /* Create a new ppp unit */
650 if (get_user(unit, (int *) arg))
651 break;
652 ppp = ppp_create_interface(unit, &err);
653 if (ppp == 0)
654 break;
655 file->private_data = &ppp->file;
656 err = -EFAULT;
657 if (put_user(ppp->file.index, (int *) arg))
658 break;
659 err = 0;
660 break;
662 case PPPIOCATTACH:
663 /* Attach to an existing ppp unit */
664 if (get_user(unit, (int *) arg))
665 break;
666 spin_lock(&all_ppp_lock);
667 ppp = ppp_find_unit(unit);
668 if (ppp != 0)
669 atomic_inc(&ppp->file.refcnt);
670 spin_unlock(&all_ppp_lock);
671 err = -ENXIO;
672 if (ppp == 0)
673 break;
674 file->private_data = &ppp->file;
675 err = 0;
676 break;
678 case PPPIOCATTCHAN:
679 if (get_user(unit, (int *) arg))
680 break;
681 spin_lock_bh(&all_channels_lock);
682 chan = ppp_find_channel(unit);
683 if (chan != 0)
684 atomic_inc(&chan->file.refcnt);
685 spin_unlock_bh(&all_channels_lock);
686 err = -ENXIO;
687 if (chan == 0)
688 break;
689 file->private_data = &chan->file;
690 err = 0;
691 break;
693 default:
694 err = -ENOTTY;
696 return err;
699 static struct file_operations ppp_device_fops = {
700 read: ppp_read,
701 write: ppp_write,
702 poll: ppp_poll,
703 ioctl: ppp_ioctl,
704 open: ppp_open,
705 release: ppp_release
708 #define PPP_MAJOR 108
710 static devfs_handle_t devfs_handle = NULL;
712 /* Called at boot time if ppp is compiled into the kernel,
713 or at module load time (from init_module) if compiled as a module. */
714 int __init ppp_init(void)
716 int err;
718 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
719 err = devfs_register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
720 if (err)
721 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
722 devfs_handle = devfs_register(NULL, "ppp", 0, DEVFS_FL_NONE,
723 PPP_MAJOR, 0,
724 S_IFCHR | S_IRUSR | S_IWUSR, 0, 0,
725 &ppp_device_fops, NULL);
727 return 0;
731 * Network interface unit routines.
733 static int
734 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
736 struct ppp *ppp = (struct ppp *) dev->priv;
737 int npi, proto;
738 unsigned char *pp;
740 npi = ethertype_to_npindex(ntohs(skb->protocol));
741 if (npi < 0)
742 goto outf;
744 /* Drop, accept or reject the packet */
745 switch (ppp->npmode[npi]) {
746 case NPMODE_PASS:
747 break;
748 case NPMODE_QUEUE:
749 /* it would be nice to have a way to tell the network
750 system to queue this one up for later. */
751 goto outf;
752 case NPMODE_DROP:
753 case NPMODE_ERROR:
754 goto outf;
757 /* Put the 2-byte PPP protocol number on the front,
758 making sure there is room for the address and control fields. */
759 if (skb_headroom(skb) < PPP_HDRLEN) {
760 struct sk_buff *ns;
762 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
763 if (ns == 0)
764 goto outf;
765 skb_reserve(ns, dev->hard_header_len);
766 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
767 kfree_skb(skb);
768 skb = ns;
770 pp = skb_push(skb, 2);
771 proto = npindex_to_proto[npi];
772 pp[0] = proto >> 8;
773 pp[1] = proto;
775 netif_stop_queue(dev);
776 skb_queue_tail(&ppp->file.xq, skb);
777 ppp_xmit_process(ppp, 0);
778 return 0;
780 outf:
781 kfree_skb(skb);
782 ++ppp->stats.tx_dropped;
783 return 0;
786 static struct net_device_stats *
787 ppp_net_stats(struct net_device *dev)
789 struct ppp *ppp = (struct ppp *) dev->priv;
791 return &ppp->stats;
794 static int
795 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
797 struct ppp *ppp = dev->priv;
798 int err = -EFAULT;
799 void *addr = (void *) ifr->ifr_ifru.ifru_data;
800 struct ppp_stats stats;
801 struct ppp_comp_stats cstats;
802 char *vers;
804 switch (cmd) {
805 case SIOCGPPPSTATS:
806 ppp_get_stats(ppp, &stats);
807 if (copy_to_user(addr, &stats, sizeof(stats)))
808 break;
809 err = 0;
810 break;
812 case SIOCGPPPCSTATS:
813 memset(&cstats, 0, sizeof(cstats));
814 if (ppp->xc_state != 0)
815 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
816 if (ppp->rc_state != 0)
817 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
818 if (copy_to_user(addr, &cstats, sizeof(cstats)))
819 break;
820 err = 0;
821 break;
823 case SIOCGPPPVER:
824 vers = PPP_VERSION;
825 if (copy_to_user(addr, vers, strlen(vers) + 1))
826 break;
827 err = 0;
828 break;
830 default:
831 err = -EINVAL;
834 return err;
838 ppp_net_init(struct net_device *dev)
840 dev->hard_header_len = PPP_HDRLEN;
841 dev->mtu = PPP_MTU;
842 dev->hard_start_xmit = ppp_start_xmit;
843 dev->get_stats = ppp_net_stats;
844 dev->do_ioctl = ppp_net_ioctl;
845 dev->addr_len = 0;
846 dev->tx_queue_len = 3;
847 dev->type = ARPHRD_PPP;
848 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
850 dev_init_buffers(dev);
851 return 0;
855 * Transmit-side routines.
859 * Called to do any work queued up on the transmit side
860 * that can now be done.
862 static void
863 ppp_xmit_process(struct ppp *ppp, int wakeup)
865 struct sk_buff *skb;
867 ppp_xmit_lock(ppp);
868 if (wakeup)
869 ppp_push(ppp);
870 while (ppp->xmit_pending == 0
871 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
872 ppp_send_frame(ppp, skb);
873 /* If there's no work left to do, tell the core net
874 code that we can accept some more. */
875 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0
876 && ppp->dev != 0)
877 netif_wake_queue(ppp->dev);
878 ppp_xmit_unlock(ppp);
882 * Compress and send a frame.
883 * The caller should have locked the xmit path,
884 * and xmit_pending should be 0.
886 static void
887 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
889 int proto = PPP_PROTO(skb);
890 struct sk_buff *new_skb;
891 int len;
892 unsigned char *cp;
894 ++ppp->stats.tx_packets;
895 ppp->stats.tx_bytes += skb->len - 2;
897 switch (proto) {
898 case PPP_IP:
899 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
900 break;
901 /* try to do VJ TCP header compression */
902 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
903 GFP_ATOMIC);
904 if (new_skb == 0) {
905 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
906 goto drop;
908 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
909 cp = skb->data + 2;
910 len = slhc_compress(ppp->vj, cp, skb->len - 2,
911 new_skb->data + 2, &cp,
912 !(ppp->flags & SC_NO_TCP_CCID));
913 if (cp == skb->data + 2) {
914 /* didn't compress */
915 kfree_skb(new_skb);
916 } else {
917 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
918 proto = PPP_VJC_COMP;
919 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
920 } else {
921 proto = PPP_VJC_UNCOMP;
922 cp[0] = skb->data[2];
924 kfree_skb(skb);
925 skb = new_skb;
926 cp = skb_put(skb, len + 2);
927 cp[0] = 0;
928 cp[1] = proto;
930 break;
932 case PPP_CCP:
933 /* peek at outbound CCP frames */
934 ppp_ccp_peek(ppp, skb, 0);
935 break;
938 /* try to do packet compression */
939 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
940 && proto != PPP_LCP && proto != PPP_CCP) {
941 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
942 GFP_ATOMIC);
943 if (new_skb == 0) {
944 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
945 goto drop;
947 if (ppp->dev->hard_header_len > PPP_HDRLEN)
948 skb_reserve(new_skb,
949 ppp->dev->hard_header_len - PPP_HDRLEN);
951 /* compressor still expects A/C bytes in hdr */
952 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
953 new_skb->data, skb->len + 2,
954 ppp->dev->mtu + PPP_HDRLEN);
955 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
956 kfree_skb(skb);
957 skb = new_skb;
958 skb_put(skb, len);
959 skb_pull(skb, 2); /* pull off A/C bytes */
960 } else {
961 /* didn't compress, or CCP not up yet */
962 kfree_skb(new_skb);
966 /* for data packets, record the time */
967 if (proto < 0x8000)
968 ppp->last_xmit = jiffies;
971 * If we are waiting for traffic (demand dialling),
972 * queue it up for pppd to receive.
974 if (ppp->flags & SC_LOOP_TRAFFIC) {
975 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
976 goto drop;
977 skb_queue_tail(&ppp->file.rq, skb);
978 wake_up_interruptible(&ppp->file.rwait);
979 return;
982 ppp->xmit_pending = skb;
983 ppp_push(ppp);
984 return;
986 drop:
987 kfree_skb(skb);
988 ++ppp->stats.tx_errors;
992 * Try to send the frame in xmit_pending.
993 * The caller should have the xmit path locked.
995 static void
996 ppp_push(struct ppp *ppp)
998 struct list_head *list;
999 struct channel *pch;
1000 struct sk_buff *skb = ppp->xmit_pending;
1002 if (skb == 0)
1003 return;
1005 list = &ppp->channels;
1006 if (list_empty(list)) {
1007 /* nowhere to send the packet, just drop it */
1008 ppp->xmit_pending = 0;
1009 kfree_skb(skb);
1010 return;
1013 if ((ppp->flags & SC_MULTILINK) == 0) {
1014 /* not doing multilink: send it down the first channel */
1015 list = list->next;
1016 pch = list_entry(list, struct channel, clist);
1018 spin_lock_bh(&pch->downl);
1019 if (pch->chan) {
1020 if (pch->chan->ops->start_xmit(pch->chan, skb))
1021 skb = 0;
1022 } else {
1023 /* channel got unregistered */
1024 kfree_skb(skb);
1025 skb = 0;
1027 if (skb_queue_len(&pch->file.xq) == 0 && skb == 0)
1028 ppp->xmit_pending = 0;
1029 spin_unlock_bh(&pch->downl);
1030 return;
1033 #ifdef CONFIG_PPP_MULTILINK
1034 /* Multilink: fragment the packet over as many links
1035 as can take the packet at the moment. */
1036 if (!ppp_mp_explode(ppp, skb))
1037 return;
1038 #endif /* CONFIG_PPP_MULTILINK */
1040 ppp->xmit_pending = 0;
1041 kfree_skb(skb);
1044 #ifdef CONFIG_PPP_MULTILINK
1046 * Divide a packet to be transmitted into fragments and
1047 * send them out the individual links.
1049 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1051 int nch, len, fragsize;
1052 int i, bits, hdrlen, mtu;
1053 int flen, fnb;
1054 unsigned char *p, *q;
1055 struct list_head *list;
1056 struct channel *pch;
1057 struct sk_buff *frag;
1058 struct ppp_channel *chan;
1060 nch = 0;
1061 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1062 list = &ppp->channels;
1063 while ((list = list->next) != &ppp->channels) {
1064 pch = list_entry(list, struct channel, clist);
1065 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1067 * If a channel hasn't had a fragment yet, it has to get
1068 * one before we send any fragments on later channels.
1069 * If it can't take a fragment now, don't give any
1070 * to subsequent channels.
1072 if (!pch->had_frag && !pch->avail) {
1073 while ((list = list->next) != &ppp->channels) {
1074 pch = list_entry(list, struct channel, clist);
1075 pch->avail = 0;
1077 break;
1080 if (nch == 0)
1081 return 0; /* can't take now, leave it in xmit_pending */
1083 /* Do protocol field compression (XXX this should be optional) */
1084 p = skb->data;
1085 len = skb->len;
1086 if (*p == 0) {
1087 ++p;
1088 --len;
1091 /* decide on fragment size */
1092 fragsize = len;
1093 if (nch > 1) {
1094 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1095 if (nch > maxch)
1096 nch = maxch;
1097 fragsize = ROUNDUP(fragsize, nch);
1100 /* skip to the channel after the one we last used
1101 and start at that one */
1102 for (i = 0; i < ppp->nxchan; ++i) {
1103 list = list->next;
1104 if (list == &ppp->channels) {
1105 i = 0;
1106 break;
1110 /* create a fragment for each channel */
1111 bits = B;
1112 do {
1113 list = list->next;
1114 if (list == &ppp->channels) {
1115 i = 0;
1116 continue;
1118 pch = list_entry(list, struct channel, clist);
1119 ++i;
1120 if (!pch->avail)
1121 continue;
1123 /* check the channel's mtu and whether it is still attached. */
1124 spin_lock_bh(&pch->downl);
1125 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1126 /* can't use this channel */
1127 spin_unlock_bh(&pch->downl);
1128 pch->avail = 0;
1129 if (--nch == 0)
1130 break;
1131 continue;
1135 * We have to create multiple fragments for this channel
1136 * if fragsize is greater than the channel's mtu.
1138 if (fragsize > len)
1139 fragsize = len;
1140 for (flen = fragsize; flen > 0; flen -= fnb) {
1141 fnb = flen;
1142 if (fnb > mtu + 2 - hdrlen)
1143 fnb = mtu + 2 - hdrlen;
1144 if (fnb >= len)
1145 bits |= E;
1146 frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1147 if (frag == 0)
1148 goto noskb;
1149 q = skb_put(frag, fnb + hdrlen);
1150 /* make the MP header */
1151 q[0] = PPP_MP >> 8;
1152 q[1] = PPP_MP;
1153 if (ppp->flags & SC_MP_XSHORTSEQ) {
1154 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1155 q[3] = ppp->nxseq;
1156 } else {
1157 q[2] = bits;
1158 q[3] = ppp->nxseq >> 16;
1159 q[4] = ppp->nxseq >> 8;
1160 q[5] = ppp->nxseq;
1163 /* copy the data in */
1164 memcpy(q + hdrlen, p, fnb);
1166 /* try to send it down the channel */
1167 chan = pch->chan;
1168 if (!chan->ops->start_xmit(chan, frag))
1169 skb_queue_tail(&pch->file.xq, frag);
1170 pch->had_frag = 1;
1171 p += fnb;
1172 len -= fnb;
1173 ++ppp->nxseq;
1174 bits = 0;
1176 spin_unlock_bh(&pch->downl);
1177 } while (len > 0);
1178 ppp->nxchan = i;
1180 return 1;
1182 noskb:
1183 spin_unlock_bh(&pch->downl);
1184 if (ppp->debug & 1)
1185 printk(KERN_ERR "PPP: no memory (fragment)\n");
1186 ++ppp->stats.tx_errors;
1187 ++ppp->nxseq;
1188 return 1; /* abandon the frame */
1190 #endif /* CONFIG_PPP_MULTILINK */
1193 * Try to send data out on a channel.
1195 static void
1196 ppp_channel_push(struct channel *pch)
1198 struct sk_buff *skb;
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);
1218 * Receive-side routines.
1221 /* misuse a few fields of the skb for MP reconstruction */
1222 #define sequence priority
1223 #define BEbits cb[0]
1225 static inline void
1226 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1228 ppp_recv_lock(ppp);
1229 /* ppp->dev == 0 means interface is closing down */
1230 if (ppp->dev != 0)
1231 ppp_receive_frame(ppp, skb, pch);
1232 else
1233 kfree_skb(skb);
1234 ppp_recv_unlock(ppp);
1237 void
1238 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1240 struct channel *pch = chan->ppp;
1241 int proto;
1243 if (pch == 0 || skb->len == 0) {
1244 kfree_skb(skb);
1245 return;
1248 proto = PPP_PROTO(skb);
1249 read_lock_bh(&pch->upl);
1250 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1251 /* put it on the channel queue */
1252 skb_queue_tail(&pch->file.rq, skb);
1253 /* drop old frames if queue too long */
1254 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1255 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1256 kfree_skb(skb);
1257 wake_up_interruptible(&pch->file.rwait);
1258 } else {
1259 ppp_do_recv(pch->ppp, skb, pch);
1261 read_unlock_bh(&pch->upl);
1264 /* Put a 0-length skb in the receive queue as an error indication */
1265 void
1266 ppp_input_error(struct ppp_channel *chan, int code)
1268 struct channel *pch = chan->ppp;
1269 struct sk_buff *skb;
1271 if (pch == 0)
1272 return;
1274 read_lock_bh(&pch->upl);
1275 if (pch->ppp != 0) {
1276 skb = alloc_skb(0, GFP_ATOMIC);
1277 if (skb != 0) {
1278 skb->len = 0; /* probably unnecessary */
1279 skb->cb[0] = code;
1280 ppp_do_recv(pch->ppp, skb, pch);
1283 read_unlock_bh(&pch->upl);
1287 * We come in here to process a received frame.
1288 * The receive side of the ppp unit is locked.
1290 static void
1291 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1293 if (skb->len >= 2) {
1294 #ifdef CONFIG_PPP_MULTILINK
1295 /* XXX do channel-level decompression here */
1296 if (PPP_PROTO(skb) == PPP_MP)
1297 ppp_receive_mp_frame(ppp, skb, pch);
1298 else
1299 #endif /* CONFIG_PPP_MULTILINK */
1300 ppp_receive_nonmp_frame(ppp, skb);
1301 return;
1304 if (skb->len > 0)
1305 /* note: a 0-length skb is used as an error indication */
1306 ++ppp->stats.rx_length_errors;
1308 kfree_skb(skb);
1309 ppp_receive_error(ppp);
1312 static void
1313 ppp_receive_error(struct ppp *ppp)
1315 ++ppp->stats.rx_errors;
1316 if (ppp->vj != 0)
1317 slhc_toss(ppp->vj);
1320 static void
1321 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1323 struct sk_buff *ns;
1324 int proto, len, npi;
1327 * Decompress the frame, if compressed.
1328 * Note that some decompressors need to see uncompressed frames
1329 * that come in as well as compressed frames.
1331 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1332 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1333 skb = ppp_decompress_frame(ppp, skb);
1335 proto = PPP_PROTO(skb);
1336 switch (proto) {
1337 case PPP_VJC_COMP:
1338 /* decompress VJ compressed packets */
1339 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1340 goto err;
1341 if (skb_tailroom(skb) < 124) {
1342 /* copy to a new sk_buff with more tailroom */
1343 ns = dev_alloc_skb(skb->len + 128);
1344 if (ns == 0) {
1345 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1346 goto err;
1348 skb_reserve(ns, 2);
1349 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
1350 kfree_skb(skb);
1351 skb = ns;
1353 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1354 if (len <= 0) {
1355 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1356 goto err;
1358 len += 2;
1359 if (len > skb->len)
1360 skb_put(skb, len - skb->len);
1361 else if (len < skb->len)
1362 skb_trim(skb, len);
1363 proto = PPP_IP;
1364 break;
1366 case PPP_VJC_UNCOMP:
1367 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1368 goto err;
1369 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1370 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1371 goto err;
1373 proto = PPP_IP;
1374 break;
1376 case PPP_CCP:
1377 ppp_ccp_peek(ppp, skb, 1);
1378 break;
1381 ++ppp->stats.rx_packets;
1382 ppp->stats.rx_bytes += skb->len - 2;
1384 npi = proto_to_npindex(proto);
1385 if (npi < 0) {
1386 /* control or unknown frame - pass it to pppd */
1387 skb_queue_tail(&ppp->file.rq, skb);
1388 /* limit queue length by dropping old frames */
1389 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1390 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1391 kfree_skb(skb);
1392 /* wake up any process polling or blocking on read */
1393 wake_up_interruptible(&ppp->file.rwait);
1395 } else {
1396 /* network protocol frame - give it to the kernel */
1397 ppp->last_recv = jiffies;
1398 if ((ppp->dev->flags & IFF_UP) == 0
1399 || ppp->npmode[npi] != NPMODE_PASS) {
1400 kfree_skb(skb);
1401 } else {
1402 skb_pull(skb, 2); /* chop off protocol */
1403 skb->dev = ppp->dev;
1404 skb->protocol = htons(npindex_to_ethertype[npi]);
1405 skb->mac.raw = skb->data;
1406 netif_rx(skb);
1409 return;
1411 err:
1412 kfree_skb(skb);
1413 ppp_receive_error(ppp);
1416 static struct sk_buff *
1417 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1419 int proto = PPP_PROTO(skb);
1420 struct sk_buff *ns;
1421 int len;
1423 if (proto == PPP_COMP) {
1424 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1425 if (ns == 0) {
1426 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1427 goto err;
1429 /* the decompressor still expects the A/C bytes in the hdr */
1430 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1431 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1432 if (len < 0) {
1433 /* Pass the compressed frame to pppd as an
1434 error indication. */
1435 if (len == DECOMP_FATALERROR)
1436 ppp->rstate |= SC_DC_FERROR;
1437 goto err;
1440 kfree_skb(skb);
1441 skb = ns;
1442 skb_put(skb, len);
1443 skb_pull(skb, 2); /* pull off the A/C bytes */
1445 } else {
1446 /* Uncompressed frame - pass to decompressor so it
1447 can update its dictionary if necessary. */
1448 if (ppp->rcomp->incomp)
1449 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1450 skb->len + 2);
1453 return skb;
1455 err:
1456 ppp->rstate |= SC_DC_ERROR;
1457 ppp_receive_error(ppp);
1458 return skb;
1461 #ifdef CONFIG_PPP_MULTILINK
1463 * Receive a multilink frame.
1464 * We put it on the reconstruction queue and then pull off
1465 * as many completed frames as we can.
1467 static void
1468 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1470 u32 mask, seq;
1471 struct list_head *l;
1472 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1474 if (skb->len < mphdrlen + 1 || ppp->mrru == 0)
1475 goto err; /* no good, throw it away */
1477 /* Decode sequence number and begin/end bits */
1478 if (ppp->flags & SC_MP_SHORTSEQ) {
1479 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1480 mask = 0xfff;
1481 } else {
1482 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1483 mask = 0xffffff;
1485 skb->BEbits = skb->data[2];
1486 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1489 * Do protocol ID decompression on the first fragment of each packet.
1491 if ((skb->BEbits & B) && (skb->data[0] & 1))
1492 *skb_push(skb, 1) = 0;
1495 * Expand sequence number to 32 bits, making it as close
1496 * as possible to ppp->minseq.
1498 seq |= ppp->minseq & ~mask;
1499 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1500 seq += mask + 1;
1501 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1502 seq -= mask + 1; /* should never happen */
1503 skb->sequence = seq;
1504 pch->lastseq = seq;
1507 * If this packet comes before the next one we were expecting,
1508 * drop it.
1510 if (seq_before(seq, ppp->nextseq)) {
1511 kfree_skb(skb);
1512 ++ppp->stats.rx_dropped;
1513 ppp_receive_error(ppp);
1514 return;
1518 * Reevaluate minseq, the minimum over all channels of the
1519 * last sequence number received on each channel. Because of
1520 * the increasing sequence number rule, we know that any fragment
1521 * before `minseq' which hasn't arrived is never going to arrive.
1522 * The list of channels can't change because we have the receive
1523 * side of the ppp unit locked.
1525 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1526 struct channel *ch = list_entry(l, struct channel, clist);
1527 if (seq_before(ch->lastseq, seq))
1528 seq = ch->lastseq;
1530 if (seq_before(ppp->minseq, seq))
1531 ppp->minseq = seq;
1533 /* Put the fragment on the reconstruction queue */
1534 ppp_mp_insert(ppp, skb);
1536 /* If the queue is getting long, don't wait any longer for packets
1537 before the start of the queue. */
1538 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1539 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1540 ppp->minseq = ppp->mrq.next->sequence;
1542 /* Pull completed packets off the queue and receive them. */
1543 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1544 ppp_receive_nonmp_frame(ppp, skb);
1546 return;
1548 err:
1549 kfree_skb(skb);
1550 ppp_receive_error(ppp);
1554 * Insert a fragment on the MP reconstruction queue.
1555 * The queue is ordered by increasing sequence number.
1557 static void
1558 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1560 struct sk_buff *p;
1561 struct sk_buff_head *list = &ppp->mrq;
1562 u32 seq = skb->sequence;
1564 /* N.B. we don't need to lock the list lock because we have the
1565 ppp unit receive-side lock. */
1566 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1567 if (seq_before(seq, p->sequence))
1568 break;
1569 __skb_insert(skb, p->prev, p, list);
1573 * Reconstruct a packet from the MP fragment queue.
1574 * We go through increasing sequence numbers until we find a
1575 * complete packet, or we get to the sequence number for a fragment
1576 * which hasn't arrived but might still do so.
1578 struct sk_buff *
1579 ppp_mp_reconstruct(struct ppp *ppp)
1581 u32 seq = ppp->nextseq;
1582 u32 minseq = ppp->minseq;
1583 struct sk_buff_head *list = &ppp->mrq;
1584 struct sk_buff *p, *next;
1585 struct sk_buff *head, *tail;
1586 struct sk_buff *skb = NULL;
1587 int lost = 0, len = 0;
1589 if (ppp->mrru == 0) /* do nothing until mrru is set */
1590 return NULL;
1591 head = list->next;
1592 tail = NULL;
1593 for (p = head; p != (struct sk_buff *) list; p = next) {
1594 next = p->next;
1595 if (seq_before(p->sequence, seq)) {
1596 /* this can't happen, anyway ignore the skb */
1597 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1598 p->sequence, seq);
1599 head = next;
1600 continue;
1602 if (p->sequence != seq) {
1603 /* Fragment `seq' is missing. If it is after
1604 minseq, it might arrive later, so stop here. */
1605 if (seq_after(seq, minseq))
1606 break;
1607 /* Fragment `seq' is lost, keep going. */
1608 lost = 1;
1609 seq = seq_before(minseq, p->sequence)?
1610 minseq + 1: p->sequence;
1611 next = p;
1612 continue;
1616 * At this point we know that all the fragments from
1617 * ppp->nextseq to seq are either present or lost.
1618 * Also, there are no complete packets in the queue
1619 * that have no missing fragments and end before this
1620 * fragment.
1623 /* B bit set indicates this fragment starts a packet */
1624 if (p->BEbits & B) {
1625 head = p;
1626 lost = 0;
1627 len = 0;
1630 len += p->len;
1632 /* Got a complete packet yet? */
1633 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1634 if (len > ppp->mrru + 2) {
1635 ++ppp->stats.rx_length_errors;
1636 printk(KERN_DEBUG "PPP: reconstructed packet"
1637 " is too long (%d)\n", len);
1638 } else if (p == head) {
1639 /* fragment is complete packet - reuse skb */
1640 tail = p;
1641 skb = skb_get(p);
1642 break;
1643 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1644 ++ppp->stats.rx_missed_errors;
1645 printk(KERN_DEBUG "PPP: no memory for "
1646 "reconstructed packet");
1647 } else {
1648 tail = p;
1649 break;
1651 ppp->nextseq = seq + 1;
1655 * If this is the ending fragment of a packet,
1656 * and we haven't found a complete valid packet yet,
1657 * we can discard up to and including this fragment.
1659 if (p->BEbits & E)
1660 head = next;
1662 ++seq;
1665 /* If we have a complete packet, copy it all into one skb. */
1666 if (tail != NULL) {
1667 /* If we have discarded any fragments,
1668 signal a receive error. */
1669 if (head->sequence != ppp->nextseq) {
1670 if (ppp->debug & 1)
1671 printk(KERN_DEBUG " missed pkts %u..%u\n",
1672 ppp->nextseq, head->sequence-1);
1673 ++ppp->stats.rx_dropped;
1674 ppp_receive_error(ppp);
1677 if (head != tail)
1678 /* copy to a single skb */
1679 for (p = head; p != tail->next; p = p->next)
1680 memcpy(skb_put(skb, p->len), p->data, p->len);
1681 ppp->nextseq = tail->sequence + 1;
1682 head = tail->next;
1685 /* Discard all the skbuffs that we have copied the data out of
1686 or that we can't use. */
1687 while ((p = list->next) != head) {
1688 __skb_unlink(p, list);
1689 kfree_skb(p);
1692 return skb;
1694 #endif /* CONFIG_PPP_MULTILINK */
1697 * Channel interface.
1701 * Create a new, unattached ppp channel.
1704 ppp_register_channel(struct ppp_channel *chan)
1706 struct channel *pch;
1708 pch = kmalloc(sizeof(struct channel), GFP_ATOMIC);
1709 if (pch == 0)
1710 return -ENOMEM;
1711 memset(pch, 0, sizeof(struct channel));
1712 pch->ppp = NULL;
1713 pch->chan = chan;
1714 chan->ppp = pch;
1715 init_ppp_file(&pch->file, CHANNEL);
1716 pch->file.hdrlen = chan->hdrlen;
1717 #ifdef CONFIG_PPP_MULTILINK
1718 pch->lastseq = -1;
1719 #endif /* CONFIG_PPP_MULTILINK */
1720 spin_lock_init(&pch->downl);
1721 pch->upl = RW_LOCK_UNLOCKED;
1722 spin_lock_bh(&all_channels_lock);
1723 pch->file.index = ++last_channel_index;
1724 list_add(&pch->file.list, &all_channels);
1725 spin_unlock_bh(&all_channels_lock);
1726 MOD_INC_USE_COUNT;
1727 return 0;
1731 * Return the index of a channel.
1733 int ppp_channel_index(struct ppp_channel *chan)
1735 struct channel *pch = chan->ppp;
1737 return pch->file.index;
1741 * Return the PPP unit number to which a channel is connected.
1743 int ppp_unit_number(struct ppp_channel *chan)
1745 struct channel *pch = chan->ppp;
1746 int unit = -1;
1748 if (pch != 0) {
1749 read_lock_bh(&pch->upl);
1750 if (pch->ppp != 0)
1751 unit = pch->ppp->file.index;
1752 read_unlock_bh(&pch->upl);
1754 return unit;
1758 * Disconnect a channel from the generic layer.
1759 * This can be called from mainline or BH/softirq level.
1761 void
1762 ppp_unregister_channel(struct ppp_channel *chan)
1764 struct channel *pch = chan->ppp;
1766 if (pch == 0)
1767 return; /* should never happen */
1768 chan->ppp = 0;
1771 * This ensures that we have returned from any calls into the
1772 * the channel's start_xmit or ioctl routine before we proceed.
1774 spin_lock_bh(&pch->downl);
1775 pch->chan = 0;
1776 spin_unlock_bh(&pch->downl);
1777 ppp_disconnect_channel(pch);
1778 wake_up_interruptible(&pch->file.rwait);
1779 spin_lock_bh(&all_channels_lock);
1780 list_del(&pch->file.list);
1781 spin_unlock_bh(&all_channels_lock);
1782 if (atomic_dec_and_test(&pch->file.refcnt))
1783 ppp_destroy_channel(pch);
1784 MOD_DEC_USE_COUNT;
1788 * Callback from a channel when it can accept more to transmit.
1789 * This should be called at BH/softirq level, not interrupt level.
1791 void
1792 ppp_output_wakeup(struct ppp_channel *chan)
1794 struct channel *pch = chan->ppp;
1795 struct ppp *ppp;
1797 if (pch == 0)
1798 return;
1799 ppp_channel_push(pch);
1800 if (skb_queue_len(&pch->file.xq) == 0) {
1801 read_lock_bh(&pch->upl);
1802 ppp = pch->ppp;
1803 if (ppp != 0)
1804 ppp_xmit_process(ppp, 1);
1805 read_unlock_bh(&pch->upl);
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 unsigned int
1834 ppp_channel_poll(struct ppp_channel *chan, struct file *file, poll_table *wait)
1836 unsigned int mask;
1837 struct channel *pch = chan->ppp;
1839 mask = POLLOUT | POLLWRNORM;
1840 if (pch != 0) {
1841 poll_wait(file, &pch->file.rwait, wait);
1842 if (skb_peek(&pch->file.rq) != 0)
1843 mask |= POLLIN | POLLRDNORM;
1845 return mask;
1848 int ppp_channel_ioctl(struct ppp_channel *chan, unsigned int cmd,
1849 unsigned long arg)
1851 struct channel *pch = chan->ppp;
1852 int err = -ENOTTY;
1853 int unit;
1855 if (!capable(CAP_NET_ADMIN))
1856 return -EPERM;
1857 if (pch == 0)
1858 return -EINVAL;
1859 switch (cmd) {
1860 case PPPIOCATTACH:
1861 if (get_user(unit, (int *) arg))
1862 break;
1863 err = ppp_connect_channel(pch, unit);
1864 break;
1865 case PPPIOCDETACH:
1866 err = ppp_disconnect_channel(pch);
1867 break;
1869 return err;
1873 * Compression control.
1876 /* Process the PPPIOCSCOMPRESS ioctl. */
1877 static int
1878 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1880 int err;
1881 struct compressor *cp;
1882 struct ppp_option_data data;
1883 void *state;
1884 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1885 #ifdef CONFIG_KMOD
1886 char modname[32];
1887 #endif
1889 err = -EFAULT;
1890 if (copy_from_user(&data, (void *) arg, sizeof(data))
1891 || (data.length <= CCP_MAX_OPTION_LENGTH
1892 && copy_from_user(ccp_option, data.ptr, data.length)))
1893 goto out;
1894 err = -EINVAL;
1895 if (data.length > CCP_MAX_OPTION_LENGTH
1896 || ccp_option[1] < 2 || ccp_option[1] > data.length)
1897 goto out;
1899 cp = find_compressor(ccp_option[0]);
1900 #ifdef CONFIG_KMOD
1901 if (cp == 0) {
1902 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
1903 request_module(modname);
1904 cp = find_compressor(ccp_option[0]);
1906 #endif /* CONFIG_KMOD */
1907 if (cp == 0)
1908 goto out;
1910 err = -ENOBUFS;
1911 if (data.transmit) {
1912 ppp_xmit_lock(ppp);
1913 ppp->xstate &= ~SC_COMP_RUN;
1914 if (ppp->xc_state != 0) {
1915 ppp->xcomp->comp_free(ppp->xc_state);
1916 ppp->xc_state = 0;
1918 ppp_xmit_unlock(ppp);
1920 state = cp->comp_alloc(ccp_option, data.length);
1921 if (state != 0) {
1922 ppp_xmit_lock(ppp);
1923 ppp->xcomp = cp;
1924 ppp->xc_state = state;
1925 ppp_xmit_unlock(ppp);
1926 err = 0;
1929 } else {
1930 ppp_recv_lock(ppp);
1931 ppp->rstate &= ~SC_DECOMP_RUN;
1932 if (ppp->rc_state != 0) {
1933 ppp->rcomp->decomp_free(ppp->rc_state);
1934 ppp->rc_state = 0;
1936 ppp_recv_unlock(ppp);
1938 state = cp->decomp_alloc(ccp_option, data.length);
1939 if (state != 0) {
1940 ppp_recv_lock(ppp);
1941 ppp->rcomp = cp;
1942 ppp->rc_state = state;
1943 ppp_recv_unlock(ppp);
1944 err = 0;
1948 out:
1949 return err;
1953 * Look at a CCP packet and update our state accordingly.
1954 * We assume the caller has the xmit or recv path locked.
1956 static void
1957 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
1959 unsigned char *dp = skb->data + 2;
1960 int len;
1962 if (skb->len < CCP_HDRLEN + 2
1963 || skb->len < (len = CCP_LENGTH(dp)) + 2)
1964 return; /* too short */
1966 switch (CCP_CODE(dp)) {
1967 case CCP_CONFREQ:
1968 case CCP_TERMREQ:
1969 case CCP_TERMACK:
1971 * CCP is going down - disable compression.
1973 if (inbound)
1974 ppp->rstate &= ~SC_DECOMP_RUN;
1975 else
1976 ppp->xstate &= ~SC_COMP_RUN;
1977 break;
1979 case CCP_CONFACK:
1980 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
1981 break;
1982 dp += CCP_HDRLEN;
1983 len -= CCP_HDRLEN;
1984 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
1985 break;
1986 if (inbound) {
1987 /* we will start receiving compressed packets */
1988 if (ppp->rc_state == 0)
1989 break;
1990 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
1991 ppp->file.index, 0, ppp->mru, ppp->debug)) {
1992 ppp->rstate |= SC_DECOMP_RUN;
1993 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
1995 } else {
1996 /* we will soon start sending compressed packets */
1997 if (ppp->xc_state == 0)
1998 break;
1999 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2000 ppp->file.index, 0, ppp->debug))
2001 ppp->xstate |= SC_COMP_RUN;
2003 break;
2005 case CCP_RESETACK:
2006 /* reset the [de]compressor */
2007 if ((ppp->flags & SC_CCP_UP) == 0)
2008 break;
2009 if (inbound) {
2010 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2011 ppp->rcomp->decomp_reset(ppp->rc_state);
2012 ppp->rstate &= ~SC_DC_ERROR;
2014 } else {
2015 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2016 ppp->xcomp->comp_reset(ppp->xc_state);
2018 break;
2022 /* Free up compression resources. */
2023 static void
2024 ppp_ccp_closed(struct ppp *ppp)
2026 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2028 ppp->xstate &= ~SC_COMP_RUN;
2029 if (ppp->xc_state) {
2030 ppp->xcomp->comp_free(ppp->xc_state);
2031 ppp->xc_state = 0;
2034 ppp->xstate &= ~SC_DECOMP_RUN;
2035 if (ppp->rc_state) {
2036 ppp->rcomp->decomp_free(ppp->rc_state);
2037 ppp->rc_state = 0;
2041 /* List of compressors. */
2042 static LIST_HEAD(compressor_list);
2043 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2045 struct compressor_entry {
2046 struct list_head list;
2047 struct compressor *comp;
2050 static struct compressor_entry *
2051 find_comp_entry(int proto)
2053 struct compressor_entry *ce;
2054 struct list_head *list = &compressor_list;
2056 while ((list = list->next) != &compressor_list) {
2057 ce = list_entry(list, struct compressor_entry, list);
2058 if (ce->comp->compress_proto == proto)
2059 return ce;
2061 return 0;
2064 /* Register a compressor */
2066 ppp_register_compressor(struct compressor *cp)
2068 struct compressor_entry *ce;
2069 int ret;
2071 spin_lock(&compressor_list_lock);
2072 ret = -EEXIST;
2073 if (find_comp_entry(cp->compress_proto) != 0)
2074 goto out;
2075 ret = -ENOMEM;
2076 ce = kmalloc(sizeof(struct compressor_entry), GFP_KERNEL);
2077 if (ce == 0)
2078 goto out;
2079 ret = 0;
2080 ce->comp = cp;
2081 list_add(&ce->list, &compressor_list);
2082 out:
2083 spin_unlock(&compressor_list_lock);
2084 return ret;
2087 /* Unregister a compressor */
2088 void
2089 ppp_unregister_compressor(struct compressor *cp)
2091 struct compressor_entry *ce;
2093 spin_lock(&compressor_list_lock);
2094 ce = find_comp_entry(cp->compress_proto);
2095 if (ce != 0 && ce->comp == cp) {
2096 list_del(&ce->list);
2097 kfree(ce);
2099 spin_unlock(&compressor_list_lock);
2102 /* Find a compressor. */
2103 static struct compressor *
2104 find_compressor(int type)
2106 struct compressor_entry *ce;
2107 struct compressor *cp = 0;
2109 spin_lock(&compressor_list_lock);
2110 ce = find_comp_entry(type);
2111 if (ce != 0)
2112 cp = ce->comp;
2113 spin_unlock(&compressor_list_lock);
2114 return cp;
2118 * Miscelleneous stuff.
2121 static void
2122 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2124 struct slcompress *vj = ppp->vj;
2126 memset(st, 0, sizeof(*st));
2127 st->p.ppp_ipackets = ppp->stats.rx_packets;
2128 st->p.ppp_ierrors = ppp->stats.rx_errors;
2129 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2130 st->p.ppp_opackets = ppp->stats.tx_packets;
2131 st->p.ppp_oerrors = ppp->stats.tx_errors;
2132 st->p.ppp_obytes = ppp->stats.tx_bytes;
2133 if (vj == 0)
2134 return;
2135 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2136 st->vj.vjs_compressed = vj->sls_o_compressed;
2137 st->vj.vjs_searches = vj->sls_o_searches;
2138 st->vj.vjs_misses = vj->sls_o_misses;
2139 st->vj.vjs_errorin = vj->sls_i_error;
2140 st->vj.vjs_tossed = vj->sls_i_tossed;
2141 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2142 st->vj.vjs_compressedin = vj->sls_i_compressed;
2146 * Stuff for handling the lists of ppp units and channels
2147 * and for initialization.
2151 * Create a new ppp interface unit. Fails if it can't allocate memory
2152 * or if there is already a unit with the requested number.
2153 * unit == -1 means allocate a new number.
2155 static struct ppp *
2156 ppp_create_interface(int unit, int *retp)
2158 struct ppp *ppp;
2159 struct net_device *dev;
2160 struct list_head *list;
2161 int last_unit = -1;
2162 int ret = -EEXIST;
2163 int i;
2165 spin_lock(&all_ppp_lock);
2166 list = &all_ppp_units;
2167 while ((list = list->next) != &all_ppp_units) {
2168 ppp = list_entry(list, struct ppp, file.list);
2169 if ((unit < 0 && ppp->file.index > last_unit + 1)
2170 || (unit >= 0 && unit < ppp->file.index))
2171 break;
2172 if (unit == ppp->file.index)
2173 goto out; /* unit already exists */
2174 last_unit = ppp->file.index;
2176 if (unit < 0)
2177 unit = last_unit + 1;
2179 /* Create a new ppp structure and link it before `list'. */
2180 ret = -ENOMEM;
2181 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2182 if (ppp == 0)
2183 goto out;
2184 memset(ppp, 0, sizeof(struct ppp));
2185 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
2186 if (dev == 0) {
2187 kfree(ppp);
2188 goto out;
2190 memset(dev, 0, sizeof(struct net_device));
2192 ppp->file.index = unit;
2193 sprintf(ppp->name, "ppp%d", 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 dev->name = ppp->name;
2209 dev->priv = ppp;
2210 dev->new_style = 1;
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;
2380 write_lock_bh(&pch->upl);
2381 ppp = pch->ppp;
2382 if (ppp != 0) {
2383 /* remove it from the ppp unit's list */
2384 pch->ppp = NULL;
2385 ppp_lock(ppp);
2386 list_del(&pch->clist);
2387 --ppp->n_channels;
2388 if (ppp->dev == 0 && ppp->n_channels == 0)
2389 /* Last disconnect from a ppp unit
2390 that is already dead: free it. */
2391 kfree(ppp);
2392 else
2393 ppp_unlock(ppp);
2394 err = 0;
2396 write_unlock_bh(&pch->upl);
2397 return err;
2401 * Free up the resources used by a ppp channel.
2403 static void ppp_destroy_channel(struct channel *pch)
2405 skb_queue_purge(&pch->file.xq);
2406 skb_queue_purge(&pch->file.rq);
2407 kfree(pch);
2410 void __exit ppp_cleanup(void)
2412 /* should never happen */
2413 if (!list_empty(&all_ppp_units) || !list_empty(&all_channels))
2414 printk(KERN_ERR "PPP: removing module but units remain!\n");
2415 if (devfs_unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2416 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2417 devfs_unregister(devfs_handle);
2420 module_init(ppp_init);
2421 module_exit(ppp_cleanup);
2423 EXPORT_SYMBOL(ppp_register_channel);
2424 EXPORT_SYMBOL(ppp_unregister_channel);
2425 EXPORT_SYMBOL(ppp_channel_index);
2426 EXPORT_SYMBOL(ppp_unit_number);
2427 EXPORT_SYMBOL(ppp_input);
2428 EXPORT_SYMBOL(ppp_input_error);
2429 EXPORT_SYMBOL(ppp_output_wakeup);
2430 EXPORT_SYMBOL(ppp_register_compressor);
2431 EXPORT_SYMBOL(ppp_unregister_compressor);
2432 EXPORT_SYMBOL(ppp_channel_read);
2433 EXPORT_SYMBOL(ppp_channel_write);
2434 EXPORT_SYMBOL(ppp_channel_poll);
2435 EXPORT_SYMBOL(ppp_channel_ioctl);
2436 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2437 EXPORT_SYMBOL(all_channels); /* for debugging */