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 20050110==
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/kmod.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/netdevice.h>
31 #include <linux/poll.h>
32 #include <linux/ppp_defs.h>
33 #include <linux/filter.h>
34 #include <linux/if_ppp.h>
35 #include <linux/ppp_channel.h>
36 #include <linux/ppp-comp.h>
37 #include <linux/skbuff.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/if_arp.h>
41 #include <linux/tcp.h>
42 #include <linux/spinlock.h>
43 #include <linux/smp_lock.h>
44 #include <linux/rwsem.h>
45 #include <linux/stddef.h>
46 #include <linux/device.h>
47 #include <linux/mutex.h>
48 #include <linux/ppp_async.h>
49 #include <net/slhc_vj.h>
50 #include <asm/atomic.h>
54 #include <ctf/hndctf.h>
55 #include <linux/if_pppox.h>
58 #define PPP_VERSION "2.4.2"
61 * Network protocols we support.
63 #define NP_IP 0 /* Internet Protocol V4 */
64 #define NP_IPV6 1 /* Internet Protocol V6 */
65 #define NP_IPX 2 /* IPX protocol */
66 #define NP_AT 3 /* Appletalk protocol */
67 #define NP_MPLS_UC 4 /* MPLS unicast */
68 #define NP_MPLS_MC 5 /* MPLS multicast */
69 #define NUM_NP 6 /* Number of NPs. */
71 #define MPHDRLEN 6 /* multilink protocol header length */
72 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
73 #define MIN_FRAG_SIZE 64
76 * An instance of /dev/ppp can be associated with either a ppp
77 * interface unit or a ppp channel. In both cases, file->private_data
78 * points to one of these.
84 struct sk_buff_head xq
; /* pppd transmit queue */
85 struct sk_buff_head rq
; /* receive queue for pppd */
86 wait_queue_head_t rwait
; /* for poll on reading /dev/ppp */
87 atomic_t refcnt
; /* # refs (incl /dev/ppp attached) */
88 int hdrlen
; /* space to leave for headers */
89 int index
; /* interface unit / channel number */
90 int dead
; /* unit/channel has been shut down */
93 #define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
95 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
96 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
98 #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
101 * Data structure describing one ppp unit.
102 * A ppp unit corresponds to a ppp network interface device
103 * and represents a multilink bundle.
104 * It can have 0 or more ppp channels connected to it.
107 typedef struct channel channel_t
;
111 struct ppp_file file
; /* stuff for read/write/poll 0 */
112 struct file
*owner
; /* file that owns this unit 48 */
113 struct list_head channels
; /* list of attached channels 4c */
114 int n_channels
; /* how many channels are attached 54 */
115 spinlock_t rlock
; /* lock for receive side 58 */
116 spinlock_t wlock
; /* lock for transmit side 5c */
117 int mru
; /* max receive unit 60 */
118 int mru_alloc
; /* MAX(1500,MRU) for dev_alloc_skb() */
119 unsigned int flags
; /* control bits 64 */
120 unsigned int xstate
; /* transmit state bits 68 */
121 unsigned int rstate
; /* receive state bits 6c */
122 int debug
; /* debug flags 70 */
123 struct slcompress
*vj
; /* state for VJ header compression */
124 enum NPmode npmode
[NUM_NP
]; /* what to do with each net proto 78 */
125 struct sk_buff
*xmit_pending
; /* a packet ready to go out 88 */
126 struct compressor
*xcomp
; /* transmit packet compressor 8c */
127 void *xc_state
; /* its internal state 90 */
128 struct compressor
*rcomp
; /* receive decompressor 94 */
129 void *rc_state
; /* its internal state 98 */
130 unsigned long last_xmit
; /* jiffies when last pkt sent 9c */
131 unsigned long last_recv
; /* jiffies when last pkt rcvd a0 */
132 struct net_device
*dev
; /* network interface device a4 */
133 #ifdef CONFIG_PPP_MULTILINK
134 int nxchan
; /* next channel to send something on */
135 u32 nxseq
; /* next sequence number to send */
136 int mrru
; /* MP: max reconst. receive unit */
137 u32 nextseq
; /* MP: seq no of next packet */
138 u32 minseq
; /* MP: min of most recent seqnos */
139 struct sk_buff_head mrq
; /* MP: receive reconstruction queue */
140 #endif /* CONFIG_PPP_MULTILINK */
141 struct net_device_stats stats
; /* statistics */
142 #ifdef CONFIG_PPP_FILTER
143 struct sock_filter
*pass_filter
; /* filter for packets to pass */
144 struct sock_filter
*active_filter
;/* filter for pkts to reset idle */
145 unsigned pass_len
, active_len
;
146 #endif /* CONFIG_PPP_FILTER */
153 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
154 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
155 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
156 * Bits in xstate: SC_COMP_RUN
158 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
159 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
160 |SC_COMP_TCP|SC_REJ_COMP_TCP)
163 * Private data structure for each channel.
164 * This includes the data structure used for multilink.
167 struct ppp_file file
; /* stuff for read/write/poll */
168 struct list_head list
; /* link in all/new_channels list */
169 struct ppp_channel
*chan
; /* public channel data structure */
170 struct rw_semaphore chan_sem
; /* protects `chan' during chan ioctl */
171 spinlock_t downl
; /* protects `chan', file.xq dequeue */
172 struct ppp
*ppp
; /* ppp unit we're connected to */
173 struct list_head clist
; /* link in list of channels per unit */
174 rwlock_t upl
; /* protects `ppp' */
175 #ifdef CONFIG_PPP_MULTILINK
176 u8 avail
; /* flag used in multilink stuff */
177 u8 had_frag
; /* >= 1 fragments have been sent */
178 u32 lastseq
; /* MP: last sequence # received */
179 #endif /* CONFIG_PPP_MULTILINK */
183 * SMP locking issues:
184 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
185 * list and the ppp.n_channels field, you need to take both locks
186 * before you modify them.
187 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
192 * A cardmap represents a mapping from unsigned integers to pointers,
193 * and provides a fast "find lowest unused number" operation.
194 * It uses a broad (32-way) tree with a bitmap at each level.
195 * It is designed to be space-efficient for small numbers of entries
196 * and time-efficient for large numbers of entries.
198 #define CARDMAP_ORDER 5
199 #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
200 #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
205 struct cardmap
*parent
;
206 void *ptr
[CARDMAP_WIDTH
];
208 static void *cardmap_get(struct cardmap
*map
, unsigned int nr
);
209 static int cardmap_set(struct cardmap
**map
, unsigned int nr
, void *ptr
);
210 static unsigned int cardmap_find_first_free(struct cardmap
*map
);
211 static void cardmap_destroy(struct cardmap
**map
);
214 * all_ppp_mutex protects the all_ppp_units mapping.
215 * It also ensures that finding a ppp unit in the all_ppp_units map
216 * and updating its file.refcnt field is atomic.
218 static DEFINE_MUTEX(all_ppp_mutex
);
219 static struct cardmap
*all_ppp_units
;
220 static atomic_t ppp_unit_count
= ATOMIC_INIT(0);
223 * all_channels_lock protects all_channels and last_channel_index,
224 * and the atomicity of find a channel and updating its file.refcnt
227 static DEFINE_SPINLOCK(all_channels_lock
);
228 static LIST_HEAD(all_channels
);
229 static LIST_HEAD(new_channels
);
230 static int last_channel_index
;
231 static atomic_t channel_count
= ATOMIC_INIT(0);
233 /* Get the PPP protocol number from a skb */
234 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
237 #define IP_PROTO(skb) (skb)->data[11]
238 #define SRC_PORT(skb) (((skb)->data[22] << 8) + (skb)->data[23])
239 #define DST_PORT(skb) (((skb)->data[24] << 8) + (skb)->data[25])
241 /* We limit the length of ppp->file.rq to this (arbitrary) value */
242 #define PPP_MAX_RQLEN 32
245 * Maximum number of multilink fragments queued up.
246 * This has to be large enough to cope with the maximum latency of
247 * the slowest channel relative to the others. Strictly it should
248 * depend on the number of channels and their characteristics.
250 #define PPP_MP_MAX_QLEN 128
252 /* Multilink header bits. */
253 #define B 0x80 /* this fragment begins a packet */
254 #define E 0x40 /* this fragment ends a packet */
256 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
257 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
258 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
261 static int ppp_unattached_ioctl(struct ppp_file
*pf
, struct file
*file
,
262 unsigned int cmd
, unsigned long arg
);
263 static void ppp_xmit_process(struct ppp
*ppp
);
264 static void ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
265 static void ppp_push(struct ppp
*ppp
);
266 static void ppp_channel_push(struct channel
*pch
);
267 static void ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
,
268 struct channel
*pch
);
269 static void ppp_receive_error(struct ppp
*ppp
);
270 static void ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
271 static struct sk_buff
*ppp_decompress_frame(struct ppp
*ppp
,
272 struct sk_buff
*skb
);
273 #ifdef CONFIG_PPP_MULTILINK
274 static void ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
,
275 struct channel
*pch
);
276 static void ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
);
277 static struct sk_buff
*ppp_mp_reconstruct(struct ppp
*ppp
);
278 static int ppp_mp_explode(struct ppp
*ppp
, struct sk_buff
*skb
);
279 #endif /* CONFIG_PPP_MULTILINK */
280 static int ppp_set_compress(struct ppp
*ppp
, unsigned long arg
);
281 static void ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
);
282 static void ppp_ccp_closed(struct ppp
*ppp
);
283 static struct compressor
*find_compressor(int type
);
284 static void ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
);
285 static struct ppp
*ppp_create_interface(int unit
, int *retp
);
286 static void init_ppp_file(struct ppp_file
*pf
, int kind
);
287 static void ppp_shutdown_interface(struct ppp
*ppp
);
288 static void ppp_destroy_interface(struct ppp
*ppp
);
289 static struct ppp
*ppp_find_unit(int unit
);
290 static struct channel
*ppp_find_channel(int unit
);
291 static int ppp_connect_channel(struct channel
*pch
, int unit
);
292 static int ppp_disconnect_channel(struct channel
*pch
);
293 static void ppp_destroy_channel(struct channel
*pch
);
295 static struct class *ppp_class
;
297 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
298 static inline int proto_to_npindex(int proto
)
317 /* Translates an NP index into a PPP protocol number */
318 static const int npindex_to_proto
[NUM_NP
] = {
327 /* Translates an ethertype into an NP index */
328 static inline int ethertype_to_npindex(int ethertype
)
348 /* Translates an NP index into an ethertype */
349 static const int npindex_to_ethertype
[NUM_NP
] = {
361 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
362 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
363 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
364 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
365 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
366 ppp_recv_lock(ppp); } while (0)
367 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
368 ppp_xmit_unlock(ppp); } while (0)
371 * /dev/ppp device routines.
372 * The /dev/ppp device is used by pppd to control the ppp unit.
373 * It supports the read, write, ioctl and poll functions.
374 * Open instances of /dev/ppp can be in one of three states:
375 * unattached, attached to a ppp unit, or attached to a ppp channel.
377 static int ppp_open(struct inode
*inode
, struct file
*file
)
380 * This could (should?) be enforced by the permissions on /dev/ppp.
382 if (!capable(CAP_NET_ADMIN
))
387 static int ppp_release(struct inode
*inode
, struct file
*file
)
389 struct ppp_file
*pf
= file
->private_data
;
393 file
->private_data
= NULL
;
394 if (pf
->kind
== INTERFACE
) {
396 if (file
== ppp
->owner
)
397 ppp_shutdown_interface(ppp
);
399 if (atomic_dec_and_test(&pf
->refcnt
)) {
402 ppp_destroy_interface(PF_TO_PPP(pf
));
405 ppp_destroy_channel(PF_TO_CHANNEL(pf
));
413 static ssize_t
ppp_read(struct file
*file
, char __user
*buf
,
414 size_t count
, loff_t
*ppos
)
416 struct ppp_file
*pf
= file
->private_data
;
417 DECLARE_WAITQUEUE(wait
, current
);
419 struct sk_buff
*skb
= NULL
;
426 add_wait_queue(&pf
->rwait
, &wait
);
428 set_current_state(TASK_INTERRUPTIBLE
);
429 skb
= skb_dequeue(&pf
->rq
);
435 if (pf
->kind
== INTERFACE
) {
437 * Return 0 (EOF) on an interface that has no
438 * channels connected, unless it is looping
439 * network traffic (demand mode).
441 struct ppp
*ppp
= PF_TO_PPP(pf
);
442 if (ppp
->n_channels
== 0
443 && (ppp
->flags
& SC_LOOP_TRAFFIC
) == 0)
447 if (file
->f_flags
& O_NONBLOCK
)
450 if (signal_pending(current
))
454 set_current_state(TASK_RUNNING
);
455 remove_wait_queue(&pf
->rwait
, &wait
);
461 if (skb
->len
> count
)
466 if (skb_copy_datagram_iovec(skb
, 0, &iov
, skb
->len
))
476 static ssize_t
ppp_write(struct file
*file
, const char __user
*buf
,
477 size_t count
, loff_t
*ppos
)
479 struct ppp_file
*pf
= file
->private_data
;
486 skb
= alloc_skb(count
+ pf
->hdrlen
, GFP_KERNEL
);
489 skb_reserve(skb
, pf
->hdrlen
);
491 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
496 skb_queue_tail(&pf
->xq
, skb
);
500 ppp_xmit_process(PF_TO_PPP(pf
));
503 ppp_channel_push(PF_TO_CHANNEL(pf
));
513 /* No kernel lock - fine */
514 static unsigned int ppp_poll(struct file
*file
, poll_table
*wait
)
516 struct ppp_file
*pf
= file
->private_data
;
521 poll_wait(file
, &pf
->rwait
, wait
);
522 mask
= POLLOUT
| POLLWRNORM
;
523 if (skb_peek(&pf
->rq
) != 0)
524 mask
|= POLLIN
| POLLRDNORM
;
527 else if (pf
->kind
== INTERFACE
) {
528 /* see comment in ppp_read */
529 struct ppp
*ppp
= PF_TO_PPP(pf
);
530 if (ppp
->n_channels
== 0
531 && (ppp
->flags
& SC_LOOP_TRAFFIC
) == 0)
532 mask
|= POLLIN
| POLLRDNORM
;
538 #ifdef CONFIG_PPP_FILTER
539 static int get_filter(void __user
*arg
, struct sock_filter
**p
)
541 struct sock_fprog uprog
;
542 struct sock_filter
*code
= NULL
;
545 if (copy_from_user(&uprog
, arg
, sizeof(uprog
)))
553 len
= uprog
.len
* sizeof(struct sock_filter
);
554 code
= kmalloc(len
, GFP_KERNEL
);
558 if (copy_from_user(code
, uprog
.filter
, len
)) {
563 err
= sk_chk_filter(code
, uprog
.len
);
572 #endif /* CONFIG_PPP_FILTER */
574 static int ppp_ioctl(struct inode
*inode
, struct file
*file
,
575 unsigned int cmd
, unsigned long arg
)
577 struct ppp_file
*pf
= file
->private_data
;
579 int err
= -EFAULT
, val
, val2
, i
;
580 struct ppp_idle idle
;
583 struct slcompress
*vj
;
584 void __user
*argp
= (void __user
*)arg
;
585 int __user
*p
= argp
;
588 return ppp_unattached_ioctl(pf
, file
, cmd
, arg
);
590 if (cmd
== PPPIOCDETACH
) {
592 * We have to be careful here... if the file descriptor
593 * has been dup'd, we could have another process in the
594 * middle of a poll using the same file *, so we had
595 * better not free the interface data structures -
596 * instead we fail the ioctl. Even in this case, we
597 * shut down the interface if we are the owner of it.
598 * Actually, we should get rid of PPPIOCDETACH, userland
599 * (i.e. pppd) could achieve the same effect by closing
600 * this fd and reopening /dev/ppp.
603 if (pf
->kind
== INTERFACE
) {
605 if (file
== ppp
->owner
)
606 ppp_shutdown_interface(ppp
);
608 if (atomic_read(&file
->f_count
) <= 2) {
609 ppp_release(inode
, file
);
612 printk(KERN_DEBUG
"PPPIOCDETACH file->f_count=%d\n",
613 atomic_read(&file
->f_count
));
617 if (pf
->kind
== CHANNEL
) {
618 struct channel
*pch
= PF_TO_CHANNEL(pf
);
619 struct ppp_channel
*chan
;
623 if (get_user(unit
, p
))
625 err
= ppp_connect_channel(pch
, unit
);
629 err
= ppp_disconnect_channel(pch
);
633 down_read(&pch
->chan_sem
);
636 if (chan
&& chan
->ops
->ioctl
)
637 err
= chan
->ops
->ioctl(chan
, cmd
, arg
);
638 up_read(&pch
->chan_sem
);
643 if (pf
->kind
!= INTERFACE
) {
645 printk(KERN_ERR
"PPP: not interface or channel??\n");
652 if (get_user(val
, p
))
654 ppp
->mru_alloc
= ppp
->mru
= val
;
655 if (ppp
->mru_alloc
< PPP_MRU
)
656 ppp
->mru_alloc
= PPP_MRU
; /* increase for broken peers */
661 if (get_user(val
, p
))
664 cflags
= ppp
->flags
& ~val
;
665 ppp
->flags
= val
& SC_FLAG_BITS
;
667 if (cflags
& SC_CCP_OPEN
)
673 val
= ppp
->flags
| ppp
->xstate
| ppp
->rstate
;
674 if (put_user(val
, p
))
679 case PPPIOCSCOMPRESS
:
680 err
= ppp_set_compress(ppp
, arg
);
684 if (put_user(ppp
->file
.index
, p
))
690 if (get_user(val
, p
))
697 if (put_user(ppp
->debug
, p
))
703 idle
.xmit_idle
= (jiffies
- ppp
->last_xmit
) / HZ
;
704 idle
.recv_idle
= (jiffies
- ppp
->last_recv
) / HZ
;
705 if (copy_to_user(argp
, &idle
, sizeof(idle
)))
711 if (get_user(val
, p
))
714 if ((val
>> 16) != 0) {
718 vj
= slhc_init(val2
+1, val
+1);
720 printk(KERN_ERR
"PPP: no memory (VJ compressor)\n");
734 if (copy_from_user(&npi
, argp
, sizeof(npi
)))
736 err
= proto_to_npindex(npi
.protocol
);
740 if (cmd
== PPPIOCGNPMODE
) {
742 npi
.mode
= ppp
->npmode
[i
];
743 if (copy_to_user(argp
, &npi
, sizeof(npi
)))
746 ppp
->npmode
[i
] = npi
.mode
;
747 /* we may be able to transmit more packets now (??) */
748 netif_wake_queue(ppp
->dev
);
753 #ifdef CONFIG_PPP_FILTER
756 struct sock_filter
*code
;
757 err
= get_filter(argp
, &code
);
760 kfree(ppp
->pass_filter
);
761 ppp
->pass_filter
= code
;
770 struct sock_filter
*code
;
771 err
= get_filter(argp
, &code
);
774 kfree(ppp
->active_filter
);
775 ppp
->active_filter
= code
;
776 ppp
->active_len
= err
;
782 #endif /* CONFIG_PPP_FILTER */
784 #ifdef CONFIG_PPP_MULTILINK
786 if (get_user(val
, p
))
790 ppp_recv_unlock(ppp
);
793 #endif /* CONFIG_PPP_MULTILINK */
802 static int ppp_unattached_ioctl(struct ppp_file
*pf
, struct file
*file
,
803 unsigned int cmd
, unsigned long arg
)
805 int unit
, err
= -EFAULT
;
807 struct channel
*chan
;
808 int __user
*p
= (int __user
*)arg
;
812 /* Create a new ppp unit */
813 if (get_user(unit
, p
))
815 ppp
= ppp_create_interface(unit
, &err
);
818 file
->private_data
= &ppp
->file
;
821 if (put_user(ppp
->file
.index
, p
))
827 /* Attach to an existing ppp unit */
828 if (get_user(unit
, p
))
830 mutex_lock(&all_ppp_mutex
);
832 ppp
= ppp_find_unit(unit
);
834 atomic_inc(&ppp
->file
.refcnt
);
835 file
->private_data
= &ppp
->file
;
838 mutex_unlock(&all_ppp_mutex
);
842 if (get_user(unit
, p
))
844 spin_lock_bh(&all_channels_lock
);
846 chan
= ppp_find_channel(unit
);
848 atomic_inc(&chan
->file
.refcnt
);
849 file
->private_data
= &chan
->file
;
852 spin_unlock_bh(&all_channels_lock
);
861 static const struct file_operations ppp_device_fops
= {
862 .owner
= THIS_MODULE
,
868 .release
= ppp_release
871 #define PPP_MAJOR 108
873 /* Called at boot time if ppp is compiled into the kernel,
874 or at module load time (from init_module) if compiled as a module. */
875 static int __init
ppp_init(void)
879 printk(KERN_INFO
"PPP generic driver version " PPP_VERSION
"\n");
880 err
= register_chrdev(PPP_MAJOR
, "ppp", &ppp_device_fops
);
882 ppp_class
= class_create(THIS_MODULE
, "ppp");
883 if (IS_ERR(ppp_class
)) {
884 err
= PTR_ERR(ppp_class
);
887 device_create(ppp_class
, NULL
, MKDEV(PPP_MAJOR
, 0), "ppp");
892 printk(KERN_ERR
"failed to register PPP device (%d)\n", err
);
896 unregister_chrdev(PPP_MAJOR
, "ppp");
901 * Network interface unit routines.
904 ppp_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
906 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
910 npi
= ethertype_to_npindex(ntohs(skb
->protocol
));
914 /* Drop, accept or reject the packet */
915 switch (ppp
->npmode
[npi
]) {
919 /* it would be nice to have a way to tell the network
920 system to queue this one up for later. */
927 /* Put the 2-byte PPP protocol number on the front,
928 making sure there is room for the address and control fields. */
929 if (skb_cow_head(skb
, PPP_HDRLEN
))
932 pp
= skb_push(skb
, 2);
933 proto
= npindex_to_proto
[npi
];
937 skb_queue_tail(&ppp
->file
.xq
, skb
);
938 ppp_xmit_process(ppp
);
943 ++ppp
->stats
.tx_dropped
;
947 static struct net_device_stats
*
948 ppp_net_stats(struct net_device
*dev
)
950 struct ppp
*ppp
= (struct ppp
*) dev
->priv
;
956 ppp_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
958 struct ppp
*ppp
= dev
->priv
;
960 void __user
*addr
= (void __user
*) ifr
->ifr_ifru
.ifru_data
;
961 struct ppp_stats stats
;
962 struct ppp_comp_stats cstats
;
967 ppp_get_stats(ppp
, &stats
);
968 if (copy_to_user(addr
, &stats
, sizeof(stats
)))
974 memset(&cstats
, 0, sizeof(cstats
));
975 if (ppp
->xc_state
!= 0)
976 ppp
->xcomp
->comp_stat(ppp
->xc_state
, &cstats
.c
);
977 if (ppp
->rc_state
!= 0)
978 ppp
->rcomp
->decomp_stat(ppp
->rc_state
, &cstats
.d
);
979 if (copy_to_user(addr
, &cstats
, sizeof(cstats
)))
986 if (copy_to_user(addr
, vers
, strlen(vers
) + 1))
998 static void ppp_setup(struct net_device
*dev
)
1000 dev
->hard_header_len
= PPP_HDRLEN
;
1003 dev
->tx_queue_len
= 3;
1004 dev
->type
= ARPHRD_PPP
;
1005 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
1009 * Transmit-side routines.
1013 * Called to do any work queued up on the transmit side
1014 * that can now be done.
1017 ppp_xmit_process(struct ppp
*ppp
)
1019 struct sk_buff
*skb
;
1022 if (ppp
->dev
!= 0) {
1024 while (ppp
->xmit_pending
== 0
1025 && (skb
= skb_dequeue(&ppp
->file
.xq
)) != 0)
1026 ppp_send_frame(ppp
, skb
);
1027 /* If there's no work left to do, tell the core net
1028 code that we can accept some more. */
1029 if (!ppp
->xmit_pending
&& !skb_peek(&ppp
->file
.xq
))
1030 netif_wake_queue(ppp
->dev
);
1032 netif_stop_queue(ppp
->dev
);
1034 ppp_xmit_unlock(ppp
);
1038 * Compress and send a frame.
1039 * The caller should have locked the xmit path,
1040 * and xmit_pending should be 0.
1043 ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1045 int proto
= PPP_PROTO(skb
);
1046 struct sk_buff
*new_skb
;
1050 if (proto
< 0x8000) {
1051 #ifdef CONFIG_PPP_FILTER
1052 /* check if we should pass this packet */
1053 /* the filter instructions are constructed assuming
1054 a four-byte PPP header on each packet */
1055 *skb_push(skb
, 2) = 1;
1056 if (ppp
->pass_filter
1057 && sk_run_filter(skb
, ppp
->pass_filter
,
1058 ppp
->pass_len
) == 0) {
1060 printk(KERN_DEBUG
"PPP: outbound frame not passed\n");
1064 /* if this packet passes the active filter, record the time */
1065 if (!(ppp
->active_filter
1066 && sk_run_filter(skb
, ppp
->active_filter
,
1067 ppp
->active_len
) == 0))
1068 ppp
->last_xmit
= jiffies
;
1073 switch (IP_PROTO(skb
)) {
1075 switch (DST_PORT(skb
)) {
1076 case 139: // netbios-ssn
1077 case 445: // microsoft-ds
1080 ppp
->last_xmit
= jiffies
;
1085 switch (DST_PORT(skb
)) {
1086 case 137: // netbios-ns
1087 case 138: // netbios-dgm
1090 ppp
->last_xmit
= jiffies
;
1095 ppp
->last_xmit
= jiffies
;
1099 /* for data packets, record the time */
1100 ppp
->last_xmit
= jiffies
;
1102 #endif /* CONFIG_PPP_FILTER */
1105 ++ppp
->stats
.tx_packets
;
1106 ppp
->stats
.tx_bytes
+= skb
->len
- 2;
1110 if (ppp
->vj
== 0 || (ppp
->flags
& SC_COMP_TCP
) == 0)
1112 /* try to do VJ TCP header compression */
1113 new_skb
= alloc_skb(skb
->len
+ ppp
->dev
->hard_header_len
- 2,
1116 printk(KERN_ERR
"PPP: no memory (VJ comp pkt)\n");
1119 skb_reserve(new_skb
, ppp
->dev
->hard_header_len
- 2);
1121 len
= slhc_compress(ppp
->vj
, cp
, skb
->len
- 2,
1122 new_skb
->data
+ 2, &cp
,
1123 !(ppp
->flags
& SC_NO_TCP_CCID
));
1124 if (cp
== skb
->data
+ 2) {
1125 /* didn't compress */
1128 if (cp
[0] & SL_TYPE_COMPRESSED_TCP
) {
1129 proto
= PPP_VJC_COMP
;
1130 cp
[0] &= ~SL_TYPE_COMPRESSED_TCP
;
1132 proto
= PPP_VJC_UNCOMP
;
1133 cp
[0] = skb
->data
[2];
1137 cp
= skb_put(skb
, len
+ 2);
1144 /* peek at outbound CCP frames */
1145 ppp_ccp_peek(ppp
, skb
, 0);
1147 * When LZS or MPPE/MPPC has been negotiated we don't send
1148 * CCP_RESETACK after receiving CCP_RESETREQ; in fact pppd
1149 * sends such a packet but we silently discard it here
1151 if (CCP_CODE(skb
->data
+2) == CCP_RESETACK
1152 && (ppp
->xcomp
->compress_proto
== CI_MPPE
1153 || ppp
->xcomp
->compress_proto
== CI_LZS
)) {
1154 --ppp
->stats
.tx_packets
;
1155 ppp
->stats
.tx_bytes
-= skb
->len
- 2;
1162 /* try to do packet compression */
1163 if ((ppp
->xstate
& SC_COMP_RUN
) && ppp
->xc_state
!= 0
1164 && proto
!= PPP_LCP
&& proto
!= PPP_CCP
) {
1167 * because of possible data expansion when MPPC or LZS
1168 * is used, allocate compressor's buffer 12.5% bigger
1171 if (ppp
->xcomp
->compress_proto
== CI_MPPE
)
1172 comp_ovhd
= ((ppp
->dev
->mtu
* 9) / 8) + 1 + MPPE_OVHD
;
1173 else if (ppp
->xcomp
->compress_proto
== CI_LZS
)
1174 comp_ovhd
= ((ppp
->dev
->mtu
* 9) / 8) + 1 + LZS_OVHD
;
1175 new_skb
= alloc_skb(ppp
->dev
->mtu
+ ppp
->dev
->hard_header_len
1176 + comp_ovhd
, GFP_ATOMIC
);
1178 printk(KERN_ERR
"PPP: no memory (comp pkt)\n");
1181 if (ppp
->dev
->hard_header_len
> PPP_HDRLEN
)
1182 skb_reserve(new_skb
,
1183 ppp
->dev
->hard_header_len
- PPP_HDRLEN
);
1185 /* compressor still expects A/C bytes in hdr */
1186 len
= ppp
->xcomp
->compress(ppp
->xc_state
, skb
->data
- 2,
1187 new_skb
->data
, skb
->len
+ 2,
1188 ppp
->dev
->mtu
+ PPP_HDRLEN
);
1189 if (len
> 0 && (ppp
->flags
& SC_CCP_UP
)) {
1193 skb_pull(skb
, 2); /* pull off A/C bytes */
1194 } else if (len
== 0) {
1195 /* didn't compress, or CCP not up yet */
1200 * MPPE requires that we do not send unencrypted
1201 * frames. The compressor will return -1 if we
1202 * should drop the frame. We cannot simply test
1203 * the compress_proto because MPPE and MPPC share
1206 printk(KERN_ERR
"ppp: compressor dropped pkt\n");
1213 * If we are waiting for traffic (demand dialling),
1214 * queue it up for pppd to receive.
1216 if (ppp
->flags
& SC_LOOP_TRAFFIC
) {
1217 if (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
)
1219 skb_queue_tail(&ppp
->file
.rq
, skb
);
1220 wake_up_interruptible(&ppp
->file
.rwait
);
1224 ppp
->xmit_pending
= skb
;
1230 ++ppp
->stats
.tx_errors
;
1234 * Try to send the frame in xmit_pending.
1235 * The caller should have the xmit path locked.
1238 ppp_push(struct ppp
*ppp
)
1240 struct list_head
*list
;
1241 struct channel
*pch
;
1242 struct sk_buff
*skb
= ppp
->xmit_pending
;
1247 list
= &ppp
->channels
;
1248 if (list_empty(list
)) {
1249 /* nowhere to send the packet, just drop it */
1250 ppp
->xmit_pending
= NULL
;
1255 if ((ppp
->flags
& SC_MULTILINK
) == 0) {
1256 /* not doing multilink: send it down the first channel */
1258 pch
= list_entry(list
, struct channel
, clist
);
1260 spin_lock_bh(&pch
->downl
);
1262 if (pch
->chan
->ops
->start_xmit(pch
->chan
, skb
))
1263 ppp
->xmit_pending
= NULL
;
1265 /* channel got unregistered */
1267 ppp
->xmit_pending
= NULL
;
1269 spin_unlock_bh(&pch
->downl
);
1273 #ifdef CONFIG_PPP_MULTILINK
1274 /* Multilink: fragment the packet over as many links
1275 as can take the packet at the moment. */
1276 if (!ppp_mp_explode(ppp
, skb
))
1278 #endif /* CONFIG_PPP_MULTILINK */
1280 ppp
->xmit_pending
= NULL
;
1284 #ifdef CONFIG_PPP_MULTILINK
1286 * Divide a packet to be transmitted into fragments and
1287 * send them out the individual links.
1289 static int ppp_mp_explode(struct ppp
*ppp
, struct sk_buff
*skb
)
1292 int i
, bits
, hdrlen
, mtu
;
1296 unsigned char *p
, *q
;
1297 struct list_head
*list
;
1298 struct channel
*pch
;
1299 struct sk_buff
*frag
;
1300 struct ppp_channel
*chan
;
1302 nfree
= 0; /* # channels which have no packet already queued */
1303 navail
= 0; /* total # of usable channels (not deregistered) */
1304 hdrlen
= (ppp
->flags
& SC_MP_XSHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1306 list_for_each_entry(pch
, &ppp
->channels
, clist
) {
1307 navail
+= pch
->avail
= (pch
->chan
!= NULL
);
1309 if (skb_queue_empty(&pch
->file
.xq
) ||
1314 if (!pch
->had_frag
&& i
< ppp
->nxchan
)
1321 * Don't start sending this packet unless at least half of
1322 * the channels are free. This gives much better TCP
1323 * performance if we have a lot of channels.
1325 if (nfree
== 0 || nfree
< navail
/ 2)
1326 return 0; /* can't take now, leave it in xmit_pending */
1328 /* Do protocol field compression (XXX this should be optional) */
1337 * Decide on fragment size.
1338 * We create a fragment for each free channel regardless of
1339 * how small they are (i.e. even 0 length) in order to minimize
1340 * the time that it will take to detect when a channel drops
1345 fragsize
= DIV_ROUND_UP(fragsize
, nfree
);
1346 /* nbigger channels get fragsize bytes, the rest get fragsize-1,
1347 except if nbigger==0, then they all get fragsize. */
1348 nbigger
= len
% nfree
;
1350 /* skip to the channel after the one we last used
1351 and start at that one */
1352 list
= &ppp
->channels
;
1353 for (i
= 0; i
< ppp
->nxchan
; ++i
) {
1355 if (list
== &ppp
->channels
) {
1361 /* create a fragment for each channel */
1363 while (nfree
> 0 || len
> 0) {
1365 if (list
== &ppp
->channels
) {
1369 pch
= list_entry(list
, struct channel
, clist
);
1375 * Skip this channel if it has a fragment pending already and
1376 * we haven't given a fragment to all of the free channels.
1378 if (pch
->avail
== 1) {
1386 /* check the channel's mtu and whether it is still attached. */
1387 spin_lock_bh(&pch
->downl
);
1388 if (pch
->chan
== NULL
) {
1389 /* can't use this channel, it's being deregistered */
1390 spin_unlock_bh(&pch
->downl
);
1398 * Create a fragment for this channel of
1399 * min(max(mtu+2-hdrlen, 4), fragsize, len) bytes.
1400 * If mtu+2-hdrlen < 4, that is a ridiculously small
1401 * MTU, so we use mtu = 2 + hdrlen.
1406 mtu
= pch
->chan
->mtu
+ 2 - hdrlen
;
1411 if (flen
== len
&& nfree
== 0)
1413 frag
= alloc_skb(flen
+ hdrlen
+ (flen
== 0), GFP_ATOMIC
);
1416 q
= skb_put(frag
, flen
+ hdrlen
);
1418 /* make the MP header */
1421 if (ppp
->flags
& SC_MP_XSHORTSEQ
) {
1422 q
[2] = bits
+ ((ppp
->nxseq
>> 8) & 0xf);
1426 q
[3] = ppp
->nxseq
>> 16;
1427 q
[4] = ppp
->nxseq
>> 8;
1433 * Unfortunately there is a bug in older versions of
1434 * the Linux PPP multilink reconstruction code where it
1435 * drops 0-length fragments. Therefore we make sure the
1436 * fragment has at least one byte of data. Any bytes
1437 * we add in this situation will end up as padding on the
1438 * end of the reconstructed packet.
1441 *skb_put(frag
, 1) = 0;
1443 memcpy(q
+ hdrlen
, p
, flen
);
1445 /* try to send it down the channel */
1447 if (!skb_queue_empty(&pch
->file
.xq
) ||
1448 !chan
->ops
->start_xmit(chan
, frag
))
1449 skb_queue_tail(&pch
->file
.xq
, frag
);
1455 spin_unlock_bh(&pch
->downl
);
1457 if (--nbigger
== 0 && fragsize
> 0)
1465 spin_unlock_bh(&pch
->downl
);
1467 printk(KERN_ERR
"PPP: no memory (fragment)\n");
1468 ++ppp
->stats
.tx_errors
;
1470 return 1; /* abandon the frame */
1472 #endif /* CONFIG_PPP_MULTILINK */
1475 * Try to send data out on a channel.
1478 ppp_channel_push(struct channel
*pch
)
1480 struct sk_buff
*skb
;
1483 spin_lock_bh(&pch
->downl
);
1484 if (pch
->chan
!= 0) {
1485 while (!skb_queue_empty(&pch
->file
.xq
)) {
1486 skb
= skb_dequeue(&pch
->file
.xq
);
1487 if (!pch
->chan
->ops
->start_xmit(pch
->chan
, skb
)) {
1488 /* put the packet back and try again later */
1489 skb_queue_head(&pch
->file
.xq
, skb
);
1494 /* channel got deregistered */
1495 skb_queue_purge(&pch
->file
.xq
);
1497 spin_unlock_bh(&pch
->downl
);
1498 /* see if there is anything from the attached unit to be sent */
1499 if (skb_queue_empty(&pch
->file
.xq
)) {
1500 read_lock_bh(&pch
->upl
);
1503 ppp_xmit_process(ppp
);
1504 read_unlock_bh(&pch
->upl
);
1509 * Receive-side routines.
1512 /* misuse a few fields of the skb for MP reconstruction */
1513 #define sequence priority
1514 #define BEbits cb[0]
1517 ppp_do_recv(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1520 /* ppp->dev == 0 means interface is closing down */
1522 ppp_receive_frame(ppp
, skb
, pch
);
1525 ppp_recv_unlock(ppp
);
1529 ppp_input(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1531 struct channel
*pch
= chan
->ppp
;
1539 read_lock_bh(&pch
->upl
);
1540 if (!pskb_may_pull(skb
, 2)) {
1543 ++pch
->ppp
->stats
.rx_length_errors
;
1544 ppp_receive_error(pch
->ppp
);
1549 proto
= PPP_PROTO(skb
);
1550 if (pch
->ppp
== 0 || proto
>= 0xc000 || proto
== PPP_CCPFRAG
) {
1551 /* put it on the channel queue */
1552 skb_queue_tail(&pch
->file
.rq
, skb
);
1553 /* drop old frames if queue too long */
1554 while (pch
->file
.rq
.qlen
> PPP_MAX_RQLEN
1555 && (skb
= skb_dequeue(&pch
->file
.rq
)) != 0)
1557 wake_up_interruptible(&pch
->file
.rwait
);
1559 ppp_do_recv(pch
->ppp
, skb
, pch
);
1563 read_unlock_bh(&pch
->upl
);
1566 /* Put a 0-length skb in the receive queue as an error indication */
1568 ppp_input_error(struct ppp_channel
*chan
, int code
)
1570 struct channel
*pch
= chan
->ppp
;
1571 struct sk_buff
*skb
;
1576 read_lock_bh(&pch
->upl
);
1577 if (pch
->ppp
!= 0) {
1578 skb
= alloc_skb(0, GFP_ATOMIC
);
1580 skb
->len
= 0; /* probably unnecessary */
1582 ppp_do_recv(pch
->ppp
, skb
, pch
);
1585 read_unlock_bh(&pch
->upl
);
1589 * We come in here to process a received frame.
1590 * The receive side of the ppp unit is locked.
1593 ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1595 /* note: a 0-length skb is used as an error indication */
1597 #ifdef CONFIG_PPP_MULTILINK
1598 /* XXX do channel-level decompression here */
1599 if (PPP_PROTO(skb
) == PPP_MP
)
1600 ppp_receive_mp_frame(ppp
, skb
, pch
);
1602 #endif /* CONFIG_PPP_MULTILINK */
1603 ppp_receive_nonmp_frame(ppp
, skb
);
1606 ppp_receive_error(ppp
);
1611 ppp_receive_error(struct ppp
*ppp
)
1613 ++ppp
->stats
.rx_errors
;
1619 ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1622 int proto
, len
, npi
;
1625 * Decompress the frame, if compressed.
1626 * Note that some decompressors need to see uncompressed frames
1627 * that come in as well as compressed frames.
1629 if (ppp
->rc_state
!= 0 && (ppp
->rstate
& SC_DECOMP_RUN
)
1630 && (ppp
->rstate
& (SC_DC_FERROR
| SC_DC_ERROR
)) == 0)
1631 skb
= ppp_decompress_frame(ppp
, skb
);
1633 proto
= PPP_PROTO(skb
);
1636 /* decompress VJ compressed packets */
1637 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
1640 if (skb_tailroom(skb
) < 124 || skb_cloned(skb
)) {
1641 /* copy to a new sk_buff with more tailroom */
1642 ns
= dev_alloc_skb(skb
->len
+ 128);
1644 printk(KERN_ERR
"PPP: no memory (VJ decomp)\n");
1648 skb_copy_bits(skb
, 0, skb_put(ns
, skb
->len
), skb
->len
);
1653 skb
->ip_summed
= CHECKSUM_NONE
;
1655 len
= slhc_uncompress(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2);
1657 printk(KERN_DEBUG
"PPP: VJ decompression error\n");
1662 skb_put(skb
, len
- skb
->len
);
1663 else if (len
< skb
->len
)
1668 case PPP_VJC_UNCOMP
:
1669 if (ppp
->vj
== 0 || (ppp
->flags
& SC_REJ_COMP_TCP
))
1672 /* Until we fix the decompressor need to make sure
1673 * data portion is linear.
1675 if (!pskb_may_pull(skb
, skb
->len
))
1678 if (slhc_remember(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2) <= 0) {
1679 printk(KERN_ERR
"PPP: VJ uncompressed error\n");
1686 ppp_ccp_peek(ppp
, skb
, 1);
1690 ++ppp
->stats
.rx_packets
;
1691 ppp
->stats
.rx_bytes
+= skb
->len
- 2;
1693 npi
= proto_to_npindex(proto
);
1695 /* control or unknown frame - pass it to pppd */
1696 skb_queue_tail(&ppp
->file
.rq
, skb
);
1697 /* limit queue length by dropping old frames */
1698 while (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
1699 && (skb
= skb_dequeue(&ppp
->file
.rq
)) != 0)
1701 /* wake up any process polling or blocking on read */
1702 wake_up_interruptible(&ppp
->file
.rwait
);
1705 /* network protocol frame - give it to the kernel */
1707 #ifdef CONFIG_PPP_FILTER
1708 /* check if the packet passes the pass and active filters */
1709 /* the filter instructions are constructed assuming
1710 a four-byte PPP header on each packet */
1711 if (ppp
->pass_filter
|| ppp
->active_filter
) {
1712 if (skb_cloned(skb
) &&
1713 pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
1716 *skb_push(skb
, 2) = 0;
1717 if (ppp
->pass_filter
1718 && sk_run_filter(skb
, ppp
->pass_filter
,
1719 ppp
->pass_len
) == 0) {
1721 printk(KERN_DEBUG
"PPP: inbound frame "
1726 if (!(ppp
->active_filter
1727 && sk_run_filter(skb
, ppp
->active_filter
,
1728 ppp
->active_len
) == 0))
1729 ppp
->last_recv
= jiffies
;
1732 #endif /* CONFIG_PPP_FILTER */
1733 ppp
->last_recv
= jiffies
;
1735 if ((ppp
->dev
->flags
& IFF_UP
) == 0
1736 || ppp
->npmode
[npi
] != NPMODE_PASS
) {
1739 /* chop off protocol */
1740 skb_pull_rcsum(skb
, 2);
1741 skb
->dev
= ppp
->dev
;
1742 skb
->protocol
= htons(npindex_to_ethertype
[npi
]);
1743 skb_reset_mac_header(skb
);
1745 ppp
->dev
->last_rx
= jiffies
;
1752 ppp_receive_error(ppp
);
1755 static struct sk_buff
*
1756 ppp_decompress_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1758 int proto
= PPP_PROTO(skb
);
1762 /* Until we fix all the decompressor's need to make sure
1763 * data portion is linear.
1765 if (!pskb_may_pull(skb
, skb
->len
))
1768 if (proto
== PPP_COMP
) {
1771 switch(ppp
->rcomp
->compress_proto
) {
1773 obuff_size
= ppp
->mru_alloc
+ PPP_HDRLEN
+ 1;
1776 obuff_size
= ppp
->mru_alloc
+ PPP_HDRLEN
;
1780 ns
= dev_alloc_skb(obuff_size
);
1782 printk(KERN_ERR
"ppp_decompress_frame: no memory\n");
1785 /* the decompressor still expects the A/C bytes in the hdr */
1786 len
= ppp
->rcomp
->decompress(ppp
->rc_state
, skb
->data
- 2,
1787 skb
->len
+ 2, ns
->data
, obuff_size
);
1789 /* Pass the compressed frame to pppd as an
1790 error indication. */
1791 if (len
== DECOMP_FATALERROR
)
1792 ppp
->rstate
|= SC_DC_FERROR
;
1800 skb_pull(skb
, 2); /* pull off the A/C bytes */
1803 /* Uncompressed frame - pass to decompressor so it
1804 can update its dictionary if necessary. */
1805 if (ppp
->rcomp
->incomp
)
1806 ppp
->rcomp
->incomp(ppp
->rc_state
, skb
->data
- 2,
1813 if (ppp
->rcomp
->compress_proto
!= CI_MPPE
1814 && ppp
->rcomp
->compress_proto
!= CI_LZS
) {
1816 * If decompression protocol isn't MPPE/MPPC or LZS, we set
1817 * SC_DC_ERROR flag and wait for CCP_RESETACK
1819 ppp
->rstate
|= SC_DC_ERROR
;
1821 ppp_receive_error(ppp
);
1825 #ifdef CONFIG_PPP_MULTILINK
1827 * Receive a multilink frame.
1828 * We put it on the reconstruction queue and then pull off
1829 * as many completed frames as we can.
1832 ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1836 int mphdrlen
= (ppp
->flags
& SC_MP_SHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1838 if (!pskb_may_pull(skb
, mphdrlen
+ 1) || ppp
->mrru
== 0)
1839 goto err
; /* no good, throw it away */
1841 /* Decode sequence number and begin/end bits */
1842 if (ppp
->flags
& SC_MP_SHORTSEQ
) {
1843 seq
= ((skb
->data
[2] & 0x0f) << 8) | skb
->data
[3];
1846 seq
= (skb
->data
[3] << 16) | (skb
->data
[4] << 8)| skb
->data
[5];
1849 skb
->BEbits
= skb
->data
[2];
1850 skb_pull(skb
, mphdrlen
); /* pull off PPP and MP headers */
1853 * Do protocol ID decompression on the first fragment of each packet.
1855 if ((skb
->BEbits
& B
) && (skb
->data
[0] & 1))
1856 *skb_push(skb
, 1) = 0;
1859 * Expand sequence number to 32 bits, making it as close
1860 * as possible to ppp->minseq.
1862 seq
|= ppp
->minseq
& ~mask
;
1863 if ((int)(ppp
->minseq
- seq
) > (int)(mask
>> 1))
1865 else if ((int)(seq
- ppp
->minseq
) > (int)(mask
>> 1))
1866 seq
-= mask
+ 1; /* should never happen */
1867 skb
->sequence
= seq
;
1871 * If this packet comes before the next one we were expecting,
1874 if (seq_before(seq
, ppp
->nextseq
)) {
1876 ++ppp
->stats
.rx_dropped
;
1877 ppp_receive_error(ppp
);
1882 * Reevaluate minseq, the minimum over all channels of the
1883 * last sequence number received on each channel. Because of
1884 * the increasing sequence number rule, we know that any fragment
1885 * before `minseq' which hasn't arrived is never going to arrive.
1886 * The list of channels can't change because we have the receive
1887 * side of the ppp unit locked.
1889 list_for_each_entry(ch
, &ppp
->channels
, clist
) {
1890 if (seq_before(ch
->lastseq
, seq
))
1893 if (seq_before(ppp
->minseq
, seq
))
1896 /* Put the fragment on the reconstruction queue */
1897 ppp_mp_insert(ppp
, skb
);
1899 /* If the queue is getting long, don't wait any longer for packets
1900 before the start of the queue. */
1901 if (skb_queue_len(&ppp
->mrq
) >= PPP_MP_MAX_QLEN
1902 && seq_before(ppp
->minseq
, ppp
->mrq
.next
->sequence
))
1903 ppp
->minseq
= ppp
->mrq
.next
->sequence
;
1905 /* Pull completed packets off the queue and receive them. */
1906 while ((skb
= ppp_mp_reconstruct(ppp
)) != 0) {
1907 if (pskb_may_pull(skb
, 2))
1908 ppp_receive_nonmp_frame(ppp
, skb
);
1910 ++ppp
->dev
->stats
.rx_length_errors
;
1912 ppp_receive_error(ppp
);
1920 ppp_receive_error(ppp
);
1924 * Insert a fragment on the MP reconstruction queue.
1925 * The queue is ordered by increasing sequence number.
1928 ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
)
1931 struct sk_buff_head
*list
= &ppp
->mrq
;
1932 u32 seq
= skb
->sequence
;
1934 /* N.B. we don't need to lock the list lock because we have the
1935 ppp unit receive-side lock. */
1936 for (p
= list
->next
; p
!= (struct sk_buff
*)list
; p
= p
->next
)
1937 if (seq_before(seq
, p
->sequence
))
1939 __skb_insert(skb
, p
->prev
, p
, list
);
1943 * Reconstruct a packet from the MP fragment queue.
1944 * We go through increasing sequence numbers until we find a
1945 * complete packet, or we get to the sequence number for a fragment
1946 * which hasn't arrived but might still do so.
1949 ppp_mp_reconstruct(struct ppp
*ppp
)
1951 u32 seq
= ppp
->nextseq
;
1952 u32 minseq
= ppp
->minseq
;
1953 struct sk_buff_head
*list
= &ppp
->mrq
;
1954 struct sk_buff
*p
, *next
;
1955 struct sk_buff
*head
, *tail
;
1956 struct sk_buff
*skb
= NULL
;
1957 int lost
= 0, len
= 0;
1959 if (ppp
->mrru
== 0) /* do nothing until mrru is set */
1963 for (p
= head
; p
!= (struct sk_buff
*) list
; p
= next
) {
1965 if (seq_before(p
->sequence
, seq
)) {
1966 /* this can't happen, anyway ignore the skb */
1967 printk(KERN_ERR
"ppp_mp_reconstruct bad seq %u < %u\n",
1972 if (p
->sequence
!= seq
) {
1973 /* Fragment `seq' is missing. If it is after
1974 minseq, it might arrive later, so stop here. */
1975 if (seq_after(seq
, minseq
))
1977 /* Fragment `seq' is lost, keep going. */
1979 seq
= seq_before(minseq
, p
->sequence
)?
1980 minseq
+ 1: p
->sequence
;
1986 * At this point we know that all the fragments from
1987 * ppp->nextseq to seq are either present or lost.
1988 * Also, there are no complete packets in the queue
1989 * that have no missing fragments and end before this
1993 /* B bit set indicates this fragment starts a packet */
1994 if (p
->BEbits
& B
) {
2002 /* Got a complete packet yet? */
2003 if (lost
== 0 && (p
->BEbits
& E
) && (head
->BEbits
& B
)) {
2004 if (len
> ppp
->mrru
+ 2) {
2005 ++ppp
->stats
.rx_length_errors
;
2006 printk(KERN_DEBUG
"PPP: reconstructed packet"
2007 " is too long (%d)\n", len
);
2008 } else if (p
== head
) {
2009 /* fragment is complete packet - reuse skb */
2013 } else if ((skb
= dev_alloc_skb(len
)) == NULL
) {
2014 ++ppp
->stats
.rx_missed_errors
;
2015 printk(KERN_DEBUG
"PPP: no memory for "
2016 "reconstructed packet");
2021 ppp
->nextseq
= seq
+ 1;
2025 * If this is the ending fragment of a packet,
2026 * and we haven't found a complete valid packet yet,
2027 * we can discard up to and including this fragment.
2035 /* If we have a complete packet, copy it all into one skb. */
2037 /* If we have discarded any fragments,
2038 signal a receive error. */
2039 if (head
->sequence
!= ppp
->nextseq
) {
2041 printk(KERN_DEBUG
" missed pkts %u..%u\n",
2042 ppp
->nextseq
, head
->sequence
-1);
2043 ++ppp
->stats
.rx_dropped
;
2044 ppp_receive_error(ppp
);
2048 /* copy to a single skb */
2049 for (p
= head
; p
!= tail
->next
; p
= p
->next
)
2050 skb_copy_bits(p
, 0, skb_put(skb
, p
->len
), p
->len
);
2051 ppp
->nextseq
= tail
->sequence
+ 1;
2055 /* Discard all the skbuffs that we have copied the data out of
2056 or that we can't use. */
2057 while ((p
= list
->next
) != head
) {
2058 __skb_unlink(p
, list
);
2064 #endif /* CONFIG_PPP_MULTILINK */
2067 * Channel interface.
2071 * Create a new, unattached ppp channel.
2074 ppp_register_channel(struct ppp_channel
*chan
)
2076 struct channel
*pch
;
2078 pch
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
2084 init_ppp_file(&pch
->file
, CHANNEL
);
2085 pch
->file
.hdrlen
= chan
->hdrlen
;
2086 #ifdef CONFIG_PPP_MULTILINK
2088 #endif /* CONFIG_PPP_MULTILINK */
2089 init_rwsem(&pch
->chan_sem
);
2090 spin_lock_init(&pch
->downl
);
2091 rwlock_init(&pch
->upl
);
2092 spin_lock_bh(&all_channels_lock
);
2093 pch
->file
.index
= ++last_channel_index
;
2094 list_add(&pch
->list
, &new_channels
);
2095 atomic_inc(&channel_count
);
2096 spin_unlock_bh(&all_channels_lock
);
2101 * Return the index of a channel.
2103 int ppp_channel_index(struct ppp_channel
*chan
)
2105 struct channel
*pch
= chan
->ppp
;
2108 return pch
->file
.index
;
2113 * Return the PPP unit number to which a channel is connected.
2115 int ppp_unit_number(struct ppp_channel
*chan
)
2117 struct channel
*pch
= chan
->ppp
;
2121 read_lock_bh(&pch
->upl
);
2123 unit
= pch
->ppp
->file
.index
;
2124 read_unlock_bh(&pch
->upl
);
2130 * Disconnect a channel from the generic layer.
2131 * This must be called in process context.
2134 ppp_unregister_channel(struct ppp_channel
*chan
)
2136 struct channel
*pch
= chan
->ppp
;
2139 return; /* should never happen */
2143 * This ensures that we have returned from any calls into the
2144 * the channel's start_xmit or ioctl routine before we proceed.
2146 down_write(&pch
->chan_sem
);
2147 spin_lock_bh(&pch
->downl
);
2149 spin_unlock_bh(&pch
->downl
);
2150 up_write(&pch
->chan_sem
);
2151 ppp_disconnect_channel(pch
);
2152 spin_lock_bh(&all_channels_lock
);
2153 list_del(&pch
->list
);
2154 spin_unlock_bh(&all_channels_lock
);
2156 wake_up_interruptible(&pch
->file
.rwait
);
2157 if (atomic_dec_and_test(&pch
->file
.refcnt
))
2158 ppp_destroy_channel(pch
);
2162 * Callback from a channel when it can accept more to transmit.
2163 * This should be called at BH/softirq level, not interrupt level.
2166 ppp_output_wakeup(struct ppp_channel
*chan
)
2168 struct channel
*pch
= chan
->ppp
;
2172 ppp_channel_push(pch
);
2176 * Compression control.
2179 /* Process the PPPIOCSCOMPRESS ioctl. */
2181 ppp_set_compress(struct ppp
*ppp
, unsigned long arg
)
2184 struct compressor
*cp
, *ocomp
;
2185 struct ppp_option_data data
;
2186 void *state
, *ostate
;
2187 unsigned char ccp_option
[CCP_MAX_OPTION_LENGTH
];
2190 if (copy_from_user(&data
, (void __user
*) arg
, sizeof(data
))
2191 || (data
.length
<= CCP_MAX_OPTION_LENGTH
2192 && copy_from_user(ccp_option
, (void __user
*) data
.ptr
, data
.length
)))
2195 if (data
.length
> CCP_MAX_OPTION_LENGTH
2196 || ccp_option
[1] < 2 || ccp_option
[1] > data
.length
)
2199 cp
= find_compressor(ccp_option
[0]);
2202 request_module("ppp-compress-%d", ccp_option
[0]);
2203 cp
= find_compressor(ccp_option
[0]);
2205 #endif /* CONFIG_KMOD */
2210 if (data
.transmit
) {
2211 state
= cp
->comp_alloc(ccp_option
, data
.length
);
2214 ppp
->xstate
&= ~SC_COMP_RUN
;
2216 ostate
= ppp
->xc_state
;
2218 ppp
->xc_state
= state
;
2219 ppp_xmit_unlock(ppp
);
2221 ocomp
->comp_free(ostate
);
2222 module_put(ocomp
->owner
);
2226 module_put(cp
->owner
);
2229 state
= cp
->decomp_alloc(ccp_option
, data
.length
);
2232 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2234 ostate
= ppp
->rc_state
;
2236 ppp
->rc_state
= state
;
2237 ppp_recv_unlock(ppp
);
2239 ocomp
->decomp_free(ostate
);
2240 module_put(ocomp
->owner
);
2244 module_put(cp
->owner
);
2252 * Look at a CCP packet and update our state accordingly.
2253 * We assume the caller has the xmit or recv path locked.
2256 ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
)
2261 if (!pskb_may_pull(skb
, CCP_HDRLEN
+ 2))
2262 return; /* no header */
2265 switch (CCP_CODE(dp
)) {
2268 /* A ConfReq starts negotiation of compression
2269 * in one direction of transmission,
2270 * and hence brings it down...but which way?
2273 * A ConfReq indicates what the sender would like to receive
2276 /* He is proposing what I should send */
2277 ppp
->xstate
&= ~SC_COMP_RUN
;
2279 /* I am proposing to what he should send */
2280 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2287 * CCP is going down, both directions of transmission
2289 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2290 ppp
->xstate
&= ~SC_COMP_RUN
;
2294 if ((ppp
->flags
& (SC_CCP_OPEN
| SC_CCP_UP
)) != SC_CCP_OPEN
)
2296 len
= CCP_LENGTH(dp
);
2297 if (!pskb_may_pull(skb
, len
+ 2))
2298 return; /* too short */
2301 if (len
< CCP_OPT_MINLEN
|| len
< CCP_OPT_LENGTH(dp
))
2304 /* we will start receiving compressed packets */
2305 if (ppp
->rc_state
== 0)
2307 if (ppp
->rcomp
->decomp_init(ppp
->rc_state
, dp
, len
,
2308 ppp
->file
.index
, 0, ppp
->mru
, ppp
->debug
)) {
2309 ppp
->rstate
|= SC_DECOMP_RUN
;
2310 ppp
->rstate
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
2313 /* we will soon start sending compressed packets */
2314 if (ppp
->xc_state
== 0)
2316 if (ppp
->xcomp
->comp_init(ppp
->xc_state
, dp
, len
,
2317 ppp
->file
.index
, 0, ppp
->debug
))
2318 ppp
->xstate
|= SC_COMP_RUN
;
2323 /* reset the [de]compressor */
2324 if ((ppp
->flags
& SC_CCP_UP
) == 0)
2327 if (ppp
->rc_state
&& (ppp
->rstate
& SC_DECOMP_RUN
)) {
2328 ppp
->rcomp
->decomp_reset(ppp
->rc_state
);
2329 ppp
->rstate
&= ~SC_DC_ERROR
;
2332 if (ppp
->xc_state
&& (ppp
->xstate
& SC_COMP_RUN
))
2333 ppp
->xcomp
->comp_reset(ppp
->xc_state
);
2339 /* Free up compression resources. */
2341 ppp_ccp_closed(struct ppp
*ppp
)
2343 void *xstate
, *rstate
;
2344 struct compressor
*xcomp
, *rcomp
;
2347 ppp
->flags
&= ~(SC_CCP_OPEN
| SC_CCP_UP
);
2350 xstate
= ppp
->xc_state
;
2351 ppp
->xc_state
= NULL
;
2354 rstate
= ppp
->rc_state
;
2355 ppp
->rc_state
= NULL
;
2359 xcomp
->comp_free(xstate
);
2360 module_put(xcomp
->owner
);
2363 rcomp
->decomp_free(rstate
);
2364 module_put(rcomp
->owner
);
2368 /* List of compressors. */
2369 static LIST_HEAD(compressor_list
);
2370 static DEFINE_SPINLOCK(compressor_list_lock
);
2372 struct compressor_entry
{
2373 struct list_head list
;
2374 struct compressor
*comp
;
2377 static struct compressor_entry
*
2378 find_comp_entry(int proto
)
2380 struct compressor_entry
*ce
;
2382 list_for_each_entry(ce
, &compressor_list
, list
) {
2383 if (ce
->comp
->compress_proto
== proto
)
2389 /* Register a compressor */
2391 ppp_register_compressor(struct compressor
*cp
)
2393 struct compressor_entry
*ce
;
2395 spin_lock(&compressor_list_lock
);
2397 if (find_comp_entry(cp
->compress_proto
) != 0)
2400 ce
= kmalloc(sizeof(struct compressor_entry
), GFP_ATOMIC
);
2405 list_add(&ce
->list
, &compressor_list
);
2407 spin_unlock(&compressor_list_lock
);
2411 /* Unregister a compressor */
2413 ppp_unregister_compressor(struct compressor
*cp
)
2415 struct compressor_entry
*ce
;
2417 spin_lock(&compressor_list_lock
);
2418 ce
= find_comp_entry(cp
->compress_proto
);
2419 if (ce
!= 0 && ce
->comp
== cp
) {
2420 list_del(&ce
->list
);
2423 spin_unlock(&compressor_list_lock
);
2426 /* Find a compressor. */
2427 static struct compressor
*
2428 find_compressor(int type
)
2430 struct compressor_entry
*ce
;
2431 struct compressor
*cp
= NULL
;
2433 spin_lock(&compressor_list_lock
);
2434 ce
= find_comp_entry(type
);
2437 if (!try_module_get(cp
->owner
))
2440 spin_unlock(&compressor_list_lock
);
2445 * Miscelleneous stuff.
2449 ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
)
2451 struct slcompress
*vj
= ppp
->vj
;
2453 memset(st
, 0, sizeof(*st
));
2454 st
->p
.ppp_ipackets
= ppp
->stats
.rx_packets
;
2455 st
->p
.ppp_ierrors
= ppp
->stats
.rx_errors
;
2456 st
->p
.ppp_ibytes
= ppp
->stats
.rx_bytes
;
2457 st
->p
.ppp_opackets
= ppp
->stats
.tx_packets
;
2458 st
->p
.ppp_oerrors
= ppp
->stats
.tx_errors
;
2459 st
->p
.ppp_obytes
= ppp
->stats
.tx_bytes
;
2462 st
->vj
.vjs_packets
= vj
->sls_o_compressed
+ vj
->sls_o_uncompressed
;
2463 st
->vj
.vjs_compressed
= vj
->sls_o_compressed
;
2464 st
->vj
.vjs_searches
= vj
->sls_o_searches
;
2465 st
->vj
.vjs_misses
= vj
->sls_o_misses
;
2466 st
->vj
.vjs_errorin
= vj
->sls_i_error
;
2467 st
->vj
.vjs_tossed
= vj
->sls_i_tossed
;
2468 st
->vj
.vjs_uncompressedin
= vj
->sls_i_uncompressed
;
2469 st
->vj
.vjs_compressedin
= vj
->sls_i_compressed
;
2473 * Stuff for handling the lists of ppp units and channels
2474 * and for initialization.
2478 * Create a new ppp interface unit. Fails if it can't allocate memory
2479 * or if there is already a unit with the requested number.
2480 * unit == -1 means allocate a new number.
2483 ppp_create_interface(int unit
, int *retp
)
2486 struct net_device
*dev
= NULL
;
2490 ppp
= kzalloc(sizeof(struct ppp
), GFP_KERNEL
);
2493 dev
= alloc_netdev(0, "", ppp_setup
);
2498 ppp
->mru_alloc
= PPP_MRU
;
2499 init_ppp_file(&ppp
->file
, INTERFACE
);
2500 ppp
->file
.hdrlen
= PPP_HDRLEN
- 2; /* don't count proto bytes */
2501 for (i
= 0; i
< NUM_NP
; ++i
)
2502 ppp
->npmode
[i
] = NPMODE_PASS
;
2503 INIT_LIST_HEAD(&ppp
->channels
);
2504 spin_lock_init(&ppp
->rlock
);
2505 spin_lock_init(&ppp
->wlock
);
2506 #ifdef CONFIG_PPP_MULTILINK
2508 skb_queue_head_init(&ppp
->mrq
);
2509 #endif /* CONFIG_PPP_MULTILINK */
2513 dev
->hard_start_xmit
= ppp_start_xmit
;
2514 dev
->get_stats
= ppp_net_stats
;
2515 dev
->do_ioctl
= ppp_net_ioctl
;
2518 mutex_lock(&all_ppp_mutex
);
2520 unit
= cardmap_find_first_free(all_ppp_units
);
2521 else if (cardmap_get(all_ppp_units
, unit
) != NULL
)
2522 goto out2
; /* unit already exists */
2524 /* Initialize the new ppp unit */
2525 ppp
->file
.index
= unit
;
2526 sprintf(dev
->name
, "ppp%d", unit
);
2528 ret
= register_netdev(dev
);
2530 printk(KERN_ERR
"PPP: couldn't register device %s (%d)\n",
2536 if ((ctf_dev_register(kcih
, dev
, FALSE
) != BCME_OK
) ||
2537 (ctf_enable(kcih
, dev
, TRUE
, NULL
) != BCME_OK
))
2538 ctf_dev_unregister(kcih
, dev
);
2541 atomic_inc(&ppp_unit_count
);
2542 ret
= cardmap_set(&all_ppp_units
, unit
, ppp
);
2546 mutex_unlock(&all_ppp_mutex
);
2551 atomic_dec(&ppp_unit_count
);
2553 mutex_unlock(&all_ppp_mutex
);
2563 * Initialize a ppp_file structure.
2566 init_ppp_file(struct ppp_file
*pf
, int kind
)
2569 skb_queue_head_init(&pf
->xq
);
2570 skb_queue_head_init(&pf
->rq
);
2571 atomic_set(&pf
->refcnt
, 1);
2572 init_waitqueue_head(&pf
->rwait
);
2576 * Take down a ppp interface unit - called when the owning file
2577 * (the one that created the unit) is closed or detached.
2579 static void ppp_shutdown_interface(struct ppp
*ppp
)
2581 struct net_device
*dev
;
2583 mutex_lock(&all_ppp_mutex
);
2588 /* This will call dev_close() for us. */
2591 ctf_dev_unregister(kcih
, dev
);
2593 unregister_netdev(dev
);
2596 cardmap_set(&all_ppp_units
, ppp
->file
.index
, NULL
);
2599 wake_up_interruptible(&ppp
->file
.rwait
);
2600 mutex_unlock(&all_ppp_mutex
);
2604 * Free the memory used by a ppp unit. This is only called once
2605 * there are no channels connected to the unit and no file structs
2606 * that reference the unit.
2608 static void ppp_destroy_interface(struct ppp
*ppp
)
2610 atomic_dec(&ppp_unit_count
);
2612 if (!ppp
->file
.dead
|| ppp
->n_channels
) {
2613 /* "can't happen" */
2614 printk(KERN_ERR
"ppp: destroying ppp struct %p but dead=%d "
2615 "n_channels=%d !\n", ppp
, ppp
->file
.dead
,
2620 ppp_ccp_closed(ppp
);
2625 skb_queue_purge(&ppp
->file
.xq
);
2626 skb_queue_purge(&ppp
->file
.rq
);
2627 #ifdef CONFIG_PPP_MULTILINK
2628 skb_queue_purge(&ppp
->mrq
);
2629 #endif /* CONFIG_PPP_MULTILINK */
2630 #ifdef CONFIG_PPP_FILTER
2631 kfree(ppp
->pass_filter
);
2632 ppp
->pass_filter
= NULL
;
2633 kfree(ppp
->active_filter
);
2634 ppp
->active_filter
= NULL
;
2635 #endif /* CONFIG_PPP_FILTER */
2637 if (ppp
->xmit_pending
)
2638 kfree_skb(ppp
->xmit_pending
);
2644 * Locate an existing ppp unit.
2645 * The caller should have locked the all_ppp_mutex.
2648 ppp_find_unit(int unit
)
2650 return cardmap_get(all_ppp_units
, unit
);
2654 * Locate an existing ppp channel.
2655 * The caller should have locked the all_channels_lock.
2656 * First we look in the new_channels list, then in the
2657 * all_channels list. If found in the new_channels list,
2658 * we move it to the all_channels list. This is for speed
2659 * when we have a lot of channels in use.
2661 static struct channel
*
2662 ppp_find_channel(int unit
)
2664 struct channel
*pch
;
2666 list_for_each_entry(pch
, &new_channels
, list
) {
2667 if (pch
->file
.index
== unit
) {
2668 list_move(&pch
->list
, &all_channels
);
2672 list_for_each_entry(pch
, &all_channels
, list
) {
2673 if (pch
->file
.index
== unit
)
2680 * Connect a PPP channel to a PPP interface unit.
2683 ppp_connect_channel(struct channel
*pch
, int unit
)
2689 mutex_lock(&all_ppp_mutex
);
2690 ppp
= ppp_find_unit(unit
);
2693 write_lock_bh(&pch
->upl
);
2699 if (pch
->file
.hdrlen
> ppp
->file
.hdrlen
)
2700 ppp
->file
.hdrlen
= pch
->file
.hdrlen
;
2701 hdrlen
= pch
->file
.hdrlen
+ 2; /* for protocol bytes */
2702 if (ppp
->dev
&& hdrlen
> ppp
->dev
->hard_header_len
)
2703 ppp
->dev
->hard_header_len
= hdrlen
;
2704 list_add_tail(&pch
->clist
, &ppp
->channels
);
2710 atomic_inc(&ppp
->file
.refcnt
);
2715 write_unlock_bh(&pch
->upl
);
2717 mutex_unlock(&all_ppp_mutex
);
2722 * Disconnect a channel from its ppp unit.
2725 ppp_disconnect_channel(struct channel
*pch
)
2730 write_lock_bh(&pch
->upl
);
2733 write_unlock_bh(&pch
->upl
);
2735 /* remove it from the ppp unit's list */
2737 list_del(&pch
->clist
);
2738 if (--ppp
->n_channels
== 0)
2739 wake_up_interruptible(&ppp
->file
.rwait
);
2741 if (atomic_dec_and_test(&ppp
->file
.refcnt
))
2742 ppp_destroy_interface(ppp
);
2749 * Free up the resources used by a ppp channel.
2751 static void ppp_destroy_channel(struct channel
*pch
)
2753 atomic_dec(&channel_count
);
2755 if (!pch
->file
.dead
) {
2756 /* "can't happen" */
2757 printk(KERN_ERR
"ppp: destroying undead channel %p !\n",
2761 skb_queue_purge(&pch
->file
.xq
);
2762 skb_queue_purge(&pch
->file
.rq
);
2766 static void __exit
ppp_cleanup(void)
2768 /* should never happen */
2769 if (atomic_read(&ppp_unit_count
) || atomic_read(&channel_count
))
2770 printk(KERN_ERR
"PPP: removing module but units remain!\n");
2771 cardmap_destroy(&all_ppp_units
);
2772 if (unregister_chrdev(PPP_MAJOR
, "ppp") != 0)
2773 printk(KERN_ERR
"PPP: failed to unregister PPP device\n");
2774 device_destroy(ppp_class
, MKDEV(PPP_MAJOR
, 0));
2775 class_destroy(ppp_class
);
2779 * Cardmap implementation.
2781 static void *cardmap_get(struct cardmap
*map
, unsigned int nr
)
2786 for (p
= map
; p
!= NULL
; ) {
2787 if ((i
= nr
>> p
->shift
) >= CARDMAP_WIDTH
)
2791 nr
&= ~(CARDMAP_MASK
<< p
->shift
);
2797 static int cardmap_set(struct cardmap
**pmap
, unsigned int nr
, void *ptr
)
2803 if (p
== NULL
|| (nr
>> p
->shift
) >= CARDMAP_WIDTH
) {
2805 /* need a new top level */
2806 struct cardmap
*np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
2811 np
->shift
= p
->shift
+ CARDMAP_ORDER
;
2816 } while ((nr
>> p
->shift
) >= CARDMAP_WIDTH
);
2819 while (p
->shift
> 0) {
2820 i
= (nr
>> p
->shift
) & CARDMAP_MASK
;
2821 if (p
->ptr
[i
] == NULL
) {
2822 struct cardmap
*np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
2825 np
->shift
= p
->shift
- CARDMAP_ORDER
;
2830 clear_bit(i
, &p
->inuse
);
2833 i
= nr
& CARDMAP_MASK
;
2836 set_bit(i
, &p
->inuse
);
2838 clear_bit(i
, &p
->inuse
);
2844 static unsigned int cardmap_find_first_free(struct cardmap
*map
)
2847 unsigned int nr
= 0;
2850 if ((p
= map
) == NULL
)
2853 i
= find_first_zero_bit(&p
->inuse
, CARDMAP_WIDTH
);
2854 if (i
>= CARDMAP_WIDTH
) {
2855 if (p
->parent
== NULL
)
2856 return CARDMAP_WIDTH
<< p
->shift
;
2858 i
= (nr
>> p
->shift
) & CARDMAP_MASK
;
2859 set_bit(i
, &p
->inuse
);
2862 nr
= (nr
& (~CARDMAP_MASK
<< p
->shift
)) | (i
<< p
->shift
);
2863 if (p
->shift
== 0 || p
->ptr
[i
] == NULL
)
2869 static void cardmap_destroy(struct cardmap
**pmap
)
2871 struct cardmap
*p
, *np
;
2874 for (p
= *pmap
; p
!= NULL
; p
= np
) {
2875 if (p
->shift
!= 0) {
2876 for (i
= 0; i
< CARDMAP_WIDTH
; ++i
)
2877 if (p
->ptr
[i
] != NULL
)
2879 if (i
< CARDMAP_WIDTH
) {
2892 #if defined(CTF_PPPOE) || defined(CTF_PPTP) || defined(CTF_L2TP)
2894 ppp_rxstats_upd(void *pppif
, struct sk_buff
*skb
)
2896 if(pppif
== NULL
|| skb
== NULL
)
2898 struct ppp
*ppp
= ((struct net_device
*)pppif
)->priv
;
2901 ++ppp
->stats
.rx_packets
;
2902 ppp
->stats
.rx_bytes
+= skb
->len
;
2903 ppp
->last_recv
= jiffies
;
2907 ppp_txstats_upd(void *pppif
, struct sk_buff
*skb
)
2909 if(pppif
== NULL
|| skb
== NULL
)
2911 struct ppp
*ppp
= ((struct net_device
*)pppif
)->priv
;
2914 ++ppp
->stats
.tx_packets
;
2915 ppp
->stats
.tx_bytes
+= skb
->len
;
2916 ppp
->last_xmit
= jiffies
;
2921 ppp_get_conn_pkt_info(int unit
, struct ctf_ppp
*ctfppp
){
2922 struct pppox_sock
*po
= NULL
;
2923 struct asyncppp
*ap
= NULL
;
2924 struct sock
*sk
= NULL
;
2925 struct ppp
*ppp
= NULL
;
2926 struct channel
*pch
= NULL
;
2927 const char *vars
= NULL
;
2929 ppp
= ppp_find_unit(unit
);
2930 if(ppp
) pch
= ppp
->ctfpch
;
2933 return (BCME_ERROR
);
2936 po
= pppox_sk((struct sock
*)pch
->chan
->private);
2937 ap
= (struct asyncppp
*)pch
->chan
->private;
2940 return (BCME_ERROR
);
2943 return (BCME_ERROR
);
2945 ctfppp
->psk
.po
= po
;
2947 sk
= po
->chan
.private;
2948 if(sk
/*&& sizeof(sk) > sizeof(struct sock_common)*/){
2949 ctfppp
->psk
.pppox_protocol
= sk
->sk_protocol
;
2950 switch (sk
->sk_protocol
){
2952 ctfppp
->pppox_id
= po
->pppoe_pa
.sid
;
2953 memcpy(ctfppp
->psk
.dhost
.octet
, po
->pppoe_pa
.remote
, ETH_ALEN
);
2956 printk("%s: Adding ppp connection: session ID =%04x, Address=%02x:%02x:%02x:%02x:%02x:%02x\n",
2957 __FUNCTION__
,ntohs(ctfppp
->pppox_id
),
2958 ctfppp
->psk
.dhost
.octet
[0], ctfppp
->psk
.dhost
.octet
[1],
2959 ctfppp
->psk
.dhost
.octet
[2], ctfppp
->psk
.dhost
.octet
[3],
2960 ctfppp
->psk
.dhost
.octet
[4], ctfppp
->psk
.dhost
.octet
[5]);
2965 ctfppp
->pppox_id
= po
->proto
.pptp
.dst_addr
.call_id
;
2968 printk("%s: Adding pptp entry: src call ID =%04x, src ip=%u.%u.%u.%u, dst call ID=%4x, dst ip=%u.%u.%u.%u\n ",
2969 __FUNCTION__
,po
->proto
.pptp
.dst_addr
.call_id
, NIPQUAD(po
->proto
.pptp
.src_addr
.sin_addr
.s_addr
),
2970 po
->proto
.pptp
.dst_addr
.call_id
,NIPQUAD(po
->proto
.pptp
.dst_addr
.sin_addr
.s_addr
));
2971 //printk("%s\n ",sk->sk_send_head->dev->name);
2976 return (BCME_ERROR
);
2980 return (BCME_ERROR
);
2986 EXPORT_SYMBOL(ppp_rxstats_upd
);
2987 EXPORT_SYMBOL(ppp_txstats_upd
);
2988 EXPORT_SYMBOL(ppp_get_conn_pkt_info
);
2990 #endif /* CTF_PPPOE | CTF_PPTP | CTF_L2TP */
2993 /* Module/initialization stuff */
2995 module_init(ppp_init
);
2996 module_exit(ppp_cleanup
);
2998 EXPORT_SYMBOL(ppp_register_channel
);
2999 EXPORT_SYMBOL(ppp_unregister_channel
);
3000 EXPORT_SYMBOL(ppp_channel_index
);
3001 EXPORT_SYMBOL(ppp_unit_number
);
3002 EXPORT_SYMBOL(ppp_input
);
3003 EXPORT_SYMBOL(ppp_input_error
);
3004 EXPORT_SYMBOL(ppp_output_wakeup
);
3005 EXPORT_SYMBOL(ppp_register_compressor
);
3006 EXPORT_SYMBOL(ppp_unregister_compressor
);
3007 MODULE_LICENSE("GPL");
3008 MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR
);
3009 MODULE_ALIAS("/dev/ppp");