ALSA: intel8x0: Don't update period unless prepared
[linux-2.6-microblaze.git] / drivers / net / ppp / ppp_generic.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Generic PPP layer for Linux.
4  *
5  * Copyright 1999-2002 Paul Mackerras.
6  *
7  * The generic PPP layer handles the PPP network interfaces, the
8  * /dev/ppp device, packet and VJ compression, and multilink.
9  * It talks to PPP `channels' via the interface defined in
10  * include/linux/ppp_channel.h.  Channels provide the basic means for
11  * sending and receiving PPP frames on some kind of communications
12  * channel.
13  *
14  * Part of the code in this driver was inspired by the old async-only
15  * PPP driver, written by Michael Callahan and Al Longyear, and
16  * subsequently hacked by Paul Mackerras.
17  *
18  * ==FILEVERSION 20041108==
19  */
20
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/sched/signal.h>
24 #include <linux/kmod.h>
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/idr.h>
28 #include <linux/netdevice.h>
29 #include <linux/poll.h>
30 #include <linux/ppp_defs.h>
31 #include <linux/filter.h>
32 #include <linux/ppp-ioctl.h>
33 #include <linux/ppp_channel.h>
34 #include <linux/ppp-comp.h>
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/if_arp.h>
38 #include <linux/ip.h>
39 #include <linux/tcp.h>
40 #include <linux/spinlock.h>
41 #include <linux/rwsem.h>
42 #include <linux/stddef.h>
43 #include <linux/device.h>
44 #include <linux/mutex.h>
45 #include <linux/slab.h>
46 #include <linux/file.h>
47 #include <asm/unaligned.h>
48 #include <net/slhc_vj.h>
49 #include <linux/atomic.h>
50 #include <linux/refcount.h>
51
52 #include <linux/nsproxy.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
55
56 #define PPP_VERSION     "2.4.2"
57
58 /*
59  * Network protocols we support.
60  */
61 #define NP_IP   0               /* Internet Protocol V4 */
62 #define NP_IPV6 1               /* Internet Protocol V6 */
63 #define NP_IPX  2               /* IPX protocol */
64 #define NP_AT   3               /* Appletalk protocol */
65 #define NP_MPLS_UC 4            /* MPLS unicast */
66 #define NP_MPLS_MC 5            /* MPLS multicast */
67 #define NUM_NP  6               /* Number of NPs. */
68
69 #define MPHDRLEN        6       /* multilink protocol header length */
70 #define MPHDRLEN_SSN    4       /* ditto with short sequence numbers */
71
72 /*
73  * An instance of /dev/ppp can be associated with either a ppp
74  * interface unit or a ppp channel.  In both cases, file->private_data
75  * points to one of these.
76  */
77 struct ppp_file {
78         enum {
79                 INTERFACE=1, CHANNEL
80         }               kind;
81         struct sk_buff_head xq;         /* pppd transmit queue */
82         struct sk_buff_head rq;         /* receive queue for pppd */
83         wait_queue_head_t rwait;        /* for poll on reading /dev/ppp */
84         refcount_t      refcnt;         /* # refs (incl /dev/ppp attached) */
85         int             hdrlen;         /* space to leave for headers */
86         int             index;          /* interface unit / channel number */
87         int             dead;           /* unit/channel has been shut down */
88 };
89
90 #define PF_TO_X(pf, X)          container_of(pf, X, file)
91
92 #define PF_TO_PPP(pf)           PF_TO_X(pf, struct ppp)
93 #define PF_TO_CHANNEL(pf)       PF_TO_X(pf, struct channel)
94
95 /*
96  * Data structure to hold primary network stats for which
97  * we want to use 64 bit storage.  Other network stats
98  * are stored in dev->stats of the ppp strucute.
99  */
100 struct ppp_link_stats {
101         u64 rx_packets;
102         u64 tx_packets;
103         u64 rx_bytes;
104         u64 tx_bytes;
105 };
106
107 /*
108  * Data structure describing one ppp unit.
109  * A ppp unit corresponds to a ppp network interface device
110  * and represents a multilink bundle.
111  * It can have 0 or more ppp channels connected to it.
112  */
113 struct ppp {
114         struct ppp_file file;           /* stuff for read/write/poll 0 */
115         struct file     *owner;         /* file that owns this unit 48 */
116         struct list_head channels;      /* list of attached channels 4c */
117         int             n_channels;     /* how many channels are attached 54 */
118         spinlock_t      rlock;          /* lock for receive side 58 */
119         spinlock_t      wlock;          /* lock for transmit side 5c */
120         int __percpu    *xmit_recursion; /* xmit recursion detect */
121         int             mru;            /* max receive unit 60 */
122         unsigned int    flags;          /* control bits 64 */
123         unsigned int    xstate;         /* transmit state bits 68 */
124         unsigned int    rstate;         /* receive state bits 6c */
125         int             debug;          /* debug flags 70 */
126         struct slcompress *vj;          /* state for VJ header compression */
127         enum NPmode     npmode[NUM_NP]; /* what to do with each net proto 78 */
128         struct sk_buff  *xmit_pending;  /* a packet ready to go out 88 */
129         struct compressor *xcomp;       /* transmit packet compressor 8c */
130         void            *xc_state;      /* its internal state 90 */
131         struct compressor *rcomp;       /* receive decompressor 94 */
132         void            *rc_state;      /* its internal state 98 */
133         unsigned long   last_xmit;      /* jiffies when last pkt sent 9c */
134         unsigned long   last_recv;      /* jiffies when last pkt rcvd a0 */
135         struct net_device *dev;         /* network interface device a4 */
136         int             closing;        /* is device closing down? a8 */
137 #ifdef CONFIG_PPP_MULTILINK
138         int             nxchan;         /* next channel to send something on */
139         u32             nxseq;          /* next sequence number to send */
140         int             mrru;           /* MP: max reconst. receive unit */
141         u32             nextseq;        /* MP: seq no of next packet */
142         u32             minseq;         /* MP: min of most recent seqnos */
143         struct sk_buff_head mrq;        /* MP: receive reconstruction queue */
144 #endif /* CONFIG_PPP_MULTILINK */
145 #ifdef CONFIG_PPP_FILTER
146         struct bpf_prog *pass_filter;   /* filter for packets to pass */
147         struct bpf_prog *active_filter; /* filter for pkts to reset idle */
148 #endif /* CONFIG_PPP_FILTER */
149         struct net      *ppp_net;       /* the net we belong to */
150         struct ppp_link_stats stats64;  /* 64 bit network stats */
151 };
152
153 /*
154  * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
155  * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
156  * SC_MUST_COMP
157  * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
158  * Bits in xstate: SC_COMP_RUN
159  */
160 #define SC_FLAG_BITS    (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
161                          |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
162                          |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
163
164 /*
165  * Private data structure for each channel.
166  * This includes the data structure used for multilink.
167  */
168 struct channel {
169         struct ppp_file file;           /* stuff for read/write/poll */
170         struct list_head list;          /* link in all/new_channels list */
171         struct ppp_channel *chan;       /* public channel data structure */
172         struct rw_semaphore chan_sem;   /* protects `chan' during chan ioctl */
173         spinlock_t      downl;          /* protects `chan', file.xq dequeue */
174         struct ppp      *ppp;           /* ppp unit we're connected to */
175         struct net      *chan_net;      /* the net channel belongs to */
176         struct list_head clist;         /* link in list of channels per unit */
177         rwlock_t        upl;            /* protects `ppp' and 'bridge' */
178         struct channel __rcu *bridge;   /* "bridged" ppp channel */
179 #ifdef CONFIG_PPP_MULTILINK
180         u8              avail;          /* flag used in multilink stuff */
181         u8              had_frag;       /* >= 1 fragments have been sent */
182         u32             lastseq;        /* MP: last sequence # received */
183         int             speed;          /* speed of the corresponding ppp channel*/
184 #endif /* CONFIG_PPP_MULTILINK */
185 };
186
187 struct ppp_config {
188         struct file *file;
189         s32 unit;
190         bool ifname_is_set;
191 };
192
193 /*
194  * SMP locking issues:
195  * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
196  * list and the ppp.n_channels field, you need to take both locks
197  * before you modify them.
198  * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
199  * channel.downl.
200  */
201
202 static DEFINE_MUTEX(ppp_mutex);
203 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
204 static atomic_t channel_count = ATOMIC_INIT(0);
205
206 /* per-net private data for this module */
207 static unsigned int ppp_net_id __read_mostly;
208 struct ppp_net {
209         /* units to ppp mapping */
210         struct idr units_idr;
211
212         /*
213          * all_ppp_mutex protects the units_idr mapping.
214          * It also ensures that finding a ppp unit in the units_idr
215          * map and updating its file.refcnt field is atomic.
216          */
217         struct mutex all_ppp_mutex;
218
219         /* channels */
220         struct list_head all_channels;
221         struct list_head new_channels;
222         int last_channel_index;
223
224         /*
225          * all_channels_lock protects all_channels and
226          * last_channel_index, and the atomicity of find
227          * a channel and updating its file.refcnt field.
228          */
229         spinlock_t all_channels_lock;
230 };
231
232 /* Get the PPP protocol number from a skb */
233 #define PPP_PROTO(skb)  get_unaligned_be16((skb)->data)
234
235 /* We limit the length of ppp->file.rq to this (arbitrary) value */
236 #define PPP_MAX_RQLEN   32
237
238 /*
239  * Maximum number of multilink fragments queued up.
240  * This has to be large enough to cope with the maximum latency of
241  * the slowest channel relative to the others.  Strictly it should
242  * depend on the number of channels and their characteristics.
243  */
244 #define PPP_MP_MAX_QLEN 128
245
246 /* Multilink header bits. */
247 #define B       0x80            /* this fragment begins a packet */
248 #define E       0x40            /* this fragment ends a packet */
249
250 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
251 #define seq_before(a, b)        ((s32)((a) - (b)) < 0)
252 #define seq_after(a, b)         ((s32)((a) - (b)) > 0)
253
254 /* Prototypes. */
255 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
256                         struct file *file, unsigned int cmd, unsigned long arg);
257 static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb);
258 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
259 static void ppp_push(struct ppp *ppp);
260 static void ppp_channel_push(struct channel *pch);
261 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
262                               struct channel *pch);
263 static void ppp_receive_error(struct ppp *ppp);
264 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
265 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
266                                             struct sk_buff *skb);
267 #ifdef CONFIG_PPP_MULTILINK
268 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
269                                 struct channel *pch);
270 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
271 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
272 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
273 #endif /* CONFIG_PPP_MULTILINK */
274 static int ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data);
275 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
276 static void ppp_ccp_closed(struct ppp *ppp);
277 static struct compressor *find_compressor(int type);
278 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
279 static int ppp_create_interface(struct net *net, struct file *file, int *unit);
280 static void init_ppp_file(struct ppp_file *pf, int kind);
281 static void ppp_destroy_interface(struct ppp *ppp);
282 static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
283 static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
284 static int ppp_connect_channel(struct channel *pch, int unit);
285 static int ppp_disconnect_channel(struct channel *pch);
286 static void ppp_destroy_channel(struct channel *pch);
287 static int unit_get(struct idr *p, void *ptr);
288 static int unit_set(struct idr *p, void *ptr, int n);
289 static void unit_put(struct idr *p, int n);
290 static void *unit_find(struct idr *p, int n);
291 static void ppp_setup(struct net_device *dev);
292
293 static const struct net_device_ops ppp_netdev_ops;
294
295 static struct class *ppp_class;
296
297 /* per net-namespace data */
298 static inline struct ppp_net *ppp_pernet(struct net *net)
299 {
300         return net_generic(net, ppp_net_id);
301 }
302
303 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
304 static inline int proto_to_npindex(int proto)
305 {
306         switch (proto) {
307         case PPP_IP:
308                 return NP_IP;
309         case PPP_IPV6:
310                 return NP_IPV6;
311         case PPP_IPX:
312                 return NP_IPX;
313         case PPP_AT:
314                 return NP_AT;
315         case PPP_MPLS_UC:
316                 return NP_MPLS_UC;
317         case PPP_MPLS_MC:
318                 return NP_MPLS_MC;
319         }
320         return -EINVAL;
321 }
322
323 /* Translates an NP index into a PPP protocol number */
324 static const int npindex_to_proto[NUM_NP] = {
325         PPP_IP,
326         PPP_IPV6,
327         PPP_IPX,
328         PPP_AT,
329         PPP_MPLS_UC,
330         PPP_MPLS_MC,
331 };
332
333 /* Translates an ethertype into an NP index */
334 static inline int ethertype_to_npindex(int ethertype)
335 {
336         switch (ethertype) {
337         case ETH_P_IP:
338                 return NP_IP;
339         case ETH_P_IPV6:
340                 return NP_IPV6;
341         case ETH_P_IPX:
342                 return NP_IPX;
343         case ETH_P_PPPTALK:
344         case ETH_P_ATALK:
345                 return NP_AT;
346         case ETH_P_MPLS_UC:
347                 return NP_MPLS_UC;
348         case ETH_P_MPLS_MC:
349                 return NP_MPLS_MC;
350         }
351         return -1;
352 }
353
354 /* Translates an NP index into an ethertype */
355 static const int npindex_to_ethertype[NUM_NP] = {
356         ETH_P_IP,
357         ETH_P_IPV6,
358         ETH_P_IPX,
359         ETH_P_PPPTALK,
360         ETH_P_MPLS_UC,
361         ETH_P_MPLS_MC,
362 };
363
364 /*
365  * Locking shorthand.
366  */
367 #define ppp_xmit_lock(ppp)      spin_lock_bh(&(ppp)->wlock)
368 #define ppp_xmit_unlock(ppp)    spin_unlock_bh(&(ppp)->wlock)
369 #define ppp_recv_lock(ppp)      spin_lock_bh(&(ppp)->rlock)
370 #define ppp_recv_unlock(ppp)    spin_unlock_bh(&(ppp)->rlock)
371 #define ppp_lock(ppp)           do { ppp_xmit_lock(ppp); \
372                                      ppp_recv_lock(ppp); } while (0)
373 #define ppp_unlock(ppp)         do { ppp_recv_unlock(ppp); \
374                                      ppp_xmit_unlock(ppp); } while (0)
375
376 /*
377  * /dev/ppp device routines.
378  * The /dev/ppp device is used by pppd to control the ppp unit.
379  * It supports the read, write, ioctl and poll functions.
380  * Open instances of /dev/ppp can be in one of three states:
381  * unattached, attached to a ppp unit, or attached to a ppp channel.
382  */
383 static int ppp_open(struct inode *inode, struct file *file)
384 {
385         /*
386          * This could (should?) be enforced by the permissions on /dev/ppp.
387          */
388         if (!ns_capable(file->f_cred->user_ns, CAP_NET_ADMIN))
389                 return -EPERM;
390         return 0;
391 }
392
393 static int ppp_release(struct inode *unused, struct file *file)
394 {
395         struct ppp_file *pf = file->private_data;
396         struct ppp *ppp;
397
398         if (pf) {
399                 file->private_data = NULL;
400                 if (pf->kind == INTERFACE) {
401                         ppp = PF_TO_PPP(pf);
402                         rtnl_lock();
403                         if (file == ppp->owner)
404                                 unregister_netdevice(ppp->dev);
405                         rtnl_unlock();
406                 }
407                 if (refcount_dec_and_test(&pf->refcnt)) {
408                         switch (pf->kind) {
409                         case INTERFACE:
410                                 ppp_destroy_interface(PF_TO_PPP(pf));
411                                 break;
412                         case CHANNEL:
413                                 ppp_destroy_channel(PF_TO_CHANNEL(pf));
414                                 break;
415                         }
416                 }
417         }
418         return 0;
419 }
420
421 static ssize_t ppp_read(struct file *file, char __user *buf,
422                         size_t count, loff_t *ppos)
423 {
424         struct ppp_file *pf = file->private_data;
425         DECLARE_WAITQUEUE(wait, current);
426         ssize_t ret;
427         struct sk_buff *skb = NULL;
428         struct iovec iov;
429         struct iov_iter to;
430
431         ret = count;
432
433         if (!pf)
434                 return -ENXIO;
435         add_wait_queue(&pf->rwait, &wait);
436         for (;;) {
437                 set_current_state(TASK_INTERRUPTIBLE);
438                 skb = skb_dequeue(&pf->rq);
439                 if (skb)
440                         break;
441                 ret = 0;
442                 if (pf->dead)
443                         break;
444                 if (pf->kind == INTERFACE) {
445                         /*
446                          * Return 0 (EOF) on an interface that has no
447                          * channels connected, unless it is looping
448                          * network traffic (demand mode).
449                          */
450                         struct ppp *ppp = PF_TO_PPP(pf);
451
452                         ppp_recv_lock(ppp);
453                         if (ppp->n_channels == 0 &&
454                             (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
455                                 ppp_recv_unlock(ppp);
456                                 break;
457                         }
458                         ppp_recv_unlock(ppp);
459                 }
460                 ret = -EAGAIN;
461                 if (file->f_flags & O_NONBLOCK)
462                         break;
463                 ret = -ERESTARTSYS;
464                 if (signal_pending(current))
465                         break;
466                 schedule();
467         }
468         set_current_state(TASK_RUNNING);
469         remove_wait_queue(&pf->rwait, &wait);
470
471         if (!skb)
472                 goto out;
473
474         ret = -EOVERFLOW;
475         if (skb->len > count)
476                 goto outf;
477         ret = -EFAULT;
478         iov.iov_base = buf;
479         iov.iov_len = count;
480         iov_iter_init(&to, READ, &iov, 1, count);
481         if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
482                 goto outf;
483         ret = skb->len;
484
485  outf:
486         kfree_skb(skb);
487  out:
488         return ret;
489 }
490
491 static ssize_t ppp_write(struct file *file, const char __user *buf,
492                          size_t count, loff_t *ppos)
493 {
494         struct ppp_file *pf = file->private_data;
495         struct sk_buff *skb;
496         ssize_t ret;
497
498         if (!pf)
499                 return -ENXIO;
500         ret = -ENOMEM;
501         skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
502         if (!skb)
503                 goto out;
504         skb_reserve(skb, pf->hdrlen);
505         ret = -EFAULT;
506         if (copy_from_user(skb_put(skb, count), buf, count)) {
507                 kfree_skb(skb);
508                 goto out;
509         }
510
511         switch (pf->kind) {
512         case INTERFACE:
513                 ppp_xmit_process(PF_TO_PPP(pf), skb);
514                 break;
515         case CHANNEL:
516                 skb_queue_tail(&pf->xq, skb);
517                 ppp_channel_push(PF_TO_CHANNEL(pf));
518                 break;
519         }
520
521         ret = count;
522
523  out:
524         return ret;
525 }
526
527 /* No kernel lock - fine */
528 static __poll_t ppp_poll(struct file *file, poll_table *wait)
529 {
530         struct ppp_file *pf = file->private_data;
531         __poll_t mask;
532
533         if (!pf)
534                 return 0;
535         poll_wait(file, &pf->rwait, wait);
536         mask = EPOLLOUT | EPOLLWRNORM;
537         if (skb_peek(&pf->rq))
538                 mask |= EPOLLIN | EPOLLRDNORM;
539         if (pf->dead)
540                 mask |= EPOLLHUP;
541         else if (pf->kind == INTERFACE) {
542                 /* see comment in ppp_read */
543                 struct ppp *ppp = PF_TO_PPP(pf);
544
545                 ppp_recv_lock(ppp);
546                 if (ppp->n_channels == 0 &&
547                     (ppp->flags & SC_LOOP_TRAFFIC) == 0)
548                         mask |= EPOLLIN | EPOLLRDNORM;
549                 ppp_recv_unlock(ppp);
550         }
551
552         return mask;
553 }
554
555 #ifdef CONFIG_PPP_FILTER
556 static struct bpf_prog *get_filter(struct sock_fprog *uprog)
557 {
558         struct sock_fprog_kern fprog;
559         struct bpf_prog *res = NULL;
560         int err;
561
562         if (!uprog->len)
563                 return NULL;
564
565         /* uprog->len is unsigned short, so no overflow here */
566         fprog.len = uprog->len;
567         fprog.filter = memdup_user(uprog->filter,
568                                    uprog->len * sizeof(struct sock_filter));
569         if (IS_ERR(fprog.filter))
570                 return ERR_CAST(fprog.filter);
571
572         err = bpf_prog_create(&res, &fprog);
573         kfree(fprog.filter);
574
575         return err ? ERR_PTR(err) : res;
576 }
577
578 static struct bpf_prog *ppp_get_filter(struct sock_fprog __user *p)
579 {
580         struct sock_fprog uprog;
581
582         if (copy_from_user(&uprog, p, sizeof(struct sock_fprog)))
583                 return ERR_PTR(-EFAULT);
584         return get_filter(&uprog);
585 }
586
587 #ifdef CONFIG_COMPAT
588 struct sock_fprog32 {
589         unsigned short len;
590         compat_caddr_t filter;
591 };
592
593 #define PPPIOCSPASS32           _IOW('t', 71, struct sock_fprog32)
594 #define PPPIOCSACTIVE32         _IOW('t', 70, struct sock_fprog32)
595
596 static struct bpf_prog *compat_ppp_get_filter(struct sock_fprog32 __user *p)
597 {
598         struct sock_fprog32 uprog32;
599         struct sock_fprog uprog;
600
601         if (copy_from_user(&uprog32, p, sizeof(struct sock_fprog32)))
602                 return ERR_PTR(-EFAULT);
603         uprog.len = uprog32.len;
604         uprog.filter = compat_ptr(uprog32.filter);
605         return get_filter(&uprog);
606 }
607 #endif
608 #endif
609
610 /* Bridge one PPP channel to another.
611  * When two channels are bridged, ppp_input on one channel is redirected to
612  * the other's ops->start_xmit handler.
613  * In order to safely bridge channels we must reject channels which are already
614  * part of a bridge instance, or which form part of an existing unit.
615  * Once successfully bridged, each channel holds a reference on the other
616  * to prevent it being freed while the bridge is extant.
617  */
618 static int ppp_bridge_channels(struct channel *pch, struct channel *pchb)
619 {
620         write_lock_bh(&pch->upl);
621         if (pch->ppp ||
622             rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl))) {
623                 write_unlock_bh(&pch->upl);
624                 return -EALREADY;
625         }
626         refcount_inc(&pchb->file.refcnt);
627         rcu_assign_pointer(pch->bridge, pchb);
628         write_unlock_bh(&pch->upl);
629
630         write_lock_bh(&pchb->upl);
631         if (pchb->ppp ||
632             rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl))) {
633                 write_unlock_bh(&pchb->upl);
634                 goto err_unset;
635         }
636         refcount_inc(&pch->file.refcnt);
637         rcu_assign_pointer(pchb->bridge, pch);
638         write_unlock_bh(&pchb->upl);
639
640         return 0;
641
642 err_unset:
643         write_lock_bh(&pch->upl);
644         /* Re-read pch->bridge with upl held in case it was modified concurrently */
645         pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
646         RCU_INIT_POINTER(pch->bridge, NULL);
647         write_unlock_bh(&pch->upl);
648         synchronize_rcu();
649
650         if (pchb)
651                 if (refcount_dec_and_test(&pchb->file.refcnt))
652                         ppp_destroy_channel(pchb);
653
654         return -EALREADY;
655 }
656
657 static int ppp_unbridge_channels(struct channel *pch)
658 {
659         struct channel *pchb, *pchbb;
660
661         write_lock_bh(&pch->upl);
662         pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
663         if (!pchb) {
664                 write_unlock_bh(&pch->upl);
665                 return -EINVAL;
666         }
667         RCU_INIT_POINTER(pch->bridge, NULL);
668         write_unlock_bh(&pch->upl);
669
670         /* Only modify pchb if phcb->bridge points back to pch.
671          * If not, it implies that there has been a race unbridging (and possibly
672          * even rebridging) pchb.  We should leave pchb alone to avoid either a
673          * refcount underflow, or breaking another established bridge instance.
674          */
675         write_lock_bh(&pchb->upl);
676         pchbb = rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl));
677         if (pchbb == pch)
678                 RCU_INIT_POINTER(pchb->bridge, NULL);
679         write_unlock_bh(&pchb->upl);
680
681         synchronize_rcu();
682
683         if (pchbb == pch)
684                 if (refcount_dec_and_test(&pch->file.refcnt))
685                         ppp_destroy_channel(pch);
686
687         if (refcount_dec_and_test(&pchb->file.refcnt))
688                 ppp_destroy_channel(pchb);
689
690         return 0;
691 }
692
693 static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
694 {
695         struct ppp_file *pf;
696         struct ppp *ppp;
697         int err = -EFAULT, val, val2, i;
698         struct ppp_idle32 idle32;
699         struct ppp_idle64 idle64;
700         struct npioctl npi;
701         int unit, cflags;
702         struct slcompress *vj;
703         void __user *argp = (void __user *)arg;
704         int __user *p = argp;
705
706         mutex_lock(&ppp_mutex);
707
708         pf = file->private_data;
709         if (!pf) {
710                 err = ppp_unattached_ioctl(current->nsproxy->net_ns,
711                                            pf, file, cmd, arg);
712                 goto out;
713         }
714
715         if (cmd == PPPIOCDETACH) {
716                 /*
717                  * PPPIOCDETACH is no longer supported as it was heavily broken,
718                  * and is only known to have been used by pppd older than
719                  * ppp-2.4.2 (released November 2003).
720                  */
721                 pr_warn_once("%s (%d) used obsolete PPPIOCDETACH ioctl\n",
722                              current->comm, current->pid);
723                 err = -EINVAL;
724                 goto out;
725         }
726
727         if (pf->kind == CHANNEL) {
728                 struct channel *pch, *pchb;
729                 struct ppp_channel *chan;
730                 struct ppp_net *pn;
731
732                 pch = PF_TO_CHANNEL(pf);
733
734                 switch (cmd) {
735                 case PPPIOCCONNECT:
736                         if (get_user(unit, p))
737                                 break;
738                         err = ppp_connect_channel(pch, unit);
739                         break;
740
741                 case PPPIOCDISCONN:
742                         err = ppp_disconnect_channel(pch);
743                         break;
744
745                 case PPPIOCBRIDGECHAN:
746                         if (get_user(unit, p))
747                                 break;
748                         err = -ENXIO;
749                         pn = ppp_pernet(current->nsproxy->net_ns);
750                         spin_lock_bh(&pn->all_channels_lock);
751                         pchb = ppp_find_channel(pn, unit);
752                         /* Hold a reference to prevent pchb being freed while
753                          * we establish the bridge.
754                          */
755                         if (pchb)
756                                 refcount_inc(&pchb->file.refcnt);
757                         spin_unlock_bh(&pn->all_channels_lock);
758                         if (!pchb)
759                                 break;
760                         err = ppp_bridge_channels(pch, pchb);
761                         /* Drop earlier refcount now bridge establishment is complete */
762                         if (refcount_dec_and_test(&pchb->file.refcnt))
763                                 ppp_destroy_channel(pchb);
764                         break;
765
766                 case PPPIOCUNBRIDGECHAN:
767                         err = ppp_unbridge_channels(pch);
768                         break;
769
770                 default:
771                         down_read(&pch->chan_sem);
772                         chan = pch->chan;
773                         err = -ENOTTY;
774                         if (chan && chan->ops->ioctl)
775                                 err = chan->ops->ioctl(chan, cmd, arg);
776                         up_read(&pch->chan_sem);
777                 }
778                 goto out;
779         }
780
781         if (pf->kind != INTERFACE) {
782                 /* can't happen */
783                 pr_err("PPP: not interface or channel??\n");
784                 err = -EINVAL;
785                 goto out;
786         }
787
788         ppp = PF_TO_PPP(pf);
789         switch (cmd) {
790         case PPPIOCSMRU:
791                 if (get_user(val, p))
792                         break;
793                 ppp->mru = val;
794                 err = 0;
795                 break;
796
797         case PPPIOCSFLAGS:
798                 if (get_user(val, p))
799                         break;
800                 ppp_lock(ppp);
801                 cflags = ppp->flags & ~val;
802 #ifdef CONFIG_PPP_MULTILINK
803                 if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
804                         ppp->nextseq = 0;
805 #endif
806                 ppp->flags = val & SC_FLAG_BITS;
807                 ppp_unlock(ppp);
808                 if (cflags & SC_CCP_OPEN)
809                         ppp_ccp_closed(ppp);
810                 err = 0;
811                 break;
812
813         case PPPIOCGFLAGS:
814                 val = ppp->flags | ppp->xstate | ppp->rstate;
815                 if (put_user(val, p))
816                         break;
817                 err = 0;
818                 break;
819
820         case PPPIOCSCOMPRESS:
821         {
822                 struct ppp_option_data data;
823                 if (copy_from_user(&data, argp, sizeof(data)))
824                         err = -EFAULT;
825                 else
826                         err = ppp_set_compress(ppp, &data);
827                 break;
828         }
829         case PPPIOCGUNIT:
830                 if (put_user(ppp->file.index, p))
831                         break;
832                 err = 0;
833                 break;
834
835         case PPPIOCSDEBUG:
836                 if (get_user(val, p))
837                         break;
838                 ppp->debug = val;
839                 err = 0;
840                 break;
841
842         case PPPIOCGDEBUG:
843                 if (put_user(ppp->debug, p))
844                         break;
845                 err = 0;
846                 break;
847
848         case PPPIOCGIDLE32:
849                 idle32.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
850                 idle32.recv_idle = (jiffies - ppp->last_recv) / HZ;
851                 if (copy_to_user(argp, &idle32, sizeof(idle32)))
852                         break;
853                 err = 0;
854                 break;
855
856         case PPPIOCGIDLE64:
857                 idle64.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
858                 idle64.recv_idle = (jiffies - ppp->last_recv) / HZ;
859                 if (copy_to_user(argp, &idle64, sizeof(idle64)))
860                         break;
861                 err = 0;
862                 break;
863
864         case PPPIOCSMAXCID:
865                 if (get_user(val, p))
866                         break;
867                 val2 = 15;
868                 if ((val >> 16) != 0) {
869                         val2 = val >> 16;
870                         val &= 0xffff;
871                 }
872                 vj = slhc_init(val2+1, val+1);
873                 if (IS_ERR(vj)) {
874                         err = PTR_ERR(vj);
875                         break;
876                 }
877                 ppp_lock(ppp);
878                 if (ppp->vj)
879                         slhc_free(ppp->vj);
880                 ppp->vj = vj;
881                 ppp_unlock(ppp);
882                 err = 0;
883                 break;
884
885         case PPPIOCGNPMODE:
886         case PPPIOCSNPMODE:
887                 if (copy_from_user(&npi, argp, sizeof(npi)))
888                         break;
889                 err = proto_to_npindex(npi.protocol);
890                 if (err < 0)
891                         break;
892                 i = err;
893                 if (cmd == PPPIOCGNPMODE) {
894                         err = -EFAULT;
895                         npi.mode = ppp->npmode[i];
896                         if (copy_to_user(argp, &npi, sizeof(npi)))
897                                 break;
898                 } else {
899                         ppp->npmode[i] = npi.mode;
900                         /* we may be able to transmit more packets now (??) */
901                         netif_wake_queue(ppp->dev);
902                 }
903                 err = 0;
904                 break;
905
906 #ifdef CONFIG_PPP_FILTER
907         case PPPIOCSPASS:
908         case PPPIOCSACTIVE:
909         {
910                 struct bpf_prog *filter = ppp_get_filter(argp);
911                 struct bpf_prog **which;
912
913                 if (IS_ERR(filter)) {
914                         err = PTR_ERR(filter);
915                         break;
916                 }
917                 if (cmd == PPPIOCSPASS)
918                         which = &ppp->pass_filter;
919                 else
920                         which = &ppp->active_filter;
921                 ppp_lock(ppp);
922                 if (*which)
923                         bpf_prog_destroy(*which);
924                 *which = filter;
925                 ppp_unlock(ppp);
926                 err = 0;
927                 break;
928         }
929 #endif /* CONFIG_PPP_FILTER */
930
931 #ifdef CONFIG_PPP_MULTILINK
932         case PPPIOCSMRRU:
933                 if (get_user(val, p))
934                         break;
935                 ppp_recv_lock(ppp);
936                 ppp->mrru = val;
937                 ppp_recv_unlock(ppp);
938                 err = 0;
939                 break;
940 #endif /* CONFIG_PPP_MULTILINK */
941
942         default:
943                 err = -ENOTTY;
944         }
945
946 out:
947         mutex_unlock(&ppp_mutex);
948
949         return err;
950 }
951
952 #ifdef CONFIG_COMPAT
953 struct ppp_option_data32 {
954         compat_uptr_t           ptr;
955         u32                     length;
956         compat_int_t            transmit;
957 };
958 #define PPPIOCSCOMPRESS32       _IOW('t', 77, struct ppp_option_data32)
959
960 static long ppp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
961 {
962         struct ppp_file *pf;
963         int err = -ENOIOCTLCMD;
964         void __user *argp = (void __user *)arg;
965
966         mutex_lock(&ppp_mutex);
967
968         pf = file->private_data;
969         if (pf && pf->kind == INTERFACE) {
970                 struct ppp *ppp = PF_TO_PPP(pf);
971                 switch (cmd) {
972 #ifdef CONFIG_PPP_FILTER
973                 case PPPIOCSPASS32:
974                 case PPPIOCSACTIVE32:
975                 {
976                         struct bpf_prog *filter = compat_ppp_get_filter(argp);
977                         struct bpf_prog **which;
978
979                         if (IS_ERR(filter)) {
980                                 err = PTR_ERR(filter);
981                                 break;
982                         }
983                         if (cmd == PPPIOCSPASS32)
984                                 which = &ppp->pass_filter;
985                         else
986                                 which = &ppp->active_filter;
987                         ppp_lock(ppp);
988                         if (*which)
989                                 bpf_prog_destroy(*which);
990                         *which = filter;
991                         ppp_unlock(ppp);
992                         err = 0;
993                         break;
994                 }
995 #endif /* CONFIG_PPP_FILTER */
996                 case PPPIOCSCOMPRESS32:
997                 {
998                         struct ppp_option_data32 data32;
999                         if (copy_from_user(&data32, argp, sizeof(data32))) {
1000                                 err = -EFAULT;
1001                         } else {
1002                                 struct ppp_option_data data = {
1003                                         .ptr = compat_ptr(data32.ptr),
1004                                         .length = data32.length,
1005                                         .transmit = data32.transmit
1006                                 };
1007                                 err = ppp_set_compress(ppp, &data);
1008                         }
1009                         break;
1010                 }
1011                 }
1012         }
1013         mutex_unlock(&ppp_mutex);
1014
1015         /* all other commands have compatible arguments */
1016         if (err == -ENOIOCTLCMD)
1017                 err = ppp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1018
1019         return err;
1020 }
1021 #endif
1022
1023 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
1024                         struct file *file, unsigned int cmd, unsigned long arg)
1025 {
1026         int unit, err = -EFAULT;
1027         struct ppp *ppp;
1028         struct channel *chan;
1029         struct ppp_net *pn;
1030         int __user *p = (int __user *)arg;
1031
1032         switch (cmd) {
1033         case PPPIOCNEWUNIT:
1034                 /* Create a new ppp unit */
1035                 if (get_user(unit, p))
1036                         break;
1037                 err = ppp_create_interface(net, file, &unit);
1038                 if (err < 0)
1039                         break;
1040
1041                 err = -EFAULT;
1042                 if (put_user(unit, p))
1043                         break;
1044                 err = 0;
1045                 break;
1046
1047         case PPPIOCATTACH:
1048                 /* Attach to an existing ppp unit */
1049                 if (get_user(unit, p))
1050                         break;
1051                 err = -ENXIO;
1052                 pn = ppp_pernet(net);
1053                 mutex_lock(&pn->all_ppp_mutex);
1054                 ppp = ppp_find_unit(pn, unit);
1055                 if (ppp) {
1056                         refcount_inc(&ppp->file.refcnt);
1057                         file->private_data = &ppp->file;
1058                         err = 0;
1059                 }
1060                 mutex_unlock(&pn->all_ppp_mutex);
1061                 break;
1062
1063         case PPPIOCATTCHAN:
1064                 if (get_user(unit, p))
1065                         break;
1066                 err = -ENXIO;
1067                 pn = ppp_pernet(net);
1068                 spin_lock_bh(&pn->all_channels_lock);
1069                 chan = ppp_find_channel(pn, unit);
1070                 if (chan) {
1071                         refcount_inc(&chan->file.refcnt);
1072                         file->private_data = &chan->file;
1073                         err = 0;
1074                 }
1075                 spin_unlock_bh(&pn->all_channels_lock);
1076                 break;
1077
1078         default:
1079                 err = -ENOTTY;
1080         }
1081
1082         return err;
1083 }
1084
1085 static const struct file_operations ppp_device_fops = {
1086         .owner          = THIS_MODULE,
1087         .read           = ppp_read,
1088         .write          = ppp_write,
1089         .poll           = ppp_poll,
1090         .unlocked_ioctl = ppp_ioctl,
1091 #ifdef CONFIG_COMPAT
1092         .compat_ioctl   = ppp_compat_ioctl,
1093 #endif
1094         .open           = ppp_open,
1095         .release        = ppp_release,
1096         .llseek         = noop_llseek,
1097 };
1098
1099 static __net_init int ppp_init_net(struct net *net)
1100 {
1101         struct ppp_net *pn = net_generic(net, ppp_net_id);
1102
1103         idr_init(&pn->units_idr);
1104         mutex_init(&pn->all_ppp_mutex);
1105
1106         INIT_LIST_HEAD(&pn->all_channels);
1107         INIT_LIST_HEAD(&pn->new_channels);
1108
1109         spin_lock_init(&pn->all_channels_lock);
1110
1111         return 0;
1112 }
1113
1114 static __net_exit void ppp_exit_net(struct net *net)
1115 {
1116         struct ppp_net *pn = net_generic(net, ppp_net_id);
1117         struct net_device *dev;
1118         struct net_device *aux;
1119         struct ppp *ppp;
1120         LIST_HEAD(list);
1121         int id;
1122
1123         rtnl_lock();
1124         for_each_netdev_safe(net, dev, aux) {
1125                 if (dev->netdev_ops == &ppp_netdev_ops)
1126                         unregister_netdevice_queue(dev, &list);
1127         }
1128
1129         idr_for_each_entry(&pn->units_idr, ppp, id)
1130                 /* Skip devices already unregistered by previous loop */
1131                 if (!net_eq(dev_net(ppp->dev), net))
1132                         unregister_netdevice_queue(ppp->dev, &list);
1133
1134         unregister_netdevice_many(&list);
1135         rtnl_unlock();
1136
1137         mutex_destroy(&pn->all_ppp_mutex);
1138         idr_destroy(&pn->units_idr);
1139         WARN_ON_ONCE(!list_empty(&pn->all_channels));
1140         WARN_ON_ONCE(!list_empty(&pn->new_channels));
1141 }
1142
1143 static struct pernet_operations ppp_net_ops = {
1144         .init = ppp_init_net,
1145         .exit = ppp_exit_net,
1146         .id   = &ppp_net_id,
1147         .size = sizeof(struct ppp_net),
1148 };
1149
1150 static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
1151 {
1152         struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1153         int ret;
1154
1155         mutex_lock(&pn->all_ppp_mutex);
1156
1157         if (unit < 0) {
1158                 ret = unit_get(&pn->units_idr, ppp);
1159                 if (ret < 0)
1160                         goto err;
1161         } else {
1162                 /* Caller asked for a specific unit number. Fail with -EEXIST
1163                  * if unavailable. For backward compatibility, return -EEXIST
1164                  * too if idr allocation fails; this makes pppd retry without
1165                  * requesting a specific unit number.
1166                  */
1167                 if (unit_find(&pn->units_idr, unit)) {
1168                         ret = -EEXIST;
1169                         goto err;
1170                 }
1171                 ret = unit_set(&pn->units_idr, ppp, unit);
1172                 if (ret < 0) {
1173                         /* Rewrite error for backward compatibility */
1174                         ret = -EEXIST;
1175                         goto err;
1176                 }
1177         }
1178         ppp->file.index = ret;
1179
1180         if (!ifname_is_set)
1181                 snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
1182
1183         mutex_unlock(&pn->all_ppp_mutex);
1184
1185         ret = register_netdevice(ppp->dev);
1186         if (ret < 0)
1187                 goto err_unit;
1188
1189         atomic_inc(&ppp_unit_count);
1190
1191         return 0;
1192
1193 err_unit:
1194         mutex_lock(&pn->all_ppp_mutex);
1195         unit_put(&pn->units_idr, ppp->file.index);
1196 err:
1197         mutex_unlock(&pn->all_ppp_mutex);
1198
1199         return ret;
1200 }
1201
1202 static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1203                              const struct ppp_config *conf)
1204 {
1205         struct ppp *ppp = netdev_priv(dev);
1206         int indx;
1207         int err;
1208         int cpu;
1209
1210         ppp->dev = dev;
1211         ppp->ppp_net = src_net;
1212         ppp->mru = PPP_MRU;
1213         ppp->owner = conf->file;
1214
1215         init_ppp_file(&ppp->file, INTERFACE);
1216         ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1217
1218         for (indx = 0; indx < NUM_NP; ++indx)
1219                 ppp->npmode[indx] = NPMODE_PASS;
1220         INIT_LIST_HEAD(&ppp->channels);
1221         spin_lock_init(&ppp->rlock);
1222         spin_lock_init(&ppp->wlock);
1223
1224         ppp->xmit_recursion = alloc_percpu(int);
1225         if (!ppp->xmit_recursion) {
1226                 err = -ENOMEM;
1227                 goto err1;
1228         }
1229         for_each_possible_cpu(cpu)
1230                 (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
1231
1232 #ifdef CONFIG_PPP_MULTILINK
1233         ppp->minseq = -1;
1234         skb_queue_head_init(&ppp->mrq);
1235 #endif /* CONFIG_PPP_MULTILINK */
1236 #ifdef CONFIG_PPP_FILTER
1237         ppp->pass_filter = NULL;
1238         ppp->active_filter = NULL;
1239 #endif /* CONFIG_PPP_FILTER */
1240
1241         err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
1242         if (err < 0)
1243                 goto err2;
1244
1245         conf->file->private_data = &ppp->file;
1246
1247         return 0;
1248 err2:
1249         free_percpu(ppp->xmit_recursion);
1250 err1:
1251         return err;
1252 }
1253
1254 static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1255         [IFLA_PPP_DEV_FD]       = { .type = NLA_S32 },
1256 };
1257
1258 static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],
1259                            struct netlink_ext_ack *extack)
1260 {
1261         if (!data)
1262                 return -EINVAL;
1263
1264         if (!data[IFLA_PPP_DEV_FD])
1265                 return -EINVAL;
1266         if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1267                 return -EBADF;
1268
1269         return 0;
1270 }
1271
1272 static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1273                           struct nlattr *tb[], struct nlattr *data[],
1274                           struct netlink_ext_ack *extack)
1275 {
1276         struct ppp_config conf = {
1277                 .unit = -1,
1278                 .ifname_is_set = true,
1279         };
1280         struct file *file;
1281         int err;
1282
1283         file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1284         if (!file)
1285                 return -EBADF;
1286
1287         /* rtnl_lock is already held here, but ppp_create_interface() locks
1288          * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1289          * possible deadlock due to lock order inversion, at the cost of
1290          * pushing the problem back to userspace.
1291          */
1292         if (!mutex_trylock(&ppp_mutex)) {
1293                 err = -EBUSY;
1294                 goto out;
1295         }
1296
1297         if (file->f_op != &ppp_device_fops || file->private_data) {
1298                 err = -EBADF;
1299                 goto out_unlock;
1300         }
1301
1302         conf.file = file;
1303
1304         /* Don't use device name generated by the rtnetlink layer when ifname
1305          * isn't specified. Let ppp_dev_configure() set the device name using
1306          * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1307          * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1308          */
1309         if (!tb[IFLA_IFNAME])
1310                 conf.ifname_is_set = false;
1311
1312         err = ppp_dev_configure(src_net, dev, &conf);
1313
1314 out_unlock:
1315         mutex_unlock(&ppp_mutex);
1316 out:
1317         fput(file);
1318
1319         return err;
1320 }
1321
1322 static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1323 {
1324         unregister_netdevice_queue(dev, head);
1325 }
1326
1327 static size_t ppp_nl_get_size(const struct net_device *dev)
1328 {
1329         return 0;
1330 }
1331
1332 static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1333 {
1334         return 0;
1335 }
1336
1337 static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1338 {
1339         struct ppp *ppp = netdev_priv(dev);
1340
1341         return ppp->ppp_net;
1342 }
1343
1344 static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1345         .kind           = "ppp",
1346         .maxtype        = IFLA_PPP_MAX,
1347         .policy         = ppp_nl_policy,
1348         .priv_size      = sizeof(struct ppp),
1349         .setup          = ppp_setup,
1350         .validate       = ppp_nl_validate,
1351         .newlink        = ppp_nl_newlink,
1352         .dellink        = ppp_nl_dellink,
1353         .get_size       = ppp_nl_get_size,
1354         .fill_info      = ppp_nl_fill_info,
1355         .get_link_net   = ppp_nl_get_link_net,
1356 };
1357
1358 #define PPP_MAJOR       108
1359
1360 /* Called at boot time if ppp is compiled into the kernel,
1361    or at module load time (from init_module) if compiled as a module. */
1362 static int __init ppp_init(void)
1363 {
1364         int err;
1365
1366         pr_info("PPP generic driver version " PPP_VERSION "\n");
1367
1368         err = register_pernet_device(&ppp_net_ops);
1369         if (err) {
1370                 pr_err("failed to register PPP pernet device (%d)\n", err);
1371                 goto out;
1372         }
1373
1374         err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1375         if (err) {
1376                 pr_err("failed to register PPP device (%d)\n", err);
1377                 goto out_net;
1378         }
1379
1380         ppp_class = class_create(THIS_MODULE, "ppp");
1381         if (IS_ERR(ppp_class)) {
1382                 err = PTR_ERR(ppp_class);
1383                 goto out_chrdev;
1384         }
1385
1386         err = rtnl_link_register(&ppp_link_ops);
1387         if (err) {
1388                 pr_err("failed to register rtnetlink PPP handler\n");
1389                 goto out_class;
1390         }
1391
1392         /* not a big deal if we fail here :-) */
1393         device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1394
1395         return 0;
1396
1397 out_class:
1398         class_destroy(ppp_class);
1399 out_chrdev:
1400         unregister_chrdev(PPP_MAJOR, "ppp");
1401 out_net:
1402         unregister_pernet_device(&ppp_net_ops);
1403 out:
1404         return err;
1405 }
1406
1407 /*
1408  * Network interface unit routines.
1409  */
1410 static netdev_tx_t
1411 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1412 {
1413         struct ppp *ppp = netdev_priv(dev);
1414         int npi, proto;
1415         unsigned char *pp;
1416
1417         npi = ethertype_to_npindex(ntohs(skb->protocol));
1418         if (npi < 0)
1419                 goto outf;
1420
1421         /* Drop, accept or reject the packet */
1422         switch (ppp->npmode[npi]) {
1423         case NPMODE_PASS:
1424                 break;
1425         case NPMODE_QUEUE:
1426                 /* it would be nice to have a way to tell the network
1427                    system to queue this one up for later. */
1428                 goto outf;
1429         case NPMODE_DROP:
1430         case NPMODE_ERROR:
1431                 goto outf;
1432         }
1433
1434         /* Put the 2-byte PPP protocol number on the front,
1435            making sure there is room for the address and control fields. */
1436         if (skb_cow_head(skb, PPP_HDRLEN))
1437                 goto outf;
1438
1439         pp = skb_push(skb, 2);
1440         proto = npindex_to_proto[npi];
1441         put_unaligned_be16(proto, pp);
1442
1443         skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
1444         ppp_xmit_process(ppp, skb);
1445
1446         return NETDEV_TX_OK;
1447
1448  outf:
1449         kfree_skb(skb);
1450         ++dev->stats.tx_dropped;
1451         return NETDEV_TX_OK;
1452 }
1453
1454 static int
1455 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1456 {
1457         struct ppp *ppp = netdev_priv(dev);
1458         int err = -EFAULT;
1459         void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1460         struct ppp_stats stats;
1461         struct ppp_comp_stats cstats;
1462         char *vers;
1463
1464         switch (cmd) {
1465         case SIOCGPPPSTATS:
1466                 ppp_get_stats(ppp, &stats);
1467                 if (copy_to_user(addr, &stats, sizeof(stats)))
1468                         break;
1469                 err = 0;
1470                 break;
1471
1472         case SIOCGPPPCSTATS:
1473                 memset(&cstats, 0, sizeof(cstats));
1474                 if (ppp->xc_state)
1475                         ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
1476                 if (ppp->rc_state)
1477                         ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1478                 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1479                         break;
1480                 err = 0;
1481                 break;
1482
1483         case SIOCGPPPVER:
1484                 vers = PPP_VERSION;
1485                 if (copy_to_user(addr, vers, strlen(vers) + 1))
1486                         break;
1487                 err = 0;
1488                 break;
1489
1490         default:
1491                 err = -EINVAL;
1492         }
1493
1494         return err;
1495 }
1496
1497 static void
1498 ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1499 {
1500         struct ppp *ppp = netdev_priv(dev);
1501
1502         ppp_recv_lock(ppp);
1503         stats64->rx_packets = ppp->stats64.rx_packets;
1504         stats64->rx_bytes   = ppp->stats64.rx_bytes;
1505         ppp_recv_unlock(ppp);
1506
1507         ppp_xmit_lock(ppp);
1508         stats64->tx_packets = ppp->stats64.tx_packets;
1509         stats64->tx_bytes   = ppp->stats64.tx_bytes;
1510         ppp_xmit_unlock(ppp);
1511
1512         stats64->rx_errors        = dev->stats.rx_errors;
1513         stats64->tx_errors        = dev->stats.tx_errors;
1514         stats64->rx_dropped       = dev->stats.rx_dropped;
1515         stats64->tx_dropped       = dev->stats.tx_dropped;
1516         stats64->rx_length_errors = dev->stats.rx_length_errors;
1517 }
1518
1519 static int ppp_dev_init(struct net_device *dev)
1520 {
1521         struct ppp *ppp;
1522
1523         netdev_lockdep_set_classes(dev);
1524
1525         ppp = netdev_priv(dev);
1526         /* Let the netdevice take a reference on the ppp file. This ensures
1527          * that ppp_destroy_interface() won't run before the device gets
1528          * unregistered.
1529          */
1530         refcount_inc(&ppp->file.refcnt);
1531
1532         return 0;
1533 }
1534
1535 static void ppp_dev_uninit(struct net_device *dev)
1536 {
1537         struct ppp *ppp = netdev_priv(dev);
1538         struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1539
1540         ppp_lock(ppp);
1541         ppp->closing = 1;
1542         ppp_unlock(ppp);
1543
1544         mutex_lock(&pn->all_ppp_mutex);
1545         unit_put(&pn->units_idr, ppp->file.index);
1546         mutex_unlock(&pn->all_ppp_mutex);
1547
1548         ppp->owner = NULL;
1549
1550         ppp->file.dead = 1;
1551         wake_up_interruptible(&ppp->file.rwait);
1552 }
1553
1554 static void ppp_dev_priv_destructor(struct net_device *dev)
1555 {
1556         struct ppp *ppp;
1557
1558         ppp = netdev_priv(dev);
1559         if (refcount_dec_and_test(&ppp->file.refcnt))
1560                 ppp_destroy_interface(ppp);
1561 }
1562
1563 static const struct net_device_ops ppp_netdev_ops = {
1564         .ndo_init        = ppp_dev_init,
1565         .ndo_uninit      = ppp_dev_uninit,
1566         .ndo_start_xmit  = ppp_start_xmit,
1567         .ndo_do_ioctl    = ppp_net_ioctl,
1568         .ndo_get_stats64 = ppp_get_stats64,
1569 };
1570
1571 static struct device_type ppp_type = {
1572         .name = "ppp",
1573 };
1574
1575 static void ppp_setup(struct net_device *dev)
1576 {
1577         dev->netdev_ops = &ppp_netdev_ops;
1578         SET_NETDEV_DEVTYPE(dev, &ppp_type);
1579
1580         dev->features |= NETIF_F_LLTX;
1581
1582         dev->hard_header_len = PPP_HDRLEN;
1583         dev->mtu = PPP_MRU;
1584         dev->addr_len = 0;
1585         dev->tx_queue_len = 3;
1586         dev->type = ARPHRD_PPP;
1587         dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1588         dev->priv_destructor = ppp_dev_priv_destructor;
1589         netif_keep_dst(dev);
1590 }
1591
1592 /*
1593  * Transmit-side routines.
1594  */
1595
1596 /* Called to do any work queued up on the transmit side that can now be done */
1597 static void __ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
1598 {
1599         ppp_xmit_lock(ppp);
1600         if (!ppp->closing) {
1601                 ppp_push(ppp);
1602
1603                 if (skb)
1604                         skb_queue_tail(&ppp->file.xq, skb);
1605                 while (!ppp->xmit_pending &&
1606                        (skb = skb_dequeue(&ppp->file.xq)))
1607                         ppp_send_frame(ppp, skb);
1608                 /* If there's no work left to do, tell the core net
1609                    code that we can accept some more. */
1610                 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
1611                         netif_wake_queue(ppp->dev);
1612                 else
1613                         netif_stop_queue(ppp->dev);
1614         } else {
1615                 kfree_skb(skb);
1616         }
1617         ppp_xmit_unlock(ppp);
1618 }
1619
1620 static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
1621 {
1622         local_bh_disable();
1623
1624         if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
1625                 goto err;
1626
1627         (*this_cpu_ptr(ppp->xmit_recursion))++;
1628         __ppp_xmit_process(ppp, skb);
1629         (*this_cpu_ptr(ppp->xmit_recursion))--;
1630
1631         local_bh_enable();
1632
1633         return;
1634
1635 err:
1636         local_bh_enable();
1637
1638         kfree_skb(skb);
1639
1640         if (net_ratelimit())
1641                 netdev_err(ppp->dev, "recursion detected\n");
1642 }
1643
1644 static inline struct sk_buff *
1645 pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1646 {
1647         struct sk_buff *new_skb;
1648         int len;
1649         int new_skb_size = ppp->dev->mtu +
1650                 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1651         int compressor_skb_size = ppp->dev->mtu +
1652                 ppp->xcomp->comp_extra + PPP_HDRLEN;
1653         new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1654         if (!new_skb) {
1655                 if (net_ratelimit())
1656                         netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
1657                 return NULL;
1658         }
1659         if (ppp->dev->hard_header_len > PPP_HDRLEN)
1660                 skb_reserve(new_skb,
1661                             ppp->dev->hard_header_len - PPP_HDRLEN);
1662
1663         /* compressor still expects A/C bytes in hdr */
1664         len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1665                                    new_skb->data, skb->len + 2,
1666                                    compressor_skb_size);
1667         if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1668                 consume_skb(skb);
1669                 skb = new_skb;
1670                 skb_put(skb, len);
1671                 skb_pull(skb, 2);       /* pull off A/C bytes */
1672         } else if (len == 0) {
1673                 /* didn't compress, or CCP not up yet */
1674                 consume_skb(new_skb);
1675                 new_skb = skb;
1676         } else {
1677                 /*
1678                  * (len < 0)
1679                  * MPPE requires that we do not send unencrypted
1680                  * frames.  The compressor will return -1 if we
1681                  * should drop the frame.  We cannot simply test
1682                  * the compress_proto because MPPE and MPPC share
1683                  * the same number.
1684                  */
1685                 if (net_ratelimit())
1686                         netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
1687                 kfree_skb(skb);
1688                 consume_skb(new_skb);
1689                 new_skb = NULL;
1690         }
1691         return new_skb;
1692 }
1693
1694 /*
1695  * Compress and send a frame.
1696  * The caller should have locked the xmit path,
1697  * and xmit_pending should be 0.
1698  */
1699 static void
1700 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1701 {
1702         int proto = PPP_PROTO(skb);
1703         struct sk_buff *new_skb;
1704         int len;
1705         unsigned char *cp;
1706
1707         if (proto < 0x8000) {
1708 #ifdef CONFIG_PPP_FILTER
1709                 /* check if we should pass this packet */
1710                 /* the filter instructions are constructed assuming
1711                    a four-byte PPP header on each packet */
1712                 *(u8 *)skb_push(skb, 2) = 1;
1713                 if (ppp->pass_filter &&
1714                     BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
1715                         if (ppp->debug & 1)
1716                                 netdev_printk(KERN_DEBUG, ppp->dev,
1717                                               "PPP: outbound frame "
1718                                               "not passed\n");
1719                         kfree_skb(skb);
1720                         return;
1721                 }
1722                 /* if this packet passes the active filter, record the time */
1723                 if (!(ppp->active_filter &&
1724                       BPF_PROG_RUN(ppp->active_filter, skb) == 0))
1725                         ppp->last_xmit = jiffies;
1726                 skb_pull(skb, 2);
1727 #else
1728                 /* for data packets, record the time */
1729                 ppp->last_xmit = jiffies;
1730 #endif /* CONFIG_PPP_FILTER */
1731         }
1732
1733         ++ppp->stats64.tx_packets;
1734         ppp->stats64.tx_bytes += skb->len - 2;
1735
1736         switch (proto) {
1737         case PPP_IP:
1738                 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
1739                         break;
1740                 /* try to do VJ TCP header compression */
1741                 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1742                                     GFP_ATOMIC);
1743                 if (!new_skb) {
1744                         netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
1745                         goto drop;
1746                 }
1747                 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1748                 cp = skb->data + 2;
1749                 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1750                                     new_skb->data + 2, &cp,
1751                                     !(ppp->flags & SC_NO_TCP_CCID));
1752                 if (cp == skb->data + 2) {
1753                         /* didn't compress */
1754                         consume_skb(new_skb);
1755                 } else {
1756                         if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1757                                 proto = PPP_VJC_COMP;
1758                                 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1759                         } else {
1760                                 proto = PPP_VJC_UNCOMP;
1761                                 cp[0] = skb->data[2];
1762                         }
1763                         consume_skb(skb);
1764                         skb = new_skb;
1765                         cp = skb_put(skb, len + 2);
1766                         cp[0] = 0;
1767                         cp[1] = proto;
1768                 }
1769                 break;
1770
1771         case PPP_CCP:
1772                 /* peek at outbound CCP frames */
1773                 ppp_ccp_peek(ppp, skb, 0);
1774                 break;
1775         }
1776
1777         /* try to do packet compression */
1778         if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1779             proto != PPP_LCP && proto != PPP_CCP) {
1780                 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1781                         if (net_ratelimit())
1782                                 netdev_err(ppp->dev,
1783                                            "ppp: compression required but "
1784                                            "down - pkt dropped.\n");
1785                         goto drop;
1786                 }
1787                 skb = pad_compress_skb(ppp, skb);
1788                 if (!skb)
1789                         goto drop;
1790         }
1791
1792         /*
1793          * If we are waiting for traffic (demand dialling),
1794          * queue it up for pppd to receive.
1795          */
1796         if (ppp->flags & SC_LOOP_TRAFFIC) {
1797                 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1798                         goto drop;
1799                 skb_queue_tail(&ppp->file.rq, skb);
1800                 wake_up_interruptible(&ppp->file.rwait);
1801                 return;
1802         }
1803
1804         ppp->xmit_pending = skb;
1805         ppp_push(ppp);
1806         return;
1807
1808  drop:
1809         kfree_skb(skb);
1810         ++ppp->dev->stats.tx_errors;
1811 }
1812
1813 /*
1814  * Try to send the frame in xmit_pending.
1815  * The caller should have the xmit path locked.
1816  */
1817 static void
1818 ppp_push(struct ppp *ppp)
1819 {
1820         struct list_head *list;
1821         struct channel *pch;
1822         struct sk_buff *skb = ppp->xmit_pending;
1823
1824         if (!skb)
1825                 return;
1826
1827         list = &ppp->channels;
1828         if (list_empty(list)) {
1829                 /* nowhere to send the packet, just drop it */
1830                 ppp->xmit_pending = NULL;
1831                 kfree_skb(skb);
1832                 return;
1833         }
1834
1835         if ((ppp->flags & SC_MULTILINK) == 0) {
1836                 /* not doing multilink: send it down the first channel */
1837                 list = list->next;
1838                 pch = list_entry(list, struct channel, clist);
1839
1840                 spin_lock(&pch->downl);
1841                 if (pch->chan) {
1842                         if (pch->chan->ops->start_xmit(pch->chan, skb))
1843                                 ppp->xmit_pending = NULL;
1844                 } else {
1845                         /* channel got unregistered */
1846                         kfree_skb(skb);
1847                         ppp->xmit_pending = NULL;
1848                 }
1849                 spin_unlock(&pch->downl);
1850                 return;
1851         }
1852
1853 #ifdef CONFIG_PPP_MULTILINK
1854         /* Multilink: fragment the packet over as many links
1855            as can take the packet at the moment. */
1856         if (!ppp_mp_explode(ppp, skb))
1857                 return;
1858 #endif /* CONFIG_PPP_MULTILINK */
1859
1860         ppp->xmit_pending = NULL;
1861         kfree_skb(skb);
1862 }
1863
1864 #ifdef CONFIG_PPP_MULTILINK
1865 static bool mp_protocol_compress __read_mostly = true;
1866 module_param(mp_protocol_compress, bool, 0644);
1867 MODULE_PARM_DESC(mp_protocol_compress,
1868                  "compress protocol id in multilink fragments");
1869
1870 /*
1871  * Divide a packet to be transmitted into fragments and
1872  * send them out the individual links.
1873  */
1874 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1875 {
1876         int len, totlen;
1877         int i, bits, hdrlen, mtu;
1878         int flen;
1879         int navail, nfree, nzero;
1880         int nbigger;
1881         int totspeed;
1882         int totfree;
1883         unsigned char *p, *q;
1884         struct list_head *list;
1885         struct channel *pch;
1886         struct sk_buff *frag;
1887         struct ppp_channel *chan;
1888
1889         totspeed = 0; /*total bitrate of the bundle*/
1890         nfree = 0; /* # channels which have no packet already queued */
1891         navail = 0; /* total # of usable channels (not deregistered) */
1892         nzero = 0; /* number of channels with zero speed associated*/
1893         totfree = 0; /*total # of channels available and
1894                                   *having no queued packets before
1895                                   *starting the fragmentation*/
1896
1897         hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1898         i = 0;
1899         list_for_each_entry(pch, &ppp->channels, clist) {
1900                 if (pch->chan) {
1901                         pch->avail = 1;
1902                         navail++;
1903                         pch->speed = pch->chan->speed;
1904                 } else {
1905                         pch->avail = 0;
1906                 }
1907                 if (pch->avail) {
1908                         if (skb_queue_empty(&pch->file.xq) ||
1909                                 !pch->had_frag) {
1910                                         if (pch->speed == 0)
1911                                                 nzero++;
1912                                         else
1913                                                 totspeed += pch->speed;
1914
1915                                         pch->avail = 2;
1916                                         ++nfree;
1917                                         ++totfree;
1918                                 }
1919                         if (!pch->had_frag && i < ppp->nxchan)
1920                                 ppp->nxchan = i;
1921                 }
1922                 ++i;
1923         }
1924         /*
1925          * Don't start sending this packet unless at least half of
1926          * the channels are free.  This gives much better TCP
1927          * performance if we have a lot of channels.
1928          */
1929         if (nfree == 0 || nfree < navail / 2)
1930                 return 0; /* can't take now, leave it in xmit_pending */
1931
1932         /* Do protocol field compression */
1933         p = skb->data;
1934         len = skb->len;
1935         if (*p == 0 && mp_protocol_compress) {
1936                 ++p;
1937                 --len;
1938         }
1939
1940         totlen = len;
1941         nbigger = len % nfree;
1942
1943         /* skip to the channel after the one we last used
1944            and start at that one */
1945         list = &ppp->channels;
1946         for (i = 0; i < ppp->nxchan; ++i) {
1947                 list = list->next;
1948                 if (list == &ppp->channels) {
1949                         i = 0;
1950                         break;
1951                 }
1952         }
1953
1954         /* create a fragment for each channel */
1955         bits = B;
1956         while (len > 0) {
1957                 list = list->next;
1958                 if (list == &ppp->channels) {
1959                         i = 0;
1960                         continue;
1961                 }
1962                 pch = list_entry(list, struct channel, clist);
1963                 ++i;
1964                 if (!pch->avail)
1965                         continue;
1966
1967                 /*
1968                  * Skip this channel if it has a fragment pending already and
1969                  * we haven't given a fragment to all of the free channels.
1970                  */
1971                 if (pch->avail == 1) {
1972                         if (nfree > 0)
1973                                 continue;
1974                 } else {
1975                         pch->avail = 1;
1976                 }
1977
1978                 /* check the channel's mtu and whether it is still attached. */
1979                 spin_lock(&pch->downl);
1980                 if (pch->chan == NULL) {
1981                         /* can't use this channel, it's being deregistered */
1982                         if (pch->speed == 0)
1983                                 nzero--;
1984                         else
1985                                 totspeed -= pch->speed;
1986
1987                         spin_unlock(&pch->downl);
1988                         pch->avail = 0;
1989                         totlen = len;
1990                         totfree--;
1991                         nfree--;
1992                         if (--navail == 0)
1993                                 break;
1994                         continue;
1995                 }
1996
1997                 /*
1998                 *if the channel speed is not set divide
1999                 *the packet evenly among the free channels;
2000                 *otherwise divide it according to the speed
2001                 *of the channel we are going to transmit on
2002                 */
2003                 flen = len;
2004                 if (nfree > 0) {
2005                         if (pch->speed == 0) {
2006                                 flen = len/nfree;
2007                                 if (nbigger > 0) {
2008                                         flen++;
2009                                         nbigger--;
2010                                 }
2011                         } else {
2012                                 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
2013                                         ((totspeed*totfree)/pch->speed)) - hdrlen;
2014                                 if (nbigger > 0) {
2015                                         flen += ((totfree - nzero)*pch->speed)/totspeed;
2016                                         nbigger -= ((totfree - nzero)*pch->speed)/
2017                                                         totspeed;
2018                                 }
2019                         }
2020                         nfree--;
2021                 }
2022
2023                 /*
2024                  *check if we are on the last channel or
2025                  *we exceded the length of the data to
2026                  *fragment
2027                  */
2028                 if ((nfree <= 0) || (flen > len))
2029                         flen = len;
2030                 /*
2031                  *it is not worth to tx on slow channels:
2032                  *in that case from the resulting flen according to the
2033                  *above formula will be equal or less than zero.
2034                  *Skip the channel in this case
2035                  */
2036                 if (flen <= 0) {
2037                         pch->avail = 2;
2038                         spin_unlock(&pch->downl);
2039                         continue;
2040                 }
2041
2042                 /*
2043                  * hdrlen includes the 2-byte PPP protocol field, but the
2044                  * MTU counts only the payload excluding the protocol field.
2045                  * (RFC1661 Section 2)
2046                  */
2047                 mtu = pch->chan->mtu - (hdrlen - 2);
2048                 if (mtu < 4)
2049                         mtu = 4;
2050                 if (flen > mtu)
2051                         flen = mtu;
2052                 if (flen == len)
2053                         bits |= E;
2054                 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
2055                 if (!frag)
2056                         goto noskb;
2057                 q = skb_put(frag, flen + hdrlen);
2058
2059                 /* make the MP header */
2060                 put_unaligned_be16(PPP_MP, q);
2061                 if (ppp->flags & SC_MP_XSHORTSEQ) {
2062                         q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
2063                         q[3] = ppp->nxseq;
2064                 } else {
2065                         q[2] = bits;
2066                         q[3] = ppp->nxseq >> 16;
2067                         q[4] = ppp->nxseq >> 8;
2068                         q[5] = ppp->nxseq;
2069                 }
2070
2071                 memcpy(q + hdrlen, p, flen);
2072
2073                 /* try to send it down the channel */
2074                 chan = pch->chan;
2075                 if (!skb_queue_empty(&pch->file.xq) ||
2076                         !chan->ops->start_xmit(chan, frag))
2077                         skb_queue_tail(&pch->file.xq, frag);
2078                 pch->had_frag = 1;
2079                 p += flen;
2080                 len -= flen;
2081                 ++ppp->nxseq;
2082                 bits = 0;
2083                 spin_unlock(&pch->downl);
2084         }
2085         ppp->nxchan = i;
2086
2087         return 1;
2088
2089  noskb:
2090         spin_unlock(&pch->downl);
2091         if (ppp->debug & 1)
2092                 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
2093         ++ppp->dev->stats.tx_errors;
2094         ++ppp->nxseq;
2095         return 1;       /* abandon the frame */
2096 }
2097 #endif /* CONFIG_PPP_MULTILINK */
2098
2099 /* Try to send data out on a channel */
2100 static void __ppp_channel_push(struct channel *pch)
2101 {
2102         struct sk_buff *skb;
2103         struct ppp *ppp;
2104
2105         spin_lock(&pch->downl);
2106         if (pch->chan) {
2107                 while (!skb_queue_empty(&pch->file.xq)) {
2108                         skb = skb_dequeue(&pch->file.xq);
2109                         if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
2110                                 /* put the packet back and try again later */
2111                                 skb_queue_head(&pch->file.xq, skb);
2112                                 break;
2113                         }
2114                 }
2115         } else {
2116                 /* channel got deregistered */
2117                 skb_queue_purge(&pch->file.xq);
2118         }
2119         spin_unlock(&pch->downl);
2120         /* see if there is anything from the attached unit to be sent */
2121         if (skb_queue_empty(&pch->file.xq)) {
2122                 ppp = pch->ppp;
2123                 if (ppp)
2124                         __ppp_xmit_process(ppp, NULL);
2125         }
2126 }
2127
2128 static void ppp_channel_push(struct channel *pch)
2129 {
2130         read_lock_bh(&pch->upl);
2131         if (pch->ppp) {
2132                 (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
2133                 __ppp_channel_push(pch);
2134                 (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
2135         } else {
2136                 __ppp_channel_push(pch);
2137         }
2138         read_unlock_bh(&pch->upl);
2139 }
2140
2141 /*
2142  * Receive-side routines.
2143  */
2144
2145 struct ppp_mp_skb_parm {
2146         u32             sequence;
2147         u8              BEbits;
2148 };
2149 #define PPP_MP_CB(skb)  ((struct ppp_mp_skb_parm *)((skb)->cb))
2150
2151 static inline void
2152 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2153 {
2154         ppp_recv_lock(ppp);
2155         if (!ppp->closing)
2156                 ppp_receive_frame(ppp, skb, pch);
2157         else
2158                 kfree_skb(skb);
2159         ppp_recv_unlock(ppp);
2160 }
2161
2162 /**
2163  * __ppp_decompress_proto - Decompress protocol field, slim version.
2164  * @skb: Socket buffer where protocol field should be decompressed. It must have
2165  *       at least 1 byte of head room and 1 byte of linear data. First byte of
2166  *       data must be a protocol field byte.
2167  *
2168  * Decompress protocol field in PPP header if it's compressed, e.g. when
2169  * Protocol-Field-Compression (PFC) was negotiated. No checks w.r.t. skb data
2170  * length are done in this function.
2171  */
2172 static void __ppp_decompress_proto(struct sk_buff *skb)
2173 {
2174         if (skb->data[0] & 0x01)
2175                 *(u8 *)skb_push(skb, 1) = 0x00;
2176 }
2177
2178 /**
2179  * ppp_decompress_proto - Check skb data room and decompress protocol field.
2180  * @skb: Socket buffer where protocol field should be decompressed. First byte
2181  *       of data must be a protocol field byte.
2182  *
2183  * Decompress protocol field in PPP header if it's compressed, e.g. when
2184  * Protocol-Field-Compression (PFC) was negotiated. This function also makes
2185  * sure that skb data room is sufficient for Protocol field, before and after
2186  * decompression.
2187  *
2188  * Return: true - decompressed successfully, false - not enough room in skb.
2189  */
2190 static bool ppp_decompress_proto(struct sk_buff *skb)
2191 {
2192         /* At least one byte should be present (if protocol is compressed) */
2193         if (!pskb_may_pull(skb, 1))
2194                 return false;
2195
2196         __ppp_decompress_proto(skb);
2197
2198         /* Protocol field should occupy 2 bytes when not compressed */
2199         return pskb_may_pull(skb, 2);
2200 }
2201
2202 /* Attempt to handle a frame via. a bridged channel, if one exists.
2203  * If the channel is bridged, the frame is consumed by the bridge.
2204  * If not, the caller must handle the frame by normal recv mechanisms.
2205  * Returns true if the frame is consumed, false otherwise.
2206  */
2207 static bool ppp_channel_bridge_input(struct channel *pch, struct sk_buff *skb)
2208 {
2209         struct channel *pchb;
2210
2211         rcu_read_lock();
2212         pchb = rcu_dereference(pch->bridge);
2213         if (!pchb)
2214                 goto out_rcu;
2215
2216         spin_lock(&pchb->downl);
2217         if (!pchb->chan) {
2218                 /* channel got unregistered */
2219                 kfree_skb(skb);
2220                 goto outl;
2221         }
2222
2223         skb_scrub_packet(skb, !net_eq(pch->chan_net, pchb->chan_net));
2224         if (!pchb->chan->ops->start_xmit(pchb->chan, skb))
2225                 kfree_skb(skb);
2226
2227 outl:
2228         spin_unlock(&pchb->downl);
2229 out_rcu:
2230         rcu_read_unlock();
2231
2232         /* If pchb is set then we've consumed the packet */
2233         return !!pchb;
2234 }
2235
2236 void
2237 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
2238 {
2239         struct channel *pch = chan->ppp;
2240         int proto;
2241
2242         if (!pch) {
2243                 kfree_skb(skb);
2244                 return;
2245         }
2246
2247         /* If the channel is bridged, transmit via. bridge */
2248         if (ppp_channel_bridge_input(pch, skb))
2249                 return;
2250
2251         read_lock_bh(&pch->upl);
2252         if (!ppp_decompress_proto(skb)) {
2253                 kfree_skb(skb);
2254                 if (pch->ppp) {
2255                         ++pch->ppp->dev->stats.rx_length_errors;
2256                         ppp_receive_error(pch->ppp);
2257                 }
2258                 goto done;
2259         }
2260
2261         proto = PPP_PROTO(skb);
2262         if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
2263                 /* put it on the channel queue */
2264                 skb_queue_tail(&pch->file.rq, skb);
2265                 /* drop old frames if queue too long */
2266                 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
2267                        (skb = skb_dequeue(&pch->file.rq)))
2268                         kfree_skb(skb);
2269                 wake_up_interruptible(&pch->file.rwait);
2270         } else {
2271                 ppp_do_recv(pch->ppp, skb, pch);
2272         }
2273
2274 done:
2275         read_unlock_bh(&pch->upl);
2276 }
2277
2278 /* Put a 0-length skb in the receive queue as an error indication */
2279 void
2280 ppp_input_error(struct ppp_channel *chan, int code)
2281 {
2282         struct channel *pch = chan->ppp;
2283         struct sk_buff *skb;
2284
2285         if (!pch)
2286                 return;
2287
2288         read_lock_bh(&pch->upl);
2289         if (pch->ppp) {
2290                 skb = alloc_skb(0, GFP_ATOMIC);
2291                 if (skb) {
2292                         skb->len = 0;           /* probably unnecessary */
2293                         skb->cb[0] = code;
2294                         ppp_do_recv(pch->ppp, skb, pch);
2295                 }
2296         }
2297         read_unlock_bh(&pch->upl);
2298 }
2299
2300 /*
2301  * We come in here to process a received frame.
2302  * The receive side of the ppp unit is locked.
2303  */
2304 static void
2305 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2306 {
2307         /* note: a 0-length skb is used as an error indication */
2308         if (skb->len > 0) {
2309                 skb_checksum_complete_unset(skb);
2310 #ifdef CONFIG_PPP_MULTILINK
2311                 /* XXX do channel-level decompression here */
2312                 if (PPP_PROTO(skb) == PPP_MP)
2313                         ppp_receive_mp_frame(ppp, skb, pch);
2314                 else
2315 #endif /* CONFIG_PPP_MULTILINK */
2316                         ppp_receive_nonmp_frame(ppp, skb);
2317         } else {
2318                 kfree_skb(skb);
2319                 ppp_receive_error(ppp);
2320         }
2321 }
2322
2323 static void
2324 ppp_receive_error(struct ppp *ppp)
2325 {
2326         ++ppp->dev->stats.rx_errors;
2327         if (ppp->vj)
2328                 slhc_toss(ppp->vj);
2329 }
2330
2331 static void
2332 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2333 {
2334         struct sk_buff *ns;
2335         int proto, len, npi;
2336
2337         /*
2338          * Decompress the frame, if compressed.
2339          * Note that some decompressors need to see uncompressed frames
2340          * that come in as well as compressed frames.
2341          */
2342         if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2343             (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
2344                 skb = ppp_decompress_frame(ppp, skb);
2345
2346         if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2347                 goto err;
2348
2349         /* At this point the "Protocol" field MUST be decompressed, either in
2350          * ppp_input(), ppp_decompress_frame() or in ppp_receive_mp_frame().
2351          */
2352         proto = PPP_PROTO(skb);
2353         switch (proto) {
2354         case PPP_VJC_COMP:
2355                 /* decompress VJ compressed packets */
2356                 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2357                         goto err;
2358
2359                 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
2360                         /* copy to a new sk_buff with more tailroom */
2361                         ns = dev_alloc_skb(skb->len + 128);
2362                         if (!ns) {
2363                                 netdev_err(ppp->dev, "PPP: no memory "
2364                                            "(VJ decomp)\n");
2365                                 goto err;
2366                         }
2367                         skb_reserve(ns, 2);
2368                         skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
2369                         consume_skb(skb);
2370                         skb = ns;
2371                 }
2372                 else
2373                         skb->ip_summed = CHECKSUM_NONE;
2374
2375                 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2376                 if (len <= 0) {
2377                         netdev_printk(KERN_DEBUG, ppp->dev,
2378                                       "PPP: VJ decompression error\n");
2379                         goto err;
2380                 }
2381                 len += 2;
2382                 if (len > skb->len)
2383                         skb_put(skb, len - skb->len);
2384                 else if (len < skb->len)
2385                         skb_trim(skb, len);
2386                 proto = PPP_IP;
2387                 break;
2388
2389         case PPP_VJC_UNCOMP:
2390                 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2391                         goto err;
2392
2393                 /* Until we fix the decompressor need to make sure
2394                  * data portion is linear.
2395                  */
2396                 if (!pskb_may_pull(skb, skb->len))
2397                         goto err;
2398
2399                 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
2400                         netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
2401                         goto err;
2402                 }
2403                 proto = PPP_IP;
2404                 break;
2405
2406         case PPP_CCP:
2407                 ppp_ccp_peek(ppp, skb, 1);
2408                 break;
2409         }
2410
2411         ++ppp->stats64.rx_packets;
2412         ppp->stats64.rx_bytes += skb->len - 2;
2413
2414         npi = proto_to_npindex(proto);
2415         if (npi < 0) {
2416                 /* control or unknown frame - pass it to pppd */
2417                 skb_queue_tail(&ppp->file.rq, skb);
2418                 /* limit queue length by dropping old frames */
2419                 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2420                        (skb = skb_dequeue(&ppp->file.rq)))
2421                         kfree_skb(skb);
2422                 /* wake up any process polling or blocking on read */
2423                 wake_up_interruptible(&ppp->file.rwait);
2424
2425         } else {
2426                 /* network protocol frame - give it to the kernel */
2427
2428 #ifdef CONFIG_PPP_FILTER
2429                 /* check if the packet passes the pass and active filters */
2430                 /* the filter instructions are constructed assuming
2431                    a four-byte PPP header on each packet */
2432                 if (ppp->pass_filter || ppp->active_filter) {
2433                         if (skb_unclone(skb, GFP_ATOMIC))
2434                                 goto err;
2435
2436                         *(u8 *)skb_push(skb, 2) = 0;
2437                         if (ppp->pass_filter &&
2438                             BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
2439                                 if (ppp->debug & 1)
2440                                         netdev_printk(KERN_DEBUG, ppp->dev,
2441                                                       "PPP: inbound frame "
2442                                                       "not passed\n");
2443                                 kfree_skb(skb);
2444                                 return;
2445                         }
2446                         if (!(ppp->active_filter &&
2447                               BPF_PROG_RUN(ppp->active_filter, skb) == 0))
2448                                 ppp->last_recv = jiffies;
2449                         __skb_pull(skb, 2);
2450                 } else
2451 #endif /* CONFIG_PPP_FILTER */
2452                         ppp->last_recv = jiffies;
2453
2454                 if ((ppp->dev->flags & IFF_UP) == 0 ||
2455                     ppp->npmode[npi] != NPMODE_PASS) {
2456                         kfree_skb(skb);
2457                 } else {
2458                         /* chop off protocol */
2459                         skb_pull_rcsum(skb, 2);
2460                         skb->dev = ppp->dev;
2461                         skb->protocol = htons(npindex_to_ethertype[npi]);
2462                         skb_reset_mac_header(skb);
2463                         skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2464                                                       dev_net(ppp->dev)));
2465                         netif_rx(skb);
2466                 }
2467         }
2468         return;
2469
2470  err:
2471         kfree_skb(skb);
2472         ppp_receive_error(ppp);
2473 }
2474
2475 static struct sk_buff *
2476 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2477 {
2478         int proto = PPP_PROTO(skb);
2479         struct sk_buff *ns;
2480         int len;
2481
2482         /* Until we fix all the decompressor's need to make sure
2483          * data portion is linear.
2484          */
2485         if (!pskb_may_pull(skb, skb->len))
2486                 goto err;
2487
2488         if (proto == PPP_COMP) {
2489                 int obuff_size;
2490
2491                 switch(ppp->rcomp->compress_proto) {
2492                 case CI_MPPE:
2493                         obuff_size = ppp->mru + PPP_HDRLEN + 1;
2494                         break;
2495                 default:
2496                         obuff_size = ppp->mru + PPP_HDRLEN;
2497                         break;
2498                 }
2499
2500                 ns = dev_alloc_skb(obuff_size);
2501                 if (!ns) {
2502                         netdev_err(ppp->dev, "ppp_decompress_frame: "
2503                                    "no memory\n");
2504                         goto err;
2505                 }
2506                 /* the decompressor still expects the A/C bytes in the hdr */
2507                 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
2508                                 skb->len + 2, ns->data, obuff_size);
2509                 if (len < 0) {
2510                         /* Pass the compressed frame to pppd as an
2511                            error indication. */
2512                         if (len == DECOMP_FATALERROR)
2513                                 ppp->rstate |= SC_DC_FERROR;
2514                         kfree_skb(ns);
2515                         goto err;
2516                 }
2517
2518                 consume_skb(skb);
2519                 skb = ns;
2520                 skb_put(skb, len);
2521                 skb_pull(skb, 2);       /* pull off the A/C bytes */
2522
2523                 /* Don't call __ppp_decompress_proto() here, but instead rely on
2524                  * corresponding algo (mppe/bsd/deflate) to decompress it.
2525                  */
2526         } else {
2527                 /* Uncompressed frame - pass to decompressor so it
2528                    can update its dictionary if necessary. */
2529                 if (ppp->rcomp->incomp)
2530                         ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2531                                            skb->len + 2);
2532         }
2533
2534         return skb;
2535
2536  err:
2537         ppp->rstate |= SC_DC_ERROR;
2538         ppp_receive_error(ppp);
2539         return skb;
2540 }
2541
2542 #ifdef CONFIG_PPP_MULTILINK
2543 /*
2544  * Receive a multilink frame.
2545  * We put it on the reconstruction queue and then pull off
2546  * as many completed frames as we can.
2547  */
2548 static void
2549 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2550 {
2551         u32 mask, seq;
2552         struct channel *ch;
2553         int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2554
2555         if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
2556                 goto err;               /* no good, throw it away */
2557
2558         /* Decode sequence number and begin/end bits */
2559         if (ppp->flags & SC_MP_SHORTSEQ) {
2560                 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2561                 mask = 0xfff;
2562         } else {
2563                 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2564                 mask = 0xffffff;
2565         }
2566         PPP_MP_CB(skb)->BEbits = skb->data[2];
2567         skb_pull(skb, mphdrlen);        /* pull off PPP and MP headers */
2568
2569         /*
2570          * Do protocol ID decompression on the first fragment of each packet.
2571          * We have to do that here, because ppp_receive_nonmp_frame() expects
2572          * decompressed protocol field.
2573          */
2574         if (PPP_MP_CB(skb)->BEbits & B)
2575                 __ppp_decompress_proto(skb);
2576
2577         /*
2578          * Expand sequence number to 32 bits, making it as close
2579          * as possible to ppp->minseq.
2580          */
2581         seq |= ppp->minseq & ~mask;
2582         if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2583                 seq += mask + 1;
2584         else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2585                 seq -= mask + 1;        /* should never happen */
2586         PPP_MP_CB(skb)->sequence = seq;
2587         pch->lastseq = seq;
2588
2589         /*
2590          * If this packet comes before the next one we were expecting,
2591          * drop it.
2592          */
2593         if (seq_before(seq, ppp->nextseq)) {
2594                 kfree_skb(skb);
2595                 ++ppp->dev->stats.rx_dropped;
2596                 ppp_receive_error(ppp);
2597                 return;
2598         }
2599
2600         /*
2601          * Reevaluate minseq, the minimum over all channels of the
2602          * last sequence number received on each channel.  Because of
2603          * the increasing sequence number rule, we know that any fragment
2604          * before `minseq' which hasn't arrived is never going to arrive.
2605          * The list of channels can't change because we have the receive
2606          * side of the ppp unit locked.
2607          */
2608         list_for_each_entry(ch, &ppp->channels, clist) {
2609                 if (seq_before(ch->lastseq, seq))
2610                         seq = ch->lastseq;
2611         }
2612         if (seq_before(ppp->minseq, seq))
2613                 ppp->minseq = seq;
2614
2615         /* Put the fragment on the reconstruction queue */
2616         ppp_mp_insert(ppp, skb);
2617
2618         /* If the queue is getting long, don't wait any longer for packets
2619            before the start of the queue. */
2620         if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
2621                 struct sk_buff *mskb = skb_peek(&ppp->mrq);
2622                 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2623                         ppp->minseq = PPP_MP_CB(mskb)->sequence;
2624         }
2625
2626         /* Pull completed packets off the queue and receive them. */
2627         while ((skb = ppp_mp_reconstruct(ppp))) {
2628                 if (pskb_may_pull(skb, 2))
2629                         ppp_receive_nonmp_frame(ppp, skb);
2630                 else {
2631                         ++ppp->dev->stats.rx_length_errors;
2632                         kfree_skb(skb);
2633                         ppp_receive_error(ppp);
2634                 }
2635         }
2636
2637         return;
2638
2639  err:
2640         kfree_skb(skb);
2641         ppp_receive_error(ppp);
2642 }
2643
2644 /*
2645  * Insert a fragment on the MP reconstruction queue.
2646  * The queue is ordered by increasing sequence number.
2647  */
2648 static void
2649 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2650 {
2651         struct sk_buff *p;
2652         struct sk_buff_head *list = &ppp->mrq;
2653         u32 seq = PPP_MP_CB(skb)->sequence;
2654
2655         /* N.B. we don't need to lock the list lock because we have the
2656            ppp unit receive-side lock. */
2657         skb_queue_walk(list, p) {
2658                 if (seq_before(seq, PPP_MP_CB(p)->sequence))
2659                         break;
2660         }
2661         __skb_queue_before(list, p, skb);
2662 }
2663
2664 /*
2665  * Reconstruct a packet from the MP fragment queue.
2666  * We go through increasing sequence numbers until we find a
2667  * complete packet, or we get to the sequence number for a fragment
2668  * which hasn't arrived but might still do so.
2669  */
2670 static struct sk_buff *
2671 ppp_mp_reconstruct(struct ppp *ppp)
2672 {
2673         u32 seq = ppp->nextseq;
2674         u32 minseq = ppp->minseq;
2675         struct sk_buff_head *list = &ppp->mrq;
2676         struct sk_buff *p, *tmp;
2677         struct sk_buff *head, *tail;
2678         struct sk_buff *skb = NULL;
2679         int lost = 0, len = 0;
2680
2681         if (ppp->mrru == 0)     /* do nothing until mrru is set */
2682                 return NULL;
2683         head = __skb_peek(list);
2684         tail = NULL;
2685         skb_queue_walk_safe(list, p, tmp) {
2686         again:
2687                 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
2688                         /* this can't happen, anyway ignore the skb */
2689                         netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2690                                    "seq %u < %u\n",
2691                                    PPP_MP_CB(p)->sequence, seq);
2692                         __skb_unlink(p, list);
2693                         kfree_skb(p);
2694                         continue;
2695                 }
2696                 if (PPP_MP_CB(p)->sequence != seq) {
2697                         u32 oldseq;
2698                         /* Fragment `seq' is missing.  If it is after
2699                            minseq, it might arrive later, so stop here. */
2700                         if (seq_after(seq, minseq))
2701                                 break;
2702                         /* Fragment `seq' is lost, keep going. */
2703                         lost = 1;
2704                         oldseq = seq;
2705                         seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2706                                 minseq + 1: PPP_MP_CB(p)->sequence;
2707
2708                         if (ppp->debug & 1)
2709                                 netdev_printk(KERN_DEBUG, ppp->dev,
2710                                               "lost frag %u..%u\n",
2711                                               oldseq, seq-1);
2712
2713                         goto again;
2714                 }
2715
2716                 /*
2717                  * At this point we know that all the fragments from
2718                  * ppp->nextseq to seq are either present or lost.
2719                  * Also, there are no complete packets in the queue
2720                  * that have no missing fragments and end before this
2721                  * fragment.
2722                  */
2723
2724                 /* B bit set indicates this fragment starts a packet */
2725                 if (PPP_MP_CB(p)->BEbits & B) {
2726                         head = p;
2727                         lost = 0;
2728                         len = 0;
2729                 }
2730
2731                 len += p->len;
2732
2733                 /* Got a complete packet yet? */
2734                 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2735                     (PPP_MP_CB(head)->BEbits & B)) {
2736                         if (len > ppp->mrru + 2) {
2737                                 ++ppp->dev->stats.rx_length_errors;
2738                                 netdev_printk(KERN_DEBUG, ppp->dev,
2739                                               "PPP: reconstructed packet"
2740                                               " is too long (%d)\n", len);
2741                         } else {
2742                                 tail = p;
2743                                 break;
2744                         }
2745                         ppp->nextseq = seq + 1;
2746                 }
2747
2748                 /*
2749                  * If this is the ending fragment of a packet,
2750                  * and we haven't found a complete valid packet yet,
2751                  * we can discard up to and including this fragment.
2752                  */
2753                 if (PPP_MP_CB(p)->BEbits & E) {
2754                         struct sk_buff *tmp2;
2755
2756                         skb_queue_reverse_walk_from_safe(list, p, tmp2) {
2757                                 if (ppp->debug & 1)
2758                                         netdev_printk(KERN_DEBUG, ppp->dev,
2759                                                       "discarding frag %u\n",
2760                                                       PPP_MP_CB(p)->sequence);
2761                                 __skb_unlink(p, list);
2762                                 kfree_skb(p);
2763                         }
2764                         head = skb_peek(list);
2765                         if (!head)
2766                                 break;
2767                 }
2768                 ++seq;
2769         }
2770
2771         /* If we have a complete packet, copy it all into one skb. */
2772         if (tail != NULL) {
2773                 /* If we have discarded any fragments,
2774                    signal a receive error. */
2775                 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
2776                         skb_queue_walk_safe(list, p, tmp) {
2777                                 if (p == head)
2778                                         break;
2779                                 if (ppp->debug & 1)
2780                                         netdev_printk(KERN_DEBUG, ppp->dev,
2781                                                       "discarding frag %u\n",
2782                                                       PPP_MP_CB(p)->sequence);
2783                                 __skb_unlink(p, list);
2784                                 kfree_skb(p);
2785                         }
2786
2787                         if (ppp->debug & 1)
2788                                 netdev_printk(KERN_DEBUG, ppp->dev,
2789                                               "  missed pkts %u..%u\n",
2790                                               ppp->nextseq,
2791                                               PPP_MP_CB(head)->sequence-1);
2792                         ++ppp->dev->stats.rx_dropped;
2793                         ppp_receive_error(ppp);
2794                 }
2795
2796                 skb = head;
2797                 if (head != tail) {
2798                         struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2799                         p = skb_queue_next(list, head);
2800                         __skb_unlink(skb, list);
2801                         skb_queue_walk_from_safe(list, p, tmp) {
2802                                 __skb_unlink(p, list);
2803                                 *fragpp = p;
2804                                 p->next = NULL;
2805                                 fragpp = &p->next;
2806
2807                                 skb->len += p->len;
2808                                 skb->data_len += p->len;
2809                                 skb->truesize += p->truesize;
2810
2811                                 if (p == tail)
2812                                         break;
2813                         }
2814                 } else {
2815                         __skb_unlink(skb, list);
2816                 }
2817
2818                 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
2819         }
2820
2821         return skb;
2822 }
2823 #endif /* CONFIG_PPP_MULTILINK */
2824
2825 /*
2826  * Channel interface.
2827  */
2828
2829 /* Create a new, unattached ppp channel. */
2830 int ppp_register_channel(struct ppp_channel *chan)
2831 {
2832         return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2833 }
2834
2835 /* Create a new, unattached ppp channel for specified net. */
2836 int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
2837 {
2838         struct channel *pch;
2839         struct ppp_net *pn;
2840
2841         pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
2842         if (!pch)
2843                 return -ENOMEM;
2844
2845         pn = ppp_pernet(net);
2846
2847         pch->ppp = NULL;
2848         pch->chan = chan;
2849         pch->chan_net = get_net(net);
2850         chan->ppp = pch;
2851         init_ppp_file(&pch->file, CHANNEL);
2852         pch->file.hdrlen = chan->hdrlen;
2853 #ifdef CONFIG_PPP_MULTILINK
2854         pch->lastseq = -1;
2855 #endif /* CONFIG_PPP_MULTILINK */
2856         init_rwsem(&pch->chan_sem);
2857         spin_lock_init(&pch->downl);
2858         rwlock_init(&pch->upl);
2859
2860         spin_lock_bh(&pn->all_channels_lock);
2861         pch->file.index = ++pn->last_channel_index;
2862         list_add(&pch->list, &pn->new_channels);
2863         atomic_inc(&channel_count);
2864         spin_unlock_bh(&pn->all_channels_lock);
2865
2866         return 0;
2867 }
2868
2869 /*
2870  * Return the index of a channel.
2871  */
2872 int ppp_channel_index(struct ppp_channel *chan)
2873 {
2874         struct channel *pch = chan->ppp;
2875
2876         if (pch)
2877                 return pch->file.index;
2878         return -1;
2879 }
2880
2881 /*
2882  * Return the PPP unit number to which a channel is connected.
2883  */
2884 int ppp_unit_number(struct ppp_channel *chan)
2885 {
2886         struct channel *pch = chan->ppp;
2887         int unit = -1;
2888
2889         if (pch) {
2890                 read_lock_bh(&pch->upl);
2891                 if (pch->ppp)
2892                         unit = pch->ppp->file.index;
2893                 read_unlock_bh(&pch->upl);
2894         }
2895         return unit;
2896 }
2897
2898 /*
2899  * Return the PPP device interface name of a channel.
2900  */
2901 char *ppp_dev_name(struct ppp_channel *chan)
2902 {
2903         struct channel *pch = chan->ppp;
2904         char *name = NULL;
2905
2906         if (pch) {
2907                 read_lock_bh(&pch->upl);
2908                 if (pch->ppp && pch->ppp->dev)
2909                         name = pch->ppp->dev->name;
2910                 read_unlock_bh(&pch->upl);
2911         }
2912         return name;
2913 }
2914
2915
2916 /*
2917  * Disconnect a channel from the generic layer.
2918  * This must be called in process context.
2919  */
2920 void
2921 ppp_unregister_channel(struct ppp_channel *chan)
2922 {
2923         struct channel *pch = chan->ppp;
2924         struct ppp_net *pn;
2925
2926         if (!pch)
2927                 return;         /* should never happen */
2928
2929         chan->ppp = NULL;
2930
2931         /*
2932          * This ensures that we have returned from any calls into the
2933          * the channel's start_xmit or ioctl routine before we proceed.
2934          */
2935         down_write(&pch->chan_sem);
2936         spin_lock_bh(&pch->downl);
2937         pch->chan = NULL;
2938         spin_unlock_bh(&pch->downl);
2939         up_write(&pch->chan_sem);
2940         ppp_disconnect_channel(pch);
2941
2942         pn = ppp_pernet(pch->chan_net);
2943         spin_lock_bh(&pn->all_channels_lock);
2944         list_del(&pch->list);
2945         spin_unlock_bh(&pn->all_channels_lock);
2946
2947         ppp_unbridge_channels(pch);
2948
2949         pch->file.dead = 1;
2950         wake_up_interruptible(&pch->file.rwait);
2951
2952         if (refcount_dec_and_test(&pch->file.refcnt))
2953                 ppp_destroy_channel(pch);
2954 }
2955
2956 /*
2957  * Callback from a channel when it can accept more to transmit.
2958  * This should be called at BH/softirq level, not interrupt level.
2959  */
2960 void
2961 ppp_output_wakeup(struct ppp_channel *chan)
2962 {
2963         struct channel *pch = chan->ppp;
2964
2965         if (!pch)
2966                 return;
2967         ppp_channel_push(pch);
2968 }
2969
2970 /*
2971  * Compression control.
2972  */
2973
2974 /* Process the PPPIOCSCOMPRESS ioctl. */
2975 static int
2976 ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data)
2977 {
2978         int err = -EFAULT;
2979         struct compressor *cp, *ocomp;
2980         void *state, *ostate;
2981         unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2982
2983         if (data->length > CCP_MAX_OPTION_LENGTH)
2984                 goto out;
2985         if (copy_from_user(ccp_option, data->ptr, data->length))
2986                 goto out;
2987
2988         err = -EINVAL;
2989         if (data->length < 2 || ccp_option[1] < 2 || ccp_option[1] > data->length)
2990                 goto out;
2991
2992         cp = try_then_request_module(
2993                 find_compressor(ccp_option[0]),
2994                 "ppp-compress-%d", ccp_option[0]);
2995         if (!cp)
2996                 goto out;
2997
2998         err = -ENOBUFS;
2999         if (data->transmit) {
3000                 state = cp->comp_alloc(ccp_option, data->length);
3001                 if (state) {
3002                         ppp_xmit_lock(ppp);
3003                         ppp->xstate &= ~SC_COMP_RUN;
3004                         ocomp = ppp->xcomp;
3005                         ostate = ppp->xc_state;
3006                         ppp->xcomp = cp;
3007                         ppp->xc_state = state;
3008                         ppp_xmit_unlock(ppp);
3009                         if (ostate) {
3010                                 ocomp->comp_free(ostate);
3011                                 module_put(ocomp->owner);
3012                         }
3013                         err = 0;
3014                 } else
3015                         module_put(cp->owner);
3016
3017         } else {
3018                 state = cp->decomp_alloc(ccp_option, data->length);
3019                 if (state) {
3020                         ppp_recv_lock(ppp);
3021                         ppp->rstate &= ~SC_DECOMP_RUN;
3022                         ocomp = ppp->rcomp;
3023                         ostate = ppp->rc_state;
3024                         ppp->rcomp = cp;
3025                         ppp->rc_state = state;
3026                         ppp_recv_unlock(ppp);
3027                         if (ostate) {
3028                                 ocomp->decomp_free(ostate);
3029                                 module_put(ocomp->owner);
3030                         }
3031                         err = 0;
3032                 } else
3033                         module_put(cp->owner);
3034         }
3035
3036  out:
3037         return err;
3038 }
3039
3040 /*
3041  * Look at a CCP packet and update our state accordingly.
3042  * We assume the caller has the xmit or recv path locked.
3043  */
3044 static void
3045 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
3046 {
3047         unsigned char *dp;
3048         int len;
3049
3050         if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
3051                 return; /* no header */
3052         dp = skb->data + 2;
3053
3054         switch (CCP_CODE(dp)) {
3055         case CCP_CONFREQ:
3056
3057                 /* A ConfReq starts negotiation of compression
3058                  * in one direction of transmission,
3059                  * and hence brings it down...but which way?
3060                  *
3061                  * Remember:
3062                  * A ConfReq indicates what the sender would like to receive
3063                  */
3064                 if(inbound)
3065                         /* He is proposing what I should send */
3066                         ppp->xstate &= ~SC_COMP_RUN;
3067                 else
3068                         /* I am proposing to what he should send */
3069                         ppp->rstate &= ~SC_DECOMP_RUN;
3070
3071                 break;
3072
3073         case CCP_TERMREQ:
3074         case CCP_TERMACK:
3075                 /*
3076                  * CCP is going down, both directions of transmission
3077                  */
3078                 ppp->rstate &= ~SC_DECOMP_RUN;
3079                 ppp->xstate &= ~SC_COMP_RUN;
3080                 break;
3081
3082         case CCP_CONFACK:
3083                 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
3084                         break;
3085                 len = CCP_LENGTH(dp);
3086                 if (!pskb_may_pull(skb, len + 2))
3087                         return;         /* too short */
3088                 dp += CCP_HDRLEN;
3089                 len -= CCP_HDRLEN;
3090                 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
3091                         break;
3092                 if (inbound) {
3093                         /* we will start receiving compressed packets */
3094                         if (!ppp->rc_state)
3095                                 break;
3096                         if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
3097                                         ppp->file.index, 0, ppp->mru, ppp->debug)) {
3098                                 ppp->rstate |= SC_DECOMP_RUN;
3099                                 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
3100                         }
3101                 } else {
3102                         /* we will soon start sending compressed packets */
3103                         if (!ppp->xc_state)
3104                                 break;
3105                         if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
3106                                         ppp->file.index, 0, ppp->debug))
3107                                 ppp->xstate |= SC_COMP_RUN;
3108                 }
3109                 break;
3110
3111         case CCP_RESETACK:
3112                 /* reset the [de]compressor */
3113                 if ((ppp->flags & SC_CCP_UP) == 0)
3114                         break;
3115                 if (inbound) {
3116                         if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
3117                                 ppp->rcomp->decomp_reset(ppp->rc_state);
3118                                 ppp->rstate &= ~SC_DC_ERROR;
3119                         }
3120                 } else {
3121                         if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
3122                                 ppp->xcomp->comp_reset(ppp->xc_state);
3123                 }
3124                 break;
3125         }
3126 }
3127
3128 /* Free up compression resources. */
3129 static void
3130 ppp_ccp_closed(struct ppp *ppp)
3131 {
3132         void *xstate, *rstate;
3133         struct compressor *xcomp, *rcomp;
3134
3135         ppp_lock(ppp);
3136         ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
3137         ppp->xstate = 0;
3138         xcomp = ppp->xcomp;
3139         xstate = ppp->xc_state;
3140         ppp->xc_state = NULL;
3141         ppp->rstate = 0;
3142         rcomp = ppp->rcomp;
3143         rstate = ppp->rc_state;
3144         ppp->rc_state = NULL;
3145         ppp_unlock(ppp);
3146
3147         if (xstate) {
3148                 xcomp->comp_free(xstate);
3149                 module_put(xcomp->owner);
3150         }
3151         if (rstate) {
3152                 rcomp->decomp_free(rstate);
3153                 module_put(rcomp->owner);
3154         }
3155 }
3156
3157 /* List of compressors. */
3158 static LIST_HEAD(compressor_list);
3159 static DEFINE_SPINLOCK(compressor_list_lock);
3160
3161 struct compressor_entry {
3162         struct list_head list;
3163         struct compressor *comp;
3164 };
3165
3166 static struct compressor_entry *
3167 find_comp_entry(int proto)
3168 {
3169         struct compressor_entry *ce;
3170
3171         list_for_each_entry(ce, &compressor_list, list) {
3172                 if (ce->comp->compress_proto == proto)
3173                         return ce;
3174         }
3175         return NULL;
3176 }
3177
3178 /* Register a compressor */
3179 int
3180 ppp_register_compressor(struct compressor *cp)
3181 {
3182         struct compressor_entry *ce;
3183         int ret;
3184         spin_lock(&compressor_list_lock);
3185         ret = -EEXIST;
3186         if (find_comp_entry(cp->compress_proto))
3187                 goto out;
3188         ret = -ENOMEM;
3189         ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
3190         if (!ce)
3191                 goto out;
3192         ret = 0;
3193         ce->comp = cp;
3194         list_add(&ce->list, &compressor_list);
3195  out:
3196         spin_unlock(&compressor_list_lock);
3197         return ret;
3198 }
3199
3200 /* Unregister a compressor */
3201 void
3202 ppp_unregister_compressor(struct compressor *cp)
3203 {
3204         struct compressor_entry *ce;
3205
3206         spin_lock(&compressor_list_lock);
3207         ce = find_comp_entry(cp->compress_proto);
3208         if (ce && ce->comp == cp) {
3209                 list_del(&ce->list);
3210                 kfree(ce);
3211         }
3212         spin_unlock(&compressor_list_lock);
3213 }
3214
3215 /* Find a compressor. */
3216 static struct compressor *
3217 find_compressor(int type)
3218 {
3219         struct compressor_entry *ce;
3220         struct compressor *cp = NULL;
3221
3222         spin_lock(&compressor_list_lock);
3223         ce = find_comp_entry(type);
3224         if (ce) {
3225                 cp = ce->comp;
3226                 if (!try_module_get(cp->owner))
3227                         cp = NULL;
3228         }
3229         spin_unlock(&compressor_list_lock);
3230         return cp;
3231 }
3232
3233 /*
3234  * Miscelleneous stuff.
3235  */
3236
3237 static void
3238 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
3239 {
3240         struct slcompress *vj = ppp->vj;
3241
3242         memset(st, 0, sizeof(*st));
3243         st->p.ppp_ipackets = ppp->stats64.rx_packets;
3244         st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
3245         st->p.ppp_ibytes = ppp->stats64.rx_bytes;
3246         st->p.ppp_opackets = ppp->stats64.tx_packets;
3247         st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
3248         st->p.ppp_obytes = ppp->stats64.tx_bytes;
3249         if (!vj)
3250                 return;
3251         st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
3252         st->vj.vjs_compressed = vj->sls_o_compressed;
3253         st->vj.vjs_searches = vj->sls_o_searches;
3254         st->vj.vjs_misses = vj->sls_o_misses;
3255         st->vj.vjs_errorin = vj->sls_i_error;
3256         st->vj.vjs_tossed = vj->sls_i_tossed;
3257         st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
3258         st->vj.vjs_compressedin = vj->sls_i_compressed;
3259 }
3260
3261 /*
3262  * Stuff for handling the lists of ppp units and channels
3263  * and for initialization.
3264  */
3265
3266 /*
3267  * Create a new ppp interface unit.  Fails if it can't allocate memory
3268  * or if there is already a unit with the requested number.
3269  * unit == -1 means allocate a new number.
3270  */
3271 static int ppp_create_interface(struct net *net, struct file *file, int *unit)
3272 {
3273         struct ppp_config conf = {
3274                 .file = file,
3275                 .unit = *unit,
3276                 .ifname_is_set = false,
3277         };
3278         struct net_device *dev;
3279         struct ppp *ppp;
3280         int err;
3281
3282         dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
3283         if (!dev) {
3284                 err = -ENOMEM;
3285                 goto err;
3286         }
3287         dev_net_set(dev, net);
3288         dev->rtnl_link_ops = &ppp_link_ops;
3289
3290         rtnl_lock();
3291
3292         err = ppp_dev_configure(net, dev, &conf);
3293         if (err < 0)
3294                 goto err_dev;
3295         ppp = netdev_priv(dev);
3296         *unit = ppp->file.index;
3297
3298         rtnl_unlock();
3299
3300         return 0;
3301
3302 err_dev:
3303         rtnl_unlock();
3304         free_netdev(dev);
3305 err:
3306         return err;
3307 }
3308
3309 /*
3310  * Initialize a ppp_file structure.
3311  */
3312 static void
3313 init_ppp_file(struct ppp_file *pf, int kind)
3314 {
3315         pf->kind = kind;
3316         skb_queue_head_init(&pf->xq);
3317         skb_queue_head_init(&pf->rq);
3318         refcount_set(&pf->refcnt, 1);
3319         init_waitqueue_head(&pf->rwait);
3320 }
3321
3322 /*
3323  * Free the memory used by a ppp unit.  This is only called once
3324  * there are no channels connected to the unit and no file structs
3325  * that reference the unit.
3326  */
3327 static void ppp_destroy_interface(struct ppp *ppp)
3328 {
3329         atomic_dec(&ppp_unit_count);
3330
3331         if (!ppp->file.dead || ppp->n_channels) {
3332                 /* "can't happen" */
3333                 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3334                            "but dead=%d n_channels=%d !\n",
3335                            ppp, ppp->file.dead, ppp->n_channels);
3336                 return;
3337         }
3338
3339         ppp_ccp_closed(ppp);
3340         if (ppp->vj) {
3341                 slhc_free(ppp->vj);
3342                 ppp->vj = NULL;
3343         }
3344         skb_queue_purge(&ppp->file.xq);
3345         skb_queue_purge(&ppp->file.rq);
3346 #ifdef CONFIG_PPP_MULTILINK
3347         skb_queue_purge(&ppp->mrq);
3348 #endif /* CONFIG_PPP_MULTILINK */
3349 #ifdef CONFIG_PPP_FILTER
3350         if (ppp->pass_filter) {
3351                 bpf_prog_destroy(ppp->pass_filter);
3352                 ppp->pass_filter = NULL;
3353         }
3354
3355         if (ppp->active_filter) {
3356                 bpf_prog_destroy(ppp->active_filter);
3357                 ppp->active_filter = NULL;
3358         }
3359 #endif /* CONFIG_PPP_FILTER */
3360
3361         kfree_skb(ppp->xmit_pending);
3362         free_percpu(ppp->xmit_recursion);
3363
3364         free_netdev(ppp->dev);
3365 }
3366
3367 /*
3368  * Locate an existing ppp unit.
3369  * The caller should have locked the all_ppp_mutex.
3370  */
3371 static struct ppp *
3372 ppp_find_unit(struct ppp_net *pn, int unit)
3373 {
3374         return unit_find(&pn->units_idr, unit);
3375 }
3376
3377 /*
3378  * Locate an existing ppp channel.
3379  * The caller should have locked the all_channels_lock.
3380  * First we look in the new_channels list, then in the
3381  * all_channels list.  If found in the new_channels list,
3382  * we move it to the all_channels list.  This is for speed
3383  * when we have a lot of channels in use.
3384  */
3385 static struct channel *
3386 ppp_find_channel(struct ppp_net *pn, int unit)
3387 {
3388         struct channel *pch;
3389
3390         list_for_each_entry(pch, &pn->new_channels, list) {
3391                 if (pch->file.index == unit) {
3392                         list_move(&pch->list, &pn->all_channels);
3393                         return pch;
3394                 }
3395         }
3396
3397         list_for_each_entry(pch, &pn->all_channels, list) {
3398                 if (pch->file.index == unit)
3399                         return pch;
3400         }
3401
3402         return NULL;
3403 }
3404
3405 /*
3406  * Connect a PPP channel to a PPP interface unit.
3407  */
3408 static int
3409 ppp_connect_channel(struct channel *pch, int unit)
3410 {
3411         struct ppp *ppp;
3412         struct ppp_net *pn;
3413         int ret = -ENXIO;
3414         int hdrlen;
3415
3416         pn = ppp_pernet(pch->chan_net);
3417
3418         mutex_lock(&pn->all_ppp_mutex);
3419         ppp = ppp_find_unit(pn, unit);
3420         if (!ppp)
3421                 goto out;
3422         write_lock_bh(&pch->upl);
3423         ret = -EINVAL;
3424         if (pch->ppp ||
3425             rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl)))
3426                 goto outl;
3427
3428         ppp_lock(ppp);
3429         spin_lock_bh(&pch->downl);
3430         if (!pch->chan) {
3431                 /* Don't connect unregistered channels */
3432                 spin_unlock_bh(&pch->downl);
3433                 ppp_unlock(ppp);
3434                 ret = -ENOTCONN;
3435                 goto outl;
3436         }
3437         spin_unlock_bh(&pch->downl);
3438         if (pch->file.hdrlen > ppp->file.hdrlen)
3439                 ppp->file.hdrlen = pch->file.hdrlen;
3440         hdrlen = pch->file.hdrlen + 2;  /* for protocol bytes */
3441         if (hdrlen > ppp->dev->hard_header_len)
3442                 ppp->dev->hard_header_len = hdrlen;
3443         list_add_tail(&pch->clist, &ppp->channels);
3444         ++ppp->n_channels;
3445         pch->ppp = ppp;
3446         refcount_inc(&ppp->file.refcnt);
3447         ppp_unlock(ppp);
3448         ret = 0;
3449
3450  outl:
3451         write_unlock_bh(&pch->upl);
3452  out:
3453         mutex_unlock(&pn->all_ppp_mutex);
3454         return ret;
3455 }
3456
3457 /*
3458  * Disconnect a channel from its ppp unit.
3459  */
3460 static int
3461 ppp_disconnect_channel(struct channel *pch)
3462 {
3463         struct ppp *ppp;
3464         int err = -EINVAL;
3465
3466         write_lock_bh(&pch->upl);
3467         ppp = pch->ppp;
3468         pch->ppp = NULL;
3469         write_unlock_bh(&pch->upl);
3470         if (ppp) {
3471                 /* remove it from the ppp unit's list */
3472                 ppp_lock(ppp);
3473                 list_del(&pch->clist);
3474                 if (--ppp->n_channels == 0)
3475                         wake_up_interruptible(&ppp->file.rwait);
3476                 ppp_unlock(ppp);
3477                 if (refcount_dec_and_test(&ppp->file.refcnt))
3478                         ppp_destroy_interface(ppp);
3479                 err = 0;
3480         }
3481         return err;
3482 }
3483
3484 /*
3485  * Free up the resources used by a ppp channel.
3486  */
3487 static void ppp_destroy_channel(struct channel *pch)
3488 {
3489         put_net(pch->chan_net);
3490         pch->chan_net = NULL;
3491
3492         atomic_dec(&channel_count);
3493
3494         if (!pch->file.dead) {
3495                 /* "can't happen" */
3496                 pr_err("ppp: destroying undead channel %p !\n", pch);
3497                 return;
3498         }
3499         skb_queue_purge(&pch->file.xq);
3500         skb_queue_purge(&pch->file.rq);
3501         kfree(pch);
3502 }
3503
3504 static void __exit ppp_cleanup(void)
3505 {
3506         /* should never happen */
3507         if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
3508                 pr_err("PPP: removing module but units remain!\n");
3509         rtnl_link_unregister(&ppp_link_ops);
3510         unregister_chrdev(PPP_MAJOR, "ppp");
3511         device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
3512         class_destroy(ppp_class);
3513         unregister_pernet_device(&ppp_net_ops);
3514 }
3515
3516 /*
3517  * Units handling. Caller must protect concurrent access
3518  * by holding all_ppp_mutex
3519  */
3520
3521 /* associate pointer with specified number */
3522 static int unit_set(struct idr *p, void *ptr, int n)
3523 {
3524         int unit;
3525
3526         unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3527         if (unit == -ENOSPC)
3528                 unit = -EINVAL;
3529         return unit;
3530 }
3531
3532 /* get new free unit number and associate pointer with it */
3533 static int unit_get(struct idr *p, void *ptr)
3534 {
3535         return idr_alloc(p, ptr, 0, 0, GFP_KERNEL);
3536 }
3537
3538 /* put unit number back to a pool */
3539 static void unit_put(struct idr *p, int n)
3540 {
3541         idr_remove(p, n);
3542 }
3543
3544 /* get pointer associated with the number */
3545 static void *unit_find(struct idr *p, int n)
3546 {
3547         return idr_find(p, n);
3548 }
3549
3550 /* Module/initialization stuff */
3551
3552 module_init(ppp_init);
3553 module_exit(ppp_cleanup);
3554
3555 EXPORT_SYMBOL(ppp_register_net_channel);
3556 EXPORT_SYMBOL(ppp_register_channel);
3557 EXPORT_SYMBOL(ppp_unregister_channel);
3558 EXPORT_SYMBOL(ppp_channel_index);
3559 EXPORT_SYMBOL(ppp_unit_number);
3560 EXPORT_SYMBOL(ppp_dev_name);
3561 EXPORT_SYMBOL(ppp_input);
3562 EXPORT_SYMBOL(ppp_input_error);
3563 EXPORT_SYMBOL(ppp_output_wakeup);
3564 EXPORT_SYMBOL(ppp_register_compressor);
3565 EXPORT_SYMBOL(ppp_unregister_compressor);
3566 MODULE_LICENSE("GPL");
3567 MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
3568 MODULE_ALIAS_RTNL_LINK("ppp");
3569 MODULE_ALIAS("devname:ppp");