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