blob: b883af93929c409f33b4de47b376068698d26a4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/kmod.h>
28#include <linux/init.h>
29#include <linux/list.h>
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -080030#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/netdevice.h>
32#include <linux/poll.h>
33#include <linux/ppp_defs.h>
34#include <linux/filter.h>
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +000035#include <linux/ppp-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/ppp_channel.h>
37#include <linux/ppp-comp.h>
38#include <linux/skbuff.h>
39#include <linux/rtnetlink.h>
40#include <linux/if_arp.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/rwsem.h>
45#include <linux/stddef.h>
46#include <linux/device.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080047#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Guillaume Nault96d934c2016-04-28 17:55:30 +020049#include <linux/file.h>
Changli Gao96545ae2011-01-06 13:37:36 +000050#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/slhc_vj.h>
Arun Sharma600634972011-07-26 16:09:06 -070052#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Cyrill Gorcunov273ec512009-01-21 15:55:35 -080054#include <linux/nsproxy.h>
55#include <net/net_namespace.h>
56#include <net/netns/generic.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define PPP_VERSION "2.4.2"
59
60/*
61 * Network protocols we support.
62 */
63#define NP_IP 0 /* Internet Protocol V4 */
64#define NP_IPV6 1 /* Internet Protocol V6 */
65#define NP_IPX 2 /* IPX protocol */
66#define NP_AT 3 /* Appletalk protocol */
67#define NP_MPLS_UC 4 /* MPLS unicast */
68#define NP_MPLS_MC 5 /* MPLS multicast */
69#define NUM_NP 6 /* Number of NPs. */
70
71#define MPHDRLEN 6 /* multilink protocol header length */
72#define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * An instance of /dev/ppp can be associated with either a ppp
76 * interface unit or a ppp channel. In both cases, file->private_data
77 * points to one of these.
78 */
79struct ppp_file {
80 enum {
81 INTERFACE=1, CHANNEL
82 } kind;
83 struct sk_buff_head xq; /* pppd transmit queue */
84 struct sk_buff_head rq; /* receive queue for pppd */
85 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
86 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
87 int hdrlen; /* space to leave for headers */
88 int index; /* interface unit / channel number */
89 int dead; /* unit/channel has been shut down */
90};
91
Robert P. J. Dayb385a142007-02-10 01:46:25 -080092#define PF_TO_X(pf, X) container_of(pf, X, file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
95#define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +000098 * Data structure to hold primary network stats for which
99 * we want to use 64 bit storage. Other network stats
100 * are stored in dev->stats of the ppp strucute.
101 */
102struct ppp_link_stats {
103 u64 rx_packets;
104 u64 tx_packets;
105 u64 rx_bytes;
106 u64 tx_bytes;
107};
108
109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * Data structure describing one ppp unit.
111 * A ppp unit corresponds to a ppp network interface device
112 * and represents a multilink bundle.
113 * It can have 0 or more ppp channels connected to it.
114 */
115struct ppp {
116 struct ppp_file file; /* stuff for read/write/poll 0 */
117 struct file *owner; /* file that owns this unit 48 */
118 struct list_head channels; /* list of attached channels 4c */
119 int n_channels; /* how many channels are attached 54 */
120 spinlock_t rlock; /* lock for receive side 58 */
121 spinlock_t wlock; /* lock for transmit side 5c */
Gao Feng3b25bfc2017-07-17 18:34:42 +0800122 int *xmit_recursion __percpu; /* xmit recursion detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int mru; /* max receive unit 60 */
124 unsigned int flags; /* control bits 64 */
125 unsigned int xstate; /* transmit state bits 68 */
126 unsigned int rstate; /* receive state bits 6c */
127 int debug; /* debug flags 70 */
128 struct slcompress *vj; /* state for VJ header compression */
129 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
130 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
131 struct compressor *xcomp; /* transmit packet compressor 8c */
132 void *xc_state; /* its internal state 90 */
133 struct compressor *rcomp; /* receive decompressor 94 */
134 void *rc_state; /* its internal state 98 */
135 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
136 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
137 struct net_device *dev; /* network interface device a4 */
James Chapman739840d2008-12-17 12:02:16 +0000138 int closing; /* is device closing down? a8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#ifdef CONFIG_PPP_MULTILINK
140 int nxchan; /* next channel to send something on */
141 u32 nxseq; /* next sequence number to send */
142 int mrru; /* MP: max reconst. receive unit */
143 u32 nextseq; /* MP: seq no of next packet */
144 u32 minseq; /* MP: min of most recent seqnos */
145 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
146#endif /* CONFIG_PPP_MULTILINK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#ifdef CONFIG_PPP_FILTER
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700148 struct bpf_prog *pass_filter; /* filter for packets to pass */
149 struct bpf_prog *active_filter; /* filter for pkts to reset idle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#endif /* CONFIG_PPP_FILTER */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800151 struct net *ppp_net; /* the net we belong to */
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +0000152 struct ppp_link_stats stats64; /* 64 bit network stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
155/*
156 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
Matt Domschb3f9b922005-11-08 09:40:47 -0800157 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
158 * SC_MUST_COMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
160 * Bits in xstate: SC_COMP_RUN
161 */
162#define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
163 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
Matt Domschb3f9b922005-11-08 09:40:47 -0800164 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
167 * Private data structure for each channel.
168 * This includes the data structure used for multilink.
169 */
170struct channel {
171 struct ppp_file file; /* stuff for read/write/poll */
172 struct list_head list; /* link in all/new_channels list */
173 struct ppp_channel *chan; /* public channel data structure */
174 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
175 spinlock_t downl; /* protects `chan', file.xq dequeue */
176 struct ppp *ppp; /* ppp unit we're connected to */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800177 struct net *chan_net; /* the net channel belongs to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct list_head clist; /* link in list of channels per unit */
179 rwlock_t upl; /* protects `ppp' */
180#ifdef CONFIG_PPP_MULTILINK
181 u8 avail; /* flag used in multilink stuff */
182 u8 had_frag; /* >= 1 fragments have been sent */
183 u32 lastseq; /* MP: last sequence # received */
Lennart Sorensenfa44a732010-01-18 12:59:55 +0000184 int speed; /* speed of the corresponding ppp channel*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif /* CONFIG_PPP_MULTILINK */
186};
187
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200188struct ppp_config {
189 struct file *file;
190 s32 unit;
Guillaume Nault96d934c2016-04-28 17:55:30 +0200191 bool ifname_is_set;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * SMP locking issues:
196 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
197 * list and the ppp.n_channels field, you need to take both locks
198 * before you modify them.
199 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
200 * channel.downl.
201 */
202
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000203static DEFINE_MUTEX(ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static atomic_t ppp_unit_count = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static atomic_t channel_count = ATOMIC_INIT(0);
206
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800207/* per-net private data for this module */
Eric Dumazetf99189b2009-11-17 10:42:49 +0000208static int ppp_net_id __read_mostly;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800209struct ppp_net {
210 /* units to ppp mapping */
211 struct idr units_idr;
212
213 /*
214 * all_ppp_mutex protects the units_idr mapping.
215 * It also ensures that finding a ppp unit in the units_idr
216 * map and updating its file.refcnt field is atomic.
217 */
218 struct mutex all_ppp_mutex;
219
220 /* channels */
221 struct list_head all_channels;
222 struct list_head new_channels;
223 int last_channel_index;
224
225 /*
226 * all_channels_lock protects all_channels and
227 * last_channel_index, and the atomicity of find
228 * a channel and updating its file.refcnt field.
229 */
230 spinlock_t all_channels_lock;
231};
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/* Get the PPP protocol number from a skb */
Changli Gao96545ae2011-01-06 13:37:36 +0000234#define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236/* We limit the length of ppp->file.rq to this (arbitrary) value */
237#define PPP_MAX_RQLEN 32
238
239/*
240 * Maximum number of multilink fragments queued up.
241 * This has to be large enough to cope with the maximum latency of
242 * the slowest channel relative to the others. Strictly it should
243 * depend on the number of channels and their characteristics.
244 */
245#define PPP_MP_MAX_QLEN 128
246
247/* Multilink header bits. */
248#define B 0x80 /* this fragment begins a packet */
249#define E 0x40 /* this fragment ends a packet */
250
251/* Compare multilink sequence numbers (assumed to be 32 bits wide) */
252#define seq_before(a, b) ((s32)((a) - (b)) < 0)
253#define seq_after(a, b) ((s32)((a) - (b)) > 0)
254
255/* Prototypes. */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800256static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
257 struct file *file, unsigned int cmd, unsigned long arg);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +0000258static void ppp_xmit_process(struct ppp *ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
260static void ppp_push(struct ppp *ppp);
261static void ppp_channel_push(struct channel *pch);
262static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
263 struct channel *pch);
264static void ppp_receive_error(struct ppp *ppp);
265static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
266static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
267 struct sk_buff *skb);
268#ifdef CONFIG_PPP_MULTILINK
269static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
270 struct channel *pch);
271static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
272static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
273static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
274#endif /* CONFIG_PPP_MULTILINK */
275static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
276static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
277static void ppp_ccp_closed(struct ppp *ppp);
278static struct compressor *find_compressor(int type);
279static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200280static int ppp_create_interface(struct net *net, struct file *file, int *unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static void init_ppp_file(struct ppp_file *pf, int kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static void ppp_destroy_interface(struct ppp *ppp);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800283static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
284static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int ppp_connect_channel(struct channel *pch, int unit);
286static int ppp_disconnect_channel(struct channel *pch);
287static void ppp_destroy_channel(struct channel *pch);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -0800288static int unit_get(struct idr *p, void *ptr);
Cyrill Gorcunov85997572009-01-12 22:11:56 -0800289static int unit_set(struct idr *p, void *ptr, int n);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -0800290static void unit_put(struct idr *p, int n);
291static void *unit_find(struct idr *p, int n);
Guillaume Nault96d934c2016-04-28 17:55:30 +0200292static void ppp_setup(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Guillaume Nault79c441a2015-08-24 11:35:30 +0200294static const struct net_device_ops ppp_netdev_ops;
295
gregkh@suse.de56b22932005-03-23 10:01:41 -0800296static struct class *ppp_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800298/* per net-namespace data */
299static inline struct ppp_net *ppp_pernet(struct net *net)
300{
301 BUG_ON(!net);
302
303 return net_generic(net, ppp_net_id);
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/* Translates a PPP protocol number to a NP index (NP == network protocol) */
307static inline int proto_to_npindex(int proto)
308{
309 switch (proto) {
310 case PPP_IP:
311 return NP_IP;
312 case PPP_IPV6:
313 return NP_IPV6;
314 case PPP_IPX:
315 return NP_IPX;
316 case PPP_AT:
317 return NP_AT;
318 case PPP_MPLS_UC:
319 return NP_MPLS_UC;
320 case PPP_MPLS_MC:
321 return NP_MPLS_MC;
322 }
323 return -EINVAL;
324}
325
326/* Translates an NP index into a PPP protocol number */
327static const int npindex_to_proto[NUM_NP] = {
328 PPP_IP,
329 PPP_IPV6,
330 PPP_IPX,
331 PPP_AT,
332 PPP_MPLS_UC,
333 PPP_MPLS_MC,
334};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/* Translates an ethertype into an NP index */
337static inline int ethertype_to_npindex(int ethertype)
338{
339 switch (ethertype) {
340 case ETH_P_IP:
341 return NP_IP;
342 case ETH_P_IPV6:
343 return NP_IPV6;
344 case ETH_P_IPX:
345 return NP_IPX;
346 case ETH_P_PPPTALK:
347 case ETH_P_ATALK:
348 return NP_AT;
349 case ETH_P_MPLS_UC:
350 return NP_MPLS_UC;
351 case ETH_P_MPLS_MC:
352 return NP_MPLS_MC;
353 }
354 return -1;
355}
356
357/* Translates an NP index into an ethertype */
358static const int npindex_to_ethertype[NUM_NP] = {
359 ETH_P_IP,
360 ETH_P_IPV6,
361 ETH_P_IPX,
362 ETH_P_PPPTALK,
363 ETH_P_MPLS_UC,
364 ETH_P_MPLS_MC,
365};
366
367/*
368 * Locking shorthand.
369 */
370#define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
371#define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
372#define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
373#define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
374#define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
375 ppp_recv_lock(ppp); } while (0)
376#define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
377 ppp_xmit_unlock(ppp); } while (0)
378
379/*
380 * /dev/ppp device routines.
381 * The /dev/ppp device is used by pppd to control the ppp unit.
382 * It supports the read, write, ioctl and poll functions.
383 * Open instances of /dev/ppp can be in one of three states:
384 * unattached, attached to a ppp unit, or attached to a ppp channel.
385 */
386static int ppp_open(struct inode *inode, struct file *file)
387{
388 /*
389 * This could (should?) be enforced by the permissions on /dev/ppp.
390 */
391 if (!capable(CAP_NET_ADMIN))
392 return -EPERM;
393 return 0;
394}
395
Alan Coxf3ff8a42008-05-25 23:40:58 -0700396static int ppp_release(struct inode *unused, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct ppp_file *pf = file->private_data;
399 struct ppp *ppp;
400
Joe Perchescd228d52007-11-12 18:07:31 -0800401 if (pf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 file->private_data = NULL;
403 if (pf->kind == INTERFACE) {
404 ppp = PF_TO_PPP(pf);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200405 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (file == ppp->owner)
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200407 unregister_netdevice(ppp->dev);
408 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 if (atomic_dec_and_test(&pf->refcnt)) {
411 switch (pf->kind) {
412 case INTERFACE:
413 ppp_destroy_interface(PF_TO_PPP(pf));
414 break;
415 case CHANNEL:
416 ppp_destroy_channel(PF_TO_CHANNEL(pf));
417 break;
418 }
419 }
420 }
421 return 0;
422}
423
424static ssize_t ppp_read(struct file *file, char __user *buf,
425 size_t count, loff_t *ppos)
426{
427 struct ppp_file *pf = file->private_data;
428 DECLARE_WAITQUEUE(wait, current);
429 ssize_t ret;
430 struct sk_buff *skb = NULL;
Simon Arlott19937d02010-05-03 10:20:27 +0000431 struct iovec iov;
Al Viroba568402014-11-24 17:48:04 -0500432 struct iov_iter to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 ret = count;
435
Joe Perchescd228d52007-11-12 18:07:31 -0800436 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -ENXIO;
438 add_wait_queue(&pf->rwait, &wait);
439 for (;;) {
440 set_current_state(TASK_INTERRUPTIBLE);
441 skb = skb_dequeue(&pf->rq);
442 if (skb)
443 break;
444 ret = 0;
445 if (pf->dead)
446 break;
447 if (pf->kind == INTERFACE) {
448 /*
449 * Return 0 (EOF) on an interface that has no
450 * channels connected, unless it is looping
451 * network traffic (demand mode).
452 */
453 struct ppp *ppp = PF_TO_PPP(pf);
Guillaume Naultedffc212016-02-26 18:45:34 +0100454
455 ppp_recv_lock(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +0000456 if (ppp->n_channels == 0 &&
Guillaume Naultedffc212016-02-26 18:45:34 +0100457 (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
458 ppp_recv_unlock(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Guillaume Naultedffc212016-02-26 18:45:34 +0100460 }
461 ppp_recv_unlock(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 ret = -EAGAIN;
464 if (file->f_flags & O_NONBLOCK)
465 break;
466 ret = -ERESTARTSYS;
467 if (signal_pending(current))
468 break;
469 schedule();
470 }
471 set_current_state(TASK_RUNNING);
472 remove_wait_queue(&pf->rwait, &wait);
473
Joe Perchescd228d52007-11-12 18:07:31 -0800474 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto out;
476
477 ret = -EOVERFLOW;
478 if (skb->len > count)
479 goto outf;
480 ret = -EFAULT;
Simon Arlott19937d02010-05-03 10:20:27 +0000481 iov.iov_base = buf;
482 iov.iov_len = count;
Al Viroba568402014-11-24 17:48:04 -0500483 iov_iter_init(&to, READ, &iov, 1, count);
484 if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto outf;
486 ret = skb->len;
487
488 outf:
489 kfree_skb(skb);
490 out:
491 return ret;
492}
493
494static ssize_t ppp_write(struct file *file, const char __user *buf,
495 size_t count, loff_t *ppos)
496{
497 struct ppp_file *pf = file->private_data;
498 struct sk_buff *skb;
499 ssize_t ret;
500
Joe Perchescd228d52007-11-12 18:07:31 -0800501 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -ENXIO;
503 ret = -ENOMEM;
504 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -0800505 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto out;
507 skb_reserve(skb, pf->hdrlen);
508 ret = -EFAULT;
509 if (copy_from_user(skb_put(skb, count), buf, count)) {
510 kfree_skb(skb);
511 goto out;
512 }
513
514 skb_queue_tail(&pf->xq, skb);
515
516 switch (pf->kind) {
517 case INTERFACE:
518 ppp_xmit_process(PF_TO_PPP(pf));
519 break;
520 case CHANNEL:
521 ppp_channel_push(PF_TO_CHANNEL(pf));
522 break;
523 }
524
525 ret = count;
526
527 out:
528 return ret;
529}
530
531/* No kernel lock - fine */
532static unsigned int ppp_poll(struct file *file, poll_table *wait)
533{
534 struct ppp_file *pf = file->private_data;
535 unsigned int mask;
536
Joe Perchescd228d52007-11-12 18:07:31 -0800537 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539 poll_wait(file, &pf->rwait, wait);
540 mask = POLLOUT | POLLWRNORM;
Joe Perchescd228d52007-11-12 18:07:31 -0800541 if (skb_peek(&pf->rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 mask |= POLLIN | POLLRDNORM;
543 if (pf->dead)
544 mask |= POLLHUP;
545 else if (pf->kind == INTERFACE) {
546 /* see comment in ppp_read */
547 struct ppp *ppp = PF_TO_PPP(pf);
Guillaume Naultedffc212016-02-26 18:45:34 +0100548
549 ppp_recv_lock(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +0000550 if (ppp->n_channels == 0 &&
551 (ppp->flags & SC_LOOP_TRAFFIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 mask |= POLLIN | POLLRDNORM;
Guillaume Naultedffc212016-02-26 18:45:34 +0100553 ppp_recv_unlock(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
556 return mask;
557}
558
559#ifdef CONFIG_PPP_FILTER
560static int get_filter(void __user *arg, struct sock_filter **p)
561{
562 struct sock_fprog uprog;
563 struct sock_filter *code = NULL;
Christoph Schulz3916a312014-07-14 08:01:10 +0200564 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (copy_from_user(&uprog, arg, sizeof(uprog)))
567 return -EFAULT;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!uprog.len) {
570 *p = NULL;
571 return 0;
572 }
573
574 len = uprog.len * sizeof(struct sock_filter);
Julia Lawallb23d00e2010-05-21 22:18:59 +0000575 code = memdup_user(uprog.filter, len);
576 if (IS_ERR(code))
577 return PTR_ERR(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 *p = code;
580 return uprog.len;
581}
582#endif /* CONFIG_PPP_FILTER */
583
Alan Coxf3ff8a42008-05-25 23:40:58 -0700584static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100586 struct ppp_file *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct ppp *ppp;
588 int err = -EFAULT, val, val2, i;
589 struct ppp_idle idle;
590 struct npioctl npi;
591 int unit, cflags;
592 struct slcompress *vj;
593 void __user *argp = (void __user *)arg;
594 int __user *p = argp;
595
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100596 mutex_lock(&ppp_mutex);
597
598 pf = file->private_data;
599 if (!pf) {
600 err = ppp_unattached_ioctl(current->nsproxy->net_ns,
601 pf, file, cmd, arg);
602 goto out;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (cmd == PPPIOCDETACH) {
606 /*
607 * We have to be careful here... if the file descriptor
608 * has been dup'd, we could have another process in the
609 * middle of a poll using the same file *, so we had
610 * better not free the interface data structures -
611 * instead we fail the ioctl. Even in this case, we
612 * shut down the interface if we are the owner of it.
613 * Actually, we should get rid of PPPIOCDETACH, userland
614 * (i.e. pppd) could achieve the same effect by closing
615 * this fd and reopening /dev/ppp.
616 */
617 err = -EINVAL;
618 if (pf->kind == INTERFACE) {
619 ppp = PF_TO_PPP(pf);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200620 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (file == ppp->owner)
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200622 unregister_netdevice(ppp->dev);
623 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Al Viro24dff962014-10-08 23:44:00 -0400625 if (atomic_long_read(&file->f_count) < 2) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700626 ppp_release(NULL, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 err = 0;
628 } else
David S. Millerb48f8c22011-01-20 22:44:36 -0800629 pr_warn("PPPIOCDETACH file->f_count=%ld\n",
630 atomic_long_read(&file->f_count));
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 if (pf->kind == CHANNEL) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700635 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct ppp_channel *chan;
637
Alan Coxf3ff8a42008-05-25 23:40:58 -0700638 pch = PF_TO_CHANNEL(pf);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 switch (cmd) {
641 case PPPIOCCONNECT:
642 if (get_user(unit, p))
643 break;
644 err = ppp_connect_channel(pch, unit);
645 break;
646
647 case PPPIOCDISCONN:
648 err = ppp_disconnect_channel(pch);
649 break;
650
651 default:
652 down_read(&pch->chan_sem);
653 chan = pch->chan;
654 err = -ENOTTY;
655 if (chan && chan->ops->ioctl)
656 err = chan->ops->ioctl(chan, cmd, arg);
657 up_read(&pch->chan_sem);
658 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100659 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 if (pf->kind != INTERFACE) {
663 /* can't happen */
David S. Millerb48f8c22011-01-20 22:44:36 -0800664 pr_err("PPP: not interface or channel??\n");
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100665 err = -EINVAL;
666 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
669 ppp = PF_TO_PPP(pf);
670 switch (cmd) {
671 case PPPIOCSMRU:
672 if (get_user(val, p))
673 break;
674 ppp->mru = val;
675 err = 0;
676 break;
677
678 case PPPIOCSFLAGS:
679 if (get_user(val, p))
680 break;
681 ppp_lock(ppp);
682 cflags = ppp->flags & ~val;
Christoph Schulza9f559c2014-07-16 23:41:26 +0200683#ifdef CONFIG_PPP_MULTILINK
Christoph Schulzd762d032014-07-15 11:51:03 +0200684 if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
685 ppp->nextseq = 0;
Christoph Schulza9f559c2014-07-16 23:41:26 +0200686#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ppp->flags = val & SC_FLAG_BITS;
688 ppp_unlock(ppp);
689 if (cflags & SC_CCP_OPEN)
690 ppp_ccp_closed(ppp);
691 err = 0;
692 break;
693
694 case PPPIOCGFLAGS:
695 val = ppp->flags | ppp->xstate | ppp->rstate;
696 if (put_user(val, p))
697 break;
698 err = 0;
699 break;
700
701 case PPPIOCSCOMPRESS:
702 err = ppp_set_compress(ppp, arg);
703 break;
704
705 case PPPIOCGUNIT:
706 if (put_user(ppp->file.index, p))
707 break;
708 err = 0;
709 break;
710
711 case PPPIOCSDEBUG:
712 if (get_user(val, p))
713 break;
714 ppp->debug = val;
715 err = 0;
716 break;
717
718 case PPPIOCGDEBUG:
719 if (put_user(ppp->debug, p))
720 break;
721 err = 0;
722 break;
723
724 case PPPIOCGIDLE:
725 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
726 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
727 if (copy_to_user(argp, &idle, sizeof(idle)))
728 break;
729 err = 0;
730 break;
731
732 case PPPIOCSMAXCID:
733 if (get_user(val, p))
734 break;
735 val2 = 15;
736 if ((val >> 16) != 0) {
737 val2 = val >> 16;
738 val &= 0xffff;
739 }
740 vj = slhc_init(val2+1, val+1);
Ben Hutchings4ab42d72015-11-01 16:22:53 +0000741 if (IS_ERR(vj)) {
742 err = PTR_ERR(vj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744 }
745 ppp_lock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -0800746 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 slhc_free(ppp->vj);
748 ppp->vj = vj;
749 ppp_unlock(ppp);
750 err = 0;
751 break;
752
753 case PPPIOCGNPMODE:
754 case PPPIOCSNPMODE:
755 if (copy_from_user(&npi, argp, sizeof(npi)))
756 break;
757 err = proto_to_npindex(npi.protocol);
758 if (err < 0)
759 break;
760 i = err;
761 if (cmd == PPPIOCGNPMODE) {
762 err = -EFAULT;
763 npi.mode = ppp->npmode[i];
764 if (copy_to_user(argp, &npi, sizeof(npi)))
765 break;
766 } else {
767 ppp->npmode[i] = npi.mode;
768 /* we may be able to transmit more packets now (??) */
769 netif_wake_queue(ppp->dev);
770 }
771 err = 0;
772 break;
773
774#ifdef CONFIG_PPP_FILTER
775 case PPPIOCSPASS:
776 {
777 struct sock_filter *code;
Daniel Borkmann568f1942014-03-28 18:58:23 +0100778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 err = get_filter(argp, &code);
780 if (err >= 0) {
Takashi Iwai5748eb82014-11-10 11:50:21 +0100781 struct bpf_prog *pass_filter = NULL;
Daniel Borkmannb1fcd352014-05-23 18:43:58 +0200782 struct sock_fprog_kern fprog = {
Daniel Borkmann568f1942014-03-28 18:58:23 +0100783 .len = err,
784 .filter = code,
785 };
786
Takashi Iwai5748eb82014-11-10 11:50:21 +0100787 err = 0;
788 if (fprog.filter)
789 err = bpf_prog_create(&pass_filter, &fprog);
790 if (!err) {
791 ppp_lock(ppp);
792 if (ppp->pass_filter)
793 bpf_prog_destroy(ppp->pass_filter);
794 ppp->pass_filter = pass_filter;
795 ppp_unlock(ppp);
Christoph Schulzcc25eaa2014-07-16 22:10:29 +0200796 }
Daniel Borkmann568f1942014-03-28 18:58:23 +0100797 kfree(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 break;
800 }
801 case PPPIOCSACTIVE:
802 {
803 struct sock_filter *code;
Daniel Borkmann568f1942014-03-28 18:58:23 +0100804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 err = get_filter(argp, &code);
806 if (err >= 0) {
Takashi Iwai5748eb82014-11-10 11:50:21 +0100807 struct bpf_prog *active_filter = NULL;
Daniel Borkmannb1fcd352014-05-23 18:43:58 +0200808 struct sock_fprog_kern fprog = {
Daniel Borkmann568f1942014-03-28 18:58:23 +0100809 .len = err,
810 .filter = code,
811 };
812
Takashi Iwai5748eb82014-11-10 11:50:21 +0100813 err = 0;
814 if (fprog.filter)
815 err = bpf_prog_create(&active_filter, &fprog);
816 if (!err) {
817 ppp_lock(ppp);
818 if (ppp->active_filter)
819 bpf_prog_destroy(ppp->active_filter);
820 ppp->active_filter = active_filter;
821 ppp_unlock(ppp);
Christoph Schulzcc25eaa2014-07-16 22:10:29 +0200822 }
Daniel Borkmann568f1942014-03-28 18:58:23 +0100823 kfree(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 break;
826 }
827#endif /* CONFIG_PPP_FILTER */
828
829#ifdef CONFIG_PPP_MULTILINK
830 case PPPIOCSMRRU:
831 if (get_user(val, p))
832 break;
833 ppp_recv_lock(ppp);
834 ppp->mrru = val;
835 ppp_recv_unlock(ppp);
836 err = 0;
837 break;
838#endif /* CONFIG_PPP_MULTILINK */
839
840 default:
841 err = -ENOTTY;
842 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100843
844out:
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000845 mutex_unlock(&ppp_mutex);
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return err;
848}
849
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800850static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
851 struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int unit, err = -EFAULT;
854 struct ppp *ppp;
855 struct channel *chan;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800856 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int __user *p = (int __user *)arg;
858
859 switch (cmd) {
860 case PPPIOCNEWUNIT:
861 /* Create a new ppp unit */
862 if (get_user(unit, p))
863 break;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200864 err = ppp_create_interface(net, file, &unit);
865 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 err = -EFAULT;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200869 if (put_user(unit, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
871 err = 0;
872 break;
873
874 case PPPIOCATTACH:
875 /* Attach to an existing ppp unit */
876 if (get_user(unit, p))
877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800879 pn = ppp_pernet(net);
880 mutex_lock(&pn->all_ppp_mutex);
881 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800882 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 atomic_inc(&ppp->file.refcnt);
884 file->private_data = &ppp->file;
885 err = 0;
886 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800887 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
889
890 case PPPIOCATTCHAN:
891 if (get_user(unit, p))
892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800894 pn = ppp_pernet(net);
895 spin_lock_bh(&pn->all_channels_lock);
896 chan = ppp_find_channel(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800897 if (chan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 atomic_inc(&chan->file.refcnt);
899 file->private_data = &chan->file;
900 err = 0;
901 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800902 spin_unlock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 break;
904
905 default:
906 err = -ENOTTY;
907 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return err;
910}
911
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800912static const struct file_operations ppp_device_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 .owner = THIS_MODULE,
914 .read = ppp_read,
915 .write = ppp_write,
916 .poll = ppp_poll,
Alan Coxf3ff8a42008-05-25 23:40:58 -0700917 .unlocked_ioctl = ppp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .open = ppp_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200919 .release = ppp_release,
920 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
922
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800923static __net_init int ppp_init_net(struct net *net)
924{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000925 struct ppp_net *pn = net_generic(net, ppp_net_id);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800926
927 idr_init(&pn->units_idr);
928 mutex_init(&pn->all_ppp_mutex);
929
930 INIT_LIST_HEAD(&pn->all_channels);
931 INIT_LIST_HEAD(&pn->new_channels);
932
933 spin_lock_init(&pn->all_channels_lock);
934
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800935 return 0;
936}
937
938static __net_exit void ppp_exit_net(struct net *net)
939{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000940 struct ppp_net *pn = net_generic(net, ppp_net_id);
Guillaume Nault79c441a2015-08-24 11:35:30 +0200941 struct net_device *dev;
942 struct net_device *aux;
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200943 struct ppp *ppp;
944 LIST_HEAD(list);
945 int id;
946
947 rtnl_lock();
Guillaume Nault79c441a2015-08-24 11:35:30 +0200948 for_each_netdev_safe(net, dev, aux) {
949 if (dev->netdev_ops == &ppp_netdev_ops)
950 unregister_netdevice_queue(dev, &list);
951 }
952
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200953 idr_for_each_entry(&pn->units_idr, ppp, id)
Guillaume Nault79c441a2015-08-24 11:35:30 +0200954 /* Skip devices already unregistered by previous loop */
955 if (!net_eq(dev_net(ppp->dev), net))
956 unregister_netdevice_queue(ppp->dev, &list);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200957
958 unregister_netdevice_many(&list);
959 rtnl_unlock();
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800960
Gao Fengcf16dac2017-10-31 18:25:37 +0800961 mutex_destroy(&pn->all_ppp_mutex);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800962 idr_destroy(&pn->units_idr);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800963}
964
Alexey Dobriyan00129852009-02-09 18:05:16 -0800965static struct pernet_operations ppp_net_ops = {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800966 .init = ppp_init_net,
967 .exit = ppp_exit_net,
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000968 .id = &ppp_net_id,
969 .size = sizeof(struct ppp_net),
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800970};
971
Guillaume Nault96d934c2016-04-28 17:55:30 +0200972static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200973{
974 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
975 int ret;
976
977 mutex_lock(&pn->all_ppp_mutex);
978
979 if (unit < 0) {
980 ret = unit_get(&pn->units_idr, ppp);
981 if (ret < 0)
982 goto err;
983 } else {
984 /* Caller asked for a specific unit number. Fail with -EEXIST
985 * if unavailable. For backward compatibility, return -EEXIST
986 * too if idr allocation fails; this makes pppd retry without
987 * requesting a specific unit number.
988 */
989 if (unit_find(&pn->units_idr, unit)) {
990 ret = -EEXIST;
991 goto err;
992 }
993 ret = unit_set(&pn->units_idr, ppp, unit);
994 if (ret < 0) {
995 /* Rewrite error for backward compatibility */
996 ret = -EEXIST;
997 goto err;
998 }
999 }
1000 ppp->file.index = ret;
1001
Guillaume Nault96d934c2016-04-28 17:55:30 +02001002 if (!ifname_is_set)
1003 snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001004
1005 ret = register_netdevice(ppp->dev);
1006 if (ret < 0)
1007 goto err_unit;
1008
1009 atomic_inc(&ppp_unit_count);
1010
1011 mutex_unlock(&pn->all_ppp_mutex);
1012
1013 return 0;
1014
1015err_unit:
1016 unit_put(&pn->units_idr, ppp->file.index);
1017err:
1018 mutex_unlock(&pn->all_ppp_mutex);
1019
1020 return ret;
1021}
1022
1023static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1024 const struct ppp_config *conf)
1025{
1026 struct ppp *ppp = netdev_priv(dev);
1027 int indx;
1028 int err;
Gao Feng3b25bfc2017-07-17 18:34:42 +08001029 int cpu;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001030
1031 ppp->dev = dev;
1032 ppp->ppp_net = src_net;
1033 ppp->mru = PPP_MRU;
1034 ppp->owner = conf->file;
1035
1036 init_ppp_file(&ppp->file, INTERFACE);
1037 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1038
1039 for (indx = 0; indx < NUM_NP; ++indx)
1040 ppp->npmode[indx] = NPMODE_PASS;
1041 INIT_LIST_HEAD(&ppp->channels);
1042 spin_lock_init(&ppp->rlock);
1043 spin_lock_init(&ppp->wlock);
Gao Feng3b25bfc2017-07-17 18:34:42 +08001044
1045 ppp->xmit_recursion = alloc_percpu(int);
1046 if (!ppp->xmit_recursion) {
1047 err = -ENOMEM;
1048 goto err1;
1049 }
1050 for_each_possible_cpu(cpu)
1051 (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
1052
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001053#ifdef CONFIG_PPP_MULTILINK
1054 ppp->minseq = -1;
1055 skb_queue_head_init(&ppp->mrq);
1056#endif /* CONFIG_PPP_MULTILINK */
1057#ifdef CONFIG_PPP_FILTER
1058 ppp->pass_filter = NULL;
1059 ppp->active_filter = NULL;
1060#endif /* CONFIG_PPP_FILTER */
1061
Guillaume Nault96d934c2016-04-28 17:55:30 +02001062 err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001063 if (err < 0)
Gao Feng3b25bfc2017-07-17 18:34:42 +08001064 goto err2;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001065
1066 conf->file->private_data = &ppp->file;
1067
1068 return 0;
Gao Feng3b25bfc2017-07-17 18:34:42 +08001069err2:
1070 free_percpu(ppp->xmit_recursion);
1071err1:
1072 return err;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001073}
1074
Guillaume Nault96d934c2016-04-28 17:55:30 +02001075static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1076 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
1077};
1078
1079static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[])
1080{
1081 if (!data)
1082 return -EINVAL;
1083
1084 if (!data[IFLA_PPP_DEV_FD])
1085 return -EINVAL;
1086 if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1087 return -EBADF;
1088
1089 return 0;
1090}
1091
1092static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1093 struct nlattr *tb[], struct nlattr *data[])
1094{
1095 struct ppp_config conf = {
1096 .unit = -1,
1097 .ifname_is_set = true,
1098 };
1099 struct file *file;
1100 int err;
1101
1102 file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1103 if (!file)
1104 return -EBADF;
1105
1106 /* rtnl_lock is already held here, but ppp_create_interface() locks
1107 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1108 * possible deadlock due to lock order inversion, at the cost of
1109 * pushing the problem back to userspace.
1110 */
1111 if (!mutex_trylock(&ppp_mutex)) {
1112 err = -EBUSY;
1113 goto out;
1114 }
1115
1116 if (file->f_op != &ppp_device_fops || file->private_data) {
1117 err = -EBADF;
1118 goto out_unlock;
1119 }
1120
1121 conf.file = file;
Guillaume Naultbb8082f2016-08-09 15:12:26 +02001122
1123 /* Don't use device name generated by the rtnetlink layer when ifname
1124 * isn't specified. Let ppp_dev_configure() set the device name using
1125 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1126 * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1127 */
1128 if (!tb[IFLA_IFNAME])
1129 conf.ifname_is_set = false;
1130
Guillaume Nault96d934c2016-04-28 17:55:30 +02001131 err = ppp_dev_configure(src_net, dev, &conf);
1132
1133out_unlock:
1134 mutex_unlock(&ppp_mutex);
1135out:
1136 fput(file);
1137
1138 return err;
1139}
1140
1141static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1142{
1143 unregister_netdevice_queue(dev, head);
1144}
1145
1146static size_t ppp_nl_get_size(const struct net_device *dev)
1147{
1148 return 0;
1149}
1150
1151static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1152{
1153 return 0;
1154}
1155
1156static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1157{
1158 struct ppp *ppp = netdev_priv(dev);
1159
1160 return ppp->ppp_net;
1161}
1162
1163static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1164 .kind = "ppp",
1165 .maxtype = IFLA_PPP_MAX,
1166 .policy = ppp_nl_policy,
1167 .priv_size = sizeof(struct ppp),
1168 .setup = ppp_setup,
1169 .validate = ppp_nl_validate,
1170 .newlink = ppp_nl_newlink,
1171 .dellink = ppp_nl_dellink,
1172 .get_size = ppp_nl_get_size,
1173 .fill_info = ppp_nl_fill_info,
1174 .get_link_net = ppp_nl_get_link_net,
1175};
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177#define PPP_MAJOR 108
1178
1179/* Called at boot time if ppp is compiled into the kernel,
1180 or at module load time (from init_module) if compiled as a module. */
1181static int __init ppp_init(void)
1182{
1183 int err;
1184
David S. Millerb48f8c22011-01-20 22:44:36 -08001185 pr_info("PPP generic driver version " PPP_VERSION "\n");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001186
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001187 err = register_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001188 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001189 pr_err("failed to register PPP pernet device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001190 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001193 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1194 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001195 pr_err("failed to register PPP device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001196 goto out_net;
1197 }
1198
1199 ppp_class = class_create(THIS_MODULE, "ppp");
1200 if (IS_ERR(ppp_class)) {
1201 err = PTR_ERR(ppp_class);
1202 goto out_chrdev;
1203 }
1204
Guillaume Nault96d934c2016-04-28 17:55:30 +02001205 err = rtnl_link_register(&ppp_link_ops);
1206 if (err) {
1207 pr_err("failed to register rtnetlink PPP handler\n");
1208 goto out_class;
1209 }
1210
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001211 /* not a big deal if we fail here :-) */
1212 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1213
1214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Guillaume Nault96d934c2016-04-28 17:55:30 +02001216out_class:
1217 class_destroy(ppp_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218out_chrdev:
1219 unregister_chrdev(PPP_MAJOR, "ppp");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001220out_net:
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001221 unregister_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001222out:
1223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226/*
1227 * Network interface unit routines.
1228 */
Stephen Hemminger424efe92009-08-31 19:50:51 +00001229static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1231{
Wang Chenc8019bf2008-11-20 04:24:17 -08001232 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int npi, proto;
1234 unsigned char *pp;
1235
1236 npi = ethertype_to_npindex(ntohs(skb->protocol));
1237 if (npi < 0)
1238 goto outf;
1239
1240 /* Drop, accept or reject the packet */
1241 switch (ppp->npmode[npi]) {
1242 case NPMODE_PASS:
1243 break;
1244 case NPMODE_QUEUE:
1245 /* it would be nice to have a way to tell the network
1246 system to queue this one up for later. */
1247 goto outf;
1248 case NPMODE_DROP:
1249 case NPMODE_ERROR:
1250 goto outf;
1251 }
1252
1253 /* Put the 2-byte PPP protocol number on the front,
1254 making sure there is room for the address and control fields. */
Herbert Xu7b797d52007-09-16 16:21:42 -07001255 if (skb_cow_head(skb, PPP_HDRLEN))
1256 goto outf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 pp = skb_push(skb, 2);
1259 proto = npindex_to_proto[npi];
Changli Gao96545ae2011-01-06 13:37:36 +00001260 put_unaligned_be16(proto, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Guillaume Nault79c441a2015-08-24 11:35:30 +02001262 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 skb_queue_tail(&ppp->file.xq, skb);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001264 ppp_xmit_process(ppp);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001265 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 outf:
1268 kfree_skb(skb);
Paulius Zaleckas6ceffd42009-02-23 04:59:43 +00001269 ++dev->stats.tx_dropped;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001270 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273static int
1274ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1275{
Wang Chenc8019bf2008-11-20 04:24:17 -08001276 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int err = -EFAULT;
1278 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1279 struct ppp_stats stats;
1280 struct ppp_comp_stats cstats;
1281 char *vers;
1282
1283 switch (cmd) {
1284 case SIOCGPPPSTATS:
1285 ppp_get_stats(ppp, &stats);
1286 if (copy_to_user(addr, &stats, sizeof(stats)))
1287 break;
1288 err = 0;
1289 break;
1290
1291 case SIOCGPPPCSTATS:
1292 memset(&cstats, 0, sizeof(cstats));
Joe Perchescd228d52007-11-12 18:07:31 -08001293 if (ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
Joe Perchescd228d52007-11-12 18:07:31 -08001295 if (ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1297 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1298 break;
1299 err = 0;
1300 break;
1301
1302 case SIOCGPPPVER:
1303 vers = PPP_VERSION;
1304 if (copy_to_user(addr, vers, strlen(vers) + 1))
1305 break;
1306 err = 0;
1307 break;
1308
1309 default:
1310 err = -EINVAL;
1311 }
1312
1313 return err;
1314}
1315
stephen hemmingerb77bc202012-10-29 08:34:02 +00001316static struct rtnl_link_stats64*
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001317ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1318{
1319 struct ppp *ppp = netdev_priv(dev);
1320
1321 ppp_recv_lock(ppp);
1322 stats64->rx_packets = ppp->stats64.rx_packets;
1323 stats64->rx_bytes = ppp->stats64.rx_bytes;
1324 ppp_recv_unlock(ppp);
1325
1326 ppp_xmit_lock(ppp);
1327 stats64->tx_packets = ppp->stats64.tx_packets;
1328 stats64->tx_bytes = ppp->stats64.tx_bytes;
1329 ppp_xmit_unlock(ppp);
1330
1331 stats64->rx_errors = dev->stats.rx_errors;
1332 stats64->tx_errors = dev->stats.tx_errors;
1333 stats64->rx_dropped = dev->stats.rx_dropped;
1334 stats64->tx_dropped = dev->stats.tx_dropped;
1335 stats64->rx_length_errors = dev->stats.rx_length_errors;
1336
1337 return stats64;
1338}
1339
Eric Dumazet303c07d2013-02-19 10:42:03 -08001340static int ppp_dev_init(struct net_device *dev)
1341{
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001342 struct ppp *ppp;
1343
Eric Dumazetd3fff6c2016-06-09 07:45:12 -07001344 netdev_lockdep_set_classes(dev);
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001345
1346 ppp = netdev_priv(dev);
1347 /* Let the netdevice take a reference on the ppp file. This ensures
1348 * that ppp_destroy_interface() won't run before the device gets
1349 * unregistered.
1350 */
1351 atomic_inc(&ppp->file.refcnt);
1352
Eric Dumazet303c07d2013-02-19 10:42:03 -08001353 return 0;
1354}
1355
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001356static void ppp_dev_uninit(struct net_device *dev)
1357{
1358 struct ppp *ppp = netdev_priv(dev);
1359 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1360
1361 ppp_lock(ppp);
1362 ppp->closing = 1;
1363 ppp_unlock(ppp);
1364
1365 mutex_lock(&pn->all_ppp_mutex);
1366 unit_put(&pn->units_idr, ppp->file.index);
1367 mutex_unlock(&pn->all_ppp_mutex);
1368
1369 ppp->owner = NULL;
1370
1371 ppp->file.dead = 1;
1372 wake_up_interruptible(&ppp->file.rwait);
1373}
1374
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001375static void ppp_dev_priv_destructor(struct net_device *dev)
1376{
1377 struct ppp *ppp;
1378
1379 ppp = netdev_priv(dev);
1380 if (atomic_dec_and_test(&ppp->file.refcnt))
1381 ppp_destroy_interface(ppp);
1382}
1383
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001384static const struct net_device_ops ppp_netdev_ops = {
Eric Dumazet303c07d2013-02-19 10:42:03 -08001385 .ndo_init = ppp_dev_init,
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001386 .ndo_uninit = ppp_dev_uninit,
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001387 .ndo_start_xmit = ppp_start_xmit,
1388 .ndo_do_ioctl = ppp_net_ioctl,
1389 .ndo_get_stats64 = ppp_get_stats64,
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001390};
1391
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001392static struct device_type ppp_type = {
1393 .name = "ppp",
1394};
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396static void ppp_setup(struct net_device *dev)
1397{
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001398 dev->netdev_ops = &ppp_netdev_ops;
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001399 SET_NETDEV_DEVTYPE(dev, &ppp_type);
1400
Guillaume Nault07712772016-08-27 22:22:51 +02001401 dev->features |= NETIF_F_LLTX;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 dev->hard_header_len = PPP_HDRLEN;
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +00001404 dev->mtu = PPP_MRU;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 dev->addr_len = 0;
1406 dev->tx_queue_len = 3;
1407 dev->type = ARPHRD_PPP;
1408 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001409 dev->destructor = ppp_dev_priv_destructor;
Eric Dumazet02875872014-10-05 18:38:35 -07001410 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413/*
1414 * Transmit-side routines.
1415 */
1416
Guillaume Nault55454a52016-08-27 22:22:32 +02001417/* Called to do any work queued up on the transmit side that can now be done */
1418static void __ppp_xmit_process(struct ppp *ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 struct sk_buff *skb;
1421
1422 ppp_xmit_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001423 if (!ppp->closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ppp_push(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +00001425 while (!ppp->xmit_pending &&
1426 (skb = skb_dequeue(&ppp->file.xq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ppp_send_frame(ppp, skb);
1428 /* If there's no work left to do, tell the core net
1429 code that we can accept some more. */
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001430 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 netif_wake_queue(ppp->dev);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001432 else
1433 netif_stop_queue(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 ppp_xmit_unlock(ppp);
1436}
1437
Guillaume Nault55454a52016-08-27 22:22:32 +02001438static void ppp_xmit_process(struct ppp *ppp)
1439{
1440 local_bh_disable();
1441
Gao Feng3b25bfc2017-07-17 18:34:42 +08001442 if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
Guillaume Nault55454a52016-08-27 22:22:32 +02001443 goto err;
1444
Gao Feng3b25bfc2017-07-17 18:34:42 +08001445 (*this_cpu_ptr(ppp->xmit_recursion))++;
Guillaume Nault55454a52016-08-27 22:22:32 +02001446 __ppp_xmit_process(ppp);
Gao Feng3b25bfc2017-07-17 18:34:42 +08001447 (*this_cpu_ptr(ppp->xmit_recursion))--;
Guillaume Nault55454a52016-08-27 22:22:32 +02001448
1449 local_bh_enable();
1450
1451 return;
1452
1453err:
1454 local_bh_enable();
1455
1456 if (net_ratelimit())
1457 netdev_err(ppp->dev, "recursion detected\n");
1458}
1459
Matt Domschb3f9b922005-11-08 09:40:47 -08001460static inline struct sk_buff *
1461pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1462{
1463 struct sk_buff *new_skb;
1464 int len;
1465 int new_skb_size = ppp->dev->mtu +
1466 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1467 int compressor_skb_size = ppp->dev->mtu +
1468 ppp->xcomp->comp_extra + PPP_HDRLEN;
1469 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1470 if (!new_skb) {
1471 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001472 netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001473 return NULL;
1474 }
1475 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1476 skb_reserve(new_skb,
1477 ppp->dev->hard_header_len - PPP_HDRLEN);
1478
1479 /* compressor still expects A/C bytes in hdr */
1480 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1481 new_skb->data, skb->len + 2,
1482 compressor_skb_size);
1483 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
Eric Dumazet968d7012012-05-18 20:23:00 +00001484 consume_skb(skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001485 skb = new_skb;
1486 skb_put(skb, len);
1487 skb_pull(skb, 2); /* pull off A/C bytes */
1488 } else if (len == 0) {
1489 /* didn't compress, or CCP not up yet */
Eric Dumazet968d7012012-05-18 20:23:00 +00001490 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001491 new_skb = skb;
1492 } else {
1493 /*
1494 * (len < 0)
1495 * MPPE requires that we do not send unencrypted
1496 * frames. The compressor will return -1 if we
1497 * should drop the frame. We cannot simply test
1498 * the compress_proto because MPPE and MPPC share
1499 * the same number.
1500 */
1501 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001502 netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001503 kfree_skb(skb);
Eric Dumazet968d7012012-05-18 20:23:00 +00001504 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001505 new_skb = NULL;
1506 }
1507 return new_skb;
1508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/*
1511 * Compress and send a frame.
1512 * The caller should have locked the xmit path,
1513 * and xmit_pending should be 0.
1514 */
1515static void
1516ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1517{
1518 int proto = PPP_PROTO(skb);
1519 struct sk_buff *new_skb;
1520 int len;
1521 unsigned char *cp;
1522
1523 if (proto < 0x8000) {
1524#ifdef CONFIG_PPP_FILTER
1525 /* check if we should pass this packet */
1526 /* the filter instructions are constructed assuming
1527 a four-byte PPP header on each packet */
1528 *skb_push(skb, 2) = 1;
Joe Perches8e95a202009-12-03 07:58:21 +00001529 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001530 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001532 netdev_printk(KERN_DEBUG, ppp->dev,
1533 "PPP: outbound frame "
1534 "not passed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 kfree_skb(skb);
1536 return;
1537 }
1538 /* if this packet passes the active filter, record the time */
Joe Perches8e95a202009-12-03 07:58:21 +00001539 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001540 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ppp->last_xmit = jiffies;
1542 skb_pull(skb, 2);
1543#else
1544 /* for data packets, record the time */
1545 ppp->last_xmit = jiffies;
1546#endif /* CONFIG_PPP_FILTER */
1547 }
1548
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001549 ++ppp->stats64.tx_packets;
1550 ppp->stats64.tx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 switch (proto) {
1553 case PPP_IP:
Joe Perchescd228d52007-11-12 18:07:31 -08001554 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
1556 /* try to do VJ TCP header compression */
1557 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1558 GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001559 if (!new_skb) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001560 netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 goto drop;
1562 }
1563 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1564 cp = skb->data + 2;
1565 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1566 new_skb->data + 2, &cp,
1567 !(ppp->flags & SC_NO_TCP_CCID));
1568 if (cp == skb->data + 2) {
1569 /* didn't compress */
Eric Dumazet968d7012012-05-18 20:23:00 +00001570 consume_skb(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 } else {
1572 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1573 proto = PPP_VJC_COMP;
1574 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1575 } else {
1576 proto = PPP_VJC_UNCOMP;
1577 cp[0] = skb->data[2];
1578 }
Eric Dumazet968d7012012-05-18 20:23:00 +00001579 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 skb = new_skb;
1581 cp = skb_put(skb, len + 2);
1582 cp[0] = 0;
1583 cp[1] = proto;
1584 }
1585 break;
1586
1587 case PPP_CCP:
1588 /* peek at outbound CCP frames */
1589 ppp_ccp_peek(ppp, skb, 0);
1590 break;
1591 }
1592
1593 /* try to do packet compression */
Joe Perches8e95a202009-12-03 07:58:21 +00001594 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1595 proto != PPP_LCP && proto != PPP_CCP) {
Matt Domschb3f9b922005-11-08 09:40:47 -08001596 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1597 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001598 netdev_err(ppp->dev,
1599 "ppp: compression required but "
1600 "down - pkt dropped.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 goto drop;
1602 }
Matt Domschb3f9b922005-11-08 09:40:47 -08001603 skb = pad_compress_skb(ppp, skb);
1604 if (!skb)
1605 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607
1608 /*
1609 * If we are waiting for traffic (demand dialling),
1610 * queue it up for pppd to receive.
1611 */
1612 if (ppp->flags & SC_LOOP_TRAFFIC) {
1613 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1614 goto drop;
1615 skb_queue_tail(&ppp->file.rq, skb);
1616 wake_up_interruptible(&ppp->file.rwait);
1617 return;
1618 }
1619
1620 ppp->xmit_pending = skb;
1621 ppp_push(ppp);
1622 return;
1623
1624 drop:
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00001625 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001626 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
1629/*
1630 * Try to send the frame in xmit_pending.
1631 * The caller should have the xmit path locked.
1632 */
1633static void
1634ppp_push(struct ppp *ppp)
1635{
1636 struct list_head *list;
1637 struct channel *pch;
1638 struct sk_buff *skb = ppp->xmit_pending;
1639
Joe Perchescd228d52007-11-12 18:07:31 -08001640 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return;
1642
1643 list = &ppp->channels;
1644 if (list_empty(list)) {
1645 /* nowhere to send the packet, just drop it */
1646 ppp->xmit_pending = NULL;
1647 kfree_skb(skb);
1648 return;
1649 }
1650
1651 if ((ppp->flags & SC_MULTILINK) == 0) {
1652 /* not doing multilink: send it down the first channel */
1653 list = list->next;
1654 pch = list_entry(list, struct channel, clist);
1655
1656 spin_lock_bh(&pch->downl);
1657 if (pch->chan) {
1658 if (pch->chan->ops->start_xmit(pch->chan, skb))
1659 ppp->xmit_pending = NULL;
1660 } else {
1661 /* channel got unregistered */
1662 kfree_skb(skb);
1663 ppp->xmit_pending = NULL;
1664 }
1665 spin_unlock_bh(&pch->downl);
1666 return;
1667 }
1668
1669#ifdef CONFIG_PPP_MULTILINK
1670 /* Multilink: fragment the packet over as many links
1671 as can take the packet at the moment. */
1672 if (!ppp_mp_explode(ppp, skb))
1673 return;
1674#endif /* CONFIG_PPP_MULTILINK */
1675
1676 ppp->xmit_pending = NULL;
1677 kfree_skb(skb);
1678}
1679
1680#ifdef CONFIG_PPP_MULTILINK
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001681static bool mp_protocol_compress __read_mostly = true;
1682module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
1683MODULE_PARM_DESC(mp_protocol_compress,
1684 "compress protocol id in multilink fragments");
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686/*
1687 * Divide a packet to be transmitted into fragments and
1688 * send them out the individual links.
1689 */
1690static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1691{
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001692 int len, totlen;
1693 int i, bits, hdrlen, mtu;
1694 int flen;
1695 int navail, nfree, nzero;
1696 int nbigger;
1697 int totspeed;
1698 int totfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 unsigned char *p, *q;
1700 struct list_head *list;
1701 struct channel *pch;
1702 struct sk_buff *frag;
1703 struct ppp_channel *chan;
1704
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001705 totspeed = 0; /*total bitrate of the bundle*/
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001706 nfree = 0; /* # channels which have no packet already queued */
1707 navail = 0; /* total # of usable channels (not deregistered) */
1708 nzero = 0; /* number of channels with zero speed associated*/
1709 totfree = 0; /*total # of channels available and
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001710 *having no queued packets before
1711 *starting the fragmentation*/
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001714 i = 0;
1715 list_for_each_entry(pch, &ppp->channels, clist) {
Dan Carpenter34297692010-09-10 01:58:10 +00001716 if (pch->chan) {
1717 pch->avail = 1;
1718 navail++;
1719 pch->speed = pch->chan->speed;
1720 } else {
1721 pch->avail = 0;
1722 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001723 if (pch->avail) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001724 if (skb_queue_empty(&pch->file.xq) ||
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001725 !pch->had_frag) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001726 if (pch->speed == 0)
1727 nzero++;
1728 else
1729 totspeed += pch->speed;
1730
1731 pch->avail = 2;
1732 ++nfree;
1733 ++totfree;
1734 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001735 if (!pch->had_frag && i < ppp->nxchan)
1736 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001738 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001740 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001741 * Don't start sending this packet unless at least half of
1742 * the channels are free. This gives much better TCP
1743 * performance if we have a lot of channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001744 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001745 if (nfree == 0 || nfree < navail / 2)
1746 return 0; /* can't take now, leave it in xmit_pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001748 /* Do protocol field compression */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001749 p = skb->data;
1750 len = skb->len;
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001751 if (*p == 0 && mp_protocol_compress) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ++p;
1753 --len;
1754 }
1755
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001756 totlen = len;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001757 nbigger = len % nfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001759 /* skip to the channel after the one we last used
1760 and start at that one */
Domen Puncer81616c52005-09-10 00:27:04 -07001761 list = &ppp->channels;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001762 for (i = 0; i < ppp->nxchan; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001764 if (list == &ppp->channels) {
1765 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
1767 }
1768 }
1769
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001770 /* create a fragment for each channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 bits = B;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001772 while (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001774 if (list == &ppp->channels) {
1775 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 continue;
1777 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001778 pch = list_entry(list, struct channel, clist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 ++i;
1780 if (!pch->avail)
1781 continue;
1782
Paul Mackerras516cd152005-05-12 19:47:12 -04001783 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001784 * Skip this channel if it has a fragment pending already and
1785 * we haven't given a fragment to all of the free channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001786 */
1787 if (pch->avail == 1) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001788 if (nfree > 0)
Paul Mackerras516cd152005-05-12 19:47:12 -04001789 continue;
1790 } else {
Paul Mackerras516cd152005-05-12 19:47:12 -04001791 pch->avail = 1;
1792 }
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* check the channel's mtu and whether it is still attached. */
1795 spin_lock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001796 if (pch->chan == NULL) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001797 /* can't use this channel, it's being deregistered */
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001798 if (pch->speed == 0)
1799 nzero--;
1800 else
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001801 totspeed -= pch->speed;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 spin_unlock_bh(&pch->downl);
1804 pch->avail = 0;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001805 totlen = len;
1806 totfree--;
1807 nfree--;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001808 if (--navail == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
1810 continue;
1811 }
1812
1813 /*
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001814 *if the channel speed is not set divide
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001815 *the packet evenly among the free channels;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001816 *otherwise divide it according to the speed
1817 *of the channel we are going to transmit on
1818 */
David S. Miller886f9fe2009-08-19 13:55:55 -07001819 flen = len;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001820 if (nfree > 0) {
1821 if (pch->speed == 0) {
Ben McKeegan536e00e2010-06-02 23:14:33 +00001822 flen = len/nfree;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001823 if (nbigger > 0) {
1824 flen++;
1825 nbigger--;
1826 }
1827 } else {
1828 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1829 ((totspeed*totfree)/pch->speed)) - hdrlen;
1830 if (nbigger > 0) {
1831 flen += ((totfree - nzero)*pch->speed)/totspeed;
1832 nbigger -= ((totfree - nzero)*pch->speed)/
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001833 totspeed;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001834 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001835 }
Ben McKeegana53a8b52009-07-28 07:43:57 +00001836 nfree--;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001837 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001838
1839 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001840 *check if we are on the last channel or
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001841 *we exceded the length of the data to
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001842 *fragment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Ben McKeegana53a8b52009-07-28 07:43:57 +00001844 if ((nfree <= 0) || (flen > len))
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001845 flen = len;
1846 /*
1847 *it is not worth to tx on slow channels:
1848 *in that case from the resulting flen according to the
1849 *above formula will be equal or less than zero.
1850 *Skip the channel in this case
1851 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001852 if (flen <= 0) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001853 pch->avail = 2;
1854 spin_unlock_bh(&pch->downl);
1855 continue;
1856 }
1857
Henry Wong22e83a22011-09-18 13:41:49 +00001858 /*
1859 * hdrlen includes the 2-byte PPP protocol field, but the
1860 * MTU counts only the payload excluding the protocol field.
1861 * (RFC1661 Section 2)
1862 */
1863 mtu = pch->chan->mtu - (hdrlen - 2);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001864 if (mtu < 4)
1865 mtu = 4;
Paul Mackerras516cd152005-05-12 19:47:12 -04001866 if (flen > mtu)
1867 flen = mtu;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001868 if (flen == len)
1869 bits |= E;
1870 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001871 if (!frag)
Paul Mackerras516cd152005-05-12 19:47:12 -04001872 goto noskb;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001873 q = skb_put(frag, flen + hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001875 /* make the MP header */
Changli Gao96545ae2011-01-06 13:37:36 +00001876 put_unaligned_be16(PPP_MP, q);
Paul Mackerras516cd152005-05-12 19:47:12 -04001877 if (ppp->flags & SC_MP_XSHORTSEQ) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001878 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
Paul Mackerras516cd152005-05-12 19:47:12 -04001879 q[3] = ppp->nxseq;
1880 } else {
1881 q[2] = bits;
1882 q[3] = ppp->nxseq >> 16;
1883 q[4] = ppp->nxseq >> 8;
1884 q[5] = ppp->nxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001886
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001887 memcpy(q + hdrlen, p, flen);
Paul Mackerras516cd152005-05-12 19:47:12 -04001888
1889 /* try to send it down the channel */
1890 chan = pch->chan;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001891 if (!skb_queue_empty(&pch->file.xq) ||
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001892 !chan->ops->start_xmit(chan, frag))
Paul Mackerras516cd152005-05-12 19:47:12 -04001893 skb_queue_tail(&pch->file.xq, frag);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001894 pch->had_frag = 1;
Paul Mackerras516cd152005-05-12 19:47:12 -04001895 p += flen;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001896 len -= flen;
Paul Mackerras516cd152005-05-12 19:47:12 -04001897 ++ppp->nxseq;
1898 bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 spin_unlock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001900 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001901 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 return 1;
1904
1905 noskb:
1906 spin_unlock_bh(&pch->downl);
1907 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001908 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001909 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ++ppp->nxseq;
1911 return 1; /* abandon the frame */
1912}
1913#endif /* CONFIG_PPP_MULTILINK */
1914
Guillaume Nault55454a52016-08-27 22:22:32 +02001915/* Try to send data out on a channel */
1916static void __ppp_channel_push(struct channel *pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 struct sk_buff *skb;
1919 struct ppp *ppp;
1920
1921 spin_lock_bh(&pch->downl);
Joe Perchescd228d52007-11-12 18:07:31 -08001922 if (pch->chan) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001923 while (!skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 skb = skb_dequeue(&pch->file.xq);
1925 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1926 /* put the packet back and try again later */
1927 skb_queue_head(&pch->file.xq, skb);
1928 break;
1929 }
1930 }
1931 } else {
1932 /* channel got deregistered */
1933 skb_queue_purge(&pch->file.xq);
1934 }
1935 spin_unlock_bh(&pch->downl);
1936 /* see if there is anything from the attached unit to be sent */
David S. Millerb03efcf2005-07-08 14:57:23 -07001937 if (skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ppp = pch->ppp;
Joe Perchescd228d52007-11-12 18:07:31 -08001939 if (ppp)
Guillaume Nault6ec6ec32017-08-08 11:43:24 +02001940 __ppp_xmit_process(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942}
1943
Guillaume Nault55454a52016-08-27 22:22:32 +02001944static void ppp_channel_push(struct channel *pch)
1945{
Guillaume Nault6ec6ec32017-08-08 11:43:24 +02001946 read_lock_bh(&pch->upl);
1947 if (pch->ppp) {
1948 (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
1949 __ppp_channel_push(pch);
1950 (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
1951 } else {
1952 __ppp_channel_push(pch);
1953 }
1954 read_unlock_bh(&pch->upl);
Guillaume Nault55454a52016-08-27 22:22:32 +02001955}
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957/*
1958 * Receive-side routines.
1959 */
1960
David S. Millera00eac02010-10-05 01:36:52 -07001961struct ppp_mp_skb_parm {
1962 u32 sequence;
1963 u8 BEbits;
1964};
1965#define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967static inline void
1968ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1969{
1970 ppp_recv_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001971 if (!ppp->closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 ppp_receive_frame(ppp, skb, pch);
1973 else
1974 kfree_skb(skb);
1975 ppp_recv_unlock(ppp);
1976}
1977
1978void
1979ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1980{
1981 struct channel *pch = chan->ppp;
1982 int proto;
1983
Simon Arlottea8420e2010-05-03 10:19:33 +00001984 if (!pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 kfree_skb(skb);
1986 return;
1987 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 read_lock_bh(&pch->upl);
Simon Arlottea8420e2010-05-03 10:19:33 +00001990 if (!pskb_may_pull(skb, 2)) {
1991 kfree_skb(skb);
1992 if (pch->ppp) {
1993 ++pch->ppp->dev->stats.rx_length_errors;
1994 ppp_receive_error(pch->ppp);
1995 }
1996 goto done;
1997 }
1998
1999 proto = PPP_PROTO(skb);
Joe Perchescd228d52007-11-12 18:07:31 -08002000 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /* put it on the channel queue */
2002 skb_queue_tail(&pch->file.rq, skb);
2003 /* drop old frames if queue too long */
Joe Perches8e95a202009-12-03 07:58:21 +00002004 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
2005 (skb = skb_dequeue(&pch->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 kfree_skb(skb);
2007 wake_up_interruptible(&pch->file.rwait);
2008 } else {
2009 ppp_do_recv(pch->ppp, skb, pch);
2010 }
Simon Arlottea8420e2010-05-03 10:19:33 +00002011
2012done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 read_unlock_bh(&pch->upl);
2014}
2015
2016/* Put a 0-length skb in the receive queue as an error indication */
2017void
2018ppp_input_error(struct ppp_channel *chan, int code)
2019{
2020 struct channel *pch = chan->ppp;
2021 struct sk_buff *skb;
2022
Joe Perchescd228d52007-11-12 18:07:31 -08002023 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return;
2025
2026 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002027 if (pch->ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 skb = alloc_skb(0, GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002029 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 skb->len = 0; /* probably unnecessary */
2031 skb->cb[0] = code;
2032 ppp_do_recv(pch->ppp, skb, pch);
2033 }
2034 }
2035 read_unlock_bh(&pch->upl);
2036}
2037
2038/*
2039 * We come in here to process a received frame.
2040 * The receive side of the ppp unit is locked.
2041 */
2042static void
2043ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2044{
Simon Arlottea8420e2010-05-03 10:19:33 +00002045 /* note: a 0-length skb is used as an error indication */
2046 if (skb->len > 0) {
Tom Herbert3dfb0532015-04-20 14:10:05 -07002047 skb_checksum_complete_unset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048#ifdef CONFIG_PPP_MULTILINK
2049 /* XXX do channel-level decompression here */
2050 if (PPP_PROTO(skb) == PPP_MP)
2051 ppp_receive_mp_frame(ppp, skb, pch);
2052 else
2053#endif /* CONFIG_PPP_MULTILINK */
2054 ppp_receive_nonmp_frame(ppp, skb);
Simon Arlottea8420e2010-05-03 10:19:33 +00002055 } else {
2056 kfree_skb(skb);
2057 ppp_receive_error(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061static void
2062ppp_receive_error(struct ppp *ppp)
2063{
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002064 ++ppp->dev->stats.rx_errors;
Joe Perchescd228d52007-11-12 18:07:31 -08002065 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 slhc_toss(ppp->vj);
2067}
2068
2069static void
2070ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2071{
2072 struct sk_buff *ns;
2073 int proto, len, npi;
2074
2075 /*
2076 * Decompress the frame, if compressed.
2077 * Note that some decompressors need to see uncompressed frames
2078 * that come in as well as compressed frames.
2079 */
Joe Perches8e95a202009-12-03 07:58:21 +00002080 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2081 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 skb = ppp_decompress_frame(ppp, skb);
2083
Matt Domschb3f9b922005-11-08 09:40:47 -08002084 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2085 goto err;
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 proto = PPP_PROTO(skb);
2088 switch (proto) {
2089 case PPP_VJC_COMP:
2090 /* decompress VJ compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002091 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 goto err;
2093
Herbert Xu2a38b772007-09-16 16:22:13 -07002094 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /* copy to a new sk_buff with more tailroom */
2096 ns = dev_alloc_skb(skb->len + 128);
Joe Perchescd228d52007-11-12 18:07:31 -08002097 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002098 netdev_err(ppp->dev, "PPP: no memory "
2099 "(VJ decomp)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto err;
2101 }
2102 skb_reserve(ns, 2);
2103 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
Eric Dumazet968d7012012-05-18 20:23:00 +00002104 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 skb = ns;
2106 }
Herbert Xue3f749c2006-02-05 20:23:33 -08002107 else
2108 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2111 if (len <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002112 netdev_printk(KERN_DEBUG, ppp->dev,
2113 "PPP: VJ decompression error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 goto err;
2115 }
2116 len += 2;
2117 if (len > skb->len)
2118 skb_put(skb, len - skb->len);
2119 else if (len < skb->len)
2120 skb_trim(skb, len);
2121 proto = PPP_IP;
2122 break;
2123
2124 case PPP_VJC_UNCOMP:
Joe Perchescd228d52007-11-12 18:07:31 -08002125 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 goto err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* Until we fix the decompressor need to make sure
2129 * data portion is linear.
2130 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002131 if (!pskb_may_pull(skb, skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 goto err;
2133
2134 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002135 netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 goto err;
2137 }
2138 proto = PPP_IP;
2139 break;
2140
2141 case PPP_CCP:
2142 ppp_ccp_peek(ppp, skb, 1);
2143 break;
2144 }
2145
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002146 ++ppp->stats64.rx_packets;
2147 ppp->stats64.rx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 npi = proto_to_npindex(proto);
2150 if (npi < 0) {
2151 /* control or unknown frame - pass it to pppd */
2152 skb_queue_tail(&ppp->file.rq, skb);
2153 /* limit queue length by dropping old frames */
Joe Perches8e95a202009-12-03 07:58:21 +00002154 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2155 (skb = skb_dequeue(&ppp->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 kfree_skb(skb);
2157 /* wake up any process polling or blocking on read */
2158 wake_up_interruptible(&ppp->file.rwait);
2159
2160 } else {
2161 /* network protocol frame - give it to the kernel */
2162
2163#ifdef CONFIG_PPP_FILTER
2164 /* check if the packet passes the pass and active filters */
2165 /* the filter instructions are constructed assuming
2166 a four-byte PPP header on each packet */
Herbert Xu2a38b772007-09-16 16:22:13 -07002167 if (ppp->pass_filter || ppp->active_filter) {
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +00002168 if (skb_unclone(skb, GFP_ATOMIC))
Herbert Xu2a38b772007-09-16 16:22:13 -07002169 goto err;
2170
2171 *skb_push(skb, 2) = 0;
Joe Perches8e95a202009-12-03 07:58:21 +00002172 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002173 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Herbert Xu2a38b772007-09-16 16:22:13 -07002174 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002175 netdev_printk(KERN_DEBUG, ppp->dev,
2176 "PPP: inbound frame "
2177 "not passed\n");
Herbert Xu2a38b772007-09-16 16:22:13 -07002178 kfree_skb(skb);
2179 return;
2180 }
Joe Perches8e95a202009-12-03 07:58:21 +00002181 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002182 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Herbert Xu2a38b772007-09-16 16:22:13 -07002183 ppp->last_recv = jiffies;
2184 __skb_pull(skb, 2);
2185 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#endif /* CONFIG_PPP_FILTER */
Herbert Xu2a38b772007-09-16 16:22:13 -07002187 ppp->last_recv = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Joe Perches8e95a202009-12-03 07:58:21 +00002189 if ((ppp->dev->flags & IFF_UP) == 0 ||
2190 ppp->npmode[npi] != NPMODE_PASS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 kfree_skb(skb);
2192 } else {
Herbert Xucbb042f2006-03-20 22:43:56 -08002193 /* chop off protocol */
2194 skb_pull_rcsum(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 skb->dev = ppp->dev;
2196 skb->protocol = htons(npindex_to_ethertype[npi]);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002197 skb_reset_mac_header(skb);
Guillaume Nault79c441a2015-08-24 11:35:30 +02002198 skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2199 dev_net(ppp->dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202 }
2203 return;
2204
2205 err:
2206 kfree_skb(skb);
2207 ppp_receive_error(ppp);
2208}
2209
2210static struct sk_buff *
2211ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2212{
2213 int proto = PPP_PROTO(skb);
2214 struct sk_buff *ns;
2215 int len;
2216
2217 /* Until we fix all the decompressor's need to make sure
2218 * data portion is linear.
2219 */
2220 if (!pskb_may_pull(skb, skb->len))
2221 goto err;
2222
2223 if (proto == PPP_COMP) {
Konstantin Sharlaimov4b2a8fb2007-06-23 23:05:54 -07002224 int obuff_size;
2225
2226 switch(ppp->rcomp->compress_proto) {
2227 case CI_MPPE:
2228 obuff_size = ppp->mru + PPP_HDRLEN + 1;
2229 break;
2230 default:
2231 obuff_size = ppp->mru + PPP_HDRLEN;
2232 break;
2233 }
2234
2235 ns = dev_alloc_skb(obuff_size);
Joe Perchescd228d52007-11-12 18:07:31 -08002236 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002237 netdev_err(ppp->dev, "ppp_decompress_frame: "
2238 "no memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto err;
2240 }
2241 /* the decompressor still expects the A/C bytes in the hdr */
2242 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
Konstantin Sharlaimov06c7af52007-08-21 00:12:44 -07002243 skb->len + 2, ns->data, obuff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (len < 0) {
2245 /* Pass the compressed frame to pppd as an
2246 error indication. */
2247 if (len == DECOMP_FATALERROR)
2248 ppp->rstate |= SC_DC_FERROR;
2249 kfree_skb(ns);
2250 goto err;
2251 }
2252
Eric Dumazet968d7012012-05-18 20:23:00 +00002253 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 skb = ns;
2255 skb_put(skb, len);
2256 skb_pull(skb, 2); /* pull off the A/C bytes */
2257
2258 } else {
2259 /* Uncompressed frame - pass to decompressor so it
2260 can update its dictionary if necessary. */
2261 if (ppp->rcomp->incomp)
2262 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2263 skb->len + 2);
2264 }
2265
2266 return skb;
2267
2268 err:
2269 ppp->rstate |= SC_DC_ERROR;
2270 ppp_receive_error(ppp);
2271 return skb;
2272}
2273
2274#ifdef CONFIG_PPP_MULTILINK
2275/*
2276 * Receive a multilink frame.
2277 * We put it on the reconstruction queue and then pull off
2278 * as many completed frames as we can.
2279 */
2280static void
2281ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2282{
2283 u32 mask, seq;
Domen Puncer81616c52005-09-10 00:27:04 -07002284 struct channel *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2286
Herbert Xu2a38b772007-09-16 16:22:13 -07002287 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto err; /* no good, throw it away */
2289
2290 /* Decode sequence number and begin/end bits */
2291 if (ppp->flags & SC_MP_SHORTSEQ) {
2292 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2293 mask = 0xfff;
2294 } else {
2295 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2296 mask = 0xffffff;
2297 }
David S. Millera00eac02010-10-05 01:36:52 -07002298 PPP_MP_CB(skb)->BEbits = skb->data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
2300
2301 /*
2302 * Do protocol ID decompression on the first fragment of each packet.
2303 */
David S. Millera00eac02010-10-05 01:36:52 -07002304 if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 *skb_push(skb, 1) = 0;
2306
2307 /*
2308 * Expand sequence number to 32 bits, making it as close
2309 * as possible to ppp->minseq.
2310 */
2311 seq |= ppp->minseq & ~mask;
2312 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2313 seq += mask + 1;
2314 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2315 seq -= mask + 1; /* should never happen */
David S. Millera00eac02010-10-05 01:36:52 -07002316 PPP_MP_CB(skb)->sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 pch->lastseq = seq;
2318
2319 /*
2320 * If this packet comes before the next one we were expecting,
2321 * drop it.
2322 */
2323 if (seq_before(seq, ppp->nextseq)) {
2324 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002325 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 ppp_receive_error(ppp);
2327 return;
2328 }
2329
2330 /*
2331 * Reevaluate minseq, the minimum over all channels of the
2332 * last sequence number received on each channel. Because of
2333 * the increasing sequence number rule, we know that any fragment
2334 * before `minseq' which hasn't arrived is never going to arrive.
2335 * The list of channels can't change because we have the receive
2336 * side of the ppp unit locked.
2337 */
Domen Puncer81616c52005-09-10 00:27:04 -07002338 list_for_each_entry(ch, &ppp->channels, clist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (seq_before(ch->lastseq, seq))
2340 seq = ch->lastseq;
2341 }
2342 if (seq_before(ppp->minseq, seq))
2343 ppp->minseq = seq;
2344
2345 /* Put the fragment on the reconstruction queue */
2346 ppp_mp_insert(ppp, skb);
2347
2348 /* If the queue is getting long, don't wait any longer for packets
2349 before the start of the queue. */
David S. Miller38ce7c72008-09-23 01:17:18 -07002350 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
stephen hemmingerc6b20d92010-06-01 06:05:46 +00002351 struct sk_buff *mskb = skb_peek(&ppp->mrq);
David S. Millera00eac02010-10-05 01:36:52 -07002352 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2353 ppp->minseq = PPP_MP_CB(mskb)->sequence;
David S. Miller38ce7c72008-09-23 01:17:18 -07002354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 /* Pull completed packets off the queue and receive them. */
Ben McKeegan82b3cc12009-11-16 03:44:25 +00002357 while ((skb = ppp_mp_reconstruct(ppp))) {
2358 if (pskb_may_pull(skb, 2))
2359 ppp_receive_nonmp_frame(ppp, skb);
2360 else {
2361 ++ppp->dev->stats.rx_length_errors;
2362 kfree_skb(skb);
2363 ppp_receive_error(ppp);
2364 }
2365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 return;
2368
2369 err:
2370 kfree_skb(skb);
2371 ppp_receive_error(ppp);
2372}
2373
2374/*
2375 * Insert a fragment on the MP reconstruction queue.
2376 * The queue is ordered by increasing sequence number.
2377 */
2378static void
2379ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2380{
2381 struct sk_buff *p;
2382 struct sk_buff_head *list = &ppp->mrq;
David S. Millera00eac02010-10-05 01:36:52 -07002383 u32 seq = PPP_MP_CB(skb)->sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 /* N.B. we don't need to lock the list lock because we have the
2386 ppp unit receive-side lock. */
David S. Miller13c98212008-10-09 16:40:29 -07002387 skb_queue_walk(list, p) {
David S. Millera00eac02010-10-05 01:36:52 -07002388 if (seq_before(seq, PPP_MP_CB(p)->sequence))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 break;
David S. Miller13c98212008-10-09 16:40:29 -07002390 }
David S. Miller43f59c82008-09-21 21:28:51 -07002391 __skb_queue_before(list, p, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
2394/*
2395 * Reconstruct a packet from the MP fragment queue.
2396 * We go through increasing sequence numbers until we find a
2397 * complete packet, or we get to the sequence number for a fragment
2398 * which hasn't arrived but might still do so.
2399 */
Stephen Hemminger3c582b32008-01-23 20:54:07 -08002400static struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401ppp_mp_reconstruct(struct ppp *ppp)
2402{
2403 u32 seq = ppp->nextseq;
2404 u32 minseq = ppp->minseq;
2405 struct sk_buff_head *list = &ppp->mrq;
David S. Millerd52344a2011-01-20 22:52:05 -08002406 struct sk_buff *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 struct sk_buff *head, *tail;
2408 struct sk_buff *skb = NULL;
2409 int lost = 0, len = 0;
2410
2411 if (ppp->mrru == 0) /* do nothing until mrru is set */
2412 return NULL;
2413 head = list->next;
2414 tail = NULL;
David S. Millerd52344a2011-01-20 22:52:05 -08002415 skb_queue_walk_safe(list, p, tmp) {
2416 again:
David S. Millera00eac02010-10-05 01:36:52 -07002417 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /* this can't happen, anyway ignore the skb */
David S. Millerb48f8c22011-01-20 22:44:36 -08002419 netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2420 "seq %u < %u\n",
2421 PPP_MP_CB(p)->sequence, seq);
David S. Millerd52344a2011-01-20 22:52:05 -08002422 __skb_unlink(p, list);
2423 kfree_skb(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 continue;
2425 }
David S. Millera00eac02010-10-05 01:36:52 -07002426 if (PPP_MP_CB(p)->sequence != seq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002427 u32 oldseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /* Fragment `seq' is missing. If it is after
2429 minseq, it might arrive later, so stop here. */
2430 if (seq_after(seq, minseq))
2431 break;
2432 /* Fragment `seq' is lost, keep going. */
2433 lost = 1;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002434 oldseq = seq;
David S. Millera00eac02010-10-05 01:36:52 -07002435 seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2436 minseq + 1: PPP_MP_CB(p)->sequence;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002437
2438 if (ppp->debug & 1)
2439 netdev_printk(KERN_DEBUG, ppp->dev,
2440 "lost frag %u..%u\n",
2441 oldseq, seq-1);
2442
David S. Millerd52344a2011-01-20 22:52:05 -08002443 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
2446 /*
2447 * At this point we know that all the fragments from
2448 * ppp->nextseq to seq are either present or lost.
2449 * Also, there are no complete packets in the queue
2450 * that have no missing fragments and end before this
2451 * fragment.
2452 */
2453
2454 /* B bit set indicates this fragment starts a packet */
David S. Millera00eac02010-10-05 01:36:52 -07002455 if (PPP_MP_CB(p)->BEbits & B) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 head = p;
2457 lost = 0;
2458 len = 0;
2459 }
2460
2461 len += p->len;
2462
2463 /* Got a complete packet yet? */
David S. Millera00eac02010-10-05 01:36:52 -07002464 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2465 (PPP_MP_CB(head)->BEbits & B)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (len > ppp->mrru + 2) {
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002467 ++ppp->dev->stats.rx_length_errors;
David S. Millerb48f8c22011-01-20 22:44:36 -08002468 netdev_printk(KERN_DEBUG, ppp->dev,
2469 "PPP: reconstructed packet"
2470 " is too long (%d)\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 } else {
2472 tail = p;
2473 break;
2474 }
2475 ppp->nextseq = seq + 1;
2476 }
2477
2478 /*
2479 * If this is the ending fragment of a packet,
2480 * and we haven't found a complete valid packet yet,
2481 * we can discard up to and including this fragment.
2482 */
David S. Millerd52344a2011-01-20 22:52:05 -08002483 if (PPP_MP_CB(p)->BEbits & E) {
2484 struct sk_buff *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
David S. Millerd52344a2011-01-20 22:52:05 -08002486 skb_queue_reverse_walk_from_safe(list, p, tmp2) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002487 if (ppp->debug & 1)
2488 netdev_printk(KERN_DEBUG, ppp->dev,
2489 "discarding frag %u\n",
2490 PPP_MP_CB(p)->sequence);
David S. Millerd52344a2011-01-20 22:52:05 -08002491 __skb_unlink(p, list);
2492 kfree_skb(p);
2493 }
2494 head = skb_peek(list);
2495 if (!head)
2496 break;
2497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 ++seq;
2499 }
2500
2501 /* If we have a complete packet, copy it all into one skb. */
2502 if (tail != NULL) {
2503 /* If we have discarded any fragments,
2504 signal a receive error. */
David S. Millera00eac02010-10-05 01:36:52 -07002505 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002506 skb_queue_walk_safe(list, p, tmp) {
2507 if (p == head)
2508 break;
2509 if (ppp->debug & 1)
2510 netdev_printk(KERN_DEBUG, ppp->dev,
2511 "discarding frag %u\n",
2512 PPP_MP_CB(p)->sequence);
2513 __skb_unlink(p, list);
2514 kfree_skb(p);
2515 }
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002518 netdev_printk(KERN_DEBUG, ppp->dev,
2519 " missed pkts %u..%u\n",
2520 ppp->nextseq,
2521 PPP_MP_CB(head)->sequence-1);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002522 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 ppp_receive_error(ppp);
2524 }
2525
David S. Miller212bfb92011-01-20 22:46:07 -08002526 skb = head;
2527 if (head != tail) {
2528 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2529 p = skb_queue_next(list, head);
2530 __skb_unlink(skb, list);
2531 skb_queue_walk_from_safe(list, p, tmp) {
2532 __skb_unlink(p, list);
2533 *fragpp = p;
2534 p->next = NULL;
2535 fragpp = &p->next;
2536
2537 skb->len += p->len;
2538 skb->data_len += p->len;
Eric Dumazet19c6c8f2012-02-13 04:23:24 +00002539 skb->truesize += p->truesize;
David S. Miller212bfb92011-01-20 22:46:07 -08002540
2541 if (p == tail)
2542 break;
2543 }
2544 } else {
2545 __skb_unlink(skb, list);
2546 }
2547
David S. Millera00eac02010-10-05 01:36:52 -07002548 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550
2551 return skb;
2552}
2553#endif /* CONFIG_PPP_MULTILINK */
2554
2555/*
2556 * Channel interface.
2557 */
2558
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002559/* Create a new, unattached ppp channel. */
2560int ppp_register_channel(struct ppp_channel *chan)
2561{
2562 return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2563}
2564
2565/* Create a new, unattached ppp channel for specified net. */
2566int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 struct channel *pch;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002569 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Panagiotis Issarisd4274b52006-08-15 16:01:07 -07002571 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -08002572 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return -ENOMEM;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002574
2575 pn = ppp_pernet(net);
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 pch->ppp = NULL;
2578 pch->chan = chan;
Guillaume Nault1f461dc2016-03-23 16:38:55 +01002579 pch->chan_net = get_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 chan->ppp = pch;
2581 init_ppp_file(&pch->file, CHANNEL);
2582 pch->file.hdrlen = chan->hdrlen;
2583#ifdef CONFIG_PPP_MULTILINK
2584 pch->lastseq = -1;
2585#endif /* CONFIG_PPP_MULTILINK */
2586 init_rwsem(&pch->chan_sem);
2587 spin_lock_init(&pch->downl);
2588 rwlock_init(&pch->upl);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002589
2590 spin_lock_bh(&pn->all_channels_lock);
2591 pch->file.index = ++pn->last_channel_index;
2592 list_add(&pch->list, &pn->new_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 atomic_inc(&channel_count);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002594 spin_unlock_bh(&pn->all_channels_lock);
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
2598
2599/*
2600 * Return the index of a channel.
2601 */
2602int ppp_channel_index(struct ppp_channel *chan)
2603{
2604 struct channel *pch = chan->ppp;
2605
Joe Perchescd228d52007-11-12 18:07:31 -08002606 if (pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return pch->file.index;
2608 return -1;
2609}
2610
2611/*
2612 * Return the PPP unit number to which a channel is connected.
2613 */
2614int ppp_unit_number(struct ppp_channel *chan)
2615{
2616 struct channel *pch = chan->ppp;
2617 int unit = -1;
2618
Joe Perchescd228d52007-11-12 18:07:31 -08002619 if (pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002621 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 unit = pch->ppp->file.index;
2623 read_unlock_bh(&pch->upl);
2624 }
2625 return unit;
2626}
2627
2628/*
James Chapman63f96072010-04-02 06:18:39 +00002629 * Return the PPP device interface name of a channel.
2630 */
2631char *ppp_dev_name(struct ppp_channel *chan)
2632{
2633 struct channel *pch = chan->ppp;
2634 char *name = NULL;
2635
2636 if (pch) {
2637 read_lock_bh(&pch->upl);
2638 if (pch->ppp && pch->ppp->dev)
2639 name = pch->ppp->dev->name;
2640 read_unlock_bh(&pch->upl);
2641 }
2642 return name;
2643}
2644
2645
2646/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 * Disconnect a channel from the generic layer.
2648 * This must be called in process context.
2649 */
2650void
2651ppp_unregister_channel(struct ppp_channel *chan)
2652{
2653 struct channel *pch = chan->ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002654 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Joe Perchescd228d52007-11-12 18:07:31 -08002656 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 return; /* should never happen */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 chan->ppp = NULL;
2660
2661 /*
2662 * This ensures that we have returned from any calls into the
2663 * the channel's start_xmit or ioctl routine before we proceed.
2664 */
2665 down_write(&pch->chan_sem);
2666 spin_lock_bh(&pch->downl);
2667 pch->chan = NULL;
2668 spin_unlock_bh(&pch->downl);
2669 up_write(&pch->chan_sem);
2670 ppp_disconnect_channel(pch);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002671
2672 pn = ppp_pernet(pch->chan_net);
2673 spin_lock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 list_del(&pch->list);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002675 spin_unlock_bh(&pn->all_channels_lock);
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 pch->file.dead = 1;
2678 wake_up_interruptible(&pch->file.rwait);
2679 if (atomic_dec_and_test(&pch->file.refcnt))
2680 ppp_destroy_channel(pch);
2681}
2682
2683/*
2684 * Callback from a channel when it can accept more to transmit.
2685 * This should be called at BH/softirq level, not interrupt level.
2686 */
2687void
2688ppp_output_wakeup(struct ppp_channel *chan)
2689{
2690 struct channel *pch = chan->ppp;
2691
Joe Perchescd228d52007-11-12 18:07:31 -08002692 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return;
2694 ppp_channel_push(pch);
2695}
2696
2697/*
2698 * Compression control.
2699 */
2700
2701/* Process the PPPIOCSCOMPRESS ioctl. */
2702static int
2703ppp_set_compress(struct ppp *ppp, unsigned long arg)
2704{
2705 int err;
2706 struct compressor *cp, *ocomp;
2707 struct ppp_option_data data;
2708 void *state, *ostate;
2709 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2710
2711 err = -EFAULT;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002712 if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 goto out;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002714 if (data.length > CCP_MAX_OPTION_LENGTH)
2715 goto out;
2716 if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
2717 goto out;
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 err = -EINVAL;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002720 if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 goto out;
2722
Johannes Berga65e5d72008-07-09 10:28:38 +02002723 cp = try_then_request_module(
2724 find_compressor(ccp_option[0]),
2725 "ppp-compress-%d", ccp_option[0]);
Joe Perchescd228d52007-11-12 18:07:31 -08002726 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 goto out;
2728
2729 err = -ENOBUFS;
2730 if (data.transmit) {
2731 state = cp->comp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002732 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 ppp_xmit_lock(ppp);
2734 ppp->xstate &= ~SC_COMP_RUN;
2735 ocomp = ppp->xcomp;
2736 ostate = ppp->xc_state;
2737 ppp->xcomp = cp;
2738 ppp->xc_state = state;
2739 ppp_xmit_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002740 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 ocomp->comp_free(ostate);
2742 module_put(ocomp->owner);
2743 }
2744 err = 0;
2745 } else
2746 module_put(cp->owner);
2747
2748 } else {
2749 state = cp->decomp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002750 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 ppp_recv_lock(ppp);
2752 ppp->rstate &= ~SC_DECOMP_RUN;
2753 ocomp = ppp->rcomp;
2754 ostate = ppp->rc_state;
2755 ppp->rcomp = cp;
2756 ppp->rc_state = state;
2757 ppp_recv_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002758 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 ocomp->decomp_free(ostate);
2760 module_put(ocomp->owner);
2761 }
2762 err = 0;
2763 } else
2764 module_put(cp->owner);
2765 }
2766
2767 out:
2768 return err;
2769}
2770
2771/*
2772 * Look at a CCP packet and update our state accordingly.
2773 * We assume the caller has the xmit or recv path locked.
2774 */
2775static void
2776ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2777{
2778 unsigned char *dp;
2779 int len;
2780
2781 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2782 return; /* no header */
2783 dp = skb->data + 2;
2784
2785 switch (CCP_CODE(dp)) {
2786 case CCP_CONFREQ:
2787
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002788 /* A ConfReq starts negotiation of compression
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 * in one direction of transmission,
2790 * and hence brings it down...but which way?
2791 *
2792 * Remember:
2793 * A ConfReq indicates what the sender would like to receive
2794 */
2795 if(inbound)
2796 /* He is proposing what I should send */
2797 ppp->xstate &= ~SC_COMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002798 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /* I am proposing to what he should send */
2800 ppp->rstate &= ~SC_DECOMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 case CCP_TERMREQ:
2805 case CCP_TERMACK:
2806 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002807 * CCP is going down, both directions of transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 */
2809 ppp->rstate &= ~SC_DECOMP_RUN;
2810 ppp->xstate &= ~SC_COMP_RUN;
2811 break;
2812
2813 case CCP_CONFACK:
2814 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2815 break;
2816 len = CCP_LENGTH(dp);
2817 if (!pskb_may_pull(skb, len + 2))
2818 return; /* too short */
2819 dp += CCP_HDRLEN;
2820 len -= CCP_HDRLEN;
2821 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2822 break;
2823 if (inbound) {
2824 /* we will start receiving compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002825 if (!ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 break;
2827 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2828 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2829 ppp->rstate |= SC_DECOMP_RUN;
2830 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2831 }
2832 } else {
2833 /* we will soon start sending compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002834 if (!ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 break;
2836 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2837 ppp->file.index, 0, ppp->debug))
2838 ppp->xstate |= SC_COMP_RUN;
2839 }
2840 break;
2841
2842 case CCP_RESETACK:
2843 /* reset the [de]compressor */
2844 if ((ppp->flags & SC_CCP_UP) == 0)
2845 break;
2846 if (inbound) {
2847 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2848 ppp->rcomp->decomp_reset(ppp->rc_state);
2849 ppp->rstate &= ~SC_DC_ERROR;
2850 }
2851 } else {
2852 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2853 ppp->xcomp->comp_reset(ppp->xc_state);
2854 }
2855 break;
2856 }
2857}
2858
2859/* Free up compression resources. */
2860static void
2861ppp_ccp_closed(struct ppp *ppp)
2862{
2863 void *xstate, *rstate;
2864 struct compressor *xcomp, *rcomp;
2865
2866 ppp_lock(ppp);
2867 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2868 ppp->xstate = 0;
2869 xcomp = ppp->xcomp;
2870 xstate = ppp->xc_state;
2871 ppp->xc_state = NULL;
2872 ppp->rstate = 0;
2873 rcomp = ppp->rcomp;
2874 rstate = ppp->rc_state;
2875 ppp->rc_state = NULL;
2876 ppp_unlock(ppp);
2877
2878 if (xstate) {
2879 xcomp->comp_free(xstate);
2880 module_put(xcomp->owner);
2881 }
2882 if (rstate) {
2883 rcomp->decomp_free(rstate);
2884 module_put(rcomp->owner);
2885 }
2886}
2887
2888/* List of compressors. */
2889static LIST_HEAD(compressor_list);
2890static DEFINE_SPINLOCK(compressor_list_lock);
2891
2892struct compressor_entry {
2893 struct list_head list;
2894 struct compressor *comp;
2895};
2896
2897static struct compressor_entry *
2898find_comp_entry(int proto)
2899{
2900 struct compressor_entry *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Domen Puncer81616c52005-09-10 00:27:04 -07002902 list_for_each_entry(ce, &compressor_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (ce->comp->compress_proto == proto)
2904 return ce;
2905 }
2906 return NULL;
2907}
2908
2909/* Register a compressor */
2910int
2911ppp_register_compressor(struct compressor *cp)
2912{
2913 struct compressor_entry *ce;
2914 int ret;
2915 spin_lock(&compressor_list_lock);
2916 ret = -EEXIST;
Joe Perchescd228d52007-11-12 18:07:31 -08002917 if (find_comp_entry(cp->compress_proto))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 goto out;
2919 ret = -ENOMEM;
2920 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002921 if (!ce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 goto out;
2923 ret = 0;
2924 ce->comp = cp;
2925 list_add(&ce->list, &compressor_list);
2926 out:
2927 spin_unlock(&compressor_list_lock);
2928 return ret;
2929}
2930
2931/* Unregister a compressor */
2932void
2933ppp_unregister_compressor(struct compressor *cp)
2934{
2935 struct compressor_entry *ce;
2936
2937 spin_lock(&compressor_list_lock);
2938 ce = find_comp_entry(cp->compress_proto);
Joe Perchescd228d52007-11-12 18:07:31 -08002939 if (ce && ce->comp == cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 list_del(&ce->list);
2941 kfree(ce);
2942 }
2943 spin_unlock(&compressor_list_lock);
2944}
2945
2946/* Find a compressor. */
2947static struct compressor *
2948find_compressor(int type)
2949{
2950 struct compressor_entry *ce;
2951 struct compressor *cp = NULL;
2952
2953 spin_lock(&compressor_list_lock);
2954 ce = find_comp_entry(type);
Joe Perchescd228d52007-11-12 18:07:31 -08002955 if (ce) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 cp = ce->comp;
2957 if (!try_module_get(cp->owner))
2958 cp = NULL;
2959 }
2960 spin_unlock(&compressor_list_lock);
2961 return cp;
2962}
2963
2964/*
2965 * Miscelleneous stuff.
2966 */
2967
2968static void
2969ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2970{
2971 struct slcompress *vj = ppp->vj;
2972
2973 memset(st, 0, sizeof(*st));
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002974 st->p.ppp_ipackets = ppp->stats64.rx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002975 st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002976 st->p.ppp_ibytes = ppp->stats64.rx_bytes;
2977 st->p.ppp_opackets = ppp->stats64.tx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002978 st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002979 st->p.ppp_obytes = ppp->stats64.tx_bytes;
Joe Perchescd228d52007-11-12 18:07:31 -08002980 if (!vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return;
2982 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2983 st->vj.vjs_compressed = vj->sls_o_compressed;
2984 st->vj.vjs_searches = vj->sls_o_searches;
2985 st->vj.vjs_misses = vj->sls_o_misses;
2986 st->vj.vjs_errorin = vj->sls_i_error;
2987 st->vj.vjs_tossed = vj->sls_i_tossed;
2988 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2989 st->vj.vjs_compressedin = vj->sls_i_compressed;
2990}
2991
2992/*
2993 * Stuff for handling the lists of ppp units and channels
2994 * and for initialization.
2995 */
2996
2997/*
2998 * Create a new ppp interface unit. Fails if it can't allocate memory
2999 * or if there is already a unit with the requested number.
3000 * unit == -1 means allocate a new number.
3001 */
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003002static int ppp_create_interface(struct net *net, struct file *file, int *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003004 struct ppp_config conf = {
3005 .file = file,
3006 .unit = *unit,
Guillaume Nault96d934c2016-04-28 17:55:30 +02003007 .ifname_is_set = false,
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003008 };
3009 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 struct ppp *ppp;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Guillaume Nault69d97282015-12-11 19:54:52 +01003013 dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003014 if (!dev) {
3015 err = -ENOMEM;
3016 goto err;
3017 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003018 dev_net_set(dev, net);
Guillaume Nault96d934c2016-04-28 17:55:30 +02003019 dev->rtnl_link_ops = &ppp_link_ops;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003020
Guillaume Nault58a89ec2015-09-24 12:54:01 +02003021 rtnl_lock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003022
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003023 err = ppp_dev_configure(net, dev, &conf);
3024 if (err < 0)
3025 goto err_dev;
3026 ppp = netdev_priv(dev);
3027 *unit = ppp->file.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Guillaume Nault58a89ec2015-09-24 12:54:01 +02003029 rtnl_unlock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003030
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003033err_dev:
Guillaume Nault6faac632016-03-07 19:36:44 +01003034 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 free_netdev(dev);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003036err:
3037 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
3040/*
3041 * Initialize a ppp_file structure.
3042 */
3043static void
3044init_ppp_file(struct ppp_file *pf, int kind)
3045{
3046 pf->kind = kind;
3047 skb_queue_head_init(&pf->xq);
3048 skb_queue_head_init(&pf->rq);
3049 atomic_set(&pf->refcnt, 1);
3050 init_waitqueue_head(&pf->rwait);
3051}
3052
3053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 * Free the memory used by a ppp unit. This is only called once
3055 * there are no channels connected to the unit and no file structs
3056 * that reference the unit.
3057 */
3058static void ppp_destroy_interface(struct ppp *ppp)
3059{
3060 atomic_dec(&ppp_unit_count);
3061
3062 if (!ppp->file.dead || ppp->n_channels) {
3063 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003064 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3065 "but dead=%d n_channels=%d !\n",
3066 ppp, ppp->file.dead, ppp->n_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return;
3068 }
3069
3070 ppp_ccp_closed(ppp);
3071 if (ppp->vj) {
3072 slhc_free(ppp->vj);
3073 ppp->vj = NULL;
3074 }
3075 skb_queue_purge(&ppp->file.xq);
3076 skb_queue_purge(&ppp->file.rq);
3077#ifdef CONFIG_PPP_MULTILINK
3078 skb_queue_purge(&ppp->mrq);
3079#endif /* CONFIG_PPP_MULTILINK */
3080#ifdef CONFIG_PPP_FILTER
Daniel Borkmann568f1942014-03-28 18:58:23 +01003081 if (ppp->pass_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003082 bpf_prog_destroy(ppp->pass_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003083 ppp->pass_filter = NULL;
3084 }
3085
3086 if (ppp->active_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003087 bpf_prog_destroy(ppp->active_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003088 ppp->active_filter = NULL;
3089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090#endif /* CONFIG_PPP_FILTER */
3091
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00003092 kfree_skb(ppp->xmit_pending);
Gao Feng3b25bfc2017-07-17 18:34:42 +08003093 free_percpu(ppp->xmit_recursion);
G. Liakhovetski165de5b2007-03-25 19:04:09 -07003094
James Chapman739840d2008-12-17 12:02:16 +00003095 free_netdev(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
3098/*
3099 * Locate an existing ppp unit.
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08003100 * The caller should have locked the all_ppp_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 */
3102static struct ppp *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003103ppp_find_unit(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003105 return unit_find(&pn->units_idr, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
3108/*
3109 * Locate an existing ppp channel.
3110 * The caller should have locked the all_channels_lock.
3111 * First we look in the new_channels list, then in the
3112 * all_channels list. If found in the new_channels list,
3113 * we move it to the all_channels list. This is for speed
3114 * when we have a lot of channels in use.
3115 */
3116static struct channel *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003117ppp_find_channel(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
3119 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003121 list_for_each_entry(pch, &pn->new_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (pch->file.index == unit) {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003123 list_move(&pch->list, &pn->all_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return pch;
3125 }
3126 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003127
3128 list_for_each_entry(pch, &pn->all_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (pch->file.index == unit)
3130 return pch;
3131 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return NULL;
3134}
3135
3136/*
3137 * Connect a PPP channel to a PPP interface unit.
3138 */
3139static int
3140ppp_connect_channel(struct channel *pch, int unit)
3141{
3142 struct ppp *ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003143 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 int ret = -ENXIO;
3145 int hdrlen;
3146
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003147 pn = ppp_pernet(pch->chan_net);
3148
3149 mutex_lock(&pn->all_ppp_mutex);
3150 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -08003151 if (!ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 goto out;
3153 write_lock_bh(&pch->upl);
3154 ret = -EINVAL;
Joe Perchescd228d52007-11-12 18:07:31 -08003155 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 goto outl;
3157
3158 ppp_lock(ppp);
3159 if (pch->file.hdrlen > ppp->file.hdrlen)
3160 ppp->file.hdrlen = pch->file.hdrlen;
3161 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
James Chapman739840d2008-12-17 12:02:16 +00003162 if (hdrlen > ppp->dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 ppp->dev->hard_header_len = hdrlen;
3164 list_add_tail(&pch->clist, &ppp->channels);
3165 ++ppp->n_channels;
3166 pch->ppp = ppp;
3167 atomic_inc(&ppp->file.refcnt);
3168 ppp_unlock(ppp);
3169 ret = 0;
3170
3171 outl:
3172 write_unlock_bh(&pch->upl);
3173 out:
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003174 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return ret;
3176}
3177
3178/*
3179 * Disconnect a channel from its ppp unit.
3180 */
3181static int
3182ppp_disconnect_channel(struct channel *pch)
3183{
3184 struct ppp *ppp;
3185 int err = -EINVAL;
3186
3187 write_lock_bh(&pch->upl);
3188 ppp = pch->ppp;
3189 pch->ppp = NULL;
3190 write_unlock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08003191 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 /* remove it from the ppp unit's list */
3193 ppp_lock(ppp);
3194 list_del(&pch->clist);
3195 if (--ppp->n_channels == 0)
3196 wake_up_interruptible(&ppp->file.rwait);
3197 ppp_unlock(ppp);
3198 if (atomic_dec_and_test(&ppp->file.refcnt))
3199 ppp_destroy_interface(ppp);
3200 err = 0;
3201 }
3202 return err;
3203}
3204
3205/*
3206 * Free up the resources used by a ppp channel.
3207 */
3208static void ppp_destroy_channel(struct channel *pch)
3209{
WANG Cong205e1e22016-07-05 22:12:36 -07003210 put_net(pch->chan_net);
3211 pch->chan_net = NULL;
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 atomic_dec(&channel_count);
3214
3215 if (!pch->file.dead) {
3216 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003217 pr_err("ppp: destroying undead channel %p !\n", pch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return;
3219 }
3220 skb_queue_purge(&pch->file.xq);
3221 skb_queue_purge(&pch->file.rq);
3222 kfree(pch);
3223}
3224
3225static void __exit ppp_cleanup(void)
3226{
3227 /* should never happen */
3228 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
David S. Millerb48f8c22011-01-20 22:44:36 -08003229 pr_err("PPP: removing module but units remain!\n");
Guillaume Nault96d934c2016-04-28 17:55:30 +02003230 rtnl_link_unregister(&ppp_link_ops);
Akinobu Mita68fc4fa2007-07-19 01:47:50 -07003231 unregister_chrdev(PPP_MAJOR, "ppp");
Greg Kroah-Hartman9a6a2a52006-09-12 17:00:10 +02003232 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08003233 class_destroy(ppp_class);
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00003234 unregister_pernet_device(&ppp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
3237/*
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003238 * Units handling. Caller must protect concurrent access
3239 * by holding all_ppp_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Cyrill Gorcunovbcc70bb2010-11-23 11:43:44 +00003242/* associate pointer with specified number */
3243static int unit_set(struct idr *p, void *ptr, int n)
3244{
3245 int unit;
3246
Tejun Heo2fa532c2013-02-27 17:04:36 -08003247 unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3248 if (unit == -ENOSPC)
3249 unit = -EINVAL;
Cyrill Gorcunov85997572009-01-12 22:11:56 -08003250 return unit;
3251}
3252
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003253/* get new free unit number and associate pointer with it */
3254static int unit_get(struct idr *p, void *ptr)
3255{
Tejun Heo2fa532c2013-02-27 17:04:36 -08003256 return idr_alloc(p, ptr, 0, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003259/* put unit number back to a pool */
3260static void unit_put(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003262 idr_remove(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003265/* get pointer associated with the number */
3266static void *unit_find(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003268 return idr_find(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
3271/* Module/initialization stuff */
3272
3273module_init(ppp_init);
3274module_exit(ppp_cleanup);
3275
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003276EXPORT_SYMBOL(ppp_register_net_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277EXPORT_SYMBOL(ppp_register_channel);
3278EXPORT_SYMBOL(ppp_unregister_channel);
3279EXPORT_SYMBOL(ppp_channel_index);
3280EXPORT_SYMBOL(ppp_unit_number);
James Chapman63f96072010-04-02 06:18:39 +00003281EXPORT_SYMBOL(ppp_dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282EXPORT_SYMBOL(ppp_input);
3283EXPORT_SYMBOL(ppp_input_error);
3284EXPORT_SYMBOL(ppp_output_wakeup);
3285EXPORT_SYMBOL(ppp_register_compressor);
3286EXPORT_SYMBOL(ppp_unregister_compressor);
3287MODULE_LICENSE("GPL");
Kay Sievers578454f2010-05-20 18:07:20 +02003288MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
Guillaume Nault96d934c2016-04-28 17:55:30 +02003289MODULE_ALIAS_RTNL_LINK("ppp");
Kay Sievers578454f2010-05-20 18:07:20 +02003290MODULE_ALIAS("devname:ppp");