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/sched/signal.h>
28 #include <linux/kmod.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/idr.h>
32 #include <linux/netdevice.h>
33 #include <linux/poll.h>
34 #include <linux/ppp_defs.h>
35 #include <linux/filter.h>
36 #include <linux/ppp-ioctl.h>
37 #include <linux/ppp_channel.h>
38 #include <linux/ppp-comp.h>
39 #include <linux/skbuff.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/if_arp.h>
43 #include <linux/tcp.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 <linux/file.h>
51 #include <asm/unaligned.h>
52 #include <net/slhc_vj.h>
53 #include <linux/atomic.h>
55 #include <linux/nsproxy.h>
56 #include <net/net_namespace.h>
57 #include <net/netns/generic.h>
59 #define PPP_VERSION "2.4.2"
62 * Network protocols we support.
64 #define NP_IP 0 /* Internet Protocol V4 */
65 #define NP_IPV6 1 /* Internet Protocol V6 */
66 #define NP_IPX 2 /* IPX protocol */
67 #define NP_AT 3 /* Appletalk protocol */
68 #define NP_MPLS_UC 4 /* MPLS unicast */
69 #define NP_MPLS_MC 5 /* MPLS multicast */
70 #define NUM_NP 6 /* Number of NPs. */
72 #define MPHDRLEN 6 /* multilink protocol header length */
73 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
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) container_of(pf, 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)
99 * Data structure to hold primary network stats for which
100 * we want to use 64 bit storage. Other network stats
101 * are stored in dev->stats of the ppp strucute.
103 struct ppp_link_stats {
111 * Data structure describing one ppp unit.
112 * A ppp unit corresponds to a ppp network interface device
113 * and represents a multilink bundle.
114 * It can have 0 or more ppp channels connected to it.
117 struct ppp_file file; /* stuff for read/write/poll 0 */
118 struct file *owner; /* file that owns this unit 48 */
119 struct list_head channels; /* list of attached channels 4c */
120 int n_channels; /* how many channels are attached 54 */
121 spinlock_t rlock; /* lock for receive side 58 */
122 spinlock_t wlock; /* lock for transmit side 5c */
123 int *xmit_recursion __percpu; /* xmit recursion detect */
124 int mru; /* max receive unit 60 */
125 unsigned int flags; /* control bits 64 */
126 unsigned int xstate; /* transmit state bits 68 */
127 unsigned int rstate; /* receive state bits 6c */
128 int debug; /* debug flags 70 */
129 struct slcompress *vj; /* state for VJ header compression */
130 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
131 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
132 struct compressor *xcomp; /* transmit packet compressor 8c */
133 void *xc_state; /* its internal state 90 */
134 struct compressor *rcomp; /* receive decompressor 94 */
135 void *rc_state; /* its internal state 98 */
136 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
137 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
138 struct net_device *dev; /* network interface device a4 */
139 int closing; /* is device closing down? a8 */
140 #ifdef CONFIG_PPP_MULTILINK
141 int nxchan; /* next channel to send something on */
142 u32 nxseq; /* next sequence number to send */
143 int mrru; /* MP: max reconst. receive unit */
144 u32 nextseq; /* MP: seq no of next packet */
145 u32 minseq; /* MP: min of most recent seqnos */
146 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
147 #endif /* CONFIG_PPP_MULTILINK */
148 #ifdef CONFIG_PPP_FILTER
149 struct bpf_prog *pass_filter; /* filter for packets to pass */
150 struct bpf_prog *active_filter; /* filter for pkts to reset idle */
151 #endif /* CONFIG_PPP_FILTER */
152 struct net *ppp_net; /* the net we belong to */
153 struct ppp_link_stats stats64; /* 64 bit network stats */
157 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
158 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
160 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
161 * Bits in xstate: SC_COMP_RUN
163 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
164 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
165 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
168 * Private data structure for each channel.
169 * This includes the data structure used for multilink.
172 struct ppp_file file; /* stuff for read/write/poll */
173 struct list_head list; /* link in all/new_channels list */
174 struct ppp_channel *chan; /* public channel data structure */
175 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
176 spinlock_t downl; /* protects `chan', file.xq dequeue */
177 struct ppp *ppp; /* ppp unit we're connected to */
178 struct net *chan_net; /* the net channel belongs to */
179 struct list_head clist; /* link in list of channels per unit */
180 rwlock_t upl; /* protects `ppp' */
181 #ifdef CONFIG_PPP_MULTILINK
182 u8 avail; /* flag used in multilink stuff */
183 u8 had_frag; /* >= 1 fragments have been sent */
184 u32 lastseq; /* MP: last sequence # received */
185 int speed; /* speed of the corresponding ppp channel*/
186 #endif /* CONFIG_PPP_MULTILINK */
196 * SMP locking issues:
197 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
198 * list and the ppp.n_channels field, you need to take both locks
199 * before you modify them.
200 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
204 static DEFINE_MUTEX(ppp_mutex);
205 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
206 static atomic_t channel_count = ATOMIC_INIT(0);
208 /* per-net private data for this module */
209 static unsigned int ppp_net_id __read_mostly;
211 /* units to ppp mapping */
212 struct idr units_idr;
215 * all_ppp_mutex protects the units_idr mapping.
216 * It also ensures that finding a ppp unit in the units_idr
217 * map and updating its file.refcnt field is atomic.
219 struct mutex all_ppp_mutex;
222 struct list_head all_channels;
223 struct list_head new_channels;
224 int last_channel_index;
227 * all_channels_lock protects all_channels and
228 * last_channel_index, and the atomicity of find
229 * a channel and updating its file.refcnt field.
231 spinlock_t all_channels_lock;
234 /* Get the PPP protocol number from a skb */
235 #define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
237 /* We limit the length of ppp->file.rq to this (arbitrary) value */
238 #define PPP_MAX_RQLEN 32
241 * Maximum number of multilink fragments queued up.
242 * This has to be large enough to cope with the maximum latency of
243 * the slowest channel relative to the others. Strictly it should
244 * depend on the number of channels and their characteristics.
246 #define PPP_MP_MAX_QLEN 128
248 /* Multilink header bits. */
249 #define B 0x80 /* this fragment begins a packet */
250 #define E 0x40 /* this fragment ends a packet */
252 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
253 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
254 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
257 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
258 struct file *file, unsigned int cmd, unsigned long arg);
259 static void ppp_xmit_process(struct ppp *ppp);
260 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
261 static void ppp_push(struct ppp *ppp);
262 static void ppp_channel_push(struct channel *pch);
263 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
264 struct channel *pch);
265 static void ppp_receive_error(struct ppp *ppp);
266 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
267 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
268 struct sk_buff *skb);
269 #ifdef CONFIG_PPP_MULTILINK
270 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
271 struct channel *pch);
272 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
273 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
274 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
275 #endif /* CONFIG_PPP_MULTILINK */
276 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
277 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
278 static void ppp_ccp_closed(struct ppp *ppp);
279 static struct compressor *find_compressor(int type);
280 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
281 static int ppp_create_interface(struct net *net, struct file *file, int *unit);
282 static void init_ppp_file(struct ppp_file *pf, int kind);
283 static void ppp_destroy_interface(struct ppp *ppp);
284 static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
285 static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
286 static int ppp_connect_channel(struct channel *pch, int unit);
287 static int ppp_disconnect_channel(struct channel *pch);
288 static void ppp_destroy_channel(struct channel *pch);
289 static int unit_get(struct idr *p, void *ptr);
290 static int unit_set(struct idr *p, void *ptr, int n);
291 static void unit_put(struct idr *p, int n);
292 static void *unit_find(struct idr *p, int n);
293 static void ppp_setup(struct net_device *dev);
295 static const struct net_device_ops ppp_netdev_ops;
297 static struct class *ppp_class;
299 /* per net-namespace data */
300 static inline struct ppp_net *ppp_pernet(struct net *net)
304 return net_generic(net, ppp_net_id);
307 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
308 static inline int proto_to_npindex(int proto)
327 /* Translates an NP index into a PPP protocol number */
328 static const int npindex_to_proto[NUM_NP] = {
337 /* Translates an ethertype into an NP index */
338 static inline int ethertype_to_npindex(int ethertype)
358 /* Translates an NP index into an ethertype */
359 static const int npindex_to_ethertype[NUM_NP] = {
371 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
372 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
373 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
374 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
375 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
376 ppp_recv_lock(ppp); } while (0)
377 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
378 ppp_xmit_unlock(ppp); } while (0)
381 * /dev/ppp device routines.
382 * The /dev/ppp device is used by pppd to control the ppp unit.
383 * It supports the read, write, ioctl and poll functions.
384 * Open instances of /dev/ppp can be in one of three states:
385 * unattached, attached to a ppp unit, or attached to a ppp channel.
387 static int ppp_open(struct inode *inode, struct file *file)
390 * This could (should?) be enforced by the permissions on /dev/ppp.
392 if (!capable(CAP_NET_ADMIN))
397 static int ppp_release(struct inode *unused, struct file *file)
399 struct ppp_file *pf = file->private_data;
403 file->private_data = NULL;
404 if (pf->kind == INTERFACE) {
407 if (file == ppp->owner)
408 unregister_netdevice(ppp->dev);
411 if (atomic_dec_and_test(&pf->refcnt)) {
414 ppp_destroy_interface(PF_TO_PPP(pf));
417 ppp_destroy_channel(PF_TO_CHANNEL(pf));
425 static ssize_t ppp_read(struct file *file, char __user *buf,
426 size_t count, loff_t *ppos)
428 struct ppp_file *pf = file->private_data;
429 DECLARE_WAITQUEUE(wait, current);
431 struct sk_buff *skb = NULL;
439 add_wait_queue(&pf->rwait, &wait);
441 set_current_state(TASK_INTERRUPTIBLE);
442 skb = skb_dequeue(&pf->rq);
448 if (pf->kind == INTERFACE) {
450 * Return 0 (EOF) on an interface that has no
451 * channels connected, unless it is looping
452 * network traffic (demand mode).
454 struct ppp *ppp = PF_TO_PPP(pf);
457 if (ppp->n_channels == 0 &&
458 (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
459 ppp_recv_unlock(ppp);
462 ppp_recv_unlock(ppp);
465 if (file->f_flags & O_NONBLOCK)
468 if (signal_pending(current))
472 set_current_state(TASK_RUNNING);
473 remove_wait_queue(&pf->rwait, &wait);
479 if (skb->len > count)
484 iov_iter_init(&to, READ, &iov, 1, count);
485 if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
495 static ssize_t ppp_write(struct file *file, const char __user *buf,
496 size_t count, loff_t *ppos)
498 struct ppp_file *pf = file->private_data;
505 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
508 skb_reserve(skb, pf->hdrlen);
510 if (copy_from_user(skb_put(skb, count), buf, count)) {
515 skb_queue_tail(&pf->xq, skb);
519 ppp_xmit_process(PF_TO_PPP(pf));
522 ppp_channel_push(PF_TO_CHANNEL(pf));
532 /* No kernel lock - fine */
533 static unsigned int ppp_poll(struct file *file, poll_table *wait)
535 struct ppp_file *pf = file->private_data;
540 poll_wait(file, &pf->rwait, wait);
541 mask = POLLOUT | POLLWRNORM;
542 if (skb_peek(&pf->rq))
543 mask |= POLLIN | POLLRDNORM;
546 else if (pf->kind == INTERFACE) {
547 /* see comment in ppp_read */
548 struct ppp *ppp = PF_TO_PPP(pf);
551 if (ppp->n_channels == 0 &&
552 (ppp->flags & SC_LOOP_TRAFFIC) == 0)
553 mask |= POLLIN | POLLRDNORM;
554 ppp_recv_unlock(ppp);
560 #ifdef CONFIG_PPP_FILTER
561 static int get_filter(void __user *arg, struct sock_filter **p)
563 struct sock_fprog uprog;
564 struct sock_filter *code = NULL;
567 if (copy_from_user(&uprog, arg, sizeof(uprog)))
575 len = uprog.len * sizeof(struct sock_filter);
576 code = memdup_user(uprog.filter, len);
578 return PTR_ERR(code);
583 #endif /* CONFIG_PPP_FILTER */
585 static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
589 int err = -EFAULT, val, val2, i;
590 struct ppp_idle idle;
593 struct slcompress *vj;
594 void __user *argp = (void __user *)arg;
595 int __user *p = argp;
597 mutex_lock(&ppp_mutex);
599 pf = file->private_data;
601 err = ppp_unattached_ioctl(current->nsproxy->net_ns,
606 if (cmd == PPPIOCDETACH) {
608 * We have to be careful here... if the file descriptor
609 * has been dup'd, we could have another process in the
610 * middle of a poll using the same file *, so we had
611 * better not free the interface data structures -
612 * instead we fail the ioctl. Even in this case, we
613 * shut down the interface if we are the owner of it.
614 * Actually, we should get rid of PPPIOCDETACH, userland
615 * (i.e. pppd) could achieve the same effect by closing
616 * this fd and reopening /dev/ppp.
619 if (pf->kind == INTERFACE) {
622 if (file == ppp->owner)
623 unregister_netdevice(ppp->dev);
626 if (atomic_long_read(&file->f_count) < 2) {
627 ppp_release(NULL, file);
630 pr_warn("PPPIOCDETACH file->f_count=%ld\n",
631 atomic_long_read(&file->f_count));
635 if (pf->kind == CHANNEL) {
637 struct ppp_channel *chan;
639 pch = PF_TO_CHANNEL(pf);
643 if (get_user(unit, p))
645 err = ppp_connect_channel(pch, unit);
649 err = ppp_disconnect_channel(pch);
653 down_read(&pch->chan_sem);
656 if (chan && chan->ops->ioctl)
657 err = chan->ops->ioctl(chan, cmd, arg);
658 up_read(&pch->chan_sem);
663 if (pf->kind != INTERFACE) {
665 pr_err("PPP: not interface or channel??\n");
673 if (get_user(val, p))
680 if (get_user(val, p))
683 cflags = ppp->flags & ~val;
684 #ifdef CONFIG_PPP_MULTILINK
685 if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
688 ppp->flags = val & SC_FLAG_BITS;
690 if (cflags & SC_CCP_OPEN)
696 val = ppp->flags | ppp->xstate | ppp->rstate;
697 if (put_user(val, p))
702 case PPPIOCSCOMPRESS:
703 err = ppp_set_compress(ppp, arg);
707 if (put_user(ppp->file.index, p))
713 if (get_user(val, p))
720 if (put_user(ppp->debug, p))
726 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
727 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
728 if (copy_to_user(argp, &idle, sizeof(idle)))
734 if (get_user(val, p))
737 if ((val >> 16) != 0) {
741 vj = slhc_init(val2+1, val+1);
756 if (copy_from_user(&npi, argp, sizeof(npi)))
758 err = proto_to_npindex(npi.protocol);
762 if (cmd == PPPIOCGNPMODE) {
764 npi.mode = ppp->npmode[i];
765 if (copy_to_user(argp, &npi, sizeof(npi)))
768 ppp->npmode[i] = npi.mode;
769 /* we may be able to transmit more packets now (??) */
770 netif_wake_queue(ppp->dev);
775 #ifdef CONFIG_PPP_FILTER
778 struct sock_filter *code;
780 err = get_filter(argp, &code);
782 struct bpf_prog *pass_filter = NULL;
783 struct sock_fprog_kern fprog = {
790 err = bpf_prog_create(&pass_filter, &fprog);
793 if (ppp->pass_filter)
794 bpf_prog_destroy(ppp->pass_filter);
795 ppp->pass_filter = pass_filter;
804 struct sock_filter *code;
806 err = get_filter(argp, &code);
808 struct bpf_prog *active_filter = NULL;
809 struct sock_fprog_kern fprog = {
816 err = bpf_prog_create(&active_filter, &fprog);
819 if (ppp->active_filter)
820 bpf_prog_destroy(ppp->active_filter);
821 ppp->active_filter = active_filter;
828 #endif /* CONFIG_PPP_FILTER */
830 #ifdef CONFIG_PPP_MULTILINK
832 if (get_user(val, p))
836 ppp_recv_unlock(ppp);
839 #endif /* CONFIG_PPP_MULTILINK */
846 mutex_unlock(&ppp_mutex);
851 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
852 struct file *file, unsigned int cmd, unsigned long arg)
854 int unit, err = -EFAULT;
856 struct channel *chan;
858 int __user *p = (int __user *)arg;
862 /* Create a new ppp unit */
863 if (get_user(unit, p))
865 err = ppp_create_interface(net, file, &unit);
870 if (put_user(unit, p))
876 /* Attach to an existing ppp unit */
877 if (get_user(unit, p))
880 pn = ppp_pernet(net);
881 mutex_lock(&pn->all_ppp_mutex);
882 ppp = ppp_find_unit(pn, unit);
884 atomic_inc(&ppp->file.refcnt);
885 file->private_data = &ppp->file;
888 mutex_unlock(&pn->all_ppp_mutex);
892 if (get_user(unit, p))
895 pn = ppp_pernet(net);
896 spin_lock_bh(&pn->all_channels_lock);
897 chan = ppp_find_channel(pn, unit);
899 atomic_inc(&chan->file.refcnt);
900 file->private_data = &chan->file;
903 spin_unlock_bh(&pn->all_channels_lock);
913 static const struct file_operations ppp_device_fops = {
914 .owner = THIS_MODULE,
918 .unlocked_ioctl = ppp_ioctl,
920 .release = ppp_release,
921 .llseek = noop_llseek,
924 static __net_init int ppp_init_net(struct net *net)
926 struct ppp_net *pn = net_generic(net, ppp_net_id);
928 idr_init(&pn->units_idr);
929 mutex_init(&pn->all_ppp_mutex);
931 INIT_LIST_HEAD(&pn->all_channels);
932 INIT_LIST_HEAD(&pn->new_channels);
934 spin_lock_init(&pn->all_channels_lock);
939 static __net_exit void ppp_exit_net(struct net *net)
941 struct ppp_net *pn = net_generic(net, ppp_net_id);
942 struct net_device *dev;
943 struct net_device *aux;
949 for_each_netdev_safe(net, dev, aux) {
950 if (dev->netdev_ops == &ppp_netdev_ops)
951 unregister_netdevice_queue(dev, &list);
954 idr_for_each_entry(&pn->units_idr, ppp, id)
955 /* Skip devices already unregistered by previous loop */
956 if (!net_eq(dev_net(ppp->dev), net))
957 unregister_netdevice_queue(ppp->dev, &list);
959 unregister_netdevice_many(&list);
962 idr_destroy(&pn->units_idr);
965 static struct pernet_operations ppp_net_ops = {
966 .init = ppp_init_net,
967 .exit = ppp_exit_net,
969 .size = sizeof(struct ppp_net),
972 static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
974 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
977 mutex_lock(&pn->all_ppp_mutex);
980 ret = unit_get(&pn->units_idr, ppp);
984 /* Caller asked for a specific unit number. Fail with -EEXIST
985 * if unavailable. For backward compatibility, return -EEXIST
986 * too if idr allocation fails; this makes pppd retry without
987 * requesting a specific unit number.
989 if (unit_find(&pn->units_idr, unit)) {
993 ret = unit_set(&pn->units_idr, ppp, unit);
995 /* Rewrite error for backward compatibility */
1000 ppp->file.index = ret;
1003 snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
1005 ret = register_netdevice(ppp->dev);
1009 atomic_inc(&ppp_unit_count);
1011 mutex_unlock(&pn->all_ppp_mutex);
1016 unit_put(&pn->units_idr, ppp->file.index);
1018 mutex_unlock(&pn->all_ppp_mutex);
1023 static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1024 const struct ppp_config *conf)
1026 struct ppp *ppp = netdev_priv(dev);
1032 ppp->ppp_net = src_net;
1034 ppp->owner = conf->file;
1036 init_ppp_file(&ppp->file, INTERFACE);
1037 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1039 for (indx = 0; indx < NUM_NP; ++indx)
1040 ppp->npmode[indx] = NPMODE_PASS;
1041 INIT_LIST_HEAD(&ppp->channels);
1042 spin_lock_init(&ppp->rlock);
1043 spin_lock_init(&ppp->wlock);
1045 ppp->xmit_recursion = alloc_percpu(int);
1046 if (!ppp->xmit_recursion) {
1050 for_each_possible_cpu(cpu)
1051 (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
1053 #ifdef CONFIG_PPP_MULTILINK
1055 skb_queue_head_init(&ppp->mrq);
1056 #endif /* CONFIG_PPP_MULTILINK */
1057 #ifdef CONFIG_PPP_FILTER
1058 ppp->pass_filter = NULL;
1059 ppp->active_filter = NULL;
1060 #endif /* CONFIG_PPP_FILTER */
1062 err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
1066 conf->file->private_data = &ppp->file;
1070 free_percpu(ppp->xmit_recursion);
1075 static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1076 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
1079 static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],
1080 struct netlink_ext_ack *extack)
1085 if (!data[IFLA_PPP_DEV_FD])
1087 if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1093 static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1094 struct nlattr *tb[], struct nlattr *data[],
1095 struct netlink_ext_ack *extack)
1097 struct ppp_config conf = {
1099 .ifname_is_set = true,
1104 file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1108 /* rtnl_lock is already held here, but ppp_create_interface() locks
1109 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1110 * possible deadlock due to lock order inversion, at the cost of
1111 * pushing the problem back to userspace.
1113 if (!mutex_trylock(&ppp_mutex)) {
1118 if (file->f_op != &ppp_device_fops || file->private_data) {
1125 /* Don't use device name generated by the rtnetlink layer when ifname
1126 * isn't specified. Let ppp_dev_configure() set the device name using
1127 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1128 * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1130 if (!tb[IFLA_IFNAME])
1131 conf.ifname_is_set = false;
1133 err = ppp_dev_configure(src_net, dev, &conf);
1136 mutex_unlock(&ppp_mutex);
1143 static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1145 unregister_netdevice_queue(dev, head);
1148 static size_t ppp_nl_get_size(const struct net_device *dev)
1153 static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1158 static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1160 struct ppp *ppp = netdev_priv(dev);
1162 return ppp->ppp_net;
1165 static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1167 .maxtype = IFLA_PPP_MAX,
1168 .policy = ppp_nl_policy,
1169 .priv_size = sizeof(struct ppp),
1171 .validate = ppp_nl_validate,
1172 .newlink = ppp_nl_newlink,
1173 .dellink = ppp_nl_dellink,
1174 .get_size = ppp_nl_get_size,
1175 .fill_info = ppp_nl_fill_info,
1176 .get_link_net = ppp_nl_get_link_net,
1179 #define PPP_MAJOR 108
1181 /* Called at boot time if ppp is compiled into the kernel,
1182 or at module load time (from init_module) if compiled as a module. */
1183 static int __init ppp_init(void)
1187 pr_info("PPP generic driver version " PPP_VERSION "\n");
1189 err = register_pernet_device(&ppp_net_ops);
1191 pr_err("failed to register PPP pernet device (%d)\n", err);
1195 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1197 pr_err("failed to register PPP device (%d)\n", err);
1201 ppp_class = class_create(THIS_MODULE, "ppp");
1202 if (IS_ERR(ppp_class)) {
1203 err = PTR_ERR(ppp_class);
1207 err = rtnl_link_register(&ppp_link_ops);
1209 pr_err("failed to register rtnetlink PPP handler\n");
1213 /* not a big deal if we fail here :-) */
1214 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1219 class_destroy(ppp_class);
1221 unregister_chrdev(PPP_MAJOR, "ppp");
1223 unregister_pernet_device(&ppp_net_ops);
1229 * Network interface unit routines.
1232 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1234 struct ppp *ppp = netdev_priv(dev);
1238 npi = ethertype_to_npindex(ntohs(skb->protocol));
1242 /* Drop, accept or reject the packet */
1243 switch (ppp->npmode[npi]) {
1247 /* it would be nice to have a way to tell the network
1248 system to queue this one up for later. */
1255 /* Put the 2-byte PPP protocol number on the front,
1256 making sure there is room for the address and control fields. */
1257 if (skb_cow_head(skb, PPP_HDRLEN))
1260 pp = skb_push(skb, 2);
1261 proto = npindex_to_proto[npi];
1262 put_unaligned_be16(proto, pp);
1264 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
1265 skb_queue_tail(&ppp->file.xq, skb);
1266 ppp_xmit_process(ppp);
1267 return NETDEV_TX_OK;
1271 ++dev->stats.tx_dropped;
1272 return NETDEV_TX_OK;
1276 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1278 struct ppp *ppp = netdev_priv(dev);
1280 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1281 struct ppp_stats stats;
1282 struct ppp_comp_stats cstats;
1287 ppp_get_stats(ppp, &stats);
1288 if (copy_to_user(addr, &stats, sizeof(stats)))
1293 case SIOCGPPPCSTATS:
1294 memset(&cstats, 0, sizeof(cstats));
1296 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
1298 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1299 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1306 if (copy_to_user(addr, vers, strlen(vers) + 1))
1319 ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1321 struct ppp *ppp = netdev_priv(dev);
1324 stats64->rx_packets = ppp->stats64.rx_packets;
1325 stats64->rx_bytes = ppp->stats64.rx_bytes;
1326 ppp_recv_unlock(ppp);
1329 stats64->tx_packets = ppp->stats64.tx_packets;
1330 stats64->tx_bytes = ppp->stats64.tx_bytes;
1331 ppp_xmit_unlock(ppp);
1333 stats64->rx_errors = dev->stats.rx_errors;
1334 stats64->tx_errors = dev->stats.tx_errors;
1335 stats64->rx_dropped = dev->stats.rx_dropped;
1336 stats64->tx_dropped = dev->stats.tx_dropped;
1337 stats64->rx_length_errors = dev->stats.rx_length_errors;
1340 static int ppp_dev_init(struct net_device *dev)
1342 netdev_lockdep_set_classes(dev);
1346 static void ppp_dev_uninit(struct net_device *dev)
1348 struct ppp *ppp = netdev_priv(dev);
1349 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1355 mutex_lock(&pn->all_ppp_mutex);
1356 unit_put(&pn->units_idr, ppp->file.index);
1357 mutex_unlock(&pn->all_ppp_mutex);
1362 wake_up_interruptible(&ppp->file.rwait);
1365 static const struct net_device_ops ppp_netdev_ops = {
1366 .ndo_init = ppp_dev_init,
1367 .ndo_uninit = ppp_dev_uninit,
1368 .ndo_start_xmit = ppp_start_xmit,
1369 .ndo_do_ioctl = ppp_net_ioctl,
1370 .ndo_get_stats64 = ppp_get_stats64,
1373 static struct device_type ppp_type = {
1377 static void ppp_setup(struct net_device *dev)
1379 dev->netdev_ops = &ppp_netdev_ops;
1380 SET_NETDEV_DEVTYPE(dev, &ppp_type);
1382 dev->features |= NETIF_F_LLTX;
1384 dev->hard_header_len = PPP_HDRLEN;
1387 dev->tx_queue_len = 3;
1388 dev->type = ARPHRD_PPP;
1389 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1390 netif_keep_dst(dev);
1394 * Transmit-side routines.
1397 /* Called to do any work queued up on the transmit side that can now be done */
1398 static void __ppp_xmit_process(struct ppp *ppp)
1400 struct sk_buff *skb;
1403 if (!ppp->closing) {
1405 while (!ppp->xmit_pending &&
1406 (skb = skb_dequeue(&ppp->file.xq)))
1407 ppp_send_frame(ppp, skb);
1408 /* If there's no work left to do, tell the core net
1409 code that we can accept some more. */
1410 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
1411 netif_wake_queue(ppp->dev);
1413 netif_stop_queue(ppp->dev);
1415 ppp_xmit_unlock(ppp);
1418 static void ppp_xmit_process(struct ppp *ppp)
1422 if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
1425 (*this_cpu_ptr(ppp->xmit_recursion))++;
1426 __ppp_xmit_process(ppp);
1427 (*this_cpu_ptr(ppp->xmit_recursion))--;
1436 if (net_ratelimit())
1437 netdev_err(ppp->dev, "recursion detected\n");
1440 static inline struct sk_buff *
1441 pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1443 struct sk_buff *new_skb;
1445 int new_skb_size = ppp->dev->mtu +
1446 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1447 int compressor_skb_size = ppp->dev->mtu +
1448 ppp->xcomp->comp_extra + PPP_HDRLEN;
1449 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1451 if (net_ratelimit())
1452 netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
1455 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1456 skb_reserve(new_skb,
1457 ppp->dev->hard_header_len - PPP_HDRLEN);
1459 /* compressor still expects A/C bytes in hdr */
1460 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1461 new_skb->data, skb->len + 2,
1462 compressor_skb_size);
1463 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1467 skb_pull(skb, 2); /* pull off A/C bytes */
1468 } else if (len == 0) {
1469 /* didn't compress, or CCP not up yet */
1470 consume_skb(new_skb);
1475 * MPPE requires that we do not send unencrypted
1476 * frames. The compressor will return -1 if we
1477 * should drop the frame. We cannot simply test
1478 * the compress_proto because MPPE and MPPC share
1481 if (net_ratelimit())
1482 netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
1484 consume_skb(new_skb);
1491 * Compress and send a frame.
1492 * The caller should have locked the xmit path,
1493 * and xmit_pending should be 0.
1496 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1498 int proto = PPP_PROTO(skb);
1499 struct sk_buff *new_skb;
1503 if (proto < 0x8000) {
1504 #ifdef CONFIG_PPP_FILTER
1505 /* check if we should pass this packet */
1506 /* the filter instructions are constructed assuming
1507 a four-byte PPP header on each packet */
1508 *(u8 *)skb_push(skb, 2) = 1;
1509 if (ppp->pass_filter &&
1510 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
1512 netdev_printk(KERN_DEBUG, ppp->dev,
1513 "PPP: outbound frame "
1518 /* if this packet passes the active filter, record the time */
1519 if (!(ppp->active_filter &&
1520 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
1521 ppp->last_xmit = jiffies;
1524 /* for data packets, record the time */
1525 ppp->last_xmit = jiffies;
1526 #endif /* CONFIG_PPP_FILTER */
1529 ++ppp->stats64.tx_packets;
1530 ppp->stats64.tx_bytes += skb->len - 2;
1534 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
1536 /* try to do VJ TCP header compression */
1537 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1540 netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
1543 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1545 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1546 new_skb->data + 2, &cp,
1547 !(ppp->flags & SC_NO_TCP_CCID));
1548 if (cp == skb->data + 2) {
1549 /* didn't compress */
1550 consume_skb(new_skb);
1552 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1553 proto = PPP_VJC_COMP;
1554 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1556 proto = PPP_VJC_UNCOMP;
1557 cp[0] = skb->data[2];
1561 cp = skb_put(skb, len + 2);
1568 /* peek at outbound CCP frames */
1569 ppp_ccp_peek(ppp, skb, 0);
1573 /* try to do packet compression */
1574 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1575 proto != PPP_LCP && proto != PPP_CCP) {
1576 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1577 if (net_ratelimit())
1578 netdev_err(ppp->dev,
1579 "ppp: compression required but "
1580 "down - pkt dropped.\n");
1583 skb = pad_compress_skb(ppp, skb);
1589 * If we are waiting for traffic (demand dialling),
1590 * queue it up for pppd to receive.
1592 if (ppp->flags & SC_LOOP_TRAFFIC) {
1593 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1595 skb_queue_tail(&ppp->file.rq, skb);
1596 wake_up_interruptible(&ppp->file.rwait);
1600 ppp->xmit_pending = skb;
1606 ++ppp->dev->stats.tx_errors;
1610 * Try to send the frame in xmit_pending.
1611 * The caller should have the xmit path locked.
1614 ppp_push(struct ppp *ppp)
1616 struct list_head *list;
1617 struct channel *pch;
1618 struct sk_buff *skb = ppp->xmit_pending;
1623 list = &ppp->channels;
1624 if (list_empty(list)) {
1625 /* nowhere to send the packet, just drop it */
1626 ppp->xmit_pending = NULL;
1631 if ((ppp->flags & SC_MULTILINK) == 0) {
1632 /* not doing multilink: send it down the first channel */
1634 pch = list_entry(list, struct channel, clist);
1636 spin_lock(&pch->downl);
1638 if (pch->chan->ops->start_xmit(pch->chan, skb))
1639 ppp->xmit_pending = NULL;
1641 /* channel got unregistered */
1643 ppp->xmit_pending = NULL;
1645 spin_unlock(&pch->downl);
1649 #ifdef CONFIG_PPP_MULTILINK
1650 /* Multilink: fragment the packet over as many links
1651 as can take the packet at the moment. */
1652 if (!ppp_mp_explode(ppp, skb))
1654 #endif /* CONFIG_PPP_MULTILINK */
1656 ppp->xmit_pending = NULL;
1660 #ifdef CONFIG_PPP_MULTILINK
1661 static bool mp_protocol_compress __read_mostly = true;
1662 module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
1663 MODULE_PARM_DESC(mp_protocol_compress,
1664 "compress protocol id in multilink fragments");
1667 * Divide a packet to be transmitted into fragments and
1668 * send them out the individual links.
1670 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1673 int i, bits, hdrlen, mtu;
1675 int navail, nfree, nzero;
1679 unsigned char *p, *q;
1680 struct list_head *list;
1681 struct channel *pch;
1682 struct sk_buff *frag;
1683 struct ppp_channel *chan;
1685 totspeed = 0; /*total bitrate of the bundle*/
1686 nfree = 0; /* # channels which have no packet already queued */
1687 navail = 0; /* total # of usable channels (not deregistered) */
1688 nzero = 0; /* number of channels with zero speed associated*/
1689 totfree = 0; /*total # of channels available and
1690 *having no queued packets before
1691 *starting the fragmentation*/
1693 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1695 list_for_each_entry(pch, &ppp->channels, clist) {
1699 pch->speed = pch->chan->speed;
1704 if (skb_queue_empty(&pch->file.xq) ||
1706 if (pch->speed == 0)
1709 totspeed += pch->speed;
1715 if (!pch->had_frag && i < ppp->nxchan)
1721 * Don't start sending this packet unless at least half of
1722 * the channels are free. This gives much better TCP
1723 * performance if we have a lot of channels.
1725 if (nfree == 0 || nfree < navail / 2)
1726 return 0; /* can't take now, leave it in xmit_pending */
1728 /* Do protocol field compression */
1731 if (*p == 0 && mp_protocol_compress) {
1737 nbigger = len % nfree;
1739 /* skip to the channel after the one we last used
1740 and start at that one */
1741 list = &ppp->channels;
1742 for (i = 0; i < ppp->nxchan; ++i) {
1744 if (list == &ppp->channels) {
1750 /* create a fragment for each channel */
1754 if (list == &ppp->channels) {
1758 pch = list_entry(list, struct channel, clist);
1764 * Skip this channel if it has a fragment pending already and
1765 * we haven't given a fragment to all of the free channels.
1767 if (pch->avail == 1) {
1774 /* check the channel's mtu and whether it is still attached. */
1775 spin_lock(&pch->downl);
1776 if (pch->chan == NULL) {
1777 /* can't use this channel, it's being deregistered */
1778 if (pch->speed == 0)
1781 totspeed -= pch->speed;
1783 spin_unlock(&pch->downl);
1794 *if the channel speed is not set divide
1795 *the packet evenly among the free channels;
1796 *otherwise divide it according to the speed
1797 *of the channel we are going to transmit on
1801 if (pch->speed == 0) {
1808 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1809 ((totspeed*totfree)/pch->speed)) - hdrlen;
1811 flen += ((totfree - nzero)*pch->speed)/totspeed;
1812 nbigger -= ((totfree - nzero)*pch->speed)/
1820 *check if we are on the last channel or
1821 *we exceded the length of the data to
1824 if ((nfree <= 0) || (flen > len))
1827 *it is not worth to tx on slow channels:
1828 *in that case from the resulting flen according to the
1829 *above formula will be equal or less than zero.
1830 *Skip the channel in this case
1834 spin_unlock(&pch->downl);
1839 * hdrlen includes the 2-byte PPP protocol field, but the
1840 * MTU counts only the payload excluding the protocol field.
1841 * (RFC1661 Section 2)
1843 mtu = pch->chan->mtu - (hdrlen - 2);
1850 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
1853 q = skb_put(frag, flen + hdrlen);
1855 /* make the MP header */
1856 put_unaligned_be16(PPP_MP, q);
1857 if (ppp->flags & SC_MP_XSHORTSEQ) {
1858 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1862 q[3] = ppp->nxseq >> 16;
1863 q[4] = ppp->nxseq >> 8;
1867 memcpy(q + hdrlen, p, flen);
1869 /* try to send it down the channel */
1871 if (!skb_queue_empty(&pch->file.xq) ||
1872 !chan->ops->start_xmit(chan, frag))
1873 skb_queue_tail(&pch->file.xq, frag);
1879 spin_unlock(&pch->downl);
1886 spin_unlock(&pch->downl);
1888 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
1889 ++ppp->dev->stats.tx_errors;
1891 return 1; /* abandon the frame */
1893 #endif /* CONFIG_PPP_MULTILINK */
1895 /* Try to send data out on a channel */
1896 static void __ppp_channel_push(struct channel *pch)
1898 struct sk_buff *skb;
1901 spin_lock(&pch->downl);
1903 while (!skb_queue_empty(&pch->file.xq)) {
1904 skb = skb_dequeue(&pch->file.xq);
1905 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1906 /* put the packet back and try again later */
1907 skb_queue_head(&pch->file.xq, skb);
1912 /* channel got deregistered */
1913 skb_queue_purge(&pch->file.xq);
1915 spin_unlock(&pch->downl);
1916 /* see if there is anything from the attached unit to be sent */
1917 if (skb_queue_empty(&pch->file.xq)) {
1920 __ppp_xmit_process(ppp);
1924 static void ppp_channel_push(struct channel *pch)
1926 read_lock_bh(&pch->upl);
1928 (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
1929 __ppp_channel_push(pch);
1930 (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
1932 __ppp_channel_push(pch);
1934 read_unlock_bh(&pch->upl);
1938 * Receive-side routines.
1941 struct ppp_mp_skb_parm {
1945 #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
1948 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1952 ppp_receive_frame(ppp, skb, pch);
1955 ppp_recv_unlock(ppp);
1959 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1961 struct channel *pch = chan->ppp;
1969 read_lock_bh(&pch->upl);
1970 if (!pskb_may_pull(skb, 2)) {
1973 ++pch->ppp->dev->stats.rx_length_errors;
1974 ppp_receive_error(pch->ppp);
1979 proto = PPP_PROTO(skb);
1980 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1981 /* put it on the channel queue */
1982 skb_queue_tail(&pch->file.rq, skb);
1983 /* drop old frames if queue too long */
1984 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
1985 (skb = skb_dequeue(&pch->file.rq)))
1987 wake_up_interruptible(&pch->file.rwait);
1989 ppp_do_recv(pch->ppp, skb, pch);
1993 read_unlock_bh(&pch->upl);
1996 /* Put a 0-length skb in the receive queue as an error indication */
1998 ppp_input_error(struct ppp_channel *chan, int code)
2000 struct channel *pch = chan->ppp;
2001 struct sk_buff *skb;
2006 read_lock_bh(&pch->upl);
2008 skb = alloc_skb(0, GFP_ATOMIC);
2010 skb->len = 0; /* probably unnecessary */
2012 ppp_do_recv(pch->ppp, skb, pch);
2015 read_unlock_bh(&pch->upl);
2019 * We come in here to process a received frame.
2020 * The receive side of the ppp unit is locked.
2023 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2025 /* note: a 0-length skb is used as an error indication */
2027 skb_checksum_complete_unset(skb);
2028 #ifdef CONFIG_PPP_MULTILINK
2029 /* XXX do channel-level decompression here */
2030 if (PPP_PROTO(skb) == PPP_MP)
2031 ppp_receive_mp_frame(ppp, skb, pch);
2033 #endif /* CONFIG_PPP_MULTILINK */
2034 ppp_receive_nonmp_frame(ppp, skb);
2037 ppp_receive_error(ppp);
2042 ppp_receive_error(struct ppp *ppp)
2044 ++ppp->dev->stats.rx_errors;
2050 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2053 int proto, len, npi;
2056 * Decompress the frame, if compressed.
2057 * Note that some decompressors need to see uncompressed frames
2058 * that come in as well as compressed frames.
2060 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2061 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
2062 skb = ppp_decompress_frame(ppp, skb);
2064 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2067 proto = PPP_PROTO(skb);
2070 /* decompress VJ compressed packets */
2071 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2074 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
2075 /* copy to a new sk_buff with more tailroom */
2076 ns = dev_alloc_skb(skb->len + 128);
2078 netdev_err(ppp->dev, "PPP: no memory "
2083 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
2088 skb->ip_summed = CHECKSUM_NONE;
2090 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2092 netdev_printk(KERN_DEBUG, ppp->dev,
2093 "PPP: VJ decompression error\n");
2098 skb_put(skb, len - skb->len);
2099 else if (len < skb->len)
2104 case PPP_VJC_UNCOMP:
2105 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2108 /* Until we fix the decompressor need to make sure
2109 * data portion is linear.
2111 if (!pskb_may_pull(skb, skb->len))
2114 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
2115 netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
2122 ppp_ccp_peek(ppp, skb, 1);
2126 ++ppp->stats64.rx_packets;
2127 ppp->stats64.rx_bytes += skb->len - 2;
2129 npi = proto_to_npindex(proto);
2131 /* control or unknown frame - pass it to pppd */
2132 skb_queue_tail(&ppp->file.rq, skb);
2133 /* limit queue length by dropping old frames */
2134 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2135 (skb = skb_dequeue(&ppp->file.rq)))
2137 /* wake up any process polling or blocking on read */
2138 wake_up_interruptible(&ppp->file.rwait);
2141 /* network protocol frame - give it to the kernel */
2143 #ifdef CONFIG_PPP_FILTER
2144 /* check if the packet passes the pass and active filters */
2145 /* the filter instructions are constructed assuming
2146 a four-byte PPP header on each packet */
2147 if (ppp->pass_filter || ppp->active_filter) {
2148 if (skb_unclone(skb, GFP_ATOMIC))
2151 *(u8 *)skb_push(skb, 2) = 0;
2152 if (ppp->pass_filter &&
2153 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
2155 netdev_printk(KERN_DEBUG, ppp->dev,
2156 "PPP: inbound frame "
2161 if (!(ppp->active_filter &&
2162 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
2163 ppp->last_recv = jiffies;
2166 #endif /* CONFIG_PPP_FILTER */
2167 ppp->last_recv = jiffies;
2169 if ((ppp->dev->flags & IFF_UP) == 0 ||
2170 ppp->npmode[npi] != NPMODE_PASS) {
2173 /* chop off protocol */
2174 skb_pull_rcsum(skb, 2);
2175 skb->dev = ppp->dev;
2176 skb->protocol = htons(npindex_to_ethertype[npi]);
2177 skb_reset_mac_header(skb);
2178 skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2179 dev_net(ppp->dev)));
2187 ppp_receive_error(ppp);
2190 static struct sk_buff *
2191 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2193 int proto = PPP_PROTO(skb);
2197 /* Until we fix all the decompressor's need to make sure
2198 * data portion is linear.
2200 if (!pskb_may_pull(skb, skb->len))
2203 if (proto == PPP_COMP) {
2206 switch(ppp->rcomp->compress_proto) {
2208 obuff_size = ppp->mru + PPP_HDRLEN + 1;
2211 obuff_size = ppp->mru + PPP_HDRLEN;
2215 ns = dev_alloc_skb(obuff_size);
2217 netdev_err(ppp->dev, "ppp_decompress_frame: "
2221 /* the decompressor still expects the A/C bytes in the hdr */
2222 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
2223 skb->len + 2, ns->data, obuff_size);
2225 /* Pass the compressed frame to pppd as an
2226 error indication. */
2227 if (len == DECOMP_FATALERROR)
2228 ppp->rstate |= SC_DC_FERROR;
2236 skb_pull(skb, 2); /* pull off the A/C bytes */
2239 /* Uncompressed frame - pass to decompressor so it
2240 can update its dictionary if necessary. */
2241 if (ppp->rcomp->incomp)
2242 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2249 ppp->rstate |= SC_DC_ERROR;
2250 ppp_receive_error(ppp);
2254 #ifdef CONFIG_PPP_MULTILINK
2256 * Receive a multilink frame.
2257 * We put it on the reconstruction queue and then pull off
2258 * as many completed frames as we can.
2261 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2265 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2267 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
2268 goto err; /* no good, throw it away */
2270 /* Decode sequence number and begin/end bits */
2271 if (ppp->flags & SC_MP_SHORTSEQ) {
2272 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2275 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2278 PPP_MP_CB(skb)->BEbits = skb->data[2];
2279 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
2282 * Do protocol ID decompression on the first fragment of each packet.
2284 if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
2285 *(u8 *)skb_push(skb, 1) = 0;
2288 * Expand sequence number to 32 bits, making it as close
2289 * as possible to ppp->minseq.
2291 seq |= ppp->minseq & ~mask;
2292 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2294 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2295 seq -= mask + 1; /* should never happen */
2296 PPP_MP_CB(skb)->sequence = seq;
2300 * If this packet comes before the next one we were expecting,
2303 if (seq_before(seq, ppp->nextseq)) {
2305 ++ppp->dev->stats.rx_dropped;
2306 ppp_receive_error(ppp);
2311 * Reevaluate minseq, the minimum over all channels of the
2312 * last sequence number received on each channel. Because of
2313 * the increasing sequence number rule, we know that any fragment
2314 * before `minseq' which hasn't arrived is never going to arrive.
2315 * The list of channels can't change because we have the receive
2316 * side of the ppp unit locked.
2318 list_for_each_entry(ch, &ppp->channels, clist) {
2319 if (seq_before(ch->lastseq, seq))
2322 if (seq_before(ppp->minseq, seq))
2325 /* Put the fragment on the reconstruction queue */
2326 ppp_mp_insert(ppp, skb);
2328 /* If the queue is getting long, don't wait any longer for packets
2329 before the start of the queue. */
2330 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
2331 struct sk_buff *mskb = skb_peek(&ppp->mrq);
2332 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2333 ppp->minseq = PPP_MP_CB(mskb)->sequence;
2336 /* Pull completed packets off the queue and receive them. */
2337 while ((skb = ppp_mp_reconstruct(ppp))) {
2338 if (pskb_may_pull(skb, 2))
2339 ppp_receive_nonmp_frame(ppp, skb);
2341 ++ppp->dev->stats.rx_length_errors;
2343 ppp_receive_error(ppp);
2351 ppp_receive_error(ppp);
2355 * Insert a fragment on the MP reconstruction queue.
2356 * The queue is ordered by increasing sequence number.
2359 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2362 struct sk_buff_head *list = &ppp->mrq;
2363 u32 seq = PPP_MP_CB(skb)->sequence;
2365 /* N.B. we don't need to lock the list lock because we have the
2366 ppp unit receive-side lock. */
2367 skb_queue_walk(list, p) {
2368 if (seq_before(seq, PPP_MP_CB(p)->sequence))
2371 __skb_queue_before(list, p, skb);
2375 * Reconstruct a packet from the MP fragment queue.
2376 * We go through increasing sequence numbers until we find a
2377 * complete packet, or we get to the sequence number for a fragment
2378 * which hasn't arrived but might still do so.
2380 static struct sk_buff *
2381 ppp_mp_reconstruct(struct ppp *ppp)
2383 u32 seq = ppp->nextseq;
2384 u32 minseq = ppp->minseq;
2385 struct sk_buff_head *list = &ppp->mrq;
2386 struct sk_buff *p, *tmp;
2387 struct sk_buff *head, *tail;
2388 struct sk_buff *skb = NULL;
2389 int lost = 0, len = 0;
2391 if (ppp->mrru == 0) /* do nothing until mrru is set */
2395 skb_queue_walk_safe(list, p, tmp) {
2397 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
2398 /* this can't happen, anyway ignore the skb */
2399 netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2401 PPP_MP_CB(p)->sequence, seq);
2402 __skb_unlink(p, list);
2406 if (PPP_MP_CB(p)->sequence != seq) {
2408 /* Fragment `seq' is missing. If it is after
2409 minseq, it might arrive later, so stop here. */
2410 if (seq_after(seq, minseq))
2412 /* Fragment `seq' is lost, keep going. */
2415 seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2416 minseq + 1: PPP_MP_CB(p)->sequence;
2419 netdev_printk(KERN_DEBUG, ppp->dev,
2420 "lost frag %u..%u\n",
2427 * At this point we know that all the fragments from
2428 * ppp->nextseq to seq are either present or lost.
2429 * Also, there are no complete packets in the queue
2430 * that have no missing fragments and end before this
2434 /* B bit set indicates this fragment starts a packet */
2435 if (PPP_MP_CB(p)->BEbits & B) {
2443 /* Got a complete packet yet? */
2444 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2445 (PPP_MP_CB(head)->BEbits & B)) {
2446 if (len > ppp->mrru + 2) {
2447 ++ppp->dev->stats.rx_length_errors;
2448 netdev_printk(KERN_DEBUG, ppp->dev,
2449 "PPP: reconstructed packet"
2450 " is too long (%d)\n", len);
2455 ppp->nextseq = seq + 1;
2459 * If this is the ending fragment of a packet,
2460 * and we haven't found a complete valid packet yet,
2461 * we can discard up to and including this fragment.
2463 if (PPP_MP_CB(p)->BEbits & E) {
2464 struct sk_buff *tmp2;
2466 skb_queue_reverse_walk_from_safe(list, p, tmp2) {
2468 netdev_printk(KERN_DEBUG, ppp->dev,
2469 "discarding frag %u\n",
2470 PPP_MP_CB(p)->sequence);
2471 __skb_unlink(p, list);
2474 head = skb_peek(list);
2481 /* If we have a complete packet, copy it all into one skb. */
2483 /* If we have discarded any fragments,
2484 signal a receive error. */
2485 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
2486 skb_queue_walk_safe(list, p, tmp) {
2490 netdev_printk(KERN_DEBUG, ppp->dev,
2491 "discarding frag %u\n",
2492 PPP_MP_CB(p)->sequence);
2493 __skb_unlink(p, list);
2498 netdev_printk(KERN_DEBUG, ppp->dev,
2499 " missed pkts %u..%u\n",
2501 PPP_MP_CB(head)->sequence-1);
2502 ++ppp->dev->stats.rx_dropped;
2503 ppp_receive_error(ppp);
2508 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2509 p = skb_queue_next(list, head);
2510 __skb_unlink(skb, list);
2511 skb_queue_walk_from_safe(list, p, tmp) {
2512 __skb_unlink(p, list);
2518 skb->data_len += p->len;
2519 skb->truesize += p->truesize;
2525 __skb_unlink(skb, list);
2528 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
2533 #endif /* CONFIG_PPP_MULTILINK */
2536 * Channel interface.
2539 /* Create a new, unattached ppp channel. */
2540 int ppp_register_channel(struct ppp_channel *chan)
2542 return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2545 /* Create a new, unattached ppp channel for specified net. */
2546 int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
2548 struct channel *pch;
2551 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
2555 pn = ppp_pernet(net);
2559 pch->chan_net = get_net(net);
2561 init_ppp_file(&pch->file, CHANNEL);
2562 pch->file.hdrlen = chan->hdrlen;
2563 #ifdef CONFIG_PPP_MULTILINK
2565 #endif /* CONFIG_PPP_MULTILINK */
2566 init_rwsem(&pch->chan_sem);
2567 spin_lock_init(&pch->downl);
2568 rwlock_init(&pch->upl);
2570 spin_lock_bh(&pn->all_channels_lock);
2571 pch->file.index = ++pn->last_channel_index;
2572 list_add(&pch->list, &pn->new_channels);
2573 atomic_inc(&channel_count);
2574 spin_unlock_bh(&pn->all_channels_lock);
2580 * Return the index of a channel.
2582 int ppp_channel_index(struct ppp_channel *chan)
2584 struct channel *pch = chan->ppp;
2587 return pch->file.index;
2592 * Return the PPP unit number to which a channel is connected.
2594 int ppp_unit_number(struct ppp_channel *chan)
2596 struct channel *pch = chan->ppp;
2600 read_lock_bh(&pch->upl);
2602 unit = pch->ppp->file.index;
2603 read_unlock_bh(&pch->upl);
2609 * Return the PPP device interface name of a channel.
2611 char *ppp_dev_name(struct ppp_channel *chan)
2613 struct channel *pch = chan->ppp;
2617 read_lock_bh(&pch->upl);
2618 if (pch->ppp && pch->ppp->dev)
2619 name = pch->ppp->dev->name;
2620 read_unlock_bh(&pch->upl);
2627 * Disconnect a channel from the generic layer.
2628 * This must be called in process context.
2631 ppp_unregister_channel(struct ppp_channel *chan)
2633 struct channel *pch = chan->ppp;
2637 return; /* should never happen */
2642 * This ensures that we have returned from any calls into the
2643 * the channel's start_xmit or ioctl routine before we proceed.
2645 down_write(&pch->chan_sem);
2646 spin_lock_bh(&pch->downl);
2648 spin_unlock_bh(&pch->downl);
2649 up_write(&pch->chan_sem);
2650 ppp_disconnect_channel(pch);
2652 pn = ppp_pernet(pch->chan_net);
2653 spin_lock_bh(&pn->all_channels_lock);
2654 list_del(&pch->list);
2655 spin_unlock_bh(&pn->all_channels_lock);
2658 wake_up_interruptible(&pch->file.rwait);
2659 if (atomic_dec_and_test(&pch->file.refcnt))
2660 ppp_destroy_channel(pch);
2664 * Callback from a channel when it can accept more to transmit.
2665 * This should be called at BH/softirq level, not interrupt level.
2668 ppp_output_wakeup(struct ppp_channel *chan)
2670 struct channel *pch = chan->ppp;
2674 ppp_channel_push(pch);
2678 * Compression control.
2681 /* Process the PPPIOCSCOMPRESS ioctl. */
2683 ppp_set_compress(struct ppp *ppp, unsigned long arg)
2686 struct compressor *cp, *ocomp;
2687 struct ppp_option_data data;
2688 void *state, *ostate;
2689 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2692 if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
2694 if (data.length > CCP_MAX_OPTION_LENGTH)
2696 if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
2700 if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
2703 cp = try_then_request_module(
2704 find_compressor(ccp_option[0]),
2705 "ppp-compress-%d", ccp_option[0]);
2710 if (data.transmit) {
2711 state = cp->comp_alloc(ccp_option, data.length);
2714 ppp->xstate &= ~SC_COMP_RUN;
2716 ostate = ppp->xc_state;
2718 ppp->xc_state = state;
2719 ppp_xmit_unlock(ppp);
2721 ocomp->comp_free(ostate);
2722 module_put(ocomp->owner);
2726 module_put(cp->owner);
2729 state = cp->decomp_alloc(ccp_option, data.length);
2732 ppp->rstate &= ~SC_DECOMP_RUN;
2734 ostate = ppp->rc_state;
2736 ppp->rc_state = state;
2737 ppp_recv_unlock(ppp);
2739 ocomp->decomp_free(ostate);
2740 module_put(ocomp->owner);
2744 module_put(cp->owner);
2752 * Look at a CCP packet and update our state accordingly.
2753 * We assume the caller has the xmit or recv path locked.
2756 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2761 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2762 return; /* no header */
2765 switch (CCP_CODE(dp)) {
2768 /* A ConfReq starts negotiation of compression
2769 * in one direction of transmission,
2770 * and hence brings it down...but which way?
2773 * A ConfReq indicates what the sender would like to receive
2776 /* He is proposing what I should send */
2777 ppp->xstate &= ~SC_COMP_RUN;
2779 /* I am proposing to what he should send */
2780 ppp->rstate &= ~SC_DECOMP_RUN;
2787 * CCP is going down, both directions of transmission
2789 ppp->rstate &= ~SC_DECOMP_RUN;
2790 ppp->xstate &= ~SC_COMP_RUN;
2794 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2796 len = CCP_LENGTH(dp);
2797 if (!pskb_may_pull(skb, len + 2))
2798 return; /* too short */
2801 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2804 /* we will start receiving compressed packets */
2807 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2808 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2809 ppp->rstate |= SC_DECOMP_RUN;
2810 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2813 /* we will soon start sending compressed packets */
2816 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2817 ppp->file.index, 0, ppp->debug))
2818 ppp->xstate |= SC_COMP_RUN;
2823 /* reset the [de]compressor */
2824 if ((ppp->flags & SC_CCP_UP) == 0)
2827 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2828 ppp->rcomp->decomp_reset(ppp->rc_state);
2829 ppp->rstate &= ~SC_DC_ERROR;
2832 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2833 ppp->xcomp->comp_reset(ppp->xc_state);
2839 /* Free up compression resources. */
2841 ppp_ccp_closed(struct ppp *ppp)
2843 void *xstate, *rstate;
2844 struct compressor *xcomp, *rcomp;
2847 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2850 xstate = ppp->xc_state;
2851 ppp->xc_state = NULL;
2854 rstate = ppp->rc_state;
2855 ppp->rc_state = NULL;
2859 xcomp->comp_free(xstate);
2860 module_put(xcomp->owner);
2863 rcomp->decomp_free(rstate);
2864 module_put(rcomp->owner);
2868 /* List of compressors. */
2869 static LIST_HEAD(compressor_list);
2870 static DEFINE_SPINLOCK(compressor_list_lock);
2872 struct compressor_entry {
2873 struct list_head list;
2874 struct compressor *comp;
2877 static struct compressor_entry *
2878 find_comp_entry(int proto)
2880 struct compressor_entry *ce;
2882 list_for_each_entry(ce, &compressor_list, list) {
2883 if (ce->comp->compress_proto == proto)
2889 /* Register a compressor */
2891 ppp_register_compressor(struct compressor *cp)
2893 struct compressor_entry *ce;
2895 spin_lock(&compressor_list_lock);
2897 if (find_comp_entry(cp->compress_proto))
2900 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2905 list_add(&ce->list, &compressor_list);
2907 spin_unlock(&compressor_list_lock);
2911 /* Unregister a compressor */
2913 ppp_unregister_compressor(struct compressor *cp)
2915 struct compressor_entry *ce;
2917 spin_lock(&compressor_list_lock);
2918 ce = find_comp_entry(cp->compress_proto);
2919 if (ce && ce->comp == cp) {
2920 list_del(&ce->list);
2923 spin_unlock(&compressor_list_lock);
2926 /* Find a compressor. */
2927 static struct compressor *
2928 find_compressor(int type)
2930 struct compressor_entry *ce;
2931 struct compressor *cp = NULL;
2933 spin_lock(&compressor_list_lock);
2934 ce = find_comp_entry(type);
2937 if (!try_module_get(cp->owner))
2940 spin_unlock(&compressor_list_lock);
2945 * Miscelleneous stuff.
2949 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2951 struct slcompress *vj = ppp->vj;
2953 memset(st, 0, sizeof(*st));
2954 st->p.ppp_ipackets = ppp->stats64.rx_packets;
2955 st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
2956 st->p.ppp_ibytes = ppp->stats64.rx_bytes;
2957 st->p.ppp_opackets = ppp->stats64.tx_packets;
2958 st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
2959 st->p.ppp_obytes = ppp->stats64.tx_bytes;
2962 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2963 st->vj.vjs_compressed = vj->sls_o_compressed;
2964 st->vj.vjs_searches = vj->sls_o_searches;
2965 st->vj.vjs_misses = vj->sls_o_misses;
2966 st->vj.vjs_errorin = vj->sls_i_error;
2967 st->vj.vjs_tossed = vj->sls_i_tossed;
2968 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2969 st->vj.vjs_compressedin = vj->sls_i_compressed;
2973 * Stuff for handling the lists of ppp units and channels
2974 * and for initialization.
2978 * Create a new ppp interface unit. Fails if it can't allocate memory
2979 * or if there is already a unit with the requested number.
2980 * unit == -1 means allocate a new number.
2982 static int ppp_create_interface(struct net *net, struct file *file, int *unit)
2984 struct ppp_config conf = {
2987 .ifname_is_set = false,
2989 struct net_device *dev;
2993 dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
2998 dev_net_set(dev, net);
2999 dev->rtnl_link_ops = &ppp_link_ops;
3003 err = ppp_dev_configure(net, dev, &conf);
3006 ppp = netdev_priv(dev);
3007 *unit = ppp->file.index;
3021 * Initialize a ppp_file structure.
3024 init_ppp_file(struct ppp_file *pf, int kind)
3027 skb_queue_head_init(&pf->xq);
3028 skb_queue_head_init(&pf->rq);
3029 atomic_set(&pf->refcnt, 1);
3030 init_waitqueue_head(&pf->rwait);
3034 * Free the memory used by a ppp unit. This is only called once
3035 * there are no channels connected to the unit and no file structs
3036 * that reference the unit.
3038 static void ppp_destroy_interface(struct ppp *ppp)
3040 atomic_dec(&ppp_unit_count);
3042 if (!ppp->file.dead || ppp->n_channels) {
3043 /* "can't happen" */
3044 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3045 "but dead=%d n_channels=%d !\n",
3046 ppp, ppp->file.dead, ppp->n_channels);
3050 ppp_ccp_closed(ppp);
3055 skb_queue_purge(&ppp->file.xq);
3056 skb_queue_purge(&ppp->file.rq);
3057 #ifdef CONFIG_PPP_MULTILINK
3058 skb_queue_purge(&ppp->mrq);
3059 #endif /* CONFIG_PPP_MULTILINK */
3060 #ifdef CONFIG_PPP_FILTER
3061 if (ppp->pass_filter) {
3062 bpf_prog_destroy(ppp->pass_filter);
3063 ppp->pass_filter = NULL;
3066 if (ppp->active_filter) {
3067 bpf_prog_destroy(ppp->active_filter);
3068 ppp->active_filter = NULL;
3070 #endif /* CONFIG_PPP_FILTER */
3072 kfree_skb(ppp->xmit_pending);
3073 free_percpu(ppp->xmit_recursion);
3075 free_netdev(ppp->dev);
3079 * Locate an existing ppp unit.
3080 * The caller should have locked the all_ppp_mutex.
3083 ppp_find_unit(struct ppp_net *pn, int unit)
3085 return unit_find(&pn->units_idr, unit);
3089 * Locate an existing ppp channel.
3090 * The caller should have locked the all_channels_lock.
3091 * First we look in the new_channels list, then in the
3092 * all_channels list. If found in the new_channels list,
3093 * we move it to the all_channels list. This is for speed
3094 * when we have a lot of channels in use.
3096 static struct channel *
3097 ppp_find_channel(struct ppp_net *pn, int unit)
3099 struct channel *pch;
3101 list_for_each_entry(pch, &pn->new_channels, list) {
3102 if (pch->file.index == unit) {
3103 list_move(&pch->list, &pn->all_channels);
3108 list_for_each_entry(pch, &pn->all_channels, list) {
3109 if (pch->file.index == unit)
3117 * Connect a PPP channel to a PPP interface unit.
3120 ppp_connect_channel(struct channel *pch, int unit)
3127 pn = ppp_pernet(pch->chan_net);
3129 mutex_lock(&pn->all_ppp_mutex);
3130 ppp = ppp_find_unit(pn, unit);
3133 write_lock_bh(&pch->upl);
3139 if (pch->file.hdrlen > ppp->file.hdrlen)
3140 ppp->file.hdrlen = pch->file.hdrlen;
3141 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
3142 if (hdrlen > ppp->dev->hard_header_len)
3143 ppp->dev->hard_header_len = hdrlen;
3144 list_add_tail(&pch->clist, &ppp->channels);
3147 atomic_inc(&ppp->file.refcnt);
3152 write_unlock_bh(&pch->upl);
3154 mutex_unlock(&pn->all_ppp_mutex);
3159 * Disconnect a channel from its ppp unit.
3162 ppp_disconnect_channel(struct channel *pch)
3167 write_lock_bh(&pch->upl);
3170 write_unlock_bh(&pch->upl);
3172 /* remove it from the ppp unit's list */
3174 list_del(&pch->clist);
3175 if (--ppp->n_channels == 0)
3176 wake_up_interruptible(&ppp->file.rwait);
3178 if (atomic_dec_and_test(&ppp->file.refcnt))
3179 ppp_destroy_interface(ppp);
3186 * Free up the resources used by a ppp channel.
3188 static void ppp_destroy_channel(struct channel *pch)
3190 put_net(pch->chan_net);
3191 pch->chan_net = NULL;
3193 atomic_dec(&channel_count);
3195 if (!pch->file.dead) {
3196 /* "can't happen" */
3197 pr_err("ppp: destroying undead channel %p !\n", pch);
3200 skb_queue_purge(&pch->file.xq);
3201 skb_queue_purge(&pch->file.rq);
3205 static void __exit ppp_cleanup(void)
3207 /* should never happen */
3208 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
3209 pr_err("PPP: removing module but units remain!\n");
3210 rtnl_link_unregister(&ppp_link_ops);
3211 unregister_chrdev(PPP_MAJOR, "ppp");
3212 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
3213 class_destroy(ppp_class);
3214 unregister_pernet_device(&ppp_net_ops);
3218 * Units handling. Caller must protect concurrent access
3219 * by holding all_ppp_mutex
3222 /* associate pointer with specified number */
3223 static int unit_set(struct idr *p, void *ptr, int n)
3227 unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3228 if (unit == -ENOSPC)
3233 /* get new free unit number and associate pointer with it */
3234 static int unit_get(struct idr *p, void *ptr)
3236 return idr_alloc(p, ptr, 0, 0, GFP_KERNEL);
3239 /* put unit number back to a pool */
3240 static void unit_put(struct idr *p, int n)
3245 /* get pointer associated with the number */
3246 static void *unit_find(struct idr *p, int n)
3248 return idr_find(p, n);
3251 /* Module/initialization stuff */
3253 module_init(ppp_init);
3254 module_exit(ppp_cleanup);
3256 EXPORT_SYMBOL(ppp_register_net_channel);
3257 EXPORT_SYMBOL(ppp_register_channel);
3258 EXPORT_SYMBOL(ppp_unregister_channel);
3259 EXPORT_SYMBOL(ppp_channel_index);
3260 EXPORT_SYMBOL(ppp_unit_number);
3261 EXPORT_SYMBOL(ppp_dev_name);
3262 EXPORT_SYMBOL(ppp_input);
3263 EXPORT_SYMBOL(ppp_input_error);
3264 EXPORT_SYMBOL(ppp_output_wakeup);
3265 EXPORT_SYMBOL(ppp_register_compressor);
3266 EXPORT_SYMBOL(ppp_unregister_compressor);
3267 MODULE_LICENSE("GPL");
3268 MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
3269 MODULE_ALIAS_RTNL_LINK("ppp");
3270 MODULE_ALIAS("devname:ppp");