blob: fc4c2ccc3d22508e8b3b217ec511368204478b72 [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
Guillaume Nault00f9e472018-01-10 16:24:45 +01001005 mutex_unlock(&pn->all_ppp_mutex);
1006
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001007 ret = register_netdevice(ppp->dev);
1008 if (ret < 0)
1009 goto err_unit;
1010
1011 atomic_inc(&ppp_unit_count);
1012
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001013 return 0;
1014
1015err_unit:
Guillaume Nault00f9e472018-01-10 16:24:45 +01001016 mutex_lock(&pn->all_ppp_mutex);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001017 unit_put(&pn->units_idr, ppp->file.index);
1018err:
1019 mutex_unlock(&pn->all_ppp_mutex);
1020
1021 return ret;
1022}
1023
1024static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1025 const struct ppp_config *conf)
1026{
1027 struct ppp *ppp = netdev_priv(dev);
1028 int indx;
1029 int err;
Gao Feng3b25bfc2017-07-17 18:34:42 +08001030 int cpu;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001031
1032 ppp->dev = dev;
1033 ppp->ppp_net = src_net;
1034 ppp->mru = PPP_MRU;
1035 ppp->owner = conf->file;
1036
1037 init_ppp_file(&ppp->file, INTERFACE);
1038 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1039
1040 for (indx = 0; indx < NUM_NP; ++indx)
1041 ppp->npmode[indx] = NPMODE_PASS;
1042 INIT_LIST_HEAD(&ppp->channels);
1043 spin_lock_init(&ppp->rlock);
1044 spin_lock_init(&ppp->wlock);
Gao Feng3b25bfc2017-07-17 18:34:42 +08001045
1046 ppp->xmit_recursion = alloc_percpu(int);
1047 if (!ppp->xmit_recursion) {
1048 err = -ENOMEM;
1049 goto err1;
1050 }
1051 for_each_possible_cpu(cpu)
1052 (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
1053
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001054#ifdef CONFIG_PPP_MULTILINK
1055 ppp->minseq = -1;
1056 skb_queue_head_init(&ppp->mrq);
1057#endif /* CONFIG_PPP_MULTILINK */
1058#ifdef CONFIG_PPP_FILTER
1059 ppp->pass_filter = NULL;
1060 ppp->active_filter = NULL;
1061#endif /* CONFIG_PPP_FILTER */
1062
Guillaume Nault96d934c2016-04-28 17:55:30 +02001063 err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001064 if (err < 0)
Gao Feng3b25bfc2017-07-17 18:34:42 +08001065 goto err2;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001066
1067 conf->file->private_data = &ppp->file;
1068
1069 return 0;
Gao Feng3b25bfc2017-07-17 18:34:42 +08001070err2:
1071 free_percpu(ppp->xmit_recursion);
1072err1:
1073 return err;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001074}
1075
Guillaume Nault96d934c2016-04-28 17:55:30 +02001076static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1077 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
1078};
1079
1080static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[])
1081{
1082 if (!data)
1083 return -EINVAL;
1084
1085 if (!data[IFLA_PPP_DEV_FD])
1086 return -EINVAL;
1087 if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1088 return -EBADF;
1089
1090 return 0;
1091}
1092
1093static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1094 struct nlattr *tb[], struct nlattr *data[])
1095{
1096 struct ppp_config conf = {
1097 .unit = -1,
1098 .ifname_is_set = true,
1099 };
1100 struct file *file;
1101 int err;
1102
1103 file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1104 if (!file)
1105 return -EBADF;
1106
1107 /* rtnl_lock is already held here, but ppp_create_interface() locks
1108 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1109 * possible deadlock due to lock order inversion, at the cost of
1110 * pushing the problem back to userspace.
1111 */
1112 if (!mutex_trylock(&ppp_mutex)) {
1113 err = -EBUSY;
1114 goto out;
1115 }
1116
1117 if (file->f_op != &ppp_device_fops || file->private_data) {
1118 err = -EBADF;
1119 goto out_unlock;
1120 }
1121
1122 conf.file = file;
Guillaume Naultbb8082f2016-08-09 15:12:26 +02001123
1124 /* Don't use device name generated by the rtnetlink layer when ifname
1125 * isn't specified. Let ppp_dev_configure() set the device name using
1126 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1127 * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1128 */
1129 if (!tb[IFLA_IFNAME])
1130 conf.ifname_is_set = false;
1131
Guillaume Nault96d934c2016-04-28 17:55:30 +02001132 err = ppp_dev_configure(src_net, dev, &conf);
1133
1134out_unlock:
1135 mutex_unlock(&ppp_mutex);
1136out:
1137 fput(file);
1138
1139 return err;
1140}
1141
1142static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1143{
1144 unregister_netdevice_queue(dev, head);
1145}
1146
1147static size_t ppp_nl_get_size(const struct net_device *dev)
1148{
1149 return 0;
1150}
1151
1152static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1153{
1154 return 0;
1155}
1156
1157static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1158{
1159 struct ppp *ppp = netdev_priv(dev);
1160
1161 return ppp->ppp_net;
1162}
1163
1164static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1165 .kind = "ppp",
1166 .maxtype = IFLA_PPP_MAX,
1167 .policy = ppp_nl_policy,
1168 .priv_size = sizeof(struct ppp),
1169 .setup = ppp_setup,
1170 .validate = ppp_nl_validate,
1171 .newlink = ppp_nl_newlink,
1172 .dellink = ppp_nl_dellink,
1173 .get_size = ppp_nl_get_size,
1174 .fill_info = ppp_nl_fill_info,
1175 .get_link_net = ppp_nl_get_link_net,
1176};
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178#define PPP_MAJOR 108
1179
1180/* Called at boot time if ppp is compiled into the kernel,
1181 or at module load time (from init_module) if compiled as a module. */
1182static int __init ppp_init(void)
1183{
1184 int err;
1185
David S. Millerb48f8c22011-01-20 22:44:36 -08001186 pr_info("PPP generic driver version " PPP_VERSION "\n");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001187
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001188 err = register_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001189 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001190 pr_err("failed to register PPP pernet device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001194 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1195 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001196 pr_err("failed to register PPP device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001197 goto out_net;
1198 }
1199
1200 ppp_class = class_create(THIS_MODULE, "ppp");
1201 if (IS_ERR(ppp_class)) {
1202 err = PTR_ERR(ppp_class);
1203 goto out_chrdev;
1204 }
1205
Guillaume Nault96d934c2016-04-28 17:55:30 +02001206 err = rtnl_link_register(&ppp_link_ops);
1207 if (err) {
1208 pr_err("failed to register rtnetlink PPP handler\n");
1209 goto out_class;
1210 }
1211
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001212 /* not a big deal if we fail here :-) */
1213 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1214
1215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Guillaume Nault96d934c2016-04-28 17:55:30 +02001217out_class:
1218 class_destroy(ppp_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219out_chrdev:
1220 unregister_chrdev(PPP_MAJOR, "ppp");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001221out_net:
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001222 unregister_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001223out:
1224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227/*
1228 * Network interface unit routines.
1229 */
Stephen Hemminger424efe92009-08-31 19:50:51 +00001230static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1232{
Wang Chenc8019bf2008-11-20 04:24:17 -08001233 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int npi, proto;
1235 unsigned char *pp;
1236
1237 npi = ethertype_to_npindex(ntohs(skb->protocol));
1238 if (npi < 0)
1239 goto outf;
1240
1241 /* Drop, accept or reject the packet */
1242 switch (ppp->npmode[npi]) {
1243 case NPMODE_PASS:
1244 break;
1245 case NPMODE_QUEUE:
1246 /* it would be nice to have a way to tell the network
1247 system to queue this one up for later. */
1248 goto outf;
1249 case NPMODE_DROP:
1250 case NPMODE_ERROR:
1251 goto outf;
1252 }
1253
1254 /* Put the 2-byte PPP protocol number on the front,
1255 making sure there is room for the address and control fields. */
Herbert Xu7b797d52007-09-16 16:21:42 -07001256 if (skb_cow_head(skb, PPP_HDRLEN))
1257 goto outf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 pp = skb_push(skb, 2);
1260 proto = npindex_to_proto[npi];
Changli Gao96545ae2011-01-06 13:37:36 +00001261 put_unaligned_be16(proto, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Guillaume Nault79c441a2015-08-24 11:35:30 +02001263 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 skb_queue_tail(&ppp->file.xq, skb);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001265 ppp_xmit_process(ppp);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001266 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 outf:
1269 kfree_skb(skb);
Paulius Zaleckas6ceffd42009-02-23 04:59:43 +00001270 ++dev->stats.tx_dropped;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001271 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274static int
1275ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1276{
Wang Chenc8019bf2008-11-20 04:24:17 -08001277 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int err = -EFAULT;
1279 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1280 struct ppp_stats stats;
1281 struct ppp_comp_stats cstats;
1282 char *vers;
1283
1284 switch (cmd) {
1285 case SIOCGPPPSTATS:
1286 ppp_get_stats(ppp, &stats);
1287 if (copy_to_user(addr, &stats, sizeof(stats)))
1288 break;
1289 err = 0;
1290 break;
1291
1292 case SIOCGPPPCSTATS:
1293 memset(&cstats, 0, sizeof(cstats));
Joe Perchescd228d52007-11-12 18:07:31 -08001294 if (ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
Joe Perchescd228d52007-11-12 18:07:31 -08001296 if (ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1298 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1299 break;
1300 err = 0;
1301 break;
1302
1303 case SIOCGPPPVER:
1304 vers = PPP_VERSION;
1305 if (copy_to_user(addr, vers, strlen(vers) + 1))
1306 break;
1307 err = 0;
1308 break;
1309
1310 default:
1311 err = -EINVAL;
1312 }
1313
1314 return err;
1315}
1316
stephen hemmingerb77bc202012-10-29 08:34:02 +00001317static struct rtnl_link_stats64*
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001318ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1319{
1320 struct ppp *ppp = netdev_priv(dev);
1321
1322 ppp_recv_lock(ppp);
1323 stats64->rx_packets = ppp->stats64.rx_packets;
1324 stats64->rx_bytes = ppp->stats64.rx_bytes;
1325 ppp_recv_unlock(ppp);
1326
1327 ppp_xmit_lock(ppp);
1328 stats64->tx_packets = ppp->stats64.tx_packets;
1329 stats64->tx_bytes = ppp->stats64.tx_bytes;
1330 ppp_xmit_unlock(ppp);
1331
1332 stats64->rx_errors = dev->stats.rx_errors;
1333 stats64->tx_errors = dev->stats.tx_errors;
1334 stats64->rx_dropped = dev->stats.rx_dropped;
1335 stats64->tx_dropped = dev->stats.tx_dropped;
1336 stats64->rx_length_errors = dev->stats.rx_length_errors;
1337
1338 return stats64;
1339}
1340
Eric Dumazet303c07d2013-02-19 10:42:03 -08001341static int ppp_dev_init(struct net_device *dev)
1342{
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001343 struct ppp *ppp;
1344
Eric Dumazetd3fff6c2016-06-09 07:45:12 -07001345 netdev_lockdep_set_classes(dev);
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001346
1347 ppp = netdev_priv(dev);
1348 /* Let the netdevice take a reference on the ppp file. This ensures
1349 * that ppp_destroy_interface() won't run before the device gets
1350 * unregistered.
1351 */
1352 atomic_inc(&ppp->file.refcnt);
1353
Eric Dumazet303c07d2013-02-19 10:42:03 -08001354 return 0;
1355}
1356
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001357static void ppp_dev_uninit(struct net_device *dev)
1358{
1359 struct ppp *ppp = netdev_priv(dev);
1360 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1361
1362 ppp_lock(ppp);
1363 ppp->closing = 1;
1364 ppp_unlock(ppp);
1365
1366 mutex_lock(&pn->all_ppp_mutex);
1367 unit_put(&pn->units_idr, ppp->file.index);
1368 mutex_unlock(&pn->all_ppp_mutex);
1369
1370 ppp->owner = NULL;
1371
1372 ppp->file.dead = 1;
1373 wake_up_interruptible(&ppp->file.rwait);
1374}
1375
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001376static void ppp_dev_priv_destructor(struct net_device *dev)
1377{
1378 struct ppp *ppp;
1379
1380 ppp = netdev_priv(dev);
1381 if (atomic_dec_and_test(&ppp->file.refcnt))
1382 ppp_destroy_interface(ppp);
1383}
1384
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001385static const struct net_device_ops ppp_netdev_ops = {
Eric Dumazet303c07d2013-02-19 10:42:03 -08001386 .ndo_init = ppp_dev_init,
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001387 .ndo_uninit = ppp_dev_uninit,
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001388 .ndo_start_xmit = ppp_start_xmit,
1389 .ndo_do_ioctl = ppp_net_ioctl,
1390 .ndo_get_stats64 = ppp_get_stats64,
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001391};
1392
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001393static struct device_type ppp_type = {
1394 .name = "ppp",
1395};
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397static void ppp_setup(struct net_device *dev)
1398{
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001399 dev->netdev_ops = &ppp_netdev_ops;
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001400 SET_NETDEV_DEVTYPE(dev, &ppp_type);
1401
Guillaume Nault07712772016-08-27 22:22:51 +02001402 dev->features |= NETIF_F_LLTX;
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 dev->hard_header_len = PPP_HDRLEN;
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +00001405 dev->mtu = PPP_MRU;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 dev->addr_len = 0;
1407 dev->tx_queue_len = 3;
1408 dev->type = ARPHRD_PPP;
1409 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001410 dev->destructor = ppp_dev_priv_destructor;
Eric Dumazet02875872014-10-05 18:38:35 -07001411 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414/*
1415 * Transmit-side routines.
1416 */
1417
Guillaume Nault55454a52016-08-27 22:22:32 +02001418/* Called to do any work queued up on the transmit side that can now be done */
1419static void __ppp_xmit_process(struct ppp *ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 struct sk_buff *skb;
1422
1423 ppp_xmit_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001424 if (!ppp->closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 ppp_push(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +00001426 while (!ppp->xmit_pending &&
1427 (skb = skb_dequeue(&ppp->file.xq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 ppp_send_frame(ppp, skb);
1429 /* If there's no work left to do, tell the core net
1430 code that we can accept some more. */
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001431 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 netif_wake_queue(ppp->dev);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001433 else
1434 netif_stop_queue(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 ppp_xmit_unlock(ppp);
1437}
1438
Guillaume Nault55454a52016-08-27 22:22:32 +02001439static void ppp_xmit_process(struct ppp *ppp)
1440{
1441 local_bh_disable();
1442
Gao Feng3b25bfc2017-07-17 18:34:42 +08001443 if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
Guillaume Nault55454a52016-08-27 22:22:32 +02001444 goto err;
1445
Gao Feng3b25bfc2017-07-17 18:34:42 +08001446 (*this_cpu_ptr(ppp->xmit_recursion))++;
Guillaume Nault55454a52016-08-27 22:22:32 +02001447 __ppp_xmit_process(ppp);
Gao Feng3b25bfc2017-07-17 18:34:42 +08001448 (*this_cpu_ptr(ppp->xmit_recursion))--;
Guillaume Nault55454a52016-08-27 22:22:32 +02001449
1450 local_bh_enable();
1451
1452 return;
1453
1454err:
1455 local_bh_enable();
1456
1457 if (net_ratelimit())
1458 netdev_err(ppp->dev, "recursion detected\n");
1459}
1460
Matt Domschb3f9b922005-11-08 09:40:47 -08001461static inline struct sk_buff *
1462pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1463{
1464 struct sk_buff *new_skb;
1465 int len;
1466 int new_skb_size = ppp->dev->mtu +
1467 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1468 int compressor_skb_size = ppp->dev->mtu +
1469 ppp->xcomp->comp_extra + PPP_HDRLEN;
1470 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1471 if (!new_skb) {
1472 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001473 netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001474 return NULL;
1475 }
1476 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1477 skb_reserve(new_skb,
1478 ppp->dev->hard_header_len - PPP_HDRLEN);
1479
1480 /* compressor still expects A/C bytes in hdr */
1481 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1482 new_skb->data, skb->len + 2,
1483 compressor_skb_size);
1484 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
Eric Dumazet968d7012012-05-18 20:23:00 +00001485 consume_skb(skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001486 skb = new_skb;
1487 skb_put(skb, len);
1488 skb_pull(skb, 2); /* pull off A/C bytes */
1489 } else if (len == 0) {
1490 /* didn't compress, or CCP not up yet */
Eric Dumazet968d7012012-05-18 20:23:00 +00001491 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001492 new_skb = skb;
1493 } else {
1494 /*
1495 * (len < 0)
1496 * MPPE requires that we do not send unencrypted
1497 * frames. The compressor will return -1 if we
1498 * should drop the frame. We cannot simply test
1499 * the compress_proto because MPPE and MPPC share
1500 * the same number.
1501 */
1502 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001503 netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001504 kfree_skb(skb);
Eric Dumazet968d7012012-05-18 20:23:00 +00001505 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001506 new_skb = NULL;
1507 }
1508 return new_skb;
1509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/*
1512 * Compress and send a frame.
1513 * The caller should have locked the xmit path,
1514 * and xmit_pending should be 0.
1515 */
1516static void
1517ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1518{
1519 int proto = PPP_PROTO(skb);
1520 struct sk_buff *new_skb;
1521 int len;
1522 unsigned char *cp;
1523
1524 if (proto < 0x8000) {
1525#ifdef CONFIG_PPP_FILTER
1526 /* check if we should pass this packet */
1527 /* the filter instructions are constructed assuming
1528 a four-byte PPP header on each packet */
1529 *skb_push(skb, 2) = 1;
Joe Perches8e95a202009-12-03 07:58:21 +00001530 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001531 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001533 netdev_printk(KERN_DEBUG, ppp->dev,
1534 "PPP: outbound frame "
1535 "not passed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 kfree_skb(skb);
1537 return;
1538 }
1539 /* if this packet passes the active filter, record the time */
Joe Perches8e95a202009-12-03 07:58:21 +00001540 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001541 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 ppp->last_xmit = jiffies;
1543 skb_pull(skb, 2);
1544#else
1545 /* for data packets, record the time */
1546 ppp->last_xmit = jiffies;
1547#endif /* CONFIG_PPP_FILTER */
1548 }
1549
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001550 ++ppp->stats64.tx_packets;
1551 ppp->stats64.tx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 switch (proto) {
1554 case PPP_IP:
Joe Perchescd228d52007-11-12 18:07:31 -08001555 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 break;
1557 /* try to do VJ TCP header compression */
1558 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1559 GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001560 if (!new_skb) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001561 netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 goto drop;
1563 }
1564 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1565 cp = skb->data + 2;
1566 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1567 new_skb->data + 2, &cp,
1568 !(ppp->flags & SC_NO_TCP_CCID));
1569 if (cp == skb->data + 2) {
1570 /* didn't compress */
Eric Dumazet968d7012012-05-18 20:23:00 +00001571 consume_skb(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 } else {
1573 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1574 proto = PPP_VJC_COMP;
1575 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1576 } else {
1577 proto = PPP_VJC_UNCOMP;
1578 cp[0] = skb->data[2];
1579 }
Eric Dumazet968d7012012-05-18 20:23:00 +00001580 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 skb = new_skb;
1582 cp = skb_put(skb, len + 2);
1583 cp[0] = 0;
1584 cp[1] = proto;
1585 }
1586 break;
1587
1588 case PPP_CCP:
1589 /* peek at outbound CCP frames */
1590 ppp_ccp_peek(ppp, skb, 0);
1591 break;
1592 }
1593
1594 /* try to do packet compression */
Joe Perches8e95a202009-12-03 07:58:21 +00001595 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1596 proto != PPP_LCP && proto != PPP_CCP) {
Matt Domschb3f9b922005-11-08 09:40:47 -08001597 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1598 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001599 netdev_err(ppp->dev,
1600 "ppp: compression required but "
1601 "down - pkt dropped.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto drop;
1603 }
Matt Domschb3f9b922005-11-08 09:40:47 -08001604 skb = pad_compress_skb(ppp, skb);
1605 if (!skb)
1606 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 /*
1610 * If we are waiting for traffic (demand dialling),
1611 * queue it up for pppd to receive.
1612 */
1613 if (ppp->flags & SC_LOOP_TRAFFIC) {
1614 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1615 goto drop;
1616 skb_queue_tail(&ppp->file.rq, skb);
1617 wake_up_interruptible(&ppp->file.rwait);
1618 return;
1619 }
1620
1621 ppp->xmit_pending = skb;
1622 ppp_push(ppp);
1623 return;
1624
1625 drop:
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00001626 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001627 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
1630/*
1631 * Try to send the frame in xmit_pending.
1632 * The caller should have the xmit path locked.
1633 */
1634static void
1635ppp_push(struct ppp *ppp)
1636{
1637 struct list_head *list;
1638 struct channel *pch;
1639 struct sk_buff *skb = ppp->xmit_pending;
1640
Joe Perchescd228d52007-11-12 18:07:31 -08001641 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return;
1643
1644 list = &ppp->channels;
1645 if (list_empty(list)) {
1646 /* nowhere to send the packet, just drop it */
1647 ppp->xmit_pending = NULL;
1648 kfree_skb(skb);
1649 return;
1650 }
1651
1652 if ((ppp->flags & SC_MULTILINK) == 0) {
1653 /* not doing multilink: send it down the first channel */
1654 list = list->next;
1655 pch = list_entry(list, struct channel, clist);
1656
1657 spin_lock_bh(&pch->downl);
1658 if (pch->chan) {
1659 if (pch->chan->ops->start_xmit(pch->chan, skb))
1660 ppp->xmit_pending = NULL;
1661 } else {
1662 /* channel got unregistered */
1663 kfree_skb(skb);
1664 ppp->xmit_pending = NULL;
1665 }
1666 spin_unlock_bh(&pch->downl);
1667 return;
1668 }
1669
1670#ifdef CONFIG_PPP_MULTILINK
1671 /* Multilink: fragment the packet over as many links
1672 as can take the packet at the moment. */
1673 if (!ppp_mp_explode(ppp, skb))
1674 return;
1675#endif /* CONFIG_PPP_MULTILINK */
1676
1677 ppp->xmit_pending = NULL;
1678 kfree_skb(skb);
1679}
1680
1681#ifdef CONFIG_PPP_MULTILINK
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001682static bool mp_protocol_compress __read_mostly = true;
1683module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
1684MODULE_PARM_DESC(mp_protocol_compress,
1685 "compress protocol id in multilink fragments");
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/*
1688 * Divide a packet to be transmitted into fragments and
1689 * send them out the individual links.
1690 */
1691static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1692{
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001693 int len, totlen;
1694 int i, bits, hdrlen, mtu;
1695 int flen;
1696 int navail, nfree, nzero;
1697 int nbigger;
1698 int totspeed;
1699 int totfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 unsigned char *p, *q;
1701 struct list_head *list;
1702 struct channel *pch;
1703 struct sk_buff *frag;
1704 struct ppp_channel *chan;
1705
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001706 totspeed = 0; /*total bitrate of the bundle*/
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001707 nfree = 0; /* # channels which have no packet already queued */
1708 navail = 0; /* total # of usable channels (not deregistered) */
1709 nzero = 0; /* number of channels with zero speed associated*/
1710 totfree = 0; /*total # of channels available and
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001711 *having no queued packets before
1712 *starting the fragmentation*/
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001715 i = 0;
1716 list_for_each_entry(pch, &ppp->channels, clist) {
Dan Carpenter34297692010-09-10 01:58:10 +00001717 if (pch->chan) {
1718 pch->avail = 1;
1719 navail++;
1720 pch->speed = pch->chan->speed;
1721 } else {
1722 pch->avail = 0;
1723 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001724 if (pch->avail) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001725 if (skb_queue_empty(&pch->file.xq) ||
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001726 !pch->had_frag) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001727 if (pch->speed == 0)
1728 nzero++;
1729 else
1730 totspeed += pch->speed;
1731
1732 pch->avail = 2;
1733 ++nfree;
1734 ++totfree;
1735 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001736 if (!pch->had_frag && i < ppp->nxchan)
1737 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001739 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001741 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001742 * Don't start sending this packet unless at least half of
1743 * the channels are free. This gives much better TCP
1744 * performance if we have a lot of channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001745 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001746 if (nfree == 0 || nfree < navail / 2)
1747 return 0; /* can't take now, leave it in xmit_pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001749 /* Do protocol field compression */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001750 p = skb->data;
1751 len = skb->len;
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001752 if (*p == 0 && mp_protocol_compress) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 ++p;
1754 --len;
1755 }
1756
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001757 totlen = len;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001758 nbigger = len % nfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001760 /* skip to the channel after the one we last used
1761 and start at that one */
Domen Puncer81616c52005-09-10 00:27:04 -07001762 list = &ppp->channels;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001763 for (i = 0; i < ppp->nxchan; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001765 if (list == &ppp->channels) {
1766 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 break;
1768 }
1769 }
1770
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001771 /* create a fragment for each channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 bits = B;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001773 while (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001775 if (list == &ppp->channels) {
1776 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 continue;
1778 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001779 pch = list_entry(list, struct channel, clist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ++i;
1781 if (!pch->avail)
1782 continue;
1783
Paul Mackerras516cd152005-05-12 19:47:12 -04001784 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001785 * Skip this channel if it has a fragment pending already and
1786 * we haven't given a fragment to all of the free channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001787 */
1788 if (pch->avail == 1) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001789 if (nfree > 0)
Paul Mackerras516cd152005-05-12 19:47:12 -04001790 continue;
1791 } else {
Paul Mackerras516cd152005-05-12 19:47:12 -04001792 pch->avail = 1;
1793 }
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* check the channel's mtu and whether it is still attached. */
1796 spin_lock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001797 if (pch->chan == NULL) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001798 /* can't use this channel, it's being deregistered */
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001799 if (pch->speed == 0)
1800 nzero--;
1801 else
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001802 totspeed -= pch->speed;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 spin_unlock_bh(&pch->downl);
1805 pch->avail = 0;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001806 totlen = len;
1807 totfree--;
1808 nfree--;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001809 if (--navail == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
1811 continue;
1812 }
1813
1814 /*
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001815 *if the channel speed is not set divide
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001816 *the packet evenly among the free channels;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001817 *otherwise divide it according to the speed
1818 *of the channel we are going to transmit on
1819 */
David S. Miller886f9fe2009-08-19 13:55:55 -07001820 flen = len;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001821 if (nfree > 0) {
1822 if (pch->speed == 0) {
Ben McKeegan536e00e2010-06-02 23:14:33 +00001823 flen = len/nfree;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001824 if (nbigger > 0) {
1825 flen++;
1826 nbigger--;
1827 }
1828 } else {
1829 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1830 ((totspeed*totfree)/pch->speed)) - hdrlen;
1831 if (nbigger > 0) {
1832 flen += ((totfree - nzero)*pch->speed)/totspeed;
1833 nbigger -= ((totfree - nzero)*pch->speed)/
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001834 totspeed;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001835 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001836 }
Ben McKeegana53a8b52009-07-28 07:43:57 +00001837 nfree--;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001838 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001839
1840 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001841 *check if we are on the last channel or
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001842 *we exceded the length of the data to
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001843 *fragment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 */
Ben McKeegana53a8b52009-07-28 07:43:57 +00001845 if ((nfree <= 0) || (flen > len))
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001846 flen = len;
1847 /*
1848 *it is not worth to tx on slow channels:
1849 *in that case from the resulting flen according to the
1850 *above formula will be equal or less than zero.
1851 *Skip the channel in this case
1852 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001853 if (flen <= 0) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001854 pch->avail = 2;
1855 spin_unlock_bh(&pch->downl);
1856 continue;
1857 }
1858
Henry Wong22e83a22011-09-18 13:41:49 +00001859 /*
1860 * hdrlen includes the 2-byte PPP protocol field, but the
1861 * MTU counts only the payload excluding the protocol field.
1862 * (RFC1661 Section 2)
1863 */
1864 mtu = pch->chan->mtu - (hdrlen - 2);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001865 if (mtu < 4)
1866 mtu = 4;
Paul Mackerras516cd152005-05-12 19:47:12 -04001867 if (flen > mtu)
1868 flen = mtu;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001869 if (flen == len)
1870 bits |= E;
1871 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001872 if (!frag)
Paul Mackerras516cd152005-05-12 19:47:12 -04001873 goto noskb;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001874 q = skb_put(frag, flen + hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001876 /* make the MP header */
Changli Gao96545ae2011-01-06 13:37:36 +00001877 put_unaligned_be16(PPP_MP, q);
Paul Mackerras516cd152005-05-12 19:47:12 -04001878 if (ppp->flags & SC_MP_XSHORTSEQ) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001879 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
Paul Mackerras516cd152005-05-12 19:47:12 -04001880 q[3] = ppp->nxseq;
1881 } else {
1882 q[2] = bits;
1883 q[3] = ppp->nxseq >> 16;
1884 q[4] = ppp->nxseq >> 8;
1885 q[5] = ppp->nxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001887
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001888 memcpy(q + hdrlen, p, flen);
Paul Mackerras516cd152005-05-12 19:47:12 -04001889
1890 /* try to send it down the channel */
1891 chan = pch->chan;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001892 if (!skb_queue_empty(&pch->file.xq) ||
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001893 !chan->ops->start_xmit(chan, frag))
Paul Mackerras516cd152005-05-12 19:47:12 -04001894 skb_queue_tail(&pch->file.xq, frag);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001895 pch->had_frag = 1;
Paul Mackerras516cd152005-05-12 19:47:12 -04001896 p += flen;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001897 len -= flen;
Paul Mackerras516cd152005-05-12 19:47:12 -04001898 ++ppp->nxseq;
1899 bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 spin_unlock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001901 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001902 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 return 1;
1905
1906 noskb:
1907 spin_unlock_bh(&pch->downl);
1908 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001909 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001910 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 ++ppp->nxseq;
1912 return 1; /* abandon the frame */
1913}
1914#endif /* CONFIG_PPP_MULTILINK */
1915
Guillaume Nault55454a52016-08-27 22:22:32 +02001916/* Try to send data out on a channel */
1917static void __ppp_channel_push(struct channel *pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 struct sk_buff *skb;
1920 struct ppp *ppp;
1921
1922 spin_lock_bh(&pch->downl);
Joe Perchescd228d52007-11-12 18:07:31 -08001923 if (pch->chan) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001924 while (!skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 skb = skb_dequeue(&pch->file.xq);
1926 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1927 /* put the packet back and try again later */
1928 skb_queue_head(&pch->file.xq, skb);
1929 break;
1930 }
1931 }
1932 } else {
1933 /* channel got deregistered */
1934 skb_queue_purge(&pch->file.xq);
1935 }
1936 spin_unlock_bh(&pch->downl);
1937 /* see if there is anything from the attached unit to be sent */
David S. Millerb03efcf2005-07-08 14:57:23 -07001938 if (skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 ppp = pch->ppp;
Joe Perchescd228d52007-11-12 18:07:31 -08001940 if (ppp)
Guillaume Nault6ec6ec32017-08-08 11:43:24 +02001941 __ppp_xmit_process(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943}
1944
Guillaume Nault55454a52016-08-27 22:22:32 +02001945static void ppp_channel_push(struct channel *pch)
1946{
Guillaume Nault6ec6ec32017-08-08 11:43:24 +02001947 read_lock_bh(&pch->upl);
1948 if (pch->ppp) {
1949 (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
1950 __ppp_channel_push(pch);
1951 (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
1952 } else {
1953 __ppp_channel_push(pch);
1954 }
1955 read_unlock_bh(&pch->upl);
Guillaume Nault55454a52016-08-27 22:22:32 +02001956}
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/*
1959 * Receive-side routines.
1960 */
1961
David S. Millera00eac02010-10-05 01:36:52 -07001962struct ppp_mp_skb_parm {
1963 u32 sequence;
1964 u8 BEbits;
1965};
1966#define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968static inline void
1969ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1970{
1971 ppp_recv_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001972 if (!ppp->closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 ppp_receive_frame(ppp, skb, pch);
1974 else
1975 kfree_skb(skb);
1976 ppp_recv_unlock(ppp);
1977}
1978
1979void
1980ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1981{
1982 struct channel *pch = chan->ppp;
1983 int proto;
1984
Simon Arlottea8420e2010-05-03 10:19:33 +00001985 if (!pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 kfree_skb(skb);
1987 return;
1988 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 read_lock_bh(&pch->upl);
Simon Arlottea8420e2010-05-03 10:19:33 +00001991 if (!pskb_may_pull(skb, 2)) {
1992 kfree_skb(skb);
1993 if (pch->ppp) {
1994 ++pch->ppp->dev->stats.rx_length_errors;
1995 ppp_receive_error(pch->ppp);
1996 }
1997 goto done;
1998 }
1999
2000 proto = PPP_PROTO(skb);
Joe Perchescd228d52007-11-12 18:07:31 -08002001 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* put it on the channel queue */
2003 skb_queue_tail(&pch->file.rq, skb);
2004 /* drop old frames if queue too long */
Joe Perches8e95a202009-12-03 07:58:21 +00002005 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
2006 (skb = skb_dequeue(&pch->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 kfree_skb(skb);
2008 wake_up_interruptible(&pch->file.rwait);
2009 } else {
2010 ppp_do_recv(pch->ppp, skb, pch);
2011 }
Simon Arlottea8420e2010-05-03 10:19:33 +00002012
2013done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 read_unlock_bh(&pch->upl);
2015}
2016
2017/* Put a 0-length skb in the receive queue as an error indication */
2018void
2019ppp_input_error(struct ppp_channel *chan, int code)
2020{
2021 struct channel *pch = chan->ppp;
2022 struct sk_buff *skb;
2023
Joe Perchescd228d52007-11-12 18:07:31 -08002024 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return;
2026
2027 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002028 if (pch->ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 skb = alloc_skb(0, GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002030 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 skb->len = 0; /* probably unnecessary */
2032 skb->cb[0] = code;
2033 ppp_do_recv(pch->ppp, skb, pch);
2034 }
2035 }
2036 read_unlock_bh(&pch->upl);
2037}
2038
2039/*
2040 * We come in here to process a received frame.
2041 * The receive side of the ppp unit is locked.
2042 */
2043static void
2044ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2045{
Simon Arlottea8420e2010-05-03 10:19:33 +00002046 /* note: a 0-length skb is used as an error indication */
2047 if (skb->len > 0) {
Tom Herbert3dfb0532015-04-20 14:10:05 -07002048 skb_checksum_complete_unset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049#ifdef CONFIG_PPP_MULTILINK
2050 /* XXX do channel-level decompression here */
2051 if (PPP_PROTO(skb) == PPP_MP)
2052 ppp_receive_mp_frame(ppp, skb, pch);
2053 else
2054#endif /* CONFIG_PPP_MULTILINK */
2055 ppp_receive_nonmp_frame(ppp, skb);
Simon Arlottea8420e2010-05-03 10:19:33 +00002056 } else {
2057 kfree_skb(skb);
2058 ppp_receive_error(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
2062static void
2063ppp_receive_error(struct ppp *ppp)
2064{
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002065 ++ppp->dev->stats.rx_errors;
Joe Perchescd228d52007-11-12 18:07:31 -08002066 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 slhc_toss(ppp->vj);
2068}
2069
2070static void
2071ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2072{
2073 struct sk_buff *ns;
2074 int proto, len, npi;
2075
2076 /*
2077 * Decompress the frame, if compressed.
2078 * Note that some decompressors need to see uncompressed frames
2079 * that come in as well as compressed frames.
2080 */
Joe Perches8e95a202009-12-03 07:58:21 +00002081 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2082 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 skb = ppp_decompress_frame(ppp, skb);
2084
Matt Domschb3f9b922005-11-08 09:40:47 -08002085 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2086 goto err;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 proto = PPP_PROTO(skb);
2089 switch (proto) {
2090 case PPP_VJC_COMP:
2091 /* decompress VJ compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002092 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 goto err;
2094
Herbert Xu2a38b772007-09-16 16:22:13 -07002095 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* copy to a new sk_buff with more tailroom */
2097 ns = dev_alloc_skb(skb->len + 128);
Joe Perchescd228d52007-11-12 18:07:31 -08002098 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002099 netdev_err(ppp->dev, "PPP: no memory "
2100 "(VJ decomp)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 goto err;
2102 }
2103 skb_reserve(ns, 2);
2104 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
Eric Dumazet968d7012012-05-18 20:23:00 +00002105 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 skb = ns;
2107 }
Herbert Xue3f749c2006-02-05 20:23:33 -08002108 else
2109 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2112 if (len <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002113 netdev_printk(KERN_DEBUG, ppp->dev,
2114 "PPP: VJ decompression error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 goto err;
2116 }
2117 len += 2;
2118 if (len > skb->len)
2119 skb_put(skb, len - skb->len);
2120 else if (len < skb->len)
2121 skb_trim(skb, len);
2122 proto = PPP_IP;
2123 break;
2124
2125 case PPP_VJC_UNCOMP:
Joe Perchescd228d52007-11-12 18:07:31 -08002126 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* Until we fix the decompressor need to make sure
2130 * data portion is linear.
2131 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002132 if (!pskb_may_pull(skb, skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 goto err;
2134
2135 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002136 netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 goto err;
2138 }
2139 proto = PPP_IP;
2140 break;
2141
2142 case PPP_CCP:
2143 ppp_ccp_peek(ppp, skb, 1);
2144 break;
2145 }
2146
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002147 ++ppp->stats64.rx_packets;
2148 ppp->stats64.rx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 npi = proto_to_npindex(proto);
2151 if (npi < 0) {
2152 /* control or unknown frame - pass it to pppd */
2153 skb_queue_tail(&ppp->file.rq, skb);
2154 /* limit queue length by dropping old frames */
Joe Perches8e95a202009-12-03 07:58:21 +00002155 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2156 (skb = skb_dequeue(&ppp->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 kfree_skb(skb);
2158 /* wake up any process polling or blocking on read */
2159 wake_up_interruptible(&ppp->file.rwait);
2160
2161 } else {
2162 /* network protocol frame - give it to the kernel */
2163
2164#ifdef CONFIG_PPP_FILTER
2165 /* check if the packet passes the pass and active filters */
2166 /* the filter instructions are constructed assuming
2167 a four-byte PPP header on each packet */
Herbert Xu2a38b772007-09-16 16:22:13 -07002168 if (ppp->pass_filter || ppp->active_filter) {
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +00002169 if (skb_unclone(skb, GFP_ATOMIC))
Herbert Xu2a38b772007-09-16 16:22:13 -07002170 goto err;
2171
2172 *skb_push(skb, 2) = 0;
Joe Perches8e95a202009-12-03 07:58:21 +00002173 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002174 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Herbert Xu2a38b772007-09-16 16:22:13 -07002175 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002176 netdev_printk(KERN_DEBUG, ppp->dev,
2177 "PPP: inbound frame "
2178 "not passed\n");
Herbert Xu2a38b772007-09-16 16:22:13 -07002179 kfree_skb(skb);
2180 return;
2181 }
Joe Perches8e95a202009-12-03 07:58:21 +00002182 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002183 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Herbert Xu2a38b772007-09-16 16:22:13 -07002184 ppp->last_recv = jiffies;
2185 __skb_pull(skb, 2);
2186 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187#endif /* CONFIG_PPP_FILTER */
Herbert Xu2a38b772007-09-16 16:22:13 -07002188 ppp->last_recv = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Joe Perches8e95a202009-12-03 07:58:21 +00002190 if ((ppp->dev->flags & IFF_UP) == 0 ||
2191 ppp->npmode[npi] != NPMODE_PASS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 kfree_skb(skb);
2193 } else {
Herbert Xucbb042f2006-03-20 22:43:56 -08002194 /* chop off protocol */
2195 skb_pull_rcsum(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 skb->dev = ppp->dev;
2197 skb->protocol = htons(npindex_to_ethertype[npi]);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002198 skb_reset_mac_header(skb);
Guillaume Nault79c441a2015-08-24 11:35:30 +02002199 skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2200 dev_net(ppp->dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 }
2204 return;
2205
2206 err:
2207 kfree_skb(skb);
2208 ppp_receive_error(ppp);
2209}
2210
2211static struct sk_buff *
2212ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2213{
2214 int proto = PPP_PROTO(skb);
2215 struct sk_buff *ns;
2216 int len;
2217
2218 /* Until we fix all the decompressor's need to make sure
2219 * data portion is linear.
2220 */
2221 if (!pskb_may_pull(skb, skb->len))
2222 goto err;
2223
2224 if (proto == PPP_COMP) {
Konstantin Sharlaimov4b2a8fb2007-06-23 23:05:54 -07002225 int obuff_size;
2226
2227 switch(ppp->rcomp->compress_proto) {
2228 case CI_MPPE:
2229 obuff_size = ppp->mru + PPP_HDRLEN + 1;
2230 break;
2231 default:
2232 obuff_size = ppp->mru + PPP_HDRLEN;
2233 break;
2234 }
2235
2236 ns = dev_alloc_skb(obuff_size);
Joe Perchescd228d52007-11-12 18:07:31 -08002237 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002238 netdev_err(ppp->dev, "ppp_decompress_frame: "
2239 "no memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 goto err;
2241 }
2242 /* the decompressor still expects the A/C bytes in the hdr */
2243 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
Konstantin Sharlaimov06c7af52007-08-21 00:12:44 -07002244 skb->len + 2, ns->data, obuff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (len < 0) {
2246 /* Pass the compressed frame to pppd as an
2247 error indication. */
2248 if (len == DECOMP_FATALERROR)
2249 ppp->rstate |= SC_DC_FERROR;
2250 kfree_skb(ns);
2251 goto err;
2252 }
2253
Eric Dumazet968d7012012-05-18 20:23:00 +00002254 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 skb = ns;
2256 skb_put(skb, len);
2257 skb_pull(skb, 2); /* pull off the A/C bytes */
2258
2259 } else {
2260 /* Uncompressed frame - pass to decompressor so it
2261 can update its dictionary if necessary. */
2262 if (ppp->rcomp->incomp)
2263 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2264 skb->len + 2);
2265 }
2266
2267 return skb;
2268
2269 err:
2270 ppp->rstate |= SC_DC_ERROR;
2271 ppp_receive_error(ppp);
2272 return skb;
2273}
2274
2275#ifdef CONFIG_PPP_MULTILINK
2276/*
2277 * Receive a multilink frame.
2278 * We put it on the reconstruction queue and then pull off
2279 * as many completed frames as we can.
2280 */
2281static void
2282ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2283{
2284 u32 mask, seq;
Domen Puncer81616c52005-09-10 00:27:04 -07002285 struct channel *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2287
Herbert Xu2a38b772007-09-16 16:22:13 -07002288 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 goto err; /* no good, throw it away */
2290
2291 /* Decode sequence number and begin/end bits */
2292 if (ppp->flags & SC_MP_SHORTSEQ) {
2293 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2294 mask = 0xfff;
2295 } else {
2296 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2297 mask = 0xffffff;
2298 }
David S. Millera00eac02010-10-05 01:36:52 -07002299 PPP_MP_CB(skb)->BEbits = skb->data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
2301
2302 /*
2303 * Do protocol ID decompression on the first fragment of each packet.
2304 */
David S. Millera00eac02010-10-05 01:36:52 -07002305 if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 *skb_push(skb, 1) = 0;
2307
2308 /*
2309 * Expand sequence number to 32 bits, making it as close
2310 * as possible to ppp->minseq.
2311 */
2312 seq |= ppp->minseq & ~mask;
2313 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2314 seq += mask + 1;
2315 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2316 seq -= mask + 1; /* should never happen */
David S. Millera00eac02010-10-05 01:36:52 -07002317 PPP_MP_CB(skb)->sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 pch->lastseq = seq;
2319
2320 /*
2321 * If this packet comes before the next one we were expecting,
2322 * drop it.
2323 */
2324 if (seq_before(seq, ppp->nextseq)) {
2325 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002326 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 ppp_receive_error(ppp);
2328 return;
2329 }
2330
2331 /*
2332 * Reevaluate minseq, the minimum over all channels of the
2333 * last sequence number received on each channel. Because of
2334 * the increasing sequence number rule, we know that any fragment
2335 * before `minseq' which hasn't arrived is never going to arrive.
2336 * The list of channels can't change because we have the receive
2337 * side of the ppp unit locked.
2338 */
Domen Puncer81616c52005-09-10 00:27:04 -07002339 list_for_each_entry(ch, &ppp->channels, clist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (seq_before(ch->lastseq, seq))
2341 seq = ch->lastseq;
2342 }
2343 if (seq_before(ppp->minseq, seq))
2344 ppp->minseq = seq;
2345
2346 /* Put the fragment on the reconstruction queue */
2347 ppp_mp_insert(ppp, skb);
2348
2349 /* If the queue is getting long, don't wait any longer for packets
2350 before the start of the queue. */
David S. Miller38ce7c72008-09-23 01:17:18 -07002351 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
stephen hemmingerc6b20d92010-06-01 06:05:46 +00002352 struct sk_buff *mskb = skb_peek(&ppp->mrq);
David S. Millera00eac02010-10-05 01:36:52 -07002353 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2354 ppp->minseq = PPP_MP_CB(mskb)->sequence;
David S. Miller38ce7c72008-09-23 01:17:18 -07002355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 /* Pull completed packets off the queue and receive them. */
Ben McKeegan82b3cc12009-11-16 03:44:25 +00002358 while ((skb = ppp_mp_reconstruct(ppp))) {
2359 if (pskb_may_pull(skb, 2))
2360 ppp_receive_nonmp_frame(ppp, skb);
2361 else {
2362 ++ppp->dev->stats.rx_length_errors;
2363 kfree_skb(skb);
2364 ppp_receive_error(ppp);
2365 }
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 return;
2369
2370 err:
2371 kfree_skb(skb);
2372 ppp_receive_error(ppp);
2373}
2374
2375/*
2376 * Insert a fragment on the MP reconstruction queue.
2377 * The queue is ordered by increasing sequence number.
2378 */
2379static void
2380ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2381{
2382 struct sk_buff *p;
2383 struct sk_buff_head *list = &ppp->mrq;
David S. Millera00eac02010-10-05 01:36:52 -07002384 u32 seq = PPP_MP_CB(skb)->sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 /* N.B. we don't need to lock the list lock because we have the
2387 ppp unit receive-side lock. */
David S. Miller13c98212008-10-09 16:40:29 -07002388 skb_queue_walk(list, p) {
David S. Millera00eac02010-10-05 01:36:52 -07002389 if (seq_before(seq, PPP_MP_CB(p)->sequence))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 break;
David S. Miller13c98212008-10-09 16:40:29 -07002391 }
David S. Miller43f59c82008-09-21 21:28:51 -07002392 __skb_queue_before(list, p, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
2395/*
2396 * Reconstruct a packet from the MP fragment queue.
2397 * We go through increasing sequence numbers until we find a
2398 * complete packet, or we get to the sequence number for a fragment
2399 * which hasn't arrived but might still do so.
2400 */
Stephen Hemminger3c582b32008-01-23 20:54:07 -08002401static struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402ppp_mp_reconstruct(struct ppp *ppp)
2403{
2404 u32 seq = ppp->nextseq;
2405 u32 minseq = ppp->minseq;
2406 struct sk_buff_head *list = &ppp->mrq;
David S. Millerd52344a2011-01-20 22:52:05 -08002407 struct sk_buff *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 struct sk_buff *head, *tail;
2409 struct sk_buff *skb = NULL;
2410 int lost = 0, len = 0;
2411
2412 if (ppp->mrru == 0) /* do nothing until mrru is set */
2413 return NULL;
2414 head = list->next;
2415 tail = NULL;
David S. Millerd52344a2011-01-20 22:52:05 -08002416 skb_queue_walk_safe(list, p, tmp) {
2417 again:
David S. Millera00eac02010-10-05 01:36:52 -07002418 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 /* this can't happen, anyway ignore the skb */
David S. Millerb48f8c22011-01-20 22:44:36 -08002420 netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2421 "seq %u < %u\n",
2422 PPP_MP_CB(p)->sequence, seq);
David S. Millerd52344a2011-01-20 22:52:05 -08002423 __skb_unlink(p, list);
2424 kfree_skb(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 continue;
2426 }
David S. Millera00eac02010-10-05 01:36:52 -07002427 if (PPP_MP_CB(p)->sequence != seq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002428 u32 oldseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /* Fragment `seq' is missing. If it is after
2430 minseq, it might arrive later, so stop here. */
2431 if (seq_after(seq, minseq))
2432 break;
2433 /* Fragment `seq' is lost, keep going. */
2434 lost = 1;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002435 oldseq = seq;
David S. Millera00eac02010-10-05 01:36:52 -07002436 seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2437 minseq + 1: PPP_MP_CB(p)->sequence;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002438
2439 if (ppp->debug & 1)
2440 netdev_printk(KERN_DEBUG, ppp->dev,
2441 "lost frag %u..%u\n",
2442 oldseq, seq-1);
2443
David S. Millerd52344a2011-01-20 22:52:05 -08002444 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
2447 /*
2448 * At this point we know that all the fragments from
2449 * ppp->nextseq to seq are either present or lost.
2450 * Also, there are no complete packets in the queue
2451 * that have no missing fragments and end before this
2452 * fragment.
2453 */
2454
2455 /* B bit set indicates this fragment starts a packet */
David S. Millera00eac02010-10-05 01:36:52 -07002456 if (PPP_MP_CB(p)->BEbits & B) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 head = p;
2458 lost = 0;
2459 len = 0;
2460 }
2461
2462 len += p->len;
2463
2464 /* Got a complete packet yet? */
David S. Millera00eac02010-10-05 01:36:52 -07002465 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2466 (PPP_MP_CB(head)->BEbits & B)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (len > ppp->mrru + 2) {
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002468 ++ppp->dev->stats.rx_length_errors;
David S. Millerb48f8c22011-01-20 22:44:36 -08002469 netdev_printk(KERN_DEBUG, ppp->dev,
2470 "PPP: reconstructed packet"
2471 " is too long (%d)\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 } else {
2473 tail = p;
2474 break;
2475 }
2476 ppp->nextseq = seq + 1;
2477 }
2478
2479 /*
2480 * If this is the ending fragment of a packet,
2481 * and we haven't found a complete valid packet yet,
2482 * we can discard up to and including this fragment.
2483 */
David S. Millerd52344a2011-01-20 22:52:05 -08002484 if (PPP_MP_CB(p)->BEbits & E) {
2485 struct sk_buff *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
David S. Millerd52344a2011-01-20 22:52:05 -08002487 skb_queue_reverse_walk_from_safe(list, p, tmp2) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002488 if (ppp->debug & 1)
2489 netdev_printk(KERN_DEBUG, ppp->dev,
2490 "discarding frag %u\n",
2491 PPP_MP_CB(p)->sequence);
David S. Millerd52344a2011-01-20 22:52:05 -08002492 __skb_unlink(p, list);
2493 kfree_skb(p);
2494 }
2495 head = skb_peek(list);
2496 if (!head)
2497 break;
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 ++seq;
2500 }
2501
2502 /* If we have a complete packet, copy it all into one skb. */
2503 if (tail != NULL) {
2504 /* If we have discarded any fragments,
2505 signal a receive error. */
David S. Millera00eac02010-10-05 01:36:52 -07002506 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002507 skb_queue_walk_safe(list, p, tmp) {
2508 if (p == head)
2509 break;
2510 if (ppp->debug & 1)
2511 netdev_printk(KERN_DEBUG, ppp->dev,
2512 "discarding frag %u\n",
2513 PPP_MP_CB(p)->sequence);
2514 __skb_unlink(p, list);
2515 kfree_skb(p);
2516 }
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002519 netdev_printk(KERN_DEBUG, ppp->dev,
2520 " missed pkts %u..%u\n",
2521 ppp->nextseq,
2522 PPP_MP_CB(head)->sequence-1);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002523 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 ppp_receive_error(ppp);
2525 }
2526
David S. Miller212bfb92011-01-20 22:46:07 -08002527 skb = head;
2528 if (head != tail) {
2529 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2530 p = skb_queue_next(list, head);
2531 __skb_unlink(skb, list);
2532 skb_queue_walk_from_safe(list, p, tmp) {
2533 __skb_unlink(p, list);
2534 *fragpp = p;
2535 p->next = NULL;
2536 fragpp = &p->next;
2537
2538 skb->len += p->len;
2539 skb->data_len += p->len;
Eric Dumazet19c6c8f2012-02-13 04:23:24 +00002540 skb->truesize += p->truesize;
David S. Miller212bfb92011-01-20 22:46:07 -08002541
2542 if (p == tail)
2543 break;
2544 }
2545 } else {
2546 __skb_unlink(skb, list);
2547 }
2548
David S. Millera00eac02010-10-05 01:36:52 -07002549 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551
2552 return skb;
2553}
2554#endif /* CONFIG_PPP_MULTILINK */
2555
2556/*
2557 * Channel interface.
2558 */
2559
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002560/* Create a new, unattached ppp channel. */
2561int ppp_register_channel(struct ppp_channel *chan)
2562{
2563 return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2564}
2565
2566/* Create a new, unattached ppp channel for specified net. */
2567int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 struct channel *pch;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002570 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Panagiotis Issarisd4274b52006-08-15 16:01:07 -07002572 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -08002573 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return -ENOMEM;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002575
2576 pn = ppp_pernet(net);
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 pch->ppp = NULL;
2579 pch->chan = chan;
Guillaume Nault1f461dc2016-03-23 16:38:55 +01002580 pch->chan_net = get_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 chan->ppp = pch;
2582 init_ppp_file(&pch->file, CHANNEL);
2583 pch->file.hdrlen = chan->hdrlen;
2584#ifdef CONFIG_PPP_MULTILINK
2585 pch->lastseq = -1;
2586#endif /* CONFIG_PPP_MULTILINK */
2587 init_rwsem(&pch->chan_sem);
2588 spin_lock_init(&pch->downl);
2589 rwlock_init(&pch->upl);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002590
2591 spin_lock_bh(&pn->all_channels_lock);
2592 pch->file.index = ++pn->last_channel_index;
2593 list_add(&pch->list, &pn->new_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 atomic_inc(&channel_count);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002595 spin_unlock_bh(&pn->all_channels_lock);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598}
2599
2600/*
2601 * Return the index of a channel.
2602 */
2603int ppp_channel_index(struct ppp_channel *chan)
2604{
2605 struct channel *pch = chan->ppp;
2606
Joe Perchescd228d52007-11-12 18:07:31 -08002607 if (pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return pch->file.index;
2609 return -1;
2610}
2611
2612/*
2613 * Return the PPP unit number to which a channel is connected.
2614 */
2615int ppp_unit_number(struct ppp_channel *chan)
2616{
2617 struct channel *pch = chan->ppp;
2618 int unit = -1;
2619
Joe Perchescd228d52007-11-12 18:07:31 -08002620 if (pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002622 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 unit = pch->ppp->file.index;
2624 read_unlock_bh(&pch->upl);
2625 }
2626 return unit;
2627}
2628
2629/*
James Chapman63f96072010-04-02 06:18:39 +00002630 * Return the PPP device interface name of a channel.
2631 */
2632char *ppp_dev_name(struct ppp_channel *chan)
2633{
2634 struct channel *pch = chan->ppp;
2635 char *name = NULL;
2636
2637 if (pch) {
2638 read_lock_bh(&pch->upl);
2639 if (pch->ppp && pch->ppp->dev)
2640 name = pch->ppp->dev->name;
2641 read_unlock_bh(&pch->upl);
2642 }
2643 return name;
2644}
2645
2646
2647/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 * Disconnect a channel from the generic layer.
2649 * This must be called in process context.
2650 */
2651void
2652ppp_unregister_channel(struct ppp_channel *chan)
2653{
2654 struct channel *pch = chan->ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002655 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Joe Perchescd228d52007-11-12 18:07:31 -08002657 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return; /* should never happen */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 chan->ppp = NULL;
2661
2662 /*
2663 * This ensures that we have returned from any calls into the
2664 * the channel's start_xmit or ioctl routine before we proceed.
2665 */
2666 down_write(&pch->chan_sem);
2667 spin_lock_bh(&pch->downl);
2668 pch->chan = NULL;
2669 spin_unlock_bh(&pch->downl);
2670 up_write(&pch->chan_sem);
2671 ppp_disconnect_channel(pch);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002672
2673 pn = ppp_pernet(pch->chan_net);
2674 spin_lock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 list_del(&pch->list);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002676 spin_unlock_bh(&pn->all_channels_lock);
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 pch->file.dead = 1;
2679 wake_up_interruptible(&pch->file.rwait);
2680 if (atomic_dec_and_test(&pch->file.refcnt))
2681 ppp_destroy_channel(pch);
2682}
2683
2684/*
2685 * Callback from a channel when it can accept more to transmit.
2686 * This should be called at BH/softirq level, not interrupt level.
2687 */
2688void
2689ppp_output_wakeup(struct ppp_channel *chan)
2690{
2691 struct channel *pch = chan->ppp;
2692
Joe Perchescd228d52007-11-12 18:07:31 -08002693 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return;
2695 ppp_channel_push(pch);
2696}
2697
2698/*
2699 * Compression control.
2700 */
2701
2702/* Process the PPPIOCSCOMPRESS ioctl. */
2703static int
2704ppp_set_compress(struct ppp *ppp, unsigned long arg)
2705{
2706 int err;
2707 struct compressor *cp, *ocomp;
2708 struct ppp_option_data data;
2709 void *state, *ostate;
2710 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2711
2712 err = -EFAULT;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002713 if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto out;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002715 if (data.length > CCP_MAX_OPTION_LENGTH)
2716 goto out;
2717 if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
2718 goto out;
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 err = -EINVAL;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002721 if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 goto out;
2723
Johannes Berga65e5d72008-07-09 10:28:38 +02002724 cp = try_then_request_module(
2725 find_compressor(ccp_option[0]),
2726 "ppp-compress-%d", ccp_option[0]);
Joe Perchescd228d52007-11-12 18:07:31 -08002727 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 goto out;
2729
2730 err = -ENOBUFS;
2731 if (data.transmit) {
2732 state = cp->comp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002733 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 ppp_xmit_lock(ppp);
2735 ppp->xstate &= ~SC_COMP_RUN;
2736 ocomp = ppp->xcomp;
2737 ostate = ppp->xc_state;
2738 ppp->xcomp = cp;
2739 ppp->xc_state = state;
2740 ppp_xmit_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002741 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 ocomp->comp_free(ostate);
2743 module_put(ocomp->owner);
2744 }
2745 err = 0;
2746 } else
2747 module_put(cp->owner);
2748
2749 } else {
2750 state = cp->decomp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002751 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 ppp_recv_lock(ppp);
2753 ppp->rstate &= ~SC_DECOMP_RUN;
2754 ocomp = ppp->rcomp;
2755 ostate = ppp->rc_state;
2756 ppp->rcomp = cp;
2757 ppp->rc_state = state;
2758 ppp_recv_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002759 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 ocomp->decomp_free(ostate);
2761 module_put(ocomp->owner);
2762 }
2763 err = 0;
2764 } else
2765 module_put(cp->owner);
2766 }
2767
2768 out:
2769 return err;
2770}
2771
2772/*
2773 * Look at a CCP packet and update our state accordingly.
2774 * We assume the caller has the xmit or recv path locked.
2775 */
2776static void
2777ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2778{
2779 unsigned char *dp;
2780 int len;
2781
2782 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2783 return; /* no header */
2784 dp = skb->data + 2;
2785
2786 switch (CCP_CODE(dp)) {
2787 case CCP_CONFREQ:
2788
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002789 /* A ConfReq starts negotiation of compression
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 * in one direction of transmission,
2791 * and hence brings it down...but which way?
2792 *
2793 * Remember:
2794 * A ConfReq indicates what the sender would like to receive
2795 */
2796 if(inbound)
2797 /* He is proposing what I should send */
2798 ppp->xstate &= ~SC_COMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002799 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /* I am proposing to what he should send */
2801 ppp->rstate &= ~SC_DECOMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 case CCP_TERMREQ:
2806 case CCP_TERMACK:
2807 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002808 * CCP is going down, both directions of transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 */
2810 ppp->rstate &= ~SC_DECOMP_RUN;
2811 ppp->xstate &= ~SC_COMP_RUN;
2812 break;
2813
2814 case CCP_CONFACK:
2815 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2816 break;
2817 len = CCP_LENGTH(dp);
2818 if (!pskb_may_pull(skb, len + 2))
2819 return; /* too short */
2820 dp += CCP_HDRLEN;
2821 len -= CCP_HDRLEN;
2822 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2823 break;
2824 if (inbound) {
2825 /* we will start receiving compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002826 if (!ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 break;
2828 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2829 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2830 ppp->rstate |= SC_DECOMP_RUN;
2831 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2832 }
2833 } else {
2834 /* we will soon start sending compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002835 if (!ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 break;
2837 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2838 ppp->file.index, 0, ppp->debug))
2839 ppp->xstate |= SC_COMP_RUN;
2840 }
2841 break;
2842
2843 case CCP_RESETACK:
2844 /* reset the [de]compressor */
2845 if ((ppp->flags & SC_CCP_UP) == 0)
2846 break;
2847 if (inbound) {
2848 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2849 ppp->rcomp->decomp_reset(ppp->rc_state);
2850 ppp->rstate &= ~SC_DC_ERROR;
2851 }
2852 } else {
2853 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2854 ppp->xcomp->comp_reset(ppp->xc_state);
2855 }
2856 break;
2857 }
2858}
2859
2860/* Free up compression resources. */
2861static void
2862ppp_ccp_closed(struct ppp *ppp)
2863{
2864 void *xstate, *rstate;
2865 struct compressor *xcomp, *rcomp;
2866
2867 ppp_lock(ppp);
2868 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2869 ppp->xstate = 0;
2870 xcomp = ppp->xcomp;
2871 xstate = ppp->xc_state;
2872 ppp->xc_state = NULL;
2873 ppp->rstate = 0;
2874 rcomp = ppp->rcomp;
2875 rstate = ppp->rc_state;
2876 ppp->rc_state = NULL;
2877 ppp_unlock(ppp);
2878
2879 if (xstate) {
2880 xcomp->comp_free(xstate);
2881 module_put(xcomp->owner);
2882 }
2883 if (rstate) {
2884 rcomp->decomp_free(rstate);
2885 module_put(rcomp->owner);
2886 }
2887}
2888
2889/* List of compressors. */
2890static LIST_HEAD(compressor_list);
2891static DEFINE_SPINLOCK(compressor_list_lock);
2892
2893struct compressor_entry {
2894 struct list_head list;
2895 struct compressor *comp;
2896};
2897
2898static struct compressor_entry *
2899find_comp_entry(int proto)
2900{
2901 struct compressor_entry *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Domen Puncer81616c52005-09-10 00:27:04 -07002903 list_for_each_entry(ce, &compressor_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (ce->comp->compress_proto == proto)
2905 return ce;
2906 }
2907 return NULL;
2908}
2909
2910/* Register a compressor */
2911int
2912ppp_register_compressor(struct compressor *cp)
2913{
2914 struct compressor_entry *ce;
2915 int ret;
2916 spin_lock(&compressor_list_lock);
2917 ret = -EEXIST;
Joe Perchescd228d52007-11-12 18:07:31 -08002918 if (find_comp_entry(cp->compress_proto))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 goto out;
2920 ret = -ENOMEM;
2921 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002922 if (!ce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 goto out;
2924 ret = 0;
2925 ce->comp = cp;
2926 list_add(&ce->list, &compressor_list);
2927 out:
2928 spin_unlock(&compressor_list_lock);
2929 return ret;
2930}
2931
2932/* Unregister a compressor */
2933void
2934ppp_unregister_compressor(struct compressor *cp)
2935{
2936 struct compressor_entry *ce;
2937
2938 spin_lock(&compressor_list_lock);
2939 ce = find_comp_entry(cp->compress_proto);
Joe Perchescd228d52007-11-12 18:07:31 -08002940 if (ce && ce->comp == cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 list_del(&ce->list);
2942 kfree(ce);
2943 }
2944 spin_unlock(&compressor_list_lock);
2945}
2946
2947/* Find a compressor. */
2948static struct compressor *
2949find_compressor(int type)
2950{
2951 struct compressor_entry *ce;
2952 struct compressor *cp = NULL;
2953
2954 spin_lock(&compressor_list_lock);
2955 ce = find_comp_entry(type);
Joe Perchescd228d52007-11-12 18:07:31 -08002956 if (ce) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 cp = ce->comp;
2958 if (!try_module_get(cp->owner))
2959 cp = NULL;
2960 }
2961 spin_unlock(&compressor_list_lock);
2962 return cp;
2963}
2964
2965/*
2966 * Miscelleneous stuff.
2967 */
2968
2969static void
2970ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2971{
2972 struct slcompress *vj = ppp->vj;
2973
2974 memset(st, 0, sizeof(*st));
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002975 st->p.ppp_ipackets = ppp->stats64.rx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002976 st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002977 st->p.ppp_ibytes = ppp->stats64.rx_bytes;
2978 st->p.ppp_opackets = ppp->stats64.tx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002979 st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002980 st->p.ppp_obytes = ppp->stats64.tx_bytes;
Joe Perchescd228d52007-11-12 18:07:31 -08002981 if (!vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return;
2983 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2984 st->vj.vjs_compressed = vj->sls_o_compressed;
2985 st->vj.vjs_searches = vj->sls_o_searches;
2986 st->vj.vjs_misses = vj->sls_o_misses;
2987 st->vj.vjs_errorin = vj->sls_i_error;
2988 st->vj.vjs_tossed = vj->sls_i_tossed;
2989 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2990 st->vj.vjs_compressedin = vj->sls_i_compressed;
2991}
2992
2993/*
2994 * Stuff for handling the lists of ppp units and channels
2995 * and for initialization.
2996 */
2997
2998/*
2999 * Create a new ppp interface unit. Fails if it can't allocate memory
3000 * or if there is already a unit with the requested number.
3001 * unit == -1 means allocate a new number.
3002 */
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003003static int ppp_create_interface(struct net *net, struct file *file, int *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003005 struct ppp_config conf = {
3006 .file = file,
3007 .unit = *unit,
Guillaume Nault96d934c2016-04-28 17:55:30 +02003008 .ifname_is_set = false,
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003009 };
3010 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 struct ppp *ppp;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Guillaume Nault69d97282015-12-11 19:54:52 +01003014 dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003015 if (!dev) {
3016 err = -ENOMEM;
3017 goto err;
3018 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003019 dev_net_set(dev, net);
Guillaume Nault96d934c2016-04-28 17:55:30 +02003020 dev->rtnl_link_ops = &ppp_link_ops;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003021
Guillaume Nault58a89ec2015-09-24 12:54:01 +02003022 rtnl_lock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003023
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003024 err = ppp_dev_configure(net, dev, &conf);
3025 if (err < 0)
3026 goto err_dev;
3027 ppp = netdev_priv(dev);
3028 *unit = ppp->file.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Guillaume Nault58a89ec2015-09-24 12:54:01 +02003030 rtnl_unlock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003031
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003034err_dev:
Guillaume Nault6faac632016-03-07 19:36:44 +01003035 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 free_netdev(dev);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003037err:
3038 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
3041/*
3042 * Initialize a ppp_file structure.
3043 */
3044static void
3045init_ppp_file(struct ppp_file *pf, int kind)
3046{
3047 pf->kind = kind;
3048 skb_queue_head_init(&pf->xq);
3049 skb_queue_head_init(&pf->rq);
3050 atomic_set(&pf->refcnt, 1);
3051 init_waitqueue_head(&pf->rwait);
3052}
3053
3054/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 * Free the memory used by a ppp unit. This is only called once
3056 * there are no channels connected to the unit and no file structs
3057 * that reference the unit.
3058 */
3059static void ppp_destroy_interface(struct ppp *ppp)
3060{
3061 atomic_dec(&ppp_unit_count);
3062
3063 if (!ppp->file.dead || ppp->n_channels) {
3064 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003065 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3066 "but dead=%d n_channels=%d !\n",
3067 ppp, ppp->file.dead, ppp->n_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return;
3069 }
3070
3071 ppp_ccp_closed(ppp);
3072 if (ppp->vj) {
3073 slhc_free(ppp->vj);
3074 ppp->vj = NULL;
3075 }
3076 skb_queue_purge(&ppp->file.xq);
3077 skb_queue_purge(&ppp->file.rq);
3078#ifdef CONFIG_PPP_MULTILINK
3079 skb_queue_purge(&ppp->mrq);
3080#endif /* CONFIG_PPP_MULTILINK */
3081#ifdef CONFIG_PPP_FILTER
Daniel Borkmann568f1942014-03-28 18:58:23 +01003082 if (ppp->pass_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003083 bpf_prog_destroy(ppp->pass_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003084 ppp->pass_filter = NULL;
3085 }
3086
3087 if (ppp->active_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003088 bpf_prog_destroy(ppp->active_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003089 ppp->active_filter = NULL;
3090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091#endif /* CONFIG_PPP_FILTER */
3092
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00003093 kfree_skb(ppp->xmit_pending);
Gao Feng3b25bfc2017-07-17 18:34:42 +08003094 free_percpu(ppp->xmit_recursion);
G. Liakhovetski165de5b2007-03-25 19:04:09 -07003095
James Chapman739840d2008-12-17 12:02:16 +00003096 free_netdev(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
3099/*
3100 * Locate an existing ppp unit.
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08003101 * The caller should have locked the all_ppp_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 */
3103static struct ppp *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003104ppp_find_unit(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003106 return unit_find(&pn->units_idr, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
3109/*
3110 * Locate an existing ppp channel.
3111 * The caller should have locked the all_channels_lock.
3112 * First we look in the new_channels list, then in the
3113 * all_channels list. If found in the new_channels list,
3114 * we move it to the all_channels list. This is for speed
3115 * when we have a lot of channels in use.
3116 */
3117static struct channel *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003118ppp_find_channel(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003122 list_for_each_entry(pch, &pn->new_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (pch->file.index == unit) {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003124 list_move(&pch->list, &pn->all_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return pch;
3126 }
3127 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003128
3129 list_for_each_entry(pch, &pn->all_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (pch->file.index == unit)
3131 return pch;
3132 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return NULL;
3135}
3136
3137/*
3138 * Connect a PPP channel to a PPP interface unit.
3139 */
3140static int
3141ppp_connect_channel(struct channel *pch, int unit)
3142{
3143 struct ppp *ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003144 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 int ret = -ENXIO;
3146 int hdrlen;
3147
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003148 pn = ppp_pernet(pch->chan_net);
3149
3150 mutex_lock(&pn->all_ppp_mutex);
3151 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -08003152 if (!ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 goto out;
3154 write_lock_bh(&pch->upl);
3155 ret = -EINVAL;
Joe Perchescd228d52007-11-12 18:07:31 -08003156 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 goto outl;
3158
3159 ppp_lock(ppp);
3160 if (pch->file.hdrlen > ppp->file.hdrlen)
3161 ppp->file.hdrlen = pch->file.hdrlen;
3162 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
James Chapman739840d2008-12-17 12:02:16 +00003163 if (hdrlen > ppp->dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 ppp->dev->hard_header_len = hdrlen;
3165 list_add_tail(&pch->clist, &ppp->channels);
3166 ++ppp->n_channels;
3167 pch->ppp = ppp;
3168 atomic_inc(&ppp->file.refcnt);
3169 ppp_unlock(ppp);
3170 ret = 0;
3171
3172 outl:
3173 write_unlock_bh(&pch->upl);
3174 out:
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003175 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return ret;
3177}
3178
3179/*
3180 * Disconnect a channel from its ppp unit.
3181 */
3182static int
3183ppp_disconnect_channel(struct channel *pch)
3184{
3185 struct ppp *ppp;
3186 int err = -EINVAL;
3187
3188 write_lock_bh(&pch->upl);
3189 ppp = pch->ppp;
3190 pch->ppp = NULL;
3191 write_unlock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08003192 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 /* remove it from the ppp unit's list */
3194 ppp_lock(ppp);
3195 list_del(&pch->clist);
3196 if (--ppp->n_channels == 0)
3197 wake_up_interruptible(&ppp->file.rwait);
3198 ppp_unlock(ppp);
3199 if (atomic_dec_and_test(&ppp->file.refcnt))
3200 ppp_destroy_interface(ppp);
3201 err = 0;
3202 }
3203 return err;
3204}
3205
3206/*
3207 * Free up the resources used by a ppp channel.
3208 */
3209static void ppp_destroy_channel(struct channel *pch)
3210{
WANG Cong205e1e22016-07-05 22:12:36 -07003211 put_net(pch->chan_net);
3212 pch->chan_net = NULL;
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 atomic_dec(&channel_count);
3215
3216 if (!pch->file.dead) {
3217 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003218 pr_err("ppp: destroying undead channel %p !\n", pch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return;
3220 }
3221 skb_queue_purge(&pch->file.xq);
3222 skb_queue_purge(&pch->file.rq);
3223 kfree(pch);
3224}
3225
3226static void __exit ppp_cleanup(void)
3227{
3228 /* should never happen */
3229 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
David S. Millerb48f8c22011-01-20 22:44:36 -08003230 pr_err("PPP: removing module but units remain!\n");
Guillaume Nault96d934c2016-04-28 17:55:30 +02003231 rtnl_link_unregister(&ppp_link_ops);
Akinobu Mita68fc4fa2007-07-19 01:47:50 -07003232 unregister_chrdev(PPP_MAJOR, "ppp");
Greg Kroah-Hartman9a6a2a52006-09-12 17:00:10 +02003233 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08003234 class_destroy(ppp_class);
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00003235 unregister_pernet_device(&ppp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
3238/*
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003239 * Units handling. Caller must protect concurrent access
3240 * by holding all_ppp_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Cyrill Gorcunovbcc70bb2010-11-23 11:43:44 +00003243/* associate pointer with specified number */
3244static int unit_set(struct idr *p, void *ptr, int n)
3245{
3246 int unit;
3247
Tejun Heo2fa532c2013-02-27 17:04:36 -08003248 unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3249 if (unit == -ENOSPC)
3250 unit = -EINVAL;
Cyrill Gorcunov85997572009-01-12 22:11:56 -08003251 return unit;
3252}
3253
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003254/* get new free unit number and associate pointer with it */
3255static int unit_get(struct idr *p, void *ptr)
3256{
Tejun Heo2fa532c2013-02-27 17:04:36 -08003257 return idr_alloc(p, ptr, 0, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003260/* put unit number back to a pool */
3261static void unit_put(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003263 idr_remove(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003266/* get pointer associated with the number */
3267static void *unit_find(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003269 return idr_find(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
3272/* Module/initialization stuff */
3273
3274module_init(ppp_init);
3275module_exit(ppp_cleanup);
3276
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003277EXPORT_SYMBOL(ppp_register_net_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278EXPORT_SYMBOL(ppp_register_channel);
3279EXPORT_SYMBOL(ppp_unregister_channel);
3280EXPORT_SYMBOL(ppp_channel_index);
3281EXPORT_SYMBOL(ppp_unit_number);
James Chapman63f96072010-04-02 06:18:39 +00003282EXPORT_SYMBOL(ppp_dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283EXPORT_SYMBOL(ppp_input);
3284EXPORT_SYMBOL(ppp_input_error);
3285EXPORT_SYMBOL(ppp_output_wakeup);
3286EXPORT_SYMBOL(ppp_register_compressor);
3287EXPORT_SYMBOL(ppp_unregister_compressor);
3288MODULE_LICENSE("GPL");
Kay Sievers578454f2010-05-20 18:07:20 +02003289MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
Guillaume Nault96d934c2016-04-28 17:55:30 +02003290MODULE_ALIAS_RTNL_LINK("ppp");
Kay Sievers578454f2010-05-20 18:07:20 +02003291MODULE_ALIAS("devname:ppp");