blob: 801bab5968d04e4f88523fa5a97ef8b57331d1b8 [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);
Guillaume Naultfe3627f2018-03-20 16:49:26 +0100258static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 switch (pf->kind) {
515 case INTERFACE:
Guillaume Naultfe3627f2018-03-20 16:49:26 +0100516 ppp_xmit_process(PF_TO_PPP(pf), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518 case CHANNEL:
Guillaume Naultfe3627f2018-03-20 16:49:26 +0100519 skb_queue_tail(&pf->xq, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ppp_channel_push(PF_TO_CHANNEL(pf));
521 break;
522 }
523
524 ret = count;
525
526 out:
527 return ret;
528}
529
530/* No kernel lock - fine */
531static unsigned int ppp_poll(struct file *file, poll_table *wait)
532{
533 struct ppp_file *pf = file->private_data;
534 unsigned int mask;
535
Joe Perchescd228d52007-11-12 18:07:31 -0800536 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538 poll_wait(file, &pf->rwait, wait);
539 mask = POLLOUT | POLLWRNORM;
Joe Perchescd228d52007-11-12 18:07:31 -0800540 if (skb_peek(&pf->rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 mask |= POLLIN | POLLRDNORM;
542 if (pf->dead)
543 mask |= POLLHUP;
544 else if (pf->kind == INTERFACE) {
545 /* see comment in ppp_read */
546 struct ppp *ppp = PF_TO_PPP(pf);
Guillaume Naultedffc212016-02-26 18:45:34 +0100547
548 ppp_recv_lock(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +0000549 if (ppp->n_channels == 0 &&
550 (ppp->flags & SC_LOOP_TRAFFIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 mask |= POLLIN | POLLRDNORM;
Guillaume Naultedffc212016-02-26 18:45:34 +0100552 ppp_recv_unlock(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
555 return mask;
556}
557
558#ifdef CONFIG_PPP_FILTER
559static int get_filter(void __user *arg, struct sock_filter **p)
560{
561 struct sock_fprog uprog;
562 struct sock_filter *code = NULL;
Christoph Schulz3916a312014-07-14 08:01:10 +0200563 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (copy_from_user(&uprog, arg, sizeof(uprog)))
566 return -EFAULT;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!uprog.len) {
569 *p = NULL;
570 return 0;
571 }
572
573 len = uprog.len * sizeof(struct sock_filter);
Julia Lawallb23d00e2010-05-21 22:18:59 +0000574 code = memdup_user(uprog.filter, len);
575 if (IS_ERR(code))
576 return PTR_ERR(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 *p = code;
579 return uprog.len;
580}
581#endif /* CONFIG_PPP_FILTER */
582
Alan Coxf3ff8a42008-05-25 23:40:58 -0700583static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100585 struct ppp_file *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct ppp *ppp;
587 int err = -EFAULT, val, val2, i;
588 struct ppp_idle idle;
589 struct npioctl npi;
590 int unit, cflags;
591 struct slcompress *vj;
592 void __user *argp = (void __user *)arg;
593 int __user *p = argp;
594
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100595 mutex_lock(&ppp_mutex);
596
597 pf = file->private_data;
598 if (!pf) {
599 err = ppp_unattached_ioctl(current->nsproxy->net_ns,
600 pf, file, cmd, arg);
601 goto out;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (cmd == PPPIOCDETACH) {
605 /*
606 * We have to be careful here... if the file descriptor
607 * has been dup'd, we could have another process in the
608 * middle of a poll using the same file *, so we had
609 * better not free the interface data structures -
610 * instead we fail the ioctl. Even in this case, we
611 * shut down the interface if we are the owner of it.
612 * Actually, we should get rid of PPPIOCDETACH, userland
613 * (i.e. pppd) could achieve the same effect by closing
614 * this fd and reopening /dev/ppp.
615 */
616 err = -EINVAL;
617 if (pf->kind == INTERFACE) {
618 ppp = PF_TO_PPP(pf);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200619 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (file == ppp->owner)
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200621 unregister_netdevice(ppp->dev);
622 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Al Viro24dff962014-10-08 23:44:00 -0400624 if (atomic_long_read(&file->f_count) < 2) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700625 ppp_release(NULL, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 err = 0;
627 } else
David S. Millerb48f8c22011-01-20 22:44:36 -0800628 pr_warn("PPPIOCDETACH file->f_count=%ld\n",
629 atomic_long_read(&file->f_count));
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
633 if (pf->kind == CHANNEL) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700634 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct ppp_channel *chan;
636
Alan Coxf3ff8a42008-05-25 23:40:58 -0700637 pch = PF_TO_CHANNEL(pf);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 switch (cmd) {
640 case PPPIOCCONNECT:
641 if (get_user(unit, p))
642 break;
643 err = ppp_connect_channel(pch, unit);
644 break;
645
646 case PPPIOCDISCONN:
647 err = ppp_disconnect_channel(pch);
648 break;
649
650 default:
651 down_read(&pch->chan_sem);
652 chan = pch->chan;
653 err = -ENOTTY;
654 if (chan && chan->ops->ioctl)
655 err = chan->ops->ioctl(chan, cmd, arg);
656 up_read(&pch->chan_sem);
657 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100658 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 if (pf->kind != INTERFACE) {
662 /* can't happen */
David S. Millerb48f8c22011-01-20 22:44:36 -0800663 pr_err("PPP: not interface or channel??\n");
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100664 err = -EINVAL;
665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
668 ppp = PF_TO_PPP(pf);
669 switch (cmd) {
670 case PPPIOCSMRU:
671 if (get_user(val, p))
672 break;
673 ppp->mru = val;
674 err = 0;
675 break;
676
677 case PPPIOCSFLAGS:
678 if (get_user(val, p))
679 break;
680 ppp_lock(ppp);
681 cflags = ppp->flags & ~val;
Christoph Schulza9f559c2014-07-16 23:41:26 +0200682#ifdef CONFIG_PPP_MULTILINK
Christoph Schulzd762d032014-07-15 11:51:03 +0200683 if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
684 ppp->nextseq = 0;
Christoph Schulza9f559c2014-07-16 23:41:26 +0200685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 ppp->flags = val & SC_FLAG_BITS;
687 ppp_unlock(ppp);
688 if (cflags & SC_CCP_OPEN)
689 ppp_ccp_closed(ppp);
690 err = 0;
691 break;
692
693 case PPPIOCGFLAGS:
694 val = ppp->flags | ppp->xstate | ppp->rstate;
695 if (put_user(val, p))
696 break;
697 err = 0;
698 break;
699
700 case PPPIOCSCOMPRESS:
701 err = ppp_set_compress(ppp, arg);
702 break;
703
704 case PPPIOCGUNIT:
705 if (put_user(ppp->file.index, p))
706 break;
707 err = 0;
708 break;
709
710 case PPPIOCSDEBUG:
711 if (get_user(val, p))
712 break;
713 ppp->debug = val;
714 err = 0;
715 break;
716
717 case PPPIOCGDEBUG:
718 if (put_user(ppp->debug, p))
719 break;
720 err = 0;
721 break;
722
723 case PPPIOCGIDLE:
724 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
725 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
726 if (copy_to_user(argp, &idle, sizeof(idle)))
727 break;
728 err = 0;
729 break;
730
731 case PPPIOCSMAXCID:
732 if (get_user(val, p))
733 break;
734 val2 = 15;
735 if ((val >> 16) != 0) {
736 val2 = val >> 16;
737 val &= 0xffff;
738 }
739 vj = slhc_init(val2+1, val+1);
Ben Hutchings4ab42d72015-11-01 16:22:53 +0000740 if (IS_ERR(vj)) {
741 err = PTR_ERR(vj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743 }
744 ppp_lock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -0800745 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 slhc_free(ppp->vj);
747 ppp->vj = vj;
748 ppp_unlock(ppp);
749 err = 0;
750 break;
751
752 case PPPIOCGNPMODE:
753 case PPPIOCSNPMODE:
754 if (copy_from_user(&npi, argp, sizeof(npi)))
755 break;
756 err = proto_to_npindex(npi.protocol);
757 if (err < 0)
758 break;
759 i = err;
760 if (cmd == PPPIOCGNPMODE) {
761 err = -EFAULT;
762 npi.mode = ppp->npmode[i];
763 if (copy_to_user(argp, &npi, sizeof(npi)))
764 break;
765 } else {
766 ppp->npmode[i] = npi.mode;
767 /* we may be able to transmit more packets now (??) */
768 netif_wake_queue(ppp->dev);
769 }
770 err = 0;
771 break;
772
773#ifdef CONFIG_PPP_FILTER
774 case PPPIOCSPASS:
775 {
776 struct sock_filter *code;
Daniel Borkmann568f1942014-03-28 18:58:23 +0100777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 err = get_filter(argp, &code);
779 if (err >= 0) {
Takashi Iwai5748eb82014-11-10 11:50:21 +0100780 struct bpf_prog *pass_filter = NULL;
Daniel Borkmannb1fcd352014-05-23 18:43:58 +0200781 struct sock_fprog_kern fprog = {
Daniel Borkmann568f1942014-03-28 18:58:23 +0100782 .len = err,
783 .filter = code,
784 };
785
Takashi Iwai5748eb82014-11-10 11:50:21 +0100786 err = 0;
787 if (fprog.filter)
788 err = bpf_prog_create(&pass_filter, &fprog);
789 if (!err) {
790 ppp_lock(ppp);
791 if (ppp->pass_filter)
792 bpf_prog_destroy(ppp->pass_filter);
793 ppp->pass_filter = pass_filter;
794 ppp_unlock(ppp);
Christoph Schulzcc25eaa2014-07-16 22:10:29 +0200795 }
Daniel Borkmann568f1942014-03-28 18:58:23 +0100796 kfree(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 break;
799 }
800 case PPPIOCSACTIVE:
801 {
802 struct sock_filter *code;
Daniel Borkmann568f1942014-03-28 18:58:23 +0100803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 err = get_filter(argp, &code);
805 if (err >= 0) {
Takashi Iwai5748eb82014-11-10 11:50:21 +0100806 struct bpf_prog *active_filter = NULL;
Daniel Borkmannb1fcd352014-05-23 18:43:58 +0200807 struct sock_fprog_kern fprog = {
Daniel Borkmann568f1942014-03-28 18:58:23 +0100808 .len = err,
809 .filter = code,
810 };
811
Takashi Iwai5748eb82014-11-10 11:50:21 +0100812 err = 0;
813 if (fprog.filter)
814 err = bpf_prog_create(&active_filter, &fprog);
815 if (!err) {
816 ppp_lock(ppp);
817 if (ppp->active_filter)
818 bpf_prog_destroy(ppp->active_filter);
819 ppp->active_filter = active_filter;
820 ppp_unlock(ppp);
Christoph Schulzcc25eaa2014-07-16 22:10:29 +0200821 }
Daniel Borkmann568f1942014-03-28 18:58:23 +0100822 kfree(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824 break;
825 }
826#endif /* CONFIG_PPP_FILTER */
827
828#ifdef CONFIG_PPP_MULTILINK
829 case PPPIOCSMRRU:
830 if (get_user(val, p))
831 break;
832 ppp_recv_lock(ppp);
833 ppp->mrru = val;
834 ppp_recv_unlock(ppp);
835 err = 0;
836 break;
837#endif /* CONFIG_PPP_MULTILINK */
838
839 default:
840 err = -ENOTTY;
841 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100842
843out:
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000844 mutex_unlock(&ppp_mutex);
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return err;
847}
848
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800849static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
850 struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 int unit, err = -EFAULT;
853 struct ppp *ppp;
854 struct channel *chan;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800855 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int __user *p = (int __user *)arg;
857
858 switch (cmd) {
859 case PPPIOCNEWUNIT:
860 /* Create a new ppp unit */
861 if (get_user(unit, p))
862 break;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200863 err = ppp_create_interface(net, file, &unit);
864 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 err = -EFAULT;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200868 if (put_user(unit, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
870 err = 0;
871 break;
872
873 case PPPIOCATTACH:
874 /* Attach to an existing ppp unit */
875 if (get_user(unit, p))
876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800878 pn = ppp_pernet(net);
879 mutex_lock(&pn->all_ppp_mutex);
880 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800881 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 atomic_inc(&ppp->file.refcnt);
883 file->private_data = &ppp->file;
884 err = 0;
885 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800886 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
888
889 case PPPIOCATTCHAN:
890 if (get_user(unit, p))
891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800893 pn = ppp_pernet(net);
894 spin_lock_bh(&pn->all_channels_lock);
895 chan = ppp_find_channel(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800896 if (chan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 atomic_inc(&chan->file.refcnt);
898 file->private_data = &chan->file;
899 err = 0;
900 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800901 spin_unlock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903
904 default:
905 err = -ENOTTY;
906 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return err;
909}
910
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800911static const struct file_operations ppp_device_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .owner = THIS_MODULE,
913 .read = ppp_read,
914 .write = ppp_write,
915 .poll = ppp_poll,
Alan Coxf3ff8a42008-05-25 23:40:58 -0700916 .unlocked_ioctl = ppp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 .open = ppp_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200918 .release = ppp_release,
919 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920};
921
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800922static __net_init int ppp_init_net(struct net *net)
923{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000924 struct ppp_net *pn = net_generic(net, ppp_net_id);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800925
926 idr_init(&pn->units_idr);
927 mutex_init(&pn->all_ppp_mutex);
928
929 INIT_LIST_HEAD(&pn->all_channels);
930 INIT_LIST_HEAD(&pn->new_channels);
931
932 spin_lock_init(&pn->all_channels_lock);
933
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800934 return 0;
935}
936
937static __net_exit void ppp_exit_net(struct net *net)
938{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000939 struct ppp_net *pn = net_generic(net, ppp_net_id);
Guillaume Nault79c441a2015-08-24 11:35:30 +0200940 struct net_device *dev;
941 struct net_device *aux;
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200942 struct ppp *ppp;
943 LIST_HEAD(list);
944 int id;
945
946 rtnl_lock();
Guillaume Nault79c441a2015-08-24 11:35:30 +0200947 for_each_netdev_safe(net, dev, aux) {
948 if (dev->netdev_ops == &ppp_netdev_ops)
949 unregister_netdevice_queue(dev, &list);
950 }
951
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200952 idr_for_each_entry(&pn->units_idr, ppp, id)
Guillaume Nault79c441a2015-08-24 11:35:30 +0200953 /* Skip devices already unregistered by previous loop */
954 if (!net_eq(dev_net(ppp->dev), net))
955 unregister_netdevice_queue(ppp->dev, &list);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200956
957 unregister_netdevice_many(&list);
958 rtnl_unlock();
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800959
Gao Fengcf16dac2017-10-31 18:25:37 +0800960 mutex_destroy(&pn->all_ppp_mutex);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800961 idr_destroy(&pn->units_idr);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800962}
963
Alexey Dobriyan00129852009-02-09 18:05:16 -0800964static struct pernet_operations ppp_net_ops = {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800965 .init = ppp_init_net,
966 .exit = ppp_exit_net,
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000967 .id = &ppp_net_id,
968 .size = sizeof(struct ppp_net),
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800969};
970
Guillaume Nault96d934c2016-04-28 17:55:30 +0200971static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200972{
973 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
974 int ret;
975
976 mutex_lock(&pn->all_ppp_mutex);
977
978 if (unit < 0) {
979 ret = unit_get(&pn->units_idr, ppp);
980 if (ret < 0)
981 goto err;
982 } else {
983 /* Caller asked for a specific unit number. Fail with -EEXIST
984 * if unavailable. For backward compatibility, return -EEXIST
985 * too if idr allocation fails; this makes pppd retry without
986 * requesting a specific unit number.
987 */
988 if (unit_find(&pn->units_idr, unit)) {
989 ret = -EEXIST;
990 goto err;
991 }
992 ret = unit_set(&pn->units_idr, ppp, unit);
993 if (ret < 0) {
994 /* Rewrite error for backward compatibility */
995 ret = -EEXIST;
996 goto err;
997 }
998 }
999 ppp->file.index = ret;
1000
Guillaume Nault96d934c2016-04-28 17:55:30 +02001001 if (!ifname_is_set)
1002 snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001003
Guillaume Nault00f9e472018-01-10 16:24:45 +01001004 mutex_unlock(&pn->all_ppp_mutex);
1005
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001006 ret = register_netdevice(ppp->dev);
1007 if (ret < 0)
1008 goto err_unit;
1009
1010 atomic_inc(&ppp_unit_count);
1011
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001012 return 0;
1013
1014err_unit:
Guillaume Nault00f9e472018-01-10 16:24:45 +01001015 mutex_lock(&pn->all_ppp_mutex);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001016 unit_put(&pn->units_idr, ppp->file.index);
1017err:
1018 mutex_unlock(&pn->all_ppp_mutex);
1019
1020 return ret;
1021}
1022
1023static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1024 const struct ppp_config *conf)
1025{
1026 struct ppp *ppp = netdev_priv(dev);
1027 int indx;
1028 int err;
Gao Feng3b25bfc2017-07-17 18:34:42 +08001029 int cpu;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001030
1031 ppp->dev = dev;
1032 ppp->ppp_net = src_net;
1033 ppp->mru = PPP_MRU;
1034 ppp->owner = conf->file;
1035
1036 init_ppp_file(&ppp->file, INTERFACE);
1037 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1038
1039 for (indx = 0; indx < NUM_NP; ++indx)
1040 ppp->npmode[indx] = NPMODE_PASS;
1041 INIT_LIST_HEAD(&ppp->channels);
1042 spin_lock_init(&ppp->rlock);
1043 spin_lock_init(&ppp->wlock);
Gao Feng3b25bfc2017-07-17 18:34:42 +08001044
1045 ppp->xmit_recursion = alloc_percpu(int);
1046 if (!ppp->xmit_recursion) {
1047 err = -ENOMEM;
1048 goto err1;
1049 }
1050 for_each_possible_cpu(cpu)
1051 (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
1052
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001053#ifdef CONFIG_PPP_MULTILINK
1054 ppp->minseq = -1;
1055 skb_queue_head_init(&ppp->mrq);
1056#endif /* CONFIG_PPP_MULTILINK */
1057#ifdef CONFIG_PPP_FILTER
1058 ppp->pass_filter = NULL;
1059 ppp->active_filter = NULL;
1060#endif /* CONFIG_PPP_FILTER */
1061
Guillaume Nault96d934c2016-04-28 17:55:30 +02001062 err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001063 if (err < 0)
Gao Feng3b25bfc2017-07-17 18:34:42 +08001064 goto err2;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001065
1066 conf->file->private_data = &ppp->file;
1067
1068 return 0;
Gao Feng3b25bfc2017-07-17 18:34:42 +08001069err2:
1070 free_percpu(ppp->xmit_recursion);
1071err1:
1072 return err;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001073}
1074
Guillaume Nault96d934c2016-04-28 17:55:30 +02001075static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1076 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
1077};
1078
1079static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[])
1080{
1081 if (!data)
1082 return -EINVAL;
1083
1084 if (!data[IFLA_PPP_DEV_FD])
1085 return -EINVAL;
1086 if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1087 return -EBADF;
1088
1089 return 0;
1090}
1091
1092static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1093 struct nlattr *tb[], struct nlattr *data[])
1094{
1095 struct ppp_config conf = {
1096 .unit = -1,
1097 .ifname_is_set = true,
1098 };
1099 struct file *file;
1100 int err;
1101
1102 file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1103 if (!file)
1104 return -EBADF;
1105
1106 /* rtnl_lock is already held here, but ppp_create_interface() locks
1107 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1108 * possible deadlock due to lock order inversion, at the cost of
1109 * pushing the problem back to userspace.
1110 */
1111 if (!mutex_trylock(&ppp_mutex)) {
1112 err = -EBUSY;
1113 goto out;
1114 }
1115
1116 if (file->f_op != &ppp_device_fops || file->private_data) {
1117 err = -EBADF;
1118 goto out_unlock;
1119 }
1120
1121 conf.file = file;
Guillaume Naultbb8082f2016-08-09 15:12:26 +02001122
1123 /* Don't use device name generated by the rtnetlink layer when ifname
1124 * isn't specified. Let ppp_dev_configure() set the device name using
1125 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1126 * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1127 */
1128 if (!tb[IFLA_IFNAME])
1129 conf.ifname_is_set = false;
1130
Guillaume Nault96d934c2016-04-28 17:55:30 +02001131 err = ppp_dev_configure(src_net, dev, &conf);
1132
1133out_unlock:
1134 mutex_unlock(&ppp_mutex);
1135out:
1136 fput(file);
1137
1138 return err;
1139}
1140
1141static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1142{
1143 unregister_netdevice_queue(dev, head);
1144}
1145
1146static size_t ppp_nl_get_size(const struct net_device *dev)
1147{
1148 return 0;
1149}
1150
1151static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1152{
1153 return 0;
1154}
1155
1156static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1157{
1158 struct ppp *ppp = netdev_priv(dev);
1159
1160 return ppp->ppp_net;
1161}
1162
1163static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1164 .kind = "ppp",
1165 .maxtype = IFLA_PPP_MAX,
1166 .policy = ppp_nl_policy,
1167 .priv_size = sizeof(struct ppp),
1168 .setup = ppp_setup,
1169 .validate = ppp_nl_validate,
1170 .newlink = ppp_nl_newlink,
1171 .dellink = ppp_nl_dellink,
1172 .get_size = ppp_nl_get_size,
1173 .fill_info = ppp_nl_fill_info,
1174 .get_link_net = ppp_nl_get_link_net,
1175};
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177#define PPP_MAJOR 108
1178
1179/* Called at boot time if ppp is compiled into the kernel,
1180 or at module load time (from init_module) if compiled as a module. */
1181static int __init ppp_init(void)
1182{
1183 int err;
1184
David S. Millerb48f8c22011-01-20 22:44:36 -08001185 pr_info("PPP generic driver version " PPP_VERSION "\n");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001186
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001187 err = register_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001188 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001189 pr_err("failed to register PPP pernet device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001190 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001193 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1194 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001195 pr_err("failed to register PPP device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001196 goto out_net;
1197 }
1198
1199 ppp_class = class_create(THIS_MODULE, "ppp");
1200 if (IS_ERR(ppp_class)) {
1201 err = PTR_ERR(ppp_class);
1202 goto out_chrdev;
1203 }
1204
Guillaume Nault96d934c2016-04-28 17:55:30 +02001205 err = rtnl_link_register(&ppp_link_ops);
1206 if (err) {
1207 pr_err("failed to register rtnetlink PPP handler\n");
1208 goto out_class;
1209 }
1210
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001211 /* not a big deal if we fail here :-) */
1212 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1213
1214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Guillaume Nault96d934c2016-04-28 17:55:30 +02001216out_class:
1217 class_destroy(ppp_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218out_chrdev:
1219 unregister_chrdev(PPP_MAJOR, "ppp");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001220out_net:
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001221 unregister_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001222out:
1223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226/*
1227 * Network interface unit routines.
1228 */
Stephen Hemminger424efe92009-08-31 19:50:51 +00001229static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1231{
Wang Chenc8019bf2008-11-20 04:24:17 -08001232 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int npi, proto;
1234 unsigned char *pp;
1235
1236 npi = ethertype_to_npindex(ntohs(skb->protocol));
1237 if (npi < 0)
1238 goto outf;
1239
1240 /* Drop, accept or reject the packet */
1241 switch (ppp->npmode[npi]) {
1242 case NPMODE_PASS:
1243 break;
1244 case NPMODE_QUEUE:
1245 /* it would be nice to have a way to tell the network
1246 system to queue this one up for later. */
1247 goto outf;
1248 case NPMODE_DROP:
1249 case NPMODE_ERROR:
1250 goto outf;
1251 }
1252
1253 /* Put the 2-byte PPP protocol number on the front,
1254 making sure there is room for the address and control fields. */
Herbert Xu7b797d52007-09-16 16:21:42 -07001255 if (skb_cow_head(skb, PPP_HDRLEN))
1256 goto outf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 pp = skb_push(skb, 2);
1259 proto = npindex_to_proto[npi];
Changli Gao96545ae2011-01-06 13:37:36 +00001260 put_unaligned_be16(proto, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Guillaume Nault79c441a2015-08-24 11:35:30 +02001262 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
Guillaume Naultfe3627f2018-03-20 16:49:26 +01001263 ppp_xmit_process(ppp, skb);
1264
Patrick McHardy6ed10652009-06-23 06:03:08 +00001265 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 outf:
1268 kfree_skb(skb);
Paulius Zaleckas6ceffd42009-02-23 04:59:43 +00001269 ++dev->stats.tx_dropped;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001270 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273static int
1274ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1275{
Wang Chenc8019bf2008-11-20 04:24:17 -08001276 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int err = -EFAULT;
1278 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1279 struct ppp_stats stats;
1280 struct ppp_comp_stats cstats;
1281 char *vers;
1282
1283 switch (cmd) {
1284 case SIOCGPPPSTATS:
1285 ppp_get_stats(ppp, &stats);
1286 if (copy_to_user(addr, &stats, sizeof(stats)))
1287 break;
1288 err = 0;
1289 break;
1290
1291 case SIOCGPPPCSTATS:
1292 memset(&cstats, 0, sizeof(cstats));
Joe Perchescd228d52007-11-12 18:07:31 -08001293 if (ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
Joe Perchescd228d52007-11-12 18:07:31 -08001295 if (ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1297 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1298 break;
1299 err = 0;
1300 break;
1301
1302 case SIOCGPPPVER:
1303 vers = PPP_VERSION;
1304 if (copy_to_user(addr, vers, strlen(vers) + 1))
1305 break;
1306 err = 0;
1307 break;
1308
1309 default:
1310 err = -EINVAL;
1311 }
1312
1313 return err;
1314}
1315
stephen hemmingerb77bc202012-10-29 08:34:02 +00001316static struct rtnl_link_stats64*
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001317ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1318{
1319 struct ppp *ppp = netdev_priv(dev);
1320
1321 ppp_recv_lock(ppp);
1322 stats64->rx_packets = ppp->stats64.rx_packets;
1323 stats64->rx_bytes = ppp->stats64.rx_bytes;
1324 ppp_recv_unlock(ppp);
1325
1326 ppp_xmit_lock(ppp);
1327 stats64->tx_packets = ppp->stats64.tx_packets;
1328 stats64->tx_bytes = ppp->stats64.tx_bytes;
1329 ppp_xmit_unlock(ppp);
1330
1331 stats64->rx_errors = dev->stats.rx_errors;
1332 stats64->tx_errors = dev->stats.tx_errors;
1333 stats64->rx_dropped = dev->stats.rx_dropped;
1334 stats64->tx_dropped = dev->stats.tx_dropped;
1335 stats64->rx_length_errors = dev->stats.rx_length_errors;
1336
1337 return stats64;
1338}
1339
Eric Dumazet303c07d2013-02-19 10:42:03 -08001340static int ppp_dev_init(struct net_device *dev)
1341{
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001342 struct ppp *ppp;
1343
Eric Dumazetd3fff6c2016-06-09 07:45:12 -07001344 netdev_lockdep_set_classes(dev);
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001345
1346 ppp = netdev_priv(dev);
1347 /* Let the netdevice take a reference on the ppp file. This ensures
1348 * that ppp_destroy_interface() won't run before the device gets
1349 * unregistered.
1350 */
1351 atomic_inc(&ppp->file.refcnt);
1352
Eric Dumazet303c07d2013-02-19 10:42:03 -08001353 return 0;
1354}
1355
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001356static void ppp_dev_uninit(struct net_device *dev)
1357{
1358 struct ppp *ppp = netdev_priv(dev);
1359 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1360
1361 ppp_lock(ppp);
1362 ppp->closing = 1;
1363 ppp_unlock(ppp);
1364
1365 mutex_lock(&pn->all_ppp_mutex);
1366 unit_put(&pn->units_idr, ppp->file.index);
1367 mutex_unlock(&pn->all_ppp_mutex);
1368
1369 ppp->owner = NULL;
1370
1371 ppp->file.dead = 1;
1372 wake_up_interruptible(&ppp->file.rwait);
1373}
1374
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001375static void ppp_dev_priv_destructor(struct net_device *dev)
1376{
1377 struct ppp *ppp;
1378
1379 ppp = netdev_priv(dev);
1380 if (atomic_dec_and_test(&ppp->file.refcnt))
1381 ppp_destroy_interface(ppp);
1382}
1383
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001384static const struct net_device_ops ppp_netdev_ops = {
Eric Dumazet303c07d2013-02-19 10:42:03 -08001385 .ndo_init = ppp_dev_init,
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001386 .ndo_uninit = ppp_dev_uninit,
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001387 .ndo_start_xmit = ppp_start_xmit,
1388 .ndo_do_ioctl = ppp_net_ioctl,
1389 .ndo_get_stats64 = ppp_get_stats64,
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001390};
1391
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001392static struct device_type ppp_type = {
1393 .name = "ppp",
1394};
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396static void ppp_setup(struct net_device *dev)
1397{
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001398 dev->netdev_ops = &ppp_netdev_ops;
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001399 SET_NETDEV_DEVTYPE(dev, &ppp_type);
1400
Guillaume Nault07712772016-08-27 22:22:51 +02001401 dev->features |= NETIF_F_LLTX;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 dev->hard_header_len = PPP_HDRLEN;
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +00001404 dev->mtu = PPP_MRU;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 dev->addr_len = 0;
1406 dev->tx_queue_len = 3;
1407 dev->type = ARPHRD_PPP;
1408 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Guillaume Naultac4cfc72017-10-06 17:05:49 +02001409 dev->destructor = ppp_dev_priv_destructor;
Eric Dumazet02875872014-10-05 18:38:35 -07001410 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413/*
1414 * Transmit-side routines.
1415 */
1416
Guillaume Nault55454a52016-08-27 22:22:32 +02001417/* Called to do any work queued up on the transmit side that can now be done */
Guillaume Naultfe3627f2018-03-20 16:49:26 +01001418static void __ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 ppp_xmit_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001421 if (!ppp->closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 ppp_push(ppp);
Guillaume Naultfe3627f2018-03-20 16:49:26 +01001423
1424 if (skb)
1425 skb_queue_tail(&ppp->file.xq, skb);
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);
Takeshi Misawaac7ac132019-09-22 16:45:31 +09001435 } else {
1436 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 ppp_xmit_unlock(ppp);
1439}
1440
Guillaume Naultfe3627f2018-03-20 16:49:26 +01001441static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
Guillaume Nault55454a52016-08-27 22:22:32 +02001442{
1443 local_bh_disable();
1444
Gao Feng3b25bfc2017-07-17 18:34:42 +08001445 if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
Guillaume Nault55454a52016-08-27 22:22:32 +02001446 goto err;
1447
Gao Feng3b25bfc2017-07-17 18:34:42 +08001448 (*this_cpu_ptr(ppp->xmit_recursion))++;
Guillaume Naultfe3627f2018-03-20 16:49:26 +01001449 __ppp_xmit_process(ppp, skb);
Gao Feng3b25bfc2017-07-17 18:34:42 +08001450 (*this_cpu_ptr(ppp->xmit_recursion))--;
Guillaume Nault55454a52016-08-27 22:22:32 +02001451
1452 local_bh_enable();
1453
1454 return;
1455
1456err:
1457 local_bh_enable();
1458
Guillaume Naultfe3627f2018-03-20 16:49:26 +01001459 kfree_skb(skb);
1460
Guillaume Nault55454a52016-08-27 22:22:32 +02001461 if (net_ratelimit())
1462 netdev_err(ppp->dev, "recursion detected\n");
1463}
1464
Matt Domschb3f9b922005-11-08 09:40:47 -08001465static inline struct sk_buff *
1466pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1467{
1468 struct sk_buff *new_skb;
1469 int len;
1470 int new_skb_size = ppp->dev->mtu +
1471 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1472 int compressor_skb_size = ppp->dev->mtu +
1473 ppp->xcomp->comp_extra + PPP_HDRLEN;
1474 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1475 if (!new_skb) {
1476 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001477 netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001478 return NULL;
1479 }
1480 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1481 skb_reserve(new_skb,
1482 ppp->dev->hard_header_len - PPP_HDRLEN);
1483
1484 /* compressor still expects A/C bytes in hdr */
1485 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1486 new_skb->data, skb->len + 2,
1487 compressor_skb_size);
1488 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
Eric Dumazet968d7012012-05-18 20:23:00 +00001489 consume_skb(skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001490 skb = new_skb;
1491 skb_put(skb, len);
1492 skb_pull(skb, 2); /* pull off A/C bytes */
1493 } else if (len == 0) {
1494 /* didn't compress, or CCP not up yet */
Eric Dumazet968d7012012-05-18 20:23:00 +00001495 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001496 new_skb = skb;
1497 } else {
1498 /*
1499 * (len < 0)
1500 * MPPE requires that we do not send unencrypted
1501 * frames. The compressor will return -1 if we
1502 * should drop the frame. We cannot simply test
1503 * the compress_proto because MPPE and MPPC share
1504 * the same number.
1505 */
1506 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001507 netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001508 kfree_skb(skb);
Eric Dumazet968d7012012-05-18 20:23:00 +00001509 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001510 new_skb = NULL;
1511 }
1512 return new_skb;
1513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515/*
1516 * Compress and send a frame.
1517 * The caller should have locked the xmit path,
1518 * and xmit_pending should be 0.
1519 */
1520static void
1521ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1522{
1523 int proto = PPP_PROTO(skb);
1524 struct sk_buff *new_skb;
1525 int len;
1526 unsigned char *cp;
1527
1528 if (proto < 0x8000) {
1529#ifdef CONFIG_PPP_FILTER
1530 /* check if we should pass this packet */
1531 /* the filter instructions are constructed assuming
1532 a four-byte PPP header on each packet */
1533 *skb_push(skb, 2) = 1;
Joe Perches8e95a202009-12-03 07:58:21 +00001534 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001535 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001537 netdev_printk(KERN_DEBUG, ppp->dev,
1538 "PPP: outbound frame "
1539 "not passed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 kfree_skb(skb);
1541 return;
1542 }
1543 /* if this packet passes the active filter, record the time */
Joe Perches8e95a202009-12-03 07:58:21 +00001544 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001545 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 ppp->last_xmit = jiffies;
1547 skb_pull(skb, 2);
1548#else
1549 /* for data packets, record the time */
1550 ppp->last_xmit = jiffies;
1551#endif /* CONFIG_PPP_FILTER */
1552 }
1553
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001554 ++ppp->stats64.tx_packets;
1555 ppp->stats64.tx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 switch (proto) {
1558 case PPP_IP:
Joe Perchescd228d52007-11-12 18:07:31 -08001559 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 break;
1561 /* try to do VJ TCP header compression */
1562 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1563 GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001564 if (!new_skb) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001565 netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 goto drop;
1567 }
1568 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1569 cp = skb->data + 2;
1570 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1571 new_skb->data + 2, &cp,
1572 !(ppp->flags & SC_NO_TCP_CCID));
1573 if (cp == skb->data + 2) {
1574 /* didn't compress */
Eric Dumazet968d7012012-05-18 20:23:00 +00001575 consume_skb(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 } else {
1577 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1578 proto = PPP_VJC_COMP;
1579 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1580 } else {
1581 proto = PPP_VJC_UNCOMP;
1582 cp[0] = skb->data[2];
1583 }
Eric Dumazet968d7012012-05-18 20:23:00 +00001584 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 skb = new_skb;
1586 cp = skb_put(skb, len + 2);
1587 cp[0] = 0;
1588 cp[1] = proto;
1589 }
1590 break;
1591
1592 case PPP_CCP:
1593 /* peek at outbound CCP frames */
1594 ppp_ccp_peek(ppp, skb, 0);
1595 break;
1596 }
1597
1598 /* try to do packet compression */
Joe Perches8e95a202009-12-03 07:58:21 +00001599 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1600 proto != PPP_LCP && proto != PPP_CCP) {
Matt Domschb3f9b922005-11-08 09:40:47 -08001601 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1602 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001603 netdev_err(ppp->dev,
1604 "ppp: compression required but "
1605 "down - pkt dropped.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto drop;
1607 }
Matt Domschb3f9b922005-11-08 09:40:47 -08001608 skb = pad_compress_skb(ppp, skb);
1609 if (!skb)
1610 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612
1613 /*
1614 * If we are waiting for traffic (demand dialling),
1615 * queue it up for pppd to receive.
1616 */
1617 if (ppp->flags & SC_LOOP_TRAFFIC) {
1618 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1619 goto drop;
1620 skb_queue_tail(&ppp->file.rq, skb);
1621 wake_up_interruptible(&ppp->file.rwait);
1622 return;
1623 }
1624
1625 ppp->xmit_pending = skb;
1626 ppp_push(ppp);
1627 return;
1628
1629 drop:
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00001630 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001631 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634/*
1635 * Try to send the frame in xmit_pending.
1636 * The caller should have the xmit path locked.
1637 */
1638static void
1639ppp_push(struct ppp *ppp)
1640{
1641 struct list_head *list;
1642 struct channel *pch;
1643 struct sk_buff *skb = ppp->xmit_pending;
1644
Joe Perchescd228d52007-11-12 18:07:31 -08001645 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return;
1647
1648 list = &ppp->channels;
1649 if (list_empty(list)) {
1650 /* nowhere to send the packet, just drop it */
1651 ppp->xmit_pending = NULL;
1652 kfree_skb(skb);
1653 return;
1654 }
1655
1656 if ((ppp->flags & SC_MULTILINK) == 0) {
1657 /* not doing multilink: send it down the first channel */
1658 list = list->next;
1659 pch = list_entry(list, struct channel, clist);
1660
1661 spin_lock_bh(&pch->downl);
1662 if (pch->chan) {
1663 if (pch->chan->ops->start_xmit(pch->chan, skb))
1664 ppp->xmit_pending = NULL;
1665 } else {
1666 /* channel got unregistered */
1667 kfree_skb(skb);
1668 ppp->xmit_pending = NULL;
1669 }
1670 spin_unlock_bh(&pch->downl);
1671 return;
1672 }
1673
1674#ifdef CONFIG_PPP_MULTILINK
1675 /* Multilink: fragment the packet over as many links
1676 as can take the packet at the moment. */
1677 if (!ppp_mp_explode(ppp, skb))
1678 return;
1679#endif /* CONFIG_PPP_MULTILINK */
1680
1681 ppp->xmit_pending = NULL;
1682 kfree_skb(skb);
1683}
1684
1685#ifdef CONFIG_PPP_MULTILINK
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001686static bool mp_protocol_compress __read_mostly = true;
1687module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
1688MODULE_PARM_DESC(mp_protocol_compress,
1689 "compress protocol id in multilink fragments");
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/*
1692 * Divide a packet to be transmitted into fragments and
1693 * send them out the individual links.
1694 */
1695static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1696{
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001697 int len, totlen;
1698 int i, bits, hdrlen, mtu;
1699 int flen;
1700 int navail, nfree, nzero;
1701 int nbigger;
1702 int totspeed;
1703 int totfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 unsigned char *p, *q;
1705 struct list_head *list;
1706 struct channel *pch;
1707 struct sk_buff *frag;
1708 struct ppp_channel *chan;
1709
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001710 totspeed = 0; /*total bitrate of the bundle*/
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001711 nfree = 0; /* # channels which have no packet already queued */
1712 navail = 0; /* total # of usable channels (not deregistered) */
1713 nzero = 0; /* number of channels with zero speed associated*/
1714 totfree = 0; /*total # of channels available and
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001715 *having no queued packets before
1716 *starting the fragmentation*/
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001719 i = 0;
1720 list_for_each_entry(pch, &ppp->channels, clist) {
Dan Carpenter34297692010-09-10 01:58:10 +00001721 if (pch->chan) {
1722 pch->avail = 1;
1723 navail++;
1724 pch->speed = pch->chan->speed;
1725 } else {
1726 pch->avail = 0;
1727 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001728 if (pch->avail) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001729 if (skb_queue_empty(&pch->file.xq) ||
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001730 !pch->had_frag) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001731 if (pch->speed == 0)
1732 nzero++;
1733 else
1734 totspeed += pch->speed;
1735
1736 pch->avail = 2;
1737 ++nfree;
1738 ++totfree;
1739 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001740 if (!pch->had_frag && i < ppp->nxchan)
1741 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001743 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001745 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001746 * Don't start sending this packet unless at least half of
1747 * the channels are free. This gives much better TCP
1748 * performance if we have a lot of channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001749 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001750 if (nfree == 0 || nfree < navail / 2)
1751 return 0; /* can't take now, leave it in xmit_pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001753 /* Do protocol field compression */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001754 p = skb->data;
1755 len = skb->len;
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001756 if (*p == 0 && mp_protocol_compress) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 ++p;
1758 --len;
1759 }
1760
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001761 totlen = len;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001762 nbigger = len % nfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001764 /* skip to the channel after the one we last used
1765 and start at that one */
Domen Puncer81616c52005-09-10 00:27:04 -07001766 list = &ppp->channels;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001767 for (i = 0; i < ppp->nxchan; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001769 if (list == &ppp->channels) {
1770 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 break;
1772 }
1773 }
1774
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001775 /* create a fragment for each channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 bits = B;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001777 while (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001779 if (list == &ppp->channels) {
1780 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 continue;
1782 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001783 pch = list_entry(list, struct channel, clist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 ++i;
1785 if (!pch->avail)
1786 continue;
1787
Paul Mackerras516cd152005-05-12 19:47:12 -04001788 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001789 * Skip this channel if it has a fragment pending already and
1790 * we haven't given a fragment to all of the free channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001791 */
1792 if (pch->avail == 1) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001793 if (nfree > 0)
Paul Mackerras516cd152005-05-12 19:47:12 -04001794 continue;
1795 } else {
Paul Mackerras516cd152005-05-12 19:47:12 -04001796 pch->avail = 1;
1797 }
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* check the channel's mtu and whether it is still attached. */
1800 spin_lock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001801 if (pch->chan == NULL) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001802 /* can't use this channel, it's being deregistered */
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001803 if (pch->speed == 0)
1804 nzero--;
1805 else
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001806 totspeed -= pch->speed;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 spin_unlock_bh(&pch->downl);
1809 pch->avail = 0;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001810 totlen = len;
1811 totfree--;
1812 nfree--;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001813 if (--navail == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 break;
1815 continue;
1816 }
1817
1818 /*
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001819 *if the channel speed is not set divide
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001820 *the packet evenly among the free channels;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001821 *otherwise divide it according to the speed
1822 *of the channel we are going to transmit on
1823 */
David S. Miller886f9fe2009-08-19 13:55:55 -07001824 flen = len;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001825 if (nfree > 0) {
1826 if (pch->speed == 0) {
Ben McKeegan536e00e2010-06-02 23:14:33 +00001827 flen = len/nfree;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001828 if (nbigger > 0) {
1829 flen++;
1830 nbigger--;
1831 }
1832 } else {
1833 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1834 ((totspeed*totfree)/pch->speed)) - hdrlen;
1835 if (nbigger > 0) {
1836 flen += ((totfree - nzero)*pch->speed)/totspeed;
1837 nbigger -= ((totfree - nzero)*pch->speed)/
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001838 totspeed;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001839 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001840 }
Ben McKeegana53a8b52009-07-28 07:43:57 +00001841 nfree--;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001842 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001843
1844 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001845 *check if we are on the last channel or
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001846 *we exceded the length of the data to
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001847 *fragment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 */
Ben McKeegana53a8b52009-07-28 07:43:57 +00001849 if ((nfree <= 0) || (flen > len))
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001850 flen = len;
1851 /*
1852 *it is not worth to tx on slow channels:
1853 *in that case from the resulting flen according to the
1854 *above formula will be equal or less than zero.
1855 *Skip the channel in this case
1856 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001857 if (flen <= 0) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001858 pch->avail = 2;
1859 spin_unlock_bh(&pch->downl);
1860 continue;
1861 }
1862
Henry Wong22e83a22011-09-18 13:41:49 +00001863 /*
1864 * hdrlen includes the 2-byte PPP protocol field, but the
1865 * MTU counts only the payload excluding the protocol field.
1866 * (RFC1661 Section 2)
1867 */
1868 mtu = pch->chan->mtu - (hdrlen - 2);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001869 if (mtu < 4)
1870 mtu = 4;
Paul Mackerras516cd152005-05-12 19:47:12 -04001871 if (flen > mtu)
1872 flen = mtu;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001873 if (flen == len)
1874 bits |= E;
1875 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001876 if (!frag)
Paul Mackerras516cd152005-05-12 19:47:12 -04001877 goto noskb;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001878 q = skb_put(frag, flen + hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001880 /* make the MP header */
Changli Gao96545ae2011-01-06 13:37:36 +00001881 put_unaligned_be16(PPP_MP, q);
Paul Mackerras516cd152005-05-12 19:47:12 -04001882 if (ppp->flags & SC_MP_XSHORTSEQ) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001883 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
Paul Mackerras516cd152005-05-12 19:47:12 -04001884 q[3] = ppp->nxseq;
1885 } else {
1886 q[2] = bits;
1887 q[3] = ppp->nxseq >> 16;
1888 q[4] = ppp->nxseq >> 8;
1889 q[5] = ppp->nxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001891
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001892 memcpy(q + hdrlen, p, flen);
Paul Mackerras516cd152005-05-12 19:47:12 -04001893
1894 /* try to send it down the channel */
1895 chan = pch->chan;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001896 if (!skb_queue_empty(&pch->file.xq) ||
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001897 !chan->ops->start_xmit(chan, frag))
Paul Mackerras516cd152005-05-12 19:47:12 -04001898 skb_queue_tail(&pch->file.xq, frag);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001899 pch->had_frag = 1;
Paul Mackerras516cd152005-05-12 19:47:12 -04001900 p += flen;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001901 len -= flen;
Paul Mackerras516cd152005-05-12 19:47:12 -04001902 ++ppp->nxseq;
1903 bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 spin_unlock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001905 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001906 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 return 1;
1909
1910 noskb:
1911 spin_unlock_bh(&pch->downl);
1912 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001913 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001914 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ++ppp->nxseq;
1916 return 1; /* abandon the frame */
1917}
1918#endif /* CONFIG_PPP_MULTILINK */
1919
Guillaume Nault55454a52016-08-27 22:22:32 +02001920/* Try to send data out on a channel */
1921static void __ppp_channel_push(struct channel *pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 struct sk_buff *skb;
1924 struct ppp *ppp;
1925
1926 spin_lock_bh(&pch->downl);
Joe Perchescd228d52007-11-12 18:07:31 -08001927 if (pch->chan) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001928 while (!skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 skb = skb_dequeue(&pch->file.xq);
1930 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1931 /* put the packet back and try again later */
1932 skb_queue_head(&pch->file.xq, skb);
1933 break;
1934 }
1935 }
1936 } else {
1937 /* channel got deregistered */
1938 skb_queue_purge(&pch->file.xq);
1939 }
1940 spin_unlock_bh(&pch->downl);
1941 /* see if there is anything from the attached unit to be sent */
David S. Millerb03efcf2005-07-08 14:57:23 -07001942 if (skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 ppp = pch->ppp;
Joe Perchescd228d52007-11-12 18:07:31 -08001944 if (ppp)
Guillaume Naultfe3627f2018-03-20 16:49:26 +01001945 __ppp_xmit_process(ppp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947}
1948
Guillaume Nault55454a52016-08-27 22:22:32 +02001949static void ppp_channel_push(struct channel *pch)
1950{
Guillaume Nault6ec6ec32017-08-08 11:43:24 +02001951 read_lock_bh(&pch->upl);
1952 if (pch->ppp) {
1953 (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
1954 __ppp_channel_push(pch);
1955 (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
1956 } else {
1957 __ppp_channel_push(pch);
1958 }
1959 read_unlock_bh(&pch->upl);
Guillaume Nault55454a52016-08-27 22:22:32 +02001960}
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962/*
1963 * Receive-side routines.
1964 */
1965
David S. Millera00eac02010-10-05 01:36:52 -07001966struct ppp_mp_skb_parm {
1967 u32 sequence;
1968 u8 BEbits;
1969};
1970#define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972static inline void
1973ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1974{
1975 ppp_recv_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001976 if (!ppp->closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 ppp_receive_frame(ppp, skb, pch);
1978 else
1979 kfree_skb(skb);
1980 ppp_recv_unlock(ppp);
1981}
1982
1983void
1984ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1985{
1986 struct channel *pch = chan->ppp;
1987 int proto;
1988
Simon Arlottea8420e2010-05-03 10:19:33 +00001989 if (!pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 kfree_skb(skb);
1991 return;
1992 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 read_lock_bh(&pch->upl);
Simon Arlottea8420e2010-05-03 10:19:33 +00001995 if (!pskb_may_pull(skb, 2)) {
1996 kfree_skb(skb);
1997 if (pch->ppp) {
1998 ++pch->ppp->dev->stats.rx_length_errors;
1999 ppp_receive_error(pch->ppp);
2000 }
2001 goto done;
2002 }
2003
2004 proto = PPP_PROTO(skb);
Joe Perchescd228d52007-11-12 18:07:31 -08002005 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /* put it on the channel queue */
2007 skb_queue_tail(&pch->file.rq, skb);
2008 /* drop old frames if queue too long */
Joe Perches8e95a202009-12-03 07:58:21 +00002009 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
2010 (skb = skb_dequeue(&pch->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 kfree_skb(skb);
2012 wake_up_interruptible(&pch->file.rwait);
2013 } else {
2014 ppp_do_recv(pch->ppp, skb, pch);
2015 }
Simon Arlottea8420e2010-05-03 10:19:33 +00002016
2017done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 read_unlock_bh(&pch->upl);
2019}
2020
2021/* Put a 0-length skb in the receive queue as an error indication */
2022void
2023ppp_input_error(struct ppp_channel *chan, int code)
2024{
2025 struct channel *pch = chan->ppp;
2026 struct sk_buff *skb;
2027
Joe Perchescd228d52007-11-12 18:07:31 -08002028 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return;
2030
2031 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002032 if (pch->ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 skb = alloc_skb(0, GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002034 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 skb->len = 0; /* probably unnecessary */
2036 skb->cb[0] = code;
2037 ppp_do_recv(pch->ppp, skb, pch);
2038 }
2039 }
2040 read_unlock_bh(&pch->upl);
2041}
2042
2043/*
2044 * We come in here to process a received frame.
2045 * The receive side of the ppp unit is locked.
2046 */
2047static void
2048ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2049{
Simon Arlottea8420e2010-05-03 10:19:33 +00002050 /* note: a 0-length skb is used as an error indication */
2051 if (skb->len > 0) {
Tom Herbert3dfb0532015-04-20 14:10:05 -07002052 skb_checksum_complete_unset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053#ifdef CONFIG_PPP_MULTILINK
2054 /* XXX do channel-level decompression here */
2055 if (PPP_PROTO(skb) == PPP_MP)
2056 ppp_receive_mp_frame(ppp, skb, pch);
2057 else
2058#endif /* CONFIG_PPP_MULTILINK */
2059 ppp_receive_nonmp_frame(ppp, skb);
Simon Arlottea8420e2010-05-03 10:19:33 +00002060 } else {
2061 kfree_skb(skb);
2062 ppp_receive_error(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066static void
2067ppp_receive_error(struct ppp *ppp)
2068{
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002069 ++ppp->dev->stats.rx_errors;
Joe Perchescd228d52007-11-12 18:07:31 -08002070 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 slhc_toss(ppp->vj);
2072}
2073
2074static void
2075ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2076{
2077 struct sk_buff *ns;
2078 int proto, len, npi;
2079
2080 /*
2081 * Decompress the frame, if compressed.
2082 * Note that some decompressors need to see uncompressed frames
2083 * that come in as well as compressed frames.
2084 */
Joe Perches8e95a202009-12-03 07:58:21 +00002085 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2086 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 skb = ppp_decompress_frame(ppp, skb);
2088
Matt Domschb3f9b922005-11-08 09:40:47 -08002089 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2090 goto err;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 proto = PPP_PROTO(skb);
2093 switch (proto) {
2094 case PPP_VJC_COMP:
2095 /* decompress VJ compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002096 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto err;
2098
Herbert Xu2a38b772007-09-16 16:22:13 -07002099 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /* copy to a new sk_buff with more tailroom */
2101 ns = dev_alloc_skb(skb->len + 128);
Joe Perchescd228d52007-11-12 18:07:31 -08002102 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002103 netdev_err(ppp->dev, "PPP: no memory "
2104 "(VJ decomp)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 goto err;
2106 }
2107 skb_reserve(ns, 2);
2108 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
Eric Dumazet968d7012012-05-18 20:23:00 +00002109 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 skb = ns;
2111 }
Herbert Xue3f749c2006-02-05 20:23:33 -08002112 else
2113 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2116 if (len <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002117 netdev_printk(KERN_DEBUG, ppp->dev,
2118 "PPP: VJ decompression error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 goto err;
2120 }
2121 len += 2;
2122 if (len > skb->len)
2123 skb_put(skb, len - skb->len);
2124 else if (len < skb->len)
2125 skb_trim(skb, len);
2126 proto = PPP_IP;
2127 break;
2128
2129 case PPP_VJC_UNCOMP:
Joe Perchescd228d52007-11-12 18:07:31 -08002130 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 goto err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /* Until we fix the decompressor need to make sure
2134 * data portion is linear.
2135 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002136 if (!pskb_may_pull(skb, skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 goto err;
2138
2139 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002140 netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 goto err;
2142 }
2143 proto = PPP_IP;
2144 break;
2145
2146 case PPP_CCP:
2147 ppp_ccp_peek(ppp, skb, 1);
2148 break;
2149 }
2150
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002151 ++ppp->stats64.rx_packets;
2152 ppp->stats64.rx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 npi = proto_to_npindex(proto);
2155 if (npi < 0) {
2156 /* control or unknown frame - pass it to pppd */
2157 skb_queue_tail(&ppp->file.rq, skb);
2158 /* limit queue length by dropping old frames */
Joe Perches8e95a202009-12-03 07:58:21 +00002159 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2160 (skb = skb_dequeue(&ppp->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 kfree_skb(skb);
2162 /* wake up any process polling or blocking on read */
2163 wake_up_interruptible(&ppp->file.rwait);
2164
2165 } else {
2166 /* network protocol frame - give it to the kernel */
2167
2168#ifdef CONFIG_PPP_FILTER
2169 /* check if the packet passes the pass and active filters */
2170 /* the filter instructions are constructed assuming
2171 a four-byte PPP header on each packet */
Herbert Xu2a38b772007-09-16 16:22:13 -07002172 if (ppp->pass_filter || ppp->active_filter) {
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +00002173 if (skb_unclone(skb, GFP_ATOMIC))
Herbert Xu2a38b772007-09-16 16:22:13 -07002174 goto err;
2175
2176 *skb_push(skb, 2) = 0;
Joe Perches8e95a202009-12-03 07:58:21 +00002177 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002178 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Herbert Xu2a38b772007-09-16 16:22:13 -07002179 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002180 netdev_printk(KERN_DEBUG, ppp->dev,
2181 "PPP: inbound frame "
2182 "not passed\n");
Herbert Xu2a38b772007-09-16 16:22:13 -07002183 kfree_skb(skb);
2184 return;
2185 }
Joe Perches8e95a202009-12-03 07:58:21 +00002186 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002187 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Herbert Xu2a38b772007-09-16 16:22:13 -07002188 ppp->last_recv = jiffies;
2189 __skb_pull(skb, 2);
2190 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#endif /* CONFIG_PPP_FILTER */
Herbert Xu2a38b772007-09-16 16:22:13 -07002192 ppp->last_recv = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Joe Perches8e95a202009-12-03 07:58:21 +00002194 if ((ppp->dev->flags & IFF_UP) == 0 ||
2195 ppp->npmode[npi] != NPMODE_PASS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 kfree_skb(skb);
2197 } else {
Herbert Xucbb042f2006-03-20 22:43:56 -08002198 /* chop off protocol */
2199 skb_pull_rcsum(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 skb->dev = ppp->dev;
2201 skb->protocol = htons(npindex_to_ethertype[npi]);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002202 skb_reset_mac_header(skb);
Guillaume Nault79c441a2015-08-24 11:35:30 +02002203 skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2204 dev_net(ppp->dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207 }
2208 return;
2209
2210 err:
2211 kfree_skb(skb);
2212 ppp_receive_error(ppp);
2213}
2214
2215static struct sk_buff *
2216ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2217{
2218 int proto = PPP_PROTO(skb);
2219 struct sk_buff *ns;
2220 int len;
2221
2222 /* Until we fix all the decompressor's need to make sure
2223 * data portion is linear.
2224 */
2225 if (!pskb_may_pull(skb, skb->len))
2226 goto err;
2227
2228 if (proto == PPP_COMP) {
Konstantin Sharlaimov4b2a8fb2007-06-23 23:05:54 -07002229 int obuff_size;
2230
2231 switch(ppp->rcomp->compress_proto) {
2232 case CI_MPPE:
2233 obuff_size = ppp->mru + PPP_HDRLEN + 1;
2234 break;
2235 default:
2236 obuff_size = ppp->mru + PPP_HDRLEN;
2237 break;
2238 }
2239
2240 ns = dev_alloc_skb(obuff_size);
Joe Perchescd228d52007-11-12 18:07:31 -08002241 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002242 netdev_err(ppp->dev, "ppp_decompress_frame: "
2243 "no memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto err;
2245 }
2246 /* the decompressor still expects the A/C bytes in the hdr */
2247 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
Konstantin Sharlaimov06c7af52007-08-21 00:12:44 -07002248 skb->len + 2, ns->data, obuff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (len < 0) {
2250 /* Pass the compressed frame to pppd as an
2251 error indication. */
2252 if (len == DECOMP_FATALERROR)
2253 ppp->rstate |= SC_DC_FERROR;
2254 kfree_skb(ns);
2255 goto err;
2256 }
2257
Eric Dumazet968d7012012-05-18 20:23:00 +00002258 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 skb = ns;
2260 skb_put(skb, len);
2261 skb_pull(skb, 2); /* pull off the A/C bytes */
2262
2263 } else {
2264 /* Uncompressed frame - pass to decompressor so it
2265 can update its dictionary if necessary. */
2266 if (ppp->rcomp->incomp)
2267 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2268 skb->len + 2);
2269 }
2270
2271 return skb;
2272
2273 err:
2274 ppp->rstate |= SC_DC_ERROR;
2275 ppp_receive_error(ppp);
2276 return skb;
2277}
2278
2279#ifdef CONFIG_PPP_MULTILINK
2280/*
2281 * Receive a multilink frame.
2282 * We put it on the reconstruction queue and then pull off
2283 * as many completed frames as we can.
2284 */
2285static void
2286ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2287{
2288 u32 mask, seq;
Domen Puncer81616c52005-09-10 00:27:04 -07002289 struct channel *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2291
Herbert Xu2a38b772007-09-16 16:22:13 -07002292 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto err; /* no good, throw it away */
2294
2295 /* Decode sequence number and begin/end bits */
2296 if (ppp->flags & SC_MP_SHORTSEQ) {
2297 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2298 mask = 0xfff;
2299 } else {
2300 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2301 mask = 0xffffff;
2302 }
David S. Millera00eac02010-10-05 01:36:52 -07002303 PPP_MP_CB(skb)->BEbits = skb->data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
2305
2306 /*
2307 * Do protocol ID decompression on the first fragment of each packet.
2308 */
David S. Millera00eac02010-10-05 01:36:52 -07002309 if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 *skb_push(skb, 1) = 0;
2311
2312 /*
2313 * Expand sequence number to 32 bits, making it as close
2314 * as possible to ppp->minseq.
2315 */
2316 seq |= ppp->minseq & ~mask;
2317 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2318 seq += mask + 1;
2319 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2320 seq -= mask + 1; /* should never happen */
David S. Millera00eac02010-10-05 01:36:52 -07002321 PPP_MP_CB(skb)->sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 pch->lastseq = seq;
2323
2324 /*
2325 * If this packet comes before the next one we were expecting,
2326 * drop it.
2327 */
2328 if (seq_before(seq, ppp->nextseq)) {
2329 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002330 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 ppp_receive_error(ppp);
2332 return;
2333 }
2334
2335 /*
2336 * Reevaluate minseq, the minimum over all channels of the
2337 * last sequence number received on each channel. Because of
2338 * the increasing sequence number rule, we know that any fragment
2339 * before `minseq' which hasn't arrived is never going to arrive.
2340 * The list of channels can't change because we have the receive
2341 * side of the ppp unit locked.
2342 */
Domen Puncer81616c52005-09-10 00:27:04 -07002343 list_for_each_entry(ch, &ppp->channels, clist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (seq_before(ch->lastseq, seq))
2345 seq = ch->lastseq;
2346 }
2347 if (seq_before(ppp->minseq, seq))
2348 ppp->minseq = seq;
2349
2350 /* Put the fragment on the reconstruction queue */
2351 ppp_mp_insert(ppp, skb);
2352
2353 /* If the queue is getting long, don't wait any longer for packets
2354 before the start of the queue. */
David S. Miller38ce7c72008-09-23 01:17:18 -07002355 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
stephen hemmingerc6b20d92010-06-01 06:05:46 +00002356 struct sk_buff *mskb = skb_peek(&ppp->mrq);
David S. Millera00eac02010-10-05 01:36:52 -07002357 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2358 ppp->minseq = PPP_MP_CB(mskb)->sequence;
David S. Miller38ce7c72008-09-23 01:17:18 -07002359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 /* Pull completed packets off the queue and receive them. */
Ben McKeegan82b3cc12009-11-16 03:44:25 +00002362 while ((skb = ppp_mp_reconstruct(ppp))) {
2363 if (pskb_may_pull(skb, 2))
2364 ppp_receive_nonmp_frame(ppp, skb);
2365 else {
2366 ++ppp->dev->stats.rx_length_errors;
2367 kfree_skb(skb);
2368 ppp_receive_error(ppp);
2369 }
2370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 return;
2373
2374 err:
2375 kfree_skb(skb);
2376 ppp_receive_error(ppp);
2377}
2378
2379/*
2380 * Insert a fragment on the MP reconstruction queue.
2381 * The queue is ordered by increasing sequence number.
2382 */
2383static void
2384ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2385{
2386 struct sk_buff *p;
2387 struct sk_buff_head *list = &ppp->mrq;
David S. Millera00eac02010-10-05 01:36:52 -07002388 u32 seq = PPP_MP_CB(skb)->sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /* N.B. we don't need to lock the list lock because we have the
2391 ppp unit receive-side lock. */
David S. Miller13c98212008-10-09 16:40:29 -07002392 skb_queue_walk(list, p) {
David S. Millera00eac02010-10-05 01:36:52 -07002393 if (seq_before(seq, PPP_MP_CB(p)->sequence))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 break;
David S. Miller13c98212008-10-09 16:40:29 -07002395 }
David S. Miller43f59c82008-09-21 21:28:51 -07002396 __skb_queue_before(list, p, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399/*
2400 * Reconstruct a packet from the MP fragment queue.
2401 * We go through increasing sequence numbers until we find a
2402 * complete packet, or we get to the sequence number for a fragment
2403 * which hasn't arrived but might still do so.
2404 */
Stephen Hemminger3c582b32008-01-23 20:54:07 -08002405static struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406ppp_mp_reconstruct(struct ppp *ppp)
2407{
2408 u32 seq = ppp->nextseq;
2409 u32 minseq = ppp->minseq;
2410 struct sk_buff_head *list = &ppp->mrq;
David S. Millerd52344a2011-01-20 22:52:05 -08002411 struct sk_buff *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 struct sk_buff *head, *tail;
2413 struct sk_buff *skb = NULL;
2414 int lost = 0, len = 0;
2415
2416 if (ppp->mrru == 0) /* do nothing until mrru is set */
2417 return NULL;
2418 head = list->next;
2419 tail = NULL;
David S. Millerd52344a2011-01-20 22:52:05 -08002420 skb_queue_walk_safe(list, p, tmp) {
2421 again:
David S. Millera00eac02010-10-05 01:36:52 -07002422 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /* this can't happen, anyway ignore the skb */
David S. Millerb48f8c22011-01-20 22:44:36 -08002424 netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2425 "seq %u < %u\n",
2426 PPP_MP_CB(p)->sequence, seq);
David S. Millerd52344a2011-01-20 22:52:05 -08002427 __skb_unlink(p, list);
2428 kfree_skb(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 continue;
2430 }
David S. Millera00eac02010-10-05 01:36:52 -07002431 if (PPP_MP_CB(p)->sequence != seq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002432 u32 oldseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* Fragment `seq' is missing. If it is after
2434 minseq, it might arrive later, so stop here. */
2435 if (seq_after(seq, minseq))
2436 break;
2437 /* Fragment `seq' is lost, keep going. */
2438 lost = 1;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002439 oldseq = seq;
David S. Millera00eac02010-10-05 01:36:52 -07002440 seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2441 minseq + 1: PPP_MP_CB(p)->sequence;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002442
2443 if (ppp->debug & 1)
2444 netdev_printk(KERN_DEBUG, ppp->dev,
2445 "lost frag %u..%u\n",
2446 oldseq, seq-1);
2447
David S. Millerd52344a2011-01-20 22:52:05 -08002448 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450
2451 /*
2452 * At this point we know that all the fragments from
2453 * ppp->nextseq to seq are either present or lost.
2454 * Also, there are no complete packets in the queue
2455 * that have no missing fragments and end before this
2456 * fragment.
2457 */
2458
2459 /* B bit set indicates this fragment starts a packet */
David S. Millera00eac02010-10-05 01:36:52 -07002460 if (PPP_MP_CB(p)->BEbits & B) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 head = p;
2462 lost = 0;
2463 len = 0;
2464 }
2465
2466 len += p->len;
2467
2468 /* Got a complete packet yet? */
David S. Millera00eac02010-10-05 01:36:52 -07002469 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2470 (PPP_MP_CB(head)->BEbits & B)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (len > ppp->mrru + 2) {
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002472 ++ppp->dev->stats.rx_length_errors;
David S. Millerb48f8c22011-01-20 22:44:36 -08002473 netdev_printk(KERN_DEBUG, ppp->dev,
2474 "PPP: reconstructed packet"
2475 " is too long (%d)\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 } else {
2477 tail = p;
2478 break;
2479 }
2480 ppp->nextseq = seq + 1;
2481 }
2482
2483 /*
2484 * If this is the ending fragment of a packet,
2485 * and we haven't found a complete valid packet yet,
2486 * we can discard up to and including this fragment.
2487 */
David S. Millerd52344a2011-01-20 22:52:05 -08002488 if (PPP_MP_CB(p)->BEbits & E) {
2489 struct sk_buff *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
David S. Millerd52344a2011-01-20 22:52:05 -08002491 skb_queue_reverse_walk_from_safe(list, p, tmp2) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002492 if (ppp->debug & 1)
2493 netdev_printk(KERN_DEBUG, ppp->dev,
2494 "discarding frag %u\n",
2495 PPP_MP_CB(p)->sequence);
David S. Millerd52344a2011-01-20 22:52:05 -08002496 __skb_unlink(p, list);
2497 kfree_skb(p);
2498 }
2499 head = skb_peek(list);
2500 if (!head)
2501 break;
2502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 ++seq;
2504 }
2505
2506 /* If we have a complete packet, copy it all into one skb. */
2507 if (tail != NULL) {
2508 /* If we have discarded any fragments,
2509 signal a receive error. */
David S. Millera00eac02010-10-05 01:36:52 -07002510 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002511 skb_queue_walk_safe(list, p, tmp) {
2512 if (p == head)
2513 break;
2514 if (ppp->debug & 1)
2515 netdev_printk(KERN_DEBUG, ppp->dev,
2516 "discarding frag %u\n",
2517 PPP_MP_CB(p)->sequence);
2518 __skb_unlink(p, list);
2519 kfree_skb(p);
2520 }
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002523 netdev_printk(KERN_DEBUG, ppp->dev,
2524 " missed pkts %u..%u\n",
2525 ppp->nextseq,
2526 PPP_MP_CB(head)->sequence-1);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002527 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 ppp_receive_error(ppp);
2529 }
2530
David S. Miller212bfb92011-01-20 22:46:07 -08002531 skb = head;
2532 if (head != tail) {
2533 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2534 p = skb_queue_next(list, head);
2535 __skb_unlink(skb, list);
2536 skb_queue_walk_from_safe(list, p, tmp) {
2537 __skb_unlink(p, list);
2538 *fragpp = p;
2539 p->next = NULL;
2540 fragpp = &p->next;
2541
2542 skb->len += p->len;
2543 skb->data_len += p->len;
Eric Dumazet19c6c8f2012-02-13 04:23:24 +00002544 skb->truesize += p->truesize;
David S. Miller212bfb92011-01-20 22:46:07 -08002545
2546 if (p == tail)
2547 break;
2548 }
2549 } else {
2550 __skb_unlink(skb, list);
2551 }
2552
David S. Millera00eac02010-10-05 01:36:52 -07002553 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555
2556 return skb;
2557}
2558#endif /* CONFIG_PPP_MULTILINK */
2559
2560/*
2561 * Channel interface.
2562 */
2563
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002564/* Create a new, unattached ppp channel. */
2565int ppp_register_channel(struct ppp_channel *chan)
2566{
2567 return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2568}
2569
2570/* Create a new, unattached ppp channel for specified net. */
2571int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 struct channel *pch;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002574 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Panagiotis Issarisd4274b52006-08-15 16:01:07 -07002576 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -08002577 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return -ENOMEM;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002579
2580 pn = ppp_pernet(net);
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 pch->ppp = NULL;
2583 pch->chan = chan;
Guillaume Nault1f461dc2016-03-23 16:38:55 +01002584 pch->chan_net = get_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 chan->ppp = pch;
2586 init_ppp_file(&pch->file, CHANNEL);
2587 pch->file.hdrlen = chan->hdrlen;
2588#ifdef CONFIG_PPP_MULTILINK
2589 pch->lastseq = -1;
2590#endif /* CONFIG_PPP_MULTILINK */
2591 init_rwsem(&pch->chan_sem);
2592 spin_lock_init(&pch->downl);
2593 rwlock_init(&pch->upl);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002594
2595 spin_lock_bh(&pn->all_channels_lock);
2596 pch->file.index = ++pn->last_channel_index;
2597 list_add(&pch->list, &pn->new_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 atomic_inc(&channel_count);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002599 spin_unlock_bh(&pn->all_channels_lock);
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return 0;
2602}
2603
2604/*
2605 * Return the index of a channel.
2606 */
2607int ppp_channel_index(struct ppp_channel *chan)
2608{
2609 struct channel *pch = chan->ppp;
2610
Joe Perchescd228d52007-11-12 18:07:31 -08002611 if (pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return pch->file.index;
2613 return -1;
2614}
2615
2616/*
2617 * Return the PPP unit number to which a channel is connected.
2618 */
2619int ppp_unit_number(struct ppp_channel *chan)
2620{
2621 struct channel *pch = chan->ppp;
2622 int unit = -1;
2623
Joe Perchescd228d52007-11-12 18:07:31 -08002624 if (pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002626 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 unit = pch->ppp->file.index;
2628 read_unlock_bh(&pch->upl);
2629 }
2630 return unit;
2631}
2632
2633/*
James Chapman63f96072010-04-02 06:18:39 +00002634 * Return the PPP device interface name of a channel.
2635 */
2636char *ppp_dev_name(struct ppp_channel *chan)
2637{
2638 struct channel *pch = chan->ppp;
2639 char *name = NULL;
2640
2641 if (pch) {
2642 read_lock_bh(&pch->upl);
2643 if (pch->ppp && pch->ppp->dev)
2644 name = pch->ppp->dev->name;
2645 read_unlock_bh(&pch->upl);
2646 }
2647 return name;
2648}
2649
2650
2651/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 * Disconnect a channel from the generic layer.
2653 * This must be called in process context.
2654 */
2655void
2656ppp_unregister_channel(struct ppp_channel *chan)
2657{
2658 struct channel *pch = chan->ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002659 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Joe Perchescd228d52007-11-12 18:07:31 -08002661 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return; /* should never happen */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 chan->ppp = NULL;
2665
2666 /*
2667 * This ensures that we have returned from any calls into the
2668 * the channel's start_xmit or ioctl routine before we proceed.
2669 */
2670 down_write(&pch->chan_sem);
2671 spin_lock_bh(&pch->downl);
2672 pch->chan = NULL;
2673 spin_unlock_bh(&pch->downl);
2674 up_write(&pch->chan_sem);
2675 ppp_disconnect_channel(pch);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002676
2677 pn = ppp_pernet(pch->chan_net);
2678 spin_lock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 list_del(&pch->list);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002680 spin_unlock_bh(&pn->all_channels_lock);
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 pch->file.dead = 1;
2683 wake_up_interruptible(&pch->file.rwait);
2684 if (atomic_dec_and_test(&pch->file.refcnt))
2685 ppp_destroy_channel(pch);
2686}
2687
2688/*
2689 * Callback from a channel when it can accept more to transmit.
2690 * This should be called at BH/softirq level, not interrupt level.
2691 */
2692void
2693ppp_output_wakeup(struct ppp_channel *chan)
2694{
2695 struct channel *pch = chan->ppp;
2696
Joe Perchescd228d52007-11-12 18:07:31 -08002697 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return;
2699 ppp_channel_push(pch);
2700}
2701
2702/*
2703 * Compression control.
2704 */
2705
2706/* Process the PPPIOCSCOMPRESS ioctl. */
2707static int
2708ppp_set_compress(struct ppp *ppp, unsigned long arg)
2709{
2710 int err;
2711 struct compressor *cp, *ocomp;
2712 struct ppp_option_data data;
2713 void *state, *ostate;
2714 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2715
2716 err = -EFAULT;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002717 if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 goto out;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002719 if (data.length > CCP_MAX_OPTION_LENGTH)
2720 goto out;
2721 if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
2722 goto out;
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 err = -EINVAL;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002725 if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 goto out;
2727
Johannes Berga65e5d72008-07-09 10:28:38 +02002728 cp = try_then_request_module(
2729 find_compressor(ccp_option[0]),
2730 "ppp-compress-%d", ccp_option[0]);
Joe Perchescd228d52007-11-12 18:07:31 -08002731 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 goto out;
2733
2734 err = -ENOBUFS;
2735 if (data.transmit) {
2736 state = cp->comp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002737 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 ppp_xmit_lock(ppp);
2739 ppp->xstate &= ~SC_COMP_RUN;
2740 ocomp = ppp->xcomp;
2741 ostate = ppp->xc_state;
2742 ppp->xcomp = cp;
2743 ppp->xc_state = state;
2744 ppp_xmit_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002745 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ocomp->comp_free(ostate);
2747 module_put(ocomp->owner);
2748 }
2749 err = 0;
2750 } else
2751 module_put(cp->owner);
2752
2753 } else {
2754 state = cp->decomp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002755 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 ppp_recv_lock(ppp);
2757 ppp->rstate &= ~SC_DECOMP_RUN;
2758 ocomp = ppp->rcomp;
2759 ostate = ppp->rc_state;
2760 ppp->rcomp = cp;
2761 ppp->rc_state = state;
2762 ppp_recv_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002763 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 ocomp->decomp_free(ostate);
2765 module_put(ocomp->owner);
2766 }
2767 err = 0;
2768 } else
2769 module_put(cp->owner);
2770 }
2771
2772 out:
2773 return err;
2774}
2775
2776/*
2777 * Look at a CCP packet and update our state accordingly.
2778 * We assume the caller has the xmit or recv path locked.
2779 */
2780static void
2781ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2782{
2783 unsigned char *dp;
2784 int len;
2785
2786 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2787 return; /* no header */
2788 dp = skb->data + 2;
2789
2790 switch (CCP_CODE(dp)) {
2791 case CCP_CONFREQ:
2792
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002793 /* A ConfReq starts negotiation of compression
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * in one direction of transmission,
2795 * and hence brings it down...but which way?
2796 *
2797 * Remember:
2798 * A ConfReq indicates what the sender would like to receive
2799 */
2800 if(inbound)
2801 /* He is proposing what I should send */
2802 ppp->xstate &= ~SC_COMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002803 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 /* I am proposing to what he should send */
2805 ppp->rstate &= ~SC_DECOMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 case CCP_TERMREQ:
2810 case CCP_TERMACK:
2811 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002812 * CCP is going down, both directions of transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 */
2814 ppp->rstate &= ~SC_DECOMP_RUN;
2815 ppp->xstate &= ~SC_COMP_RUN;
2816 break;
2817
2818 case CCP_CONFACK:
2819 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2820 break;
2821 len = CCP_LENGTH(dp);
2822 if (!pskb_may_pull(skb, len + 2))
2823 return; /* too short */
2824 dp += CCP_HDRLEN;
2825 len -= CCP_HDRLEN;
2826 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2827 break;
2828 if (inbound) {
2829 /* we will start receiving compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002830 if (!ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 break;
2832 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2833 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2834 ppp->rstate |= SC_DECOMP_RUN;
2835 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2836 }
2837 } else {
2838 /* we will soon start sending compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002839 if (!ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 break;
2841 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2842 ppp->file.index, 0, ppp->debug))
2843 ppp->xstate |= SC_COMP_RUN;
2844 }
2845 break;
2846
2847 case CCP_RESETACK:
2848 /* reset the [de]compressor */
2849 if ((ppp->flags & SC_CCP_UP) == 0)
2850 break;
2851 if (inbound) {
2852 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2853 ppp->rcomp->decomp_reset(ppp->rc_state);
2854 ppp->rstate &= ~SC_DC_ERROR;
2855 }
2856 } else {
2857 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2858 ppp->xcomp->comp_reset(ppp->xc_state);
2859 }
2860 break;
2861 }
2862}
2863
2864/* Free up compression resources. */
2865static void
2866ppp_ccp_closed(struct ppp *ppp)
2867{
2868 void *xstate, *rstate;
2869 struct compressor *xcomp, *rcomp;
2870
2871 ppp_lock(ppp);
2872 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2873 ppp->xstate = 0;
2874 xcomp = ppp->xcomp;
2875 xstate = ppp->xc_state;
2876 ppp->xc_state = NULL;
2877 ppp->rstate = 0;
2878 rcomp = ppp->rcomp;
2879 rstate = ppp->rc_state;
2880 ppp->rc_state = NULL;
2881 ppp_unlock(ppp);
2882
2883 if (xstate) {
2884 xcomp->comp_free(xstate);
2885 module_put(xcomp->owner);
2886 }
2887 if (rstate) {
2888 rcomp->decomp_free(rstate);
2889 module_put(rcomp->owner);
2890 }
2891}
2892
2893/* List of compressors. */
2894static LIST_HEAD(compressor_list);
2895static DEFINE_SPINLOCK(compressor_list_lock);
2896
2897struct compressor_entry {
2898 struct list_head list;
2899 struct compressor *comp;
2900};
2901
2902static struct compressor_entry *
2903find_comp_entry(int proto)
2904{
2905 struct compressor_entry *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Domen Puncer81616c52005-09-10 00:27:04 -07002907 list_for_each_entry(ce, &compressor_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (ce->comp->compress_proto == proto)
2909 return ce;
2910 }
2911 return NULL;
2912}
2913
2914/* Register a compressor */
2915int
2916ppp_register_compressor(struct compressor *cp)
2917{
2918 struct compressor_entry *ce;
2919 int ret;
2920 spin_lock(&compressor_list_lock);
2921 ret = -EEXIST;
Joe Perchescd228d52007-11-12 18:07:31 -08002922 if (find_comp_entry(cp->compress_proto))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 goto out;
2924 ret = -ENOMEM;
2925 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002926 if (!ce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 goto out;
2928 ret = 0;
2929 ce->comp = cp;
2930 list_add(&ce->list, &compressor_list);
2931 out:
2932 spin_unlock(&compressor_list_lock);
2933 return ret;
2934}
2935
2936/* Unregister a compressor */
2937void
2938ppp_unregister_compressor(struct compressor *cp)
2939{
2940 struct compressor_entry *ce;
2941
2942 spin_lock(&compressor_list_lock);
2943 ce = find_comp_entry(cp->compress_proto);
Joe Perchescd228d52007-11-12 18:07:31 -08002944 if (ce && ce->comp == cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 list_del(&ce->list);
2946 kfree(ce);
2947 }
2948 spin_unlock(&compressor_list_lock);
2949}
2950
2951/* Find a compressor. */
2952static struct compressor *
2953find_compressor(int type)
2954{
2955 struct compressor_entry *ce;
2956 struct compressor *cp = NULL;
2957
2958 spin_lock(&compressor_list_lock);
2959 ce = find_comp_entry(type);
Joe Perchescd228d52007-11-12 18:07:31 -08002960 if (ce) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 cp = ce->comp;
2962 if (!try_module_get(cp->owner))
2963 cp = NULL;
2964 }
2965 spin_unlock(&compressor_list_lock);
2966 return cp;
2967}
2968
2969/*
2970 * Miscelleneous stuff.
2971 */
2972
2973static void
2974ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2975{
2976 struct slcompress *vj = ppp->vj;
2977
2978 memset(st, 0, sizeof(*st));
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002979 st->p.ppp_ipackets = ppp->stats64.rx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002980 st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002981 st->p.ppp_ibytes = ppp->stats64.rx_bytes;
2982 st->p.ppp_opackets = ppp->stats64.tx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002983 st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002984 st->p.ppp_obytes = ppp->stats64.tx_bytes;
Joe Perchescd228d52007-11-12 18:07:31 -08002985 if (!vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return;
2987 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2988 st->vj.vjs_compressed = vj->sls_o_compressed;
2989 st->vj.vjs_searches = vj->sls_o_searches;
2990 st->vj.vjs_misses = vj->sls_o_misses;
2991 st->vj.vjs_errorin = vj->sls_i_error;
2992 st->vj.vjs_tossed = vj->sls_i_tossed;
2993 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2994 st->vj.vjs_compressedin = vj->sls_i_compressed;
2995}
2996
2997/*
2998 * Stuff for handling the lists of ppp units and channels
2999 * and for initialization.
3000 */
3001
3002/*
3003 * Create a new ppp interface unit. Fails if it can't allocate memory
3004 * or if there is already a unit with the requested number.
3005 * unit == -1 means allocate a new number.
3006 */
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003007static int ppp_create_interface(struct net *net, struct file *file, int *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003009 struct ppp_config conf = {
3010 .file = file,
3011 .unit = *unit,
Guillaume Nault96d934c2016-04-28 17:55:30 +02003012 .ifname_is_set = false,
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003013 };
3014 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct ppp *ppp;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003016 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Guillaume Nault69d97282015-12-11 19:54:52 +01003018 dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003019 if (!dev) {
3020 err = -ENOMEM;
3021 goto err;
3022 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003023 dev_net_set(dev, net);
Guillaume Nault96d934c2016-04-28 17:55:30 +02003024 dev->rtnl_link_ops = &ppp_link_ops;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003025
Guillaume Nault58a89ec2015-09-24 12:54:01 +02003026 rtnl_lock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003027
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003028 err = ppp_dev_configure(net, dev, &conf);
3029 if (err < 0)
3030 goto err_dev;
3031 ppp = netdev_priv(dev);
3032 *unit = ppp->file.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Guillaume Nault58a89ec2015-09-24 12:54:01 +02003034 rtnl_unlock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003035
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003038err_dev:
Guillaume Nault6faac632016-03-07 19:36:44 +01003039 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 free_netdev(dev);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003041err:
3042 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
3045/*
3046 * Initialize a ppp_file structure.
3047 */
3048static void
3049init_ppp_file(struct ppp_file *pf, int kind)
3050{
3051 pf->kind = kind;
3052 skb_queue_head_init(&pf->xq);
3053 skb_queue_head_init(&pf->rq);
3054 atomic_set(&pf->refcnt, 1);
3055 init_waitqueue_head(&pf->rwait);
3056}
3057
3058/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 * Free the memory used by a ppp unit. This is only called once
3060 * there are no channels connected to the unit and no file structs
3061 * that reference the unit.
3062 */
3063static void ppp_destroy_interface(struct ppp *ppp)
3064{
3065 atomic_dec(&ppp_unit_count);
3066
3067 if (!ppp->file.dead || ppp->n_channels) {
3068 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003069 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3070 "but dead=%d n_channels=%d !\n",
3071 ppp, ppp->file.dead, ppp->n_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return;
3073 }
3074
3075 ppp_ccp_closed(ppp);
3076 if (ppp->vj) {
3077 slhc_free(ppp->vj);
3078 ppp->vj = NULL;
3079 }
3080 skb_queue_purge(&ppp->file.xq);
3081 skb_queue_purge(&ppp->file.rq);
3082#ifdef CONFIG_PPP_MULTILINK
3083 skb_queue_purge(&ppp->mrq);
3084#endif /* CONFIG_PPP_MULTILINK */
3085#ifdef CONFIG_PPP_FILTER
Daniel Borkmann568f1942014-03-28 18:58:23 +01003086 if (ppp->pass_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003087 bpf_prog_destroy(ppp->pass_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003088 ppp->pass_filter = NULL;
3089 }
3090
3091 if (ppp->active_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003092 bpf_prog_destroy(ppp->active_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003093 ppp->active_filter = NULL;
3094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095#endif /* CONFIG_PPP_FILTER */
3096
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00003097 kfree_skb(ppp->xmit_pending);
Gao Feng3b25bfc2017-07-17 18:34:42 +08003098 free_percpu(ppp->xmit_recursion);
G. Liakhovetski165de5b2007-03-25 19:04:09 -07003099
James Chapman739840d2008-12-17 12:02:16 +00003100 free_netdev(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
3103/*
3104 * Locate an existing ppp unit.
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08003105 * The caller should have locked the all_ppp_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 */
3107static struct ppp *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003108ppp_find_unit(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003110 return unit_find(&pn->units_idr, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
3113/*
3114 * Locate an existing ppp channel.
3115 * The caller should have locked the all_channels_lock.
3116 * First we look in the new_channels list, then in the
3117 * all_channels list. If found in the new_channels list,
3118 * we move it to the all_channels list. This is for speed
3119 * when we have a lot of channels in use.
3120 */
3121static struct channel *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003122ppp_find_channel(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003126 list_for_each_entry(pch, &pn->new_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (pch->file.index == unit) {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003128 list_move(&pch->list, &pn->all_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return pch;
3130 }
3131 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003132
3133 list_for_each_entry(pch, &pn->all_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (pch->file.index == unit)
3135 return pch;
3136 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return NULL;
3139}
3140
3141/*
3142 * Connect a PPP channel to a PPP interface unit.
3143 */
3144static int
3145ppp_connect_channel(struct channel *pch, int unit)
3146{
3147 struct ppp *ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003148 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 int ret = -ENXIO;
3150 int hdrlen;
3151
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003152 pn = ppp_pernet(pch->chan_net);
3153
3154 mutex_lock(&pn->all_ppp_mutex);
3155 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -08003156 if (!ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 goto out;
3158 write_lock_bh(&pch->upl);
3159 ret = -EINVAL;
Joe Perchescd228d52007-11-12 18:07:31 -08003160 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 goto outl;
3162
3163 ppp_lock(ppp);
Guillaume Nault3741c8f2018-03-02 18:41:16 +01003164 spin_lock_bh(&pch->downl);
3165 if (!pch->chan) {
3166 /* Don't connect unregistered channels */
3167 spin_unlock_bh(&pch->downl);
3168 ppp_unlock(ppp);
3169 ret = -ENOTCONN;
3170 goto outl;
3171 }
3172 spin_unlock_bh(&pch->downl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (pch->file.hdrlen > ppp->file.hdrlen)
3174 ppp->file.hdrlen = pch->file.hdrlen;
3175 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
James Chapman739840d2008-12-17 12:02:16 +00003176 if (hdrlen > ppp->dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 ppp->dev->hard_header_len = hdrlen;
3178 list_add_tail(&pch->clist, &ppp->channels);
3179 ++ppp->n_channels;
3180 pch->ppp = ppp;
3181 atomic_inc(&ppp->file.refcnt);
3182 ppp_unlock(ppp);
3183 ret = 0;
3184
3185 outl:
3186 write_unlock_bh(&pch->upl);
3187 out:
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003188 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return ret;
3190}
3191
3192/*
3193 * Disconnect a channel from its ppp unit.
3194 */
3195static int
3196ppp_disconnect_channel(struct channel *pch)
3197{
3198 struct ppp *ppp;
3199 int err = -EINVAL;
3200
3201 write_lock_bh(&pch->upl);
3202 ppp = pch->ppp;
3203 pch->ppp = NULL;
3204 write_unlock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08003205 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 /* remove it from the ppp unit's list */
3207 ppp_lock(ppp);
3208 list_del(&pch->clist);
3209 if (--ppp->n_channels == 0)
3210 wake_up_interruptible(&ppp->file.rwait);
3211 ppp_unlock(ppp);
3212 if (atomic_dec_and_test(&ppp->file.refcnt))
3213 ppp_destroy_interface(ppp);
3214 err = 0;
3215 }
3216 return err;
3217}
3218
3219/*
3220 * Free up the resources used by a ppp channel.
3221 */
3222static void ppp_destroy_channel(struct channel *pch)
3223{
WANG Cong205e1e22016-07-05 22:12:36 -07003224 put_net(pch->chan_net);
3225 pch->chan_net = NULL;
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 atomic_dec(&channel_count);
3228
3229 if (!pch->file.dead) {
3230 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003231 pr_err("ppp: destroying undead channel %p !\n", pch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return;
3233 }
3234 skb_queue_purge(&pch->file.xq);
3235 skb_queue_purge(&pch->file.rq);
3236 kfree(pch);
3237}
3238
3239static void __exit ppp_cleanup(void)
3240{
3241 /* should never happen */
3242 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
David S. Millerb48f8c22011-01-20 22:44:36 -08003243 pr_err("PPP: removing module but units remain!\n");
Guillaume Nault96d934c2016-04-28 17:55:30 +02003244 rtnl_link_unregister(&ppp_link_ops);
Akinobu Mita68fc4fa2007-07-19 01:47:50 -07003245 unregister_chrdev(PPP_MAJOR, "ppp");
Greg Kroah-Hartman9a6a2a52006-09-12 17:00:10 +02003246 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08003247 class_destroy(ppp_class);
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00003248 unregister_pernet_device(&ppp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
3251/*
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003252 * Units handling. Caller must protect concurrent access
3253 * by holding all_ppp_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Cyrill Gorcunovbcc70bb2010-11-23 11:43:44 +00003256/* associate pointer with specified number */
3257static int unit_set(struct idr *p, void *ptr, int n)
3258{
3259 int unit;
3260
Tejun Heo2fa532c2013-02-27 17:04:36 -08003261 unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3262 if (unit == -ENOSPC)
3263 unit = -EINVAL;
Cyrill Gorcunov85997572009-01-12 22:11:56 -08003264 return unit;
3265}
3266
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003267/* get new free unit number and associate pointer with it */
3268static int unit_get(struct idr *p, void *ptr)
3269{
Tejun Heo2fa532c2013-02-27 17:04:36 -08003270 return idr_alloc(p, ptr, 0, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003273/* put unit number back to a pool */
3274static void unit_put(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003276 idr_remove(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003279/* get pointer associated with the number */
3280static void *unit_find(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003282 return idr_find(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285/* Module/initialization stuff */
3286
3287module_init(ppp_init);
3288module_exit(ppp_cleanup);
3289
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003290EXPORT_SYMBOL(ppp_register_net_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291EXPORT_SYMBOL(ppp_register_channel);
3292EXPORT_SYMBOL(ppp_unregister_channel);
3293EXPORT_SYMBOL(ppp_channel_index);
3294EXPORT_SYMBOL(ppp_unit_number);
James Chapman63f96072010-04-02 06:18:39 +00003295EXPORT_SYMBOL(ppp_dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296EXPORT_SYMBOL(ppp_input);
3297EXPORT_SYMBOL(ppp_input_error);
3298EXPORT_SYMBOL(ppp_output_wakeup);
3299EXPORT_SYMBOL(ppp_register_compressor);
3300EXPORT_SYMBOL(ppp_unregister_compressor);
3301MODULE_LICENSE("GPL");
Kay Sievers578454f2010-05-20 18:07:20 +02003302MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
Guillaume Nault96d934c2016-04-28 17:55:30 +02003303MODULE_ALIAS_RTNL_LINK("ppp");
Kay Sievers578454f2010-05-20 18:07:20 +02003304MODULE_ALIAS("devname:ppp");