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