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