1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Generic PPP layer for Linux.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 1999-2002 Paul Mackerras.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * The generic PPP layer handles the PPP network interfaces, the
8*4882a593Smuzhiyun * /dev/ppp device, packet and VJ compression, and multilink.
9*4882a593Smuzhiyun * It talks to PPP `channels' via the interface defined in
10*4882a593Smuzhiyun * include/linux/ppp_channel.h. Channels provide the basic means for
11*4882a593Smuzhiyun * sending and receiving PPP frames on some kind of communications
12*4882a593Smuzhiyun * channel.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Part of the code in this driver was inspired by the old async-only
15*4882a593Smuzhiyun * PPP driver, written by Michael Callahan and Al Longyear, and
16*4882a593Smuzhiyun * subsequently hacked by Paul Mackerras.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * ==FILEVERSION 20041108==
19*4882a593Smuzhiyun */
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/kernel.h>
23*4882a593Smuzhiyun #include <linux/sched/signal.h>
24*4882a593Smuzhiyun #include <linux/kmod.h>
25*4882a593Smuzhiyun #include <linux/init.h>
26*4882a593Smuzhiyun #include <linux/list.h>
27*4882a593Smuzhiyun #include <linux/idr.h>
28*4882a593Smuzhiyun #include <linux/netdevice.h>
29*4882a593Smuzhiyun #include <linux/poll.h>
30*4882a593Smuzhiyun #include <linux/ppp_defs.h>
31*4882a593Smuzhiyun #include <linux/filter.h>
32*4882a593Smuzhiyun #include <linux/ppp-ioctl.h>
33*4882a593Smuzhiyun #include <linux/ppp_channel.h>
34*4882a593Smuzhiyun #include <linux/ppp-comp.h>
35*4882a593Smuzhiyun #include <linux/skbuff.h>
36*4882a593Smuzhiyun #include <linux/rtnetlink.h>
37*4882a593Smuzhiyun #include <linux/if_arp.h>
38*4882a593Smuzhiyun #include <linux/ip.h>
39*4882a593Smuzhiyun #include <linux/tcp.h>
40*4882a593Smuzhiyun #include <linux/spinlock.h>
41*4882a593Smuzhiyun #include <linux/rwsem.h>
42*4882a593Smuzhiyun #include <linux/stddef.h>
43*4882a593Smuzhiyun #include <linux/device.h>
44*4882a593Smuzhiyun #include <linux/mutex.h>
45*4882a593Smuzhiyun #include <linux/slab.h>
46*4882a593Smuzhiyun #include <linux/file.h>
47*4882a593Smuzhiyun #include <asm/unaligned.h>
48*4882a593Smuzhiyun #include <net/slhc_vj.h>
49*4882a593Smuzhiyun #include <linux/atomic.h>
50*4882a593Smuzhiyun #include <linux/refcount.h>
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #include <linux/nsproxy.h>
53*4882a593Smuzhiyun #include <net/net_namespace.h>
54*4882a593Smuzhiyun #include <net/netns/generic.h>
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #define PPP_VERSION "2.4.2"
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * Network protocols we support.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun #define NP_IP 0 /* Internet Protocol V4 */
62*4882a593Smuzhiyun #define NP_IPV6 1 /* Internet Protocol V6 */
63*4882a593Smuzhiyun #define NP_IPX 2 /* IPX protocol */
64*4882a593Smuzhiyun #define NP_AT 3 /* Appletalk protocol */
65*4882a593Smuzhiyun #define NP_MPLS_UC 4 /* MPLS unicast */
66*4882a593Smuzhiyun #define NP_MPLS_MC 5 /* MPLS multicast */
67*4882a593Smuzhiyun #define NUM_NP 6 /* Number of NPs. */
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define MPHDRLEN 6 /* multilink protocol header length */
70*4882a593Smuzhiyun #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun #define PPP_PROTO_LEN 2
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * An instance of /dev/ppp can be associated with either a ppp
76*4882a593Smuzhiyun * interface unit or a ppp channel. In both cases, file->private_data
77*4882a593Smuzhiyun * points to one of these.
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun struct ppp_file {
80*4882a593Smuzhiyun enum {
81*4882a593Smuzhiyun INTERFACE=1, CHANNEL
82*4882a593Smuzhiyun } kind;
83*4882a593Smuzhiyun struct sk_buff_head xq; /* pppd transmit queue */
84*4882a593Smuzhiyun struct sk_buff_head rq; /* receive queue for pppd */
85*4882a593Smuzhiyun wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
86*4882a593Smuzhiyun refcount_t refcnt; /* # refs (incl /dev/ppp attached) */
87*4882a593Smuzhiyun int hdrlen; /* space to leave for headers */
88*4882a593Smuzhiyun int index; /* interface unit / channel number */
89*4882a593Smuzhiyun int dead; /* unit/channel has been shut down */
90*4882a593Smuzhiyun };
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun #define PF_TO_X(pf, X) container_of(pf, X, file)
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
95*4882a593Smuzhiyun #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * Data structure to hold primary network stats for which
99*4882a593Smuzhiyun * we want to use 64 bit storage. Other network stats
100*4882a593Smuzhiyun * are stored in dev->stats of the ppp strucute.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun struct ppp_link_stats {
103*4882a593Smuzhiyun u64 rx_packets;
104*4882a593Smuzhiyun u64 tx_packets;
105*4882a593Smuzhiyun u64 rx_bytes;
106*4882a593Smuzhiyun u64 tx_bytes;
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun * Data structure describing one ppp unit.
111*4882a593Smuzhiyun * A ppp unit corresponds to a ppp network interface device
112*4882a593Smuzhiyun * and represents a multilink bundle.
113*4882a593Smuzhiyun * It can have 0 or more ppp channels connected to it.
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun struct ppp {
116*4882a593Smuzhiyun struct ppp_file file; /* stuff for read/write/poll 0 */
117*4882a593Smuzhiyun struct file *owner; /* file that owns this unit 48 */
118*4882a593Smuzhiyun struct list_head channels; /* list of attached channels 4c */
119*4882a593Smuzhiyun int n_channels; /* how many channels are attached 54 */
120*4882a593Smuzhiyun spinlock_t rlock; /* lock for receive side 58 */
121*4882a593Smuzhiyun spinlock_t wlock; /* lock for transmit side 5c */
122*4882a593Smuzhiyun int __percpu *xmit_recursion; /* xmit recursion detect */
123*4882a593Smuzhiyun int mru; /* max receive unit 60 */
124*4882a593Smuzhiyun unsigned int flags; /* control bits 64 */
125*4882a593Smuzhiyun unsigned int xstate; /* transmit state bits 68 */
126*4882a593Smuzhiyun unsigned int rstate; /* receive state bits 6c */
127*4882a593Smuzhiyun int debug; /* debug flags 70 */
128*4882a593Smuzhiyun struct slcompress *vj; /* state for VJ header compression */
129*4882a593Smuzhiyun enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
130*4882a593Smuzhiyun struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
131*4882a593Smuzhiyun struct compressor *xcomp; /* transmit packet compressor 8c */
132*4882a593Smuzhiyun void *xc_state; /* its internal state 90 */
133*4882a593Smuzhiyun struct compressor *rcomp; /* receive decompressor 94 */
134*4882a593Smuzhiyun void *rc_state; /* its internal state 98 */
135*4882a593Smuzhiyun unsigned long last_xmit; /* jiffies when last pkt sent 9c */
136*4882a593Smuzhiyun unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
137*4882a593Smuzhiyun struct net_device *dev; /* network interface device a4 */
138*4882a593Smuzhiyun int closing; /* is device closing down? a8 */
139*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
140*4882a593Smuzhiyun int nxchan; /* next channel to send something on */
141*4882a593Smuzhiyun u32 nxseq; /* next sequence number to send */
142*4882a593Smuzhiyun int mrru; /* MP: max reconst. receive unit */
143*4882a593Smuzhiyun u32 nextseq; /* MP: seq no of next packet */
144*4882a593Smuzhiyun u32 minseq; /* MP: min of most recent seqnos */
145*4882a593Smuzhiyun struct sk_buff_head mrq; /* MP: receive reconstruction queue */
146*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
147*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
148*4882a593Smuzhiyun struct bpf_prog *pass_filter; /* filter for packets to pass */
149*4882a593Smuzhiyun struct bpf_prog *active_filter; /* filter for pkts to reset idle */
150*4882a593Smuzhiyun #endif /* CONFIG_PPP_FILTER */
151*4882a593Smuzhiyun struct net *ppp_net; /* the net we belong to */
152*4882a593Smuzhiyun struct ppp_link_stats stats64; /* 64 bit network stats */
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
157*4882a593Smuzhiyun * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
158*4882a593Smuzhiyun * SC_MUST_COMP
159*4882a593Smuzhiyun * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
160*4882a593Smuzhiyun * Bits in xstate: SC_COMP_RUN
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
163*4882a593Smuzhiyun |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
164*4882a593Smuzhiyun |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /*
167*4882a593Smuzhiyun * Private data structure for each channel.
168*4882a593Smuzhiyun * This includes the data structure used for multilink.
169*4882a593Smuzhiyun */
170*4882a593Smuzhiyun struct channel {
171*4882a593Smuzhiyun struct ppp_file file; /* stuff for read/write/poll */
172*4882a593Smuzhiyun struct list_head list; /* link in all/new_channels list */
173*4882a593Smuzhiyun struct ppp_channel *chan; /* public channel data structure */
174*4882a593Smuzhiyun struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
175*4882a593Smuzhiyun spinlock_t downl; /* protects `chan', file.xq dequeue */
176*4882a593Smuzhiyun struct ppp *ppp; /* ppp unit we're connected to */
177*4882a593Smuzhiyun struct net *chan_net; /* the net channel belongs to */
178*4882a593Smuzhiyun struct list_head clist; /* link in list of channels per unit */
179*4882a593Smuzhiyun rwlock_t upl; /* protects `ppp' */
180*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
181*4882a593Smuzhiyun u8 avail; /* flag used in multilink stuff */
182*4882a593Smuzhiyun u8 had_frag; /* >= 1 fragments have been sent */
183*4882a593Smuzhiyun u32 lastseq; /* MP: last sequence # received */
184*4882a593Smuzhiyun int speed; /* speed of the corresponding ppp channel*/
185*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun struct ppp_config {
189*4882a593Smuzhiyun struct file *file;
190*4882a593Smuzhiyun s32 unit;
191*4882a593Smuzhiyun bool ifname_is_set;
192*4882a593Smuzhiyun };
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * SMP locking issues:
196*4882a593Smuzhiyun * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
197*4882a593Smuzhiyun * list and the ppp.n_channels field, you need to take both locks
198*4882a593Smuzhiyun * before you modify them.
199*4882a593Smuzhiyun * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
200*4882a593Smuzhiyun * channel.downl.
201*4882a593Smuzhiyun */
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun static DEFINE_MUTEX(ppp_mutex);
204*4882a593Smuzhiyun static atomic_t ppp_unit_count = ATOMIC_INIT(0);
205*4882a593Smuzhiyun static atomic_t channel_count = ATOMIC_INIT(0);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* per-net private data for this module */
208*4882a593Smuzhiyun static unsigned int ppp_net_id __read_mostly;
209*4882a593Smuzhiyun struct ppp_net {
210*4882a593Smuzhiyun /* units to ppp mapping */
211*4882a593Smuzhiyun struct idr units_idr;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /*
214*4882a593Smuzhiyun * all_ppp_mutex protects the units_idr mapping.
215*4882a593Smuzhiyun * It also ensures that finding a ppp unit in the units_idr
216*4882a593Smuzhiyun * map and updating its file.refcnt field is atomic.
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun struct mutex all_ppp_mutex;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /* channels */
221*4882a593Smuzhiyun struct list_head all_channels;
222*4882a593Smuzhiyun struct list_head new_channels;
223*4882a593Smuzhiyun int last_channel_index;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /*
226*4882a593Smuzhiyun * all_channels_lock protects all_channels and
227*4882a593Smuzhiyun * last_channel_index, and the atomicity of find
228*4882a593Smuzhiyun * a channel and updating its file.refcnt field.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun spinlock_t all_channels_lock;
231*4882a593Smuzhiyun };
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /* Get the PPP protocol number from a skb */
234*4882a593Smuzhiyun #define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun /* We limit the length of ppp->file.rq to this (arbitrary) value */
237*4882a593Smuzhiyun #define PPP_MAX_RQLEN 32
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /*
240*4882a593Smuzhiyun * Maximum number of multilink fragments queued up.
241*4882a593Smuzhiyun * This has to be large enough to cope with the maximum latency of
242*4882a593Smuzhiyun * the slowest channel relative to the others. Strictly it should
243*4882a593Smuzhiyun * depend on the number of channels and their characteristics.
244*4882a593Smuzhiyun */
245*4882a593Smuzhiyun #define PPP_MP_MAX_QLEN 128
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /* Multilink header bits. */
248*4882a593Smuzhiyun #define B 0x80 /* this fragment begins a packet */
249*4882a593Smuzhiyun #define E 0x40 /* this fragment ends a packet */
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
252*4882a593Smuzhiyun #define seq_before(a, b) ((s32)((a) - (b)) < 0)
253*4882a593Smuzhiyun #define seq_after(a, b) ((s32)((a) - (b)) > 0)
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /* Prototypes. */
256*4882a593Smuzhiyun static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
257*4882a593Smuzhiyun struct file *file, unsigned int cmd, unsigned long arg);
258*4882a593Smuzhiyun static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb);
259*4882a593Smuzhiyun static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
260*4882a593Smuzhiyun static void ppp_push(struct ppp *ppp);
261*4882a593Smuzhiyun static void ppp_channel_push(struct channel *pch);
262*4882a593Smuzhiyun static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
263*4882a593Smuzhiyun struct channel *pch);
264*4882a593Smuzhiyun static void ppp_receive_error(struct ppp *ppp);
265*4882a593Smuzhiyun static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
266*4882a593Smuzhiyun static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
267*4882a593Smuzhiyun struct sk_buff *skb);
268*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
269*4882a593Smuzhiyun static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
270*4882a593Smuzhiyun struct channel *pch);
271*4882a593Smuzhiyun static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
272*4882a593Smuzhiyun static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
273*4882a593Smuzhiyun static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
274*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
275*4882a593Smuzhiyun static int ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data);
276*4882a593Smuzhiyun static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
277*4882a593Smuzhiyun static void ppp_ccp_closed(struct ppp *ppp);
278*4882a593Smuzhiyun static struct compressor *find_compressor(int type);
279*4882a593Smuzhiyun static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
280*4882a593Smuzhiyun static int ppp_create_interface(struct net *net, struct file *file, int *unit);
281*4882a593Smuzhiyun static void init_ppp_file(struct ppp_file *pf, int kind);
282*4882a593Smuzhiyun static void ppp_destroy_interface(struct ppp *ppp);
283*4882a593Smuzhiyun static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
284*4882a593Smuzhiyun static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
285*4882a593Smuzhiyun static int ppp_connect_channel(struct channel *pch, int unit);
286*4882a593Smuzhiyun static int ppp_disconnect_channel(struct channel *pch);
287*4882a593Smuzhiyun static void ppp_destroy_channel(struct channel *pch);
288*4882a593Smuzhiyun static int unit_get(struct idr *p, void *ptr, int min);
289*4882a593Smuzhiyun static int unit_set(struct idr *p, void *ptr, int n);
290*4882a593Smuzhiyun static void unit_put(struct idr *p, int n);
291*4882a593Smuzhiyun static void *unit_find(struct idr *p, int n);
292*4882a593Smuzhiyun static void ppp_setup(struct net_device *dev);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun static const struct net_device_ops ppp_netdev_ops;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun static struct class *ppp_class;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /* per net-namespace data */
ppp_pernet(struct net * net)299*4882a593Smuzhiyun static inline struct ppp_net *ppp_pernet(struct net *net)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun return net_generic(net, ppp_net_id);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun /* Translates a PPP protocol number to a NP index (NP == network protocol) */
proto_to_npindex(int proto)305*4882a593Smuzhiyun static inline int proto_to_npindex(int proto)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun switch (proto) {
308*4882a593Smuzhiyun case PPP_IP:
309*4882a593Smuzhiyun return NP_IP;
310*4882a593Smuzhiyun case PPP_IPV6:
311*4882a593Smuzhiyun return NP_IPV6;
312*4882a593Smuzhiyun case PPP_IPX:
313*4882a593Smuzhiyun return NP_IPX;
314*4882a593Smuzhiyun case PPP_AT:
315*4882a593Smuzhiyun return NP_AT;
316*4882a593Smuzhiyun case PPP_MPLS_UC:
317*4882a593Smuzhiyun return NP_MPLS_UC;
318*4882a593Smuzhiyun case PPP_MPLS_MC:
319*4882a593Smuzhiyun return NP_MPLS_MC;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun return -EINVAL;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun /* Translates an NP index into a PPP protocol number */
325*4882a593Smuzhiyun static const int npindex_to_proto[NUM_NP] = {
326*4882a593Smuzhiyun PPP_IP,
327*4882a593Smuzhiyun PPP_IPV6,
328*4882a593Smuzhiyun PPP_IPX,
329*4882a593Smuzhiyun PPP_AT,
330*4882a593Smuzhiyun PPP_MPLS_UC,
331*4882a593Smuzhiyun PPP_MPLS_MC,
332*4882a593Smuzhiyun };
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /* Translates an ethertype into an NP index */
ethertype_to_npindex(int ethertype)335*4882a593Smuzhiyun static inline int ethertype_to_npindex(int ethertype)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun switch (ethertype) {
338*4882a593Smuzhiyun case ETH_P_IP:
339*4882a593Smuzhiyun return NP_IP;
340*4882a593Smuzhiyun case ETH_P_IPV6:
341*4882a593Smuzhiyun return NP_IPV6;
342*4882a593Smuzhiyun case ETH_P_IPX:
343*4882a593Smuzhiyun return NP_IPX;
344*4882a593Smuzhiyun case ETH_P_PPPTALK:
345*4882a593Smuzhiyun case ETH_P_ATALK:
346*4882a593Smuzhiyun return NP_AT;
347*4882a593Smuzhiyun case ETH_P_MPLS_UC:
348*4882a593Smuzhiyun return NP_MPLS_UC;
349*4882a593Smuzhiyun case ETH_P_MPLS_MC:
350*4882a593Smuzhiyun return NP_MPLS_MC;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun return -1;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /* Translates an NP index into an ethertype */
356*4882a593Smuzhiyun static const int npindex_to_ethertype[NUM_NP] = {
357*4882a593Smuzhiyun ETH_P_IP,
358*4882a593Smuzhiyun ETH_P_IPV6,
359*4882a593Smuzhiyun ETH_P_IPX,
360*4882a593Smuzhiyun ETH_P_PPPTALK,
361*4882a593Smuzhiyun ETH_P_MPLS_UC,
362*4882a593Smuzhiyun ETH_P_MPLS_MC,
363*4882a593Smuzhiyun };
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /*
366*4882a593Smuzhiyun * Locking shorthand.
367*4882a593Smuzhiyun */
368*4882a593Smuzhiyun #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
369*4882a593Smuzhiyun #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
370*4882a593Smuzhiyun #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
371*4882a593Smuzhiyun #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
372*4882a593Smuzhiyun #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
373*4882a593Smuzhiyun ppp_recv_lock(ppp); } while (0)
374*4882a593Smuzhiyun #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
375*4882a593Smuzhiyun ppp_xmit_unlock(ppp); } while (0)
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /*
378*4882a593Smuzhiyun * /dev/ppp device routines.
379*4882a593Smuzhiyun * The /dev/ppp device is used by pppd to control the ppp unit.
380*4882a593Smuzhiyun * It supports the read, write, ioctl and poll functions.
381*4882a593Smuzhiyun * Open instances of /dev/ppp can be in one of three states:
382*4882a593Smuzhiyun * unattached, attached to a ppp unit, or attached to a ppp channel.
383*4882a593Smuzhiyun */
ppp_open(struct inode * inode,struct file * file)384*4882a593Smuzhiyun static int ppp_open(struct inode *inode, struct file *file)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun /*
387*4882a593Smuzhiyun * This could (should?) be enforced by the permissions on /dev/ppp.
388*4882a593Smuzhiyun */
389*4882a593Smuzhiyun if (!ns_capable(file->f_cred->user_ns, CAP_NET_ADMIN))
390*4882a593Smuzhiyun return -EPERM;
391*4882a593Smuzhiyun return 0;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
ppp_release(struct inode * unused,struct file * file)394*4882a593Smuzhiyun static int ppp_release(struct inode *unused, struct file *file)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun struct ppp_file *pf = file->private_data;
397*4882a593Smuzhiyun struct ppp *ppp;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun if (pf) {
400*4882a593Smuzhiyun file->private_data = NULL;
401*4882a593Smuzhiyun if (pf->kind == INTERFACE) {
402*4882a593Smuzhiyun ppp = PF_TO_PPP(pf);
403*4882a593Smuzhiyun rtnl_lock();
404*4882a593Smuzhiyun if (file == ppp->owner)
405*4882a593Smuzhiyun unregister_netdevice(ppp->dev);
406*4882a593Smuzhiyun rtnl_unlock();
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun if (refcount_dec_and_test(&pf->refcnt)) {
409*4882a593Smuzhiyun switch (pf->kind) {
410*4882a593Smuzhiyun case INTERFACE:
411*4882a593Smuzhiyun ppp_destroy_interface(PF_TO_PPP(pf));
412*4882a593Smuzhiyun break;
413*4882a593Smuzhiyun case CHANNEL:
414*4882a593Smuzhiyun ppp_destroy_channel(PF_TO_CHANNEL(pf));
415*4882a593Smuzhiyun break;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun return 0;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
ppp_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)422*4882a593Smuzhiyun static ssize_t ppp_read(struct file *file, char __user *buf,
423*4882a593Smuzhiyun size_t count, loff_t *ppos)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun struct ppp_file *pf = file->private_data;
426*4882a593Smuzhiyun DECLARE_WAITQUEUE(wait, current);
427*4882a593Smuzhiyun ssize_t ret;
428*4882a593Smuzhiyun struct sk_buff *skb = NULL;
429*4882a593Smuzhiyun struct iovec iov;
430*4882a593Smuzhiyun struct iov_iter to;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun ret = count;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun if (!pf)
435*4882a593Smuzhiyun return -ENXIO;
436*4882a593Smuzhiyun add_wait_queue(&pf->rwait, &wait);
437*4882a593Smuzhiyun for (;;) {
438*4882a593Smuzhiyun set_current_state(TASK_INTERRUPTIBLE);
439*4882a593Smuzhiyun skb = skb_dequeue(&pf->rq);
440*4882a593Smuzhiyun if (skb)
441*4882a593Smuzhiyun break;
442*4882a593Smuzhiyun ret = 0;
443*4882a593Smuzhiyun if (pf->dead)
444*4882a593Smuzhiyun break;
445*4882a593Smuzhiyun if (pf->kind == INTERFACE) {
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun * Return 0 (EOF) on an interface that has no
448*4882a593Smuzhiyun * channels connected, unless it is looping
449*4882a593Smuzhiyun * network traffic (demand mode).
450*4882a593Smuzhiyun */
451*4882a593Smuzhiyun struct ppp *ppp = PF_TO_PPP(pf);
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun ppp_recv_lock(ppp);
454*4882a593Smuzhiyun if (ppp->n_channels == 0 &&
455*4882a593Smuzhiyun (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
456*4882a593Smuzhiyun ppp_recv_unlock(ppp);
457*4882a593Smuzhiyun break;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun ppp_recv_unlock(ppp);
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun ret = -EAGAIN;
462*4882a593Smuzhiyun if (file->f_flags & O_NONBLOCK)
463*4882a593Smuzhiyun break;
464*4882a593Smuzhiyun ret = -ERESTARTSYS;
465*4882a593Smuzhiyun if (signal_pending(current))
466*4882a593Smuzhiyun break;
467*4882a593Smuzhiyun schedule();
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun set_current_state(TASK_RUNNING);
470*4882a593Smuzhiyun remove_wait_queue(&pf->rwait, &wait);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun if (!skb)
473*4882a593Smuzhiyun goto out;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun ret = -EOVERFLOW;
476*4882a593Smuzhiyun if (skb->len > count)
477*4882a593Smuzhiyun goto outf;
478*4882a593Smuzhiyun ret = -EFAULT;
479*4882a593Smuzhiyun iov.iov_base = buf;
480*4882a593Smuzhiyun iov.iov_len = count;
481*4882a593Smuzhiyun iov_iter_init(&to, READ, &iov, 1, count);
482*4882a593Smuzhiyun if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
483*4882a593Smuzhiyun goto outf;
484*4882a593Smuzhiyun ret = skb->len;
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun outf:
487*4882a593Smuzhiyun kfree_skb(skb);
488*4882a593Smuzhiyun out:
489*4882a593Smuzhiyun return ret;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
ppp_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)492*4882a593Smuzhiyun static ssize_t ppp_write(struct file *file, const char __user *buf,
493*4882a593Smuzhiyun size_t count, loff_t *ppos)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun struct ppp_file *pf = file->private_data;
496*4882a593Smuzhiyun struct sk_buff *skb;
497*4882a593Smuzhiyun ssize_t ret;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun if (!pf)
500*4882a593Smuzhiyun return -ENXIO;
501*4882a593Smuzhiyun /* All PPP packets should start with the 2-byte protocol */
502*4882a593Smuzhiyun if (count < PPP_PROTO_LEN)
503*4882a593Smuzhiyun return -EINVAL;
504*4882a593Smuzhiyun ret = -ENOMEM;
505*4882a593Smuzhiyun skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
506*4882a593Smuzhiyun if (!skb)
507*4882a593Smuzhiyun goto out;
508*4882a593Smuzhiyun skb_reserve(skb, pf->hdrlen);
509*4882a593Smuzhiyun ret = -EFAULT;
510*4882a593Smuzhiyun if (copy_from_user(skb_put(skb, count), buf, count)) {
511*4882a593Smuzhiyun kfree_skb(skb);
512*4882a593Smuzhiyun goto out;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun switch (pf->kind) {
516*4882a593Smuzhiyun case INTERFACE:
517*4882a593Smuzhiyun ppp_xmit_process(PF_TO_PPP(pf), skb);
518*4882a593Smuzhiyun break;
519*4882a593Smuzhiyun case CHANNEL:
520*4882a593Smuzhiyun skb_queue_tail(&pf->xq, skb);
521*4882a593Smuzhiyun ppp_channel_push(PF_TO_CHANNEL(pf));
522*4882a593Smuzhiyun break;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun ret = count;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun out:
528*4882a593Smuzhiyun return ret;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun /* No kernel lock - fine */
ppp_poll(struct file * file,poll_table * wait)532*4882a593Smuzhiyun static __poll_t ppp_poll(struct file *file, poll_table *wait)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun struct ppp_file *pf = file->private_data;
535*4882a593Smuzhiyun __poll_t mask;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun if (!pf)
538*4882a593Smuzhiyun return 0;
539*4882a593Smuzhiyun poll_wait(file, &pf->rwait, wait);
540*4882a593Smuzhiyun mask = EPOLLOUT | EPOLLWRNORM;
541*4882a593Smuzhiyun if (skb_peek(&pf->rq))
542*4882a593Smuzhiyun mask |= EPOLLIN | EPOLLRDNORM;
543*4882a593Smuzhiyun if (pf->dead)
544*4882a593Smuzhiyun mask |= EPOLLHUP;
545*4882a593Smuzhiyun else if (pf->kind == INTERFACE) {
546*4882a593Smuzhiyun /* see comment in ppp_read */
547*4882a593Smuzhiyun struct ppp *ppp = PF_TO_PPP(pf);
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun ppp_recv_lock(ppp);
550*4882a593Smuzhiyun if (ppp->n_channels == 0 &&
551*4882a593Smuzhiyun (ppp->flags & SC_LOOP_TRAFFIC) == 0)
552*4882a593Smuzhiyun mask |= EPOLLIN | EPOLLRDNORM;
553*4882a593Smuzhiyun ppp_recv_unlock(ppp);
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun return mask;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
get_filter(struct sock_fprog * uprog)560*4882a593Smuzhiyun static struct bpf_prog *get_filter(struct sock_fprog *uprog)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun struct sock_fprog_kern fprog;
563*4882a593Smuzhiyun struct bpf_prog *res = NULL;
564*4882a593Smuzhiyun int err;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun if (!uprog->len)
567*4882a593Smuzhiyun return NULL;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /* uprog->len is unsigned short, so no overflow here */
570*4882a593Smuzhiyun fprog.len = uprog->len;
571*4882a593Smuzhiyun fprog.filter = memdup_user(uprog->filter,
572*4882a593Smuzhiyun uprog->len * sizeof(struct sock_filter));
573*4882a593Smuzhiyun if (IS_ERR(fprog.filter))
574*4882a593Smuzhiyun return ERR_CAST(fprog.filter);
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun err = bpf_prog_create(&res, &fprog);
577*4882a593Smuzhiyun kfree(fprog.filter);
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun return err ? ERR_PTR(err) : res;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
ppp_get_filter(struct sock_fprog __user * p)582*4882a593Smuzhiyun static struct bpf_prog *ppp_get_filter(struct sock_fprog __user *p)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun struct sock_fprog uprog;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun if (copy_from_user(&uprog, p, sizeof(struct sock_fprog)))
587*4882a593Smuzhiyun return ERR_PTR(-EFAULT);
588*4882a593Smuzhiyun return get_filter(&uprog);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
592*4882a593Smuzhiyun struct sock_fprog32 {
593*4882a593Smuzhiyun unsigned short len;
594*4882a593Smuzhiyun compat_caddr_t filter;
595*4882a593Smuzhiyun };
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun #define PPPIOCSPASS32 _IOW('t', 71, struct sock_fprog32)
598*4882a593Smuzhiyun #define PPPIOCSACTIVE32 _IOW('t', 70, struct sock_fprog32)
599*4882a593Smuzhiyun
compat_ppp_get_filter(struct sock_fprog32 __user * p)600*4882a593Smuzhiyun static struct bpf_prog *compat_ppp_get_filter(struct sock_fprog32 __user *p)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun struct sock_fprog32 uprog32;
603*4882a593Smuzhiyun struct sock_fprog uprog;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun if (copy_from_user(&uprog32, p, sizeof(struct sock_fprog32)))
606*4882a593Smuzhiyun return ERR_PTR(-EFAULT);
607*4882a593Smuzhiyun uprog.len = uprog32.len;
608*4882a593Smuzhiyun uprog.filter = compat_ptr(uprog32.filter);
609*4882a593Smuzhiyun return get_filter(&uprog);
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun #endif
612*4882a593Smuzhiyun #endif
613*4882a593Smuzhiyun
ppp_ioctl(struct file * file,unsigned int cmd,unsigned long arg)614*4882a593Smuzhiyun static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun struct ppp_file *pf;
617*4882a593Smuzhiyun struct ppp *ppp;
618*4882a593Smuzhiyun int err = -EFAULT, val, val2, i;
619*4882a593Smuzhiyun struct ppp_idle32 idle32;
620*4882a593Smuzhiyun struct ppp_idle64 idle64;
621*4882a593Smuzhiyun struct npioctl npi;
622*4882a593Smuzhiyun int unit, cflags;
623*4882a593Smuzhiyun struct slcompress *vj;
624*4882a593Smuzhiyun void __user *argp = (void __user *)arg;
625*4882a593Smuzhiyun int __user *p = argp;
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun mutex_lock(&ppp_mutex);
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun pf = file->private_data;
630*4882a593Smuzhiyun if (!pf) {
631*4882a593Smuzhiyun err = ppp_unattached_ioctl(current->nsproxy->net_ns,
632*4882a593Smuzhiyun pf, file, cmd, arg);
633*4882a593Smuzhiyun goto out;
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun if (cmd == PPPIOCDETACH) {
637*4882a593Smuzhiyun /*
638*4882a593Smuzhiyun * PPPIOCDETACH is no longer supported as it was heavily broken,
639*4882a593Smuzhiyun * and is only known to have been used by pppd older than
640*4882a593Smuzhiyun * ppp-2.4.2 (released November 2003).
641*4882a593Smuzhiyun */
642*4882a593Smuzhiyun pr_warn_once("%s (%d) used obsolete PPPIOCDETACH ioctl\n",
643*4882a593Smuzhiyun current->comm, current->pid);
644*4882a593Smuzhiyun err = -EINVAL;
645*4882a593Smuzhiyun goto out;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun if (pf->kind == CHANNEL) {
649*4882a593Smuzhiyun struct channel *pch;
650*4882a593Smuzhiyun struct ppp_channel *chan;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun pch = PF_TO_CHANNEL(pf);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun switch (cmd) {
655*4882a593Smuzhiyun case PPPIOCCONNECT:
656*4882a593Smuzhiyun if (get_user(unit, p))
657*4882a593Smuzhiyun break;
658*4882a593Smuzhiyun err = ppp_connect_channel(pch, unit);
659*4882a593Smuzhiyun break;
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun case PPPIOCDISCONN:
662*4882a593Smuzhiyun err = ppp_disconnect_channel(pch);
663*4882a593Smuzhiyun break;
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun default:
666*4882a593Smuzhiyun down_read(&pch->chan_sem);
667*4882a593Smuzhiyun chan = pch->chan;
668*4882a593Smuzhiyun err = -ENOTTY;
669*4882a593Smuzhiyun if (chan && chan->ops->ioctl)
670*4882a593Smuzhiyun err = chan->ops->ioctl(chan, cmd, arg);
671*4882a593Smuzhiyun up_read(&pch->chan_sem);
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun goto out;
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun if (pf->kind != INTERFACE) {
677*4882a593Smuzhiyun /* can't happen */
678*4882a593Smuzhiyun pr_err("PPP: not interface or channel??\n");
679*4882a593Smuzhiyun err = -EINVAL;
680*4882a593Smuzhiyun goto out;
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun ppp = PF_TO_PPP(pf);
684*4882a593Smuzhiyun switch (cmd) {
685*4882a593Smuzhiyun case PPPIOCSMRU:
686*4882a593Smuzhiyun if (get_user(val, p))
687*4882a593Smuzhiyun break;
688*4882a593Smuzhiyun ppp->mru = val;
689*4882a593Smuzhiyun err = 0;
690*4882a593Smuzhiyun break;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun case PPPIOCSFLAGS:
693*4882a593Smuzhiyun if (get_user(val, p))
694*4882a593Smuzhiyun break;
695*4882a593Smuzhiyun ppp_lock(ppp);
696*4882a593Smuzhiyun cflags = ppp->flags & ~val;
697*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
698*4882a593Smuzhiyun if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
699*4882a593Smuzhiyun ppp->nextseq = 0;
700*4882a593Smuzhiyun #endif
701*4882a593Smuzhiyun ppp->flags = val & SC_FLAG_BITS;
702*4882a593Smuzhiyun ppp_unlock(ppp);
703*4882a593Smuzhiyun if (cflags & SC_CCP_OPEN)
704*4882a593Smuzhiyun ppp_ccp_closed(ppp);
705*4882a593Smuzhiyun err = 0;
706*4882a593Smuzhiyun break;
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun case PPPIOCGFLAGS:
709*4882a593Smuzhiyun val = ppp->flags | ppp->xstate | ppp->rstate;
710*4882a593Smuzhiyun if (put_user(val, p))
711*4882a593Smuzhiyun break;
712*4882a593Smuzhiyun err = 0;
713*4882a593Smuzhiyun break;
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun case PPPIOCSCOMPRESS:
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun struct ppp_option_data data;
718*4882a593Smuzhiyun if (copy_from_user(&data, argp, sizeof(data)))
719*4882a593Smuzhiyun err = -EFAULT;
720*4882a593Smuzhiyun else
721*4882a593Smuzhiyun err = ppp_set_compress(ppp, &data);
722*4882a593Smuzhiyun break;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun case PPPIOCGUNIT:
725*4882a593Smuzhiyun if (put_user(ppp->file.index, p))
726*4882a593Smuzhiyun break;
727*4882a593Smuzhiyun err = 0;
728*4882a593Smuzhiyun break;
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun case PPPIOCSDEBUG:
731*4882a593Smuzhiyun if (get_user(val, p))
732*4882a593Smuzhiyun break;
733*4882a593Smuzhiyun ppp->debug = val;
734*4882a593Smuzhiyun err = 0;
735*4882a593Smuzhiyun break;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun case PPPIOCGDEBUG:
738*4882a593Smuzhiyun if (put_user(ppp->debug, p))
739*4882a593Smuzhiyun break;
740*4882a593Smuzhiyun err = 0;
741*4882a593Smuzhiyun break;
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun case PPPIOCGIDLE32:
744*4882a593Smuzhiyun idle32.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
745*4882a593Smuzhiyun idle32.recv_idle = (jiffies - ppp->last_recv) / HZ;
746*4882a593Smuzhiyun if (copy_to_user(argp, &idle32, sizeof(idle32)))
747*4882a593Smuzhiyun break;
748*4882a593Smuzhiyun err = 0;
749*4882a593Smuzhiyun break;
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun case PPPIOCGIDLE64:
752*4882a593Smuzhiyun idle64.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
753*4882a593Smuzhiyun idle64.recv_idle = (jiffies - ppp->last_recv) / HZ;
754*4882a593Smuzhiyun if (copy_to_user(argp, &idle64, sizeof(idle64)))
755*4882a593Smuzhiyun break;
756*4882a593Smuzhiyun err = 0;
757*4882a593Smuzhiyun break;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun case PPPIOCSMAXCID:
760*4882a593Smuzhiyun if (get_user(val, p))
761*4882a593Smuzhiyun break;
762*4882a593Smuzhiyun val2 = 15;
763*4882a593Smuzhiyun if ((val >> 16) != 0) {
764*4882a593Smuzhiyun val2 = val >> 16;
765*4882a593Smuzhiyun val &= 0xffff;
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun vj = slhc_init(val2+1, val+1);
768*4882a593Smuzhiyun if (IS_ERR(vj)) {
769*4882a593Smuzhiyun err = PTR_ERR(vj);
770*4882a593Smuzhiyun break;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun ppp_lock(ppp);
773*4882a593Smuzhiyun if (ppp->vj)
774*4882a593Smuzhiyun slhc_free(ppp->vj);
775*4882a593Smuzhiyun ppp->vj = vj;
776*4882a593Smuzhiyun ppp_unlock(ppp);
777*4882a593Smuzhiyun err = 0;
778*4882a593Smuzhiyun break;
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun case PPPIOCGNPMODE:
781*4882a593Smuzhiyun case PPPIOCSNPMODE:
782*4882a593Smuzhiyun if (copy_from_user(&npi, argp, sizeof(npi)))
783*4882a593Smuzhiyun break;
784*4882a593Smuzhiyun err = proto_to_npindex(npi.protocol);
785*4882a593Smuzhiyun if (err < 0)
786*4882a593Smuzhiyun break;
787*4882a593Smuzhiyun i = err;
788*4882a593Smuzhiyun if (cmd == PPPIOCGNPMODE) {
789*4882a593Smuzhiyun err = -EFAULT;
790*4882a593Smuzhiyun npi.mode = ppp->npmode[i];
791*4882a593Smuzhiyun if (copy_to_user(argp, &npi, sizeof(npi)))
792*4882a593Smuzhiyun break;
793*4882a593Smuzhiyun } else {
794*4882a593Smuzhiyun ppp->npmode[i] = npi.mode;
795*4882a593Smuzhiyun /* we may be able to transmit more packets now (??) */
796*4882a593Smuzhiyun netif_wake_queue(ppp->dev);
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun err = 0;
799*4882a593Smuzhiyun break;
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
802*4882a593Smuzhiyun case PPPIOCSPASS:
803*4882a593Smuzhiyun case PPPIOCSACTIVE:
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun struct bpf_prog *filter = ppp_get_filter(argp);
806*4882a593Smuzhiyun struct bpf_prog **which;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun if (IS_ERR(filter)) {
809*4882a593Smuzhiyun err = PTR_ERR(filter);
810*4882a593Smuzhiyun break;
811*4882a593Smuzhiyun }
812*4882a593Smuzhiyun if (cmd == PPPIOCSPASS)
813*4882a593Smuzhiyun which = &ppp->pass_filter;
814*4882a593Smuzhiyun else
815*4882a593Smuzhiyun which = &ppp->active_filter;
816*4882a593Smuzhiyun ppp_lock(ppp);
817*4882a593Smuzhiyun if (*which)
818*4882a593Smuzhiyun bpf_prog_destroy(*which);
819*4882a593Smuzhiyun *which = filter;
820*4882a593Smuzhiyun ppp_unlock(ppp);
821*4882a593Smuzhiyun err = 0;
822*4882a593Smuzhiyun break;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun #endif /* CONFIG_PPP_FILTER */
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
827*4882a593Smuzhiyun case PPPIOCSMRRU:
828*4882a593Smuzhiyun if (get_user(val, p))
829*4882a593Smuzhiyun break;
830*4882a593Smuzhiyun ppp_recv_lock(ppp);
831*4882a593Smuzhiyun ppp->mrru = val;
832*4882a593Smuzhiyun ppp_recv_unlock(ppp);
833*4882a593Smuzhiyun err = 0;
834*4882a593Smuzhiyun break;
835*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun default:
838*4882a593Smuzhiyun err = -ENOTTY;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun out:
842*4882a593Smuzhiyun mutex_unlock(&ppp_mutex);
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun return err;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
848*4882a593Smuzhiyun struct ppp_option_data32 {
849*4882a593Smuzhiyun compat_uptr_t ptr;
850*4882a593Smuzhiyun u32 length;
851*4882a593Smuzhiyun compat_int_t transmit;
852*4882a593Smuzhiyun };
853*4882a593Smuzhiyun #define PPPIOCSCOMPRESS32 _IOW('t', 77, struct ppp_option_data32)
854*4882a593Smuzhiyun
ppp_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)855*4882a593Smuzhiyun static long ppp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
856*4882a593Smuzhiyun {
857*4882a593Smuzhiyun struct ppp_file *pf;
858*4882a593Smuzhiyun int err = -ENOIOCTLCMD;
859*4882a593Smuzhiyun void __user *argp = (void __user *)arg;
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun mutex_lock(&ppp_mutex);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun pf = file->private_data;
864*4882a593Smuzhiyun if (pf && pf->kind == INTERFACE) {
865*4882a593Smuzhiyun struct ppp *ppp = PF_TO_PPP(pf);
866*4882a593Smuzhiyun switch (cmd) {
867*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
868*4882a593Smuzhiyun case PPPIOCSPASS32:
869*4882a593Smuzhiyun case PPPIOCSACTIVE32:
870*4882a593Smuzhiyun {
871*4882a593Smuzhiyun struct bpf_prog *filter = compat_ppp_get_filter(argp);
872*4882a593Smuzhiyun struct bpf_prog **which;
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun if (IS_ERR(filter)) {
875*4882a593Smuzhiyun err = PTR_ERR(filter);
876*4882a593Smuzhiyun break;
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun if (cmd == PPPIOCSPASS32)
879*4882a593Smuzhiyun which = &ppp->pass_filter;
880*4882a593Smuzhiyun else
881*4882a593Smuzhiyun which = &ppp->active_filter;
882*4882a593Smuzhiyun ppp_lock(ppp);
883*4882a593Smuzhiyun if (*which)
884*4882a593Smuzhiyun bpf_prog_destroy(*which);
885*4882a593Smuzhiyun *which = filter;
886*4882a593Smuzhiyun ppp_unlock(ppp);
887*4882a593Smuzhiyun err = 0;
888*4882a593Smuzhiyun break;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun #endif /* CONFIG_PPP_FILTER */
891*4882a593Smuzhiyun case PPPIOCSCOMPRESS32:
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun struct ppp_option_data32 data32;
894*4882a593Smuzhiyun if (copy_from_user(&data32, argp, sizeof(data32))) {
895*4882a593Smuzhiyun err = -EFAULT;
896*4882a593Smuzhiyun } else {
897*4882a593Smuzhiyun struct ppp_option_data data = {
898*4882a593Smuzhiyun .ptr = compat_ptr(data32.ptr),
899*4882a593Smuzhiyun .length = data32.length,
900*4882a593Smuzhiyun .transmit = data32.transmit
901*4882a593Smuzhiyun };
902*4882a593Smuzhiyun err = ppp_set_compress(ppp, &data);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun break;
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun mutex_unlock(&ppp_mutex);
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun /* all other commands have compatible arguments */
911*4882a593Smuzhiyun if (err == -ENOIOCTLCMD)
912*4882a593Smuzhiyun err = ppp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun return err;
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun #endif
917*4882a593Smuzhiyun
ppp_unattached_ioctl(struct net * net,struct ppp_file * pf,struct file * file,unsigned int cmd,unsigned long arg)918*4882a593Smuzhiyun static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
919*4882a593Smuzhiyun struct file *file, unsigned int cmd, unsigned long arg)
920*4882a593Smuzhiyun {
921*4882a593Smuzhiyun int unit, err = -EFAULT;
922*4882a593Smuzhiyun struct ppp *ppp;
923*4882a593Smuzhiyun struct channel *chan;
924*4882a593Smuzhiyun struct ppp_net *pn;
925*4882a593Smuzhiyun int __user *p = (int __user *)arg;
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun switch (cmd) {
928*4882a593Smuzhiyun case PPPIOCNEWUNIT:
929*4882a593Smuzhiyun /* Create a new ppp unit */
930*4882a593Smuzhiyun if (get_user(unit, p))
931*4882a593Smuzhiyun break;
932*4882a593Smuzhiyun err = ppp_create_interface(net, file, &unit);
933*4882a593Smuzhiyun if (err < 0)
934*4882a593Smuzhiyun break;
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun err = -EFAULT;
937*4882a593Smuzhiyun if (put_user(unit, p))
938*4882a593Smuzhiyun break;
939*4882a593Smuzhiyun err = 0;
940*4882a593Smuzhiyun break;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun case PPPIOCATTACH:
943*4882a593Smuzhiyun /* Attach to an existing ppp unit */
944*4882a593Smuzhiyun if (get_user(unit, p))
945*4882a593Smuzhiyun break;
946*4882a593Smuzhiyun err = -ENXIO;
947*4882a593Smuzhiyun pn = ppp_pernet(net);
948*4882a593Smuzhiyun mutex_lock(&pn->all_ppp_mutex);
949*4882a593Smuzhiyun ppp = ppp_find_unit(pn, unit);
950*4882a593Smuzhiyun if (ppp) {
951*4882a593Smuzhiyun refcount_inc(&ppp->file.refcnt);
952*4882a593Smuzhiyun file->private_data = &ppp->file;
953*4882a593Smuzhiyun err = 0;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun mutex_unlock(&pn->all_ppp_mutex);
956*4882a593Smuzhiyun break;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun case PPPIOCATTCHAN:
959*4882a593Smuzhiyun if (get_user(unit, p))
960*4882a593Smuzhiyun break;
961*4882a593Smuzhiyun err = -ENXIO;
962*4882a593Smuzhiyun pn = ppp_pernet(net);
963*4882a593Smuzhiyun spin_lock_bh(&pn->all_channels_lock);
964*4882a593Smuzhiyun chan = ppp_find_channel(pn, unit);
965*4882a593Smuzhiyun if (chan) {
966*4882a593Smuzhiyun refcount_inc(&chan->file.refcnt);
967*4882a593Smuzhiyun file->private_data = &chan->file;
968*4882a593Smuzhiyun err = 0;
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun spin_unlock_bh(&pn->all_channels_lock);
971*4882a593Smuzhiyun break;
972*4882a593Smuzhiyun
973*4882a593Smuzhiyun default:
974*4882a593Smuzhiyun err = -ENOTTY;
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun return err;
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun static const struct file_operations ppp_device_fops = {
981*4882a593Smuzhiyun .owner = THIS_MODULE,
982*4882a593Smuzhiyun .read = ppp_read,
983*4882a593Smuzhiyun .write = ppp_write,
984*4882a593Smuzhiyun .poll = ppp_poll,
985*4882a593Smuzhiyun .unlocked_ioctl = ppp_ioctl,
986*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
987*4882a593Smuzhiyun .compat_ioctl = ppp_compat_ioctl,
988*4882a593Smuzhiyun #endif
989*4882a593Smuzhiyun .open = ppp_open,
990*4882a593Smuzhiyun .release = ppp_release,
991*4882a593Smuzhiyun .llseek = noop_llseek,
992*4882a593Smuzhiyun };
993*4882a593Smuzhiyun
ppp_init_net(struct net * net)994*4882a593Smuzhiyun static __net_init int ppp_init_net(struct net *net)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun struct ppp_net *pn = net_generic(net, ppp_net_id);
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun idr_init(&pn->units_idr);
999*4882a593Smuzhiyun mutex_init(&pn->all_ppp_mutex);
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun INIT_LIST_HEAD(&pn->all_channels);
1002*4882a593Smuzhiyun INIT_LIST_HEAD(&pn->new_channels);
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun spin_lock_init(&pn->all_channels_lock);
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun return 0;
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun
ppp_exit_net(struct net * net)1009*4882a593Smuzhiyun static __net_exit void ppp_exit_net(struct net *net)
1010*4882a593Smuzhiyun {
1011*4882a593Smuzhiyun struct ppp_net *pn = net_generic(net, ppp_net_id);
1012*4882a593Smuzhiyun struct net_device *dev;
1013*4882a593Smuzhiyun struct net_device *aux;
1014*4882a593Smuzhiyun struct ppp *ppp;
1015*4882a593Smuzhiyun LIST_HEAD(list);
1016*4882a593Smuzhiyun int id;
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun rtnl_lock();
1019*4882a593Smuzhiyun for_each_netdev_safe(net, dev, aux) {
1020*4882a593Smuzhiyun if (dev->netdev_ops == &ppp_netdev_ops)
1021*4882a593Smuzhiyun unregister_netdevice_queue(dev, &list);
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun idr_for_each_entry(&pn->units_idr, ppp, id)
1025*4882a593Smuzhiyun /* Skip devices already unregistered by previous loop */
1026*4882a593Smuzhiyun if (!net_eq(dev_net(ppp->dev), net))
1027*4882a593Smuzhiyun unregister_netdevice_queue(ppp->dev, &list);
1028*4882a593Smuzhiyun
1029*4882a593Smuzhiyun unregister_netdevice_many(&list);
1030*4882a593Smuzhiyun rtnl_unlock();
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun mutex_destroy(&pn->all_ppp_mutex);
1033*4882a593Smuzhiyun idr_destroy(&pn->units_idr);
1034*4882a593Smuzhiyun WARN_ON_ONCE(!list_empty(&pn->all_channels));
1035*4882a593Smuzhiyun WARN_ON_ONCE(!list_empty(&pn->new_channels));
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun static struct pernet_operations ppp_net_ops = {
1039*4882a593Smuzhiyun .init = ppp_init_net,
1040*4882a593Smuzhiyun .exit = ppp_exit_net,
1041*4882a593Smuzhiyun .id = &ppp_net_id,
1042*4882a593Smuzhiyun .size = sizeof(struct ppp_net),
1043*4882a593Smuzhiyun };
1044*4882a593Smuzhiyun
ppp_unit_register(struct ppp * ppp,int unit,bool ifname_is_set)1045*4882a593Smuzhiyun static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1048*4882a593Smuzhiyun int ret;
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun mutex_lock(&pn->all_ppp_mutex);
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun if (unit < 0) {
1053*4882a593Smuzhiyun ret = unit_get(&pn->units_idr, ppp, 0);
1054*4882a593Smuzhiyun if (ret < 0)
1055*4882a593Smuzhiyun goto err;
1056*4882a593Smuzhiyun if (!ifname_is_set) {
1057*4882a593Smuzhiyun while (1) {
1058*4882a593Smuzhiyun snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ret);
1059*4882a593Smuzhiyun if (!__dev_get_by_name(ppp->ppp_net, ppp->dev->name))
1060*4882a593Smuzhiyun break;
1061*4882a593Smuzhiyun unit_put(&pn->units_idr, ret);
1062*4882a593Smuzhiyun ret = unit_get(&pn->units_idr, ppp, ret + 1);
1063*4882a593Smuzhiyun if (ret < 0)
1064*4882a593Smuzhiyun goto err;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun } else {
1068*4882a593Smuzhiyun /* Caller asked for a specific unit number. Fail with -EEXIST
1069*4882a593Smuzhiyun * if unavailable. For backward compatibility, return -EEXIST
1070*4882a593Smuzhiyun * too if idr allocation fails; this makes pppd retry without
1071*4882a593Smuzhiyun * requesting a specific unit number.
1072*4882a593Smuzhiyun */
1073*4882a593Smuzhiyun if (unit_find(&pn->units_idr, unit)) {
1074*4882a593Smuzhiyun ret = -EEXIST;
1075*4882a593Smuzhiyun goto err;
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun ret = unit_set(&pn->units_idr, ppp, unit);
1078*4882a593Smuzhiyun if (ret < 0) {
1079*4882a593Smuzhiyun /* Rewrite error for backward compatibility */
1080*4882a593Smuzhiyun ret = -EEXIST;
1081*4882a593Smuzhiyun goto err;
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun ppp->file.index = ret;
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun if (!ifname_is_set)
1087*4882a593Smuzhiyun snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun mutex_unlock(&pn->all_ppp_mutex);
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun ret = register_netdevice(ppp->dev);
1092*4882a593Smuzhiyun if (ret < 0)
1093*4882a593Smuzhiyun goto err_unit;
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun atomic_inc(&ppp_unit_count);
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun return 0;
1098*4882a593Smuzhiyun
1099*4882a593Smuzhiyun err_unit:
1100*4882a593Smuzhiyun mutex_lock(&pn->all_ppp_mutex);
1101*4882a593Smuzhiyun unit_put(&pn->units_idr, ppp->file.index);
1102*4882a593Smuzhiyun err:
1103*4882a593Smuzhiyun mutex_unlock(&pn->all_ppp_mutex);
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun return ret;
1106*4882a593Smuzhiyun }
1107*4882a593Smuzhiyun
ppp_dev_configure(struct net * src_net,struct net_device * dev,const struct ppp_config * conf)1108*4882a593Smuzhiyun static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
1109*4882a593Smuzhiyun const struct ppp_config *conf)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun struct ppp *ppp = netdev_priv(dev);
1112*4882a593Smuzhiyun int indx;
1113*4882a593Smuzhiyun int err;
1114*4882a593Smuzhiyun int cpu;
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun ppp->dev = dev;
1117*4882a593Smuzhiyun ppp->ppp_net = src_net;
1118*4882a593Smuzhiyun ppp->mru = PPP_MRU;
1119*4882a593Smuzhiyun ppp->owner = conf->file;
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun init_ppp_file(&ppp->file, INTERFACE);
1122*4882a593Smuzhiyun ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun for (indx = 0; indx < NUM_NP; ++indx)
1125*4882a593Smuzhiyun ppp->npmode[indx] = NPMODE_PASS;
1126*4882a593Smuzhiyun INIT_LIST_HEAD(&ppp->channels);
1127*4882a593Smuzhiyun spin_lock_init(&ppp->rlock);
1128*4882a593Smuzhiyun spin_lock_init(&ppp->wlock);
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun ppp->xmit_recursion = alloc_percpu(int);
1131*4882a593Smuzhiyun if (!ppp->xmit_recursion) {
1132*4882a593Smuzhiyun err = -ENOMEM;
1133*4882a593Smuzhiyun goto err1;
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun for_each_possible_cpu(cpu)
1136*4882a593Smuzhiyun (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
1139*4882a593Smuzhiyun ppp->minseq = -1;
1140*4882a593Smuzhiyun skb_queue_head_init(&ppp->mrq);
1141*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
1142*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
1143*4882a593Smuzhiyun ppp->pass_filter = NULL;
1144*4882a593Smuzhiyun ppp->active_filter = NULL;
1145*4882a593Smuzhiyun #endif /* CONFIG_PPP_FILTER */
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
1148*4882a593Smuzhiyun if (err < 0)
1149*4882a593Smuzhiyun goto err2;
1150*4882a593Smuzhiyun
1151*4882a593Smuzhiyun conf->file->private_data = &ppp->file;
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun return 0;
1154*4882a593Smuzhiyun err2:
1155*4882a593Smuzhiyun free_percpu(ppp->xmit_recursion);
1156*4882a593Smuzhiyun err1:
1157*4882a593Smuzhiyun return err;
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
1161*4882a593Smuzhiyun [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
1162*4882a593Smuzhiyun };
1163*4882a593Smuzhiyun
ppp_nl_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1164*4882a593Smuzhiyun static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],
1165*4882a593Smuzhiyun struct netlink_ext_ack *extack)
1166*4882a593Smuzhiyun {
1167*4882a593Smuzhiyun if (!data)
1168*4882a593Smuzhiyun return -EINVAL;
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun if (!data[IFLA_PPP_DEV_FD])
1171*4882a593Smuzhiyun return -EINVAL;
1172*4882a593Smuzhiyun if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
1173*4882a593Smuzhiyun return -EBADF;
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun return 0;
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun
ppp_nl_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1178*4882a593Smuzhiyun static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1179*4882a593Smuzhiyun struct nlattr *tb[], struct nlattr *data[],
1180*4882a593Smuzhiyun struct netlink_ext_ack *extack)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun struct ppp_config conf = {
1183*4882a593Smuzhiyun .unit = -1,
1184*4882a593Smuzhiyun .ifname_is_set = true,
1185*4882a593Smuzhiyun };
1186*4882a593Smuzhiyun struct file *file;
1187*4882a593Smuzhiyun int err;
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
1190*4882a593Smuzhiyun if (!file)
1191*4882a593Smuzhiyun return -EBADF;
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun /* rtnl_lock is already held here, but ppp_create_interface() locks
1194*4882a593Smuzhiyun * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
1195*4882a593Smuzhiyun * possible deadlock due to lock order inversion, at the cost of
1196*4882a593Smuzhiyun * pushing the problem back to userspace.
1197*4882a593Smuzhiyun */
1198*4882a593Smuzhiyun if (!mutex_trylock(&ppp_mutex)) {
1199*4882a593Smuzhiyun err = -EBUSY;
1200*4882a593Smuzhiyun goto out;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun if (file->f_op != &ppp_device_fops || file->private_data) {
1204*4882a593Smuzhiyun err = -EBADF;
1205*4882a593Smuzhiyun goto out_unlock;
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun conf.file = file;
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun /* Don't use device name generated by the rtnetlink layer when ifname
1211*4882a593Smuzhiyun * isn't specified. Let ppp_dev_configure() set the device name using
1212*4882a593Smuzhiyun * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
1213*4882a593Smuzhiyun * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
1214*4882a593Smuzhiyun */
1215*4882a593Smuzhiyun if (!tb[IFLA_IFNAME] || !nla_len(tb[IFLA_IFNAME]) || !*(char *)nla_data(tb[IFLA_IFNAME]))
1216*4882a593Smuzhiyun conf.ifname_is_set = false;
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun err = ppp_dev_configure(src_net, dev, &conf);
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun out_unlock:
1221*4882a593Smuzhiyun mutex_unlock(&ppp_mutex);
1222*4882a593Smuzhiyun out:
1223*4882a593Smuzhiyun fput(file);
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun return err;
1226*4882a593Smuzhiyun }
1227*4882a593Smuzhiyun
ppp_nl_dellink(struct net_device * dev,struct list_head * head)1228*4882a593Smuzhiyun static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
1229*4882a593Smuzhiyun {
1230*4882a593Smuzhiyun unregister_netdevice_queue(dev, head);
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun
ppp_nl_get_size(const struct net_device * dev)1233*4882a593Smuzhiyun static size_t ppp_nl_get_size(const struct net_device *dev)
1234*4882a593Smuzhiyun {
1235*4882a593Smuzhiyun return 0;
1236*4882a593Smuzhiyun }
1237*4882a593Smuzhiyun
ppp_nl_fill_info(struct sk_buff * skb,const struct net_device * dev)1238*4882a593Smuzhiyun static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1239*4882a593Smuzhiyun {
1240*4882a593Smuzhiyun return 0;
1241*4882a593Smuzhiyun }
1242*4882a593Smuzhiyun
ppp_nl_get_link_net(const struct net_device * dev)1243*4882a593Smuzhiyun static struct net *ppp_nl_get_link_net(const struct net_device *dev)
1244*4882a593Smuzhiyun {
1245*4882a593Smuzhiyun struct ppp *ppp = netdev_priv(dev);
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun return ppp->ppp_net;
1248*4882a593Smuzhiyun }
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun static struct rtnl_link_ops ppp_link_ops __read_mostly = {
1251*4882a593Smuzhiyun .kind = "ppp",
1252*4882a593Smuzhiyun .maxtype = IFLA_PPP_MAX,
1253*4882a593Smuzhiyun .policy = ppp_nl_policy,
1254*4882a593Smuzhiyun .priv_size = sizeof(struct ppp),
1255*4882a593Smuzhiyun .setup = ppp_setup,
1256*4882a593Smuzhiyun .validate = ppp_nl_validate,
1257*4882a593Smuzhiyun .newlink = ppp_nl_newlink,
1258*4882a593Smuzhiyun .dellink = ppp_nl_dellink,
1259*4882a593Smuzhiyun .get_size = ppp_nl_get_size,
1260*4882a593Smuzhiyun .fill_info = ppp_nl_fill_info,
1261*4882a593Smuzhiyun .get_link_net = ppp_nl_get_link_net,
1262*4882a593Smuzhiyun };
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun #define PPP_MAJOR 108
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun /* Called at boot time if ppp is compiled into the kernel,
1267*4882a593Smuzhiyun or at module load time (from init_module) if compiled as a module. */
ppp_init(void)1268*4882a593Smuzhiyun static int __init ppp_init(void)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun int err;
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun pr_info("PPP generic driver version " PPP_VERSION "\n");
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun err = register_pernet_device(&ppp_net_ops);
1275*4882a593Smuzhiyun if (err) {
1276*4882a593Smuzhiyun pr_err("failed to register PPP pernet device (%d)\n", err);
1277*4882a593Smuzhiyun goto out;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun
1280*4882a593Smuzhiyun err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
1281*4882a593Smuzhiyun if (err) {
1282*4882a593Smuzhiyun pr_err("failed to register PPP device (%d)\n", err);
1283*4882a593Smuzhiyun goto out_net;
1284*4882a593Smuzhiyun }
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun ppp_class = class_create(THIS_MODULE, "ppp");
1287*4882a593Smuzhiyun if (IS_ERR(ppp_class)) {
1288*4882a593Smuzhiyun err = PTR_ERR(ppp_class);
1289*4882a593Smuzhiyun goto out_chrdev;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun err = rtnl_link_register(&ppp_link_ops);
1293*4882a593Smuzhiyun if (err) {
1294*4882a593Smuzhiyun pr_err("failed to register rtnetlink PPP handler\n");
1295*4882a593Smuzhiyun goto out_class;
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun
1298*4882a593Smuzhiyun /* not a big deal if we fail here :-) */
1299*4882a593Smuzhiyun device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
1300*4882a593Smuzhiyun
1301*4882a593Smuzhiyun return 0;
1302*4882a593Smuzhiyun
1303*4882a593Smuzhiyun out_class:
1304*4882a593Smuzhiyun class_destroy(ppp_class);
1305*4882a593Smuzhiyun out_chrdev:
1306*4882a593Smuzhiyun unregister_chrdev(PPP_MAJOR, "ppp");
1307*4882a593Smuzhiyun out_net:
1308*4882a593Smuzhiyun unregister_pernet_device(&ppp_net_ops);
1309*4882a593Smuzhiyun out:
1310*4882a593Smuzhiyun return err;
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun /*
1314*4882a593Smuzhiyun * Network interface unit routines.
1315*4882a593Smuzhiyun */
1316*4882a593Smuzhiyun static netdev_tx_t
ppp_start_xmit(struct sk_buff * skb,struct net_device * dev)1317*4882a593Smuzhiyun ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1318*4882a593Smuzhiyun {
1319*4882a593Smuzhiyun struct ppp *ppp = netdev_priv(dev);
1320*4882a593Smuzhiyun int npi, proto;
1321*4882a593Smuzhiyun unsigned char *pp;
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun npi = ethertype_to_npindex(ntohs(skb->protocol));
1324*4882a593Smuzhiyun if (npi < 0)
1325*4882a593Smuzhiyun goto outf;
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun /* Drop, accept or reject the packet */
1328*4882a593Smuzhiyun switch (ppp->npmode[npi]) {
1329*4882a593Smuzhiyun case NPMODE_PASS:
1330*4882a593Smuzhiyun break;
1331*4882a593Smuzhiyun case NPMODE_QUEUE:
1332*4882a593Smuzhiyun /* it would be nice to have a way to tell the network
1333*4882a593Smuzhiyun system to queue this one up for later. */
1334*4882a593Smuzhiyun goto outf;
1335*4882a593Smuzhiyun case NPMODE_DROP:
1336*4882a593Smuzhiyun case NPMODE_ERROR:
1337*4882a593Smuzhiyun goto outf;
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun /* Put the 2-byte PPP protocol number on the front,
1341*4882a593Smuzhiyun making sure there is room for the address and control fields. */
1342*4882a593Smuzhiyun if (skb_cow_head(skb, PPP_HDRLEN))
1343*4882a593Smuzhiyun goto outf;
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun pp = skb_push(skb, 2);
1346*4882a593Smuzhiyun proto = npindex_to_proto[npi];
1347*4882a593Smuzhiyun put_unaligned_be16(proto, pp);
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
1350*4882a593Smuzhiyun ppp_xmit_process(ppp, skb);
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun return NETDEV_TX_OK;
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun outf:
1355*4882a593Smuzhiyun kfree_skb(skb);
1356*4882a593Smuzhiyun ++dev->stats.tx_dropped;
1357*4882a593Smuzhiyun return NETDEV_TX_OK;
1358*4882a593Smuzhiyun }
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun static int
ppp_net_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1361*4882a593Smuzhiyun ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1362*4882a593Smuzhiyun {
1363*4882a593Smuzhiyun struct ppp *ppp = netdev_priv(dev);
1364*4882a593Smuzhiyun int err = -EFAULT;
1365*4882a593Smuzhiyun void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1366*4882a593Smuzhiyun struct ppp_stats stats;
1367*4882a593Smuzhiyun struct ppp_comp_stats cstats;
1368*4882a593Smuzhiyun char *vers;
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun switch (cmd) {
1371*4882a593Smuzhiyun case SIOCGPPPSTATS:
1372*4882a593Smuzhiyun ppp_get_stats(ppp, &stats);
1373*4882a593Smuzhiyun if (copy_to_user(addr, &stats, sizeof(stats)))
1374*4882a593Smuzhiyun break;
1375*4882a593Smuzhiyun err = 0;
1376*4882a593Smuzhiyun break;
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun case SIOCGPPPCSTATS:
1379*4882a593Smuzhiyun memset(&cstats, 0, sizeof(cstats));
1380*4882a593Smuzhiyun if (ppp->xc_state)
1381*4882a593Smuzhiyun ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
1382*4882a593Smuzhiyun if (ppp->rc_state)
1383*4882a593Smuzhiyun ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1384*4882a593Smuzhiyun if (copy_to_user(addr, &cstats, sizeof(cstats)))
1385*4882a593Smuzhiyun break;
1386*4882a593Smuzhiyun err = 0;
1387*4882a593Smuzhiyun break;
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun case SIOCGPPPVER:
1390*4882a593Smuzhiyun vers = PPP_VERSION;
1391*4882a593Smuzhiyun if (copy_to_user(addr, vers, strlen(vers) + 1))
1392*4882a593Smuzhiyun break;
1393*4882a593Smuzhiyun err = 0;
1394*4882a593Smuzhiyun break;
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun default:
1397*4882a593Smuzhiyun err = -EINVAL;
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun return err;
1401*4882a593Smuzhiyun }
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun static void
ppp_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats64)1404*4882a593Smuzhiyun ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun struct ppp *ppp = netdev_priv(dev);
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun ppp_recv_lock(ppp);
1409*4882a593Smuzhiyun stats64->rx_packets = ppp->stats64.rx_packets;
1410*4882a593Smuzhiyun stats64->rx_bytes = ppp->stats64.rx_bytes;
1411*4882a593Smuzhiyun ppp_recv_unlock(ppp);
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun ppp_xmit_lock(ppp);
1414*4882a593Smuzhiyun stats64->tx_packets = ppp->stats64.tx_packets;
1415*4882a593Smuzhiyun stats64->tx_bytes = ppp->stats64.tx_bytes;
1416*4882a593Smuzhiyun ppp_xmit_unlock(ppp);
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun stats64->rx_errors = dev->stats.rx_errors;
1419*4882a593Smuzhiyun stats64->tx_errors = dev->stats.tx_errors;
1420*4882a593Smuzhiyun stats64->rx_dropped = dev->stats.rx_dropped;
1421*4882a593Smuzhiyun stats64->tx_dropped = dev->stats.tx_dropped;
1422*4882a593Smuzhiyun stats64->rx_length_errors = dev->stats.rx_length_errors;
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun
ppp_dev_init(struct net_device * dev)1425*4882a593Smuzhiyun static int ppp_dev_init(struct net_device *dev)
1426*4882a593Smuzhiyun {
1427*4882a593Smuzhiyun struct ppp *ppp;
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun netdev_lockdep_set_classes(dev);
1430*4882a593Smuzhiyun
1431*4882a593Smuzhiyun ppp = netdev_priv(dev);
1432*4882a593Smuzhiyun /* Let the netdevice take a reference on the ppp file. This ensures
1433*4882a593Smuzhiyun * that ppp_destroy_interface() won't run before the device gets
1434*4882a593Smuzhiyun * unregistered.
1435*4882a593Smuzhiyun */
1436*4882a593Smuzhiyun refcount_inc(&ppp->file.refcnt);
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun return 0;
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun
ppp_dev_uninit(struct net_device * dev)1441*4882a593Smuzhiyun static void ppp_dev_uninit(struct net_device *dev)
1442*4882a593Smuzhiyun {
1443*4882a593Smuzhiyun struct ppp *ppp = netdev_priv(dev);
1444*4882a593Smuzhiyun struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun ppp_lock(ppp);
1447*4882a593Smuzhiyun ppp->closing = 1;
1448*4882a593Smuzhiyun ppp_unlock(ppp);
1449*4882a593Smuzhiyun
1450*4882a593Smuzhiyun mutex_lock(&pn->all_ppp_mutex);
1451*4882a593Smuzhiyun unit_put(&pn->units_idr, ppp->file.index);
1452*4882a593Smuzhiyun mutex_unlock(&pn->all_ppp_mutex);
1453*4882a593Smuzhiyun
1454*4882a593Smuzhiyun ppp->owner = NULL;
1455*4882a593Smuzhiyun
1456*4882a593Smuzhiyun ppp->file.dead = 1;
1457*4882a593Smuzhiyun wake_up_interruptible(&ppp->file.rwait);
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun
ppp_dev_priv_destructor(struct net_device * dev)1460*4882a593Smuzhiyun static void ppp_dev_priv_destructor(struct net_device *dev)
1461*4882a593Smuzhiyun {
1462*4882a593Smuzhiyun struct ppp *ppp;
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun ppp = netdev_priv(dev);
1465*4882a593Smuzhiyun if (refcount_dec_and_test(&ppp->file.refcnt))
1466*4882a593Smuzhiyun ppp_destroy_interface(ppp);
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun
1469*4882a593Smuzhiyun static const struct net_device_ops ppp_netdev_ops = {
1470*4882a593Smuzhiyun .ndo_init = ppp_dev_init,
1471*4882a593Smuzhiyun .ndo_uninit = ppp_dev_uninit,
1472*4882a593Smuzhiyun .ndo_start_xmit = ppp_start_xmit,
1473*4882a593Smuzhiyun .ndo_do_ioctl = ppp_net_ioctl,
1474*4882a593Smuzhiyun .ndo_get_stats64 = ppp_get_stats64,
1475*4882a593Smuzhiyun };
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun static struct device_type ppp_type = {
1478*4882a593Smuzhiyun .name = "ppp",
1479*4882a593Smuzhiyun };
1480*4882a593Smuzhiyun
ppp_setup(struct net_device * dev)1481*4882a593Smuzhiyun static void ppp_setup(struct net_device *dev)
1482*4882a593Smuzhiyun {
1483*4882a593Smuzhiyun dev->netdev_ops = &ppp_netdev_ops;
1484*4882a593Smuzhiyun SET_NETDEV_DEVTYPE(dev, &ppp_type);
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun dev->features |= NETIF_F_LLTX;
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun dev->hard_header_len = PPP_HDRLEN;
1489*4882a593Smuzhiyun dev->mtu = PPP_MRU;
1490*4882a593Smuzhiyun dev->addr_len = 0;
1491*4882a593Smuzhiyun dev->tx_queue_len = 3;
1492*4882a593Smuzhiyun dev->type = ARPHRD_PPP;
1493*4882a593Smuzhiyun dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1494*4882a593Smuzhiyun dev->priv_destructor = ppp_dev_priv_destructor;
1495*4882a593Smuzhiyun netif_keep_dst(dev);
1496*4882a593Smuzhiyun }
1497*4882a593Smuzhiyun
1498*4882a593Smuzhiyun /*
1499*4882a593Smuzhiyun * Transmit-side routines.
1500*4882a593Smuzhiyun */
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun /* Called to do any work queued up on the transmit side that can now be done */
__ppp_xmit_process(struct ppp * ppp,struct sk_buff * skb)1503*4882a593Smuzhiyun static void __ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
1504*4882a593Smuzhiyun {
1505*4882a593Smuzhiyun ppp_xmit_lock(ppp);
1506*4882a593Smuzhiyun if (!ppp->closing) {
1507*4882a593Smuzhiyun ppp_push(ppp);
1508*4882a593Smuzhiyun
1509*4882a593Smuzhiyun if (skb)
1510*4882a593Smuzhiyun skb_queue_tail(&ppp->file.xq, skb);
1511*4882a593Smuzhiyun while (!ppp->xmit_pending &&
1512*4882a593Smuzhiyun (skb = skb_dequeue(&ppp->file.xq)))
1513*4882a593Smuzhiyun ppp_send_frame(ppp, skb);
1514*4882a593Smuzhiyun /* If there's no work left to do, tell the core net
1515*4882a593Smuzhiyun code that we can accept some more. */
1516*4882a593Smuzhiyun if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
1517*4882a593Smuzhiyun netif_wake_queue(ppp->dev);
1518*4882a593Smuzhiyun else
1519*4882a593Smuzhiyun netif_stop_queue(ppp->dev);
1520*4882a593Smuzhiyun } else {
1521*4882a593Smuzhiyun kfree_skb(skb);
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun ppp_xmit_unlock(ppp);
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun
ppp_xmit_process(struct ppp * ppp,struct sk_buff * skb)1526*4882a593Smuzhiyun static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun local_bh_disable();
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
1531*4882a593Smuzhiyun goto err;
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun (*this_cpu_ptr(ppp->xmit_recursion))++;
1534*4882a593Smuzhiyun __ppp_xmit_process(ppp, skb);
1535*4882a593Smuzhiyun (*this_cpu_ptr(ppp->xmit_recursion))--;
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun local_bh_enable();
1538*4882a593Smuzhiyun
1539*4882a593Smuzhiyun return;
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun err:
1542*4882a593Smuzhiyun local_bh_enable();
1543*4882a593Smuzhiyun
1544*4882a593Smuzhiyun kfree_skb(skb);
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun if (net_ratelimit())
1547*4882a593Smuzhiyun netdev_err(ppp->dev, "recursion detected\n");
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun
1550*4882a593Smuzhiyun static inline struct sk_buff *
pad_compress_skb(struct ppp * ppp,struct sk_buff * skb)1551*4882a593Smuzhiyun pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1552*4882a593Smuzhiyun {
1553*4882a593Smuzhiyun struct sk_buff *new_skb;
1554*4882a593Smuzhiyun int len;
1555*4882a593Smuzhiyun int new_skb_size = ppp->dev->mtu +
1556*4882a593Smuzhiyun ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1557*4882a593Smuzhiyun int compressor_skb_size = ppp->dev->mtu +
1558*4882a593Smuzhiyun ppp->xcomp->comp_extra + PPP_HDRLEN;
1559*4882a593Smuzhiyun new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1560*4882a593Smuzhiyun if (!new_skb) {
1561*4882a593Smuzhiyun if (net_ratelimit())
1562*4882a593Smuzhiyun netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
1563*4882a593Smuzhiyun return NULL;
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun if (ppp->dev->hard_header_len > PPP_HDRLEN)
1566*4882a593Smuzhiyun skb_reserve(new_skb,
1567*4882a593Smuzhiyun ppp->dev->hard_header_len - PPP_HDRLEN);
1568*4882a593Smuzhiyun
1569*4882a593Smuzhiyun /* compressor still expects A/C bytes in hdr */
1570*4882a593Smuzhiyun len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1571*4882a593Smuzhiyun new_skb->data, skb->len + 2,
1572*4882a593Smuzhiyun compressor_skb_size);
1573*4882a593Smuzhiyun if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1574*4882a593Smuzhiyun consume_skb(skb);
1575*4882a593Smuzhiyun skb = new_skb;
1576*4882a593Smuzhiyun skb_put(skb, len);
1577*4882a593Smuzhiyun skb_pull(skb, 2); /* pull off A/C bytes */
1578*4882a593Smuzhiyun } else if (len == 0) {
1579*4882a593Smuzhiyun /* didn't compress, or CCP not up yet */
1580*4882a593Smuzhiyun consume_skb(new_skb);
1581*4882a593Smuzhiyun new_skb = skb;
1582*4882a593Smuzhiyun } else {
1583*4882a593Smuzhiyun /*
1584*4882a593Smuzhiyun * (len < 0)
1585*4882a593Smuzhiyun * MPPE requires that we do not send unencrypted
1586*4882a593Smuzhiyun * frames. The compressor will return -1 if we
1587*4882a593Smuzhiyun * should drop the frame. We cannot simply test
1588*4882a593Smuzhiyun * the compress_proto because MPPE and MPPC share
1589*4882a593Smuzhiyun * the same number.
1590*4882a593Smuzhiyun */
1591*4882a593Smuzhiyun if (net_ratelimit())
1592*4882a593Smuzhiyun netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
1593*4882a593Smuzhiyun kfree_skb(skb);
1594*4882a593Smuzhiyun consume_skb(new_skb);
1595*4882a593Smuzhiyun new_skb = NULL;
1596*4882a593Smuzhiyun }
1597*4882a593Smuzhiyun return new_skb;
1598*4882a593Smuzhiyun }
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun /*
1601*4882a593Smuzhiyun * Compress and send a frame.
1602*4882a593Smuzhiyun * The caller should have locked the xmit path,
1603*4882a593Smuzhiyun * and xmit_pending should be 0.
1604*4882a593Smuzhiyun */
1605*4882a593Smuzhiyun static void
ppp_send_frame(struct ppp * ppp,struct sk_buff * skb)1606*4882a593Smuzhiyun ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1607*4882a593Smuzhiyun {
1608*4882a593Smuzhiyun int proto = PPP_PROTO(skb);
1609*4882a593Smuzhiyun struct sk_buff *new_skb;
1610*4882a593Smuzhiyun int len;
1611*4882a593Smuzhiyun unsigned char *cp;
1612*4882a593Smuzhiyun
1613*4882a593Smuzhiyun if (proto < 0x8000) {
1614*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
1615*4882a593Smuzhiyun /* check if we should pass this packet */
1616*4882a593Smuzhiyun /* the filter instructions are constructed assuming
1617*4882a593Smuzhiyun a four-byte PPP header on each packet */
1618*4882a593Smuzhiyun *(u8 *)skb_push(skb, 2) = 1;
1619*4882a593Smuzhiyun if (ppp->pass_filter &&
1620*4882a593Smuzhiyun BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
1621*4882a593Smuzhiyun if (ppp->debug & 1)
1622*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
1623*4882a593Smuzhiyun "PPP: outbound frame "
1624*4882a593Smuzhiyun "not passed\n");
1625*4882a593Smuzhiyun kfree_skb(skb);
1626*4882a593Smuzhiyun return;
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun /* if this packet passes the active filter, record the time */
1629*4882a593Smuzhiyun if (!(ppp->active_filter &&
1630*4882a593Smuzhiyun BPF_PROG_RUN(ppp->active_filter, skb) == 0))
1631*4882a593Smuzhiyun ppp->last_xmit = jiffies;
1632*4882a593Smuzhiyun skb_pull(skb, 2);
1633*4882a593Smuzhiyun #else
1634*4882a593Smuzhiyun /* for data packets, record the time */
1635*4882a593Smuzhiyun ppp->last_xmit = jiffies;
1636*4882a593Smuzhiyun #endif /* CONFIG_PPP_FILTER */
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun ++ppp->stats64.tx_packets;
1640*4882a593Smuzhiyun ppp->stats64.tx_bytes += skb->len - PPP_PROTO_LEN;
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun switch (proto) {
1643*4882a593Smuzhiyun case PPP_IP:
1644*4882a593Smuzhiyun if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
1645*4882a593Smuzhiyun break;
1646*4882a593Smuzhiyun /* try to do VJ TCP header compression */
1647*4882a593Smuzhiyun new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1648*4882a593Smuzhiyun GFP_ATOMIC);
1649*4882a593Smuzhiyun if (!new_skb) {
1650*4882a593Smuzhiyun netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
1651*4882a593Smuzhiyun goto drop;
1652*4882a593Smuzhiyun }
1653*4882a593Smuzhiyun skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1654*4882a593Smuzhiyun cp = skb->data + 2;
1655*4882a593Smuzhiyun len = slhc_compress(ppp->vj, cp, skb->len - 2,
1656*4882a593Smuzhiyun new_skb->data + 2, &cp,
1657*4882a593Smuzhiyun !(ppp->flags & SC_NO_TCP_CCID));
1658*4882a593Smuzhiyun if (cp == skb->data + 2) {
1659*4882a593Smuzhiyun /* didn't compress */
1660*4882a593Smuzhiyun consume_skb(new_skb);
1661*4882a593Smuzhiyun } else {
1662*4882a593Smuzhiyun if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1663*4882a593Smuzhiyun proto = PPP_VJC_COMP;
1664*4882a593Smuzhiyun cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1665*4882a593Smuzhiyun } else {
1666*4882a593Smuzhiyun proto = PPP_VJC_UNCOMP;
1667*4882a593Smuzhiyun cp[0] = skb->data[2];
1668*4882a593Smuzhiyun }
1669*4882a593Smuzhiyun consume_skb(skb);
1670*4882a593Smuzhiyun skb = new_skb;
1671*4882a593Smuzhiyun cp = skb_put(skb, len + 2);
1672*4882a593Smuzhiyun cp[0] = 0;
1673*4882a593Smuzhiyun cp[1] = proto;
1674*4882a593Smuzhiyun }
1675*4882a593Smuzhiyun break;
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun case PPP_CCP:
1678*4882a593Smuzhiyun /* peek at outbound CCP frames */
1679*4882a593Smuzhiyun ppp_ccp_peek(ppp, skb, 0);
1680*4882a593Smuzhiyun break;
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun
1683*4882a593Smuzhiyun /* try to do packet compression */
1684*4882a593Smuzhiyun if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1685*4882a593Smuzhiyun proto != PPP_LCP && proto != PPP_CCP) {
1686*4882a593Smuzhiyun if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1687*4882a593Smuzhiyun if (net_ratelimit())
1688*4882a593Smuzhiyun netdev_err(ppp->dev,
1689*4882a593Smuzhiyun "ppp: compression required but "
1690*4882a593Smuzhiyun "down - pkt dropped.\n");
1691*4882a593Smuzhiyun goto drop;
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun skb = pad_compress_skb(ppp, skb);
1694*4882a593Smuzhiyun if (!skb)
1695*4882a593Smuzhiyun goto drop;
1696*4882a593Smuzhiyun }
1697*4882a593Smuzhiyun
1698*4882a593Smuzhiyun /*
1699*4882a593Smuzhiyun * If we are waiting for traffic (demand dialling),
1700*4882a593Smuzhiyun * queue it up for pppd to receive.
1701*4882a593Smuzhiyun */
1702*4882a593Smuzhiyun if (ppp->flags & SC_LOOP_TRAFFIC) {
1703*4882a593Smuzhiyun if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1704*4882a593Smuzhiyun goto drop;
1705*4882a593Smuzhiyun skb_queue_tail(&ppp->file.rq, skb);
1706*4882a593Smuzhiyun wake_up_interruptible(&ppp->file.rwait);
1707*4882a593Smuzhiyun return;
1708*4882a593Smuzhiyun }
1709*4882a593Smuzhiyun
1710*4882a593Smuzhiyun ppp->xmit_pending = skb;
1711*4882a593Smuzhiyun ppp_push(ppp);
1712*4882a593Smuzhiyun return;
1713*4882a593Smuzhiyun
1714*4882a593Smuzhiyun drop:
1715*4882a593Smuzhiyun kfree_skb(skb);
1716*4882a593Smuzhiyun ++ppp->dev->stats.tx_errors;
1717*4882a593Smuzhiyun }
1718*4882a593Smuzhiyun
1719*4882a593Smuzhiyun /*
1720*4882a593Smuzhiyun * Try to send the frame in xmit_pending.
1721*4882a593Smuzhiyun * The caller should have the xmit path locked.
1722*4882a593Smuzhiyun */
1723*4882a593Smuzhiyun static void
ppp_push(struct ppp * ppp)1724*4882a593Smuzhiyun ppp_push(struct ppp *ppp)
1725*4882a593Smuzhiyun {
1726*4882a593Smuzhiyun struct list_head *list;
1727*4882a593Smuzhiyun struct channel *pch;
1728*4882a593Smuzhiyun struct sk_buff *skb = ppp->xmit_pending;
1729*4882a593Smuzhiyun
1730*4882a593Smuzhiyun if (!skb)
1731*4882a593Smuzhiyun return;
1732*4882a593Smuzhiyun
1733*4882a593Smuzhiyun list = &ppp->channels;
1734*4882a593Smuzhiyun if (list_empty(list)) {
1735*4882a593Smuzhiyun /* nowhere to send the packet, just drop it */
1736*4882a593Smuzhiyun ppp->xmit_pending = NULL;
1737*4882a593Smuzhiyun kfree_skb(skb);
1738*4882a593Smuzhiyun return;
1739*4882a593Smuzhiyun }
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun if ((ppp->flags & SC_MULTILINK) == 0) {
1742*4882a593Smuzhiyun /* not doing multilink: send it down the first channel */
1743*4882a593Smuzhiyun list = list->next;
1744*4882a593Smuzhiyun pch = list_entry(list, struct channel, clist);
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun spin_lock(&pch->downl);
1747*4882a593Smuzhiyun if (pch->chan) {
1748*4882a593Smuzhiyun if (pch->chan->ops->start_xmit(pch->chan, skb))
1749*4882a593Smuzhiyun ppp->xmit_pending = NULL;
1750*4882a593Smuzhiyun } else {
1751*4882a593Smuzhiyun /* channel got unregistered */
1752*4882a593Smuzhiyun kfree_skb(skb);
1753*4882a593Smuzhiyun ppp->xmit_pending = NULL;
1754*4882a593Smuzhiyun }
1755*4882a593Smuzhiyun spin_unlock(&pch->downl);
1756*4882a593Smuzhiyun return;
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun
1759*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
1760*4882a593Smuzhiyun /* Multilink: fragment the packet over as many links
1761*4882a593Smuzhiyun as can take the packet at the moment. */
1762*4882a593Smuzhiyun if (!ppp_mp_explode(ppp, skb))
1763*4882a593Smuzhiyun return;
1764*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun ppp->xmit_pending = NULL;
1767*4882a593Smuzhiyun kfree_skb(skb);
1768*4882a593Smuzhiyun }
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
1771*4882a593Smuzhiyun static bool mp_protocol_compress __read_mostly = true;
1772*4882a593Smuzhiyun module_param(mp_protocol_compress, bool, 0644);
1773*4882a593Smuzhiyun MODULE_PARM_DESC(mp_protocol_compress,
1774*4882a593Smuzhiyun "compress protocol id in multilink fragments");
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun /*
1777*4882a593Smuzhiyun * Divide a packet to be transmitted into fragments and
1778*4882a593Smuzhiyun * send them out the individual links.
1779*4882a593Smuzhiyun */
ppp_mp_explode(struct ppp * ppp,struct sk_buff * skb)1780*4882a593Smuzhiyun static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1781*4882a593Smuzhiyun {
1782*4882a593Smuzhiyun int len, totlen;
1783*4882a593Smuzhiyun int i, bits, hdrlen, mtu;
1784*4882a593Smuzhiyun int flen;
1785*4882a593Smuzhiyun int navail, nfree, nzero;
1786*4882a593Smuzhiyun int nbigger;
1787*4882a593Smuzhiyun int totspeed;
1788*4882a593Smuzhiyun int totfree;
1789*4882a593Smuzhiyun unsigned char *p, *q;
1790*4882a593Smuzhiyun struct list_head *list;
1791*4882a593Smuzhiyun struct channel *pch;
1792*4882a593Smuzhiyun struct sk_buff *frag;
1793*4882a593Smuzhiyun struct ppp_channel *chan;
1794*4882a593Smuzhiyun
1795*4882a593Smuzhiyun totspeed = 0; /*total bitrate of the bundle*/
1796*4882a593Smuzhiyun nfree = 0; /* # channels which have no packet already queued */
1797*4882a593Smuzhiyun navail = 0; /* total # of usable channels (not deregistered) */
1798*4882a593Smuzhiyun nzero = 0; /* number of channels with zero speed associated*/
1799*4882a593Smuzhiyun totfree = 0; /*total # of channels available and
1800*4882a593Smuzhiyun *having no queued packets before
1801*4882a593Smuzhiyun *starting the fragmentation*/
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1804*4882a593Smuzhiyun i = 0;
1805*4882a593Smuzhiyun list_for_each_entry(pch, &ppp->channels, clist) {
1806*4882a593Smuzhiyun if (pch->chan) {
1807*4882a593Smuzhiyun pch->avail = 1;
1808*4882a593Smuzhiyun navail++;
1809*4882a593Smuzhiyun pch->speed = pch->chan->speed;
1810*4882a593Smuzhiyun } else {
1811*4882a593Smuzhiyun pch->avail = 0;
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun if (pch->avail) {
1814*4882a593Smuzhiyun if (skb_queue_empty(&pch->file.xq) ||
1815*4882a593Smuzhiyun !pch->had_frag) {
1816*4882a593Smuzhiyun if (pch->speed == 0)
1817*4882a593Smuzhiyun nzero++;
1818*4882a593Smuzhiyun else
1819*4882a593Smuzhiyun totspeed += pch->speed;
1820*4882a593Smuzhiyun
1821*4882a593Smuzhiyun pch->avail = 2;
1822*4882a593Smuzhiyun ++nfree;
1823*4882a593Smuzhiyun ++totfree;
1824*4882a593Smuzhiyun }
1825*4882a593Smuzhiyun if (!pch->had_frag && i < ppp->nxchan)
1826*4882a593Smuzhiyun ppp->nxchan = i;
1827*4882a593Smuzhiyun }
1828*4882a593Smuzhiyun ++i;
1829*4882a593Smuzhiyun }
1830*4882a593Smuzhiyun /*
1831*4882a593Smuzhiyun * Don't start sending this packet unless at least half of
1832*4882a593Smuzhiyun * the channels are free. This gives much better TCP
1833*4882a593Smuzhiyun * performance if we have a lot of channels.
1834*4882a593Smuzhiyun */
1835*4882a593Smuzhiyun if (nfree == 0 || nfree < navail / 2)
1836*4882a593Smuzhiyun return 0; /* can't take now, leave it in xmit_pending */
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun /* Do protocol field compression */
1839*4882a593Smuzhiyun p = skb->data;
1840*4882a593Smuzhiyun len = skb->len;
1841*4882a593Smuzhiyun if (*p == 0 && mp_protocol_compress) {
1842*4882a593Smuzhiyun ++p;
1843*4882a593Smuzhiyun --len;
1844*4882a593Smuzhiyun }
1845*4882a593Smuzhiyun
1846*4882a593Smuzhiyun totlen = len;
1847*4882a593Smuzhiyun nbigger = len % nfree;
1848*4882a593Smuzhiyun
1849*4882a593Smuzhiyun /* skip to the channel after the one we last used
1850*4882a593Smuzhiyun and start at that one */
1851*4882a593Smuzhiyun list = &ppp->channels;
1852*4882a593Smuzhiyun for (i = 0; i < ppp->nxchan; ++i) {
1853*4882a593Smuzhiyun list = list->next;
1854*4882a593Smuzhiyun if (list == &ppp->channels) {
1855*4882a593Smuzhiyun i = 0;
1856*4882a593Smuzhiyun break;
1857*4882a593Smuzhiyun }
1858*4882a593Smuzhiyun }
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun /* create a fragment for each channel */
1861*4882a593Smuzhiyun bits = B;
1862*4882a593Smuzhiyun while (len > 0) {
1863*4882a593Smuzhiyun list = list->next;
1864*4882a593Smuzhiyun if (list == &ppp->channels) {
1865*4882a593Smuzhiyun i = 0;
1866*4882a593Smuzhiyun continue;
1867*4882a593Smuzhiyun }
1868*4882a593Smuzhiyun pch = list_entry(list, struct channel, clist);
1869*4882a593Smuzhiyun ++i;
1870*4882a593Smuzhiyun if (!pch->avail)
1871*4882a593Smuzhiyun continue;
1872*4882a593Smuzhiyun
1873*4882a593Smuzhiyun /*
1874*4882a593Smuzhiyun * Skip this channel if it has a fragment pending already and
1875*4882a593Smuzhiyun * we haven't given a fragment to all of the free channels.
1876*4882a593Smuzhiyun */
1877*4882a593Smuzhiyun if (pch->avail == 1) {
1878*4882a593Smuzhiyun if (nfree > 0)
1879*4882a593Smuzhiyun continue;
1880*4882a593Smuzhiyun } else {
1881*4882a593Smuzhiyun pch->avail = 1;
1882*4882a593Smuzhiyun }
1883*4882a593Smuzhiyun
1884*4882a593Smuzhiyun /* check the channel's mtu and whether it is still attached. */
1885*4882a593Smuzhiyun spin_lock(&pch->downl);
1886*4882a593Smuzhiyun if (pch->chan == NULL) {
1887*4882a593Smuzhiyun /* can't use this channel, it's being deregistered */
1888*4882a593Smuzhiyun if (pch->speed == 0)
1889*4882a593Smuzhiyun nzero--;
1890*4882a593Smuzhiyun else
1891*4882a593Smuzhiyun totspeed -= pch->speed;
1892*4882a593Smuzhiyun
1893*4882a593Smuzhiyun spin_unlock(&pch->downl);
1894*4882a593Smuzhiyun pch->avail = 0;
1895*4882a593Smuzhiyun totlen = len;
1896*4882a593Smuzhiyun totfree--;
1897*4882a593Smuzhiyun nfree--;
1898*4882a593Smuzhiyun if (--navail == 0)
1899*4882a593Smuzhiyun break;
1900*4882a593Smuzhiyun continue;
1901*4882a593Smuzhiyun }
1902*4882a593Smuzhiyun
1903*4882a593Smuzhiyun /*
1904*4882a593Smuzhiyun *if the channel speed is not set divide
1905*4882a593Smuzhiyun *the packet evenly among the free channels;
1906*4882a593Smuzhiyun *otherwise divide it according to the speed
1907*4882a593Smuzhiyun *of the channel we are going to transmit on
1908*4882a593Smuzhiyun */
1909*4882a593Smuzhiyun flen = len;
1910*4882a593Smuzhiyun if (nfree > 0) {
1911*4882a593Smuzhiyun if (pch->speed == 0) {
1912*4882a593Smuzhiyun flen = len/nfree;
1913*4882a593Smuzhiyun if (nbigger > 0) {
1914*4882a593Smuzhiyun flen++;
1915*4882a593Smuzhiyun nbigger--;
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun } else {
1918*4882a593Smuzhiyun flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1919*4882a593Smuzhiyun ((totspeed*totfree)/pch->speed)) - hdrlen;
1920*4882a593Smuzhiyun if (nbigger > 0) {
1921*4882a593Smuzhiyun flen += ((totfree - nzero)*pch->speed)/totspeed;
1922*4882a593Smuzhiyun nbigger -= ((totfree - nzero)*pch->speed)/
1923*4882a593Smuzhiyun totspeed;
1924*4882a593Smuzhiyun }
1925*4882a593Smuzhiyun }
1926*4882a593Smuzhiyun nfree--;
1927*4882a593Smuzhiyun }
1928*4882a593Smuzhiyun
1929*4882a593Smuzhiyun /*
1930*4882a593Smuzhiyun *check if we are on the last channel or
1931*4882a593Smuzhiyun *we exceded the length of the data to
1932*4882a593Smuzhiyun *fragment
1933*4882a593Smuzhiyun */
1934*4882a593Smuzhiyun if ((nfree <= 0) || (flen > len))
1935*4882a593Smuzhiyun flen = len;
1936*4882a593Smuzhiyun /*
1937*4882a593Smuzhiyun *it is not worth to tx on slow channels:
1938*4882a593Smuzhiyun *in that case from the resulting flen according to the
1939*4882a593Smuzhiyun *above formula will be equal or less than zero.
1940*4882a593Smuzhiyun *Skip the channel in this case
1941*4882a593Smuzhiyun */
1942*4882a593Smuzhiyun if (flen <= 0) {
1943*4882a593Smuzhiyun pch->avail = 2;
1944*4882a593Smuzhiyun spin_unlock(&pch->downl);
1945*4882a593Smuzhiyun continue;
1946*4882a593Smuzhiyun }
1947*4882a593Smuzhiyun
1948*4882a593Smuzhiyun /*
1949*4882a593Smuzhiyun * hdrlen includes the 2-byte PPP protocol field, but the
1950*4882a593Smuzhiyun * MTU counts only the payload excluding the protocol field.
1951*4882a593Smuzhiyun * (RFC1661 Section 2)
1952*4882a593Smuzhiyun */
1953*4882a593Smuzhiyun mtu = pch->chan->mtu - (hdrlen - 2);
1954*4882a593Smuzhiyun if (mtu < 4)
1955*4882a593Smuzhiyun mtu = 4;
1956*4882a593Smuzhiyun if (flen > mtu)
1957*4882a593Smuzhiyun flen = mtu;
1958*4882a593Smuzhiyun if (flen == len)
1959*4882a593Smuzhiyun bits |= E;
1960*4882a593Smuzhiyun frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
1961*4882a593Smuzhiyun if (!frag)
1962*4882a593Smuzhiyun goto noskb;
1963*4882a593Smuzhiyun q = skb_put(frag, flen + hdrlen);
1964*4882a593Smuzhiyun
1965*4882a593Smuzhiyun /* make the MP header */
1966*4882a593Smuzhiyun put_unaligned_be16(PPP_MP, q);
1967*4882a593Smuzhiyun if (ppp->flags & SC_MP_XSHORTSEQ) {
1968*4882a593Smuzhiyun q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1969*4882a593Smuzhiyun q[3] = ppp->nxseq;
1970*4882a593Smuzhiyun } else {
1971*4882a593Smuzhiyun q[2] = bits;
1972*4882a593Smuzhiyun q[3] = ppp->nxseq >> 16;
1973*4882a593Smuzhiyun q[4] = ppp->nxseq >> 8;
1974*4882a593Smuzhiyun q[5] = ppp->nxseq;
1975*4882a593Smuzhiyun }
1976*4882a593Smuzhiyun
1977*4882a593Smuzhiyun memcpy(q + hdrlen, p, flen);
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun /* try to send it down the channel */
1980*4882a593Smuzhiyun chan = pch->chan;
1981*4882a593Smuzhiyun if (!skb_queue_empty(&pch->file.xq) ||
1982*4882a593Smuzhiyun !chan->ops->start_xmit(chan, frag))
1983*4882a593Smuzhiyun skb_queue_tail(&pch->file.xq, frag);
1984*4882a593Smuzhiyun pch->had_frag = 1;
1985*4882a593Smuzhiyun p += flen;
1986*4882a593Smuzhiyun len -= flen;
1987*4882a593Smuzhiyun ++ppp->nxseq;
1988*4882a593Smuzhiyun bits = 0;
1989*4882a593Smuzhiyun spin_unlock(&pch->downl);
1990*4882a593Smuzhiyun }
1991*4882a593Smuzhiyun ppp->nxchan = i;
1992*4882a593Smuzhiyun
1993*4882a593Smuzhiyun return 1;
1994*4882a593Smuzhiyun
1995*4882a593Smuzhiyun noskb:
1996*4882a593Smuzhiyun spin_unlock(&pch->downl);
1997*4882a593Smuzhiyun if (ppp->debug & 1)
1998*4882a593Smuzhiyun netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
1999*4882a593Smuzhiyun ++ppp->dev->stats.tx_errors;
2000*4882a593Smuzhiyun ++ppp->nxseq;
2001*4882a593Smuzhiyun return 1; /* abandon the frame */
2002*4882a593Smuzhiyun }
2003*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
2004*4882a593Smuzhiyun
2005*4882a593Smuzhiyun /* Try to send data out on a channel */
__ppp_channel_push(struct channel * pch)2006*4882a593Smuzhiyun static void __ppp_channel_push(struct channel *pch)
2007*4882a593Smuzhiyun {
2008*4882a593Smuzhiyun struct sk_buff *skb;
2009*4882a593Smuzhiyun struct ppp *ppp;
2010*4882a593Smuzhiyun
2011*4882a593Smuzhiyun spin_lock(&pch->downl);
2012*4882a593Smuzhiyun if (pch->chan) {
2013*4882a593Smuzhiyun while (!skb_queue_empty(&pch->file.xq)) {
2014*4882a593Smuzhiyun skb = skb_dequeue(&pch->file.xq);
2015*4882a593Smuzhiyun if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
2016*4882a593Smuzhiyun /* put the packet back and try again later */
2017*4882a593Smuzhiyun skb_queue_head(&pch->file.xq, skb);
2018*4882a593Smuzhiyun break;
2019*4882a593Smuzhiyun }
2020*4882a593Smuzhiyun }
2021*4882a593Smuzhiyun } else {
2022*4882a593Smuzhiyun /* channel got deregistered */
2023*4882a593Smuzhiyun skb_queue_purge(&pch->file.xq);
2024*4882a593Smuzhiyun }
2025*4882a593Smuzhiyun spin_unlock(&pch->downl);
2026*4882a593Smuzhiyun /* see if there is anything from the attached unit to be sent */
2027*4882a593Smuzhiyun if (skb_queue_empty(&pch->file.xq)) {
2028*4882a593Smuzhiyun ppp = pch->ppp;
2029*4882a593Smuzhiyun if (ppp)
2030*4882a593Smuzhiyun __ppp_xmit_process(ppp, NULL);
2031*4882a593Smuzhiyun }
2032*4882a593Smuzhiyun }
2033*4882a593Smuzhiyun
ppp_channel_push(struct channel * pch)2034*4882a593Smuzhiyun static void ppp_channel_push(struct channel *pch)
2035*4882a593Smuzhiyun {
2036*4882a593Smuzhiyun read_lock_bh(&pch->upl);
2037*4882a593Smuzhiyun if (pch->ppp) {
2038*4882a593Smuzhiyun (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
2039*4882a593Smuzhiyun __ppp_channel_push(pch);
2040*4882a593Smuzhiyun (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
2041*4882a593Smuzhiyun } else {
2042*4882a593Smuzhiyun __ppp_channel_push(pch);
2043*4882a593Smuzhiyun }
2044*4882a593Smuzhiyun read_unlock_bh(&pch->upl);
2045*4882a593Smuzhiyun }
2046*4882a593Smuzhiyun
2047*4882a593Smuzhiyun /*
2048*4882a593Smuzhiyun * Receive-side routines.
2049*4882a593Smuzhiyun */
2050*4882a593Smuzhiyun
2051*4882a593Smuzhiyun struct ppp_mp_skb_parm {
2052*4882a593Smuzhiyun u32 sequence;
2053*4882a593Smuzhiyun u8 BEbits;
2054*4882a593Smuzhiyun };
2055*4882a593Smuzhiyun #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun static inline void
ppp_do_recv(struct ppp * ppp,struct sk_buff * skb,struct channel * pch)2058*4882a593Smuzhiyun ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2059*4882a593Smuzhiyun {
2060*4882a593Smuzhiyun ppp_recv_lock(ppp);
2061*4882a593Smuzhiyun if (!ppp->closing)
2062*4882a593Smuzhiyun ppp_receive_frame(ppp, skb, pch);
2063*4882a593Smuzhiyun else
2064*4882a593Smuzhiyun kfree_skb(skb);
2065*4882a593Smuzhiyun ppp_recv_unlock(ppp);
2066*4882a593Smuzhiyun }
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun /**
2069*4882a593Smuzhiyun * __ppp_decompress_proto - Decompress protocol field, slim version.
2070*4882a593Smuzhiyun * @skb: Socket buffer where protocol field should be decompressed. It must have
2071*4882a593Smuzhiyun * at least 1 byte of head room and 1 byte of linear data. First byte of
2072*4882a593Smuzhiyun * data must be a protocol field byte.
2073*4882a593Smuzhiyun *
2074*4882a593Smuzhiyun * Decompress protocol field in PPP header if it's compressed, e.g. when
2075*4882a593Smuzhiyun * Protocol-Field-Compression (PFC) was negotiated. No checks w.r.t. skb data
2076*4882a593Smuzhiyun * length are done in this function.
2077*4882a593Smuzhiyun */
__ppp_decompress_proto(struct sk_buff * skb)2078*4882a593Smuzhiyun static void __ppp_decompress_proto(struct sk_buff *skb)
2079*4882a593Smuzhiyun {
2080*4882a593Smuzhiyun if (skb->data[0] & 0x01)
2081*4882a593Smuzhiyun *(u8 *)skb_push(skb, 1) = 0x00;
2082*4882a593Smuzhiyun }
2083*4882a593Smuzhiyun
2084*4882a593Smuzhiyun /**
2085*4882a593Smuzhiyun * ppp_decompress_proto - Check skb data room and decompress protocol field.
2086*4882a593Smuzhiyun * @skb: Socket buffer where protocol field should be decompressed. First byte
2087*4882a593Smuzhiyun * of data must be a protocol field byte.
2088*4882a593Smuzhiyun *
2089*4882a593Smuzhiyun * Decompress protocol field in PPP header if it's compressed, e.g. when
2090*4882a593Smuzhiyun * Protocol-Field-Compression (PFC) was negotiated. This function also makes
2091*4882a593Smuzhiyun * sure that skb data room is sufficient for Protocol field, before and after
2092*4882a593Smuzhiyun * decompression.
2093*4882a593Smuzhiyun *
2094*4882a593Smuzhiyun * Return: true - decompressed successfully, false - not enough room in skb.
2095*4882a593Smuzhiyun */
ppp_decompress_proto(struct sk_buff * skb)2096*4882a593Smuzhiyun static bool ppp_decompress_proto(struct sk_buff *skb)
2097*4882a593Smuzhiyun {
2098*4882a593Smuzhiyun /* At least one byte should be present (if protocol is compressed) */
2099*4882a593Smuzhiyun if (!pskb_may_pull(skb, 1))
2100*4882a593Smuzhiyun return false;
2101*4882a593Smuzhiyun
2102*4882a593Smuzhiyun __ppp_decompress_proto(skb);
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun /* Protocol field should occupy 2 bytes when not compressed */
2105*4882a593Smuzhiyun return pskb_may_pull(skb, 2);
2106*4882a593Smuzhiyun }
2107*4882a593Smuzhiyun
2108*4882a593Smuzhiyun void
ppp_input(struct ppp_channel * chan,struct sk_buff * skb)2109*4882a593Smuzhiyun ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
2110*4882a593Smuzhiyun {
2111*4882a593Smuzhiyun struct channel *pch = chan->ppp;
2112*4882a593Smuzhiyun int proto;
2113*4882a593Smuzhiyun
2114*4882a593Smuzhiyun if (!pch) {
2115*4882a593Smuzhiyun kfree_skb(skb);
2116*4882a593Smuzhiyun return;
2117*4882a593Smuzhiyun }
2118*4882a593Smuzhiyun
2119*4882a593Smuzhiyun read_lock_bh(&pch->upl);
2120*4882a593Smuzhiyun if (!ppp_decompress_proto(skb)) {
2121*4882a593Smuzhiyun kfree_skb(skb);
2122*4882a593Smuzhiyun if (pch->ppp) {
2123*4882a593Smuzhiyun ++pch->ppp->dev->stats.rx_length_errors;
2124*4882a593Smuzhiyun ppp_receive_error(pch->ppp);
2125*4882a593Smuzhiyun }
2126*4882a593Smuzhiyun goto done;
2127*4882a593Smuzhiyun }
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun proto = PPP_PROTO(skb);
2130*4882a593Smuzhiyun if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
2131*4882a593Smuzhiyun /* put it on the channel queue */
2132*4882a593Smuzhiyun skb_queue_tail(&pch->file.rq, skb);
2133*4882a593Smuzhiyun /* drop old frames if queue too long */
2134*4882a593Smuzhiyun while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
2135*4882a593Smuzhiyun (skb = skb_dequeue(&pch->file.rq)))
2136*4882a593Smuzhiyun kfree_skb(skb);
2137*4882a593Smuzhiyun wake_up_interruptible(&pch->file.rwait);
2138*4882a593Smuzhiyun } else {
2139*4882a593Smuzhiyun ppp_do_recv(pch->ppp, skb, pch);
2140*4882a593Smuzhiyun }
2141*4882a593Smuzhiyun
2142*4882a593Smuzhiyun done:
2143*4882a593Smuzhiyun read_unlock_bh(&pch->upl);
2144*4882a593Smuzhiyun }
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun /* Put a 0-length skb in the receive queue as an error indication */
2147*4882a593Smuzhiyun void
ppp_input_error(struct ppp_channel * chan,int code)2148*4882a593Smuzhiyun ppp_input_error(struct ppp_channel *chan, int code)
2149*4882a593Smuzhiyun {
2150*4882a593Smuzhiyun struct channel *pch = chan->ppp;
2151*4882a593Smuzhiyun struct sk_buff *skb;
2152*4882a593Smuzhiyun
2153*4882a593Smuzhiyun if (!pch)
2154*4882a593Smuzhiyun return;
2155*4882a593Smuzhiyun
2156*4882a593Smuzhiyun read_lock_bh(&pch->upl);
2157*4882a593Smuzhiyun if (pch->ppp) {
2158*4882a593Smuzhiyun skb = alloc_skb(0, GFP_ATOMIC);
2159*4882a593Smuzhiyun if (skb) {
2160*4882a593Smuzhiyun skb->len = 0; /* probably unnecessary */
2161*4882a593Smuzhiyun skb->cb[0] = code;
2162*4882a593Smuzhiyun ppp_do_recv(pch->ppp, skb, pch);
2163*4882a593Smuzhiyun }
2164*4882a593Smuzhiyun }
2165*4882a593Smuzhiyun read_unlock_bh(&pch->upl);
2166*4882a593Smuzhiyun }
2167*4882a593Smuzhiyun
2168*4882a593Smuzhiyun /*
2169*4882a593Smuzhiyun * We come in here to process a received frame.
2170*4882a593Smuzhiyun * The receive side of the ppp unit is locked.
2171*4882a593Smuzhiyun */
2172*4882a593Smuzhiyun static void
ppp_receive_frame(struct ppp * ppp,struct sk_buff * skb,struct channel * pch)2173*4882a593Smuzhiyun ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2174*4882a593Smuzhiyun {
2175*4882a593Smuzhiyun /* note: a 0-length skb is used as an error indication */
2176*4882a593Smuzhiyun if (skb->len > 0) {
2177*4882a593Smuzhiyun skb_checksum_complete_unset(skb);
2178*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
2179*4882a593Smuzhiyun /* XXX do channel-level decompression here */
2180*4882a593Smuzhiyun if (PPP_PROTO(skb) == PPP_MP)
2181*4882a593Smuzhiyun ppp_receive_mp_frame(ppp, skb, pch);
2182*4882a593Smuzhiyun else
2183*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
2184*4882a593Smuzhiyun ppp_receive_nonmp_frame(ppp, skb);
2185*4882a593Smuzhiyun } else {
2186*4882a593Smuzhiyun kfree_skb(skb);
2187*4882a593Smuzhiyun ppp_receive_error(ppp);
2188*4882a593Smuzhiyun }
2189*4882a593Smuzhiyun }
2190*4882a593Smuzhiyun
2191*4882a593Smuzhiyun static void
ppp_receive_error(struct ppp * ppp)2192*4882a593Smuzhiyun ppp_receive_error(struct ppp *ppp)
2193*4882a593Smuzhiyun {
2194*4882a593Smuzhiyun ++ppp->dev->stats.rx_errors;
2195*4882a593Smuzhiyun if (ppp->vj)
2196*4882a593Smuzhiyun slhc_toss(ppp->vj);
2197*4882a593Smuzhiyun }
2198*4882a593Smuzhiyun
2199*4882a593Smuzhiyun static void
ppp_receive_nonmp_frame(struct ppp * ppp,struct sk_buff * skb)2200*4882a593Smuzhiyun ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2201*4882a593Smuzhiyun {
2202*4882a593Smuzhiyun struct sk_buff *ns;
2203*4882a593Smuzhiyun int proto, len, npi;
2204*4882a593Smuzhiyun
2205*4882a593Smuzhiyun /*
2206*4882a593Smuzhiyun * Decompress the frame, if compressed.
2207*4882a593Smuzhiyun * Note that some decompressors need to see uncompressed frames
2208*4882a593Smuzhiyun * that come in as well as compressed frames.
2209*4882a593Smuzhiyun */
2210*4882a593Smuzhiyun if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2211*4882a593Smuzhiyun (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
2212*4882a593Smuzhiyun skb = ppp_decompress_frame(ppp, skb);
2213*4882a593Smuzhiyun
2214*4882a593Smuzhiyun if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2215*4882a593Smuzhiyun goto err;
2216*4882a593Smuzhiyun
2217*4882a593Smuzhiyun /* At this point the "Protocol" field MUST be decompressed, either in
2218*4882a593Smuzhiyun * ppp_input(), ppp_decompress_frame() or in ppp_receive_mp_frame().
2219*4882a593Smuzhiyun */
2220*4882a593Smuzhiyun proto = PPP_PROTO(skb);
2221*4882a593Smuzhiyun switch (proto) {
2222*4882a593Smuzhiyun case PPP_VJC_COMP:
2223*4882a593Smuzhiyun /* decompress VJ compressed packets */
2224*4882a593Smuzhiyun if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2225*4882a593Smuzhiyun goto err;
2226*4882a593Smuzhiyun
2227*4882a593Smuzhiyun if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
2228*4882a593Smuzhiyun /* copy to a new sk_buff with more tailroom */
2229*4882a593Smuzhiyun ns = dev_alloc_skb(skb->len + 128);
2230*4882a593Smuzhiyun if (!ns) {
2231*4882a593Smuzhiyun netdev_err(ppp->dev, "PPP: no memory "
2232*4882a593Smuzhiyun "(VJ decomp)\n");
2233*4882a593Smuzhiyun goto err;
2234*4882a593Smuzhiyun }
2235*4882a593Smuzhiyun skb_reserve(ns, 2);
2236*4882a593Smuzhiyun skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
2237*4882a593Smuzhiyun consume_skb(skb);
2238*4882a593Smuzhiyun skb = ns;
2239*4882a593Smuzhiyun }
2240*4882a593Smuzhiyun else
2241*4882a593Smuzhiyun skb->ip_summed = CHECKSUM_NONE;
2242*4882a593Smuzhiyun
2243*4882a593Smuzhiyun len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2244*4882a593Smuzhiyun if (len <= 0) {
2245*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
2246*4882a593Smuzhiyun "PPP: VJ decompression error\n");
2247*4882a593Smuzhiyun goto err;
2248*4882a593Smuzhiyun }
2249*4882a593Smuzhiyun len += 2;
2250*4882a593Smuzhiyun if (len > skb->len)
2251*4882a593Smuzhiyun skb_put(skb, len - skb->len);
2252*4882a593Smuzhiyun else if (len < skb->len)
2253*4882a593Smuzhiyun skb_trim(skb, len);
2254*4882a593Smuzhiyun proto = PPP_IP;
2255*4882a593Smuzhiyun break;
2256*4882a593Smuzhiyun
2257*4882a593Smuzhiyun case PPP_VJC_UNCOMP:
2258*4882a593Smuzhiyun if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2259*4882a593Smuzhiyun goto err;
2260*4882a593Smuzhiyun
2261*4882a593Smuzhiyun /* Until we fix the decompressor need to make sure
2262*4882a593Smuzhiyun * data portion is linear.
2263*4882a593Smuzhiyun */
2264*4882a593Smuzhiyun if (!pskb_may_pull(skb, skb->len))
2265*4882a593Smuzhiyun goto err;
2266*4882a593Smuzhiyun
2267*4882a593Smuzhiyun if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
2268*4882a593Smuzhiyun netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
2269*4882a593Smuzhiyun goto err;
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun proto = PPP_IP;
2272*4882a593Smuzhiyun break;
2273*4882a593Smuzhiyun
2274*4882a593Smuzhiyun case PPP_CCP:
2275*4882a593Smuzhiyun ppp_ccp_peek(ppp, skb, 1);
2276*4882a593Smuzhiyun break;
2277*4882a593Smuzhiyun }
2278*4882a593Smuzhiyun
2279*4882a593Smuzhiyun ++ppp->stats64.rx_packets;
2280*4882a593Smuzhiyun ppp->stats64.rx_bytes += skb->len - 2;
2281*4882a593Smuzhiyun
2282*4882a593Smuzhiyun npi = proto_to_npindex(proto);
2283*4882a593Smuzhiyun if (npi < 0) {
2284*4882a593Smuzhiyun /* control or unknown frame - pass it to pppd */
2285*4882a593Smuzhiyun skb_queue_tail(&ppp->file.rq, skb);
2286*4882a593Smuzhiyun /* limit queue length by dropping old frames */
2287*4882a593Smuzhiyun while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2288*4882a593Smuzhiyun (skb = skb_dequeue(&ppp->file.rq)))
2289*4882a593Smuzhiyun kfree_skb(skb);
2290*4882a593Smuzhiyun /* wake up any process polling or blocking on read */
2291*4882a593Smuzhiyun wake_up_interruptible(&ppp->file.rwait);
2292*4882a593Smuzhiyun
2293*4882a593Smuzhiyun } else {
2294*4882a593Smuzhiyun /* network protocol frame - give it to the kernel */
2295*4882a593Smuzhiyun
2296*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
2297*4882a593Smuzhiyun /* check if the packet passes the pass and active filters */
2298*4882a593Smuzhiyun /* the filter instructions are constructed assuming
2299*4882a593Smuzhiyun a four-byte PPP header on each packet */
2300*4882a593Smuzhiyun if (ppp->pass_filter || ppp->active_filter) {
2301*4882a593Smuzhiyun if (skb_unclone(skb, GFP_ATOMIC))
2302*4882a593Smuzhiyun goto err;
2303*4882a593Smuzhiyun
2304*4882a593Smuzhiyun *(u8 *)skb_push(skb, 2) = 0;
2305*4882a593Smuzhiyun if (ppp->pass_filter &&
2306*4882a593Smuzhiyun BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
2307*4882a593Smuzhiyun if (ppp->debug & 1)
2308*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
2309*4882a593Smuzhiyun "PPP: inbound frame "
2310*4882a593Smuzhiyun "not passed\n");
2311*4882a593Smuzhiyun kfree_skb(skb);
2312*4882a593Smuzhiyun return;
2313*4882a593Smuzhiyun }
2314*4882a593Smuzhiyun if (!(ppp->active_filter &&
2315*4882a593Smuzhiyun BPF_PROG_RUN(ppp->active_filter, skb) == 0))
2316*4882a593Smuzhiyun ppp->last_recv = jiffies;
2317*4882a593Smuzhiyun __skb_pull(skb, 2);
2318*4882a593Smuzhiyun } else
2319*4882a593Smuzhiyun #endif /* CONFIG_PPP_FILTER */
2320*4882a593Smuzhiyun ppp->last_recv = jiffies;
2321*4882a593Smuzhiyun
2322*4882a593Smuzhiyun if ((ppp->dev->flags & IFF_UP) == 0 ||
2323*4882a593Smuzhiyun ppp->npmode[npi] != NPMODE_PASS) {
2324*4882a593Smuzhiyun kfree_skb(skb);
2325*4882a593Smuzhiyun } else {
2326*4882a593Smuzhiyun /* chop off protocol */
2327*4882a593Smuzhiyun skb_pull_rcsum(skb, 2);
2328*4882a593Smuzhiyun skb->dev = ppp->dev;
2329*4882a593Smuzhiyun skb->protocol = htons(npindex_to_ethertype[npi]);
2330*4882a593Smuzhiyun skb_reset_mac_header(skb);
2331*4882a593Smuzhiyun skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2332*4882a593Smuzhiyun dev_net(ppp->dev)));
2333*4882a593Smuzhiyun netif_rx(skb);
2334*4882a593Smuzhiyun }
2335*4882a593Smuzhiyun }
2336*4882a593Smuzhiyun return;
2337*4882a593Smuzhiyun
2338*4882a593Smuzhiyun err:
2339*4882a593Smuzhiyun kfree_skb(skb);
2340*4882a593Smuzhiyun ppp_receive_error(ppp);
2341*4882a593Smuzhiyun }
2342*4882a593Smuzhiyun
2343*4882a593Smuzhiyun static struct sk_buff *
ppp_decompress_frame(struct ppp * ppp,struct sk_buff * skb)2344*4882a593Smuzhiyun ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2345*4882a593Smuzhiyun {
2346*4882a593Smuzhiyun int proto = PPP_PROTO(skb);
2347*4882a593Smuzhiyun struct sk_buff *ns;
2348*4882a593Smuzhiyun int len;
2349*4882a593Smuzhiyun
2350*4882a593Smuzhiyun /* Until we fix all the decompressor's need to make sure
2351*4882a593Smuzhiyun * data portion is linear.
2352*4882a593Smuzhiyun */
2353*4882a593Smuzhiyun if (!pskb_may_pull(skb, skb->len))
2354*4882a593Smuzhiyun goto err;
2355*4882a593Smuzhiyun
2356*4882a593Smuzhiyun if (proto == PPP_COMP) {
2357*4882a593Smuzhiyun int obuff_size;
2358*4882a593Smuzhiyun
2359*4882a593Smuzhiyun switch(ppp->rcomp->compress_proto) {
2360*4882a593Smuzhiyun case CI_MPPE:
2361*4882a593Smuzhiyun obuff_size = ppp->mru + PPP_HDRLEN + 1;
2362*4882a593Smuzhiyun break;
2363*4882a593Smuzhiyun default:
2364*4882a593Smuzhiyun obuff_size = ppp->mru + PPP_HDRLEN;
2365*4882a593Smuzhiyun break;
2366*4882a593Smuzhiyun }
2367*4882a593Smuzhiyun
2368*4882a593Smuzhiyun ns = dev_alloc_skb(obuff_size);
2369*4882a593Smuzhiyun if (!ns) {
2370*4882a593Smuzhiyun netdev_err(ppp->dev, "ppp_decompress_frame: "
2371*4882a593Smuzhiyun "no memory\n");
2372*4882a593Smuzhiyun goto err;
2373*4882a593Smuzhiyun }
2374*4882a593Smuzhiyun /* the decompressor still expects the A/C bytes in the hdr */
2375*4882a593Smuzhiyun len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
2376*4882a593Smuzhiyun skb->len + 2, ns->data, obuff_size);
2377*4882a593Smuzhiyun if (len < 0) {
2378*4882a593Smuzhiyun /* Pass the compressed frame to pppd as an
2379*4882a593Smuzhiyun error indication. */
2380*4882a593Smuzhiyun if (len == DECOMP_FATALERROR)
2381*4882a593Smuzhiyun ppp->rstate |= SC_DC_FERROR;
2382*4882a593Smuzhiyun kfree_skb(ns);
2383*4882a593Smuzhiyun goto err;
2384*4882a593Smuzhiyun }
2385*4882a593Smuzhiyun
2386*4882a593Smuzhiyun consume_skb(skb);
2387*4882a593Smuzhiyun skb = ns;
2388*4882a593Smuzhiyun skb_put(skb, len);
2389*4882a593Smuzhiyun skb_pull(skb, 2); /* pull off the A/C bytes */
2390*4882a593Smuzhiyun
2391*4882a593Smuzhiyun /* Don't call __ppp_decompress_proto() here, but instead rely on
2392*4882a593Smuzhiyun * corresponding algo (mppe/bsd/deflate) to decompress it.
2393*4882a593Smuzhiyun */
2394*4882a593Smuzhiyun } else {
2395*4882a593Smuzhiyun /* Uncompressed frame - pass to decompressor so it
2396*4882a593Smuzhiyun can update its dictionary if necessary. */
2397*4882a593Smuzhiyun if (ppp->rcomp->incomp)
2398*4882a593Smuzhiyun ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2399*4882a593Smuzhiyun skb->len + 2);
2400*4882a593Smuzhiyun }
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun return skb;
2403*4882a593Smuzhiyun
2404*4882a593Smuzhiyun err:
2405*4882a593Smuzhiyun ppp->rstate |= SC_DC_ERROR;
2406*4882a593Smuzhiyun ppp_receive_error(ppp);
2407*4882a593Smuzhiyun return skb;
2408*4882a593Smuzhiyun }
2409*4882a593Smuzhiyun
2410*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
2411*4882a593Smuzhiyun /*
2412*4882a593Smuzhiyun * Receive a multilink frame.
2413*4882a593Smuzhiyun * We put it on the reconstruction queue and then pull off
2414*4882a593Smuzhiyun * as many completed frames as we can.
2415*4882a593Smuzhiyun */
2416*4882a593Smuzhiyun static void
ppp_receive_mp_frame(struct ppp * ppp,struct sk_buff * skb,struct channel * pch)2417*4882a593Smuzhiyun ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2418*4882a593Smuzhiyun {
2419*4882a593Smuzhiyun u32 mask, seq;
2420*4882a593Smuzhiyun struct channel *ch;
2421*4882a593Smuzhiyun int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2422*4882a593Smuzhiyun
2423*4882a593Smuzhiyun if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
2424*4882a593Smuzhiyun goto err; /* no good, throw it away */
2425*4882a593Smuzhiyun
2426*4882a593Smuzhiyun /* Decode sequence number and begin/end bits */
2427*4882a593Smuzhiyun if (ppp->flags & SC_MP_SHORTSEQ) {
2428*4882a593Smuzhiyun seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2429*4882a593Smuzhiyun mask = 0xfff;
2430*4882a593Smuzhiyun } else {
2431*4882a593Smuzhiyun seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2432*4882a593Smuzhiyun mask = 0xffffff;
2433*4882a593Smuzhiyun }
2434*4882a593Smuzhiyun PPP_MP_CB(skb)->BEbits = skb->data[2];
2435*4882a593Smuzhiyun skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
2436*4882a593Smuzhiyun
2437*4882a593Smuzhiyun /*
2438*4882a593Smuzhiyun * Do protocol ID decompression on the first fragment of each packet.
2439*4882a593Smuzhiyun * We have to do that here, because ppp_receive_nonmp_frame() expects
2440*4882a593Smuzhiyun * decompressed protocol field.
2441*4882a593Smuzhiyun */
2442*4882a593Smuzhiyun if (PPP_MP_CB(skb)->BEbits & B)
2443*4882a593Smuzhiyun __ppp_decompress_proto(skb);
2444*4882a593Smuzhiyun
2445*4882a593Smuzhiyun /*
2446*4882a593Smuzhiyun * Expand sequence number to 32 bits, making it as close
2447*4882a593Smuzhiyun * as possible to ppp->minseq.
2448*4882a593Smuzhiyun */
2449*4882a593Smuzhiyun seq |= ppp->minseq & ~mask;
2450*4882a593Smuzhiyun if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2451*4882a593Smuzhiyun seq += mask + 1;
2452*4882a593Smuzhiyun else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2453*4882a593Smuzhiyun seq -= mask + 1; /* should never happen */
2454*4882a593Smuzhiyun PPP_MP_CB(skb)->sequence = seq;
2455*4882a593Smuzhiyun pch->lastseq = seq;
2456*4882a593Smuzhiyun
2457*4882a593Smuzhiyun /*
2458*4882a593Smuzhiyun * If this packet comes before the next one we were expecting,
2459*4882a593Smuzhiyun * drop it.
2460*4882a593Smuzhiyun */
2461*4882a593Smuzhiyun if (seq_before(seq, ppp->nextseq)) {
2462*4882a593Smuzhiyun kfree_skb(skb);
2463*4882a593Smuzhiyun ++ppp->dev->stats.rx_dropped;
2464*4882a593Smuzhiyun ppp_receive_error(ppp);
2465*4882a593Smuzhiyun return;
2466*4882a593Smuzhiyun }
2467*4882a593Smuzhiyun
2468*4882a593Smuzhiyun /*
2469*4882a593Smuzhiyun * Reevaluate minseq, the minimum over all channels of the
2470*4882a593Smuzhiyun * last sequence number received on each channel. Because of
2471*4882a593Smuzhiyun * the increasing sequence number rule, we know that any fragment
2472*4882a593Smuzhiyun * before `minseq' which hasn't arrived is never going to arrive.
2473*4882a593Smuzhiyun * The list of channels can't change because we have the receive
2474*4882a593Smuzhiyun * side of the ppp unit locked.
2475*4882a593Smuzhiyun */
2476*4882a593Smuzhiyun list_for_each_entry(ch, &ppp->channels, clist) {
2477*4882a593Smuzhiyun if (seq_before(ch->lastseq, seq))
2478*4882a593Smuzhiyun seq = ch->lastseq;
2479*4882a593Smuzhiyun }
2480*4882a593Smuzhiyun if (seq_before(ppp->minseq, seq))
2481*4882a593Smuzhiyun ppp->minseq = seq;
2482*4882a593Smuzhiyun
2483*4882a593Smuzhiyun /* Put the fragment on the reconstruction queue */
2484*4882a593Smuzhiyun ppp_mp_insert(ppp, skb);
2485*4882a593Smuzhiyun
2486*4882a593Smuzhiyun /* If the queue is getting long, don't wait any longer for packets
2487*4882a593Smuzhiyun before the start of the queue. */
2488*4882a593Smuzhiyun if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
2489*4882a593Smuzhiyun struct sk_buff *mskb = skb_peek(&ppp->mrq);
2490*4882a593Smuzhiyun if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2491*4882a593Smuzhiyun ppp->minseq = PPP_MP_CB(mskb)->sequence;
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun
2494*4882a593Smuzhiyun /* Pull completed packets off the queue and receive them. */
2495*4882a593Smuzhiyun while ((skb = ppp_mp_reconstruct(ppp))) {
2496*4882a593Smuzhiyun if (pskb_may_pull(skb, 2))
2497*4882a593Smuzhiyun ppp_receive_nonmp_frame(ppp, skb);
2498*4882a593Smuzhiyun else {
2499*4882a593Smuzhiyun ++ppp->dev->stats.rx_length_errors;
2500*4882a593Smuzhiyun kfree_skb(skb);
2501*4882a593Smuzhiyun ppp_receive_error(ppp);
2502*4882a593Smuzhiyun }
2503*4882a593Smuzhiyun }
2504*4882a593Smuzhiyun
2505*4882a593Smuzhiyun return;
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun err:
2508*4882a593Smuzhiyun kfree_skb(skb);
2509*4882a593Smuzhiyun ppp_receive_error(ppp);
2510*4882a593Smuzhiyun }
2511*4882a593Smuzhiyun
2512*4882a593Smuzhiyun /*
2513*4882a593Smuzhiyun * Insert a fragment on the MP reconstruction queue.
2514*4882a593Smuzhiyun * The queue is ordered by increasing sequence number.
2515*4882a593Smuzhiyun */
2516*4882a593Smuzhiyun static void
ppp_mp_insert(struct ppp * ppp,struct sk_buff * skb)2517*4882a593Smuzhiyun ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2518*4882a593Smuzhiyun {
2519*4882a593Smuzhiyun struct sk_buff *p;
2520*4882a593Smuzhiyun struct sk_buff_head *list = &ppp->mrq;
2521*4882a593Smuzhiyun u32 seq = PPP_MP_CB(skb)->sequence;
2522*4882a593Smuzhiyun
2523*4882a593Smuzhiyun /* N.B. we don't need to lock the list lock because we have the
2524*4882a593Smuzhiyun ppp unit receive-side lock. */
2525*4882a593Smuzhiyun skb_queue_walk(list, p) {
2526*4882a593Smuzhiyun if (seq_before(seq, PPP_MP_CB(p)->sequence))
2527*4882a593Smuzhiyun break;
2528*4882a593Smuzhiyun }
2529*4882a593Smuzhiyun __skb_queue_before(list, p, skb);
2530*4882a593Smuzhiyun }
2531*4882a593Smuzhiyun
2532*4882a593Smuzhiyun /*
2533*4882a593Smuzhiyun * Reconstruct a packet from the MP fragment queue.
2534*4882a593Smuzhiyun * We go through increasing sequence numbers until we find a
2535*4882a593Smuzhiyun * complete packet, or we get to the sequence number for a fragment
2536*4882a593Smuzhiyun * which hasn't arrived but might still do so.
2537*4882a593Smuzhiyun */
2538*4882a593Smuzhiyun static struct sk_buff *
ppp_mp_reconstruct(struct ppp * ppp)2539*4882a593Smuzhiyun ppp_mp_reconstruct(struct ppp *ppp)
2540*4882a593Smuzhiyun {
2541*4882a593Smuzhiyun u32 seq = ppp->nextseq;
2542*4882a593Smuzhiyun u32 minseq = ppp->minseq;
2543*4882a593Smuzhiyun struct sk_buff_head *list = &ppp->mrq;
2544*4882a593Smuzhiyun struct sk_buff *p, *tmp;
2545*4882a593Smuzhiyun struct sk_buff *head, *tail;
2546*4882a593Smuzhiyun struct sk_buff *skb = NULL;
2547*4882a593Smuzhiyun int lost = 0, len = 0;
2548*4882a593Smuzhiyun
2549*4882a593Smuzhiyun if (ppp->mrru == 0) /* do nothing until mrru is set */
2550*4882a593Smuzhiyun return NULL;
2551*4882a593Smuzhiyun head = __skb_peek(list);
2552*4882a593Smuzhiyun tail = NULL;
2553*4882a593Smuzhiyun skb_queue_walk_safe(list, p, tmp) {
2554*4882a593Smuzhiyun again:
2555*4882a593Smuzhiyun if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
2556*4882a593Smuzhiyun /* this can't happen, anyway ignore the skb */
2557*4882a593Smuzhiyun netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2558*4882a593Smuzhiyun "seq %u < %u\n",
2559*4882a593Smuzhiyun PPP_MP_CB(p)->sequence, seq);
2560*4882a593Smuzhiyun __skb_unlink(p, list);
2561*4882a593Smuzhiyun kfree_skb(p);
2562*4882a593Smuzhiyun continue;
2563*4882a593Smuzhiyun }
2564*4882a593Smuzhiyun if (PPP_MP_CB(p)->sequence != seq) {
2565*4882a593Smuzhiyun u32 oldseq;
2566*4882a593Smuzhiyun /* Fragment `seq' is missing. If it is after
2567*4882a593Smuzhiyun minseq, it might arrive later, so stop here. */
2568*4882a593Smuzhiyun if (seq_after(seq, minseq))
2569*4882a593Smuzhiyun break;
2570*4882a593Smuzhiyun /* Fragment `seq' is lost, keep going. */
2571*4882a593Smuzhiyun lost = 1;
2572*4882a593Smuzhiyun oldseq = seq;
2573*4882a593Smuzhiyun seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2574*4882a593Smuzhiyun minseq + 1: PPP_MP_CB(p)->sequence;
2575*4882a593Smuzhiyun
2576*4882a593Smuzhiyun if (ppp->debug & 1)
2577*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
2578*4882a593Smuzhiyun "lost frag %u..%u\n",
2579*4882a593Smuzhiyun oldseq, seq-1);
2580*4882a593Smuzhiyun
2581*4882a593Smuzhiyun goto again;
2582*4882a593Smuzhiyun }
2583*4882a593Smuzhiyun
2584*4882a593Smuzhiyun /*
2585*4882a593Smuzhiyun * At this point we know that all the fragments from
2586*4882a593Smuzhiyun * ppp->nextseq to seq are either present or lost.
2587*4882a593Smuzhiyun * Also, there are no complete packets in the queue
2588*4882a593Smuzhiyun * that have no missing fragments and end before this
2589*4882a593Smuzhiyun * fragment.
2590*4882a593Smuzhiyun */
2591*4882a593Smuzhiyun
2592*4882a593Smuzhiyun /* B bit set indicates this fragment starts a packet */
2593*4882a593Smuzhiyun if (PPP_MP_CB(p)->BEbits & B) {
2594*4882a593Smuzhiyun head = p;
2595*4882a593Smuzhiyun lost = 0;
2596*4882a593Smuzhiyun len = 0;
2597*4882a593Smuzhiyun }
2598*4882a593Smuzhiyun
2599*4882a593Smuzhiyun len += p->len;
2600*4882a593Smuzhiyun
2601*4882a593Smuzhiyun /* Got a complete packet yet? */
2602*4882a593Smuzhiyun if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2603*4882a593Smuzhiyun (PPP_MP_CB(head)->BEbits & B)) {
2604*4882a593Smuzhiyun if (len > ppp->mrru + 2) {
2605*4882a593Smuzhiyun ++ppp->dev->stats.rx_length_errors;
2606*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
2607*4882a593Smuzhiyun "PPP: reconstructed packet"
2608*4882a593Smuzhiyun " is too long (%d)\n", len);
2609*4882a593Smuzhiyun } else {
2610*4882a593Smuzhiyun tail = p;
2611*4882a593Smuzhiyun break;
2612*4882a593Smuzhiyun }
2613*4882a593Smuzhiyun ppp->nextseq = seq + 1;
2614*4882a593Smuzhiyun }
2615*4882a593Smuzhiyun
2616*4882a593Smuzhiyun /*
2617*4882a593Smuzhiyun * If this is the ending fragment of a packet,
2618*4882a593Smuzhiyun * and we haven't found a complete valid packet yet,
2619*4882a593Smuzhiyun * we can discard up to and including this fragment.
2620*4882a593Smuzhiyun */
2621*4882a593Smuzhiyun if (PPP_MP_CB(p)->BEbits & E) {
2622*4882a593Smuzhiyun struct sk_buff *tmp2;
2623*4882a593Smuzhiyun
2624*4882a593Smuzhiyun skb_queue_reverse_walk_from_safe(list, p, tmp2) {
2625*4882a593Smuzhiyun if (ppp->debug & 1)
2626*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
2627*4882a593Smuzhiyun "discarding frag %u\n",
2628*4882a593Smuzhiyun PPP_MP_CB(p)->sequence);
2629*4882a593Smuzhiyun __skb_unlink(p, list);
2630*4882a593Smuzhiyun kfree_skb(p);
2631*4882a593Smuzhiyun }
2632*4882a593Smuzhiyun head = skb_peek(list);
2633*4882a593Smuzhiyun if (!head)
2634*4882a593Smuzhiyun break;
2635*4882a593Smuzhiyun }
2636*4882a593Smuzhiyun ++seq;
2637*4882a593Smuzhiyun }
2638*4882a593Smuzhiyun
2639*4882a593Smuzhiyun /* If we have a complete packet, copy it all into one skb. */
2640*4882a593Smuzhiyun if (tail != NULL) {
2641*4882a593Smuzhiyun /* If we have discarded any fragments,
2642*4882a593Smuzhiyun signal a receive error. */
2643*4882a593Smuzhiyun if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
2644*4882a593Smuzhiyun skb_queue_walk_safe(list, p, tmp) {
2645*4882a593Smuzhiyun if (p == head)
2646*4882a593Smuzhiyun break;
2647*4882a593Smuzhiyun if (ppp->debug & 1)
2648*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
2649*4882a593Smuzhiyun "discarding frag %u\n",
2650*4882a593Smuzhiyun PPP_MP_CB(p)->sequence);
2651*4882a593Smuzhiyun __skb_unlink(p, list);
2652*4882a593Smuzhiyun kfree_skb(p);
2653*4882a593Smuzhiyun }
2654*4882a593Smuzhiyun
2655*4882a593Smuzhiyun if (ppp->debug & 1)
2656*4882a593Smuzhiyun netdev_printk(KERN_DEBUG, ppp->dev,
2657*4882a593Smuzhiyun " missed pkts %u..%u\n",
2658*4882a593Smuzhiyun ppp->nextseq,
2659*4882a593Smuzhiyun PPP_MP_CB(head)->sequence-1);
2660*4882a593Smuzhiyun ++ppp->dev->stats.rx_dropped;
2661*4882a593Smuzhiyun ppp_receive_error(ppp);
2662*4882a593Smuzhiyun }
2663*4882a593Smuzhiyun
2664*4882a593Smuzhiyun skb = head;
2665*4882a593Smuzhiyun if (head != tail) {
2666*4882a593Smuzhiyun struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2667*4882a593Smuzhiyun p = skb_queue_next(list, head);
2668*4882a593Smuzhiyun __skb_unlink(skb, list);
2669*4882a593Smuzhiyun skb_queue_walk_from_safe(list, p, tmp) {
2670*4882a593Smuzhiyun __skb_unlink(p, list);
2671*4882a593Smuzhiyun *fragpp = p;
2672*4882a593Smuzhiyun p->next = NULL;
2673*4882a593Smuzhiyun fragpp = &p->next;
2674*4882a593Smuzhiyun
2675*4882a593Smuzhiyun skb->len += p->len;
2676*4882a593Smuzhiyun skb->data_len += p->len;
2677*4882a593Smuzhiyun skb->truesize += p->truesize;
2678*4882a593Smuzhiyun
2679*4882a593Smuzhiyun if (p == tail)
2680*4882a593Smuzhiyun break;
2681*4882a593Smuzhiyun }
2682*4882a593Smuzhiyun } else {
2683*4882a593Smuzhiyun __skb_unlink(skb, list);
2684*4882a593Smuzhiyun }
2685*4882a593Smuzhiyun
2686*4882a593Smuzhiyun ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
2687*4882a593Smuzhiyun }
2688*4882a593Smuzhiyun
2689*4882a593Smuzhiyun return skb;
2690*4882a593Smuzhiyun }
2691*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
2692*4882a593Smuzhiyun
2693*4882a593Smuzhiyun /*
2694*4882a593Smuzhiyun * Channel interface.
2695*4882a593Smuzhiyun */
2696*4882a593Smuzhiyun
2697*4882a593Smuzhiyun /* Create a new, unattached ppp channel. */
ppp_register_channel(struct ppp_channel * chan)2698*4882a593Smuzhiyun int ppp_register_channel(struct ppp_channel *chan)
2699*4882a593Smuzhiyun {
2700*4882a593Smuzhiyun return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2701*4882a593Smuzhiyun }
2702*4882a593Smuzhiyun
2703*4882a593Smuzhiyun /* Create a new, unattached ppp channel for specified net. */
ppp_register_net_channel(struct net * net,struct ppp_channel * chan)2704*4882a593Smuzhiyun int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
2705*4882a593Smuzhiyun {
2706*4882a593Smuzhiyun struct channel *pch;
2707*4882a593Smuzhiyun struct ppp_net *pn;
2708*4882a593Smuzhiyun
2709*4882a593Smuzhiyun pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
2710*4882a593Smuzhiyun if (!pch)
2711*4882a593Smuzhiyun return -ENOMEM;
2712*4882a593Smuzhiyun
2713*4882a593Smuzhiyun pn = ppp_pernet(net);
2714*4882a593Smuzhiyun
2715*4882a593Smuzhiyun pch->ppp = NULL;
2716*4882a593Smuzhiyun pch->chan = chan;
2717*4882a593Smuzhiyun pch->chan_net = get_net(net);
2718*4882a593Smuzhiyun chan->ppp = pch;
2719*4882a593Smuzhiyun init_ppp_file(&pch->file, CHANNEL);
2720*4882a593Smuzhiyun pch->file.hdrlen = chan->hdrlen;
2721*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
2722*4882a593Smuzhiyun pch->lastseq = -1;
2723*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
2724*4882a593Smuzhiyun init_rwsem(&pch->chan_sem);
2725*4882a593Smuzhiyun spin_lock_init(&pch->downl);
2726*4882a593Smuzhiyun rwlock_init(&pch->upl);
2727*4882a593Smuzhiyun
2728*4882a593Smuzhiyun spin_lock_bh(&pn->all_channels_lock);
2729*4882a593Smuzhiyun pch->file.index = ++pn->last_channel_index;
2730*4882a593Smuzhiyun list_add(&pch->list, &pn->new_channels);
2731*4882a593Smuzhiyun atomic_inc(&channel_count);
2732*4882a593Smuzhiyun spin_unlock_bh(&pn->all_channels_lock);
2733*4882a593Smuzhiyun
2734*4882a593Smuzhiyun return 0;
2735*4882a593Smuzhiyun }
2736*4882a593Smuzhiyun
2737*4882a593Smuzhiyun /*
2738*4882a593Smuzhiyun * Return the index of a channel.
2739*4882a593Smuzhiyun */
ppp_channel_index(struct ppp_channel * chan)2740*4882a593Smuzhiyun int ppp_channel_index(struct ppp_channel *chan)
2741*4882a593Smuzhiyun {
2742*4882a593Smuzhiyun struct channel *pch = chan->ppp;
2743*4882a593Smuzhiyun
2744*4882a593Smuzhiyun if (pch)
2745*4882a593Smuzhiyun return pch->file.index;
2746*4882a593Smuzhiyun return -1;
2747*4882a593Smuzhiyun }
2748*4882a593Smuzhiyun
2749*4882a593Smuzhiyun /*
2750*4882a593Smuzhiyun * Return the PPP unit number to which a channel is connected.
2751*4882a593Smuzhiyun */
ppp_unit_number(struct ppp_channel * chan)2752*4882a593Smuzhiyun int ppp_unit_number(struct ppp_channel *chan)
2753*4882a593Smuzhiyun {
2754*4882a593Smuzhiyun struct channel *pch = chan->ppp;
2755*4882a593Smuzhiyun int unit = -1;
2756*4882a593Smuzhiyun
2757*4882a593Smuzhiyun if (pch) {
2758*4882a593Smuzhiyun read_lock_bh(&pch->upl);
2759*4882a593Smuzhiyun if (pch->ppp)
2760*4882a593Smuzhiyun unit = pch->ppp->file.index;
2761*4882a593Smuzhiyun read_unlock_bh(&pch->upl);
2762*4882a593Smuzhiyun }
2763*4882a593Smuzhiyun return unit;
2764*4882a593Smuzhiyun }
2765*4882a593Smuzhiyun
2766*4882a593Smuzhiyun /*
2767*4882a593Smuzhiyun * Return the PPP device interface name of a channel.
2768*4882a593Smuzhiyun */
ppp_dev_name(struct ppp_channel * chan)2769*4882a593Smuzhiyun char *ppp_dev_name(struct ppp_channel *chan)
2770*4882a593Smuzhiyun {
2771*4882a593Smuzhiyun struct channel *pch = chan->ppp;
2772*4882a593Smuzhiyun char *name = NULL;
2773*4882a593Smuzhiyun
2774*4882a593Smuzhiyun if (pch) {
2775*4882a593Smuzhiyun read_lock_bh(&pch->upl);
2776*4882a593Smuzhiyun if (pch->ppp && pch->ppp->dev)
2777*4882a593Smuzhiyun name = pch->ppp->dev->name;
2778*4882a593Smuzhiyun read_unlock_bh(&pch->upl);
2779*4882a593Smuzhiyun }
2780*4882a593Smuzhiyun return name;
2781*4882a593Smuzhiyun }
2782*4882a593Smuzhiyun
2783*4882a593Smuzhiyun
2784*4882a593Smuzhiyun /*
2785*4882a593Smuzhiyun * Disconnect a channel from the generic layer.
2786*4882a593Smuzhiyun * This must be called in process context.
2787*4882a593Smuzhiyun */
2788*4882a593Smuzhiyun void
ppp_unregister_channel(struct ppp_channel * chan)2789*4882a593Smuzhiyun ppp_unregister_channel(struct ppp_channel *chan)
2790*4882a593Smuzhiyun {
2791*4882a593Smuzhiyun struct channel *pch = chan->ppp;
2792*4882a593Smuzhiyun struct ppp_net *pn;
2793*4882a593Smuzhiyun
2794*4882a593Smuzhiyun if (!pch)
2795*4882a593Smuzhiyun return; /* should never happen */
2796*4882a593Smuzhiyun
2797*4882a593Smuzhiyun chan->ppp = NULL;
2798*4882a593Smuzhiyun
2799*4882a593Smuzhiyun /*
2800*4882a593Smuzhiyun * This ensures that we have returned from any calls into the
2801*4882a593Smuzhiyun * the channel's start_xmit or ioctl routine before we proceed.
2802*4882a593Smuzhiyun */
2803*4882a593Smuzhiyun down_write(&pch->chan_sem);
2804*4882a593Smuzhiyun spin_lock_bh(&pch->downl);
2805*4882a593Smuzhiyun pch->chan = NULL;
2806*4882a593Smuzhiyun spin_unlock_bh(&pch->downl);
2807*4882a593Smuzhiyun up_write(&pch->chan_sem);
2808*4882a593Smuzhiyun ppp_disconnect_channel(pch);
2809*4882a593Smuzhiyun
2810*4882a593Smuzhiyun pn = ppp_pernet(pch->chan_net);
2811*4882a593Smuzhiyun spin_lock_bh(&pn->all_channels_lock);
2812*4882a593Smuzhiyun list_del(&pch->list);
2813*4882a593Smuzhiyun spin_unlock_bh(&pn->all_channels_lock);
2814*4882a593Smuzhiyun
2815*4882a593Smuzhiyun pch->file.dead = 1;
2816*4882a593Smuzhiyun wake_up_interruptible(&pch->file.rwait);
2817*4882a593Smuzhiyun if (refcount_dec_and_test(&pch->file.refcnt))
2818*4882a593Smuzhiyun ppp_destroy_channel(pch);
2819*4882a593Smuzhiyun }
2820*4882a593Smuzhiyun
2821*4882a593Smuzhiyun /*
2822*4882a593Smuzhiyun * Callback from a channel when it can accept more to transmit.
2823*4882a593Smuzhiyun * This should be called at BH/softirq level, not interrupt level.
2824*4882a593Smuzhiyun */
2825*4882a593Smuzhiyun void
ppp_output_wakeup(struct ppp_channel * chan)2826*4882a593Smuzhiyun ppp_output_wakeup(struct ppp_channel *chan)
2827*4882a593Smuzhiyun {
2828*4882a593Smuzhiyun struct channel *pch = chan->ppp;
2829*4882a593Smuzhiyun
2830*4882a593Smuzhiyun if (!pch)
2831*4882a593Smuzhiyun return;
2832*4882a593Smuzhiyun ppp_channel_push(pch);
2833*4882a593Smuzhiyun }
2834*4882a593Smuzhiyun
2835*4882a593Smuzhiyun /*
2836*4882a593Smuzhiyun * Compression control.
2837*4882a593Smuzhiyun */
2838*4882a593Smuzhiyun
2839*4882a593Smuzhiyun /* Process the PPPIOCSCOMPRESS ioctl. */
2840*4882a593Smuzhiyun static int
ppp_set_compress(struct ppp * ppp,struct ppp_option_data * data)2841*4882a593Smuzhiyun ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data)
2842*4882a593Smuzhiyun {
2843*4882a593Smuzhiyun int err = -EFAULT;
2844*4882a593Smuzhiyun struct compressor *cp, *ocomp;
2845*4882a593Smuzhiyun void *state, *ostate;
2846*4882a593Smuzhiyun unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2847*4882a593Smuzhiyun
2848*4882a593Smuzhiyun if (data->length > CCP_MAX_OPTION_LENGTH)
2849*4882a593Smuzhiyun goto out;
2850*4882a593Smuzhiyun if (copy_from_user(ccp_option, data->ptr, data->length))
2851*4882a593Smuzhiyun goto out;
2852*4882a593Smuzhiyun
2853*4882a593Smuzhiyun err = -EINVAL;
2854*4882a593Smuzhiyun if (data->length < 2 || ccp_option[1] < 2 || ccp_option[1] > data->length)
2855*4882a593Smuzhiyun goto out;
2856*4882a593Smuzhiyun
2857*4882a593Smuzhiyun cp = try_then_request_module(
2858*4882a593Smuzhiyun find_compressor(ccp_option[0]),
2859*4882a593Smuzhiyun "ppp-compress-%d", ccp_option[0]);
2860*4882a593Smuzhiyun if (!cp)
2861*4882a593Smuzhiyun goto out;
2862*4882a593Smuzhiyun
2863*4882a593Smuzhiyun err = -ENOBUFS;
2864*4882a593Smuzhiyun if (data->transmit) {
2865*4882a593Smuzhiyun state = cp->comp_alloc(ccp_option, data->length);
2866*4882a593Smuzhiyun if (state) {
2867*4882a593Smuzhiyun ppp_xmit_lock(ppp);
2868*4882a593Smuzhiyun ppp->xstate &= ~SC_COMP_RUN;
2869*4882a593Smuzhiyun ocomp = ppp->xcomp;
2870*4882a593Smuzhiyun ostate = ppp->xc_state;
2871*4882a593Smuzhiyun ppp->xcomp = cp;
2872*4882a593Smuzhiyun ppp->xc_state = state;
2873*4882a593Smuzhiyun ppp_xmit_unlock(ppp);
2874*4882a593Smuzhiyun if (ostate) {
2875*4882a593Smuzhiyun ocomp->comp_free(ostate);
2876*4882a593Smuzhiyun module_put(ocomp->owner);
2877*4882a593Smuzhiyun }
2878*4882a593Smuzhiyun err = 0;
2879*4882a593Smuzhiyun } else
2880*4882a593Smuzhiyun module_put(cp->owner);
2881*4882a593Smuzhiyun
2882*4882a593Smuzhiyun } else {
2883*4882a593Smuzhiyun state = cp->decomp_alloc(ccp_option, data->length);
2884*4882a593Smuzhiyun if (state) {
2885*4882a593Smuzhiyun ppp_recv_lock(ppp);
2886*4882a593Smuzhiyun ppp->rstate &= ~SC_DECOMP_RUN;
2887*4882a593Smuzhiyun ocomp = ppp->rcomp;
2888*4882a593Smuzhiyun ostate = ppp->rc_state;
2889*4882a593Smuzhiyun ppp->rcomp = cp;
2890*4882a593Smuzhiyun ppp->rc_state = state;
2891*4882a593Smuzhiyun ppp_recv_unlock(ppp);
2892*4882a593Smuzhiyun if (ostate) {
2893*4882a593Smuzhiyun ocomp->decomp_free(ostate);
2894*4882a593Smuzhiyun module_put(ocomp->owner);
2895*4882a593Smuzhiyun }
2896*4882a593Smuzhiyun err = 0;
2897*4882a593Smuzhiyun } else
2898*4882a593Smuzhiyun module_put(cp->owner);
2899*4882a593Smuzhiyun }
2900*4882a593Smuzhiyun
2901*4882a593Smuzhiyun out:
2902*4882a593Smuzhiyun return err;
2903*4882a593Smuzhiyun }
2904*4882a593Smuzhiyun
2905*4882a593Smuzhiyun /*
2906*4882a593Smuzhiyun * Look at a CCP packet and update our state accordingly.
2907*4882a593Smuzhiyun * We assume the caller has the xmit or recv path locked.
2908*4882a593Smuzhiyun */
2909*4882a593Smuzhiyun static void
ppp_ccp_peek(struct ppp * ppp,struct sk_buff * skb,int inbound)2910*4882a593Smuzhiyun ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2911*4882a593Smuzhiyun {
2912*4882a593Smuzhiyun unsigned char *dp;
2913*4882a593Smuzhiyun int len;
2914*4882a593Smuzhiyun
2915*4882a593Smuzhiyun if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2916*4882a593Smuzhiyun return; /* no header */
2917*4882a593Smuzhiyun dp = skb->data + 2;
2918*4882a593Smuzhiyun
2919*4882a593Smuzhiyun switch (CCP_CODE(dp)) {
2920*4882a593Smuzhiyun case CCP_CONFREQ:
2921*4882a593Smuzhiyun
2922*4882a593Smuzhiyun /* A ConfReq starts negotiation of compression
2923*4882a593Smuzhiyun * in one direction of transmission,
2924*4882a593Smuzhiyun * and hence brings it down...but which way?
2925*4882a593Smuzhiyun *
2926*4882a593Smuzhiyun * Remember:
2927*4882a593Smuzhiyun * A ConfReq indicates what the sender would like to receive
2928*4882a593Smuzhiyun */
2929*4882a593Smuzhiyun if(inbound)
2930*4882a593Smuzhiyun /* He is proposing what I should send */
2931*4882a593Smuzhiyun ppp->xstate &= ~SC_COMP_RUN;
2932*4882a593Smuzhiyun else
2933*4882a593Smuzhiyun /* I am proposing to what he should send */
2934*4882a593Smuzhiyun ppp->rstate &= ~SC_DECOMP_RUN;
2935*4882a593Smuzhiyun
2936*4882a593Smuzhiyun break;
2937*4882a593Smuzhiyun
2938*4882a593Smuzhiyun case CCP_TERMREQ:
2939*4882a593Smuzhiyun case CCP_TERMACK:
2940*4882a593Smuzhiyun /*
2941*4882a593Smuzhiyun * CCP is going down, both directions of transmission
2942*4882a593Smuzhiyun */
2943*4882a593Smuzhiyun ppp->rstate &= ~SC_DECOMP_RUN;
2944*4882a593Smuzhiyun ppp->xstate &= ~SC_COMP_RUN;
2945*4882a593Smuzhiyun break;
2946*4882a593Smuzhiyun
2947*4882a593Smuzhiyun case CCP_CONFACK:
2948*4882a593Smuzhiyun if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2949*4882a593Smuzhiyun break;
2950*4882a593Smuzhiyun len = CCP_LENGTH(dp);
2951*4882a593Smuzhiyun if (!pskb_may_pull(skb, len + 2))
2952*4882a593Smuzhiyun return; /* too short */
2953*4882a593Smuzhiyun dp += CCP_HDRLEN;
2954*4882a593Smuzhiyun len -= CCP_HDRLEN;
2955*4882a593Smuzhiyun if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2956*4882a593Smuzhiyun break;
2957*4882a593Smuzhiyun if (inbound) {
2958*4882a593Smuzhiyun /* we will start receiving compressed packets */
2959*4882a593Smuzhiyun if (!ppp->rc_state)
2960*4882a593Smuzhiyun break;
2961*4882a593Smuzhiyun if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2962*4882a593Smuzhiyun ppp->file.index, 0, ppp->mru, ppp->debug)) {
2963*4882a593Smuzhiyun ppp->rstate |= SC_DECOMP_RUN;
2964*4882a593Smuzhiyun ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2965*4882a593Smuzhiyun }
2966*4882a593Smuzhiyun } else {
2967*4882a593Smuzhiyun /* we will soon start sending compressed packets */
2968*4882a593Smuzhiyun if (!ppp->xc_state)
2969*4882a593Smuzhiyun break;
2970*4882a593Smuzhiyun if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2971*4882a593Smuzhiyun ppp->file.index, 0, ppp->debug))
2972*4882a593Smuzhiyun ppp->xstate |= SC_COMP_RUN;
2973*4882a593Smuzhiyun }
2974*4882a593Smuzhiyun break;
2975*4882a593Smuzhiyun
2976*4882a593Smuzhiyun case CCP_RESETACK:
2977*4882a593Smuzhiyun /* reset the [de]compressor */
2978*4882a593Smuzhiyun if ((ppp->flags & SC_CCP_UP) == 0)
2979*4882a593Smuzhiyun break;
2980*4882a593Smuzhiyun if (inbound) {
2981*4882a593Smuzhiyun if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2982*4882a593Smuzhiyun ppp->rcomp->decomp_reset(ppp->rc_state);
2983*4882a593Smuzhiyun ppp->rstate &= ~SC_DC_ERROR;
2984*4882a593Smuzhiyun }
2985*4882a593Smuzhiyun } else {
2986*4882a593Smuzhiyun if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2987*4882a593Smuzhiyun ppp->xcomp->comp_reset(ppp->xc_state);
2988*4882a593Smuzhiyun }
2989*4882a593Smuzhiyun break;
2990*4882a593Smuzhiyun }
2991*4882a593Smuzhiyun }
2992*4882a593Smuzhiyun
2993*4882a593Smuzhiyun /* Free up compression resources. */
2994*4882a593Smuzhiyun static void
ppp_ccp_closed(struct ppp * ppp)2995*4882a593Smuzhiyun ppp_ccp_closed(struct ppp *ppp)
2996*4882a593Smuzhiyun {
2997*4882a593Smuzhiyun void *xstate, *rstate;
2998*4882a593Smuzhiyun struct compressor *xcomp, *rcomp;
2999*4882a593Smuzhiyun
3000*4882a593Smuzhiyun ppp_lock(ppp);
3001*4882a593Smuzhiyun ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
3002*4882a593Smuzhiyun ppp->xstate = 0;
3003*4882a593Smuzhiyun xcomp = ppp->xcomp;
3004*4882a593Smuzhiyun xstate = ppp->xc_state;
3005*4882a593Smuzhiyun ppp->xc_state = NULL;
3006*4882a593Smuzhiyun ppp->rstate = 0;
3007*4882a593Smuzhiyun rcomp = ppp->rcomp;
3008*4882a593Smuzhiyun rstate = ppp->rc_state;
3009*4882a593Smuzhiyun ppp->rc_state = NULL;
3010*4882a593Smuzhiyun ppp_unlock(ppp);
3011*4882a593Smuzhiyun
3012*4882a593Smuzhiyun if (xstate) {
3013*4882a593Smuzhiyun xcomp->comp_free(xstate);
3014*4882a593Smuzhiyun module_put(xcomp->owner);
3015*4882a593Smuzhiyun }
3016*4882a593Smuzhiyun if (rstate) {
3017*4882a593Smuzhiyun rcomp->decomp_free(rstate);
3018*4882a593Smuzhiyun module_put(rcomp->owner);
3019*4882a593Smuzhiyun }
3020*4882a593Smuzhiyun }
3021*4882a593Smuzhiyun
3022*4882a593Smuzhiyun /* List of compressors. */
3023*4882a593Smuzhiyun static LIST_HEAD(compressor_list);
3024*4882a593Smuzhiyun static DEFINE_SPINLOCK(compressor_list_lock);
3025*4882a593Smuzhiyun
3026*4882a593Smuzhiyun struct compressor_entry {
3027*4882a593Smuzhiyun struct list_head list;
3028*4882a593Smuzhiyun struct compressor *comp;
3029*4882a593Smuzhiyun };
3030*4882a593Smuzhiyun
3031*4882a593Smuzhiyun static struct compressor_entry *
find_comp_entry(int proto)3032*4882a593Smuzhiyun find_comp_entry(int proto)
3033*4882a593Smuzhiyun {
3034*4882a593Smuzhiyun struct compressor_entry *ce;
3035*4882a593Smuzhiyun
3036*4882a593Smuzhiyun list_for_each_entry(ce, &compressor_list, list) {
3037*4882a593Smuzhiyun if (ce->comp->compress_proto == proto)
3038*4882a593Smuzhiyun return ce;
3039*4882a593Smuzhiyun }
3040*4882a593Smuzhiyun return NULL;
3041*4882a593Smuzhiyun }
3042*4882a593Smuzhiyun
3043*4882a593Smuzhiyun /* Register a compressor */
3044*4882a593Smuzhiyun int
ppp_register_compressor(struct compressor * cp)3045*4882a593Smuzhiyun ppp_register_compressor(struct compressor *cp)
3046*4882a593Smuzhiyun {
3047*4882a593Smuzhiyun struct compressor_entry *ce;
3048*4882a593Smuzhiyun int ret;
3049*4882a593Smuzhiyun spin_lock(&compressor_list_lock);
3050*4882a593Smuzhiyun ret = -EEXIST;
3051*4882a593Smuzhiyun if (find_comp_entry(cp->compress_proto))
3052*4882a593Smuzhiyun goto out;
3053*4882a593Smuzhiyun ret = -ENOMEM;
3054*4882a593Smuzhiyun ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
3055*4882a593Smuzhiyun if (!ce)
3056*4882a593Smuzhiyun goto out;
3057*4882a593Smuzhiyun ret = 0;
3058*4882a593Smuzhiyun ce->comp = cp;
3059*4882a593Smuzhiyun list_add(&ce->list, &compressor_list);
3060*4882a593Smuzhiyun out:
3061*4882a593Smuzhiyun spin_unlock(&compressor_list_lock);
3062*4882a593Smuzhiyun return ret;
3063*4882a593Smuzhiyun }
3064*4882a593Smuzhiyun
3065*4882a593Smuzhiyun /* Unregister a compressor */
3066*4882a593Smuzhiyun void
ppp_unregister_compressor(struct compressor * cp)3067*4882a593Smuzhiyun ppp_unregister_compressor(struct compressor *cp)
3068*4882a593Smuzhiyun {
3069*4882a593Smuzhiyun struct compressor_entry *ce;
3070*4882a593Smuzhiyun
3071*4882a593Smuzhiyun spin_lock(&compressor_list_lock);
3072*4882a593Smuzhiyun ce = find_comp_entry(cp->compress_proto);
3073*4882a593Smuzhiyun if (ce && ce->comp == cp) {
3074*4882a593Smuzhiyun list_del(&ce->list);
3075*4882a593Smuzhiyun kfree(ce);
3076*4882a593Smuzhiyun }
3077*4882a593Smuzhiyun spin_unlock(&compressor_list_lock);
3078*4882a593Smuzhiyun }
3079*4882a593Smuzhiyun
3080*4882a593Smuzhiyun /* Find a compressor. */
3081*4882a593Smuzhiyun static struct compressor *
find_compressor(int type)3082*4882a593Smuzhiyun find_compressor(int type)
3083*4882a593Smuzhiyun {
3084*4882a593Smuzhiyun struct compressor_entry *ce;
3085*4882a593Smuzhiyun struct compressor *cp = NULL;
3086*4882a593Smuzhiyun
3087*4882a593Smuzhiyun spin_lock(&compressor_list_lock);
3088*4882a593Smuzhiyun ce = find_comp_entry(type);
3089*4882a593Smuzhiyun if (ce) {
3090*4882a593Smuzhiyun cp = ce->comp;
3091*4882a593Smuzhiyun if (!try_module_get(cp->owner))
3092*4882a593Smuzhiyun cp = NULL;
3093*4882a593Smuzhiyun }
3094*4882a593Smuzhiyun spin_unlock(&compressor_list_lock);
3095*4882a593Smuzhiyun return cp;
3096*4882a593Smuzhiyun }
3097*4882a593Smuzhiyun
3098*4882a593Smuzhiyun /*
3099*4882a593Smuzhiyun * Miscelleneous stuff.
3100*4882a593Smuzhiyun */
3101*4882a593Smuzhiyun
3102*4882a593Smuzhiyun static void
ppp_get_stats(struct ppp * ppp,struct ppp_stats * st)3103*4882a593Smuzhiyun ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
3104*4882a593Smuzhiyun {
3105*4882a593Smuzhiyun struct slcompress *vj = ppp->vj;
3106*4882a593Smuzhiyun
3107*4882a593Smuzhiyun memset(st, 0, sizeof(*st));
3108*4882a593Smuzhiyun st->p.ppp_ipackets = ppp->stats64.rx_packets;
3109*4882a593Smuzhiyun st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
3110*4882a593Smuzhiyun st->p.ppp_ibytes = ppp->stats64.rx_bytes;
3111*4882a593Smuzhiyun st->p.ppp_opackets = ppp->stats64.tx_packets;
3112*4882a593Smuzhiyun st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
3113*4882a593Smuzhiyun st->p.ppp_obytes = ppp->stats64.tx_bytes;
3114*4882a593Smuzhiyun if (!vj)
3115*4882a593Smuzhiyun return;
3116*4882a593Smuzhiyun st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
3117*4882a593Smuzhiyun st->vj.vjs_compressed = vj->sls_o_compressed;
3118*4882a593Smuzhiyun st->vj.vjs_searches = vj->sls_o_searches;
3119*4882a593Smuzhiyun st->vj.vjs_misses = vj->sls_o_misses;
3120*4882a593Smuzhiyun st->vj.vjs_errorin = vj->sls_i_error;
3121*4882a593Smuzhiyun st->vj.vjs_tossed = vj->sls_i_tossed;
3122*4882a593Smuzhiyun st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
3123*4882a593Smuzhiyun st->vj.vjs_compressedin = vj->sls_i_compressed;
3124*4882a593Smuzhiyun }
3125*4882a593Smuzhiyun
3126*4882a593Smuzhiyun /*
3127*4882a593Smuzhiyun * Stuff for handling the lists of ppp units and channels
3128*4882a593Smuzhiyun * and for initialization.
3129*4882a593Smuzhiyun */
3130*4882a593Smuzhiyun
3131*4882a593Smuzhiyun /*
3132*4882a593Smuzhiyun * Create a new ppp interface unit. Fails if it can't allocate memory
3133*4882a593Smuzhiyun * or if there is already a unit with the requested number.
3134*4882a593Smuzhiyun * unit == -1 means allocate a new number.
3135*4882a593Smuzhiyun */
ppp_create_interface(struct net * net,struct file * file,int * unit)3136*4882a593Smuzhiyun static int ppp_create_interface(struct net *net, struct file *file, int *unit)
3137*4882a593Smuzhiyun {
3138*4882a593Smuzhiyun struct ppp_config conf = {
3139*4882a593Smuzhiyun .file = file,
3140*4882a593Smuzhiyun .unit = *unit,
3141*4882a593Smuzhiyun .ifname_is_set = false,
3142*4882a593Smuzhiyun };
3143*4882a593Smuzhiyun struct net_device *dev;
3144*4882a593Smuzhiyun struct ppp *ppp;
3145*4882a593Smuzhiyun int err;
3146*4882a593Smuzhiyun
3147*4882a593Smuzhiyun dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
3148*4882a593Smuzhiyun if (!dev) {
3149*4882a593Smuzhiyun err = -ENOMEM;
3150*4882a593Smuzhiyun goto err;
3151*4882a593Smuzhiyun }
3152*4882a593Smuzhiyun dev_net_set(dev, net);
3153*4882a593Smuzhiyun dev->rtnl_link_ops = &ppp_link_ops;
3154*4882a593Smuzhiyun
3155*4882a593Smuzhiyun rtnl_lock();
3156*4882a593Smuzhiyun
3157*4882a593Smuzhiyun err = ppp_dev_configure(net, dev, &conf);
3158*4882a593Smuzhiyun if (err < 0)
3159*4882a593Smuzhiyun goto err_dev;
3160*4882a593Smuzhiyun ppp = netdev_priv(dev);
3161*4882a593Smuzhiyun *unit = ppp->file.index;
3162*4882a593Smuzhiyun
3163*4882a593Smuzhiyun rtnl_unlock();
3164*4882a593Smuzhiyun
3165*4882a593Smuzhiyun return 0;
3166*4882a593Smuzhiyun
3167*4882a593Smuzhiyun err_dev:
3168*4882a593Smuzhiyun rtnl_unlock();
3169*4882a593Smuzhiyun free_netdev(dev);
3170*4882a593Smuzhiyun err:
3171*4882a593Smuzhiyun return err;
3172*4882a593Smuzhiyun }
3173*4882a593Smuzhiyun
3174*4882a593Smuzhiyun /*
3175*4882a593Smuzhiyun * Initialize a ppp_file structure.
3176*4882a593Smuzhiyun */
3177*4882a593Smuzhiyun static void
init_ppp_file(struct ppp_file * pf,int kind)3178*4882a593Smuzhiyun init_ppp_file(struct ppp_file *pf, int kind)
3179*4882a593Smuzhiyun {
3180*4882a593Smuzhiyun pf->kind = kind;
3181*4882a593Smuzhiyun skb_queue_head_init(&pf->xq);
3182*4882a593Smuzhiyun skb_queue_head_init(&pf->rq);
3183*4882a593Smuzhiyun refcount_set(&pf->refcnt, 1);
3184*4882a593Smuzhiyun init_waitqueue_head(&pf->rwait);
3185*4882a593Smuzhiyun }
3186*4882a593Smuzhiyun
3187*4882a593Smuzhiyun /*
3188*4882a593Smuzhiyun * Free the memory used by a ppp unit. This is only called once
3189*4882a593Smuzhiyun * there are no channels connected to the unit and no file structs
3190*4882a593Smuzhiyun * that reference the unit.
3191*4882a593Smuzhiyun */
ppp_destroy_interface(struct ppp * ppp)3192*4882a593Smuzhiyun static void ppp_destroy_interface(struct ppp *ppp)
3193*4882a593Smuzhiyun {
3194*4882a593Smuzhiyun atomic_dec(&ppp_unit_count);
3195*4882a593Smuzhiyun
3196*4882a593Smuzhiyun if (!ppp->file.dead || ppp->n_channels) {
3197*4882a593Smuzhiyun /* "can't happen" */
3198*4882a593Smuzhiyun netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3199*4882a593Smuzhiyun "but dead=%d n_channels=%d !\n",
3200*4882a593Smuzhiyun ppp, ppp->file.dead, ppp->n_channels);
3201*4882a593Smuzhiyun return;
3202*4882a593Smuzhiyun }
3203*4882a593Smuzhiyun
3204*4882a593Smuzhiyun ppp_ccp_closed(ppp);
3205*4882a593Smuzhiyun if (ppp->vj) {
3206*4882a593Smuzhiyun slhc_free(ppp->vj);
3207*4882a593Smuzhiyun ppp->vj = NULL;
3208*4882a593Smuzhiyun }
3209*4882a593Smuzhiyun skb_queue_purge(&ppp->file.xq);
3210*4882a593Smuzhiyun skb_queue_purge(&ppp->file.rq);
3211*4882a593Smuzhiyun #ifdef CONFIG_PPP_MULTILINK
3212*4882a593Smuzhiyun skb_queue_purge(&ppp->mrq);
3213*4882a593Smuzhiyun #endif /* CONFIG_PPP_MULTILINK */
3214*4882a593Smuzhiyun #ifdef CONFIG_PPP_FILTER
3215*4882a593Smuzhiyun if (ppp->pass_filter) {
3216*4882a593Smuzhiyun bpf_prog_destroy(ppp->pass_filter);
3217*4882a593Smuzhiyun ppp->pass_filter = NULL;
3218*4882a593Smuzhiyun }
3219*4882a593Smuzhiyun
3220*4882a593Smuzhiyun if (ppp->active_filter) {
3221*4882a593Smuzhiyun bpf_prog_destroy(ppp->active_filter);
3222*4882a593Smuzhiyun ppp->active_filter = NULL;
3223*4882a593Smuzhiyun }
3224*4882a593Smuzhiyun #endif /* CONFIG_PPP_FILTER */
3225*4882a593Smuzhiyun
3226*4882a593Smuzhiyun kfree_skb(ppp->xmit_pending);
3227*4882a593Smuzhiyun free_percpu(ppp->xmit_recursion);
3228*4882a593Smuzhiyun
3229*4882a593Smuzhiyun free_netdev(ppp->dev);
3230*4882a593Smuzhiyun }
3231*4882a593Smuzhiyun
3232*4882a593Smuzhiyun /*
3233*4882a593Smuzhiyun * Locate an existing ppp unit.
3234*4882a593Smuzhiyun * The caller should have locked the all_ppp_mutex.
3235*4882a593Smuzhiyun */
3236*4882a593Smuzhiyun static struct ppp *
ppp_find_unit(struct ppp_net * pn,int unit)3237*4882a593Smuzhiyun ppp_find_unit(struct ppp_net *pn, int unit)
3238*4882a593Smuzhiyun {
3239*4882a593Smuzhiyun return unit_find(&pn->units_idr, unit);
3240*4882a593Smuzhiyun }
3241*4882a593Smuzhiyun
3242*4882a593Smuzhiyun /*
3243*4882a593Smuzhiyun * Locate an existing ppp channel.
3244*4882a593Smuzhiyun * The caller should have locked the all_channels_lock.
3245*4882a593Smuzhiyun * First we look in the new_channels list, then in the
3246*4882a593Smuzhiyun * all_channels list. If found in the new_channels list,
3247*4882a593Smuzhiyun * we move it to the all_channels list. This is for speed
3248*4882a593Smuzhiyun * when we have a lot of channels in use.
3249*4882a593Smuzhiyun */
3250*4882a593Smuzhiyun static struct channel *
ppp_find_channel(struct ppp_net * pn,int unit)3251*4882a593Smuzhiyun ppp_find_channel(struct ppp_net *pn, int unit)
3252*4882a593Smuzhiyun {
3253*4882a593Smuzhiyun struct channel *pch;
3254*4882a593Smuzhiyun
3255*4882a593Smuzhiyun list_for_each_entry(pch, &pn->new_channels, list) {
3256*4882a593Smuzhiyun if (pch->file.index == unit) {
3257*4882a593Smuzhiyun list_move(&pch->list, &pn->all_channels);
3258*4882a593Smuzhiyun return pch;
3259*4882a593Smuzhiyun }
3260*4882a593Smuzhiyun }
3261*4882a593Smuzhiyun
3262*4882a593Smuzhiyun list_for_each_entry(pch, &pn->all_channels, list) {
3263*4882a593Smuzhiyun if (pch->file.index == unit)
3264*4882a593Smuzhiyun return pch;
3265*4882a593Smuzhiyun }
3266*4882a593Smuzhiyun
3267*4882a593Smuzhiyun return NULL;
3268*4882a593Smuzhiyun }
3269*4882a593Smuzhiyun
3270*4882a593Smuzhiyun /*
3271*4882a593Smuzhiyun * Connect a PPP channel to a PPP interface unit.
3272*4882a593Smuzhiyun */
3273*4882a593Smuzhiyun static int
ppp_connect_channel(struct channel * pch,int unit)3274*4882a593Smuzhiyun ppp_connect_channel(struct channel *pch, int unit)
3275*4882a593Smuzhiyun {
3276*4882a593Smuzhiyun struct ppp *ppp;
3277*4882a593Smuzhiyun struct ppp_net *pn;
3278*4882a593Smuzhiyun int ret = -ENXIO;
3279*4882a593Smuzhiyun int hdrlen;
3280*4882a593Smuzhiyun
3281*4882a593Smuzhiyun pn = ppp_pernet(pch->chan_net);
3282*4882a593Smuzhiyun
3283*4882a593Smuzhiyun mutex_lock(&pn->all_ppp_mutex);
3284*4882a593Smuzhiyun ppp = ppp_find_unit(pn, unit);
3285*4882a593Smuzhiyun if (!ppp)
3286*4882a593Smuzhiyun goto out;
3287*4882a593Smuzhiyun write_lock_bh(&pch->upl);
3288*4882a593Smuzhiyun ret = -EINVAL;
3289*4882a593Smuzhiyun if (pch->ppp)
3290*4882a593Smuzhiyun goto outl;
3291*4882a593Smuzhiyun
3292*4882a593Smuzhiyun ppp_lock(ppp);
3293*4882a593Smuzhiyun spin_lock_bh(&pch->downl);
3294*4882a593Smuzhiyun if (!pch->chan) {
3295*4882a593Smuzhiyun /* Don't connect unregistered channels */
3296*4882a593Smuzhiyun spin_unlock_bh(&pch->downl);
3297*4882a593Smuzhiyun ppp_unlock(ppp);
3298*4882a593Smuzhiyun ret = -ENOTCONN;
3299*4882a593Smuzhiyun goto outl;
3300*4882a593Smuzhiyun }
3301*4882a593Smuzhiyun spin_unlock_bh(&pch->downl);
3302*4882a593Smuzhiyun if (pch->file.hdrlen > ppp->file.hdrlen)
3303*4882a593Smuzhiyun ppp->file.hdrlen = pch->file.hdrlen;
3304*4882a593Smuzhiyun hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
3305*4882a593Smuzhiyun if (hdrlen > ppp->dev->hard_header_len)
3306*4882a593Smuzhiyun ppp->dev->hard_header_len = hdrlen;
3307*4882a593Smuzhiyun list_add_tail(&pch->clist, &ppp->channels);
3308*4882a593Smuzhiyun ++ppp->n_channels;
3309*4882a593Smuzhiyun pch->ppp = ppp;
3310*4882a593Smuzhiyun refcount_inc(&ppp->file.refcnt);
3311*4882a593Smuzhiyun ppp_unlock(ppp);
3312*4882a593Smuzhiyun ret = 0;
3313*4882a593Smuzhiyun
3314*4882a593Smuzhiyun outl:
3315*4882a593Smuzhiyun write_unlock_bh(&pch->upl);
3316*4882a593Smuzhiyun out:
3317*4882a593Smuzhiyun mutex_unlock(&pn->all_ppp_mutex);
3318*4882a593Smuzhiyun return ret;
3319*4882a593Smuzhiyun }
3320*4882a593Smuzhiyun
3321*4882a593Smuzhiyun /*
3322*4882a593Smuzhiyun * Disconnect a channel from its ppp unit.
3323*4882a593Smuzhiyun */
3324*4882a593Smuzhiyun static int
ppp_disconnect_channel(struct channel * pch)3325*4882a593Smuzhiyun ppp_disconnect_channel(struct channel *pch)
3326*4882a593Smuzhiyun {
3327*4882a593Smuzhiyun struct ppp *ppp;
3328*4882a593Smuzhiyun int err = -EINVAL;
3329*4882a593Smuzhiyun
3330*4882a593Smuzhiyun write_lock_bh(&pch->upl);
3331*4882a593Smuzhiyun ppp = pch->ppp;
3332*4882a593Smuzhiyun pch->ppp = NULL;
3333*4882a593Smuzhiyun write_unlock_bh(&pch->upl);
3334*4882a593Smuzhiyun if (ppp) {
3335*4882a593Smuzhiyun /* remove it from the ppp unit's list */
3336*4882a593Smuzhiyun ppp_lock(ppp);
3337*4882a593Smuzhiyun list_del(&pch->clist);
3338*4882a593Smuzhiyun if (--ppp->n_channels == 0)
3339*4882a593Smuzhiyun wake_up_interruptible(&ppp->file.rwait);
3340*4882a593Smuzhiyun ppp_unlock(ppp);
3341*4882a593Smuzhiyun if (refcount_dec_and_test(&ppp->file.refcnt))
3342*4882a593Smuzhiyun ppp_destroy_interface(ppp);
3343*4882a593Smuzhiyun err = 0;
3344*4882a593Smuzhiyun }
3345*4882a593Smuzhiyun return err;
3346*4882a593Smuzhiyun }
3347*4882a593Smuzhiyun
3348*4882a593Smuzhiyun /*
3349*4882a593Smuzhiyun * Free up the resources used by a ppp channel.
3350*4882a593Smuzhiyun */
ppp_destroy_channel(struct channel * pch)3351*4882a593Smuzhiyun static void ppp_destroy_channel(struct channel *pch)
3352*4882a593Smuzhiyun {
3353*4882a593Smuzhiyun put_net(pch->chan_net);
3354*4882a593Smuzhiyun pch->chan_net = NULL;
3355*4882a593Smuzhiyun
3356*4882a593Smuzhiyun atomic_dec(&channel_count);
3357*4882a593Smuzhiyun
3358*4882a593Smuzhiyun if (!pch->file.dead) {
3359*4882a593Smuzhiyun /* "can't happen" */
3360*4882a593Smuzhiyun pr_err("ppp: destroying undead channel %p !\n", pch);
3361*4882a593Smuzhiyun return;
3362*4882a593Smuzhiyun }
3363*4882a593Smuzhiyun skb_queue_purge(&pch->file.xq);
3364*4882a593Smuzhiyun skb_queue_purge(&pch->file.rq);
3365*4882a593Smuzhiyun kfree(pch);
3366*4882a593Smuzhiyun }
3367*4882a593Smuzhiyun
ppp_cleanup(void)3368*4882a593Smuzhiyun static void __exit ppp_cleanup(void)
3369*4882a593Smuzhiyun {
3370*4882a593Smuzhiyun /* should never happen */
3371*4882a593Smuzhiyun if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
3372*4882a593Smuzhiyun pr_err("PPP: removing module but units remain!\n");
3373*4882a593Smuzhiyun rtnl_link_unregister(&ppp_link_ops);
3374*4882a593Smuzhiyun unregister_chrdev(PPP_MAJOR, "ppp");
3375*4882a593Smuzhiyun device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
3376*4882a593Smuzhiyun class_destroy(ppp_class);
3377*4882a593Smuzhiyun unregister_pernet_device(&ppp_net_ops);
3378*4882a593Smuzhiyun }
3379*4882a593Smuzhiyun
3380*4882a593Smuzhiyun /*
3381*4882a593Smuzhiyun * Units handling. Caller must protect concurrent access
3382*4882a593Smuzhiyun * by holding all_ppp_mutex
3383*4882a593Smuzhiyun */
3384*4882a593Smuzhiyun
3385*4882a593Smuzhiyun /* associate pointer with specified number */
unit_set(struct idr * p,void * ptr,int n)3386*4882a593Smuzhiyun static int unit_set(struct idr *p, void *ptr, int n)
3387*4882a593Smuzhiyun {
3388*4882a593Smuzhiyun int unit;
3389*4882a593Smuzhiyun
3390*4882a593Smuzhiyun unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3391*4882a593Smuzhiyun if (unit == -ENOSPC)
3392*4882a593Smuzhiyun unit = -EINVAL;
3393*4882a593Smuzhiyun return unit;
3394*4882a593Smuzhiyun }
3395*4882a593Smuzhiyun
3396*4882a593Smuzhiyun /* get new free unit number and associate pointer with it */
unit_get(struct idr * p,void * ptr,int min)3397*4882a593Smuzhiyun static int unit_get(struct idr *p, void *ptr, int min)
3398*4882a593Smuzhiyun {
3399*4882a593Smuzhiyun return idr_alloc(p, ptr, min, 0, GFP_KERNEL);
3400*4882a593Smuzhiyun }
3401*4882a593Smuzhiyun
3402*4882a593Smuzhiyun /* put unit number back to a pool */
unit_put(struct idr * p,int n)3403*4882a593Smuzhiyun static void unit_put(struct idr *p, int n)
3404*4882a593Smuzhiyun {
3405*4882a593Smuzhiyun idr_remove(p, n);
3406*4882a593Smuzhiyun }
3407*4882a593Smuzhiyun
3408*4882a593Smuzhiyun /* get pointer associated with the number */
unit_find(struct idr * p,int n)3409*4882a593Smuzhiyun static void *unit_find(struct idr *p, int n)
3410*4882a593Smuzhiyun {
3411*4882a593Smuzhiyun return idr_find(p, n);
3412*4882a593Smuzhiyun }
3413*4882a593Smuzhiyun
3414*4882a593Smuzhiyun /* Module/initialization stuff */
3415*4882a593Smuzhiyun
3416*4882a593Smuzhiyun module_init(ppp_init);
3417*4882a593Smuzhiyun module_exit(ppp_cleanup);
3418*4882a593Smuzhiyun
3419*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_register_net_channel);
3420*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_register_channel);
3421*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_unregister_channel);
3422*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_channel_index);
3423*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_unit_number);
3424*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_dev_name);
3425*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_input);
3426*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_input_error);
3427*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_output_wakeup);
3428*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_register_compressor);
3429*4882a593Smuzhiyun EXPORT_SYMBOL(ppp_unregister_compressor);
3430*4882a593Smuzhiyun MODULE_LICENSE("GPL");
3431*4882a593Smuzhiyun MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
3432*4882a593Smuzhiyun MODULE_ALIAS_RTNL_LINK("ppp");
3433*4882a593Smuzhiyun MODULE_ALIAS("devname:ppp");
3434