blob: 75e8903c37540d89ae8036a47de8de185297a023 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic PPP layer for Linux.
3 *
4 * Copyright 1999-2002 Paul Mackerras.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * The generic PPP layer handles the PPP network interfaces, the
12 * /dev/ppp device, packet and VJ compression, and multilink.
13 * It talks to PPP `channels' via the interface defined in
14 * include/linux/ppp_channel.h. Channels provide the basic means for
15 * sending and receiving PPP frames on some kind of communications
16 * channel.
17 *
18 * Part of the code in this driver was inspired by the old async-only
19 * PPP driver, written by Michael Callahan and Al Longyear, and
20 * subsequently hacked by Paul Mackerras.
21 *
22 * ==FILEVERSION 20041108==
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/kmod.h>
28#include <linux/init.h>
29#include <linux/list.h>
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -080030#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/netdevice.h>
32#include <linux/poll.h>
33#include <linux/ppp_defs.h>
34#include <linux/filter.h>
35#include <linux/if_ppp.h>
36#include <linux/ppp_channel.h>
37#include <linux/ppp-comp.h>
38#include <linux/skbuff.h>
39#include <linux/rtnetlink.h>
40#include <linux/if_arp.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
Jonathan Corbetf2b98572008-05-18 15:32:43 -060043#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/rwsem.h>
46#include <linux/stddef.h>
47#include <linux/device.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080048#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/slhc_vj.h>
51#include <asm/atomic.h>
52
Cyrill Gorcunov273ec512009-01-21 15:55:35 -080053#include <linux/nsproxy.h>
54#include <net/net_namespace.h>
55#include <net/netns/generic.h>
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define PPP_VERSION "2.4.2"
58
59/*
60 * Network protocols we support.
61 */
62#define NP_IP 0 /* Internet Protocol V4 */
63#define NP_IPV6 1 /* Internet Protocol V6 */
64#define NP_IPX 2 /* IPX protocol */
65#define NP_AT 3 /* Appletalk protocol */
66#define NP_MPLS_UC 4 /* MPLS unicast */
67#define NP_MPLS_MC 5 /* MPLS multicast */
68#define NUM_NP 6 /* Number of NPs. */
69
70#define MPHDRLEN 6 /* multilink protocol header length */
71#define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
72#define MIN_FRAG_SIZE 64
73
74/*
75 * An instance of /dev/ppp can be associated with either a ppp
76 * interface unit or a ppp channel. In both cases, file->private_data
77 * points to one of these.
78 */
79struct ppp_file {
80 enum {
81 INTERFACE=1, CHANNEL
82 } kind;
83 struct sk_buff_head xq; /* pppd transmit queue */
84 struct sk_buff_head rq; /* receive queue for pppd */
85 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
86 atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
87 int hdrlen; /* space to leave for headers */
88 int index; /* interface unit / channel number */
89 int dead; /* unit/channel has been shut down */
90};
91
Robert P. J. Dayb385a142007-02-10 01:46:25 -080092#define PF_TO_X(pf, X) container_of(pf, X, file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
95#define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * Data structure describing one ppp unit.
99 * A ppp unit corresponds to a ppp network interface device
100 * and represents a multilink bundle.
101 * It can have 0 or more ppp channels connected to it.
102 */
103struct ppp {
104 struct ppp_file file; /* stuff for read/write/poll 0 */
105 struct file *owner; /* file that owns this unit 48 */
106 struct list_head channels; /* list of attached channels 4c */
107 int n_channels; /* how many channels are attached 54 */
108 spinlock_t rlock; /* lock for receive side 58 */
109 spinlock_t wlock; /* lock for transmit side 5c */
110 int mru; /* max receive unit 60 */
111 unsigned int flags; /* control bits 64 */
112 unsigned int xstate; /* transmit state bits 68 */
113 unsigned int rstate; /* receive state bits 6c */
114 int debug; /* debug flags 70 */
115 struct slcompress *vj; /* state for VJ header compression */
116 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
117 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
118 struct compressor *xcomp; /* transmit packet compressor 8c */
119 void *xc_state; /* its internal state 90 */
120 struct compressor *rcomp; /* receive decompressor 94 */
121 void *rc_state; /* its internal state 98 */
122 unsigned long last_xmit; /* jiffies when last pkt sent 9c */
123 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
124 struct net_device *dev; /* network interface device a4 */
James Chapman739840d2008-12-17 12:02:16 +0000125 int closing; /* is device closing down? a8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#ifdef CONFIG_PPP_MULTILINK
127 int nxchan; /* next channel to send something on */
128 u32 nxseq; /* next sequence number to send */
129 int mrru; /* MP: max reconst. receive unit */
130 u32 nextseq; /* MP: seq no of next packet */
131 u32 minseq; /* MP: min of most recent seqnos */
132 struct sk_buff_head mrq; /* MP: receive reconstruction queue */
133#endif /* CONFIG_PPP_MULTILINK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#ifdef CONFIG_PPP_FILTER
135 struct sock_filter *pass_filter; /* filter for packets to pass */
136 struct sock_filter *active_filter;/* filter for pkts to reset idle */
137 unsigned pass_len, active_len;
138#endif /* CONFIG_PPP_FILTER */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800139 struct net *ppp_net; /* the net we belong to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
142/*
143 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
Matt Domschb3f9b922005-11-08 09:40:47 -0800144 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
145 * SC_MUST_COMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
147 * Bits in xstate: SC_COMP_RUN
148 */
149#define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
150 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
Matt Domschb3f9b922005-11-08 09:40:47 -0800151 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
154 * Private data structure for each channel.
155 * This includes the data structure used for multilink.
156 */
157struct channel {
158 struct ppp_file file; /* stuff for read/write/poll */
159 struct list_head list; /* link in all/new_channels list */
160 struct ppp_channel *chan; /* public channel data structure */
161 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
162 spinlock_t downl; /* protects `chan', file.xq dequeue */
163 struct ppp *ppp; /* ppp unit we're connected to */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800164 struct net *chan_net; /* the net channel belongs to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct list_head clist; /* link in list of channels per unit */
166 rwlock_t upl; /* protects `ppp' */
167#ifdef CONFIG_PPP_MULTILINK
168 u8 avail; /* flag used in multilink stuff */
169 u8 had_frag; /* >= 1 fragments have been sent */
170 u32 lastseq; /* MP: last sequence # received */
Lennart Sorensenfa44a732010-01-18 12:59:55 +0000171 int speed; /* speed of the corresponding ppp channel*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif /* CONFIG_PPP_MULTILINK */
173};
174
175/*
176 * SMP locking issues:
177 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
178 * list and the ppp.n_channels field, you need to take both locks
179 * before you modify them.
180 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
181 * channel.downl.
182 */
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static atomic_t ppp_unit_count = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static atomic_t channel_count = ATOMIC_INIT(0);
186
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800187/* per-net private data for this module */
Eric Dumazetf99189b2009-11-17 10:42:49 +0000188static int ppp_net_id __read_mostly;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800189struct ppp_net {
190 /* units to ppp mapping */
191 struct idr units_idr;
192
193 /*
194 * all_ppp_mutex protects the units_idr mapping.
195 * It also ensures that finding a ppp unit in the units_idr
196 * map and updating its file.refcnt field is atomic.
197 */
198 struct mutex all_ppp_mutex;
199
200 /* channels */
201 struct list_head all_channels;
202 struct list_head new_channels;
203 int last_channel_index;
204
205 /*
206 * all_channels_lock protects all_channels and
207 * last_channel_index, and the atomicity of find
208 * a channel and updating its file.refcnt field.
209 */
210 spinlock_t all_channels_lock;
211};
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* Get the PPP protocol number from a skb */
214#define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
215
216/* We limit the length of ppp->file.rq to this (arbitrary) value */
217#define PPP_MAX_RQLEN 32
218
219/*
220 * Maximum number of multilink fragments queued up.
221 * This has to be large enough to cope with the maximum latency of
222 * the slowest channel relative to the others. Strictly it should
223 * depend on the number of channels and their characteristics.
224 */
225#define PPP_MP_MAX_QLEN 128
226
227/* Multilink header bits. */
228#define B 0x80 /* this fragment begins a packet */
229#define E 0x40 /* this fragment ends a packet */
230
231/* Compare multilink sequence numbers (assumed to be 32 bits wide) */
232#define seq_before(a, b) ((s32)((a) - (b)) < 0)
233#define seq_after(a, b) ((s32)((a) - (b)) > 0)
234
235/* Prototypes. */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800236static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
237 struct file *file, unsigned int cmd, unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void ppp_xmit_process(struct ppp *ppp);
239static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
240static void ppp_push(struct ppp *ppp);
241static void ppp_channel_push(struct channel *pch);
242static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
243 struct channel *pch);
244static void ppp_receive_error(struct ppp *ppp);
245static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
246static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
247 struct sk_buff *skb);
248#ifdef CONFIG_PPP_MULTILINK
249static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
250 struct channel *pch);
251static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
252static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
253static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
254#endif /* CONFIG_PPP_MULTILINK */
255static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
256static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
257static void ppp_ccp_closed(struct ppp *ppp);
258static struct compressor *find_compressor(int type);
259static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800260static struct ppp *ppp_create_interface(struct net *net, int unit, int *retp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static void init_ppp_file(struct ppp_file *pf, int kind);
262static void ppp_shutdown_interface(struct ppp *ppp);
263static void ppp_destroy_interface(struct ppp *ppp);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800264static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
265static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static int ppp_connect_channel(struct channel *pch, int unit);
267static int ppp_disconnect_channel(struct channel *pch);
268static void ppp_destroy_channel(struct channel *pch);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -0800269static int unit_get(struct idr *p, void *ptr);
Cyrill Gorcunov85997572009-01-12 22:11:56 -0800270static int unit_set(struct idr *p, void *ptr, int n);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -0800271static void unit_put(struct idr *p, int n);
272static void *unit_find(struct idr *p, int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
gregkh@suse.de56b22932005-03-23 10:01:41 -0800274static struct class *ppp_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800276/* per net-namespace data */
277static inline struct ppp_net *ppp_pernet(struct net *net)
278{
279 BUG_ON(!net);
280
281 return net_generic(net, ppp_net_id);
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* Translates a PPP protocol number to a NP index (NP == network protocol) */
285static inline int proto_to_npindex(int proto)
286{
287 switch (proto) {
288 case PPP_IP:
289 return NP_IP;
290 case PPP_IPV6:
291 return NP_IPV6;
292 case PPP_IPX:
293 return NP_IPX;
294 case PPP_AT:
295 return NP_AT;
296 case PPP_MPLS_UC:
297 return NP_MPLS_UC;
298 case PPP_MPLS_MC:
299 return NP_MPLS_MC;
300 }
301 return -EINVAL;
302}
303
304/* Translates an NP index into a PPP protocol number */
305static const int npindex_to_proto[NUM_NP] = {
306 PPP_IP,
307 PPP_IPV6,
308 PPP_IPX,
309 PPP_AT,
310 PPP_MPLS_UC,
311 PPP_MPLS_MC,
312};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Translates an ethertype into an NP index */
315static inline int ethertype_to_npindex(int ethertype)
316{
317 switch (ethertype) {
318 case ETH_P_IP:
319 return NP_IP;
320 case ETH_P_IPV6:
321 return NP_IPV6;
322 case ETH_P_IPX:
323 return NP_IPX;
324 case ETH_P_PPPTALK:
325 case ETH_P_ATALK:
326 return NP_AT;
327 case ETH_P_MPLS_UC:
328 return NP_MPLS_UC;
329 case ETH_P_MPLS_MC:
330 return NP_MPLS_MC;
331 }
332 return -1;
333}
334
335/* Translates an NP index into an ethertype */
336static const int npindex_to_ethertype[NUM_NP] = {
337 ETH_P_IP,
338 ETH_P_IPV6,
339 ETH_P_IPX,
340 ETH_P_PPPTALK,
341 ETH_P_MPLS_UC,
342 ETH_P_MPLS_MC,
343};
344
345/*
346 * Locking shorthand.
347 */
348#define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
349#define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
350#define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
351#define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
352#define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
353 ppp_recv_lock(ppp); } while (0)
354#define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
355 ppp_xmit_unlock(ppp); } while (0)
356
357/*
358 * /dev/ppp device routines.
359 * The /dev/ppp device is used by pppd to control the ppp unit.
360 * It supports the read, write, ioctl and poll functions.
361 * Open instances of /dev/ppp can be in one of three states:
362 * unattached, attached to a ppp unit, or attached to a ppp channel.
363 */
364static int ppp_open(struct inode *inode, struct file *file)
365{
Jonathan Corbetf2b98572008-05-18 15:32:43 -0600366 cycle_kernel_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
368 * This could (should?) be enforced by the permissions on /dev/ppp.
369 */
370 if (!capable(CAP_NET_ADMIN))
371 return -EPERM;
372 return 0;
373}
374
Alan Coxf3ff8a42008-05-25 23:40:58 -0700375static int ppp_release(struct inode *unused, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct ppp_file *pf = file->private_data;
378 struct ppp *ppp;
379
Joe Perchescd228d52007-11-12 18:07:31 -0800380 if (pf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 file->private_data = NULL;
382 if (pf->kind == INTERFACE) {
383 ppp = PF_TO_PPP(pf);
384 if (file == ppp->owner)
385 ppp_shutdown_interface(ppp);
386 }
387 if (atomic_dec_and_test(&pf->refcnt)) {
388 switch (pf->kind) {
389 case INTERFACE:
390 ppp_destroy_interface(PF_TO_PPP(pf));
391 break;
392 case CHANNEL:
393 ppp_destroy_channel(PF_TO_CHANNEL(pf));
394 break;
395 }
396 }
397 }
398 return 0;
399}
400
401static ssize_t ppp_read(struct file *file, char __user *buf,
402 size_t count, loff_t *ppos)
403{
404 struct ppp_file *pf = file->private_data;
405 DECLARE_WAITQUEUE(wait, current);
406 ssize_t ret;
407 struct sk_buff *skb = NULL;
408
409 ret = count;
410
Joe Perchescd228d52007-11-12 18:07:31 -0800411 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -ENXIO;
413 add_wait_queue(&pf->rwait, &wait);
414 for (;;) {
415 set_current_state(TASK_INTERRUPTIBLE);
416 skb = skb_dequeue(&pf->rq);
417 if (skb)
418 break;
419 ret = 0;
420 if (pf->dead)
421 break;
422 if (pf->kind == INTERFACE) {
423 /*
424 * Return 0 (EOF) on an interface that has no
425 * channels connected, unless it is looping
426 * network traffic (demand mode).
427 */
428 struct ppp *ppp = PF_TO_PPP(pf);
Joe Perches8e95a202009-12-03 07:58:21 +0000429 if (ppp->n_channels == 0 &&
430 (ppp->flags & SC_LOOP_TRAFFIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 }
433 ret = -EAGAIN;
434 if (file->f_flags & O_NONBLOCK)
435 break;
436 ret = -ERESTARTSYS;
437 if (signal_pending(current))
438 break;
439 schedule();
440 }
441 set_current_state(TASK_RUNNING);
442 remove_wait_queue(&pf->rwait, &wait);
443
Joe Perchescd228d52007-11-12 18:07:31 -0800444 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto out;
446
447 ret = -EOVERFLOW;
448 if (skb->len > count)
449 goto outf;
450 ret = -EFAULT;
451 if (copy_to_user(buf, skb->data, skb->len))
452 goto outf;
453 ret = skb->len;
454
455 outf:
456 kfree_skb(skb);
457 out:
458 return ret;
459}
460
461static ssize_t ppp_write(struct file *file, const char __user *buf,
462 size_t count, loff_t *ppos)
463{
464 struct ppp_file *pf = file->private_data;
465 struct sk_buff *skb;
466 ssize_t ret;
467
Joe Perchescd228d52007-11-12 18:07:31 -0800468 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -ENXIO;
470 ret = -ENOMEM;
471 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -0800472 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto out;
474 skb_reserve(skb, pf->hdrlen);
475 ret = -EFAULT;
476 if (copy_from_user(skb_put(skb, count), buf, count)) {
477 kfree_skb(skb);
478 goto out;
479 }
480
481 skb_queue_tail(&pf->xq, skb);
482
483 switch (pf->kind) {
484 case INTERFACE:
485 ppp_xmit_process(PF_TO_PPP(pf));
486 break;
487 case CHANNEL:
488 ppp_channel_push(PF_TO_CHANNEL(pf));
489 break;
490 }
491
492 ret = count;
493
494 out:
495 return ret;
496}
497
498/* No kernel lock - fine */
499static unsigned int ppp_poll(struct file *file, poll_table *wait)
500{
501 struct ppp_file *pf = file->private_data;
502 unsigned int mask;
503
Joe Perchescd228d52007-11-12 18:07:31 -0800504 if (!pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506 poll_wait(file, &pf->rwait, wait);
507 mask = POLLOUT | POLLWRNORM;
Joe Perchescd228d52007-11-12 18:07:31 -0800508 if (skb_peek(&pf->rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 mask |= POLLIN | POLLRDNORM;
510 if (pf->dead)
511 mask |= POLLHUP;
512 else if (pf->kind == INTERFACE) {
513 /* see comment in ppp_read */
514 struct ppp *ppp = PF_TO_PPP(pf);
Joe Perches8e95a202009-12-03 07:58:21 +0000515 if (ppp->n_channels == 0 &&
516 (ppp->flags & SC_LOOP_TRAFFIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 mask |= POLLIN | POLLRDNORM;
518 }
519
520 return mask;
521}
522
523#ifdef CONFIG_PPP_FILTER
524static int get_filter(void __user *arg, struct sock_filter **p)
525{
526 struct sock_fprog uprog;
527 struct sock_filter *code = NULL;
528 int len, err;
529
530 if (copy_from_user(&uprog, arg, sizeof(uprog)))
531 return -EFAULT;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!uprog.len) {
534 *p = NULL;
535 return 0;
536 }
537
538 len = uprog.len * sizeof(struct sock_filter);
539 code = kmalloc(len, GFP_KERNEL);
540 if (code == NULL)
541 return -ENOMEM;
542
543 if (copy_from_user(code, uprog.filter, len)) {
544 kfree(code);
545 return -EFAULT;
546 }
547
548 err = sk_chk_filter(code, uprog.len);
549 if (err) {
550 kfree(code);
551 return err;
552 }
553
554 *p = code;
555 return uprog.len;
556}
557#endif /* CONFIG_PPP_FILTER */
558
Alan Coxf3ff8a42008-05-25 23:40:58 -0700559static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct ppp_file *pf = file->private_data;
562 struct ppp *ppp;
563 int err = -EFAULT, val, val2, i;
564 struct ppp_idle idle;
565 struct npioctl npi;
566 int unit, cflags;
567 struct slcompress *vj;
568 void __user *argp = (void __user *)arg;
569 int __user *p = argp;
570
Joe Perchescd228d52007-11-12 18:07:31 -0800571 if (!pf)
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800572 return ppp_unattached_ioctl(current->nsproxy->net_ns,
573 pf, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (cmd == PPPIOCDETACH) {
576 /*
577 * We have to be careful here... if the file descriptor
578 * has been dup'd, we could have another process in the
579 * middle of a poll using the same file *, so we had
580 * better not free the interface data structures -
581 * instead we fail the ioctl. Even in this case, we
582 * shut down the interface if we are the owner of it.
583 * Actually, we should get rid of PPPIOCDETACH, userland
584 * (i.e. pppd) could achieve the same effect by closing
585 * this fd and reopening /dev/ppp.
586 */
587 err = -EINVAL;
Alan Coxf3ff8a42008-05-25 23:40:58 -0700588 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (pf->kind == INTERFACE) {
590 ppp = PF_TO_PPP(pf);
591 if (file == ppp->owner)
592 ppp_shutdown_interface(ppp);
593 }
Al Viro516e0cc2008-07-26 00:39:17 -0400594 if (atomic_long_read(&file->f_count) <= 2) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700595 ppp_release(NULL, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 err = 0;
597 } else
Al Viro516e0cc2008-07-26 00:39:17 -0400598 printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%ld\n",
599 atomic_long_read(&file->f_count));
Alan Coxf3ff8a42008-05-25 23:40:58 -0700600 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return err;
602 }
603
604 if (pf->kind == CHANNEL) {
Alan Coxf3ff8a42008-05-25 23:40:58 -0700605 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct ppp_channel *chan;
607
Alan Coxf3ff8a42008-05-25 23:40:58 -0700608 lock_kernel();
609 pch = PF_TO_CHANNEL(pf);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 switch (cmd) {
612 case PPPIOCCONNECT:
613 if (get_user(unit, p))
614 break;
615 err = ppp_connect_channel(pch, unit);
616 break;
617
618 case PPPIOCDISCONN:
619 err = ppp_disconnect_channel(pch);
620 break;
621
622 default:
623 down_read(&pch->chan_sem);
624 chan = pch->chan;
625 err = -ENOTTY;
626 if (chan && chan->ops->ioctl)
627 err = chan->ops->ioctl(chan, cmd, arg);
628 up_read(&pch->chan_sem);
629 }
Alan Coxf3ff8a42008-05-25 23:40:58 -0700630 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return err;
632 }
633
634 if (pf->kind != INTERFACE) {
635 /* can't happen */
636 printk(KERN_ERR "PPP: not interface or channel??\n");
637 return -EINVAL;
638 }
639
Alan Coxf3ff8a42008-05-25 23:40:58 -0700640 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ppp = PF_TO_PPP(pf);
642 switch (cmd) {
643 case PPPIOCSMRU:
644 if (get_user(val, p))
645 break;
646 ppp->mru = val;
647 err = 0;
648 break;
649
650 case PPPIOCSFLAGS:
651 if (get_user(val, p))
652 break;
653 ppp_lock(ppp);
654 cflags = ppp->flags & ~val;
655 ppp->flags = val & SC_FLAG_BITS;
656 ppp_unlock(ppp);
657 if (cflags & SC_CCP_OPEN)
658 ppp_ccp_closed(ppp);
659 err = 0;
660 break;
661
662 case PPPIOCGFLAGS:
663 val = ppp->flags | ppp->xstate | ppp->rstate;
664 if (put_user(val, p))
665 break;
666 err = 0;
667 break;
668
669 case PPPIOCSCOMPRESS:
670 err = ppp_set_compress(ppp, arg);
671 break;
672
673 case PPPIOCGUNIT:
674 if (put_user(ppp->file.index, p))
675 break;
676 err = 0;
677 break;
678
679 case PPPIOCSDEBUG:
680 if (get_user(val, p))
681 break;
682 ppp->debug = val;
683 err = 0;
684 break;
685
686 case PPPIOCGDEBUG:
687 if (put_user(ppp->debug, p))
688 break;
689 err = 0;
690 break;
691
692 case PPPIOCGIDLE:
693 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
694 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
695 if (copy_to_user(argp, &idle, sizeof(idle)))
696 break;
697 err = 0;
698 break;
699
700 case PPPIOCSMAXCID:
701 if (get_user(val, p))
702 break;
703 val2 = 15;
704 if ((val >> 16) != 0) {
705 val2 = val >> 16;
706 val &= 0xffff;
707 }
708 vj = slhc_init(val2+1, val+1);
Joe Perchescd228d52007-11-12 18:07:31 -0800709 if (!vj) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
711 err = -ENOMEM;
712 break;
713 }
714 ppp_lock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -0800715 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 slhc_free(ppp->vj);
717 ppp->vj = vj;
718 ppp_unlock(ppp);
719 err = 0;
720 break;
721
722 case PPPIOCGNPMODE:
723 case PPPIOCSNPMODE:
724 if (copy_from_user(&npi, argp, sizeof(npi)))
725 break;
726 err = proto_to_npindex(npi.protocol);
727 if (err < 0)
728 break;
729 i = err;
730 if (cmd == PPPIOCGNPMODE) {
731 err = -EFAULT;
732 npi.mode = ppp->npmode[i];
733 if (copy_to_user(argp, &npi, sizeof(npi)))
734 break;
735 } else {
736 ppp->npmode[i] = npi.mode;
737 /* we may be able to transmit more packets now (??) */
738 netif_wake_queue(ppp->dev);
739 }
740 err = 0;
741 break;
742
743#ifdef CONFIG_PPP_FILTER
744 case PPPIOCSPASS:
745 {
746 struct sock_filter *code;
747 err = get_filter(argp, &code);
748 if (err >= 0) {
749 ppp_lock(ppp);
750 kfree(ppp->pass_filter);
751 ppp->pass_filter = code;
752 ppp->pass_len = err;
753 ppp_unlock(ppp);
754 err = 0;
755 }
756 break;
757 }
758 case PPPIOCSACTIVE:
759 {
760 struct sock_filter *code;
761 err = get_filter(argp, &code);
762 if (err >= 0) {
763 ppp_lock(ppp);
764 kfree(ppp->active_filter);
765 ppp->active_filter = code;
766 ppp->active_len = err;
767 ppp_unlock(ppp);
768 err = 0;
769 }
770 break;
771 }
772#endif /* CONFIG_PPP_FILTER */
773
774#ifdef CONFIG_PPP_MULTILINK
775 case PPPIOCSMRRU:
776 if (get_user(val, p))
777 break;
778 ppp_recv_lock(ppp);
779 ppp->mrru = val;
780 ppp_recv_unlock(ppp);
781 err = 0;
782 break;
783#endif /* CONFIG_PPP_MULTILINK */
784
785 default:
786 err = -ENOTTY;
787 }
Alan Coxf3ff8a42008-05-25 23:40:58 -0700788 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return err;
790}
791
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800792static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
793 struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 int unit, err = -EFAULT;
796 struct ppp *ppp;
797 struct channel *chan;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800798 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int __user *p = (int __user *)arg;
800
Alan Coxf3ff8a42008-05-25 23:40:58 -0700801 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 switch (cmd) {
803 case PPPIOCNEWUNIT:
804 /* Create a new ppp unit */
805 if (get_user(unit, p))
806 break;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800807 ppp = ppp_create_interface(net, unit, &err);
Joe Perchescd228d52007-11-12 18:07:31 -0800808 if (!ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
810 file->private_data = &ppp->file;
811 ppp->owner = file;
812 err = -EFAULT;
813 if (put_user(ppp->file.index, p))
814 break;
815 err = 0;
816 break;
817
818 case PPPIOCATTACH:
819 /* Attach to an existing ppp unit */
820 if (get_user(unit, p))
821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800823 pn = ppp_pernet(net);
824 mutex_lock(&pn->all_ppp_mutex);
825 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800826 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 atomic_inc(&ppp->file.refcnt);
828 file->private_data = &ppp->file;
829 err = 0;
830 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800831 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
833
834 case PPPIOCATTCHAN:
835 if (get_user(unit, p))
836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 err = -ENXIO;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800838 pn = ppp_pernet(net);
839 spin_lock_bh(&pn->all_channels_lock);
840 chan = ppp_find_channel(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -0800841 if (chan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 atomic_inc(&chan->file.refcnt);
843 file->private_data = &chan->file;
844 err = 0;
845 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800846 spin_unlock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 break;
848
849 default:
850 err = -ENOTTY;
851 }
Alan Coxf3ff8a42008-05-25 23:40:58 -0700852 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return err;
854}
855
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800856static const struct file_operations ppp_device_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .owner = THIS_MODULE,
858 .read = ppp_read,
859 .write = ppp_write,
860 .poll = ppp_poll,
Alan Coxf3ff8a42008-05-25 23:40:58 -0700861 .unlocked_ioctl = ppp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 .open = ppp_open,
863 .release = ppp_release
864};
865
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800866static __net_init int ppp_init_net(struct net *net)
867{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000868 struct ppp_net *pn = net_generic(net, ppp_net_id);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800869
870 idr_init(&pn->units_idr);
871 mutex_init(&pn->all_ppp_mutex);
872
873 INIT_LIST_HEAD(&pn->all_channels);
874 INIT_LIST_HEAD(&pn->new_channels);
875
876 spin_lock_init(&pn->all_channels_lock);
877
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800878 return 0;
879}
880
881static __net_exit void ppp_exit_net(struct net *net)
882{
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000883 struct ppp_net *pn = net_generic(net, ppp_net_id);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800884
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800885 idr_destroy(&pn->units_idr);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800886}
887
Alexey Dobriyan00129852009-02-09 18:05:16 -0800888static struct pernet_operations ppp_net_ops = {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800889 .init = ppp_init_net,
890 .exit = ppp_exit_net,
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000891 .id = &ppp_net_id,
892 .size = sizeof(struct ppp_net),
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800893};
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#define PPP_MAJOR 108
896
897/* Called at boot time if ppp is compiled into the kernel,
898 or at module load time (from init_module) if compiled as a module. */
899static int __init ppp_init(void)
900{
901 int err;
902
903 printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800904
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000905 err = register_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800906 if (err) {
907 printk(KERN_ERR "failed to register PPP pernet device (%d)\n", err);
908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800911 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
912 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800914 goto out_net;
915 }
916
917 ppp_class = class_create(THIS_MODULE, "ppp");
918 if (IS_ERR(ppp_class)) {
919 err = PTR_ERR(ppp_class);
920 goto out_chrdev;
921 }
922
923 /* not a big deal if we fail here :-) */
924 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
925
926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928out_chrdev:
929 unregister_chrdev(PPP_MAJOR, "ppp");
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800930out_net:
Eric W. Biederman741a6fa2009-11-29 15:46:09 +0000931 unregister_pernet_device(&ppp_net_ops);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -0800932out:
933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
937 * Network interface unit routines.
938 */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000939static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
941{
Wang Chenc8019bf2008-11-20 04:24:17 -0800942 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 int npi, proto;
944 unsigned char *pp;
945
946 npi = ethertype_to_npindex(ntohs(skb->protocol));
947 if (npi < 0)
948 goto outf;
949
950 /* Drop, accept or reject the packet */
951 switch (ppp->npmode[npi]) {
952 case NPMODE_PASS:
953 break;
954 case NPMODE_QUEUE:
955 /* it would be nice to have a way to tell the network
956 system to queue this one up for later. */
957 goto outf;
958 case NPMODE_DROP:
959 case NPMODE_ERROR:
960 goto outf;
961 }
962
963 /* Put the 2-byte PPP protocol number on the front,
964 making sure there is room for the address and control fields. */
Herbert Xu7b797d52007-09-16 16:21:42 -0700965 if (skb_cow_head(skb, PPP_HDRLEN))
966 goto outf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 pp = skb_push(skb, 2);
969 proto = npindex_to_proto[npi];
970 pp[0] = proto >> 8;
971 pp[1] = proto;
972
973 netif_stop_queue(dev);
974 skb_queue_tail(&ppp->file.xq, skb);
975 ppp_xmit_process(ppp);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000976 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 outf:
979 kfree_skb(skb);
Paulius Zaleckas6ceffd42009-02-23 04:59:43 +0000980 ++dev->stats.tx_dropped;
Patrick McHardy6ed10652009-06-23 06:03:08 +0000981 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static int
985ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
986{
Wang Chenc8019bf2008-11-20 04:24:17 -0800987 struct ppp *ppp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int err = -EFAULT;
989 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
990 struct ppp_stats stats;
991 struct ppp_comp_stats cstats;
992 char *vers;
993
994 switch (cmd) {
995 case SIOCGPPPSTATS:
996 ppp_get_stats(ppp, &stats);
997 if (copy_to_user(addr, &stats, sizeof(stats)))
998 break;
999 err = 0;
1000 break;
1001
1002 case SIOCGPPPCSTATS:
1003 memset(&cstats, 0, sizeof(cstats));
Joe Perchescd228d52007-11-12 18:07:31 -08001004 if (ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
Joe Perchescd228d52007-11-12 18:07:31 -08001006 if (ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
1008 if (copy_to_user(addr, &cstats, sizeof(cstats)))
1009 break;
1010 err = 0;
1011 break;
1012
1013 case SIOCGPPPVER:
1014 vers = PPP_VERSION;
1015 if (copy_to_user(addr, vers, strlen(vers) + 1))
1016 break;
1017 err = 0;
1018 break;
1019
1020 default:
1021 err = -EINVAL;
1022 }
1023
1024 return err;
1025}
1026
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001027static const struct net_device_ops ppp_netdev_ops = {
Stephen Hemminger00829822008-11-20 20:14:53 -08001028 .ndo_start_xmit = ppp_start_xmit,
1029 .ndo_do_ioctl = ppp_net_ioctl,
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001030};
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032static void ppp_setup(struct net_device *dev)
1033{
Stephen Hemminger52256cf2008-11-19 22:22:30 -08001034 dev->netdev_ops = &ppp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 dev->hard_header_len = PPP_HDRLEN;
1036 dev->mtu = PPP_MTU;
1037 dev->addr_len = 0;
1038 dev->tx_queue_len = 3;
1039 dev->type = ARPHRD_PPP;
1040 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08001041 dev->features |= NETIF_F_NETNS_LOCAL;
Eric Dumazet8b2d8502009-05-19 14:24:37 -07001042 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/*
1046 * Transmit-side routines.
1047 */
1048
1049/*
1050 * Called to do any work queued up on the transmit side
1051 * that can now be done.
1052 */
1053static void
1054ppp_xmit_process(struct ppp *ppp)
1055{
1056 struct sk_buff *skb;
1057
1058 ppp_xmit_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001059 if (!ppp->closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 ppp_push(ppp);
Joe Perches8e95a202009-12-03 07:58:21 +00001061 while (!ppp->xmit_pending &&
1062 (skb = skb_dequeue(&ppp->file.xq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ppp_send_frame(ppp, skb);
1064 /* If there's no work left to do, tell the core net
1065 code that we can accept some more. */
Joe Perchescd228d52007-11-12 18:07:31 -08001066 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 netif_wake_queue(ppp->dev);
1068 }
1069 ppp_xmit_unlock(ppp);
1070}
1071
Matt Domschb3f9b922005-11-08 09:40:47 -08001072static inline struct sk_buff *
1073pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
1074{
1075 struct sk_buff *new_skb;
1076 int len;
1077 int new_skb_size = ppp->dev->mtu +
1078 ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
1079 int compressor_skb_size = ppp->dev->mtu +
1080 ppp->xcomp->comp_extra + PPP_HDRLEN;
1081 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
1082 if (!new_skb) {
1083 if (net_ratelimit())
1084 printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1085 return NULL;
1086 }
1087 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1088 skb_reserve(new_skb,
1089 ppp->dev->hard_header_len - PPP_HDRLEN);
1090
1091 /* compressor still expects A/C bytes in hdr */
1092 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1093 new_skb->data, skb->len + 2,
1094 compressor_skb_size);
1095 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1096 kfree_skb(skb);
1097 skb = new_skb;
1098 skb_put(skb, len);
1099 skb_pull(skb, 2); /* pull off A/C bytes */
1100 } else if (len == 0) {
1101 /* didn't compress, or CCP not up yet */
1102 kfree_skb(new_skb);
1103 new_skb = skb;
1104 } else {
1105 /*
1106 * (len < 0)
1107 * MPPE requires that we do not send unencrypted
1108 * frames. The compressor will return -1 if we
1109 * should drop the frame. We cannot simply test
1110 * the compress_proto because MPPE and MPPC share
1111 * the same number.
1112 */
1113 if (net_ratelimit())
1114 printk(KERN_ERR "ppp: compressor dropped pkt\n");
1115 kfree_skb(skb);
1116 kfree_skb(new_skb);
1117 new_skb = NULL;
1118 }
1119 return new_skb;
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/*
1123 * Compress and send a frame.
1124 * The caller should have locked the xmit path,
1125 * and xmit_pending should be 0.
1126 */
1127static void
1128ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
1129{
1130 int proto = PPP_PROTO(skb);
1131 struct sk_buff *new_skb;
1132 int len;
1133 unsigned char *cp;
1134
1135 if (proto < 0x8000) {
1136#ifdef CONFIG_PPP_FILTER
1137 /* check if we should pass this packet */
1138 /* the filter instructions are constructed assuming
1139 a four-byte PPP header on each packet */
1140 *skb_push(skb, 2) = 1;
Joe Perches8e95a202009-12-03 07:58:21 +00001141 if (ppp->pass_filter &&
1142 sk_run_filter(skb, ppp->pass_filter,
1143 ppp->pass_len) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (ppp->debug & 1)
1145 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
1146 kfree_skb(skb);
1147 return;
1148 }
1149 /* if this packet passes the active filter, record the time */
Joe Perches8e95a202009-12-03 07:58:21 +00001150 if (!(ppp->active_filter &&
1151 sk_run_filter(skb, ppp->active_filter,
1152 ppp->active_len) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ppp->last_xmit = jiffies;
1154 skb_pull(skb, 2);
1155#else
1156 /* for data packets, record the time */
1157 ppp->last_xmit = jiffies;
1158#endif /* CONFIG_PPP_FILTER */
1159 }
1160
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001161 ++ppp->dev->stats.tx_packets;
1162 ppp->dev->stats.tx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 switch (proto) {
1165 case PPP_IP:
Joe Perchescd228d52007-11-12 18:07:31 -08001166 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168 /* try to do VJ TCP header compression */
1169 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1170 GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001171 if (!new_skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1173 goto drop;
1174 }
1175 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1176 cp = skb->data + 2;
1177 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1178 new_skb->data + 2, &cp,
1179 !(ppp->flags & SC_NO_TCP_CCID));
1180 if (cp == skb->data + 2) {
1181 /* didn't compress */
1182 kfree_skb(new_skb);
1183 } else {
1184 if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1185 proto = PPP_VJC_COMP;
1186 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1187 } else {
1188 proto = PPP_VJC_UNCOMP;
1189 cp[0] = skb->data[2];
1190 }
1191 kfree_skb(skb);
1192 skb = new_skb;
1193 cp = skb_put(skb, len + 2);
1194 cp[0] = 0;
1195 cp[1] = proto;
1196 }
1197 break;
1198
1199 case PPP_CCP:
1200 /* peek at outbound CCP frames */
1201 ppp_ccp_peek(ppp, skb, 0);
1202 break;
1203 }
1204
1205 /* try to do packet compression */
Joe Perches8e95a202009-12-03 07:58:21 +00001206 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1207 proto != PPP_LCP && proto != PPP_CCP) {
Matt Domschb3f9b922005-11-08 09:40:47 -08001208 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1209 if (net_ratelimit())
1210 printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto drop;
1212 }
Matt Domschb3f9b922005-11-08 09:40:47 -08001213 skb = pad_compress_skb(ppp, skb);
1214 if (!skb)
1215 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 /*
1219 * If we are waiting for traffic (demand dialling),
1220 * queue it up for pppd to receive.
1221 */
1222 if (ppp->flags & SC_LOOP_TRAFFIC) {
1223 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1224 goto drop;
1225 skb_queue_tail(&ppp->file.rq, skb);
1226 wake_up_interruptible(&ppp->file.rwait);
1227 return;
1228 }
1229
1230 ppp->xmit_pending = skb;
1231 ppp_push(ppp);
1232 return;
1233
1234 drop:
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00001235 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001236 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239/*
1240 * Try to send the frame in xmit_pending.
1241 * The caller should have the xmit path locked.
1242 */
1243static void
1244ppp_push(struct ppp *ppp)
1245{
1246 struct list_head *list;
1247 struct channel *pch;
1248 struct sk_buff *skb = ppp->xmit_pending;
1249
Joe Perchescd228d52007-11-12 18:07:31 -08001250 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return;
1252
1253 list = &ppp->channels;
1254 if (list_empty(list)) {
1255 /* nowhere to send the packet, just drop it */
1256 ppp->xmit_pending = NULL;
1257 kfree_skb(skb);
1258 return;
1259 }
1260
1261 if ((ppp->flags & SC_MULTILINK) == 0) {
1262 /* not doing multilink: send it down the first channel */
1263 list = list->next;
1264 pch = list_entry(list, struct channel, clist);
1265
1266 spin_lock_bh(&pch->downl);
1267 if (pch->chan) {
1268 if (pch->chan->ops->start_xmit(pch->chan, skb))
1269 ppp->xmit_pending = NULL;
1270 } else {
1271 /* channel got unregistered */
1272 kfree_skb(skb);
1273 ppp->xmit_pending = NULL;
1274 }
1275 spin_unlock_bh(&pch->downl);
1276 return;
1277 }
1278
1279#ifdef CONFIG_PPP_MULTILINK
1280 /* Multilink: fragment the packet over as many links
1281 as can take the packet at the moment. */
1282 if (!ppp_mp_explode(ppp, skb))
1283 return;
1284#endif /* CONFIG_PPP_MULTILINK */
1285
1286 ppp->xmit_pending = NULL;
1287 kfree_skb(skb);
1288}
1289
1290#ifdef CONFIG_PPP_MULTILINK
1291/*
1292 * Divide a packet to be transmitted into fragments and
1293 * send them out the individual links.
1294 */
1295static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1296{
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001297 int len, totlen;
1298 int i, bits, hdrlen, mtu;
1299 int flen;
1300 int navail, nfree, nzero;
1301 int nbigger;
1302 int totspeed;
1303 int totfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 unsigned char *p, *q;
1305 struct list_head *list;
1306 struct channel *pch;
1307 struct sk_buff *frag;
1308 struct ppp_channel *chan;
1309
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001310 totspeed = 0; /*total bitrate of the bundle*/
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001311 nfree = 0; /* # channels which have no packet already queued */
1312 navail = 0; /* total # of usable channels (not deregistered) */
1313 nzero = 0; /* number of channels with zero speed associated*/
1314 totfree = 0; /*total # of channels available and
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001315 *having no queued packets before
1316 *starting the fragmentation*/
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001319 i = 0;
1320 list_for_each_entry(pch, &ppp->channels, clist) {
Paul Mackerras516cd152005-05-12 19:47:12 -04001321 navail += pch->avail = (pch->chan != NULL);
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001322 pch->speed = pch->chan->speed;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001323 if (pch->avail) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001324 if (skb_queue_empty(&pch->file.xq) ||
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001325 !pch->had_frag) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001326 if (pch->speed == 0)
1327 nzero++;
1328 else
1329 totspeed += pch->speed;
1330
1331 pch->avail = 2;
1332 ++nfree;
1333 ++totfree;
1334 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001335 if (!pch->had_frag && i < ppp->nxchan)
1336 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001338 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001340 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001341 * Don't start sending this packet unless at least half of
1342 * the channels are free. This gives much better TCP
1343 * performance if we have a lot of channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001344 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001345 if (nfree == 0 || nfree < navail / 2)
1346 return 0; /* can't take now, leave it in xmit_pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /* Do protocol field compression (XXX this should be optional) */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001349 p = skb->data;
1350 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (*p == 0) {
1352 ++p;
1353 --len;
1354 }
1355
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001356 totlen = len;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001357 nbigger = len % nfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001359 /* skip to the channel after the one we last used
1360 and start at that one */
Domen Puncer81616c52005-09-10 00:27:04 -07001361 list = &ppp->channels;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001362 for (i = 0; i < ppp->nxchan; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001364 if (list == &ppp->channels) {
1365 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 break;
1367 }
1368 }
1369
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001370 /* create a fragment for each channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 bits = B;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001372 while (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 list = list->next;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001374 if (list == &ppp->channels) {
1375 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 continue;
1377 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001378 pch = list_entry(list, struct channel, clist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 ++i;
1380 if (!pch->avail)
1381 continue;
1382
Paul Mackerras516cd152005-05-12 19:47:12 -04001383 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001384 * Skip this channel if it has a fragment pending already and
1385 * we haven't given a fragment to all of the free channels.
Paul Mackerras516cd152005-05-12 19:47:12 -04001386 */
1387 if (pch->avail == 1) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001388 if (nfree > 0)
Paul Mackerras516cd152005-05-12 19:47:12 -04001389 continue;
1390 } else {
Paul Mackerras516cd152005-05-12 19:47:12 -04001391 pch->avail = 1;
1392 }
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* check the channel's mtu and whether it is still attached. */
1395 spin_lock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001396 if (pch->chan == NULL) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001397 /* can't use this channel, it's being deregistered */
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001398 if (pch->speed == 0)
1399 nzero--;
1400 else
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001401 totspeed -= pch->speed;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 spin_unlock_bh(&pch->downl);
1404 pch->avail = 0;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001405 totlen = len;
1406 totfree--;
1407 nfree--;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001408 if (--navail == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 continue;
1411 }
1412
1413 /*
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001414 *if the channel speed is not set divide
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001415 *the packet evenly among the free channels;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001416 *otherwise divide it according to the speed
1417 *of the channel we are going to transmit on
1418 */
David S. Miller886f9fe2009-08-19 13:55:55 -07001419 flen = len;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001420 if (nfree > 0) {
1421 if (pch->speed == 0) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001422 flen = totlen/nfree;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001423 if (nbigger > 0) {
1424 flen++;
1425 nbigger--;
1426 }
1427 } else {
1428 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1429 ((totspeed*totfree)/pch->speed)) - hdrlen;
1430 if (nbigger > 0) {
1431 flen += ((totfree - nzero)*pch->speed)/totspeed;
1432 nbigger -= ((totfree - nzero)*pch->speed)/
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001433 totspeed;
Ben McKeegana53a8b52009-07-28 07:43:57 +00001434 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001435 }
Ben McKeegana53a8b52009-07-28 07:43:57 +00001436 nfree--;
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001437 }
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001438
1439 /*
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001440 *check if we are on the last channel or
1441 *we exceded the lenght of the data to
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001442 *fragment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Ben McKeegana53a8b52009-07-28 07:43:57 +00001444 if ((nfree <= 0) || (flen > len))
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001445 flen = len;
1446 /*
1447 *it is not worth to tx on slow channels:
1448 *in that case from the resulting flen according to the
1449 *above formula will be equal or less than zero.
1450 *Skip the channel in this case
1451 */
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001452 if (flen <= 0) {
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001453 pch->avail = 2;
1454 spin_unlock_bh(&pch->downl);
1455 continue;
1456 }
1457
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001458 mtu = pch->chan->mtu - hdrlen;
1459 if (mtu < 4)
1460 mtu = 4;
Paul Mackerras516cd152005-05-12 19:47:12 -04001461 if (flen > mtu)
1462 flen = mtu;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001463 if (flen == len)
1464 bits |= E;
1465 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001466 if (!frag)
Paul Mackerras516cd152005-05-12 19:47:12 -04001467 goto noskb;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001468 q = skb_put(frag, flen + hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001470 /* make the MP header */
Paul Mackerras516cd152005-05-12 19:47:12 -04001471 q[0] = PPP_MP >> 8;
1472 q[1] = PPP_MP;
1473 if (ppp->flags & SC_MP_XSHORTSEQ) {
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001474 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
Paul Mackerras516cd152005-05-12 19:47:12 -04001475 q[3] = ppp->nxseq;
1476 } else {
1477 q[2] = bits;
1478 q[3] = ppp->nxseq >> 16;
1479 q[4] = ppp->nxseq >> 8;
1480 q[5] = ppp->nxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001482
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001483 memcpy(q + hdrlen, p, flen);
Paul Mackerras516cd152005-05-12 19:47:12 -04001484
1485 /* try to send it down the channel */
1486 chan = pch->chan;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001487 if (!skb_queue_empty(&pch->file.xq) ||
Gabriele Paoloni9c705262009-03-13 16:09:12 -07001488 !chan->ops->start_xmit(chan, frag))
Paul Mackerras516cd152005-05-12 19:47:12 -04001489 skb_queue_tail(&pch->file.xq, frag);
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001490 pch->had_frag = 1;
Paul Mackerras516cd152005-05-12 19:47:12 -04001491 p += flen;
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001492 len -= flen;
Paul Mackerras516cd152005-05-12 19:47:12 -04001493 ++ppp->nxseq;
1494 bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 spin_unlock_bh(&pch->downl);
Paul Mackerras516cd152005-05-12 19:47:12 -04001496 }
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001497 ppp->nxchan = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 return 1;
1500
1501 noskb:
1502 spin_unlock_bh(&pch->downl);
1503 if (ppp->debug & 1)
Lennart Sorensenfa44a732010-01-18 12:59:55 +00001504 printk(KERN_ERR "PPP: no memory (fragment)\n");
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001505 ++ppp->dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 ++ppp->nxseq;
1507 return 1; /* abandon the frame */
1508}
1509#endif /* CONFIG_PPP_MULTILINK */
1510
1511/*
1512 * Try to send data out on a channel.
1513 */
1514static void
1515ppp_channel_push(struct channel *pch)
1516{
1517 struct sk_buff *skb;
1518 struct ppp *ppp;
1519
1520 spin_lock_bh(&pch->downl);
Joe Perchescd228d52007-11-12 18:07:31 -08001521 if (pch->chan) {
David S. Millerb03efcf2005-07-08 14:57:23 -07001522 while (!skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 skb = skb_dequeue(&pch->file.xq);
1524 if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1525 /* put the packet back and try again later */
1526 skb_queue_head(&pch->file.xq, skb);
1527 break;
1528 }
1529 }
1530 } else {
1531 /* channel got deregistered */
1532 skb_queue_purge(&pch->file.xq);
1533 }
1534 spin_unlock_bh(&pch->downl);
1535 /* see if there is anything from the attached unit to be sent */
David S. Millerb03efcf2005-07-08 14:57:23 -07001536 if (skb_queue_empty(&pch->file.xq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 read_lock_bh(&pch->upl);
1538 ppp = pch->ppp;
Joe Perchescd228d52007-11-12 18:07:31 -08001539 if (ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 ppp_xmit_process(ppp);
1541 read_unlock_bh(&pch->upl);
1542 }
1543}
1544
1545/*
1546 * Receive-side routines.
1547 */
1548
1549/* misuse a few fields of the skb for MP reconstruction */
1550#define sequence priority
1551#define BEbits cb[0]
1552
1553static inline void
1554ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1555{
1556 ppp_recv_lock(ppp);
James Chapman739840d2008-12-17 12:02:16 +00001557 if (!ppp->closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ppp_receive_frame(ppp, skb, pch);
1559 else
1560 kfree_skb(skb);
1561 ppp_recv_unlock(ppp);
1562}
1563
1564void
1565ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1566{
1567 struct channel *pch = chan->ppp;
1568 int proto;
1569
Simon Arlottea8420e2010-05-03 10:19:33 +00001570 if (!pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 kfree_skb(skb);
1572 return;
1573 }
Paul Mackerras516cd152005-05-12 19:47:12 -04001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 read_lock_bh(&pch->upl);
Simon Arlottea8420e2010-05-03 10:19:33 +00001576 if (!pskb_may_pull(skb, 2)) {
1577 kfree_skb(skb);
1578 if (pch->ppp) {
1579 ++pch->ppp->dev->stats.rx_length_errors;
1580 ppp_receive_error(pch->ppp);
1581 }
1582 goto done;
1583 }
1584
1585 proto = PPP_PROTO(skb);
Joe Perchescd228d52007-11-12 18:07:31 -08001586 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* put it on the channel queue */
1588 skb_queue_tail(&pch->file.rq, skb);
1589 /* drop old frames if queue too long */
Joe Perches8e95a202009-12-03 07:58:21 +00001590 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
1591 (skb = skb_dequeue(&pch->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 kfree_skb(skb);
1593 wake_up_interruptible(&pch->file.rwait);
1594 } else {
1595 ppp_do_recv(pch->ppp, skb, pch);
1596 }
Simon Arlottea8420e2010-05-03 10:19:33 +00001597
1598done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 read_unlock_bh(&pch->upl);
1600}
1601
1602/* Put a 0-length skb in the receive queue as an error indication */
1603void
1604ppp_input_error(struct ppp_channel *chan, int code)
1605{
1606 struct channel *pch = chan->ppp;
1607 struct sk_buff *skb;
1608
Joe Perchescd228d52007-11-12 18:07:31 -08001609 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return;
1611
1612 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08001613 if (pch->ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 skb = alloc_skb(0, GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08001615 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 skb->len = 0; /* probably unnecessary */
1617 skb->cb[0] = code;
1618 ppp_do_recv(pch->ppp, skb, pch);
1619 }
1620 }
1621 read_unlock_bh(&pch->upl);
1622}
1623
1624/*
1625 * We come in here to process a received frame.
1626 * The receive side of the ppp unit is locked.
1627 */
1628static void
1629ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1630{
Simon Arlottea8420e2010-05-03 10:19:33 +00001631 /* note: a 0-length skb is used as an error indication */
1632 if (skb->len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633#ifdef CONFIG_PPP_MULTILINK
1634 /* XXX do channel-level decompression here */
1635 if (PPP_PROTO(skb) == PPP_MP)
1636 ppp_receive_mp_frame(ppp, skb, pch);
1637 else
1638#endif /* CONFIG_PPP_MULTILINK */
1639 ppp_receive_nonmp_frame(ppp, skb);
Simon Arlottea8420e2010-05-03 10:19:33 +00001640 } else {
1641 kfree_skb(skb);
1642 ppp_receive_error(ppp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
1646static void
1647ppp_receive_error(struct ppp *ppp)
1648{
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001649 ++ppp->dev->stats.rx_errors;
Joe Perchescd228d52007-11-12 18:07:31 -08001650 if (ppp->vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 slhc_toss(ppp->vj);
1652}
1653
1654static void
1655ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1656{
1657 struct sk_buff *ns;
1658 int proto, len, npi;
1659
1660 /*
1661 * Decompress the frame, if compressed.
1662 * Note that some decompressors need to see uncompressed frames
1663 * that come in as well as compressed frames.
1664 */
Joe Perches8e95a202009-12-03 07:58:21 +00001665 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
1666 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 skb = ppp_decompress_frame(ppp, skb);
1668
Matt Domschb3f9b922005-11-08 09:40:47 -08001669 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
1670 goto err;
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 proto = PPP_PROTO(skb);
1673 switch (proto) {
1674 case PPP_VJC_COMP:
1675 /* decompress VJ compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08001676 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto err;
1678
Herbert Xu2a38b772007-09-16 16:22:13 -07001679 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* copy to a new sk_buff with more tailroom */
1681 ns = dev_alloc_skb(skb->len + 128);
Joe Perchescd228d52007-11-12 18:07:31 -08001682 if (!ns) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1684 goto err;
1685 }
1686 skb_reserve(ns, 2);
1687 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
1688 kfree_skb(skb);
1689 skb = ns;
1690 }
Herbert Xue3f749c2006-02-05 20:23:33 -08001691 else
1692 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1695 if (len <= 0) {
1696 printk(KERN_DEBUG "PPP: VJ decompression error\n");
1697 goto err;
1698 }
1699 len += 2;
1700 if (len > skb->len)
1701 skb_put(skb, len - skb->len);
1702 else if (len < skb->len)
1703 skb_trim(skb, len);
1704 proto = PPP_IP;
1705 break;
1706
1707 case PPP_VJC_UNCOMP:
Joe Perchescd228d52007-11-12 18:07:31 -08001708 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 goto err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* Until we fix the decompressor need to make sure
1712 * data portion is linear.
1713 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001714 if (!pskb_may_pull(skb, skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 goto err;
1716
1717 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1718 printk(KERN_ERR "PPP: VJ uncompressed error\n");
1719 goto err;
1720 }
1721 proto = PPP_IP;
1722 break;
1723
1724 case PPP_CCP:
1725 ppp_ccp_peek(ppp, skb, 1);
1726 break;
1727 }
1728
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001729 ++ppp->dev->stats.rx_packets;
1730 ppp->dev->stats.rx_bytes += skb->len - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 npi = proto_to_npindex(proto);
1733 if (npi < 0) {
1734 /* control or unknown frame - pass it to pppd */
1735 skb_queue_tail(&ppp->file.rq, skb);
1736 /* limit queue length by dropping old frames */
Joe Perches8e95a202009-12-03 07:58:21 +00001737 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
1738 (skb = skb_dequeue(&ppp->file.rq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 kfree_skb(skb);
1740 /* wake up any process polling or blocking on read */
1741 wake_up_interruptible(&ppp->file.rwait);
1742
1743 } else {
1744 /* network protocol frame - give it to the kernel */
1745
1746#ifdef CONFIG_PPP_FILTER
1747 /* check if the packet passes the pass and active filters */
1748 /* the filter instructions are constructed assuming
1749 a four-byte PPP header on each packet */
Herbert Xu2a38b772007-09-16 16:22:13 -07001750 if (ppp->pass_filter || ppp->active_filter) {
1751 if (skb_cloned(skb) &&
1752 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1753 goto err;
1754
1755 *skb_push(skb, 2) = 0;
Joe Perches8e95a202009-12-03 07:58:21 +00001756 if (ppp->pass_filter &&
1757 sk_run_filter(skb, ppp->pass_filter,
1758 ppp->pass_len) == 0) {
Herbert Xu2a38b772007-09-16 16:22:13 -07001759 if (ppp->debug & 1)
1760 printk(KERN_DEBUG "PPP: inbound frame "
1761 "not passed\n");
1762 kfree_skb(skb);
1763 return;
1764 }
Joe Perches8e95a202009-12-03 07:58:21 +00001765 if (!(ppp->active_filter &&
1766 sk_run_filter(skb, ppp->active_filter,
1767 ppp->active_len) == 0))
Herbert Xu2a38b772007-09-16 16:22:13 -07001768 ppp->last_recv = jiffies;
1769 __skb_pull(skb, 2);
1770 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771#endif /* CONFIG_PPP_FILTER */
Herbert Xu2a38b772007-09-16 16:22:13 -07001772 ppp->last_recv = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Joe Perches8e95a202009-12-03 07:58:21 +00001774 if ((ppp->dev->flags & IFF_UP) == 0 ||
1775 ppp->npmode[npi] != NPMODE_PASS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 kfree_skb(skb);
1777 } else {
Herbert Xucbb042f2006-03-20 22:43:56 -08001778 /* chop off protocol */
1779 skb_pull_rcsum(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 skb->dev = ppp->dev;
1781 skb->protocol = htons(npindex_to_ethertype[npi]);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001782 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 }
1786 return;
1787
1788 err:
1789 kfree_skb(skb);
1790 ppp_receive_error(ppp);
1791}
1792
1793static struct sk_buff *
1794ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1795{
1796 int proto = PPP_PROTO(skb);
1797 struct sk_buff *ns;
1798 int len;
1799
1800 /* Until we fix all the decompressor's need to make sure
1801 * data portion is linear.
1802 */
1803 if (!pskb_may_pull(skb, skb->len))
1804 goto err;
1805
1806 if (proto == PPP_COMP) {
Konstantin Sharlaimov4b2a8fb2007-06-23 23:05:54 -07001807 int obuff_size;
1808
1809 switch(ppp->rcomp->compress_proto) {
1810 case CI_MPPE:
1811 obuff_size = ppp->mru + PPP_HDRLEN + 1;
1812 break;
1813 default:
1814 obuff_size = ppp->mru + PPP_HDRLEN;
1815 break;
1816 }
1817
1818 ns = dev_alloc_skb(obuff_size);
Joe Perchescd228d52007-11-12 18:07:31 -08001819 if (!ns) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1821 goto err;
1822 }
1823 /* the decompressor still expects the A/C bytes in the hdr */
1824 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
Konstantin Sharlaimov06c7af52007-08-21 00:12:44 -07001825 skb->len + 2, ns->data, obuff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (len < 0) {
1827 /* Pass the compressed frame to pppd as an
1828 error indication. */
1829 if (len == DECOMP_FATALERROR)
1830 ppp->rstate |= SC_DC_FERROR;
1831 kfree_skb(ns);
1832 goto err;
1833 }
1834
1835 kfree_skb(skb);
1836 skb = ns;
1837 skb_put(skb, len);
1838 skb_pull(skb, 2); /* pull off the A/C bytes */
1839
1840 } else {
1841 /* Uncompressed frame - pass to decompressor so it
1842 can update its dictionary if necessary. */
1843 if (ppp->rcomp->incomp)
1844 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1845 skb->len + 2);
1846 }
1847
1848 return skb;
1849
1850 err:
1851 ppp->rstate |= SC_DC_ERROR;
1852 ppp_receive_error(ppp);
1853 return skb;
1854}
1855
1856#ifdef CONFIG_PPP_MULTILINK
1857/*
1858 * Receive a multilink frame.
1859 * We put it on the reconstruction queue and then pull off
1860 * as many completed frames as we can.
1861 */
1862static void
1863ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1864{
1865 u32 mask, seq;
Domen Puncer81616c52005-09-10 00:27:04 -07001866 struct channel *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1868
Herbert Xu2a38b772007-09-16 16:22:13 -07001869 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto err; /* no good, throw it away */
1871
1872 /* Decode sequence number and begin/end bits */
1873 if (ppp->flags & SC_MP_SHORTSEQ) {
1874 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1875 mask = 0xfff;
1876 } else {
1877 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1878 mask = 0xffffff;
1879 }
1880 skb->BEbits = skb->data[2];
1881 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
1882
1883 /*
1884 * Do protocol ID decompression on the first fragment of each packet.
1885 */
1886 if ((skb->BEbits & B) && (skb->data[0] & 1))
1887 *skb_push(skb, 1) = 0;
1888
1889 /*
1890 * Expand sequence number to 32 bits, making it as close
1891 * as possible to ppp->minseq.
1892 */
1893 seq |= ppp->minseq & ~mask;
1894 if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1895 seq += mask + 1;
1896 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1897 seq -= mask + 1; /* should never happen */
1898 skb->sequence = seq;
1899 pch->lastseq = seq;
1900
1901 /*
1902 * If this packet comes before the next one we were expecting,
1903 * drop it.
1904 */
1905 if (seq_before(seq, ppp->nextseq)) {
1906 kfree_skb(skb);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07001907 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ppp_receive_error(ppp);
1909 return;
1910 }
1911
1912 /*
1913 * Reevaluate minseq, the minimum over all channels of the
1914 * last sequence number received on each channel. Because of
1915 * the increasing sequence number rule, we know that any fragment
1916 * before `minseq' which hasn't arrived is never going to arrive.
1917 * The list of channels can't change because we have the receive
1918 * side of the ppp unit locked.
1919 */
Domen Puncer81616c52005-09-10 00:27:04 -07001920 list_for_each_entry(ch, &ppp->channels, clist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (seq_before(ch->lastseq, seq))
1922 seq = ch->lastseq;
1923 }
1924 if (seq_before(ppp->minseq, seq))
1925 ppp->minseq = seq;
1926
1927 /* Put the fragment on the reconstruction queue */
1928 ppp_mp_insert(ppp, skb);
1929
1930 /* If the queue is getting long, don't wait any longer for packets
1931 before the start of the queue. */
David S. Miller38ce7c72008-09-23 01:17:18 -07001932 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
1933 struct sk_buff *skb = skb_peek(&ppp->mrq);
1934 if (seq_before(ppp->minseq, skb->sequence))
1935 ppp->minseq = skb->sequence;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* Pull completed packets off the queue and receive them. */
Ben McKeegan82b3cc12009-11-16 03:44:25 +00001939 while ((skb = ppp_mp_reconstruct(ppp))) {
1940 if (pskb_may_pull(skb, 2))
1941 ppp_receive_nonmp_frame(ppp, skb);
1942 else {
1943 ++ppp->dev->stats.rx_length_errors;
1944 kfree_skb(skb);
1945 ppp_receive_error(ppp);
1946 }
1947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 return;
1950
1951 err:
1952 kfree_skb(skb);
1953 ppp_receive_error(ppp);
1954}
1955
1956/*
1957 * Insert a fragment on the MP reconstruction queue.
1958 * The queue is ordered by increasing sequence number.
1959 */
1960static void
1961ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1962{
1963 struct sk_buff *p;
1964 struct sk_buff_head *list = &ppp->mrq;
1965 u32 seq = skb->sequence;
1966
1967 /* N.B. we don't need to lock the list lock because we have the
1968 ppp unit receive-side lock. */
David S. Miller13c98212008-10-09 16:40:29 -07001969 skb_queue_walk(list, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (seq_before(seq, p->sequence))
1971 break;
David S. Miller13c98212008-10-09 16:40:29 -07001972 }
David S. Miller43f59c82008-09-21 21:28:51 -07001973 __skb_queue_before(list, p, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
1976/*
1977 * Reconstruct a packet from the MP fragment queue.
1978 * We go through increasing sequence numbers until we find a
1979 * complete packet, or we get to the sequence number for a fragment
1980 * which hasn't arrived but might still do so.
1981 */
Stephen Hemminger3c582b32008-01-23 20:54:07 -08001982static struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983ppp_mp_reconstruct(struct ppp *ppp)
1984{
1985 u32 seq = ppp->nextseq;
1986 u32 minseq = ppp->minseq;
1987 struct sk_buff_head *list = &ppp->mrq;
1988 struct sk_buff *p, *next;
1989 struct sk_buff *head, *tail;
1990 struct sk_buff *skb = NULL;
1991 int lost = 0, len = 0;
1992
1993 if (ppp->mrru == 0) /* do nothing until mrru is set */
1994 return NULL;
1995 head = list->next;
1996 tail = NULL;
1997 for (p = head; p != (struct sk_buff *) list; p = next) {
1998 next = p->next;
1999 if (seq_before(p->sequence, seq)) {
2000 /* this can't happen, anyway ignore the skb */
2001 printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
2002 p->sequence, seq);
2003 head = next;
2004 continue;
2005 }
2006 if (p->sequence != seq) {
2007 /* Fragment `seq' is missing. If it is after
2008 minseq, it might arrive later, so stop here. */
2009 if (seq_after(seq, minseq))
2010 break;
2011 /* Fragment `seq' is lost, keep going. */
2012 lost = 1;
2013 seq = seq_before(minseq, p->sequence)?
2014 minseq + 1: p->sequence;
2015 next = p;
2016 continue;
2017 }
2018
2019 /*
2020 * At this point we know that all the fragments from
2021 * ppp->nextseq to seq are either present or lost.
2022 * Also, there are no complete packets in the queue
2023 * that have no missing fragments and end before this
2024 * fragment.
2025 */
2026
2027 /* B bit set indicates this fragment starts a packet */
2028 if (p->BEbits & B) {
2029 head = p;
2030 lost = 0;
2031 len = 0;
2032 }
2033
2034 len += p->len;
2035
2036 /* Got a complete packet yet? */
2037 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
2038 if (len > ppp->mrru + 2) {
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002039 ++ppp->dev->stats.rx_length_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 printk(KERN_DEBUG "PPP: reconstructed packet"
2041 " is too long (%d)\n", len);
2042 } else if (p == head) {
2043 /* fragment is complete packet - reuse skb */
2044 tail = p;
2045 skb = skb_get(p);
2046 break;
2047 } else if ((skb = dev_alloc_skb(len)) == NULL) {
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002048 ++ppp->dev->stats.rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 printk(KERN_DEBUG "PPP: no memory for "
2050 "reconstructed packet");
2051 } else {
2052 tail = p;
2053 break;
2054 }
2055 ppp->nextseq = seq + 1;
2056 }
2057
2058 /*
2059 * If this is the ending fragment of a packet,
2060 * and we haven't found a complete valid packet yet,
2061 * we can discard up to and including this fragment.
2062 */
2063 if (p->BEbits & E)
2064 head = next;
2065
2066 ++seq;
2067 }
2068
2069 /* If we have a complete packet, copy it all into one skb. */
2070 if (tail != NULL) {
2071 /* If we have discarded any fragments,
2072 signal a receive error. */
2073 if (head->sequence != ppp->nextseq) {
2074 if (ppp->debug & 1)
2075 printk(KERN_DEBUG " missed pkts %u..%u\n",
2076 ppp->nextseq, head->sequence-1);
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002077 ++ppp->dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 ppp_receive_error(ppp);
2079 }
2080
2081 if (head != tail)
2082 /* copy to a single skb */
2083 for (p = head; p != tail->next; p = p->next)
2084 skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
2085 ppp->nextseq = tail->sequence + 1;
2086 head = tail->next;
2087 }
2088
2089 /* Discard all the skbuffs that we have copied the data out of
2090 or that we can't use. */
2091 while ((p = list->next) != head) {
2092 __skb_unlink(p, list);
2093 kfree_skb(p);
2094 }
2095
2096 return skb;
2097}
2098#endif /* CONFIG_PPP_MULTILINK */
2099
2100/*
2101 * Channel interface.
2102 */
2103
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002104/* Create a new, unattached ppp channel. */
2105int ppp_register_channel(struct ppp_channel *chan)
2106{
2107 return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2108}
2109
2110/* Create a new, unattached ppp channel for specified net. */
2111int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct channel *pch;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002114 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Panagiotis Issarisd4274b52006-08-15 16:01:07 -07002116 pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
Joe Perchescd228d52007-11-12 18:07:31 -08002117 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return -ENOMEM;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002119
2120 pn = ppp_pernet(net);
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 pch->ppp = NULL;
2123 pch->chan = chan;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002124 pch->chan_net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 chan->ppp = pch;
2126 init_ppp_file(&pch->file, CHANNEL);
2127 pch->file.hdrlen = chan->hdrlen;
2128#ifdef CONFIG_PPP_MULTILINK
2129 pch->lastseq = -1;
2130#endif /* CONFIG_PPP_MULTILINK */
2131 init_rwsem(&pch->chan_sem);
2132 spin_lock_init(&pch->downl);
2133 rwlock_init(&pch->upl);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002134
2135 spin_lock_bh(&pn->all_channels_lock);
2136 pch->file.index = ++pn->last_channel_index;
2137 list_add(&pch->list, &pn->new_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 atomic_inc(&channel_count);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002139 spin_unlock_bh(&pn->all_channels_lock);
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 0;
2142}
2143
2144/*
2145 * Return the index of a channel.
2146 */
2147int ppp_channel_index(struct ppp_channel *chan)
2148{
2149 struct channel *pch = chan->ppp;
2150
Joe Perchescd228d52007-11-12 18:07:31 -08002151 if (pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return pch->file.index;
2153 return -1;
2154}
2155
2156/*
2157 * Return the PPP unit number to which a channel is connected.
2158 */
2159int ppp_unit_number(struct ppp_channel *chan)
2160{
2161 struct channel *pch = chan->ppp;
2162 int unit = -1;
2163
Joe Perchescd228d52007-11-12 18:07:31 -08002164 if (pch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 read_lock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002166 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 unit = pch->ppp->file.index;
2168 read_unlock_bh(&pch->upl);
2169 }
2170 return unit;
2171}
2172
2173/*
2174 * Disconnect a channel from the generic layer.
2175 * This must be called in process context.
2176 */
2177void
2178ppp_unregister_channel(struct ppp_channel *chan)
2179{
2180 struct channel *pch = chan->ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002181 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Joe Perchescd228d52007-11-12 18:07:31 -08002183 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return; /* should never happen */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 chan->ppp = NULL;
2187
2188 /*
2189 * This ensures that we have returned from any calls into the
2190 * the channel's start_xmit or ioctl routine before we proceed.
2191 */
2192 down_write(&pch->chan_sem);
2193 spin_lock_bh(&pch->downl);
2194 pch->chan = NULL;
2195 spin_unlock_bh(&pch->downl);
2196 up_write(&pch->chan_sem);
2197 ppp_disconnect_channel(pch);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002198
2199 pn = ppp_pernet(pch->chan_net);
2200 spin_lock_bh(&pn->all_channels_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 list_del(&pch->list);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002202 spin_unlock_bh(&pn->all_channels_lock);
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 pch->file.dead = 1;
2205 wake_up_interruptible(&pch->file.rwait);
2206 if (atomic_dec_and_test(&pch->file.refcnt))
2207 ppp_destroy_channel(pch);
2208}
2209
2210/*
2211 * Callback from a channel when it can accept more to transmit.
2212 * This should be called at BH/softirq level, not interrupt level.
2213 */
2214void
2215ppp_output_wakeup(struct ppp_channel *chan)
2216{
2217 struct channel *pch = chan->ppp;
2218
Joe Perchescd228d52007-11-12 18:07:31 -08002219 if (!pch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return;
2221 ppp_channel_push(pch);
2222}
2223
2224/*
2225 * Compression control.
2226 */
2227
2228/* Process the PPPIOCSCOMPRESS ioctl. */
2229static int
2230ppp_set_compress(struct ppp *ppp, unsigned long arg)
2231{
2232 int err;
2233 struct compressor *cp, *ocomp;
2234 struct ppp_option_data data;
2235 void *state, *ostate;
2236 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2237
2238 err = -EFAULT;
Joe Perches8e95a202009-12-03 07:58:21 +00002239 if (copy_from_user(&data, (void __user *) arg, sizeof(data)) ||
2240 (data.length <= CCP_MAX_OPTION_LENGTH &&
2241 copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 goto out;
2243 err = -EINVAL;
Joe Perches8e95a202009-12-03 07:58:21 +00002244 if (data.length > CCP_MAX_OPTION_LENGTH ||
2245 ccp_option[1] < 2 || ccp_option[1] > data.length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 goto out;
2247
Johannes Berga65e5d72008-07-09 10:28:38 +02002248 cp = try_then_request_module(
2249 find_compressor(ccp_option[0]),
2250 "ppp-compress-%d", ccp_option[0]);
Joe Perchescd228d52007-11-12 18:07:31 -08002251 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 goto out;
2253
2254 err = -ENOBUFS;
2255 if (data.transmit) {
2256 state = cp->comp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002257 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 ppp_xmit_lock(ppp);
2259 ppp->xstate &= ~SC_COMP_RUN;
2260 ocomp = ppp->xcomp;
2261 ostate = ppp->xc_state;
2262 ppp->xcomp = cp;
2263 ppp->xc_state = state;
2264 ppp_xmit_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002265 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 ocomp->comp_free(ostate);
2267 module_put(ocomp->owner);
2268 }
2269 err = 0;
2270 } else
2271 module_put(cp->owner);
2272
2273 } else {
2274 state = cp->decomp_alloc(ccp_option, data.length);
Joe Perchescd228d52007-11-12 18:07:31 -08002275 if (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 ppp_recv_lock(ppp);
2277 ppp->rstate &= ~SC_DECOMP_RUN;
2278 ocomp = ppp->rcomp;
2279 ostate = ppp->rc_state;
2280 ppp->rcomp = cp;
2281 ppp->rc_state = state;
2282 ppp_recv_unlock(ppp);
Joe Perchescd228d52007-11-12 18:07:31 -08002283 if (ostate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 ocomp->decomp_free(ostate);
2285 module_put(ocomp->owner);
2286 }
2287 err = 0;
2288 } else
2289 module_put(cp->owner);
2290 }
2291
2292 out:
2293 return err;
2294}
2295
2296/*
2297 * Look at a CCP packet and update our state accordingly.
2298 * We assume the caller has the xmit or recv path locked.
2299 */
2300static void
2301ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2302{
2303 unsigned char *dp;
2304 int len;
2305
2306 if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2307 return; /* no header */
2308 dp = skb->data + 2;
2309
2310 switch (CCP_CODE(dp)) {
2311 case CCP_CONFREQ:
2312
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002313 /* A ConfReq starts negotiation of compression
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 * in one direction of transmission,
2315 * and hence brings it down...but which way?
2316 *
2317 * Remember:
2318 * A ConfReq indicates what the sender would like to receive
2319 */
2320 if(inbound)
2321 /* He is proposing what I should send */
2322 ppp->xstate &= ~SC_COMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002323 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* I am proposing to what he should send */
2325 ppp->rstate &= ~SC_DECOMP_RUN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 case CCP_TERMREQ:
2330 case CCP_TERMACK:
2331 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002332 * CCP is going down, both directions of transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 */
2334 ppp->rstate &= ~SC_DECOMP_RUN;
2335 ppp->xstate &= ~SC_COMP_RUN;
2336 break;
2337
2338 case CCP_CONFACK:
2339 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2340 break;
2341 len = CCP_LENGTH(dp);
2342 if (!pskb_may_pull(skb, len + 2))
2343 return; /* too short */
2344 dp += CCP_HDRLEN;
2345 len -= CCP_HDRLEN;
2346 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2347 break;
2348 if (inbound) {
2349 /* we will start receiving compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002350 if (!ppp->rc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 break;
2352 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2353 ppp->file.index, 0, ppp->mru, ppp->debug)) {
2354 ppp->rstate |= SC_DECOMP_RUN;
2355 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2356 }
2357 } else {
2358 /* we will soon start sending compressed packets */
Joe Perchescd228d52007-11-12 18:07:31 -08002359 if (!ppp->xc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 break;
2361 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2362 ppp->file.index, 0, ppp->debug))
2363 ppp->xstate |= SC_COMP_RUN;
2364 }
2365 break;
2366
2367 case CCP_RESETACK:
2368 /* reset the [de]compressor */
2369 if ((ppp->flags & SC_CCP_UP) == 0)
2370 break;
2371 if (inbound) {
2372 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2373 ppp->rcomp->decomp_reset(ppp->rc_state);
2374 ppp->rstate &= ~SC_DC_ERROR;
2375 }
2376 } else {
2377 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2378 ppp->xcomp->comp_reset(ppp->xc_state);
2379 }
2380 break;
2381 }
2382}
2383
2384/* Free up compression resources. */
2385static void
2386ppp_ccp_closed(struct ppp *ppp)
2387{
2388 void *xstate, *rstate;
2389 struct compressor *xcomp, *rcomp;
2390
2391 ppp_lock(ppp);
2392 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2393 ppp->xstate = 0;
2394 xcomp = ppp->xcomp;
2395 xstate = ppp->xc_state;
2396 ppp->xc_state = NULL;
2397 ppp->rstate = 0;
2398 rcomp = ppp->rcomp;
2399 rstate = ppp->rc_state;
2400 ppp->rc_state = NULL;
2401 ppp_unlock(ppp);
2402
2403 if (xstate) {
2404 xcomp->comp_free(xstate);
2405 module_put(xcomp->owner);
2406 }
2407 if (rstate) {
2408 rcomp->decomp_free(rstate);
2409 module_put(rcomp->owner);
2410 }
2411}
2412
2413/* List of compressors. */
2414static LIST_HEAD(compressor_list);
2415static DEFINE_SPINLOCK(compressor_list_lock);
2416
2417struct compressor_entry {
2418 struct list_head list;
2419 struct compressor *comp;
2420};
2421
2422static struct compressor_entry *
2423find_comp_entry(int proto)
2424{
2425 struct compressor_entry *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Domen Puncer81616c52005-09-10 00:27:04 -07002427 list_for_each_entry(ce, &compressor_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (ce->comp->compress_proto == proto)
2429 return ce;
2430 }
2431 return NULL;
2432}
2433
2434/* Register a compressor */
2435int
2436ppp_register_compressor(struct compressor *cp)
2437{
2438 struct compressor_entry *ce;
2439 int ret;
2440 spin_lock(&compressor_list_lock);
2441 ret = -EEXIST;
Joe Perchescd228d52007-11-12 18:07:31 -08002442 if (find_comp_entry(cp->compress_proto))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto out;
2444 ret = -ENOMEM;
2445 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
Joe Perchescd228d52007-11-12 18:07:31 -08002446 if (!ce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto out;
2448 ret = 0;
2449 ce->comp = cp;
2450 list_add(&ce->list, &compressor_list);
2451 out:
2452 spin_unlock(&compressor_list_lock);
2453 return ret;
2454}
2455
2456/* Unregister a compressor */
2457void
2458ppp_unregister_compressor(struct compressor *cp)
2459{
2460 struct compressor_entry *ce;
2461
2462 spin_lock(&compressor_list_lock);
2463 ce = find_comp_entry(cp->compress_proto);
Joe Perchescd228d52007-11-12 18:07:31 -08002464 if (ce && ce->comp == cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 list_del(&ce->list);
2466 kfree(ce);
2467 }
2468 spin_unlock(&compressor_list_lock);
2469}
2470
2471/* Find a compressor. */
2472static struct compressor *
2473find_compressor(int type)
2474{
2475 struct compressor_entry *ce;
2476 struct compressor *cp = NULL;
2477
2478 spin_lock(&compressor_list_lock);
2479 ce = find_comp_entry(type);
Joe Perchescd228d52007-11-12 18:07:31 -08002480 if (ce) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 cp = ce->comp;
2482 if (!try_module_get(cp->owner))
2483 cp = NULL;
2484 }
2485 spin_unlock(&compressor_list_lock);
2486 return cp;
2487}
2488
2489/*
2490 * Miscelleneous stuff.
2491 */
2492
2493static void
2494ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2495{
2496 struct slcompress *vj = ppp->vj;
2497
2498 memset(st, 0, sizeof(*st));
Paulius Zaleckas8c0469c2008-04-23 18:54:01 -07002499 st->p.ppp_ipackets = ppp->dev->stats.rx_packets;
2500 st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
2501 st->p.ppp_ibytes = ppp->dev->stats.rx_bytes;
2502 st->p.ppp_opackets = ppp->dev->stats.tx_packets;
2503 st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
2504 st->p.ppp_obytes = ppp->dev->stats.tx_bytes;
Joe Perchescd228d52007-11-12 18:07:31 -08002505 if (!vj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return;
2507 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2508 st->vj.vjs_compressed = vj->sls_o_compressed;
2509 st->vj.vjs_searches = vj->sls_o_searches;
2510 st->vj.vjs_misses = vj->sls_o_misses;
2511 st->vj.vjs_errorin = vj->sls_i_error;
2512 st->vj.vjs_tossed = vj->sls_i_tossed;
2513 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2514 st->vj.vjs_compressedin = vj->sls_i_compressed;
2515}
2516
2517/*
2518 * Stuff for handling the lists of ppp units and channels
2519 * and for initialization.
2520 */
2521
2522/*
2523 * Create a new ppp interface unit. Fails if it can't allocate memory
2524 * or if there is already a unit with the requested number.
2525 * unit == -1 means allocate a new number.
2526 */
2527static struct ppp *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002528ppp_create_interface(struct net *net, int unit, int *retp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 struct ppp *ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002531 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 struct net_device *dev = NULL;
2533 int ret = -ENOMEM;
2534 int i;
2535
Wang Chenc8019bf2008-11-20 04:24:17 -08002536 dev = alloc_netdev(sizeof(struct ppp), "", ppp_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (!dev)
2538 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002540 pn = ppp_pernet(net);
2541
Wang Chenc8019bf2008-11-20 04:24:17 -08002542 ppp = netdev_priv(dev);
2543 ppp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 ppp->mru = PPP_MRU;
2545 init_ppp_file(&ppp->file, INTERFACE);
2546 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
2547 for (i = 0; i < NUM_NP; ++i)
2548 ppp->npmode[i] = NPMODE_PASS;
2549 INIT_LIST_HEAD(&ppp->channels);
2550 spin_lock_init(&ppp->rlock);
2551 spin_lock_init(&ppp->wlock);
2552#ifdef CONFIG_PPP_MULTILINK
2553 ppp->minseq = -1;
2554 skb_queue_head_init(&ppp->mrq);
2555#endif /* CONFIG_PPP_MULTILINK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002557 /*
2558 * drum roll: don't forget to set
2559 * the net device is belong to
2560 */
2561 dev_net_set(dev, net);
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 ret = -EEXIST;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002564 mutex_lock(&pn->all_ppp_mutex);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002565
2566 if (unit < 0) {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002567 unit = unit_get(&pn->units_idr, ppp);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002568 if (unit < 0) {
2569 *retp = unit;
2570 goto out2;
2571 }
2572 } else {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002573 if (unit_find(&pn->units_idr, unit))
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002574 goto out2; /* unit already exists */
Cyrill Gorcunov85997572009-01-12 22:11:56 -08002575 /*
2576 * if caller need a specified unit number
2577 * lets try to satisfy him, otherwise --
2578 * he should better ask us for new unit number
2579 *
2580 * NOTE: yes I know that returning EEXIST it's not
2581 * fair but at least pppd will ask us to allocate
2582 * new unit in this case so user is happy :)
2583 */
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002584 unit = unit_set(&pn->units_idr, ppp, unit);
Cyrill Gorcunov85997572009-01-12 22:11:56 -08002585 if (unit < 0)
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002586 goto out2;
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /* Initialize the new ppp unit */
2590 ppp->file.index = unit;
2591 sprintf(dev->name, "ppp%d", unit);
2592
2593 ret = register_netdev(dev);
2594 if (ret != 0) {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002595 unit_put(&pn->units_idr, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2597 dev->name, ret);
2598 goto out2;
2599 }
2600
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002601 ppp->ppp_net = net;
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 atomic_inc(&ppp_unit_count);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002604 mutex_unlock(&pn->all_ppp_mutex);
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 *retp = 0;
2607 return ppp;
2608
2609out2:
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002610 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 free_netdev(dev);
2612out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 *retp = ret;
2614 return NULL;
2615}
2616
2617/*
2618 * Initialize a ppp_file structure.
2619 */
2620static void
2621init_ppp_file(struct ppp_file *pf, int kind)
2622{
2623 pf->kind = kind;
2624 skb_queue_head_init(&pf->xq);
2625 skb_queue_head_init(&pf->rq);
2626 atomic_set(&pf->refcnt, 1);
2627 init_waitqueue_head(&pf->rwait);
2628}
2629
2630/*
2631 * Take down a ppp interface unit - called when the owning file
2632 * (the one that created the unit) is closed or detached.
2633 */
2634static void ppp_shutdown_interface(struct ppp *ppp)
2635{
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002636 struct ppp_net *pn;
2637
2638 pn = ppp_pernet(ppp->ppp_net);
2639 mutex_lock(&pn->all_ppp_mutex);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 /* This will call dev_close() for us. */
James Chapman739840d2008-12-17 12:02:16 +00002642 ppp_lock(ppp);
2643 if (!ppp->closing) {
2644 ppp->closing = 1;
2645 ppp_unlock(ppp);
2646 unregister_netdev(ppp->dev);
2647 } else
2648 ppp_unlock(ppp);
2649
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002650 unit_put(&pn->units_idr, ppp->file.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 ppp->file.dead = 1;
2652 ppp->owner = NULL;
2653 wake_up_interruptible(&ppp->file.rwait);
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002654
2655 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/*
2659 * Free the memory used by a ppp unit. This is only called once
2660 * there are no channels connected to the unit and no file structs
2661 * that reference the unit.
2662 */
2663static void ppp_destroy_interface(struct ppp *ppp)
2664{
2665 atomic_dec(&ppp_unit_count);
2666
2667 if (!ppp->file.dead || ppp->n_channels) {
2668 /* "can't happen" */
2669 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2670 "n_channels=%d !\n", ppp, ppp->file.dead,
2671 ppp->n_channels);
2672 return;
2673 }
2674
2675 ppp_ccp_closed(ppp);
2676 if (ppp->vj) {
2677 slhc_free(ppp->vj);
2678 ppp->vj = NULL;
2679 }
2680 skb_queue_purge(&ppp->file.xq);
2681 skb_queue_purge(&ppp->file.rq);
2682#ifdef CONFIG_PPP_MULTILINK
2683 skb_queue_purge(&ppp->mrq);
2684#endif /* CONFIG_PPP_MULTILINK */
2685#ifdef CONFIG_PPP_FILTER
Jesper Juhl96edf832005-05-03 14:38:09 -07002686 kfree(ppp->pass_filter);
2687 ppp->pass_filter = NULL;
2688 kfree(ppp->active_filter);
2689 ppp->active_filter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690#endif /* CONFIG_PPP_FILTER */
2691
Wei Yongjun1d2f8c92009-02-25 00:16:08 +00002692 kfree_skb(ppp->xmit_pending);
G. Liakhovetski165de5b2007-03-25 19:04:09 -07002693
James Chapman739840d2008-12-17 12:02:16 +00002694 free_netdev(ppp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
2697/*
2698 * Locate an existing ppp unit.
Arjan van de Ven8ed965d2006-03-23 03:00:21 -08002699 * The caller should have locked the all_ppp_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 */
2701static struct ppp *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002702ppp_find_unit(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002704 return unit_find(&pn->units_idr, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
2707/*
2708 * Locate an existing ppp channel.
2709 * The caller should have locked the all_channels_lock.
2710 * First we look in the new_channels list, then in the
2711 * all_channels list. If found in the new_channels list,
2712 * we move it to the all_channels list. This is for speed
2713 * when we have a lot of channels in use.
2714 */
2715static struct channel *
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002716ppp_find_channel(struct ppp_net *pn, int unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 struct channel *pch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002720 list_for_each_entry(pch, &pn->new_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (pch->file.index == unit) {
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002722 list_move(&pch->list, &pn->all_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return pch;
2724 }
2725 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002726
2727 list_for_each_entry(pch, &pn->all_channels, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 if (pch->file.index == unit)
2729 return pch;
2730 }
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return NULL;
2733}
2734
2735/*
2736 * Connect a PPP channel to a PPP interface unit.
2737 */
2738static int
2739ppp_connect_channel(struct channel *pch, int unit)
2740{
2741 struct ppp *ppp;
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002742 struct ppp_net *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 int ret = -ENXIO;
2744 int hdrlen;
2745
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002746 pn = ppp_pernet(pch->chan_net);
2747
2748 mutex_lock(&pn->all_ppp_mutex);
2749 ppp = ppp_find_unit(pn, unit);
Joe Perchescd228d52007-11-12 18:07:31 -08002750 if (!ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 goto out;
2752 write_lock_bh(&pch->upl);
2753 ret = -EINVAL;
Joe Perchescd228d52007-11-12 18:07:31 -08002754 if (pch->ppp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 goto outl;
2756
2757 ppp_lock(ppp);
2758 if (pch->file.hdrlen > ppp->file.hdrlen)
2759 ppp->file.hdrlen = pch->file.hdrlen;
2760 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
James Chapman739840d2008-12-17 12:02:16 +00002761 if (hdrlen > ppp->dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 ppp->dev->hard_header_len = hdrlen;
2763 list_add_tail(&pch->clist, &ppp->channels);
2764 ++ppp->n_channels;
2765 pch->ppp = ppp;
2766 atomic_inc(&ppp->file.refcnt);
2767 ppp_unlock(ppp);
2768 ret = 0;
2769
2770 outl:
2771 write_unlock_bh(&pch->upl);
2772 out:
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002773 mutex_unlock(&pn->all_ppp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return ret;
2775}
2776
2777/*
2778 * Disconnect a channel from its ppp unit.
2779 */
2780static int
2781ppp_disconnect_channel(struct channel *pch)
2782{
2783 struct ppp *ppp;
2784 int err = -EINVAL;
2785
2786 write_lock_bh(&pch->upl);
2787 ppp = pch->ppp;
2788 pch->ppp = NULL;
2789 write_unlock_bh(&pch->upl);
Joe Perchescd228d52007-11-12 18:07:31 -08002790 if (ppp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /* remove it from the ppp unit's list */
2792 ppp_lock(ppp);
2793 list_del(&pch->clist);
2794 if (--ppp->n_channels == 0)
2795 wake_up_interruptible(&ppp->file.rwait);
2796 ppp_unlock(ppp);
2797 if (atomic_dec_and_test(&ppp->file.refcnt))
2798 ppp_destroy_interface(ppp);
2799 err = 0;
2800 }
2801 return err;
2802}
2803
2804/*
2805 * Free up the resources used by a ppp channel.
2806 */
2807static void ppp_destroy_channel(struct channel *pch)
2808{
2809 atomic_dec(&channel_count);
2810
2811 if (!pch->file.dead) {
2812 /* "can't happen" */
2813 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2814 pch);
2815 return;
2816 }
2817 skb_queue_purge(&pch->file.xq);
2818 skb_queue_purge(&pch->file.rq);
2819 kfree(pch);
2820}
2821
2822static void __exit ppp_cleanup(void)
2823{
2824 /* should never happen */
2825 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2826 printk(KERN_ERR "PPP: removing module but units remain!\n");
Akinobu Mita68fc4fa2007-07-19 01:47:50 -07002827 unregister_chrdev(PPP_MAJOR, "ppp");
Greg Kroah-Hartman9a6a2a52006-09-12 17:00:10 +02002828 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08002829 class_destroy(ppp_class);
Eric W. Biederman741a6fa2009-11-29 15:46:09 +00002830 unregister_pernet_device(&ppp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
2833/*
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002834 * Units handling. Caller must protect concurrent access
2835 * by holding all_ppp_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Cyrill Gorcunov85997572009-01-12 22:11:56 -08002838/* associate pointer with specified number */
2839static int unit_set(struct idr *p, void *ptr, int n)
2840{
2841 int unit, err;
2842
2843again:
2844 if (!idr_pre_get(p, GFP_KERNEL)) {
2845 printk(KERN_ERR "PPP: No free memory for idr\n");
2846 return -ENOMEM;
2847 }
2848
2849 err = idr_get_new_above(p, ptr, n, &unit);
2850 if (err == -EAGAIN)
2851 goto again;
2852
2853 if (unit != n) {
2854 idr_remove(p, unit);
2855 return -EINVAL;
2856 }
2857
2858 return unit;
2859}
2860
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002861/* get new free unit number and associate pointer with it */
2862static int unit_get(struct idr *p, void *ptr)
2863{
2864 int unit, err;
2865
2866again:
Cyrill Gorcunov85997572009-01-12 22:11:56 -08002867 if (!idr_pre_get(p, GFP_KERNEL)) {
2868 printk(KERN_ERR "PPP: No free memory for idr\n");
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002869 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002871
2872 err = idr_get_new_above(p, ptr, 0, &unit);
2873 if (err == -EAGAIN)
2874 goto again;
2875
2876 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002879/* put unit number back to a pool */
2880static void unit_put(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002882 idr_remove(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002885/* get pointer associated with the number */
2886static void *unit_find(struct idr *p, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Cyrill Gorcunov7a95d262008-12-17 00:34:06 -08002888 return idr_find(p, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
2891/* Module/initialization stuff */
2892
2893module_init(ppp_init);
2894module_exit(ppp_cleanup);
2895
Cyrill Gorcunov273ec512009-01-21 15:55:35 -08002896EXPORT_SYMBOL(ppp_register_net_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897EXPORT_SYMBOL(ppp_register_channel);
2898EXPORT_SYMBOL(ppp_unregister_channel);
2899EXPORT_SYMBOL(ppp_channel_index);
2900EXPORT_SYMBOL(ppp_unit_number);
2901EXPORT_SYMBOL(ppp_input);
2902EXPORT_SYMBOL(ppp_input_error);
2903EXPORT_SYMBOL(ppp_output_wakeup);
2904EXPORT_SYMBOL(ppp_register_compressor);
2905EXPORT_SYMBOL(ppp_unregister_compressor);
2906MODULE_LICENSE("GPL");
2907MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
2908MODULE_ALIAS("/dev/ppp");