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
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>
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.
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.
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.
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 ->
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)
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
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])
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)
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
)
301 /* Translates an NP index into a PPP protocol number */
302 static const int npindex_to_proto
[NUM_NP
] = {
309 /* Translates an ethertype into an NP index */
310 static inline int ethertype_to_npindex(int ethertype
)
326 /* Translates an NP index into an ethertype */
327 static const int npindex_to_ethertype
[NUM_NP
] = {
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
))
363 static int ppp_release(struct inode
*inode
, struct file
*file
)
365 struct ppp_file
*pf
= file
->private_data
;
369 file
->private_data
= 0;
370 if (pf
->kind
== INTERFACE
) {
372 if (file
== ppp
->owner
)
373 ppp_shutdown_interface(ppp
);
375 if (atomic_dec_and_test(&pf
->refcnt
)) {
378 ppp_destroy_interface(PF_TO_PPP(pf
));
381 ppp_destroy_channel(PF_TO_CHANNEL(pf
));
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
);
395 struct sk_buff
*skb
= 0;
399 add_wait_queue(&pf
->rwait
, &wait
);
401 set_current_state(TASK_INTERRUPTIBLE
);
402 skb
= skb_dequeue(&pf
->rq
);
409 if (file
->f_flags
& O_NONBLOCK
)
412 if (signal_pending(current
))
416 set_current_state(TASK_RUNNING
);
417 remove_wait_queue(&pf
->rwait
, &wait
);
423 if (skb
->len
> count
)
426 if (copy_to_user(buf
, skb
->data
, skb
->len
))
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
;
446 skb
= alloc_skb(count
+ pf
->hdrlen
, GFP_KERNEL
);
449 skb_reserve(skb
, pf
->hdrlen
);
451 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
456 skb_queue_tail(&pf
->xq
, skb
);
460 ppp_xmit_process(PF_TO_PPP(pf
));
463 ppp_channel_push(PF_TO_CHANNEL(pf
));
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
;
481 poll_wait(file
, &pf
->rwait
, wait
);
482 mask
= POLLOUT
| POLLWRNORM
;
483 if (skb_peek(&pf
->rq
) != 0)
484 mask
|= POLLIN
| POLLRDNORM
;
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
;
495 int err
= -EFAULT
, val
, val2
, i
;
496 struct ppp_idle idle
;
499 struct slcompress
*vj
;
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.
517 if (pf
->kind
== INTERFACE
) {
519 if (file
== ppp
->owner
)
520 ppp_shutdown_interface(ppp
);
522 if (atomic_read(&file
->f_count
) <= 2) {
523 ppp_release(inode
, file
);
526 printk(KERN_DEBUG
"PPPIOCDETACH file->f_count=%d\n",
527 atomic_read(&file
->f_count
));
531 if (pf
->kind
== CHANNEL
) {
532 struct channel
*pch
= PF_TO_CHANNEL(pf
);
533 struct ppp_channel
*chan
;
537 if (get_user(unit
, (int *) arg
))
539 err
= ppp_connect_channel(pch
, unit
);
543 err
= ppp_disconnect_channel(pch
);
547 down_read(&pch
->chan_sem
);
550 if (chan
&& chan
->ops
->ioctl
)
551 err
= chan
->ops
->ioctl(chan
, cmd
, arg
);
552 up_read(&pch
->chan_sem
);
557 if (pf
->kind
!= INTERFACE
) {
559 printk(KERN_ERR
"PPP: not interface or channel??\n");
566 if (get_user(val
, (int *) arg
))
568 ppp
->mru_alloc
= ppp
->mru
= val
;
569 if (ppp
->mru_alloc
< PPP_MRU
)
570 ppp
->mru_alloc
= PPP_MRU
; /* increase for broken peers */
575 if (get_user(val
, (int *) arg
))
578 cflags
= ppp
->flags
& ~val
;
579 ppp
->flags
= val
& SC_FLAG_BITS
;
581 if (cflags
& SC_CCP_OPEN
)
587 val
= ppp
->flags
| ppp
->xstate
| ppp
->rstate
;
588 if (put_user(val
, (int *) arg
))
593 case PPPIOCSCOMPRESS
:
594 err
= ppp_set_compress(ppp
, arg
);
598 if (put_user(ppp
->file
.index
, (int *) arg
))
604 if (get_user(val
, (int *) arg
))
611 if (put_user(ppp
->debug
, (int *) arg
))
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
)))
625 if (get_user(val
, (int *) arg
))
628 if ((val
>> 16) != 0) {
632 vj
= slhc_init(val2
+1, val
+1);
634 printk(KERN_ERR
"PPP: no memory (VJ compressor)\n");
648 if (copy_from_user(&npi
, (void *) arg
, sizeof(npi
)))
650 err
= proto_to_npindex(npi
.protocol
);
654 if (cmd
== PPPIOCGNPMODE
) {
656 npi
.mode
= ppp
->npmode
[i
];
657 if (copy_to_user((void *) arg
, &npi
, sizeof(npi
)))
660 ppp
->npmode
[i
] = npi
.mode
;
661 /* we may be able to transmit more packets now (??) */
662 netif_wake_queue(ppp
->dev
);
667 #ifdef CONFIG_PPP_FILTER
671 struct sock_fprog uprog
, *filtp
;
672 struct sock_filter
*code
= NULL
;
675 if (copy_from_user(&uprog
, (void *) arg
, sizeof(uprog
)))
677 if (uprog
.len
> 0 && uprog
.len
< 65536) {
679 len
= uprog
.len
* sizeof(struct sock_filter
);
680 code
= kmalloc(len
, GFP_KERNEL
);
684 if (copy_from_user(code
, uprog
.filter
, len
)) {
688 err
= sk_chk_filter(code
, uprog
.len
);
694 filtp
= (cmd
== PPPIOCSPASS
)? &ppp
->pass_filter
: &ppp
->active_filter
;
697 kfree(filtp
->filter
);
698 filtp
->filter
= code
;
699 filtp
->len
= uprog
.len
;
704 #endif /* CONFIG_PPP_FILTER */
706 #ifdef CONFIG_PPP_MULTILINK
708 if (get_user(val
, (int *) arg
))
712 ppp_recv_unlock(ppp
);
715 #endif /* CONFIG_PPP_MULTILINK */
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
;
729 struct channel
*chan
;
733 /* Create a new ppp unit */
734 if (get_user(unit
, (int *) arg
))
736 ppp
= ppp_create_interface(unit
, &err
);
739 file
->private_data
= &ppp
->file
;
742 if (put_user(ppp
->file
.index
, (int *) arg
))
748 /* Attach to an existing ppp unit */
749 if (get_user(unit
, (int *) arg
))
753 ppp
= ppp_find_unit(unit
);
755 atomic_inc(&ppp
->file
.refcnt
);
756 file
->private_data
= &ppp
->file
;
763 if (get_user(unit
, (int *) arg
))
765 spin_lock_bh(&all_channels_lock
);
767 chan
= ppp_find_channel(unit
);
769 atomic_inc(&chan
->file
.refcnt
);
770 file
->private_data
= &chan
->file
;
773 spin_unlock_bh(&all_channels_lock
);
782 static struct file_operations ppp_device_fops
= {
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)
802 printk(KERN_INFO
"PPP generic driver version " PPP_VERSION
"\n");
803 err
= devfs_register_chrdev(PPP_MAJOR
, "ppp", &ppp_device_fops
);
805 printk(KERN_ERR
"failed to register PPP device (%d)\n", err
);
806 devfs_handle
= devfs_register(NULL
, "ppp", DEVFS_FL_DEFAULT
,
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);
818 * Network interface unit routines.
821 ppp_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
823 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
827 npi
= ethertype_to_npindex(ntohs(skb
->protocol
));
831 /* Drop, accept or reject the packet */
832 switch (ppp
->npmode
[npi
]) {
836 /* it would be nice to have a way to tell the network
837 system to queue this one up for later. */
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
) {
849 ns
= alloc_skb(skb
->len
+ dev
->hard_header_len
, GFP_ATOMIC
);
852 skb_reserve(ns
, dev
->hard_header_len
);
853 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
857 pp
= skb_push(skb
, 2);
858 proto
= npindex_to_proto
[npi
];
862 netif_stop_queue(dev
);
863 skb_queue_tail(&ppp
->file
.xq
, skb
);
864 ppp_xmit_process(ppp
);
869 ++ppp
->stats
.tx_dropped
;
873 static struct net_device_stats
*
874 ppp_net_stats(struct net_device
*dev
)
876 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
882 ppp_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
884 struct ppp
*ppp
= dev
->priv
;
886 void *addr
= (void *) ifr
->ifr_ifru
.ifru_data
;
887 struct ppp_stats stats
;
888 struct ppp_comp_stats cstats
;
893 ppp_get_stats(ppp
, &stats
);
894 if (copy_to_user(addr
, &stats
, sizeof(stats
)))
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
)))
912 if (copy_to_user(addr
, vers
, strlen(vers
) + 1))
925 ppp_net_init(struct net_device
*dev
)
927 dev
->hard_header_len
= PPP_HDRLEN
;
929 dev
->hard_start_xmit
= ppp_start_xmit
;
930 dev
->get_stats
= ppp_net_stats
;
931 dev
->do_ioctl
= ppp_net_ioctl
;
933 dev
->tx_queue_len
= 3;
934 dev
->type
= ARPHRD_PPP
;
935 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
940 * Transmit-side routines.
944 * Called to do any work queued up on the transmit side
945 * that can now be done.
948 ppp_xmit_process(struct ppp
*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.
972 ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
974 int proto
= PPP_PROTO(skb
);
975 struct sk_buff
*new_skb
;
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) {
989 printk(KERN_DEBUG
"PPP: outbound frame not passed\n");
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
;
1002 switch (IP_PROTO(skb
)) {
1004 switch (DST_PORT(skb
)) {
1005 case 139: // netbios-ssn
1006 case 445: // microsoft-ds
1009 ppp
->last_xmit
= jiffies
;
1014 switch (DST_PORT(skb
)) {
1015 case 137: // netbios-ns
1016 case 138: // netbios-dgm
1019 ppp
->last_xmit
= jiffies
;
1024 ppp
->last_xmit
= jiffies
;
1028 /* for data packets, record the time */
1029 ppp
->last_xmit
= jiffies
;
1031 #endif /* CONFIG_PPP_FILTER */
1034 ++ppp
->stats
.tx_packets
;
1035 ppp
->stats
.tx_bytes
+= skb
->len
- 2;
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
)
1051 if (ppp
->vj
== 0 || (ppp
->flags
& SC_COMP_TCP
) == 0)
1053 /* try to do VJ TCP header compression */
1054 new_skb
= alloc_skb(skb
->len
+ ppp
->dev
->hard_header_len
- 2,
1057 printk(KERN_ERR
"PPP: no memory (VJ comp pkt)\n");
1060 skb_reserve(new_skb
, ppp
->dev
->hard_header_len
- 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 */
1069 if (cp
[0] & SL_TYPE_COMPRESSED_TCP
) {
1070 proto
= PPP_VJC_COMP
;
1071 cp
[0] &= ~SL_TYPE_COMPRESSED_TCP
;
1073 proto
= PPP_VJC_UNCOMP
;
1074 cp
[0] = skb
->data
[2];
1078 cp
= skb_put(skb
, len
+ 2);
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;
1103 /* try to do packet compression */
1104 if ((ppp
->xstate
& SC_COMP_RUN
) && ppp
->xc_state
!= 0
1105 && proto
!= PPP_LCP
&& proto
!= PPP_CCP
) {
1108 * because of possible data expansion when MPPC or LZS
1109 * is used, allocate compressor's buffer 12.5% bigger
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
);
1119 printk(KERN_ERR
"PPP: no memory (comp pkt)\n");
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
)) {
1134 skb_pull(skb
, 2); /* pull off A/C bytes */
1135 } else if (len
== 0) {
1136 /* didn't compress, or CCP not up yet */
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
1147 printk(KERN_ERR
"ppp: compressor dropped pkt\n");
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
)
1160 skb_queue_tail(&ppp
->file
.rq
, skb
);
1161 wake_up_interruptible(&ppp
->file
.rwait
);
1165 ppp
->xmit_pending
= skb
;
1171 ++ppp
->stats
.tx_errors
;
1175 * Try to send the frame in xmit_pending.
1176 * The caller should have the xmit path locked.
1179 ppp_push(struct ppp
*ppp
)
1181 struct list_head
*list
;
1182 struct channel
*pch
;
1183 struct sk_buff
*skb
= ppp
->xmit_pending
;
1188 list
= &ppp
->channels
;
1189 if (list_empty(list
)) {
1190 /* nowhere to send the packet, just drop it */
1191 ppp
->xmit_pending
= 0;
1196 if ((ppp
->flags
& SC_MULTILINK
) == 0) {
1197 /* not doing multilink: send it down the first channel */
1199 pch
= list_entry(list
, struct channel
, clist
);
1201 spin_lock_bh(&pch
->downl
);
1203 if (pch
->chan
->ops
->start_xmit(pch
->chan
, skb
))
1204 ppp
->xmit_pending
= 0;
1206 /* channel got unregistered */
1208 ppp
->xmit_pending
= 0;
1210 spin_unlock_bh(&pch
->downl
);
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
))
1219 #endif /* CONFIG_PPP_MULTILINK */
1221 ppp
->xmit_pending
= 0;
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
;
1235 unsigned char *p
, *q
;
1236 struct list_head
*list
;
1237 struct channel
*pch
;
1238 struct sk_buff
*frag
;
1239 struct ppp_channel
*chan
;
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
);
1262 return 0; /* can't take now, leave it in xmit_pending */
1264 /* Do protocol field compression (XXX this should be optional) */
1272 /* decide on fragment size */
1275 int maxch
= ROUNDUP(len
, MIN_FRAG_SIZE
);
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
) {
1285 if (list
== &ppp
->channels
) {
1291 /* create a fragment for each channel */
1295 if (list
== &ppp
->channels
) {
1299 pch
= list_entry(list
, struct channel
, clist
);
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
);
1316 * We have to create multiple fragments for this channel
1317 * if fragsize is greater than the channel's mtu.
1321 for (flen
= fragsize
; flen
> 0; flen
-= fnb
) {
1323 if (fnb
> mtu
+ 2 - hdrlen
)
1324 fnb
= mtu
+ 2 - hdrlen
;
1327 frag
= alloc_skb(fnb
+ hdrlen
, GFP_ATOMIC
);
1330 q
= skb_put(frag
, fnb
+ hdrlen
);
1331 /* make the MP header */
1334 if (ppp
->flags
& SC_MP_XSHORTSEQ
) {
1335 q
[2] = bits
+ ((ppp
->nxseq
>> 8) & 0xf);
1339 q
[3] = ppp
->nxseq
>> 16;
1340 q
[4] = ppp
->nxseq
>> 8;
1344 /* copy the data in */
1345 memcpy(q
+ hdrlen
, p
, fnb
);
1347 /* try to send it down the channel */
1349 if (!chan
->ops
->start_xmit(chan
, frag
))
1350 skb_queue_tail(&pch
->file
.xq
, frag
);
1357 spin_unlock_bh(&pch
->downl
);
1364 spin_unlock_bh(&pch
->downl
);
1366 printk(KERN_ERR
"PPP: no memory (fragment)\n");
1367 ++ppp
->stats
.tx_errors
;
1369 return 1; /* abandon the frame */
1371 #endif /* CONFIG_PPP_MULTILINK */
1374 * Try to send data out on a channel.
1377 ppp_channel_push(struct channel
*pch
)
1379 struct sk_buff
*skb
;
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
);
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
);
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]
1416 ppp_do_recv(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1419 /* ppp->dev == 0 means interface is closing down */
1421 ppp_receive_frame(ppp
, skb
, pch
);
1424 ppp_recv_unlock(ppp
);
1428 ppp_input(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1430 struct channel
*pch
= chan
->ppp
;
1433 if (pch
== 0 || skb
->len
== 0) {
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)
1447 wake_up_interruptible(&pch
->file
.rwait
);
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 */
1456 ppp_input_error(struct ppp_channel
*chan
, int code
)
1458 struct channel
*pch
= chan
->ppp
;
1459 struct sk_buff
*skb
;
1464 read_lock_bh(&pch
->upl
);
1465 if (pch
->ppp
!= 0) {
1466 skb
= alloc_skb(0, GFP_ATOMIC
);
1468 skb
->len
= 0; /* probably unnecessary */
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.
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
);
1489 #endif /* CONFIG_PPP_MULTILINK */
1490 ppp_receive_nonmp_frame(ppp
, skb
);
1495 /* note: a 0-length skb is used as an error indication */
1496 ++ppp
->stats
.rx_length_errors
;
1499 ppp_receive_error(ppp
);
1503 ppp_receive_error(struct ppp
*ppp
)
1505 ++ppp
->stats
.rx_errors
;
1511 ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
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
);
1528 /* decompress VJ compressed packets */
1529 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
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);
1535 printk(KERN_ERR
"PPP: no memory (VJ decomp)\n");
1539 memcpy(skb_put(ns
, skb
->len
), skb
->data
, skb
->len
);
1543 len
= slhc_uncompress(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2);
1545 printk(KERN_DEBUG
"PPP: VJ decompression error\n");
1550 skb_put(skb
, len
- skb
->len
);
1551 else if (len
< skb
->len
)
1556 case PPP_VJC_UNCOMP
:
1557 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
1559 if (slhc_remember(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2) <= 0) {
1560 printk(KERN_ERR
"PPP: VJ uncompressed error\n");
1567 ppp_ccp_peek(ppp
, skb
, 1);
1571 ++ppp
->stats
.rx_packets
;
1572 ppp
->stats
.rx_bytes
+= skb
->len
- 2;
1574 npi
= proto_to_npindex(proto
);
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)
1582 /* wake up any process polling or blocking on read */
1583 wake_up_interruptible(&ppp
->file
.rwait
);
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
))
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) {
1602 printk(KERN_DEBUG
"PPP: inbound frame "
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
;
1613 #endif /* CONFIG_PPP_FILTER */
1614 ppp
->last_recv
= jiffies
;
1616 if ((ppp
->dev
->flags
& IFF_UP
) == 0
1617 || ppp
->npmode
[npi
] != NPMODE_PASS
) {
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
;
1625 ppp
->dev
->last_rx
= jiffies
;
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
);
1642 if (proto
== PPP_COMP
) {
1643 ns
= dev_alloc_skb(ppp
->mru_alloc
+ PPP_HDRLEN
);
1645 printk(KERN_ERR
"ppp_decompress_frame: no memory\n");
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
);
1653 /* Pass the compressed frame to pppd as an
1654 error indication. */
1655 if (len
== DECOMP_FATALERROR
)
1656 ppp
->rstate
|= SC_DC_FERROR
;
1664 skb_pull(skb
, 2); /* pull off the A/C bytes */
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,
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
);
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.
1696 ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
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];
1710 seq
= (skb
->data
[3] << 16) | (skb
->data
[4] << 8)| skb
->data
[5];
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))
1729 else if ((int)(seq
- ppp
->minseq
) > (int)(mask
>> 1))
1730 seq
-= mask
+ 1; /* should never happen */
1731 skb
->sequence
= seq
;
1735 * If this packet comes before the next one we were expecting,
1738 if (seq_before(seq
, ppp
->nextseq
)) {
1740 ++ppp
->stats
.rx_dropped
;
1741 ppp_receive_error(ppp
);
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
))
1758 if (seq_before(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);
1776 ++ppp->stats.rx_length_errors;
1778 ppp_receive_error(ppp);
1783 //shibby - backport from ddwrt source - fix mlppp
1784 while ((skb
= ppp_mp_reconstruct(ppp
)) != 0)
1785 ppp_receive_nonmp_frame(ppp
, skb
);
1792 ppp_receive_error(ppp
);
1796 * Insert a fragment on the MP reconstruction queue.
1797 * The queue is ordered by increasing sequence number.
1800 ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
)
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
))
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.
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 */
1835 for (p
= head
; p
!= (struct sk_buff
*) list
; 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",
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
))
1849 /* Fragment `seq' is lost, keep going. */
1851 seq
= seq_before(minseq
, p
->sequence
)?
1852 minseq
+ 1: p
->sequence
;
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
1865 /* B bit set indicates this fragment starts a packet */
1866 if (p
->BEbits
& B
) {
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 */
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");
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.
1907 /* If we have a complete packet, copy it all into one skb. */
1909 /* If we have discarded any fragments,
1910 signal a receive error. */
1911 if (head
->sequence
!= ppp
->nextseq
) {
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
);
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;
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
);
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
);
1953 memset(pch
, 0, sizeof(struct channel
));
1957 init_ppp_file(&pch
->file
, CHANNEL
);
1958 pch
->file
.hdrlen
= chan
->hdrlen
;
1959 #ifdef CONFIG_PPP_MULTILINK
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
);
1975 * Return the index of a channel.
1977 int ppp_channel_index(struct ppp_channel
*chan
)
1979 struct channel
*pch
= chan
->ppp
;
1982 return pch
->file
.index
;
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
;
1995 read_lock_bh(&pch
->upl
);
1997 unit
= pch
->ppp
->file
.index
;
1998 read_unlock_bh(&pch
->upl
);
2004 * Disconnect a channel from the generic layer.
2005 * This must be called in process context.
2008 ppp_unregister_channel(struct ppp_channel
*chan
)
2010 struct channel
*pch
= chan
->ppp
;
2013 return; /* should never happen */
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
);
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
);
2030 wake_up_interruptible(&pch
->file
.rwait
);
2031 if (atomic_dec_and_test(&pch
->file
.refcnt
))
2032 ppp_destroy_channel(pch
);
2037 * Callback from a channel when it can accept more to transmit.
2038 * This should be called at BH/softirq level, not interrupt level.
2041 ppp_output_wakeup(struct ppp_channel
*chan
)
2043 struct channel
*pch
= chan
->ppp
;
2047 ppp_channel_push(pch
);
2051 * Compression control.
2054 /* Process the PPPIOCSCOMPRESS ioctl. */
2056 ppp_set_compress(struct ppp
*ppp
, unsigned long arg
)
2059 struct compressor
*cp
, *ocomp
;
2060 struct ppp_option_data data
;
2061 void *state
, *ostate
;
2062 unsigned char ccp_option
[CCP_MAX_OPTION_LENGTH
];
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
)))
2073 if (data
.length
> CCP_MAX_OPTION_LENGTH
2074 || ccp_option
[1] < 2 || ccp_option
[1] > data
.length
)
2077 cp
= find_compressor(ccp_option
[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 */
2088 * XXX race: the compressor module could get unloaded between
2089 * here and when we do the comp_alloc or decomp_alloc call below.
2093 if (data
.transmit
) {
2094 state
= cp
->comp_alloc(ccp_option
, data
.length
);
2097 ppp
->xstate
&= ~SC_COMP_RUN
;
2099 ostate
= ppp
->xc_state
;
2101 ppp
->xc_state
= state
;
2102 ppp_xmit_unlock(ppp
);
2104 ocomp
->comp_free(ostate
);
2109 state
= cp
->decomp_alloc(ccp_option
, data
.length
);
2112 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2114 ostate
= ppp
->rc_state
;
2116 ppp
->rc_state
= state
;
2117 ppp_recv_unlock(ppp
);
2119 ocomp
->decomp_free(ostate
);
2129 * Look at a CCP packet and update our state accordingly.
2130 * We assume the caller has the xmit or recv path locked.
2133 ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
)
2135 unsigned char *dp
= skb
->data
+ 2;
2138 if (skb
->len
< CCP_HDRLEN
+ 2
2139 || skb
->len
< (len
= CCP_LENGTH(dp
)) + 2)
2140 return; /* too short */
2142 switch (CCP_CODE(dp
)) {
2145 /* A ConfReq starts negotiation of compression
2146 * in one direction of transmission,
2147 * and hence brings it down...but which way?
2150 * A ConfReq indicates what the sender would like to receive
2153 /* He is proposing what I should send */
2154 ppp
->xstate
&= ~SC_COMP_RUN
;
2156 /* I am proposing to what he should send */
2157 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2164 * CCP is going down, both directions of transmission
2166 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2167 ppp
->xstate
&= ~SC_COMP_RUN
;
2171 if ((ppp
->flags
& (SC_CCP_OPEN
| SC_CCP_UP
)) != SC_CCP_OPEN
)
2175 if (len
< CCP_OPT_MINLEN
|| len
< CCP_OPT_LENGTH(dp
))
2178 /* we will start receiving compressed packets */
2179 if (ppp
->rc_state
== 0)
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
);
2187 /* we will soon start sending compressed packets */
2188 if (ppp
->xc_state
== 0)
2190 if (ppp
->xcomp
->comp_init(ppp
->xc_state
, dp
, len
,
2191 ppp
->file
.index
, 0, ppp
->debug
))
2192 ppp
->xstate
|= SC_COMP_RUN
;
2197 /* reset the [de]compressor */
2198 if ((ppp
->flags
& SC_CCP_UP
) == 0)
2201 if (ppp
->rc_state
&& (ppp
->rstate
& SC_DECOMP_RUN
)) {
2202 ppp
->rcomp
->decomp_reset(ppp
->rc_state
);
2203 ppp
->rstate
&= ~SC_DC_ERROR
;
2206 if (ppp
->xc_state
&& (ppp
->xstate
& SC_COMP_RUN
))
2207 ppp
->xcomp
->comp_reset(ppp
->xc_state
);
2213 /* Free up compression resources. */
2215 ppp_ccp_closed(struct ppp
*ppp
)
2217 void *xstate
, *rstate
;
2218 struct compressor
*xcomp
, *rcomp
;
2221 ppp
->flags
&= ~(SC_CCP_OPEN
| SC_CCP_UP
);
2224 xstate
= ppp
->xc_state
;
2228 rstate
= ppp
->rc_state
;
2233 xcomp
->comp_free(xstate
);
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
)
2261 /* Register a compressor */
2263 ppp_register_compressor(struct compressor
*cp
)
2265 struct compressor_entry
*ce
;
2267 spin_lock(&compressor_list_lock
);
2269 if (find_comp_entry(cp
->compress_proto
) != 0)
2272 ce
= kmalloc(sizeof(struct compressor_entry
), GFP_ATOMIC
);
2277 list_add(&ce
->list
, &compressor_list
);
2279 spin_unlock(&compressor_list_lock
);
2283 /* Unregister a compressor */
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
);
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
);
2309 spin_unlock(&compressor_list_lock
);
2314 * Miscelleneous stuff.
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
;
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.
2352 ppp_create_interface(int unit
, int *retp
)
2355 struct net_device
*dev
= NULL
;
2359 ppp
= kmalloc(sizeof(struct ppp
), GFP_KERNEL
);
2362 dev
= kmalloc(sizeof(struct net_device
), GFP_KERNEL
);
2365 memset(ppp
, 0, sizeof(struct ppp
));
2366 memset(dev
, 0, sizeof(struct net_device
));
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
;
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
2388 skb_queue_head_init(&ppp
->mrq
);
2389 #endif /* CONFIG_PPP_MULTILINK */
2392 dev
->init
= ppp_net_init
;
2393 sprintf(dev
->name
, "ppp%d", unit
);
2395 dev
->features
|= NETIF_F_DYNALLOC
;
2398 ret
= register_netdevice(dev
);
2401 printk(KERN_ERR
"PPP: couldn't register device %s (%d)\n",
2406 atomic_inc(&ppp_unit_count
);
2407 cardmap_set(&all_ppp_units
, unit
, ppp
);
2424 * Initialize a ppp_file structure.
2427 init_ppp_file(struct ppp_file
*pf
, int 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
;
2452 unregister_netdevice(dev
);
2455 cardmap_set(&all_ppp_units
, ppp
->file
.index
, NULL
);
2458 wake_up_interruptible(&ppp
->file
.rwait
);
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
,
2479 ppp_ccp_closed(ppp
);
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
);
2507 * Locate an existing ppp unit.
2508 * The caller should have locked the all_ppp_sem.
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
);
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
)
2549 * Connect a PPP channel to a PPP interface unit.
2552 ppp_connect_channel(struct channel
*pch
, int unit
)
2559 ppp
= ppp_find_unit(unit
);
2563 write_lock_bh(&pch
->upl
);
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
);
2577 atomic_inc(&ppp
->file
.refcnt
);
2582 write_unlock_bh(&pch
->upl
);
2589 * Disconnect a channel from its ppp unit.
2592 ppp_disconnect_channel(struct channel
*pch
)
2597 write_lock_bh(&pch
->upl
);
2600 write_unlock_bh(&pch
->upl
);
2602 /* remove it from the ppp unit's list */
2604 list_del(&pch
->clist
);
2607 if (atomic_dec_and_test(&ppp
->file
.refcnt
))
2608 ppp_destroy_interface(ppp
);
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",
2627 skb_queue_purge(&pch
->file
.xq
);
2628 skb_queue_purge(&pch
->file
.rq
);
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
)
2652 for (p
= map
; p
!= NULL
; ) {
2653 if ((i
= nr
>> p
->shift
) >= CARDMAP_WIDTH
)
2657 nr
&= ~(CARDMAP_MASK
<< p
->shift
);
2663 static void cardmap_set(struct cardmap
**pmap
, unsigned int nr
, void *ptr
)
2669 if (p
== NULL
|| (nr
>> p
->shift
) >= CARDMAP_WIDTH
) {
2671 /* need a new top level */
2672 struct cardmap
*np
= kmalloc(sizeof(*np
), GFP_KERNEL
);
2673 memset(np
, 0, sizeof(*np
));
2676 np
->shift
= p
->shift
+ CARDMAP_ORDER
;
2681 } while ((nr
>> p
->shift
) >= CARDMAP_WIDTH
);
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
;
2694 clear_bit(i
, &p
->inuse
);
2697 i
= nr
& CARDMAP_MASK
;
2700 set_bit(i
, &p
->inuse
);
2702 clear_bit(i
, &p
->inuse
);
2705 static unsigned int cardmap_find_first_free(struct cardmap
*map
)
2708 unsigned int nr
= 0;
2711 if ((p
= map
) == NULL
)
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
;
2719 i
= (nr
>> p
->shift
) & CARDMAP_MASK
;
2720 set_bit(i
, &p
->inuse
);
2723 nr
= (nr
& (~CARDMAP_MASK
<< p
->shift
)) | (i
<< p
->shift
);
2724 if (p
->shift
== 0 || p
->ptr
[i
] == NULL
)
2730 static void cardmap_destroy(struct cardmap
**pmap
)
2732 struct cardmap
*p
, *np
;
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
)
2740 if (i
< CARDMAP_WIDTH
) {
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
},
2761 static ctl_table ppp_filter_dir_table
[] = {
2762 {NET_IPV4
, "ipv4", NULL
, 0, 0555, ppp_filter_table
, 0, 0, 0, 0, 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},
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");