Changes for kernel and Busybox
[tomato.git] / release / src / linux / linux / drivers / net / ppp_generic.c
blobf680a17c6e8d12b49cc4d040f9af90b361e4d12f
1 /*
2 * Generic PPP layer for Linux.
4 * Copyright 1999-2002 Paul Mackerras.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * The generic PPP layer handles the PPP network interfaces, the
12 * /dev/ppp device, packet and VJ compression, and multilink.
13 * It talks to PPP `channels' via the interface defined in
14 * include/linux/ppp_channel.h. Channels provide the basic means for
15 * sending and receiving PPP frames on some kind of communications
16 * channel.
18 * Part of the code in this driver was inspired by the old async-only
19 * PPP driver, written by Michael Callahan and Al Longyear, and
20 * subsequently hacked by Paul Mackerras.
22 * ==FILEVERSION 20040509==
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/inetdevice.h>
34 #include <linux/poll.h>
35 #include <linux/ppp_defs.h>
36 #include <linux/filter.h>
37 #include <linux/if_ppp.h>
38 #include <linux/ppp_channel.h>
39 #include <linux/ppp-comp.h>
40 #include <linux/skbuff.h>
41 #include <linux/rtnetlink.h>
42 #include <linux/if_arp.h>
43 #include <linux/ip.h>
44 #include <linux/tcp.h>
45 #include <linux/spinlock.h>
46 #include <linux/smp_lock.h>
47 #include <linux/rwsem.h>
48 #include <linux/stddef.h>
49 #include <net/slhc_vj.h>
50 #include <asm/atomic.h>
51 #include <linux/sysctl.h>
53 #define PPP_VERSION "2.4.2"
56 * Network protocols we support.
58 #define NP_IP 0 /* Internet Protocol V4 */
59 #define NP_IPV6 1 /* Internet Protocol V6 */
60 #define NP_IPX 2 /* IPX protocol */
61 #define NP_AT 3 /* Appletalk protocol */
62 #define NUM_NP 4 /* Number of NPs. */
64 #define MPHDRLEN 6 /* multilink protocol header length */
65 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
66 #define MIN_FRAG_SIZE 64
69 * An instance of /dev/ppp can be associated with either a ppp
70 * interface unit or a ppp channel. In both cases, file->private_data
71 * points to one of these.
73 struct ppp_file {
74 enum {
75 INTERFACE=1, CHANNEL
76 } kind;
77 struct sk_buff_head xq; /* pppd transmit queue */
78 struct sk_buff_head rq; /* receive queue for pppd */
79 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
80 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
81 int hdrlen; /* space to leave for headers */
82 int index; /* interface unit / channel number */
83 int dead; /* unit/channel has been shut down */
86 #define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
88 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
89 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
91 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
94 * Data structure describing one ppp unit.
95 * A ppp unit corresponds to a ppp network interface device
96 * and represents a multilink bundle.
97 * It can have 0 or more ppp channels connected to it.
99 struct ppp {
100 struct ppp_file file; /* stuff for read/write/poll 0 */
101 struct file *owner; /* file that owns this unit 48 */
102 struct list_head channels; /* list of attached channels 4c */
103 int n_channels; /* how many channels are attached 54 */
104 spinlock_t rlock; /* lock for receive side 58 */
105 spinlock_t wlock; /* lock for transmit side 5c */
106 int mru; /* max receive unit 60 */
107 int mru_alloc; /* MAX(1500,MRU) for dev_alloc_skb() */
108 unsigned int flags; /* control bits 64 */
109 unsigned int xstate; /* transmit state bits 68 */
110 unsigned int rstate; /* receive state bits 6c */
111 int debug; /* debug flags 70 */
112 struct slcompress *vj; /* state for VJ header compression */
113 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
114 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
115 struct compressor *xcomp; /* transmit packet compressor 8c */
116 void *xc_state; /* its internal state 90 */
117 struct compressor *rcomp; /* receive decompressor 94 */
118 void *rc_state; /* its internal state 98 */
119 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
120 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
121 struct net_device *dev; /* network interface device a4 */
122 #ifdef CONFIG_PPP_MULTILINK
123 int nxchan; /* next channel to send something on */
124 u32 nxseq; /* next sequence number to send */
125 int mrru; /* MP: max reconst. receive unit */
126 u32 nextseq; /* MP: seq no of next packet */
127 u32 minseq; /* MP: min of most recent seqnos */
128 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
129 #endif /* CONFIG_PPP_MULTILINK */
130 struct net_device_stats stats; /* statistics */
131 #ifdef CONFIG_PPP_FILTER
132 struct sock_fprog pass_filter; /* filter for packets to pass */
133 struct sock_fprog active_filter;/* filter for pkts to reset idle */
134 #endif /* CONFIG_PPP_FILTER */
138 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
139 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
140 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
141 * Bits in xstate: SC_COMP_RUN
143 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
144 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
145 |SC_COMP_TCP|SC_REJ_COMP_TCP)
148 * Private data structure for each channel.
149 * This includes the data structure used for multilink.
151 struct channel {
152 struct ppp_file file; /* stuff for read/write/poll */
153 struct list_head list; /* link in all/new_channels list */
154 struct ppp_channel *chan; /* public channel data structure */
155 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
156 spinlock_t downl; /* protects `chan', file.xq dequeue */
157 struct ppp *ppp; /* ppp unit we're connected to */
158 struct list_head clist; /* link in list of channels per unit */
159 rwlock_t upl; /* protects `ppp' */
160 #ifdef CONFIG_PPP_MULTILINK
161 u8 avail; /* flag used in multilink stuff */
162 u8 had_frag; /* >= 1 fragments have been sent */
163 u32 lastseq; /* MP: last sequence # received */
164 #endif /* CONFIG_PPP_MULTILINK */
168 * SMP locking issues:
169 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
170 * list and the ppp.n_channels field, you need to take both locks
171 * before you modify them.
172 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
173 * channel.downl.
176 static int ppp_filter = 0;
177 static ctl_table ppp_filter_root_table[];
178 static struct ctl_table_header *ppp_filter_sysctl_header;
181 * A cardmap represents a mapping from unsigned integers to pointers,
182 * and provides a fast "find lowest unused number" operation.
183 * It uses a broad (32-way) tree with a bitmap at each level.
184 * It is designed to be space-efficient for small numbers of entries
185 * and time-efficient for large numbers of entries.
187 #define CARDMAP_ORDER 5
188 #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
189 #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
191 struct cardmap {
192 int shift;
193 unsigned long inuse;
194 struct cardmap *parent;
195 void *ptr[CARDMAP_WIDTH];
197 static void *cardmap_get(struct cardmap *map, unsigned int nr);
198 static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
199 static unsigned int cardmap_find_first_free(struct cardmap *map);
200 static void cardmap_destroy(struct cardmap **map);
203 * all_ppp_sem protects the all_ppp_units mapping.
204 * It also ensures that finding a ppp unit in the all_ppp_units map
205 * and updating its file.refcnt field is atomic.
207 static DECLARE_MUTEX(all_ppp_sem);
208 static struct cardmap *all_ppp_units;
209 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
212 * all_channels_lock protects all_channels and last_channel_index,
213 * and the atomicity of find a channel and updating its file.refcnt
214 * field.
216 static spinlock_t all_channels_lock = SPIN_LOCK_UNLOCKED;
217 static LIST_HEAD(all_channels);
218 static LIST_HEAD(new_channels);
219 static int last_channel_index;
220 static atomic_t channel_count = ATOMIC_INIT(0);
222 /* Get the PPP protocol number from a skb */
223 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
225 // zzz
226 #define IP_PROTO(skb) (skb)->data[11]
227 #define SRC_PORT(skb) (((skb)->data[22] << 8) + (skb)->data[23])
228 #define DST_PORT(skb) (((skb)->data[24] << 8) + (skb)->data[25])
229 //#define MARK_LAN2WAN 0x100
231 /* We limit the length of ppp->file.rq to this (arbitrary) value */
232 #define PPP_MAX_RQLEN 32
235 * Maximum number of multilink fragments queued up.
236 * This has to be large enough to cope with the maximum latency of
237 * the slowest channel relative to the others. Strictly it should
238 * depend on the number of channels and their characteristics.
240 #define PPP_MP_MAX_QLEN 128
242 /* Multilink header bits. */
243 #define B 0x80 /* this fragment begins a packet */
244 #define E 0x40 /* this fragment ends a packet */
246 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
247 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
248 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
250 /* Prototypes. */
251 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
252 unsigned int cmd, unsigned long arg);
253 static void ppp_xmit_process(struct ppp *ppp);
254 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
255 static void ppp_push(struct ppp *ppp);
256 static void ppp_channel_push(struct channel *pch);
257 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
258 struct channel *pch);
259 static void ppp_receive_error(struct ppp *ppp);
260 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
261 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
262 struct sk_buff *skb);
263 #ifdef CONFIG_PPP_MULTILINK
264 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
265 struct channel *pch);
266 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
267 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
268 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
269 #endif /* CONFIG_PPP_MULTILINK */
270 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
271 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
272 static void ppp_ccp_closed(struct ppp *ppp);
273 static struct compressor *find_compressor(int type);
274 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
275 static struct ppp *ppp_create_interface(int unit, int *retp);
276 static void init_ppp_file(struct ppp_file *pf, int kind);
277 static void ppp_shutdown_interface(struct ppp *ppp);
278 static void ppp_destroy_interface(struct ppp *ppp);
279 static struct ppp *ppp_find_unit(int unit);
280 static struct channel *ppp_find_channel(int unit);
281 static int ppp_connect_channel(struct channel *pch, int unit);
282 static int ppp_disconnect_channel(struct channel *pch);
283 static void ppp_destroy_channel(struct channel *pch);
285 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
286 static inline int proto_to_npindex(int proto)
288 switch (proto) {
289 case PPP_IP:
290 return NP_IP;
291 case PPP_IPV6:
292 return NP_IPV6;
293 case PPP_IPX:
294 return NP_IPX;
295 case PPP_AT:
296 return NP_AT;
298 return -EINVAL;
301 /* Translates an NP index into a PPP protocol number */
302 static const int npindex_to_proto[NUM_NP] = {
303 PPP_IP,
304 PPP_IPV6,
305 PPP_IPX,
306 PPP_AT,
309 /* Translates an ethertype into an NP index */
310 static inline int ethertype_to_npindex(int ethertype)
312 switch (ethertype) {
313 case ETH_P_IP:
314 return NP_IP;
315 case ETH_P_IPV6:
316 return NP_IPV6;
317 case ETH_P_IPX:
318 return NP_IPX;
319 case ETH_P_PPPTALK:
320 case ETH_P_ATALK:
321 return NP_AT;
323 return -1;
326 /* Translates an NP index into an ethertype */
327 static const int npindex_to_ethertype[NUM_NP] = {
328 ETH_P_IP,
329 ETH_P_IPV6,
330 ETH_P_IPX,
331 ETH_P_PPPTALK,
335 * Locking shorthand.
337 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
338 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
339 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
340 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
341 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
342 ppp_recv_lock(ppp); } while (0)
343 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
344 ppp_xmit_unlock(ppp); } while (0)
347 * /dev/ppp device routines.
348 * The /dev/ppp device is used by pppd to control the ppp unit.
349 * It supports the read, write, ioctl and poll functions.
350 * Open instances of /dev/ppp can be in one of three states:
351 * unattached, attached to a ppp unit, or attached to a ppp channel.
353 static int ppp_open(struct inode *inode, struct file *file)
356 * This could (should?) be enforced by the permissions on /dev/ppp.
358 if (!capable(CAP_NET_ADMIN))
359 return -EPERM;
360 return 0;
363 static int ppp_release(struct inode *inode, struct file *file)
365 struct ppp_file *pf = file->private_data;
366 struct ppp *ppp;
368 if (pf != 0) {
369 file->private_data = 0;
370 if (pf->kind == INTERFACE) {
371 ppp = PF_TO_PPP(pf);
372 if (file == ppp->owner)
373 ppp_shutdown_interface(ppp);
375 if (atomic_dec_and_test(&pf->refcnt)) {
376 switch (pf->kind) {
377 case INTERFACE:
378 ppp_destroy_interface(PF_TO_PPP(pf));
379 break;
380 case CHANNEL:
381 ppp_destroy_channel(PF_TO_CHANNEL(pf));
382 break;
386 return 0;
389 static ssize_t ppp_read(struct file *file, char *buf,
390 size_t count, loff_t *ppos)
392 struct ppp_file *pf = file->private_data;
393 DECLARE_WAITQUEUE(wait, current);
394 ssize_t ret = 0;
395 struct sk_buff *skb = 0;
397 if (pf == 0)
398 return -ENXIO;
399 add_wait_queue(&pf->rwait, &wait);
400 for (;;) {
401 set_current_state(TASK_INTERRUPTIBLE);
402 skb = skb_dequeue(&pf->rq);
403 if (skb)
404 break;
405 ret = 0;
406 if (pf->dead)
407 break;
408 ret = -EAGAIN;
409 if (file->f_flags & O_NONBLOCK)
410 break;
411 ret = -ERESTARTSYS;
412 if (signal_pending(current))
413 break;
414 schedule();
416 set_current_state(TASK_RUNNING);
417 remove_wait_queue(&pf->rwait, &wait);
419 if (skb == 0)
420 goto err1;
422 ret = -EOVERFLOW;
423 if (skb->len > count)
424 goto err2;
425 ret = -EFAULT;
426 if (copy_to_user(buf, skb->data, skb->len))
427 goto err2;
428 ret = skb->len;
430 err2:
431 kfree_skb(skb);
432 err1:
433 return ret;
436 static ssize_t ppp_write(struct file *file, const char *buf,
437 size_t count, loff_t *ppos)
439 struct ppp_file *pf = file->private_data;
440 struct sk_buff *skb;
441 ssize_t ret;
443 if (pf == 0)
444 return -ENXIO;
445 ret = -ENOMEM;
446 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
447 if (skb == 0)
448 goto err1;
449 skb_reserve(skb, pf->hdrlen);
450 ret = -EFAULT;
451 if (copy_from_user(skb_put(skb, count), buf, count)) {
452 kfree_skb(skb);
453 goto err1;
456 skb_queue_tail(&pf->xq, skb);
458 switch (pf->kind) {
459 case INTERFACE:
460 ppp_xmit_process(PF_TO_PPP(pf));
461 break;
462 case CHANNEL:
463 ppp_channel_push(PF_TO_CHANNEL(pf));
464 break;
467 ret = count;
469 err1:
470 return ret;
473 /* No kernel lock - fine */
474 static unsigned int ppp_poll(struct file *file, poll_table *wait)
476 struct ppp_file *pf = file->private_data;
477 unsigned int mask;
479 if (pf == 0)
480 return 0;
481 poll_wait(file, &pf->rwait, wait);
482 mask = POLLOUT | POLLWRNORM;
483 if (skb_peek(&pf->rq) != 0)
484 mask |= POLLIN | POLLRDNORM;
485 if (pf->dead)
486 mask |= POLLHUP;
487 return mask;
490 static int ppp_ioctl(struct inode *inode, struct file *file,
491 unsigned int cmd, unsigned long arg)
493 struct ppp_file *pf = file->private_data;
494 struct ppp *ppp;
495 int err = -EFAULT, val, val2, i;
496 struct ppp_idle idle;
497 struct npioctl npi;
498 int unit, cflags;
499 struct slcompress *vj;
501 if (pf == 0)
502 return ppp_unattached_ioctl(pf, file, cmd, arg);
504 if (cmd == PPPIOCDETACH) {
506 * We have to be careful here... if the file descriptor
507 * has been dup'd, we could have another process in the
508 * middle of a poll using the same file *, so we had
509 * better not free the interface data structures -
510 * instead we fail the ioctl. Even in this case, we
511 * shut down the interface if we are the owner of it.
512 * Actually, we should get rid of PPPIOCDETACH, userland
513 * (i.e. pppd) could achieve the same effect by closing
514 * this fd and reopening /dev/ppp.
516 err = -EINVAL;
517 if (pf->kind == INTERFACE) {
518 ppp = PF_TO_PPP(pf);
519 if (file == ppp->owner)
520 ppp_shutdown_interface(ppp);
522 if (atomic_read(&file->f_count) <= 2) {
523 ppp_release(inode, file);
524 err = 0;
525 } else
526 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
527 atomic_read(&file->f_count));
528 return err;
531 if (pf->kind == CHANNEL) {
532 struct channel *pch = PF_TO_CHANNEL(pf);
533 struct ppp_channel *chan;
535 switch (cmd) {
536 case PPPIOCCONNECT:
537 if (get_user(unit, (int *) arg))
538 break;
539 err = ppp_connect_channel(pch, unit);
540 break;
542 case PPPIOCDISCONN:
543 err = ppp_disconnect_channel(pch);
544 break;
546 default:
547 down_read(&pch->chan_sem);
548 chan = pch->chan;
549 err = -ENOTTY;
550 if (chan && chan->ops->ioctl)
551 err = chan->ops->ioctl(chan, cmd, arg);
552 up_read(&pch->chan_sem);
554 return err;
557 if (pf->kind != INTERFACE) {
558 /* can't happen */
559 printk(KERN_ERR "PPP: not interface or channel??\n");
560 return -EINVAL;
563 ppp = PF_TO_PPP(pf);
564 switch (cmd) {
565 case PPPIOCSMRU:
566 if (get_user(val, (int *) arg))
567 break;
568 ppp->mru_alloc = ppp->mru = val;
569 if (ppp->mru_alloc < PPP_MRU)
570 ppp->mru_alloc = PPP_MRU; /* increase for broken peers */
571 err = 0;
572 break;
574 case PPPIOCSFLAGS:
575 if (get_user(val, (int *) arg))
576 break;
577 ppp_lock(ppp);
578 cflags = ppp->flags & ~val;
579 ppp->flags = val & SC_FLAG_BITS;
580 ppp_unlock(ppp);
581 if (cflags & SC_CCP_OPEN)
582 ppp_ccp_closed(ppp);
583 err = 0;
584 break;
586 case PPPIOCGFLAGS:
587 val = ppp->flags | ppp->xstate | ppp->rstate;
588 if (put_user(val, (int *) arg))
589 break;
590 err = 0;
591 break;
593 case PPPIOCSCOMPRESS:
594 err = ppp_set_compress(ppp, arg);
595 break;
597 case PPPIOCGUNIT:
598 if (put_user(ppp->file.index, (int *) arg))
599 break;
600 err = 0;
601 break;
603 case PPPIOCSDEBUG:
604 if (get_user(val, (int *) arg))
605 break;
606 ppp->debug = val;
607 err = 0;
608 break;
610 case PPPIOCGDEBUG:
611 if (put_user(ppp->debug, (int *) arg))
612 break;
613 err = 0;
614 break;
616 case PPPIOCGIDLE:
617 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
618 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
619 if (copy_to_user((void *) arg, &idle, sizeof(idle)))
620 break;
621 err = 0;
622 break;
624 case PPPIOCSMAXCID:
625 if (get_user(val, (int *) arg))
626 break;
627 val2 = 15;
628 if ((val >> 16) != 0) {
629 val2 = val >> 16;
630 val &= 0xffff;
632 vj = slhc_init(val2+1, val+1);
633 if (vj == 0) {
634 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
635 err = -ENOMEM;
636 break;
638 ppp_lock(ppp);
639 if (ppp->vj != 0)
640 slhc_free(ppp->vj);
641 ppp->vj = vj;
642 ppp_unlock(ppp);
643 err = 0;
644 break;
646 case PPPIOCGNPMODE:
647 case PPPIOCSNPMODE:
648 if (copy_from_user(&npi, (void *) arg, sizeof(npi)))
649 break;
650 err = proto_to_npindex(npi.protocol);
651 if (err < 0)
652 break;
653 i = err;
654 if (cmd == PPPIOCGNPMODE) {
655 err = -EFAULT;
656 npi.mode = ppp->npmode[i];
657 if (copy_to_user((void *) arg, &npi, sizeof(npi)))
658 break;
659 } else {
660 ppp->npmode[i] = npi.mode;
661 /* we may be able to transmit more packets now (??) */
662 netif_wake_queue(ppp->dev);
664 err = 0;
665 break;
667 #ifdef CONFIG_PPP_FILTER
668 case PPPIOCSPASS:
669 case PPPIOCSACTIVE:
671 struct sock_fprog uprog, *filtp;
672 struct sock_filter *code = NULL;
673 int len;
675 if (copy_from_user(&uprog, (void *) arg, sizeof(uprog)))
676 break;
677 if (uprog.len > 0 && uprog.len < 65536) {
678 err = -ENOMEM;
679 len = uprog.len * sizeof(struct sock_filter);
680 code = kmalloc(len, GFP_KERNEL);
681 if (code == 0)
682 break;
683 err = -EFAULT;
684 if (copy_from_user(code, uprog.filter, len)) {
685 kfree(code);
686 break;
688 err = sk_chk_filter(code, uprog.len);
689 if (err) {
690 kfree(code);
691 break;
694 filtp = (cmd == PPPIOCSPASS)? &ppp->pass_filter: &ppp->active_filter;
695 ppp_lock(ppp);
696 if (filtp->filter)
697 kfree(filtp->filter);
698 filtp->filter = code;
699 filtp->len = uprog.len;
700 ppp_unlock(ppp);
701 err = 0;
702 break;
704 #endif /* CONFIG_PPP_FILTER */
706 #ifdef CONFIG_PPP_MULTILINK
707 case PPPIOCSMRRU:
708 if (get_user(val, (int *) arg))
709 break;
710 ppp_recv_lock(ppp);
711 ppp->mrru = val;
712 ppp_recv_unlock(ppp);
713 err = 0;
714 break;
715 #endif /* CONFIG_PPP_MULTILINK */
717 default:
718 err = -ENOTTY;
721 return err;
724 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
725 unsigned int cmd, unsigned long arg)
727 int unit, err = -EFAULT;
728 struct ppp *ppp;
729 struct channel *chan;
731 switch (cmd) {
732 case PPPIOCNEWUNIT:
733 /* Create a new ppp unit */
734 if (get_user(unit, (int *) arg))
735 break;
736 ppp = ppp_create_interface(unit, &err);
737 if (ppp == 0)
738 break;
739 file->private_data = &ppp->file;
740 ppp->owner = file;
741 err = -EFAULT;
742 if (put_user(ppp->file.index, (int *) arg))
743 break;
744 err = 0;
745 break;
747 case PPPIOCATTACH:
748 /* Attach to an existing ppp unit */
749 if (get_user(unit, (int *) arg))
750 break;
751 down(&all_ppp_sem);
752 err = -ENXIO;
753 ppp = ppp_find_unit(unit);
754 if (ppp != 0) {
755 atomic_inc(&ppp->file.refcnt);
756 file->private_data = &ppp->file;
757 err = 0;
759 up(&all_ppp_sem);
760 break;
762 case PPPIOCATTCHAN:
763 if (get_user(unit, (int *) arg))
764 break;
765 spin_lock_bh(&all_channels_lock);
766 err = -ENXIO;
767 chan = ppp_find_channel(unit);
768 if (chan != 0) {
769 atomic_inc(&chan->file.refcnt);
770 file->private_data = &chan->file;
771 err = 0;
773 spin_unlock_bh(&all_channels_lock);
774 break;
776 default:
777 err = -ENOTTY;
779 return err;
782 static struct file_operations ppp_device_fops = {
783 owner: THIS_MODULE,
784 read: ppp_read,
785 write: ppp_write,
786 poll: ppp_poll,
787 ioctl: ppp_ioctl,
788 open: ppp_open,
789 release: ppp_release
792 #define PPP_MAJOR 108
794 static devfs_handle_t devfs_handle;
796 /* Called at boot time if ppp is compiled into the kernel,
797 or at module load time (from init_module) if compiled as a module. */
798 int __init ppp_init(void)
800 int err;
802 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
803 err = devfs_register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
804 if (err)
805 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
806 devfs_handle = devfs_register(NULL, "ppp", DEVFS_FL_DEFAULT,
807 PPP_MAJOR, 0,
808 S_IFCHR | S_IRUSR | S_IWUSR,
809 &ppp_device_fops, NULL);
811 ppp_filter_sysctl_header
812 = register_sysctl_table(ppp_filter_root_table, 0);
814 return 0;
818 * Network interface unit routines.
820 static int
821 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
823 struct ppp *ppp = (struct ppp *) dev->priv;
824 int npi, proto;
825 unsigned char *pp;
827 npi = ethertype_to_npindex(ntohs(skb->protocol));
828 if (npi < 0)
829 goto err1;
831 /* Drop, accept or reject the packet */
832 switch (ppp->npmode[npi]) {
833 case NPMODE_PASS:
834 break;
835 case NPMODE_QUEUE:
836 /* it would be nice to have a way to tell the network
837 system to queue this one up for later. */
838 goto err1;
839 case NPMODE_DROP:
840 case NPMODE_ERROR:
841 goto err1;
844 /* Put the 2-byte PPP protocol number on the front,
845 making sure there is room for the address and control fields. */
846 if (skb_headroom(skb) < PPP_HDRLEN) {
847 struct sk_buff *ns;
849 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
850 if (ns == 0)
851 goto err1;
852 skb_reserve(ns, dev->hard_header_len);
853 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
854 kfree_skb(skb);
855 skb = ns;
857 pp = skb_push(skb, 2);
858 proto = npindex_to_proto[npi];
859 pp[0] = proto >> 8;
860 pp[1] = proto;
862 netif_stop_queue(dev);
863 skb_queue_tail(&ppp->file.xq, skb);
864 ppp_xmit_process(ppp);
865 return 0;
867 err1:
868 kfree_skb(skb);
869 ++ppp->stats.tx_dropped;
870 return 0;
873 static struct net_device_stats *
874 ppp_net_stats(struct net_device *dev)
876 struct ppp *ppp = (struct ppp *) dev->priv;
878 return &ppp->stats;
881 static int
882 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
884 struct ppp *ppp = dev->priv;
885 int err = -EFAULT;
886 void *addr = (void *) ifr->ifr_ifru.ifru_data;
887 struct ppp_stats stats;
888 struct ppp_comp_stats cstats;
889 char *vers;
891 switch (cmd) {
892 case SIOCGPPPSTATS:
893 ppp_get_stats(ppp, &stats);
894 if (copy_to_user(addr, &stats, sizeof(stats)))
895 break;
896 err = 0;
897 break;
899 case SIOCGPPPCSTATS:
900 memset(&cstats, 0, sizeof(cstats));
901 if (ppp->xc_state != 0)
902 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
903 if (ppp->rc_state != 0)
904 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
905 if (copy_to_user(addr, &cstats, sizeof(cstats)))
906 break;
907 err = 0;
908 break;
910 case SIOCGPPPVER:
911 vers = PPP_VERSION;
912 if (copy_to_user(addr, vers, strlen(vers) + 1))
913 break;
914 err = 0;
915 break;
917 default:
918 err = -EINVAL;
921 return err;
924 static int
925 ppp_net_init(struct net_device *dev)
927 dev->hard_header_len = PPP_HDRLEN;
928 dev->mtu = PPP_MTU;
929 dev->hard_start_xmit = ppp_start_xmit;
930 dev->get_stats = ppp_net_stats;
931 dev->do_ioctl = ppp_net_ioctl;
932 dev->addr_len = 0;
933 dev->tx_queue_len = 3;
934 dev->type = ARPHRD_PPP;
935 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
936 return 0;
940 * Transmit-side routines.
944 * Called to do any work queued up on the transmit side
945 * that can now be done.
947 static void
948 ppp_xmit_process(struct ppp *ppp)
950 struct sk_buff *skb;
952 ppp_xmit_lock(ppp);
953 if (ppp->dev != 0) {
954 ppp_push(ppp);
955 while (ppp->xmit_pending == 0
956 && (skb = skb_dequeue(&ppp->file.xq)) != 0)
957 ppp_send_frame(ppp, skb);
958 /* If there's no work left to do, tell the core net
959 code that we can accept some more. */
960 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
961 netif_wake_queue(ppp->dev);
963 ppp_xmit_unlock(ppp);
967 * Compress and send a frame.
968 * The caller should have locked the xmit path,
969 * and xmit_pending should be 0.
971 static void
972 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
974 int proto = PPP_PROTO(skb);
975 struct sk_buff *new_skb;
976 int len;
977 unsigned char *cp;
979 if (proto < 0x8000) {
980 #ifdef CONFIG_PPP_FILTER
981 /* check if we should pass this packet */
982 /* the filter instructions are constructed assuming
983 a four-byte PPP header on each packet */
984 *skb_push(skb, 2) = 1;
985 if (ppp->pass_filter.filter
986 && sk_run_filter(skb, ppp->pass_filter.filter,
987 ppp->pass_filter.len) == 0) {
988 if (ppp->debug & 1)
989 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
990 kfree_skb(skb);
991 return;
993 /* if this packet passes the active filter, record the time */
994 if (!(ppp->active_filter.filter
995 && sk_run_filter(skb, ppp->active_filter.filter,
996 ppp->active_filter.len) == 0))
997 ppp->last_xmit = jiffies;
998 skb_pull(skb, 2);
999 #else
1001 #if 1 // zzz
1002 switch (IP_PROTO(skb)) {
1003 case 6: // TCP
1004 switch (DST_PORT(skb)) {
1005 case 139: // netbios-ssn
1006 case 445: // microsoft-ds
1007 break;
1008 default:
1009 ppp->last_xmit = jiffies;
1010 break;
1012 break;
1013 case 17: // UDP
1014 switch (DST_PORT(skb)) {
1015 case 137: // netbios-ns
1016 case 138: // netbios-dgm
1017 break;
1018 default:
1019 ppp->last_xmit = jiffies;
1020 break;
1022 break;
1023 default:
1024 ppp->last_xmit = jiffies;
1025 break;
1027 #else
1028 /* for data packets, record the time */
1029 ppp->last_xmit = jiffies;
1030 #endif
1031 #endif /* CONFIG_PPP_FILTER */
1034 ++ppp->stats.tx_packets;
1035 ppp->stats.tx_bytes += skb->len - 2;
1037 switch (proto) {
1038 case PPP_IP:
1039 if (ppp_filter &&
1040 (((struct iphdr *)(skb->data + 2))->saddr &
1041 ((struct in_device *)(ppp->dev->ip_ptr))->ifa_list->ifa_mask)
1043 (((struct in_device *)(ppp->dev->ip_ptr))->ifa_list->ifa_local &
1044 ((struct in_device *)(ppp->dev->ip_ptr))->ifa_list->ifa_mask)
1047 kfree_skb(skb);
1048 return;
1051 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
1052 break;
1053 /* try to do VJ TCP header compression */
1054 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1055 GFP_ATOMIC);
1056 if (new_skb == 0) {
1057 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1058 goto drop;
1060 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1061 cp = skb->data + 2;
1062 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1063 new_skb->data + 2, &cp,
1064 !(ppp->flags & SC_NO_TCP_CCID));
1065 if (cp == skb->data + 2) {
1066 /* didn't compress */
1067 kfree_skb(new_skb);
1068 } else {
1069 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1070 proto = PPP_VJC_COMP;
1071 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1072 } else {
1073 proto = PPP_VJC_UNCOMP;
1074 cp[0] = skb->data[2];
1076 kfree_skb(skb);
1077 skb = new_skb;
1078 cp = skb_put(skb, len + 2);
1079 cp[0] = 0;
1080 cp[1] = proto;
1082 break;
1084 case PPP_CCP:
1085 /* peek at outbound CCP frames */
1086 ppp_ccp_peek(ppp, skb, 0);
1088 * When LZS or MPPE/MPPC has been negotiated we don't send
1089 * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
1090 * sends such a packet but we silently discard it here
1092 if (CCP_CODE(skb->data+2) == CCP_RESETACK
1093 && (ppp->xcomp->compress_proto == CI_MPPE
1094 || ppp->xcomp->compress_proto == CI_LZS)) {
1095 --ppp->stats.tx_packets;
1096 ppp->stats.tx_bytes -= skb->len - 2;
1097 kfree_skb(skb);
1098 return;
1100 break;
1103 /* try to do packet compression */
1104 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1105 && proto != PPP_LCP && proto != PPP_CCP) {
1106 int comp_ovhd = 0;
1108 * because of possible data expansion when MPPC or LZS
1109 * is used, allocate compressor's buffer 12.5% bigger
1110 * than MTU
1112 if (ppp->xcomp->compress_proto == CI_MPPE)
1113 comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
1114 else if (ppp->xcomp->compress_proto == CI_LZS)
1115 comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
1116 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
1117 + comp_ovhd, GFP_ATOMIC);
1118 if (new_skb == 0) {
1119 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1120 goto drop;
1122 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1123 skb_reserve(new_skb,
1124 ppp->dev->hard_header_len - PPP_HDRLEN);
1126 /* compressor still expects A/C bytes in hdr */
1127 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1128 new_skb->data, skb->len + 2,
1129 ppp->dev->mtu + PPP_HDRLEN);
1130 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1131 kfree_skb(skb);
1132 skb = new_skb;
1133 skb_put(skb, len);
1134 skb_pull(skb, 2); /* pull off A/C bytes */
1135 } else if (len == 0) {
1136 /* didn't compress, or CCP not up yet */
1137 kfree_skb(new_skb);
1138 } else {
1140 * (len < 0)
1141 * MPPE requires that we do not send unencrypted
1142 * frames. The compressor will return -1 if we
1143 * should drop the frame. We cannot simply test
1144 * the compress_proto because MPPE and MPPC share
1145 * the same number.
1147 printk(KERN_ERR "ppp: compressor dropped pkt\n");
1148 kfree_skb(new_skb);
1149 goto drop;
1154 * If we are waiting for traffic (demand dialling),
1155 * queue it up for pppd to receive.
1157 if (ppp->flags & SC_LOOP_TRAFFIC) {
1158 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1159 goto drop;
1160 skb_queue_tail(&ppp->file.rq, skb);
1161 wake_up_interruptible(&ppp->file.rwait);
1162 return;
1165 ppp->xmit_pending = skb;
1166 ppp_push(ppp);
1167 return;
1169 drop:
1170 kfree_skb(skb);
1171 ++ppp->stats.tx_errors;
1175 * Try to send the frame in xmit_pending.
1176 * The caller should have the xmit path locked.
1178 static void
1179 ppp_push(struct ppp *ppp)
1181 struct list_head *list;
1182 struct channel *pch;
1183 struct sk_buff *skb = ppp->xmit_pending;
1185 if (skb == 0)
1186 return;
1188 list = &ppp->channels;
1189 if (list_empty(list)) {
1190 /* nowhere to send the packet, just drop it */
1191 ppp->xmit_pending = 0;
1192 kfree_skb(skb);
1193 return;
1196 if ((ppp->flags & SC_MULTILINK) == 0) {
1197 /* not doing multilink: send it down the first channel */
1198 list = list->next;
1199 pch = list_entry(list, struct channel, clist);
1201 spin_lock_bh(&pch->downl);
1202 if (pch->chan) {
1203 if (pch->chan->ops->start_xmit(pch->chan, skb))
1204 ppp->xmit_pending = 0;
1205 } else {
1206 /* channel got unregistered */
1207 kfree_skb(skb);
1208 ppp->xmit_pending = 0;
1210 spin_unlock_bh(&pch->downl);
1211 return;
1214 #ifdef CONFIG_PPP_MULTILINK
1215 /* Multilink: fragment the packet over as many links
1216 as can take the packet at the moment. */
1217 if (!ppp_mp_explode(ppp, skb))
1218 return;
1219 #endif /* CONFIG_PPP_MULTILINK */
1221 ppp->xmit_pending = 0;
1222 kfree_skb(skb);
1225 #ifdef CONFIG_PPP_MULTILINK
1227 * Divide a packet to be transmitted into fragments and
1228 * send them out the individual links.
1230 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1232 int nch, len, fragsize;
1233 int i, bits, hdrlen, mtu;
1234 int flen, fnb;
1235 unsigned char *p, *q;
1236 struct list_head *list;
1237 struct channel *pch;
1238 struct sk_buff *frag;
1239 struct ppp_channel *chan;
1241 nch = 0;
1242 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1243 list = &ppp->channels;
1244 while ((list = list->next) != &ppp->channels) {
1245 pch = list_entry(list, struct channel, clist);
1246 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1248 * If a channel hasn't had a fragment yet, it has to get
1249 * one before we send any fragments on later channels.
1250 * If it can't take a fragment now, don't give any
1251 * to subsequent channels.
1253 if (!pch->had_frag && !pch->avail) {
1254 while ((list = list->next) != &ppp->channels) {
1255 pch = list_entry(list, struct channel, clist);
1256 pch->avail = 0;
1258 break;
1261 if (nch == 0)
1262 return 0; /* can't take now, leave it in xmit_pending */
1264 /* Do protocol field compression (XXX this should be optional) */
1265 p = skb->data;
1266 len = skb->len;
1267 if (*p == 0) {
1268 ++p;
1269 --len;
1272 /* decide on fragment size */
1273 fragsize = len;
1274 if (nch > 1) {
1275 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1276 if (nch > maxch)
1277 nch = maxch;
1278 fragsize = ROUNDUP(fragsize, nch);
1281 /* skip to the channel after the one we last used
1282 and start at that one */
1283 for (i = 0; i < ppp->nxchan; ++i) {
1284 list = list->next;
1285 if (list == &ppp->channels) {
1286 i = 0;
1287 break;
1291 /* create a fragment for each channel */
1292 bits = B;
1293 do {
1294 list = list->next;
1295 if (list == &ppp->channels) {
1296 i = 0;
1297 continue;
1299 pch = list_entry(list, struct channel, clist);
1300 ++i;
1301 if (!pch->avail)
1302 continue;
1304 /* check the channel's mtu and whether it is still attached. */
1305 spin_lock_bh(&pch->downl);
1306 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1307 /* can't use this channel */
1308 spin_unlock_bh(&pch->downl);
1309 pch->avail = 0;
1310 if (--nch == 0)
1311 break;
1312 continue;
1316 * We have to create multiple fragments for this channel
1317 * if fragsize is greater than the channel's mtu.
1319 if (fragsize > len)
1320 fragsize = len;
1321 for (flen = fragsize; flen > 0; flen -= fnb) {
1322 fnb = flen;
1323 if (fnb > mtu + 2 - hdrlen)
1324 fnb = mtu + 2 - hdrlen;
1325 if (fnb >= len)
1326 bits |= E;
1327 frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1328 if (frag == 0)
1329 goto noskb;
1330 q = skb_put(frag, fnb + hdrlen);
1331 /* make the MP header */
1332 q[0] = PPP_MP >> 8;
1333 q[1] = PPP_MP;
1334 if (ppp->flags & SC_MP_XSHORTSEQ) {
1335 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1336 q[3] = ppp->nxseq;
1337 } else {
1338 q[2] = bits;
1339 q[3] = ppp->nxseq >> 16;
1340 q[4] = ppp->nxseq >> 8;
1341 q[5] = ppp->nxseq;
1344 /* copy the data in */
1345 memcpy(q + hdrlen, p, fnb);
1347 /* try to send it down the channel */
1348 chan = pch->chan;
1349 if (!chan->ops->start_xmit(chan, frag))
1350 skb_queue_tail(&pch->file.xq, frag);
1351 pch->had_frag = 1;
1352 p += fnb;
1353 len -= fnb;
1354 ++ppp->nxseq;
1355 bits = 0;
1357 spin_unlock_bh(&pch->downl);
1358 } while (len > 0);
1359 ppp->nxchan = i;
1361 return 1;
1363 noskb:
1364 spin_unlock_bh(&pch->downl);
1365 if (ppp->debug & 1)
1366 printk(KERN_ERR "PPP: no memory (fragment)\n");
1367 ++ppp->stats.tx_errors;
1368 ++ppp->nxseq;
1369 return 1; /* abandon the frame */
1371 #endif /* CONFIG_PPP_MULTILINK */
1374 * Try to send data out on a channel.
1376 static void
1377 ppp_channel_push(struct channel *pch)
1379 struct sk_buff *skb;
1380 struct ppp *ppp;
1382 spin_lock_bh(&pch->downl);
1383 if (pch->chan != 0) {
1384 while (skb_queue_len(&pch->file.xq) > 0) {
1385 skb = skb_dequeue(&pch->file.xq);
1386 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1387 /* put the packet back and try again later */
1388 skb_queue_head(&pch->file.xq, skb);
1389 break;
1392 } else {
1393 /* channel got deregistered */
1394 skb_queue_purge(&pch->file.xq);
1396 spin_unlock_bh(&pch->downl);
1397 /* see if there is anything from the attached unit to be sent */
1398 if (skb_queue_len(&pch->file.xq) == 0) {
1399 read_lock_bh(&pch->upl);
1400 ppp = pch->ppp;
1401 if (ppp != 0)
1402 ppp_xmit_process(ppp);
1403 read_unlock_bh(&pch->upl);
1408 * Receive-side routines.
1411 /* misuse a few fields of the skb for MP reconstruction */
1412 #define sequence priority
1413 #define BEbits cb[0]
1415 static inline void
1416 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1418 ppp_recv_lock(ppp);
1419 /* ppp->dev == 0 means interface is closing down */
1420 if (ppp->dev != 0)
1421 ppp_receive_frame(ppp, skb, pch);
1422 else
1423 kfree_skb(skb);
1424 ppp_recv_unlock(ppp);
1427 void
1428 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1430 struct channel *pch = chan->ppp;
1431 int proto;
1433 if (pch == 0 || skb->len == 0) {
1434 kfree_skb(skb);
1435 return;
1438 proto = PPP_PROTO(skb);
1439 read_lock_bh(&pch->upl);
1440 if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1441 /* put it on the channel queue */
1442 skb_queue_tail(&pch->file.rq, skb);
1443 /* drop old frames if queue too long */
1444 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1445 && (skb = skb_dequeue(&pch->file.rq)) != 0)
1446 kfree_skb(skb);
1447 wake_up_interruptible(&pch->file.rwait);
1448 } else {
1449 ppp_do_recv(pch->ppp, skb, pch);
1451 read_unlock_bh(&pch->upl);
1454 /* Put a 0-length skb in the receive queue as an error indication */
1455 void
1456 ppp_input_error(struct ppp_channel *chan, int code)
1458 struct channel *pch = chan->ppp;
1459 struct sk_buff *skb;
1461 if (pch == 0)
1462 return;
1464 read_lock_bh(&pch->upl);
1465 if (pch->ppp != 0) {
1466 skb = alloc_skb(0, GFP_ATOMIC);
1467 if (skb != 0) {
1468 skb->len = 0; /* probably unnecessary */
1469 skb->cb[0] = code;
1470 ppp_do_recv(pch->ppp, skb, pch);
1473 read_unlock_bh(&pch->upl);
1477 * We come in here to process a received frame.
1478 * The receive side of the ppp unit is locked.
1480 static void
1481 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1483 if (pskb_may_pull(skb, 2)) {
1484 #ifdef CONFIG_PPP_MULTILINK
1485 /* XXX do channel-level decompression here */
1486 if (PPP_PROTO(skb) == PPP_MP)
1487 ppp_receive_mp_frame(ppp, skb, pch);
1488 else
1489 #endif /* CONFIG_PPP_MULTILINK */
1490 ppp_receive_nonmp_frame(ppp, skb);
1491 return;
1494 if (skb->len > 0)
1495 /* note: a 0-length skb is used as an error indication */
1496 ++ppp->stats.rx_length_errors;
1498 kfree_skb(skb);
1499 ppp_receive_error(ppp);
1502 static void
1503 ppp_receive_error(struct ppp *ppp)
1505 ++ppp->stats.rx_errors;
1506 if (ppp->vj != 0)
1507 slhc_toss(ppp->vj);
1510 static void
1511 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1513 struct sk_buff *ns;
1514 int proto, len, npi;
1517 * Decompress the frame, if compressed.
1518 * Note that some decompressors need to see uncompressed frames
1519 * that come in as well as compressed frames.
1521 if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1522 && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1523 skb = ppp_decompress_frame(ppp, skb);
1525 proto = PPP_PROTO(skb);
1526 switch (proto) {
1527 case PPP_VJC_COMP:
1528 /* decompress VJ compressed packets */
1529 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1530 goto err;
1531 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
1532 /* copy to a new sk_buff with more tailroom */
1533 ns = dev_alloc_skb(skb->len + 128);
1534 if (ns == 0) {
1535 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1536 goto err;
1538 skb_reserve(ns, 2);
1539 memcpy(skb_put(ns, skb->len), skb->data, skb->len);
1540 kfree_skb(skb);
1541 skb = ns;
1543 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1544 if (len <= 0) {
1545 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1546 goto err;
1548 len += 2;
1549 if (len > skb->len)
1550 skb_put(skb, len - skb->len);
1551 else if (len < skb->len)
1552 skb_trim(skb, len);
1553 proto = PPP_IP;
1554 break;
1556 case PPP_VJC_UNCOMP:
1557 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1558 goto err;
1559 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1560 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1561 goto err;
1563 proto = PPP_IP;
1564 break;
1566 case PPP_CCP:
1567 ppp_ccp_peek(ppp, skb, 1);
1568 break;
1571 ++ppp->stats.rx_packets;
1572 ppp->stats.rx_bytes += skb->len - 2;
1574 npi = proto_to_npindex(proto);
1575 if (npi < 0) {
1576 /* control or unknown frame - pass it to pppd */
1577 skb_queue_tail(&ppp->file.rq, skb);
1578 /* limit queue length by dropping old frames */
1579 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1580 && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1581 kfree_skb(skb);
1582 /* wake up any process polling or blocking on read */
1583 wake_up_interruptible(&ppp->file.rwait);
1585 } else {
1586 /* network protocol frame - give it to the kernel */
1588 #ifdef CONFIG_PPP_FILTER
1589 /* check if the packet passes the pass and active filters */
1590 /* the filter instructions are constructed assuming
1591 a four-byte PPP header on each packet */
1592 if (ppp->pass_filter.filter || ppp->active_filter.filter) {
1593 if (skb_cloned(skb) &&
1594 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1595 goto err;
1597 *skb_push(skb, 2) = 0;
1598 if (ppp->pass_filter.filter
1599 && sk_run_filter(skb, ppp->pass_filter.filter,
1600 ppp->pass_filter.len) == 0) {
1601 if (ppp->debug & 1)
1602 printk(KERN_DEBUG "PPP: inbound frame "
1603 "not passed\n");
1604 kfree_skb(skb);
1605 return;
1607 if (!(ppp->active_filter.filter
1608 && sk_run_filter(skb, ppp->active_filter.filter,
1609 ppp->active_filter.len) == 0))
1610 ppp->last_recv = jiffies;
1611 __skb_pull(skb, 2);
1612 } else
1613 #endif /* CONFIG_PPP_FILTER */
1614 ppp->last_recv = jiffies;
1616 if ((ppp->dev->flags & IFF_UP) == 0
1617 || ppp->npmode[npi] != NPMODE_PASS) {
1618 kfree_skb(skb);
1619 } else {
1620 skb_pull(skb, 2); /* chop off protocol */
1621 skb->dev = ppp->dev;
1622 skb->protocol = htons(npindex_to_ethertype[npi]);
1623 skb->mac.raw = skb->data;
1624 netif_rx(skb);
1625 ppp->dev->last_rx = jiffies;
1628 return;
1630 err:
1631 kfree_skb(skb);
1632 ppp_receive_error(ppp);
1635 static struct sk_buff *
1636 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1638 int proto = PPP_PROTO(skb);
1639 struct sk_buff *ns;
1640 int len;
1642 if (proto == PPP_COMP) {
1643 ns = dev_alloc_skb(ppp->mru_alloc + PPP_HDRLEN);
1644 if (ns == 0) {
1645 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1646 goto err;
1648 /* the decompressor still expects the A/C bytes in the hdr */
1649 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1650 skb->len + 2, ns->data,
1651 ppp->mru_alloc + PPP_HDRLEN);
1652 if (len < 0) {
1653 /* Pass the compressed frame to pppd as an
1654 error indication. */
1655 if (len == DECOMP_FATALERROR)
1656 ppp->rstate |= SC_DC_FERROR;
1657 kfree_skb(ns);
1658 goto err;
1661 kfree_skb(skb);
1662 skb = ns;
1663 skb_put(skb, len);
1664 skb_pull(skb, 2); /* pull off the A/C bytes */
1666 } else {
1667 /* Uncompressed frame - pass to decompressor so it
1668 can update its dictionary if necessary. */
1669 if (ppp->rcomp->incomp)
1670 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1671 skb->len + 2);
1674 return skb;
1676 err:
1677 if (ppp->rcomp->compress_proto != CI_MPPE
1678 && ppp->rcomp->compress_proto != CI_LZS) {
1680 * If decompression protocol isn't MPPE/MPPC or LZS, we set
1681 * SC_DC_ERROR flag and wait for CCP_RESETACK
1683 ppp->rstate |= SC_DC_ERROR;
1685 ppp_receive_error(ppp);
1686 return skb;
1689 #ifdef CONFIG_PPP_MULTILINK
1691 * Receive a multilink frame.
1692 * We put it on the reconstruction queue and then pull off
1693 * as many completed frames as we can.
1695 static void
1696 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1698 u32 mask, seq;
1699 struct list_head *l;
1700 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1702 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
1703 goto err; /* no good, throw it away */
1705 /* Decode sequence number and begin/end bits */
1706 if (ppp->flags & SC_MP_SHORTSEQ) {
1707 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1708 mask = 0xfff;
1709 } else {
1710 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1711 mask = 0xffffff;
1713 skb->BEbits = skb->data[2];
1714 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1717 * Do protocol ID decompression on the first fragment of each packet.
1719 if ((skb->BEbits & B) && (skb->data[0] & 1))
1720 *skb_push(skb, 1) = 0;
1723 * Expand sequence number to 32 bits, making it as close
1724 * as possible to ppp->minseq.
1726 seq |= ppp->minseq & ~mask;
1727 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1728 seq += mask + 1;
1729 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1730 seq -= mask + 1; /* should never happen */
1731 skb->sequence = seq;
1732 pch->lastseq = seq;
1735 * If this packet comes before the next one we were expecting,
1736 * drop it.
1738 if (seq_before(seq, ppp->nextseq)) {
1739 kfree_skb(skb);
1740 ++ppp->stats.rx_dropped;
1741 ppp_receive_error(ppp);
1742 return;
1746 * Reevaluate minseq, the minimum over all channels of the
1747 * last sequence number received on each channel. Because of
1748 * the increasing sequence number rule, we know that any fragment
1749 * before `minseq' which hasn't arrived is never going to arrive.
1750 * The list of channels can't change because we have the receive
1751 * side of the ppp unit locked.
1753 for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1754 struct channel *ch = list_entry(l, struct channel, clist);
1755 if (seq_before(ch->lastseq, seq))
1756 seq = ch->lastseq;
1758 if (seq_before(ppp->minseq, seq))
1759 ppp->minseq = seq;
1761 /* Put the fragment on the reconstruction queue */
1762 ppp_mp_insert(ppp, skb);
1764 /* If the queue is getting long, don't wait any longer for packets
1765 before the start of the queue. */
1766 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1767 && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1768 ppp->minseq = ppp->mrq.next->sequence;
1770 /* Pull completed packets off the queue and receive them. */
1771 /* shibby - fix mlppp
1772 while ((skb = ppp_mp_reconstruct(ppp)) != 0) {
1773 if (pskb_may_pull(skb, 2))
1774 ppp_receive_nonmp_frame(ppp, skb);
1775 else {
1776 ++ppp->stats.rx_length_errors;
1777 kfree_skb(skb);
1778 ppp_receive_error(ppp);
1781 shibby */
1783 //shibby - backport from ddwrt source - fix mlppp
1784 while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1785 ppp_receive_nonmp_frame(ppp, skb);
1786 //end
1788 return;
1790 err:
1791 kfree_skb(skb);
1792 ppp_receive_error(ppp);
1796 * Insert a fragment on the MP reconstruction queue.
1797 * The queue is ordered by increasing sequence number.
1799 static void
1800 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1802 struct sk_buff *p;
1803 struct sk_buff_head *list = &ppp->mrq;
1804 u32 seq = skb->sequence;
1806 /* N.B. we don't need to lock the list lock because we have the
1807 ppp unit receive-side lock. */
1808 for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1809 if (seq_before(seq, p->sequence))
1810 break;
1811 __skb_insert(skb, p->prev, p, list);
1815 * Reconstruct a packet from the MP fragment queue.
1816 * We go through increasing sequence numbers until we find a
1817 * complete packet, or we get to the sequence number for a fragment
1818 * which hasn't arrived but might still do so.
1820 struct sk_buff *
1821 ppp_mp_reconstruct(struct ppp *ppp)
1823 u32 seq = ppp->nextseq;
1824 u32 minseq = ppp->minseq;
1825 struct sk_buff_head *list = &ppp->mrq;
1826 struct sk_buff *p, *next;
1827 struct sk_buff *head, *tail;
1828 struct sk_buff *skb = NULL;
1829 int lost = 0, len = 0;
1831 if (ppp->mrru == 0) /* do nothing until mrru is set */
1832 return NULL;
1833 head = list->next;
1834 tail = NULL;
1835 for (p = head; p != (struct sk_buff *) list; p = next) {
1836 next = p->next;
1837 if (seq_before(p->sequence, seq)) {
1838 /* this can't happen, anyway ignore the skb */
1839 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1840 p->sequence, seq);
1841 head = next;
1842 continue;
1844 if (p->sequence != seq) {
1845 /* Fragment `seq' is missing. If it is after
1846 minseq, it might arrive later, so stop here. */
1847 if (seq_after(seq, minseq))
1848 break;
1849 /* Fragment `seq' is lost, keep going. */
1850 lost = 1;
1851 seq = seq_before(minseq, p->sequence)?
1852 minseq + 1: p->sequence;
1853 next = p;
1854 continue;
1858 * At this point we know that all the fragments from
1859 * ppp->nextseq to seq are either present or lost.
1860 * Also, there are no complete packets in the queue
1861 * that have no missing fragments and end before this
1862 * fragment.
1865 /* B bit set indicates this fragment starts a packet */
1866 if (p->BEbits & B) {
1867 head = p;
1868 lost = 0;
1869 len = 0;
1872 len += p->len;
1874 /* Got a complete packet yet? */
1875 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1876 if (len > ppp->mrru + 2) {
1877 ++ppp->stats.rx_length_errors;
1878 printk(KERN_DEBUG "PPP: reconstructed packet"
1879 " is too long (%d)\n", len);
1880 } else if (p == head) {
1881 /* fragment is complete packet - reuse skb */
1882 tail = p;
1883 skb = skb_get(p);
1884 break;
1885 } else if ((skb = dev_alloc_skb(len)) == NULL) {
1886 ++ppp->stats.rx_missed_errors;
1887 printk(KERN_DEBUG "PPP: no memory for "
1888 "reconstructed packet");
1889 } else {
1890 tail = p;
1891 break;
1893 ppp->nextseq = seq + 1;
1897 * If this is the ending fragment of a packet,
1898 * and we haven't found a complete valid packet yet,
1899 * we can discard up to and including this fragment.
1901 if (p->BEbits & E)
1902 head = next;
1904 ++seq;
1907 /* If we have a complete packet, copy it all into one skb. */
1908 if (tail != NULL) {
1909 /* If we have discarded any fragments,
1910 signal a receive error. */
1911 if (head->sequence != ppp->nextseq) {
1912 if (ppp->debug & 1)
1913 printk(KERN_DEBUG " missed pkts %u..%u\n",
1914 ppp->nextseq, head->sequence-1);
1915 ++ppp->stats.rx_dropped;
1916 ppp_receive_error(ppp);
1919 if (head != tail)
1920 /* copy to a single skb */
1921 for (p = head; p != tail->next; p = p->next)
1922 memcpy(skb_put(skb, p->len), p->data, p->len);
1923 ppp->nextseq = tail->sequence + 1;
1924 head = tail->next;
1927 /* Discard all the skbuffs that we have copied the data out of
1928 or that we can't use. */
1929 while ((p = list->next) != head) {
1930 __skb_unlink(p, list);
1931 kfree_skb(p);
1934 return skb;
1936 #endif /* CONFIG_PPP_MULTILINK */
1939 * Channel interface.
1943 * Create a new, unattached ppp channel.
1946 ppp_register_channel(struct ppp_channel *chan)
1948 struct channel *pch;
1950 pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
1951 if (pch == 0)
1952 return -ENOMEM;
1953 memset(pch, 0, sizeof(struct channel));
1954 pch->ppp = NULL;
1955 pch->chan = chan;
1956 chan->ppp = pch;
1957 init_ppp_file(&pch->file, CHANNEL);
1958 pch->file.hdrlen = chan->hdrlen;
1959 #ifdef CONFIG_PPP_MULTILINK
1960 pch->lastseq = -1;
1961 #endif /* CONFIG_PPP_MULTILINK */
1962 init_rwsem(&pch->chan_sem);
1963 spin_lock_init(&pch->downl);
1964 pch->upl = RW_LOCK_UNLOCKED;
1965 spin_lock_bh(&all_channels_lock);
1966 pch->file.index = ++last_channel_index;
1967 list_add(&pch->list, &new_channels);
1968 atomic_inc(&channel_count);
1969 spin_unlock_bh(&all_channels_lock);
1970 MOD_INC_USE_COUNT;
1971 return 0;
1975 * Return the index of a channel.
1977 int ppp_channel_index(struct ppp_channel *chan)
1979 struct channel *pch = chan->ppp;
1981 if (pch != 0)
1982 return pch->file.index;
1983 return -1;
1987 * Return the PPP unit number to which a channel is connected.
1989 int ppp_unit_number(struct ppp_channel *chan)
1991 struct channel *pch = chan->ppp;
1992 int unit = -1;
1994 if (pch != 0) {
1995 read_lock_bh(&pch->upl);
1996 if (pch->ppp != 0)
1997 unit = pch->ppp->file.index;
1998 read_unlock_bh(&pch->upl);
2000 return unit;
2004 * Disconnect a channel from the generic layer.
2005 * This must be called in process context.
2007 void
2008 ppp_unregister_channel(struct ppp_channel *chan)
2010 struct channel *pch = chan->ppp;
2012 if (pch == 0)
2013 return; /* should never happen */
2014 chan->ppp = 0;
2017 * This ensures that we have returned from any calls into the
2018 * the channel's start_xmit or ioctl routine before we proceed.
2020 down_write(&pch->chan_sem);
2021 spin_lock_bh(&pch->downl);
2022 pch->chan = 0;
2023 spin_unlock_bh(&pch->downl);
2024 up_write(&pch->chan_sem);
2025 ppp_disconnect_channel(pch);
2026 spin_lock_bh(&all_channels_lock);
2027 list_del(&pch->list);
2028 spin_unlock_bh(&all_channels_lock);
2029 pch->file.dead = 1;
2030 wake_up_interruptible(&pch->file.rwait);
2031 if (atomic_dec_and_test(&pch->file.refcnt))
2032 ppp_destroy_channel(pch);
2033 MOD_DEC_USE_COUNT;
2037 * Callback from a channel when it can accept more to transmit.
2038 * This should be called at BH/softirq level, not interrupt level.
2040 void
2041 ppp_output_wakeup(struct ppp_channel *chan)
2043 struct channel *pch = chan->ppp;
2045 if (pch == 0)
2046 return;
2047 ppp_channel_push(pch);
2051 * Compression control.
2054 /* Process the PPPIOCSCOMPRESS ioctl. */
2055 static int
2056 ppp_set_compress(struct ppp *ppp, unsigned long arg)
2058 int err;
2059 struct compressor *cp, *ocomp;
2060 struct ppp_option_data data;
2061 void *state, *ostate;
2062 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2063 #ifdef CONFIG_KMOD
2064 char modname[32];
2065 #endif
2067 err = -EFAULT;
2068 if (copy_from_user(&data, (void *) arg, sizeof(data))
2069 || (data.length <= CCP_MAX_OPTION_LENGTH
2070 && copy_from_user(ccp_option, data.ptr, data.length)))
2071 goto err1;
2072 err = -EINVAL;
2073 if (data.length > CCP_MAX_OPTION_LENGTH
2074 || ccp_option[1] < 2 || ccp_option[1] > data.length)
2075 goto err1;
2077 cp = find_compressor(ccp_option[0]);
2078 #ifdef CONFIG_KMOD
2079 if (cp == 0) {
2080 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
2081 request_module(modname);
2082 cp = find_compressor(ccp_option[0]);
2084 #endif /* CONFIG_KMOD */
2085 if (cp == 0)
2086 goto err1;
2088 * XXX race: the compressor module could get unloaded between
2089 * here and when we do the comp_alloc or decomp_alloc call below.
2092 err = -ENOBUFS;
2093 if (data.transmit) {
2094 state = cp->comp_alloc(ccp_option, data.length);
2095 if (state != 0) {
2096 ppp_xmit_lock(ppp);
2097 ppp->xstate &= ~SC_COMP_RUN;
2098 ocomp = ppp->xcomp;
2099 ostate = ppp->xc_state;
2100 ppp->xcomp = cp;
2101 ppp->xc_state = state;
2102 ppp_xmit_unlock(ppp);
2103 if (ostate != 0)
2104 ocomp->comp_free(ostate);
2105 err = 0;
2108 } else {
2109 state = cp->decomp_alloc(ccp_option, data.length);
2110 if (state != 0) {
2111 ppp_recv_lock(ppp);
2112 ppp->rstate &= ~SC_DECOMP_RUN;
2113 ocomp = ppp->rcomp;
2114 ostate = ppp->rc_state;
2115 ppp->rcomp = cp;
2116 ppp->rc_state = state;
2117 ppp_recv_unlock(ppp);
2118 if (ostate != 0)
2119 ocomp->decomp_free(ostate);
2120 err = 0;
2124 err1:
2125 return err;
2129 * Look at a CCP packet and update our state accordingly.
2130 * We assume the caller has the xmit or recv path locked.
2132 static void
2133 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2135 unsigned char *dp = skb->data + 2;
2136 int len;
2138 if (skb->len < CCP_HDRLEN + 2
2139 || skb->len < (len = CCP_LENGTH(dp)) + 2)
2140 return; /* too short */
2142 switch (CCP_CODE(dp)) {
2143 case CCP_CONFREQ:
2145 /* A ConfReq starts negotiation of compression
2146 * in one direction of transmission,
2147 * and hence brings it down...but which way?
2149 * Remember:
2150 * A ConfReq indicates what the sender would like to receive
2152 if(inbound)
2153 /* He is proposing what I should send */
2154 ppp->xstate &= ~SC_COMP_RUN;
2155 else
2156 /* I am proposing to what he should send */
2157 ppp->rstate &= ~SC_DECOMP_RUN;
2159 break;
2161 case CCP_TERMREQ:
2162 case CCP_TERMACK:
2164 * CCP is going down, both directions of transmission
2166 ppp->rstate &= ~SC_DECOMP_RUN;
2167 ppp->xstate &= ~SC_COMP_RUN;
2168 break;
2170 case CCP_CONFACK:
2171 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2172 break;
2173 dp += CCP_HDRLEN;
2174 len -= CCP_HDRLEN;
2175 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2176 break;
2177 if (inbound) {
2178 /* we will start receiving compressed packets */
2179 if (ppp->rc_state == 0)
2180 break;
2181 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2182 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2183 ppp->rstate |= SC_DECOMP_RUN;
2184 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2186 } else {
2187 /* we will soon start sending compressed packets */
2188 if (ppp->xc_state == 0)
2189 break;
2190 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2191 ppp->file.index, 0, ppp->debug))
2192 ppp->xstate |= SC_COMP_RUN;
2194 break;
2196 case CCP_RESETACK:
2197 /* reset the [de]compressor */
2198 if ((ppp->flags & SC_CCP_UP) == 0)
2199 break;
2200 if (inbound) {
2201 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2202 ppp->rcomp->decomp_reset(ppp->rc_state);
2203 ppp->rstate &= ~SC_DC_ERROR;
2205 } else {
2206 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2207 ppp->xcomp->comp_reset(ppp->xc_state);
2209 break;
2213 /* Free up compression resources. */
2214 static void
2215 ppp_ccp_closed(struct ppp *ppp)
2217 void *xstate, *rstate;
2218 struct compressor *xcomp, *rcomp;
2220 ppp_lock(ppp);
2221 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2222 ppp->xstate = 0;
2223 xcomp = ppp->xcomp;
2224 xstate = ppp->xc_state;
2225 ppp->xc_state = 0;
2226 ppp->rstate = 0;
2227 rcomp = ppp->rcomp;
2228 rstate = ppp->rc_state;
2229 ppp->rc_state = 0;
2230 ppp_unlock(ppp);
2232 if (xstate)
2233 xcomp->comp_free(xstate);
2234 if (rstate)
2235 rcomp->decomp_free(rstate);
2238 /* List of compressors. */
2239 static LIST_HEAD(compressor_list);
2240 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2242 struct compressor_entry {
2243 struct list_head list;
2244 struct compressor *comp;
2247 static struct compressor_entry *
2248 find_comp_entry(int proto)
2250 struct compressor_entry *ce;
2251 struct list_head *list = &compressor_list;
2253 while ((list = list->next) != &compressor_list) {
2254 ce = list_entry(list, struct compressor_entry, list);
2255 if (ce->comp->compress_proto == proto)
2256 return ce;
2258 return 0;
2261 /* Register a compressor */
2263 ppp_register_compressor(struct compressor *cp)
2265 struct compressor_entry *ce;
2266 int ret;
2267 spin_lock(&compressor_list_lock);
2268 ret = -EEXIST;
2269 if (find_comp_entry(cp->compress_proto) != 0)
2270 goto err1;
2271 ret = -ENOMEM;
2272 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2273 if (ce == 0)
2274 goto err1;
2275 ret = 0;
2276 ce->comp = cp;
2277 list_add(&ce->list, &compressor_list);
2278 err1:
2279 spin_unlock(&compressor_list_lock);
2280 return ret;
2283 /* Unregister a compressor */
2284 void
2285 ppp_unregister_compressor(struct compressor *cp)
2287 struct compressor_entry *ce;
2289 spin_lock(&compressor_list_lock);
2290 ce = find_comp_entry(cp->compress_proto);
2291 if (ce != 0 && ce->comp == cp) {
2292 list_del(&ce->list);
2293 kfree(ce);
2295 spin_unlock(&compressor_list_lock);
2298 /* Find a compressor. */
2299 static struct compressor *
2300 find_compressor(int type)
2302 struct compressor_entry *ce;
2303 struct compressor *cp = 0;
2305 spin_lock(&compressor_list_lock);
2306 ce = find_comp_entry(type);
2307 if (ce != 0)
2308 cp = ce->comp;
2309 spin_unlock(&compressor_list_lock);
2310 return cp;
2314 * Miscelleneous stuff.
2317 static void
2318 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2320 struct slcompress *vj = ppp->vj;
2322 memset(st, 0, sizeof(*st));
2323 st->p.ppp_ipackets = ppp->stats.rx_packets;
2324 st->p.ppp_ierrors = ppp->stats.rx_errors;
2325 st->p.ppp_ibytes = ppp->stats.rx_bytes;
2326 st->p.ppp_opackets = ppp->stats.tx_packets;
2327 st->p.ppp_oerrors = ppp->stats.tx_errors;
2328 st->p.ppp_obytes = ppp->stats.tx_bytes;
2329 if (vj == 0)
2330 return;
2331 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2332 st->vj.vjs_compressed = vj->sls_o_compressed;
2333 st->vj.vjs_searches = vj->sls_o_searches;
2334 st->vj.vjs_misses = vj->sls_o_misses;
2335 st->vj.vjs_errorin = vj->sls_i_error;
2336 st->vj.vjs_tossed = vj->sls_i_tossed;
2337 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2338 st->vj.vjs_compressedin = vj->sls_i_compressed;
2342 * Stuff for handling the lists of ppp units and channels
2343 * and for initialization.
2347 * Create a new ppp interface unit. Fails if it can't allocate memory
2348 * or if there is already a unit with the requested number.
2349 * unit == -1 means allocate a new number.
2351 static struct ppp *
2352 ppp_create_interface(int unit, int *retp)
2354 struct ppp *ppp;
2355 struct net_device *dev = NULL;
2356 int ret = -ENOMEM;
2357 int i;
2359 ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2360 if (ppp == 0)
2361 goto err;
2362 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
2363 if (dev == 0)
2364 goto err;
2365 memset(ppp, 0, sizeof(struct ppp));
2366 memset(dev, 0, sizeof(struct net_device));
2368 ret = -EEXIST;
2369 down(&all_ppp_sem);
2370 if (unit < 0)
2371 unit = cardmap_find_first_free(all_ppp_units);
2372 else if (cardmap_get(all_ppp_units, unit) != NULL)
2373 goto err_unlock; /* unit already exists */
2375 /* Initialize the new ppp unit */
2376 ppp->file.index = unit;
2377 ppp->mru = PPP_MRU;
2378 ppp->mru_alloc = PPP_MRU;
2379 init_ppp_file(&ppp->file, INTERFACE);
2380 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2381 for (i = 0; i < NUM_NP; ++i)
2382 ppp->npmode[i] = NPMODE_PASS;
2383 INIT_LIST_HEAD(&ppp->channels);
2384 spin_lock_init(&ppp->rlock);
2385 spin_lock_init(&ppp->wlock);
2386 #ifdef CONFIG_PPP_MULTILINK
2387 ppp->minseq = -1;
2388 skb_queue_head_init(&ppp->mrq);
2389 #endif /* CONFIG_PPP_MULTILINK */
2391 ppp->dev = dev;
2392 dev->init = ppp_net_init;
2393 sprintf(dev->name, "ppp%d", unit);
2394 dev->priv = ppp;
2395 dev->features |= NETIF_F_DYNALLOC;
2397 rtnl_lock();
2398 ret = register_netdevice(dev);
2399 rtnl_unlock();
2400 if (ret != 0) {
2401 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2402 dev->name, ret);
2403 goto err_unlock;
2406 atomic_inc(&ppp_unit_count);
2407 cardmap_set(&all_ppp_units, unit, ppp);
2408 up(&all_ppp_sem);
2409 *retp = 0;
2410 return ppp;
2412 err_unlock:
2413 up(&all_ppp_sem);
2414 err:
2415 *retp = ret;
2416 if (ppp)
2417 kfree(ppp);
2418 if (dev)
2419 kfree(dev);
2420 return NULL;
2424 * Initialize a ppp_file structure.
2426 static void
2427 init_ppp_file(struct ppp_file *pf, int kind)
2429 pf->kind = kind;
2430 skb_queue_head_init(&pf->xq);
2431 skb_queue_head_init(&pf->rq);
2432 atomic_set(&pf->refcnt, 1);
2433 init_waitqueue_head(&pf->rwait);
2437 * Take down a ppp interface unit - called when the owning file
2438 * (the one that created the unit) is closed or detached.
2440 static void ppp_shutdown_interface(struct ppp *ppp)
2442 struct net_device *dev;
2444 down(&all_ppp_sem);
2445 ppp_lock(ppp);
2446 dev = ppp->dev;
2447 ppp->dev = 0;
2448 ppp_unlock(ppp);
2449 if (dev) {
2450 rtnl_lock();
2451 dev_close(dev);
2452 unregister_netdevice(dev);
2453 rtnl_unlock();
2455 cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2456 ppp->file.dead = 1;
2457 ppp->owner = NULL;
2458 wake_up_interruptible(&ppp->file.rwait);
2459 up(&all_ppp_sem);
2463 * Free the memory used by a ppp unit. This is only called once
2464 * there are no channels connected to the unit and no file structs
2465 * that reference the unit.
2467 static void ppp_destroy_interface(struct ppp *ppp)
2469 atomic_dec(&ppp_unit_count);
2471 if (!ppp->file.dead || ppp->n_channels) {
2472 /* "can't happen" */
2473 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2474 "n_channels=%d !\n", ppp, ppp->file.dead,
2475 ppp->n_channels);
2476 return;
2479 ppp_ccp_closed(ppp);
2480 if (ppp->vj) {
2481 slhc_free(ppp->vj);
2482 ppp->vj = 0;
2484 skb_queue_purge(&ppp->file.xq);
2485 skb_queue_purge(&ppp->file.rq);
2486 #ifdef CONFIG_PPP_MULTILINK
2487 skb_queue_purge(&ppp->mrq);
2488 #endif /* CONFIG_PPP_MULTILINK */
2489 #ifdef CONFIG_PPP_FILTER
2490 if (ppp->pass_filter.filter) {
2491 kfree(ppp->pass_filter.filter);
2492 ppp->pass_filter.filter = NULL;
2494 if (ppp->active_filter.filter) {
2495 kfree(ppp->active_filter.filter);
2496 ppp->active_filter.filter = 0;
2498 #endif /* CONFIG_PPP_FILTER */
2500 if (ppp->xmit_pending)
2501 kfree_skb(ppp->xmit_pending);
2503 kfree(ppp);
2507 * Locate an existing ppp unit.
2508 * The caller should have locked the all_ppp_sem.
2510 static struct ppp *
2511 ppp_find_unit(int unit)
2513 return cardmap_get(all_ppp_units, unit);
2517 * Locate an existing ppp channel.
2518 * The caller should have locked the all_channels_lock.
2519 * First we look in the new_channels list, then in the
2520 * all_channels list. If found in the new_channels list,
2521 * we move it to the all_channels list. This is for speed
2522 * when we have a lot of channels in use.
2524 static struct channel *
2525 ppp_find_channel(int unit)
2527 struct channel *pch;
2528 struct list_head *list;
2530 list = &new_channels;
2531 while ((list = list->next) != &new_channels) {
2532 pch = list_entry(list, struct channel, list);
2533 if (pch->file.index == unit) {
2534 list_del(&pch->list);
2535 list_add(&pch->list, &all_channels);
2536 return pch;
2539 list = &all_channels;
2540 while ((list = list->next) != &all_channels) {
2541 pch = list_entry(list, struct channel, list);
2542 if (pch->file.index == unit)
2543 return pch;
2545 return 0;
2549 * Connect a PPP channel to a PPP interface unit.
2551 static int
2552 ppp_connect_channel(struct channel *pch, int unit)
2554 struct ppp *ppp;
2555 int ret = -ENXIO;
2556 int hdrlen;
2558 down(&all_ppp_sem);
2559 ppp = ppp_find_unit(unit);
2560 if (ppp == 0)
2561 goto err1;
2563 write_lock_bh(&pch->upl);
2564 ret = -EINVAL;
2565 if (pch->ppp != 0)
2566 goto err2;
2568 ppp_lock(ppp);
2569 if (pch->file.hdrlen > ppp->file.hdrlen)
2570 ppp->file.hdrlen = pch->file.hdrlen;
2571 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
2572 if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2573 ppp->dev->hard_header_len = hdrlen;
2574 list_add_tail(&pch->clist, &ppp->channels);
2575 ++ppp->n_channels;
2576 pch->ppp = ppp;
2577 atomic_inc(&ppp->file.refcnt);
2578 ppp_unlock(ppp);
2579 ret = 0;
2581 err2:
2582 write_unlock_bh(&pch->upl);
2583 err1:
2584 up(&all_ppp_sem);
2585 return ret;
2589 * Disconnect a channel from its ppp unit.
2591 static int
2592 ppp_disconnect_channel(struct channel *pch)
2594 struct ppp *ppp;
2595 int err = -EINVAL;
2597 write_lock_bh(&pch->upl);
2598 ppp = pch->ppp;
2599 pch->ppp = NULL;
2600 write_unlock_bh(&pch->upl);
2601 if (ppp != 0) {
2602 /* remove it from the ppp unit's list */
2603 ppp_lock(ppp);
2604 list_del(&pch->clist);
2605 --ppp->n_channels;
2606 ppp_unlock(ppp);
2607 if (atomic_dec_and_test(&ppp->file.refcnt))
2608 ppp_destroy_interface(ppp);
2609 err = 0;
2611 return err;
2615 * Free up the resources used by a ppp channel.
2617 static void ppp_destroy_channel(struct channel *pch)
2619 atomic_dec(&channel_count);
2621 if (!pch->file.dead) {
2622 /* "can't happen" */
2623 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2624 pch);
2625 return;
2627 skb_queue_purge(&pch->file.xq);
2628 skb_queue_purge(&pch->file.rq);
2629 kfree(pch);
2632 static void __exit ppp_cleanup(void)
2634 /* should never happen */
2635 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2636 printk(KERN_ERR "PPP: removing module but units remain!\n");
2637 cardmap_destroy(&all_ppp_units);
2638 if (devfs_unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2639 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2640 devfs_unregister(devfs_handle);
2641 unregister_sysctl_table(ppp_filter_sysctl_header);
2645 * Cardmap implementation.
2647 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2649 struct cardmap *p;
2650 int i;
2652 for (p = map; p != NULL; ) {
2653 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2654 return NULL;
2655 if (p->shift == 0)
2656 return p->ptr[i];
2657 nr &= ~(CARDMAP_MASK << p->shift);
2658 p = p->ptr[i];
2660 return NULL;
2663 static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2665 struct cardmap *p;
2666 int i;
2668 p = *pmap;
2669 if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2670 do {
2671 /* need a new top level */
2672 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2673 memset(np, 0, sizeof(*np));
2674 np->ptr[0] = p;
2675 if (p != NULL) {
2676 np->shift = p->shift + CARDMAP_ORDER;
2677 p->parent = np;
2678 } else
2679 np->shift = 0;
2680 p = np;
2681 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2682 *pmap = p;
2684 while (p->shift > 0) {
2685 i = (nr >> p->shift) & CARDMAP_MASK;
2686 if (p->ptr[i] == NULL) {
2687 struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2688 memset(np, 0, sizeof(*np));
2689 np->shift = p->shift - CARDMAP_ORDER;
2690 np->parent = p;
2691 p->ptr[i] = np;
2693 if (ptr == NULL)
2694 clear_bit(i, &p->inuse);
2695 p = p->ptr[i];
2697 i = nr & CARDMAP_MASK;
2698 p->ptr[i] = ptr;
2699 if (ptr != NULL)
2700 set_bit(i, &p->inuse);
2701 else
2702 clear_bit(i, &p->inuse);
2705 static unsigned int cardmap_find_first_free(struct cardmap *map)
2707 struct cardmap *p;
2708 unsigned int nr = 0;
2709 int i;
2711 if ((p = map) == NULL)
2712 return 0;
2713 for (;;) {
2714 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2715 if (i >= CARDMAP_WIDTH) {
2716 if (p->parent == NULL)
2717 return CARDMAP_WIDTH << p->shift;
2718 p = p->parent;
2719 i = (nr >> p->shift) & CARDMAP_MASK;
2720 set_bit(i, &p->inuse);
2721 continue;
2723 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2724 if (p->shift == 0 || p->ptr[i] == NULL)
2725 return nr;
2726 p = p->ptr[i];
2730 static void cardmap_destroy(struct cardmap **pmap)
2732 struct cardmap *p, *np;
2733 int i;
2735 for (p = *pmap; p != NULL; p = np) {
2736 if (p->shift != 0) {
2737 for (i = 0; i < CARDMAP_WIDTH; ++i)
2738 if (p->ptr[i] != NULL)
2739 break;
2740 if (i < CARDMAP_WIDTH) {
2741 np = p->ptr[i];
2742 p->ptr[i] = NULL;
2743 continue;
2746 np = p->parent;
2747 kfree(p);
2749 *pmap = NULL;
2752 #define NET_PPP_FILTER 2091
2753 #define NET_PPP_FILTER_NAME "ppp_filter"
2755 static ctl_table ppp_filter_table[] = {
2756 { NET_PPP_FILTER, NET_PPP_FILTER_NAME, &ppp_filter,
2757 sizeof(ppp_filter), 0644, NULL, proc_dointvec },
2758 { 0 }
2761 static ctl_table ppp_filter_dir_table[] = {
2762 {NET_IPV4, "ipv4", NULL, 0, 0555, ppp_filter_table, 0, 0, 0, 0, 0},
2763 { 0 }
2766 static ctl_table ppp_filter_root_table[] = {
2767 {CTL_NET, "net", NULL, 0, 0555, ppp_filter_dir_table, 0, 0, 0, 0, 0},
2768 { 0 }
2772 /* Module/initialization stuff */
2774 module_init(ppp_init);
2775 module_exit(ppp_cleanup);
2777 EXPORT_SYMBOL(ppp_register_channel);
2778 EXPORT_SYMBOL(ppp_unregister_channel);
2779 EXPORT_SYMBOL(ppp_channel_index);
2780 EXPORT_SYMBOL(ppp_unit_number);
2781 EXPORT_SYMBOL(ppp_input);
2782 EXPORT_SYMBOL(ppp_input_error);
2783 EXPORT_SYMBOL(ppp_output_wakeup);
2784 EXPORT_SYMBOL(ppp_register_compressor);
2785 EXPORT_SYMBOL(ppp_unregister_compressor);
2786 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2787 EXPORT_SYMBOL(all_channels); /* for debugging */
2788 MODULE_LICENSE("GPL");