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