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 20041108==
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/idr.h>
31 #include <linux/netdevice.h>
32 #include <linux/poll.h>
33 #include <linux/ppp_defs.h>
34 #include <linux/filter.h>
35 #include <linux/if_ppp.h>
36 #include <linux/ppp_channel.h>
37 #include <linux/ppp-comp.h>
38 #include <linux/skbuff.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/if_arp.h>
42 #include <linux/tcp.h>
43 #include <linux/smp_lock.h>
44 #include <linux/spinlock.h>
45 #include <linux/rwsem.h>
46 #include <linux/stddef.h>
47 #include <linux/device.h>
48 #include <linux/mutex.h>
49 #include <linux/slab.h>
50 #include <net/slhc_vj.h>
51 #include <asm/atomic.h>
53 #include <linux/nsproxy.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
57 #define PPP_VERSION "2.4.2"
60 * Network protocols we support.
62 #define NP_IP 0 /* Internet Protocol V4 */
63 #define NP_IPV6 1 /* Internet Protocol V6 */
64 #define NP_IPX 2 /* IPX protocol */
65 #define NP_AT 3 /* Appletalk protocol */
66 #define NP_MPLS_UC 4 /* MPLS unicast */
67 #define NP_MPLS_MC 5 /* MPLS multicast */
68 #define NUM_NP 6 /* Number of NPs. */
70 #define MPHDRLEN 6 /* multilink protocol header length */
71 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
72 #define MIN_FRAG_SIZE 64
75 * An instance of /dev/ppp can be associated with either a ppp
76 * interface unit or a ppp channel. In both cases, file->private_data
77 * points to one of these.
83 struct sk_buff_head xq
; /* pppd transmit queue */
84 struct sk_buff_head rq
; /* receive queue for pppd */
85 wait_queue_head_t rwait
; /* for poll on reading /dev/ppp */
86 atomic_t refcnt
; /* # refs (incl /dev/ppp attached) */
87 int hdrlen
; /* space to leave for headers */
88 int index
; /* interface unit / channel number */
89 int dead
; /* unit/channel has been shut down */
92 #define PF_TO_X(pf, X) container_of(pf, X, file)
94 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
95 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
98 * Data structure describing one ppp unit.
99 * A ppp unit corresponds to a ppp network interface device
100 * and represents a multilink bundle.
101 * It can have 0 or more ppp channels connected to it.
104 struct ppp_file file
; /* stuff for read/write/poll 0 */
105 struct file
*owner
; /* file that owns this unit 48 */
106 struct list_head channels
; /* list of attached channels 4c */
107 int n_channels
; /* how many channels are attached 54 */
108 spinlock_t rlock
; /* lock for receive side 58 */
109 spinlock_t wlock
; /* lock for transmit side 5c */
110 int mru
; /* max receive unit 60 */
111 unsigned int flags
; /* control bits 64 */
112 unsigned int xstate
; /* transmit state bits 68 */
113 unsigned int rstate
; /* receive state bits 6c */
114 int debug
; /* debug flags 70 */
115 struct slcompress
*vj
; /* state for VJ header compression */
116 enum NPmode npmode
[NUM_NP
]; /* what to do with each net proto 78 */
117 struct sk_buff
*xmit_pending
; /* a packet ready to go out 88 */
118 struct compressor
*xcomp
; /* transmit packet compressor 8c */
119 void *xc_state
; /* its internal state 90 */
120 struct compressor
*rcomp
; /* receive decompressor 94 */
121 void *rc_state
; /* its internal state 98 */
122 unsigned long last_xmit
; /* jiffies when last pkt sent 9c */
123 unsigned long last_recv
; /* jiffies when last pkt rcvd a0 */
124 struct net_device
*dev
; /* network interface device a4 */
125 int closing
; /* is device closing down? a8 */
126 #ifdef CONFIG_PPP_MULTILINK
127 int nxchan
; /* next channel to send something on */
128 u32 nxseq
; /* next sequence number to send */
129 int mrru
; /* MP: max reconst. receive unit */
130 u32 nextseq
; /* MP: seq no of next packet */
131 u32 minseq
; /* MP: min of most recent seqnos */
132 struct sk_buff_head mrq
; /* MP: receive reconstruction queue */
133 #endif /* CONFIG_PPP_MULTILINK */
134 #ifdef CONFIG_PPP_FILTER
135 struct sock_filter
*pass_filter
; /* filter for packets to pass */
136 struct sock_filter
*active_filter
;/* filter for pkts to reset idle */
137 unsigned pass_len
, active_len
;
138 #endif /* CONFIG_PPP_FILTER */
139 struct net
*ppp_net
; /* the net we belong to */
143 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
144 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
146 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
147 * Bits in xstate: SC_COMP_RUN
149 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
150 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
151 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
154 * Private data structure for each channel.
155 * This includes the data structure used for multilink.
158 struct ppp_file file
; /* stuff for read/write/poll */
159 struct list_head list
; /* link in all/new_channels list */
160 struct ppp_channel
*chan
; /* public channel data structure */
161 struct rw_semaphore chan_sem
; /* protects `chan' during chan ioctl */
162 spinlock_t downl
; /* protects `chan', file.xq dequeue */
163 struct ppp
*ppp
; /* ppp unit we're connected to */
164 struct net
*chan_net
; /* the net channel belongs to */
165 struct list_head clist
; /* link in list of channels per unit */
166 rwlock_t upl
; /* protects `ppp' */
167 #ifdef CONFIG_PPP_MULTILINK
168 u8 avail
; /* flag used in multilink stuff */
169 u8 had_frag
; /* >= 1 fragments have been sent */
170 u32 lastseq
; /* MP: last sequence # received */
171 int speed
; /* speed of the corresponding ppp channel*/
172 #endif /* CONFIG_PPP_MULTILINK */
176 * SMP locking issues:
177 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
178 * list and the ppp.n_channels field, you need to take both locks
179 * before you modify them.
180 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
184 static atomic_t ppp_unit_count
= ATOMIC_INIT(0);
185 static atomic_t channel_count
= ATOMIC_INIT(0);
187 /* per-net private data for this module */
188 static int ppp_net_id __read_mostly
;
190 /* units to ppp mapping */
191 struct idr units_idr
;
194 * all_ppp_mutex protects the units_idr mapping.
195 * It also ensures that finding a ppp unit in the units_idr
196 * map and updating its file.refcnt field is atomic.
198 struct mutex all_ppp_mutex
;
201 struct list_head all_channels
;
202 struct list_head new_channels
;
203 int last_channel_index
;
206 * all_channels_lock protects all_channels and
207 * last_channel_index, and the atomicity of find
208 * a channel and updating its file.refcnt field.
210 spinlock_t all_channels_lock
;
213 /* Get the PPP protocol number from a skb */
214 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
216 /* We limit the length of ppp->file.rq to this (arbitrary) value */
217 #define PPP_MAX_RQLEN 32
220 * Maximum number of multilink fragments queued up.
221 * This has to be large enough to cope with the maximum latency of
222 * the slowest channel relative to the others. Strictly it should
223 * depend on the number of channels and their characteristics.
225 #define PPP_MP_MAX_QLEN 128
227 /* Multilink header bits. */
228 #define B 0x80 /* this fragment begins a packet */
229 #define E 0x40 /* this fragment ends a packet */
231 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
232 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
233 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
236 static int ppp_unattached_ioctl(struct net
*net
, struct ppp_file
*pf
,
237 struct file
*file
, unsigned int cmd
, unsigned long arg
);
238 static void ppp_xmit_process(struct ppp
*ppp
);
239 static void ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
240 static void ppp_push(struct ppp
*ppp
);
241 static void ppp_channel_push(struct channel
*pch
);
242 static void ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
,
243 struct channel
*pch
);
244 static void ppp_receive_error(struct ppp
*ppp
);
245 static void ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
);
246 static struct sk_buff
*ppp_decompress_frame(struct ppp
*ppp
,
247 struct sk_buff
*skb
);
248 #ifdef CONFIG_PPP_MULTILINK
249 static void ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
,
250 struct channel
*pch
);
251 static void ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
);
252 static struct sk_buff
*ppp_mp_reconstruct(struct ppp
*ppp
);
253 static int ppp_mp_explode(struct ppp
*ppp
, struct sk_buff
*skb
);
254 #endif /* CONFIG_PPP_MULTILINK */
255 static int ppp_set_compress(struct ppp
*ppp
, unsigned long arg
);
256 static void ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
);
257 static void ppp_ccp_closed(struct ppp
*ppp
);
258 static struct compressor
*find_compressor(int type
);
259 static void ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
);
260 static struct ppp
*ppp_create_interface(struct net
*net
, int unit
, int *retp
);
261 static void init_ppp_file(struct ppp_file
*pf
, int kind
);
262 static void ppp_shutdown_interface(struct ppp
*ppp
);
263 static void ppp_destroy_interface(struct ppp
*ppp
);
264 static struct ppp
*ppp_find_unit(struct ppp_net
*pn
, int unit
);
265 static struct channel
*ppp_find_channel(struct ppp_net
*pn
, int unit
);
266 static int ppp_connect_channel(struct channel
*pch
, int unit
);
267 static int ppp_disconnect_channel(struct channel
*pch
);
268 static void ppp_destroy_channel(struct channel
*pch
);
269 static int unit_get(struct idr
*p
, void *ptr
);
270 static int unit_set(struct idr
*p
, void *ptr
, int n
);
271 static void unit_put(struct idr
*p
, int n
);
272 static void *unit_find(struct idr
*p
, int n
);
274 static struct class *ppp_class
;
276 /* per net-namespace data */
277 static inline struct ppp_net
*ppp_pernet(struct net
*net
)
281 return net_generic(net
, ppp_net_id
);
284 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
285 static inline int proto_to_npindex(int proto
)
304 /* Translates an NP index into a PPP protocol number */
305 static const int npindex_to_proto
[NUM_NP
] = {
314 /* Translates an ethertype into an NP index */
315 static inline int ethertype_to_npindex(int ethertype
)
335 /* Translates an NP index into an ethertype */
336 static const int npindex_to_ethertype
[NUM_NP
] = {
348 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
349 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
350 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
351 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
352 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
353 ppp_recv_lock(ppp); } while (0)
354 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
355 ppp_xmit_unlock(ppp); } while (0)
358 * /dev/ppp device routines.
359 * The /dev/ppp device is used by pppd to control the ppp unit.
360 * It supports the read, write, ioctl and poll functions.
361 * Open instances of /dev/ppp can be in one of three states:
362 * unattached, attached to a ppp unit, or attached to a ppp channel.
364 static int ppp_open(struct inode
*inode
, struct file
*file
)
368 * This could (should?) be enforced by the permissions on /dev/ppp.
370 if (!capable(CAP_NET_ADMIN
))
375 static int ppp_release(struct inode
*unused
, struct file
*file
)
377 struct ppp_file
*pf
= file
->private_data
;
381 file
->private_data
= NULL
;
382 if (pf
->kind
== INTERFACE
) {
384 if (file
== ppp
->owner
)
385 ppp_shutdown_interface(ppp
);
387 if (atomic_dec_and_test(&pf
->refcnt
)) {
390 ppp_destroy_interface(PF_TO_PPP(pf
));
393 ppp_destroy_channel(PF_TO_CHANNEL(pf
));
401 static ssize_t
ppp_read(struct file
*file
, char __user
*buf
,
402 size_t count
, loff_t
*ppos
)
404 struct ppp_file
*pf
= file
->private_data
;
405 DECLARE_WAITQUEUE(wait
, current
);
407 struct sk_buff
*skb
= NULL
;
413 add_wait_queue(&pf
->rwait
, &wait
);
415 set_current_state(TASK_INTERRUPTIBLE
);
416 skb
= skb_dequeue(&pf
->rq
);
422 if (pf
->kind
== INTERFACE
) {
424 * Return 0 (EOF) on an interface that has no
425 * channels connected, unless it is looping
426 * network traffic (demand mode).
428 struct ppp
*ppp
= PF_TO_PPP(pf
);
429 if (ppp
->n_channels
== 0 &&
430 (ppp
->flags
& SC_LOOP_TRAFFIC
) == 0)
434 if (file
->f_flags
& O_NONBLOCK
)
437 if (signal_pending(current
))
441 set_current_state(TASK_RUNNING
);
442 remove_wait_queue(&pf
->rwait
, &wait
);
448 if (skb
->len
> count
)
451 if (copy_to_user(buf
, skb
->data
, skb
->len
))
461 static ssize_t
ppp_write(struct file
*file
, const char __user
*buf
,
462 size_t count
, loff_t
*ppos
)
464 struct ppp_file
*pf
= file
->private_data
;
471 skb
= alloc_skb(count
+ pf
->hdrlen
, GFP_KERNEL
);
474 skb_reserve(skb
, pf
->hdrlen
);
476 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
481 skb_queue_tail(&pf
->xq
, skb
);
485 ppp_xmit_process(PF_TO_PPP(pf
));
488 ppp_channel_push(PF_TO_CHANNEL(pf
));
498 /* No kernel lock - fine */
499 static unsigned int ppp_poll(struct file
*file
, poll_table
*wait
)
501 struct ppp_file
*pf
= file
->private_data
;
506 poll_wait(file
, &pf
->rwait
, wait
);
507 mask
= POLLOUT
| POLLWRNORM
;
508 if (skb_peek(&pf
->rq
))
509 mask
|= POLLIN
| POLLRDNORM
;
512 else if (pf
->kind
== INTERFACE
) {
513 /* see comment in ppp_read */
514 struct ppp
*ppp
= PF_TO_PPP(pf
);
515 if (ppp
->n_channels
== 0 &&
516 (ppp
->flags
& SC_LOOP_TRAFFIC
) == 0)
517 mask
|= POLLIN
| POLLRDNORM
;
523 #ifdef CONFIG_PPP_FILTER
524 static int get_filter(void __user
*arg
, struct sock_filter
**p
)
526 struct sock_fprog uprog
;
527 struct sock_filter
*code
= NULL
;
530 if (copy_from_user(&uprog
, arg
, sizeof(uprog
)))
538 len
= uprog
.len
* sizeof(struct sock_filter
);
539 code
= kmalloc(len
, GFP_KERNEL
);
543 if (copy_from_user(code
, uprog
.filter
, len
)) {
548 err
= sk_chk_filter(code
, uprog
.len
);
557 #endif /* CONFIG_PPP_FILTER */
559 static long ppp_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
561 struct ppp_file
*pf
= file
->private_data
;
563 int err
= -EFAULT
, val
, val2
, i
;
564 struct ppp_idle idle
;
567 struct slcompress
*vj
;
568 void __user
*argp
= (void __user
*)arg
;
569 int __user
*p
= argp
;
572 return ppp_unattached_ioctl(current
->nsproxy
->net_ns
,
575 if (cmd
== PPPIOCDETACH
) {
577 * We have to be careful here... if the file descriptor
578 * has been dup'd, we could have another process in the
579 * middle of a poll using the same file *, so we had
580 * better not free the interface data structures -
581 * instead we fail the ioctl. Even in this case, we
582 * shut down the interface if we are the owner of it.
583 * Actually, we should get rid of PPPIOCDETACH, userland
584 * (i.e. pppd) could achieve the same effect by closing
585 * this fd and reopening /dev/ppp.
589 if (pf
->kind
== INTERFACE
) {
591 if (file
== ppp
->owner
)
592 ppp_shutdown_interface(ppp
);
594 if (atomic_long_read(&file
->f_count
) <= 2) {
595 ppp_release(NULL
, file
);
598 printk(KERN_DEBUG
"PPPIOCDETACH file->f_count=%ld\n",
599 atomic_long_read(&file
->f_count
));
604 if (pf
->kind
== CHANNEL
) {
606 struct ppp_channel
*chan
;
609 pch
= PF_TO_CHANNEL(pf
);
613 if (get_user(unit
, p
))
615 err
= ppp_connect_channel(pch
, unit
);
619 err
= ppp_disconnect_channel(pch
);
623 down_read(&pch
->chan_sem
);
626 if (chan
&& chan
->ops
->ioctl
)
627 err
= chan
->ops
->ioctl(chan
, cmd
, arg
);
628 up_read(&pch
->chan_sem
);
634 if (pf
->kind
!= INTERFACE
) {
636 printk(KERN_ERR
"PPP: not interface or channel??\n");
644 if (get_user(val
, p
))
651 if (get_user(val
, p
))
654 cflags
= ppp
->flags
& ~val
;
655 ppp
->flags
= val
& SC_FLAG_BITS
;
657 if (cflags
& SC_CCP_OPEN
)
663 val
= ppp
->flags
| ppp
->xstate
| ppp
->rstate
;
664 if (put_user(val
, p
))
669 case PPPIOCSCOMPRESS
:
670 err
= ppp_set_compress(ppp
, arg
);
674 if (put_user(ppp
->file
.index
, p
))
680 if (get_user(val
, p
))
687 if (put_user(ppp
->debug
, p
))
693 idle
.xmit_idle
= (jiffies
- ppp
->last_xmit
) / HZ
;
694 idle
.recv_idle
= (jiffies
- ppp
->last_recv
) / HZ
;
695 if (copy_to_user(argp
, &idle
, sizeof(idle
)))
701 if (get_user(val
, p
))
704 if ((val
>> 16) != 0) {
708 vj
= slhc_init(val2
+1, val
+1);
710 printk(KERN_ERR
"PPP: no memory (VJ compressor)\n");
724 if (copy_from_user(&npi
, argp
, sizeof(npi
)))
726 err
= proto_to_npindex(npi
.protocol
);
730 if (cmd
== PPPIOCGNPMODE
) {
732 npi
.mode
= ppp
->npmode
[i
];
733 if (copy_to_user(argp
, &npi
, sizeof(npi
)))
736 ppp
->npmode
[i
] = npi
.mode
;
737 /* we may be able to transmit more packets now (??) */
738 netif_wake_queue(ppp
->dev
);
743 #ifdef CONFIG_PPP_FILTER
746 struct sock_filter
*code
;
747 err
= get_filter(argp
, &code
);
750 kfree(ppp
->pass_filter
);
751 ppp
->pass_filter
= code
;
760 struct sock_filter
*code
;
761 err
= get_filter(argp
, &code
);
764 kfree(ppp
->active_filter
);
765 ppp
->active_filter
= code
;
766 ppp
->active_len
= err
;
772 #endif /* CONFIG_PPP_FILTER */
774 #ifdef CONFIG_PPP_MULTILINK
776 if (get_user(val
, p
))
780 ppp_recv_unlock(ppp
);
783 #endif /* CONFIG_PPP_MULTILINK */
792 static int ppp_unattached_ioctl(struct net
*net
, struct ppp_file
*pf
,
793 struct file
*file
, unsigned int cmd
, unsigned long arg
)
795 int unit
, err
= -EFAULT
;
797 struct channel
*chan
;
799 int __user
*p
= (int __user
*)arg
;
804 /* Create a new ppp unit */
805 if (get_user(unit
, p
))
807 ppp
= ppp_create_interface(net
, unit
, &err
);
810 file
->private_data
= &ppp
->file
;
813 if (put_user(ppp
->file
.index
, p
))
819 /* Attach to an existing ppp unit */
820 if (get_user(unit
, p
))
823 pn
= ppp_pernet(net
);
824 mutex_lock(&pn
->all_ppp_mutex
);
825 ppp
= ppp_find_unit(pn
, unit
);
827 atomic_inc(&ppp
->file
.refcnt
);
828 file
->private_data
= &ppp
->file
;
831 mutex_unlock(&pn
->all_ppp_mutex
);
835 if (get_user(unit
, p
))
838 pn
= ppp_pernet(net
);
839 spin_lock_bh(&pn
->all_channels_lock
);
840 chan
= ppp_find_channel(pn
, unit
);
842 atomic_inc(&chan
->file
.refcnt
);
843 file
->private_data
= &chan
->file
;
846 spin_unlock_bh(&pn
->all_channels_lock
);
856 static const struct file_operations ppp_device_fops
= {
857 .owner
= THIS_MODULE
,
861 .unlocked_ioctl
= ppp_ioctl
,
863 .release
= ppp_release
866 static __net_init
int ppp_init_net(struct net
*net
)
868 struct ppp_net
*pn
= net_generic(net
, ppp_net_id
);
870 idr_init(&pn
->units_idr
);
871 mutex_init(&pn
->all_ppp_mutex
);
873 INIT_LIST_HEAD(&pn
->all_channels
);
874 INIT_LIST_HEAD(&pn
->new_channels
);
876 spin_lock_init(&pn
->all_channels_lock
);
881 static __net_exit
void ppp_exit_net(struct net
*net
)
883 struct ppp_net
*pn
= net_generic(net
, ppp_net_id
);
885 idr_destroy(&pn
->units_idr
);
888 static struct pernet_operations ppp_net_ops
= {
889 .init
= ppp_init_net
,
890 .exit
= ppp_exit_net
,
892 .size
= sizeof(struct ppp_net
),
895 #define PPP_MAJOR 108
897 /* Called at boot time if ppp is compiled into the kernel,
898 or at module load time (from init_module) if compiled as a module. */
899 static int __init
ppp_init(void)
903 printk(KERN_INFO
"PPP generic driver version " PPP_VERSION
"\n");
905 err
= register_pernet_device(&ppp_net_ops
);
907 printk(KERN_ERR
"failed to register PPP pernet device (%d)\n", err
);
911 err
= register_chrdev(PPP_MAJOR
, "ppp", &ppp_device_fops
);
913 printk(KERN_ERR
"failed to register PPP device (%d)\n", err
);
917 ppp_class
= class_create(THIS_MODULE
, "ppp");
918 if (IS_ERR(ppp_class
)) {
919 err
= PTR_ERR(ppp_class
);
923 /* not a big deal if we fail here :-) */
924 device_create(ppp_class
, NULL
, MKDEV(PPP_MAJOR
, 0), NULL
, "ppp");
929 unregister_chrdev(PPP_MAJOR
, "ppp");
931 unregister_pernet_device(&ppp_net_ops
);
937 * Network interface unit routines.
940 ppp_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
942 struct ppp
*ppp
= netdev_priv(dev
);
946 npi
= ethertype_to_npindex(ntohs(skb
->protocol
));
950 /* Drop, accept or reject the packet */
951 switch (ppp
->npmode
[npi
]) {
955 /* it would be nice to have a way to tell the network
956 system to queue this one up for later. */
963 /* Put the 2-byte PPP protocol number on the front,
964 making sure there is room for the address and control fields. */
965 if (skb_cow_head(skb
, PPP_HDRLEN
))
968 pp
= skb_push(skb
, 2);
969 proto
= npindex_to_proto
[npi
];
973 netif_stop_queue(dev
);
974 skb_queue_tail(&ppp
->file
.xq
, skb
);
975 ppp_xmit_process(ppp
);
980 ++dev
->stats
.tx_dropped
;
985 ppp_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
987 struct ppp
*ppp
= netdev_priv(dev
);
989 void __user
*addr
= (void __user
*) ifr
->ifr_ifru
.ifru_data
;
990 struct ppp_stats stats
;
991 struct ppp_comp_stats cstats
;
996 ppp_get_stats(ppp
, &stats
);
997 if (copy_to_user(addr
, &stats
, sizeof(stats
)))
1002 case SIOCGPPPCSTATS
:
1003 memset(&cstats
, 0, sizeof(cstats
));
1005 ppp
->xcomp
->comp_stat(ppp
->xc_state
, &cstats
.c
);
1007 ppp
->rcomp
->decomp_stat(ppp
->rc_state
, &cstats
.d
);
1008 if (copy_to_user(addr
, &cstats
, sizeof(cstats
)))
1015 if (copy_to_user(addr
, vers
, strlen(vers
) + 1))
1027 static const struct net_device_ops ppp_netdev_ops
= {
1028 .ndo_start_xmit
= ppp_start_xmit
,
1029 .ndo_do_ioctl
= ppp_net_ioctl
,
1032 static void ppp_setup(struct net_device
*dev
)
1034 dev
->netdev_ops
= &ppp_netdev_ops
;
1035 dev
->hard_header_len
= PPP_HDRLEN
;
1038 dev
->tx_queue_len
= 3;
1039 dev
->type
= ARPHRD_PPP
;
1040 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
1041 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1042 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1046 * Transmit-side routines.
1050 * Called to do any work queued up on the transmit side
1051 * that can now be done.
1054 ppp_xmit_process(struct ppp
*ppp
)
1056 struct sk_buff
*skb
;
1059 if (!ppp
->closing
) {
1061 while (!ppp
->xmit_pending
&&
1062 (skb
= skb_dequeue(&ppp
->file
.xq
)))
1063 ppp_send_frame(ppp
, skb
);
1064 /* If there's no work left to do, tell the core net
1065 code that we can accept some more. */
1066 if (!ppp
->xmit_pending
&& !skb_peek(&ppp
->file
.xq
))
1067 netif_wake_queue(ppp
->dev
);
1069 ppp_xmit_unlock(ppp
);
1072 static inline struct sk_buff
*
1073 pad_compress_skb(struct ppp
*ppp
, struct sk_buff
*skb
)
1075 struct sk_buff
*new_skb
;
1077 int new_skb_size
= ppp
->dev
->mtu
+
1078 ppp
->xcomp
->comp_extra
+ ppp
->dev
->hard_header_len
;
1079 int compressor_skb_size
= ppp
->dev
->mtu
+
1080 ppp
->xcomp
->comp_extra
+ PPP_HDRLEN
;
1081 new_skb
= alloc_skb(new_skb_size
, GFP_ATOMIC
);
1083 if (net_ratelimit())
1084 printk(KERN_ERR
"PPP: no memory (comp pkt)\n");
1087 if (ppp
->dev
->hard_header_len
> PPP_HDRLEN
)
1088 skb_reserve(new_skb
,
1089 ppp
->dev
->hard_header_len
- PPP_HDRLEN
);
1091 /* compressor still expects A/C bytes in hdr */
1092 len
= ppp
->xcomp
->compress(ppp
->xc_state
, skb
->data
- 2,
1093 new_skb
->data
, skb
->len
+ 2,
1094 compressor_skb_size
);
1095 if (len
> 0 && (ppp
->flags
& SC_CCP_UP
)) {
1099 skb_pull(skb
, 2); /* pull off A/C bytes */
1100 } else if (len
== 0) {
1101 /* didn't compress, or CCP not up yet */
1107 * MPPE requires that we do not send unencrypted
1108 * frames. The compressor will return -1 if we
1109 * should drop the frame. We cannot simply test
1110 * the compress_proto because MPPE and MPPC share
1113 if (net_ratelimit())
1114 printk(KERN_ERR
"ppp: compressor dropped pkt\n");
1123 * Compress and send a frame.
1124 * The caller should have locked the xmit path,
1125 * and xmit_pending should be 0.
1128 ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1130 int proto
= PPP_PROTO(skb
);
1131 struct sk_buff
*new_skb
;
1135 if (proto
< 0x8000) {
1136 #ifdef CONFIG_PPP_FILTER
1137 /* check if we should pass this packet */
1138 /* the filter instructions are constructed assuming
1139 a four-byte PPP header on each packet */
1140 *skb_push(skb
, 2) = 1;
1141 if (ppp
->pass_filter
&&
1142 sk_run_filter(skb
, ppp
->pass_filter
,
1143 ppp
->pass_len
) == 0) {
1145 printk(KERN_DEBUG
"PPP: outbound frame not passed\n");
1149 /* if this packet passes the active filter, record the time */
1150 if (!(ppp
->active_filter
&&
1151 sk_run_filter(skb
, ppp
->active_filter
,
1152 ppp
->active_len
) == 0))
1153 ppp
->last_xmit
= jiffies
;
1156 /* for data packets, record the time */
1157 ppp
->last_xmit
= jiffies
;
1158 #endif /* CONFIG_PPP_FILTER */
1161 ++ppp
->dev
->stats
.tx_packets
;
1162 ppp
->dev
->stats
.tx_bytes
+= skb
->len
- 2;
1166 if (!ppp
->vj
|| (ppp
->flags
& SC_COMP_TCP
) == 0)
1168 /* try to do VJ TCP header compression */
1169 new_skb
= alloc_skb(skb
->len
+ ppp
->dev
->hard_header_len
- 2,
1172 printk(KERN_ERR
"PPP: no memory (VJ comp pkt)\n");
1175 skb_reserve(new_skb
, ppp
->dev
->hard_header_len
- 2);
1177 len
= slhc_compress(ppp
->vj
, cp
, skb
->len
- 2,
1178 new_skb
->data
+ 2, &cp
,
1179 !(ppp
->flags
& SC_NO_TCP_CCID
));
1180 if (cp
== skb
->data
+ 2) {
1181 /* didn't compress */
1184 if (cp
[0] & SL_TYPE_COMPRESSED_TCP
) {
1185 proto
= PPP_VJC_COMP
;
1186 cp
[0] &= ~SL_TYPE_COMPRESSED_TCP
;
1188 proto
= PPP_VJC_UNCOMP
;
1189 cp
[0] = skb
->data
[2];
1193 cp
= skb_put(skb
, len
+ 2);
1200 /* peek at outbound CCP frames */
1201 ppp_ccp_peek(ppp
, skb
, 0);
1205 /* try to do packet compression */
1206 if ((ppp
->xstate
& SC_COMP_RUN
) && ppp
->xc_state
&&
1207 proto
!= PPP_LCP
&& proto
!= PPP_CCP
) {
1208 if (!(ppp
->flags
& SC_CCP_UP
) && (ppp
->flags
& SC_MUST_COMP
)) {
1209 if (net_ratelimit())
1210 printk(KERN_ERR
"ppp: compression required but down - pkt dropped.\n");
1213 skb
= pad_compress_skb(ppp
, skb
);
1219 * If we are waiting for traffic (demand dialling),
1220 * queue it up for pppd to receive.
1222 if (ppp
->flags
& SC_LOOP_TRAFFIC
) {
1223 if (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
)
1225 skb_queue_tail(&ppp
->file
.rq
, skb
);
1226 wake_up_interruptible(&ppp
->file
.rwait
);
1230 ppp
->xmit_pending
= skb
;
1236 ++ppp
->dev
->stats
.tx_errors
;
1240 * Try to send the frame in xmit_pending.
1241 * The caller should have the xmit path locked.
1244 ppp_push(struct ppp
*ppp
)
1246 struct list_head
*list
;
1247 struct channel
*pch
;
1248 struct sk_buff
*skb
= ppp
->xmit_pending
;
1253 list
= &ppp
->channels
;
1254 if (list_empty(list
)) {
1255 /* nowhere to send the packet, just drop it */
1256 ppp
->xmit_pending
= NULL
;
1261 if ((ppp
->flags
& SC_MULTILINK
) == 0) {
1262 /* not doing multilink: send it down the first channel */
1264 pch
= list_entry(list
, struct channel
, clist
);
1266 spin_lock_bh(&pch
->downl
);
1268 if (pch
->chan
->ops
->start_xmit(pch
->chan
, skb
))
1269 ppp
->xmit_pending
= NULL
;
1271 /* channel got unregistered */
1273 ppp
->xmit_pending
= NULL
;
1275 spin_unlock_bh(&pch
->downl
);
1279 #ifdef CONFIG_PPP_MULTILINK
1280 /* Multilink: fragment the packet over as many links
1281 as can take the packet at the moment. */
1282 if (!ppp_mp_explode(ppp
, skb
))
1284 #endif /* CONFIG_PPP_MULTILINK */
1286 ppp
->xmit_pending
= NULL
;
1290 #ifdef CONFIG_PPP_MULTILINK
1292 * Divide a packet to be transmitted into fragments and
1293 * send them out the individual links.
1295 static int ppp_mp_explode(struct ppp
*ppp
, struct sk_buff
*skb
)
1298 int i
, bits
, hdrlen
, mtu
;
1300 int navail
, nfree
, nzero
;
1304 unsigned char *p
, *q
;
1305 struct list_head
*list
;
1306 struct channel
*pch
;
1307 struct sk_buff
*frag
;
1308 struct ppp_channel
*chan
;
1310 totspeed
= 0; /*total bitrate of the bundle*/
1311 nfree
= 0; /* # channels which have no packet already queued */
1312 navail
= 0; /* total # of usable channels (not deregistered) */
1313 nzero
= 0; /* number of channels with zero speed associated*/
1314 totfree
= 0; /*total # of channels available and
1315 *having no queued packets before
1316 *starting the fragmentation*/
1318 hdrlen
= (ppp
->flags
& SC_MP_XSHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1320 list_for_each_entry(pch
, &ppp
->channels
, clist
) {
1321 navail
+= pch
->avail
= (pch
->chan
!= NULL
);
1322 pch
->speed
= pch
->chan
->speed
;
1324 if (skb_queue_empty(&pch
->file
.xq
) ||
1326 if (pch
->speed
== 0)
1329 totspeed
+= pch
->speed
;
1335 if (!pch
->had_frag
&& i
< ppp
->nxchan
)
1341 * Don't start sending this packet unless at least half of
1342 * the channels are free. This gives much better TCP
1343 * performance if we have a lot of channels.
1345 if (nfree
== 0 || nfree
< navail
/ 2)
1346 return 0; /* can't take now, leave it in xmit_pending */
1348 /* Do protocol field compression (XXX this should be optional) */
1357 nbigger
= len
% nfree
;
1359 /* skip to the channel after the one we last used
1360 and start at that one */
1361 list
= &ppp
->channels
;
1362 for (i
= 0; i
< ppp
->nxchan
; ++i
) {
1364 if (list
== &ppp
->channels
) {
1370 /* create a fragment for each channel */
1374 if (list
== &ppp
->channels
) {
1378 pch
= list_entry(list
, struct channel
, clist
);
1384 * Skip this channel if it has a fragment pending already and
1385 * we haven't given a fragment to all of the free channels.
1387 if (pch
->avail
== 1) {
1394 /* check the channel's mtu and whether it is still attached. */
1395 spin_lock_bh(&pch
->downl
);
1396 if (pch
->chan
== NULL
) {
1397 /* can't use this channel, it's being deregistered */
1398 if (pch
->speed
== 0)
1401 totspeed
-= pch
->speed
;
1403 spin_unlock_bh(&pch
->downl
);
1414 *if the channel speed is not set divide
1415 *the packet evenly among the free channels;
1416 *otherwise divide it according to the speed
1417 *of the channel we are going to transmit on
1421 if (pch
->speed
== 0) {
1422 flen
= totlen
/nfree
;
1428 flen
= (((totfree
- nzero
)*(totlen
+ hdrlen
*totfree
)) /
1429 ((totspeed
*totfree
)/pch
->speed
)) - hdrlen
;
1431 flen
+= ((totfree
- nzero
)*pch
->speed
)/totspeed
;
1432 nbigger
-= ((totfree
- nzero
)*pch
->speed
)/
1440 *check if we are on the last channel or
1441 *we exceded the lenght of the data to
1444 if ((nfree
<= 0) || (flen
> len
))
1447 *it is not worth to tx on slow channels:
1448 *in that case from the resulting flen according to the
1449 *above formula will be equal or less than zero.
1450 *Skip the channel in this case
1454 spin_unlock_bh(&pch
->downl
);
1458 mtu
= pch
->chan
->mtu
- hdrlen
;
1465 frag
= alloc_skb(flen
+ hdrlen
+ (flen
== 0), GFP_ATOMIC
);
1468 q
= skb_put(frag
, flen
+ hdrlen
);
1470 /* make the MP header */
1473 if (ppp
->flags
& SC_MP_XSHORTSEQ
) {
1474 q
[2] = bits
+ ((ppp
->nxseq
>> 8) & 0xf);
1478 q
[3] = ppp
->nxseq
>> 16;
1479 q
[4] = ppp
->nxseq
>> 8;
1483 memcpy(q
+ hdrlen
, p
, flen
);
1485 /* try to send it down the channel */
1487 if (!skb_queue_empty(&pch
->file
.xq
) ||
1488 !chan
->ops
->start_xmit(chan
, frag
))
1489 skb_queue_tail(&pch
->file
.xq
, frag
);
1495 spin_unlock_bh(&pch
->downl
);
1502 spin_unlock_bh(&pch
->downl
);
1504 printk(KERN_ERR
"PPP: no memory (fragment)\n");
1505 ++ppp
->dev
->stats
.tx_errors
;
1507 return 1; /* abandon the frame */
1509 #endif /* CONFIG_PPP_MULTILINK */
1512 * Try to send data out on a channel.
1515 ppp_channel_push(struct channel
*pch
)
1517 struct sk_buff
*skb
;
1520 spin_lock_bh(&pch
->downl
);
1522 while (!skb_queue_empty(&pch
->file
.xq
)) {
1523 skb
= skb_dequeue(&pch
->file
.xq
);
1524 if (!pch
->chan
->ops
->start_xmit(pch
->chan
, skb
)) {
1525 /* put the packet back and try again later */
1526 skb_queue_head(&pch
->file
.xq
, skb
);
1531 /* channel got deregistered */
1532 skb_queue_purge(&pch
->file
.xq
);
1534 spin_unlock_bh(&pch
->downl
);
1535 /* see if there is anything from the attached unit to be sent */
1536 if (skb_queue_empty(&pch
->file
.xq
)) {
1537 read_lock_bh(&pch
->upl
);
1540 ppp_xmit_process(ppp
);
1541 read_unlock_bh(&pch
->upl
);
1546 * Receive-side routines.
1549 /* misuse a few fields of the skb for MP reconstruction */
1550 #define sequence priority
1551 #define BEbits cb[0]
1554 ppp_do_recv(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1558 ppp_receive_frame(ppp
, skb
, pch
);
1561 ppp_recv_unlock(ppp
);
1565 ppp_input(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1567 struct channel
*pch
= chan
->ppp
;
1570 if (!pch
|| skb
->len
== 0) {
1575 proto
= PPP_PROTO(skb
);
1576 read_lock_bh(&pch
->upl
);
1577 if (!pch
->ppp
|| proto
>= 0xc000 || proto
== PPP_CCPFRAG
) {
1578 /* put it on the channel queue */
1579 skb_queue_tail(&pch
->file
.rq
, skb
);
1580 /* drop old frames if queue too long */
1581 while (pch
->file
.rq
.qlen
> PPP_MAX_RQLEN
&&
1582 (skb
= skb_dequeue(&pch
->file
.rq
)))
1584 wake_up_interruptible(&pch
->file
.rwait
);
1586 ppp_do_recv(pch
->ppp
, skb
, pch
);
1588 read_unlock_bh(&pch
->upl
);
1591 /* Put a 0-length skb in the receive queue as an error indication */
1593 ppp_input_error(struct ppp_channel
*chan
, int code
)
1595 struct channel
*pch
= chan
->ppp
;
1596 struct sk_buff
*skb
;
1601 read_lock_bh(&pch
->upl
);
1603 skb
= alloc_skb(0, GFP_ATOMIC
);
1605 skb
->len
= 0; /* probably unnecessary */
1607 ppp_do_recv(pch
->ppp
, skb
, pch
);
1610 read_unlock_bh(&pch
->upl
);
1614 * We come in here to process a received frame.
1615 * The receive side of the ppp unit is locked.
1618 ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1620 if (pskb_may_pull(skb
, 2)) {
1621 #ifdef CONFIG_PPP_MULTILINK
1622 /* XXX do channel-level decompression here */
1623 if (PPP_PROTO(skb
) == PPP_MP
)
1624 ppp_receive_mp_frame(ppp
, skb
, pch
);
1626 #endif /* CONFIG_PPP_MULTILINK */
1627 ppp_receive_nonmp_frame(ppp
, skb
);
1632 /* note: a 0-length skb is used as an error indication */
1633 ++ppp
->dev
->stats
.rx_length_errors
;
1636 ppp_receive_error(ppp
);
1640 ppp_receive_error(struct ppp
*ppp
)
1642 ++ppp
->dev
->stats
.rx_errors
;
1648 ppp_receive_nonmp_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1651 int proto
, len
, npi
;
1654 * Decompress the frame, if compressed.
1655 * Note that some decompressors need to see uncompressed frames
1656 * that come in as well as compressed frames.
1658 if (ppp
->rc_state
&& (ppp
->rstate
& SC_DECOMP_RUN
) &&
1659 (ppp
->rstate
& (SC_DC_FERROR
| SC_DC_ERROR
)) == 0)
1660 skb
= ppp_decompress_frame(ppp
, skb
);
1662 if (ppp
->flags
& SC_MUST_COMP
&& ppp
->rstate
& SC_DC_FERROR
)
1665 proto
= PPP_PROTO(skb
);
1668 /* decompress VJ compressed packets */
1669 if (!ppp
->vj
|| (ppp
->flags
& SC_REJ_COMP_TCP
))
1672 if (skb_tailroom(skb
) < 124 || skb_cloned(skb
)) {
1673 /* copy to a new sk_buff with more tailroom */
1674 ns
= dev_alloc_skb(skb
->len
+ 128);
1676 printk(KERN_ERR
"PPP: no memory (VJ decomp)\n");
1680 skb_copy_bits(skb
, 0, skb_put(ns
, skb
->len
), skb
->len
);
1685 skb
->ip_summed
= CHECKSUM_NONE
;
1687 len
= slhc_uncompress(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2);
1689 printk(KERN_DEBUG
"PPP: VJ decompression error\n");
1694 skb_put(skb
, len
- skb
->len
);
1695 else if (len
< skb
->len
)
1700 case PPP_VJC_UNCOMP
:
1701 if (!ppp
->vj
|| (ppp
->flags
& SC_REJ_COMP_TCP
))
1704 /* Until we fix the decompressor need to make sure
1705 * data portion is linear.
1707 if (!pskb_may_pull(skb
, skb
->len
))
1710 if (slhc_remember(ppp
->vj
, skb
->data
+ 2, skb
->len
- 2) <= 0) {
1711 printk(KERN_ERR
"PPP: VJ uncompressed error\n");
1718 ppp_ccp_peek(ppp
, skb
, 1);
1722 ++ppp
->dev
->stats
.rx_packets
;
1723 ppp
->dev
->stats
.rx_bytes
+= skb
->len
- 2;
1725 npi
= proto_to_npindex(proto
);
1727 /* control or unknown frame - pass it to pppd */
1728 skb_queue_tail(&ppp
->file
.rq
, skb
);
1729 /* limit queue length by dropping old frames */
1730 while (ppp
->file
.rq
.qlen
> PPP_MAX_RQLEN
&&
1731 (skb
= skb_dequeue(&ppp
->file
.rq
)))
1733 /* wake up any process polling or blocking on read */
1734 wake_up_interruptible(&ppp
->file
.rwait
);
1737 /* network protocol frame - give it to the kernel */
1739 #ifdef CONFIG_PPP_FILTER
1740 /* check if the packet passes the pass and active filters */
1741 /* the filter instructions are constructed assuming
1742 a four-byte PPP header on each packet */
1743 if (ppp
->pass_filter
|| ppp
->active_filter
) {
1744 if (skb_cloned(skb
) &&
1745 pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
1748 *skb_push(skb
, 2) = 0;
1749 if (ppp
->pass_filter
&&
1750 sk_run_filter(skb
, ppp
->pass_filter
,
1751 ppp
->pass_len
) == 0) {
1753 printk(KERN_DEBUG
"PPP: inbound frame "
1758 if (!(ppp
->active_filter
&&
1759 sk_run_filter(skb
, ppp
->active_filter
,
1760 ppp
->active_len
) == 0))
1761 ppp
->last_recv
= jiffies
;
1764 #endif /* CONFIG_PPP_FILTER */
1765 ppp
->last_recv
= jiffies
;
1767 if ((ppp
->dev
->flags
& IFF_UP
) == 0 ||
1768 ppp
->npmode
[npi
] != NPMODE_PASS
) {
1771 /* chop off protocol */
1772 skb_pull_rcsum(skb
, 2);
1773 skb
->dev
= ppp
->dev
;
1774 skb
->protocol
= htons(npindex_to_ethertype
[npi
]);
1775 skb_reset_mac_header(skb
);
1783 ppp_receive_error(ppp
);
1786 static struct sk_buff
*
1787 ppp_decompress_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
1789 int proto
= PPP_PROTO(skb
);
1793 /* Until we fix all the decompressor's need to make sure
1794 * data portion is linear.
1796 if (!pskb_may_pull(skb
, skb
->len
))
1799 if (proto
== PPP_COMP
) {
1802 switch(ppp
->rcomp
->compress_proto
) {
1804 obuff_size
= ppp
->mru
+ PPP_HDRLEN
+ 1;
1807 obuff_size
= ppp
->mru
+ PPP_HDRLEN
;
1811 ns
= dev_alloc_skb(obuff_size
);
1813 printk(KERN_ERR
"ppp_decompress_frame: no memory\n");
1816 /* the decompressor still expects the A/C bytes in the hdr */
1817 len
= ppp
->rcomp
->decompress(ppp
->rc_state
, skb
->data
- 2,
1818 skb
->len
+ 2, ns
->data
, obuff_size
);
1820 /* Pass the compressed frame to pppd as an
1821 error indication. */
1822 if (len
== DECOMP_FATALERROR
)
1823 ppp
->rstate
|= SC_DC_FERROR
;
1831 skb_pull(skb
, 2); /* pull off the A/C bytes */
1834 /* Uncompressed frame - pass to decompressor so it
1835 can update its dictionary if necessary. */
1836 if (ppp
->rcomp
->incomp
)
1837 ppp
->rcomp
->incomp(ppp
->rc_state
, skb
->data
- 2,
1844 ppp
->rstate
|= SC_DC_ERROR
;
1845 ppp_receive_error(ppp
);
1849 #ifdef CONFIG_PPP_MULTILINK
1851 * Receive a multilink frame.
1852 * We put it on the reconstruction queue and then pull off
1853 * as many completed frames as we can.
1856 ppp_receive_mp_frame(struct ppp
*ppp
, struct sk_buff
*skb
, struct channel
*pch
)
1860 int mphdrlen
= (ppp
->flags
& SC_MP_SHORTSEQ
)? MPHDRLEN_SSN
: MPHDRLEN
;
1862 if (!pskb_may_pull(skb
, mphdrlen
+ 1) || ppp
->mrru
== 0)
1863 goto err
; /* no good, throw it away */
1865 /* Decode sequence number and begin/end bits */
1866 if (ppp
->flags
& SC_MP_SHORTSEQ
) {
1867 seq
= ((skb
->data
[2] & 0x0f) << 8) | skb
->data
[3];
1870 seq
= (skb
->data
[3] << 16) | (skb
->data
[4] << 8)| skb
->data
[5];
1873 skb
->BEbits
= skb
->data
[2];
1874 skb_pull(skb
, mphdrlen
); /* pull off PPP and MP headers */
1877 * Do protocol ID decompression on the first fragment of each packet.
1879 if ((skb
->BEbits
& B
) && (skb
->data
[0] & 1))
1880 *skb_push(skb
, 1) = 0;
1883 * Expand sequence number to 32 bits, making it as close
1884 * as possible to ppp->minseq.
1886 seq
|= ppp
->minseq
& ~mask
;
1887 if ((int)(ppp
->minseq
- seq
) > (int)(mask
>> 1))
1889 else if ((int)(seq
- ppp
->minseq
) > (int)(mask
>> 1))
1890 seq
-= mask
+ 1; /* should never happen */
1891 skb
->sequence
= seq
;
1895 * If this packet comes before the next one we were expecting,
1898 if (seq_before(seq
, ppp
->nextseq
)) {
1900 ++ppp
->dev
->stats
.rx_dropped
;
1901 ppp_receive_error(ppp
);
1906 * Reevaluate minseq, the minimum over all channels of the
1907 * last sequence number received on each channel. Because of
1908 * the increasing sequence number rule, we know that any fragment
1909 * before `minseq' which hasn't arrived is never going to arrive.
1910 * The list of channels can't change because we have the receive
1911 * side of the ppp unit locked.
1913 list_for_each_entry(ch
, &ppp
->channels
, clist
) {
1914 if (seq_before(ch
->lastseq
, seq
))
1917 if (seq_before(ppp
->minseq
, seq
))
1920 /* Put the fragment on the reconstruction queue */
1921 ppp_mp_insert(ppp
, skb
);
1923 /* If the queue is getting long, don't wait any longer for packets
1924 before the start of the queue. */
1925 if (skb_queue_len(&ppp
->mrq
) >= PPP_MP_MAX_QLEN
) {
1926 struct sk_buff
*skb
= skb_peek(&ppp
->mrq
);
1927 if (seq_before(ppp
->minseq
, skb
->sequence
))
1928 ppp
->minseq
= skb
->sequence
;
1931 /* Pull completed packets off the queue and receive them. */
1932 while ((skb
= ppp_mp_reconstruct(ppp
))) {
1933 if (pskb_may_pull(skb
, 2))
1934 ppp_receive_nonmp_frame(ppp
, skb
);
1936 ++ppp
->dev
->stats
.rx_length_errors
;
1938 ppp_receive_error(ppp
);
1946 ppp_receive_error(ppp
);
1950 * Insert a fragment on the MP reconstruction queue.
1951 * The queue is ordered by increasing sequence number.
1954 ppp_mp_insert(struct ppp
*ppp
, struct sk_buff
*skb
)
1957 struct sk_buff_head
*list
= &ppp
->mrq
;
1958 u32 seq
= skb
->sequence
;
1960 /* N.B. we don't need to lock the list lock because we have the
1961 ppp unit receive-side lock. */
1962 skb_queue_walk(list
, p
) {
1963 if (seq_before(seq
, p
->sequence
))
1966 __skb_queue_before(list
, p
, skb
);
1970 * Reconstruct a packet from the MP fragment queue.
1971 * We go through increasing sequence numbers until we find a
1972 * complete packet, or we get to the sequence number for a fragment
1973 * which hasn't arrived but might still do so.
1975 static struct sk_buff
*
1976 ppp_mp_reconstruct(struct ppp
*ppp
)
1978 u32 seq
= ppp
->nextseq
;
1979 u32 minseq
= ppp
->minseq
;
1980 struct sk_buff_head
*list
= &ppp
->mrq
;
1981 struct sk_buff
*p
, *next
;
1982 struct sk_buff
*head
, *tail
;
1983 struct sk_buff
*skb
= NULL
;
1984 int lost
= 0, len
= 0;
1986 if (ppp
->mrru
== 0) /* do nothing until mrru is set */
1990 for (p
= head
; p
!= (struct sk_buff
*) list
; p
= next
) {
1992 if (seq_before(p
->sequence
, seq
)) {
1993 /* this can't happen, anyway ignore the skb */
1994 printk(KERN_ERR
"ppp_mp_reconstruct bad seq %u < %u\n",
1999 if (p
->sequence
!= seq
) {
2000 /* Fragment `seq' is missing. If it is after
2001 minseq, it might arrive later, so stop here. */
2002 if (seq_after(seq
, minseq
))
2004 /* Fragment `seq' is lost, keep going. */
2006 seq
= seq_before(minseq
, p
->sequence
)?
2007 minseq
+ 1: p
->sequence
;
2013 * At this point we know that all the fragments from
2014 * ppp->nextseq to seq are either present or lost.
2015 * Also, there are no complete packets in the queue
2016 * that have no missing fragments and end before this
2020 /* B bit set indicates this fragment starts a packet */
2021 if (p
->BEbits
& B
) {
2029 /* Got a complete packet yet? */
2030 if (lost
== 0 && (p
->BEbits
& E
) && (head
->BEbits
& B
)) {
2031 if (len
> ppp
->mrru
+ 2) {
2032 ++ppp
->dev
->stats
.rx_length_errors
;
2033 printk(KERN_DEBUG
"PPP: reconstructed packet"
2034 " is too long (%d)\n", len
);
2035 } else if (p
== head
) {
2036 /* fragment is complete packet - reuse skb */
2040 } else if ((skb
= dev_alloc_skb(len
)) == NULL
) {
2041 ++ppp
->dev
->stats
.rx_missed_errors
;
2042 printk(KERN_DEBUG
"PPP: no memory for "
2043 "reconstructed packet");
2048 ppp
->nextseq
= seq
+ 1;
2052 * If this is the ending fragment of a packet,
2053 * and we haven't found a complete valid packet yet,
2054 * we can discard up to and including this fragment.
2062 /* If we have a complete packet, copy it all into one skb. */
2064 /* If we have discarded any fragments,
2065 signal a receive error. */
2066 if (head
->sequence
!= ppp
->nextseq
) {
2068 printk(KERN_DEBUG
" missed pkts %u..%u\n",
2069 ppp
->nextseq
, head
->sequence
-1);
2070 ++ppp
->dev
->stats
.rx_dropped
;
2071 ppp_receive_error(ppp
);
2075 /* copy to a single skb */
2076 for (p
= head
; p
!= tail
->next
; p
= p
->next
)
2077 skb_copy_bits(p
, 0, skb_put(skb
, p
->len
), p
->len
);
2078 ppp
->nextseq
= tail
->sequence
+ 1;
2082 /* Discard all the skbuffs that we have copied the data out of
2083 or that we can't use. */
2084 while ((p
= list
->next
) != head
) {
2085 __skb_unlink(p
, list
);
2091 #endif /* CONFIG_PPP_MULTILINK */
2094 * Channel interface.
2097 /* Create a new, unattached ppp channel. */
2098 int ppp_register_channel(struct ppp_channel
*chan
)
2100 return ppp_register_net_channel(current
->nsproxy
->net_ns
, chan
);
2103 /* Create a new, unattached ppp channel for specified net. */
2104 int ppp_register_net_channel(struct net
*net
, struct ppp_channel
*chan
)
2106 struct channel
*pch
;
2109 pch
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
2113 pn
= ppp_pernet(net
);
2117 pch
->chan_net
= net
;
2119 init_ppp_file(&pch
->file
, CHANNEL
);
2120 pch
->file
.hdrlen
= chan
->hdrlen
;
2121 #ifdef CONFIG_PPP_MULTILINK
2123 #endif /* CONFIG_PPP_MULTILINK */
2124 init_rwsem(&pch
->chan_sem
);
2125 spin_lock_init(&pch
->downl
);
2126 rwlock_init(&pch
->upl
);
2128 spin_lock_bh(&pn
->all_channels_lock
);
2129 pch
->file
.index
= ++pn
->last_channel_index
;
2130 list_add(&pch
->list
, &pn
->new_channels
);
2131 atomic_inc(&channel_count
);
2132 spin_unlock_bh(&pn
->all_channels_lock
);
2138 * Return the index of a channel.
2140 int ppp_channel_index(struct ppp_channel
*chan
)
2142 struct channel
*pch
= chan
->ppp
;
2145 return pch
->file
.index
;
2150 * Return the PPP unit number to which a channel is connected.
2152 int ppp_unit_number(struct ppp_channel
*chan
)
2154 struct channel
*pch
= chan
->ppp
;
2158 read_lock_bh(&pch
->upl
);
2160 unit
= pch
->ppp
->file
.index
;
2161 read_unlock_bh(&pch
->upl
);
2167 * Disconnect a channel from the generic layer.
2168 * This must be called in process context.
2171 ppp_unregister_channel(struct ppp_channel
*chan
)
2173 struct channel
*pch
= chan
->ppp
;
2177 return; /* should never happen */
2182 * This ensures that we have returned from any calls into the
2183 * the channel's start_xmit or ioctl routine before we proceed.
2185 down_write(&pch
->chan_sem
);
2186 spin_lock_bh(&pch
->downl
);
2188 spin_unlock_bh(&pch
->downl
);
2189 up_write(&pch
->chan_sem
);
2190 ppp_disconnect_channel(pch
);
2192 pn
= ppp_pernet(pch
->chan_net
);
2193 spin_lock_bh(&pn
->all_channels_lock
);
2194 list_del(&pch
->list
);
2195 spin_unlock_bh(&pn
->all_channels_lock
);
2198 wake_up_interruptible(&pch
->file
.rwait
);
2199 if (atomic_dec_and_test(&pch
->file
.refcnt
))
2200 ppp_destroy_channel(pch
);
2204 * Callback from a channel when it can accept more to transmit.
2205 * This should be called at BH/softirq level, not interrupt level.
2208 ppp_output_wakeup(struct ppp_channel
*chan
)
2210 struct channel
*pch
= chan
->ppp
;
2214 ppp_channel_push(pch
);
2218 * Compression control.
2221 /* Process the PPPIOCSCOMPRESS ioctl. */
2223 ppp_set_compress(struct ppp
*ppp
, unsigned long arg
)
2226 struct compressor
*cp
, *ocomp
;
2227 struct ppp_option_data data
;
2228 void *state
, *ostate
;
2229 unsigned char ccp_option
[CCP_MAX_OPTION_LENGTH
];
2232 if (copy_from_user(&data
, (void __user
*) arg
, sizeof(data
)) ||
2233 (data
.length
<= CCP_MAX_OPTION_LENGTH
&&
2234 copy_from_user(ccp_option
, (void __user
*) data
.ptr
, data
.length
)))
2237 if (data
.length
> CCP_MAX_OPTION_LENGTH
||
2238 ccp_option
[1] < 2 || ccp_option
[1] > data
.length
)
2241 cp
= try_then_request_module(
2242 find_compressor(ccp_option
[0]),
2243 "ppp-compress-%d", ccp_option
[0]);
2248 if (data
.transmit
) {
2249 state
= cp
->comp_alloc(ccp_option
, data
.length
);
2252 ppp
->xstate
&= ~SC_COMP_RUN
;
2254 ostate
= ppp
->xc_state
;
2256 ppp
->xc_state
= state
;
2257 ppp_xmit_unlock(ppp
);
2259 ocomp
->comp_free(ostate
);
2260 module_put(ocomp
->owner
);
2264 module_put(cp
->owner
);
2267 state
= cp
->decomp_alloc(ccp_option
, data
.length
);
2270 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2272 ostate
= ppp
->rc_state
;
2274 ppp
->rc_state
= state
;
2275 ppp_recv_unlock(ppp
);
2277 ocomp
->decomp_free(ostate
);
2278 module_put(ocomp
->owner
);
2282 module_put(cp
->owner
);
2290 * Look at a CCP packet and update our state accordingly.
2291 * We assume the caller has the xmit or recv path locked.
2294 ppp_ccp_peek(struct ppp
*ppp
, struct sk_buff
*skb
, int inbound
)
2299 if (!pskb_may_pull(skb
, CCP_HDRLEN
+ 2))
2300 return; /* no header */
2303 switch (CCP_CODE(dp
)) {
2306 /* A ConfReq starts negotiation of compression
2307 * in one direction of transmission,
2308 * and hence brings it down...but which way?
2311 * A ConfReq indicates what the sender would like to receive
2314 /* He is proposing what I should send */
2315 ppp
->xstate
&= ~SC_COMP_RUN
;
2317 /* I am proposing to what he should send */
2318 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2325 * CCP is going down, both directions of transmission
2327 ppp
->rstate
&= ~SC_DECOMP_RUN
;
2328 ppp
->xstate
&= ~SC_COMP_RUN
;
2332 if ((ppp
->flags
& (SC_CCP_OPEN
| SC_CCP_UP
)) != SC_CCP_OPEN
)
2334 len
= CCP_LENGTH(dp
);
2335 if (!pskb_may_pull(skb
, len
+ 2))
2336 return; /* too short */
2339 if (len
< CCP_OPT_MINLEN
|| len
< CCP_OPT_LENGTH(dp
))
2342 /* we will start receiving compressed packets */
2345 if (ppp
->rcomp
->decomp_init(ppp
->rc_state
, dp
, len
,
2346 ppp
->file
.index
, 0, ppp
->mru
, ppp
->debug
)) {
2347 ppp
->rstate
|= SC_DECOMP_RUN
;
2348 ppp
->rstate
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
2351 /* we will soon start sending compressed packets */
2354 if (ppp
->xcomp
->comp_init(ppp
->xc_state
, dp
, len
,
2355 ppp
->file
.index
, 0, ppp
->debug
))
2356 ppp
->xstate
|= SC_COMP_RUN
;
2361 /* reset the [de]compressor */
2362 if ((ppp
->flags
& SC_CCP_UP
) == 0)
2365 if (ppp
->rc_state
&& (ppp
->rstate
& SC_DECOMP_RUN
)) {
2366 ppp
->rcomp
->decomp_reset(ppp
->rc_state
);
2367 ppp
->rstate
&= ~SC_DC_ERROR
;
2370 if (ppp
->xc_state
&& (ppp
->xstate
& SC_COMP_RUN
))
2371 ppp
->xcomp
->comp_reset(ppp
->xc_state
);
2377 /* Free up compression resources. */
2379 ppp_ccp_closed(struct ppp
*ppp
)
2381 void *xstate
, *rstate
;
2382 struct compressor
*xcomp
, *rcomp
;
2385 ppp
->flags
&= ~(SC_CCP_OPEN
| SC_CCP_UP
);
2388 xstate
= ppp
->xc_state
;
2389 ppp
->xc_state
= NULL
;
2392 rstate
= ppp
->rc_state
;
2393 ppp
->rc_state
= NULL
;
2397 xcomp
->comp_free(xstate
);
2398 module_put(xcomp
->owner
);
2401 rcomp
->decomp_free(rstate
);
2402 module_put(rcomp
->owner
);
2406 /* List of compressors. */
2407 static LIST_HEAD(compressor_list
);
2408 static DEFINE_SPINLOCK(compressor_list_lock
);
2410 struct compressor_entry
{
2411 struct list_head list
;
2412 struct compressor
*comp
;
2415 static struct compressor_entry
*
2416 find_comp_entry(int proto
)
2418 struct compressor_entry
*ce
;
2420 list_for_each_entry(ce
, &compressor_list
, list
) {
2421 if (ce
->comp
->compress_proto
== proto
)
2427 /* Register a compressor */
2429 ppp_register_compressor(struct compressor
*cp
)
2431 struct compressor_entry
*ce
;
2433 spin_lock(&compressor_list_lock
);
2435 if (find_comp_entry(cp
->compress_proto
))
2438 ce
= kmalloc(sizeof(struct compressor_entry
), GFP_ATOMIC
);
2443 list_add(&ce
->list
, &compressor_list
);
2445 spin_unlock(&compressor_list_lock
);
2449 /* Unregister a compressor */
2451 ppp_unregister_compressor(struct compressor
*cp
)
2453 struct compressor_entry
*ce
;
2455 spin_lock(&compressor_list_lock
);
2456 ce
= find_comp_entry(cp
->compress_proto
);
2457 if (ce
&& ce
->comp
== cp
) {
2458 list_del(&ce
->list
);
2461 spin_unlock(&compressor_list_lock
);
2464 /* Find a compressor. */
2465 static struct compressor
*
2466 find_compressor(int type
)
2468 struct compressor_entry
*ce
;
2469 struct compressor
*cp
= NULL
;
2471 spin_lock(&compressor_list_lock
);
2472 ce
= find_comp_entry(type
);
2475 if (!try_module_get(cp
->owner
))
2478 spin_unlock(&compressor_list_lock
);
2483 * Miscelleneous stuff.
2487 ppp_get_stats(struct ppp
*ppp
, struct ppp_stats
*st
)
2489 struct slcompress
*vj
= ppp
->vj
;
2491 memset(st
, 0, sizeof(*st
));
2492 st
->p
.ppp_ipackets
= ppp
->dev
->stats
.rx_packets
;
2493 st
->p
.ppp_ierrors
= ppp
->dev
->stats
.rx_errors
;
2494 st
->p
.ppp_ibytes
= ppp
->dev
->stats
.rx_bytes
;
2495 st
->p
.ppp_opackets
= ppp
->dev
->stats
.tx_packets
;
2496 st
->p
.ppp_oerrors
= ppp
->dev
->stats
.tx_errors
;
2497 st
->p
.ppp_obytes
= ppp
->dev
->stats
.tx_bytes
;
2500 st
->vj
.vjs_packets
= vj
->sls_o_compressed
+ vj
->sls_o_uncompressed
;
2501 st
->vj
.vjs_compressed
= vj
->sls_o_compressed
;
2502 st
->vj
.vjs_searches
= vj
->sls_o_searches
;
2503 st
->vj
.vjs_misses
= vj
->sls_o_misses
;
2504 st
->vj
.vjs_errorin
= vj
->sls_i_error
;
2505 st
->vj
.vjs_tossed
= vj
->sls_i_tossed
;
2506 st
->vj
.vjs_uncompressedin
= vj
->sls_i_uncompressed
;
2507 st
->vj
.vjs_compressedin
= vj
->sls_i_compressed
;
2511 * Stuff for handling the lists of ppp units and channels
2512 * and for initialization.
2516 * Create a new ppp interface unit. Fails if it can't allocate memory
2517 * or if there is already a unit with the requested number.
2518 * unit == -1 means allocate a new number.
2521 ppp_create_interface(struct net
*net
, int unit
, int *retp
)
2525 struct net_device
*dev
= NULL
;
2529 dev
= alloc_netdev(sizeof(struct ppp
), "", ppp_setup
);
2533 pn
= ppp_pernet(net
);
2535 ppp
= netdev_priv(dev
);
2538 init_ppp_file(&ppp
->file
, INTERFACE
);
2539 ppp
->file
.hdrlen
= PPP_HDRLEN
- 2; /* don't count proto bytes */
2540 for (i
= 0; i
< NUM_NP
; ++i
)
2541 ppp
->npmode
[i
] = NPMODE_PASS
;
2542 INIT_LIST_HEAD(&ppp
->channels
);
2543 spin_lock_init(&ppp
->rlock
);
2544 spin_lock_init(&ppp
->wlock
);
2545 #ifdef CONFIG_PPP_MULTILINK
2547 skb_queue_head_init(&ppp
->mrq
);
2548 #endif /* CONFIG_PPP_MULTILINK */
2551 * drum roll: don't forget to set
2552 * the net device is belong to
2554 dev_net_set(dev
, net
);
2557 mutex_lock(&pn
->all_ppp_mutex
);
2560 unit
= unit_get(&pn
->units_idr
, ppp
);
2566 if (unit_find(&pn
->units_idr
, unit
))
2567 goto out2
; /* unit already exists */
2569 * if caller need a specified unit number
2570 * lets try to satisfy him, otherwise --
2571 * he should better ask us for new unit number
2573 * NOTE: yes I know that returning EEXIST it's not
2574 * fair but at least pppd will ask us to allocate
2575 * new unit in this case so user is happy :)
2577 unit
= unit_set(&pn
->units_idr
, ppp
, unit
);
2582 /* Initialize the new ppp unit */
2583 ppp
->file
.index
= unit
;
2584 sprintf(dev
->name
, "ppp%d", unit
);
2586 ret
= register_netdev(dev
);
2588 unit_put(&pn
->units_idr
, unit
);
2589 printk(KERN_ERR
"PPP: couldn't register device %s (%d)\n",
2596 atomic_inc(&ppp_unit_count
);
2597 mutex_unlock(&pn
->all_ppp_mutex
);
2603 mutex_unlock(&pn
->all_ppp_mutex
);
2611 * Initialize a ppp_file structure.
2614 init_ppp_file(struct ppp_file
*pf
, int kind
)
2617 skb_queue_head_init(&pf
->xq
);
2618 skb_queue_head_init(&pf
->rq
);
2619 atomic_set(&pf
->refcnt
, 1);
2620 init_waitqueue_head(&pf
->rwait
);
2624 * Take down a ppp interface unit - called when the owning file
2625 * (the one that created the unit) is closed or detached.
2627 static void ppp_shutdown_interface(struct ppp
*ppp
)
2631 pn
= ppp_pernet(ppp
->ppp_net
);
2632 mutex_lock(&pn
->all_ppp_mutex
);
2634 /* This will call dev_close() for us. */
2636 if (!ppp
->closing
) {
2639 unregister_netdev(ppp
->dev
);
2643 unit_put(&pn
->units_idr
, ppp
->file
.index
);
2646 wake_up_interruptible(&ppp
->file
.rwait
);
2648 mutex_unlock(&pn
->all_ppp_mutex
);
2652 * Free the memory used by a ppp unit. This is only called once
2653 * there are no channels connected to the unit and no file structs
2654 * that reference the unit.
2656 static void ppp_destroy_interface(struct ppp
*ppp
)
2658 atomic_dec(&ppp_unit_count
);
2660 if (!ppp
->file
.dead
|| ppp
->n_channels
) {
2661 /* "can't happen" */
2662 printk(KERN_ERR
"ppp: destroying ppp struct %p but dead=%d "
2663 "n_channels=%d !\n", ppp
, ppp
->file
.dead
,
2668 ppp_ccp_closed(ppp
);
2673 skb_queue_purge(&ppp
->file
.xq
);
2674 skb_queue_purge(&ppp
->file
.rq
);
2675 #ifdef CONFIG_PPP_MULTILINK
2676 skb_queue_purge(&ppp
->mrq
);
2677 #endif /* CONFIG_PPP_MULTILINK */
2678 #ifdef CONFIG_PPP_FILTER
2679 kfree(ppp
->pass_filter
);
2680 ppp
->pass_filter
= NULL
;
2681 kfree(ppp
->active_filter
);
2682 ppp
->active_filter
= NULL
;
2683 #endif /* CONFIG_PPP_FILTER */
2685 kfree_skb(ppp
->xmit_pending
);
2687 free_netdev(ppp
->dev
);
2691 * Locate an existing ppp unit.
2692 * The caller should have locked the all_ppp_mutex.
2695 ppp_find_unit(struct ppp_net
*pn
, int unit
)
2697 return unit_find(&pn
->units_idr
, unit
);
2701 * Locate an existing ppp channel.
2702 * The caller should have locked the all_channels_lock.
2703 * First we look in the new_channels list, then in the
2704 * all_channels list. If found in the new_channels list,
2705 * we move it to the all_channels list. This is for speed
2706 * when we have a lot of channels in use.
2708 static struct channel
*
2709 ppp_find_channel(struct ppp_net
*pn
, int unit
)
2711 struct channel
*pch
;
2713 list_for_each_entry(pch
, &pn
->new_channels
, list
) {
2714 if (pch
->file
.index
== unit
) {
2715 list_move(&pch
->list
, &pn
->all_channels
);
2720 list_for_each_entry(pch
, &pn
->all_channels
, list
) {
2721 if (pch
->file
.index
== unit
)
2729 * Connect a PPP channel to a PPP interface unit.
2732 ppp_connect_channel(struct channel
*pch
, int unit
)
2739 pn
= ppp_pernet(pch
->chan_net
);
2741 mutex_lock(&pn
->all_ppp_mutex
);
2742 ppp
= ppp_find_unit(pn
, unit
);
2745 write_lock_bh(&pch
->upl
);
2751 if (pch
->file
.hdrlen
> ppp
->file
.hdrlen
)
2752 ppp
->file
.hdrlen
= pch
->file
.hdrlen
;
2753 hdrlen
= pch
->file
.hdrlen
+ 2; /* for protocol bytes */
2754 if (hdrlen
> ppp
->dev
->hard_header_len
)
2755 ppp
->dev
->hard_header_len
= hdrlen
;
2756 list_add_tail(&pch
->clist
, &ppp
->channels
);
2759 atomic_inc(&ppp
->file
.refcnt
);
2764 write_unlock_bh(&pch
->upl
);
2766 mutex_unlock(&pn
->all_ppp_mutex
);
2771 * Disconnect a channel from its ppp unit.
2774 ppp_disconnect_channel(struct channel
*pch
)
2779 write_lock_bh(&pch
->upl
);
2782 write_unlock_bh(&pch
->upl
);
2784 /* remove it from the ppp unit's list */
2786 list_del(&pch
->clist
);
2787 if (--ppp
->n_channels
== 0)
2788 wake_up_interruptible(&ppp
->file
.rwait
);
2790 if (atomic_dec_and_test(&ppp
->file
.refcnt
))
2791 ppp_destroy_interface(ppp
);
2798 * Free up the resources used by a ppp channel.
2800 static void ppp_destroy_channel(struct channel
*pch
)
2802 atomic_dec(&channel_count
);
2804 if (!pch
->file
.dead
) {
2805 /* "can't happen" */
2806 printk(KERN_ERR
"ppp: destroying undead channel %p !\n",
2810 skb_queue_purge(&pch
->file
.xq
);
2811 skb_queue_purge(&pch
->file
.rq
);
2815 static void __exit
ppp_cleanup(void)
2817 /* should never happen */
2818 if (atomic_read(&ppp_unit_count
) || atomic_read(&channel_count
))
2819 printk(KERN_ERR
"PPP: removing module but units remain!\n");
2820 unregister_chrdev(PPP_MAJOR
, "ppp");
2821 device_destroy(ppp_class
, MKDEV(PPP_MAJOR
, 0));
2822 class_destroy(ppp_class
);
2823 unregister_pernet_device(&ppp_net_ops
);
2827 * Units handling. Caller must protect concurrent access
2828 * by holding all_ppp_mutex
2831 /* associate pointer with specified number */
2832 static int unit_set(struct idr
*p
, void *ptr
, int n
)
2837 if (!idr_pre_get(p
, GFP_KERNEL
)) {
2838 printk(KERN_ERR
"PPP: No free memory for idr\n");
2842 err
= idr_get_new_above(p
, ptr
, n
, &unit
);
2847 idr_remove(p
, unit
);
2854 /* get new free unit number and associate pointer with it */
2855 static int unit_get(struct idr
*p
, void *ptr
)
2860 if (!idr_pre_get(p
, GFP_KERNEL
)) {
2861 printk(KERN_ERR
"PPP: No free memory for idr\n");
2865 err
= idr_get_new_above(p
, ptr
, 0, &unit
);
2872 /* put unit number back to a pool */
2873 static void unit_put(struct idr
*p
, int n
)
2878 /* get pointer associated with the number */
2879 static void *unit_find(struct idr
*p
, int n
)
2881 return idr_find(p
, n
);
2884 /* Module/initialization stuff */
2886 module_init(ppp_init
);
2887 module_exit(ppp_cleanup
);
2889 EXPORT_SYMBOL(ppp_register_net_channel
);
2890 EXPORT_SYMBOL(ppp_register_channel
);
2891 EXPORT_SYMBOL(ppp_unregister_channel
);
2892 EXPORT_SYMBOL(ppp_channel_index
);
2893 EXPORT_SYMBOL(ppp_unit_number
);
2894 EXPORT_SYMBOL(ppp_input
);
2895 EXPORT_SYMBOL(ppp_input_error
);
2896 EXPORT_SYMBOL(ppp_output_wakeup
);
2897 EXPORT_SYMBOL(ppp_register_compressor
);
2898 EXPORT_SYMBOL(ppp_unregister_compressor
);
2899 MODULE_LICENSE("GPL");
2900 MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR
);
2901 MODULE_ALIAS("/dev/ppp");