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