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