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