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 */
75 #define PPP_PROTO_LEN 2
78 * An instance of /dev/ppp can be associated with either a ppp
79 * interface unit or a ppp channel. In both cases, file->private_data
80 * points to one of these.
86 struct sk_buff_head xq; /* pppd transmit queue */
87 struct sk_buff_head rq; /* receive queue for pppd */
88 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
89 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
90 int hdrlen; /* space to leave for headers */
91 int index; /* interface unit / channel number */
92 int dead; /* unit/channel has been shut down */
95 #define PF_TO_X(pf, X) container_of(pf, X, file)
97 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
98 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
101 * Data structure to hold primary network stats for which
102 * we want to use 64 bit storage. Other network stats
103 * are stored in dev->stats of the ppp strucute.
105 struct ppp_link_stats {
113 * Data structure describing one ppp unit.
114 * A ppp unit corresponds to a ppp network interface device
115 * and represents a multilink bundle.
116 * It can have 0 or more ppp channels connected to it.
119 struct ppp_file file; /* stuff for read/write/poll 0 */
120 struct file *owner; /* file that owns this unit 48 */
121 struct list_head channels; /* list of attached channels 4c */
122 int n_channels; /* how many channels are attached 54 */
123 spinlock_t rlock; /* lock for receive side 58 */
124 spinlock_t wlock; /* lock for transmit side 5c */
125 int __percpu *xmit_recursion; /* xmit recursion detect */
126 int mru; /* max receive unit 60 */
127 unsigned int flags; /* control bits 64 */
128 unsigned int xstate; /* transmit state bits 68 */
129 unsigned int rstate; /* receive state bits 6c */
130 int debug; /* debug flags 70 */
131 struct slcompress *vj; /* state for VJ header compression */
132 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
133 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
134 struct compressor *xcomp; /* transmit packet compressor 8c */
135 void *xc_state; /* its internal state 90 */
136 struct compressor *rcomp; /* receive decompressor 94 */
137 void *rc_state; /* its internal state 98 */
138 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
139 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
140 struct net_device *dev; /* network interface device a4 */
141 int closing; /* is device closing down? a8 */
142 #ifdef CONFIG_PPP_MULTILINK
143 int nxchan; /* next channel to send something on */
144 u32 nxseq; /* next sequence number to send */
145 int mrru; /* MP: max reconst. receive unit */
146 u32 nextseq; /* MP: seq no of next packet */
147 u32 minseq; /* MP: min of most recent seqnos */
148 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
149 #endif /* CONFIG_PPP_MULTILINK */
150 #ifdef CONFIG_PPP_FILTER
151 struct bpf_prog *pass_filter; /* filter for packets to pass */
152 struct bpf_prog *active_filter; /* filter for pkts to reset idle */
153 #endif /* CONFIG_PPP_FILTER */
154 struct net *ppp_net; /* the net we belong to */
155 struct ppp_link_stats stats64; /* 64 bit network stats */
159 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
160 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
162 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
163 * Bits in xstate: SC_COMP_RUN
165 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
166 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
167 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
170 * Private data structure for each channel.
171 * This includes the data structure used for multilink.
174 struct ppp_file file; /* stuff for read/write/poll */
175 struct list_head list; /* link in all/new_channels list */
176 struct ppp_channel *chan; /* public channel data structure */
177 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
178 spinlock_t downl; /* protects `chan', file.xq dequeue */
179 struct ppp *ppp; /* ppp unit we're connected to */
180 struct net *chan_net; /* the net channel belongs to */
181 struct list_head clist; /* link in list of channels per unit */
182 rwlock_t upl; /* protects `ppp' */
183 #ifdef CONFIG_PPP_MULTILINK
184 u8 avail; /* flag used in multilink stuff */
185 u8 had_frag; /* >= 1 fragments have been sent */
186 u32 lastseq; /* MP: last sequence # received */
187 int speed; /* speed of the corresponding ppp channel*/
188 #endif /* CONFIG_PPP_MULTILINK */
198 * SMP locking issues:
199 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
200 * list and the ppp.n_channels field, you need to take both locks
201 * before you modify them.
202 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
206 static DEFINE_MUTEX(ppp_mutex);
207 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
208 static atomic_t channel_count = ATOMIC_INIT(0);
210 /* per-net private data for this module */
211 static unsigned int ppp_net_id __read_mostly;
213 /* units to ppp mapping */
214 struct idr units_idr;
217 * all_ppp_mutex protects the units_idr mapping.
218 * It also ensures that finding a ppp unit in the units_idr
219 * map and updating its file.refcnt field is atomic.
221 struct mutex all_ppp_mutex;
224 struct list_head all_channels;
225 struct list_head new_channels;
226 int last_channel_index;
229 * all_channels_lock protects all_channels and
230 * last_channel_index, and the atomicity of find
231 * a channel and updating its file.refcnt field.
233 spinlock_t all_channels_lock;
236 /* Get the PPP protocol number from a skb */
237 #define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
239 /* We limit the length of ppp->file.rq to this (arbitrary) value */
240 #define PPP_MAX_RQLEN 32
243 * Maximum number of multilink fragments queued up.
244 * This has to be large enough to cope with the maximum latency of
245 * the slowest channel relative to the others. Strictly it should
246 * depend on the number of channels and their characteristics.
248 #define PPP_MP_MAX_QLEN 128
250 /* Multilink header bits. */
251 #define B 0x80 /* this fragment begins a packet */
252 #define E 0x40 /* this fragment ends a packet */
254 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
255 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
256 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
259 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
260 struct file *file, unsigned int cmd, unsigned long arg);
261 static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb);
262 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
263 static void ppp_push(struct ppp *ppp);
264 static void ppp_channel_push(struct channel *pch);
265 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
266 struct channel *pch);
267 static void ppp_receive_error(struct ppp *ppp);
268 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
269 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
270 struct sk_buff *skb);
271 #ifdef CONFIG_PPP_MULTILINK
272 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
273 struct channel *pch);
274 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
275 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
276 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
277 #endif /* CONFIG_PPP_MULTILINK */
278 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
279 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
280 static void ppp_ccp_closed(struct ppp *ppp);
281 static struct compressor *find_compressor(int type);
282 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
283 static int ppp_create_interface(struct net *net, struct file *file, int *unit);
284 static void init_ppp_file(struct ppp_file *pf, int kind);
285 static void ppp_destroy_interface(struct ppp *ppp);
286 static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
287 static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
288 static int ppp_connect_channel(struct channel *pch, int unit);
289 static int ppp_disconnect_channel(struct channel *pch);
290 static void ppp_destroy_channel(struct channel *pch);
291 static int unit_get(struct idr *p, void *ptr, int min);
292 static int unit_set(struct idr *p, void *ptr, int n);
293 static void unit_put(struct idr *p, int n);
294 static void *unit_find(struct idr *p, int n);
295 static void ppp_setup(struct net_device *dev);
297 static const struct net_device_ops ppp_netdev_ops;
299 static struct class *ppp_class;
301 /* per net-namespace data */
302 static inline struct ppp_net *ppp_pernet(struct net *net)
306 return net_generic(net, ppp_net_id);
309 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
310 static inline int proto_to_npindex(int proto)
329 /* Translates an NP index into a PPP protocol number */
330 static const int npindex_to_proto[NUM_NP] = {
339 /* Translates an ethertype into an NP index */
340 static inline int ethertype_to_npindex(int ethertype)
360 /* Translates an NP index into an ethertype */
361 static const int npindex_to_ethertype[NUM_NP] = {
373 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
374 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
375 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
376 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
377 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
378 ppp_recv_lock(ppp); } while (0)
379 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
380 ppp_xmit_unlock(ppp); } while (0)
383 * /dev/ppp device routines.
384 * The /dev/ppp device is used by pppd to control the ppp unit.
385 * It supports the read, write, ioctl and poll functions.
386 * Open instances of /dev/ppp can be in one of three states:
387 * unattached, attached to a ppp unit, or attached to a ppp channel.
389 static int ppp_open(struct inode *inode, struct file *file)
392 * This could (should?) be enforced by the permissions on /dev/ppp.
394 if (!capable(CAP_NET_ADMIN))
399 static int ppp_release(struct inode *unused, struct file *file)
401 struct ppp_file *pf = file->private_data;
405 file->private_data = NULL;
406 if (pf->kind == INTERFACE) {
409 if (file == ppp->owner)
410 unregister_netdevice(ppp->dev);
413 if (atomic_dec_and_test(&pf->refcnt)) {
416 ppp_destroy_interface(PF_TO_PPP(pf));
419 ppp_destroy_channel(PF_TO_CHANNEL(pf));
427 static ssize_t ppp_read(struct file *file, char __user *buf,
428 size_t count, loff_t *ppos)
430 struct ppp_file *pf = file->private_data;
431 DECLARE_WAITQUEUE(wait, current);
433 struct sk_buff *skb = NULL;
441 add_wait_queue(&pf->rwait, &wait);
443 set_current_state(TASK_INTERRUPTIBLE);
444 skb = skb_dequeue(&pf->rq);
450 if (pf->kind == INTERFACE) {
452 * Return 0 (EOF) on an interface that has no
453 * channels connected, unless it is looping
454 * network traffic (demand mode).
456 struct ppp *ppp = PF_TO_PPP(pf);
459 if (ppp->n_channels == 0 &&
460 (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
461 ppp_recv_unlock(ppp);
464 ppp_recv_unlock(ppp);
467 if (file->f_flags & O_NONBLOCK)
470 if (signal_pending(current))
474 set_current_state(TASK_RUNNING);
475 remove_wait_queue(&pf->rwait, &wait);
481 if (skb->len > count)
486 iov_iter_init(&to, READ, &iov, 1, count);
487 if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
497 static ssize_t ppp_write(struct file *file, const char __user *buf,
498 size_t count, loff_t *ppos)
500 struct ppp_file *pf = file->private_data;
506 /* All PPP packets should start with the 2-byte protocol */
507 if (count < PPP_PROTO_LEN)
510 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
513 skb_reserve(skb, pf->hdrlen);
515 if (copy_from_user(skb_put(skb, count), buf, count)) {
522 ppp_xmit_process(PF_TO_PPP(pf), skb);
525 skb_queue_tail(&pf->xq, skb);
526 ppp_channel_push(PF_TO_CHANNEL(pf));
536 /* No kernel lock - fine */
537 static unsigned int ppp_poll(struct file *file, poll_table *wait)
539 struct ppp_file *pf = file->private_data;
544 poll_wait(file, &pf->rwait, wait);
545 mask = POLLOUT | POLLWRNORM;
546 if (skb_peek(&pf->rq))
547 mask |= POLLIN | POLLRDNORM;
550 else if (pf->kind == INTERFACE) {
551 /* see comment in ppp_read */
552 struct ppp *ppp = PF_TO_PPP(pf);
555 if (ppp->n_channels == 0 &&
556 (ppp->flags & SC_LOOP_TRAFFIC) == 0)
557 mask |= POLLIN | POLLRDNORM;
558 ppp_recv_unlock(ppp);
564 #ifdef CONFIG_PPP_FILTER
565 static int get_filter(void __user *arg, struct sock_filter **p)
567 struct sock_fprog uprog;
568 struct sock_filter *code = NULL;
571 if (copy_from_user(&uprog, arg, sizeof(uprog)))
579 len = uprog.len * sizeof(struct sock_filter);
580 code = memdup_user(uprog.filter, len);
582 return PTR_ERR(code);
587 #endif /* CONFIG_PPP_FILTER */
589 static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
593 int err = -EFAULT, val, val2, i;
594 struct ppp_idle idle;
597 struct slcompress *vj;
598 void __user *argp = (void __user *)arg;
599 int __user *p = argp;
601 mutex_lock(&ppp_mutex);
603 pf = file->private_data;
605 err = ppp_unattached_ioctl(current->nsproxy->net_ns,
610 if (cmd == PPPIOCDETACH) {
612 * We have to be careful here... if the file descriptor
613 * has been dup'd, we could have another process in the
614 * middle of a poll using the same file *, so we had
615 * better not free the interface data structures -
616 * instead we fail the ioctl. Even in this case, we
617 * shut down the interface if we are the owner of it.
618 * Actually, we should get rid of PPPIOCDETACH, userland
619 * (i.e. pppd) could achieve the same effect by closing
620 * this fd and reopening /dev/ppp.
623 if (pf->kind == INTERFACE) {
626 if (file == ppp->owner)
627 unregister_netdevice(ppp->dev);
630 if (atomic_long_read(&file->f_count) < 2) {
631 ppp_release(NULL, file);
634 pr_warn("PPPIOCDETACH file->f_count=%ld\n",
635 atomic_long_read(&file->f_count));
639 if (pf->kind == CHANNEL) {
641 struct ppp_channel *chan;
643 pch = PF_TO_CHANNEL(pf);
647 if (get_user(unit, p))
649 err = ppp_connect_channel(pch, unit);
653 err = ppp_disconnect_channel(pch);
657 down_read(&pch->chan_sem);
660 if (chan && chan->ops->ioctl)
661 err = chan->ops->ioctl(chan, cmd, arg);
662 up_read(&pch->chan_sem);
667 if (pf->kind != INTERFACE) {
669 pr_err("PPP: not interface or channel??\n");
677 if (get_user(val, p))
684 if (get_user(val, p))
687 cflags = ppp->flags & ~val;
688 #ifdef CONFIG_PPP_MULTILINK
689 if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
692 ppp->flags = val & SC_FLAG_BITS;
694 if (cflags & SC_CCP_OPEN)
700 val = ppp->flags | ppp->xstate | ppp->rstate;
701 if (put_user(val, p))
706 case PPPIOCSCOMPRESS:
707 err = ppp_set_compress(ppp, arg);
711 if (put_user(ppp->file.index, p))
717 if (get_user(val, p))
724 if (put_user(ppp->debug, p))
730 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
731 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
732 if (copy_to_user(argp, &idle, sizeof(idle)))
738 if (get_user(val, p))
741 if ((val >> 16) != 0) {
745 vj = slhc_init(val2+1, val+1);
760 if (copy_from_user(&npi, argp, sizeof(npi)))
762 err = proto_to_npindex(npi.protocol);
766 if (cmd == PPPIOCGNPMODE) {
768 npi.mode = ppp->npmode[i];
769 if (copy_to_user(argp, &npi, sizeof(npi)))
772 ppp->npmode[i] = npi.mode;
773 /* we may be able to transmit more packets now (??) */
774 netif_wake_queue(ppp->dev);
779 #ifdef CONFIG_PPP_FILTER
782 struct sock_filter *code;
784 err = get_filter(argp, &code);
786 struct bpf_prog *pass_filter = NULL;
787 struct sock_fprog_kern fprog = {
794 err = bpf_prog_create(&pass_filter, &fprog);
797 if (ppp->pass_filter)
798 bpf_prog_destroy(ppp->pass_filter);
799 ppp->pass_filter = pass_filter;
808 struct sock_filter *code;
810 err = get_filter(argp, &code);
812 struct bpf_prog *active_filter = NULL;
813 struct sock_fprog_kern fprog = {
820 err = bpf_prog_create(&active_filter, &fprog);
823 if (ppp->active_filter)
824 bpf_prog_destroy(ppp->active_filter);
825 ppp->active_filter = active_filter;
832 #endif /* CONFIG_PPP_FILTER */
834 #ifdef CONFIG_PPP_MULTILINK
836 if (get_user(val, p))
840 ppp_recv_unlock(ppp);
843 #endif /* CONFIG_PPP_MULTILINK */
850 mutex_unlock(&ppp_mutex);
855 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
856 struct file *file, unsigned int cmd, unsigned long arg)
858 int unit, err = -EFAULT;
860 struct channel *chan;
862 int __user *p = (int __user *)arg;
866 /* Create a new ppp unit */
867 if (get_user(unit, p))
869 err = ppp_create_interface(net, file, &unit);
874 if (put_user(unit, p))
880 /* Attach to an existing ppp unit */
881 if (get_user(unit, p))
884 pn = ppp_pernet(net);
885 mutex_lock(&pn->all_ppp_mutex);
886 ppp = ppp_find_unit(pn, unit);
888 atomic_inc(&ppp->file.refcnt);
889 file->private_data = &ppp->file;
892 mutex_unlock(&pn->all_ppp_mutex);
896 if (get_user(unit, p))
899 pn = ppp_pernet(net);
900 spin_lock_bh(&pn->all_channels_lock);
901 chan = ppp_find_channel(pn, unit);
903 atomic_inc(&chan->file.refcnt);
904 file->private_data = &chan->file;
907 spin_unlock_bh(&pn->all_channels_lock);
917 static const struct file_operations ppp_device_fops = {
918 .owner = THIS_MODULE,
922 .unlocked_ioctl = ppp_ioctl,
924 .release = ppp_release,
925 .llseek = noop_llseek,
928 static __net_init int ppp_init_net(struct net *net)
930 struct ppp_net *pn = net_generic(net, ppp_net_id);
932 idr_init(&pn->units_idr);
933 mutex_init(&pn->all_ppp_mutex);
935 INIT_LIST_HEAD(&pn->all_channels);
936 INIT_LIST_HEAD(&pn->new_channels);
938 spin_lock_init(&pn->all_channels_lock);
943 static __net_exit void ppp_exit_net(struct net *net)
945 struct ppp_net *pn = net_generic(net, ppp_net_id);
946 struct net_device *dev;
947 struct net_device *aux;
953 for_each_netdev_safe(net, dev, aux) {
954 if (dev->netdev_ops == &ppp_netdev_ops)
955 unregister_netdevice_queue(dev, &list);
958 idr_for_each_entry(&pn->units_idr, ppp, id)
959 /* Skip devices already unregistered by previous loop */
960 if (!net_eq(dev_net(ppp->dev), net))
961 unregister_netdevice_queue(ppp->dev, &list);
963 unregister_netdevice_many(&list);
966 mutex_destroy(&pn->all_ppp_mutex);
967 idr_destroy(&pn->units_idr);
970 static struct pernet_operations ppp_net_ops = {
971 .init = ppp_init_net,
972 .exit = ppp_exit_net,
974 .size = sizeof(struct ppp_net),
977 static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
979 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
982 mutex_lock(&pn->all_ppp_mutex);
985 ret = unit_get(&pn->units_idr, ppp, 0);
988 if (!ifname_is_set) {
990 snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ret);
991 if (!__dev_get_by_name(ppp->ppp_net, ppp->dev->name))
993 unit_put(&pn->units_idr, ret);
994 ret = unit_get(&pn->units_idr, ppp, ret + 1);
1000 /* Caller asked for a specific unit number. Fail with -EEXIST
1001 * if unavailable. For backward compatibility, return -EEXIST
1002 * too if idr allocation fails; this makes pppd retry without
1003 * requesting a specific unit number.
1005 if (unit_find(&pn->units_idr, unit)) {
1009 ret = unit_set(&pn->units_idr, ppp, unit);
1011 /* Rewrite error for backward compatibility */
1016 ppp->file.index = ret;
1019 snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
1021 mutex_unlock(&pn->all_ppp_mutex);
1023 ret = register_netdevice(ppp->dev);
1027 atomic_inc(&ppp_unit_count);
1032 mutex_lock(&pn->all_ppp_mutex);
1033 unit_put(&pn->units_idr, ppp->file.index);
1035 mutex_unlock(&pn->all_ppp_mutex);
1040 static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1041 const struct ppp_config *conf)
1043 struct ppp *ppp = netdev_priv(dev);
1049 ppp->ppp_net = src_net;
1051 ppp->owner = conf->file;
1053 init_ppp_file(&ppp->file, INTERFACE);
1054 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1056 for (indx = 0; indx < NUM_NP; ++indx)
1057 ppp->npmode[indx] = NPMODE_PASS;
1058 INIT_LIST_HEAD(&ppp->channels);
1059 spin_lock_init(&ppp->rlock);
1060 spin_lock_init(&ppp->wlock);
1062 ppp->xmit_recursion = alloc_percpu(int);
1063 if (!ppp->xmit_recursion) {
1067 for_each_possible_cpu(cpu)
1068 (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
1070 #ifdef CONFIG_PPP_MULTILINK
1072 skb_queue_head_init(&ppp->mrq);
1073 #endif /* CONFIG_PPP_MULTILINK */
1074 #ifdef CONFIG_PPP_FILTER
1075 ppp->pass_filter = NULL;
1076 ppp->active_filter = NULL;
1077 #endif /* CONFIG_PPP_FILTER */
1079 err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
1083 conf->file->private_data = &ppp->file;
1087 free_percpu(ppp->xmit_recursion);
1092 static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1093 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
1096 static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],
1097 struct netlink_ext_ack *extack)
1102 if (!data[IFLA_PPP_DEV_FD])
1104 if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1110 static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1111 struct nlattr *tb[], struct nlattr *data[],
1112 struct netlink_ext_ack *extack)
1114 struct ppp_config conf = {
1116 .ifname_is_set = true,
1121 file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1125 /* rtnl_lock is already held here, but ppp_create_interface() locks
1126 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1127 * possible deadlock due to lock order inversion, at the cost of
1128 * pushing the problem back to userspace.
1130 if (!mutex_trylock(&ppp_mutex)) {
1135 if (file->f_op != &ppp_device_fops || file->private_data) {
1142 /* Don't use device name generated by the rtnetlink layer when ifname
1143 * isn't specified. Let ppp_dev_configure() set the device name using
1144 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1145 * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1147 if (!tb[IFLA_IFNAME] || !nla_len(tb[IFLA_IFNAME]) || !*(char *)nla_data(tb[IFLA_IFNAME]))
1148 conf.ifname_is_set = false;
1150 err = ppp_dev_configure(src_net, dev, &conf);
1153 mutex_unlock(&ppp_mutex);
1160 static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1162 unregister_netdevice_queue(dev, head);
1165 static size_t ppp_nl_get_size(const struct net_device *dev)
1170 static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1175 static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1177 struct ppp *ppp = netdev_priv(dev);
1179 return ppp->ppp_net;
1182 static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1184 .maxtype = IFLA_PPP_MAX,
1185 .policy = ppp_nl_policy,
1186 .priv_size = sizeof(struct ppp),
1188 .validate = ppp_nl_validate,
1189 .newlink = ppp_nl_newlink,
1190 .dellink = ppp_nl_dellink,
1191 .get_size = ppp_nl_get_size,
1192 .fill_info = ppp_nl_fill_info,
1193 .get_link_net = ppp_nl_get_link_net,
1196 #define PPP_MAJOR 108
1198 /* Called at boot time if ppp is compiled into the kernel,
1199 or at module load time (from init_module) if compiled as a module. */
1200 static int __init ppp_init(void)
1204 pr_info("PPP generic driver version " PPP_VERSION "\n");
1206 err = register_pernet_device(&ppp_net_ops);
1208 pr_err("failed to register PPP pernet device (%d)\n", err);
1212 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1214 pr_err("failed to register PPP device (%d)\n", err);
1218 ppp_class = class_create(THIS_MODULE, "ppp");
1219 if (IS_ERR(ppp_class)) {
1220 err = PTR_ERR(ppp_class);
1224 err = rtnl_link_register(&ppp_link_ops);
1226 pr_err("failed to register rtnetlink PPP handler\n");
1230 /* not a big deal if we fail here :-) */
1231 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1236 class_destroy(ppp_class);
1238 unregister_chrdev(PPP_MAJOR, "ppp");
1240 unregister_pernet_device(&ppp_net_ops);
1246 * Network interface unit routines.
1249 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1251 struct ppp *ppp = netdev_priv(dev);
1255 npi = ethertype_to_npindex(ntohs(skb->protocol));
1259 /* Drop, accept or reject the packet */
1260 switch (ppp->npmode[npi]) {
1264 /* it would be nice to have a way to tell the network
1265 system to queue this one up for later. */
1272 /* Put the 2-byte PPP protocol number on the front,
1273 making sure there is room for the address and control fields. */
1274 if (skb_cow_head(skb, PPP_HDRLEN))
1277 pp = skb_push(skb, 2);
1278 proto = npindex_to_proto[npi];
1279 put_unaligned_be16(proto, pp);
1281 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
1282 ppp_xmit_process(ppp, skb);
1284 return NETDEV_TX_OK;
1288 ++dev->stats.tx_dropped;
1289 return NETDEV_TX_OK;
1293 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1295 struct ppp *ppp = netdev_priv(dev);
1297 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1298 struct ppp_stats stats;
1299 struct ppp_comp_stats cstats;
1304 ppp_get_stats(ppp, &stats);
1305 if (copy_to_user(addr, &stats, sizeof(stats)))
1310 case SIOCGPPPCSTATS:
1311 memset(&cstats, 0, sizeof(cstats));
1313 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
1315 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1316 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1323 if (copy_to_user(addr, vers, strlen(vers) + 1))
1336 ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1338 struct ppp *ppp = netdev_priv(dev);
1341 stats64->rx_packets = ppp->stats64.rx_packets;
1342 stats64->rx_bytes = ppp->stats64.rx_bytes;
1343 ppp_recv_unlock(ppp);
1346 stats64->tx_packets = ppp->stats64.tx_packets;
1347 stats64->tx_bytes = ppp->stats64.tx_bytes;
1348 ppp_xmit_unlock(ppp);
1350 stats64->rx_errors = dev->stats.rx_errors;
1351 stats64->tx_errors = dev->stats.tx_errors;
1352 stats64->rx_dropped = dev->stats.rx_dropped;
1353 stats64->tx_dropped = dev->stats.tx_dropped;
1354 stats64->rx_length_errors = dev->stats.rx_length_errors;
1357 static int ppp_dev_init(struct net_device *dev)
1361 netdev_lockdep_set_classes(dev);
1363 ppp = netdev_priv(dev);
1364 /* Let the netdevice take a reference on the ppp file. This ensures
1365 * that ppp_destroy_interface() won't run before the device gets
1368 atomic_inc(&ppp->file.refcnt);
1373 static void ppp_dev_uninit(struct net_device *dev)
1375 struct ppp *ppp = netdev_priv(dev);
1376 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1382 mutex_lock(&pn->all_ppp_mutex);
1383 unit_put(&pn->units_idr, ppp->file.index);
1384 mutex_unlock(&pn->all_ppp_mutex);
1389 wake_up_interruptible(&ppp->file.rwait);
1392 static void ppp_dev_priv_destructor(struct net_device *dev)
1396 ppp = netdev_priv(dev);
1397 if (atomic_dec_and_test(&ppp->file.refcnt))
1398 ppp_destroy_interface(ppp);
1401 static const struct net_device_ops ppp_netdev_ops = {
1402 .ndo_init = ppp_dev_init,
1403 .ndo_uninit = ppp_dev_uninit,
1404 .ndo_start_xmit = ppp_start_xmit,
1405 .ndo_do_ioctl = ppp_net_ioctl,
1406 .ndo_get_stats64 = ppp_get_stats64,
1409 static struct device_type ppp_type = {
1413 static void ppp_setup(struct net_device *dev)
1415 dev->netdev_ops = &ppp_netdev_ops;
1416 SET_NETDEV_DEVTYPE(dev, &ppp_type);
1418 dev->features |= NETIF_F_LLTX;
1420 dev->hard_header_len = PPP_HDRLEN;
1423 dev->tx_queue_len = 3;
1424 dev->type = ARPHRD_PPP;
1425 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1426 dev->priv_destructor = ppp_dev_priv_destructor;
1427 netif_keep_dst(dev);
1431 * Transmit-side routines.
1434 /* Called to do any work queued up on the transmit side that can now be done */
1435 static void __ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
1438 if (!ppp->closing) {
1442 skb_queue_tail(&ppp->file.xq, skb);
1443 while (!ppp->xmit_pending &&
1444 (skb = skb_dequeue(&ppp->file.xq)))
1445 ppp_send_frame(ppp, skb);
1446 /* If there's no work left to do, tell the core net
1447 code that we can accept some more. */
1448 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
1449 netif_wake_queue(ppp->dev);
1451 netif_stop_queue(ppp->dev);
1455 ppp_xmit_unlock(ppp);
1458 static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
1462 if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
1465 (*this_cpu_ptr(ppp->xmit_recursion))++;
1466 __ppp_xmit_process(ppp, skb);
1467 (*this_cpu_ptr(ppp->xmit_recursion))--;
1478 if (net_ratelimit())
1479 netdev_err(ppp->dev, "recursion detected\n");
1482 static inline struct sk_buff *
1483 pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1485 struct sk_buff *new_skb;
1487 int new_skb_size = ppp->dev->mtu +
1488 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1489 int compressor_skb_size = ppp->dev->mtu +
1490 ppp->xcomp->comp_extra + PPP_HDRLEN;
1491 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1493 if (net_ratelimit())
1494 netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
1497 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1498 skb_reserve(new_skb,
1499 ppp->dev->hard_header_len - PPP_HDRLEN);
1501 /* compressor still expects A/C bytes in hdr */
1502 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1503 new_skb->data, skb->len + 2,
1504 compressor_skb_size);
1505 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1509 skb_pull(skb, 2); /* pull off A/C bytes */
1510 } else if (len == 0) {
1511 /* didn't compress, or CCP not up yet */
1512 consume_skb(new_skb);
1517 * MPPE requires that we do not send unencrypted
1518 * frames. The compressor will return -1 if we
1519 * should drop the frame. We cannot simply test
1520 * the compress_proto because MPPE and MPPC share
1523 if (net_ratelimit())
1524 netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
1526 consume_skb(new_skb);
1533 * Compress and send a frame.
1534 * The caller should have locked the xmit path,
1535 * and xmit_pending should be 0.
1538 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1540 int proto = PPP_PROTO(skb);
1541 struct sk_buff *new_skb;
1545 skb->dev = ppp->dev;
1547 if (proto < 0x8000) {
1548 #ifdef CONFIG_PPP_FILTER
1549 /* check if we should pass this packet */
1550 /* the filter instructions are constructed assuming
1551 a four-byte PPP header on each packet */
1552 *(u8 *)skb_push(skb, 2) = 1;
1553 if (ppp->pass_filter &&
1554 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
1556 netdev_printk(KERN_DEBUG, ppp->dev,
1557 "PPP: outbound frame "
1562 /* if this packet passes the active filter, record the time */
1563 if (!(ppp->active_filter &&
1564 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
1565 ppp->last_xmit = jiffies;
1568 /* for data packets, record the time */
1569 ppp->last_xmit = jiffies;
1570 #endif /* CONFIG_PPP_FILTER */
1573 ++ppp->stats64.tx_packets;
1574 ppp->stats64.tx_bytes += skb->len - PPP_PROTO_LEN;
1578 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
1580 /* try to do VJ TCP header compression */
1581 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1584 netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
1587 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1589 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1590 new_skb->data + 2, &cp,
1591 !(ppp->flags & SC_NO_TCP_CCID));
1592 if (cp == skb->data + 2) {
1593 /* didn't compress */
1594 consume_skb(new_skb);
1596 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1597 proto = PPP_VJC_COMP;
1598 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1600 proto = PPP_VJC_UNCOMP;
1601 cp[0] = skb->data[2];
1605 cp = skb_put(skb, len + 2);
1612 /* peek at outbound CCP frames */
1613 ppp_ccp_peek(ppp, skb, 0);
1617 /* try to do packet compression */
1618 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1619 proto != PPP_LCP && proto != PPP_CCP) {
1620 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1621 if (net_ratelimit())
1622 netdev_err(ppp->dev,
1623 "ppp: compression required but "
1624 "down - pkt dropped.\n");
1627 skb = pad_compress_skb(ppp, skb);
1633 * If we are waiting for traffic (demand dialling),
1634 * queue it up for pppd to receive.
1636 if (ppp->flags & SC_LOOP_TRAFFIC) {
1637 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1639 skb_queue_tail(&ppp->file.rq, skb);
1640 wake_up_interruptible(&ppp->file.rwait);
1644 ppp->xmit_pending = skb;
1650 ++ppp->dev->stats.tx_errors;
1654 * Try to send the frame in xmit_pending.
1655 * The caller should have the xmit path locked.
1658 ppp_push(struct ppp *ppp)
1660 struct list_head *list;
1661 struct channel *pch;
1662 struct sk_buff *skb = ppp->xmit_pending;
1667 list = &ppp->channels;
1668 if (list_empty(list)) {
1669 /* nowhere to send the packet, just drop it */
1670 ppp->xmit_pending = NULL;
1675 if ((ppp->flags & SC_MULTILINK) == 0) {
1676 /* not doing multilink: send it down the first channel */
1678 pch = list_entry(list, struct channel, clist);
1680 spin_lock(&pch->downl);
1682 if (pch->chan->ops->start_xmit(pch->chan, skb))
1683 ppp->xmit_pending = NULL;
1685 /* channel got unregistered */
1687 ppp->xmit_pending = NULL;
1689 spin_unlock(&pch->downl);
1693 #ifdef CONFIG_PPP_MULTILINK
1694 /* Multilink: fragment the packet over as many links
1695 as can take the packet at the moment. */
1696 if (!ppp_mp_explode(ppp, skb))
1698 #endif /* CONFIG_PPP_MULTILINK */
1700 ppp->xmit_pending = NULL;
1704 #ifdef CONFIG_PPP_MULTILINK
1705 static bool mp_protocol_compress __read_mostly = true;
1706 module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
1707 MODULE_PARM_DESC(mp_protocol_compress,
1708 "compress protocol id in multilink fragments");
1711 * Divide a packet to be transmitted into fragments and
1712 * send them out the individual links.
1714 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1717 int i, bits, hdrlen, mtu;
1719 int navail, nfree, nzero;
1723 unsigned char *p, *q;
1724 struct list_head *list;
1725 struct channel *pch;
1726 struct sk_buff *frag;
1727 struct ppp_channel *chan;
1729 totspeed = 0; /*total bitrate of the bundle*/
1730 nfree = 0; /* # channels which have no packet already queued */
1731 navail = 0; /* total # of usable channels (not deregistered) */
1732 nzero = 0; /* number of channels with zero speed associated*/
1733 totfree = 0; /*total # of channels available and
1734 *having no queued packets before
1735 *starting the fragmentation*/
1737 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1739 list_for_each_entry(pch, &ppp->channels, clist) {
1743 pch->speed = pch->chan->speed;
1748 if (skb_queue_empty(&pch->file.xq) ||
1750 if (pch->speed == 0)
1753 totspeed += pch->speed;
1759 if (!pch->had_frag && i < ppp->nxchan)
1765 * Don't start sending this packet unless at least half of
1766 * the channels are free. This gives much better TCP
1767 * performance if we have a lot of channels.
1769 if (nfree == 0 || nfree < navail / 2)
1770 return 0; /* can't take now, leave it in xmit_pending */
1772 /* Do protocol field compression */
1775 if (*p == 0 && mp_protocol_compress) {
1781 nbigger = len % nfree;
1783 /* skip to the channel after the one we last used
1784 and start at that one */
1785 list = &ppp->channels;
1786 for (i = 0; i < ppp->nxchan; ++i) {
1788 if (list == &ppp->channels) {
1794 /* create a fragment for each channel */
1798 if (list == &ppp->channels) {
1802 pch = list_entry(list, struct channel, clist);
1808 * Skip this channel if it has a fragment pending already and
1809 * we haven't given a fragment to all of the free channels.
1811 if (pch->avail == 1) {
1818 /* check the channel's mtu and whether it is still attached. */
1819 spin_lock(&pch->downl);
1820 if (pch->chan == NULL) {
1821 /* can't use this channel, it's being deregistered */
1822 if (pch->speed == 0)
1825 totspeed -= pch->speed;
1827 spin_unlock(&pch->downl);
1838 *if the channel speed is not set divide
1839 *the packet evenly among the free channels;
1840 *otherwise divide it according to the speed
1841 *of the channel we are going to transmit on
1845 if (pch->speed == 0) {
1852 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1853 ((totspeed*totfree)/pch->speed)) - hdrlen;
1855 flen += ((totfree - nzero)*pch->speed)/totspeed;
1856 nbigger -= ((totfree - nzero)*pch->speed)/
1864 *check if we are on the last channel or
1865 *we exceded the length of the data to
1868 if ((nfree <= 0) || (flen > len))
1871 *it is not worth to tx on slow channels:
1872 *in that case from the resulting flen according to the
1873 *above formula will be equal or less than zero.
1874 *Skip the channel in this case
1878 spin_unlock(&pch->downl);
1883 * hdrlen includes the 2-byte PPP protocol field, but the
1884 * MTU counts only the payload excluding the protocol field.
1885 * (RFC1661 Section 2)
1887 mtu = pch->chan->mtu - (hdrlen - 2);
1894 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
1897 q = skb_put(frag, flen + hdrlen);
1899 /* make the MP header */
1900 put_unaligned_be16(PPP_MP, q);
1901 if (ppp->flags & SC_MP_XSHORTSEQ) {
1902 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1906 q[3] = ppp->nxseq >> 16;
1907 q[4] = ppp->nxseq >> 8;
1911 memcpy(q + hdrlen, p, flen);
1913 /* try to send it down the channel */
1915 if (!skb_queue_empty(&pch->file.xq) ||
1916 !chan->ops->start_xmit(chan, frag))
1917 skb_queue_tail(&pch->file.xq, frag);
1923 spin_unlock(&pch->downl);
1930 spin_unlock(&pch->downl);
1932 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
1933 ++ppp->dev->stats.tx_errors;
1935 return 1; /* abandon the frame */
1937 #endif /* CONFIG_PPP_MULTILINK */
1939 /* Try to send data out on a channel */
1940 static void __ppp_channel_push(struct channel *pch)
1942 struct sk_buff *skb;
1945 spin_lock(&pch->downl);
1947 while (!skb_queue_empty(&pch->file.xq)) {
1948 skb = skb_dequeue(&pch->file.xq);
1949 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1950 /* put the packet back and try again later */
1951 skb_queue_head(&pch->file.xq, skb);
1956 /* channel got deregistered */
1957 skb_queue_purge(&pch->file.xq);
1959 spin_unlock(&pch->downl);
1960 /* see if there is anything from the attached unit to be sent */
1961 if (skb_queue_empty(&pch->file.xq)) {
1964 __ppp_xmit_process(ppp, NULL);
1968 static void ppp_channel_push(struct channel *pch)
1970 read_lock_bh(&pch->upl);
1972 (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
1973 __ppp_channel_push(pch);
1974 (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
1976 __ppp_channel_push(pch);
1978 read_unlock_bh(&pch->upl);
1982 * Receive-side routines.
1985 struct ppp_mp_skb_parm {
1989 #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
1992 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1996 ppp_receive_frame(ppp, skb, pch);
1999 ppp_recv_unlock(ppp);
2003 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
2005 struct channel *pch = chan->ppp;
2013 read_lock_bh(&pch->upl);
2014 if (!pskb_may_pull(skb, 2)) {
2017 ++pch->ppp->dev->stats.rx_length_errors;
2018 ppp_receive_error(pch->ppp);
2023 proto = PPP_PROTO(skb);
2024 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
2025 /* put it on the channel queue */
2026 skb_queue_tail(&pch->file.rq, skb);
2027 /* drop old frames if queue too long */
2028 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
2029 (skb = skb_dequeue(&pch->file.rq)))
2031 wake_up_interruptible(&pch->file.rwait);
2033 ppp_do_recv(pch->ppp, skb, pch);
2037 read_unlock_bh(&pch->upl);
2040 /* Put a 0-length skb in the receive queue as an error indication */
2042 ppp_input_error(struct ppp_channel *chan, int code)
2044 struct channel *pch = chan->ppp;
2045 struct sk_buff *skb;
2050 read_lock_bh(&pch->upl);
2052 skb = alloc_skb(0, GFP_ATOMIC);
2054 skb->len = 0; /* probably unnecessary */
2056 ppp_do_recv(pch->ppp, skb, pch);
2059 read_unlock_bh(&pch->upl);
2063 * We come in here to process a received frame.
2064 * The receive side of the ppp unit is locked.
2067 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2069 /* note: a 0-length skb is used as an error indication */
2071 skb_checksum_complete_unset(skb);
2072 #ifdef CONFIG_PPP_MULTILINK
2073 /* XXX do channel-level decompression here */
2074 if (PPP_PROTO(skb) == PPP_MP)
2075 ppp_receive_mp_frame(ppp, skb, pch);
2077 #endif /* CONFIG_PPP_MULTILINK */
2078 ppp_receive_nonmp_frame(ppp, skb);
2081 ppp_receive_error(ppp);
2086 ppp_receive_error(struct ppp *ppp)
2088 ++ppp->dev->stats.rx_errors;
2094 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2097 int proto, len, npi;
2100 * Decompress the frame, if compressed.
2101 * Note that some decompressors need to see uncompressed frames
2102 * that come in as well as compressed frames.
2104 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2105 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
2106 skb = ppp_decompress_frame(ppp, skb);
2108 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2111 proto = PPP_PROTO(skb);
2114 /* decompress VJ compressed packets */
2115 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2118 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
2119 /* copy to a new sk_buff with more tailroom */
2120 ns = dev_alloc_skb(skb->len + 128);
2122 netdev_err(ppp->dev, "PPP: no memory "
2127 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
2132 skb->ip_summed = CHECKSUM_NONE;
2134 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2136 netdev_printk(KERN_DEBUG, ppp->dev,
2137 "PPP: VJ decompression error\n");
2142 skb_put(skb, len - skb->len);
2143 else if (len < skb->len)
2148 case PPP_VJC_UNCOMP:
2149 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2152 /* Until we fix the decompressor need to make sure
2153 * data portion is linear.
2155 if (!pskb_may_pull(skb, skb->len))
2158 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
2159 netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
2166 ppp_ccp_peek(ppp, skb, 1);
2170 ++ppp->stats64.rx_packets;
2171 ppp->stats64.rx_bytes += skb->len - 2;
2173 npi = proto_to_npindex(proto);
2175 /* control or unknown frame - pass it to pppd */
2176 skb_queue_tail(&ppp->file.rq, skb);
2177 /* limit queue length by dropping old frames */
2178 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2179 (skb = skb_dequeue(&ppp->file.rq)))
2181 /* wake up any process polling or blocking on read */
2182 wake_up_interruptible(&ppp->file.rwait);
2185 /* network protocol frame - give it to the kernel */
2187 #ifdef CONFIG_PPP_FILTER
2188 /* check if the packet passes the pass and active filters */
2189 /* the filter instructions are constructed assuming
2190 a four-byte PPP header on each packet */
2191 if (ppp->pass_filter || ppp->active_filter) {
2192 if (skb_unclone(skb, GFP_ATOMIC))
2195 *(u8 *)skb_push(skb, 2) = 0;
2196 if (ppp->pass_filter &&
2197 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
2199 netdev_printk(KERN_DEBUG, ppp->dev,
2200 "PPP: inbound frame "
2205 if (!(ppp->active_filter &&
2206 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
2207 ppp->last_recv = jiffies;
2210 #endif /* CONFIG_PPP_FILTER */
2211 ppp->last_recv = jiffies;
2213 if ((ppp->dev->flags & IFF_UP) == 0 ||
2214 ppp->npmode[npi] != NPMODE_PASS) {
2217 /* chop off protocol */
2218 skb_pull_rcsum(skb, 2);
2219 skb->dev = ppp->dev;
2220 skb->protocol = htons(npindex_to_ethertype[npi]);
2221 skb_reset_mac_header(skb);
2222 skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2223 dev_net(ppp->dev)));
2231 ppp_receive_error(ppp);
2234 static struct sk_buff *
2235 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2237 int proto = PPP_PROTO(skb);
2241 /* Until we fix all the decompressor's need to make sure
2242 * data portion is linear.
2244 if (!pskb_may_pull(skb, skb->len))
2247 if (proto == PPP_COMP) {
2250 switch(ppp->rcomp->compress_proto) {
2252 obuff_size = ppp->mru + PPP_HDRLEN + 1;
2255 obuff_size = ppp->mru + PPP_HDRLEN;
2259 ns = dev_alloc_skb(obuff_size);
2261 netdev_err(ppp->dev, "ppp_decompress_frame: "
2265 /* the decompressor still expects the A/C bytes in the hdr */
2266 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
2267 skb->len + 2, ns->data, obuff_size);
2269 /* Pass the compressed frame to pppd as an
2270 error indication. */
2271 if (len == DECOMP_FATALERROR)
2272 ppp->rstate |= SC_DC_FERROR;
2280 skb_pull(skb, 2); /* pull off the A/C bytes */
2283 /* Uncompressed frame - pass to decompressor so it
2284 can update its dictionary if necessary. */
2285 if (ppp->rcomp->incomp)
2286 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2293 ppp->rstate |= SC_DC_ERROR;
2294 ppp_receive_error(ppp);
2298 #ifdef CONFIG_PPP_MULTILINK
2300 * Receive a multilink frame.
2301 * We put it on the reconstruction queue and then pull off
2302 * as many completed frames as we can.
2305 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2309 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2311 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
2312 goto err; /* no good, throw it away */
2314 /* Decode sequence number and begin/end bits */
2315 if (ppp->flags & SC_MP_SHORTSEQ) {
2316 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2319 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2322 PPP_MP_CB(skb)->BEbits = skb->data[2];
2323 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
2326 * Do protocol ID decompression on the first fragment of each packet.
2328 if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
2329 *(u8 *)skb_push(skb, 1) = 0;
2332 * Expand sequence number to 32 bits, making it as close
2333 * as possible to ppp->minseq.
2335 seq |= ppp->minseq & ~mask;
2336 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2338 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2339 seq -= mask + 1; /* should never happen */
2340 PPP_MP_CB(skb)->sequence = seq;
2344 * If this packet comes before the next one we were expecting,
2347 if (seq_before(seq, ppp->nextseq)) {
2349 ++ppp->dev->stats.rx_dropped;
2350 ppp_receive_error(ppp);
2355 * Reevaluate minseq, the minimum over all channels of the
2356 * last sequence number received on each channel. Because of
2357 * the increasing sequence number rule, we know that any fragment
2358 * before `minseq' which hasn't arrived is never going to arrive.
2359 * The list of channels can't change because we have the receive
2360 * side of the ppp unit locked.
2362 list_for_each_entry(ch, &ppp->channels, clist) {
2363 if (seq_before(ch->lastseq, seq))
2366 if (seq_before(ppp->minseq, seq))
2369 /* Put the fragment on the reconstruction queue */
2370 ppp_mp_insert(ppp, skb);
2372 /* If the queue is getting long, don't wait any longer for packets
2373 before the start of the queue. */
2374 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
2375 struct sk_buff *mskb = skb_peek(&ppp->mrq);
2376 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2377 ppp->minseq = PPP_MP_CB(mskb)->sequence;
2380 /* Pull completed packets off the queue and receive them. */
2381 while ((skb = ppp_mp_reconstruct(ppp))) {
2382 if (pskb_may_pull(skb, 2))
2383 ppp_receive_nonmp_frame(ppp, skb);
2385 ++ppp->dev->stats.rx_length_errors;
2387 ppp_receive_error(ppp);
2395 ppp_receive_error(ppp);
2399 * Insert a fragment on the MP reconstruction queue.
2400 * The queue is ordered by increasing sequence number.
2403 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2406 struct sk_buff_head *list = &ppp->mrq;
2407 u32 seq = PPP_MP_CB(skb)->sequence;
2409 /* N.B. we don't need to lock the list lock because we have the
2410 ppp unit receive-side lock. */
2411 skb_queue_walk(list, p) {
2412 if (seq_before(seq, PPP_MP_CB(p)->sequence))
2415 __skb_queue_before(list, p, skb);
2419 * Reconstruct a packet from the MP fragment queue.
2420 * We go through increasing sequence numbers until we find a
2421 * complete packet, or we get to the sequence number for a fragment
2422 * which hasn't arrived but might still do so.
2424 static struct sk_buff *
2425 ppp_mp_reconstruct(struct ppp *ppp)
2427 u32 seq = ppp->nextseq;
2428 u32 minseq = ppp->minseq;
2429 struct sk_buff_head *list = &ppp->mrq;
2430 struct sk_buff *p, *tmp;
2431 struct sk_buff *head, *tail;
2432 struct sk_buff *skb = NULL;
2433 int lost = 0, len = 0;
2435 if (ppp->mrru == 0) /* do nothing until mrru is set */
2439 skb_queue_walk_safe(list, p, tmp) {
2441 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
2442 /* this can't happen, anyway ignore the skb */
2443 netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2445 PPP_MP_CB(p)->sequence, seq);
2446 __skb_unlink(p, list);
2450 if (PPP_MP_CB(p)->sequence != seq) {
2452 /* Fragment `seq' is missing. If it is after
2453 minseq, it might arrive later, so stop here. */
2454 if (seq_after(seq, minseq))
2456 /* Fragment `seq' is lost, keep going. */
2459 seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2460 minseq + 1: PPP_MP_CB(p)->sequence;
2463 netdev_printk(KERN_DEBUG, ppp->dev,
2464 "lost frag %u..%u\n",
2471 * At this point we know that all the fragments from
2472 * ppp->nextseq to seq are either present or lost.
2473 * Also, there are no complete packets in the queue
2474 * that have no missing fragments and end before this
2478 /* B bit set indicates this fragment starts a packet */
2479 if (PPP_MP_CB(p)->BEbits & B) {
2487 /* Got a complete packet yet? */
2488 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2489 (PPP_MP_CB(head)->BEbits & B)) {
2490 if (len > ppp->mrru + 2) {
2491 ++ppp->dev->stats.rx_length_errors;
2492 netdev_printk(KERN_DEBUG, ppp->dev,
2493 "PPP: reconstructed packet"
2494 " is too long (%d)\n", len);
2499 ppp->nextseq = seq + 1;
2503 * If this is the ending fragment of a packet,
2504 * and we haven't found a complete valid packet yet,
2505 * we can discard up to and including this fragment.
2507 if (PPP_MP_CB(p)->BEbits & E) {
2508 struct sk_buff *tmp2;
2510 skb_queue_reverse_walk_from_safe(list, p, tmp2) {
2512 netdev_printk(KERN_DEBUG, ppp->dev,
2513 "discarding frag %u\n",
2514 PPP_MP_CB(p)->sequence);
2515 __skb_unlink(p, list);
2518 head = skb_peek(list);
2525 /* If we have a complete packet, copy it all into one skb. */
2527 /* If we have discarded any fragments,
2528 signal a receive error. */
2529 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
2530 skb_queue_walk_safe(list, p, tmp) {
2534 netdev_printk(KERN_DEBUG, ppp->dev,
2535 "discarding frag %u\n",
2536 PPP_MP_CB(p)->sequence);
2537 __skb_unlink(p, list);
2542 netdev_printk(KERN_DEBUG, ppp->dev,
2543 " missed pkts %u..%u\n",
2545 PPP_MP_CB(head)->sequence-1);
2546 ++ppp->dev->stats.rx_dropped;
2547 ppp_receive_error(ppp);
2552 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2553 p = skb_queue_next(list, head);
2554 __skb_unlink(skb, list);
2555 skb_queue_walk_from_safe(list, p, tmp) {
2556 __skb_unlink(p, list);
2562 skb->data_len += p->len;
2563 skb->truesize += p->truesize;
2569 __skb_unlink(skb, list);
2572 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
2577 #endif /* CONFIG_PPP_MULTILINK */
2580 * Channel interface.
2583 /* Create a new, unattached ppp channel. */
2584 int ppp_register_channel(struct ppp_channel *chan)
2586 return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2589 /* Create a new, unattached ppp channel for specified net. */
2590 int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
2592 struct channel *pch;
2595 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
2599 pn = ppp_pernet(net);
2603 pch->chan_net = get_net(net);
2605 init_ppp_file(&pch->file, CHANNEL);
2606 pch->file.hdrlen = chan->hdrlen;
2607 #ifdef CONFIG_PPP_MULTILINK
2609 #endif /* CONFIG_PPP_MULTILINK */
2610 init_rwsem(&pch->chan_sem);
2611 spin_lock_init(&pch->downl);
2612 rwlock_init(&pch->upl);
2614 spin_lock_bh(&pn->all_channels_lock);
2615 pch->file.index = ++pn->last_channel_index;
2616 list_add(&pch->list, &pn->new_channels);
2617 atomic_inc(&channel_count);
2618 spin_unlock_bh(&pn->all_channels_lock);
2624 * Return the index of a channel.
2626 int ppp_channel_index(struct ppp_channel *chan)
2628 struct channel *pch = chan->ppp;
2631 return pch->file.index;
2636 * Return the PPP unit number to which a channel is connected.
2638 int ppp_unit_number(struct ppp_channel *chan)
2640 struct channel *pch = chan->ppp;
2644 read_lock_bh(&pch->upl);
2646 unit = pch->ppp->file.index;
2647 read_unlock_bh(&pch->upl);
2653 * Return the PPP device interface name of a channel.
2655 char *ppp_dev_name(struct ppp_channel *chan)
2657 struct channel *pch = chan->ppp;
2661 read_lock_bh(&pch->upl);
2662 if (pch->ppp && pch->ppp->dev)
2663 name = pch->ppp->dev->name;
2664 read_unlock_bh(&pch->upl);
2671 * Disconnect a channel from the generic layer.
2672 * This must be called in process context.
2675 ppp_unregister_channel(struct ppp_channel *chan)
2677 struct channel *pch = chan->ppp;
2681 return; /* should never happen */
2686 * This ensures that we have returned from any calls into the
2687 * the channel's start_xmit or ioctl routine before we proceed.
2689 down_write(&pch->chan_sem);
2690 spin_lock_bh(&pch->downl);
2692 spin_unlock_bh(&pch->downl);
2693 up_write(&pch->chan_sem);
2694 ppp_disconnect_channel(pch);
2696 pn = ppp_pernet(pch->chan_net);
2697 spin_lock_bh(&pn->all_channels_lock);
2698 list_del(&pch->list);
2699 spin_unlock_bh(&pn->all_channels_lock);
2702 wake_up_interruptible(&pch->file.rwait);
2703 if (atomic_dec_and_test(&pch->file.refcnt))
2704 ppp_destroy_channel(pch);
2708 * Callback from a channel when it can accept more to transmit.
2709 * This should be called at BH/softirq level, not interrupt level.
2712 ppp_output_wakeup(struct ppp_channel *chan)
2714 struct channel *pch = chan->ppp;
2718 ppp_channel_push(pch);
2722 * Compression control.
2725 /* Process the PPPIOCSCOMPRESS ioctl. */
2727 ppp_set_compress(struct ppp *ppp, unsigned long arg)
2730 struct compressor *cp, *ocomp;
2731 struct ppp_option_data data;
2732 void *state, *ostate;
2733 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2736 if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
2738 if (data.length > CCP_MAX_OPTION_LENGTH)
2740 if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
2744 if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
2747 cp = try_then_request_module(
2748 find_compressor(ccp_option[0]),
2749 "ppp-compress-%d", ccp_option[0]);
2754 if (data.transmit) {
2755 state = cp->comp_alloc(ccp_option, data.length);
2758 ppp->xstate &= ~SC_COMP_RUN;
2760 ostate = ppp->xc_state;
2762 ppp->xc_state = state;
2763 ppp_xmit_unlock(ppp);
2765 ocomp->comp_free(ostate);
2766 module_put(ocomp->owner);
2770 module_put(cp->owner);
2773 state = cp->decomp_alloc(ccp_option, data.length);
2776 ppp->rstate &= ~SC_DECOMP_RUN;
2778 ostate = ppp->rc_state;
2780 ppp->rc_state = state;
2781 ppp_recv_unlock(ppp);
2783 ocomp->decomp_free(ostate);
2784 module_put(ocomp->owner);
2788 module_put(cp->owner);
2796 * Look at a CCP packet and update our state accordingly.
2797 * We assume the caller has the xmit or recv path locked.
2800 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2805 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2806 return; /* no header */
2809 switch (CCP_CODE(dp)) {
2812 /* A ConfReq starts negotiation of compression
2813 * in one direction of transmission,
2814 * and hence brings it down...but which way?
2817 * A ConfReq indicates what the sender would like to receive
2820 /* He is proposing what I should send */
2821 ppp->xstate &= ~SC_COMP_RUN;
2823 /* I am proposing to what he should send */
2824 ppp->rstate &= ~SC_DECOMP_RUN;
2831 * CCP is going down, both directions of transmission
2833 ppp->rstate &= ~SC_DECOMP_RUN;
2834 ppp->xstate &= ~SC_COMP_RUN;
2838 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2840 len = CCP_LENGTH(dp);
2841 if (!pskb_may_pull(skb, len + 2))
2842 return; /* too short */
2845 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2848 /* we will start receiving compressed packets */
2851 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2852 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2853 ppp->rstate |= SC_DECOMP_RUN;
2854 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2857 /* we will soon start sending compressed packets */
2860 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2861 ppp->file.index, 0, ppp->debug))
2862 ppp->xstate |= SC_COMP_RUN;
2867 /* reset the [de]compressor */
2868 if ((ppp->flags & SC_CCP_UP) == 0)
2871 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2872 ppp->rcomp->decomp_reset(ppp->rc_state);
2873 ppp->rstate &= ~SC_DC_ERROR;
2876 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2877 ppp->xcomp->comp_reset(ppp->xc_state);
2883 /* Free up compression resources. */
2885 ppp_ccp_closed(struct ppp *ppp)
2887 void *xstate, *rstate;
2888 struct compressor *xcomp, *rcomp;
2891 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2894 xstate = ppp->xc_state;
2895 ppp->xc_state = NULL;
2898 rstate = ppp->rc_state;
2899 ppp->rc_state = NULL;
2903 xcomp->comp_free(xstate);
2904 module_put(xcomp->owner);
2907 rcomp->decomp_free(rstate);
2908 module_put(rcomp->owner);
2912 /* List of compressors. */
2913 static LIST_HEAD(compressor_list);
2914 static DEFINE_SPINLOCK(compressor_list_lock);
2916 struct compressor_entry {
2917 struct list_head list;
2918 struct compressor *comp;
2921 static struct compressor_entry *
2922 find_comp_entry(int proto)
2924 struct compressor_entry *ce;
2926 list_for_each_entry(ce, &compressor_list, list) {
2927 if (ce->comp->compress_proto == proto)
2933 /* Register a compressor */
2935 ppp_register_compressor(struct compressor *cp)
2937 struct compressor_entry *ce;
2939 spin_lock(&compressor_list_lock);
2941 if (find_comp_entry(cp->compress_proto))
2944 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2949 list_add(&ce->list, &compressor_list);
2951 spin_unlock(&compressor_list_lock);
2955 /* Unregister a compressor */
2957 ppp_unregister_compressor(struct compressor *cp)
2959 struct compressor_entry *ce;
2961 spin_lock(&compressor_list_lock);
2962 ce = find_comp_entry(cp->compress_proto);
2963 if (ce && ce->comp == cp) {
2964 list_del(&ce->list);
2967 spin_unlock(&compressor_list_lock);
2970 /* Find a compressor. */
2971 static struct compressor *
2972 find_compressor(int type)
2974 struct compressor_entry *ce;
2975 struct compressor *cp = NULL;
2977 spin_lock(&compressor_list_lock);
2978 ce = find_comp_entry(type);
2981 if (!try_module_get(cp->owner))
2984 spin_unlock(&compressor_list_lock);
2989 * Miscelleneous stuff.
2993 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2995 struct slcompress *vj = ppp->vj;
2997 memset(st, 0, sizeof(*st));
2998 st->p.ppp_ipackets = ppp->stats64.rx_packets;
2999 st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
3000 st->p.ppp_ibytes = ppp->stats64.rx_bytes;
3001 st->p.ppp_opackets = ppp->stats64.tx_packets;
3002 st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
3003 st->p.ppp_obytes = ppp->stats64.tx_bytes;
3006 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
3007 st->vj.vjs_compressed = vj->sls_o_compressed;
3008 st->vj.vjs_searches = vj->sls_o_searches;
3009 st->vj.vjs_misses = vj->sls_o_misses;
3010 st->vj.vjs_errorin = vj->sls_i_error;
3011 st->vj.vjs_tossed = vj->sls_i_tossed;
3012 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
3013 st->vj.vjs_compressedin = vj->sls_i_compressed;
3017 * Stuff for handling the lists of ppp units and channels
3018 * and for initialization.
3022 * Create a new ppp interface unit. Fails if it can't allocate memory
3023 * or if there is already a unit with the requested number.
3024 * unit == -1 means allocate a new number.
3026 static int ppp_create_interface(struct net *net, struct file *file, int *unit)
3028 struct ppp_config conf = {
3031 .ifname_is_set = false,
3033 struct net_device *dev;
3037 dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
3042 dev_net_set(dev, net);
3043 dev->rtnl_link_ops = &ppp_link_ops;
3047 err = ppp_dev_configure(net, dev, &conf);
3050 ppp = netdev_priv(dev);
3051 *unit = ppp->file.index;
3065 * Initialize a ppp_file structure.
3068 init_ppp_file(struct ppp_file *pf, int kind)
3071 skb_queue_head_init(&pf->xq);
3072 skb_queue_head_init(&pf->rq);
3073 atomic_set(&pf->refcnt, 1);
3074 init_waitqueue_head(&pf->rwait);
3078 * Free the memory used by a ppp unit. This is only called once
3079 * there are no channels connected to the unit and no file structs
3080 * that reference the unit.
3082 static void ppp_destroy_interface(struct ppp *ppp)
3084 atomic_dec(&ppp_unit_count);
3086 if (!ppp->file.dead || ppp->n_channels) {
3087 /* "can't happen" */
3088 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3089 "but dead=%d n_channels=%d !\n",
3090 ppp, ppp->file.dead, ppp->n_channels);
3094 ppp_ccp_closed(ppp);
3099 skb_queue_purge(&ppp->file.xq);
3100 skb_queue_purge(&ppp->file.rq);
3101 #ifdef CONFIG_PPP_MULTILINK
3102 skb_queue_purge(&ppp->mrq);
3103 #endif /* CONFIG_PPP_MULTILINK */
3104 #ifdef CONFIG_PPP_FILTER
3105 if (ppp->pass_filter) {
3106 bpf_prog_destroy(ppp->pass_filter);
3107 ppp->pass_filter = NULL;
3110 if (ppp->active_filter) {
3111 bpf_prog_destroy(ppp->active_filter);
3112 ppp->active_filter = NULL;
3114 #endif /* CONFIG_PPP_FILTER */
3116 kfree_skb(ppp->xmit_pending);
3117 free_percpu(ppp->xmit_recursion);
3119 free_netdev(ppp->dev);
3123 * Locate an existing ppp unit.
3124 * The caller should have locked the all_ppp_mutex.
3127 ppp_find_unit(struct ppp_net *pn, int unit)
3129 return unit_find(&pn->units_idr, unit);
3133 * Locate an existing ppp channel.
3134 * The caller should have locked the all_channels_lock.
3135 * First we look in the new_channels list, then in the
3136 * all_channels list. If found in the new_channels list,
3137 * we move it to the all_channels list. This is for speed
3138 * when we have a lot of channels in use.
3140 static struct channel *
3141 ppp_find_channel(struct ppp_net *pn, int unit)
3143 struct channel *pch;
3145 list_for_each_entry(pch, &pn->new_channels, list) {
3146 if (pch->file.index == unit) {
3147 list_move(&pch->list, &pn->all_channels);
3152 list_for_each_entry(pch, &pn->all_channels, list) {
3153 if (pch->file.index == unit)
3161 * Connect a PPP channel to a PPP interface unit.
3164 ppp_connect_channel(struct channel *pch, int unit)
3171 pn = ppp_pernet(pch->chan_net);
3173 mutex_lock(&pn->all_ppp_mutex);
3174 ppp = ppp_find_unit(pn, unit);
3177 write_lock_bh(&pch->upl);
3183 spin_lock_bh(&pch->downl);
3185 /* Don't connect unregistered channels */
3186 spin_unlock_bh(&pch->downl);
3191 spin_unlock_bh(&pch->downl);
3192 if (pch->file.hdrlen > ppp->file.hdrlen)
3193 ppp->file.hdrlen = pch->file.hdrlen;
3194 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
3195 if (hdrlen > ppp->dev->hard_header_len)
3196 ppp->dev->hard_header_len = hdrlen;
3197 list_add_tail(&pch->clist, &ppp->channels);
3200 atomic_inc(&ppp->file.refcnt);
3205 write_unlock_bh(&pch->upl);
3207 mutex_unlock(&pn->all_ppp_mutex);
3212 * Disconnect a channel from its ppp unit.
3215 ppp_disconnect_channel(struct channel *pch)
3220 write_lock_bh(&pch->upl);
3223 write_unlock_bh(&pch->upl);
3225 /* remove it from the ppp unit's list */
3227 list_del(&pch->clist);
3228 if (--ppp->n_channels == 0)
3229 wake_up_interruptible(&ppp->file.rwait);
3231 if (atomic_dec_and_test(&ppp->file.refcnt))
3232 ppp_destroy_interface(ppp);
3239 * Free up the resources used by a ppp channel.
3241 static void ppp_destroy_channel(struct channel *pch)
3243 put_net(pch->chan_net);
3244 pch->chan_net = NULL;
3246 atomic_dec(&channel_count);
3248 if (!pch->file.dead) {
3249 /* "can't happen" */
3250 pr_err("ppp: destroying undead channel %p !\n", pch);
3253 skb_queue_purge(&pch->file.xq);
3254 skb_queue_purge(&pch->file.rq);
3258 static void __exit ppp_cleanup(void)
3260 /* should never happen */
3261 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
3262 pr_err("PPP: removing module but units remain!\n");
3263 rtnl_link_unregister(&ppp_link_ops);
3264 unregister_chrdev(PPP_MAJOR, "ppp");
3265 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
3266 class_destroy(ppp_class);
3267 unregister_pernet_device(&ppp_net_ops);
3271 * Units handling. Caller must protect concurrent access
3272 * by holding all_ppp_mutex
3275 /* associate pointer with specified number */
3276 static int unit_set(struct idr *p, void *ptr, int n)
3280 unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3281 if (unit == -ENOSPC)
3286 /* get new free unit number and associate pointer with it */
3287 static int unit_get(struct idr *p, void *ptr, int min)
3289 return idr_alloc(p, ptr, min, 0, GFP_KERNEL);
3292 /* put unit number back to a pool */
3293 static void unit_put(struct idr *p, int n)
3298 /* get pointer associated with the number */
3299 static void *unit_find(struct idr *p, int n)
3301 return idr_find(p, n);
3304 /* Module/initialization stuff */
3306 module_init(ppp_init);
3307 module_exit(ppp_cleanup);
3309 EXPORT_SYMBOL(ppp_register_net_channel);
3310 EXPORT_SYMBOL(ppp_register_channel);
3311 EXPORT_SYMBOL(ppp_unregister_channel);
3312 EXPORT_SYMBOL(ppp_channel_index);
3313 EXPORT_SYMBOL(ppp_unit_number);
3314 EXPORT_SYMBOL(ppp_dev_name);
3315 EXPORT_SYMBOL(ppp_input);
3316 EXPORT_SYMBOL(ppp_input_error);
3317 EXPORT_SYMBOL(ppp_output_wakeup);
3318 EXPORT_SYMBOL(ppp_register_compressor);
3319 EXPORT_SYMBOL(ppp_unregister_compressor);
3320 MODULE_LICENSE("GPL");
3321 MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
3322 MODULE_ALIAS_RTNL_LINK("ppp");
3323 MODULE_ALIAS("devname:ppp");