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