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