blob: 8479c130fe2ecef1b54c54cd67c7b77504e61e74 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010027#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kmod.h>
29#include <linux/init.h>
30#include <linux/list.h>
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -080031#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/netdevice.h>
33#include <linux/poll.h>
34#include <linux/ppp_defs.h>
35#include <linux/filter.h>
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +000036#include <linux/ppp-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/ppp_channel.h>
38#include <linux/ppp-comp.h>
39#include <linux/skbuff.h>
40#include <linux/rtnetlink.h>
41#include <linux/if_arp.h>
42#include <linux/ip.h>
43#include <linux/tcp.h>
44#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/rwsem.h>
46#include <linux/stddef.h>
47#include <linux/device.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080048#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Guillaume Nault96d934c2016-04-28 17:55:30 +020050#include <linux/file.h>
Changli Gao96545ae2011-01-06 13:37:36 +000051#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/slhc_vj.h>
Arun Sharma600634972011-07-26 16:09:06 -070053#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Cyrill Gorcunov273ec512009-01-21 15:55:35 -080055#include <linux/nsproxy.h>
56#include <net/net_namespace.h>
57#include <net/netns/generic.h>
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define PPP_VERSION "2.4.2"
60
61/*
62 * Network protocols we support.
63 */
64#define NP_IP 0 /* Internet Protocol V4 */
65#define NP_IPV6 1 /* Internet Protocol V6 */
66#define NP_IPX 2 /* IPX protocol */
67#define NP_AT 3 /* Appletalk protocol */
68#define NP_MPLS_UC 4 /* MPLS unicast */
69#define NP_MPLS_MC 5 /* MPLS multicast */
70#define NUM_NP 6 /* Number of NPs. */
71
72#define MPHDRLEN 6 /* multilink protocol header length */
73#define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * An instance of /dev/ppp can be associated with either a ppp
77 * interface unit or a ppp channel. In both cases, file->private_data
78 * points to one of these.
79 */
80struct ppp_file {
81 enum {
82 INTERFACE=1, CHANNEL
83 } kind;
84 struct sk_buff_head xq; /* pppd transmit queue */
85 struct sk_buff_head rq; /* receive queue for pppd */
86 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
87 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
88 int hdrlen; /* space to leave for headers */
89 int index; /* interface unit / channel number */
90 int dead; /* unit/channel has been shut down */
91};
92
Robert P. J. Dayb385a142007-02-10 01:46:25 -080093#define PF_TO_X(pf, X) container_of(pf, X, file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
96#define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +000099 * Data structure to hold primary network stats for which
100 * we want to use 64 bit storage. Other network stats
101 * are stored in dev->stats of the ppp strucute.
102 */
103struct ppp_link_stats {
104 u64 rx_packets;
105 u64 tx_packets;
106 u64 rx_bytes;
107 u64 tx_bytes;
108};
109
110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * Data structure describing one ppp unit.
112 * A ppp unit corresponds to a ppp network interface device
113 * and represents a multilink bundle.
114 * It can have 0 or more ppp channels connected to it.
115 */
116struct ppp {
117 struct ppp_file file; /* stuff for read/write/poll 0 */
118 struct file *owner; /* file that owns this unit 48 */
119 struct list_head channels; /* list of attached channels 4c */
120 int n_channels; /* how many channels are attached 54 */
121 spinlock_t rlock; /* lock for receive side 58 */
122 spinlock_t wlock; /* lock for transmit side 5c */
123 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 */
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300208static unsigned int ppp_net_id __read_mostly;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800209struct ppp_net {
210 /* units to ppp mapping */
211 struct idr units_idr;
212
213 /*
214 * all_ppp_mutex protects the units_idr mapping.
215 * It also ensures that finding a ppp unit in the units_idr
216 * map and updating its file.refcnt field is atomic.
217 */
218 struct mutex all_ppp_mutex;
219
220 /* channels */
221 struct list_head all_channels;
222 struct list_head new_channels;
223 int last_channel_index;
224
225 /*
226 * all_channels_lock protects all_channels and
227 * last_channel_index, and the atomicity of find
228 * a channel and updating its file.refcnt field.
229 */
230 spinlock_t all_channels_lock;
231};
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/* Get the PPP protocol number from a skb */
Changli Gao96545ae2011-01-06 13:37:36 +0000234#define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236/* We limit the length of ppp->file.rq to this (arbitrary) value */
237#define PPP_MAX_RQLEN 32
238
239/*
240 * Maximum number of multilink fragments queued up.
241 * This has to be large enough to cope with the maximum latency of
242 * the slowest channel relative to the others. Strictly it should
243 * depend on the number of channels and their characteristics.
244 */
245#define PPP_MP_MAX_QLEN 128
246
247/* Multilink header bits. */
248#define B 0x80 /* this fragment begins a packet */
249#define E 0x40 /* this fragment ends a packet */
250
251/* Compare multilink sequence numbers (assumed to be 32 bits wide) */
252#define seq_before(a, b) ((s32)((a) - (b)) < 0)
253#define seq_after(a, b) ((s32)((a) - (b)) > 0)
254
255/* Prototypes. */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800256static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
257 struct file *file, unsigned int cmd, unsigned long arg);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +0000258static void ppp_xmit_process(struct ppp *ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
260static void ppp_push(struct ppp *ppp);
261static void ppp_channel_push(struct channel *pch);
262static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
263 struct channel *pch);
264static void ppp_receive_error(struct ppp *ppp);
265static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
266static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
267 struct sk_buff *skb);
268#ifdef CONFIG_PPP_MULTILINK
269static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
270 struct channel *pch);
271static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
272static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
273static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
274#endif /* CONFIG_PPP_MULTILINK */
275static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
276static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
277static void ppp_ccp_closed(struct ppp *ppp);
278static struct compressor *find_compressor(int type);
279static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200280static int ppp_create_interface(struct net *net, struct file *file, int *unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static void init_ppp_file(struct ppp_file *pf, int kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static void ppp_destroy_interface(struct ppp *ppp);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800283static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
284static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int ppp_connect_channel(struct channel *pch, int unit);
286static int ppp_disconnect_channel(struct channel *pch);
287static void ppp_destroy_channel(struct channel *pch);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -0800288static int unit_get(struct idr *p, void *ptr);
Cyrill Gorcunov85997572009-01-12 22:11:56 -0800289static int unit_set(struct idr *p, void *ptr, int n);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -0800290static void unit_put(struct idr *p, int n);
291static void *unit_find(struct idr *p, int n);
Guillaume Nault96d934c2016-04-28 17:55:30 +0200292static void ppp_setup(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Guillaume Nault79c441a2015-08-24 11:35:30 +0200294static const struct net_device_ops ppp_netdev_ops;
295
gregkh@suse.de56b22932005-03-23 10:01:41 -0800296static struct class *ppp_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800298/* per net-namespace data */
299static inline struct ppp_net *ppp_pernet(struct net *net)
300{
301 BUG_ON(!net);
302
303 return net_generic(net, ppp_net_id);
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/* Translates a PPP protocol number to a NP index (NP == network protocol) */
307static inline int proto_to_npindex(int proto)
308{
309 switch (proto) {
310 case PPP_IP:
311 return NP_IP;
312 case PPP_IPV6:
313 return NP_IPV6;
314 case PPP_IPX:
315 return NP_IPX;
316 case PPP_AT:
317 return NP_AT;
318 case PPP_MPLS_UC:
319 return NP_MPLS_UC;
320 case PPP_MPLS_MC:
321 return NP_MPLS_MC;
322 }
323 return -EINVAL;
324}
325
326/* Translates an NP index into a PPP protocol number */
327static const int npindex_to_proto[NUM_NP] = {
328 PPP_IP,
329 PPP_IPV6,
330 PPP_IPX,
331 PPP_AT,
332 PPP_MPLS_UC,
333 PPP_MPLS_MC,
334};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/* Translates an ethertype into an NP index */
337static inline int ethertype_to_npindex(int ethertype)
338{
339 switch (ethertype) {
340 case ETH_P_IP:
341 return NP_IP;
342 case ETH_P_IPV6:
343 return NP_IPV6;
344 case ETH_P_IPX:
345 return NP_IPX;
346 case ETH_P_PPPTALK:
347 case ETH_P_ATALK:
348 return NP_AT;
349 case ETH_P_MPLS_UC:
350 return NP_MPLS_UC;
351 case ETH_P_MPLS_MC:
352 return NP_MPLS_MC;
353 }
354 return -1;
355}
356
357/* Translates an NP index into an ethertype */
358static const int npindex_to_ethertype[NUM_NP] = {
359 ETH_P_IP,
360 ETH_P_IPV6,
361 ETH_P_IPX,
362 ETH_P_PPPTALK,
363 ETH_P_MPLS_UC,
364 ETH_P_MPLS_MC,
365};
366
367/*
368 * Locking shorthand.
369 */
370#define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
371#define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
372#define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
373#define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
374#define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
375 ppp_recv_lock(ppp); } while (0)
376#define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
377 ppp_xmit_unlock(ppp); } while (0)
378
379/*
380 * /dev/ppp device routines.
381 * The /dev/ppp device is used by pppd to control the ppp unit.
382 * It supports the read, write, ioctl and poll functions.
383 * Open instances of /dev/ppp can be in one of three states:
384 * unattached, attached to a ppp unit, or attached to a ppp channel.
385 */
386static int ppp_open(struct inode *inode, struct file *file)
387{
388 /*
389 * This could (should?) be enforced by the permissions on /dev/ppp.
390 */
391 if (!capable(CAP_NET_ADMIN))
392 return -EPERM;
393 return 0;
394}
395
Alan Coxf3ff8a42008-05-25 23:40:58 -0700396static int ppp_release(struct inode *unused, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct ppp_file *pf = file->private_data;
399 struct ppp *ppp;
400
Joe Perchescd228d52007-11-12 18:07:31 -0800401 if (pf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 file->private_data = NULL;
403 if (pf->kind == INTERFACE) {
404 ppp = PF_TO_PPP(pf);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200405 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (file == ppp->owner)
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200407 unregister_netdevice(ppp->dev);
408 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 if (atomic_dec_and_test(&pf->refcnt)) {
411 switch (pf->kind) {
412 case INTERFACE:
413 ppp_destroy_interface(PF_TO_PPP(pf));
414 break;
415 case CHANNEL:
416 ppp_destroy_channel(PF_TO_CHANNEL(pf));
417 break;
418 }
419 }
420 }
421 return 0;
422}
423
424static ssize_t ppp_read(struct file *file, char __user *buf,
425 size_t count, loff_t *ppos)
426{
427 struct ppp_file *pf = file->private_data;
428 DECLARE_WAITQUEUE(wait, current);
429 ssize_t ret;
430 struct sk_buff *skb = NULL;
Simon Arlott19937d02010-05-03 10:20:27 +0000431 struct iovec iov;
Al Viroba568402014-11-24 17:48:04 -0500432 struct iov_iter to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 ret = count;
435
Joe Perchescd228d52007-11-12 18:07:31 -0800436 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -ENXIO;
438 add_wait_queue(&pf->rwait, &wait);
439 for (;;) {
440 set_current_state(TASK_INTERRUPTIBLE);
441 skb = skb_dequeue(&pf->rq);
442 if (skb)
443 break;
444 ret = 0;
445 if (pf->dead)
446 break;
447 if (pf->kind == INTERFACE) {
448 /*
449 * Return 0 (EOF) on an interface that has no
450 * channels connected, unless it is looping
451 * network traffic (demand mode).
452 */
453 struct ppp *ppp = PF_TO_PPP(pf);
Guillaume Naultedffc212016-02-26 18:45:34 +0100454
455 ppp_recv_lock(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +0000456 if (ppp->n_channels == 0 &&
Guillaume Naultedffc212016-02-26 18:45:34 +0100457 (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
458 ppp_recv_unlock(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Guillaume Naultedffc212016-02-26 18:45:34 +0100460 }
461 ppp_recv_unlock(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 ret = -EAGAIN;
464 if (file->f_flags & O_NONBLOCK)
465 break;
466 ret = -ERESTARTSYS;
467 if (signal_pending(current))
468 break;
469 schedule();
470 }
471 set_current_state(TASK_RUNNING);
472 remove_wait_queue(&pf->rwait, &wait);
473
Joe Perchescd228d52007-11-12 18:07:31 -0800474 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto out;
476
477 ret = -EOVERFLOW;
478 if (skb->len > count)
479 goto outf;
480 ret = -EFAULT;
Simon Arlott19937d02010-05-03 10:20:27 +0000481 iov.iov_base = buf;
482 iov.iov_len = count;
Al Viroba568402014-11-24 17:48:04 -0500483 iov_iter_init(&to, READ, &iov, 1, count);
484 if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto outf;
486 ret = skb->len;
487
488 outf:
489 kfree_skb(skb);
490 out:
491 return ret;
492}
493
494static ssize_t ppp_write(struct file *file, const char __user *buf,
495 size_t count, loff_t *ppos)
496{
497 struct ppp_file *pf = file->private_data;
498 struct sk_buff *skb;
499 ssize_t ret;
500
Joe Perchescd228d52007-11-12 18:07:31 -0800501 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -ENXIO;
503 ret = -ENOMEM;
504 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -0800505 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto out;
507 skb_reserve(skb, pf->hdrlen);
508 ret = -EFAULT;
509 if (copy_from_user(skb_put(skb, count), buf, count)) {
510 kfree_skb(skb);
511 goto out;
512 }
513
514 skb_queue_tail(&pf->xq, skb);
515
516 switch (pf->kind) {
517 case INTERFACE:
518 ppp_xmit_process(PF_TO_PPP(pf));
519 break;
520 case CHANNEL:
521 ppp_channel_push(PF_TO_CHANNEL(pf));
522 break;
523 }
524
525 ret = count;
526
527 out:
528 return ret;
529}
530
531/* No kernel lock - fine */
532static unsigned int ppp_poll(struct file *file, poll_table *wait)
533{
534 struct ppp_file *pf = file->private_data;
535 unsigned int mask;
536
Joe Perchescd228d52007-11-12 18:07:31 -0800537 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539 poll_wait(file, &pf->rwait, wait);
540 mask = POLLOUT | POLLWRNORM;
Joe Perchescd228d52007-11-12 18:07:31 -0800541 if (skb_peek(&pf->rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 mask |= POLLIN | POLLRDNORM;
543 if (pf->dead)
544 mask |= POLLHUP;
545 else if (pf->kind == INTERFACE) {
546 /* see comment in ppp_read */
547 struct ppp *ppp = PF_TO_PPP(pf);
Guillaume Naultedffc212016-02-26 18:45:34 +0100548
549 ppp_recv_lock(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +0000550 if (ppp->n_channels == 0 &&
551 (ppp->flags & SC_LOOP_TRAFFIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 mask |= POLLIN | POLLRDNORM;
Guillaume Naultedffc212016-02-26 18:45:34 +0100553 ppp_recv_unlock(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
556 return mask;
557}
558
559#ifdef CONFIG_PPP_FILTER
560static int get_filter(void __user *arg, struct sock_filter **p)
561{
562 struct sock_fprog uprog;
563 struct sock_filter *code = NULL;
Christoph Schulz3916a312014-07-14 08:01:10 +0200564 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (copy_from_user(&uprog, arg, sizeof(uprog)))
567 return -EFAULT;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!uprog.len) {
570 *p = NULL;
571 return 0;
572 }
573
574 len = uprog.len * sizeof(struct sock_filter);
Julia Lawallb23d00e2010-05-21 22:18:59 +0000575 code = memdup_user(uprog.filter, len);
576 if (IS_ERR(code))
577 return PTR_ERR(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 *p = code;
580 return uprog.len;
581}
582#endif /* CONFIG_PPP_FILTER */
583
Alan Coxf3ff8a42008-05-25 23:40:58 -0700584static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100586 struct ppp_file *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct ppp *ppp;
588 int err = -EFAULT, val, val2, i;
589 struct ppp_idle idle;
590 struct npioctl npi;
591 int unit, cflags;
592 struct slcompress *vj;
593 void __user *argp = (void __user *)arg;
594 int __user *p = argp;
595
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100596 mutex_lock(&ppp_mutex);
597
598 pf = file->private_data;
599 if (!pf) {
600 err = ppp_unattached_ioctl(current->nsproxy->net_ns,
601 pf, file, cmd, arg);
602 goto out;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (cmd == PPPIOCDETACH) {
606 /*
607 * We have to be careful here... if the file descriptor
608 * has been dup'd, we could have another process in the
609 * middle of a poll using the same file *, so we had
610 * better not free the interface data structures -
611 * instead we fail the ioctl. Even in this case, we
612 * shut down the interface if we are the owner of it.
613 * Actually, we should get rid of PPPIOCDETACH, userland
614 * (i.e. pppd) could achieve the same effect by closing
615 * this fd and reopening /dev/ppp.
616 */
617 err = -EINVAL;
618 if (pf->kind == INTERFACE) {
619 ppp = PF_TO_PPP(pf);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200620 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (file == ppp->owner)
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200622 unregister_netdevice(ppp->dev);
623 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Al Viro24dff962014-10-08 23:44:00 -0400625 if (atomic_long_read(&file->f_count) < 2) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700626 ppp_release(NULL, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 err = 0;
628 } else
David S. Millerb48f8c22011-01-20 22:44:36 -0800629 pr_warn("PPPIOCDETACH file->f_count=%ld\n",
630 atomic_long_read(&file->f_count));
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 if (pf->kind == CHANNEL) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700635 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct ppp_channel *chan;
637
Alan Coxf3ff8a42008-05-25 23:40:58 -0700638 pch = PF_TO_CHANNEL(pf);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 switch (cmd) {
641 case PPPIOCCONNECT:
642 if (get_user(unit, p))
643 break;
644 err = ppp_connect_channel(pch, unit);
645 break;
646
647 case PPPIOCDISCONN:
648 err = ppp_disconnect_channel(pch);
649 break;
650
651 default:
652 down_read(&pch->chan_sem);
653 chan = pch->chan;
654 err = -ENOTTY;
655 if (chan && chan->ops->ioctl)
656 err = chan->ops->ioctl(chan, cmd, arg);
657 up_read(&pch->chan_sem);
658 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100659 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 if (pf->kind != INTERFACE) {
663 /* can't happen */
David S. Millerb48f8c22011-01-20 22:44:36 -0800664 pr_err("PPP: not interface or channel??\n");
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100665 err = -EINVAL;
666 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
669 ppp = PF_TO_PPP(pf);
670 switch (cmd) {
671 case PPPIOCSMRU:
672 if (get_user(val, p))
673 break;
674 ppp->mru = val;
675 err = 0;
676 break;
677
678 case PPPIOCSFLAGS:
679 if (get_user(val, p))
680 break;
681 ppp_lock(ppp);
682 cflags = ppp->flags & ~val;
Christoph Schulza9f559c2014-07-16 23:41:26 +0200683#ifdef CONFIG_PPP_MULTILINK
Christoph Schulzd762d032014-07-15 11:51:03 +0200684 if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
685 ppp->nextseq = 0;
Christoph Schulza9f559c2014-07-16 23:41:26 +0200686#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ppp->flags = val & SC_FLAG_BITS;
688 ppp_unlock(ppp);
689 if (cflags & SC_CCP_OPEN)
690 ppp_ccp_closed(ppp);
691 err = 0;
692 break;
693
694 case PPPIOCGFLAGS:
695 val = ppp->flags | ppp->xstate | ppp->rstate;
696 if (put_user(val, p))
697 break;
698 err = 0;
699 break;
700
701 case PPPIOCSCOMPRESS:
702 err = ppp_set_compress(ppp, arg);
703 break;
704
705 case PPPIOCGUNIT:
706 if (put_user(ppp->file.index, p))
707 break;
708 err = 0;
709 break;
710
711 case PPPIOCSDEBUG:
712 if (get_user(val, p))
713 break;
714 ppp->debug = val;
715 err = 0;
716 break;
717
718 case PPPIOCGDEBUG:
719 if (put_user(ppp->debug, p))
720 break;
721 err = 0;
722 break;
723
724 case PPPIOCGIDLE:
725 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
726 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
727 if (copy_to_user(argp, &idle, sizeof(idle)))
728 break;
729 err = 0;
730 break;
731
732 case PPPIOCSMAXCID:
733 if (get_user(val, p))
734 break;
735 val2 = 15;
736 if ((val >> 16) != 0) {
737 val2 = val >> 16;
738 val &= 0xffff;
739 }
740 vj = slhc_init(val2+1, val+1);
Ben Hutchings4ab42d72015-11-01 16:22:53 +0000741 if (IS_ERR(vj)) {
742 err = PTR_ERR(vj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744 }
745 ppp_lock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -0800746 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 slhc_free(ppp->vj);
748 ppp->vj = vj;
749 ppp_unlock(ppp);
750 err = 0;
751 break;
752
753 case PPPIOCGNPMODE:
754 case PPPIOCSNPMODE:
755 if (copy_from_user(&npi, argp, sizeof(npi)))
756 break;
757 err = proto_to_npindex(npi.protocol);
758 if (err < 0)
759 break;
760 i = err;
761 if (cmd == PPPIOCGNPMODE) {
762 err = -EFAULT;
763 npi.mode = ppp->npmode[i];
764 if (copy_to_user(argp, &npi, sizeof(npi)))
765 break;
766 } else {
767 ppp->npmode[i] = npi.mode;
768 /* we may be able to transmit more packets now (??) */
769 netif_wake_queue(ppp->dev);
770 }
771 err = 0;
772 break;
773
774#ifdef CONFIG_PPP_FILTER
775 case PPPIOCSPASS:
776 {
777 struct sock_filter *code;
Daniel Borkmann568f1942014-03-28 18:58:23 +0100778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 err = get_filter(argp, &code);
780 if (err >= 0) {
Takashi Iwai5748eb82014-11-10 11:50:21 +0100781 struct bpf_prog *pass_filter = NULL;
Daniel Borkmannb1fcd352014-05-23 18:43:58 +0200782 struct sock_fprog_kern fprog = {
Daniel Borkmann568f1942014-03-28 18:58:23 +0100783 .len = err,
784 .filter = code,
785 };
786
Takashi Iwai5748eb82014-11-10 11:50:21 +0100787 err = 0;
788 if (fprog.filter)
789 err = bpf_prog_create(&pass_filter, &fprog);
790 if (!err) {
791 ppp_lock(ppp);
792 if (ppp->pass_filter)
793 bpf_prog_destroy(ppp->pass_filter);
794 ppp->pass_filter = pass_filter;
795 ppp_unlock(ppp);
Christoph Schulzcc25eaa2014-07-16 22:10:29 +0200796 }
Daniel Borkmann568f1942014-03-28 18:58:23 +0100797 kfree(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 break;
800 }
801 case PPPIOCSACTIVE:
802 {
803 struct sock_filter *code;
Daniel Borkmann568f1942014-03-28 18:58:23 +0100804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 err = get_filter(argp, &code);
806 if (err >= 0) {
Takashi Iwai5748eb82014-11-10 11:50:21 +0100807 struct bpf_prog *active_filter = NULL;
Daniel Borkmannb1fcd352014-05-23 18:43:58 +0200808 struct sock_fprog_kern fprog = {
Daniel Borkmann568f1942014-03-28 18:58:23 +0100809 .len = err,
810 .filter = code,
811 };
812
Takashi Iwai5748eb82014-11-10 11:50:21 +0100813 err = 0;
814 if (fprog.filter)
815 err = bpf_prog_create(&active_filter, &fprog);
816 if (!err) {
817 ppp_lock(ppp);
818 if (ppp->active_filter)
819 bpf_prog_destroy(ppp->active_filter);
820 ppp->active_filter = active_filter;
821 ppp_unlock(ppp);
Christoph Schulzcc25eaa2014-07-16 22:10:29 +0200822 }
Daniel Borkmann568f1942014-03-28 18:58:23 +0100823 kfree(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 break;
826 }
827#endif /* CONFIG_PPP_FILTER */
828
829#ifdef CONFIG_PPP_MULTILINK
830 case PPPIOCSMRRU:
831 if (get_user(val, p))
832 break;
833 ppp_recv_lock(ppp);
834 ppp->mrru = val;
835 ppp_recv_unlock(ppp);
836 err = 0;
837 break;
838#endif /* CONFIG_PPP_MULTILINK */
839
840 default:
841 err = -ENOTTY;
842 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100843
844out:
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000845 mutex_unlock(&ppp_mutex);
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return err;
848}
849
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800850static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
851 struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int unit, err = -EFAULT;
854 struct ppp *ppp;
855 struct channel *chan;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800856 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int __user *p = (int __user *)arg;
858
859 switch (cmd) {
860 case PPPIOCNEWUNIT:
861 /* Create a new ppp unit */
862 if (get_user(unit, p))
863 break;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200864 err = ppp_create_interface(net, file, &unit);
865 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 err = -EFAULT;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +0200869 if (put_user(unit, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
871 err = 0;
872 break;
873
874 case PPPIOCATTACH:
875 /* Attach to an existing ppp unit */
876 if (get_user(unit, p))
877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800879 pn = ppp_pernet(net);
880 mutex_lock(&pn->all_ppp_mutex);
881 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800882 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 atomic_inc(&ppp->file.refcnt);
884 file->private_data = &ppp->file;
885 err = 0;
886 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800887 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
889
890 case PPPIOCATTCHAN:
891 if (get_user(unit, p))
892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800894 pn = ppp_pernet(net);
895 spin_lock_bh(&pn->all_channels_lock);
896 chan = ppp_find_channel(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800897 if (chan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 atomic_inc(&chan->file.refcnt);
899 file->private_data = &chan->file;
900 err = 0;
901 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800902 spin_unlock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 break;
904
905 default:
906 err = -ENOTTY;
907 }
Guillaume Naulte8e56ff2016-03-14 21:17:16 +0100908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return err;
910}
911
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800912static const struct file_operations ppp_device_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 .owner = THIS_MODULE,
914 .read = ppp_read,
915 .write = ppp_write,
916 .poll = ppp_poll,
Alan Coxf3ff8a42008-05-25 23:40:58 -0700917 .unlocked_ioctl = ppp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .open = ppp_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200919 .release = ppp_release,
920 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
922
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800923static __net_init int ppp_init_net(struct net *net)
924{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000925 struct ppp_net *pn = net_generic(net, ppp_net_id);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800926
927 idr_init(&pn->units_idr);
928 mutex_init(&pn->all_ppp_mutex);
929
930 INIT_LIST_HEAD(&pn->all_channels);
931 INIT_LIST_HEAD(&pn->new_channels);
932
933 spin_lock_init(&pn->all_channels_lock);
934
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800935 return 0;
936}
937
938static __net_exit void ppp_exit_net(struct net *net)
939{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000940 struct ppp_net *pn = net_generic(net, ppp_net_id);
Guillaume Nault79c441a2015-08-24 11:35:30 +0200941 struct net_device *dev;
942 struct net_device *aux;
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200943 struct ppp *ppp;
944 LIST_HEAD(list);
945 int id;
946
947 rtnl_lock();
Guillaume Nault79c441a2015-08-24 11:35:30 +0200948 for_each_netdev_safe(net, dev, aux) {
949 if (dev->netdev_ops == &ppp_netdev_ops)
950 unregister_netdevice_queue(dev, &list);
951 }
952
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200953 idr_for_each_entry(&pn->units_idr, ppp, id)
Guillaume Nault79c441a2015-08-24 11:35:30 +0200954 /* Skip devices already unregistered by previous loop */
955 if (!net_eq(dev_net(ppp->dev), net))
956 unregister_netdevice_queue(ppp->dev, &list);
Guillaume Nault8cb775b2015-08-14 10:42:56 +0200957
958 unregister_netdevice_many(&list);
959 rtnl_unlock();
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800960
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
1004 ret = register_netdevice(ppp->dev);
1005 if (ret < 0)
1006 goto err_unit;
1007
1008 atomic_inc(&ppp_unit_count);
1009
1010 mutex_unlock(&pn->all_ppp_mutex);
1011
1012 return 0;
1013
1014err_unit:
1015 unit_put(&pn->units_idr, ppp->file.index);
1016err:
1017 mutex_unlock(&pn->all_ppp_mutex);
1018
1019 return ret;
1020}
1021
1022static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1023 const struct ppp_config *conf)
1024{
1025 struct ppp *ppp = netdev_priv(dev);
1026 int indx;
1027 int err;
1028
1029 ppp->dev = dev;
1030 ppp->ppp_net = src_net;
1031 ppp->mru = PPP_MRU;
1032 ppp->owner = conf->file;
1033
1034 init_ppp_file(&ppp->file, INTERFACE);
1035 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1036
1037 for (indx = 0; indx < NUM_NP; ++indx)
1038 ppp->npmode[indx] = NPMODE_PASS;
1039 INIT_LIST_HEAD(&ppp->channels);
1040 spin_lock_init(&ppp->rlock);
1041 spin_lock_init(&ppp->wlock);
1042#ifdef CONFIG_PPP_MULTILINK
1043 ppp->minseq = -1;
1044 skb_queue_head_init(&ppp->mrq);
1045#endif /* CONFIG_PPP_MULTILINK */
1046#ifdef CONFIG_PPP_FILTER
1047 ppp->pass_filter = NULL;
1048 ppp->active_filter = NULL;
1049#endif /* CONFIG_PPP_FILTER */
1050
Guillaume Nault96d934c2016-04-28 17:55:30 +02001051 err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02001052 if (err < 0)
1053 return err;
1054
1055 conf->file->private_data = &ppp->file;
1056
1057 return 0;
1058}
1059
Guillaume Nault96d934c2016-04-28 17:55:30 +02001060static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1061 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
1062};
1063
1064static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[])
1065{
1066 if (!data)
1067 return -EINVAL;
1068
1069 if (!data[IFLA_PPP_DEV_FD])
1070 return -EINVAL;
1071 if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1072 return -EBADF;
1073
1074 return 0;
1075}
1076
1077static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001078 struct nlattr *tb[], struct nlattr *data[],
1079 struct netlink_ext_ack *extack)
Guillaume Nault96d934c2016-04-28 17:55:30 +02001080{
1081 struct ppp_config conf = {
1082 .unit = -1,
1083 .ifname_is_set = true,
1084 };
1085 struct file *file;
1086 int err;
1087
1088 file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1089 if (!file)
1090 return -EBADF;
1091
1092 /* rtnl_lock is already held here, but ppp_create_interface() locks
1093 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1094 * possible deadlock due to lock order inversion, at the cost of
1095 * pushing the problem back to userspace.
1096 */
1097 if (!mutex_trylock(&ppp_mutex)) {
1098 err = -EBUSY;
1099 goto out;
1100 }
1101
1102 if (file->f_op != &ppp_device_fops || file->private_data) {
1103 err = -EBADF;
1104 goto out_unlock;
1105 }
1106
1107 conf.file = file;
Guillaume Naultbb8082f2016-08-09 15:12:26 +02001108
1109 /* Don't use device name generated by the rtnetlink layer when ifname
1110 * isn't specified. Let ppp_dev_configure() set the device name using
1111 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1112 * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1113 */
1114 if (!tb[IFLA_IFNAME])
1115 conf.ifname_is_set = false;
1116
Guillaume Nault96d934c2016-04-28 17:55:30 +02001117 err = ppp_dev_configure(src_net, dev, &conf);
1118
1119out_unlock:
1120 mutex_unlock(&ppp_mutex);
1121out:
1122 fput(file);
1123
1124 return err;
1125}
1126
1127static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1128{
1129 unregister_netdevice_queue(dev, head);
1130}
1131
1132static size_t ppp_nl_get_size(const struct net_device *dev)
1133{
1134 return 0;
1135}
1136
1137static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1138{
1139 return 0;
1140}
1141
1142static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1143{
1144 struct ppp *ppp = netdev_priv(dev);
1145
1146 return ppp->ppp_net;
1147}
1148
1149static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1150 .kind = "ppp",
1151 .maxtype = IFLA_PPP_MAX,
1152 .policy = ppp_nl_policy,
1153 .priv_size = sizeof(struct ppp),
1154 .setup = ppp_setup,
1155 .validate = ppp_nl_validate,
1156 .newlink = ppp_nl_newlink,
1157 .dellink = ppp_nl_dellink,
1158 .get_size = ppp_nl_get_size,
1159 .fill_info = ppp_nl_fill_info,
1160 .get_link_net = ppp_nl_get_link_net,
1161};
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163#define PPP_MAJOR 108
1164
1165/* Called at boot time if ppp is compiled into the kernel,
1166 or at module load time (from init_module) if compiled as a module. */
1167static int __init ppp_init(void)
1168{
1169 int err;
1170
David S. Millerb48f8c22011-01-20 22:44:36 -08001171 pr_info("PPP generic driver version " PPP_VERSION "\n");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001172
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001173 err = register_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001174 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001175 pr_err("failed to register PPP pernet device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001176 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001179 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1180 if (err) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001181 pr_err("failed to register PPP device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001182 goto out_net;
1183 }
1184
1185 ppp_class = class_create(THIS_MODULE, "ppp");
1186 if (IS_ERR(ppp_class)) {
1187 err = PTR_ERR(ppp_class);
1188 goto out_chrdev;
1189 }
1190
Guillaume Nault96d934c2016-04-28 17:55:30 +02001191 err = rtnl_link_register(&ppp_link_ops);
1192 if (err) {
1193 pr_err("failed to register rtnetlink PPP handler\n");
1194 goto out_class;
1195 }
1196
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001197 /* not a big deal if we fail here :-) */
1198 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1199
1200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Guillaume Nault96d934c2016-04-28 17:55:30 +02001202out_class:
1203 class_destroy(ppp_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204out_chrdev:
1205 unregister_chrdev(PPP_MAJOR, "ppp");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001206out_net:
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00001207 unregister_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001208out:
1209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212/*
1213 * Network interface unit routines.
1214 */
Stephen Hemminger424efe92009-08-31 19:50:51 +00001215static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1217{
Wang Chenc8019bf2008-11-20 04:24:17 -08001218 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int npi, proto;
1220 unsigned char *pp;
1221
1222 npi = ethertype_to_npindex(ntohs(skb->protocol));
1223 if (npi < 0)
1224 goto outf;
1225
1226 /* Drop, accept or reject the packet */
1227 switch (ppp->npmode[npi]) {
1228 case NPMODE_PASS:
1229 break;
1230 case NPMODE_QUEUE:
1231 /* it would be nice to have a way to tell the network
1232 system to queue this one up for later. */
1233 goto outf;
1234 case NPMODE_DROP:
1235 case NPMODE_ERROR:
1236 goto outf;
1237 }
1238
1239 /* Put the 2-byte PPP protocol number on the front,
1240 making sure there is room for the address and control fields. */
Herbert Xu7b797d52007-09-16 16:21:42 -07001241 if (skb_cow_head(skb, PPP_HDRLEN))
1242 goto outf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 pp = skb_push(skb, 2);
1245 proto = npindex_to_proto[npi];
Changli Gao96545ae2011-01-06 13:37:36 +00001246 put_unaligned_be16(proto, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Guillaume Nault79c441a2015-08-24 11:35:30 +02001248 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 skb_queue_tail(&ppp->file.xq, skb);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001250 ppp_xmit_process(ppp);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001251 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 outf:
1254 kfree_skb(skb);
Paulius Zaleckas6ceffd42009-02-23 04:59:43 +00001255 ++dev->stats.tx_dropped;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001256 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static int
1260ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1261{
Wang Chenc8019bf2008-11-20 04:24:17 -08001262 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int err = -EFAULT;
1264 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1265 struct ppp_stats stats;
1266 struct ppp_comp_stats cstats;
1267 char *vers;
1268
1269 switch (cmd) {
1270 case SIOCGPPPSTATS:
1271 ppp_get_stats(ppp, &stats);
1272 if (copy_to_user(addr, &stats, sizeof(stats)))
1273 break;
1274 err = 0;
1275 break;
1276
1277 case SIOCGPPPCSTATS:
1278 memset(&cstats, 0, sizeof(cstats));
Joe Perchescd228d52007-11-12 18:07:31 -08001279 if (ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
Joe Perchescd228d52007-11-12 18:07:31 -08001281 if (ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1283 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1284 break;
1285 err = 0;
1286 break;
1287
1288 case SIOCGPPPVER:
1289 vers = PPP_VERSION;
1290 if (copy_to_user(addr, vers, strlen(vers) + 1))
1291 break;
1292 err = 0;
1293 break;
1294
1295 default:
1296 err = -EINVAL;
1297 }
1298
1299 return err;
1300}
1301
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001302static void
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001303ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1304{
1305 struct ppp *ppp = netdev_priv(dev);
1306
1307 ppp_recv_lock(ppp);
1308 stats64->rx_packets = ppp->stats64.rx_packets;
1309 stats64->rx_bytes = ppp->stats64.rx_bytes;
1310 ppp_recv_unlock(ppp);
1311
1312 ppp_xmit_lock(ppp);
1313 stats64->tx_packets = ppp->stats64.tx_packets;
1314 stats64->tx_bytes = ppp->stats64.tx_bytes;
1315 ppp_xmit_unlock(ppp);
1316
1317 stats64->rx_errors = dev->stats.rx_errors;
1318 stats64->tx_errors = dev->stats.tx_errors;
1319 stats64->rx_dropped = dev->stats.rx_dropped;
1320 stats64->tx_dropped = dev->stats.tx_dropped;
1321 stats64->rx_length_errors = dev->stats.rx_length_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001322}
1323
Eric Dumazet303c07d2013-02-19 10:42:03 -08001324static int ppp_dev_init(struct net_device *dev)
1325{
Eric Dumazetd3fff6c2016-06-09 07:45:12 -07001326 netdev_lockdep_set_classes(dev);
Eric Dumazet303c07d2013-02-19 10:42:03 -08001327 return 0;
1328}
1329
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001330static void ppp_dev_uninit(struct net_device *dev)
1331{
1332 struct ppp *ppp = netdev_priv(dev);
1333 struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1334
1335 ppp_lock(ppp);
1336 ppp->closing = 1;
1337 ppp_unlock(ppp);
1338
1339 mutex_lock(&pn->all_ppp_mutex);
1340 unit_put(&pn->units_idr, ppp->file.index);
1341 mutex_unlock(&pn->all_ppp_mutex);
1342
1343 ppp->owner = NULL;
1344
1345 ppp->file.dead = 1;
1346 wake_up_interruptible(&ppp->file.rwait);
1347}
1348
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001349static const struct net_device_ops ppp_netdev_ops = {
Eric Dumazet303c07d2013-02-19 10:42:03 -08001350 .ndo_init = ppp_dev_init,
Guillaume Nault8cb775b2015-08-14 10:42:56 +02001351 .ndo_uninit = ppp_dev_uninit,
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001352 .ndo_start_xmit = ppp_start_xmit,
1353 .ndo_do_ioctl = ppp_net_ioctl,
1354 .ndo_get_stats64 = ppp_get_stats64,
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001355};
1356
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001357static struct device_type ppp_type = {
1358 .name = "ppp",
1359};
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361static void ppp_setup(struct net_device *dev)
1362{
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001363 dev->netdev_ops = &ppp_netdev_ops;
Guillaume Nault94dbffe2015-12-11 19:54:49 +01001364 SET_NETDEV_DEVTYPE(dev, &ppp_type);
1365
Guillaume Nault07712772016-08-27 22:22:51 +02001366 dev->features |= NETIF_F_LLTX;
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 dev->hard_header_len = PPP_HDRLEN;
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +00001369 dev->mtu = PPP_MRU;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 dev->addr_len = 0;
1371 dev->tx_queue_len = 3;
1372 dev->type = ARPHRD_PPP;
1373 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Eric Dumazet02875872014-10-05 18:38:35 -07001374 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377/*
1378 * Transmit-side routines.
1379 */
1380
Guillaume Nault55454a52016-08-27 22:22:32 +02001381/* Called to do any work queued up on the transmit side that can now be done */
1382static void __ppp_xmit_process(struct ppp *ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct sk_buff *skb;
1385
1386 ppp_xmit_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001387 if (!ppp->closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ppp_push(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +00001389 while (!ppp->xmit_pending &&
1390 (skb = skb_dequeue(&ppp->file.xq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 ppp_send_frame(ppp, skb);
1392 /* If there's no work left to do, tell the core net
1393 code that we can accept some more. */
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001394 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 netif_wake_queue(ppp->dev);
David Woodhouse9a5d2bd2012-04-08 10:01:44 +00001396 else
1397 netif_stop_queue(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 ppp_xmit_unlock(ppp);
1400}
1401
Guillaume Nault55454a52016-08-27 22:22:32 +02001402static DEFINE_PER_CPU(int, ppp_xmit_recursion);
1403
1404static void ppp_xmit_process(struct ppp *ppp)
1405{
1406 local_bh_disable();
1407
1408 if (unlikely(__this_cpu_read(ppp_xmit_recursion)))
1409 goto err;
1410
1411 __this_cpu_inc(ppp_xmit_recursion);
1412 __ppp_xmit_process(ppp);
1413 __this_cpu_dec(ppp_xmit_recursion);
1414
1415 local_bh_enable();
1416
1417 return;
1418
1419err:
1420 local_bh_enable();
1421
1422 if (net_ratelimit())
1423 netdev_err(ppp->dev, "recursion detected\n");
1424}
1425
Matt Domschb3f9b922005-11-08 09:40:47 -08001426static inline struct sk_buff *
1427pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1428{
1429 struct sk_buff *new_skb;
1430 int len;
1431 int new_skb_size = ppp->dev->mtu +
1432 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1433 int compressor_skb_size = ppp->dev->mtu +
1434 ppp->xcomp->comp_extra + PPP_HDRLEN;
1435 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1436 if (!new_skb) {
1437 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001438 netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001439 return NULL;
1440 }
1441 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1442 skb_reserve(new_skb,
1443 ppp->dev->hard_header_len - PPP_HDRLEN);
1444
1445 /* compressor still expects A/C bytes in hdr */
1446 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1447 new_skb->data, skb->len + 2,
1448 compressor_skb_size);
1449 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
Eric Dumazet968d7012012-05-18 20:23:00 +00001450 consume_skb(skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001451 skb = new_skb;
1452 skb_put(skb, len);
1453 skb_pull(skb, 2); /* pull off A/C bytes */
1454 } else if (len == 0) {
1455 /* didn't compress, or CCP not up yet */
Eric Dumazet968d7012012-05-18 20:23:00 +00001456 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001457 new_skb = skb;
1458 } else {
1459 /*
1460 * (len < 0)
1461 * MPPE requires that we do not send unencrypted
1462 * frames. The compressor will return -1 if we
1463 * should drop the frame. We cannot simply test
1464 * the compress_proto because MPPE and MPPC share
1465 * the same number.
1466 */
1467 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001468 netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
Matt Domschb3f9b922005-11-08 09:40:47 -08001469 kfree_skb(skb);
Eric Dumazet968d7012012-05-18 20:23:00 +00001470 consume_skb(new_skb);
Matt Domschb3f9b922005-11-08 09:40:47 -08001471 new_skb = NULL;
1472 }
1473 return new_skb;
1474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
1477 * Compress and send a frame.
1478 * The caller should have locked the xmit path,
1479 * and xmit_pending should be 0.
1480 */
1481static void
1482ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1483{
1484 int proto = PPP_PROTO(skb);
1485 struct sk_buff *new_skb;
1486 int len;
1487 unsigned char *cp;
1488
1489 if (proto < 0x8000) {
1490#ifdef CONFIG_PPP_FILTER
1491 /* check if we should pass this packet */
1492 /* the filter instructions are constructed assuming
1493 a four-byte PPP header on each packet */
Johannes Bergd58ff352017-06-16 14:29:23 +02001494 *(u8 *)skb_push(skb, 2) = 1;
Joe Perches8e95a202009-12-03 07:58:21 +00001495 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001496 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001498 netdev_printk(KERN_DEBUG, ppp->dev,
1499 "PPP: outbound frame "
1500 "not passed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 kfree_skb(skb);
1502 return;
1503 }
1504 /* if this packet passes the active filter, record the time */
Joe Perches8e95a202009-12-03 07:58:21 +00001505 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001506 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 ppp->last_xmit = jiffies;
1508 skb_pull(skb, 2);
1509#else
1510 /* for data packets, record the time */
1511 ppp->last_xmit = jiffies;
1512#endif /* CONFIG_PPP_FILTER */
1513 }
1514
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00001515 ++ppp->stats64.tx_packets;
1516 ppp->stats64.tx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 switch (proto) {
1519 case PPP_IP:
Joe Perchescd228d52007-11-12 18:07:31 -08001520 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
1522 /* try to do VJ TCP header compression */
1523 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1524 GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001525 if (!new_skb) {
David S. Millerb48f8c22011-01-20 22:44:36 -08001526 netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 goto drop;
1528 }
1529 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1530 cp = skb->data + 2;
1531 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1532 new_skb->data + 2, &cp,
1533 !(ppp->flags & SC_NO_TCP_CCID));
1534 if (cp == skb->data + 2) {
1535 /* didn't compress */
Eric Dumazet968d7012012-05-18 20:23:00 +00001536 consume_skb(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 } else {
1538 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1539 proto = PPP_VJC_COMP;
1540 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1541 } else {
1542 proto = PPP_VJC_UNCOMP;
1543 cp[0] = skb->data[2];
1544 }
Eric Dumazet968d7012012-05-18 20:23:00 +00001545 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 skb = new_skb;
1547 cp = skb_put(skb, len + 2);
1548 cp[0] = 0;
1549 cp[1] = proto;
1550 }
1551 break;
1552
1553 case PPP_CCP:
1554 /* peek at outbound CCP frames */
1555 ppp_ccp_peek(ppp, skb, 0);
1556 break;
1557 }
1558
1559 /* try to do packet compression */
Joe Perches8e95a202009-12-03 07:58:21 +00001560 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1561 proto != PPP_LCP && proto != PPP_CCP) {
Matt Domschb3f9b922005-11-08 09:40:47 -08001562 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1563 if (net_ratelimit())
David S. Millerb48f8c22011-01-20 22:44:36 -08001564 netdev_err(ppp->dev,
1565 "ppp: compression required but "
1566 "down - pkt dropped.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 goto drop;
1568 }
Matt Domschb3f9b922005-11-08 09:40:47 -08001569 skb = pad_compress_skb(ppp, skb);
1570 if (!skb)
1571 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573
1574 /*
1575 * If we are waiting for traffic (demand dialling),
1576 * queue it up for pppd to receive.
1577 */
1578 if (ppp->flags & SC_LOOP_TRAFFIC) {
1579 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1580 goto drop;
1581 skb_queue_tail(&ppp->file.rq, skb);
1582 wake_up_interruptible(&ppp->file.rwait);
1583 return;
1584 }
1585
1586 ppp->xmit_pending = skb;
1587 ppp_push(ppp);
1588 return;
1589
1590 drop:
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00001591 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001592 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595/*
1596 * Try to send the frame in xmit_pending.
1597 * The caller should have the xmit path locked.
1598 */
1599static void
1600ppp_push(struct ppp *ppp)
1601{
1602 struct list_head *list;
1603 struct channel *pch;
1604 struct sk_buff *skb = ppp->xmit_pending;
1605
Joe Perchescd228d52007-11-12 18:07:31 -08001606 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return;
1608
1609 list = &ppp->channels;
1610 if (list_empty(list)) {
1611 /* nowhere to send the packet, just drop it */
1612 ppp->xmit_pending = NULL;
1613 kfree_skb(skb);
1614 return;
1615 }
1616
1617 if ((ppp->flags & SC_MULTILINK) == 0) {
1618 /* not doing multilink: send it down the first channel */
1619 list = list->next;
1620 pch = list_entry(list, struct channel, clist);
1621
Gao Feng97fcc192017-06-01 17:58:39 +08001622 spin_lock(&pch->downl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (pch->chan) {
1624 if (pch->chan->ops->start_xmit(pch->chan, skb))
1625 ppp->xmit_pending = NULL;
1626 } else {
1627 /* channel got unregistered */
1628 kfree_skb(skb);
1629 ppp->xmit_pending = NULL;
1630 }
Gao Feng97fcc192017-06-01 17:58:39 +08001631 spin_unlock(&pch->downl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return;
1633 }
1634
1635#ifdef CONFIG_PPP_MULTILINK
1636 /* Multilink: fragment the packet over as many links
1637 as can take the packet at the moment. */
1638 if (!ppp_mp_explode(ppp, skb))
1639 return;
1640#endif /* CONFIG_PPP_MULTILINK */
1641
1642 ppp->xmit_pending = NULL;
1643 kfree_skb(skb);
1644}
1645
1646#ifdef CONFIG_PPP_MULTILINK
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001647static bool mp_protocol_compress __read_mostly = true;
1648module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
1649MODULE_PARM_DESC(mp_protocol_compress,
1650 "compress protocol id in multilink fragments");
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652/*
1653 * Divide a packet to be transmitted into fragments and
1654 * send them out the individual links.
1655 */
1656static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1657{
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001658 int len, totlen;
1659 int i, bits, hdrlen, mtu;
1660 int flen;
1661 int navail, nfree, nzero;
1662 int nbigger;
1663 int totspeed;
1664 int totfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 unsigned char *p, *q;
1666 struct list_head *list;
1667 struct channel *pch;
1668 struct sk_buff *frag;
1669 struct ppp_channel *chan;
1670
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001671 totspeed = 0; /*total bitrate of the bundle*/
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001672 nfree = 0; /* # channels which have no packet already queued */
1673 navail = 0; /* total # of usable channels (not deregistered) */
1674 nzero = 0; /* number of channels with zero speed associated*/
1675 totfree = 0; /*total # of channels available and
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001676 *having no queued packets before
1677 *starting the fragmentation*/
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001680 i = 0;
1681 list_for_each_entry(pch, &ppp->channels, clist) {
Dan Carpenter34297692010-09-10 01:58:10 +00001682 if (pch->chan) {
1683 pch->avail = 1;
1684 navail++;
1685 pch->speed = pch->chan->speed;
1686 } else {
1687 pch->avail = 0;
1688 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001689 if (pch->avail) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001690 if (skb_queue_empty(&pch->file.xq) ||
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001691 !pch->had_frag) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001692 if (pch->speed == 0)
1693 nzero++;
1694 else
1695 totspeed += pch->speed;
1696
1697 pch->avail = 2;
1698 ++nfree;
1699 ++totfree;
1700 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001701 if (!pch->had_frag && i < ppp->nxchan)
1702 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001704 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001706 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001707 * Don't start sending this packet unless at least half of
1708 * the channels are free. This gives much better TCP
1709 * performance if we have a lot of channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001710 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001711 if (nfree == 0 || nfree < navail / 2)
1712 return 0; /* can't take now, leave it in xmit_pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001714 /* Do protocol field compression */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001715 p = skb->data;
1716 len = skb->len;
stephen hemmingerd39cd5e2010-12-20 17:58:33 +00001717 if (*p == 0 && mp_protocol_compress) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ++p;
1719 --len;
1720 }
1721
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001722 totlen = len;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001723 nbigger = len % nfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001725 /* skip to the channel after the one we last used
1726 and start at that one */
Domen Puncer81616c52005-09-10 00:27:04 -07001727 list = &ppp->channels;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001728 for (i = 0; i < ppp->nxchan; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001730 if (list == &ppp->channels) {
1731 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 break;
1733 }
1734 }
1735
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001736 /* create a fragment for each channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 bits = B;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001738 while (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001740 if (list == &ppp->channels) {
1741 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 continue;
1743 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001744 pch = list_entry(list, struct channel, clist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ++i;
1746 if (!pch->avail)
1747 continue;
1748
Paul Mackerras516cd152005-05-12 19:47:12 -04001749 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001750 * Skip this channel if it has a fragment pending already and
1751 * we haven't given a fragment to all of the free channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001752 */
1753 if (pch->avail == 1) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001754 if (nfree > 0)
Paul Mackerras516cd152005-05-12 19:47:12 -04001755 continue;
1756 } else {
Paul Mackerras516cd152005-05-12 19:47:12 -04001757 pch->avail = 1;
1758 }
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 /* check the channel's mtu and whether it is still attached. */
Gao Feng97fcc192017-06-01 17:58:39 +08001761 spin_lock(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001762 if (pch->chan == NULL) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001763 /* can't use this channel, it's being deregistered */
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001764 if (pch->speed == 0)
1765 nzero--;
1766 else
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001767 totspeed -= pch->speed;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001768
Gao Feng97fcc192017-06-01 17:58:39 +08001769 spin_unlock(&pch->downl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 pch->avail = 0;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001771 totlen = len;
1772 totfree--;
1773 nfree--;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001774 if (--navail == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 break;
1776 continue;
1777 }
1778
1779 /*
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001780 *if the channel speed is not set divide
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001781 *the packet evenly among the free channels;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001782 *otherwise divide it according to the speed
1783 *of the channel we are going to transmit on
1784 */
David S. Miller886f9fe2009-08-19 13:55:55 -07001785 flen = len;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001786 if (nfree > 0) {
1787 if (pch->speed == 0) {
Ben McKeegan536e00e2010-06-02 23:14:33 +00001788 flen = len/nfree;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001789 if (nbigger > 0) {
1790 flen++;
1791 nbigger--;
1792 }
1793 } else {
1794 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1795 ((totspeed*totfree)/pch->speed)) - hdrlen;
1796 if (nbigger > 0) {
1797 flen += ((totfree - nzero)*pch->speed)/totspeed;
1798 nbigger -= ((totfree - nzero)*pch->speed)/
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001799 totspeed;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001800 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001801 }
Ben McKeegana53a8b52009-07-28 07:43:57 +00001802 nfree--;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001803 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001804
1805 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001806 *check if we are on the last channel or
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001807 *we exceded the length of the data to
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001808 *fragment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Ben McKeegana53a8b52009-07-28 07:43:57 +00001810 if ((nfree <= 0) || (flen > len))
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001811 flen = len;
1812 /*
1813 *it is not worth to tx on slow channels:
1814 *in that case from the resulting flen according to the
1815 *above formula will be equal or less than zero.
1816 *Skip the channel in this case
1817 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001818 if (flen <= 0) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001819 pch->avail = 2;
Gao Feng97fcc192017-06-01 17:58:39 +08001820 spin_unlock(&pch->downl);
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001821 continue;
1822 }
1823
Henry Wong22e83a22011-09-18 13:41:49 +00001824 /*
1825 * hdrlen includes the 2-byte PPP protocol field, but the
1826 * MTU counts only the payload excluding the protocol field.
1827 * (RFC1661 Section 2)
1828 */
1829 mtu = pch->chan->mtu - (hdrlen - 2);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001830 if (mtu < 4)
1831 mtu = 4;
Paul Mackerras516cd152005-05-12 19:47:12 -04001832 if (flen > mtu)
1833 flen = mtu;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001834 if (flen == len)
1835 bits |= E;
1836 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001837 if (!frag)
Paul Mackerras516cd152005-05-12 19:47:12 -04001838 goto noskb;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001839 q = skb_put(frag, flen + hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001841 /* make the MP header */
Changli Gao96545ae2011-01-06 13:37:36 +00001842 put_unaligned_be16(PPP_MP, q);
Paul Mackerras516cd152005-05-12 19:47:12 -04001843 if (ppp->flags & SC_MP_XSHORTSEQ) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001844 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
Paul Mackerras516cd152005-05-12 19:47:12 -04001845 q[3] = ppp->nxseq;
1846 } else {
1847 q[2] = bits;
1848 q[3] = ppp->nxseq >> 16;
1849 q[4] = ppp->nxseq >> 8;
1850 q[5] = ppp->nxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001852
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001853 memcpy(q + hdrlen, p, flen);
Paul Mackerras516cd152005-05-12 19:47:12 -04001854
1855 /* try to send it down the channel */
1856 chan = pch->chan;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001857 if (!skb_queue_empty(&pch->file.xq) ||
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001858 !chan->ops->start_xmit(chan, frag))
Paul Mackerras516cd152005-05-12 19:47:12 -04001859 skb_queue_tail(&pch->file.xq, frag);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001860 pch->had_frag = 1;
Paul Mackerras516cd152005-05-12 19:47:12 -04001861 p += flen;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001862 len -= flen;
Paul Mackerras516cd152005-05-12 19:47:12 -04001863 ++ppp->nxseq;
1864 bits = 0;
Gao Feng97fcc192017-06-01 17:58:39 +08001865 spin_unlock(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001866 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001867 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 return 1;
1870
1871 noskb:
Gao Feng97fcc192017-06-01 17:58:39 +08001872 spin_unlock(&pch->downl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08001874 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001875 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 ++ppp->nxseq;
1877 return 1; /* abandon the frame */
1878}
1879#endif /* CONFIG_PPP_MULTILINK */
1880
Guillaume Nault55454a52016-08-27 22:22:32 +02001881/* Try to send data out on a channel */
1882static void __ppp_channel_push(struct channel *pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 struct sk_buff *skb;
1885 struct ppp *ppp;
1886
Gao Feng97fcc192017-06-01 17:58:39 +08001887 spin_lock(&pch->downl);
Joe Perchescd228d52007-11-12 18:07:31 -08001888 if (pch->chan) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001889 while (!skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 skb = skb_dequeue(&pch->file.xq);
1891 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1892 /* put the packet back and try again later */
1893 skb_queue_head(&pch->file.xq, skb);
1894 break;
1895 }
1896 }
1897 } else {
1898 /* channel got deregistered */
1899 skb_queue_purge(&pch->file.xq);
1900 }
Gao Feng97fcc192017-06-01 17:58:39 +08001901 spin_unlock(&pch->downl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* see if there is anything from the attached unit to be sent */
David S. Millerb03efcf2005-07-08 14:57:23 -07001903 if (skb_queue_empty(&pch->file.xq)) {
Gao Feng97fcc192017-06-01 17:58:39 +08001904 read_lock(&pch->upl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 ppp = pch->ppp;
Joe Perchescd228d52007-11-12 18:07:31 -08001906 if (ppp)
Guillaume Nault55454a52016-08-27 22:22:32 +02001907 __ppp_xmit_process(ppp);
Gao Feng97fcc192017-06-01 17:58:39 +08001908 read_unlock(&pch->upl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910}
1911
Guillaume Nault55454a52016-08-27 22:22:32 +02001912static void ppp_channel_push(struct channel *pch)
1913{
1914 local_bh_disable();
1915
1916 __this_cpu_inc(ppp_xmit_recursion);
1917 __ppp_channel_push(pch);
1918 __this_cpu_dec(ppp_xmit_recursion);
1919
1920 local_bh_enable();
1921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/*
1924 * Receive-side routines.
1925 */
1926
David S. Millera00eac02010-10-05 01:36:52 -07001927struct ppp_mp_skb_parm {
1928 u32 sequence;
1929 u8 BEbits;
1930};
1931#define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933static inline void
1934ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1935{
1936 ppp_recv_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001937 if (!ppp->closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ppp_receive_frame(ppp, skb, pch);
1939 else
1940 kfree_skb(skb);
1941 ppp_recv_unlock(ppp);
1942}
1943
1944void
1945ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1946{
1947 struct channel *pch = chan->ppp;
1948 int proto;
1949
Simon Arlottea8420e2010-05-03 10:19:33 +00001950 if (!pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 kfree_skb(skb);
1952 return;
1953 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 read_lock_bh(&pch->upl);
Simon Arlottea8420e2010-05-03 10:19:33 +00001956 if (!pskb_may_pull(skb, 2)) {
1957 kfree_skb(skb);
1958 if (pch->ppp) {
1959 ++pch->ppp->dev->stats.rx_length_errors;
1960 ppp_receive_error(pch->ppp);
1961 }
1962 goto done;
1963 }
1964
1965 proto = PPP_PROTO(skb);
Joe Perchescd228d52007-11-12 18:07:31 -08001966 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* put it on the channel queue */
1968 skb_queue_tail(&pch->file.rq, skb);
1969 /* drop old frames if queue too long */
Joe Perches8e95a202009-12-03 07:58:21 +00001970 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
1971 (skb = skb_dequeue(&pch->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 kfree_skb(skb);
1973 wake_up_interruptible(&pch->file.rwait);
1974 } else {
1975 ppp_do_recv(pch->ppp, skb, pch);
1976 }
Simon Arlottea8420e2010-05-03 10:19:33 +00001977
1978done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 read_unlock_bh(&pch->upl);
1980}
1981
1982/* Put a 0-length skb in the receive queue as an error indication */
1983void
1984ppp_input_error(struct ppp_channel *chan, int code)
1985{
1986 struct channel *pch = chan->ppp;
1987 struct sk_buff *skb;
1988
Joe Perchescd228d52007-11-12 18:07:31 -08001989 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return;
1991
1992 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08001993 if (pch->ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 skb = alloc_skb(0, GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001995 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 skb->len = 0; /* probably unnecessary */
1997 skb->cb[0] = code;
1998 ppp_do_recv(pch->ppp, skb, pch);
1999 }
2000 }
2001 read_unlock_bh(&pch->upl);
2002}
2003
2004/*
2005 * We come in here to process a received frame.
2006 * The receive side of the ppp unit is locked.
2007 */
2008static void
2009ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2010{
Simon Arlottea8420e2010-05-03 10:19:33 +00002011 /* note: a 0-length skb is used as an error indication */
2012 if (skb->len > 0) {
Tom Herbert3dfb0532015-04-20 14:10:05 -07002013 skb_checksum_complete_unset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014#ifdef CONFIG_PPP_MULTILINK
2015 /* XXX do channel-level decompression here */
2016 if (PPP_PROTO(skb) == PPP_MP)
2017 ppp_receive_mp_frame(ppp, skb, pch);
2018 else
2019#endif /* CONFIG_PPP_MULTILINK */
2020 ppp_receive_nonmp_frame(ppp, skb);
Simon Arlottea8420e2010-05-03 10:19:33 +00002021 } else {
2022 kfree_skb(skb);
2023 ppp_receive_error(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027static void
2028ppp_receive_error(struct ppp *ppp)
2029{
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002030 ++ppp->dev->stats.rx_errors;
Joe Perchescd228d52007-11-12 18:07:31 -08002031 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 slhc_toss(ppp->vj);
2033}
2034
2035static void
2036ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2037{
2038 struct sk_buff *ns;
2039 int proto, len, npi;
2040
2041 /*
2042 * Decompress the frame, if compressed.
2043 * Note that some decompressors need to see uncompressed frames
2044 * that come in as well as compressed frames.
2045 */
Joe Perches8e95a202009-12-03 07:58:21 +00002046 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2047 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 skb = ppp_decompress_frame(ppp, skb);
2049
Matt Domschb3f9b922005-11-08 09:40:47 -08002050 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2051 goto err;
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 proto = PPP_PROTO(skb);
2054 switch (proto) {
2055 case PPP_VJC_COMP:
2056 /* decompress VJ compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002057 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 goto err;
2059
Herbert Xu2a38b772007-09-16 16:22:13 -07002060 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /* copy to a new sk_buff with more tailroom */
2062 ns = dev_alloc_skb(skb->len + 128);
Joe Perchescd228d52007-11-12 18:07:31 -08002063 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002064 netdev_err(ppp->dev, "PPP: no memory "
2065 "(VJ decomp)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 goto err;
2067 }
2068 skb_reserve(ns, 2);
2069 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
Eric Dumazet968d7012012-05-18 20:23:00 +00002070 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 skb = ns;
2072 }
Herbert Xue3f749c2006-02-05 20:23:33 -08002073 else
2074 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2077 if (len <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002078 netdev_printk(KERN_DEBUG, ppp->dev,
2079 "PPP: VJ decompression error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto err;
2081 }
2082 len += 2;
2083 if (len > skb->len)
2084 skb_put(skb, len - skb->len);
2085 else if (len < skb->len)
2086 skb_trim(skb, len);
2087 proto = PPP_IP;
2088 break;
2089
2090 case PPP_VJC_UNCOMP:
Joe Perchescd228d52007-11-12 18:07:31 -08002091 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 goto err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* Until we fix the decompressor need to make sure
2095 * data portion is linear.
2096 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002097 if (!pskb_may_pull(skb, skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto err;
2099
2100 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002101 netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 goto err;
2103 }
2104 proto = PPP_IP;
2105 break;
2106
2107 case PPP_CCP:
2108 ppp_ccp_peek(ppp, skb, 1);
2109 break;
2110 }
2111
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002112 ++ppp->stats64.rx_packets;
2113 ppp->stats64.rx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 npi = proto_to_npindex(proto);
2116 if (npi < 0) {
2117 /* control or unknown frame - pass it to pppd */
2118 skb_queue_tail(&ppp->file.rq, skb);
2119 /* limit queue length by dropping old frames */
Joe Perches8e95a202009-12-03 07:58:21 +00002120 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2121 (skb = skb_dequeue(&ppp->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 kfree_skb(skb);
2123 /* wake up any process polling or blocking on read */
2124 wake_up_interruptible(&ppp->file.rwait);
2125
2126 } else {
2127 /* network protocol frame - give it to the kernel */
2128
2129#ifdef CONFIG_PPP_FILTER
2130 /* check if the packet passes the pass and active filters */
2131 /* the filter instructions are constructed assuming
2132 a four-byte PPP header on each packet */
Herbert Xu2a38b772007-09-16 16:22:13 -07002133 if (ppp->pass_filter || ppp->active_filter) {
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +00002134 if (skb_unclone(skb, GFP_ATOMIC))
Herbert Xu2a38b772007-09-16 16:22:13 -07002135 goto err;
2136
Johannes Bergd58ff352017-06-16 14:29:23 +02002137 *(u8 *)skb_push(skb, 2) = 0;
Joe Perches8e95a202009-12-03 07:58:21 +00002138 if (ppp->pass_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002139 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
Herbert Xu2a38b772007-09-16 16:22:13 -07002140 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002141 netdev_printk(KERN_DEBUG, ppp->dev,
2142 "PPP: inbound frame "
2143 "not passed\n");
Herbert Xu2a38b772007-09-16 16:22:13 -07002144 kfree_skb(skb);
2145 return;
2146 }
Joe Perches8e95a202009-12-03 07:58:21 +00002147 if (!(ppp->active_filter &&
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002148 BPF_PROG_RUN(ppp->active_filter, skb) == 0))
Herbert Xu2a38b772007-09-16 16:22:13 -07002149 ppp->last_recv = jiffies;
2150 __skb_pull(skb, 2);
2151 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#endif /* CONFIG_PPP_FILTER */
Herbert Xu2a38b772007-09-16 16:22:13 -07002153 ppp->last_recv = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Joe Perches8e95a202009-12-03 07:58:21 +00002155 if ((ppp->dev->flags & IFF_UP) == 0 ||
2156 ppp->npmode[npi] != NPMODE_PASS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 kfree_skb(skb);
2158 } else {
Herbert Xucbb042f2006-03-20 22:43:56 -08002159 /* chop off protocol */
2160 skb_pull_rcsum(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 skb->dev = ppp->dev;
2162 skb->protocol = htons(npindex_to_ethertype[npi]);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002163 skb_reset_mac_header(skb);
Guillaume Nault79c441a2015-08-24 11:35:30 +02002164 skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2165 dev_net(ppp->dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 }
2169 return;
2170
2171 err:
2172 kfree_skb(skb);
2173 ppp_receive_error(ppp);
2174}
2175
2176static struct sk_buff *
2177ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2178{
2179 int proto = PPP_PROTO(skb);
2180 struct sk_buff *ns;
2181 int len;
2182
2183 /* Until we fix all the decompressor's need to make sure
2184 * data portion is linear.
2185 */
2186 if (!pskb_may_pull(skb, skb->len))
2187 goto err;
2188
2189 if (proto == PPP_COMP) {
Konstantin Sharlaimov4b2a8fb2007-06-23 23:05:54 -07002190 int obuff_size;
2191
2192 switch(ppp->rcomp->compress_proto) {
2193 case CI_MPPE:
2194 obuff_size = ppp->mru + PPP_HDRLEN + 1;
2195 break;
2196 default:
2197 obuff_size = ppp->mru + PPP_HDRLEN;
2198 break;
2199 }
2200
2201 ns = dev_alloc_skb(obuff_size);
Joe Perchescd228d52007-11-12 18:07:31 -08002202 if (!ns) {
David S. Millerb48f8c22011-01-20 22:44:36 -08002203 netdev_err(ppp->dev, "ppp_decompress_frame: "
2204 "no memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto err;
2206 }
2207 /* the decompressor still expects the A/C bytes in the hdr */
2208 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
Konstantin Sharlaimov06c7af52007-08-21 00:12:44 -07002209 skb->len + 2, ns->data, obuff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (len < 0) {
2211 /* Pass the compressed frame to pppd as an
2212 error indication. */
2213 if (len == DECOMP_FATALERROR)
2214 ppp->rstate |= SC_DC_FERROR;
2215 kfree_skb(ns);
2216 goto err;
2217 }
2218
Eric Dumazet968d7012012-05-18 20:23:00 +00002219 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 skb = ns;
2221 skb_put(skb, len);
2222 skb_pull(skb, 2); /* pull off the A/C bytes */
2223
2224 } else {
2225 /* Uncompressed frame - pass to decompressor so it
2226 can update its dictionary if necessary. */
2227 if (ppp->rcomp->incomp)
2228 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2229 skb->len + 2);
2230 }
2231
2232 return skb;
2233
2234 err:
2235 ppp->rstate |= SC_DC_ERROR;
2236 ppp_receive_error(ppp);
2237 return skb;
2238}
2239
2240#ifdef CONFIG_PPP_MULTILINK
2241/*
2242 * Receive a multilink frame.
2243 * We put it on the reconstruction queue and then pull off
2244 * as many completed frames as we can.
2245 */
2246static void
2247ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2248{
2249 u32 mask, seq;
Domen Puncer81616c52005-09-10 00:27:04 -07002250 struct channel *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2252
Herbert Xu2a38b772007-09-16 16:22:13 -07002253 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto err; /* no good, throw it away */
2255
2256 /* Decode sequence number and begin/end bits */
2257 if (ppp->flags & SC_MP_SHORTSEQ) {
2258 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2259 mask = 0xfff;
2260 } else {
2261 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2262 mask = 0xffffff;
2263 }
David S. Millera00eac02010-10-05 01:36:52 -07002264 PPP_MP_CB(skb)->BEbits = skb->data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
2266
2267 /*
2268 * Do protocol ID decompression on the first fragment of each packet.
2269 */
David S. Millera00eac02010-10-05 01:36:52 -07002270 if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
Johannes Bergd58ff352017-06-16 14:29:23 +02002271 *(u8 *)skb_push(skb, 1) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /*
2274 * Expand sequence number to 32 bits, making it as close
2275 * as possible to ppp->minseq.
2276 */
2277 seq |= ppp->minseq & ~mask;
2278 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2279 seq += mask + 1;
2280 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2281 seq -= mask + 1; /* should never happen */
David S. Millera00eac02010-10-05 01:36:52 -07002282 PPP_MP_CB(skb)->sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 pch->lastseq = seq;
2284
2285 /*
2286 * If this packet comes before the next one we were expecting,
2287 * drop it.
2288 */
2289 if (seq_before(seq, ppp->nextseq)) {
2290 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002291 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 ppp_receive_error(ppp);
2293 return;
2294 }
2295
2296 /*
2297 * Reevaluate minseq, the minimum over all channels of the
2298 * last sequence number received on each channel. Because of
2299 * the increasing sequence number rule, we know that any fragment
2300 * before `minseq' which hasn't arrived is never going to arrive.
2301 * The list of channels can't change because we have the receive
2302 * side of the ppp unit locked.
2303 */
Domen Puncer81616c52005-09-10 00:27:04 -07002304 list_for_each_entry(ch, &ppp->channels, clist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (seq_before(ch->lastseq, seq))
2306 seq = ch->lastseq;
2307 }
2308 if (seq_before(ppp->minseq, seq))
2309 ppp->minseq = seq;
2310
2311 /* Put the fragment on the reconstruction queue */
2312 ppp_mp_insert(ppp, skb);
2313
2314 /* If the queue is getting long, don't wait any longer for packets
2315 before the start of the queue. */
David S. Miller38ce7c72008-09-23 01:17:18 -07002316 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
stephen hemmingerc6b20d92010-06-01 06:05:46 +00002317 struct sk_buff *mskb = skb_peek(&ppp->mrq);
David S. Millera00eac02010-10-05 01:36:52 -07002318 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2319 ppp->minseq = PPP_MP_CB(mskb)->sequence;
David S. Miller38ce7c72008-09-23 01:17:18 -07002320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 /* Pull completed packets off the queue and receive them. */
Ben McKeegan82b3cc12009-11-16 03:44:25 +00002323 while ((skb = ppp_mp_reconstruct(ppp))) {
2324 if (pskb_may_pull(skb, 2))
2325 ppp_receive_nonmp_frame(ppp, skb);
2326 else {
2327 ++ppp->dev->stats.rx_length_errors;
2328 kfree_skb(skb);
2329 ppp_receive_error(ppp);
2330 }
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 return;
2334
2335 err:
2336 kfree_skb(skb);
2337 ppp_receive_error(ppp);
2338}
2339
2340/*
2341 * Insert a fragment on the MP reconstruction queue.
2342 * The queue is ordered by increasing sequence number.
2343 */
2344static void
2345ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2346{
2347 struct sk_buff *p;
2348 struct sk_buff_head *list = &ppp->mrq;
David S. Millera00eac02010-10-05 01:36:52 -07002349 u32 seq = PPP_MP_CB(skb)->sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 /* N.B. we don't need to lock the list lock because we have the
2352 ppp unit receive-side lock. */
David S. Miller13c98212008-10-09 16:40:29 -07002353 skb_queue_walk(list, p) {
David S. Millera00eac02010-10-05 01:36:52 -07002354 if (seq_before(seq, PPP_MP_CB(p)->sequence))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 break;
David S. Miller13c98212008-10-09 16:40:29 -07002356 }
David S. Miller43f59c82008-09-21 21:28:51 -07002357 __skb_queue_before(list, p, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360/*
2361 * Reconstruct a packet from the MP fragment queue.
2362 * We go through increasing sequence numbers until we find a
2363 * complete packet, or we get to the sequence number for a fragment
2364 * which hasn't arrived but might still do so.
2365 */
Stephen Hemminger3c582b32008-01-23 20:54:07 -08002366static struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367ppp_mp_reconstruct(struct ppp *ppp)
2368{
2369 u32 seq = ppp->nextseq;
2370 u32 minseq = ppp->minseq;
2371 struct sk_buff_head *list = &ppp->mrq;
David S. Millerd52344a72011-01-20 22:52:05 -08002372 struct sk_buff *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 struct sk_buff *head, *tail;
2374 struct sk_buff *skb = NULL;
2375 int lost = 0, len = 0;
2376
2377 if (ppp->mrru == 0) /* do nothing until mrru is set */
2378 return NULL;
2379 head = list->next;
2380 tail = NULL;
David S. Millerd52344a72011-01-20 22:52:05 -08002381 skb_queue_walk_safe(list, p, tmp) {
2382 again:
David S. Millera00eac02010-10-05 01:36:52 -07002383 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 /* this can't happen, anyway ignore the skb */
David S. Millerb48f8c22011-01-20 22:44:36 -08002385 netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2386 "seq %u < %u\n",
2387 PPP_MP_CB(p)->sequence, seq);
David S. Millerd52344a72011-01-20 22:52:05 -08002388 __skb_unlink(p, list);
2389 kfree_skb(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 continue;
2391 }
David S. Millera00eac02010-10-05 01:36:52 -07002392 if (PPP_MP_CB(p)->sequence != seq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002393 u32 oldseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 /* Fragment `seq' is missing. If it is after
2395 minseq, it might arrive later, so stop here. */
2396 if (seq_after(seq, minseq))
2397 break;
2398 /* Fragment `seq' is lost, keep going. */
2399 lost = 1;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002400 oldseq = seq;
David S. Millera00eac02010-10-05 01:36:52 -07002401 seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2402 minseq + 1: PPP_MP_CB(p)->sequence;
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002403
2404 if (ppp->debug & 1)
2405 netdev_printk(KERN_DEBUG, ppp->dev,
2406 "lost frag %u..%u\n",
2407 oldseq, seq-1);
2408
David S. Millerd52344a72011-01-20 22:52:05 -08002409 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411
2412 /*
2413 * At this point we know that all the fragments from
2414 * ppp->nextseq to seq are either present or lost.
2415 * Also, there are no complete packets in the queue
2416 * that have no missing fragments and end before this
2417 * fragment.
2418 */
2419
2420 /* B bit set indicates this fragment starts a packet */
David S. Millera00eac02010-10-05 01:36:52 -07002421 if (PPP_MP_CB(p)->BEbits & B) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 head = p;
2423 lost = 0;
2424 len = 0;
2425 }
2426
2427 len += p->len;
2428
2429 /* Got a complete packet yet? */
David S. Millera00eac02010-10-05 01:36:52 -07002430 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2431 (PPP_MP_CB(head)->BEbits & B)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (len > ppp->mrru + 2) {
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002433 ++ppp->dev->stats.rx_length_errors;
David S. Millerb48f8c22011-01-20 22:44:36 -08002434 netdev_printk(KERN_DEBUG, ppp->dev,
2435 "PPP: reconstructed packet"
2436 " is too long (%d)\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 } else {
2438 tail = p;
2439 break;
2440 }
2441 ppp->nextseq = seq + 1;
2442 }
2443
2444 /*
2445 * If this is the ending fragment of a packet,
2446 * and we haven't found a complete valid packet yet,
2447 * we can discard up to and including this fragment.
2448 */
David S. Millerd52344a72011-01-20 22:52:05 -08002449 if (PPP_MP_CB(p)->BEbits & E) {
2450 struct sk_buff *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
David S. Millerd52344a72011-01-20 22:52:05 -08002452 skb_queue_reverse_walk_from_safe(list, p, tmp2) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002453 if (ppp->debug & 1)
2454 netdev_printk(KERN_DEBUG, ppp->dev,
2455 "discarding frag %u\n",
2456 PPP_MP_CB(p)->sequence);
David S. Millerd52344a72011-01-20 22:52:05 -08002457 __skb_unlink(p, list);
2458 kfree_skb(p);
2459 }
2460 head = skb_peek(list);
2461 if (!head)
2462 break;
2463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 ++seq;
2465 }
2466
2467 /* If we have a complete packet, copy it all into one skb. */
2468 if (tail != NULL) {
2469 /* If we have discarded any fragments,
2470 signal a receive error. */
David S. Millera00eac02010-10-05 01:36:52 -07002471 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
Ben McKeegan8a49ad62012-02-24 06:33:56 +00002472 skb_queue_walk_safe(list, p, tmp) {
2473 if (p == head)
2474 break;
2475 if (ppp->debug & 1)
2476 netdev_printk(KERN_DEBUG, ppp->dev,
2477 "discarding frag %u\n",
2478 PPP_MP_CB(p)->sequence);
2479 __skb_unlink(p, list);
2480 kfree_skb(p);
2481 }
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (ppp->debug & 1)
David S. Millerb48f8c22011-01-20 22:44:36 -08002484 netdev_printk(KERN_DEBUG, ppp->dev,
2485 " missed pkts %u..%u\n",
2486 ppp->nextseq,
2487 PPP_MP_CB(head)->sequence-1);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002488 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 ppp_receive_error(ppp);
2490 }
2491
David S. Miller212bfb92011-01-20 22:46:07 -08002492 skb = head;
2493 if (head != tail) {
2494 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2495 p = skb_queue_next(list, head);
2496 __skb_unlink(skb, list);
2497 skb_queue_walk_from_safe(list, p, tmp) {
2498 __skb_unlink(p, list);
2499 *fragpp = p;
2500 p->next = NULL;
2501 fragpp = &p->next;
2502
2503 skb->len += p->len;
2504 skb->data_len += p->len;
Eric Dumazet19c6c8f2012-02-13 04:23:24 +00002505 skb->truesize += p->truesize;
David S. Miller212bfb92011-01-20 22:46:07 -08002506
2507 if (p == tail)
2508 break;
2509 }
2510 } else {
2511 __skb_unlink(skb, list);
2512 }
2513
David S. Millera00eac02010-10-05 01:36:52 -07002514 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516
2517 return skb;
2518}
2519#endif /* CONFIG_PPP_MULTILINK */
2520
2521/*
2522 * Channel interface.
2523 */
2524
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002525/* Create a new, unattached ppp channel. */
2526int ppp_register_channel(struct ppp_channel *chan)
2527{
2528 return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2529}
2530
2531/* Create a new, unattached ppp channel for specified net. */
2532int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 struct channel *pch;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002535 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Panagiotis Issarisd4274b52006-08-15 16:01:07 -07002537 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -08002538 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return -ENOMEM;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002540
2541 pn = ppp_pernet(net);
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 pch->ppp = NULL;
2544 pch->chan = chan;
Guillaume Nault1f461dc2016-03-23 16:38:55 +01002545 pch->chan_net = get_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 chan->ppp = pch;
2547 init_ppp_file(&pch->file, CHANNEL);
2548 pch->file.hdrlen = chan->hdrlen;
2549#ifdef CONFIG_PPP_MULTILINK
2550 pch->lastseq = -1;
2551#endif /* CONFIG_PPP_MULTILINK */
2552 init_rwsem(&pch->chan_sem);
2553 spin_lock_init(&pch->downl);
2554 rwlock_init(&pch->upl);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002555
2556 spin_lock_bh(&pn->all_channels_lock);
2557 pch->file.index = ++pn->last_channel_index;
2558 list_add(&pch->list, &pn->new_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 atomic_inc(&channel_count);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002560 spin_unlock_bh(&pn->all_channels_lock);
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return 0;
2563}
2564
2565/*
2566 * Return the index of a channel.
2567 */
2568int ppp_channel_index(struct ppp_channel *chan)
2569{
2570 struct channel *pch = chan->ppp;
2571
Joe Perchescd228d52007-11-12 18:07:31 -08002572 if (pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return pch->file.index;
2574 return -1;
2575}
2576
2577/*
2578 * Return the PPP unit number to which a channel is connected.
2579 */
2580int ppp_unit_number(struct ppp_channel *chan)
2581{
2582 struct channel *pch = chan->ppp;
2583 int unit = -1;
2584
Joe Perchescd228d52007-11-12 18:07:31 -08002585 if (pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002587 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 unit = pch->ppp->file.index;
2589 read_unlock_bh(&pch->upl);
2590 }
2591 return unit;
2592}
2593
2594/*
James Chapman63f96072010-04-02 06:18:39 +00002595 * Return the PPP device interface name of a channel.
2596 */
2597char *ppp_dev_name(struct ppp_channel *chan)
2598{
2599 struct channel *pch = chan->ppp;
2600 char *name = NULL;
2601
2602 if (pch) {
2603 read_lock_bh(&pch->upl);
2604 if (pch->ppp && pch->ppp->dev)
2605 name = pch->ppp->dev->name;
2606 read_unlock_bh(&pch->upl);
2607 }
2608 return name;
2609}
2610
2611
2612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 * Disconnect a channel from the generic layer.
2614 * This must be called in process context.
2615 */
2616void
2617ppp_unregister_channel(struct ppp_channel *chan)
2618{
2619 struct channel *pch = chan->ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002620 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Joe Perchescd228d52007-11-12 18:07:31 -08002622 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return; /* should never happen */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 chan->ppp = NULL;
2626
2627 /*
2628 * This ensures that we have returned from any calls into the
2629 * the channel's start_xmit or ioctl routine before we proceed.
2630 */
2631 down_write(&pch->chan_sem);
2632 spin_lock_bh(&pch->downl);
2633 pch->chan = NULL;
2634 spin_unlock_bh(&pch->downl);
2635 up_write(&pch->chan_sem);
2636 ppp_disconnect_channel(pch);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002637
2638 pn = ppp_pernet(pch->chan_net);
2639 spin_lock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 list_del(&pch->list);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002641 spin_unlock_bh(&pn->all_channels_lock);
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 pch->file.dead = 1;
2644 wake_up_interruptible(&pch->file.rwait);
2645 if (atomic_dec_and_test(&pch->file.refcnt))
2646 ppp_destroy_channel(pch);
2647}
2648
2649/*
2650 * Callback from a channel when it can accept more to transmit.
2651 * This should be called at BH/softirq level, not interrupt level.
2652 */
2653void
2654ppp_output_wakeup(struct ppp_channel *chan)
2655{
2656 struct channel *pch = chan->ppp;
2657
Joe Perchescd228d52007-11-12 18:07:31 -08002658 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return;
2660 ppp_channel_push(pch);
2661}
2662
2663/*
2664 * Compression control.
2665 */
2666
2667/* Process the PPPIOCSCOMPRESS ioctl. */
2668static int
2669ppp_set_compress(struct ppp *ppp, unsigned long arg)
2670{
2671 int err;
2672 struct compressor *cp, *ocomp;
2673 struct ppp_option_data data;
2674 void *state, *ostate;
2675 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2676
2677 err = -EFAULT;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002678 if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 goto out;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002680 if (data.length > CCP_MAX_OPTION_LENGTH)
2681 goto out;
2682 if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
2683 goto out;
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 err = -EINVAL;
Guillaume Nault555d5b72016-02-23 13:59:43 +01002686 if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 goto out;
2688
Johannes Berga65e5d72008-07-09 10:28:38 +02002689 cp = try_then_request_module(
2690 find_compressor(ccp_option[0]),
2691 "ppp-compress-%d", ccp_option[0]);
Joe Perchescd228d52007-11-12 18:07:31 -08002692 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 goto out;
2694
2695 err = -ENOBUFS;
2696 if (data.transmit) {
2697 state = cp->comp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002698 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 ppp_xmit_lock(ppp);
2700 ppp->xstate &= ~SC_COMP_RUN;
2701 ocomp = ppp->xcomp;
2702 ostate = ppp->xc_state;
2703 ppp->xcomp = cp;
2704 ppp->xc_state = state;
2705 ppp_xmit_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002706 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 ocomp->comp_free(ostate);
2708 module_put(ocomp->owner);
2709 }
2710 err = 0;
2711 } else
2712 module_put(cp->owner);
2713
2714 } else {
2715 state = cp->decomp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002716 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 ppp_recv_lock(ppp);
2718 ppp->rstate &= ~SC_DECOMP_RUN;
2719 ocomp = ppp->rcomp;
2720 ostate = ppp->rc_state;
2721 ppp->rcomp = cp;
2722 ppp->rc_state = state;
2723 ppp_recv_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002724 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 ocomp->decomp_free(ostate);
2726 module_put(ocomp->owner);
2727 }
2728 err = 0;
2729 } else
2730 module_put(cp->owner);
2731 }
2732
2733 out:
2734 return err;
2735}
2736
2737/*
2738 * Look at a CCP packet and update our state accordingly.
2739 * We assume the caller has the xmit or recv path locked.
2740 */
2741static void
2742ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2743{
2744 unsigned char *dp;
2745 int len;
2746
2747 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2748 return; /* no header */
2749 dp = skb->data + 2;
2750
2751 switch (CCP_CODE(dp)) {
2752 case CCP_CONFREQ:
2753
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002754 /* A ConfReq starts negotiation of compression
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 * in one direction of transmission,
2756 * and hence brings it down...but which way?
2757 *
2758 * Remember:
2759 * A ConfReq indicates what the sender would like to receive
2760 */
2761 if(inbound)
2762 /* He is proposing what I should send */
2763 ppp->xstate &= ~SC_COMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002764 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* I am proposing to what he should send */
2766 ppp->rstate &= ~SC_DECOMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 case CCP_TERMREQ:
2771 case CCP_TERMACK:
2772 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002773 * CCP is going down, both directions of transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 */
2775 ppp->rstate &= ~SC_DECOMP_RUN;
2776 ppp->xstate &= ~SC_COMP_RUN;
2777 break;
2778
2779 case CCP_CONFACK:
2780 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2781 break;
2782 len = CCP_LENGTH(dp);
2783 if (!pskb_may_pull(skb, len + 2))
2784 return; /* too short */
2785 dp += CCP_HDRLEN;
2786 len -= CCP_HDRLEN;
2787 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2788 break;
2789 if (inbound) {
2790 /* we will start receiving compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002791 if (!ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 break;
2793 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2794 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2795 ppp->rstate |= SC_DECOMP_RUN;
2796 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2797 }
2798 } else {
2799 /* we will soon start sending compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002800 if (!ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 break;
2802 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2803 ppp->file.index, 0, ppp->debug))
2804 ppp->xstate |= SC_COMP_RUN;
2805 }
2806 break;
2807
2808 case CCP_RESETACK:
2809 /* reset the [de]compressor */
2810 if ((ppp->flags & SC_CCP_UP) == 0)
2811 break;
2812 if (inbound) {
2813 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2814 ppp->rcomp->decomp_reset(ppp->rc_state);
2815 ppp->rstate &= ~SC_DC_ERROR;
2816 }
2817 } else {
2818 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2819 ppp->xcomp->comp_reset(ppp->xc_state);
2820 }
2821 break;
2822 }
2823}
2824
2825/* Free up compression resources. */
2826static void
2827ppp_ccp_closed(struct ppp *ppp)
2828{
2829 void *xstate, *rstate;
2830 struct compressor *xcomp, *rcomp;
2831
2832 ppp_lock(ppp);
2833 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2834 ppp->xstate = 0;
2835 xcomp = ppp->xcomp;
2836 xstate = ppp->xc_state;
2837 ppp->xc_state = NULL;
2838 ppp->rstate = 0;
2839 rcomp = ppp->rcomp;
2840 rstate = ppp->rc_state;
2841 ppp->rc_state = NULL;
2842 ppp_unlock(ppp);
2843
2844 if (xstate) {
2845 xcomp->comp_free(xstate);
2846 module_put(xcomp->owner);
2847 }
2848 if (rstate) {
2849 rcomp->decomp_free(rstate);
2850 module_put(rcomp->owner);
2851 }
2852}
2853
2854/* List of compressors. */
2855static LIST_HEAD(compressor_list);
2856static DEFINE_SPINLOCK(compressor_list_lock);
2857
2858struct compressor_entry {
2859 struct list_head list;
2860 struct compressor *comp;
2861};
2862
2863static struct compressor_entry *
2864find_comp_entry(int proto)
2865{
2866 struct compressor_entry *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Domen Puncer81616c52005-09-10 00:27:04 -07002868 list_for_each_entry(ce, &compressor_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (ce->comp->compress_proto == proto)
2870 return ce;
2871 }
2872 return NULL;
2873}
2874
2875/* Register a compressor */
2876int
2877ppp_register_compressor(struct compressor *cp)
2878{
2879 struct compressor_entry *ce;
2880 int ret;
2881 spin_lock(&compressor_list_lock);
2882 ret = -EEXIST;
Joe Perchescd228d52007-11-12 18:07:31 -08002883 if (find_comp_entry(cp->compress_proto))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 goto out;
2885 ret = -ENOMEM;
2886 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002887 if (!ce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 goto out;
2889 ret = 0;
2890 ce->comp = cp;
2891 list_add(&ce->list, &compressor_list);
2892 out:
2893 spin_unlock(&compressor_list_lock);
2894 return ret;
2895}
2896
2897/* Unregister a compressor */
2898void
2899ppp_unregister_compressor(struct compressor *cp)
2900{
2901 struct compressor_entry *ce;
2902
2903 spin_lock(&compressor_list_lock);
2904 ce = find_comp_entry(cp->compress_proto);
Joe Perchescd228d52007-11-12 18:07:31 -08002905 if (ce && ce->comp == cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 list_del(&ce->list);
2907 kfree(ce);
2908 }
2909 spin_unlock(&compressor_list_lock);
2910}
2911
2912/* Find a compressor. */
2913static struct compressor *
2914find_compressor(int type)
2915{
2916 struct compressor_entry *ce;
2917 struct compressor *cp = NULL;
2918
2919 spin_lock(&compressor_list_lock);
2920 ce = find_comp_entry(type);
Joe Perchescd228d52007-11-12 18:07:31 -08002921 if (ce) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 cp = ce->comp;
2923 if (!try_module_get(cp->owner))
2924 cp = NULL;
2925 }
2926 spin_unlock(&compressor_list_lock);
2927 return cp;
2928}
2929
2930/*
2931 * Miscelleneous stuff.
2932 */
2933
2934static void
2935ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2936{
2937 struct slcompress *vj = ppp->vj;
2938
2939 memset(st, 0, sizeof(*st));
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002940 st->p.ppp_ipackets = ppp->stats64.rx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002941 st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002942 st->p.ppp_ibytes = ppp->stats64.rx_bytes;
2943 st->p.ppp_opackets = ppp->stats64.tx_packets;
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002944 st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
Kevin Groenevelde51f6ff2012-07-27 17:38:53 +00002945 st->p.ppp_obytes = ppp->stats64.tx_bytes;
Joe Perchescd228d52007-11-12 18:07:31 -08002946 if (!vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return;
2948 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2949 st->vj.vjs_compressed = vj->sls_o_compressed;
2950 st->vj.vjs_searches = vj->sls_o_searches;
2951 st->vj.vjs_misses = vj->sls_o_misses;
2952 st->vj.vjs_errorin = vj->sls_i_error;
2953 st->vj.vjs_tossed = vj->sls_i_tossed;
2954 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2955 st->vj.vjs_compressedin = vj->sls_i_compressed;
2956}
2957
2958/*
2959 * Stuff for handling the lists of ppp units and channels
2960 * and for initialization.
2961 */
2962
2963/*
2964 * Create a new ppp interface unit. Fails if it can't allocate memory
2965 * or if there is already a unit with the requested number.
2966 * unit == -1 means allocate a new number.
2967 */
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002968static int ppp_create_interface(struct net *net, struct file *file, int *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002970 struct ppp_config conf = {
2971 .file = file,
2972 .unit = *unit,
Guillaume Nault96d934c2016-04-28 17:55:30 +02002973 .ifname_is_set = false,
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002974 };
2975 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct ppp *ppp;
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002977 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Guillaume Nault69d97282015-12-11 19:54:52 +01002979 dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002980 if (!dev) {
2981 err = -ENOMEM;
2982 goto err;
2983 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002984 dev_net_set(dev, net);
Guillaume Nault96d934c2016-04-28 17:55:30 +02002985 dev->rtnl_link_ops = &ppp_link_ops;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002986
Guillaume Nault58a89ec2015-09-24 12:54:01 +02002987 rtnl_lock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002988
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002989 err = ppp_dev_configure(net, dev, &conf);
2990 if (err < 0)
2991 goto err_dev;
2992 ppp = netdev_priv(dev);
2993 *unit = ppp->file.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Guillaume Nault58a89ec2015-09-24 12:54:01 +02002995 rtnl_unlock();
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002996
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02002999err_dev:
Guillaume Nault6faac632016-03-07 19:36:44 +01003000 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 free_netdev(dev);
Guillaume Nault7d9f0b42016-04-28 17:55:28 +02003002err:
3003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
3006/*
3007 * Initialize a ppp_file structure.
3008 */
3009static void
3010init_ppp_file(struct ppp_file *pf, int kind)
3011{
3012 pf->kind = kind;
3013 skb_queue_head_init(&pf->xq);
3014 skb_queue_head_init(&pf->rq);
3015 atomic_set(&pf->refcnt, 1);
3016 init_waitqueue_head(&pf->rwait);
3017}
3018
3019/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 * Free the memory used by a ppp unit. This is only called once
3021 * there are no channels connected to the unit and no file structs
3022 * that reference the unit.
3023 */
3024static void ppp_destroy_interface(struct ppp *ppp)
3025{
3026 atomic_dec(&ppp_unit_count);
3027
3028 if (!ppp->file.dead || ppp->n_channels) {
3029 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003030 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3031 "but dead=%d n_channels=%d !\n",
3032 ppp, ppp->file.dead, ppp->n_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return;
3034 }
3035
3036 ppp_ccp_closed(ppp);
3037 if (ppp->vj) {
3038 slhc_free(ppp->vj);
3039 ppp->vj = NULL;
3040 }
3041 skb_queue_purge(&ppp->file.xq);
3042 skb_queue_purge(&ppp->file.rq);
3043#ifdef CONFIG_PPP_MULTILINK
3044 skb_queue_purge(&ppp->mrq);
3045#endif /* CONFIG_PPP_MULTILINK */
3046#ifdef CONFIG_PPP_FILTER
Daniel Borkmann568f1942014-03-28 18:58:23 +01003047 if (ppp->pass_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003048 bpf_prog_destroy(ppp->pass_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003049 ppp->pass_filter = NULL;
3050 }
3051
3052 if (ppp->active_filter) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003053 bpf_prog_destroy(ppp->active_filter);
Daniel Borkmann568f1942014-03-28 18:58:23 +01003054 ppp->active_filter = NULL;
3055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056#endif /* CONFIG_PPP_FILTER */
3057
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00003058 kfree_skb(ppp->xmit_pending);
G. Liakhovetski165de5b2007-03-25 19:04:09 -07003059
James Chapman739840d2008-12-17 12:02:16 +00003060 free_netdev(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063/*
3064 * Locate an existing ppp unit.
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08003065 * The caller should have locked the all_ppp_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 */
3067static struct ppp *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003068ppp_find_unit(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003070 return unit_find(&pn->units_idr, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073/*
3074 * Locate an existing ppp channel.
3075 * The caller should have locked the all_channels_lock.
3076 * First we look in the new_channels list, then in the
3077 * all_channels list. If found in the new_channels list,
3078 * we move it to the all_channels list. This is for speed
3079 * when we have a lot of channels in use.
3080 */
3081static struct channel *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003082ppp_find_channel(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003086 list_for_each_entry(pch, &pn->new_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (pch->file.index == unit) {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003088 list_move(&pch->list, &pn->all_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return pch;
3090 }
3091 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003092
3093 list_for_each_entry(pch, &pn->all_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (pch->file.index == unit)
3095 return pch;
3096 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return NULL;
3099}
3100
3101/*
3102 * Connect a PPP channel to a PPP interface unit.
3103 */
3104static int
3105ppp_connect_channel(struct channel *pch, int unit)
3106{
3107 struct ppp *ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003108 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 int ret = -ENXIO;
3110 int hdrlen;
3111
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003112 pn = ppp_pernet(pch->chan_net);
3113
3114 mutex_lock(&pn->all_ppp_mutex);
3115 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -08003116 if (!ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 goto out;
3118 write_lock_bh(&pch->upl);
3119 ret = -EINVAL;
Joe Perchescd228d52007-11-12 18:07:31 -08003120 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 goto outl;
3122
3123 ppp_lock(ppp);
3124 if (pch->file.hdrlen > ppp->file.hdrlen)
3125 ppp->file.hdrlen = pch->file.hdrlen;
3126 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
James Chapman739840d2008-12-17 12:02:16 +00003127 if (hdrlen > ppp->dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 ppp->dev->hard_header_len = hdrlen;
3129 list_add_tail(&pch->clist, &ppp->channels);
3130 ++ppp->n_channels;
3131 pch->ppp = ppp;
3132 atomic_inc(&ppp->file.refcnt);
3133 ppp_unlock(ppp);
3134 ret = 0;
3135
3136 outl:
3137 write_unlock_bh(&pch->upl);
3138 out:
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003139 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return ret;
3141}
3142
3143/*
3144 * Disconnect a channel from its ppp unit.
3145 */
3146static int
3147ppp_disconnect_channel(struct channel *pch)
3148{
3149 struct ppp *ppp;
3150 int err = -EINVAL;
3151
3152 write_lock_bh(&pch->upl);
3153 ppp = pch->ppp;
3154 pch->ppp = NULL;
3155 write_unlock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08003156 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /* remove it from the ppp unit's list */
3158 ppp_lock(ppp);
3159 list_del(&pch->clist);
3160 if (--ppp->n_channels == 0)
3161 wake_up_interruptible(&ppp->file.rwait);
3162 ppp_unlock(ppp);
3163 if (atomic_dec_and_test(&ppp->file.refcnt))
3164 ppp_destroy_interface(ppp);
3165 err = 0;
3166 }
3167 return err;
3168}
3169
3170/*
3171 * Free up the resources used by a ppp channel.
3172 */
3173static void ppp_destroy_channel(struct channel *pch)
3174{
WANG Cong205e1e22016-07-05 22:12:36 -07003175 put_net(pch->chan_net);
3176 pch->chan_net = NULL;
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 atomic_dec(&channel_count);
3179
3180 if (!pch->file.dead) {
3181 /* "can't happen" */
David S. Millerb48f8c22011-01-20 22:44:36 -08003182 pr_err("ppp: destroying undead channel %p !\n", pch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return;
3184 }
3185 skb_queue_purge(&pch->file.xq);
3186 skb_queue_purge(&pch->file.rq);
3187 kfree(pch);
3188}
3189
3190static void __exit ppp_cleanup(void)
3191{
3192 /* should never happen */
3193 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
David S. Millerb48f8c22011-01-20 22:44:36 -08003194 pr_err("PPP: removing module but units remain!\n");
Guillaume Nault96d934c2016-04-28 17:55:30 +02003195 rtnl_link_unregister(&ppp_link_ops);
Akinobu Mita68fc4fa2007-07-19 01:47:50 -07003196 unregister_chrdev(PPP_MAJOR, "ppp");
Greg Kroah-Hartman9a6a2a52006-09-12 17:00:10 +02003197 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08003198 class_destroy(ppp_class);
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00003199 unregister_pernet_device(&ppp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
3202/*
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003203 * Units handling. Caller must protect concurrent access
3204 * by holding all_ppp_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Cyrill Gorcunovbcc70bb2010-11-23 11:43:44 +00003207/* associate pointer with specified number */
3208static int unit_set(struct idr *p, void *ptr, int n)
3209{
3210 int unit;
3211
Tejun Heo2fa532c2013-02-27 17:04:36 -08003212 unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3213 if (unit == -ENOSPC)
3214 unit = -EINVAL;
Cyrill Gorcunov85997572009-01-12 22:11:56 -08003215 return unit;
3216}
3217
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003218/* get new free unit number and associate pointer with it */
3219static int unit_get(struct idr *p, void *ptr)
3220{
Tejun Heo2fa532c2013-02-27 17:04:36 -08003221 return idr_alloc(p, ptr, 0, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003224/* put unit number back to a pool */
3225static void unit_put(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003227 idr_remove(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003230/* get pointer associated with the number */
3231static void *unit_find(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08003233 return idr_find(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236/* Module/initialization stuff */
3237
3238module_init(ppp_init);
3239module_exit(ppp_cleanup);
3240
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08003241EXPORT_SYMBOL(ppp_register_net_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242EXPORT_SYMBOL(ppp_register_channel);
3243EXPORT_SYMBOL(ppp_unregister_channel);
3244EXPORT_SYMBOL(ppp_channel_index);
3245EXPORT_SYMBOL(ppp_unit_number);
James Chapman63f96072010-04-02 06:18:39 +00003246EXPORT_SYMBOL(ppp_dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247EXPORT_SYMBOL(ppp_input);
3248EXPORT_SYMBOL(ppp_input_error);
3249EXPORT_SYMBOL(ppp_output_wakeup);
3250EXPORT_SYMBOL(ppp_register_compressor);
3251EXPORT_SYMBOL(ppp_unregister_compressor);
3252MODULE_LICENSE("GPL");
Kay Sievers578454f2010-05-20 18:07:20 +02003253MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
Guillaume Nault96d934c2016-04-28 17:55:30 +02003254MODULE_ALIAS_RTNL_LINK("ppp");
Kay Sievers578454f2010-05-20 18:07:20 +02003255MODULE_ALIAS("devname:ppp");